diff --git a/bin/fm-supervision-host.sh b/bin/fm-supervision-host.sh index 59fefa34af5..5bb7dac01e0 100755 --- a/bin/fm-supervision-host.sh +++ b/bin/fm-supervision-host.sh @@ -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 @@ -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 diff --git a/bin/fm-watch-arm.sh b/bin/fm-watch-arm.sh index cfb2c79d637..c3705d64b87 100755 --- a/bin/fm-watch-arm.sh +++ b/bin/fm-watch-arm.sh @@ -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)" } diff --git a/bin/fm-watch.sh b/bin/fm-watch.sh index 263944e0dc8..dde7afd0dd7 100755 --- a/bin/fm-watch.sh +++ b/bin/fm-watch.sh @@ -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 @@ -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 diff --git a/docs/watcher-continuity.md b/docs/watcher-continuity.md index 821fcb25295..adc0d854ea8 100644 --- a/docs/watcher-continuity.md +++ b/docs/watcher-continuity.md @@ -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. diff --git a/tests/fm-supervision-host-stop-hook.test.sh b/tests/fm-supervision-host-stop-hook.test.sh index fd53b1d9af1..82741f466d8 100755 --- a/tests/fm-supervision-host-stop-hook.test.sh +++ b/tests/fm-supervision-host-stop-hook.test.sh @@ -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 . +queue_check() { # + 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 : 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() { #