Metadata-Version: 2.5
Name: whenthen
Version: 0.0.5
Summary: When a condition holds, then run a task: one light interface for one-off and recurring commands over the OS scheduler, made to be driven by AI agents.
Project-URL: Homepage, https://github.com/thorwhalen/whenthen
Project-URL: Repository, https://github.com/thorwhalen/whenthen
Project-URL: Documentation, https://thorwhalen.github.io/whenthen
Author: Thor Whalen
License-Expression: MIT
License-File: LICENSE
Keywords: agents,automation,cron,scheduling,triggers
Requires-Python: >=3.10
Requires-Dist: cw<0.2,>=0.1
Provides-Extra: dev
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff>=0.1.0; extra == 'dev'
Provides-Extra: docs
Requires-Dist: sphinx-rtd-theme>=1.0; extra == 'docs'
Requires-Dist: sphinx>=6.0; extra == 'docs'
Description-Content-Type: text/markdown

# whenthen

When a condition holds, then run a task: a light scheduler for commands, made to be driven by AI agents.

```bash
pip install whenthen
whenthen add backup --command "rsync -a /home/me/notes /backup/notes" --times 03:00 --dry-run
```

```text
backup: runs `/usr/bin/rsync -a /home/me/notes /backup/notes` at 03:00 every day (Europe/Paris); next Thu 3 Dec 2026 03:00 CET (in 15h 52m), then Fri 4 Dec 2026 03:00 CET, Sat 5 Dec 2026 03:00 CET.
```

Drop `--dry-run` and the job is stored, and one entry is added to your machine's own scheduler (a crontab line; on macOS a launchd agent). That entry, the clock, runs `whenthen tick` every minute; the tick works out what is due and runs it. Jobs are small JSON records, not cron lines, so things cron cannot do work anyway:

```bash
whenthen add check-build --command "curl -fsS https://example.com/build" --in 10h   # once
whenthen add poll --command "curl -fsS https://example.com/health" --every 90m     # any interval
whenthen add report --script "weekly-report" --times 09:00 --weekdays mon          # a stored script
whenthen status                                                                    # is everything running?
```

A laptop that was asleep runs what it missed, once, when it wakes.

## Made for agents

A person rarely types these commands. An agent reads "update my feeds every weekday at six", emits typed flags, and whenthen checks them and answers with the sentence above, which the agent reads back. The command never parses English, and never stores a cron expression: models get schedules wrong often enough ([research](docs/research/README.md)) that a deterministic check and a plain read-back sit between the sentence and the schedule.

Two things ship for Claude Code and are linked into `~/.claude` by one command:

```bash
whenthen install-skills
```

- the **`whenthen` skill**: English to flags, which tool to use when it is not this one, the dry-run and read-back procedure, what needs a person's yes;
- the **`whenthen-keeper` agent**: sets up several jobs, writes the script when a task is more than one command, audits the schedule, finds out why a job did not run, and returns a short JSON report.

Every command prints JSON.

## What to run

| Form | Flag | Notes |
|---|---|---|
| one program with arguments | `--command "prog arg"` | no shell syntax; the program's full path is resolved and stored when you add the job |
| a stored script | `--script "name arg"` | store it first: `whenthen script-put name --from file` |
| a Python function | `--call pkg.module:function --param k=v` | runs under the interpreter that added the job |
| a named recurring request | `--intent key --param k=v` | from the vocabulary, below |

Secrets do not go in any of these (they would be stored in the job). Name a file of `NAME=value` lines with `--env-file`; it is read when the job runs.

A job runs the script it was added with and no other: the script's hash is stored with the job, and a script changed afterwards is not run until the job is added again with `--replace`. A run started by whenthen cannot add or change jobs, scripts, the vocabulary or the clock.

## When to run it

