# Runs and completion

> How a run ends, why zelk doesn't take the model's word for it, and what each outcome means.

A model that stops talking hasn't necessarily finished. zelk decides when a
run is done from what the run did, and records why.

## The goal

Every run has a goal. By default it is the request that started the run;
each new request revises it. A host can set an explicit goal that holds
across later messages and compaction (`zelk.SetGoal`), and clear it again.
Only the host sets goals: the model has no tool for it.

A message sent while a run is going, a **steer**, joins the run's next model
turn, and the run can't finish before the model has seen it.

## The report

A run ends when the model calls `ReportOutcome`. It says whether the goal
is `met`, `partial`, `not_met` or `blocked` (and by what), gives a summary
that becomes the run's final message, lists what is still open, and, for a
`met` report, names each requirement and how it was confirmed.

zelk doesn't take the report on trust. The record of the ending is
recomputed from the log, so a report can't claim what the log doesn't show:
which files the run changed, which checks it ran and how they ended, and
which claims in the final message no evidence supports.

## Verification

The `Verify` tool runs a project check and records its evidence: the
command, its exit status, the end of its output, and the state of the
workspace it ran against. The model supplies none of that. A host names its
checks (`-verify 'test=go test ./...'` on the command line,
[`WithVerify`](/reference/go/zelk/#withverify) in Go); the model may add
its own for its run. A command written to hide its own failure, such as one
ending in `|| true`, is refused as evidence. Plain Bash runs are never
evidence.

Evidence goes stale when the workspace changes after the check ran.

## The completion check

When the model ends its turn, the run goes through one check before it
ends. It fails when:

- a check's latest evidence failed, or is stale;
- the final message claims that tests, builds or lint pass, and fresh
  passing evidence doesn't back it;
- there is no report.

A failed check sends the run back to the model with a note saying why, at
most three times per run; after that the run ends `incomplete`. A model
that repeats the same failing action, or retries a call the policy refused,
is told to step back, and then stopped.

When the model ends its turn, the run goes through the completion check, which passes when: no check failed or went stale; claims are backed by fresh passing evidence; there is a report. A failed check sends the run back to the model with a note saying why, at most 3 times; after that the next failure ends the run `incomplete`. Three example runs:

**Claims, no evidence**

1. The model reports “Done. The tests pass.”; the check sends it back (return 1 of 3) with the note “Your message says the tests pass, but no fresh passing check backs it.”
2. The model runs `go test ./...` (exit 0), reports “Done. The tests pass.”; the check passes and the run ends `success`.

**Evidence goes stale**

1. The model runs `go test ./...` (exit 0), edits `pkg/parse.go`, reports “Done. The tests pass.”; the check sends it back (return 1 of 3) with the note “The workspace changed after the latest check, so its evidence is stale. Your message says the tests pass, but no fresh passing check backs it.”
2. The model runs `go test ./...` (exit 0), reports “Done. The tests pass.”; the check passes and the run ends `success`.

**Keeps failing**

1. The model runs `go test ./...` (exit 1), reports “Done. The tests pass.”; the check sends it back (return 1 of 3) with the note “The latest check failed. Your message says the tests pass, but no fresh passing check backs it.”
2. The model edits `pkg/parse.go`, runs `go test ./...` (exit 1), reports “Fixed. The tests pass.”; the check sends it back (return 2 of 3) with the note “The latest check failed. Your message says the tests pass, but no fresh passing check backs it.”
3. The model edits `pkg/parse.go`, runs `go test ./...` (exit 1), reports “Fixed. The tests pass.”; the check sends it back (return 3 of 3) with the note “The latest check failed. Your message says the tests pass, but no fresh passing check backs it.”
4. The model edits `pkg/parse.go`, runs `go test ./...` (exit 1), reports “Fixed. The tests pass.”; the check fails with all 3 returns spent, so the run ends `incomplete`.

## Outcomes

| Outcome | Means |
|---|---|
| `success` | the goal was reported met and the completion check passed |
| `incomplete` | the run ended without meeting the goal, or ran out of chances to |
| `blocked` | the run needs something only a person can give, and none is available |
| `unverified` | done as far as the model can tell, but a requirement remains it had no way to check |
| `max_turns`, `max_budget` | a limit the host set ended the run |
| `interrupted` | the host or the user stopped it |
| `suspended` | the run waits for an answer the host will give later |
| `refused` | the model refused the request |
| `error` | a model call failed, and retries didn't clear it |

The `zelk` command shows the report at the end of a run and exits 0 only for
`success`; `zelk sessions report` lists every action of a run and how it
was decided. In Go, the outcome and report are on
[`zelk.Result`](/reference/go/zelk/#result).

## Where to go deeper

The [completion contract](/internals/completion/) specifies every rule
here: how evidence is labelled, which checks apply to which run, the
shortcut flags, and how decisions are recorded.
