# Phase 2 Dev  -  Multi-Repo Mode

> Loaded on demand from `phases/phase-2-dev.md`. Conditional on
> `state.projects[].length > 1` (set by Phase 0 multi-select); a single-repo run
> never needs it. Token forwarding and the build-log offload stay in the phase
> doc because every run uses them.

Active when `state.projects[].length > 1`. The single-repo flow in the phase doc is preserved verbatim  -  this file adds the deltas.

**Todo tagging**: Phase 1 plan items in multi-repo mode carry a `repo` field naming the target entry's `project`. The dev agent uses this tag to dispatch each todo:

```json
{
  "id": "T-3",
  "title": "Add tokenResolver public API",
  "repo": "common",                         // ← matches state.projects[i].project
  "files": ["Sources/Common/TokenResolver.swift"],
  "tdd": { "test": "...", "code": "..." }
}
```

If a todo has no `repo` tag in multi-repo mode → log warning + ask user, do not auto-pick.

**Per-todo worktree switch**: Before each todo's TDD cycle, resolve `WT_PATH = state.projects[ findIndex(project === todo.repo) ].worktreePath` and run **all** subsequent commands with `git -C "$WT_PATH"` and `cd "$WT_PATH"` for build/test. Identity (`user.name`/`user.email`) was already pinned per worktree by Phase 0 Step 8  -  no re-config needed here.

**Status update (single ticket, multi-repo)**: The Jira/GitHub issue is shared across all repos in the group  -  call the status-update API exactly **once per todo** (not once per repo per todo). The post-mutation verify rule still applies.

**Build serialization**:
- Xcode worktrees still share `BUILD_LOCK = /tmp/claude-xcodebuild.lock`  -  multi-repo just means more callers contending for the same lock; the existing `mkdir`-based queue handles it correctly.
- Non-Xcode repos may build in parallel across worktrees  -  but ONLY when builds are independent. If repo B's build depends on repo A's freshly-published artifact (e.g. SPM local path dependency), serialize them in dependency order. Plan items declare dependencies via Phase 1's `dependsOn` field; multi-repo dispatch respects that order.

**Failure isolation**: A build/test failure in one repo's worktree must NOT silently leave the other repos in a half-built state. On failure:
1. Capture the failure into `state.projects[i].buildStatus = { ok: false, attempts: N, lastError: "..." }`
2. The ENTIRE Phase 2 retries within that repo's worktree (max 3, same as single-repo)
3. After 3 attempts, escalate; do NOT proceed to Phase 3 with any unbuilt repo

**Recording a pass (default-FAIL evidence gate):** before setting `buildStatus.ok = true`, the build output must be tee'd to a log and that log must substantiate the success  -  a zero exit code alone is not trusted. Run the evidence gate; on exit 1, do NOT record a pass:
```bash
<build-command> 2>&1 | tee "{worktreePath}/.build.log" \
  | bash $HOME/.claude/scripts/offload-ref.sh --phase 2 --label build --root "$WORKTREE"
node $HOME/.claude/scripts/evidence-gate.mjs --claim build --status passed --evidence "$WORKTREE/.build.log"
```
Non-zero from the gate: the build pass is unverified, so treat it as a failure and keep `buildStatus.ok=false`.
This closes the gap where an agent records "built" without ever producing build output.
The offload pipe is explained in the phase doc ("Why the pipe").

**Telemetry**: Per-repo metrics in addition to per-task metrics:
```bash
$HOME/.claude/scripts/log-metric.sh "$TASK_ID" 2 build.completed repo=common duration_ms=$D status=ok
$HOME/.claude/scripts/log-metric.sh "$TASK_ID" 2 build.completed repo=uicomponents duration_ms=$D status=ok
```
