### Phase 2: Dev (Sonnet)

> **TLDR**  -  Sonnet executes the plan task-by-task with TDD (red→green→refactor). Required: issue-tracker status moved to "In Progress" before any code, with a post-mutation verify step (re-reads the field, retries once on silent VALIDATION failures). Build verification after each task (up to 3 retries). Build-queue lock serializes concurrent xcodebuild. `taskType === component` **short-circuits the TDD path** and delegates the whole phase to the enabled `ai-<platform>-toolkit` marketplace plugin's component skill (`create-component`, fallback `create-ui-component`)  -  see next subsection.

## Phase 2 Pre-flight (BLOCKING, v9.0.0)

Per Locked decision 30, Phase 2 Dev consumes the analysis document as the sole design source. **Figma** MCP / REST / URL fetches are forbidden; the toolkit MCP is not (Steps 3.4, 3.55; `repo_read` checks `state.productContext` at its sha, copying nothing).

Pre-flight steps (run in order, abort on failure).

**Steps 1, 2, 3, 5 and 6 read the analysis document.** Phase 1 always runs, so the document always exists; what varies is how much evidence it carries. A section the evidence did not support is absent by the Locked 2 omission rule, and a step whose section is absent is recorded `not-applicable (no <section> in this document)` rather than aborting. Steps 4, 7, 8 and 9 read nothing from it and apply always.

1. **Analysis document presence** (Phase 1 modes only): read `state.analysis.docStatus` and `state.analysis.docPath[]`, both set by Phase 1 Step 4.
   - `produced` | `reused` -> read the active platform's file; multi-repo runs need one per selected repo.
   - `not-applicable` -> the analysis mode exported a document this run does not consume; record it for steps 1, 2, 3, 5, 6 and skip them.
   - **Abort**: `produced` but unreadable -> `ERR: analysis doc at <path> unreadable. Resume with /multi-agent:resume #N.` Producing it is Phase 1's job.

2. **Parse YAML front-matter** into `state.analysis.frontMatter`: `feature`, `platform`, `language`, `mode`, `evidence_digest`, `template_version`. **Abort** when `template_version` < `v3`; there is no degraded mode.

3. **Spec freshness**: two checks, both non-blocking, both flagged for Phase 3.
   - **Digest**: `state.run.lastAnalysisDigest` against `frontMatter.evidence_digest`. Mismatch = written from different evidence. Key absent -> `not-verifiable`, never `fresh`.
   - **Repo drift**: `git diff --name-only <state.run.analysisBaseCommit>..HEAD` intersected with Section 14 paths. Non-empty = the repo moved under the spec. This is the one that fires on a `reused` doc, where the digest still matches because the evidence inputs did not change.

4. **Code Connect mapping lookup**: for each component named in analysis Section 6 (Bileşen Envanteri), search the repo for matching `*.figma.swift` / `*.figma.kt` files. Persist hits to `state.dev.codeConnect[<componentName>]`. Missing mappings are not blockers but the Phase 3 reviewer will flag them.

5. **Standards binding citations**: read `analysis Section 21 References` table. For each row with `Rol: bağlayıcı / binding`, persist the source path into `state.dev.standardsBindings[]`. Phase 2 Dev tasks MUST cite at least one binding source per architectural decision (pre-existing Locked decision 8).

5b. **Test plan handoff (the RED input)**: read `analysis Section 15` into `state.dev.testPlan[]` (15.1 unit rows with name/arrange/act/expected/`BR-` id, 15.2 snapshot variants, 15.6 UI flows, 15.7 manual scenarios), tagging each row's `kind` so Phase 3 knows which rows a code test must satisfy. **RED writes these tests, not invented ones** - development is TDD, so the analysis matrix is literally the first thing written. A row too vague to write from is an analysis defect: open a Section 20 question, do not improvise.

6. **Conventions handoff**: read `analysis Section 13.1 Concept Table` (Pass B output with footnotes). Persist concept-to-realization mapping into `state.dev.conventions[<concept>]`. Phase 2 implementation uses these names verbatim, and a non-empty `sharedUtilities` bucket is binding: bind those formatters/rule-facades/tokens, never hand-roll a duplicate (e.g., if Section 13.1 says "State holder: OrderSummaryViewModel", Phase 2 names the class exactly `OrderSummaryViewModel`).

7. **MCP forbidden**: calling `mcp__claude_ai_Figma__*` in Phase 2 is a violation. Record any call in `state.telemetry.mcpCalls[]` with its phase; the maintainer regression check `smoke-no-mcp-in-dev-phases.sh` flags a Figma entry at phase 2 or later.

8. **Criteria ledger (required, every mode)**: the moment this phase consults a skill, a marketplace plugin skill, a stack guide or a module `CLAUDE.md` in order to write code, append an entry to `state.telemetry.skillCalls[]`:

   ```json
   {"skill": "ios-coding-standard", "phase": 3, "targetFiles": ["Sources/Login/LoginViewModel.swift"], "timestamp": "<ISO-8601>"}
   ```

   `targetFiles` is required  -  without it a skill applied to the wrong files still reads as "applied". Append at the moment of consultation, not at the end of the phase. Phase 3 Step 1.78 treats this as self-report only and resolves criteria independently; it is the one signal separating "applied to the wrong files" from "never opened".

