Skip to content

The agent that grows with you

docs(cron): document the delivery_queued lifecycle and its write-back; add upgrade notes

Tekniumee7670c8b4b2
diff --git a/website/docs/developer-guide/cron-internals.md b/website/docs/developer-guide/cron-internals.md
index d870cb2fa99..70106b13f40 100644
--- a/website/docs/developer-guide/cron-internals.md
+++ b/website/docs/developer-guide/cron-internals.md
@@ -65,18 +65,48 @@ Jobs are stored in `~/.hermes/cron/jobs.json` with atomic write semantics (write
### `last_status` literals
-`last_status` is a closed set written only by `cron.jobs.mark_job_run`. Every
-renderer (`hermes cron list`/`doctor`, the `cronjob_manage` tool, the web dashboard
-badge, the Desktop routine inspector) maps each literal explicitly — a consumer
-must never test `== "ok"` for "the user got their result":
+`last_status` is written by `cron.jobs.mark_job_run` when a run finishes and, for a
+queued delivery, rewritten once by the delivery write-back below. Every renderer
+(`hermes cron list`/`doctor`, the `cronjob_manage` tool, the web dashboard badge, the
+Desktop routine inspector) maps each literal explicitly — a consumer must never test
+`== "ok"` for "the user got their result":
| Literal | Meaning | Detail field |
|---------|---------|--------------|
| `ok` | Agent run succeeded and (if targeted) delivery was confirmed | — |
+| `delivery_queued` | Agent run succeeded; its send was handed to the durable delivery queue and has not settled yet (transitional) | `last_delivery_queued` |
| `error` | Agent run failed | `last_error` |
| `delivery_failed` | Agent run succeeded, but the output never reached its target | `last_delivery_error` (`last_error` is `null`) |
| `blocked_config` | Pre-dispatch validation refused to burn a run | `last_error` |
+### Queued delivery lifecycle and write-back
+
+An agent run with a delivery target and an `execution_id` does not send inline: it
+hands the content to `cron/delivery_queue.py` (the profile's `cron/deliveries.db`,
+keyed by `execution_id`) and books `last_status = "delivery_queued"` with ledger
+`delivery_outcome = "queued"`. A gateway's housekeeping tick drains the queue through
+its live adapters (`scheduler.drain_delivery_queue`); each row is claimed at most once
+(`pending → delivering → delivered | failed | suppressed`). A row whose claiming gateway
+died mid-send is fenced `unknown` and never retried.
+
+`cron/delivery_outcome.py::settle_queued_delivery` then writes the terminal outcome
+back onto the run that queued it:
+
+| Queue row | Ledger `delivery_outcome` | Job `last_status` / `last_delivery_error` |
+|---|---|---|
+| `delivered` | `delivered` | `ok` / `null` |
+| `suppressed` | `suppressed` | `ok` / `null` |
+| `failed` | `failed` | `delivery_failed` / the send error |
+| `unknown` | `failed` | `delivery_failed` / "send outcome is unknown and was not retried" |
+
+Both sides call it: the drain right after it terminalizes a row, and the run right after
+its own bookkeeping, because a drain can finish before the run's `mark_job_run` lands.
+Every write is fenced on the `execution_id` (it must still be the job's latest ledger
+attempt and latest recorded completion), so the call is idempotent and never rewrites a
+later run's status; the ledger only ever moves `queued` to a terminal outcome. Until a
+gateway drains the row, `last_status` stays `delivery_queued` and `hermes cron list`
+shows "finished; delivery is still in progress".
+
### Job Lifecycle States
| State | Meaning |
@@ -220,6 +250,11 @@ dispatching instead of yielding every tick to nobody.
In CLI mode, cron jobs only fire when `hermes cron` commands are run or during active CLI sessions.
+Upgrade notes for the gateway-owned runtime (the retired
+`cron.bot_chat_delivery_timeout_seconds` key, legacy `cron/bot_chat_pending/` records,
+and runs interrupted by the cutover restart) are in
+[Updating](../getting-started/updating.md#upgrading-to-the-gateway-owned-session-runtime).
+
### Managed cron (Chronos) for scale-to-zero
Hosted gateways can run the **Chronos** provider (`cron.provider: chronos`)
@@ -363,7 +398,7 @@ Platforms in the first group have explicit, validated target syntax — named ch
For **Telegram topics**, use `telegram:<chat_id>:<thread_id>` (e.g., `telegram:-1001234567890:17585`). For **Slack threads**, the third segment is the parent message's `thread_ts` (e.g., `slack:C0123ABCD45:1700000000.000100`), so it only applies when replying under an existing message.
-**Bot Chat** (`bot-chat`, `bot-chat:<profile>`) is a machine-local pseudo-platform, not a gateway adapter. A mailbox-capable canonical live owner receives durable admission immediately (idle or busy); only that owner executes the incoming turn. `scheduler_delivery._deliver_to_bot_chat` resolves the target with `get_profile_dir` or the job's current `get_hermes_home`, derives the receipt ID from the source home, job ID, durable `execution_id`, and target home, and checks the receipt before discovering an owner. A profile with no Bot Chat yet gets one created by the authority on first delivery (the `--create-if-missing` semantics of the retired CLI lane). There is no local CLI fallback: without a running authority the run is recorded `delivery_failed` with the reason in `last_delivery_error` and the output kept under `hermes cron runs`; it is not redelivered automatically, and the scheduler never becomes a second writer to the profile's store. Delivery is a real inbound turn, not a transcript mirror. Queued/claimed receipts populate `last_delivery_queued` with receipt IDs. The delivery aggregator excludes admission notices from genuine errors and records execution `delivery_outcome=queued`; successful jobs use `last_status=delivery_queued`. Genuine errors on mixed targets take precedence as failed while retaining queued receipt metadata. The target profile’s durable receipt is authoritative for terminal completion. Queued is the historical admission outcome, not proof of delivery. Historical cron status does not automatically track later receipt completion. Bot-chat targets are excluded from `all` and credential preflight. Bot-chat-only external workers bypass the gateway delivery queue; mixed external-worker targets retain gateway handoff.
+**Bot Chat** (`bot-chat`, `bot-chat:<profile>`) is a machine-local pseudo-platform, not a gateway adapter. A mailbox-capable canonical live owner receives durable admission immediately (idle or busy); only that owner executes the incoming turn. `scheduler_delivery._deliver_to_bot_chat` resolves the target with `get_profile_dir` or the job's current `get_hermes_home`, derives the receipt ID from the source home, job ID, durable `execution_id`, and target home, and checks the receipt before discovering an owner. A profile with no Bot Chat yet gets one created by the authority on first delivery (the `--create-if-missing` semantics of the retired CLI lane). There is no local CLI fallback: without a running authority the run is recorded `delivery_failed` with the reason in `last_delivery_error` and the output kept under `hermes cron runs`; it is not redelivered automatically, and the scheduler never becomes a second writer to the profile's store. Delivery is a real inbound turn, not a transcript mirror. Queued/claimed receipts populate `last_delivery_queued` with receipt IDs. The delivery aggregator excludes admission notices from genuine errors and records execution `delivery_outcome=queued`; successful jobs use `last_status=delivery_queued`. Genuine errors on mixed targets take precedence as failed while retaining queued receipt metadata. The target profile’s durable receipt is authoritative for terminal completion. Queued is the historical admission outcome, not proof of delivery. When the Bot Chat send went through the gateway delivery queue (an agent run), the write-back above settles the cron run once the drain's send returns: `ok`/`delivered` then means the target's authority **accepted** the turn, and `last_delivery_queued` keeps the receipt IDs whose Bot Chat turn may still be running. A Bot Chat delivery made inline (a `no_agent` job, or a Bot-Chat-only external worker) is not on the queue, so its run keeps `last_status=delivery_queued` / `delivery_outcome=queued`; nothing writes the later receipt completion back into cron history. Bot-chat targets are excluded from `all` and credential preflight. Bot-chat-only external workers bypass the gateway delivery queue; mixed external-worker targets retain gateway handoff.
### Response Wrapping
diff --git a/website/docs/getting-started/updating.md b/website/docs/getting-started/updating.md
index c6508ab8bee..897fb33902d 100644
--- a/website/docs/getting-started/updating.md
+++ b/website/docs/getting-started/updating.md
@@ -115,6 +115,32 @@ On Linux and macOS, `hermes update` stops every gateway running from this instal
Windows already paused its gateways before the update (they lock files the dependency sync replaces) and restarts them once the update completes, after the builds.
+### Upgrading to the gateway-owned session runtime
+
+From this release one gateway per host owns every local session (CLI, TUI, Desktop,
+API, ACP, cron, Bot Chat). What an existing install sees on its first update:
+
+- **`cron.bot_chat_delivery_timeout_seconds` is retired.** Bot Chat deliveries are
+ admitted to the target profile's running gateway and tracked by receipt; there is no
+ cron-side turn left to time out. Config migration v51 removes the key with a one-time
+ note (also from an unversioned `config.yaml`). Without a running gateway the run is
+ recorded `delivery_failed`; it is not redelivered automatically.
+- **Legacy Bot Chat pending records** (`cron/bot_chat_pending/*.json`, left by the
+ retired `hermes chat` delivery lane) are handed to the target's live owner on later
+ scheduler ticks under their own IDs. A `claimed` record was an uncertain CLI turn and
+ is never resent. Records are kept with their final status as evidence.
+- **Work interrupted by the cutover restart is never replayed.** On the first gateway
+ start, an input that was mid-turn (`started`) and any managed worker that was running
+ become `unknown`; queued inputs behind it wait. Inspect the session, then acknowledge
+ with `/discard <admission_id>` in `hermes chat` (or the Desktop/TUI equivalent) before
+ later inputs run. A cron delivery a gateway had claimed but not finished is likewise
+ fenced `unknown` and recorded `delivery_failed`.
+- **Rolling back after the new gateway has written `state.db`:** the new runtime-ledger
+ tables reference `sessions` with `ON DELETE RESTRICT`, so an older release that deletes
+ or prunes such a session fails with `FOREIGN KEY constraint failed`. To go back, restore
+ the pre-update snapshot (`hermes update --backup` takes a full one) rather than running
+ the older release on the upgraded database.
+
### Why the gateway restart can take a while
The restart is drain-first: the running gateway refuses new turns, then waits for in-flight work (chat turns, cron jobs, API runs) to finish before exiting, capped by `agent.restart_after_turn_timeout` (30 minutes by default) so a long-running job is never cut off mid-run. While that wait is in progress the updater prints, every 30 seconds, what the gateway is still holding for — for example:
diff --git a/website/docs/user-guide/features/cron.md b/website/docs/user-guide/features/cron.md
index edae93ac301..6e315619f22 100644
--- a/website/docs/user-guide/features/cron.md
+++ b/website/docs/user-guide/features/cron.md
@@ -641,7 +641,7 @@ error. A delivery failure does not count toward the job's `failure_streak`
- Each delivery costs the target bot one full agent turn — mind the schedule frequency.
- Composes with other targets (`bot-chat,telegram`) but is never included in `all`.
- If the canonical chat is open in a mailbox-capable Desktop/TUI backend, delivery is **durably queued immediately**, whether the bot is idle or busy. Only that live owner runs the incoming turn; cron does not start a competing CLI writer. A profile that has no Bot Chat yet gets one created on first delivery. If the target profile's gateway is not running, the run is recorded `delivery_failed` with the reason (the output stays in `hermes cron runs`; it is not redelivered automatically) and cron never starts its own Bot Chat turn.
-- **Queued is not completed.** Cron records receipt IDs and `queued`/`claimed` statuses in `last_delivery_queued`, with delivery outcome `queued` (neither delivered nor failed). A successful job shows `delivery_queued`; genuine errors on other targets still take precedence as delivery failures. The bot may complete later. The durable receipt in the target profile's `runtime/bot_live_delivery/<receipt-id>.json` is authoritative; cron's historical status is not automatically refreshed.
+- **Queued is not completed.** Cron records receipt IDs and `queued`/`claimed` statuses in `last_delivery_queued`, with delivery outcome `queued` (neither delivered nor failed). A successful job shows `delivery_queued`; genuine errors on other targets still take precedence as delivery failures. The bot may complete later. The durable receipt in the target profile's `runtime/bot_live_delivery/<receipt-id>.json` is authoritative for the turn itself. For an agent job the send rides the gateway delivery queue, and once the gateway has handed it to the bot the run settles to `ok` (accepted, not answered) while `last_delivery_queued` keeps the receipt; a refused hand-off settles to `delivery_failed`. Script-only (`no_agent`) Bot Chat deliveries stay `delivery_queued`: cron never rewrites that history when the bot's turn later completes.
- Rechecking the same execution inspects its existing receipt, even if the owner has disappeared. It never falls back to another writer after acceptance. `failed`, `cancelled`, or `ambiguous` receipts are not automatically replayed; inspect the chat and receipt before intentionally starting new work. Each new cron execution has a distinct delivery ID.
### Routing intent (`all`)