Repository navigation
fix(bin): extend worktree-isolation guards to scout briefs and teardown - #1915
eduardstan wants to merge 3 commits into
Conversation
|
Automated reminder: thanks for the PR! This branch currently has a merge conflict with the base branch. When you get a chance, please rebase onto (or merge) the latest base branch, resolve the conflict, and push. After that, checks will re-run and the PR will get looked at again. Noted for firstmate#1915 at |
954cb14 to
31c7607
Compare
9f06a89 to
ad7f176
Compare
…, the project clone A scout brief invites the worker to install, run, and make scratch commits in its worktree, and teardown discards that worktree. When the recorded worktree is the project's own clone, all three become operations on the captain's primary checkout, and a read-only scout still reports a clean result from unisolated code. Two guards close that: - The scout scaffold now carries the same isolation assertion ship briefs have, ahead of the scratch-worktree invitation. Both scaffolds render one shared assertion, so they cannot drift. - Cleanup refuses outright, --force included, when a ship or scout task's recorded worktree resolves to the same path as its project clone. Only a secondmate home is legitimately its own worktree. The refusal lands before any endpoint kill, worktree return, or state removal. Closes kunchenguid#1746.
ad7f176 to
43d065d
Compare
|
A maintenance note across our open PRs, and an offer. All fourteen of the PRs we have open here were rebased onto current Separately, while investigating a red check on #1449 we measured something you may want independent of any of our PRs: the |
Closes #1746.
The four suggested directions
The issue proposes four. Three are now in place; the fourth is deliberately out of scope.
1. Give the scout scaffold the same isolation assertion ship briefs have. Done, in
bin/fm-brief.sh.Rather than copying the ship wording, both scaffolds now render one shared
ISOLATION_ASSERTIONstring,so they cannot drift apart again — drift is how this defect arose in the first place. The assertion is
placed ahead of the scout's "the worktree is your laboratory — install, run, edit, and make scratch
commits freely" invitation, and its STOP clause was widened from "do not branch or commit here" to
"do not branch, commit, install, or write anything here", because a scout is invited to install and run,
not only to commit.
2. Make
fm-spawnfail closed for--scoutwhen it cannot resolve a worktree distinct from theproject clone. This already holds on
main:validate_spawn_worktree()inbin/fm-spawn.shrefuseswhen the resolved worktree path equals the primary checkout path, and it has no ship/scout branching.
No production change was needed and none was made. What this PR adds is a test that pins the behaviour
on the scout path so it cannot regress — see the note on the near-duplicate test below.
3. Have teardown refuse outright when the recorded worktree resolves to the same path as the project
clone. Done, in
bin/fm-teardown.sh. The refusal lands before any endpoint kill, worktree return, orstate removal, and
--forcedoes not bypass it: discard authority covers a task's own work, never theclone that work came from.
kind=secondmateis exempt because a secondmate home legitimately is itsown worktree —
bin/fm-spawn.shis the only production writer ofworktree=equal toproject=.4. Reconcile the recorded worktree against the pane's real
cwdat spawn time — deliberately leftout. It is a genuinely different failure mode: not "the clone was recorded", but "a real worktree was
recorded and the agent then did not adopt it" (row 1 of the issue's evidence table). Closing it needs
per-backend live
cwdpolling at spawn, which is a different change with a different risk profile anddeserves its own review. The two guards here already make every row in that table safe to clean up,
which was the issue's own stated bar for direction 3. The leaked pool slot the issue notes under
consequence 2 is attributed upstream to #1441 and is likewise not addressed here.
Why this is not theoretical
On 2026-08-06 a scout in our fleet created a
scratch/directory in the captain's primary checkoutand reported a clean result from unisolated code. It was read-only, so nothing was lost. It surfaced only
because that particular brief happened to demand a path line — nothing in the tooling would have caught
it, which is precisely the gap direction 1 closes.
The test that fails when the guard is removed
tests/fm-scout-isolation.test.shis new, registered inbin/fm-test-run.sh. Deleting theclone-as-worktree guard from
bin/fm-teardown.shand re-running the suite fails exactly one case:The other three cases continue to pass, so the failure isolates the guard rather than the fixture.
A note on one near-duplicate test
Automated review flagged
test_scout_spawn_refuses_a_path_that_is_not_its_own_worktreeas duplicatingtest_spawn_isolation_abortintests/fm-tangle-guard.test.sh, on the grounds thatvalidate_spawn_worktreeis kind-agnostic and so--scoutadds no line coverage. That observation iscorrect, and we kept the case anyway — so the reasoning should be visible rather than left as an
unexplained near-duplicate.
The case does not exist to cover lines; it exists to pin that the guard applies on the scout path
specifically. The guard being kind-agnostic today is exactly what makes the pin cheap now and valuable
later: the day someone adds a kind-aware or scout-only branch, this is the test that fails. Coverage
cannot express that property, because the lines are the same lines — what is being pinned is which
callers reach them. And the defect being fixed here is itself a rule enforced on one path and absent
from another, so a differential pin is the natural anti-regression for the whole class. If you would
rather see it as a
--scoutrow on the existing table infm-tangle-guard.test.sh, or droppedoutright, say so and we will move it.
One limit this change does not close
A linked git worktree does not isolate a third-party tool that resolves the repository root through the
.gitfile pointing back into the primary clone. We hit this while preparing this contribution: aTypeScript build run inside a correctly isolated worktree wrote its cache directory into the primary
clone anyway, at the minute it ran. Every path check — including the ones added here — passes in that
case, because the worktree genuinely is isolated by every measure
pwd -Pandgit rev-parsecan see.This change closes the spawn and cleanup paths. It does not close that one, and no assertion of this
shape can.
What Changed
bin/fm-brief.shnow builds the worktree-isolation assertion once and emits it in both the ship and scout scaffolds, so a scout stops with ablocked: launched in primary checkout, not an isolated worktreestatus before it branches, commits, or installs; the shared wording also covers installing/writing, and the scout's "laboratory" invitation is gated behind verifying isolation first.bin/fm-teardown.shresolves a task's recordedworktree=andproject=withpwd -Pand refuses with aREFUSED:message and exit 1 when they are the same path for a ship or scout task, before any process kill, worktree return, or state removal, and--forcedoes not bypass it; secondmate teardowns are exempt because a secondmate home legitimately is its own worktree.tests/fm-scout-isolation.test.sh(registered in thebackend-dispatchfamily inbin/fm-test-run.sh) covering the scout brief assertion, scout spawn refusal on a non-worktree path, and teardown refusal on a clone with and without--force; existing teardown fixtures intests/fm-public-followup.test.shandtests/fm-remote-secondmate-parent-binding.test.shwere updated to record a worktree distinct from the project clone, andAGENTS.md/docs/architecture.mddocument both guards.Risk Assessment
✅ Low: The source change is narrow and fail-closed - the teardown guard refuses before any destructive step while preserving all task state, the brief refactor is a text extraction with no behavior change, and an exhaustive scan confirmed the fix round's fixture sweep left no teardown-invoking test with worktree= equal to project=; the remaining findings are test-selection and test-duplication issues that are safe to address as follow-ups.
Testing
Exercised the change the way an operator hits it: generated a real scout brief and drove fm-teardown.sh on a task whose meta records the project clone as its worktree, at both the base and target commits. Before the fix the scout brief carried no isolation assertion andfm-teardown.sh <id> --forcecompleted, runningtreehouse return --force <project clone>, killing the window and deleting task state; after the fix the brief opens with the isolation assertion and teardown refuses with a clear REFUSED message both plain and under --force, executing no runtime command and preserving the meta. The new fm-scout-isolation suite passes and fails as expected against base bin/, the runner registers it under backend-dispatch, and the neighbouring brief/teardown/secondmate suites touched by the meta-fixture changes all pass. No UI surface is involved — this is CLI/brief-generation behavior, so evidence is CLI transcripts and generated brief text rather than screenshots.Evidence: Teardown refuses the project clone as discard target (base vs target, plain and --force)
=== BEFORE fix (70aeba8) — with --force === teardown scout-1746 complete (window firstmate:fm-scout-1746, worktree /tmp/fm1746-before-force.c7Q5X9/project) runtime commands run : 2 -> treehouse return --force /tmp/fm1746-before-force.c7Q5X9/project;tmux kill-window -t =firstmate:=fm-scout-1746; task meta : REMOVED === AFTER fix (954cb14) — with --force === REFUSED: scout-1746 records the project clone '/tmp/fm1746-after-force.LZLonx/project' as its worktree, so cleanup would discard the primary checkout; preserving task state. Correct worktree= in .../state/scout-1746.meta before tearing down. [exit 1] runtime commands run : 0 -> task meta : preservedEvidence: Generated scout brief now opens with the isolation assertion
$ bin/fm-brief.sh scout-1746 my-repo --scout # Setup section of data/scout-1746/brief.md # Setup You are in a disposable git worktree of my-repo, at a detached HEAD on a clean default branch. Verify isolation before anything else. Runpwd -Pandgit rev-parse --show-toplevel; both must resolve to the disposable task worktree you were launched in ... not the primary checkout firstmate operates from. The path check is authoritative:git rev-parse --git-dirandgit rev-parse --git-common-dircan help inspect the repo, but they do not prove you are outside the primary checkout. If the top-level path is the primary checkout or not the worktree you were launched in, STOP - do not branch, commit, install, or write anything here - appendblocked: launched in primary checkout, not an isolated worktreeto the status file and stop. This is a SCOUT task: the deliverable is a written report, not a PR. Once isolation is verified, the worktree is your laboratory - install, run, edit, and make scratch commits freely; all of it is discarded at teardown.Evidence: Same scout brief at base commit — no assertion (regression proof)
not ok - scout brief has no isolation assertion (missing: 'Verify isolation before anything else') (base brief Setup section: "...This is a SCOUT task... The worktree is your laboratory - install, run, edit, and make scratch commits freely" with no isolation check)Evidence: Manual repro script used for the teardown transcript
Pipeline
Updates from git push no-mistakes
⏭️ **intent** - skipped
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-teardown.sh:415- The new clone-as-worktree refusal fires on eleven pre-existing kind=ship teardown fixtures that use worktree= == project= as shorthand. tests/fm-public-followup.test.sh writes such metas at lines 599, 623, 732, 765, 800, 832, 862, 926, 964, 1000 and 1031 and then runs $TEARDOWN, asserting rc 0 (line 807) or a specific different refusal ("still owes a public reply", "cannot resolve the primary home"); tests/fm-remote-secondmate-parent-binding.test.sh:227 does the same in run_child_teardown with worktree=project=$REMOTE_HOME/projects/alpha and asserts rc 0. All of them now exit 1 with the new REFUSED before reaching the behavior under test. Give those fixtures a worktree path distinct from project; the guard behavior itself is correct.AGENTS.md:491- AGENTS.md still says "Every ship brief must retain the worktree-isolation assertion and stop if launched in the primary checkout." Scout briefs now carry the same assertion, but the scout scaffold guard is prompt-level with no runtime enforcement, so an agent editing a generated scout brief per section 11 has no contract requiring it be kept - and stripping it re-opens the exact fm-spawn --scout runs in the primary project clone, and the recorded worktree does not match where the agent actually is #1746 condition. Extend that line to cover scout briefs.bin/fm-teardown.sh:419- The commit says "Closes fm-spawn --scout runs in the primary project clone, and the recorded worktree does not match where the agent actually is #1746", but the issue's consequence 2 (a treehouse worktree allocated, recorded in worktree=, never adopted by the agent, and never reclaimed) is unaddressed. The new refusal also means an affected task cannot be torn down at all - pane, status file, meta and any leased pool slot persist until an operator hand-edits worktree=. The issue itself attributes the leak to Worktree allocation can hand a still-owned pool slot to a new spawn and destroy unlanded work #1441, so this is plausibly a deliberate split; confirm the closure scope is intended.🔧 Fix: give teardown fixtures worktrees distinct from project
1 warning still open:
docs/architecture.md:149- The "Worktrees, not branches in your checkout" section enumerates the isolation guarantees (line 139: fm-spawn refuses a path that is not a worktree distinct from the project; line 149: "Ship briefs also tell the crewmate to verifypwd -Pandgit rev-parse --show-toplevel"), but the change adds two guarantees to that same set without recording either: scout briefs now carry the identical assertion (bin/fm-brief.sh:302-311, shared by both scaffolds), and fm-teardown.sh now refuses outright - --force included - when worktree= resolves to project= (bin/fm-teardown.sh:412-424). AGENTS.md:491 was updated for the scout brief in the same change, so the doc set is now internally inconsistent, and the strongest of the three guards is undocumented outside the script header. Extend line 149 to "Ship and scout briefs" and add a sentence for the teardown refusal next to line 139. Same wording gap at AGENTS.md:401 ("generated ship brief").🔧 Fix: document scout brief and teardown isolation guards
3 issues (2 warnings, 1 info) still open:
bin/fm-test-run.sh:196- tests/fm-scout-isolation.test.sh is classified into the backend-dispatch family, but the two scripts whose guards it pins map elsewhere in families_for_changed_path: bin/fm-teardown.sh emits pr-forge (line 919) and bin/fm-brief.sh emits pure-contract-unit (line 943). The scoped selector at line 1053 keeps only tests whose family_for_basename equals a selected family, so a later commit touching only bin/fm-teardown.sh runs pr-forge and skips this file entirely - the clone-as-worktree refusal and its --force case go unverified. Same for bin/fm-brief.sh and the scout-brief assertion test. Only a bin/fm-spawn.sh edit or a full run reaches it. Give bin/fm-teardown.sh and bin/fm-brief.sh their own arms that also emit backend-dispatch, following the two-family precedent already used for bin/fm-nm-run-lib.sh at line 923.tests/fm-scout-isolation.test.sh:66- test_scout_spawn_refuses_a_path_that_is_not_its_own_worktree duplicates tests/fm-tangle-guard.test.sh:190 test_spawn_isolation_abort, which already spawns with a pane resolving to a plain non-git dir and to $proj/sub inside the primary checkout, asserting both 'did not yield an isolated worktree' and that no meta is recorded. make_spawn_fakebin at line 47 is a byte-level copy of fm-tangle-guard.test.sh:156. validate_spawn_worktree (bin/fm-spawn.sh:1398) has no kind branching and is reached only from the shared treehouse and orca paths, so --scout exercises the same code with no added coverage; the fm_git_worktree call at line 74 is dead fixture setup on top of that. Prefer adding a --scout row to the existing table in fm-tangle-guard.test.sh, or drop the duplicated case and helper here and keep this file to the genuinely new brief and teardown guards.tests/fm-scout-isolation.test.sh:33- The scout-brief test asserts only that the isolation assertion appears somewhere in the generated brief. Move $ISOLATION_ASSERTION (bin/fm-brief.sh:321) below the 'the worktree is your laboratory - install, run, edit, and make scratch commits freely' line and all three assert_contains checks still pass, while the verify-before-you-install guarantee this change exists to create is gone. The ship counterpart already models the fix at fm-tangle-guard.test.sh:141-145: compare the assertion's line number against the laboratory line and require it to come first.✅ **Test** - passed
✅ No issues found.
bash tests/fm-scout-isolation.test.sh(4 checks: scout brief assertion, scout spawn refusal + no metadata, teardown refusal for ship and scout, teardown refusal under --force)Regression proof for the brief:git checkout 70aeba8 -- bin/fm-brief.sh bin/fm-teardown.shthen re-ran the new suite — fails withnot ok - scout brief has no isolation assertion; restored to 954cb14 and it passesManual end-to-end teardown repro/tmp/no-mistakes-evidence/01KZETM5M48WP38K4F0Q3B91WR/repro-teardown-clone.shrun against base and target bin/fm-teardown.sh, with and without--force, using logging tmux/treehouse stubsGenerated a real scout brief:bin/fm-brief.sh scout-1746 my-repo --scoutand inspected the Setup section of data/scout-1746/brief.mdShip-brief diff: generatedbin/fm-brief.sh ship-x my-repo --mode local-onlyat base vs target — only the widened STOP sentence differsbash tests/fm-brief.test.sh,bash tests/fm-teardown.test.sh,bash tests/fm-teardown-endpoint-safety.test.sh,bash tests/fm-public-followup.test.sh,bash tests/fm-remote-secondmate-parent-binding.test.sh,bash tests/fm-secondmate-safety.test.sh— all passbin/fm-test-run.sh --list --family backend-dispatchlists tests/fm-scout-isolation.test.sh✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.