Skip to content

fix(bin): guard worktree isolation by filesystem identity, not path text - #10

Merged
uayyagari merged 1 commit into
mainfrom
fix/spawn-isolation-guard-identity
Aug 20, 2026
Merged

uayyagari merged 1 commit into
mainfrom
fix/spawn-isolation-guard-identity

Conversation

@uayyagari

Copy link
Copy Markdown

Intent

Fix the worktree-isolation guard in bin/fm-spawn.sh so it cannot bless the primary checkout as an isolated task worktree on a case-insensitive filesystem (macOS default). Reported as issue kunchenguid#2654.

Problem

  • The guard decided "this launch worktree is not the primary checkout" with a string compare of two canonicalized paths.
  • bash's BUILTIN pwd -P resolves symlinks, ., .., and trailing slashes, but it does NOT case-fold (unlike /bin/pwd -P, which calls getcwd and does).
  • So on a case-insensitive filesystem the primary checkout reached under a different case survived canonicalization as unequal text and passed the guard.
  • Consequence: a crewmate branches and commits inside firstmate's primary checkout, the exact "tangle" this guard exists to prevent.
  • Reproduced end to end before changing anything: project arg .../PROJECT, pane never leaving the true-cased .../Project -> spawn exited 0 and recorded worktree=.../Project alongside project=.../PROJECT, the same directory.

Fix

  • Both comparisons in validate_spawn_worktree now test filesystem identity (device + inode) via test -ef instead of path text, so every alias of one directory is recognized as that directory.
  • Guard fails closed: test -ef is also false when a side cannot be stat'd, which would read as "distinct", so each side is confirmed to be an existing directory before any identity test runs. An unresolvable path now refuses the spawn where the old text compare passed it.
  • The error message gained a specific reason string while keeping the did not yield an isolated worktree substring that four existing suites assert on.
  • Also removes a false REJECTION the text compare had: a genuinely distinct worktree whose launch-path spelling differed from git's report of its root (git normalizes case, the pane read does not) was wrongly refused.

Deliberate scope decisions

  • Scoped to the isolation guard only, per the task. The same wrong-cased PROJ_ABS_REAL also feeds the treehouse settle loop's [ "$p_real" != "$PROJ_ABS_REAL" ] check, which is why the loop handed the primary checkout to the guard. Canonicalizing PROJ_ABS_REAL at its source would fix both and give a more precise diagnosis, but it is a behavior change beyond the guard and was deliberately left for a follow-up. The outcome is already safe: the loop's miscompare now ends in a refusal.
  • No symlink end-to-end regression case: verified empirically that pwd -P DOES resolve symlinks, so a symlinked alias never reaches the guard - the settle loop catches it 60s earlier via timeout. Case-folding was the only surviving gap. Keeping a symlink case would have asserted the guard's message for the wrong reason and cost 60s per run. The test comment records this.
  • No AGENTS.md change: per firstmate-coding-guidelines' knowledge-placement tree this is tier-7 mechanics, so the rationale lives in the script comment and the guarantee in the regression test, not in prose every session pays for.

Proof

  • Regression test added to the existing tests/fm-tangle-guard.test.sh (extending the suite that already covers this guard, per repo convention), asserting the case-variant primary checkout is refused with no meta recorded, and that a genuinely distinct worktree is still accepted.
  • Verified red/green: with the guard reverted the new test fails (expected exit 1, got 0); with the fix it passes.
  • The case-variant assertion self-skips with a note on a case-sensitive filesystem (Linux CI), where the two spellings are genuinely different directories and there is nothing to reject.
  • bin/fm-lint.sh exit 0 (shellcheck 0.11.0 pinned); bin/fm-doc-audience-check.sh ok.
  • Suites pass: fm-tangle-guard, fm-spawn-worktree-settle, fm-spawn-batch, fm-spawn-dispatch-profile, fm-backend-orca.

What Changed

  • Both comparisons in validate_spawn_worktree (bin/fm-spawn.sh) now test filesystem identity with test -ef (device + inode) instead of comparing canonicalized path text, so a case-variant alias of the primary checkout on a case-insensitive filesystem is recognized as the primary and refused rather than blessed as an isolated task worktree.
  • The guard fails closed: each side is confirmed to be an existing directory before any identity test runs, so an unresolvable path now refuses the spawn; it also drops a false rejection where a genuinely distinct worktree whose launch-path spelling differed from git's report was wrongly refused.
  • Added a regression test to tests/fm-tangle-guard.test.sh asserting the case-variant primary checkout is refused with no meta recorded and a distinct worktree is still accepted; the case-variant assertion self-skips on case-sensitive filesystems.

Risk Assessment

✅ Low: A tightly-scoped, fail-closed fix that replaces path-text comparison with device+inode identity, fully closing the case-fold gap from issue kunchenguid#2654 with a matching regression test and preserved error-substring contract.

Testing

Confirmed the temp filesystem is case-insensitive so the meaningful guard branch runs, ran the extended fm-tangle-guard suite (7/7 pass), proved the new regression test is genuine by reverting the guard and seeing the exact expected exit 1, got 0 failure before restoring, and drove the real fm-spawn.sh binary end-to-end to reproduce issue kunchenguid#2654: the case-variant primary checkout is now refused (exit 1, specific reason, no meta) while a distinct worktree still spawns cleanly. The four related spawn/backend suites also pass and the working tree is clean. This is a shell/CLI change with no rendered UI, so evidence is CLI transcripts rather than screenshots.

Evidence: End-to-end issue kunchenguid#2654 reproduction transcript (real fm-spawn.sh: case-variant primary refused, distinct worktree accepted)

