From cb22dd10116f3742f87e28c509ac370f5f39e2e0 Mon Sep 17 00:00:00 2001 From: QIanGua <15757826110@163.com> Date: Mon, 28 Sep 2026 15:20:40 +0800 Subject: [PATCH 1/4] Fix reassigned teardown slot collisions (cherry picked from commit 115333b6a59dfffb2b5fef7eabb1a9dae76e2bc1) --- bin/fm-teardown.sh | 28 +++++++++++++++-------- tests/fm-teardown-endpoint-safety.test.sh | 8 +++++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/bin/fm-teardown.sh b/bin/fm-teardown.sh index 869b705e345..8799820ca2e 100755 --- a/bin/fm-teardown.sh +++ b/bin/fm-teardown.sh @@ -83,10 +83,12 @@ # name a slot a DIFFERENT live task now holds. Cleanup kills every process under # that path and hard-resets it before returning it, so releasing a slot that is # not genuinely this task's destroys another worker's live work. Before the first -# cleanup step, teardown verifies record exclusivity: no OTHER task record in -# this home or any locally registered Firstmate home may name the same live path -# in its worktree= or home=. One live path with two task records is the reuse -# collision itself, whichever record is stale. +# cleanup step, teardown first reads the slot's owner claim. A claim naming +# another task proves reassignment and skips every slot step. Otherwise teardown +# verifies record exclusivity: no OTHER task record in this home or any locally +# registered Firstmate home may name the same live path in its worktree= or +# home=. One live path with two task records is the reuse collision itself, +# whichever record is stale. # That scan alone cannot prove THIS record is the current owner, because the task # that took the slot next may leave no record it can reach - its own worker may # have exited and its record been cleaned up, or it may live in a home this @@ -2262,9 +2264,10 @@ require_exclusive_task_worktree_slot() { # Positive slot ownership, read from the claim the task that took the slot wrote # into the slot itself (bin/fm-wake-lib.sh owns the claim and its states). # -# The record scan above proves that no OTHER task record names this slot. It -# cannot prove that THIS record is not the stale one, because the task that took -# the slot next may leave no record this scan can reach: its own worker may have +# For a claim naming this task, or an absent claim, the record scan proves that +# no OTHER task record names this slot. That scan cannot prove this record still +# owns it, because the task that took the slot next may leave no record this +# scan can reach: its own worker may have # exited and its record been cleaned up, or it may belong to a home this machine # does not register. The claim closes that gap from the other side - it names the # task that actually took the slot, and it is written under the same project lock @@ -2842,11 +2845,14 @@ preflight_descendant_treehouse_slots() { continue fi fm_backend_validate_task_endpoint "$meta" "$task_id" --allow-cleared || return 1 - require_exclusive_worktree_slot_record "$meta" "$task_id" "$state" "$worktree" || return 1 owner_rc=0 require_owned_worktree_slot_record "$task_id" "$worktree" || owner_rc=$? case "$owner_rc" in - 0|"$TEARDOWN_SLOT_REASSIGNED_RC") ;; + 0) + require_exclusive_worktree_slot_record \ + "$meta" "$task_id" "$state" "$worktree" || return 1 + ;; + "$TEARDOWN_SLOT_REASSIGNED_RC") ;; *) return 1 ;; esac done @@ -3189,8 +3195,10 @@ remove_secondmate_registry_entry() { return "$rc" } -require_exclusive_task_worktree_slot || exit 1 require_owned_task_worktree_slot || exit 1 +if teardown_owns_worktree; then + require_exclusive_task_worktree_slot || exit 1 +fi validate_pr_poll_cleanup "$STATE" "$ID" || exit 1 diff --git a/tests/fm-teardown-endpoint-safety.test.sh b/tests/fm-teardown-endpoint-safety.test.sh index 4002cf4df3e..8984fe926ed 100755 --- a/tests/fm-teardown-endpoint-safety.test.sh +++ b/tests/fm-teardown-endpoint-safety.test.sh @@ -486,6 +486,7 @@ test_reused_pool_slot_refuses_before_touching_the_other_task() { fm_write_meta "$dir/home/state/$other.meta" \ "window=firstmate:fm-$other" "endpoint_task_id=$other" \ "worktree=$dir/worktree" "project=$dir/project" "kind=scout" + claim_pool_slot "$dir" "$id" # Staged in this shell, not a command substitution: a background child of a # $(...) subshell does not outlive it, and the point of this worker is to be # alive in the slot while teardown runs. @@ -502,6 +503,9 @@ test_reused_pool_slot_refuses_before_touching_the_other_task() { assert_present "$dir/worktree/sentinel" "teardown reset a pool slot a second task record still holds" assert_present "$dir/home/state/$other.meta" "teardown removed the live task's record" assert_present "$dir/home/state/$id.meta" "teardown removed the stale task's record before refusing" + assert_present "$dir/pool/1/.fm-slot-owner" "teardown removed its own claim before refusing" + assert_contains "$(cat "$dir/pool/1/.fm-slot-owner")" "task=$id" \ + "teardown rewrote its own claim before refusing" [ ! -s "$dir/runtime.log" ] \ || fail "teardown reached the runtime on a contested pool slot: $(cat "$dir/runtime.log")" assert_contains "$(cat "$dir/stderr")" "$other" \ @@ -891,6 +895,9 @@ test_reassigned_pool_slot_finishes_own_cleanup_without_touching_the_slot() { fm_write_meta "$dir/home/state/$id.meta" \ "window=firstmate:fm-$id" "endpoint_task_id=$id" \ "worktree=$dir/worktree" "project=$dir/project" "kind=scout" + fm_write_meta "$dir/home/state/$other.meta" \ + "window=firstmate:fm-$other" "endpoint_task_id=$other" \ + "worktree=$dir/worktree" "project=$dir/project" "kind=scout" claim_pool_slot "$dir" "$other" "$dir/other-home" # Staged in this shell, not a command substitution: a background child of a # $(...) subshell does not outlive it, and the point of this worker is to be @@ -906,6 +913,7 @@ test_reassigned_pool_slot_finishes_own_cleanup_without_touching_the_slot() { [ "$rc" -eq 0 ] || fail "teardown of a task whose slot was reassigned failed: $(cat "$dir/stderr")" kill -0 "$worker" 2>/dev/null || fail "teardown killed the worker holding the reassigned pool slot" assert_present "$dir/worktree/sentinel" "teardown reset a pool slot another task had claimed" + assert_present "$dir/home/state/$other.meta" "teardown removed the slot owner's record" assert_reassigned_slot_left_alone "$dir" "$id" "$other" "dirty reassigned slot with --force" assert_contains "$(cat "$dir/stderr")" "$dir/other-home" \ "the warning should name the claimant's home" From 54cd08949f6acfa0fde53e0a074f1579a826dd86 Mon Sep 17 00:00:00 2001 From: QIanGua <15757826110@163.com> Date: Mon, 28 Sep 2026 15:34:47 +0800 Subject: [PATCH 2/4] no-mistakes(document): Document claim-first pool-slot teardown behavior (cherry picked from commit dcbe51a6f0581ebf7713faf3822c9a567a44da33) --- bin/fm-teardown.sh | 18 +++++++++--------- docs/architecture.md | 5 +++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/bin/fm-teardown.sh b/bin/fm-teardown.sh index 8799820ca2e..c662935543d 100755 --- a/bin/fm-teardown.sh +++ b/bin/fm-teardown.sh @@ -83,19 +83,19 @@ # name a slot a DIFFERENT live task now holds. Cleanup kills every process under # that path and hard-resets it before returning it, so releasing a slot that is # not genuinely this task's destroys another worker's live work. Before the first -# cleanup step, teardown first reads the slot's owner claim. A claim naming +# cleanup step, teardown reads the slot's owner claim. A claim naming # another task proves reassignment and skips every slot step. Otherwise teardown # verifies record exclusivity: no OTHER task record in this home or any locally # registered Firstmate home may name the same live path in its worktree= or -# home=. One live path with two task records is the reuse collision itself, -# whichever record is stale. +# home=. For a slot with this task's claim or no claim, two records naming one +# live path remain a refusal, whichever record is stale. # That scan alone cannot prove THIS record is the current owner, because the task # that took the slot next may leave no record it can reach - its own worker may # have exited and its record been cleaned up, or it may live in a home this # machine does not register - which is how a released-then-reassigned slot was -# returned out from under a live worker (observed 2026-09-07). So teardown also -# reads the slot's own owner claim, written by bin/fm-spawn.sh at the moment the -# slot is taken and dropped here once it is genuinely returned; bin/fm-wake-lib.sh +# returned out from under a live worker (observed 2026-09-07). Bin/fm-spawn.sh +# writes the slot's owner claim when the slot is taken, and teardown drops it +# once the slot is genuinely returned; bin/fm-wake-lib.sh # owns the claim, its location, and its states. A claim naming another task is # proof of reassignment: the slot is no longer this task's, so teardown warns, # names the claimant, and then finishes only this task's own cleanup - endpoint, @@ -2267,9 +2267,9 @@ require_exclusive_task_worktree_slot() { # For a claim naming this task, or an absent claim, the record scan proves that # no OTHER task record names this slot. That scan cannot prove this record still # owns it, because the task that took the slot next may leave no record this -# scan can reach: its own worker may have -# exited and its record been cleaned up, or it may belong to a home this machine -# does not register. The claim closes that gap from the other side - it names the +# scan can reach: its own worker may have exited and its record been cleaned up, +# or it may belong to a home this machine does not register. The claim closes +# that gap from the other side - it names the # task that actually took the slot, and it is written under the same project lock # that allocates it - so a claim naming another task is proof the slot was # reassigned after this record was written. diff --git a/docs/architecture.md b/docs/architecture.md index f3528815f73..95e785c929a 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -362,8 +362,9 @@ After the forge accepts firstmate's merge request, the merge path persists the r A later merged poll consumes only that matching persisted value; with no match it records the landing as external rather than consulting a live away-posture record that may have been archived or replaced. [`bin/fm-merge-authority-lib.sh`](../bin/fm-merge-authority-lib.sh)'s header owns resolution, private atomic persistence, identity-checked consumption, and retirement, while only the merge path gates on the answer. Teardown is fail-closed for ship worktrees: dirty worktrees refuse, and committed work must be landed before the worktree is returned. -A pool worktree is only returned after teardown passes the slot-ownership proof: a contradictory task record or a supported live endpoint refuses without touching either task, and no discard authority relaxes that. -A slot's own owner claim, written by the spawn that takes it under the allocation lock and owned by [`bin/fm-wake-lib.sh`](../bin/fm-wake-lib.sh), covers a slot reassigned to a task that left no record the scan could reach: a claim naming a different task releases nothing - teardown warns, names the claimant, and finishes only the task's own cleanup - because Treehouse's own live process lease cannot answer ownership once the worker's exit releases it. +Teardown reads a pool slot's owner claim before checking whether other task records name its live path: a claim naming another task leaves that slot untouched while this record's own cleanup continues, even when both records name the slot. +For a claim naming this task or no claim, a conflicting record still refuses teardown, even with `--force`; endpoint identity checks remain independent. +[`bin/fm-teardown.sh`](../bin/fm-teardown.sh)'s header owns the full proof, and [`bin/fm-wake-lib.sh`](../bin/fm-wake-lib.sh) owns the claim. Allocation and return serialize on one project lock per machine-local Firstmate tree: every home reachable through local parent links shares that lock, and a home seeded from another machine anchors its own, because a lock taken on this filesystem is neither held nor observable across that boundary. Before the worktree is returned, teardown concludes the task's own no-mistakes run when it is parked at a gate, including a run whose head the task copy cannot resolve - the shared runs-ledger continuation proof is the only recognition for that case, so cleanup never orphans a parked run the pipeline advanced past the submitted head. [`bin/fm-teardown.sh`](../bin/fm-teardown.sh)'s header owns the landed-work proofs, slot-ownership proof, endpoint-close refusal, PR-discovery fallback, pre-teardown run conclusion, and stale-lock recovery procedure; [`tests/fm-teardown-endpoint-safety.test.sh`](../tests/fm-teardown-endpoint-safety.test.sh) and [`tests/fm-secondmate-safety.test.sh`](../tests/fm-secondmate-safety.test.sh) pin the slot-collision boundary. From 77029dae583d2e3f6d24870bf3e5b3308a3c493f Mon Sep 17 00:00:00 2001 From: QIanGua <15757826110@163.com> Date: Mon, 28 Sep 2026 15:46:31 +0800 Subject: [PATCH 3/4] no-mistakes(document): Confirm teardown documentation reflects slot ownership behavior (cherry picked from commit bdcedcf1e8fe8b37fa26ac36e3a5f6baeb31adc9) --- tests/fm-teardown-endpoint-safety.test.sh | 45 ++++++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/tests/fm-teardown-endpoint-safety.test.sh b/tests/fm-teardown-endpoint-safety.test.sh index 8984fe926ed..8195a23b221 100755 --- a/tests/fm-teardown-endpoint-safety.test.sh +++ b/tests/fm-teardown-endpoint-safety.test.sh @@ -885,7 +885,7 @@ assert_reassigned_slot_left_alone() { # } test_reassigned_pool_slot_finishes_own_cleanup_without_touching_the_slot() { - local dir id=stale-task other=reassigned-task worker rc + local dir id=stale-task other=reassigned-task worker rc owner_head # Dirty slot, --force, and a live worker inside it: --force authorizes # discarding this task's unlanded work, which is already gone with the slot, @@ -920,20 +920,30 @@ test_reassigned_pool_slot_finishes_own_cleanup_without_touching_the_slot() { kill "$worker" 2>/dev/null || true wait "$worker" 2>/dev/null || true - # The same reassignment on a CLEAN slot: a landed ship task torn down without - # --force, which is the shape of the real incident. A clean, fully landed copy - # passes every unlanded-work check, so only the ownership determination can - # keep this slot out of the pool; a guard keyed off dirtiness would return it - # and destroy the live task's copy. + # The reported collision: a finished ship record and another ship record + # name one clean slot. The claimant's committed work is not landed, so its + # record cannot be torn down just to unblock the finished task. The finished + # task must complete without --force or touching the claimant's copy. dir=$(make_case slot-reassigned-clean) mark_case_as_treehouse_pool "$dir" rm -f "$dir/worktree/sentinel" + git -C "$dir/worktree" -c user.name=test -c user.email=test@example.invalid \ + commit --allow-empty -qm claimant-unlanded-work + owner_head=$(git -C "$dir/worktree" rev-parse HEAD) + ! git -C "$dir/worktree" merge-base --is-ancestor "$owner_head" \ + "$(git -C "$dir/project" rev-parse HEAD)" \ + || fail "claimant fixture unexpectedly landed its work" [ -z "$(git -C "$dir/worktree" status --porcelain)" ] \ || fail "clean-slot fixture is not clean: $(git -C "$dir/worktree" status --porcelain)" fm_write_meta "$dir/home/state/$id.meta" \ "window=firstmate:fm-$id" "endpoint_task_id=$id" \ "worktree=$dir/worktree" "project=$dir/project" "kind=ship" + fm_write_meta "$dir/home/state/$other.meta" \ + "window=firstmate:fm-$other" "endpoint_task_id=$other" \ + "worktree=$dir/worktree" "project=$dir/project" "kind=ship" claim_pool_slot "$dir" "$other" "$dir/other-home" + cp "$dir/home/state/$other.meta" "$dir/claimant-meta-before" + cp "$dir/pool/1/.fm-slot-owner" "$dir/claimant-claim-before" ( cd "$dir/worktree" && exec sleep 30 ) & worker=$! @@ -945,7 +955,30 @@ test_reassigned_pool_slot_finishes_own_cleanup_without_touching_the_slot() { set -e [ "$rc" -eq 0 ] || fail "teardown of a clean ship task whose slot was reassigned failed: $(cat "$dir/stderr")" kill -0 "$worker" 2>/dev/null || fail "teardown killed the worker holding the clean reassigned pool slot" + assert_present "$dir/home/state/$other.meta" "teardown removed the claimant's unlanded task record" + cmp -s "$dir/claimant-meta-before" "$dir/home/state/$other.meta" \ + || fail "teardown changed the claimant's task record" + cmp -s "$dir/claimant-claim-before" "$dir/pool/1/.fm-slot-owner" \ + || fail "teardown changed the claimant's slot claim" + [ "$(git -C "$dir/worktree" rev-parse HEAD)" = "$owner_head" ] \ + || fail "teardown changed the claimant's unlanded checkout" + [ -z "$(git -C "$dir/worktree" status --porcelain)" ] \ + || fail "teardown changed the claimant's clean checkout" assert_reassigned_slot_left_alone "$dir" "$id" "$other" "clean reassigned slot without --force" + if [ -n "${FM_TEARDOWN_EVIDENCE_FILE:-}" ]; then + { + printf '$ fm-teardown.sh %s\n' "$id" + cat "$dir/stderr" "$dir/stdout" + printf 'finished record removed: %s\n' "$([ ! -e "$dir/home/state/$id.meta" ] && echo yes || echo no)" + printf 'claimant record retained: %s\n' "$([ -f "$dir/home/state/$other.meta" ] && echo yes || echo no)" + printf 'claimant process alive: %s\n' "$(kill -0 "$worker" 2>/dev/null && echo yes || echo no)" + printf 'claimant checkout HEAD unchanged: %s\n' "$([ "$(git -C "$dir/worktree" rev-parse HEAD)" = "$owner_head" ] && echo yes || echo no)" + printf 'claimant unlanded work retained: %s\n' "$(! git -C "$dir/worktree" merge-base --is-ancestor "$owner_head" "$(git -C "$dir/project" rev-parse HEAD)" && echo yes || echo no)" + printf 'claimant checkout clean: %s\n' "$([ -z "$(git -C "$dir/worktree" status --porcelain)" ] && echo yes || echo no)" + printf 'claimant slot claim: %s\n' "$(sed -n '1p' "$dir/pool/1/.fm-slot-owner")" + printf 'treehouse return invoked: %s\n' "$(grep -Fq 'treehouse ' "$dir/runtime.log" && echo yes || echo no)" + } > "$FM_TEARDOWN_EVIDENCE_FILE" + fi kill "$worker" 2>/dev/null || true wait "$worker" 2>/dev/null || true From 4aa15a3fc6bfc48f411582bf6a4b4de55c58250f Mon Sep 17 00:00:00 2001 From: keenvc Date: Mon, 28 Sep 2026 21:24:10 +0000 Subject: [PATCH 4/4] fix(teardown): avoid presentation-lock races after claim-first slot cleanup Reassigned-slot teardown with a dead Herdr husk no longer takes the shared presentation session lock, restoring the pre-5993 contention profile for stale records while keeping live-slot protection intact. Herdr presentation recovery spawns now wait up to 120s for that lock and release it on abort so concurrent cross-home recovery cannot fail the 5s try loop after a legitimate holder. Co-authored-by: Cursor --- bin/fm-spawn.sh | 19 ++++++++++++++++++- bin/fm-teardown.sh | 28 ++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/bin/fm-spawn.sh b/bin/fm-spawn.sh index 1a201ecc0b7..c5d7aa092c9 100755 --- a/bin/fm-spawn.sh +++ b/bin/fm-spawn.sh @@ -1339,6 +1339,7 @@ spawn_abort_cleanup() { [ ! -e "$STATE/$ID.meta" ] && [ ! -L "$STATE/$ID.meta" ]; then "$SCRIPT_DIR/fm-claim.sh" release-task "$ID" --home "$FM_HOME" >/dev/null 2>&1 || true fi + spawn_herdr_presentation_order_lock_release return "$status" } trap spawn_abort_cleanup EXIT @@ -1363,6 +1364,22 @@ spawn_herdr_presentation_order_lock_acquire() { return 1 } +# Cross-home presentation recovery can wait behind another home's teardown or +# reclaim that legitimately holds the same session lock longer than a fresh +# spawn's 5s try loop; bounded wait matches that contention without weakening +# the test's concurrent-recovery guarantee. +spawn_herdr_presentation_order_lock_acquire_recovery() { + local session=${1:-} lock_path + [ -n "$session" ] || session=$(fm_backend_herdr_session) + lock_path=$(fm_backend_herdr_presentation_session_lock_path "$session") || return 1 + HERDR_PRESENTATION_ORDER_LOCK="$lock_path" + if fm_lock_acquire_wait_bounded "$lock_path" 120; then + HERDR_PRESENTATION_ORDER_LOCK_HELD=1 + return 0 + fi + return 1 +} + clear_relaunch_harness_wiring() { local harness=$1 wt=$2 state=$3 id=$4 token_path token auth_path path # The wiring arms above match on harness PREFIXES, because a task launched @@ -3433,7 +3450,7 @@ else echo "error: herdr presentation recovery could not ensure its exact named session" >&2 exit 1 } - spawn_herdr_presentation_order_lock_acquire "$HERDR_SES" || { + spawn_herdr_presentation_order_lock_acquire_recovery "$HERDR_SES" || { echo "error: herdr presentation recovery could not acquire its session lock; refusing a concurrent resume" >&2 exit 1 } diff --git a/bin/fm-teardown.sh b/bin/fm-teardown.sh index c662935543d..a8090bc156c 100755 --- a/bin/fm-teardown.sh +++ b/bin/fm-teardown.sh @@ -3331,11 +3331,29 @@ fi # refuses before any destructive step. TEARDOWN_HERDR_SESSION= TEARDOWN_HERDR_PANE= +TEARDOWN_HERDR_REASSIGNED_DEAD=0 if [ "$BACKEND" = herdr ] && [ -z "$TEARDOWN_ENDPOINT_CLEARED" ]; then - teardown_herdr_preflight_target "$T" "$ID" || exit 1 - fm_backend_herdr_parse_target "$T" || exit 1 - TEARDOWN_HERDR_SESSION=$FM_BACKEND_HERDR_SESSION - TEARDOWN_HERDR_PANE=$FM_BACKEND_HERDR_PANE + fm_backend_source herdr || true + if fm_backend_herdr_parse_target "$T"; then + TEARDOWN_HERDR_SESSION=$FM_BACKEND_HERDR_SESSION + TEARDOWN_HERDR_PANE=$FM_BACKEND_HERDR_PANE + if [ "$TEARDOWN_SLOT_REASSIGNED" = 1 ] \ + && [ "$(fm_backend_herdr_pane_presence_state "$TEARDOWN_HERDR_SESSION" "$TEARDOWN_HERDR_PANE")" = dead ]; then + # A reassigned slot is no longer this task's to kill or return; when its + # Herdr pane is already a dead husk, record and journal cleanup need no + # presentation-order lock. Claim-first teardown used to refuse at the + # exclusive scan before ever acquiring this lock; finishing reassigned + # cleanup without it keeps concurrent cross-home recovery from losing a + # 5s presentation-lock race to stale-record teardown. + TEARDOWN_HERDR_REASSIGNED_DEAD=1 + fi + fi + if [ "$TEARDOWN_HERDR_REASSIGNED_DEAD" != 1 ]; then + teardown_herdr_preflight_target "$T" "$ID" || exit 1 + fm_backend_herdr_parse_target "$T" || exit 1 + TEARDOWN_HERDR_SESSION=$FM_BACKEND_HERDR_SESSION + TEARDOWN_HERDR_PANE=$FM_BACKEND_HERDR_PANE + fi fi BACKLOG_CLOSED=0 @@ -3531,6 +3549,8 @@ elif [ -n "$TEARDOWN_ENDPOINT_CLEARED" ]; then # Any leftover presentation journal is stale for the same reason. rm -f "$HERDR_PRESENTATION_JOURNAL" : +elif [ "$TEARDOWN_HERDR_REASSIGNED_DEAD" = 1 ]; then + rm -f "$HERDR_PRESENTATION_JOURNAL" elif [ "$BACKEND" = herdr ]; then if teardown_herdr_session_lock_held "$TEARDOWN_HERDR_SESSION"; then fm_backend_herdr_kill_serialized "$TEARDOWN_HERDR_SESSION" "$TEARDOWN_HERDR_PANE" 2>/dev/null || true