From 5f2c38b07457684061df7b6667acf18a248567c3 Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 21:24:48 -0500 Subject: [PATCH 1/2] fix/no-ticket(bin): deliver a wake appended during the host's announced gap instead of re-handing rearm-resurface --- bin/fm-watch-arm.sh | 16 +++++++ bin/fm-watch.sh | 38 ++++++++++++++++- docs/watcher-continuity.md | 4 +- tests/fm-supervision-host-stop-hook.test.sh | 46 +++++++++++++++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) diff --git a/bin/fm-watch-arm.sh b/bin/fm-watch-arm.sh index 5b1a816a47f..361c5ddea7c 100755 --- a/bin/fm-watch-arm.sh +++ b/bin/fm-watch-arm.sh @@ -76,6 +76,9 @@ # arm would, and otherwise this arm owns a fresh cycle as a plain arm does. # Recovery restoration follows docs/watcher-continuity.md "Generation reuse"; # an unconfirmed stop leaves downtime for the fresh cycle's recovery check. +# The host's take-over (FM_WATCH_KEEP_ANNOUNCED) of a stopped cycle whose marker +# is already pending downtime sets FM_WATCH_APPENDED_EPISODE on the fresh cycle +# so it delivers that queued wake instead of check: rearm-resurface. # Any other watcher, or one that outlives the stop, # is attached to exactly as a plain arm attaches. # @@ -795,6 +798,19 @@ confirm_extend_deadline() { deadline=$candidate } deadline=$(confirm_pre_lock_deadline) +# Set only for the supervision host's take-over (FM_WATCH_KEEP_ANNOUNCED) that +# stopped a live cycle whose marker was already pending downtime: a durable +# append during the pass-through gap. A plain --take-over keeps its own +# check: rearm-resurface contract. The fresh +# cycle delivers that queued wake instead of check: rearm-resurface +# (docs/watcher-continuity.md "Generation reuse"). +FM_WATCH_APPENDED_EPISODE=0 +if [ "$TAKEN_OVER" = 1 ] && [ "${FM_WATCH_KEEP_ANNOUNCED:-0}" = 1 ]; then + case "$FM_RECOVERY_HANDOVER_TOKEN" in + pending:downtime:*) FM_WATCH_APPENDED_EPISODE=1 ;; + esac +fi +export FM_WATCH_APPENDED_EPISODE if [ -n "${FM_WATCH_PREDECESSOR_ARM_PID:-}" ]; then FM_WATCH_HANDLING_SUCCESSOR=1 "$WATCH" >"$child_out" & else diff --git a/bin/fm-watch.sh b/bin/fm-watch.sh index fb1fedf0540..32e23ea63b3 100755 --- a/bin/fm-watch.sh +++ b/bin/fm-watch.sh @@ -3084,7 +3084,12 @@ fi # a quarantined malformed marker above all, which is approved over no queue row # at all - would otherwise be absorbed and the arm would never report it. WATCHER_RECOVERY_MARKER_APPROVED=0 -if [ "${FM_WATCH_HANDLING_SUCCESSOR:-0}" = 1 ]; then +if [ "${FM_WATCH_HANDLING_SUCCESSOR:-0}" = 1 ] \ + || [ "${FM_WATCH_APPENDED_EPISODE:-0}" = 1 ]; then + # FM_WATCH_APPENDED_EPISODE is the take-over of a gap whose append already + # opened a pending generation (docs/watcher-continuity.md "Generation reuse"). + # The arm check above still announces that generation. Waking + # check: rearm-resurface here handed the same episode straight back. WATCHER_RECOVERY_PENDING=0 elif [ "$FM_RECOVERY_MARKER_ACTION" = recover ]; then WATCHER_RECOVERY_PENDING=1 @@ -3286,7 +3291,38 @@ rerecord_device_shifted_pr_poll() { # # every cycle, and a line per poll would bury the triage log it shares. RECLAIMED_LOCK_ABSORB_LOGGED=0 +# Newest queued reason for a take-over whose gap append reopened the episode. +# A signal or stale outranks a check, and the generic recovery hand-back is +# never replayed as itself. Empty when the flag is off or the queue has none. +appended_episode_reason() { + [ "${FM_WATCH_APPENDED_EPISODE:-0}" = 1 ] || return 1 + [ -s "${FM_WAKE_QUEUE:-}" ] || return 1 + awk -F '\t' ' + NF < 5 { next } + $3 == "signal" { signal = $5 } + $3 == "stale" { stale = $5 } + $3 == "heartbeat" { heartbeat = $5 } + $3 == "check" && $5 != "check: rearm-resurface" { check = $5 } + END { + if (signal != "") print signal + else if (stale != "") print stale + else if (heartbeat != "") print heartbeat + else if (check != "") print check + } + ' "$FM_WAKE_QUEUE" 2>/dev/null +} + resurface_after_downtime() { + # A take-over of a pending episode an append opened during the gap delivers + # that wake once. check: rearm-resurface from here is the hand-back loop. + if [ "${FM_WATCH_APPENDED_EPISODE:-0}" = 1 ]; then + local appended_reason= + appended_reason=$(appended_episode_reason || true) + if [ -n "$appended_reason" ]; then + wake "$appended_reason" + fi + return 0 + fi # Handling successors already have a predecessor-delivered wake on the way. # Re-announcing from this cycle is what turned a lost handshake into an # unbounded recovery loop; stay in the poll loop and supervise instead. diff --git a/docs/watcher-continuity.md b/docs/watcher-continuity.md index de3d7e5e772..4b62dd1e10c 100644 --- a/docs/watcher-continuity.md +++ b/docs/watcher-continuity.md @@ -251,9 +251,11 @@ The supervision host's take-over sets `FM_WATCH_KEEP_ANNOUNCED`, and the fresh c 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 durable append during that gap still opens a fresh pending generation, because the gap successor does not run the check sweep or the recovery wake. +The take-over that finds the live successor holding that pending episode announces the generation and delivers the queued wake's own reason, so the close is not `check: rearm-resurface` 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. +A pending episode with queued work, including a row appended after main acknowledged the last one, still surfaces once through the ordinary arm check when the park has no live successor to take over. 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 3017934e045..b63dcf984ea 100755 --- a/tests/fm-supervision-host-stop-hook.test.sh +++ b/tests/fm-supervision-host-stop-hook.test.sh @@ -798,6 +798,51 @@ test_due_check_during_rearm_gap_stays_a_take_over() { pass "host+hook: a due check during the re-arm gap does not hand check: rearm-resurface again" } +# The 2026-11-06 long-turn loop, after one batch had already been handled. +# The pass-through leaves a gap successor holding an announced episode. A +# durable append during that gap opens a fresh pending generation, and the +# gap successor does not deliver it. The next Stop takes the live arm over +# and must hand that wake over once as itself. Handing check: rearm-resurface +# again leaves another gap, and the next append repeats it. +test_append_during_announced_gap_is_delivered_once() { + local home resurfaces + home=$(make_primary_home hook-gap-append) + : > "$home/hook.own-group" + export FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=999999 FM_SIGNAL_GRACE=0 + start_hook_session "$home" + queue_check "$home" resurface-fixture "check: work stranded before the host parked" \ + || fail "gap append: could not queue the stranded wake" + turn_end "$home" + wait_until 250 hook_exited "$home" \ + || fail "gap append: 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" "gap append (first)" + watcher_live "$home" || fail "gap append: the first pass-through left no successor" + queue_check "$home" lab-seat "check: lab-seat during the long turn" \ + || fail "gap append: could not append the wake during the gap" + turn_end "$home" + wait_until 250 hook_exited "$home" \ + || fail "gap append: the wake queued during the gap never reached the Stop hook: $(cat "$home/state/.supervision-host.log" 2>/dev/null)" + assert_re ' pass-through attended main-only check: lab-seat during the long turn$' "$home/state/.supervision-host.log" \ + "the wake queued during the gap must reach main as itself: $(cat "$home/state/.supervision-host.log")" + resurfaces=$(grep -c 'check: rearm-resurface' "$home/state/.supervision-host.log" || true) + [ "$resurfaces" -eq 1 ] \ + || fail "gap append: recovery was handed back again ($resurfaces times): $(cat "$home/state/.supervision-host.log")" + # No further append. The next turn end must not open another recovery hand-back. + turn_end "$home" + wait_until 200 grep -q $'\ttake-over\tarm=' "$home/state/.supervision-host.log" \ + || fail "gap append: the following turn end did not take the cycle over: $(cat "$home/state/.supervision-host.log")" + sleep 3 + resurfaces=$(grep -c 'check: rearm-resurface' "$home/state/.supervision-host.log" || true) + [ "$resurfaces" -eq 1 ] \ + || fail "gap append: the quiet takeover handed recovery back ($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 "gap append: the quiet takeover handed check: rearm-resurface: $(cat "$home/hook.err")" + fi + pass "host+hook: a wake appended during the announced gap is delivered once and does not hand check: rearm-resurface back" +} + # 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. @@ -999,6 +1044,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_append_during_announced_gap_is_delivered_once 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 From 24c47cc7a0b1f86c990bfb71657c8a00dbfbd7ca Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 21:28:03 -0500 Subject: [PATCH 2/2] no-mistakes(review): Rank queued check above heartbeat for gap-append wake --- bin/fm-watch.sh | 6 +++--- tests/fm-supervision-host-stop-hook.test.sh | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/bin/fm-watch.sh b/bin/fm-watch.sh index 32e23ea63b3..6017b1f9357 100755 --- a/bin/fm-watch.sh +++ b/bin/fm-watch.sh @@ -3292,8 +3292,8 @@ rerecord_device_shifted_pr_poll() { # RECLAIMED_LOCK_ABSORB_LOGGED=0 # Newest queued reason for a take-over whose gap append reopened the episode. -# A signal or stale outranks a check, and the generic recovery hand-back is -# never replayed as itself. Empty when the flag is off or the queue has none. +# A signal or stale outranks a check, a check outranks a heartbeat, and the +# generic recovery hand-back is never replayed as itself. Empty when the flag is off or the queue has none. appended_episode_reason() { [ "${FM_WATCH_APPENDED_EPISODE:-0}" = 1 ] || return 1 [ -s "${FM_WAKE_QUEUE:-}" ] || return 1 @@ -3306,8 +3306,8 @@ appended_episode_reason() { END { if (signal != "") print signal else if (stale != "") print stale - else if (heartbeat != "") print heartbeat else if (check != "") print check + else if (heartbeat != "") print heartbeat } ' "$FM_WAKE_QUEUE" 2>/dev/null } diff --git a/tests/fm-supervision-host-stop-hook.test.sh b/tests/fm-supervision-host-stop-hook.test.sh index b63dcf984ea..d46e9fb6c04 100755 --- a/tests/fm-supervision-host-stop-hook.test.sh +++ b/tests/fm-supervision-host-stop-hook.test.sh @@ -819,6 +819,8 @@ test_append_during_announced_gap_is_delivered_once() { "fixture: the stranded queue was not handed back once" assert_rewoke_main "$home" "gap append (first)" watcher_live "$home" || fail "gap append: the first pass-through left no successor" + FM_HOME="$home" bash -c '. "$1"; fm_wake_append heartbeat heartbeat heartbeat' _ "$ROOT/bin/fm-wake-lib.sh" \ + || fail "gap append: could not append the heartbeat during the gap" queue_check "$home" lab-seat "check: lab-seat during the long turn" \ || fail "gap append: could not append the wake during the gap" turn_end "$home"