Repository navigation
fix(spawn): refuse pool slots another task's meta still claims - #4108
jayjongcheolpark wants to merge 7 commits into
Conversation
A dead endpoint can make Treehouse offer a slot whose task metadata still owns the copy. Refuse fresh allocation before Treehouse runs when this home's records claim a registered project worktree, using the same metadata matcher as teardown. Relaunch retains its existing slot path. Treehouse exposes no slot exclusion selector, so the guard conservatively refuses rather than risking another task's files. Only teardown releases the claim. Cover the dead-endpoint case and preservation of its artifact, metadata, and HEAD, and retire seed task fixtures before slot reuse.
…med by task metas
…ied in the worktree (7 files: bin/fm-backend.sh, bin/fm-spawn.sh, bin/fm-install-treehouse.sh, tests/fixtures.sh, tests/fm-secondmate-harness.test.sh, tests/fm-spawn-dispatch-profile.test.sh, tests/fm-task-delivery.test.sh). 1. Lint (SC2100 on fm-spawn.sh `HERDR_LAUNCHER_RELATIONSHIP=other-home`): the new fm_meta_find_directory_claim introduced a variable named `other`, so ShellCheck (with CI's --external-sources) read the pre-existing literal as `other - home`. Renamed the loop variable to `claimant`. Full-mode lint over every changed file is clean. 2. Behavior tests (Herdr): four e2e spawns died with "treehouse status failed ... refusing Treehouse allocation". `treehouse status --json` exists only from Treehouse v2.1.0; CI pinned v2.0.1. Bumped bin/fm-install-treehouse.sh to v2.1.1 (the version the local real-Herdr suite already runs) with verified SHA-256s for all four assets. Installer exercised locally (installs v2.1.1, status accepts --json); tests/fm-backend-autodetect-smoke.test.sh passes against real Herdr + treehouse 2.1.1. 3. Portable parallel 2 / serial 3 / serial 4: fm-spawn.sh now runs treehouse on the spawn host, and the preflight sat before the missing-brief check. CI portable shards have no treehouse installed, so fm-spawn-batch got the status error instead of the brief error, and spawns in fm-task-delivery and fm-secondmate-harness (fakebins/restricted PATH without a treehouse fake) failed. Fixes: moved the status preflight to just before endpoint creation (still under the project lock, after brief/backlog refusals); added the shared no-op treehouse fake to the secondmate launch-capturing fakebin and task-delivery make_home. In fm-spawn-dispatch-profile the symlink-spelled second spawn reused the first task's fake worktree, which the new entered-slot guard correctly refuses (reproduced locally); it now gets its own worktree. Fixing that exposed a latent sibling in the same file (the Ultra batch test spawns two tasks sharing one fake pane path; previously masked because the suite exits on first failure). The spawn fake tmux in tests/fixtures.sh gained an opt-in FM_FAKE_PANE_PATHS_DIR mode (new-window returns the fm-<id> name as window id; pane path is <dir>/<id>) and the batch test gives each task its own worktree through it. Verified locally: lint clean; installer with new pin; fm-backend-autodetect-smoke; fm-spawn-pool-base-freshen (the PR's regression tests); fm-teardown-endpoint-safety; fm-test-fixtures; fm-spawn-batch and fm-task-delivery with treehouse removed from PATH. Still running when this result was demanded: fm-secondmate-harness (no-treehouse PATH) and the reworked fm-spawn-dispatch-profile; their earlier direct reruns confirmed the diagnosed error lines that these fixes target
…ered-slot guard correctly refusing in two test fixtures that bound one worktree to two task records; fixed both tests (2 files: tests/fm-spawn-dispatch-profile.test.sh, tests/fm-backend-herdr-presentation-e2e.test.sh). 1. Behavior portable serial 3: test_batch_forwards_shared_profile_flags spawned two tasks through one fake pane path, so the second spawn re-entered the first task's worktree and the guard refused (exit 1). Previously masked because the suite exits on first failure and the Ultra batch case (fixed in the prior commit) failed first. Fix mirrors that prior fix: each batch task gets its own git worktree via the fixture's FM_FAKE_PANE_PATHS_DIR mode. Verified locally: the full fm-spawn-dispatch-profile suite passes. 2. Behavior tests (Herdr): fm-backend-herdr-presentation-e2e failed at the post-legacy spawn with 'available slot .../project-166e25/2/project is still task p1's recorded worktree'. The retained multi-home workers (p1, p2, pcw) lose their processes when the restart fixtures stop/reprovision the Herdr session, so Treehouse reports their PROJECT_DIR slots available while their metas still claim them; the guard refuses by design (the PR had already moved the recovery fixtures to RECOVERY_PROJECT_DIR for the same reason). Fix: post-legacy now spawns into RECOVERY_PROJECT_DIR, with a comment explaining why. Verification limit: this e2e cannot run to completion on this macOS host even against CI-pinned Herdr 0.7.4 installed in a temp dir; it fails at workspace-ordering assertions near line 806 (long before the edited line 1380) that pass on CI's Linux runner, so the Herdr lane needs CI confirmation. Shellcheck clean on both files. 'Behavior portable parallel 1' was only cancelled by fail-fast, not a real failure
|
Speaking as Kun's firstmate: Diff review: preflight VISION per-rule: One captain/interface — aligns. Authority — aligns. Scripts vs agents — aligns. Restart — aligns. Delegation — aligns. Fleet/vendor — aligns. Scope — aligns. contract-class: restore — same promised exclusive-slot invariant as #2754 / #4093. overlap: Prefer helping #4093 (closes ready-for-pr #2754). Leave this open; if #4093 merges and this is leftover duplicate, we will close with thanks. Do not treat this comment as waiting on you for a captain decision. next: waiting-ci (checks already running). Not a captain hold. |
Intent
Captain asked (2026-09-09: "진행해주세요") to fix this firstmate defect: the Treehouse worktree pool hands a slot to a NEW spawn while another task's live state/.meta still records that slot as its worktree, so two task records end up bound to one worktree. Consequences observed:
The header of bin/fm-spawn.sh states that a project-identity lock held from allocation through metadata publication prevents reuse "before its owner record is published" - but reuse happens AFTER publication, when the owner's endpoint is dead and treehouse considers the slot free. Upstream fix: refresh pooled worktrees from authoritative task base #2448 "fix: refresh pooled worktrees from authoritative task base" is CLOSED unmerged; evaluate whether any of it is still relevant before designing. That change selected an authoritative local or remote default-branch base, handled local-only ancestry, and reset a clean allocated worktree to a frozen selected commit; it concerned base freshness after allocation.
Acceptance:
Firstmate's subsequent validation instruction expressly requests these constraints in the intent: keep the guard small and one-owner, sharing teardown's metadata matcher rather than adding a second scanner; release remains teardown-only; relaunch remains unaffected; code must be shellcheck-clean; test only isolated fixtures, never live task state or projects. Inspect Treehouse's available selection/exclusion API; if it cannot be steered, refuse loudly naming the claiming task. The PR body must state the verdict on #2448 and what, if anything, was taken from it. Deliver a PR to kunchenguid/firstmate with checks green; do not merge.
What Changed
bin/fm-spawn.shnow, under the existing project lock and beforetreehouse get, readstreehouse status --jsonand refuses a fresh spawn when any slot Treehouse reportsavailableis still recorded asworktree=by another task meta in the same home; the slottreehouse getactually enters is matched again before the copy is freshened or the task record is published. Both refusals name the claiming task and point atfm-crew-state.sh/fm-teardown.sh, change nothing in the pool or the owner record, and do not apply to relaunch or to slots Treehouse reports in use. The header offm-spawn.showns the rule.fm_meta_find_directory_claiminbin/fm-backend.shand shared bybin/fm-teardown.sh'srequire_exclusive_worktree_slot_record, so spawn and teardown run one scanner;jqjoins the tmux backend's required tools anddocs/configuration.mdrecords the toolchain delta.tests/fm-spawn-pool-base-freshen.test.shadds three fixture-only cases: a dead-endpoint claim on a Treehouse-available slot refuses at preflight, a claim on the slot entered after preflight refuses before publication, and a live claim on a different slot leaves the free slot allocatable. Existing cases now retire the first task's meta before respawning into the same slot.Verdict on #2448: not relevant to this defect and nothing was taken from it. It addressed base freshness after allocation, which
mainalready covers viafreshen_spawn_worktree_base(#2116); this change is about slot ownership before allocation, and it deliberately resets or releases nothing on refusal.Risk Assessment
✅ Low: Both guards are small, run before any state is published or reset, share teardown's matcher, leave relaunch and secondmate paths untouched, and the behavior-level tests separately pin the preflight, the post-get race, and the no-collision case; the only residual is a lost diagnostic on a failing treehouse status call.
Testing
On head
1852d546, Behavior portable parallel 1 ran all 11 tests with zero failures (FM_TEST_SUMMARY total=11 failed=0, duration 587726ms), then hit the job's 10-minute timeout at the summary/artifact step; every other job is green. A maintainer rerun of that job is requested.Ran the pool base-freshen suite with the three new guard regressions plus the teardown, bootstrap, backend, worktree-settle and relaunch suites; proved the regression tests fail at the base commit; and captured a before/after CLI transcript showing base binding two task records to one clean claimed slot while the target refuses loudly, names the claiming task and reconciliation path, and leaves the claimant's record and copy intact. All targeted tests pass except one relaunch case that fails identically at base from a host Node toolchain error.
Evidence: Clean claimed slot: double binding at base vs loud refusal at target
Source: Clean claimed slot: double binding at base vs loud refusal at target
### base 269f8fe (before fix) $ bin/fm-spawn.sh demo-fresh-spawn ... --scout spawned demo-fresh-spawn harness=codex kind=scout window=firstmate:fm-demo-fresh-spawn worktree=.../demo/pool exit=0 $ grep -l worktree=.../demo/pool state/.meta (records bound to slot 3) demo-fresh-spawn.meta held.meta ### target f3736d7 (after fix) $ bin/fm-spawn.sh demo-fresh-spawn ... --scout error: Treehouse allocation refused: available slot .../demo/pool is still task held's recorded worktree, so treehouse get could hand it out again. Reconcile with bin/fm-crew-state.sh held, then bin/fm-teardown.sh held when its work is landed; only teardown releases the slot. exit=1 $ grep -l worktree=.../demo/pool state/.meta (records bound to slot 3) held.metaEvidence: New guard regression tests run against base commit scripts (fail before fix)
Source: New guard regression tests run against base commit scripts (fail before fix)
Evidence: fm-spawn-pool-base-freshen suite at target
Source: fm-spawn-pool-base-freshen suite at target
Evidence: fm-teardown-endpoint-safety suite at target (shared matcher refusals)
Source: fm-teardown-endpoint-safety suite at target (shared matcher refusals)
Evidence: fm-bootstrap suite at target (tmux now requires jq)
Source: fm-bootstrap suite at target (tmux now requires jq)
Evidence: fm-control-relaunch suite at target and at base (pre-existing Node/Volta failure)
Source: fm-control-relaunch suite at target and at base (pre-existing Node/Volta failure)
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-spawn.sh:2207- The preflight refuses a fresh spawn whenever ANY registered worktree of the project is claimed by ANY other task meta in the home, with no regard to whether that slot is one Treehouse would hand out. Concrete trace: task A is live in pool slot 1 (state/A.meta worktree=<slot1>, endpoint alive, Treehouse status 'in use'); slot 2 is free. Spawn B enters the loop,git worktree listyields slot1, fm_meta_find_directory_claim matches A.meta, and B exits 1 with 'Treehouse allocation refused'. So a home can never run two pooled tasks concurrently, which is the normal operating state (the intent's own 2026-09-09 example had four tasks in one home). This contradicts acceptance 1's preferred behavior ('skip that slot and take another; if none is free, refuse loudly') and acceptance 4 ('No behavior change for spawns when no claim exists' on the slot being taken). The header's justification ('another slot being free does not make allocation safe') only holds for claimed slots Treehouse considers available; a slot with a live endpoint is never offered bytreehouse get. The edits to the existing tests (rm of the prior task's .meta at lines 187 and 534 of the test file) are symptoms of this regression, not teardown modelling. Treehouse exposesstatus --json(name/path/status per slot, which the new test even fakes but the guard never consults). Remedy: refuse only when a slot Treehouse reports asavailable(or, minimally, one without a live endpoint) is claimed by another meta; otherwise proceed. This changes the guard's product behavior relative to the author's stated conservative design, so it needs the author's decision rather than an auto-fix.tests/fm-spawn-pool-base-freshen.test.sh:697- The test installs a faketreehouse status --jsonreporting the claimed slot as available and asserts on its output ('fixture must offer the claimed slot'), but nothing in bin/fm-spawn.sh callstreehouse status; the assertion only checks the fixture against itself and proves nothing about the guard's handling of Treehouse availability. The test also does not cover the no-collision concurrent case required by acceptance 3 (a live task claiming slot N while the spawn takes a different free slot), which is exactly the case the guard currently breaks. Once the guard is narrowed to Treehouse-available slots (see the error finding), make the guard consume this status output and add the concurrent no-collision case; otherwise drop the self-referential assertion.tests/fm-spawn-pool-base-freshen.test.sh:714- The 'guard ran after allocation' check is wrapped inif [ -f launch.log ], so on the expected refusal path (no send-keys recorded, file absent) it is skipped and the ordering claim is unverified by this assertion; the artifact and HEAD checks that follow are what actually carry the 'before allocation' evidence. Consider asserting the log is absent or contains notreehouse getunconditionally.🔧 Fix: Refuse only Treehouse-available slots still claimed by task metas
2 infos still open:
bin/fm-spawn.sh:2202- The guard only checks slots thattreehouse status --jsonreports as available at preflight time. Concrete trace: task A's slot isin-usewhen status is read (A's shell still alive), so it is skipped; A's window then closes during the seconds between this preflight and thetreehouse getsent at line 3074 (the spawn waits up to 60s for get to enter a slot). Treehouse now judges A's slot free and can hand it out while state/A.meta still records it as worktree=, reproducing the double binding the intent forbids. The pre-get check is the right primary guard and covers the reported 2026-09-09 shape; closing this window would need a post-allocation check of the slottreehouse getactually entered (at validate_spawn_worktree, line 3133) against fm_meta_find_directory_claim, refusing loudly without releasing anything. That remedy extends the change beyond the accepted narrow preflight, so it needs the author's authorization rather than an auto-fix; it also cannot undo the reset get already performed, only prevent the second owner record.bin/fm-spawn.sh:2206-jq ... 2>/dev/nullswallows jq's own diagnostics, so a missing jq binary (now required for the tmux backend, but only checked at bootstrap, not at spawn) and genuinely non-JSON status output both surface as 'treehouse status for <proj> was not pool JSON'. Verified with the real CLI that treehouse prints[]for an empty pool and sends its update notice to stderr, so the JSON path is sound; the message is only wrong when jq itself is absent. Drop the2>/dev/nullon the jq pipeline (or checkcommand -v jqfirst) so the operator sees the real cause.🔧 Fix: Recheck entered slot claim after treehouse get
3 issues (1 warning, 2 infos) still open:
tests/fm-spawn-pool-base-freshen.test.sh:719- test_claimed_pool_refuses_before_allocation no longer proves the preflight guard exists. Its fake pane path is POOL_DIR, the same slot held.meta claims, so with the preflight block (bin/fm-spawn.sh lines 2213-2227) deleted entirely the post-get recheck at line 3142 refuses with the identical 'task held's recorded worktree' text and identical exit status. The other assertions do not separate the two paths either: the fake tmux only appends send-keys arguments that follow -l to FM_FAKE_LAUNCH_LOG, and 'treehouse get' is sent via fm_backend_tmux_send_text_line without -l, so launch.log is absent on both paths (the late-claim test asserts the same absence after get has run, which confirms this); the fake treehouse get resets nothing, so the HEAD and artifact checks pass on both paths too. The primary guard the author accepted is therefore untested against removal. Fix: assert the preflight wording, e.g. assert_contains "$out" 'available slot' and assert the 'entered slot' text is absent in this test (and the inverse in the late-claim test), so each guard's test fails when that guard is removed.bin/fm-spawn.sh:2197- On the post-get refusal the spawn has already opened window $T and its shell has runtreehouse get, so an orphaned shell sits inside the claimed slot (Treehouse now reports it in-use). The sibling refusal in validate_spawn_worktree ends with 'Inspect target <window>' so the operator can find and close it; refuse_claimed_spawn_slot prints no window id. Add the endpoint to the message on the post-get call (the preflight call has no window yet), matching the neighbouring refusals' contract that a refusal leaving an endpoint behind names it.tests/fm-spawn-pool-base-freshen.test.sh:188- The round-1 decision asked to revert the test edits that rm the prior task's .meta before a second spawn enters the same pooled worktree; they are still present here and at line 534. With the post-allocation recheck the round-2 decision then authorized, they are now required: these suites drive two consecutive spawns into the same POOL_DIR via FM_FAKE_PANE_PATH, and the recheck correctly refuses the second one while the first task's record still claims the slot. The rm models teardown-only release, which is the intended invariant, so keeping them is correct; noting it only because it contradicts the earlier instruction.🔧 Fix: Name orphan window on late refusal; split guard tests
1 info still open:
bin/fm-spawn.sh:2213-treehouse status --json 2>/dev/nulldiscards treehouse's own diagnostics, so when status genuinely fails (corrupt pool state, unreadable project) the operator only sees 'treehouse status failed for <proj>' with no cause. Silencing is understandable because treehouse prints its update notice to stderr on every call, but the diagnostic is lost exactly when it matters. Capture stderr into a variable (or temp file) and print it only on the failure branch.tests/fm-control-relaunch.test.sh- tests/fm-control-relaunch.test.sh fails one case on this host ('a relaunch must not re-run a transition the row already reflects') because the backlog read inside the relaunch path hits Volta's 'No version is set for command node'. The identical error occurs at base commit 269f8fe (there it surfaces as 'a same-harness relaunch should succeed'), so it is a local environment issue and not caused by this change; every relaunch guard case in the suite that this change could affect passes. CI owns the authoritative run.bash tests/fm-spawn-pool-base-freshen.test.sh- all 25 cases pass at f3736d7, including the three new guard cases: preflight refusal of a claimed available slot, post-get refusal of a slot claimed after preflight, and concurrent allocation beside a live task on another slotFail-before-fix: the three new test functions run against base 269f8fe scripts - both refusal cases fail (base proceeds past the guard), the no-collision case passes on both (log: fail-before-fix.log)Manual before/after reproduction of the 2026-09-09 shape with a clean slot:bin/fm-spawn.sh ... --scoutwith state/held.meta claiming the slot and faketreehouse status --jsonreporting it available; base publishes demo-fresh-spawn.meta beside held.meta (double binding), target exits 1 with the refusal naming task held and only held.meta remains boundbash tests/fm-teardown-endpoint-safety.test.sh- 16 cases pass, including test_reused_pool_slot_refuses_before_touching_the_other_task and test_cross_home_pool_slot_collision_refuses, which exercise the teardown refusal now built on fm_meta_find_directory_claimbash tests/fm-bootstrap.test.sh- 28 cases pass, including the per-backend required-tools case covering the new jq requirement for tmuxbash tests/fm-backend.test.sh(28 pass) andbash tests/fm-spawn-worktree-settle.test.sh(4 pass) - neighbours of validate_spawn_worktree and fm_backend_required_toolsbash tests/fm-control-relaunch.test.sh- 51 pass, 1 pre-existing host Node/Volta failure reproduced at base 269f8febash bin/fm-test-run.sh --changed --base 269f8fe --listto pick the targeted neighbouring suites✅ **Document** - passed
✅ No issues found.
🔧 **Lint** - 1 issue found → auto-fixed (2) ✅
🔧 Fix: Lint passes with pinned tools; no source changes needed
1 warning still open:
🔧 Fix: Lint passes with pinned tools; no source changes needed
✅ Re-checked - no issues remain.
✅ **Push** - passed
✅ No issues found.