9. **Stack skill routing (every `taskType`, always)**: the write that opens this phase resolves the routing itself  -  `write-state.mjs` runs the resolver in-process, stores `mode`, `toolkits`, `excluded`, `unscoped`, `hints` and `fallbacks` in `state.telemetry.skillRouting`, and prints the line `skill routing resolved  -  load <toolkit>:index, ... before writing code`. That line is the step's input: **load each named `index` through the Skill tool before the first edit**, ask it which of its skills govern this task, load those too, and record each into `state.telemetry.skillCalls[]` with `phase: 2`, `targetFiles` and `routedBy: "<toolkit>:index@<version>"`. A host with hooks also writes `telemetry.skillLoads[]` for every Skill call it sees, so what was opened is not this phase's to remember; the ledger entry stays what attributes a skill to files. Nothing is resolved by hand: a code write before a kept index was loaded is stopped by `run-scope-guard`, and the write that closes this phase is refused while any kept index is unrecorded (`write-state.mjs` exit 6). Precedence: repo-local skill (`source: "repo"`) > detected-stack toolkit skill > common toolkit; `overlaps[]` names the owner of a skill two toolkits share. Unattended or autopilot runs load `fallbacks[]` read-only (`source: "marketplace-fallback"`) and never route to `unscoped[]`; a missing stack toolkit is a hint, never another stack's toolkit. The routing table stays in the plugin. A screen-creation task loads the routed toolkit's `workflow/create-screen` when one exists. No toolkit is a recorded no-op, not a halt. Contract: [`features/stack-skill-routing.md`]($HOME/.claude/multi-agent-refs/features/stack-skill-routing.md).

10. **Autopilot** (or `MULTI_AGENT_UNATTENDED=1`): a run that Phase 1's `open-questions-gate.mjs` parked (`waitingFor: "question"`), or that `spec-consistency-gate.mjs` or `plan-critique-gate.mjs` failed (`verificationFailed.gate: spec-consistency` / `plan-critique`), does not enter this phase. When `state.research.md` exists, read it before writing code: what research closed, each answer with its source ([`features/research.md`]($HOME/.claude/multi-agent-refs/features/research.md)).

The analysis document is the SOLE design source in Phase 2. Variant choices, padding values, color tokens, copy strings, accessibility identifiers, and test method names all come from the rendered Pass B cells. If something is missing in the analysis doc, the fix is to re-run `/multi-agent:analysis`, not to fetch from Figma.

<!-- progress-contract: applied -->

**Progress (per `$HOME/.claude/multi-agent-refs/progress-contract.md`):** every non-trivial step emits one `→ <verb> <object>` line at immediate flush. Canonical set for standard TDD: `→ reading <file>`, `→ writing test <name>`, `→ running xcodebuild RED`, `→ writing code <file>`, `→ running xcodebuild GREEN`, `→ committing WIP <summary>`. Component-task dispatch emits the line `→ dispatching create-component <componentName>` before the Skill-tool call, then relays the plugin skill's progress lines verbatim under indent `    →`.

#### Input contract

Phase 2 consumes the Phase 1 output object conforming to `$HOME/.claude/schemas/planning-output.schema.json`  -  the task graph (`tasks[]` with `id`, `title`, `type`, `files`, and optional `dependsOn` / `acceptanceCriteria`) plus the architecture review notes. Tasks execute in dependency order; the schema's `dependsOn` field drives the ready-task picker. Task briefs quote `globalConstraints[]`; a `reviewFocus[].ownerTaskId` task tests that input in RED.

**Plan Todo iteration (opt-in)**: gated by `prefs.global.planTodos.enabled` (default `false`). When enabled and Phase 1 Step 11 emitted `plan.todos[]`, iterate via `$HOME/.claude/lib/plan-todos.sh next/start/complete/fail/drift`; disabled, walk `tasks[]`, same TDD contract. Each todo records RED and GREEN runs (`tdd-red` / `tdd-green`); `complete` checks them per `prefs.global.tddEvidence.mode`. Loop + exemptions: `$HOME/.claude/multi-agent-refs/features/plan-todos.md`. A todo with `sourceTag: Reuse` binds the file analysis already found; `Modify` edits in place. Writing a new file over a `Reuse` step is a Locked 11 violation and Phase 3 flags it.

**Shadow-Git checkpoints (opt-in)**: gated by `prefs.global.shadowGit.enabled` (default: `false`). When enabled, the orchestrator snapshots the worktree via `$HOME/.claude/lib/shadow-git.sh` so sub-phase rollback is possible without touching the project's real `.git` history. Lifecycle: `shadow-git.sh init` (Phase 0 baseline), `shadow-git.sh snapshot` (per step after `plan-todos complete`), `shadow-git.sh restore <sha> --files` (rollback). Modes: `per-todo-step` (default) or `per-tool-call`. Full wiring + storage cap: `$HOME/.claude/multi-agent-refs/features/shadow-git.md`.

#### Component tasks  -  delegated dispatch (taskType === "component")

