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
2 changes: 1 addition & 1 deletion bin/fm-supervision-host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ while :; do

# A turn that could outlive the boundary would outlive the hook registration.
turn_crosses_boundary && boundary_exit
if ! ARM_OWN_GROUP=1 start_successor "$CLOSED_ARM_PID"; then
if ! FM_WATCH_HANDOFF_CONFIRM=1 ARM_OWN_GROUP=1 start_successor "$CLOSED_ARM_PID"; then
exit_to_main "the successor watcher cycle could not be verified before handling; this wake is yours"
fi
if [ -n "$SUCCESSOR_GENERATION" ]; then
Expand Down
29 changes: 29 additions & 0 deletions bin/fm-wake-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,35 @@ fm_recovery_marker_begin_handling() {
_fm_recovery_marker_begin_handling "$1" "${2:-}"
}

# A handling successor confirms one generation (bin/fm-watch-arm.sh
# --handling-delivered). Announced downtime is the state where a queue append
# mints a new generation, so hold that episode pending on the same generation
# until the confirm moves it to handling. docs/watcher-continuity.md
# "Generation reuse".
fm_recovery_marker_hold_for_handling() {
local marker=$1 lock
lock="${marker}.lock"
fm_lock_acquire_wait "$lock" || return 1
if [ ! -e "$marker" ] && [ ! -L "$marker" ]; then
fm_lock_release "$lock"
return 0
fi
if ! fm_recovery_marker_read "$marker"; then
fm_lock_release "$lock"
fm_recovery_marker_arm_check "$marker"
return
fi
case "$FM_RECOVERY_MARKER_TOKEN" in
announced:downtime:*)
if ! _fm_recovery_marker_write_locked "$marker" downtime "${FM_RECOVERY_MARKER_TOKEN##*:}" pending; then
fm_lock_release "$lock"
return 1
fi
;;
esac
fm_lock_release "$lock"
}

fm_recovery_marker_arm_check() {
fm_recovery_transition "$1" arm-check
}
Expand Down
14 changes: 14 additions & 0 deletions bin/fm-watch-arm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,20 @@ case "${1:-}" in
esac

