# How a run works

Source: https://www.zerd.si/docs/how-a-run-works

The model writes the code, but zerd’s own code decides the rest: which model to use, which checks to run, and whether the work counts as finished. The run below is an example put together to show the flow. Each status line is a message zerd prints, and the last two lines are a summary, not the exact format.

```text
> change unique_by() to take a key function and drop the seen= argument

  Routing to the strong model (read by the fast model): claude-opus-5-5
  Verifying: python3 -m pytest -q -x tests/test_unique.py
  Verification failed (python3 -m pytest ...); repair attempt 1/2
  Verifying: python3 -m pytest -q -x tests/test_unique.py
  The change breaks 1 public signature(s) (utils.py: unique_by() no longer accepts `seen`); asking to keep old calls working
  Verifying: python3 -m pytest -q -x tests/test_unique.py
  Independent review of the diff...
  Reviewer found issues; sending them back for a fix
  Independent review of the diff...

  status: verified   checks: 1 passed   review: clean   citations: 4 checked, 0 unverified
  Rounds: 9 used, 10 estimated, +10 points (within the target)
```

Code makes every decision in that run: which checks to run, whether a failure is new or was already there, whether a public signature broke, and whether the review goes again.

## The path of a request

```mermaid
flowchart LR
    A([Your request]) --> B{"Classify<br/>no model call"}
    B -->|small| F[fast model]
    B -->|ordinary| D[default model]
    B -->|hard| S[strong model]
    F & D & S --> L["Agent loop<br/>stream, run tools, repair"]
    L -->|only reads| ANS
    L -->|edits files| V{"Checks on the<br/>changed files"}
    V -->|fail, new| R1["Repair round<br/>max 2"]
    R1 --> V
    V -->|fail, already broken| NOTE["Leave it, report it,<br/>end as unverified"]
    V -->|pass| SG{"Signature<br/>guard"}
    SG -->|public API broke| R1
    SG -->|ok| RV{"Independent review<br/>if enabled or asked"}
    RV -->|findings| R2["One fix,<br/>review again"]
    R2 --> G
    RV -->|clean| G["Citation check<br/>paths and lines on disk"]
    NOTE --> G
    G --> ANS([Answer + honest status])
    L -. loop detected .-> S
    R1 -. still failing .-> S
```

1. Classify. Rules pick a model size for the request without a model call, and say why.
2. Work. The agent loop reads, edits and runs tools.
3. Check. If files changed, checks run on those files only. New failures go back for repair, up to 2 rounds.
4. Guard. A broken public signature sends the agent back once.
5. Review. An independent review of the diff runs if you asked for it, turned on `auto_review`, or the change touches a path where a mistake is expensive (`src/auth/`, `db/migrations/`, `billing/`). Tests and docs don't count, and `review_risky = false` turns this off.
6. Cite. Every file and line in the answer is looked up on disk.

## The five endings

Every run ends in exactly one of these states.

| Status | Meaning |
|---|---|
| `verified` | Files changed and the checks passed. |
| `unverified` | No checks exist for this change, or a check was already failing before the run. |
| `verification_failed` | Checks still fail after the repair rounds. |
| `failed` | An API error, a loop, or the deadline. |
| `done_no_changes` | Nothing was edited, for example you asked a question. |

Each request is a task with a fixed lifecycle. The code checks every transition, so a run can't jump from `failed` to `verified`, and every transition is written to a trace you can read later.

```mermaid
stateDiagram-v2
    [*] --> running
    running --> verifying: files changed
    running --> done_no_changes: nothing edited
    running --> unverified: no checks exist
    running --> failed: API error, loop, deadline
    verifying --> verified: checks pass
    verifying --> repairing: checks or review failed
    verifying --> unverified: a check was already failing
    verifying --> verification_failed: still failing after repairs
    repairing --> verifying: fix applied
    repairing --> failed
    verified --> [*]
    unverified --> [*]
    verification_failed --> [*]
    done_no_changes --> [*]
    failed --> [*]
```

## How the pieces fit

Everything the model can do goes through one door, the tool registry, and everything zerd decides is traced.

```mermaid
flowchart TB
    subgraph UI["Front end"]
        TUI["Full-screen terminal UI"]
        CLI["One-shot, --json, --rpc"]
    end
    subgraph CORE["Agent core"]
        LOOP["Agent loop"]
        TASK["Task state machine"]
        VER["Verify + signature guard"]
        REV["Review pipeline"]
        GRD["Grounding: citation check"]
        LRN["Learning + ledger"]
    end
    subgraph CTX["Context"]
        MGR["Prune, compact, restore"]
        ARC[("Result archive")]
        GRAPH["Symbol + import graph"]
    end
    subgraph TOOLS["Tools"]
        REG["Registry: the single door"]
        BI["read, edit, search, git, shell, advisor"]
        MCP["MCP servers, project tools"]
    end
    subgraph SAFE["Safety"]
        GD["Guard rules"] --> WS["Workspace confinement"] --> AP["Approval"] --> SB["OS sandbox"]
    end
    subgraph LLM["Client"]
        RT["Routing + failover"] --> AD["Provider adapters"]
    end
    MEM[("Per-repo memory")]
    TR[("Trace JSONL")]
    UI --> LOOP
    LOOP <--> TASK
    LOOP --> REG --> GD
    REG --> BI
    REG --> MCP
    BI --> GRAPH
    LOOP <--> MGR <--> ARC
    LOOP --> RT
    LOOP --> VER --> REV --> GRD
    MEM -.feeds.-> LOOP
    LRN -.writes.-> MEM
    CORE -.-> TR
    LLM -.-> TR
```

## Go deeper

- [Verification](/docs/verification): how checks are picked and how zerd knows whose failure it is.
- [Reviews](/docs/reviews): the review and fix pipelines.
- [Safety](/docs/safety): the layers every tool call passes through.
