# Triggers

> Index of all pages: https://anywhere.so/llms.txt

A worker wakes on any `[[trigger]]` defined in `anywhere.toml` — either a cron schedule or an inbound HTTP webhook — as well as manual `anywhere run` commands and run replays. A worker can define between 1 and 16 `[[trigger]]` blocks. Different triggers on the same worker can overlap (up to 16 concurrent runs per worker), while each individual trigger enforces safe single-file ordering.

## Cron triggers

Add a `[[trigger]]` with a `cron` expression to `anywhere.toml`:

```toml
[[trigger]]
name = "morning-triage"
cron = "0 9 * * 1-5"
timezone = "America/New_York"
run = "bash run.sh"
timeout = 1800
```

- **Any-minute resolution:** Standard 5-field cron supporting numbers, `*`, ranges (`1-5`), steps (`*/15`), lists (`1,15`), and names (`MON`, `JAN`). There is no 1-hour minimum interval.
- **Daylight saving time:** On spring-forward gaps, a fixed time inside the skipped hour runs once immediately after the gap. On fall-back repeats, it runs once.
- **Spread starts:** A cron run becomes eligible to start at its scheduled time plus its trigger's offset, a fixed 0 to 20 seconds that stays the same for that trigger every time, so workers scheduled on the same minute don't all start together. Webhook runs, `anywhere run` and replays become eligible as soon as they're made. When a run really starts varies with load, such as waking its worker and memory on its host; no start time is promised. Each cron run gets its scheduled time in `ANYWHERE_RUN_SCHEDULED_AT` (RFC 3339 UTC), so a run that starts late can still work on the period it was meant for.
- **No overlapping runs:** If a cron trigger's previous run is still active when the next tick arrives, the new tick fails with `trigger_still_running` rather than piling up duplicate work.
- **Skipped, never silently dropped:** A cron run that hasn't started by its trigger's next scheduled time, for example because it's still waiting for memory on a full host, ends `not_started` with `run_skipped`, and the next run takes its place. Two runs of one trigger never wait in line together.
- **No missed-tick storms:** After an outage, the newest scheduled time that has come gets its run, late, and one `not_started` record with `run_missed` counts the times before it.
- **Paused workers:** A paused worker skips scheduled ticks cleanly and resumes on `anywhere workers resume NAME`.

## Webhook triggers

Add a `[[trigger]]` with `webhook = true` to `anywhere.toml` and deploy:

```toml
[[trigger]]
name = "on-issue"
webhook = true
dedupe_header = "x-github-delivery"
run = "node dist/on-issue.js"
timeout = 600
```

Inspect triggers and generate a webhook trigger's secret URL (printed once):

```sh
anywhere triggers --worker issue-responder
anywhere triggers url on-issue --worker issue-responder
```

Calling `anywhere triggers url` again rotates the URL and immediately invalidates the previous one (`404 webhook_not_found`). Only the SHA-256 hash of the token is stored by the platform, and request logs redact the token path.

### Request handling and payloads

Send any HTTP `POST` to the trigger's URL (`POST /v1/webhooks/<token>`):

- **Durably saved before 202:** The control plane persists the run, headers, and body to Postgres before answering `202 Accepted` with `{"run": "<id>", "duplicate": false}`.
- **Body on stdin:** Up to **1 MiB**, piped byte for byte to your command's standard input. Payloads over 1 MiB return `413 input_too_large`.
- **Headers in env:** Request headers (up to 16 KiB encoded as JSON with lower-case header names) are passed in `ANYWHERE_WEBHOOK_HEADERS`. Larger header sets return `431 webhook_headers_too_large`.
- **Verify signatures in your code:** The platform does not hardcode provider signature schemes. Verify GitHub (`x-hub-signature-256`), Stripe (`stripe-signature`), or Linear HMAC headers directly in your command against your stored webhook secret.

### Native deduplication (`dedupe_header`)

Webhook senders routinely retry deliveries on slow responses or transient network drops. Set `dedupe_header` on a webhook `[[trigger]]` to a delivery ID header name such as `X-GitHub-Delivery` or `svix-id` (matched case-insensitively):

- If a request arrives without that header, the endpoint returns `400 dedupe_header_missing`.
- If a request carries a delivery ID already accepted for that trigger, anywhere returns `202 Accepted` with `{"run": "<original-run-id>", "duplicate": true}` without queueing a second run.
- A delivery ID is remembered as long as its run's input is kept: a worker keeps the inputs of its newest 50 ended runs that have one, so a repeat that arrives after 50 newer runs starts a new run.

### Serial webhook queueing

Each webhook trigger executes **one run at a time, in arrival order**, so agents never stomp on each other's files during a burst:

- If a webhook arrives while the trigger already has a run active, it is saved as `queued`, still answered `202` with its run ID, and starts automatically as soon as the run ahead of it finishes.
- Up to **100 queued runs** wait per webhook trigger. Past 100, the endpoint returns `429 webhook_queue_full` (retryable) without saving a run.
- Each queued run's `timeout` clock begins only when it leaves the queue and starts executing, not while waiting.
- Running `anywhere sleep WORKER`, pausing, deleting, or deploying an `anywhere.toml` that removes the trigger cancels its queued runs.

## Manual runs, local testing & replays

Trigger any cron or webhook trigger on demand with `anywhere run`:

```sh
anywhere run morning-triage --worker triage
```

Test a webhook trigger without waiting for an external sender by piping a sample payload into `stdin`:

```sh
anywhere run on-issue --worker issue-responder < sample-issue.json
```

Pass `< /dev/null` when running a webhook trigger with an empty body.

To re-run a past webhook or manual run using its kept input body and headers — for example after fixing a bug and redeploying — use `anywhere runs replay` (or press **Replay** on the run's page in the portal):

```sh
anywhere runs replay RUN_ID --worker issue-responder
```

Replays always use the trigger's current `run` command and `timeout`, and bypass `dedupe_header` so you can re-verify the same delivery safely.

## Automatic sleep

Workers sleep automatically as soon as their last active run ends. Sleep stops all processes inside the sandbox while keeping the persistent disk intact (`/app`, `/app/data/`, `/tmp`, and installed packages). While asleep, CPU and memory billing drops to `$0`; only persistent disk is billed.

To end active and queued runs as `cancelled` and sleep a worker immediately:

```sh
anywhere sleep triage
```
