# Repeats & Statistics

One run is a sample. `--repeat` turns a sample into a distribution, so a gate
can compare spreads instead of single numbers.

---

## Why one run is not a verdict

Agents are stochastic. Sampling, tool retries, timing, and context layout all
move the result from run to run. A single passing run proves the task is
possible once, and a single failing run proves it failed once. Neither answers
"did this change make the agent better?"

A prompt or model change that looks better can be a reshuffle of the same
underlying distribution. The only honest fixes are to take more samples and to
require a difference to clear the noise before it counts.

## Running repeats

```bash
shipyard run evalsets/starter-eval --repeat 3
```

Or pin it in the manifest:

```toml
# eval.toml; evalset.toml can set a suite-wide default
repeat = 3
```

`--repeat K` overrides the configured value. `K` must be at least `1`.

Repeats are isolated samples, not continuations. The eval workspace is
restored between repeats and after the run, so repeat 2 starts from the same
initial state as repeat 1 and the next run starts clean. `--keep-workdir`
(implied by `--keep-env`) turns that off and leaves the final state of the
workspace in place for inspection, which is useful for debugging and wrong for
statistics.

Repeats multiply wall-clock time and spend: `--repeat 3` runs the suite three
times.

## pass@k, pass^k, mean and sigma

Each eval in an aggregate carries:

| Metric | Meaning |
|---|---|
| `k` | Number of repeats run for this eval. |
| `pass@k` | The eval passed in at least one of the `k` repeats. |
| `pass^k` | The eval passed in every one of the `k` repeats. |
| `mean_score` | Mean of the repeat scores. |
| `sigma_score` | Standard deviation of the repeat scores. |
| `score_ci95` | 95% confidence interval for the mean score. |
| `flaked` | True when the repeats disagreed (some passed, some did not). |

`pass@k` can only go up as `k` grows, and `pass^k` can only go down. A suite
that reports `pass@3 = 1.00` but `pass^3 = 0.67` is telling you the task is
reachable but not reliable. That gap is the flake, and it is a different
finding from "the agent cannot do this".

## The noise floor

Score changes are judged against a 95% Student-t noise margin (a Welch
t-interval computed over the repeats). The margin applies when either side of
the comparison has `k >= 3`, which is the `min_k_noise_floor` default, and the
confidence level is configurable with `[gate] confidence`.

`shipyard compare` prints the margin for every eval and states in plain words
whether a drop cleared it. A drop blocks only when it is bigger than **both**
the `max_score_drop_pct` budget (default 2%) **and** the noise margin. Inside
the margin, the status is `same` no matter how suspicious the number looks.

### Worked example

Three repeats per side, scores from 0 to 1:

| Metric | Baseline | Candidate A | Candidate B |
|---|---|---|---|
| Repeats | 1.00, 0.95, 0.90 | 0.95, 0.90, 0.85 | 0.60, 0.55, 0.50 |
| `mean_score` | 0.95 | 0.90 | 0.55 |
| `sigma_score` | 0.05 | 0.05 | 0.05 |
| Drop vs baseline | - | 5 points | 40 points |

Candidate A drops 5 points, which is over the 2% budget. But the t-interval
half-width for two 3-sample sets with this spread is about 0.11, so 5 points
is inside the noise margin. The delta table says `same`, and the gate stays
quiet: the data cannot distinguish the candidate from the baseline.

Candidate B drops 40 points, far beyond both the budget and the margin. The
delta table says `regressed`, and the gate blocks. This is the case repeats
exist to make visible: with `k = 1` the same comparison would be one number
against one number, and 0.60 against 1.00 could still be a bad day.

Two more shapes to recognize:

- **Same mean, flaked repeats.** Repeats `1.0, 0.0, 1.0` average 0.67, but the
  interesting fact is that the eval is unstable. The aggregate verdict is
  `flaked`, and flakes block by default (`block_on_flake = true`).
- **A pass to fail flip.** A repeat that passed in the baseline and failed in
  the candidate blocks even when the mean looks close.

## When to use k=1 vs k=3

| Setting | Use it for | Why |
|---|---|---|
| `k = 1` | Inner-loop iteration: drafting prompts, checking that a new eval itself works, fast smoke runs. | Fast and cheap. It is a sample, not a gate. |
| `k = 3` | Pull-request gates and agent comparisons. | With either side at `k >= 3` the t-interval noise margin applies, and flakes become visible. |
| `k > 3` | Suites that flake often, or decisions worth the bill. | The margin tightens and rare failures appear. Cost and time scale linearly with `k`. |

A gate run that reports `flaked` is not broken; it is doing its job. Fix the
variance (pin the model, raise `timeout_s`, make the environment less racy) or
accept the flake explicitly with `block_on_flake = false` in `[gate]` and read
the spread instead.

## Where the numbers live

Per-repeat results are in `rollouts/<run-id>/repeat-<n>/metrics.json`. The
aggregate is `rollouts/<run-id>/aggregate.json`: `k`, counts, `flaked`,
`verdict`, `pass@k`, `pass^k`, `mean_score`, `sigma_score`, `score_ci95`,
`usage`, the per-repeat records, and the `suite_digest`,
`definition_digest`, and `definition_files` that bind the aggregate to the
exact eval definition it measured.

See [Rollouts & ATIF](/docs/rollouts-and-atif) for the file schemas and
[Gate policy](/docs/gate-policy) for how the numbers block or pass.