When Phase 0 Step 7 classified the task as `component`, Phase 2 delegates the entire phase to the enabled `ai-<platform>-toolkit` marketplace plugin's component skill (`create-component`, fallback `create-ui-component`) via the Skill tool and does NOT run the TDD loop below. The dispatch layer passes the plugin skill the analysis Section 6 (Bileşen Envanteri) entry + Section 13.1 conventions for the named component as context. Because plugin skills do not write pipeline state, the **dispatch layer** (not the skill) owns `state.phases["2"].componentDispatch`, and the writer refuses to close Phase 2 without it. Resolution, failure/resume, multi-repo and cross-CLI detail: `$HOME/.claude/multi-agent-refs/component-dispatch.md`. Phase 2 still owns: progress line `-> dispatching create-component <name>`, the build-retry cap (`retryCount`, 3), and the one case that runs the TDD loop below on a component task  -  a run carrying no design reference at all, which records `componentDispatch.outcome: "fell-through"` with its reason first. Every other unmet prerequisite halts Phase 2 with no record and the phase left `in_progress`; the TDD loop is never the answer to a dispatch that could not run.

For non-component taskTypes (`bugfix`, `feature`, `refactor`, `chore`), continue with the standard TDD section below.

#### Design fidelity contract (BLOCKING when the task carries a Figma reference)

Applies to EVERY task whose analysis doc carries design ground truth (Section 6 rows, node IDs, screenshots)  -  component-dispatch AND TDD-path UI work alike. MCP stays forbidden here (analysis-only rule); the analysis doc IS the design:

1. **1:1, not interpretation.** The captured frames are the reference for every UI line. Layout, variant states, copy, and colours come from the analysis doc's measured values  -  never eyeballed, never "close enough".
2. **Code Connect decides the component.** A mapped component (`CodeConnectSnippet` / repo `*.figma.swift` / `*.figma.kt` row) MUST be used verbatim  -  sound-alike substitutes are forbidden; a missing modifier is added to the component's `+Modifiers` extension, never forked. No mapping exists -> write a NEW component per the Configuration/View/Modifiers architecture; never inline ad-hoc UI into the consumer screen.
3. **Inter-component spacing is part of the design.** Gaps, paddings, and alignment BETWEEN components must match the frame's measured values, mapped to spacing tokens (`Spacing*`)  -  never invented numbers. A spacing/layout deviation is a `review_blocking` finding in Phase 3 Step 1.8, not a nitpick.

Missing design data (variant, padding, copy) → HALT and instruct the user to re-run `/multi-agent:analysis`; never guess and never call Figma MCP from this phase.

#### Re-entry from Phase 3 triage

Phase 2 runs first for initial development, then once per rework round after Phase 3 review (at most the review limit, 1 to 3, default 2). **Phase 2 never acts on raw reviewer output.** It only consumes `triage.accepted` findings  -  Fable triage in Phase 3 already filtered false-positives, deferred out-of-scope items, and rejected noise.

When re-entering from Phase 3:

1. Read `state.reviewIterations[-1]`  -  the latest iteration's **accepted** list.
   Before the first edit, record the round's base (`features/scope-check.md`, "Rework pass").
2. For each `accepted` finding where `severity === "blocking"` or `severity === "important"`:
   - Treat it exactly like a Phase 1 plan task (TDD cycle, build verify).
   - Quote the original `issue` + `fix` text in the task description so the dev agent has full context.
3. `accepted.suggestion` items are applied opportunistically (no TDD loop required) unless the user asked for suggestions to be treated strictly.
4. `deferred` items are NOT actioned in this re-entry  -  Phase 4 Step 0b lists them before the commit and the PR description carries them as follow-ups.
5. `rejected` items are never touched. Log their IDs + triage reasons for audit only; Step 3.7 checks it.
6. The review limit and the rework count are owned by Phase 3 Step 4 (`features/review-rounds.md`): it decides `rework` or `rule` with `review-rounds.mjs next`, and a `rework` has already recorded one more `state.phases["2"].reworkCount` before this re-entry starts. This step neither increments a counter nor compares one: `retryCount` counts build retries only. The re-entry is the rework Step 4 recorded: `phases["2"].reworkForIteration` equals the number of `reviewIterations`; a resumed run with that state comes straight here and never repeats the decision. Run the rework, then return to Phase 3, which reviews it again. Do not loop indefinitely: the limit is 1 to 3 reworks, 2 by default.
7. When `state.reviewIterations[-1].delta` exists, `stillPresent[]` findings come first in the task list, quoted `STILL PRESENT after round N-1's fix`; repeating the previous attempt is the loop the circuit-breaker stops.
8. Before each rework dispatch, `rework-escalation.mjs` picks the rung; findings and the prior report go as files (`features/rework-escalation.md`).

If the latest iteration has `triage.approved === true` AND `accepted === []`, Phase 2 was entered by mistake  -  log the anomaly and return to Phase 3.

**Telemetry**: at the start of every re-entry, emit:

```bash
$HOME/.claude/scripts/log-metric.sh "$TASK_ID" 2 rework.started \
  iteration=$ITERATION accepted_blocking=$BLOCKING accepted_important=$IMPORTANT
```

---

For each task (respecting dependency order), on start and on end: `bash $HOME/.claude/scripts/phase-tracker.sh sub 2 <task_id> "<title>" in_progress|completed|failed`.

