Skip to content

fix(bin): judge watcher beacon staleness on awake time across host sleep - #5768

Open
bartwesthoff-fyrm wants to merge 3 commits into
kunchenguid:mainfrom
bartwesthoff-fyrm:fm/fm-watcher-beat-stall
Open

bartwesthoff-fyrm wants to merge 3 commits into
kunchenguid:mainfrom
bartwesthoff-fyrm:fm/fm-watcher-beat-stall

Conversation

@bartwesthoff-fyrm

Copy link
Copy Markdown

Problem

In a Claude-primary home on the herdr backend, the fleet watcher (bin/fm-watch.sh) repeatedly stopped touching state/.last-watcher-beat for 7-10 minutes while its process stayed alive.
The Stop-hook auto-arm then reported lock held by live pid ... heartbeat is stale for 600s (later 465s) and failed after two attempts, and once the watcher exited entirely.

Cause

The stalls are macOS system sleep, not a blocking poll step.
Every triage-log gap and failed auto-arm lines up with a pmset -g log sleep and wake entry (Idle, Clamshell and Maintenance Sleep, with DarkWakes of about 45s).
For example, the first gap (16:19:30-16:29:39) matches Idle Sleep at 16:19:45 and wake at 16:29:34.
No process runs while the host sleeps, so a healthy watcher's beacon ages by the whole sleep.
Judged on wall-clock age, the first arm after a wake (often a DarkWake) refused the healthy watcher as stale, or TERM-evicted it once the age passed the hard bound.
The herdr event wait (bin/backends/herdr-eventwait.py) is not the cause: it is bounded by time.monotonic, which does not advance during sleep on macOS, so it only ever blocks for POLL seconds of awake time.

Fix

  • fm_beacon_age in bin/fm-wake-lib.sh reports the beacon's awake age: wall-clock age minus the part of the host's most recent sleep (kern.sleeptime/kern.waketime on macOS) that falls after the last beat.
    Hosts that expose no sleep record keep wall-clock ages, and the probe errs toward stale when several sleeps fall between two beats.
  • Every reader that turns the beacon into a liveness or abandonment verdict now uses it:
    • the watcher's stale-holder refusal and eviction
    • fm_watcher_healthy, used by the arm and the Stop-hook auto-arm
    • fm_watcher_supervision_verdict, and the beacon age the supervision banner shows
    • the turn-end guard's away-mode daemon-ownership branch
    • the auto-arm claim's stuck proof and the legacy-claim abandonment test
  • Report-only ages, such as the away-mode return brief's watcher-beat gap, stay on wall-clock time, because a gap while the host slept truly means supervision did not run.
  • The stale grace (max(300, FM_POLL+60)) and the FM_WATCHER_STALL_BOUND eviction contract are unchanged: a watcher that stalls while the host is awake still crosses both.
  • tests/lib.sh pins FM_HOST_SLEEP_WINDOW='' so fixtures that backdate the beacon are not skewed by a real sleep during the suite.
  • Also fixes a bin/fm-watch.sh comment that said the beacon is touched again before the terminal wait; it is touched once per cycle.

The watcher that exited after 18:27:31

This is the arm/successor chain, not the beacon, and this PR does not change it.
The watcher exited normally at 18:36:35 with an actionable wake (a declared-paused lane recheck).
Its successor re-arm waits on the primary's handling turn, which host sleep delayed until 19:17:14.
A genuinely wedged watcher is still recovered by the existing hard-bound eviction, now measured on awake time.

Testing

Regression tests (each fails on the base code and passes with the fix):

  • tests/fm-watcher-lock.test.sh test_live_holder_is_not_stale_across_host_sleep: a live holder whose beacon gap was host sleep is attached to and reads as healthy. The same beacon with no sleep, or with a sleep that ended long before, is still refused.
  • tests/fm-claude-stop-autoarm.test.sh: a live generation claim and a live legacy owner paused by host sleep keep deferring. An awake-stale claim is still superseded.

Live runs in a disposable lab home, with a real bin/fm-watch.sh holder and a second arm against a backdated beacon:

Scenario Result
Beacon gap was host sleep (2400s) Fixed arm: watcher: already running pid 47552. Base arm: lock held by live pid 47552 but heartbeat is stale for 2400s (>660s), the reported failure.
Partial sleep, 800s awake, over the 660s grace Still refused: heartbeat is stale for 800s (>660s)
Awake stall past the hard bound Still evicted: replaced stalled pid 78807 (beacon 2400s past hard bound 1980s)
bin/fm-guard.sh banner Silent across a sleep; WATCHER DOWN when no sleep explains the gap
Real macOS probe fm_host_sleep_window parses this host's kern.sleeptime/kern.waketime correctly

tests/fm-watcher-lock.test.sh, tests/fm-claude-stop-autoarm.test.sh and tests/fm-turnend-guard.test.sh pass.
tests/fm-procevent.test.sh failed locally on three runs, each at a different timing-sensitive case with host load between 25 and 63.
This PR only changes how that file sources the libraries, and it will be tracked separately as a load-sensitive flake.
The Stop-hook auto-arm was covered by its test file, not driven live through a real Claude primary.

On macOS no process runs while the host sleeps, so a healthy watcher's
beacon aged by the whole sleep and the first arm after a wake refused it
as stale, or evicted it past the hard bound. The healthy-watcher check,
the supervision verdict, the watcher's stale-holder refusal and eviction,
and the away-mode guard now use fm_beacon_age, which subtracts the most
recent host sleep after the last beat. A stall while the host is awake
still crosses the grace and the hard bound unchanged.
@FocalFactotum

Copy link
Copy Markdown
Contributor

Independent confirmation from a second home: same symptom, same cause.

Setup: MacBook Pro (MacBookPro18,3), macOS 26.5.2, Claude-primary home on the herdr backend. The Stop-hook auto-arm repeatedly reported lock held by live pid ... but heartbeat is stale for 400-2074s (>300s) against a watcher that was alive and healthy.

An investigation on 2026-09-28 matched each failure to pmset -g log:

  • Sleep 07:19:03, DarkWake 07:31:54 (771 s). The arm ledger at 07:32:05 shows reason=attached-cycle-ended beacon_age=813 for the same watcher pid.
  • Sleep 04:46:18 to 04:55:43 (about 565 s), ledger at 04:55:54 shows beacon_age=596.
  • Asleep 01:59:00 to 02:14:40 (about 940 s), watcher evicted at 02:14:51.
  • Asleep 07:00:02 to 07:17:23 (about 1041 s), eviction at 07:17:34.

While awake, the beacon advanced every 95 to 113 seconds, and no bounded cycle component came close to the 300 s grace. We reached the same conclusion as this PR: liveness should be judged on awake time across host sleep, keeping wall-clock ages for report-only gaps.

@bartwesthoff-fyrm, this now conflicts with main. Are you planning to bring it up to date? We'd be glad to test a rebased branch on this MacBook and report back.

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