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
16 changes: 16 additions & 0 deletions bin/fm-watch-arm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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
Expand Down
38 changes: 37 additions & 1 deletion bin/fm-watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3286,7 +3291,38 @@ rerecord_device_shifted_pr_poll() { # <id>
# 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, 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
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 (check != "") print check
else if (heartbeat != "") print heartbeat
}
' "$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.
Expand Down
4 changes: 3 additions & 1 deletion docs/watcher-continuity.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 48 additions & 0 deletions tests/fm-supervision-host-stop-hook.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,53 @@ 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"
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"
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.
Expand Down Expand Up @@ -999,6 +1046,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
Expand Down