Repository navigation
fix(bin): bound herdr control RPCs and beat the watcher beacon mid-cycle - #23
Merged
Merged
Conversation
A shared herdr server wedged under host load could block fm_backend_herdr_cli indefinitely, and the watcher only touched its liveness beacon once per cycle, so a slow check sweep or stale-pane scan could stall it past the guard grace (311-500s stalls observed 2026-09-27/28). Adapts upstream kunchenguid#2789 and kunchenguid#5421: every herdr control RPC now runs under a configurable FM_BACKEND_HERDR_CLI_TIMEOUT bound (a timeout classifies as unreadable/unknown, never dead), and the watcher's main loop re-touches the beacon after each check in the sweep and after each stale-pane window capture. Not adopted: kunchenguid#5421's separate recovery-wake-generation change.
…late on consecutive wedge round 1" in tests/fm-watch-triage.test.sh. It failed on every local run and passed once `bin/fm-watch.sh` was restored to the base commit. Taking out only the new per-window `watcher_beat` after the pane capture also made it pass. Invariant broken: the test helper `wait_poll_cycle` treated any change in the beacon's (`state/.last-watcher-beat`) mtime as "a whole poll cycle finished". The PR now touches the beacon mid-cycle as well. When the capture beat landed in a later second than the top-of-cycle beat, the helper returned early. The priming round was then killed before it wrote `.stale-$key`, so every later round saw the hash as new and reset the wedge timer. That timer was about 5s old instead of the backdated 500s, so no escalation fired. The other watcher tests that read the beacon only check that it keeps advancing, which still holds. `wait_poll_cycle` was the only place that relied on the old meaning. Fix, taken from upstream PR kunchenguid#5421 as the intent asks: - `bin/fm-watch.sh`: new `cycle_turnover()`. It touches `state/.last-cycle-turnover` exactly once per cycle, right before `event_wait_or_sleep`, and at no mid-cycle progress point. - `tests/fm-watch-triage.test.sh`: `wait_poll_cycle` now waits on that marker instead of the beacon (and its comment explains why). - `docs/watcher-continuity.md`: one sentence documenting the marker. The mid-cycle beats the intent requires are unchanged. Verification: - The failing test now passes (`FM_TEST_ONLY=test_wedge_escalation_marks_demand_deep_inspection_after_threshold`). - `tests/fm-watch-beacon.test.sh` passes in full. - The full triage suite is still running (69 ok and no failures when this was written, including past the previously failing test). I have not seen it finish. - `test_term_stops_a_watcher_blocked_inside_a_poll` fails locally on macOS, but it fails the same way with the HEAD and base-commit versions of `fm-watch.sh`, so this PR didn't cause it. CI never reached it because the suite stopped at the wedge test first, so I don't know whether it passes on Linux. I skipped it in a temporary copy to run the rest of the suite and changed nothing for it
…xed. The other is a flake the PR didn't cause. Nothing was committed or pushed, and the deletion is staged. 1. Behavior portable parallel 1, fixed. The failing test was tests/fm-lint.test.sh: "fm-lint.sh --list-files did not return the complete shell inventory". Invariant broken: the set of `*.sh` files under bin/, bin/backends/ and tests/ must match the inventory from `bin/fm-lint.sh --list-files`. The previous CI round accidentally committed `tests/.tmp-triage-run.sh`, a 6,545-line temporary copy of the triage suite made so it could skip one test, and that broke the match. The fix is to delete the file (`git rm tests/.tmp-triage-run.sh`). Nothing else used it, and no other files changed. Verification: `bash tests/fm-lint.test.sh` now prints "ok - fm-lint.sh --list-files reports the complete shell inventory". 2. Behavior portable serial 4, not caused by this PR, and no code change made. The failing test was tests/fm-watcher-lock.test.sh: "acquirer could not reclaim dead steal owner (rc=8)". It covers the lock code in bin/fm-wake-lib.sh, which this PR does not change (the PR touches bin/backends/herdr.sh, bin/fm-watch.sh, docs, and the herdr/beacon/triage tests). The same test passed on this branch's previous CI run (36428775085, job 108949257213) and on main (36365050608). Locally, a full run of tests/fm-watcher-lock.test.sh with the fix applied printed "ok - dead steal owner is reclaimed once without a nested steal marker" and no failures. I read it as a flake on the CI runner, outside this PR's scope
Hozzy02
added a commit
that referenced
this pull request
Sep 28, 2026
…ine change in `bin/fm-session-start.sh`. **What failed:** In the "Behavior portable serial 6" job (CI job 109115683167, head 2ad86f2), `tests/fm-session-start.test.sh` failed at "a killed endpoint read was not reported as that task's own error line". The digest printed `endpoint: alive` for `sess:p-doom` instead of `endpoint: error`. The suite stops at its first failure. The next test, the hanging-read test, was never reached in CI, and it also fails locally ("left 1 hung herdr process(es) behind"). **Root cause:** The merge combined two changes that both put a time bound on the same call: - Upstream kunchenguid#5917 runs each session-start endpoint read in its own child, with a bound from `FM_SESSION_START_ENDPOINT_TIMEOUT`. - Fork #23 (921e3f7) also wraps every herdr RPC in `fm_backend_herdr_bounded`, a second `fm_run_timed`. The inner bound adds processes between the herdr CLI and the read shell, and gives the CLI its own process group. That causes two problems: 1. The test fake kills its grandparent, expecting that to be the read shell. With the extra processes it hits a different one, so the read survives and reports `alive`. 2. When the outer bound fires, its process-group kill cannot reach the inner group, so a hung herdr CLI is left running after the read is reported. **Invariant violated:** a session-start endpoint read has exactly one bound, and its process-group kill reaches every process the read started. **Fix:** `fm_session_start_endpoint_read` now runs its child with `env FM_BACKEND_HERDR_CLI_TIMEOUT=0`. That turns off the redundant inner herdr bound for this read only, and the outer bound stays the only one. `env` execs `bash` without adding a process, so the process tree is the same as upstream's. No test was changed, and herdr RPC bounds everywhere else, including the watcher paths from #23, are unchanged. **Verification (macOS):** - `shellcheck -x bin/fm-session-start.sh` is clean. - The endpoint tests pass: tmux and herdr liveness, the hanging-read test, the padded-zero bound, and the perl signal-death test. The hanging-read test failed before the fix, and no stray `herdr` is left after it. - The killed-read test skips on macOS because it needs `/proc`. To check it anyway, I logged process IDs: with the fix, the fake herdr's grandparent is the endpoint read shell (7584 = 7584). Before the fix it was a different process (7692 vs 7701). So the unchanged test fake now kills the right process on Linux, and that death produces the expected `endpoint: error` line. - A full local suite run hit a timeout in the unrelated secondmate slow-network test (a deferred network worker under local load). That test passed in CI and in an earlier local run. **Not fixed:** Other code in `bin/` that runs a bounded command around herdr calls (e.g. `fm-crew-state.sh`, `fm-fleet-snapshot.sh`) has the same nested-bound shape. No failing check covers them, so I left them alone
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
Bring the two upstream watcher fixes into this fork so the background monitor stops going beacon-stale during long cycles under host load (stalls of 311-500s seen four times 2026-09-27/28). Root cause and evidence: ~/Repos/firstmate/data/firstmate-watcher-slow-cycle/report.md. Upstream kunchenguid/firstmate PR kunchenguid#2789 bounds every herdr CLI RPC (fm_backend_herdr_cli has no wall-clock timeout; used by secondmate_liveness_tick and the stale-pane capture) and adds a mid-cycle watcher beat; PR kunchenguid#5421 re-touches state/.last-watcher-beat at proven-progress points inside a cycle (after each check in the sweep, after each window capture). Both are open upstream and this fork has diverged, so adapt rather than cherry-pick blindly.
What Changed
bin/backends/herdr.shnow puts a time limit on every herdr control-socket RPC through a newfm_backend_herdr_boundedwrapper built onfm_run_timed. The limit is set byFM_BACKEND_HERDR_CLI_TIMEOUT(default 20s; 0 or a non-numeric value turns it off). It coversfm_backend_herdr_cli, including the retry after a protocol mismatch, plus client status, the version check, session list, events-capability probes and socket-path lookup. The long-livedserverlaunch still runs without a limit. If a status read infm_backend_herdr_server_ensurehits the limit, the call fails right away as an unresponsive server instead of relaunching and polling again.bin/fm-watch.shadds awatcher_beathelper that touchesstate/.last-watcher-beat. The watcher now calls it at the top of each cycle, before each check in the slow-check sweep, and after each stale-pane window capture, even a failed one. A long but healthy cycle no longer lets the beacon go stale.configuration.md,herdr-backend.mdandwatcher-continuity.md. New tests are intests/fm-backend-herdr.test.shand a newtests/fm-watch-beacon.test.sh.🤖 Generated with Claude Code
Risk Assessment
✅ Low: All three round-1 fixes are in place and hold up. server_ensure now stops as soon as a status call hits its time limit, and every caller (endpoint_absence_recheck, container_ensure, the projection recovery path, the spawn and doctor paths) treats that failure as a refusal or 'unreadable'. client_status is now wrapped in the RPC bound. The watcher now beats after each capture whether it succeeded or failed. The tightened beacon test uses FM_POLL=60 over a ~15s window, so it only passes if the beat inside the cycle fires on failed captures.
Testing
I ran the new watcher-beacon test file and the 4 new herdr bound unit tests; all passed. I then provisioned an isolated fm-lab-* herdr session and froze only its dedicated server process with SIGSTOP. That reproduces the incident's wedged server, which accepts connections but never answers. Against that frozen server, bounded RPCs returned 124 in about 3.3s and server_ensure failed fast without starting a second server. With the bound disabled, the same call hung until an outer alarm killed it. Last, I ran the real fm-watch.sh at HEAD and at the base commit against the frozen server with 4 distinct lab panes. At HEAD the beacon updated about every 5.5s (5 updates, cycle done in ~25s, no hung children). At base it updated once and stayed stuck behind a hung herdr call. The lab was torn down and nothing was left behind. The 'unreadable, not dead' agent-state behavior was checked only by a unit test with a fake herdr, so it is marked untested for live validation. This change has no UI, so the evidence is CLI transcripts rather than screenshots.
Evidence: Live: bounded herdr RPCs against a SIGSTOP-frozen lab server
Source: Live: bounded herdr RPCs against a SIGSTOP-frozen lab server
healthy: cli status rc=0 .05s; server_ensure rc=0 .03s WEDGED (T), timeout=3: cli status rc=124 3.35s; pane list rc=124 3.33s; server_ensure rc=1 3.37s ('did not answer status within 3s'), still one server pid TIMEOUT=0: hung until outer alarm, rc=142 12.08s resumed: cli status rc=0 .05sEvidence: Live: real fm-watch.sh beacon, fix vs base, frozen lab herdr, 4 windows
Source: Live: real fm-watch.sh beacon, fix vs base, frozen lab herdr, 4 windows
HEAD 302d5b59: beacon at t+1.0, 8.3, 13.8, 19.2, 24.7s -> 5 touches, 0 hung herdr children BASE d6c999c2: beacon at t+0.7s only -> 1 touch, age 86s, 1 hung herdr childEvidence: tests/fm-watch-beacon.test.sh transcript
Source: tests/fm-watch-beacon.test.sh transcript
Evidence: New herdr RPC-bound tests transcript
Source: New herdr RPC-bound tests transcript
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 3 issues found → auto-fixed ✅
bin/backends/herdr.sh:1698- The stale-pane capture can still stall for about 430s, which is the incident this change is meant to fix. The path is fm_backend_capture → fm_backend_herdr_capture → fm_backend_herdr_target_ready → fm_backend_herdr_server_ensure. In the incident a shared herdr server is wedged: it accepts connections but does not answer. Thenstatus --jsonhits the new 20s bound and returns 124, andrunningcomes back empty. server_ensure reads that as 'not running'. It launchesherdr server(line 1703) and polls status 20 times (line 1706), and each poll can take the full 20s bound plus 0.5s. That is up to ~20 + 20×20.5 ≈ 430s inside one capture, with no beat, which is past the 300s grace. The RPC bound makes each call finite, but this retry loop multiplies the bound. The new watcher test sidesteps this on purpose: its fake answersstatusimmediately and its comment calls this 'a separate, unbounded-as-a-whole concern this task does not touch'. Fix at the shared boundary: in server_ensure, capture the status call's exit code. If fm_timed_out says the bound fired, return 1 right away (unresponsive, so don't relaunch or retry). Alternatively, cap the whole loop with a wall-clock deadline. Sibling: fm_backend_herdr_endpoint_absence_recheck (line ~2476) goes through the same server_ensure loop.bin/fm-watch.sh:2995- The per-window beat only runs when the capture succeeds.tail40=$(fm_backend_capture ...) || continueskipswatcher_beatwhenever the capture fails. With a wedged herdr server every capture fails: it hits the bound, returns 124, and fm_backend_herdr_capture returns 1. So in exactly the load case this fix targets, K windows × (bounded RPCs per capture) runs with no beat until the next cycle. For example, 8 windows × 2 bounded RPCs × 20s = 320s, which is over the 300s grace. A bounded, failed capture is still progress, so beat regardless of the result: run the capture, save its exit code, call watcher_beat, thencontinueif it failed. Related test gap: tests/fm-watch-beacon.test.sh test_beacon_advances_promptly_when_herdr_capture_hangs claims 'at least one advance per hanging herdr window'. But every capture in that fixture fails, so the per-window beat never runs, and the ≥3 distinct mtimes come from top-of-cycle beats across the fast 1s-poll cycles. The test would pass with the per-window beat removed. Tighten it so it only passes when beats happen inside a single cycle, e.g. more windows and a bound long enough that one cycle's sweep outlasts FM_POLL, then assert the beat interval.bin/backends/herdr.sh:511- The intent says to bound 'every herdr CLI RPC', but fm_backend_herdr_client_status still runs"$bin" status --json --sessionwith no bound. It is reached from fm_backend_herdr_cli's protocol_mismatch retry path (line 447 → client_select → lines 536/541, once per herdr client on PATH) and from bin/fm-remote-doctor.sh:569. That path is documented for hosts with more than one herdr client. On such a host with a wedged server, one watcher RPC that gets a protocol_mismatch refusal would then block with no bound. Fix: wrap the call in fm_backend_herdr_bounded, like the other rawherdrcalls this change wrapped (lines 563, 3766, 3830, 3854, 3857).🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
bash tests/fm-watch-beacon.test.sh(real fm-watch.sh: 4 herdr windows whose captures hang, plus a sweep of 3 slow registered checks)The 4 new tests in tests/fm-backend-herdr.test.sh, run alone: test_cli_bounded_by_configurable_timeout, test_cli_timeout_zero_disables_the_bound, test_agent_state_unreadable_not_dead_on_wedged_rpc, test_server_ensure_gives_up_when_status_rpc_is_wedgedLive:bin/fm-herdr-lab.sh provision fm-lab-wedge-71128-29218, then SIGSTOP only that lab's dedicatedherdr server --session fm-lab-wedge-…process (default server pid 1853 never touched)Live: fm_backend_herdr_cli status/pane list and fm_backend_herdr_server_ensure against the healthy, then frozen, then resumed lab server with FM_BACKEND_HERDR_CLI_TIMEOUT=3; adversarial FM_BACKEND_HERDR_CLI_TIMEOUT=0 inside an outer 12s alarmLive: real bin/fm-watch.sh at HEAD vs base d6c999c2 (git archive to a temp dir), each on a freshbin/fm-lab-home.shlab home with 4 herdr task windows on distinct lab panes w1..w4:p1, FM_POLL=120, FM_BACKEND_HERDR_CLI_TIMEOUT=5, frozen lab server; beacon mtime sampled every 0.1s for ~45sbin/fm-herdr-lab.sh teardown, lab homes removed, no stray processes left✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.