Repository navigation
fix(bin): refuse spawn worktrees outside the project's own repository - #2562
Open
SilvioLima94 wants to merge 14 commits into
Open
SilvioLima94 wants to merge 14 commits into
SilvioLima94 wants to merge 14 commits into
Conversation
SilvioLima94
force-pushed
the
fm/fm-spawn-isolation-a1
branch
from
August 18, 2026 04:44
7dcdb4e to
4bab329
Compare
This was referenced Aug 22, 2026
The worker-isolation assertion proved only that the resolved worktree was a real git top-level distinct from the project's primary checkout. Any other repository satisfied that, so a spawn whose pane never left the firstmate home recorded worktree=<firstmate home> and passed, which would have launched a project worker straight into firstmate's own primary checkout. The settle loop's two-consecutive-reads guard does not cover it: a pane that never moved agrees with itself. Prove repository membership positively instead. validate_spawn_worktree now resolves the physical git common directory of both the project and the worktree and requires them to be equal, so one repository's checkout and its linked worktrees are accepted and every other repository is refused by name. An identity that cannot be established on either side - not a repository, a failed query, empty or unexpected output, an unresolvable path - refuses the launch rather than passing as inconclusive. The check sits in the single assertion every call site already shares, so the treehouse-get path (tmux, herdr, zellij, cmux), the Orca worktree-create path, and relaunch are all covered; secondmate spawns skip the assertion as before. tests/fm-spawn-worktree-identity.test.sh covers the firstmate-home misrouting case, wrong-repo in general, all three unreadable-identity shapes, the unreadable project side, and both Orca call-site directions, and asserts a legitimate worktree still spawns so the fix cannot pass by refusing everything.
…tion docs with the code
…isolation contract
SilvioLima94
force-pushed
the
fm/fm-spawn-isolation-a1
branch
from
August 23, 2026 19:43
4bab329 to
b608a60
Compare
Confidence Score: 5/5The PR appears safe to merge, with no concrete blocking or independently actionable new defect remaining after review. The changed spawn paths fail closed when repository identity or isolation cannot be established, while the pooled-home lifecycle preserves ownership, lease, and recovery state across tested success and failure paths. Reviews (1): Last reviewed commit: "no-mistakes(document): require project r..." | Re-trigger Greptile |
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
Close a safety hole in validate_spawn_worktree() in bin/fm-spawn.sh: the worker-isolation assertion accepts a worktree belonging to the wrong repository, including firstmate's own home.
The defect: validate_spawn_worktree() asserts that the worktree path resolves, is a real git top-level, that the resolved path equals that top-level, and that it is not the project's own primary checkout. It never asserts that the worktree belongs to the project's repository. Found live dispatching a scout task: a spawn whose terminal never left the firstmate home recorded worktree=/Users/young/work/firstmate and passed, because the firstmate home is a valid git top-level distinct from the project clone. The worker would have been launched against firstmate's own primary checkout, which AGENTS.md sections 7 and 8 exist to prevent. The two-consecutive-reads guard does not cover this: it defends against a transient stale path, and a pane that never moved trivially agrees with itself. Call sites to keep correct: the Orca path, the relaunch path, and the treehouse get path.
Required outcomes:
This change touches firstmate's shared tracked material, so the firstmate-coding-guidelines skill's knowledge-placement, one-owner, compatibility, and repo-style rules are binding.
Later accepted corrections, decided by the captain, to be carried in this run:
Preserve all existing safety contracts. Do not merge; the captain approves the merge personally.
Further accepted requirements after the first PR went red in upstream CI (PR #2562, head 7dcdb4e6, 12 of 13 checks passed):
FIX IN THIS TASK - a regression this branch introduced. The check "Behavior portable serial 1" failed because tests/fm-gotmp.test.sh reports "not ok - teardown exited non-zero with a valid tasktmp". Root cause established: bin/fm-teardown.sh line 192 now unconditionally sources bin/fm-home-return-lib.sh, but the fake root built by tests/fm-gotmp.test.sh symlinks each library teardown sources and was never given the new one, so the real teardown subprocess fails at source time and exits non-zero. Evidence that this is ours: upstream CI on the exact base commit 64d61ae is green, the test file is byte-identical between base and this branch, and running the test from a clean export reproduces "teardown exited non-zero with a valid tasktmp" on this branch while base fails only a different, macOS-local assertion. Fix it so the fixture provides every library the real teardown requires, following the pattern and commentary already used for the other symlinked siblings in that fixture, and check whether any other test fixture enumerating teardown's sourced libraries needs the same addition.
DO NOT FIX HERE, keep this PR scoped - tests/fm-pi-watch-extension.test.sh reported "not ok - Pi must deliver the actionable wake after bounded hung-successor recovery: expected exit 0, got 1" in the same CI shard. This branch touches no Pi, watcher, or wake code (the changed files are bin/fm-ff-lib.sh, bin/fm-home-return-lib.sh, bin/fm-home-seed.sh, bin/fm-spawn.sh, bin/fm-teardown.sh, bin/fm-test-isolation-proof.sh, bin/fm-test-run.sh, two docs, one skill pointer, and the two new identity test files), and the test passes from a clean export of this branch, so it is not attributable to this change and is being recorded as a separate follow-up item. Do not expand this task to chase it.
Validate the final head exactly once, push normally to the existing fork branch, and do not merge; the captain approves the merge personally.
What Changed
validate_spawn_worktree()inbin/fm-spawn.shnow proves repository identity instead of only checking worktree shape: it compares the physically resolved--git-common-dirof the task worktree against the project's, requires the project directory to be its own repository's top level (so the identity is not walked up to from a subdirectory), and separately refuses any worker root that is a firstmate home (active home, firstmate repo root, a directory carrying the seeded-home marker, or the holder of this fleet'sstate/data/config/projectsdirectories). Every unresolvable path, git failure, or unexpected output fails closed with a diagnostic naming what could not be established; secondmate spawns still skip the assertion.bin/fm-home-return-lib.showns the shared "clear a leased home's identity, then return it to the treehouse pool" transaction used by both retirement (bin/fm-teardown.sh) and failed-seed rollback (bin/fm-home-seed.sh), staging owned artifacts aside and restoring them if the return fails. Its outcome codes distinguishFM_HOME_RETURN_NOT_OWNED— proven foreign ownership, the only refusal on which a pooled lease may be released — from refusals that leave this run's own marker on the slot.bin/fm-spawn.shalso drops its duplicated operational-directory validator in favor ofvalidate_operational_dirsfrombin/fm-ff-lib.sh.tests/fm-spawn-worktree-identity.test.shandtests/fm-teardown-home-identity.test.sh(wrong-repo, firstmate-home misrouting, identity-unknown, and legitimate-worktree cases), registered both inbin/fm-test-run.shfamilies andbin/fm-test-isolation-proof.sh, taughtfamilies_for_changed_path()that teardown/home-return changes re-selectsession-bootstrap, and gave thetests/fm-gotmp.test.shfake root thefm-home-return-lib.shsymlink the real teardown subprocess now requires at source time. Docs inAGENTS.md,docs/architecture.md,docs/configuration.md,docs/verification/runtime-backends.md, and the project-management skill were updated for the repository-identity requirement and repository-root-only project registration.Risk Assessment
Testing
I reproduced the reported defect end-to-end before validating the fix: running the new identity suite against a clean export of the head with only bin/fm-spawn.sh reverted to base shows the old assertion accepting firstmate's own home and printingspawned ... worktree=<firstmate home>. On the fixed head the same scenario is refused with a specific non-zero diagnostic naming the repository mismatch, and I captured the actual operator-facing CLI refusal text for all cases — foreign repository, unreadable/empty/garbage worktree identity, unresolvable project identity, self-hosted firstmate-home signals, and the corrected non-top-level project-registration wording that no longer accuses a foreign repository — alongside the two accept cases that still spawn, so the fix cannot pass by refusing everything. The Orca and relaunch call sites are driven by their own cases; the tmux/herdr/zellij/cmux backends share the single treehouse-get path that the suite drives with a fake tmux. I also reproduced the CI regression (teardown exiting non-zero because the fake root lacked fm-home-return-lib.sh) and confirmed the fixture fix clears it, and swept the tests tree to confirm no other fixture enumerates teardown's sourced libraries. Related existing spawn, relaunch, teardown, and tangle-guard suites pass; two suites fail one assertion each, but identically on the base commit, so they are pre-existing macOS-local failures rather than regressions. This is a shell/CLI change with no rendered UI surface, so the reviewer-visible evidence is CLI transcripts rather than screenshots. Lint was not run — it belongs to a different phase.Evidence: Pre-fix reproduction: base assertion accepts the firstmate home
not ok - spawn accepted the firstmate home as the task worktree --- output --- spawned identity-firstmate-home-i1 harness=codex kind=ship mode=no-mistakes yolo=off window=firstmate:fm-identity-firstmate-home-i1 worktree=/var/.../identity-firstmate-home/homeEvidence: Post-fix CLI refusal transcript (operator-visible diagnostics for every identity case)
===== case: identity-firstmate-home-i1 (exit=1) ===== error: treehouse get yielded a worktree that belongs to a different repository (resolved '.../identity-firstmate-home/home'; its repository '.../identity-firstmate-home/home/.git'; the project's repository '.../identity-firstmate-home/project/.git'); refusing to launch a worker for .../project outside that project's own repository. ===== case: identity-unreadable-fail-i3 (exit=1) ===== error: the repository identity of treehouse get's worktree '.../wt' could not be established (its git common directory did not resolve); refusing to launch without proof it belongs to the project's repository '.../project/.git'. ===== case: identity-legit-worktree-i5 (exit=0) ===== spawned identity-legit-worktree-i5 harness=codex kind=ship mode=no-mistakes yolo=off window=firstmate:fm-identity-legit-worktree-i5 worktree=.../identity-legit-worktree/wtEvidence: fm-gotmp CI regression: before/after the fixture fix, with base for comparison
### Before the fixture fix (branch bin/ + base tests/fm-gotmp.test.sh) - the CI regression not ok - teardown exited non-zero with a valid tasktmp ### After the fixture fix (branch HEAD) not ok - teardown did not remove the tasktmp dir (... still exists) ### Base commit d023c45 for comparison (same macOS-local removal assertion) not ok - teardown did not remove the tasktmp dir (... still exists)Evidence: New spawn worktree-identity suite result on the fixed head
Evidence: Teardown home-identity suite result on the fixed head
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
tests/fm-gotmp.test.sh:80- The intent's required fix is missing from the change. Criterion: "FIX IN THIS TASK - a regression this branch introduced. ... bin/fm-teardown.sh line 192 now unconditionally sources bin/fm-home-return-lib.sh, but the fake root built by tests/fm-gotmp.test.sh symlinks each library teardown sources and was never given the new one ... Fix it so the fixture provides every library the real teardown requires, following the pattern and commentary already used for the other symlinked siblings in that fixture." tests/fm-gotmp.test.sh is not in this branch's diff and contains no reference to fm-home-return-lib.sh. Both fixture builders (make_fake_root, ending at line 80, and the inline root in test_teardown_skips_gracefully_without_tasktmp, ending at line 158) symlink every other sourced library but not the new one. bin/fm-teardown.sh:158 sets SCRIPT_DIR from dirname "$BASH_SOURCE" without resolving the symlink, so it points at the fake bin/; with set -eu (line 156), the source at line 192 fails and the subprocess exits non-zero. All three cases fail, reproducing "not ok - teardown exited non-zero with a valid tasktmp". Fix: add ln -s "$ROOT/bin/fm-home-return-lib.sh" "$fake/bin/fm-home-return-lib.sh" to both builders with the sibling commentary. No other fixture needs it - tests/fm-gotmp.test.sh is the only file matching 'ln -s .bin/fm-.-lib.sh', and the three fixtures that copy bin/ wholesale (fm-teardown.test.sh:1545, fm-herdr-session-cleanup.test.sh:287, fm-claude-stop-autoarm-live-e2e.test.sh:45) pick the library up automatically.bin/fm-test-run.sh:943- The new families_for_changed_path arm maps bin/fm-home-return-lib.sh to pr-forge and secondmate only, but tests/fm-gotmp.test.sh - which drives the real bin/fm-teardown.sh subprocess and is the test this branch broke - is mapped to session-bootstrap (bin/fm-test-run.sh:180). bin/fm-teardown.sh itself maps to pr-forge only (line 932), so no targeted family run selected by either the library or teardown ever runs the fixture that enumerates teardown's sourced libraries. That is precisely why the breakage surfaced only in the full portable-serial lane. Consider adding session-bootstrap to the bin/fm-home-return-lib.sh arm (and, if the author agrees, to the bin/fm-teardown.sh arm) so a change to teardown's sourced-library set re-selects the fixture that models it. Flagging rather than fixing because the enumeration is a deliberate authoring decision and the teardown side is pre-existing.bin/fm-ff-lib.sh:152- validate_secondmate_home_shape() and VALIDATED_MARKER_ID were extracted in commit c4976f9 so bin/fm-spawn.sh's isolation gate could require a full seeded-home shape; commit 8071387 reverted that approach ("the marker alone is enough"), and grep across bin/ and tests/ shows the only caller left is validate_secondmate_home() at line 225 in the same file. The split now buys nothing and silently reorders a refusal diagnostic for ff-lib's five consumers (fm-config-push.sh:173, fm-stow-cascade.sh:201, fm-fleet-snapshot.sh:1179, fm-bootstrap.sh:382/430, ff-lib's own process_secondmate:427): a home marked for another secondmate that also lacks AGENTS.md previously reported "marked for secondmate X, expected Y" and now reports "not a firstmate home (missing AGENTS.md)", because the id comparison moved after the home-material checks. No existing test asserts that combination (fm-secondmate-safety.test.sh:1401 goes through fm-spawn.sh's own validate_firstmate_home_for_spawn, which kept the original order), so nothing breaks - but folding the shape function back into validate_secondmate_home would drop the indirection and restore the original diagnostic precedence.tests/fm-teardown-home-identity.test.sh:1- The file is committed as mode 100644 while 147 of the 149 tests/*.test.sh files are 100755 (the only other 644 is the pre-existing tests/fm-ask-user-authority.test.sh). bin/fm-test-run.sh invokes scripts with bash "$script" (lines 1556, 1663), so CI still runs it, but invoking ./tests/fm-teardown-home-identity.test.sh directly - the way its own header and the sibling identity test invite - fails with permission denied. chmod +x for consistency.🔧 Fix: restore teardown fixture libs, family map, and ff-lib shape
1 info still open:
tests/fm-gotmp.test.sh:141- test_teardown_skips_gracefully_without_tasktmp() builds its fake root inline with a library symlink list that is byte-for-byte the same set as make_fake_root()'s (I diffed the two extracted lists: identical, 14 entries plus the three stubs). The only real difference is the meta block, which omits tasktmp=. That duplication is exactly what made this round's regression fix a two-place edit, and it is the same shape that let the fixture drift behind bin/fm-teardown.sh's source list in the first place: a future library added to teardown will again silently break only whichever copy is forgotten. Simplification: have test_teardown_skips_gracefully_without_tasktmp call make_fake_root and then rewrite $fake/state/$id.meta without the tasktmp= line, leaving one authoritative enumeration of teardown's sourced siblings. Non-functional; the assertions and the real-subprocess exercise are unchanged.tests/fm-backend-orca.test.sh- tests/fm-backend-orca.test.sh ("Orca spawn should fail when metadata cannot be written"), tests/fm-teardown.test.sh ("herdr-preflight-missing-adapter"), and tests/fm-gotmp.test.sh ("teardown did not remove the tasktmp dir") each fail one assertion locally on macOS. All three fail identically on the base commit d023c45 from a clean export, so they are pre-existing environment-local failures, not introduced by this change. No action taken.bash tests/fm-spawn-worktree-identity.test.shon HEAD — 18/18 pass (firstmate-home, wrong-repo, unreadable/empty/garbage worktree identity, unreadable project identity, self-hosted home signals, non-top-level project, relaunch path, Orca path, two accept cases)Pre-fix repro: cleangit archive HEADexport withbin/fm-spawn.shreplaced bygit show d023c451:bin/fm-spawn.sh, thenbash tests/fm-spawn-worktree-identity.test.sh→not ok - spawn accepted the firstmate home as the task worktreebash tests/fm-teardown-home-identity.test.shon HEAD — all pass, including the lease-preserving vs. FM_HOME_RETURN_NOT_OWNED release casesCI-regression repro: clean HEAD export with basetests/fm-gotmp.test.sh→not ok - teardown exited non-zero with a valid tasktmp; with the fixture fix that assertion passesbash tests/fm-gotmp.test.shon HEAD and on a clean base d023c45 export — identical remaining failure (teardown did not remove the tasktmp dir), confirming it is pre-existinggrep -rn 'ln -s .*bin/fm-' tests/*.shand a sweep for teardown's sourced libs — fm-gotmp.test.sh is the only fixture symlinking teardown's librariesbash bin/fm-test-run.sh tests/fm-spawn-worktree-settle.test.sh tests/fm-spawn-batch.test.sh tests/fm-spawn-pool-base-freshen.test.sh tests/fm-backend-orca.test.sh tests/fm-control-relaunch.test.sh tests/fm-teardown.test.sh tests/fm-teardown-endpoint-safety.test.sh tests/fm-tangle-guard.test.sh tests/fm-trace-context-spawn.test.shbash bin/fm-test-run.sh tests/fm-backend-orca.test.sh tests/fm-teardown.test.shon a clean base d023c45 export — same two assertions failCaptured the real CLI refusal transcript for every identity case by running the suite with the per-case spawn output echoed to stderrdocs/scripts.md:87- docs/scripts.md does not list the new shared library bin/fm-home-return-lib.sh, though peer shared libs (fm-lock-lib.sh, fm-config-inherit-lib.sh) are listed. Left unchanged deliberately: that table is already non-exhaustive (33 of 43 bin scripts are absent, including recently added ones like fm-procevent-lib.sh and fm-transition-lib.sh), no drift check enforces coverage, and adding a row here would be an addition rather than a staleness fix. Worth a separate decision on whether that inventory is meant to be complete.✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.