| Flag | Meaning |
|---|---|
| `--in 10h` | once, a duration from now (`90s`, `20m`, `1h30m`, `2d`, `1w`) |
| `--at 2026-12-03T07:00` or `--at 15:00` | once, at that time (a bare time means the next time it is that time) |
| `--every 90m` | repeatedly, at any interval of a minute or more |
| `--times 07:00,19:00` with `--weekdays mon-fri`, `--days 1,15`, `--months jun-aug` | on a calendar rule |
| `--cron "0 6 * * 1-6"` | a five-field cron expression, converted to the calendar form |
| `--tz Europe/Paris` | the zone for wall-clock times (default: the machine's, stored by name when the job is added) |
| `--on-event build-finished` | no time: the job runs when another program says so (below) |

A rule whenthen cannot honour exactly is refused with the reason, not approximated. On the days clocks change, a daily job runs once and an hourly one runs every hour.

A job added with `--on-event` waits for a program that already knows the moment: a build script, a git hook, a shortcut on a phone. That program calls `whenthen fire <job> --occurrence <id>`, with an id unique to the event; the same id never runs twice.

Policy: `--timeout 30m` stops a long run and everything it started; `--grace 1h` skips an occurrence that would start later than that (default: run it once, however late); `--overlap allow` lets a run start while the previous one is going (default: skip and record it); `--delay 20s` makes each run wait that long before it starts, so that several jobs due in the same minute do not all start in the same second.

## Looking after jobs

```bash
whenthen ls                 # every job: when, next, last run
whenthen show <name>        # the full record and its latest runs
whenthen run <name>         # run it now, exactly as the clock would
whenthen runs <name>        # history
whenthen log <name>         # the end of the latest run's log
whenthen pause <name>       # resume <name>, rm <name>
whenthen clock              # what wakes the tick: status | install | remove (--clock cron | launchd)
```

`whenthen status` reports the things that go wrong silently: a clock that stopped ticking, a job that keeps failing, a job that is overdue, an occurrence skipped as too late or whose run never started, a job record that cannot be read.

A job record and the JSON these commands print hold the machine's name, absolute paths and the stored request. Quote the `echo` line in a bug report, not the raw output.

## The vocabulary

Name a request once, use it by name afterwards:

```bash
whenthen intent-add backup-notes \
  --command "{python} -m mynotes backup {target?}" \
  --phrase "back up my notes" "save my notes"
whenthen add nightly-backup --intent backup-notes --times 02:00
```

`{name}` is a required parameter, an argument that is exactly `{name?}` an optional one, `{python}` the interpreter running whenthen. A package can contribute its own entry as a JSON document: `whenthen intent-add --from entry.json`.

## From Python

```python
import whenthen

result = whenthen.add("poll", command="curl -fsS https://example.com/health", every="90m", dry_run=True)
print(result["echo"])
whenthen.status()
```

The verbs are plain functions (`whenthen.tools.verbs`, each importable from `whenthen`); the command line is generated from them. Everything whenthen raises on purpose is a `whenthen.WhenthenError`.

## How it is built, and where it grows

A job is `(trigger, task, policy)`. One function, `fire(job, occurrence)`, runs a task: it refuses an occurrence it has already seen, applies the overlap rule and the time limit, and writes a run record. The tick asks each job's trigger one question, "is an occurrence due, and under which key?", and knows nothing about times. The places made to be extended:

| Seam | Default | Could be |
|---|---|---|
| `triggers.KINDS`: a kind of trigger and its answer to the tick | `at`, `every`, `calendar`, `event` | a condition polled each minute, recurrence rules |
| `clock=` (an object, or a name in `clocks.CLOCKS`) | `launchd` on macOS (one agent file), `cron` elsewhere (one crontab line) | a systemd user timer |
| `fire <job> --occurrence <key>`, from any program | the tick | a webhook, a file watcher, a phone reporting a named place |
| `root=` | the data directory: `~/.local/share/whenthen/` (or `$WHENTHEN_DATA_DIR`) | another directory |
| `spawn=` on `tick` | a detached process on this machine | a Python program that embeds the tick |

Conditions that whenthen itself watches (a state, a place) are the next phase: each is one more entry in `KINDS`.

The reasoning is in [the decision record](docs/adr/0001-home-language-seams-and-v1.md); the evidence in [the research](docs/research/README.md).

## Limits

- Times are honoured to the minute, and only while the machine is awake.
- Jobs run on the machine where they were added.
- Two data roots on one machine each get their own clock entry.
- The guards (a run cannot reschedule; a job is bound to its script) protect against accidents and careless agents. They are not a security boundary: anything running as the same user can edit the records.
- The clock is launchd on macOS and cron on Linux. The cron clock also works on macOS (`whenthen clock install --clock cron`), but a job it starts there cannot read the login Keychain, so `gh` and other tools that keep a token in it fail; that is why it is not the default. Installing one clock removes the other.
- Nothing reports a failure unasked: ask `whenthen status`.
- Requires Python 3.10 or later. One dependency: [`cw`](https://pypi.org/project/cw/), which generates the command line.