1. **Update task status: `in_progress` (required, before any code)**: the board shows "Todo" until this runs.
   - Jira: transition to "In Progress" via REST (operations.md "Confirmed writes"); Projects V2: `updateProjectV2ItemFieldValue` on Status; state: `phases["2"].status = "in_progress"`.
   - **Verify**: re-read Status. Not `In Progress` / `In Develop` (stale option ids after a board rebuild) → re-fetch the ids, retry once; still wrong → log it and do NOT start TDD.
   - **Record** `phases["2"].trackerStatus` (`agent-state.schema.json`); write-state will not leave Phase 2 of an attended tracked run while it is missing or `failed`.
2. **Multi-submodule awareness**: a task may span the submodules Phase 0 Step 7 detected (e.g. tokens in **common**, the component in **uicomponents**); that is expected.
3. **TDD cycle** (Launch Agent with `model: "sonnet"`)  -  gated by `state.testPolicy`: `tdd` = the loop below; `tests-after` = skip RED, author the same tests once green; `none` = author no tests (existing ones never weakened), report "tests not written - project policy" rather than a gap.

   **Bugfix (`taskType === bugfix`, rework included): root cause before any fix.** `root-cause-gate.mjs check` passes on an evidenced record first; each fix attempt is recorded, and the third red one stops the loop with a design question, never a fourth patch: [`features/root-cause.md`]($HOME/.claude/multi-agent-refs/features/root-cause.md).

   **RED  -  Write ONE failing test first:**
   - **Rework re-entry**: if `state.reviewIterations[-1].verifyByTest.redTests[]` exists (Phase 3 Step 3.7 ran), those failing repro tests ARE the RED step for their findings  -  make them green, do not write a duplicate failing test and do not delete or weaken them. See `$HOME/.claude/multi-agent-refs/features/verify-by-test.md`.
   - Test framework: use whatever the project already uses. Detect from existing test files:
     - `@Test` / `#expect` → Swift Testing
     - `XCTestCase` / `XCTAssert` → XCTest
     - `describe` / `it` → Quick/Nimble
   - Test naming: `test{Scenario}_{Expected}` (e.g. `testKeychainReturnsNil_doesNotCrash`)
   - One test per behavior change  -  not one test per file
   - Run test to confirm RED with the stack's command. ios, under the build lock:
     ```bash
     bash $HOME/.claude/scripts/build-lock.sh acquire "$TASK_ID"
     xcodebuild test -scheme "{scheme}" -destination "platform=iOS Simulator,name={simulator}" \
       -derivedDataPath "{worktreePath}/.DerivedData" -only-testing:"{testTarget}/{testClass}/{testMethod}" 2>&1 | tail -5
     bash $HOME/.claude/scripts/build-lock.sh release "$TASK_ID"
     ```
     android: `./gradlew test --tests "{testClass}.{testMethod}" 2>&1 | tail -5` (same lock); backend: `pytest "{test_file}::{test_name}" 2>&1 | tail -5`; web: `node $HOME/.claude/scripts/package-manager.mjs test --dir "{worktreePath}" --pattern "--testPathPattern={file}"` prints the manager's command, then run that command `2>&1 | tail -5`.
   - The web step resolves the manager instead of typing `npm`; exit 3 means the repo
     declares no such script - say so, never substitute one, and never let an empty
     command pass for a pass (`features/package-manager.md`).

   - Must fail for the RIGHT reason (expected assertion, not compilation error)

   **GREEN  -  Minimal code to pass:**
   - Smallest change that makes the test green  -  no extras
   - **Existing tests are immutable**: deleting, renaming, skipping, or weakening an existing assertion to reach green is a violation. A test may change only when the task itself changes the spec that test encodes, and the commit body must name the changed test and the spec change. (Deterministic backstop: the `test_lines_removed` signal in Phase 3 Step 1.75 flags test files that shrink.)
   - Run the same test again → must PASS
   - Run full test suite → no regressions:
     ```bash
     bash $HOME/.claude/scripts/build-lock.sh acquire "$TASK_ID"
     xcodebuild test \
       -scheme "{scheme}" \
       -destination "platform=iOS Simulator,name={simulator}" \
       -derivedDataPath "{worktreePath}/.DerivedData" \
       2>&1 | tail -20
     bash $HOME/.claude/scripts/build-lock.sh release "$TASK_ID"
     ```

   **REFACTOR (if needed):**
   - Only if duplication or naming is poor
   - Re-run tests after refactor → still GREEN
   - **Stability (required):** run every test added or changed in this diff `prefs.global.testStability.repeatCount` times (default 3; 1 disables) with the single-test invocation above. Disagreeing outcomes are not GREEN: log `test.flake_signal file=<f> passed=<k> of=<N>` and fix the test or the code first. Same rule on the Phase 3 rework re-entry. Keep each repeat's `.pipeline/repeat-<k>.junit.xml` and judge them with `test-stability-gate.mjs` (`features/verify-by-test.md`, "Repeat runs").

   **Target resolution** (auto-detect once per project, cache in `agent-state.json`; ios resolves scheme + simulator, android resolves module + variant, backend/web need none):
   ios (prefer the scheme matching the project name, then the first non-test scheme):
   ```bash
   xcodebuild -list -json -project "{projectPath}" 2>/dev/null || xcodebuild -list -json -workspace "{workspacePath}" 2>/dev/null
   xcrun simctl list devices available -j | jq '.devices | to_entries[] | select(.key | contains("iOS")) | .value[0].name'
   ```
   android: `./gradlew projects 2>/dev/null | grep -E "^\+--- Project"` and `./gradlew tasks --all 2>/dev/null | grep -m5 "assemble.*Debug"`.

4. **Build verification** (per stack; ios/android under the build queue lock, see below):
   - **ios, preferred (MCP, multi-agent-mcp >= 3.0.0)**: `build-lock.sh acquire "$TASK_ID"` → `mcp__multi-agent__ios_xcodebuild({project|workspace, scheme, configuration: "Release", destination: "generic/platform=iOS", derived_data_path: "{worktreePath}/.DerivedData"})` → `build-lock.sh release "$TASK_ID"`. Returns one line `Build: SUCCESS|FAILURE (E errors, W warnings) [xcresult-<id>]`; on failure drill in via `mcp__multi-agent__ios_xcresult({id, mode: "errors"})`, never dump the full log.
   - **ios, fallback (raw)**: same lock pair around `xcodebuild build -scheme "{scheme}" -destination "generic/platform=iOS" -derivedDataPath "{worktreePath}/.DerivedData" 2>&1 | tail -5`.
   - **android**: lock pair around `./gradlew assembleDebug 2>&1 | tail -5` (the Gradle daemon and `build/` outputs contend across parallel worktrees exactly as DerivedData does  -  the lock applies).
   - **backend / web**: `python -m compileall .` / run the command `node $HOME/.claude/scripts/package-manager.mjs run --dir "{worktreePath}" --script build` prints, `2>&1 | tail -5` (exit 3: no build script); no lock.
5. If build fails → fix → rebuild (max 3 attempts, the build-retry cap; track `retryCount` in state. It is one counter on `phases["2"]` that nothing resets, so it is cumulative across every Phase 2 entry of the run, initial development and each rework included, and reaching 3 stops the run whatever the review limit is). A failure that says `ENOSPC` or no space left is not a code defect, and retrying will not fix it: save the output and run `node $HOME/.claude/lib/low-disk.mjs --scan <log>`, print its line, and stop retrying. Attended, name `/multi-agent:clean-disk` for the user to type; unattended, record the line and halt. Never delete anything from here.
6. **Intermediate commit** (after each completed task in the plan):
   ```bash
   git -C "{worktreePath}" add -A
   git -C "{worktreePath}" commit -F "/tmp/wip-message-$TASK_ID.txt"
   ```
   Write `wip({scope}): {task summary} [{jiraId}]` to `/tmp/wip-message-$TASK_ID.txt` with the file tool first. The summary comes from the issue, and inside a double-quoted `-m` the shell would run any `$(...)` in it.
   Standard path item 3 of Phase 4 Step 2 folds these back before the real commit. Keep the subject shape: that step recognises a checkpoint by it. This prevents work loss if a later task fails or the session crashes.
7. Update task status: `completed` (apply the same verify rule  -  re-read the Status field and retry once on mismatch)
8. Log: "Phase 2: {task}  -  Test + Build passed"

---

#### Build Queue (parallel task serialization)

**Problem**: Multiple parallel worktrees may reach build/test phase simultaneously. `xcodebuild` cannot run in parallel  -  DerivedData, simulators, and project locks cause conflicts.

**Solution**: Lock-file based queue using `mkdir` atomicity (`build-lock.sh`). Before ANY `xcodebuild` call, acquire the lock; release after completion:

```bash
bash $HOME/.claude/scripts/build-lock.sh acquire "{jiraId}"
xcodebuild -derivedDataPath "{worktreePath}/.DerivedData" ...
bash $HOME/.claude/scripts/build-lock.sh release "{jiraId}"
```

**Key details:**

- Lock path: `/tmp/claude-xcodebuild.lock` (shared across all Claude instances; `MA_BUILD_LOCK` overrides)
- Stale lock timeout: 15 minutes (auto-cleanup if a session crashes)
- Each worktree uses its OWN `-derivedDataPath` to avoid cache poisoning
- `sleep 5` between retries  -  not aggressive polling
- Lock owner tracked for visibility: which task is currently building
- `release` takes the same task id and leaves a lock held by another task in place
- A lock whose owner file is missing is taken over only after a 5-second grace window; with `MA_BUILD_LOCK_PID` set, a lock whose recorded process is gone is taken over at once

**This applies to ALL xcodebuild calls in the pipeline:**

- Phase 2 Step 4 (build after development)
- Exit gate Step 1 Gate 1 (build gate before review)
- Exit gate Step 1 Gate 3 (test gate before review)

**Android**: same lock discipline  -  the Gradle daemon and `build/` outputs contend across parallel worktrees. **Backend/web** (Python, Node.js): no lock needed  -  these build/test in parallel without conflicts.

---

#### Step 3.5  -  Dev Critic (Evaluator-Optimizer, opt-in)

Gated by `prefs.global.devCritic.enabled` (default: `false`). When enabled, after the generator's last edit and BEFORE Phase 3: dispatch `dev-critic` sub-agent (Sonnet), run 4 deterministic gates (build / lint / test / secrets), then platform checklist. STRICT loop cap  -  **max 2 iterations** (round 2 re-checks round-1 failures only); round 3+ returns `escalate: true`. Severity routing: `blocking` → generator must fix; `important` → SHOULD fix or pass through to Phase 3; `suggestion` → generator's judgement. Contract: `$HOME/.claude/multi-agent-refs/features/dev-critic.md` + `$HOME/.claude/agents/dev-critic.md`.

