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
8 changes: 6 additions & 2 deletions bin/fm-supervision-host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
# conditions under which that restores a single owner and the fallback.
# That take-over sets FM_WATCH_KEEP_ANNOUNCED so an episode already
# announced is not reopened into another main hand-back
# (docs/watcher-continuity.md "Generation reuse").
# (docs/watcher-continuity.md "Generation reuse"). The successor left for
# main does not run the check sweep while nobody is reading it.
# - away (an away record exists): every close goes to the engine.
# Every turn that starts attended meets that rule again at its start, so a
# close accepted away whose turn starts attended (the captain returned in
Expand Down Expand Up @@ -752,7 +753,10 @@ detach_successor() {
# downtime (autoarm_commit in bin/fm-claude-stop-autoarm.sh). A failed start
# returns 1; the caller still prints the close unchanged.
leave_successor_for_main() {
if ! ARM_OWN_GROUP=1 start_successor "$CLOSED_ARM_PID"; then
# FM_WATCH_GAP_SUCCESSOR: this cycle is unread until the next park. It keeps
# the beacon and does not run the check sweep (bin/fm-watch.sh). A signal
# still closes it, so the next park can deliver that close.
if ! FM_WATCH_GAP_SUCCESSOR=1 ARM_OWN_GROUP=1 start_successor "$CLOSED_ARM_PID"; then
log_line "pass-through successor-unverified $(printf '%s\n' "$REASON" | head -n 1)"
return 1
fi
Expand Down
1 change: 1 addition & 0 deletions bin/fm-watch-arm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ healthy_watcher() {

report_attached() {
local age
printf '%s\n' "$HEALTHY_PID" > "$STATE/.watch-attached" 2>/dev/null || true
fm_watcher_beacon_age_to age "$BEAT"
echo "watcher: attached pid=$HEALTHY_PID (beacon ${age}s)"
}
Expand Down
31 changes: 30 additions & 1 deletion bin/fm-watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3319,6 +3319,29 @@ prev=
last=
last_read=0

# A successor left for main while nobody reads it (FM_WATCH_GAP_SUCCESSOR,
# bin/fm-supervision-host.sh leave_successor_for_main) skips the check sweep
# only for that gap. The gap ends once an arm attaches to this watcher
# (state/.watch-attached, bin/fm-watch-arm.sh report_attached) or once the
# host's left record, after naming this watcher's arm, no longer does.
watch_gap=${FM_WATCH_GAP_SUCCESSOR:-0}
watch_gap_recorded=0
watch_gap_tick() {
local attached left_arm=''
[ "$watch_gap" = 1 ] || return 0
fm_read_file_to attached "$STATE/.watch-attached" || attached=
if [ -f "$STATE/.supervision-host-left" ]; then
IFS="$(printf '\t')" read -r left_arm _ < "$STATE/.supervision-host-left" || true
fi
if [ "$attached" = "$WATCHER_PID" ]; then
watch_gap=0
elif [ "$left_arm" = "$PPID" ]; then
watch_gap_recorded=1
elif [ "$watch_gap_recorded" = 1 ]; then
watch_gap=0
fi
}

while :; do
# Home-gone exit: a deleted home, state directory, or code root means this
# watcher's world is gone (a torn-down temporary home or a discarded
Expand Down Expand Up @@ -3450,7 +3473,13 @@ while :; do
# keeps producing signals - the slow poll (e.g. merge detection) would then
# never run until the fleet went quiet. Checks are due only every
# CHECK_INTERVAL, so most cycles skip this block and fall straight through.
if [ "$check_age" -ge "$CHECK_INTERVAL" ]; then
# 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".
watch_gap_tick
if [ "$watch_gap" != 1 ] && [ "$check_age" -ge "$CHECK_INTERVAL" ]; then
rejected_checks=
contribution_check_output=
for c in "$STATE"/*.check.sh; do
Expand Down
10 changes: 7 additions & 3 deletions docs/watcher-continuity.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,13 @@ A watcher close leaves an announced downtime episode announced, while a successf
An announced handling episode becomes pending downtime on the same generation because its handling turn may have been interrupted.
That handling republication gives a successor exactly one recovery presentation without orphaning the acknowledgement already printed for that generation.
A watcher stopped so an arm can take its cycle over (`bin/fm-watch-arm.sh --take-over`) publishes downtime like any close, but the taking arm restores an acknowledged episode that stop reopened only when the taken-over arm's cycle-ledger row for that exact arm and watcher records the watcher ending by the take-over's TERM and no wake was appended in between.
The supervision host's take-over also 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.
A pending episode with queued work still surfaces once through the ordinary arm check.
Without that hold, the fresh cycle's `check: rearm-resurface` is an attended main-only close, the host hands it straight back, and the next turn end takes over and announces again.
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.
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.
Without the take-over's `FM_WATCH_KEEP_ANNOUNCED` hold, the fresh cycle's `check: rearm-resurface` is an attended main-only close, and the next turn end hands it straight back.
The taking arm waits within a short bound for that row; a missing row or any other signal leaves downtime for the fresh cycle's ordinary recovery wake, while take-over still proceeds.
Any other episode is left for the next cycle's arm check.

Expand Down
152 changes: 152 additions & 0 deletions tests/fm-supervision-host-stop-hook.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,154 @@ test_take_over_does_not_hand_the_same_resurface_straight_back() {
pass "host+hook: a takeover does not hand the same rearm-resurface straight back"
}

# Queue one durable check row in <home>.
queue_check() { # <home> <key> <payload>
FM_HOME="$1" bash -c '
. "$1"
fm_wake_append check "$2" "$3"
' _ "$ROOT/bin/fm-wake-lib.sh" "$2" "$3"
}

# The 2026-11-06 loop, once the successor is still alive. The first
# pass-through is check: rearm-resurface. A check is due on the next poll
# (the production successor's .last-check was stale, so its first poll ran
# the sweep and exited). The successor must stay up, the next park must take
# it over, and that park must not hand check: rearm-resurface again.
test_due_check_during_rearm_gap_stays_a_take_over() {
local home resurfaces saved_interval
saved_interval=${FM_CHECK_INTERVAL:-999999}
export FM_CHECK_INTERVAL=0
home=$(make_primary_home hook-rearm-due-check)
: > "$home/hook.own-group"
printf '#!/bin/sh\necho seats out of quota\n' > "$home/state/lab-seat.check.sh"
chmod 700 "$home/state/lab-seat.check.sh"
start_hook_session "$home"
queue_check "$home" resurface-fixture "check: work stranded before the host parked" \
|| fail "fixture: could not queue the stranded wake"
turn_end "$home"
wait_until 250 hook_exited "$home" \
|| fail "due check: the first close never reached the Stop hook: $(cat "$home/state/.supervision-host.log" 2>/dev/null)"
assert_re ' pass-through attended main-only check: rearm-resurface' "$home/state/.supervision-host.log" \
"fixture: the stranded queue was not handed back once"
assert_rewoke_main "$home" "due check (first)"
# Main's turn is the gap. On the current code the due check exits the
# successor before the next park, so that park is not a take-over.
turn_end "$home"
wait_until 200 grep -q $'\ttake-over\tarm=' "$home/state/.supervision-host.log" \
|| fail "due check: the next park did not take the cycle over: $(cat "$home/state/.supervision-host.log"; echo '--- deliveries ---'; cat "$home/state/.watch-deliveries.log" 2>/dev/null)"
sleep 3
resurfaces=$(grep -c 'check: rearm-resurface' "$home/state/.supervision-host.log" || true)
[ "$resurfaces" -eq 1 ] \
|| fail "due check: recovery was handed back again ($resurfaces times): $(cat "$home/state/.supervision-host.log")"
if hook_exited "$home" && grep -q 'check: rearm-resurface' "$home/hook.err" 2>/dev/null; then
fail "due check: the takeover handed check: rearm-resurface: $(cat "$home/hook.err")"
fi
export FM_CHECK_INTERVAL=$saved_interval
pass "host+hook: a due check during the re-arm gap does not hand check: rearm-resurface again"
}

# A row queued after main drained and acknowledged a rearm-resurface is new
# work. When the successor left for main is already gone at the next park,
# that park has no cycle to take over, and the row must still reach main once.
# The host log keeps the earlier rearm-resurface line, as a real home does.
test_row_queued_after_ack_reaches_main_when_the_left_successor_is_gone() {
local home left_arm watcher drained
home=$(make_primary_home hook-rearm-dead-successor)
: > "$home/hook.own-group"
start_hook_session "$home"
queue_check "$home" resurface-fixture "check: work stranded before the host parked" \
|| fail "fixture: could not queue the stranded wake"
turn_end "$home"
wait_until 250 hook_exited "$home" \
|| fail "dead successor: the first close never reached the Stop hook: $(cat "$home/state/.supervision-host.log" 2>/dev/null)"
assert_re ' pass-through attended main-only check: rearm-resurface' "$home/state/.supervision-host.log" \
"fixture: the stranded queue was not handed back once"
assert_rewoke_main "$home" "dead successor (first)"
watcher_live "$home" || fail "dead successor: the first pass-through left no successor"
main_drain_and_ack "$home"
left_arm=$(awk -F '\t' 'NR == 1 { print $1 }' "$home/state/.supervision-host-left")
[ -n "$left_arm" ] || fail "dead successor: the pass-through recorded no arm"
watcher=$(cat "$home/state/.watch.lock/pid" 2>/dev/null || true)
kill -TERM "$left_arm" 2>/dev/null || fail "dead successor: could not stop the left arm $left_arm"
wait_until 50 bash -c 'if [ -n "$1" ] && kill -0 "$1" 2>/dev/null; then exit 1; fi; exit 0' _ "$watcher" \
|| fail "dead successor: the left arm did not stop its watcher"
queue_check "$home" lab-seat "check: lab-seat: seats are out of quota" \
|| fail "fixture: could not append the new check"
turn_end "$home"
wait_until 250 hook_exited "$home" \
|| fail "dead successor: the new row never reached main: $(cat "$home/state/.supervision-host.log"; cat "$home/state/.watcher-down" 2>/dev/null)"
assert_rewoke_main "$home" "dead successor (new row)"
drained=$(main_drain "$home")
assert_contains "$drained" 'seats are out of quota' "the row queued after the acknowledgement must reach main's drain"
pass "host+hook: a row queued after main's acknowledgement reaches main once when the left successor is gone"
}

# Leave a gap successor for main in <home>: the first park hands a stranded
# row back as check: rearm-resurface, then an unregistered check is installed.
# A check sweep that runs rejects it with a wake, so the successor must stay
# up while nobody reads it.
leave_gap_successor_with_a_due_check() { # <home> <label>
local home=$1 label=$2
start_hook_session "$home"
queue_check "$home" resurface-fixture "check: work stranded before the host parked" \
|| fail "$label: could not queue the stranded wake"
turn_end "$home"
wait_until 250 hook_exited "$home" \
|| fail "$label: the first close never reached the Stop hook: $(cat "$home/state/.supervision-host.log" 2>/dev/null)"
assert_rewoke_main "$home" "$label (first)"
watcher_live "$home" || fail "$label: the pass-through left no successor"
GAP_WATCHER=$(cat "$home/state/.watch.lock/pid")
printf '#!/bin/sh\necho seats out of quota\n' > "$home/state/lab-seat.check.sh"
chmod 700 "$home/state/lab-seat.check.sh"
sleep 3
if ! kill -0 "$GAP_WATCHER" 2>/dev/null || gap_watcher_ran_checks "$home"; then
fail "$label: the gap successor ran the check sweep while nobody was reading"
fi
}

# True once the gap successor's own delivery record shows its check sweep ran
# (the rejected unregistered check), the watcher's persisted delivery contract.
gap_watcher_ran_checks() { # <home>
awk -F '\t' -v pid="$GAP_WATCHER" '$1 == pid && $3 ~ /^check: rejected unauthenticated state checks:/ { found = 1 } END { exit !found }' \
"$1/state/.watch-deliveries.log" 2>/dev/null
}

# The host is turned off again while a gap successor runs. The plain Stop-hook
# arm that follows attaches to that watcher, and its checks must run again.
test_host_off_attach_ends_the_gap_check_skip() {
local home saved_interval
saved_interval=${FM_CHECK_INTERVAL:-999999}
export FM_CHECK_INTERVAL=0
home=$(make_primary_home hook-gap-host-off)
: > "$home/hook.own-group"
leave_gap_successor_with_a_due_check "$home" "host off"
: > "$home/config/supervision-host-off"
turn_end "$home"
wait_until 150 gap_watcher_ran_checks "$home" \
|| fail "host off: checks never ran after the Stop hook attached: $(cat "$home/hook.out" "$home/hook.err" 2>/dev/null)"
export FM_CHECK_INTERVAL=$saved_interval
pass "host+hook: an attach after the host is turned off ends the gap successor's check skip"
}

# A take-over that cannot confirm the watcher is the named arm's child attaches
# instead; that attach must end the gap successor's check skip too.
test_take_over_attach_fallback_ends_the_gap_check_skip() {
local home saved_interval arm
saved_interval=${FM_CHECK_INTERVAL:-999999}
export FM_CHECK_INTERVAL=0
home=$(make_primary_home hook-gap-take-over-fallback)
: > "$home/hook.own-group"
leave_gap_successor_with_a_due_check "$home" "take-over fallback"
FM_HOME="$home" "$home/bin/fm-watch-arm.sh" --take-over "$$" > "$home/fallback.out" 2>&1 &
arm=$!
wait_until 150 gap_watcher_ran_checks "$home" \
|| fail "take-over fallback: checks never ran after the arm attached: $(cat "$home/fallback.out")"
assert_grep 'watcher: attached pid=' "$home/fallback.out" "fixture: the take-over did not fall back to attaching"
kill -TERM "$arm" 2>/dev/null || true
export FM_CHECK_INTERVAL=$saved_interval
pass "host+hook: a take-over that falls back to attaching ends the gap successor's check skip"
}

# FM_TEST_ONLY runs a single named case, the same entry point the sibling
# suites expose.
if [ -n "${FM_TEST_ONLY:-}" ]; then
Expand All @@ -774,6 +922,10 @@ test_successor_close_during_main_turn_is_delivered_at_the_next_turn_end
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_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
test_a_park_stopped_mid_take_over_leaves_the_take_over_to_the_next_park
test_unrecorded_successor_is_stopped_rather_than_left_for_main
test_primary_without_a_verified_mirror_runs_away_only
Expand Down
Loading