Repository navigation
fix(bin): renew watcher beacon after each completed step in slow cycles - #24
Merged
Merged
Conversation
Scratch snapshot timing (seconds; before -> after): fleet ledger 0 -> 0; home summary 0 -> 0; reconcile requests 0 -> 0; pending replies 120 -> 7; secondmate liveness 3 -> 0; secondmate stall 0 -> 0; procevent 0 -> 1; inactive outcomes not reached -> 0; checks not reached -> 0; signal scan not reached -> 3; signal triage not reached -> 84. Baseline ended on recovery before later stages; liveness/procevent/check execution was suppressed in the scratch copy to avoid touching live routes. 377 of 378 reply records were resolved. Jevhelper rated pending replies dominant in measured baseline (confidence 0.92); signal triage was measured only after optimization. Refresh the beacon at completed steps, per signal/report and before terminal wait; retain the 300-second floor. Make resolved-reply scanning use one shell read per record and skip unnecessary closure locks. Exercise a backdated beacon during a live slow check and real auto-arm attachment. Tests: fm-watch-beacon, fm-pending-reply, fm-watch-arm and targeted fm-watch-triage slice passed; full fm-watch-triage exceeded the contained 900-second limit without a reported failure before timeout; fm-lint passed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Reviewer's GuideThe PR makes watcher liveness progress-aware: fm-watch.sh refreshes its beacon only after completed work, including granular checks and report scans, while retaining stale detection for blocked steps and the existing grace policy. It also substantially optimizes pending-reply scans and adds documentation and regression coverage for slow-cycle auto-arm behavior. Sequence diagram for progress-aware watcher beacon renewalsequenceDiagram
participant W as fm-watch.sh
participant B as .last-watcher-beat
participant C as CompletedStep
participant A as fm-watch-arm.sh
W->>B: watch_step_done cycle-start
W->>C: Run expensive step
C-->>W: Step completes
W->>B: watch_step_done step
W->>C: Run next step
C-->>W: Step completes
W->>B: watch_step_done step
A->>B: Check beacon age
B-->>A: Fresh beacon
A-->>A: Attach to live watcher
alt Step stalls
W-->>C: Blocked step does not complete
B-->>A: Beacon becomes stale
A-->>A: Refuse stale live watcher
end
Flow diagram for watcher cycle beacon checkpointsflowchart TD
Start[Cycle starts] --> Ledger[fleet-ledger]
Ledger --> Beat1[watch_step_done]
Beat1 --> Summary[home-summary and reconciliation]
Summary --> Beat2[watch_step_done]
Beat2 --> Replies[pending-replies and liveness checks]
Replies --> Beat3[watch_step_done]
Beat3 --> Checks[Each custom or PR check]
Checks --> Beat4[watch_step_done per check]
Beat4 --> Signals[Signal and heartbeat scans]
Signals --> Beat5[watch_step_done per scan]
Beat5 --> Reports[Each direct report inspection]
Reports --> Beat6[watch_step_done per report]
Beat6 --> Wait[Terminal wait]
Wait --> Start
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
…ests/fm-watch-beacon.test.sh loads wake-helpers.sh through a dynamic path but was missing the `# shellcheck source=tests/wake-helpers.sh` comment that every other test loading that file has. CI's lint run (--external-sources) therefore reported SC1091 and failed. Rule: every file a linted script loads must be one ShellCheck can resolve. The other file-loading lines in the files this PR changes pass the same lint run, so this file was the only one affected. Fix: added that one comment above the loading line in tests/fm-watch-beacon.test.sh. The test's behaviour does not change. Checks run: before the fix, `bin/fm-lint.sh tests/fm-watch-beacon.test.sh` (explicit paths use CI's settings) failed with exit 1 and the same SC1091 error. After the fix, `bin/fm-lint.sh` on all 4 shell files the PR changes (bin/fm-watch.sh, bin/fm-pending-reply-lib.sh, tests/fm-watch-beacon.test.sh, tests/fm-watch-triage.test.sh) exits 0, and `bash tests/fm-watch-beacon.test.sh` still passes. The change is in the working tree and not committed
Owner
Author
|
Final disposition for head 72667ed:
|
This was referenced Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
fm-watch.sh: under load, watcher cycles of 3-7 minutes exceed the 300 s beacon grace and trip auto-arm FAILED on every turn end.
Observation (2026-09-26 05:00-05:45, 8-core VPS, 1-minute load 25-33): every started fm-watch.sh cycle in state/.watch-cycle-exits.log ran 183-438 s and exited on actionable-signal with beacon_age equal to the cycle duration, meaning state/.last-watcher-beat is touched once at cycle start and never again before the cycle ends.
Once beacon age passes the 300 s grace (fm_poll_derived_grace), the Stop hook’s auto-arm refuses (lock held by a live pid, stale beat), fails twice, and the supervisor gets the ‘auto-arm FAILED’ notice on every turn end until the watcher exits on its own.
Four episodes in 45 minutes; all self-healed; no wake lost.
Ask:
(1) instrument one cycle, reading the main home read-only, to find which per-iteration step dominates under load (candidates: per-report fm-crew-state, secondmate liveness, procevent reconcile, PR polls, custom checks);
(2) fix it so a slow-but-alive cycle cannot look dead: touch the beacon after each expensive step or per direct report, and/or raise the derived grace when poll-independent work exceeds it;
(3) cut the dominant cost if it is redundant (for example, cache per-report state within a cycle).
docs/turnend-guard.md and docs/watcher-continuity.md own the liveness contract; do not change the 300 s floor’s meaning without updating those docs.
What Changed
bin/fm-watch.shadds awatch_step_donehelper. The watcher now renewsstate/.last-watcher-beatat cycle start, after each expensive step, and after each direct report, signal file and heartbeat scan. The expensive steps are fleet-ledger, home-summary, reconcile requests, pending replies, secondmate liveness and stall checks, procevent, inactive outcomes, each custom check, signal scan and triage. A slow cycle that is still alive no longer lets the beacon age past the 300 s grace. A step that hangs still does not renew it. SettingFM_WATCH_TRACEturns on an optional per-step trace (<epoch> <pid> <step>) that can be used to find which step is slow.fm_pending_reply_tickinbin/fm-pending-reply-lib.shmakes each cycle cheaper by reading every pending-reply record in a single pass, instead of running grep, tail and cut once per field. It also only callsfm_pending_reply_close_escalationfor a resolved reply that was escalated and not yet closed, so it no longer takes a lock for every old resolved reply.docs/turnend-guard.mdanddocs/watcher-continuity.mdnow describe when the beacon renews within a cycle and documentFM_WATCH_TRACE; themax(300, FM_POLL + 60)grace is unchanged. The newtests/fm-watch-beacon.test.shchecks that after a slow custom check makes the beacon stale, the live watcher renews it and auto-arm attaches to it. Intests/fm-watch-triage.test.sh,wait_poll_cyclenow countscycle-starttrace lines instead of watching the beacon's mtime, because the beacon now changes during a cycle.Risk Assessment
✅ Low: The change adds beacon renewals only after completed steps, and only in the executing watcher that holds the lock; the pending-reply scan now reads each record once with the same skip conditions as the locked close path; every prior-round finding is fixed in the current code, and existing beacon readers (midturn-healthy mtime comparison, stale/stuck grace checks) still behave correctly.
Testing
I ran real fm-watch.sh and fm-watch-arm.sh processes against throwaway FM_HOMEs, both at HEAD and at base f843953. Load was scaled down with slow trusted custom checks and with direct-report panes in a private real tmux server whose
capture-panecalls were slowed by a shim. At HEAD the beacon age never went above 5 s across 20–30 s cycles, while at base it grew with the cycle length (24–30 s). In a mid-cycle scenario whose beacon was already older than the 300 s floor, base arm printed the incident's "lock held by live pid but heartbeat is stale" message followed by "watcher: FAILED", while HEAD printed "attached". A hung check let the beacon age until its timeout fired, and the 300 s floor is unchanged. A live cycle over 400 historical pending-reply records still closed the one open escalation. Function-level timing put the tick at 2.8 s on HEAD versus 103 s on base. The new tests/fm-watch-beacon.test.sh fails on base and passes on HEAD, and tests/fm-pending-reply.test.sh passed. The modified tests/fm-watch-triage.test.sh was still running when this phase ended: 86 ok, 0 not ok, no exit code yet. I used the private tmux server in place of a Herdr lab, and this change touches no Herdr-specific path.Evidence: Live slow-cycle beacon timeline, HEAD
Source: Live slow-cycle beacon timeline, HEAD
Evidence: Live slow-cycle beacon timeline, base f843953
Source: Live slow-cycle beacon timeline, base f843953
Evidence: Arm attaches mid-cycle (HEAD)
Source: Arm attaches mid-cycle (HEAD)
watcher: attached pid=1782602 (beacon 3s)Evidence: Arm FAILED mid-cycle (base f843953, incident reproduction)
Source: Arm FAILED mid-cycle (base f843953, incident reproduction)
watcher: lock held by live pid 1782596 but heartbeat is stale for 843719625s (>300s); inspect or stop that watcher before re-arming. watcher: FAILED - watcher cycle exited 1 without an actionable reasonEvidence: Per-direct-report renewal with real tmux panes, HEAD
Source: Per-direct-report renewal with real tmux panes, HEAD
Evidence: Per-direct-report timeline, base f843953
Source: Per-direct-report timeline, base f843953
Evidence: Signal-path renewal, HEAD
Source: Signal-path renewal, HEAD
Evidence: Hung check does not keep the beacon fresh
Source: Hung check does not keep the beacon fresh
Evidence: Beacon regression test on base (fails)
Source: Beacon regression test on base (fails)
Evidence: Beacon regression test on HEAD (passes)
Source: Beacon regression test on HEAD (passes)
Evidence: Live pending-replies cycle over 400 records
Source: Live pending-replies cycle over 400 records
Evidence: Pending-reply tick timing HEAD vs base
Source: Pending-reply tick timing HEAD vs base
HEAD 2.81s BASE-f843953 103.03sEvidence: FM_WATCH_TRACE off by default; derived grace floor unchanged
Source: FM_WATCH_TRACE off by default; derived grace floor unchanged
Evidence: Pending-reply suite output
Source: Pending-reply suite output
Evidence: Watcher triage suite output (partial: still running when this phase returned)
Source: Watcher triage suite output (partial: still running when this phase returned)
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 6 issues found → auto-fixed → no changes applied ✅
bin/fm-watch.sh:2647- Custom/PR-poll check loop renews the beacon only once, after every check. The checks block runs each due *.check.sh / PR poll through run_check_capture (bounded by CHECK_TIMEOUT=30s each, plus fm_lock_acquire_wait on the PR control lock) and only calls watch_step_done once at bin/fm-watch.sh:2761. With >=10 due checks under load, one cycle can spend >300s inside this block without renewing .last-watcher-beat, reproducing the exact auto-arm 'lock held by live pid, stale beat' refusal the change targets. The intent names 'PR polls, custom checks' as candidates and asks to 'touch the beacon after each expensive step'; the commit notes check execution was suppressed during measurement, so this cost was never measured. Fix: call watch_step_done "check:$(basename "$c")" at the top of each for-loop iteration (after the previous check completed, covering every continue path).tests/fm-watch-triage.test.sh:78- wait_poll_cycle no longer guarantees a full cycle that started after the call. The new helper targets previous_consumed+1 cycle-starts, which only proves the cycle already running at the previous return (possibly started before the caller's mutation) completed. The old helper deleted the beacon and waited for two fresh touches, guaranteeing a cycle that began after the call. Repeated calls on one watcher (hold_watch_churn at tests/fm-watch-triage.test.sh:3754, which needs 3 post-mutation cycles; also :2562, :2752) now observe one fewer post-mutation cycle, so 'stays in loop / no wake' assertions can pass vacuously. Fix: snapshot the cycle-start count on entry and wait for entry+2; this also removes the .watch-test-consumed state file.tests/fm-watch-triage.test.sh:88- Tests introduce an undeclared ripgrep dependency with a silent failure mode. No test executed rg before this change; the suite uses grep. If rg is missing,rg -c ... 2>/dev/null || trueyields starts=0, so every wait_poll_cycle (~50 call sites) times out after 30s and returns 1. Same dependency at tests/fm-watch-beacon.test.sh:55 and :60 (rg -q). Replace with grep -c / grep -q.tests/fm-watch-beacon.test.sh:28- Beacon regression test also passes on the pre-fix code. The watcher runs with FM_POLL=1; on the base commit, after the slow check backdates the beacon, the remainder of the cycle plus a 1s sleep reaches the next cycle-start touch within ~1-2s, well inside the 60x0.1s health-poll window at lines 40-49, so fm_watcher_healthy succeeds without any in-cycle renewal. The test does not reproduce the reported failure. Use a much larger watcher FM_POLL so only in-cycle renewal can make the beacon healthy within the window.bin/fm-pending-reply-lib.sh:1463- Resolved records whose escalation was already closed still take a lock every cycle: only escalated_epoch is checked, so every historically escalated-and-closed resolved record still re-sources fm-wake-lib.sh and acquires the per-corr lock each cycle before _fm_pending_reply_close_escalation_locked returns 0 on escalation_closed_epoch. Also parse escalation_closed_epoch=* in the same read loop and skip when set.bin/fm-watch.sh:2310- New FM_WATCH_TRACE opt-in trace mode exceeds the intent's one-off instrumentation. The intent asked to instrument one cycle to find the dominant step (reported in the commit message) and to renew the beacon; it does not require a permanent, undocumented env-var trace mode in the watcher. The mode is now also load-bearing for tests/fm-watch-triage.test.sh wait_poll_cycle, coupling test synchronization to a diagnostic hook. Recommend removing it (or, if kept, documenting it in the docs that own the liveness contract).🔧 No changes applied.
5 issues (4 warnings, 1 info) still open:
bin/fm-watch.sh:2647- Custom/PR-poll check loop renews the beacon only once, after every check. The checks block runs each due *.check.sh / PR poll through run_check_capture (bounded by CHECK_TIMEOUT=30s each, plus fm_lock_acquire_wait on the PR control lock) and only calls watch_step_done once at bin/fm-watch.sh:2761. With >=10 due checks under load, one cycle can spend >300s inside this block without renewing .last-watcher-beat, reproducing the exact auto-arm 'lock held by live pid, stale beat' refusal the change targets. The intent names 'PR polls, custom checks' as candidates and asks to 'touch the beacon after each expensive step'; the commit notes check execution was suppressed during measurement, so this cost was never measured. Fix: call watch_step_done "check:$(basename "$c")" at the top of each for-loop iteration (after the previous check completed, covering every continue path).tests/fm-watch-triage.test.sh:78- wait_poll_cycle no longer guarantees a full cycle that started after the call. The new helper targets previous_consumed+1 cycle-starts, which only proves the cycle already running at the previous return (possibly started before the caller's mutation) completed. The old helper deleted the beacon and waited for two fresh touches, guaranteeing a cycle that began after the call. Repeated calls on one watcher (hold_watch_churn at tests/fm-watch-triage.test.sh:3754, which needs 3 post-mutation cycles; also :2562, :2752) now observe one fewer post-mutation cycle, so 'stays in loop / no wake' assertions can pass vacuously. Fix: snapshot the cycle-start count on entry and wait for entry+2; this also removes the .watch-test-consumed state file.tests/fm-watch-triage.test.sh:88- Tests introduce an undeclared ripgrep dependency with a silent failure mode. No test executed rg before this change; the suite uses grep. If rg is missing,rg -c ... 2>/dev/null || trueyields starts=0, so every wait_poll_cycle (~50 call sites) times out after 30s and returns 1. Same dependency at tests/fm-watch-beacon.test.sh:55 and :60 (rg -q). Replace with grep -c / grep -q.tests/fm-watch-beacon.test.sh:28- Beacon regression test also passes on the pre-fix code. The watcher runs with FM_POLL=1; on the base commit, after the slow check backdates the beacon, the remainder of the cycle plus a 1s sleep reaches the next cycle-start touch within ~1-2s, well inside the 60x0.1s health-poll window at lines 40-49, so fm_watcher_healthy succeeds without any in-cycle renewal. The test does not reproduce the reported failure. Use a much larger watcher FM_POLL so only in-cycle renewal can make the beacon healthy within the window.bin/fm-pending-reply-lib.sh:1463- Resolved records whose escalation was already closed still take a lock every cycle: only escalated_epoch is checked, so every historically escalated-and-closed resolved record still re-sources fm-wake-lib.sh and acquires the per-corr lock each cycle before _fm_pending_reply_close_escalation_locked returns 0 on escalation_closed_epoch. Also parse escalation_closed_epoch=* in the same read loop and skip when set.🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
bash tests/fm-watch-beacon.test.shat HEAD and against base f843953's bin/drive-slow-cycle.shon HEAD and base (real fm-watch.sh, trusted slow custom checks)drive-slow-cycle.sh <root> HEAD-hung-check 1 1000 16 10(adversarial hung check)drive-arm-midcycle.shon HEAD and base (real fm-watch-arm.sh against a live mid-cycle watcher)drive-per-report.shon HEAD and base (private real tmux server, capture-pane latency shim), plus the signal-path variantdrive-pending-replies.sh <root> HEAD 400Function-levelfm_pending_reply_ticktiming, HEAD vs baseWatcher run with FM_WATCH_TRACE unset, plus afm_poll_derived_gracesweepbash tests/fm-pending-reply.test.shbash tests/fm-watch-triage.test.sh(still running when this phase returned: 86 ok, 0 not ok)✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.
Summary by Sourcery
Keep slow watcher cycles visibly alive while reducing pending-reply polling overhead and preserving the existing liveness grace contract.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: