# Test result classification

A red test says something about the change only when the base was green and
the test stayed red when run again. This step measures that per test instead of
reading one run's exit code, and the gates act on the measured class.

Used at the Phase 2 exit gate (Gate 3), on the Phase 3 rework re-entry, and by
the classify stage of `/multi-agent:test-gen`.

## Classes

| Class | Means | Gate |
|---|---|---|
| `passed` | green | pass |
| `regression` | green on the base, red in two runs of the head | blocks: fix it |
| `preexisting` | red on the base too, or listed failing in the Phase 0 baseline | inherited: does not block, is reported |
| `unstable` | runs disagree, or the test was unstable in two or more separate runs over the last 7 days | blocks: never green. Log `test.flake_signal` and fix the test or the code |
| `not_measured` | no base to compare, or a setup failure before the test ran; carries a `reason` | blocks: stays red, the reason is reported |

## Inputs

| Input | Source |
|---|---|
| `head` | the Gate 3 run's report: JUnit XML, the xcresult bundle, or Playwright JSON. UI tests: `UI_TEST_RESULT` from `run-ui-tests.sh run` (`UI_TEST_RESULT_FORMAT` names the format) |
| `confirm` | a second run of the red tests: one of the `testStability.repeatCount` repeats Phase 2 already ran |
| `base` | the Phase 0 baseline's report when it captured one (`state.baseline.tests.logPath`) |
| `flake_ledger` | `$HOME/.claude/knowledge/<project>/flake-ledger.jsonl`, the project's history of measured unstable results. The toolkit counts only runs that turned green on confirm, never its own earlier `unstable` verdicts, so one flaky result cannot exempt a test for good |

## With the toolkit (`test_results_classify`)

Needs `@mmerterden/multi-agent-mcp` >= v3.25.0. When the tool is
listed, call `mcp__multi-agent__test_results_classify` with
`{head, base, confirm, flake_ledger, format}`, save its answer with the Write
tool as `$WORKTREE/.pipeline/test-classified.json`, then:

```bash
node $HOME/.claude/scripts/test-classify.mjs merge \
  --classified "$WORKTREE/.pipeline/test-classified.json" \
  --state "$STATE_FILE" --out "$WORKTREE/.pipeline/test-classification.json"
```

`merge` applies the pipeline's rules on top of the toolkit's answer: `unstable`
is never green; the Phase 0 baseline joins the preexisting set (a listed test the
toolkit could not measure becomes `preexisting`; a listed test the toolkit calls
a `regression` becomes `unstable`, because the base disagreed with itself); a
tripped breaker is not green; nothing classified is not green.

## Without it (older toolkit, or no server)

Say so once in the run log. When the repeats wrote JUnit XML, classify from them
alone:

```bash
node $HOME/.claude/scripts/test-classify.mjs fallback \
  --run "$WORKTREE/.pipeline/repeat-1.junit.xml" --run "$WORKTREE/.pipeline/repeat-2.junit.xml" \
  --state "$STATE_FILE" --out "$WORKTREE/.pipeline/test-classification.json"
```

Runs that disagree, and a run that saw one test both fail and pass (a retry,
reports from several devices), are `unstable`; red in every run is `not_measured` (there is
no base run), green in every run is `passed`. With no per-test report at all
(an xcodebuild log only), Gate 3 keeps its log-and-baseline rule and logs
`test.classify_skipped reason=<why>`.

## Verdict

| Exit | Gate 3 |
|---|---|
| 0 | pass. `inherited[]` is what the "red + `failing[]`" row subtracts; log `test:pass (inherited {N})` when it is not empty |
| 1 | fail. `blocking[]` names the tests and `tests[].reason` says why. A tripped `breaker` is one environment failure seen across most runs: report its signature; autopilot parks the run as `verification-failed` rather than reworking code that is not at fault |
| 2 | call error, never a pass |
| 3 | the classification could not be read: Gate 3 keeps its log-and-baseline rule, and the reason is logged |
| 4 | the script crashed: a defect to report, never a red verdict; Gate 3 keeps its log-and-baseline rule |

Script: `scripts/test-classify.mjs`. The toolkit tool and its minimum version
are listed with the other end-to-end testing tools in the cross-CLI contract.
