Skip to content
Delegate

ArchitectureADR 0007

Architecture · Decision record 0007

ADR 0007: Durable runs from the event log, without a job service

On this page
  • Status: Accepted
  • Date: 2026-11-24
  • Amends: ADR 0005 (consequence "true resume comes with M3"); SPEC §5 ("runs are durable jobs (Inngest)")

Context#

M3 requires that killing the server mid-run still completes the run. The spec planned Inngest for this. Inngest's free tier needs an account and a signing key, and the hosted path routes run state through a third party. ADR 0003 makes $0 and keyless binding: nothing on the default path may need an account. The event log (run_events) is already the single source of truth for the UI, traces and replay (ADR 0005), and every event is written before it is published.

Decision#

  1. Checkpoints are events. When a step succeeds, the orchestrator writes step.completed with an optional checkpoint field holding the step's full SubAgentResult (summary, findings, sources, confidence, artifacts). The field is optional and additive, so old logs and consumers are unaffected. Everything a dependent step needs is either in a checkpoint or in an artifact's content, which is also in the log (artifact.created / artifact.updated).
  2. resumeMission({ runId, events, ...runMission deps }) folds the log (analyseForResume):
    • terminal runs return their status and emit nothing;
    • no plan yet: the mission starts normally;
    • plan proposed but not approved: the approval hook is asked again, no new plan.proposed;
    • plan approved: completed steps are restored from their checkpoints and never re-run; steps that failed stay failed; steps that started but did not complete (in flight at the crash) run again from scratch.
  3. In-flight steps are idempotent by construction. Tool calls are reads. The crawler's fetch cache is the documents table itself, keyed by (workspace_id, collection, source_url): a re-run crawl reuses stored pages (and their stored links, for breadth-first discovery) instead of fetching them again. A re-run step that publishes an artifact it already created updates that artifact (artifact.updated, next version) instead of creating a duplicate.
  4. Missions declare restorability. A mission is restorable when every executor gets its state only from inputs and artifacts. Build-an-Agent is written that way. Company X-ray and the Automation Audit still keep per-run state in closures (the research kit's source store), so on resume they re-run every step of the approved plan; this is correct but not cheap, and is listed as a known gap.
  5. Lease, so one worker resumes a run. runs.lease_owner and runs.lease_expires_at (migration 0004) hold a time-bound lease. createDbRunLease claims it with one conditional UPDATE ... WHERE lease_owner IS NULL OR lease_owner = $me OR lease_expires_at < now(), so the database decides atomically. resumeMission({ lease }) acquires it (or returns status: "busy"), renews it every ttl/3, and releases it at the end. A worker that loses its lease aborts and its later events are dropped, so a stale worker cannot write a terminal event over the new owner's run. A column, not a new event type, because the lease is coordination state with a TTL rather than history, and a new RunEvent type would change the database's event-type check and every exhaustive consumer.
  6. Who calls resume. Whatever process notices a non-terminal run with an expired lease: the web app's stale-run sweeper (replacing ADR 0005's "mark failed after timeout"), a cron, or a CLI. No job service is required.

Why not Inngest (now)#

  • It needs an account and keys; ADR 0003 forbids that on the default path, and self-hosters would need to run the Inngest dev server or its self-hosted stack.
  • Our state already lives in an append-only log with checkpoints; Inngest's step memoisation would duplicate it in a second store we do not control.
  • Inngest remains a drop-in later: each plan step maps to one step.run(stepId, ...) whose memoised value is the checkpoint, and resumeMission is what an Inngest function would call on retry. Adding it is a deployment choice, not a runtime change.

Consequences#

  • A crash costs at most the in-flight steps. Measured on plausible.io (owner chain, 2026-11-25): a run killed after design (107 s, 204 events) resumed in 40 s, skipped crawl, index and design, re-ran only evalset, evaluate and gaps, fetched no page twice and completed.
  • step.completed payloads grow (a few KB to tens of KB per step). Acceptable at demo scale; large step outputs belong in artifacts.
  • Budgets restart on resume: tokens spent before the crash are in the log totals but not charged against the resumed run's budget. Acceptable for $0 providers; to fix when paid BYOK runs need hard caps across restarts.
  • Agent ids restart from _1 on resume, so lanes in the UI may show the same agent id twice for a re-run step.
  • Amendment (2026-11-25): Custom goal and Repo X-ray are restorable. Custom goal writes a custom-goal.checkpoint artifact per research step (the resolved company, crawl discoveries and the source documents the step used, under their original source ids), and the critic rebuilds its state from the drafted brief. Repo X-ray writes a repo-xray.state artifact per step (pinned commit, documentation facts, architect and improver drafts); its snapshot (up to 24 MB of text) is too large for the log, so a resumed step re-downloads the archive at the pinned commit under the fetch step's approved capability and re-runs the deterministic analysis, which yields the same evidence ids. Both missions also have a hard wall-clock limit (230 s and 250 s from the start of the invocation) after which remaining research or model steps are skipped and a deliverable marked partial is published.
  • Amendment (2026-11-27, live demo robustness): four gaps made long or approval-gated runs fail on Vercel with "worker lost" although no server restarted.
    1. Work starts at once. Runs were scheduled with after(callback), which Next.js runs only when the response closes. From the run page's event stream (open for up to 300 s) a resume or an Autopilot child launch waited until the stream ended, while its lease expired and every read wrote another resume marker until the cap failed the run. createScheduler now starts the task immediately and passes its promise to after(), which only keeps the function alive; work scheduled from scheduled work joins the same invocation.
    2. Plan approval holds no worker. A run waiting for a plan decision polls for 10 s, then parks: it stops writing, releases its lease and ends the invocation. The log (a plan proposed and not approved) and the pending approval row say it is waiting, so it is never resumed or swept as lost. Recording a decision wakes the run, which continues from the log without a resume marker; the approval row is reused by plan version, and the 10-minute timeout is measured from the row and recorded by whichever read notices it.
    3. Planned hand-offs. Each invocation knows when it started and its limit (300 s on Vercel, DELEGATE_FUNCTION_MAX_MS, none elsewhere). 25 s before the limit a run stops writing, appends a durable-yield policy.decided marker and releases its lease; work is not started with less than 90 s left. The next read continues a handed-off run at once, with no quiet period, no resume marker and no resume cap; at most 12 hand-offs per run. Steps in flight at the hand-off re-run, as after a crash. The run page's event stream rotates every 60 s (stream.rotate), so work it triggers gets most of a fresh invocation. A run nobody reads still waits for its next read.
    4. One failed lease renewal is tolerated. A transient database error on renewal no longer aborts the worker. Writes are fenced by the time the last successful renewal was issued: once a full TTL has passed without one, the worker stops writing and aborts, before another worker can take the expired lease. Also: Build-it's pages and chunks were kept in a per-process memory store, so a resumed step or the assistant's chat on another instance found nothing ("No sources indexed"). Missions and owner chat now use the workspace's documents and chunks tables (ADR 0008's store), so the crawl cache in point 3 of the decision works across instances. The index is also mirrored into process memory as before, so the public embed keeps answering anonymous visitors on the instance that built the assistant; on any other instance it still finds nothing, because reading a workspace's chunks without its session needs a definer function, which is a migration. Ship-a-PR runs do not hand off, so a push is never cut between the GitHub write and its checkpoint. A continued run fails after two hand-offs in a row without a completed step, and never runs more than 12. Runs keep a fresh lease owner per lease object, so two resumes in one process cannot both hold a run.