---

#### Step 3.55  -  Visual evidence capture (UI changes only)

Re-decide `visualEvidence.required` from the real diff first - Phase 0's `bugfix` verdict is provisional (section 1a). When required, capture with the build that just went green - here, not Phase 3 (section 3). Exit 4 anywhere below is a gap, not a failure. Contract: `features/visual-evidence.md`.

`EVIDENCE_PLATFORM=$(jq -r '.visualEvidence.platform // ""' "$STATE_FILE")`. Empty closes every tier below with that reason; passing it on is exit 2.

1. **Still**: `$HOME/.claude/scripts/capture-evidence.sh after --task "$TASK_ID" --platform "$EVIDENCE_PLATFORM" --label <slug>`.
2. **Re-check the device.** `evidenceCapability` was measured at intake; a simulator booted then can be gone now. Re-measure the one volatile row with `$HOME/.claude/scripts/probe-evidence-capability.sh --platform "$EVIDENCE_PLATFORM" --repo "$WORKTREE" --only device`, which skips the repo scan the full probe does; gone means fall to the next open tier and write `visualEvidence.videoTierReason` as the transition (`tier 1 -> 3: simulator no longer booted`).
3. **Recording**, by `state.testDepth`:
   - `unit+ui`: `video start` -> `$HOME/.claude/scripts/run-ui-tests.sh run --platform "$EVIDENCE_PLATFORM" --repo "$WORKTREE" --changed "$CHANGED_CSV"` -> `video stop`. Runner exit 3 (no matching test) or 4 (no target) -> stop, discard, fall to tier 2. Exit 1 is a red UI test: keep the recording, it shows the failure.
   - `unit+mcp`: `video start` -> drive with `mcp__multi-agent__agent_run_steps` -> `video stop`. The only MCP-dependent path; MCP absent -> tier 3 gap.
   - `unit`: no recording; the gap reason is the user's own answer.
4. **Fit**: `$HOME/.claude/scripts/capture-evidence.sh fit --file <path>` per artefact. Quality degrades, the artefact is never dropped.

`video stop` warns when the recording is under a second: both recorders encode on change, so a screen that never moved yields a valid two-frame file. Keep it, record the warning as a gap reason, never present a still as a flow.

Persist `state.uiTest` and the artefact entries. A UI test that ran is subject to the default-FAIL rule like the build: run `evidence-gate.mjs --claim test --status passed --evidence "$WORKTREE/.pipeline/ui-test.log"` first, because a runner that died before reaching the tests also exits non-zero.

#### Step 3.6  -  Code-simplifier pass (required diff shrink, before Phase 3 handoff)

After the build/test green step and BEFORE Phase 3 handoff, run one diff-shrink round (bloated diffs waste Phase 3 reviewer tokens and unfocus the PR).

