Repository navigation
fix(bin): key primary-scope guard on physical location, not inherited env - #22
Merged
Merged
Conversation
fm_primary_scope_matches trusted FM_ROOT_OVERRIDE/FM_HOME alone, so a crewmate whose task worktree is a firstmate-repo checkout inherited those vars from its launch environment and was misclassified as PRIMARY, misfiring the turn-end/subagent guards inside the crew worktree. Now requires the git-dir of this library's own on-disk location (where the guarded script actually runs from) to match the claimed root's git-dir, so a leaked-env task worktree fails classification even though the override still points at a genuine primary home. fm-sessionstart-nudge.test.sh's primary/worktree fixtures updated to stage per-fixture script copies so scope matching stays keyed to real locations.
…ture guard bins in tests
sKeLe100
added a commit
that referenced
this pull request
Nov 23, 2026
… to fork contracts Test-only adaptations found in CI triage. No production code changes. - fm-stow-guard: run a staged copy of the guard from each fixture root's bin/, because the fork's primary-scope check (#22) keys on the guard's physical location; same pattern as fm-sessionstart-nudge. - fm-backend-t3code: give codex spawns an explicit --effort (the fork refuses a codex launch without one), and give the native-restart case a fake tmux pane so the fork's pre-arm host check (#31) passes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Fix primary-scope guard env leakage in bin/fm-primary-scope-lib.sh. Problem: a crewmate whose task worktree is a checkout of the firstmate repo itself inherits ambient FM_ROOT_OVERRIDE/FM_HOME from its launch environment; fm-primary-scope-lib.sh keyed its primary-scope match on those env values instead of the process's actual cwd/git-dir, so the crewmate was misclassified as the firstmate PRIMARY and the primary-only turn-end and subagent-dispatch guards misfired inside the crew worktree. Acceptance: (1) a failing-first colocated test simulating a firstmate-repo crew worktree with FM_ROOT_OVERRIDE/FM_HOME pointing at the real primary home, asserting fm-primary-scope-lib.sh classifies it as NOT primary; (2) fix scope match to key on the actual cwd/git-dir (the resolved repo root the guarded process is running in) rather than inherited env alone, so a process running in a treehouse/task worktree of the firstmate repo is never PRIMARY even when env points at the primary home; (3) the genuine primary session (cwd = the real primary checkout) must still classify as PRIMARY, keeping existing tests green; (4) considered whether fm-spawn should also sanitize these vars at crew launch as defense in depth. This is firstmate shared tracked material governed by firstmate-coding-guidelines.
What Changed
bin/fm-primary-scope-lib.shnow requires the physical location it is actually sourced from (the resolved parent ofbin/) to share a git dir with the scope root before any other check, so a crewmate task worktree of the firstmate repo that inheritsFM_ROOT_OVERRIDE/FM_HOMEfrom its launch environment is no longer classified as PRIMARY and the primary-only turn-end and subagent-dispatch guards stop misfiring there.fm_resolve_git_path, which resolves--git-dir/--git-common-dirto physical absolute paths (handling relative git paths andCDPATH), and reused it for the existing plain-checkout git-dir vs git-common-dir comparison so symlinked homes, leases, or temp dirs cannot defeat either match.tests/fm-primary-scope-lib.test.shcovering the env-leaked firstmate crew worktree, the genuine primary checkout, secondmate markers, and symlinked paths; updated the sessionstart-nudge and subagent-pretool-check tests to stage per-fixture guard bins so their scopes resolve by location.docs/turnend-guard.md(now the owner of the in-scope conditions), with pointers fromdocs/configuration.md,docs/scripts.md, anddocs/subagent-guard.md.Risk Assessment
✅ Low: The change adds a single, well-bounded location-vs-env invariant to one sourced predicate with correctly physicalized path comparison, every caller and fixture shape traced still classifies as before except the leaked-env crew worktree it targets, and the new colocated test is genuinely failing-first.
Testing
Ran the targeted primary-scope tests plus the two guard entrypoint suites, verified the new colocated test is genuinely failing-first against the pre-fix library, and captured a CLI transcript showing the real subagent-dispatch guard firing inside a firstmate-repo crew worktree before the fix and going inert after it while the genuine primary session stays guarded; the only failures are two Pi/TypeScript tests that fail identically at the base commit because this machine's Node cannot import .ts files.
Evidence: Crew-worktree vs primary guard transcript (pre-fix vs fixed library)
Source: Crew-worktree vs primary guard transcript (pre-fix vs fixed library)
BASE lib (bb69fc5) | cwd=primary -> exit 2 (guard fires) BASE lib (bb69fc5) | cwd=crew -> exit 2 (guard fires: THE BUG) FIXED lib (763caf0) | cwd=primary -> exit 2 (guard fires) FIXED lib (763caf0) | cwd=crew -> exit 0 (inert, not primary) Both runs export FM_ROOT_OVERRIDE=FM_HOME=<primary home>.Evidence: Failing-first proof for the new colocated test
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 4 issues found → auto-fixed (2) ✅
bin/fm-primary-scope-lib.sh:49- The new git-dir equality check makes fm_primary_scope_matches reject any invocation where the running script lives outside the root named by FM_ROOT_OVERRIDE/FM_HOME. tests/fm-subagent-pretool-check.test.sh invokes the REAL repo script (CHECK="$ROOT/bin/fm-subagent-pretool-check.sh", line 9) with FM_ROOT_OVERRIDE pointing at tmp fixtures ($PRIMARY line 49, $second line 214), so the guard now exits 0 instead of 2 for every deny/secondmate assertion in that file. Verified directly: sourcing bin/fm-primary-scope-lib.sh from this repo and calling fm_primary_scope_matches on a well-formed external fixture root returns 1. The nudge test was migrated to per-fixture script copies (tests/fm-sessionstart-nudge.test.sh:55) but fm-subagent-pretool-check.test.sh was not; apply the same staging there.bin/fm-primary-scope-lib.sh:46- Only the relative branch of each git-dir is normalized withpwd -P; an absolute git-dir (whatgit rev-parse --git-dirreturns for a linked worktree, e.g. a treehouse-leased secondmate home) is compared unresolved, while actual_git_dir is always physical. If the home is reached through a symlinked path (macOS /tmp -> /private/tmp, symlinked $HOME, symlinked treehouse lease), root_git_dir keeps the logical path and never equals the physical actual_git_dir, so a genuine primary/secondmate session silently classifies as NOT primary and the turn-end and subagent guards fail open — the exact blind window they exist to close. Physicalize both values in the absolute branch too (e.g.cd "$dir" && pwd -P).bin/fm-primary-scope-lib.sh:51- root_git_dir (line 44) and git_dir (line 51) run the identicalgit -C "$root" rev-parse --git-dir; the second call can reuse root_git_dir (comparing it against a similarly normalized git-common-dir), removing one subprocess and one normalization asymmetry.bin/fm-spawn.sh:2857- Acceptance item (4) asked whether fm-spawn should sanitize FM_ROOT_OVERRIDE/FM_HOME at crew launch as defense in depth. bin/fm-spawn.sh:2857 clears those vars only for KIND=secondmate; crew/scout launches still propagate the ambient values, which is the leak source this fix mitigates downstream. No code or note in the change records the outcome of that consideration — worth a one-line rationale in the lib header or commit trail so the deferred decision is traceable.🔧 Fix: physicalize git-dir resolution and stage per-fixture guard bins in tests
3 issues (2 warnings, 1 info) still open:
bin/fm-primary-scope-lib.sh:51- actual_dir is computed with a barecd "$(dirname "${BASH_SOURCE[0]}")/.."— noCDPATH=''and no--, unlike the guarded(CDPATH='' cd -- "$path" ... )in the new fm_resolve_git_path helper and unlike every caller's own SCRIPT_DIR computation (e.g. bin/fm-subagent-pretool-check.sh:173). When a hook is invoked by a relative path (BASH_SOURCE relative, as inbin/fm-turnend-guard.shrun from the home) and the captain's environment exports CDPATH,cdresolves through CDPATH and echoes the resolved directory to stdout, so the command substitution captures "<echoed path>\n<pwd>" or an unrelated directory. actual_git_dir then never equals root_git_dir and fm_primary_scope_matches returns 1, silently failing the turn-end, session-start and subagent guards open in a genuine primary home — the exact blind window this predicate exists to close. Fix:actual_dir=$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." 2>/dev/null && pwd -P) || return 1.tests/fm-sessionstart-nudge.test.sh:106- test_gate_env_is_silent (line 91) and test_gate_common_dir_is_silent (line 106) still invoke the real repo entrypoint$NUDGE(line 32) against tmp fixtures, while every other nudge assertion was migrated to per-fixture staged copies (run_nudge, line 63). Under the new predicate the real bin/ copy's git-dir never equals the fixture root's git-dir, so fm_primary_scope_matches returns 1 and the hook exits silently at line 21 of bin/fm-sessionstart-nudge.sh regardless of whether fm_is_gate_agent (line 20) works at all. Both assertions expect only silence, so they now pass with the gate-refuse logic they were written to protect fully broken — a regression in NO_MISTAKES_GATE or .no-mistakes common-dir detection would go undetected. Stage bin/ for those two fixtures (call run_nudge / stage_nudge_bin) so the gate branch is the thing actually exercised.tests/fm-sessionstart-nudge.test.sh:200- stage_run_bin doescp -R "$ROOT/bin/."(3.5 MB, 160 files) into each of ~14 run-tier fixtures, ~50 MB of copying per suite run. Pragmatic given the run wrapper's wide dependency fan-out, but if the suite gets slow, a hardlinked copy (cp -al) or an explicit dependency list would cut it; noting the tradeoff, no action required.🔧 Fix: guard CDPATH in scope match and de-vacuum gate nudge tests
✅ Re-checked - no issues remain.
bin/fm-test-run.sh tests/fm-primary-scope-lib.test.sh tests/fm-sessionstart-nudge.test.sh tests/fm-subagent-pretool-check.test.sh tests/fm-turnend-guard.test.shFailing-first check: replaced bin/fm-primary-scope-lib.sh with the bb69fc5 version and ranbash tests/fm-primary-scope-lib.test.sh(fails on the leaked-env crew case), then restored the fixed libManual end-to-end: clone of the repo as PRIMARY +git worktree addcrew worktree; rancd <crew> && FM_ROOT_OVERRIDE=<primary> FM_HOME=<primary> bin/fm-subagent-pretool-check.sh --claude --tool Agentand the same from <primary>, under both the pre-fix and fixed libraryBaseline comparison: ranbin/fm-test-run.sh tests/fm-turnend-guard.test.shandtests/fm-sessionstart-nudge.test.shin a checkout of base commit bb69fc5 to confirm the two Pi failures are pre-existingnode -e 'import("file:///tmp/t.ts")'to confirm ERR_UNKNOWN_FILE_EXTENSION is the root cause of the Pi test failuresdocs/turnend-guard.md:22- The intent's item (4) — whether bin/fm-spawn.sh should also sanitize FM_ROOT_OVERRIDE/FM_HOME at crew launch as defense in depth — was considered but not implemented in this change, and I did not document a decision that the code does not yet make. If the maintainers want that non-sanitization recorded as deliberate, its owner would be docs/turnend-guard.md's Guard Predicates section (or the spawn docs) once the decision is settled.✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.