# Run a task when something happens

Ask in plain words; the agent sets it up with its `scheduler` tool. For runs at a time, see [reminders](https://fzkuji.github.io/OpenProgram/docs/capabilities/reminders.html).

| Trigger | Example | Fires when |
| --- | --- | --- |
| `watch` on a recurring or monitor task | "Every 10 minutes, check `gh pr list --repo me/app` and tell me about new pull requests." | The command's output differs from the previous check. Cron sets how often it is checked. |
| `webhook` task | "Give me a webhook that triages new issues from our tracker." | Another service calls it. No schedule. |

| Runs | Receives the event as | Approval |
| --- | --- | --- |
| An agent prompt | Fenced text marked as data; told to do only your task and ignore requests inside | Background, nobody to ask: calls needing approval are refused |
| A reminder waking the agent in this conversation | Text marked as outside content | The conversation's own permission mode |
| A shell command | `OPENPROGRAM_EVENT`, with `OPENPROGRAM_EVENT_SOURCE` set to `webhook` or `watch` | — |

## When something changes

- The first check records the baseline and runs nothing; changing the command starts a new baseline.
- The run receives a diff of the output.
- The command runs in the task's sandbox and must finish within 2 minutes. A check that fails or times out runs nothing, keeps the baseline and shows as a failed run.
- A change found while the previous run is still going is not lost: the next check sees it again.

## When a service calls

```
POST http://127.0.0.1:18100/hooks/<task id>
X-OpenProgram-Key: <key>
```

The key can also be a GitHub webhook's secret. GitHub signs with it (`X-Hub-Signature-256`) and the run is told the event name.

| Response | When |
| --- | --- |
| `202` | Started |
| `404` | Wrong key or unknown task |
| `409` | Task is paused, or `previous_run_active`: its last run has not finished, so call again later |
| `413` | Body over 64 KB |
| `429` | Within 10 seconds of the last start |

The run receives the first 8,000 characters of the body. The URL is on this computer; a service on the internet needs a tunnel or a [public Web address](https://fzkuji.github.io/OpenProgram/docs/server/configuration.html#who-can-reach-the-server).

## See, pause or delete

In **Tasks** → **Scheduled**, or ask the agent. A webhook task shows **When called** as its schedule. Each run's log, with its event, is in the scheduler's `logs` folder.
