# Event triggers

|  | Watch | Webhook |
| --- | --- | --- |
| Applies to | `recurring`, `monitor` tasks with `watch` (at most 4,000 characters) | `webhook` tasks: no `cron` or `run_at`, so `_entry_due` never fires them |
| Starts when | A command's output changed | An outside service POSTs to it |
| Event source | `watch` | `webhook` |
| Event text | Command and unified diff, at most 6,000 characters | Body (pretty-printed when JSON), prefixed with `X-GitHub-Event` when present, at most 8,000 characters |
| Trust | Both reuse the task's signed execution spec. Event text is untrusted data. | Both reuse the task's signed execution spec. Event text is untrusted data. |

## Watch

| Check result | Effect |
| --- | --- |
| First check of a command | Baseline; nothing starts |
| A different command | New baseline |
| Unchanged digest | Nothing starts |
| Changed digest | `_spawn` with the event |
| Failure or timeout | Logged; baseline untouched |

| Watch check | Detail |
| --- | --- |
| Frozen | `watch` sits in the signed spec next to the body; editing the task file cannot change what runs |
| Due tick | `_tick` calls `_check_watch` instead of `_spawn`; each minute's claim is taken as for any task, so a tick checks a watch once |
| Run | Spec verified, then a daemon thread runs the command through `_task_invocation` under the conversation's Sandbox switch; 120 s timeout |
| State | `<schedule dir>/watch-state.json`, under the store lock: per task, the SHA-256 of command and output, and the first 20,000 characters of output |

## Webhook

`POST /hooks/{task_id}` is the one HTTP path `OwnerAuthMiddleware` passes without login, only for `POST` of exactly that shape; Host and Origin checks still run. It reads at most 64 KB and calls `hooks.receive` on a worker thread.

| Status | Meaning |
| --- | --- |
| `202` | Run started |
| `404` | Unknown task, another type, or wrong key, indistinguishably |
| `409` | Paused |
| `429` | More than one call per task in 10 s, per process |

Creation stores a random 24-byte URL-safe `hook_key`; the tool result prints the URL (`hooks.url`, from the active Web access snapshot) and the key. It is accepted from `X-OpenProgram-Key` or as the HMAC-SHA256 secret of `X-Hub-Signature-256` (GitHub), compared in constant time.

## Event reaches the run as data

| Body kind | Delivery |
| --- | --- |
| prompt | `event_note` appends the text inside `<event source="…" trust="untrusted">`, with `</event>` escaped, and a line saying it is data. Usual non-interactive scheduler turn (`source="scheduler"`, the conversation's permission mode): a call needing approval is denied. |
| reminder | `reminders.fire` adds `event` to `reminder.fired`. A waking reminder names the source and carries the same note; `_wake` calls `start_owner_turn`, and approvals wait in that conversation. |
| command | Environment: `OPENPROGRAM_EVENT`, `OPENPROGRAM_EVENT_SOURCE` |

The task log records it under `# started by <source>:`.

**Where each piece lives**

| Piece | File |
| --- | --- |
| Task types, `watch`, hook key | `openprogram/runtime/scheduler/service.py` |
| Signed spec | `openprogram/programs/tools/jobs/cron/cron.py` |
| Checks, delivery | `openprogram/programs/tools/jobs/cron/worker.py` |
| Webhook verification, rate limit | `openprogram/runtime/scheduler/hooks.py` |
| Route | `openprogram/interfaces/server/routes/execution/scheduler.py` |
| Login exemption | `openprogram/interfaces/server/owner_auth.py` |
| Wake turns | `openprogram/runtime/conversation/turn_admission/owner_turn.py`, `openprogram/runtime/scheduler/reminders.py` |

## Implementation status

| State | Items |
| --- | --- |
| Implemented | Everything above, and Scheduler page labels |
| Not implemented | A page form for watch and webhook tasks (use the `scheduler` tool or `POST /api/scheduler/tasks` with `watch` or `"type": "webhook"`). Showing the URL and key again after creation (the key is in `GET /api/scheduler/tasks/{id}`). |
