---
url: /another-agentic-system/decisions/0001-rust-state-machine-on-postgres.md
---

# ADR 0001 — Orchestrator: a Rust state machine on Postgres

* **Status:** accepted (2026-09-28). *Amended (2026-11-19) by
  [ADR 0076](0076-the-orchestrator-as-a-library-in-an-application.md):* the orchestrator embedded in an application
  keeps a thread's log in SQLite, written by that one process, with the same version check and an in-process wakeup;
  a thread's home (a deployment or one application) is fixed. Amended (2026-09-29) by
  [ADR 0015](0015-control-plane-and-workers-on-adam-rs.md): the job ledger also holds the journal of
  an in-process (local) agent, in `orch_agent_*` tables of the same Postgres. Processes stay
  stateless; the orchestrator now runs as a control plane and workers over this one database.
  Status note (2026-09-30): [ADR 0016](0016-inbox-timers-and-job-ledger-on-the-thread.md) makes the
  "transactional inbox" and timers of this decision concrete (planned, not built): the job ledger is
  a `threads.job` column committed with the state, the inbox carries webhooks and timers only, and MCP
  bypasses it ([ADR 0019](0019-mcp-server-over-streamable-http.md)). The decision stands.
  Status note (2026-10-02): [ADR 0032](0032-files-from-agents-live-in-an-artifact-store.md) amends the wording of "only the
  job ledger and the event log persist": the files agents hand over are durable outside Postgres, in an artifact store
  behind a port (a directory or an S3 bucket), by the hash of their content, like git in
  [ADR 0003](0003-git-as-durable-state-ephemeral-workers.md); the log keeps only the reference and stays the only state the
  orchestrator reasons on. Processes stay stateless. The decision stands.
  Status note (2026-10-03): [ADR 0042](0042-the-thread-list-is-the-owners.md) (proposed) adds that the thread row also keeps
  its owner's organisation of the list (pin, archive, order, nesting under a parent), which no event records and which is not
  copied into forks, exports or shared views. A thread table rebuilt from the log alone loses it and falls back to the
  default order. The log stays the only state the orchestrator reasons on. The decision stands.
  Status note (2026-10-11): [ADR 0046](0046-people-add-their-own-mcp-servers-secrets-in-a-credential-broker.md) (accepted) adds that Postgres also keeps a person's
  definitions of their own MCP servers and, **sealed** (AES-256-GCM, by the default adapter of the `SecretStore` port), their credentials. A sealed secret is not the
  log, is read only to lease a credential and is never reasoned on; a deployment may keep them in another store by choosing another adapter at build time.
  A database backup holds envelopes only. The decision stands.
  Status note (2026-10-27): [ADR 0065](0065-a-usage-ledger-per-person.md) (proposed) adds that Postgres also keeps the **usage ledger** of the people: counts per task
  and UTC month (the tokens by provider and model and the sandbox time of `usage/v1`), written in the transaction that appends the `model_usage` or
  `model_usage_total` event that carries them, and kept, with the thread id cleared, when the thread is erased. It is a function of the log
  (the same events, folded by a pure function of `orch-core`) that outlives the thread; no content is in it. The decision stands.
  Status note (2026-11-15): [ADR 0068](0068-a-person-searches-their-own-threads.md) (proposed) adds that Postgres also keeps the **search index** of a person's threads:
  the folded words (keys) of their titles, descriptions and messages, written in the transaction that appends the event, erased in the transaction that erases the
  thread and rebuilt from the log by `orchestrator search backfill`. It is a function of the log and holds content, so it is the owner's alone and never read
  across people. The decision stands.
  Status note (2026-11-15): [ADR 0070](0070-a-persons-prompt-library.md) (proposed) adds that Postgres also keeps a person's **prompt library**: their saved prompts (a name, a title, a body with `{{variables}}`, the variables as questions of the UI catalog) and the roles each is shared with, in `user_prompts`. It is not the log and no event records it: it is read and changed only through the API, never folded by the core, never copied into a thread, a fork or an export, and nothing of it is erased with a thread; it is the person's and goes with their account. The decision stands.
  Status note (2026-11-19): [ADR 0077](0077-push-notifications-for-a-persons-runs.md) (accepted) adds that Postgres also keeps, for push notifications, a person's **devices** (their push addresses, sealed through `SecretStore`), their **settings** and each thread's, the **presence leases** of open connect streams, and the **notification intents** with their `notify` outbox rows. Intents are a function of the log, made by the pure core and written in the transaction of the event; the rest is the person's own, read and changed only through the API. A thread's intents, setting and presence are erased with it; nothing of it is copied into a fork or an export. The decision stands.

## Context

Jobs are long-running (minutes to hours), wait on agents, CI and humans, and
must survive restarts. Candidates: eve (TypeScript durable agent framework on
the AI SDK), Restate (durable-execution engine written in Rust, Rust SDK), or a
state machine of our own.

## Decision

A Rust service implementing an explicit job state machine, persisted in the
Postgres (CNPG) we already run: transactional inbox/outbox, `SELECT … FOR
UPDATE SKIP LOCKED` for claiming, `LISTEN/NOTIFY` for wake-ups and live UI
updates.

## Why

* **Correctness over speed.** The orchestrator mostly waits on LLM calls and
  CI; its own overhead is irrelevant. What matters is that impossible
  transitions don't compile (exhaustive `match` over state and event enums)
  and that a crash anywhere resumes from the last committed state.
* **One datastore.** Postgres is already the chat store; making it the queue
  and the bus too avoids operating a broker or a second stateful engine.
* **Same stack** as our other Rust services (tooling, CI, conventions).

## Alternatives rejected

* **eve** — would add a second agent-hosting framework next to kagent; its
  self-hosted durability outside Vercel is unverified.
* **Restate** — capable, but the server is BSL-licensed and is another
  stateful system to run, back up and upgrade. Revisit if waits/timers outgrow
  a ~10-state machine.

## Consequences

We own retries, timers, idempotency and the outbox. They are small and
well-understood patterns; see [orchestrator.md](../orchestrator.md).