if [ "$mode" = handling-delivered ]; then
# The arm has already snapshotted the generation into the argument. Production
# spends the next stretch starting this process; a due check can append in
# that stretch and mint a new downtime generation. FM_TEST_HANDLING_CONFIRM_DELAY
# (seconds) holds that stretch open, and only when FM_TEST_SEAM=1. The stamp
# is the fixture check's cue that the snapshot is already fixed.
if [ "${FM_TEST_SEAM:-}" = 1 ]; then
case "${FM_TEST_HANDLING_CONFIRM_DELAY:-}" in
''|*[!0-9.]*|*.*.*|.*|*.) ;;
*)
printf 'wait\n' > "$STATE/.fm-test-handling-confirm-wait" 2>/dev/null || true
sleep "$FM_TEST_HANDLING_CONFIRM_DELAY"
;;
esac
fi
fm_pid_alive "$handling_watcher_pid" \
&& fm_watcher_lock_matches_pid "$STATE" "$WATCH" "$handling_watcher_pid" "$FM_HOME" \
&& fm_recovery_marker_begin_handling "$STATE/.watcher-down" "$handling_generation"
Expand Down
42 changes: 36 additions & 6 deletions bin/fm-watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3061,9 +3061,22 @@ if [ "${FM_WATCH_HANDLING_SUCCESSOR:-0}" != 1 ] \
exit 1
fi
fi
if ! fm_recovery_marker_arm_check "$WATCHER_DOWNTIME_MARKER"; then
echo "watcher: recovery state could not be consumed safely; retaining stale lock evidence" >&2
exit 1
if [ "${FM_WATCH_HANDLING_SUCCESSOR:-0}" = 1 ] && [ "${FM_WATCH_HANDOFF_CONFIRM:-0}" = 1 ]; then
# FM_WATCH_HANDOFF_CONFIRM is the supervision host's handling handoff: the
# arm snapshots this generation for --handling-delivered. Announcing it here
# is what lets a queue append mint another generation before that confirm
# reads the snapshotted one. No other launcher confirms a handoff
# (docs/watcher-continuity.md "Generation reuse").
if ! fm_recovery_marker_hold_for_handling "$WATCHER_DOWNTIME_MARKER"; then
echo "watcher: recovery state could not be held for the handling handoff; retaining stale lock evidence" >&2
exit 1
fi
FM_RECOVERY_MARKER_ACTION="wait"
else
if ! fm_recovery_marker_arm_check "$WATCHER_DOWNTIME_MARKER"; then
echo "watcher: recovery state could not be consumed safely; retaining stale lock evidence" >&2
exit 1
fi
fi
# A recovery the marker arm check approved has already passed the announcement
# gate once, under the queue lock and over the marker this cycle read. The poll
Expand Down Expand Up @@ -3326,6 +3339,12 @@ last_read=0
# host's left record, after naming this watcher's arm, no longer does.
watch_gap=${FM_WATCH_GAP_SUCCESSOR:-0}
watch_gap_recorded=0
# The host's handoff successor runs no check until --handling-delivered has
# moved the marker to handling. Every other watcher opens checks immediately.
handling_checks_open=1
if [ "${FM_WATCH_HANDLING_SUCCESSOR:-0}" = 1 ] && [ "${FM_WATCH_HANDOFF_CONFIRM:-0}" = 1 ]; then
handling_checks_open=0
fi
watch_gap_tick() {
local attached left_arm=''
[ "$watch_gap" = 1 ] || return 0
Expand Down Expand Up @@ -3476,10 +3495,21 @@ while :; do
# A successor left for main skips this sweep while its gap lasts
# (watch_gap_tick). A due check here exited the cycle and the append minted a
# fresh downtime generation; the next park, finding this arm dead, handed
# check: rearm-resurface again. Signals below still close that cycle.
# docs/watcher-continuity.md "Generation reuse".
# check: rearm-resurface again. The host's handoff successor skips it until
# the handoff confirms the generation, for the same mint. Signals below still
# close that cycle. docs/watcher-continuity.md "Generation reuse".
if [ "$handling_checks_open" != 1 ]; then
if fm_recovery_marker_read "$WATCHER_DOWNTIME_MARKER"; then
case "$FM_RECOVERY_MARKER_TOKEN" in
pending:downtime:*|announced:downtime:*) ;;
*) handling_checks_open=1 ;;
esac
else
handling_checks_open=1
fi
fi
watch_gap_tick
if [ "$watch_gap" != 1 ] && [ "$check_age" -ge "$CHECK_INTERVAL" ]; then
if [ "$handling_checks_open" = 1 ] && [ "$watch_gap" != 1 ] && [ "$check_age" -ge "$CHECK_INTERVAL" ]; then
rejected_checks=
contribution_check_output=
for c in "$STATE"/*.check.sh; do
Expand Down
5 changes: 4 additions & 1 deletion docs/watcher-continuity.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ So a finished, hung, or identity-mismatched claim cannot suppress that recovery

The recovery-episode contract below owns once-per-generation announcement.
A handling successor does not re-announce.
It enters its poll loop immediately and keeps scanning signals, stale panes, and checks.
[Generation reuse](#generation-reuse) owns how the confirmed handoff holds that generation and how the pass-through successor keeps the announced episode.

### Manual recovery and other harnesses

Expand Down Expand Up @@ -239,6 +239,8 @@ Its ordinary arm check still announces a never-announced generation that has que

An ordinary watcher close attempts to publish downtime, and every durable queue append publishes it.
A handling successor closing to resurface recovery preserves the existing marker instead.
The supervision host starts the successor whose handling handoff it then confirms (`bin/fm-watch-arm.sh --handling-delivered`) with `FM_WATCH_HANDOFF_CONFIRM`; only that successor holds announced downtime as pending on the same generation until the confirm, and does not run its check sweep until the marker reads handling.
A handling successor from any other launcher (the host-off Claude Stop hook, omp, or OpenCode) runs the ordinary arm check and its check sweep from the start.
If EXIT cleanup cannot acquire the downtime-marker lock within its bound, it retains the stale singleton for the next arm to publish the missing downtime before clearing that lock (see [Grace, beacon, and stop signals](#grace-beacon-and-stop-signals)).
A downtime republication of a pending episode reuses its generation.
A watcher close leaves an announced downtime episode announced, while a successful durable append opens a fresh pending generation so a live watcher can recover the new work.
Expand All @@ -248,6 +250,7 @@ A watcher stopped so an arm can take its cycle over (`bin/fm-watch-arm.sh --take
The supervision host's take-over sets `FM_WATCH_KEEP_ANNOUNCED`, and the fresh cycle then leaves an already-announced episode announced instead of minting another generation for the same queued work.
The successor that pass-through leaves for main keeps its beacon and still delivers a signal, and it does not run the check sweep until the next park.
That skip ends early when any arm attaches to that watcher (the arm writes `state/.watch-attached`), or when the host's left record, once it named the successor's arm, no longer does, so checks run again whenever a reader has returned.
It keeps an announced downtime episode announced, so the next park's take-over does not announce that same work again.
A due check while nobody is reading used to exit that successor, and the queue append minted a fresh pending generation.
The next park then found no live arm to take over, so its ordinary arm check announced that generation and handed `check: rearm-resurface` back.
A pending episode with queued work, including a row appended after main acknowledged the last one, still surfaces once through the ordinary arm check.
Expand Down
77 changes: 77 additions & 0 deletions tests/fm-supervision-host-stop-hook.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,82 @@ test_take_over_attach_fallback_ends_the_gap_check_skip() {
pass "host+hook: a take-over that falls back to attaching ends the gap successor's check skip"
}

# Register <id>.check.sh from <body-file> the way a trusted home check is recorded.
register_lab_check() { # <home> <id> <body-file>
local home=$1 id=$2 src=$3 hash
cp "$src" "$home/state/$id.check.sh"
chmod 700 "$home/state/$id.check.sh"
hash=$(sha256sum "$home/state/$id.check.sh" | awk '{ print $1 }')
printf 'fm-custom-check-v1\n%s\n' "$hash" > "$home/state/$id.check-trust"
chmod 600 "$home/state/$id.check-trust"
}

# True once the host has taken a wake, or the Stop hook has already returned.
handoff_settled() { # <home>
grep -q $'\thandled\t' "$1/state/.supervision-host.log" 2>/dev/null && return 0
hook_exited "$1"
}

# The live loop after PR 238 (2026-11-06): the host was already on, Claude's
# Stop hook parked again, and a due check appended while the handling
# successor's recovery generation was announced downtime. That append minted
# a new generation, so the handoff confirmation failed while the watcher was
# still alive, and the next Stop handed check: rearm-resurface.
# The check waits for the confirm window, appends, and stays up, so the
# failure is that generation mismatch rather than a dead watcher.
test_due_check_during_handling_handoff_confirms() {
local home saved_interval body
saved_interval=${FM_CHECK_INTERVAL:-999999}
export FM_CHECK_INTERVAL=0
export FM_TEST_HANDLING_CONFIRM_DELAY=1
home=$(make_primary_home hook-handling-confirm)
: > "$home/hook.own-group"
ln -s "$ROOT/.agents" "$home/.agents"
body="$home/check-body.sh"
cat > "$body" <<'SH'
#!/usr/bin/env bash
stamp="$FM_HOME/state/.fm-test-handling-confirm-wait"
if [ "${FM_WATCH_HANDLING_SUCCESSOR:-0}" != 1 ]; then
exit 0
fi
i=0
while [ "$i" -lt 80 ]; do
[ -f "$stamp" ] && break
sleep 0.05
i=$((i + 1))
done
if [ -f "$stamp" ]; then
FM_HOME="$FM_HOME" bash -c '. "$1"; fm_wake_append check lab-seat "check: lab-seat during handoff"' _ \
"$FM_HOME/bin/fm-wake-lib.sh" || exit 0
sleep 3
fi
exit 0
SH
register_lab_check "$home" lab-seat "$body"
start_hook_session "$home"
turn_end "$home"
wait_until 150 watcher_live "$home" \
|| fail "handling handoff: watcher never started: $(cat "$home/hook.err" 2>/dev/null)"
append_status "$home" 'step one'
wait_until 250 handoff_settled "$home" \
|| fail "handling handoff: the signal was neither taken nor handed back: $(cat "$home/state/.supervision-host.log" 2>/dev/null)"
# The failed confirm returns the Stop hook, and the next Stop is the park
# that hands check: rearm-resurface. A park that stayed up has no next Stop.
if hook_exited "$home"; then
turn_end "$home"
wait_until 200 hook_exited "$home" || true
fi
assert_no_re 'could not be confirmed' "$home/state/.supervision-host.log" \
"handling handoff: the successor handoff was not confirmed"
assert_re $'\thandled\t.*signal: ' "$home/state/.supervision-host.log" \
"handling handoff: the signal was not taken"
assert_no_re 'check: rearm-resurface' "$home/state/.supervision-host.log" \
"handling handoff: the next park handed check: rearm-resurface"
export FM_CHECK_INTERVAL=$saved_interval
unset FM_TEST_HANDLING_CONFIRM_DELAY
pass "host+hook: a due check during the handling handoff confirms and does not hand check: rearm-resurface"
}

# FM_TEST_ONLY runs a single named case, the same entry point the sibling
# suites expose.
if [ -n "${FM_TEST_ONLY:-}" ]; then
Expand All @@ -923,6 +999,7 @@ test_pass_through_successor_survives_the_hook_process_group_teardown
test_next_park_takes_over_the_cycle_a_pass_through_left_for_main
test_take_over_does_not_hand_the_same_resurface_straight_back
test_due_check_during_rearm_gap_stays_a_take_over
test_due_check_during_handling_handoff_confirms
test_row_queued_after_ack_reaches_main_when_the_left_successor_is_gone
test_host_off_attach_ends_the_gap_check_skip
test_take_over_attach_fallback_ends_the_gap_check_skip
Expand Down
75 changes: 75 additions & 0 deletions tests/fm-watch-recovery-loop.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,80 @@ test_handling_successor_does_not_go_blind() {
pass "a resurfacing handling successor stays alive and supervises instead of going blind"
}

# T3: a handling successor that finds an unreadable recovery marker quarantines
# it and starts, exactly as every other watcher arm does, instead of exiting
# and leaving the handling turn without a watcher.
test_handling_successor_quarantines_malformed_marker() {
local dir home state fakebin child now out quarantined
dir=$(make_case recovery-handling-malformed)
home="$dir/home"
state="$dir/state"
fakebin="$dir/fakebin"
mkdir -p "$home/data"
: > "$state/crew.meta"
printf 'not a recovery marker\n' > "$state/.watcher-down"
chmod 600 "$state/.watcher-down"
out="$dir/watch.out"
PATH="$fakebin:$PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$state" \
FM_POLL=1 FM_SIGNAL_GRACE=0 FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=600 \
FM_WATCH_HANDLING_SUCCESSOR=1 FM_WATCH_HANDOFF_CONFIRM=1 "$WATCH" > "$out" 2>&1 &
child=$!
now=0
while [ "$now" -lt 40 ]; do
[ "$(cat "$state/.watch.lock/pid" 2>/dev/null || true)" = "$child" ] && break
sleep 0.1
now=$((now + 1))
done
[ "$(cat "$state/.watch.lock/pid" 2>/dev/null || true)" = "$child" ] \
|| { kill -TERM "$child" 2>/dev/null || true; wait "$child" 2>/dev/null || true; fail "handling successor did not start over a malformed marker: $(cat "$out")"; }
sleep 0.5
kill -0 "$child" 2>/dev/null \
|| fail "handling successor exited over a malformed marker: $(cat "$out")"
quarantined=$(cat "$state"/.watcher-down.invalid.*/marker 2>/dev/null || true)
kill -TERM "$child" 2>/dev/null || true
wait "$child" 2>/dev/null || true
[ "$quarantined" = 'not a recovery marker' ] \
|| fail "handling successor did not quarantine the malformed marker: $(ls -a "$state")"
pass "a handling successor quarantines a malformed recovery marker and keeps supervising"
}

