---
url: /another-agentic-system/architecture.md
---
# Architecture

A stateless Rust orchestrator over one Postgres event log drives any A2A agent and shows people the log over AG-UI. Read the goal and the principles, then the picture of what is built today; the detail is under the blocks, and the target design of a job comes after.

> Status: **MVP steps 1–2 built** (one thread, one A2A agent, durable delegation, spoken to people
> over AG-UI and A2UI); the rest is design. [As built](#as-built) is what the code does today, with diagrams; the sections after
> it, [How a job flows](#how-a-job-flows) and [Job lifecycle](#job-lifecycle), are the target
> design. Facts about third-party components are marked **verified** (checked against source,
> docs or a live system on 2026-09-28) or **unverified**; statements about this repository's
> code were checked against it on 2026-09-29.

## Goal

You start a job from a chat — or another system starts one over A2A, MCP or a
webhook. Agents plan it, split it, work in parallel, verify against real
checks, review, and hand back a pull request plus a summary. You read the chat
surface.

This system is the **orchestration layer**. It does not host agents: it drives
any agent that speaks A2A, uses tools over MCP, reacts to webhooks, and can
itself be driven the same way. Its processes are stateless; the only durable
state is the job ledger and event log (the chat) in Postgres.

## Principles

1. **Protocols only.** Everything this system depends on is a protocol: A2A,
   MCP, webhooks, an OpenAI-compatible model endpoint. No agent host, gateway
   product or SDK is a hard dependency.
   ([ADR 0007](decisions/0007-protocol-only-dependencies.md))
2. **Stateless processes, one event log.** Jobs outlive processes, so the job
   ledger and event log live in Postgres; nothing else does.
   ([ADR 0001](decisions/0001-rust-state-machine-on-postgres.md))
3. **Verification over consensus.** Quality comes from tests/CI, with bounded
   rework loops — not from agents agreeing.
   ([ADR 0002](decisions/0002-verification-over-consensus.md))
4. **git is the artifact.** Every step that changes code ends as a pushed
   branch; the result is a PR.
   ([ADR 0003](decisions/0003-git-as-durable-state-ephemeral-workers.md))
5. **The orchestrator never sees a protocol.** Adapters translate to one
   canonical event/command model around a pure core.
   ([orchestrator](orchestrator.md))

## Components

The orchestrator, Postgres, the web chat surface and the desktop app run here; agents, tools and the model endpoint are external.

::: in-depth Each component: its role and notes

| Component | Role | Notes |
|---|---|---|
| **Orchestrator** (Rust, this repo) | Durable thread state machine; decides what happens next | Stateless replicas over Postgres, run as a **control plane** (API, surfaces) and **workers** (dispatcher, inbox worker, in-process agents), or both in one process. ([ADR 0001](decisions/0001-rust-state-machine-on-postgres.md), [ADR 0015](decisions/0015-control-plane-and-workers-on-adam-rs.md)) |
| **Postgres (CNPG)** | Threads, the event log (= the chat), the A2A binding, the outbox | Also the work queue (`SKIP LOCKED`) and the wake-up bus (`LISTEN/NOTIFY`). The inbox (webhook reports and timers, an `inbox` table) and the `watches` that route a report to its thread are built ([ADR 0016](decisions/0016-inbox-timers-and-job-ledger-on-the-thread.md)); the generic webhook `POST /webhooks/ci` writes reports (MVP slice 6; the GitHub adapter is slice 9). |
| **Web chat surface** (Next.js static export + assistant-ui, this repo) | Chat surface and thread list | Renders the AG-UI 1.0 projection of the event log the orchestrator serves: it opens a thread from the newest turns of its history (`GET /agui/threads/{id}/history`, older turns as the person scrolls up, [ADR 0059](decisions/0059-a-thread-opens-at-its-end-and-older-turns-load-on-scroll-up.md)) and follows its connect stream from there, starts runs with `POST /agui/agents/{agentId}` and answers interrupts by `resume` ([ADR 0012](decisions/0012-ag-ui-user-facing-protocol.md), binding in [`api/agui.md`](api/agui.md), `@assistant-ui/react-ag-ui` per [ADR 0006](decisions/0006-assistant-ui-external-store.md)). Generative UI is A2UI ([ADR 0013](decisions/0013-a2ui-generative-ui.md)), rendered behind the web's own validator. The agent and thread lists and Cancel use the resource API. It has no server-side code: a static export served by Caddy (`web/Caddyfile`, [ADR 0047](decisions/0047-one-ui-for-web-desktop-and-mobile-each-signs-in-as-a-public-oauth-client.md)), and the browser talks to the orchestrator through the edge. |
| **Desktop app** (Tauri 2, `apps/tauri`, this repo) | The same UI in a window of its own | The web's static export packed into the app; it signs in as the public client `another-agentic-desktop` through the system browser and a loopback redirect (RFC 8252) and calls the API's origin directly with DPoP-bound tokens, which the orchestrator allows from the app's origins only (`server.cors.allowedOrigins`, [ADR 0047](decisions/0047-one-ui-for-web-desktop-and-mobile-each-signs-in-as-a-public-oauth-client.md)). Linux is built in CI; mobile is not built. |
| **Agents** (external) | Planner, coding workers, reviewers, specialists | Anything reachable by an A2A agent-card URL. |
| **Tools** (external) | GitHub, docs, search, … | MCP servers, relayed to the agent on the thread's own endpoint with the credentials kept here ([ADR 0024](decisions/0024-mcp-tools-attached-per-conversation.md), built in slice 8; the servers a person adds themselves, [ADR 0046](decisions/0046-people-add-their-own-mcp-servers-secrets-in-a-credential-broker.md)); the thread tools, `ask_agent` included, are in [`api/thread-tools-v1.md`](api/thread-tools-v1.md) ([ADR 0023](decisions/0023-ui-component-catalog-as-an-a2a-extension.md), [0024](decisions/0024-mcp-tools-attached-per-conversation.md), [0026](decisions/0026-agent-mentions-as-structured-references.md)). Built (*verified 2026-11-27* against `orchestrator/crates/surface-thread-tools/src/relay/`, `orchestrator/crates/tools-mcp/` and `dev/tools-e2e.sh`). |
| **Model endpoint** (external) | The orchestrator's own model calls | Any OpenAI-compatible endpoint — EAIG / Agent Router, AISIX, … ([ADR 0005](decisions/0005-openai-compatible-model-endpoint.md)). Built: `orch-model-openai` writes a thread's title and description ([ADR 0035](decisions/0035-utility-model-tasks.md)); an endpoint per thread for the agents' own model calls ([ADR 0063](decisions/0063-an-openai-compatible-model-endpoint-per-thread-on-the-orchestrator.md)) is not (*verified 2026-11-27* against `orchestrator/crates/model-openai/src/lib.rs` and `orchestrator/bin/orchestrator/src/model.rs`, and a search of `orchestrator/` for the relay found none). |

:::

### Agent hosts

This system does not care where an agent runs. Known hosts:

adam-coder, another-agentic-platform and kagent are the hosts known today, and any other A2A server works too.

::: in-depth Known hosts and what each adds

| Host | What it adds |
|---|---|
| **adam-coder** (default agent, shown as **Adam**, [ADR 0049](decisions/0049-the-coder-is-shown-as-adam-agents-may-have-aliases.md)) | An A2A 1.0 agent that clones a repository, works on a branch, runs the checks, pushes and opens a pull request. It is the default because it is the first entry of `AGENTS_FILE`; the orchestrator has no code path of its own for it. Published as an image (about 2.9 GB, `linux/amd64`). ([ADR 0014](decisions/0014-adam-coder-default-agent-over-a2a.md), [The default agent](#the-default-agent)) |
| **another-agentic-platform** (first-class) | Versioned agent services, release channels, scale-to-zero runtimes, per-run worktrees, credential broker. Its coding harness is ADK-Rust driving `opencode acp`. When a target comes from the platform, this system offers **release selection** through the platform's A2A extension. ([ADR 0008](decisions/0008-platform-integration-via-a2a-extension.md)) |
| **kagent** | Declarative agents on Kubernetes, reached over A2A 1.0: kagent 0.10 at `/api/a2a/{namespace}/{name}` when the client sends `A2A-Version: 1.0` (an agent is a plain Deployment), and kagent 1.x at `/agents/{namespace}/{name}` (alpha, on Agent Substrate). [`dev/kagent-e2e.sh`](../dev/README.md#kagent-an-agent-on-kubernetes-over-plain-a2a) is the scenario (`KAGENT_VERSION=0.10` or `1.x`, written 2026-10-07 against `v0.10.3` and `v1.0.0-alpha8`, **not yet run**): plain A2A, no extension of ours, so no steps, streamed text, UI surfaces, release channels or thread tools; kagent's default authentication reads no credential; a paused task reaches the person as its question only (kagent's HITL extension is not activated); and **a first message names no contextId** ([ADR 0055](decisions/0055-the-agent-assigns-the-a2a-context.md)): kagent assigns the contextId and refuses one it did not (from its source), so the thread's binding adopts the one it answers with, and every later message is sent in it. |
| **Anything else** | Any A2A server. |

:::

Considered and **not** chosen for this layer: **eve** (TypeScript durable
agents — an agent host, not an orchestration layer), **Restate** (BSL server,
extra stateful system — see ADR 0001), **OpenHands Agent Canvas** (what we ran
first — see [lessons](lessons-from-agent-canvas.md)).

## As built

What runs today (MVP steps 1–2, spoken to people over AG-UI: the run route, the connect stream, the
capabilities document and A2UI surfaces), read from the code on 2026-09-29. Solid boxes exist; dashed boxes are planned. The design that goes beyond it is
[further down](#how-a-job-flows); the crate map is in [Orchestrator: crate layout](orchestrator.md#crate-layout).

### Components

```mermaid
flowchart LR
  browser(("Browser"))
  subgraph EDGE["Edge: one origin"]
    edge["oauth2-proxy in production<br/>Caddy in compose, in front of a real oauth2-proxy<br/>and a mock issuer that approves anybody"]
  end
  desktop(("Desktop app<br/>Tauri 2: the same export"))
  web["<b>web</b>: Next.js static export + assistant-ui<br/>served by Caddy: the UI only<br/>no API routes, no server-side calls"]
  subgraph REPLICA["Orchestrator process: stateless, any number, one binary (ORCH_ROLE: all, control-plane, worker)"]
    direction TB
    api["<b>orch-api</b><br/>CORS (server.cors), identity layer,<br/>resource API, health"]
    surfaces["surfaces mounted by ORCH_SURFACES<br/>agui (run, connect, capabilities): built, the default<br/>chat-api: removed 2026-09-30<br/>a2a: planned"]
    app["<b>orch-app</b><br/>App: transition + commit loop, event streams"]
    disp["<b>Dispatcher</b><br/>claims outbox rows, delegates, applies replies"]
    adapters["adapters chosen in bin/orchestrator<br/>PgStore, PgWakeup, A2aAgentClient,<br/>PlatformRegistry (AGENT_REGISTRY_URL)"]
    api --> surfaces --> app
    disp --> app
    app --> adapters
    disp --> adapters
  end
  subgraph EMBED["The same core, embedded in an application (ADR 0076 decision 4)"]
    embed["<b>orch-embed</b><br/>App + Dispatcher over orch-store-sqlite,<br/>the caller's agents and directory,<br/>no HTTP server"]
    sqlite[("one SQLite file<br/>a thread's log, one process")]
  end
  subgraph PG["Postgres: the only state"]
    tables[("threads · events (the chat)<br/>a2a_bindings · outbox")]
    notify(["LISTEN / NOTIFY<br/>orch_thread · orch_outbox<br/>hints, never data"])
  end
  subgraph AGENTS["A2A agents: any host"]
    card["agent card, read live<br/>release-channels extension, optional"]
    a2a["A2A 1.0 JSON-RPC + SSE<br/>bearer token from AGENTS_FILE tokenEnv"]
  end
  subgraph PLATFORM["The platform: optional"]
    registry["agent registry, read live<br/>agent-registry/v1: a linkset of agent cards<br/>held in the process, never in Postgres"]
  end
  browser -- "GET / : the UI" --> edge
  browser -- "/api/* and /agui/*, incl. SSE" --> edge
  desktop -- "/api/* and /agui/* with DPoP<br/>CORS: server.cors.allowedOrigins" --> edge
  edge -- "everything else" --> web
  edge -- "/api/*, /agui/*, /mcp (bearer, no identity), /healthz, /readyz" --> api
  app -. "the same App and Dispatcher, wired twice" .-> embed
  embed --> sqlite
  adapters -- "sqlx: one txn per commit" --> tables
  tables --> notify
  notify -. "wake every replica" .-> adapters
  adapters -- "SendStreamingMessage · SubscribeToTask<br/>GetTask · CancelTask" --> a2a
  adapters -- "read card: agent list, release check" --> card
  adapters -- "GET the linkset: the agents the platform provisions<br/>Cache-Control, ETag; a failure lists none of them" --> registry
  more["replicas 2…N: the same process"] --- tables
  mcp["MCP tools · model endpoint<br/>webhooks · timers · A2A / MCP callers"]:::planned
  mcp -.-> api
  classDef planned stroke-dasharray: 5 5,fill:none
  style surfaces stroke-dasharray: 5 5
```

The notes below say how the core runs embedded, how the web reaches the orchestrator, how a person is authenticated, what a process runs for its role and how agents are found.

::: in-depth The same core runs embedded

* **The same core runs embedded.** The orchestrator is also a library: `orch-embed`
  (`orchestrator/crates/embed`) wires the same `orch-app` `App` and `Dispatcher` to one SQLite file and
  the caller's own agents, with no HTTP server and no Postgres, so an application runs a thread in its
  own process ([ADR 0076](decisions/0076-the-orchestrator-as-a-library-in-an-application.md), decision
  4\). The two compounds are roots, not layers: `bin/orchestrator` is the server's and does not depend on
  the crate, and the crate reimplements nothing the application already owns. A thread has one home —
  one process opens the file, and a second is refused by the store's lock.

:::

::: in-depth The web never talks to the orchestrator server to server

* **The web never talks to the orchestrator server to server.** It is a static export served by Caddy (ADR 0047), whose
  runtime settings are `/config.json`; the browser calls `/api/*` and `/agui/*` on its own origin, and the edge routes those paths
  to the orchestrator and everything else to the web. The desktop app is the same export in a Tauri window: it calls the API's
  origin from its own (`tauri://localhost`, `http://tauri.localhost`), which the orchestrator answers with CORS for the origins
  of `server.cors.allowedOrigins` only, never with credentials (a preflight before identity; `apps/tauri/README.md`). There are no Next.js API routes, no server-side fetches and no secrets in the web
  (`web/README.md`). Its settings, when it has any, are the public `ui` section of the orchestrator's configuration
  file, read from `GET /api/config` (built with S9 and S18, *verified 2026-11-27* against `orchestrator/crates/api/src/lib.rs:332`, [ADR 0034](decisions/0034-one-yaml-configuration-secrets-by-reference.md)). *Accepted (2026-10-05, [ADR 0045](decisions/0045-admin-dashboard-in-the-web-and-agent-access-from-the-registry.md)), not built:* an `/admin` area whose one route handler forwards the edge's bearer to the platform's API, the web's only server-side call, and its one setting, `PLATFORM_API_URL`. SSE goes browser → edge → orchestrator, unbuffered.

:::

::: in-depth The orchestrator authenticates; the edge logs people in

* **The orchestrator authenticates; the edge logs people in.** Identity is what the `Authenticator` port makes of a
  request's credentials, and it fails closed ([ADR 0033](decisions/0033-the-orchestrator-is-an-oauth2-resource-server.md),
  amending ADR 0012). With `auth.mode: jwt` the orchestrator is an OAuth2 resource server: oauth2-proxy stays in front
  of the web and forwards `Authorization: Bearer <JWT>`, a CLI sends its own token, and the orchestrator validates
  either against the issuer's JWKS (401 with `WWW-Authenticate: Bearer`; 503 while the keys cannot be fetched, and
  `/readyz` with it). The default `proxy_header` mode still trusts `X-Auth-Request-Email` and answers 401 without it,
  so it must only run behind a proxy that strips client-supplied copies; it is refused when `server.environment` is
  `production`. **Roles decide what a person may do**: the roles of the credential map, by `auth.roles`, to the
  permissions `agent.read`, `agent.invoke`, `thread.read`, `thread.write`, `thread.share`, `thread.delete`, `artifact.read`, `admin` and `tools.user.add` (people's own MCP servers, [ADR 0046](decisions/0046-people-add-their-own-mcp-servers-secrets-in-a-credential-broker.md); granted to no role by default; the routes it gates are `POST` and `PATCH` of `/api/user-tool-servers`, while listing and deleting one's own servers need no permission), which the
  application enforces (a person reads and changes their own threads, and **nobody else's, an administrator included**
  ([ADR 0039](decisions/0039-nobody-reads-another-persons-thread.md)); a thread that is not one's own is a 404) and `GET /api/me` reports
  ([`config.md`](api/config.md#roles-and-permissions), ADR 0033). Locally (S16), `edge` is Caddy in front of a real oauth2-proxy and a mock issuer that approves
  anybody (`dev@example.com`, or the user the sign-in is told to be), the orchestrator runs `auth.mode: jwt`, and the scripts send a token of the
  mock ([`dev/README.md`](../dev/README.md#sign-in-a-mock-issuer-and-oauth2-proxy)). **Decided, not built:** a thread's owner may share it, `private`, `internal` or `public`, capped by `sharing.mode` and read through a link of its own, with one edge route that skips sign-in for the public link only ([ADR 0040](decisions/0040-thread-sharing-by-revocable-link.md)).

:::

::: in-depth A process runs the halves its role asks for

* **A process runs the halves its role asks for** (`ORCH_ROLE`, ADR 0015): the HTTP server
  (`orch-api` plus the mounted surfaces) as the **control plane**, the dispatcher as a **worker**, or
  both (`all`, the default). A worker serves only `/healthz` and `/readyz`. The two halves meet only in
  Postgres, so control planes and workers scale apart. A process that dies loses nothing: its
  outbox leases lapse and another replica resumes the agent's task. Any replica can serve any thread's stream, because streams read the log.

:::

::: in-depth Agents are A2A agent-card URLs, and the registry

* **Agents are A2A agent-card URLs**, from a static `AGENTS_FILE` read once at boot and, when
  `AGENT_REGISTRY_URL` is set, from the platform's agent registry
  ([ADR 0022](decisions/0022-platform-provisions-agents-system-discovers-them.md), the contract
  `agent-registry/v1` of another-agentic-platform) through the port `AgentRegistry`. The registry is
  read live: an agent the platform adds is in the picker without a restart, held in the process for as
  long as the registry's `Cache-Control` allows and never in Postgres. It fails closed: a registry
  that cannot be read lists none of its agents, the file's agents stay, and
  `GET /api/registry` says which source is down, so the UI can. A delegation to one of its agents is
  retried while the registry cannot answer and dead-lettered only when it answers without the agent.
  An item may carry an `audience` ([ADR 0045](decisions/0045-admin-dashboard-in-the-web-and-agent-access-from-the-registry.md)): a person may read or invoke a registry's agent only when a role of theirs grants it and the audience holds `*` or a value of their roles claim, or a role of theirs holds `admin`; with none, only the administrators may. The deployment's own agents have none.
  The cards themselves are read live and never cached, and a release selection is offered only when
  the live card advertises the release-channels extension (ADR 0008): the registry lists no
  releases. Gate layers and the verifier are for the file's agents.

:::

### A chat turn

One user message, from the browser to the agent and back, over **AG-UI**, the default user-facing
protocol ([ADR 0012](decisions/0012-ag-ui-user-facing-protocol.md); every route, status and mapping
is in [`api/agui.md`](api/agui.md)). The browser holds one **connect stream** per open thread and
sends **runs** beside it. The dispatcher may run on a different replica than the one that took the
request, and the connect stream may be served by a third.

```mermaid
sequenceDiagram
  autonumber
  actor B as Browser (web UI, ThreadAgent)
  participant E as Edge proxy
  participant A as Replica A<br/>orch-api + orch-surface-agui + App
  participant DB as Postgres
  participant D as Dispatcher<br/>(any replica)
  participant G as A2A agent
  opt a thread that is opened (ui.history, ADR 0059)
    B->>E: GET /agui/threads/{id}/history?limit=12: the newest settled turns, one JSON page
    E->>A: the request plus X-Auth-Request-Email
    A->>DB: App::history_log: events from seq 1, folded until the page cannot change
    A-->>B: HistoryPage: frames, start, end (a settled Last-Event-ID), carry, and older pages by before=start as the person scrolls up
  end
  B->>E: GET /agui/threads/{id}/connect, Last-Event-ID: n (a page's end, or none where there is no history route: from the start)
  E->>A: the request plus X-Auth-Request-Email, unbuffered
  A->>DB: App::event_stream: events from seq 1 (fold up to n without writing), then wait for NOTIFY, poll every 5 s
  A-->>B: SSE: the preamble when a run is open at n, then frames, id: seq on resume points
  alt a new message
    B->>E: POST /agui/agents/{agentId}: RunAgentInput {threadId (a UUID the browser minted), runId, one new user message}
  else the answer to an interrupt
    B->>E: POST /agui/agents/{agentId}: RunAgentInput {resume: [{interruptId, status: resolved, payload: {text}}]}
  else a button on an A2UI surface
    B->>E: POST /agui/agents/{agentId}: forwardedProps.a2uiAction.userAction, no message, no resume
  end
  E->>A: the request plus X-Auth-Request-Email
  A->>A: checks before any stream byte: body, thread owner (404), agent, release (read the live card, fail closed)
  A->>A: translate(RunAgentInput, thread view) → one core Input
  A->>A: transition(state, Input) → (state, [Append user_message, Delegate])
  A->>DB: ONE txn: (thread and A2A binding, new thread only), state + version, event user_message (key agui:threadId:msg:messageId), outbox row (pending), NOTIFY
  A-->>B: POST response, SSE: RUN_STARTED, STATE_SNAPSHOT (the requester's projection of the log)
  Note over B,A: a standard AG-UI client reads this response to the run's terminal event, the web stops at RUN_STARTED and reads the run from the connect stream
  DB-->>A: NOTIFY orch_thread (every replica with an open stream)
  A-->>B: connect stream: user message, SUBAGENT_STARTED, ACTIVITY_SNAPSHOT vymalo.status (id: seq)
  DB-->>D: NOTIFY orch_outbox (or the 2 s poll)
  D->>DB: claim_outbox: SKIP LOCKED, 30 s lease, status inflight
  D->>G: read the agent card, then SendStreamingMessage<br/>messageId = outbox row id, no contextId on a thread's first message (the agent assigns it, ADR 0055), bearer token
  G-->>D: SSE frames: task submitted, working, artifact, completed
  D->>DB: first frame: mark_sent (sent_at, task id, the context the agent assigned)
  loop each frame the agent sends
    D->>DB: App.apply(Input::Agent, idempotency key): read the thread, transition, commit at the expected version (events, thread_state, NOTIFY)
    DB-->>A: NOTIFY orch_thread
    A->>DB: events after the stream's cursor
    A-->>B: TEXT_MESSAGE_*, ACTIVITY_SNAPSHOT (vymalo.status, vymalo.artifact, a2ui-surface), id: seq
  end
  D->>DB: the turn ended: outbox row delivered
  A-->>B: STATE_SNAPSHOT, then RUN_FINISHED: success, cancelled or (the agent needs input) interrupt, RUN_ERROR on failure
  Note over B,A: the connection drops, or replica A is killed
  B->>E: GET /agui/threads/{id}/connect, Last-Event-ID: n
  E->>A: served by any replica
  A->>DB: events from seq 1: fold up to n, write the rest
  A-->>B: preamble (RUN_STARTED, SUBAGENT_STARTED, STATE_SNAPSHOT), then seq n+1 … live (the client dedupes by seq)
```

Prose for what the diagram compresses:

::: in-depth Point by point: the transaction, crashes, interrupts, streams and cancel

* **One transaction is the whole decision** (the `ONE txn` message). `App::apply` reads the thread, runs the pure
  `transition`, and asks the store to commit the new state, the events and the outbox rows at the
  version it read. A lost race is a `VersionConflict`; `apply` re-reads and retries up to 8 times,
  then answers 503. A replayed input carries an idempotency key and is a no-op (`Duplicate`); on
  the run route the key is `agui:<threadId>:msg:<messageId>` (`…:run:<runId>` for an answer with no
  message id of its own), so a retried POST attaches to the run instead of duplicating it.
* **A crash cannot lose or repeat the delegation.** The outbox row exists exactly when the message
  is in the log. If the dispatcher's replica dies, the lease (30 s by default) expires and another
  replica re-claims the row; because `sent_at` is set it resumes the agent's task
  (`SubscribeToTask`, else polling `GetTask`) instead of sending again, and the agent's own ids
  make each stored update idempotent.
* **The run is bound when its task id is first seen.** With `RUN_BINDING_URL` set, the dispatcher tells
  the credential broker who spends for a run (the thread's owner), which agent it is, the model endpoint
  label the live card maps the run's pick to (the picked alias's `endpoint`, or the card's default choice)
  and its deadline (24 h, the broker's own cap) on the **first frame** of the delegation's stream, before
  that frame is applied, and unbinds it once the thread is `done`, `failed` or `cancelled`
  ([ADR 0084](decisions/0084-the-run-lease-the-broker-binds-a-run-and-lends-its-model-credential.md)).
  The run's first model call can beat that: the injector answers `run_not_bound` and adam parks and
  retries it (ADR 0067 decision 9). A broker that cannot be reached is logged and the run goes on.
* **An `input-required` turn ends the run and the delegation, not the thread.** The thread becomes
  `blocked` and the run ends `RUN_FINISHED{outcome: interrupt}`, carrying an interrupt id and the
  agent's question. The user's answer is the next run: `resume` (or, as accepted today, a plain user
  message) is a `user_message`, which re-queues the thread and sends a new outbox row that continues
  the *same* A2A task. A2A method names are those of A2A 1.0 (`SendStreamingMessage`; the 0.3
  spelling is `message/stream`); recorded in `orch-agent-a2a`, *verified* against the SDK sources
  2026-09-29 by that crate's author, not re-checked for this page.
* **A surface is an activity, an action is a run.** An agent's A2UI part becomes a `ui_surface`
  event and, on the wire, the whole surface as an `a2ui-surface` activity; a click on it comes back
  as `forwardedProps.a2uiAction`, becomes a `ui_action` event and is delivered to the same A2A task
  ([ADR 0013](decisions/0013-a2ui-generative-ui.md), [`api/agui.md`](api/agui.md#a2ui-generative-ui)).
* **The stream is the log.** `App::event_stream` replays every event with `seq >` the cursor, then
  follows; a clamped cursor, a poll under the `NOTIFY`, and a subscribe-before-read order mean no
  gap and no duplicate. The projection is a function of the log prefix, so frames are identical on
  every replica and on every replay. A stream ends at its lifetime (60 s by default, ADR 0082) and
  when the process shuts down, either way without a terminal event, so the client reopens it with
  `Last-Event-ID` and is shown each later event once.
  The web hands frames to its runtime in whole groups (an `id:` closes one) and drops groups it has
  delivered, so a cut connection never leaves half a message.
* **Closing a stream never cancels a run.** Truncation is not cancellation (the AG-UI rule). Cancel is
  `POST /api/threads/{id}/cancel` of the resource API, and its outcome arrives as
  `RUN_FINISHED{outcome: cancelled}`.
* **There is one door.** The legacy chat API (`POST /api/threads`, `POST /api/threads/{id}/messages`,
  `GET /api/threads/{id}/events` and `…/stream`, our own `Event` JSON over SSE) drove `App` the same
  way from the orchestrator inward; it was removed on 2026-09-30. Those routes answer 404, but
  `POST /api/threads` answers 405, because its path is shared with the resource API's `GET /api/threads`.

:::

The run as a state machine, as the projection shows it (a run is open exactly while the thread is
`queued` or `working`; the thread's own states are in the next section):

```mermaid
stateDiagram-v2
  [*] --> RunActive: user_message, ui_action or a producer event (RUN_STARTED)
  RunActive --> Interrupted: input or auth required (RUN_FINISHED interrupt)
  RunActive --> Succeeded: agent completed (RUN_FINISHED success)
  RunActive --> Cancelled: cancel endpoint (RUN_FINISHED cancelled)
  RunActive --> Errored: agent or delivery failed (RUN_ERROR)
  Interrupted --> RunActive: next run with resume, or a new user message
  Errored --> RunActive: next run (thread still open after a retryable failure)
  Succeeded --> RunActive: a new user message starts the next job (ADR 0020)
  Cancelled --> RunActive: a new user message starts the next job
  Errored --> RunActive: a new user message after the thread failed starts the next job
```

### Thread state

The state of a thread is `orch-core`'s `ThreadState`; `transition(&state, &input)` is the only
place it changes. It is a subset of the [target job lifecycle](#job-lifecycle) below.

```mermaid
stateDiagram-v2
  [*] --> Queued: thread created with its first user message
  state Open {
    Queued --> Working: the agent reports working
    Queued --> Blocked: input or auth required; retryable delivery failure
    Working --> Blocked: input or auth required; retryable delivery failure
    Blocked --> Queued: a user message (the answer, or a follow-up)
    Blocked --> Working: the agent resumes the same task
  }
  Open --> Done: the agent completed
  Open --> Failed: the agent failed or rejected; permanent delivery failure
  Open --> Cancelled: the agent cancelled; cancelled before the agent started
  Done --> Queued: a user message starts the next job
  Failed --> Queued: a user message starts the next job
  Cancelled --> Queued: a user message starts the next job
```

`Open` is `queued`, `working` and `blocked`; `done`, `failed` and `cancelled` end the **current job**, not the
thread ([ADR 0020](decisions/0020-a-thread-is-a-conversation.md)). Inside
`Open`, a user message in `queued` or `working` keeps the state and sends another delegation, a
cancel keeps the state and asks the agent to cancel (the outcome arrives as the agent's `canceled`),
and artifacts and agent messages keep the state. On a finished thread a user message starts job *n+1*
(`user_message`, `job_started`, a delegation; attempt 1 of the same gate, the agent's context kept), an A2UI action
is refused (409: its card belongs to a finished request) and a late agent update is dropped. A `thread_state`
event is appended only when a thread *enters* `blocked`, `done`, `failed` or `cancelled`. The full table, row by
row, is in [Orchestrator: thread state and transitions](orchestrator.md#thread-state-and-transitions).

#### A follow-up after the job ended

```mermaid
sequenceDiagram
  participant U as Person (chat)
  participant O as Orchestrator
  participant L as Event log
  participant A as Agent (A2A)
  A-->>O: task T1 completed
  O->>L: agent_status completed, thread_state done
  O-->>U: the turn ends, the composer stays enabled
  U->>O: a follow-up message
  O->>L: user_message, job_started {job: 2}
  O->>A: new task T2, same contextId, referenceTaskIds [T1]
  A-->>O: working, artifacts, completed
  O->>L: the events of job 2, thread_state done
  O-->>U: the turn of job 2
```

```mermaid
stateDiagram-v2
  [*] --> Job1Open: first message
  Job1Open --> Job1Over: done, failed or cancelled
  Job1Over --> Job2Open: a user message (job_started 2, attempt 1)
  Job2Open --> Job2Over: done, failed or cancelled
  Job2Over --> Job3Open: a user message
```

Only the current job is stored (`threads.job`, with its `number`); earlier jobs are in the log between
`job_started` events. The A2A side is a new task on the same context that names the previous one
([ADR 0021](decisions/0021-context-across-a2a-tasks.md)).

### The default agent

The default agent is the first entry of `AGENTS_FILE`, and in the dev stack that is
[adam-coder](https://github.com/vymalo/another-adam-rs) ([ADR 0014](decisions/0014-adam-coder-default-agent-over-a2a.md)),
shown as Adam: its id is `adam`, and `coder` is an alias of it, so a thread made before the rename, a link or a mention that says `coder` is about the same agent
([ADR 0049](decisions/0049-the-coder-is-shown-as-adam-agents-may-have-aliases.md)).
`GET /api/agents` keeps the file's order, the chat UI preselects the first agent, and if its card
cannot be read it stays first, listed without live details, instead of giving way to the next agent.
The orchestrator reads the coder like any other agent (a card URL and a bearer token from `tokenEnv`)
and sends the chat text unchanged, so the first message has to name the repository and the base branch.

```mermaid
sequenceDiagram
  participant D as Orchestrator dispatcher
  participant C as adam-coder
  participant G as git remote
  participant H as GitHub API
  D->>C: GET /.well-known/agent-card.json (no token)
  C-->>D: card (streaming, JSON-RPC URL = PUBLIC_URL)
  D->>C: SendStreamingMessage, bearer token, text = repository + base branch
  C-->>D: task submitted, then status working
  C->>G: clone, work on a branch, run the checks, push
  C-->>D: artifact branch (JSON data part)
  C->>H: open the pull request
  C-->>D: artifact pull_request (JSON data part)
  C-->>D: status completed
  Note over D,C: A dropped stream resumes with SubscribeToTask, or GetTask when the task is unknown. ListTasks is unsupported.
```

A2A task states become thread states through the pure transition function, exactly as for every
other agent; the coder adds no state of its own. `submitted` changes nothing, and `rejected` is a
failure whose detail starts with `rejected:`.

```mermaid
stateDiagram-v2
  [*] --> Queued: thread created, the task is submitted
  Queued --> Working: task working
  Queued --> Blocked: task input-required or auth-required
  Working --> Blocked: task input-required or auth-required
  Blocked --> Working: task working again
  Blocked --> Queued: the user answers, a new message
  Queued --> Done: task completed
  Working --> Done: task completed
  Queued --> Failed: task failed or rejected
  Working --> Failed: task failed or rejected
  Queued --> Cancelled: task canceled
  Working --> Cancelled: task canceled
  Done --> [*]
  Failed --> [*]
  Cancelled --> [*]
```

::: in-depth What the coder sends back, and that what it says is a folder

The `branch` and `pull_request` artifacts arrive as JSON data parts, so the chat shows their JSON
(`data.text` of the artifact event) and not a link until the coder also sends a URL part. The dev
stack runs the published image against scripted mocks, and [`dev/coder-e2e.sh`](../dev/coder-e2e.sh)
turns one chat message into a pull request (how: [`dev/README.md`](../dev/README.md#the-default-agent)).

**What the coder says is a folder, not code.** The coder reads its agent folder (its name, card and
instructions) once, at startup, from the directory its `ADAM_AGENT_DIR` names, and falls back to the copy
embedded in the image only when that is unset. The dev stack mounts
[`dev/coder/agent/`](../dev/coder/agent/instructions.md), vendored byte for byte from adam-rs
(`bin/adam-coder/agent/`, the commit in `dev/coder/UPSTREAM`, drift-checked like the mocks), at `/etc/adam/agent`: a change
to it, or `CODER_AGENT_DIR` pointed at a copy, needs a restart of the coder and no build
([`dev/README.md`](../dev/README.md#change-what-the-coder-says); `dev/agent-folder-e2e.sh` proves it). The orchestrator
changes nothing for this: it reads the card of the restarted coder when it delegates, like any agent's, and it sends
the chat text unchanged, so "hi" reaches the coder as "hi" and gets a greeting back, with the coder's name, what it does and
a question (the thread waits, `blocked`), instead of a request for a task
([adam-rs#55](https://github.com/vymalo/another-adam-rs/issues/55), `dev/greeting-e2e.sh`). How a live model follows
the instructions is *unverified*; the mocks prove that the folder reaches the model.

:::

### Agents that are only a folder

The dev stack runs a chat, a researcher and a browser agent beside the coder, and none of them has a build of its own: each is a folder served by `adam-agent`.

::: in-depth The chat and the researcher: two folders on adam-agent

The dev stack runs two more agents beside the coder, and neither has a build of its own: a **chat** and a **researcher**. Each is a folder
([`dev/agents/chat/agent/`](../dev/agents/chat/agent/instructions.md), [`dev/agents/researcher/agent/`](../dev/agents/researcher/agent/instructions.md)) served by
`adam-agent`, the second binary of the coder's image, so a service is that image with another entrypoint and the folder mounted at `/etc/adam/agent`
(read once, at startup, like the coder's). To the orchestrator they are two more entries of `AGENTS_FILE`, after the coder, which stays the default
agent: a card URL and a bearer token, no code path of their own ([ADR 0014](decisions/0014-adam-coder-default-agent-over-a2a.md)). The researcher's
`mcp.json` names an MCP server whose tool (`web_search`) it gets as `search__web_search`; in the dev stack that is the mock web search, and the
researcher's service allows plain http to it on purpose. An agent with no gate is `done` when it says so, so a chat's greeting ends the thread `done`
where the coder's (a question) leaves it `blocked`. How each is scripted, the diagrams and how to add a fourth by writing a folder:
[`dev/README.md`](../dev/README.md#several-agents). Their real behaviour with a live model is *unverified*; the mocks prove that the folder reaches the model and
that the tool call reaches the server.

:::

::: in-depth The browser agent and its sidecar

A fourth folder agent, the **browser** ([ADR 0057](decisions/0057-a-browser-agent-an-adam-folder-with-obscura-as-its-sidecar.md)), has a
sidecar: obscura, a headless browser whose MCP server listens on the pod's loopback (in compose, the agent runs in the sidecar's network
namespace) behind a bearer, so each replica has a browser of its own, one replica with one worker. Others ask it: a person mentions
`@browser` and the chat's model calls `ask_agent`, or, with `browser.chatSubagent`, the chat calls it as its own remote sub-agent (`a2a:`).
In the chart it is `browser.enabled` ([`deploy/chart`](../deploy/chart/README.md#the-browser-agent-adr-0057)), with a NetworkPolicy that lets
it reach the public web only. Its screenshots are files of its run (obscura's `files: true`, adam-rs ADR 0033): the thread keeps them when
the person talks to the browser or the chat's sub-agent passes them on; an `ask_agent` names them only (ADR 0057, amended 2026-10-09).

:::

### AG-UI: how it is served

The user-facing protocol is **AG-UI 1.0**, with the event log as the only source of truth
([ADR 0012](decisions/0012-ag-ui-user-facing-protocol.md); the mapping tables, endpoints and
`vymalo.*` schemas are [`api/agui.md`](api/agui.md)). Everything in
[A chat turn](#a-chat-turn) runs on it: the projection, the run route, the connect stream (replay,
cursor, following across runs) and the capabilities document are built, and the web runs on them
([`web/README.md`](../web/README.md#the-chat-layer)). The facts about the protocol itself were
*verified 2026-09-29* against the AG-UI 1.0 specification, <https://docs.ag-ui.com/spec/1.0/index.md>
(ADR 0012, section "Verified", lists each page). Two of them carry the design: the standard HTTP + SSE
binding has no stream resumption (`Last-Event-ID` is not used), so **the connect stream is our own
documented extension**, announced as `transport.resumable` and ignorable by a plain client; and a
consumer that abandons a stream has a *truncated* run, not a cancelled one, so cancel is a resource
call.

The two directions, both through the same pure crates:

```mermaid
flowchart LR
  subgraph INB["Inbound: RunAgentInput to a core input"]
    post["POST /agui/agents/{agentId}<br/>RunAgentInput"]
    parse["orch-agui-proto<br/>RunAgentInput::parse<br/>drops unknown members, rejects malformed"]
    tr["orch-agui-projection<br/>translate(input, ThreadView)"]
    apply["orch-app<br/>App::apply(Input)"]
    post --> parse --> tr --> apply
  end
  subgraph OUTB["Outbound: log event to AG-UI frames"]
    log[("events<br/>Postgres")]
    es["orch-app<br/>App::thread_feed(user, thread, after)<br/>the log (replay, then live) with live text mixed in"]
    pj["orch-agui-projection<br/>Projector::apply(event, Audience)<br/>resume_preamble()<br/>LiveOverlay (live text, ADR 0027)"]
    fr["Frame: AG-UI event + resume_id"]
    sse["orch-surface-agui<br/>SSE: data: frame, id: seq<br/>run route and connect stream"]
    hs["orch-agui-projection<br/>History: the frames cut into pages of settled chains<br/>and the carry of what a page leaves out"]
    hr["orch-surface-agui<br/>JSON: GET /agui/threads/{id}/history"]
    cli["AG-UI client"]
    log --> es --> pj --> fr --> sse --> cli
    fr --> hs --> hr --> cli
  end
  apply --> log
  classDef planned stroke-dasharray: 5 5,fill:none
```

The log-to-frames direction, as a state machine: what one connect request does.

```mermaid
stateDiagram-v2
  [*] --> Folding: connect (cursor c, 0 = none)
  Folding --> Writing: the event at c is folded (preamble if a run is open)
  Writing --> Writing: next event, frames with id seq
  Writing --> Over: mode=run, replay done, no run open
  Writing --> Expired: the lifetime, the token's expiry, or an hour (ADR 0082)
  Expired --> Folding: reopen with the last id: each later event once
  Writing --> Truncated: the process shuts down, or the connection is lost
  Truncated --> Folding: reconnect with the last id
  Over --> [*]
```

The run's own lifecycle is the state diagram under [A chat turn](#a-chat-turn); how the streams are
produced from the log, and why no replica remembers a connection, is
[Orchestrator: live updates](orchestrator.md#live-updates).

Every part of the AG-UI side is built, from the wire types to the web, and the legacy chat API is removed.

::: in-depth What is built, part by part

| Part | What is built |
|---|---|
| Wire types | `orch-agui-proto`: all 31 AG-UI 1.0 events and `RunAgentInput` as closed enums, checked against the vendored official schema |
| Log → frames | `orch-agui-projection`: `Projector` (audiences, runs, subagents, interrupts, `resume_preamble`); a function of the log, with no async and no I/O |
| `RunAgentInput` → input | `orch-agui-projection::translate` (new message, `resume`, cancel, attach, refusals with their HTTP status) |
| Live text | The words of a reply that is still being written ([ADR 0027](decisions/0027-live-text-relayed-not-stored.md), [`api/text-stream-v1.md`](api/text-stream-v1.md)): the dispatcher that holds the agent's stream relays each piece on the wakeup port (Postgres `NOTIFY orch_live`, every process listens, never stored), `App::thread_feed` mixes the pieces of a thread into its events, and `LiveOverlay` turns them into frames beside the projection (never resume points, merged by message id with the log's final message); the `split` profile works because every role listens ([`api/agui.md`](api/agui.md#live-text)) |
| Conformance | Schema validation of every frame; well-formedness properties; resume-from-any-point property; goldens read through `@ag-ui/client` 1.0.0 by `tools/agui-conformance` in CI |
| HTTP routes | `orch-surface-agui`: `POST /agui/agents/{agentId}` (a consumer-minted thread id, id reconciliation, `resume`, refusals as RFC 9457 problems before the stream); `GET /agui/threads/{id}/connect` (replay, `Last-Event-ID`, `?mode=run`, keepalive, a stream that ends at its lifetime — 60 s by default, [ADR 0082](decisions/0082-a-stream-ends-after-a-minute-and-a-message-keeps-its-id.md) — and is reopened from the cursor, follows across runs and replicas); `GET /agui/threads/{id}/history` and its two shared variants (a page of settled chains from the newest back, a catch-up; [`api/history.md`](api/history.md)); `GET /agui/agents/{agentId}/capabilities`; `agui` as an `ORCH_SURFACES` value and a `surface-agui` feature |
| Idempotent runs | A retried POST attaches instead of duplicating: the idempotency key `agui:<threadId>:msg:<messageId>` on the event log (the inbox is for machine input only: webhook reports and timers). A resend of a known message under a **fresh** `runId` attaches to the run that message opened, which the log's `user_message` records ([ADR 0082](decisions/0082-a-stream-ends-after-a-minute-and-a-message-keeps-its-id.md)): five sends of one message are one message, one run and one charge |
| The web | `@assistant-ui/react-ag-ui` (pinned, one patch) over a `ThreadAgent`: the newest turns by `history` and the connect stream with `Last-Event-ID` from where the page ends, runs by `POST /agui/agents/{agentId}`, interrupts by `resume`, Cancel by the resource API ([ADR 0012](decisions/0012-ag-ui-user-facing-protocol.md#the-web)) |
| Generative UI | A2UI ([ADR 0013](decisions/0013-a2ui-generative-ui.md)) on the orchestrator side: `ui_surface` and `ui_action` events, the A2A adapter's `application/a2ui+json` parts (envelope check, size caps), capability detection of both extension URIs, `a2ui-surface` snapshots of the whole surface, `forwardedProps.a2uiAction` validated and delivered to the same A2A task, the capabilities document ([`api/agui.md`](api/agui.md#a2ui-generative-ui)); in the web, the validator, the shadcn vocabulary and actions on a user gesture only ([`web/README.md`](../web/README.md#a2ui-surfaces)) |
| MCP server | `orch-surface-mcp` ([ADR 0019](decisions/0019-mcp-server-over-streamable-http.md)): `list_agents`, `start_job`, `get_job`, `answer` and `cancel_job` at `/mcp` over streamable HTTP, stateless (any replica serves any call), a machine route behind static bearer tokens that map to users; straight to `App`, not through the inbox; `mcp` as an `ORCH_SURFACES` value and a `surface-mcp` feature (default). `wait_for_job` follows a job through the event log with `notifications/progress` (one per event, a heartbeat every 60 s), returns when the job is finished or blocked or on a timeout with `resume_after_seq`, and continues on any replica with `after_seq` |
| Thread tools | `orch-surface-thread-tools` and `orch-thread-token` ([`api/thread-tools-v1.md`](api/thread-tools-v1.md), [ADR 0023](decisions/0023-ui-component-catalog-as-an-a2a-extension.md)): a second MCP endpoint, per thread, for the agents the orchestrator sends work to, at `/thread-tools/{threadId}/mcp` over streamable HTTP, stateless, a machine route behind a short-lived HS256 token scoped to one thread and one caller (minted by the A2A adapter at send time and put in the message metadata of an agent whose live card lists `thread-tools/v1`, verified by any replica, never in the log or the outbox); the built-in `get_ui_catalog` (the refetch seam) and a `ThreadToolProvider` seam for the tools of later slices, of which the **relay** is the first ([below](#the-relay-of-an-attached-mcp-servers-tools)); `thread-tools` as an `ORCH_SURFACES` value and a `surface-thread-tools` feature (default) |
| The legacy chat API | Removed (2026-09-30, [ADR 0012](decisions/0012-ag-ui-user-facing-protocol.md#the-legacy-interaction-endpoints-are-deprecated-by-the-flag), step 3): the crate `orch-surface-chat-api`, its feature `surface-chat-api`, the four interaction operations of `chat-api.yaml` and their goldens. The resource API stayed |

:::

`ORCH_SURFACES` accepts `agui` and defaults to it. The removed `chat-api` fails closed: naming it is a
startup error (exit 78) that says it was removed and points to AG-UI. The resource API and health are
mounted whatever it says. A name whose Cargo feature is not compiled in is also a startup error
(exit 78).

### The relay of an attached MCP server's tools

*Built 2026-10-02 ([ADR 0024](decisions/0024-mcp-tools-attached-per-conversation.md), slice 8; contract in
[`api/thread-tools-v1.md`](api/thread-tools-v1.md#attached-servers-and-the-relay-slice-8)).* A person attaches MCP servers
the deployment lists (`toolServers`) to a conversation. The agent never gets a server's URL or credential: it lists the
tools of its own per-thread endpoint, finds `websearch__search` among them, and calls it there. The endpoint's **relay**
(`RelayTools`, a `ThreadToolProvider`, composed by the binary behind the feature `tool-relay`) holds the credentials,
calls the server through the `ToolServerClient` port (`orch-tools-mcp` in the binary), and reports each call as one **step**
with the server's icon, so the person sees it and the agent does not report it twice (`_meta.reportsStep`).

::: in-depth Signed requests and the 2026-07-28 MCP protocol (ADR 0080)

*2026-11-02 ([ADR 0080](decisions/0080-requests-to-a-tool-server-are-signed-for-the-person.md)).* A server of the deployment's list can speak
MCP `2026-07-28` (`toolServers[].protocol`: the session opens with `server/discover`) and can take **signed requests** instead of a bearer
(`toolServers[].signing`): every HTTP request the relay makes to it carries one header that binds the request, the thread and **the thread's owner**
(the person the tool runs for, not the agent), so the tool server can authorise the person; the contract is
[`api/signed-tool-requests-v1.md`](api/signed-tool-requests-v1.md). A server that is not configured for it is called exactly as above.

:::

```mermaid
sequenceDiagram
  autonumber
  participant A as Agent (any A2A agent that lists thread-tools/v1)
  participant R as Thread tools endpoint (relay)
  participant L as Event log (steps)
  participant S as MCP server (credentials held by the relay)

  A->>R: tools/list (bearer: the thread's token)
  R->>S: tools/list (the server's bearer and headers)
  S-->>R: search
  R-->>A: websearch__search with _meta {reportsStep: true, timeoutSecs}
  A->>R: tools/call websearch__search {query}, _meta {callId, parentStepId}
  R->>L: Input::Step running (tool-<callId>, icon mcp-server:websearch, input)
  R->>S: tools/call search {query} (the server's bearer)
  alt the server answers
    S-->>R: result (at most 256 KiB, or cut with a note)
    R->>L: Input::Step completed or failed (output, at most 8 KiB)
    R-->>A: the whole result, isError passed through
  else unreachable, no answer in time, 401 or 403, a JSON-RPC error
    R->>L: Input::Step failed (detail: the public error)
    R-->>A: isError with a message that names no credential
  else the agent cancels or its connection closes
    R->>S: the request is dropped
    R->>L: Input::Step canceled
  end
```

```mermaid
stateDiagram-v2
  [*] --> Running: tools/call accepted, the step starts
  Running --> Completed: the server answered (isError false)
  Running --> Failed: unreachable, timeout, 401 or 403, a JSON-RPC error, or isError
  Running --> Canceled: the agent cancelled or dropped the call
  Completed --> [*]
  Failed --> [*]
  Canceled --> [*]
```

::: in-depth What the relay refuses, caches and logs

A name that is not on the endpoint (an unknown or detached server, a tool the allow-list or the agent filter leaves out) is
`-32602` and has no step; a call after the thread's task is over is a result with `isError` and has no step. Nothing is
cached (each `tools/list` lists again) and the relay keeps nothing between calls, so any replica serves any call; what it
writes is steps, in the thread's event log. The relay is an outbound surface: the servers come from the deployment (and,
below, from the thread's owner), a result is cut at 256 KiB and is untrusted text for the agent's model, and no credential
reaches a log line, an event or a table (the end-to-end tests search for them).

:::

::: in-depth A person's own servers (ADR 0046)

**A person's own servers** (*built 2026-10-15, [ADR 0046](decisions/0046-people-add-their-own-mcp-servers-secrets-in-a-credential-broker.md),
slice 5a; the contract is [`api/thread-tools-v1.md`](api/thread-tools-v1.md#a-persons-own-servers)*). With
`userToolServers.enabled`, the same relay also serves the `u-` servers the thread's owner added and attached. They differ in
trust, and so in four things: the credential is not the relay's but the **broker's** (`CredentialBroker::lease`, a fresh lease for
every `tools/list` and every `tools/call`, moved into the request and never kept); the client that calls the server is the
**guarded** one (`McpToolClient::guarded`), which judges the URL and then the address it is about to connect to, so a name that
resolves to a private address is never connected to; what the server says is believed less (no `annotations`, a 2 KiB
description); and a server whose credential cannot be had is **left out and fails a call as a step**, never skipped silently.
A 401 or 403 from the server sets the definition to `needs_reauth`, and the picker tells the person to authorize or key it again.

:::

### Child sessions: a helper an agent runs is a thread

*Built 2026-11-18 ([ADR 0072](decisions/0072-child-sessions-are-nested-threads-followed-through-child-sessions-v1.md), slices S2a to
S2d; the contract is [`api/child-sessions-v1.md`](api/child-sessions-v1.md)).* An agent whose card lists `child-sessions/v1` runs
a helper as a **session** and says so on its task. The orchestrator makes a **child thread** for each session, nested under its
parent in the person's list, whose log is the helper's own work, and writes three events in the **parent's** log (`child_session_opened`,
`child_session_reported`, `child_session_closed`), each once. The orchestrator **sends the child nothing**: each round is a task the agent
already runs, and the child's job follows it (an `observe` row of the outbox, claimed, leased and resumed like a delegation).

```mermaid
sequenceDiagram
  autonumber
  participant A as Agent (A2A)
  participant D as Dispatcher (crates/app/src/dispatcher.rs)
  participant P as Parent thread
  participant C as Child thread
  A-->>D: the parent's task lists its sessions
  D->>C: the child thread is made, queued, with no event
  D->>P: child_session_opened
  D->>C: job_started with the round (RoundStarted)
  D->>A: resubscribe, else get_task, of the round's task
  A-->>D: steps, text, usage, the round completed
  D->>C: the helper's events, usage with counted false, thread_state done
  D->>P: child_session_reported (and closed when the session is over)
```

```mermaid
stateDiagram-v2
  [*] --> Queued: made with a round to follow (open_child_thread)
  [*] --> Done: made at rest, with nothing to follow (idle or closed)
  Queued --> Working: RoundStarted, job 1
  Queued --> Cancelled: stopped before its round began
  Queued --> Failed: the agent is no longer listed
  Done --> Working: a round starts, job 1 for a child at rest
  Working --> Done: the round completed
  Working --> Blocked: the round waits for the person
  Working --> Failed: the round failed, or the follow failed
  Working --> Cancelled: the round was canceled
  Blocked --> Working: the same round goes on (followed again) or the next round, the next job
  Done --> Working: the next round, the next job
  Failed --> Working: the next round, the next job
  Cancelled --> Working: the next round, the next job
```

::: in-depth A second round, the helper's tokens and deletion

A kept-open session that is sent another message by its parent's model has a round 2: the parent's task says so, and the **same**
child thread starts job 2. The helper's tokens are in the parent's totals, so the child logs its own with `counted: false` and the
usage ledger counts the parent's alone. A child is neither forked nor shared, and deleting its parent deletes it (a child that works
refuses the delete). How each part works is in [Orchestrator: child sessions](orchestrator.md#child-sessions-the-applications-half-slice-s2d).

:::

## How a job flows

**Target design** (MVP steps 3–7; steps 1–2 exist, see [As built](#as-built); the verify and rework part
of it for one agent is [designed in more detail below](#verifying-the-rework-loop-and-attempts)). The built system
delegates a whole thread to one agent; this is the multi-agent flow it grows into.

```mermaid
sequenceDiagram
  actor U as You
  participant CP as Web chat surface (static export + assistant-ui)
  participant O as Orchestrator (Rust, stateless)
  participant DB as Postgres (CNPG)
  participant P as Planner (any A2A agent)
  participant W as Workers (A2A agents, e.g. platform coder)
  participant V as Verifier (CI via webhook, or a verifier agent)
  participant R as Reviewers (A2A agents)
  U->>CP: start job "…" (target agent, optional release)
  CP->>O: POST /agui/agents/{agentId}, RunAgentInput (through the edge)
  O->>DB: thread + first event + outbox row, one txn
  DB-->>O: NOTIFY → dispatcher claims it
  O->>P: A2A task: plan
  P-->>O: subtasks + acceptance criteria
  par each subtask
    O->>W: A2A task (repo, branch, criteria)
    W-->>O: branch pushed
  end
  O->>V: run checks on branches
  V-->>O: fail + findings
  O->>W: rework with findings
  V-->>O: green
  O->>R: review diff
  R-->>O: approve / change requests
  O->>DB: every step appended to the event log
  DB-->>O: NOTIFY (every replica)
  O-->>CP: AG-UI frames on the connect stream → chat renders live
  O-->>U: PR + summary in chat
```

## Job lifecycle

**Target design.** The built states are the subset `queued`, `working`, `blocked`, `done`, `failed`
and `cancelled` ([Thread state](#thread-state)); planning, verifying and reviewing arrive with MVP
steps 3–5.

```mermaid
stateDiagram-v2
  [*] --> Queued
  Queued --> Planning
  Planning --> Working
  Working --> Verifying: branches pushed
  Verifying --> Working: checks fail, attempts < N
  Verifying --> Failed: attempt budget spent
  Verifying --> Reviewing: green
  Reviewing --> Working: change requests
  Reviewing --> AwaitingYou: approved → PR open
  Planning --> Blocked: needs your input
  Working --> Blocked: needs your input
  Blocked --> Planning: answered in chat
  AwaitingYou --> Done: you merge
  Queued --> Cancelled: cancel
  Planning --> Cancelled: cancel
  Working --> Cancelled: cancel
  Failed --> [*]
  Done --> [*]
  Cancelled --> [*]
```

The two backward edges (checks fail → rework, change requests → rework) carry
concrete findings and are bounded by budgets (attempts, wall clock, tokens).
Exhausting a budget ends in `Failed` with the findings in the chat — never a
silent "done". A finished job is not the end of the thread: the next message starts the next job
([Thread state](#thread-state)).

### Verifying, the rework loop and attempts

**Partly built** (designed 2026-09-30; the core, the configuration and the AG-UI projection are built for
the agent's own checks, MVP slices 2 and 3, and the verifier agent is built, slice 10; CI is built, slices 6 and 9,
and the web draws the gate, slices 4 and 8).
[ADR 0018](decisions/0018-verification-gate-and-rework-loop.md) makes the `Verifying` edges above
concrete for the single-agent thread that exists today, and
[ADR 0016](decisions/0016-inbox-timers-and-job-ledger-on-the-thread.md) holds the job ledger they need
(`threads.job`). It differs from the target diagram in three ways: there is no `Reworking` state (a
rework is `Queued` or `Working` with `attempt > 1`); a timeout in `Verifying` goes to `Blocked`, not
`Failed`, and spends no attempt; and `Reviewing` is not part of it (step 5).

```mermaid
sequenceDiagram
  participant A as Agent (A2A)
  participant O as Orchestrator (pure transition)
  participant X as Sources: CI webhook · agent checks · verifier agent
  participant Y as You (chat)
  A-->>O: branch and checks artifacts, then completed
  alt the gate requires nothing (the default)
    O-->>Y: Done, as today
  else the agent pushed nothing (and no earlier attempt did): an answer
    O-->>Y: Done, nothing was verified, no check_result
  else the gate requires sources
    O->>X: Watch CI on the pushed SHA, Schedule the CI deadline, ask the verifier
    O-->>Y: Verifying, attempt 1 of 3
    X-->>O: results
    alt every required source passed
      O-->>Y: Done
    else a source failed and attempts remain
      O->>A: Delegate with the findings (quoted as untrusted), attempt + 1
      O-->>Y: rework, then Verifying again
    else a source failed on the last attempt
      O-->>Y: Failed, with the findings
    end
  end
```

```mermaid
stateDiagram-v2
  [*] --> Queued
  Queued --> Working
  Working --> Done: completed, empty gate
  Working --> Done: completed, nothing pushed (an answer)
  Working --> Verifying: completed, gate requires sources
  Verifying --> Done: all required sources passed
  Verifying --> Queued: failed, attempt < max: rework, attempt + 1
  Verifying --> Failed: failed on the last attempt
  Verifying --> Blocked: CI or verifier timeout, verifier failure
  Verifying --> Queued: a user message (no attempt counted)
  Verifying --> Cancelled: cancel
  Blocked --> Queued: a user message
  Done --> Queued: a user message: the next job, attempt 1
  Failed --> Queued: a user message: the next job, attempt 1
  Cancelled --> Queued: a user message: the next job, attempt 1
```

::: in-depth The gate verifies only pushed work

The gate verifies **only pushed work** ([ADR 0018](decisions/0018-verification-gate-and-rework-loop.md#status-note-2026-10-04-only-pushed-work-is-verified),
2026-10-04, completed 2026-10-07 by [a status note](decisions/0018-verification-gate-and-rework-loop.md#status-note-2026-10-07-pushed-work-is-verified-in-every-attempt-and-a-failure-on-the-base-is-a-note)): an agent that finishes an attempt with no `branch` artifact gave an answer (a question, a demo, a
"hi", an investigation), and the thread is `Done` with no rework and no `check_result`, whatever else it reported, in any job of the
thread. A `branch` artifact the gate cannot use, and a rework of a commit that was pushed, still fail. A failing check that fails
on the base commit too (the agent marks it `preexisting`) does not fail the work.

:::

::: in-depth Sources, attempts and what the chat shows

Three sources can be required, in any combination: CI on the pushed commit (a signed webhook,
[ADR 0017](decisions/0017-ci-results-by-webhook.md)), the agent's own reported checks, and a verifier
agent. The default gate is empty, which is today's behaviour. The attempts are 3 by default, raised no
higher than 10; a target or a thread may add sources, never remove one its target requires. The gate
applies to **each job** of a thread (attempts start again at 1 with a new job), while verifications are counted
per thread ([ADR 0020](decisions/0020-a-thread-is-a-conversation.md)). The chat
shows the pill **Checking the work…** while the gate runs; its checks (with the findings of a failed one), the CI reports and each rework
("Checks failed — trying again (2/3)") are steps in the **Activity** tab of the side panel, and the header has no attempt counter
([`api/agui.md`](api/agui.md) carries the `vymalo.check`, `vymalo.rework` and `vymalo.ci` activities).

:::

## Where it runs

* **netcup** (`kubectl --context admin@netcup`) is the natural home: CNPG runs
  there and `*.sls.servers.segning.pro` resolves to its Traefik.
* Deployed via ArgoCD from `WhyThatFunction/home-os` like everything else: the Applications are declared there, the Helm
  chart is [`deploy/chart`](../deploy/chart/README.md) in this repository and its secrets are `ExternalSecret`s on AWS
  Secrets Manager ([ADR 0041](decisions/0041-deployed-with-helm-on-kubernetes-secrets-by-externalsecret.md)); the first
  deployment is one orchestrator process (`all`), the web, oauth2-proxy and a Caddy edge behind Traefik, with the coder
  deployed by adam-rs's chart. Not run on a cluster yet.
* The orchestrator is one image with a role per Deployment: `control-plane` pods for users and
  `worker` pods for agent work, or `all` in one pod for development and small installs
  ([ADR 0015](decisions/0015-control-plane-and-workers-on-adam-rs.md)).
* The system's own footprint is small: stateless orchestrator replicas, the
  web chat surface (static files behind Caddy), and a Postgres database. Agents run on their hosts.
