Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion bin/fm-spawn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
66 changes: 47 additions & 19 deletions bin/fm-teardown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +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 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 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=. 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,
Expand Down Expand Up @@ -2262,11 +2264,12 @@ 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
# 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
# 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
# that allocates it - so a claim naming another task is proof the slot was
# reassigned after this record was written.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -3323,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
Expand Down Expand Up @@ -3523,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
Expand Down
5 changes: 3 additions & 2 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 47 additions & 6 deletions tests/fm-teardown-endpoint-safety.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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" \
Expand Down Expand Up @@ -881,7 +885,7 @@ assert_reassigned_slot_left_alone() { # <case> <id> <other> <description>
}

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,
Expand All @@ -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
Expand All @@ -906,26 +913,37 @@ 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"
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=$!

Expand All @@ -937,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 <return>' "$dir/runtime.log" && echo yes || echo no)"
} > "$FM_TEARDOWN_EVIDENCE_FILE"
fi
kill "$worker" 2>/dev/null || true
wait "$worker" 2>/dev/null || true

Expand Down
Loading