# Feature: Root cause before the fix (bugfix tasks)

**Applies to** every Phase 2 task of a run whose `taskType` is `bugfix`, and to
every rework of one. A symptom patch passes its test and leaves the cause in
place; the record below makes the cause a thing written down and checked
before any production line changes.

## 1. The record

`<worktree>/.pipeline/root-cause.json`, written with the Write tool before the
first fix:

```json
{
  "symptom": "what fails, as the user or the test sees it",
  "hypothesis": "the cause, as one falsifiable sentence",
  "origin": "src/money.js:2",
  "evidence": [
    { "kind": "test", "ref": ".pipeline/red.log:2", "quote": "NaN !== 0" },
    { "kind": "read", "ref": "src/money.js:2", "quote": "Number.parseFloat" }
  ],
  "rejected": ["hypotheses ruled out, each with what ruled it out"],
  "attempts": []
}
```

- `origin` is where the cause is, not where the symptom shows. Trace back
  from the failing line through each caller until the value or state first
  goes wrong; the frames in between go in the hypothesis.
- `evidence` holds at least one observation: a failing test's output, a log,
  a stack trace (`kind` `test` | `log` | `trace`), saved under `.pipeline/`
  and cited by `path:line` with the text on that line. A `read` of the code
  backs the claim but is not enough alone.
- Instrument before guessing: a log line or an assertion at the suspected
  boundary, run once, beats a second hypothesis.

```bash
node $HOME/.claude/scripts/root-cause-gate.mjs check \
  --record "$WORKTREE/.pipeline/root-cause.json" --repo "$WORKTREE" --state "$STATE_FILE"
```

Exit 1 lists what does not resolve. No production edit before it exits 0.
The RED test of the TDD cycle is the test that reproduces the symptom; when
it exists before the record, its output is the `test` evidence.

## 2. Attempts and the stop

After each fix attempt, with the outcome of the reproducing test:

```bash
node $HOME/.claude/scripts/root-cause-gate.mjs attempt \
  --record "$WORKTREE/.pipeline/root-cause.json" --repo "$WORKTREE" \
  --outcome red|green --hypothesis "<what this attempt assumed>" --state "$STATE_FILE"
```

An attempt is the diff against HEAD; recording the same diff twice counts
once. A red attempt is a hypothesis that failed: before the next one, move it
to `rejected` with what the run showed, and write the new hypothesis.

**The third red attempt in a row (since the last green one) exits 4, and a fourth is refused.** Three
fixes that each failed are a sign the cause is in the structure (a shared
mutable state, a wrong ownership boundary, a contract two callers read
differently), not in the line being patched. Do not try again:

- **Attended**: stop and ask one `AskUserQuestion` per `$HOME/.claude/multi-agent-refs/picker-contract.md`, `header` "Root cause",
  the question in `outputLanguage`: the three hypotheses that failed, what
  each showed, and the design question they point at (which boundary, which
  owner, which contract). Options: **Rethink** (the person names the design
  change; their answer is the `--decision`) and **Stop** (leave the task
  parked, work kept on the branch).
- **Unattended**: the gate parks the run itself on `pendingQuestion`
  `root-cause-escalation` (stepId `phase-2/root-cause`, options `rethink` and
  `stop`). Resume carries the answer back to this step.

On **Rethink**:

```bash
node $HOME/.claude/scripts/root-cause-gate.mjs reset \
  --record "$WORKTREE/.pipeline/root-cause.json" --decision "<the design change agreed>"
```

The round is archived with the decision, the hypothesis is cleared, and
`check` must pass again on a new one before the next attempt.

## 3. At commit

`state.rootCause` is written on every `check`, `attempt` and `reset`. For a
`bugfix` run the gate ledger refuses the commit while it is missing, its
check has not passed, or it is escalated: the unattended commit check
(`gate-ledger.mjs check`) lists it as failing `root-cause`, and the attended
pre-commit check names it beside the build and test evidence.

## 4. What the gate cannot know

It proves that the origin and every cited line exist and that the evidence
includes an observation. Whether the hypothesis explains the evidence is
judgement, and so is whether the fix reaches `origin`: a diff whose changed
lines never touch the origin file says why in the record's `hypothesis`.