# T4: a handling successor that no host handoff will confirm (the host-off
# Claude Stop hook, omp, OpenCode) runs its due check sweep at once and leaves
# an announced episode announced; only the host's confirmed handoff holds it.
test_unconfirmed_handling_successor_runs_checks() {
local dir home state fakebin child now out hash
dir=$(make_case recovery-handling-unconfirmed)
home="$dir/home"
state="$dir/state"
fakebin="$dir/fakebin"
mkdir -p "$home/data"
: > "$state/crew.meta"
printf 'announced:downtime:off.1.aaa\n' > "$state/.watcher-down"
chmod 600 "$state/.watcher-down"
printf '#!/usr/bin/env bash\necho lab ready\n' > "$state/lab.check.sh"
chmod 700 "$state/lab.check.sh"
hash=$(sha256sum "$state/lab.check.sh" | awk '{ print $1 }')
printf 'fm-custom-check-v1\n%s\n' "$hash" > "$state/lab.check-trust"
chmod 600 "$state/lab.check-trust"
out="$dir/watch.out"
PATH="$fakebin:$PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$state" \
FM_POLL=1 FM_SIGNAL_GRACE=0 FM_CHECK_INTERVAL=0 FM_HEARTBEAT=600 \
FM_WATCH_HANDLING_SUCCESSOR=1 "$WATCH" > "$out" 2>&1 &
child=$!
now=0
while [ "$now" -lt 40 ]; do
grep -q '^check: ' "$out" 2>/dev/null && break
sleep 0.25
now=$((now + 1))
done
kill -TERM "$child" 2>/dev/null || true
wait "$child" 2>/dev/null || true
grep -F 'lab ready' "$out" >/dev/null \
|| fail "an unconfirmed handling successor held its due check sweep: $(cat "$out")"
pass "a handling successor no host handoff confirms runs its due check sweep at once"
}

test_handling_successor_does_not_go_blind
test_handling_successor_quarantines_malformed_marker
test_unconfirmed_handling_successor_runs_checks
test_unacknowledged_recovery_is_announced_once_per_generation
Loading