# Feature: Scope self-check (Phase 2 Step 3.7)

**Pattern**: Phase 3 reconstructs everything from the diff. The one thing it cannot reconstruct is why each file was touched and what was left out on purpose, so Dev states both before the handoff, and a deterministic gate checks the statement against the real diff. The record also carries the code-simplifier rationales (Step 3.6) that would otherwise be discarded, and it feeds two later consumers: the `<scope-self-check>` block in the Phase 3 reviewer prefix and the PR body in Phase 4.

## The record

`$WORKTREE/.pipeline/scope-check.json`, schema `schemas/scope-check.schema.json`:

```json
{
  "version": "1.0.0",
  "taskId": "<taskId>",
  "files": [{ "path": "<repo-relative path>", "reason": "<why this task needs this exact file>" }],
  "notDone": [{ "what": "<change considered and not made>", "why": "out-of-scope | follow-up | rejected-abstraction" }],
  "simplifier": { "applied": 0, "skipped": 0, "rationales": ["<Step 3.6 rationale strings>"] }
}
```

Rules:

- One entry per file in the diff, no entry for a file outside it. A reason names the task requirement, never "while I was here".
- `notDone` carries every hypothetical the dev chose not to defend against and every abstraction it considered and rejected, so review does not re-propose them.
- Component tasks (`taskType === "component"`) write the record too; the plugin skill's file table is the `files[]` source.

## The gate

```bash
git -C "$WORKTREE" diff --name-only "origin/$BASE_BRANCH"...HEAD \
  | node $HOME/.claude/scripts/scope-check-gate.mjs --check "$WORKTREE/.pipeline/scope-check.json" --diff-files -
```

Output `{ok, justified, unjustified[], unlisted[], notDone}`, plus `formatOnly[]` with `--patch` / `--patch-range` (`patchError` when the diff could not be read). With `--patch <unified diff>` (or `--repo <dir> --patch-range <a>...<b>`, which reads that diff itself), `formatOnly[]` lists hunks whose removed and added lines differ only in whitespace: a reformat of lines the task did not need to touch. It is reported, never fatal, and reviewers see it in `<scope-self-check>`. On a rework pass, `--rejected <triage-output.json> --repo <dir> --rework-base <rev>` (the two come together) adds `rejectedTouched[]` (`rejectedError` when unreadable): each finding triage rejected whose line a hunk of `git diff <rework base>` against the working tree changed, read on the hunk's old side within two lines. Phase 2 acts on accepted findings only, so such a hunk is a fix triage ruled out; it is reported, never fatal. Exit 1 lists `unjustified[]` (in the diff, no reason or an empty one); `unlisted[]` (in the record, not in the diff) is reported and never fatal. Complete the record once and re-run. A second exit 1 does not block: log `dev.scope_check=incomplete unjustified=<n>` and hand off; Phase 4 receives the gate output inside `<scope-self-check>`, so reviewers see which files arrived without a stated reason. `--advisory` (used by Phase 4) always exits 0. Progress line: `    → checking scope self-check ({n} files, {m} not done)`.

## Rework pass

Rejected findings carry the line numbers of the tree the review read, and every round rewrites `triage-output.json`, so a rework is compared against that round's own base.

1. Before the first rework edit, record the base: `node $HOME/.claude/scripts/rework-base.mjs record --worktree "$WORKTREE" --iteration "$(jq '.reviewIterations | length' "$STATE_FILE")"`. One file per round, `.pipeline/rework-base-<N>`; a resumed re-entry of the same round keeps the base it started from.
2. In Step 3.7, after the normal gate:

```bash
REWORK_BASE=$(node $HOME/.claude/scripts/rework-base.mjs get --worktree "$WORKTREE" --iteration "$(jq '.reviewIterations | length' "$STATE_FILE")")
node $HOME/.claude/scripts/scope-check-gate.mjs --check "$WORKTREE/.pipeline/scope-check.json" --diff-files /dev/null --advisory \
  --rejected "$WORKTREE/triage-output.json" --repo "$WORKTREE" --rework-base "$REWORK_BASE"
```

Each `rejectedTouched[]` entry is an edit to lines triage ruled out: revert that hunk unless an accepted finding needs it, and say which in the record's reason. Log `dev.rework_rejected_touch=<n>`.

## Known limits in the code

A limit accepted on purpose is marked at the line, in the file's comment
style: `shortcut: <the limit>, <when to replace it>`. `notDone[]` says what the
change left out; the marker says what the code that shipped does not handle
yet. `debt-ledger.mjs` collects the markers a diff adds into the repo's debt
list, reviewers do not re-raise a marked limit unless it breaks a requirement
or a security rule, and the PR description lists them.

## Consumers

- With plan todos on, Phase 2 applies the same two signals per step as it goes: `plan-todos.sh drift` compares each step's changed files with its planned `files[]` and counts its not-done items, and flags the steps built on a drifted one for re-plan (`features/plan-todos.md`).
- Phase 3 Step 2.2 renders file reasons, unjustified files and `notDone[]` into the shared reviewer prefix (`features/review-delta.md`).
- Phase 4 Step 3 builds the PR `## Changes` bullets from `files[].reason` and lists `notDone[]` under `## Related` as "Follow-ups not done in this PR", merged with the final triage `deferred[]` (`channels/pr.md`).

## Reference

Script: `$HOME/.claude/scripts/scope-check-gate.mjs`. Schema: `$HOME/.claude/schemas/scope-check.schema.json`. Smoke: `smoke-scope-check.sh`. Unit: `test/scope-check-gate.test.mjs`.