$ fm-spawn.sh crew-2654 SPAWN-PROJ codex error: treehouse get did not yield an isolated worktree (launch path is the primary checkout under another name; ...); refusing to launch to avoid tangling the primary checkout. -> exit status: 1 -> meta recorded? no (spawn refused; primary checkout protected) $ fm-spawn.sh crew-ok spawn-proj codex (pane in isolated worktree spawn-wt) spawned crew-ok harness=codex ... worktree=.../spawn-wt -> exit status: 0 -> meta recorded? yes (spawn proceeded)

################################################################
# issue #2654 reproduction: case-variant primary checkout
# project arg : /var/folders/vr/_90lw3_s0bs_nw2f7b6t9yrm0000gn/T/tmp.LdftsZb5oN/e2e/SPAWN-PROJ   (upper-case alias)
# pane cwd    : /var/folders/vr/_90lw3_s0bs_nw2f7b6t9yrm0000gn/T/tmp.LdftsZb5oN/e2e/spawn-proj      (true case, never leaves primary)
# same dir on disk (test -ef)? YES (case-insensitive FS)
################################################################

$ fm-spawn.sh crew-2654 SPAWN-PROJ codex
warn: no registry at /var/folders/vr/_90lw3_s0bs_nw2f7b6t9yrm0000gn/T/tmp.LdftsZb5oN/e2e/home/data/projects.md; defaulting SPAWN-PROJ to no-mistakes off
error: treehouse get did not yield an isolated worktree (launch path is the primary checkout under another name; resolved '/var/folders/vr/_90lw3_s0bs_nw2f7b6t9yrm0000gn/T/tmp.LdftsZb5oN/e2e/spawn-proj'; worktree root '/private/var/folders/vr/_90lw3_s0bs_nw2f7b6t9yrm0000gn/T/tmp.LdftsZb5oN/e2e/spawn-proj'; primary '/var/folders/vr/_90lw3_s0bs_nw2f7b6t9yrm0000gn/T/tmp.LdftsZb5oN/e2e/SPAWN-PROJ'); refusing to launch to avoid tangling the primary checkout. Inspect target firstmate:fm-crew-2654
-> exit status: 1
-> meta recorded? no (spawn refused; primary checkout protected)

################################################################
# control: a genuinely distinct isolated worktree still works
################################################################

$ fm-spawn.sh crew-ok spawn-proj codex   (pane in isolated worktree spawn-wt)
warn: no registry at /var/folders/vr/_90lw3_s0bs_nw2f7b6t9yrm0000gn/T/tmp.LdftsZb5oN/e2e/home/data/projects.md; defaulting spawn-proj to no-mistakes off
spawned crew-ok harness=codex kind=ship mode=no-mistakes yolo=off window=firstmate:fm-crew-ok worktree=/var/folders/vr/_90lw3_s0bs_nw2f7b6t9yrm0000gn/T/tmp.LdftsZb5oN/e2e/spawn-wt
-> exit status: 0
-> meta recorded? yes (spawn proceeded)
Evidence: fm-tangle-guard suite (green, with fix)
ok - fm_primary_tangle_branch: feature branch alarms; default/detached/non-git stay silent
ok - fm-guard: bordered tangle banner fires only for a feature branch and suppresses repair commands in read-only mode
ok - fm-bootstrap: TANGLE problem line fires only for a feature branch and suppresses repair commands in detect-only mode
ok - fm-brief: ship brief asserts worktree isolation before the branch step
ok - fm-spawn: aborts unless the resolved worktree is a genuine, isolated worktree
ok - fm-spawn: isolation guard compares filesystem identity, not path text
ok - fm-spawn: appends windows by session-colon, pins the name, and targets the window id
Evidence: fm-tangle-guard suite (RED: guard reverted to old text compare — new test fails)

not ok - spawn must abort when the launch path is the primary checkout under another case: expected exit 1, got 0

ok - fm_primary_tangle_branch: feature branch alarms; default/detached/non-git stay silent
ok - fm-guard: bordered tangle banner fires only for a feature branch and suppresses repair commands in read-only mode
ok - fm-bootstrap: TANGLE problem line fires only for a feature branch and suppresses repair commands in detect-only mode
ok - fm-brief: ship brief asserts worktree isolation before the branch step
ok - fm-spawn: aborts unless the resolved worktree is a genuine, isolated worktree
not ok - spawn must abort when the launch path is the primary checkout under another case: expected exit 1, got 0

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-tangle-guard.test.sh (7/7 pass incl. new identity test)
  • Red/green: reverted guard -> new test fails expected exit 1, got 0; restored -> passes; tree clean
  • End-to-end repro against real bin/fm-spawn.sh: case-variant primary refused (exit 1, no meta), isolated worktree accepted (exit 0)
  • fm-spawn-worktree-settle, fm-spawn-batch, fm-spawn-dispatch-profile, fm-backend-orca all pass
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

… text

The spawn isolation guard decided "this launch worktree is not the primary
checkout" with a string compare of two canonicalized paths. bash's builtin
`pwd -P` resolves symlinks, `.`, `..`, and trailing slashes, but it does not
case-fold, so on a case-insensitive filesystem (macOS's default) the primary
checkout reached under a different case survived canonicalization as unequal
text and the guard blessed it as isolated. A crewmate then branched and
committed inside the primary checkout, the exact tangle this guard exists to
prevent.

Compare device and inode instead, so every alias of one directory is
recognized as that directory. Each side is confirmed to be an existing
directory first, because `test -ef` is also false when a side cannot be
stat'd and that would read as "distinct"; anything that cannot be proven
distinct is now refused rather than allowed. The identity compare also drops
a false rejection the text compare had: a genuinely distinct worktree whose
launch path spelling differed from git's report of its root.

Refs kunchenguid#2654
@uayyagari
uayyagari merged commit 70eda3d into main Aug 20, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant