# Baselines & Compare

A baseline is a score you have decided to defend. `shipyard baseline save`
freezes a completed run into `baselines/<name>.json`; you commit that file, and
`shipyard compare` turns the difference between a new run and that reference
into a verdict with an exit code CI can trust.

---

## What a baseline is

`baselines/<name>.json` is a schema-stable, review-friendly record of one
completed run:

| Field | Meaning |
|---|---|
| `name` | Baseline name, from `--name`. |
| `created_at` | When the baseline file was written. |
| `created_by` | Who wrote it (`--created-by`, defaults to the local user). |
| `source_run_id` | The rollout the numbers came from. |
| `suite_digest` | Digest of the eval definitions the numbers were produced against. |
| `k` | Repeats per eval in the source run. |
| `verdict` | The aggregate verdict the baseline was saved from. |
| `totals` | Suite totals: `evals`, `passed`, `failed`, `flaked`, `errored`, `mean_score`, `usage`. |
| `evals` | The full per-eval aggregates, including scores and variance. |

Because it is small JSON with no machine-local paths, it belongs in version
control next to the evalset. A baseline is the answer to "what did this suite
score on main, with the agent config that was actually shipped?"

## Saving a baseline

`shipyard baseline save` reads the newest completed run under the target, or a
run you choose:

```bash
# Newest run under the target
shipyard baseline save evalsets/starter-eval --name main

# A specific rollout
shipyard baseline save evalsets/starter-eval --name main \
  --run-id 2026-10-15T08-00-00Z

# From a results.json produced by another job (run/report split)
shipyard baseline save evalsets/starter-eval --name main --results ./results.json

# Attribution, plus the baseline echoed as JSON
shipyard baseline save evalsets/starter-eval --name main \
  --created-by release-bot --json
```

| Flag | Description |
|---|---|
| `--name string` | Baseline name (default `main`). Writes `baselines/<name>.json` under the target. |
| `--run-id string` | Read a specific rollout instead of the newest one. |
| `--results string` | Build the baseline from a `results.json` file instead of local rollouts. |
| `--created-by string` | Record who produced the baseline. |
| `--json` | Print the written baseline document to stdout. |

The file is written to `<target>/baselines/<name>.json`. Commit it:

```bash
git add evalsets/starter-eval/baselines/main.json
git commit -m "chore: refresh main baseline"
```

## Comparing a run against a baseline

```bash
shipyard compare evalsets/starter-eval --baseline baselines/main.json
```

Variants you will actually use:

```bash
# Candidate results from another job instead of local rollouts
shipyard compare evalsets/starter-eval --baseline baselines/main.json \
  --candidate ./results.json

# A specific rollout
shipyard compare evalsets/starter-eval --baseline baselines/main.json \
  --run-id 2026-10-16T09-00-00Z

# Machine-readable output for a bot or a script
shipyard compare evalsets/starter-eval --baseline baselines/main.json --json

# CI: a checked-in gate policy file
shipyard compare evalsets/starter-eval --baseline baselines/main.json \
  --policy .shipyard/gate.toml
```

| Flag | Description |
|---|---|
| `--baseline string` | Path to the baseline file. Required. |
| `--candidate string` | `results.json` to compare instead of local rollouts. |
| `--run-id string` | Compare a specific local rollout. |
| `--json` | Print the delta document to stdout. |
| `--allow-suite-mismatch` | Compare only the evals whose definitions still match, and say so. |
| `--policy string` | Gate policy file to use for this comparison instead of the manifest's `[gate]` block. |

### Per-eval status

Compare classifies every eval in the delta table:

| Status | Meaning |
|---|---|
| `improved` | The candidate rose above the baseline by more than the gate treats as noise. |
| `same` | No change the gate considers real. |
| `regressed` | A fall the gate treats as real, including any pass to fail flip. |
| `flaked` | The candidate's repeats disagreed with each other. |
| `errored` | The candidate failed for infra or rubric reasons, not because the agent scored low. |
| `skipped` | The eval was not compared. For example, its definition changed and `--allow-suite-mismatch` was passed. |

Compare prints the noise margin for each eval, and says in plain words whether
a score change cleared it. The exact blocking rules are in
[Gate policy](/docs/gate-policy); the short version: flips, flakes, infra
errors, and score drops beyond both the percentage budget and the noise margin
block.

### Exit codes

| Code | Meaning |
|---|---|
| `0` | Gate passed. Nothing blocked. |
| `1` | Gate blocked: a regression, a flake, an infra error, or a definition mismatch. |
| `2` | Usage error: missing or unusable inputs (no baseline, no candidate, unreadable file). |

`--json` does not change the exit code: the machine-readable document and the
human verdict agree, and the exit code still reflects the verdict.

## Same-suite binding

A comparison is only meaningful if both sides ran the same test. Compare
enforces this with the binding digest, which covers the eval **definition**:

- `eval.toml`, minus the `[[agent]]`, `[gate]`, and `[redaction]` blocks. The
  agent is the subject of the comparison, not part of the bound suite.
- `instruction.md`.
- `tests/**` (the rubric).
- `environment/**` (the sandbox).
- `evalset.toml`, minus `[gate]` and `repeat` (execution policy, not
  definition).

Excluded from the digest, on purpose:

- `workdir/` workspace fixtures are restored between runs, not graded as part
  of the definition.
- `solutions/`, `rollouts/`, and `baselines/` are not part of the suite.

If a bound file changed, compare refuses with exit `1` and names both the eval
and the changed files. That is the guardrail: you cannot quietly compare a new
rubric against an old baseline.

`--allow-suite-mismatch` compares only the evals whose definitions still match,
prints that fact, and blocks (exit `1`) if none remain. Use it to get signal on
the untouched part of a suite, never to hide a definition change.

## Workflows

### 1. Gate a pull request

```bash
shipyard run evalsets/starter-eval --repeat 3 --json > results.json
shipyard compare evalsets/starter-eval \
  --baseline baselines/main.json --candidate results.json
```

The same steps split across CI jobs work too; the GitHub Action does all of it
in one step (see [CI/CD & Pipelines](/docs/ci-cd-pipelines)).

### 2. Run once, report many times

`shipyard run --report ...` writes the reports at run time. When the run and
the rendering are separate jobs, `shipyard report` re-renders Markdown, JSON,
and JUnit from `results.json` alone:

```bash
shipyard report evalsets/starter-eval --results ./results.json \
  --format markdown,json,junit --out ./shipyard-out \
  --baseline baselines/main.json --baseline-name main
```

The rendering job needs the results file and the baseline, not the rollouts.

### 3. Agent A/B swaps

The `[[agent]]` block is excluded from the binding digest, so swapping the
agent, provider, or model is not a definition change:

```bash
shipyard run evalsets/starter-eval --agent claude-code --repeat 3 --json > candidate-a.json
shipyard run evalsets/starter-eval --agent codex --repeat 3 --json > candidate-b.json

shipyard compare evalsets/starter-eval --baseline baselines/main.json --candidate candidate-a.json
shipyard compare evalsets/starter-eval --baseline baselines/main.json --candidate candidate-b.json
```

`run.json` in each rollout records which agent actually ran, so the comparison
is traceable after the fact (see [Provenance](/docs/provenance)).

### 4. Regenerate the baseline on main only

Treat `baselines/main.json` like a lockfile: refresh it from the default
branch after a merge, never from a pull request. A bot run on `main` produces
the new numbers, `baseline save` writes the file, and the job commits it. PRs
then compare against the committed baseline, or fetch it as an artifact when
the checkout does not have it (the GitHub Action's baseline-from-artifact
fallback).

If you change an instruction, rubric, or environment on purpose, refresh the
baseline in the same change. Until the definitions line up again, every
comparison against the old baseline is refused, which is the point.
