Skip to content

Builder Bash commands can silently target the main checkout — extend the #1018 worktree guard to Bash (reads/executions, not just writes) #1536

Open

Description

@amrmelsayed

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:

  1. 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-").
  2. A relative cd apps/<pkg> failed once (it ran during a persisted-cwd moment, not at the worktree root).
  3. 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 Bash to 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

  1. added
    area/tower
    on Aug 20, 2026
  2. amrmelsayed commented on Aug 22, 2026

    @amrmelsayed
    CollaboratorAuthor

    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.

  3. amrmelsayed commented on Aug 22, 2026

    @amrmelsayed
    CollaboratorAuthor

    On it! Working on this with the PIR protocol (plan + dev-approval gates before PR).

  4. added 9 commits that reference this issue on Aug 22, 2026
  5. added 10 commits that reference this issue on Sep 1, 2026
  6. added 2 commits that reference this issue on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/tower

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions