# Swarms and runs

> A swarm is a named declaration; a run is one execution of it. Files start a swarm, and the server holds its state.

A **swarm** is the set of agents, topics and limits declared by a folder of `.cvy` files. A **run** is one execution of that swarm on the server. The swarm is the name; the run is what happens.

## Swarms are keyed by name

The label on the `swarm` block is the swarm's name: `swarm "edge_chips"` is `edge_chips` everywhere. `convoy run` sends the files to `PUT /v1/swarms/edge_chips`. Every other command, such as `convoy status edge_chips`, addresses the swarm by that name. Exactly one `swarm` block must exist across all files in the folder.

## One active run at a time

Each `convoy run` starts a new run with the next run number. A swarm has at most one active run, meaning one that is Pending or Running.

- `convoy run` on a swarm with an active run is refused with `swarm_active`. Wait for the run to finish, or [`convoy stop`](/docs/cli/stop/) it.
- `convoy run` on a swarm whose latest run has ended starts run N+1.
- A running swarm's spec never changes. Applying edits to a live run is post-MVP.

## History

Finished runs are kept. `convoy status edge_chips` shows the latest run; `convoy status edge_chips --run 1` shows an earlier one. `--json` prints the same data for scripts. Each run keeps its own spec, spend, agents, outputs and event log, so comparing two runs of the same swarm is a matter of reading two rows.

## Run status and end reasons

A run is Pending until the controller's first pass, then Running, then one of three terminal states. Terminal runs carry an end reason.

| Status | End reason | Meaning |
| --- | --- | --- |
| Done | `done` | `done_when` became true |
| Stopped | `budget` | Spend reached `max_cost_usd`. Outputs already produced are kept |
| Stopped | `max_runtime` | Wall-clock time reached `max_runtime` |
| Stopped | `stopped` | Someone ran `convoy stop` |
| Failed | `failed` | Every agent ended and `done_when` was still false |
| Failed | `server_restart` | `convoyd` restarted while the run was active |

The controller checks these in the order of the table above, so a pass that sees both the budget crossed and `done_when` true ends the run as `budget`. When a run ends, every agent still alive is stopped. See [budgets and limits](/docs/concepts/budgets/).

`done_when` is evaluated whenever an agent it references finishes, and the server also re-evaluates it on every controller pass. A `done_when` that cannot be evaluated yet does not end the run.

Durability is . In the MVP a runner that exits without calling `finish` marks its agent Failed, and a `convoyd` restart marks every active run Failed with reason `server_restart`. Everything recovery needs is stored from the start, so adding it later changes behavior, not the schema.

## Where state lives

The files start the swarm. After `convoy run`, `convoyd` holds the state: the stored spec, every agent's status, every topic and message, spend, and the event log, in a SQLite file (`--db`). Three consequences follow.

- The CLI is stateless. It exits after `convoy run`, and the swarm keeps going. `convoy watch` and `convoy say` attach to the server, not to your files.
- Editing `main.cvy` after `convoy run` changes nothing about the running swarm.
- Agents created at runtime by the overseer or by an agent with `can_spawn` exist only in the server. They are not written back to your files. Exporting a live swarm as `.cvy` is post-MVP.

For the HTTP surface behind these commands see [the HTTP API](/docs/architecture/api/).
