# Feature: Plan Todos Iteration (Phase 3)

**Gated by `prefs.global.planTodos.enabled`** (default: `false`). When enabled and Phase 1 Step 11 emitted a `plan.todos[]` (conforming to `$HOME/.claude/schemas/plan-todos.schema.json`), Phase 2 iterates with the helper instead of walking `tasks[]` directly:

```bash
while next=$(bash "$HOME/.claude/lib/plan-todos.sh" next "$TASK_ID"); [ -n "$next" ]; do
  id=$(printf '%s' "$next" | cut -f1)
  task=$(printf '%s' "$next" | cut -f2)
  bash "$HOME/.claude/lib/plan-todos.sh" start "$TASK_ID" "$id"
  # ... RED: write the failing test, run it, keep the JUnit XML / .xcresult / log ...
  bash "$HOME/.claude/lib/plan-todos.sh" tdd-red "$TASK_ID" "$id" "$RED_RESULTS" --tests "$TEST_NAME"
  # ... GREEN: implement, run the suite ...
  bash "$HOME/.claude/lib/plan-todos.sh" tdd-green "$TASK_ID" "$id" "$GREEN_RESULTS"
  if build_passed; then
    bash "$HOME/.claude/lib/plan-todos.sh" complete "$TASK_ID" "$id" "${COMMIT_HASH} · tests:${TEST_COUNT}"
    git -C "$WORKTREE" diff --name-only HEAD~1 HEAD \
      | bash "$HOME/.claude/lib/plan-todos.sh" drift "$TASK_ID" "$id" --files - --not-done "$NOT_DONE"
  else
    bash "$HOME/.claude/lib/plan-todos.sh" fail "$TASK_ID" "$id" "build failed after 3 retries"
    break
  fi
done
```

When disabled, the loop still walks `tasks[]` from `planning-output`  -  `plan-todos.sh` adds visibility (status, notes, deps-respecting `next`) but the underlying TDD contract is unchanged.

## Drift propagation

A step that ends off-plan invalidates the plan of every step built on it. `drift` takes the same two signals the scope self-check records for the whole run (`features/scope-check.md`), per step: files the step changed that its `files[]` does not list, and planned items it did not do (`--not-done N`, the entries that go to `scope-check.json` `notDone[]`). `--reason <text>` records drift the two counts do not show, such as an interface whose shape changed.

On drift the step gets `drift: {outside, notDone, at}` and every **pending** step that depends on it, directly or transitively, gets `needsReplan: {because, at, reason}`; steps that already started or finished keep their state, and steps on other branches of the graph are untouched. Output: `{drifted, outside, notDone, flagged}`.

A flagged step:

- is reported by `next` as waiting (`task-graph.mjs` `reason: "needs-replan"`), and named on stderr when nothing else is ready;
- is refused by `start` and `complete` (exit 2), which name the drifted step;
- starts again after `plan-todos.sh confirm-replan "$TASK_ID" <todo-id> [note]`, run once its plan has been re-read against what the drifted step actually did (interactive: ask; autopilot: confirm when the drifted files are not in the flagged step's `files[]` and none of its `files[]` were deleted, otherwise stop the loop and log `plan.replan_needed={ids}`).

A later drift replaces the flag and its confirmation. `files[]` and `creates[]` come from the planning-output task through `set`, and both count as planned; paths are normalised on both sides (`./a`, `a` and `a//b/..` compare equal). A changed test file is planned when its name covers the stem of a planned file: `a.test.ts`, `a.spec.ts`, `a_test.go`, `ATests.swift`, `ATest.kt` and `test_a.py` cover `a.ts` / `A.swift` / `a.py` in any directory. A step with neither list is judged on `--not-done` and `--reason` only.

## TDD evidence

`tdd-red` and `tdd-green` read the runner's own results (`lib/tdd-evidence.mjs`): JUnit XML file or directory (Gradle, Surefire, Jest/Vitest junit, `pytest --junitxml`), an `.xcresult` bundle via `xcrun xcresulttool`, that summary exported to JSON, or the xcodebuild / `swift test` log. They write `plan.todos[].tdd`.

| Call | Holds when | Refused (exit 2) when |
|------|-----------|------------------------|
| `tdd-red <task> <todo> <results> [--tests a,b]` | at least one test fails; with `--tests`, one of the failing tests is a named one | nothing failed, nothing ran, or every failure is in another test; under `enforce`, `--tests` on results that name no failing test (use JUnit XML); `warn` records that run with `red.testsUnverifiable` and a warning |
| `tdd-red ... --already-passing <reason>` | the run has no failure: the behaviour already exists; the run stands for RED and GREEN, the reason is kept | any test fails |
| `tdd-green <task> <todo> <results>` | RED is recorded (not needed under test policy `tests-after`), the run has no failure, and it is the RED suite: RED's named tests pass in it, or, without names on either side, it ran at least as many tests | a test still fails, nothing ran, no RED, a RED test is missing, fewer tests than RED |
| `tdd-green ... --test-edit-reason <why>` | as above, when a test file RED kept was changed because the test itself was wrong; the edit and the reason go to `green.testEdits[]` and Phase 3 reviews them | the reason is missing |
| `tdd-restore <task> <todo>` | writes the RED bytes of every kept test file that changed back into the worktree | a kept path leaves the worktree |

Both refuse results written before the todo's `startedAt`.

`tdd-red` keeps the test files the run wrote (committed since the run base, `state.baseSha` or `MA_RUN_BASE_SHA`, plus new and changed ones in the worktree) as `red.testFiles[]` ({path, blob}; the bytes go into the repository's object store). `tdd-green` refuses (exit 2) when one of them was modified or deleted since: a suite made green by weakening its own new test proves nothing. Restore it with `tdd-restore`, or give `--test-edit-reason`. `test-integrity-gate.mjs --state` reports a recorded edit as important (blocking in an unattended run, where nobody read the reason) and an unrecorded one as blocking.

`complete` then reads `prefs.global.tddEvidence.mode`:

- `off`: no check.
- `warn` (default): a todo without both runs completes with a warning and `tdd.outcome: "unverified"`; Phase 3 notes it.
- `enforce`: `complete` refuses (exit 2) and the todo stays open.

Never blocking, always recorded with `tdd.outcome: "exempt"` and a reason: a `docs` or `chore` todo (type carried from `planning-output`), a `component` task (the component skill short-circuits the TDD path), project test policy `none`, and `complete ... --no-tests "<reason>"` for a repo with no test target. The last one is the todo's own claim, so it is stored with `tdd.selfDeclared: true` and Phase 3 notes it next to the unverified todos.
