Skip to content

fix(bin): stop a watcher promptly when a take-over lands in its event wait - #7740

Merged
kunchenguid merged 3 commits into
kunchenguid:mainfrom
vatayush:fm/fm-rearm-resurface-pr
Oct 25, 2026
Merged

kunchenguid merged 3 commits into
kunchenguid:mainfrom
vatayush:fm/fm-rearm-resurface-pr

Conversation

@vatayush

@vatayush vatayush commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Contribute the fix to upstream as a PR to the firstmate project.

The bug: on macOS with the system /bin/bash 3.2, a supervision-host take-over (bin/fm-watch-arm.sh --take-over) of a watcher that is inside its herdr event wait cannot confirm the stop within its 5-second bound, because event_wait_or_sleep runs the up-to-POLL event wait as a $(...) command substitution and bash 3.2 holds the TERM until that child exits. The unconfirmed take-over skips restoring main's acknowledgement, the stopped watcher's close mints a fresh downtime generation over an already-acknowledged recovery episode, and the next watcher announces it as "check: rearm-resurface" with an empty queue; the cycle repeats roughly every 30 seconds. The fix runs the event wait through the same waited background process group the watcher already uses for pane reads (watcher_capture), generalized to a waited helper, maps the helper's own setup failures to rc 2 for the event wait, and silences bash's job-termination notice in watcher_cleanup's stop; a regression case in tests/fm-watch-arm.test.sh takes over a watcher inside its herdr event wait and asserts a confirmed take-over, an unchanged acknowledged marker, and a quiet fresh cycle.

What Changed

  • bin/fm-watch.sh: the herdr event wait in event_wait_or_sleep no longer runs inside $(...). It now goes through watcher_waited, which is the old watcher_capture pane-read helper turned into a general waited background process group. On bash 3.2, the old command substitution held a HUP/TERM until the wait finished, which could take up to POLL. Now a take-over's stop is handled right away. The helper's own setup failures (mktemp, setpgid mismatch, stop failure) return rc 2, which the event wait treats as an unusable event path. watcher_capture is now a thin wrapper around watcher_waited.
  • Each event wait gets its own scratch TMPDIR, owned by the watcher. It is removed after each wait and in watcher_cleanup, so a stop that kills the wait mid-flight doesn't leave its reader FIFO behind. watcher_cleanup also hides bash's "Terminated" job notice when it stops a waited group still in flight.
  • Tests and docs: a new regression case in tests/fm-watch-arm.test.sh takes over a watcher while it is inside its herdr event wait. It checks that the take-over is confirmed, the acknowledged downtime marker is unchanged, and the fresh cycle stays quiet. tests/fm-supervision-events.test.sh now records the capability-confirmed flag to a file, because the wait now runs in a child process. docs/watcher-continuity.md notes that stops are now handled during the event wait too.

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The change sends the event wait through the same waited process-group mechanism the watcher already uses for pane reads. The rc mapping still matches every caller: watcher_capture callers only test for success or failure, and event_wait_or_sleep reads 2 as an unusable event path. The watcher-owned TMPDIR scratch only redirects the backends' own mktemp calls, and recovery-marker writes in cleanup use absolute paths. The regression test checks behavior without grepping implementation source.

Testing

I ran the new regression case on its own: it passes on HEAD and fails against the base bin/fm-watch.sh. The modified supervision-events test also passes. These are test-suite results, not live runs. Then I provisioned a real Herdr lab session (fm-lab-*) and ran the real arm and watcher on /bin/bash 3.2 with the real event reader against a marked disposable lab home. With the fix, a --take-over issued while the watcher sat inside its 40s event wait confirmed within 1-2s (ledger reason=taken-over). The acknowledged marker was unchanged, the fresh cycle stayed running with no rearm-resurface line, the stopped watcher printed no "Terminated" notice, and no wait scratch was left behind. With the base watcher, the take-over only attached after about 50s, the cycle failed, and the marker flipped from acked to pending:downtime. The lab session and homes were torn down, and the worktree is clean. This is CLI-only behavior, so the evidence is transcripts, not screenshots.

  • Live validation: ✅ go - 5 of 7 scenarios driven live against the product
