# convoyd

> convoyd is the Go server that holds every swarm, run, agent, topic and message. It is the only way anything changes, and it starts one runner process per agent.

`convoyd` is a single Go binary. It stores the state of every swarm, serves the HTTP API the CLI and the runners call, runs the controller loop, hosts the message board, and starts and stops the runner processes. The `convoy` CLI never holds state; if you close your terminal, the swarm keeps running here.

## The parts

| Part | What it does | Page |
| --- | --- | --- |
| API server | REST, one SSE stream per run, and an MCP endpoint. One bearer token per agent. | [HTTP API](/docs/architecture/api/) |
| Web UI | A browser view of every swarm, served at `/` on the same port. | [Web UI](/docs/runtime/web-ui/) |
| Message board | Topics as append-only logs, one read cursor per agent per topic, the `wake` long poll. | [Message board](/docs/runtime/board/) |
| Controller | A single loop that starts agents, flags stuck ones, enforces limits and the budget, and evaluates `done_when`. | [Controller](/docs/runtime/controller/) |
| Overseer | A built-in agent started with every run. A runner with a fixed prompt. | [The overseer](/docs/concepts/overseer/) |
| State store | SQLite in WAL mode: specs, status, the event log, messages, tokens. | [Storage](#storage) |
| Process backend | Creates a work folder and runs the runner in it. Local processes in the MVP. | [Seams and contracts](/docs/architecture/contracts/#process-backend) |

## Process model

One `convoyd` process serves every swarm on the machine. For each active run it starts one runner process per agent, the overseer included. A runner is a Python process, almost always blocked in a `wake` call. The example `edge_chips` swarm is eight runner processes on one laptop.

- Each agent gets its own folder, `.convoy/work/<swarm>/<run>/<agent>/`, under `--work-dir`. Brackets in an instance name are replaced with `__` on disk.
- The runner reaches convoyd over HTTP only. It calls the REST API itself and the model calls the MCP tools, both with the agent's bearer token. See [Runner](/docs/runtime/runner/).
- `--max-processes` (default 50) caps live runner processes across all runs, overseers included. An agent over the cap stays `pending` until a slot frees.
- Provider API keys are read from convoyd's own environment when it starts a runner and handed to that runner only. They are never stored, never written to the bundle, and never returned by any endpoint.

## Storage

The MVP ships one storage backend: SQLite through a pure-Go driver, in WAL mode, in one file (`--db`). No cgo, one binary, enough for one laptop and dozens of agents. Everything above storage depends on a small `store.Store` interface, so other backends stay possible.

The store's contract:

- Transactions are serializable. Every write goes through `Tx`, so a status change and its event row are atomic.
- `seq` values for events and messages are strictly increasing per run with no gaps visible to readers. SSE replay with `after=` depends on this.
- Run and agent status updates are compare-and-set: they apply only if the current status is the expected one.
- A turn's row, the agent's spend, and the run's spend update in one transaction, so a reader never sees a turn without its cost.

A **swarm** is a name. A **run** is one execution of that swarm from one bundle, with its own integer id and a number (1, 2, 3 within the swarm). Agents, turns, events, messages, topics and tokens each belong to exactly one run. Every run has its own board; a new run starts with an empty one. Earlier runs stay readable with `?run=N`.

| Table | Holds |
| --- | --- |
| `swarms`, `runs` | Names; per run: status, goal, the stored bundle and its hash, limits, spend, `unpriced_turns`, the resolved overseer, the provider type version and digest each provider block resolved to (`providers`), `end_reason`, `end_detail` |
| `providers` | The [provider types](/docs/providers/) convoyd holds: source, version, digest, kind, origin, and the compiled manifest. Built-in types have no rows |
| `agents` | Status, resolved spec, parent and depth, counters, spend, `sdk_session_id`, `last_response_id`, `process_id`, output, progress timestamps |
| `turns` | Per turn: model, the four token counts, `cost_usd` at the agent's price, the SDK's own `sdk_cost_usd` |
| `progress_reports` | Every `report_progress` summary |
| `events` | The event log |
| `tokens` | Agent bearer tokens, stored as a sha256 hash |
| `transcript_items` | Each agent's transcript: its prompt, inputs, text, reasoning, tool calls and tool results. See [Web UI](/docs/runtime/web-ui/#transcripts) |
| `topics`, `messages`, `watches`, `read_cursors`, `wake_state`, `run_counters` | The board, see [Message board](/docs/runtime/board/#storage) |

### Event log

Every write that changes an agent's or a run's status also inserts an event in the same transaction. The controller and the SSE broadcaster both consume the `events` table, so the log is the single source of ordering. `convoy watch` is that log, streamed.

| Type | Payload |
| --- | --- |
| `run.created`, `run.started`, `run.ended` | `run`, `status`, `end_reason`, `end_detail`, `spend_usd` |
| `agent.created` | `parent`, `declared`, `agent`, `depth`, provider, SDK, provider type `source` and `version`, model, tools, writes, watches, `can_spawn`, `max_turns`, `role_summary`, `input_deps`. Emitted for every agent, declared ones included |
| `agent.started` | `process_id` |
| `agent.session` | `sdk_session_id`, `last_response_id` (when first reported or changed) |
| `agent.turn_started` | `turn` |
| `agent.turn_ended` | `turn`, the four token counts, `cost_usd`, `run_spend_usd` |
| `agent.progress` | `seq`, `summary` |
| `agent.finished` | `output` |
| `agent.exited` | `exit_code`, `signal` |
| `agent.stuck`, `agent.unstuck`, `agent.stopped`, `agent.failed` | `reason` |
| `agent.log` | `chunk` (4 KB; streamed over SSE only with `logs=1`) |
| `message.published` | `seq`, `topic`, `to`, `reply_to`, `body` |
| `topic.created` | `name`, `description`, `declared`, `builtin`. Emitted for every topic, declared and built-in ones included |
| `topic.read` | `topic`, `cursor`, `read`: an agent's read cursor moved. At most one per agent and topic every 2 seconds |
| `agent.watches` | `watches`: the agent's watches after `watch_topic` changed them |
| `agent.tool_called` | `turn`, `idx`, `tool`: one per tool call in the agent's transcript |
| `done_when.error` | `diagnostic` |

The controller also emits `agent.blocked` and `agent.stop_requested`; see [Controller](/docs/runtime/controller/).

A run's log starts with `run.created`, then a `topic.created` for every topic and an `agent.created` for the overseer and every declared agent, so the log alone rebuilds the run's graph. `topic.read`, `agent.watches` and `agent.tool_called` exist for the [Web UI](/docs/runtime/web-ui/); `convoy watch` ignores them.

### Pricing

Prices come from provider types: each type's manifest lists its models with USD per million input, output, cache-read and cache-write tokens, and a provider's `price` attribute overrides them for that provider's models. The price is resolved once, into each agent's spec, when the agent is created. The server prices each turn from the runner's token counts: `cost_usd = tokens_in * in_price + tokens_out * out_price` (plus cache terms). The SDK's own figure is kept in `sdk_cost_usd` for comparison and is never used for the budget, so every SDK is charged by one rule.

A model missing from the table is priced as unknown: `cost_usd` is NULL and `unpriced_turns` goes up. `convoy validate` rejects a swarm that sets `max_cost_usd` and uses an unlisted model, and the server repeats the check on `PUT` against its own table, so a run with a budget never has unpriced turns.

## Authentication

Middleware runs on every `/v1/` route.

1. Read `Authorization: Bearer <token>`. If absent, the request comes from a loopback address and `--token` is empty, the caller is the CLI (`human`) and needs no token. The CLI token, when set, maps to the same identity. A browser page also connects from loopback, so tokenless requests are refused when `Host` is not `localhost` or a loopback IP (`bad_host`), or when `Origin` or `Sec-Fetch-Site` marks them cross-origin (`cross_origin`). Requests with a token skip these checks.
2. Otherwise `sha256(token)` is looked up in `tokens`. Missing or revoked is 401.
3. Routes under `/v1/swarms/{name}/...` return 403 when the token's run is not the latest run of `{name}`. A runner token stops working when it is revoked or a new run of the swarm starts.
4. Agent-only operations (`report_progress`, `finish`, `wake`, `watches`, and appending to its transcript) require the token's agent to match the agent in the path. The CLI may call `stop`, `say` and the read endpoints. The swarm list, the graph snapshot and transcript reads are for people only: a runner token gets 403 `people_only`.
5. `create_agent` from a runner records the caller as parent and requires its `can_spawn`. `stop_agent` from a runner must target an agent the caller created.

Tokens are 32 random bytes, base64url, minted when the controller starts a runner and revoked when that runner process exits. A finished or stopped agent's runner can still report its last turn's usage and the end of its transcript while it shuts down, even after the run has ended. The sender of every message and the creator of every agent come from the token, never from the request body.

## Flags

Every flag has an environment fallback where one is listed.

| Flag | Env | Default | Meaning |
| --- | --- | --- | --- |
| `--port` | `CONVOYD_PORT` | `7070` | Listen port |
| `--db` | `CONVOYD_DB` | `./convoy.db` | SQLite file |
| `--work-dir` | `CONVOYD_WORK` | `./.convoy/work` | Root of agent work folders |
| `--runner` | `CONVOYD_RUNNER` | the runner built into `convoyd`, started with `uv` | A runner executable of your own to use instead |
| `--token` | `CONVOYD_TOKEN` | empty | The CLI token. Empty means loopback requests need none |
| `--public-url` | `CONVOYD_PUBLIC_URL` | `http://127.0.0.1:<port>` | Address runners use to reach convoyd. Required by a backend whose processes run on another host |
| `--max-processes` | `CONVOYD_MAX_PROCESSES` | `50` | Most runner processes alive at once, across all runs |
| `--no-ui` | `CONVOYD_NO_UI` (`1` or `true`) | off | Do not serve the [Web UI](/docs/runtime/web-ui/) at `/`. The API is unchanged |
| `--stuck-after` | | `20m` | Time without progress before a running agent is marked stuck |
| `--debounce` | | `5s` | Quiet period before a message gives an agent a turn |
| `--tick` | | `5s` | Controller pass interval |
| `--stop-grace` | | `5s` | How long a stopping agent has to exit |
| `--kill-grace` | | `3s` | SIGTERM to SIGKILL |
| `--overseer-interval` | | `5m` | Heartbeat to the overseer |
| `--wake-timeout` | | `30s` | Length of one `wake` long poll |
| `--provider-dir` | `CONVOYD_PROVIDER_DIR` | `./.convoy/providers` | Where [driver plugin](/docs/providers/) packages are kept, as `<source>/<version>/`. Presets keep nothing on disk |
| `--provider-timeout` | | `5m` | How long a request waits for a provider fetch or check. A fetch continues after the request gives up; a check is stopped |
| `--overseer-provider`, `--overseer-model`, `--overseer-base-url`, `--overseer-api-key-env` | | none | Server-wide default overseer, used only when the swarm declares no `overseer`. `--overseer-provider` is a built-in type; the base URL and key variable set its `base_url` and `api_key`. Optional |
| `--overseer-can-spawn` | | `true` | `can_spawn` for the default overseer |

Fetching provider types needs `git` on convoyd's `PATH`, and a Python driver plugin needs `uv` on the runner's `PATH`. convoyd fetches a type the first time a swarm names a version it does not hold; there is no setting for it. See [Provider types](/docs/providers/).

convoyd starts without any overseer flags. A run whose swarm declares no `overseer`, on a server with no default, is rejected with `overseer_missing`.

`convoyd gc` removes the work folders and boards of ended runs. `SIGTERM` stops accepting requests, sends stop to every running agent, waits up to 10 seconds, kills what remains, marks every active run `failed` with `end_reason = server_restart`, and exits.

In the MVP, restarting convoyd ends every active run. On start it marks each non-terminal agent and run `failed` with `end_reason = server_restart` and kills leftover runner processes. See [Durability](/docs/runtime/durability/).

## Logging

convoyd logs JSON lines to stderr with `log/slog`. At start it logs the address it listens on and, unless `--no-ui` is set, the Web UI's address (`{"msg":"web ui","ui":"http://127.0.0.1:7070/"}`). Runner stderr is stored separately as `agent.log` events.
