From 8176fd27533b2418b394edfd1095bb0321945e6b Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 09:07:56 -0500 Subject: [PATCH 1/5] fix/no-ticket(supervision-host): stop rearm-resurface on a dead successor A pass-through successor that ran a due check exited before the next park, so that park was not a take-over and handed check: rearm-resurface again. Skip that check sweep while nobody is reading, and when the left arm is already gone with the resurface still unacknowledged, announce the episode in place and start the replacement with the same hold a take-over uses. --- bin/fm-supervision-host.sh | 63 ++++++++++-- bin/fm-watch.sh | 7 +- docs/supervision-host.md | 2 +- docs/watcher-continuity.md | 10 +- tests/fm-supervision-host-stop-hook.test.sh | 100 ++++++++++++++++++++ 5 files changed, 170 insertions(+), 12 deletions(-) diff --git a/bin/fm-supervision-host.sh b/bin/fm-supervision-host.sh index 59fefa34af5..86490cb3bd7 100755 --- a/bin/fm-supervision-host.sh +++ b/bin/fm-supervision-host.sh @@ -67,7 +67,10 @@ # 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. A park +# that finds that arm already gone applies the same hold, so the unacked +# episode is not handed back as check: rearm-resurface again. # - 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 @@ -156,9 +159,10 @@ # engine turn. It also reads the record of a successor a pass-through left for # main: while that arm still runs under its recorded identity, the first cycle # without --restart requests a take-over rather than an ordinary attach. -# Activation removes the -# record only once that identity is no longer alive, so a later host retries a -# take-over that left it running. +# Activation removes the record once that identity is no longer alive. That +# park applies the same announced-episode hold instead of an ordinary arm +# (docs/watcher-continuity.md "Generation reuse"). A later host retries a +# take-over only while the recorded arm is still alive. # # STATE (all under state/, owned here): .supervision-host (this host's pid and # the processes it runs), .supervision-host-engine (the engine conversation: @@ -293,6 +297,9 @@ ENGINE_RUNNING=0 # The successor arm a predecessor's pass-through left for main, which the # first cycle takes over. LEFT_ARM= +# That arm's pid when the record was present and the arm was already gone. +# The first cycle then holds an unacked resurface instead of handing it back. +DEAD_LEFT_ARM= # The running turn's result and diagnostics files, removed by the cleanup when # the host is stopped mid-turn. TURN_RESULT= @@ -404,19 +411,50 @@ reap_recorded_host() { "$STATE"/.supervision-host-errors.* "$STATE"/.supervision-host-readback.* "$TURN_FILE" "$MIRROR_FEED" 2>/dev/null || true } +# True when this park continues an unacked main-only resurface whose left +# successor did not survive to be taken over. An acknowledged episode, or a +# park with no left arm, is not this hold: the ordinary arm check still +# announces work that has not been handed back. +repeat_resurface_hold() { + [ -n "$DEAD_LEFT_ARM" ] || return 1 + grep -q $'pass-through\tattended\tmain-only\tcheck: rearm-resurface' "$HOST_LOG" 2>/dev/null || return 1 + fm_recovery_marker_snapshot "$STATE/.watcher-down" || return 1 + case "$FM_RECOVERY_MARKER_TOKEN" in + acked:*) return 1 ;; + esac + return 0 +} + +# Announce an unacked downtime episode in place. The replacement cycle's arm +# check then waits, instead of recovering it into another check: rearm-resurface. +# docs/watcher-continuity.md "Generation reuse" owns the hold. +settle_unacked_resurface() { + fm_recovery_marker_snapshot "$STATE/.watcher-down" || return 1 + case "$FM_RECOVERY_MARKER_TOKEN" in + announced:*|pending:handling:*|acked:*) return 0 ;; + esac + fm_recovery_queue_has_work || return 0 + fm_recovery_marker_arm_check "$STATE/.watcher-down" +} + activate() { local pid identity mkdir -p "$STATE" || return 1 reap_recorded_host || return 1 # The successor a pass-through left for main: the first cycle takes it over - # while it still answers to its recorded identity, and its record goes only - # once it does not. + # while it still answers to its recorded identity. A record whose arm is + # already gone is remembered for the resurface hold, then removed. + DEAD_LEFT_ARM= if [ -f "$LEFT_RECORD" ]; then pid='' identity='' IFS="$(printf '\t')" read -r pid identity < "$LEFT_RECORD" || true if fm_pid_alive "$pid" && [ -n "$identity" ] && [ "$(identity_of "$pid")" = "$identity" ]; then LEFT_ARM=$pid else + case "$pid" in + ''|*[!0-9]*) ;; + *) DEAD_LEFT_ARM=$pid ;; + esac rm -f "$LEFT_RECORD" fi fi @@ -752,7 +790,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 @@ -1144,6 +1185,14 @@ elif [ -n "$LEFT_ARM" ]; then # generation for the same queued work, and that generation's resurface is # attended main-only, so every following turn end handed it straight back. FM_WATCH_KEEP_ANNOUNCED=1 start_arm "$OWNER_PREDECESSOR" --take-over "$LEFT_ARM" +elif repeat_resurface_hold; then + log_line "hold dead-arm=$DEAD_LEFT_ARM" + # The left successor is gone and the resurface it followed is still + # unacknowledged. Announce that episode in place, then start the replacement + # with the same hold a take-over uses, so this park does not hand + # check: rearm-resurface back. + settle_unacked_resurface || { echo "watcher: FAILED - the supervision host could not keep the unacked recovery announced"; exit 1; } + FM_WATCH_KEEP_ANNOUNCED=1 start_arm "" else start_arm "$OWNER_PREDECESSOR" fi || { echo "watcher: FAILED - the supervision host could not start a watcher cycle"; exit 1; } diff --git a/bin/fm-watch.sh b/bin/fm-watch.sh index 263944e0dc8..197e2524cdb 100755 --- a/bin/fm-watch.sh +++ b/bin/fm-watch.sh @@ -3450,7 +3450,12 @@ 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 while nobody is reading (bin/fm-supervision-host.sh + # leave_successor_for_main) skips this sweep. 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". + if [ "${FM_WATCH_GAP_SUCCESSOR:-0}" != 1 ] && [ "$check_age" -ge "$CHECK_INTERVAL" ]; then rejected_checks= contribution_check_output= for c in "$STATE"/*.check.sh; do diff --git a/docs/supervision-host.md b/docs/supervision-host.md index cf175b4cf9a..94ccc031dc8 100644 --- a/docs/supervision-host.md +++ b/docs/supervision-host.md @@ -112,7 +112,7 @@ On that main-only pass-through the host starts the successor watcher cycle and l That successor, like one a turn hands back at its start (see [Away](#away)), runs in a process group of its own, so the harness tearing down the hook's group after the rewake does not stop it. The host leaves the watcher's recovery marker reading downtime, confirming no handling handoff, because the re-arm owner delivers a close to main only while that marker reads downtime. The session's next park without `--restart` requests a take-over to restore a single host-owned arm; the [host header](../bin/fm-supervision-host.sh) owns successor persistence and cleanup, and the [arm header](../bin/fm-watch-arm.sh) owns take-over eligibility and fallback. -That take-over keeps an already-announced recovery from being handed straight back to main; [Generation reuse](watcher-continuity.md#generation-reuse) owns the hold. +That take-over, and a later park whose left successor has already exited, keeps an already-announced recovery from being handed straight back to main; [Generation reuse](watcher-continuity.md#generation-reuse) owns the hold. OpenCode and omp still launch the host with `--restart`, which takes precedence over recorded take-over and lacks its acknowledgement-preserving handover; changing that first-cycle path remains a follow-up. The host-off Claude Stop hook's detached handling successor is also unchanged; see [Claude handling successor](watcher-continuity.md#claude-handling-successor). It also passes the close through unchanged, with no added line, when any of these holds (`fm_supervision_host_attended_ready` in `bin/fm-supervision-engine-lib.sh` owns the list): diff --git a/docs/watcher-continuity.md b/docs/watcher-continuity.md index 821fcb25295..c0c8106ad5a 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. +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. +When that left arm is already gone and the resurface is still unacknowledged, the host announces the pending generation in place and starts the replacement with the same hold, so the replacement does not hand `check: rearm-resurface` again. +A pending episode a take-over finds, or one no pass-through has handed yet, still surfaces once through the ordinary arm check. +Without that 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..2d7aab7bbc5 100755 --- a/tests/fm-supervision-host-stop-hook.test.sh +++ b/tests/fm-supervision-host-stop-hook.test.sh @@ -752,6 +752,104 @@ 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" +} + +# The same loop when the successor is already dead at the next park, which is +# the start that logged no take-over. An append during the gap mints a fresh +# pending generation (what the unread check did); the next park must not hand +# check: rearm-resurface for that unacked episode. +test_dead_left_successor_does_not_hand_resurface_again() { + local home left_arm watcher starts resurfaces + 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" + # The unread check's append. The successor is then gone, so the next start + # cannot take its cycle over. + queue_check "$home" lab-seat "check: lab-seat: seats are out of quota" \ + || fail "fixture: could not append the gap check" + 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" + starts=$(grep -c $'\tstart\t' "$home/state/.supervision-host.log" || true) + turn_end "$home" + wait_until 200 bash -c '[ "$(grep -c $'"'"'\tstart\t'"'"' "$1" 2>/dev/null || true)" -gt "$2" ]' _ \ + "$home/state/.supervision-host.log" "$starts" \ + || fail "dead successor: the next park never started: $(cat "$home/state/.supervision-host.log")" + sleep 3 + if grep -q $'\ttake-over\t' "$home/state/.supervision-host.log"; then + fail "dead successor: a dead arm was taken over: $(cat "$home/state/.supervision-host.log")" + fi + resurfaces=$(grep -c 'check: rearm-resurface' "$home/state/.supervision-host.log" || true) + [ "$resurfaces" -eq 1 ] \ + || fail "dead successor: 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 "dead successor: the next park handed check: rearm-resurface: $(cat "$home/hook.err")" + fi + grep -q $'\thold\tdead-arm=' "$home/state/.supervision-host.log" \ + || fail "dead successor: the park did not hold the unacked recovery: $(cat "$home/state/.supervision-host.log")" + watcher_live "$home" \ + || fail "dead successor: the held park left no watcher: $(cat "$home/state/.supervision-host.log")" + ! hook_exited "$home" \ + || fail "dead successor: the held park handed a wake: $(cat "$home/hook.err" 2>/dev/null)" + pass "host+hook: a park whose left successor is already dead does not hand check: rearm-resurface again" +} + # FM_TEST_ONLY runs a single named case, the same entry point the sibling # suites expose. if [ -n "${FM_TEST_ONLY:-}" ]; then @@ -774,6 +872,8 @@ 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_dead_left_successor_does_not_hand_resurface_again 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 From d10e431463869a61dcdfe8657626dddf3d55e10c Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 09:15:47 -0500 Subject: [PATCH 2/5] no-mistakes(review): Remove dead-left-arm resurface hold; test new row still reaches main --- bin/fm-supervision-host.sh | 57 +++------------------ docs/supervision-host.md | 2 +- docs/watcher-continuity.md | 3 +- tests/fm-supervision-host-stop-hook.test.sh | 48 ++++++----------- 4 files changed, 24 insertions(+), 86 deletions(-) diff --git a/bin/fm-supervision-host.sh b/bin/fm-supervision-host.sh index 86490cb3bd7..5bb7dac01e0 100755 --- a/bin/fm-supervision-host.sh +++ b/bin/fm-supervision-host.sh @@ -68,9 +68,7 @@ # 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"). The successor left for -# main does not run the check sweep while nobody is reading it. A park -# that finds that arm already gone applies the same hold, so the unacked -# episode is not handed back as check: rearm-resurface again. +# 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 @@ -159,10 +157,9 @@ # engine turn. It also reads the record of a successor a pass-through left for # main: while that arm still runs under its recorded identity, the first cycle # without --restart requests a take-over rather than an ordinary attach. -# Activation removes the record once that identity is no longer alive. That -# park applies the same announced-episode hold instead of an ordinary arm -# (docs/watcher-continuity.md "Generation reuse"). A later host retries a -# take-over only while the recorded arm is still alive. +# Activation removes the +# record only once that identity is no longer alive, so a later host retries a +# take-over that left it running. # # STATE (all under state/, owned here): .supervision-host (this host's pid and # the processes it runs), .supervision-host-engine (the engine conversation: @@ -297,9 +294,6 @@ ENGINE_RUNNING=0 # The successor arm a predecessor's pass-through left for main, which the # first cycle takes over. LEFT_ARM= -# That arm's pid when the record was present and the arm was already gone. -# The first cycle then holds an unacked resurface instead of handing it back. -DEAD_LEFT_ARM= # The running turn's result and diagnostics files, removed by the cleanup when # the host is stopped mid-turn. TURN_RESULT= @@ -411,50 +405,19 @@ reap_recorded_host() { "$STATE"/.supervision-host-errors.* "$STATE"/.supervision-host-readback.* "$TURN_FILE" "$MIRROR_FEED" 2>/dev/null || true } -# True when this park continues an unacked main-only resurface whose left -# successor did not survive to be taken over. An acknowledged episode, or a -# park with no left arm, is not this hold: the ordinary arm check still -# announces work that has not been handed back. -repeat_resurface_hold() { - [ -n "$DEAD_LEFT_ARM" ] || return 1 - grep -q $'pass-through\tattended\tmain-only\tcheck: rearm-resurface' "$HOST_LOG" 2>/dev/null || return 1 - fm_recovery_marker_snapshot "$STATE/.watcher-down" || return 1 - case "$FM_RECOVERY_MARKER_TOKEN" in - acked:*) return 1 ;; - esac - return 0 -} - -# Announce an unacked downtime episode in place. The replacement cycle's arm -# check then waits, instead of recovering it into another check: rearm-resurface. -# docs/watcher-continuity.md "Generation reuse" owns the hold. -settle_unacked_resurface() { - fm_recovery_marker_snapshot "$STATE/.watcher-down" || return 1 - case "$FM_RECOVERY_MARKER_TOKEN" in - announced:*|pending:handling:*|acked:*) return 0 ;; - esac - fm_recovery_queue_has_work || return 0 - fm_recovery_marker_arm_check "$STATE/.watcher-down" -} - activate() { local pid identity mkdir -p "$STATE" || return 1 reap_recorded_host || return 1 # The successor a pass-through left for main: the first cycle takes it over - # while it still answers to its recorded identity. A record whose arm is - # already gone is remembered for the resurface hold, then removed. - DEAD_LEFT_ARM= + # while it still answers to its recorded identity, and its record goes only + # once it does not. if [ -f "$LEFT_RECORD" ]; then pid='' identity='' IFS="$(printf '\t')" read -r pid identity < "$LEFT_RECORD" || true if fm_pid_alive "$pid" && [ -n "$identity" ] && [ "$(identity_of "$pid")" = "$identity" ]; then LEFT_ARM=$pid else - case "$pid" in - ''|*[!0-9]*) ;; - *) DEAD_LEFT_ARM=$pid ;; - esac rm -f "$LEFT_RECORD" fi fi @@ -1185,14 +1148,6 @@ elif [ -n "$LEFT_ARM" ]; then # generation for the same queued work, and that generation's resurface is # attended main-only, so every following turn end handed it straight back. FM_WATCH_KEEP_ANNOUNCED=1 start_arm "$OWNER_PREDECESSOR" --take-over "$LEFT_ARM" -elif repeat_resurface_hold; then - log_line "hold dead-arm=$DEAD_LEFT_ARM" - # The left successor is gone and the resurface it followed is still - # unacknowledged. Announce that episode in place, then start the replacement - # with the same hold a take-over uses, so this park does not hand - # check: rearm-resurface back. - settle_unacked_resurface || { echo "watcher: FAILED - the supervision host could not keep the unacked recovery announced"; exit 1; } - FM_WATCH_KEEP_ANNOUNCED=1 start_arm "" else start_arm "$OWNER_PREDECESSOR" fi || { echo "watcher: FAILED - the supervision host could not start a watcher cycle"; exit 1; } diff --git a/docs/supervision-host.md b/docs/supervision-host.md index 94ccc031dc8..cf175b4cf9a 100644 --- a/docs/supervision-host.md +++ b/docs/supervision-host.md @@ -112,7 +112,7 @@ On that main-only pass-through the host starts the successor watcher cycle and l That successor, like one a turn hands back at its start (see [Away](#away)), runs in a process group of its own, so the harness tearing down the hook's group after the rewake does not stop it. The host leaves the watcher's recovery marker reading downtime, confirming no handling handoff, because the re-arm owner delivers a close to main only while that marker reads downtime. The session's next park without `--restart` requests a take-over to restore a single host-owned arm; the [host header](../bin/fm-supervision-host.sh) owns successor persistence and cleanup, and the [arm header](../bin/fm-watch-arm.sh) owns take-over eligibility and fallback. -That take-over, and a later park whose left successor has already exited, keeps an already-announced recovery from being handed straight back to main; [Generation reuse](watcher-continuity.md#generation-reuse) owns the hold. +That take-over keeps an already-announced recovery from being handed straight back to main; [Generation reuse](watcher-continuity.md#generation-reuse) owns the hold. OpenCode and omp still launch the host with `--restart`, which takes precedence over recorded take-over and lacks its acknowledgement-preserving handover; changing that first-cycle path remains a follow-up. The host-off Claude Stop hook's detached handling successor is also unchanged; see [Claude handling successor](watcher-continuity.md#claude-handling-successor). It also passes the close through unchanged, with no added line, when any of these holds (`fm_supervision_host_attended_ready` in `bin/fm-supervision-engine-lib.sh` owns the list): diff --git a/docs/watcher-continuity.md b/docs/watcher-continuity.md index c0c8106ad5a..656f7dd23bc 100644 --- a/docs/watcher-continuity.md +++ b/docs/watcher-continuity.md @@ -249,8 +249,7 @@ 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. 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. -When that left arm is already gone and the resurface is still unacknowledged, the host announces the pending generation in place and starts the replacement with the same hold, so the replacement does not hand `check: rearm-resurface` again. -A pending episode a take-over finds, or one no pass-through has handed yet, 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. Without that 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 2d7aab7bbc5..546531fa5a7 100755 --- a/tests/fm-supervision-host-stop-hook.test.sh +++ b/tests/fm-supervision-host-stop-hook.test.sh @@ -798,12 +798,12 @@ 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 same loop when the successor is already dead at the next park, which is -# the start that logged no take-over. An append during the gap mints a fresh -# pending generation (what the unread check did); the next park must not hand -# check: rearm-resurface for that unacked episode. -test_dead_left_successor_does_not_hand_resurface_again() { - local home left_arm watcher starts resurfaces +# 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" @@ -816,38 +816,22 @@ test_dead_left_successor_does_not_hand_resurface_again() { "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" - # The unread check's append. The successor is then gone, so the next start - # cannot take its cycle over. - queue_check "$home" lab-seat "check: lab-seat: seats are out of quota" \ - || fail "fixture: could not append the gap check" + 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" - starts=$(grep -c $'\tstart\t' "$home/state/.supervision-host.log" || true) + 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 200 bash -c '[ "$(grep -c $'"'"'\tstart\t'"'"' "$1" 2>/dev/null || true)" -gt "$2" ]' _ \ - "$home/state/.supervision-host.log" "$starts" \ - || fail "dead successor: the next park never started: $(cat "$home/state/.supervision-host.log")" - sleep 3 - if grep -q $'\ttake-over\t' "$home/state/.supervision-host.log"; then - fail "dead successor: a dead arm was taken over: $(cat "$home/state/.supervision-host.log")" - fi - resurfaces=$(grep -c 'check: rearm-resurface' "$home/state/.supervision-host.log" || true) - [ "$resurfaces" -eq 1 ] \ - || fail "dead successor: 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 "dead successor: the next park handed check: rearm-resurface: $(cat "$home/hook.err")" - fi - grep -q $'\thold\tdead-arm=' "$home/state/.supervision-host.log" \ - || fail "dead successor: the park did not hold the unacked recovery: $(cat "$home/state/.supervision-host.log")" - watcher_live "$home" \ - || fail "dead successor: the held park left no watcher: $(cat "$home/state/.supervision-host.log")" - ! hook_exited "$home" \ - || fail "dead successor: the held park handed a wake: $(cat "$home/hook.err" 2>/dev/null)" - pass "host+hook: a park whose left successor is already dead does not hand check: rearm-resurface again" + 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" } # FM_TEST_ONLY runs a single named case, the same entry point the sibling @@ -873,7 +857,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_dead_left_successor_does_not_hand_resurface_again +test_row_queued_after_ack_reaches_main_when_the_left_successor_is_gone 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 From 7c7bd5f79ac7ae8d94dcd03ac4bc49df170eb42f Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 09:31:24 -0500 Subject: [PATCH 3/5] no-mistakes(review): End gap successor's check skip on attach or record change --- bin/fm-watch-arm.sh | 1 + bin/fm-watch.sh | 36 +++++++++-- docs/watcher-continuity.md | 1 + tests/fm-supervision-host-stop-hook.test.sh | 67 +++++++++++++++++++++ 4 files changed, 99 insertions(+), 6 deletions(-) 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 197e2524cdb..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,12 +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. - # A successor left for main while nobody is reading (bin/fm-supervision-host.sh - # leave_successor_for_main) skips this sweep. 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". - if [ "${FM_WATCH_GAP_SUCCESSOR:-0}" != 1 ] && [ "$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 656f7dd23bc..d85836a75cb 100644 --- a/docs/watcher-continuity.md +++ b/docs/watcher-continuity.md @@ -247,6 +247,7 @@ That handling republication gives a successor exactly one recovery presentation 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 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. diff --git a/tests/fm-supervision-host-stop-hook.test.sh b/tests/fm-supervision-host-stop-hook.test.sh index 546531fa5a7..cf7454a707d 100755 --- a/tests/fm-supervision-host-stop-hook.test.sh +++ b/tests/fm-supervision-host-stop-hook.test.sh @@ -834,6 +834,71 @@ test_row_queued_after_ack_reaches_main_when_the_left_successor_is_gone() { 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() { #