1. **Dispatch ONE subagent** (`subagent_type: "general-purpose"`, model: `sonnet`); input is ONLY the working diff (`git -C "$WORKTREE" diff "origin/$BASE_BRANCH"...HEAD`) + task summary. No correctness re-review (Phase 3's job), no new features. Exactly four smells:
   - **Comment bloat**  -  comments restating code, scaffolding notes, TODO chatter
   - **Unrelated rewrites**  -  hunks outside task scope (re-formatting, unrequested rename sweeps)
   - **Dead code**  -  unused symbols, unreachable branches, commented-out blocks from this diff
   - **Over-abstraction**  -  single-call-site protocols/wrappers/helpers from this diff

   Progress line: `    → dispatching code-simplifier diff-shrink`
2. **Return contract**  -  a list of shrink edits `{ "file", "lines", "edit", "rationale", "risk": "safe|unsafe" }`; the subagent never writes files. Keep the `rationale` strings: Step 3.7 writes them into `scope-check.json`.
3. **Apply safe edits only**  -  skip `unsafe` (logged). Zero edits is normal  -  log and continue. Progress line: `    → applying shrink edits ({N} applied, {M} skipped)`
4. **Re-run build + tests** (same build-queue lock + evidence-gate rule as Step 4). Any breakage -> revert shrink edits wholesale and proceed pre-shrink; the simplifier must never cost a green state.
5. **Record tokens in the cost ledger** so Phase 5's Cost Breakdown captures the pass:

```bash
LOG_METRIC_FORWARD_TO_TRACKER=1 $HOME/.claude/scripts/log-metric.sh "$TASK_ID" 2 dev.simplifier_pass \
  model=sonnet tokens_in=$IN tokens_out=$OUT duration_ms=$DUR \
  edits_returned=$RET edits_applied=$APPLIED edits_skipped=$SKIPPED
```

Scope guard: a single pass, never looped. Component tasks (`taskType === "component"`) skip it (the figma skill owns its own checklist).

---

#### Step 3.7  -  Scope self-check (required handoff artifact)

Dev states why each file was touched and what was left out on purpose: write `$WORKTREE/.pipeline/scope-check.json` (`schemas/scope-check.schema.json`: one `{path, reason}` per file in the diff, `notDone[]` with `why` in `out-of-scope | follow-up | rejected-abstraction`, and the Step 3.6 `simplifier` rationales), then run the gate. **Record rules, consumers and the full contract: `$HOME/.claude/multi-agent-refs/features/scope-check.md`.**

```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 - \
      --repo "$WORKTREE" --patch-range "origin/$BASE_BRANCH...HEAD"
```

On a re-entry from Phase 3, also run the rejected-findings check (`features/scope-check.md`, "Rework pass").

Exit 1 lists `unjustified[]`: complete the record, re-run once. A second exit 1 logs `dev.scope_check=incomplete unjustified=<n>` and hands off; reviewers see it in `<scope-self-check>`. Progress line: `    → checking scope self-check ({n} files, {m} not done)`.

---

#### v2.1.0+ Multi-Repo Mode

**Only when `state.projects[].length > 1`.** Load `$HOME/.claude/multi-agent-refs/features/dev-multi-repo.md` and follow it: the per-todo `repo` tag and worktree switch, one status update per todo, build serialization across worktrees, per-repo failure isolation with the evidence-gated `buildStatus`, and per-repo telemetry. A single-repo run skips it.

#### Token forwarding

Every TDD round (red, green, refactor) that hits the dev model MUST forward token totals into the tracker so Phase 5's Cost Breakdown captures Phase 2:

```bash
LOG_METRIC_FORWARD_TO_TRACKER=1 $HOME/.claude/scripts/log-metric.sh "$TASK_ID" 2 dev.tdd_round \
  model=<sonnet|opus> step=<red|green|refactor> \
  tokens_in=$IN tokens_out=$OUT duration_ms=$DUR
```

Model is Sonnet unless routing names another rung (`multi-agent-refs/features/model-fallback.md`). Best-effort. See `$HOME/.claude/multi-agent-refs/progress-contract.md#token-telemetry-forwarding`.

---

## Token telemetry  -  invoke after every LLM call

```bash
bash $HOME/.claude/scripts/phase-tracker.sh tokens 2 <input_count> <output_count>
```

Contract and rationale: `progress-contract.md` -> Token telemetry forwarding.

#### Generated trees are not yours to edit

Many repos generate part of their source: a service client from an OpenAPI spec, mock
scenario indexes, localization keys, testing identifiers, design tokens. A generated
file is regenerated on the next build, so an edit there is lost silently, and the
matching hand-authored tree is the one that takes the change.

Before writing into any path, check whether it is generated or owned; the repo
profile answers both:

```bash
node $HOME/.claude/scripts/repo-profile.mjs resolve generators --repo "$PROJECT_ROOT" --state "$STATE_FILE"
node $HOME/.claude/scripts/repo-profile.mjs resolve ownedPaths --repo "$PROJECT_ROOT" --state "$STATE_FILE"
```

A target under a `generators[].output` goes to that entry's `input` and is
regenerated with its `command` (in the same commit when `sameCommit`). A target
under an `ownedPaths[].glob` is not edited at all: report the `owner`, and the
`bypass` label when there is one; exit Gate 0 enforces this. Without a profile,
look for the signals directly, case-insensitively:

```bash
# a Generated/ segment, or a header saying so, is the signal
find . -type d -iname Generated -not -path './.*' | head
grep -rliE "do not edit|auto-?generated|generated by" --include="*.swift" --include="*.kt" . | head
```

The pairing is usually `Generated/<x>` for output and `Custom<X>/` or
`CustomSources/` for input. Two concrete shapes seen in the wild:

| Want to | Wrong place | Right place |
|---|---|---|
| add a mock fixture / named scenario | a `Fixtures/` file under a generated tree | the repo's custom fixture tree, plus registering the scenario in the generated index the build reads |
| add or change a service endpoint | the generated client method | the OpenAPI source the generator consumes, then regenerate |

When the analysis doc has not recorded which trees are generated, that is a Phase 1
gap  -  say so rather than guessing, since guessing wrong is invisible until the next
regeneration.

Shared localization keys: reuse before minting (`features/localization-reuse.md`).

---

## Exit gate: Verify (BLOCKING)

These deterministic gates run once, at this phase's exit, so the build runs once:
Review inherits the logs instead of regenerating them.

#### Step 1  -  Deterministic Gates (run BEFORE AI review)

```bash
# Gate 0: Owned paths (repo profile), before the build
node $HOME/.claude/scripts/owned-path-gate.mjs --repo "$WORKTREE" --base "origin/$BASE_BRANCH" --state "$STATE_FILE"
# Gate 1: Build (xcodebuild/gradle assemble/tsc/py compile  -  stack-dependent; Xcode uses the build queue lock, see Phase 2)  -  tee output to a log
<build-command> 2>&1 | tee "{worktreePath}/.build.log" \
  | bash $HOME/.claude/scripts/offload-ref.sh --phase 2 --label build --root "$WORKTREE"
# Gate 2: Lint (swiftlint/ktlint/ruff/eslint  -  stack-dependent)
# Gate 3: Tests pass (xcodebuild/gradle/pytest/the resolved node command)  -  tee output to a log
<test-command> 2>&1 | tee "{worktreePath}/.test.log"
# Gate 4: Secrets  -  run the scanner against the staged diff
bash $HOME/.claude/scripts/pre-commit-check.sh
# Gate 6: features/valven.md
```

**Gate 0** exit 1 blocks the phase: move each change where its `fix` points, revert the owned path, re-run. Exit 2 is a call error, never a pass: an unknown `--base` ref means fetch the base branch. Exit 0 with `skipped: true` gives its reason in `note`; log it. Contract: `features/repo-profile.md`, "Owned-path gate".

**Default-FAIL evidence gate (required before recording any pass):** a green exit code is not enough  -  the captured log must actually show success. Before marking build/test passed, run the evidence gate against the tee'd log; it fails CLOSED when the log is missing, empty, or shows failure markers:

```bash
node $HOME/.claude/scripts/evidence-gate.mjs --claim build --status passed --evidence "$WORKTREE/.build.log" --repo "$WORKTREE" || BUILD_PASS=false
node $HOME/.claude/scripts/evidence-gate.mjs --claim test  --status passed --evidence "$WORKTREE/.test.log" --repo "$WORKTREE" --command "<test-command>" || TEST_PASS=false
```

This prevents a false "it built" claim with no log behind it. `--repo` fails a log older than the last edit, `--command` a narrowed run (`-only-testing`, `-k`, `-t`, one file). On exit 1, treat the gate as failed (do NOT proceed to AI review) and surface the gate's `reason`.

**Why the pipe (opt-in via `prefs.global.contextOffload.enabled`).** `tee` decides where the log is written, not how much of it the model reads. The filter parks the full text at `.multi-agent/refs/<node_id>.md` and prints a stub plus the tail, where a failing build's error already is; read that file before re-running a failed build. The evidence gate still reads the whole `.build.log`, so what counts as a verified pass is unchanged. Pref off = pass-through.

**Autopilot runs** (`state.autopilot` or `MULTI_AGENT_UNATTENDED=1`) also run the stack-aware checks, each recording its verdict in `state.gates[]`: `evidence-gate.mjs --stack`, `test-summary.mjs` (zero tests executed parks the run as `verification-failed` instead of reworking), `test-strength.mjs --head worktree` and `symbol-existence-gate.mjs`. With the variable set, the Phase 4 commit hook refuses a commit whose ledger lacks them. Commands and verdicts: `$HOME/.claude/multi-agent-refs/features/unattended-gates.md` section 4.

**Scaffolded repo** (`.scaffold.json` at the worktree root): at this phase's entry and exit, `scaffold-gate.mjs --dir "$WORKTREE" --phase story --story <id>`; exit 1 or 3 halts and the next story does not start; 2 is a call error, never a pass (`$HOME/.claude/multi-agent-refs/features/scaffold.md`).

**Inherited failures (when `state.baseline.tests` exists).** Gate 3 blocks on what this work broke, not on what Phase 0 Step 7.6 found red:

| baseline status | Gate 3 |
|---|---|
| `green` | unchanged; every failure is this run's |
| `red` + `failing[]` | subtract those ids. Nothing left -> pass, logged `test:pass (inherited {N})`. A NEW failure still blocks. |
| `red`, empty `failing[]` | do NOT pass and do NOT silently block: report `test:inherited-red (not attributable, <logPath>)` and ask. Inventing a set here masks regressions. |
| `unknown` / absent | unchanged |

The subtraction never widens: identifier matches only. Classify a per-test report first (`$HOME/.claude/multi-agent-refs/features/test-classification.md`); `unstable` is never green.

**Gate results:**

- All pass (including the evidence gate) -> proceed to AI review
- Any fail -> fix, re-run gates (no AI review until clean)
  Log: "Phase 3: Gates  -  owned:{ok/fail} build:{pass/fail} lint:{pass/fail} test:{pass/fail/inherited-red} secrets:{clean/found} evidence:{ok/unverified}"

##### Gate 5  -  Fortify SSC findings (runs when `state.contextLinks[]` contains a `fortify` entry, or when `prefs.global.fortify.alwaysCheck === true`)

If the task description referenced a Fortify version, or named a bare issue instance id, `~/.claude/lib/fetch-fortify.sh` already populated `state.fortifyFinding` in Phase 0 (`alwaysCheck` needs `prefs.global.fortify.versionIds` to know what to scan). Phase 3 reuses that payload and applies the deterministic gate:

```bash
jq -r '.fortifyFinding as $f | if ($f.gateOutcome // null) == null then "n/a (no Fortify URL referenced)" elif $f.gateOutcome.blocking == true then "BLOCKED (\($f.gateOutcome.reason), critical=\($f.severityCounts.Critical))" else "pass (high=\($f.severityCounts.High // 0) warnings carry into the channel summary)" end' "$STATE_FILE"
```

Print it as `→ fortify gate: <line>`. `BLOCKED` is a deterministic gate failure: fix the critical findings before AI review.

Gate semantics:

| `gateOutcome.reason` | Phase 3 action |
|---|---|
| `critical-findings` | **Blocking**  -  Phase 2 re-dispatches with the finding list; pipeline does not advance until Critical count is 0. |
| `high-findings-warning` | Pass-with-warning  -  High findings appear in the channels summary (`## Security Scan (Fortify)` section in PR + Jira); they don't block the merge. |
| `clean` | Pass silently  -  section omitted from the channels summary. |

When `state.fortifyFinding.status === "skipped"` (token missing, VPN unreachable, or host mismatch) the gate logs `→ fortify gate: skipped (<reason>)` and never blocks  -  the user already saw the structured Save Flow signal at Phase 0 and chose to proceed.
