Repository navigation
Description
Incident
A PIR builder in an adopter workspace ran its verification commands (jest, tsc) against the main checkout instead of its own worktree, and initially misdiagnosed the wrong results as a stale jest cache. Chain, from the builder's own (accurate) post-mortem:
- The harness's shell cwd is not stable from the builder's seat — it sometimes persists inside a subdirectory and sometimes resets to the worktree root ("Shell cwd was reset to …/.builders/pir-").
- A relative
cd apps/<pkg>failed once (it ran during a persisted-cwd moment, not at the worktree root). - The builder over-corrected to absolute paths "to be safe" — and synthesized the main-checkout absolute path (
<repo-root>/apps/<pkg>), which is the worktree path minus the low-salience.builders/<id>/segment. Tests then ran against main's copy: 4 tests instead of the worktree's 7, and the builder's new files "missing".
The builder's role doc warns about this exact trap (codev-skeleton/roles/builder.md:93-99, "a path that drops the .builders/<id>/ segment silently reads and writes main's copy") — and the builder had demonstrably read it, quoting it in the post-mortem. The prose warning did not hold. Only a structural guard will.
The gap
Issue #1018 already built the structural guard for this threat model — .claude/hooks/worktree-write-guard.cjs, installed by every spawn, denying absolute Write/Edit paths that resolve outside the worktree. But its matcher is Write|Edit|MultiEdit only (packages/codev/src/agent-farm/utils/worktree-write-guard.ts:60,222). Bash is unguarded: cd <main-checkout>/… && npx jest passes untouched. Reads and executions against the wrong tree are exactly the uncovered half.
Why this class goes underreported
The failure is self-concealing. Running main's copy doesn't error — it yields plausible-but-wrong results (missing suites, stale behavior) that present as flaky tooling or stale caches, and the natural response is clear-cache-and-rerun. Because some commands in the same session use relative paths (correct tree) and some absolute (wrong tree), results flip-flop in a way that imitates flakiness perfectly. Sibling of the #1502 self-concealing class; expect more occurrences than reports.
Proposed fix: extend the #1018 guard to Bash
- Add
Bashto the PreToolUse matcher in the spawn-installed settings wiring. - In the hook, for Bash tool calls, scan the command string for absolute paths rooted at the main checkout that fall outside the worktree (the guard already derives both roots — the worktree path and its
.builders-parent). Deny on match. - Deny message must teach, not just refuse: name the offending path, state the worktree root, and say "use a relative path (cwd is your worktree) or the worktree-absolute form".
- Keep Builder worktree write-guard: prevent writes anchored at the main checkout root #1018's fail-open contract: any hook error allows the call — a safety net must never brick a builder session.
- Consider a narrow, explicit escape hatch (e.g. an env toggle) for the rare legitimate main-reference, so a deliberate act stays possible while the accidental one becomes loud.
This converts a silent wrong-tree run into a loud, self-explaining refusal — the visible-failure bound this class needs. The role-doc prose stays as the explanation; the hook becomes the enforcement.
Secondary observation (not the fix, but recordable)
The cwd instability that triggered the over-correction is worth a look of its own: from the builder's seat, "cwd persists between calls" and "cwd was reset" both happen, and the inconsistency is what made relative paths feel unsafe. If the reset-to-worktree-root behavior is harness-driven and reliable, the builder guidance can lean on it harder ("relative paths are always anchored correctly"); if it isn't reliable, that's its own bug.
Activity
amrmelsayed commentedon Aug 22, 2026
Frequency update, 2026-08-22 (owner report): builders keep hitting this — another instance today with the exact signature ("I accidentally ran against the main checkout, not my worktree — re-running in the worktree so my new test file is included"). That is at least the second confirmed occurrence in three days, in different workspaces, and the self-concealment argument in the body predicts more unreported ones. The prose warning demonstrably does not hold; the Bash-guard extension is the fix and this is now recurring, not hypothetical.
amrmelsayed commentedon Aug 22, 2026
On it! Working on this with the PIR protocol (plan + dev-approval gates before PR).
[PIR #1536] Plan draft
[PIR #1536] Plan revised: lexical-first Bash scan to avoid worktree-s…
[PIR #1536] Extend worktree guard to Bash: deny main-checkout paths i…
[PIR #1536] Test the Bash worktree guard (deny, allow, symlink, escap…
[PIR #1536] Document the Bash guard and per-command escape hatch in b…
[PIR #1536] Update builder thread log through implement phase
[PIR #1536] Replace Bash tokenizer with literal main-root search (no …
[PIR #1536] Test main-root search boundaries (sibling, prefix-superse…
[PIR #1536] Thread log: tokenizer -> literal main-root search
[PIR #1536] Plan draft
[PIR #1536] Plan revised: lexical-first Bash scan to avoid worktree-s…
[PIR #1536] Extend worktree guard to Bash: deny main-checkout paths i…
[PIR #1536] Test the Bash worktree guard (deny, allow, symlink, escap…
[PIR #1536] Document the Bash guard and per-command escape hatch in b…
[PIR #1536] Update builder thread log through implement phase
[PIR #1536] Replace Bash tokenizer with literal main-root search (no …
[PIR #1536] Test main-root search boundaries (sibling, prefix-superse…
[PIR #1536] Thread log: tokenizer -> literal main-root search
[PIR #1536] Merge origin/main into builder/pir-1536
[PIR #1536] Merge origin/main into builder/pir-1536
[PIR #1536] Thread log: relevance check + main merge (Spec 1658 integ…