Scenario Result Live Evidence
Captain's supervision host runs fm-watch-arm.sh --take-over while the watcher sits in its real Herdr event wait on /bin/bash 3.2; the take-over confirms the stop and a fresh watcher starts within the… ✅ pass live live-herdr-takeover-fix.txt and live-herdr-takeover-fix-run2.txt: fresh watcher started 1-2s after take-over; ledger arm_pid=... reason=taken-over successor=started:<pid>; old watcher gone
Main's acknowledged recovery marker survives the take-over unchanged (no new downtime generation minted) ✅ pass live live-herdr-takeover-fix*.txt: 'marker UNCHANGED (acknowledgement kept)'; base run shows acked -> pending:downtime instead
The fresh cycle after take-over stays quiet: it keeps running and prints no 'check: rearm-resurface' ✅ pass live live-herdr-takeover-fix*.txt: 'fresh cycle still running (quiet)', 'no rearm-resurface line' 5s after take-over
Adversarial base comparison: the same live take-over against the unfixed 842eab1 watcher reproduces the held-TERM bug ✅ pass live live-herdr-takeover-base.txt: take-over waited 50s, 'watcher: FAILED - cycle ended without an actionable reason', ledger reason=attached-cycle-ended, marker reopened to pending:downtime, fresh cycle C…
A watcher stopped inside its event wait prints no bash 'Terminated' job notice and leaves no fm-watch-wait/fm-herdr-eventwait scratch in TMPDIR ✅ pass live live-herdr-takeover-fix-run2.txt: 'no Terminated job notice in owner output'; 'scratch left in TMPDIR: []'
The regression case test_take_over_confirms_a_watcher_stopped_inside_its_event_wait fails before the fix and passes after it ⏸️ untested no The prior payload did not establish a live result for this one. It was run as a real-process test with a fake herdr binary and reader: it passed on HEAD and failed on base (regression-after-fix.txt, r…
The event wait still gets the cached capability verdict on each bounded wait now that it runs in a separate waited process ⏸️ untested no The prior payload did not establish a live result for this one. It was checked only through tests/fm-supervision-events.test.sh, which passed. Driving it live would need a real agent in the lab pane t…
Evidence: Live Herdr take-over with fix (run 1)

Source: Live Herdr take-over with fix (run 1)

[18:04:39 fix] bash: GNU bash, version 3.2.57(1)-release (arm64-apple-darwin23)
[18:04:39 fix] lab home: /private/var/folders/r_/qvbcjm3d7md61rf1qx66rcjh0000gn/T/fm-lab.1joRvm  herdr session: fm-lab-evtwait-29293-16627  pane: w1:p1
[18:04:40 fix] owner arm 10036: watcher: started pid=10067 (beacon fresh)
[18:04:41 fix] acknowledged marker: acked:handling:10808.1792890280.Ci6Byk
[18:04:41 fix] watcher 10067 is inside its event wait: reader pid=11158 (/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Resources/Python.app/Contents/MacO)
[18:04:42 fix] issuing: fm-watch-arm.sh --take-over 10036
[18:04:44 fix] take-over arm output after 2s:
    watcher: started pid=12354 (beacon fresh)
    ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/backends/herdr.sh: line 3913: printf: write error: Broken pipe
    ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/backends/herdr.sh: line 3914: printf: write error: Broken pipe
[18:04:44 fix] old watcher 10067 is gone
[18:04:44 fix] cycle-exit ledger:
    arm_pid=10036	watcher_pid=10067	origin=started	started_at=1792890279	ended_at=1792890282	exit_code=143	signal=TERM	reason=signal-exit	beacon_age=3	lock_before=pid:10067|identity:Sat Oct 24 18:04:39 2026     bash ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/fm-watch.sh	lock_after=pid:none|identity:none	successor=none
    arm_pid=11812	watcher_pid=10067	origin=attached	started_at=1792890282	ended_at=1792890283	exit_code=unknown	signal=unknown	reason=taken-over	beacon_age=4	lock_before=pid:10067|identity:Sat Oct 24 18:04:39 2026     bash ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/fm-watch.sh	lock_after=pid:none|identity:none	successor=started:12354
[18:04:44 fix] marker now: acked:handling:10808.1792890280.Ci6Byk
[18:04:44 fix] marker UNCHANGED (acknowledgement kept)
[18:04:49 fix] take-over arm output 5s later:
    watcher: started pid=12354 (beacon fresh)
    ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/backends/herdr.sh: line 3913: printf: write error: Broken pipe
    ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/backends/herdr.sh: line 3914: printf: write error: Broken pipe
[18:04:49 fix] fresh cycle still running (quiet)
[18:04:49 fix] no rearm-resurface line
[18:04:52 fix] scratch left in TMPDIR: []
[18:04:52 fix] lab home removed
Evidence: Live Herdr take-over with fix (run 2, includes stopped watcher output: no Terminated notice)

Source: Live Herdr take-over with fix (run 2, includes stopped watcher output: no Terminated notice)

[18:06:12 fix-run2] bash: GNU bash, version 3.2.57(1)-release (arm64-apple-darwin23)
[18:06:12 fix-run2] lab home: /private/var/folders/r_/qvbcjm3d7md61rf1qx66rcjh0000gn/T/fm-lab.kAsxUi  herdr session: fm-lab-evtwait-29293-16627  pane: w1:p1
[18:06:13 fix-run2] owner arm 40209: watcher: started pid=40240 (beacon fresh)
[18:06:14 fix-run2] acknowledged marker: acked:handling:41373.1792890373.qajhLy
[18:06:14 fix-run2] watcher 40240 is inside its event wait: reader pid=41696 (/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Resources/Python.app/Contents/MacO)
[18:06:15 fix-run2] issuing: fm-watch-arm.sh --take-over 40209
[18:06:16 fix-run2] take-over arm output after 1s:
    watcher: started pid=43682 (beacon fresh)
[18:06:16 fix-run2] old watcher 40240 is gone
[18:06:16 fix-run2] cycle-exit ledger:
    arm_pid=40209	watcher_pid=40240	origin=started	started_at=1792890372	ended_at=1792890375	exit_code=143	signal=TERM	reason=signal-exit	beacon_age=2	lock_before=pid:40240|identity:Sat Oct 24 18:06:12 2026     bash ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/fm-watch.sh	lock_after=pid:none|identity:none	successor=none
    arm_pid=43008	watcher_pid=40240	origin=attached	started_at=1792890375	ended_at=1792890376	exit_code=unknown	signal=unknown	reason=taken-over	beacon_age=3	lock_before=pid:40240|identity:Sat Oct 24 18:06:12 2026     bash ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/fm-watch.sh	lock_after=pid:none|identity:none	successor=started:43682
[18:06:16 fix-run2] marker now: acked:handling:41373.1792890373.qajhLy
[18:06:16 fix-run2] marker UNCHANGED (acknowledgement kept)
[18:06:22 fix-run2] take-over arm output 5s later:
    watcher: started pid=43682 (beacon fresh)
    ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/backends/herdr.sh: line 3913: printf: write error: Broken pipe
    ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/backends/herdr.sh: line 3914: printf: write error: Broken pipe
[18:06:22 fix-run2] fresh cycle still running (quiet)
[18:06:22 fix-run2] no rearm-resurface line
[18:06:22 fix-run2] stopped owner arm+watcher output (stdout+stderr):
    watcher: started pid=40240 (beacon fresh)
    ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/backends/herdr.sh: line 3913: printf: write error: Broken pipe
    ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/backends/herdr.sh: line 3914: printf: write error: Broken pipe
    watcher: FAILED - watcher cycle exited 143 without an actionable reason
[18:06:22 fix-run2] no Terminated job notice in owner output
[18:06:25 fix-run2] scratch left in TMPDIR: []
[18:06:25 fix-run2] lab home removed
Evidence: Live Herdr take-over with base 842eab1 watcher (bug reproduced: 50s, marker reopened)

Source: Live Herdr take-over with base 842eab1 watcher (bug reproduced: 50s, marker reopened)

[18:04:59 base-842eab1] bash: GNU bash, version 3.2.57(1)-release (arm64-apple-darwin23)
[18:04:59 base-842eab1] lab home: /private/var/folders/r_/qvbcjm3d7md61rf1qx66rcjh0000gn/T/fm-lab.3IklE7  herdr session: fm-lab-evtwait-29293-16627  pane: w1:p1
[18:05:00 base-842eab1] owner arm 18163: watcher: started pid=18195 (beacon fresh)
[18:05:01 base-842eab1] acknowledged marker: acked:handling:19063.1792890300.dXELu5
[18:05:01 base-842eab1] watcher 18195 is inside its event wait: reader pid=19193 (/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Resources/Python.app/Contents/MacO)
[18:05:02 base-842eab1] issuing: fm-watch-arm.sh --take-over 18163
[18:05:52 base-842eab1] take-over arm output after 50s:
    watcher: attached pid=18195 (beacon 14s)
    watcher: FAILED - cycle ended without an actionable reason
[18:05:52 base-842eab1] old watcher 18195 is gone
[18:05:52 base-842eab1] cycle-exit ledger:
    arm_pid=18163	watcher_pid=18195	origin=started	started_at=1792890299	ended_at=1792890341	exit_code=143	signal=TERM	reason=signal-exit	beacon_age=41	lock_before=pid:18195|identity:Sat Oct 24 18:04:59 2026     bash ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/fm-watch.sh	lock_after=pid:none|identity:none	successor=none
    arm_pid=19933	watcher_pid=18195	origin=attached	started_at=1792890314	ended_at=1792890352	exit_code=unknown	signal=unknown	reason=attached-cycle-ended	beacon_age=52	lock_before=pid:18195|identity:Sat Oct 24 18:04:59 2026     bash ~/.no-mistakes/worktrees/1d27782a5a0a/01M5R7SJTJ12BZPASRRDYXFB6P/bin/fm-watch.sh	lock_after=pid:none|identity:none	successor=none
[18:05:52 base-842eab1] marker now: pending:downtime:18195.1792890341.1b3Yxh
[18:05:52 base-842eab1] marker CHANGED (episode reopened)
[18:05:57 base-842eab1] take-over arm output 5s later:
    watcher: attached pid=18195 (beacon 14s)
    watcher: FAILED - cycle ended without an actionable reason
[18:05:57 base-842eab1] fresh cycle CLOSED
[18:05:57 base-842eab1] no rearm-resurface line
[18:06:00 base-842eab1] scratch left in TMPDIR: []
[18:06:00 base-842eab1] lab home removed
Evidence: Live driver script

Source: Live driver script

#!/usr/bin/env bash
# Live driver: real bin/fm-watch-arm.sh + bin/fm-watch.sh on /bin/bash 3.2 against a
# real Herdr lab session (fm-lab-*), real capability probe and real
# herdr-eventwait.py reader. Takes over the watcher while it sits in its event wait.
# Usage: live-herdr-takeover-driver.sh <worktree> <herdr-lab-session> <label>
set -u
WT=$1 S=$2 LABEL=$3
cd "$WT" || exit 1
LAB=$(cd "$(mktemp -d "${TMPDIR:-/tmp}/fm-lab.XXXXXX")" && pwd -P)
bin/fm-lab-home.sh create "$LAB" >/dev/null || exit 1
mkdir -p "$LAB/tmp"
STATE="$LAB/state"
printf 'window=%s:w1:p1\nbackend=herdr\nkind=ship\n' "$S" > "$STATE/demo.meta"
log() { printf '[%s %s] %s\n' "$(date +%T)" "$LABEL" "$*"; }

arm() {  # <pred> [args...]
  local pred=$1; shift
  exec env -u NO_MISTAKES_GATE -u FM_GATE_REFUSE_BYPASS -u FM_ROOT_OVERRIDE -u FM_STATE_OVERRIDE \
    -u FM_DATA_OVERRIDE -u FM_CONFIG_OVERRIDE -u FM_PROJECTS_OVERRIDE \
    -u HERDR_ENV -u HERDR_PANE_ID -u HERDR_TAB_ID -u HERDR_WORKSPACE_ID -u HERDR_SOCKET_PATH \
    -u HERDR_SESSION -u FM_BACKEND_HERDR_BIN \
    TMPDIR="$LAB/tmp" FM_HOME="$LAB" FM_POLL=40 FM_SIGNAL_GRACE=0 FM_CHECK_INTERVAL=999999 \
    FM_HEARTBEAT=999999 FM_BUSY_TURN_MAX_SECS=999999 FM_STALE_ESCALATE_SECS=999999 \
    FM_WATCH_PREDECESSOR_ARM_PID="$pred" /bin/bash bin/fm-watch-arm.sh "$@"
}
drain() { env -u FM_STATE_OVERRIDE -u FM_ROOT_OVERRIDE FM_HOME="$LAB" bin/fm-wake-drain.sh "$@"; }

log "bash: $(/bin/bash --version | head -1)"
log "lab home: $LAB  herdr session: $S  pane: w1:p1"
arm "$$" > "$LAB/owner.out" 2>&1 &
OWNER=$!
for _ in $(seq 1 400); do grep -q '^watcher: started pid=' "$LAB/owner.out" && break; sleep 0.05; done
log "owner arm $OWNER: $(cat "$LAB/owner.out")"
WATCHER=$(cat "$STATE/.watch.lock/pid")
# main handles and acknowledges a wake
env -u FM_ROOT_OVERRIDE FM_STATE_OVERRIDE="$STATE" bash -c '. bin/fm-wake-lib.sh; fm_wake_append signal take-over "signal: fixture handled by main"'
drain > /dev/null 2> "$LAB/drain.err"
seq_=$(sed -n 's/^WAKE_ACK_REQUIRED:.*--ack-through \([0-9]*\) --recovery-generation .*/\1/p' "$LAB/drain.err")
gen=$(sed -n 's/^WAKE_ACK_REQUIRED:.*--recovery-generation \([A-Za-z0-9._-]*\)$/\1/p' "$LAB/drain.err")
drain --ack-through "$seq_" --recovery-generation "$gen" > /dev/null 2>&1
ACKED=$(cat "$STATE/.watcher-down")
log "acknowledged marker: $ACKED"
# wait until the watcher's real event reader (herdr-eventwait.py) is subscribed
READER=
for _ in $(seq 1 1200); do [ -n "$WATCHER" ] || break
  READER=$(pgrep -f "herdr-eventwait.py" | while read -r p; do
    a=$p; while [ -n "$a" ] && [ "$a" -gt 1 ]; do [ "$a" = "$WATCHER" ] && { echo "$p"; break; }; a=$(ps -o ppid= -p "$a" | tr -d ' '); done; done | head -1)
  [ -n "$READER" ] && break; sleep 0.05
done
log "watcher $WATCHER is inside its event wait: reader pid=$READER ($(ps -o command= -p "$READER" 2>/dev/null | cut -c1-120))"
sleep 1
T0=$(date +%s)
log "issuing: fm-watch-arm.sh --take-over $OWNER"
arm "" --take-over "$OWNER" > "$LAB/arm.out" 2>&1 &
ARM=$!
for _ in $(seq 1 600); do grep -q '^watcher: started pid=' "$LAB/arm.out" && break; kill -0 "$ARM" 2>/dev/null || break; sleep 0.05; done
T1=$(date +%s)
log "take-over arm output after $((T1 - T0))s:"; sed 's/^/    /' "$LAB/arm.out"
kill -0 "$WATCHER" 2>/dev/null && log "old watcher $WATCHER STILL RUNNING" || log "old watcher $WATCHER is gone"
log "cycle-exit ledger:"; sed 's/^/    /' "$STATE/.watch-cycle-exits.log" 2>/dev/null
NOW=$(cat "$STATE/.watcher-down")
log "marker now: $NOW"
[ "$NOW" = "$ACKED" ] && log "marker UNCHANGED (acknowledgement kept)" || log "marker CHANGED (episode reopened)"
sleep 5
log "take-over arm output 5s later:"; sed 's/^/    /' "$LAB/arm.out"
kill -0 "$ARM" 2>/dev/null && log "fresh cycle still running (quiet)" || log "fresh cycle CLOSED"
grep -q '^check: rearm-resurface' "$LAB/arm.out" && log "RESURFACED: check: rearm-resurface" || log "no rearm-resurface line"
log "stopped owner arm+watcher output (stdout+stderr):"; sed "s/^/    /" "$LAB/owner.out"; grep -q Terminated "$LAB/owner.out" && log "owner output has a Terminated job notice" || log "no Terminated job notice in owner output"
kill -TERM "$ARM" 2>/dev/null; sleep 2
pkill -TERM -P "$OWNER" 2>/dev/null; kill -TERM "$OWNER" 2>/dev/null
FRESH=$(cat "$STATE/.watch.lock/pid" 2>/dev/null)
for _ in $(seq 1 100); do
  pgrep -f "$LAB" >/dev/null || break; sleep 0.1
done
pkill -TERM -f "$LAB" 2>/dev/null
sleep 1
log "scratch left in TMPDIR: [$(find "$LAB/tmp" \( -name 'fm-herdr-eventwait.*' -o -name 'fm-watch-wait.*' \) -print 2>/dev/null | tr '\n' ' ')]"
rm -rf "$LAB"
log "lab home removed"
Evidence: Regression case passes on fix

Source: Regression case passes on fix

ok - watch-arm: --take-over of a watcher inside its herdr event wait confirms the stop and resurfaces nothing
/bin/bash tests/zz-evt-only.test.sh  2.42s user 4.22s system 13% cpu 50.310 total
Evidence: Regression case fails on base watcher

Source: Regression case fails on base watcher

not ok - the take-over did not own a fresh cycle within 20s of a stop during the event wait: watcher: attached pid=72717 (beacon 14s)
watcher: FAILED - cycle ended without an actionable reason
/bin/bash tests/zz-evt-only.test.sh  2.79s user 5.93s system 9% cpu 1:33.85 total
Evidence: Base vs fix live comparison
fix: take-over output after 1s: 'watcher: started pid=43682'; ledger reason=taken-over; marker acked:handling:...qajhLy UNCHANGED; fresh cycle still running; no rearm-resurface
base: take-over output after 50s: 'watcher: attached ... / watcher: FAILED - cycle ended without an actionable reason'; ledger reason=attached-cycle-ended; marker acked:... -> pending:downtime:18195...; fresh cycle CLOSED
- Outcome: 🔧 1 issue found → auto-fixed ✅ across 2 runs (29m18s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

🔧 **Test** - 1 issue found → auto-fixed ✅
  • ⚠️ tests/fm-watch-arm.test.sh:1330 - The new regression case test_take_over_confirms_a_watcher_stopped_inside_its_event_wait (FM_POLL=12) passes against the unfixed bin/fm-watch.sh from 842eab1 on this macOS bash 3.2 host. The arm's 50×0.1s stop loop runs a ps fork on each iteration, so it stretches to about 11-12s and outlasts the 12s POLL. On base the held TERM was released just inside that stretched bound: the base ledger shows the take-over starting at 924 and confirming at 936. With FM_POLL=40 (and a longer wait for the start line) the same case fails on base and passes on the fix, so raising the POLL (and the matching start-line wait ceiling) would make this a real fail-before / pass-after regression.
  • Live validation: ✅ go - 5 of 7 scenarios driven live against the product
Scenario Result Live Evidence
A supervision host runs --take-over while the watcher sits in its herdr event wait; the stop is confirmed (reason=taken-over) and a fresh cycle starts ✅ pass live fixed-run and fixed-poll40 cycle ledgers: the attached take-over arm started and ended in the same second with reason=taken-over and successor=started:<pid>
After the take-over, main's acknowledged recovery marker is unchanged and the fresh cycle does not announce check: rearm-resurface ✅ pass live The regression case's .watcher-down equality and no-rearm-resurface checks passed on the fix (3/3 at POLL=12, 1/1 at POLL=40); arm.out shows only 'watcher: started'
Adversarial: the same take-over against the unfixed watcher (base 842eab1) shows the reported bug ✅ pass live base-poll40: the stop was held about 28s, the take-over fell through to attach, 'watcher: FAILED - cycle ended without an actionable reason', and .watcher-down became a fresh pending:downtime generati…
A stopped watcher prints no bash 'Terminated' job line and leaves no fm-watch-wait or fm-herdr-eventwait scratch behind ✅ pass live fixed-full-output.log has 0 'terminated' matches; the case's leftover-scratch check passed
A herdr blocked edge streamed during the event wait still wakes the watcher before POLL ends, now through watcher_waited ✅ pass live fixed-push-edge: elapsed 5s with FM_POLL=40, arm printed 'stale: evt:wG:pQ (herdr: agent blocked ...)'
The cached capability verdict reaches each waited event wait (updated supervision-events case) ⏸️ untested no The prior payload covered this only with a sourced function-level test (bash tests/fm-supervision-events.test.sh, all assertions passed). That is not a live product run, so it did not establish a li…
watcher_waited's own setup failures (mktemp or setpgid) make the event wait return rc 2 (sleep the budget) ⏸️ untested no Triggering this means making mktemp fail inside $STATE for the waited helper alone. Making STATE unwritable also breaks the watcher's own lock and beacon writes before it ever reaches the event wait,…
  • bash tests/fm-watch-arm.test.sh cut down to only test_take_over_confirms_a_watcher_stopped_inside_its_event_wait, fixed watcher, 3 runs (all ok)
  • Same single case with bin/fm-watch.sh restored from 842eab1 (base): still passes at FM_POLL=12, but its ledger shows the stop took about 12s
  • Copy of the case with FM_POLL=40 and a longer wait for the start line, against base bin/fm-watch.sh: not ok (take-over unconfirmed, attached-cycle-ended, acknowledged marker replaced by pending:downtime)
  • Same FM_POLL=40 copy against the fixed watcher: ok (taken-over in the same second, quiet fresh cycle)
  • bash tests/fm-supervision-events.test.sh (all assertions passed, including the updated per-wait capability check)
  • Full stdout and stderr capture of the fixed take-over case, checked for a 'Terminated' job notice (0 matches)
  • Hand-run live check: real arm and watcher with FM_POLL=40 and a fake reader that streams a blocked edge after 3s; the watcher woke with the stale/blocked reason after 5s and left no scratch dirs behind

🔧 Fix applied.
✅ Re-checked - no issues remain.

  • Live validation: ✅ go - 5 of 7 scenarios driven live against the product
Scenario Result Live Evidence
Captain's supervision host runs fm-watch-arm.sh --take-over while the watcher sits in its real Herdr event wait on /bin/bash 3.2; the take-over confirms the stop and a fresh watcher starts within the… ✅ pass live live-herdr-takeover-fix.txt and live-herdr-takeover-fix-run2.txt: fresh watcher started 1-2s after take-over; ledger arm_pid=... reason=taken-over successor=started:<pid>; old watcher gone
Main's acknowledged recovery marker survives the take-over unchanged (no new downtime generation minted) ✅ pass live live-herdr-takeover-fix*.txt: 'marker UNCHANGED (acknowledgement kept)'; base run shows acked -> pending:downtime instead
The fresh cycle after take-over stays quiet: it keeps running and prints no 'check: rearm-resurface' ✅ pass live live-herdr-takeover-fix*.txt: 'fresh cycle still running (quiet)', 'no rearm-resurface line' 5s after take-over
Adversarial base comparison: the same live take-over against the unfixed 842eab1 watcher reproduces the held-TERM bug ✅ pass live live-herdr-takeover-base.txt: take-over waited 50s, 'watcher: FAILED - cycle ended without an actionable reason', ledger reason=attached-cycle-ended, marker reopened to pending:downtime, fresh cycle C…
A watcher stopped inside its event wait prints no bash 'Terminated' job notice and leaves no fm-watch-wait/fm-herdr-eventwait scratch in TMPDIR ✅ pass live live-herdr-takeover-fix-run2.txt: 'no Terminated job notice in owner output'; 'scratch left in TMPDIR: []'
The regression case test_take_over_confirms_a_watcher_stopped_inside_its_event_wait fails before the fix and passes after it ⏸️ untested no The prior payload did not establish a live result for this one. It was run as a real-process test with a fake herdr binary and reader: it passed on HEAD and failed on base (regression-after-fix.txt, r…
The event wait still gets the cached capability verdict on each bounded wait now that it runs in a separate waited process ⏸️ untested no The prior payload did not establish a live result for this one. It was checked only through tests/fm-supervision-events.test.sh, which passed. Driving it live would need a real agent in the lab pane t…
  • /bin/bash tests/zz-evt-only.test.sh (a temporary copy of tests/fm-watch-arm.test.sh that runs only test_take_over_confirms_a_watcher_stopped_inside_its_event_wait), run on HEAD: pass
  • Same filtered case with bin/fm-watch.sh swapped to 842eab1 (base), then restored with git checkout -- bin/fm-watch.sh: fails with 'the take-over did not own a fresh cycle within 20s'
  • /bin/bash tests/fm-supervision-events.test.sh (the modified test): all assertions pass
  • Live Herdr lab: bin/fm-herdr-lab.sh provision fm-lab-evtwait-29293-16627, plus workspace create for pane w1:p1 through bin/fm-herdr-lab.sh run
  • Live driver live-herdr-takeover-driver.sh (fix, twice). Uses a marked lab home from bin/fm-lab-home.sh create, starts the real bin/fm-watch-arm.sh on /bin/bash 3.2 with FM_POLL=40 and the real capability probe and herdr-eventwait.py reader, acknowledges a wake through real fm-wake-drain.sh, waits for the reader subscription, then runs fm-watch-arm.sh --take-over &lt;owner&gt;
  • Same live driver with base 842eab1 bin/fm-watch.sh (restored afterward)
  • bin/fm-herdr-lab.sh teardown fm-lab-evtwait-29293-16627; lab homes removed with rm -rf
✅ **Document** - passed

✅ No issues found.

⚠️ **Lint** - 1 warning
  • ⚠️ linter found issues (exit code 1)
✅ **Push** - passed

✅ No issues found.

Local suite results (macOS, /bin/bash 3.2.57)

Run with bin/fm-test-run.sh --changed (26 suites) on a host that was also running other work (load average 5-11 on 8 cores), then each failing suite rerun alone on this branch and on unpatched main (d96bff0).

  • 19 suites passed, including fm-supervision-events, fm-watch-recovery-loop, fm-watch-inbox-meta, fm-watch-wedge-cap, fm-backend-t3code and fm-turnend-guard.
  • bin/fm-lint.sh passes with the pinned ShellCheck 0.11.0 and actionlint 1.7.12 (the pipeline's lint step could not find ShellCheck on its PATH, so it was checked locally on the pipeline head).

Failures that are pre-existing or environmental, not caused by this change:

  • fm-watch-arm: test_append_wakes_handling_successor ... identical fails with "re-append was not byte-identical to the inherited row", identically on unpatched main. The suite stops at its first failure, so every other case was run on its own: all 39 pass on this branch, and the 38 that exist on main pass there.
  • fm-guard-stale-banner: "an extension-owned hand-off with a live tagged session must stay silent" fails identically on unpatched main.
  • fm-watch-wedge-cap-unconfigured-parity: fails on both trees with cap_args[@]: unbound variable (an empty array under set -u on bash 3.2).
  • fm-wake-queue: "the oversized unread status line was truncated or omitted" follows grep: out of memory; the host's /usr/bin/grep (BSD grep 2.6.0) fails standalone on that 20000-character -Fx pattern.
  • fm-watch-checkpoint: failed only under load; passes alone on both trees.
  • fm-watch-triage: exceeded the runner's 1500s bound under load. Alone on this branch it failed one case near the bound at load ~10 (test_reheld_captain_call_starts_its_own_resurface_window); that case passes when run by itself on this branch and on main. Its fixtures use tmux panes, so the event wait is not on their path.
  • fm-watcher-lock: exceeded the 1500s bound, and a solo rerun on this branch hung in lock-stale-concurrency (a test child blocked reading a FIFO, "Interrupted system call") until it was stopped. The same hang was seen on this host before this change was written. That case drives only bin/fm-wake-lib.sh's lock, which this change does not touch. It was not rerun on main.

CI note

The "Behavior portable serial 1" job (tests/fm-supervision-host.test.sh) was cancelled by the CI provider at the job's 30-minute timeout rather than failing: every case it reached passed, and it was still progressing when cancelled. The same shard has been cancelled at that timeout on several recent main runs. It needs a maintainer rerun; every other required check passed.

vatsa added 3 commits October 24, 2026 17:36
On bash 3.2 a TERM is held until a running command substitution's child
exits. The watcher's push-backend event wait ran as rec=$(...) and blocks
for up to POLL, so a supervision-host take-over that landed there could not
confirm the stop within its 5s bound. The unconfirmed take-over skipped
restoring main's acknowledgement, the stopped watcher's close minted a fresh
downtime generation, and the next watcher announced it as
check: rearm-resurface with nothing queued, repeating every ~30s.

Run the event wait through the same waited background process group the
pane reads already use (watcher_capture, generalized to watcher_waited).
The helper's own setup failures now return 2, which the event wait reads as
an unusable event path rather than a full-budget wait. Because a stop now
kills the wait before its own cleanup, the wait gets a watcher-owned scratch
TMPDIR that the watcher removes after each wait and on exit, and the
cleanup's group stop is silenced so bash's job-termination notice does not
reach the arm's output.

Add a fm-watch-arm regression case that takes over a watcher inside its
herdr event wait and asserts a confirmed take-over, an unchanged
acknowledged marker, a quiet fresh cycle, and no leftover wait scratch.
Make the event-wait unit case assert the capability flag outside the wait,
since the wait now runs in its own process.
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: thank you @vatayush, and welcome. This is a clear write-up, and the base-vs-fix live comparison on /bin/bash 3.2 is exactly the evidence that helps.

I read the diff on head 09173aca against main 842eab16. It only touches bin/fm-watch.sh, docs/watcher-continuity.md and two test files, with no workflow changes, so I approved the first-time CI runs. They're running now.

My read:

  • Contract class: restore. A take-over (fm-watch-arm.sh --take-over) is already meant to confirm the TERM within its bound and stay quiet, and watcher_stop_signals already says a stop must not be held behind a $(...) child. The herdr event wait was the one blocking call still inside $(...), so on bash 3.2 the documented take-over path broke. Moving it onto the same waited process group that pane reads already use restores that path. It adds no new surface, wake, or config.
  • VISION, rule by rule: One captain aligns (no false rearm-resurface noise every ~30s). Authority aligns (no new permissions or defaults). Scripts/judgment aligns (deterministic signal handling). Restart aligns (the acknowledged downtime marker survives a take-over). Spine aligns (regression case that fails on base, live 5 of 7, gaps stated). Vendor aligns (fixes bash 3.2, Herdr-only path, no vendor coupling). Scope aligns.
  • no-mistakes: the attestation in the body binds 09173aca, and live validation says go.

Waiting on CI only. If CI comes back green and the PR stays mergeable, I expect to merge it next pass. Nothing is needed from you right now.

@vatayush

Copy link
Copy Markdown
Contributor Author

CI is done, can we merge this now?

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: thanks for checking in, @vatayush, and sorry for the wait. Your change is not the holdup.

All of CI is green on 09173aca except Behavior portable serial 1. That job keeps hitting the 30-minute job cap: in the latest attempt every test it ran printed ok, and then the runner cancelled the job at the time limit. That slowness comes from main's fm-supervision-host suite, so other PRs hit it too, and a fix is in flight separately. I never merge on a cancelled job, so I re-ran serial 1 again (attempt 5).

Nothing is needed from you. My read hasn't changed: contract class restore, VISION aligns on every rule, the no-mistakes attestation binds this head, and it merges cleanly. I also ran your two test files on current main 147b2672 plus this PR, and both pass. Once serial 1 comes back green and the head is still 09173aca, I'll merge it.

@kunchenguid
kunchenguid merged commit ffb2aca into kunchenguid:main Oct 25, 2026
89 of 93 checks passed
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: this is merged. Thank you @vatayush — really appreciate you taking the time on this. A watcher stop that lands inside the event wait now exits promptly on bash 3.2.

prishsu0730 pushed a commit to prishsu0730/firstmate that referenced this pull request Oct 29, 2026
… wait (kunchenguid#7740)

* fix(bin): stop a watcher promptly when a stop lands in its event wait

On bash 3.2 a TERM is held until a running command substitution's child
exits. The watcher's push-backend event wait ran as rec=$(...) and blocks
for up to POLL, so a supervision-host take-over that landed there could not
confirm the stop within its 5s bound. The unconfirmed take-over skipped
restoring main's acknowledgement, the stopped watcher's close minted a fresh
downtime generation, and the next watcher announced it as
check: rearm-resurface with nothing queued, repeating every ~30s.

Run the event wait through the same waited background process group the
pane reads already use (watcher_capture, generalized to watcher_waited).
The helper's own setup failures now return 2, which the event wait reads as
an unusable event path rather than a full-budget wait. Because a stop now
kills the wait before its own cleanup, the wait gets a watcher-owned scratch
TMPDIR that the watcher removes after each wait and on exit, and the
cleanup's group stop is silenced so bash's job-termination notice does not
reach the arm's output.

Add a fm-watch-arm regression case that takes over a watcher inside its
herdr event wait and asserts a confirmed take-over, an unchanged
acknowledged marker, a quiet fresh cycle, and no leftover wait scratch.
Make the event-wait unit case assert the capability flag outside the wait,
since the wait now runs in its own process.

* no-mistakes(test): Raise event-wait take-over regression POLL so it fails on base

* no-mistakes(document): Document event-wait stop handling in watcher continuity

---------

Co-authored-by: vatsa <[email protected]>
(cherry picked from commit ffb2aca)
vinicius928e8r84 pushed a commit to vinicius928e8r84/firstmate that referenced this pull request Nov 11, 2026
… wait (kunchenguid#7740)

* fix(bin): stop a watcher promptly when a stop lands in its event wait

On bash 3.2 a TERM is held until a running command substitution's child
exits. The watcher's push-backend event wait ran as rec=$(...) and blocks
for up to POLL, so a supervision-host take-over that landed there could not
confirm the stop within its 5s bound. The unconfirmed take-over skipped
restoring main's acknowledgement, the stopped watcher's close minted a fresh
downtime generation, and the next watcher announced it as
check: rearm-resurface with nothing queued, repeating every ~30s.

Run the event wait through the same waited background process group the
pane reads already use (watcher_capture, generalized to watcher_waited).
The helper's own setup failures now return 2, which the event wait reads as
an unusable event path rather than a full-budget wait. Because a stop now
kills the wait before its own cleanup, the wait gets a watcher-owned scratch
TMPDIR that the watcher removes after each wait and on exit, and the
cleanup's group stop is silenced so bash's job-termination notice does not
reach the arm's output.

Add a fm-watch-arm regression case that takes over a watcher inside its
herdr event wait and asserts a confirmed take-over, an unchanged
acknowledged marker, a quiet fresh cycle, and no leftover wait scratch.
Make the event-wait unit case assert the capability flag outside the wait,
since the wait now runs in its own process.

* no-mistakes(test): Raise event-wait take-over regression POLL so it fails on base

* no-mistakes(document): Document event-wait stop handling in watcher continuity

---------

Co-authored-by: vatsa <[email protected]>
(cherry picked from commit ffb2aca)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants