Skip to content

fix(bin): evict a watcher whose beacon stalls past a hard bound instead of refusing every re-arm - #5594

Merged
kunchenguid merged 3 commits into
kunchenguid:mainfrom
karotkriss:fm/fm-watch-cycle-exceeds-beacon
Sep 25, 2026
Merged

kunchenguid merged 3 commits into
kunchenguid:mainfrom
karotkriss:fm/fm-watch-cycle-exceeds-beacon

Conversation

@karotkriss

Copy link
Copy Markdown
Contributor

Intent

Fixes #4400

A fleet watcher that is alive but whose liveness beacon has gone stale can never be replaced: every re-arm is refused because the lock holder is a live pid, and the holder is never evicted because it is not dead. Observed on 2026-09-24: one poll cycle ran for 1 h 48 min without touching the beacon and no replacement started, so nothing was polled for that time, and 74 re-arm attempts the same day each died within a second against a live-but-stale holder while the home stayed unsupervised. When the holder's beacon is stale past a hard bound well beyond the ordinary grace, the re-arm should replace that holder rather than refuse.

What Changed

  • bin/fm-watch.sh: added evict_stalled_holder and a retry loop around fm_lock_try_acquire so a re-arm now evicts a live lock holder (identity-verified TERM, once per arm) whose beacon is stale past the new hard bound FM_WATCHER_STALL_BOUND (default 3x FM_WATCHER_STALE_GRACE) and starts in its place, printing watcher: replaced stalled pid <N> ...; a beacon stale only past the ordinary grace still gets the existing refusal, and a holder that survives TERM keeps the refusal and nonzero exit.
  • bin/fm-watch-arm.sh: when the started child reports it replaced a stalled holder, ledger a stalled-holder-replaced cycle row and reopen the cycle so its normal close row follows.
  • docs/configuration.md and docs/turnend-guard.md: documented FM_WATCHER_STALL_BOUND and the eviction ceiling on the stale-beacon refusal.
  • tests/fm-watcher-lock.test.sh: added test_live_stalled_watch_lock_is_replaced_past_hard_bound, asserting a beacon under the bound stays refused while one past it evicts and replaces the holder.

Risk Assessment

✅ Low: A small, well-bounded fix that closes the live-but-stale watcher deadlock exactly as the intent specifies; the new eviction is identity-verified, TERM-only with a bounded wait, single-shot per arm, and falls back to the unchanged refusal, with behavioral test coverage.

Testing

Drove the real fm-watch.sh binary through its regression test end-to-end. The past-hard-bound eviction/replacement and the under-bound refusal both pass live, the reported deadlock reproduces on the pre-fix binary and is gone after the fix, and the flaky replacement-message grep is now poll-synchronized (10/10 clean). No screenshot applies - this is a process-lifecycle shell change with no rendered UI surface; evidence is the CLI transcript of the watcher acquiring/refusing the singleton lock. Worktree left clean.

  • Live validation: ✅ go - 4 of 5 scenarios driven live against the product
Scenario Result Live Evidence
Live watcher with beacon stale past the hard bound is evicted and the re-arm replaces it ✅ pass live stall-replace-test-10x.log - replacement watcher takes the lock pid, stalled holder no longer alive, prints 'watcher: replaced stalled pid <N> (beacon <A>s past hard bound 3s)'
Live watcher stale past grace but under the hard bound is still refused, holder untouched (adversarial: guard must not over-evict) ✅ pass live First invocation in the test (FM_WATCHER_STALL_BOUND=9999999999) exits non-zero with 'heartbeat is stale' and the holder stays live/non-zombie
Regression: pre-fix binary refuses every re-arm and never replaces the stalled holder ✅ pass live Ran the new test against git show b42d4fa:bin/fm-watch.sh; it fails with the ticket's exact refusal message, proving fail-before / pass-after
Replacement-message assertion no longer races the lock-acquire/echo gap (the flake fix) ✅ pass live 10 consecutive runs all report the replacement message; poll-for-message replaces the single racy grep
A holder that survives TERM keeps the refusal and nonzero exit (survives-TERM fallback) ⏸️ untested no Driving this needs a fixture that installs a TERM trap on a process still matching the lock's recorded identity (starttime+cmdline proof); no such case exists in the suite and building one was out of…
Evidence: stall-replace test 10x (no flake)

Source: stall-replace test 10x (no flake)

=== run 1 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
=== run 2 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
=== run 3 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
=== run 4 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
=== run 5 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
=== run 6 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
=== run 7 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
=== run 8 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
=== run 9 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
=== run 10 ===
ok - live watcher lock with a beacon past the hard bound is replaced, under it is still refused
10/10 PASSED (no flake)
Evidence: regression: new test fails on pre-fix fm-watch.sh
not ok - replacement watcher did not stay alive: watcher: lock held by live pid 1452467 but heartbeat is stale for 843594766s (>1s); inspect or stop that watcher before re-arming.
driver_exit=1
- Outcome: 🔧 1 issue found → auto-fixed ✅ across 2 runs (11m2s)

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-watcher-lock.test.sh:215 - test_live_stalled_watch_lock_is_replaced_past_hard_bound in tests/fm-watcher-lock.test.sh raced the watcher's post-acquire echo: it synchronized on the lock-pid file but asserted the 'watcher: replaced stalled pid ...' stdout line, which is printed only after the lock is acquired, so a single grep read an empty file ~1 in 3 runs. Fixed by polling for the message before asserting; 8/8 runs clean after. Product behavior was correct throughout.
  • Live validation: ✅ go - 4 of 4 scenarios driven live against the product
Scenario Result Live Evidence
Beacon stale past the hard bound: live holder is evicted and the re-arm starts in its place, reporting the replacement ✅ pass live Manual drive of bin/fm-watch.sh with FM_WATCHER_STALL_BOUND=3; holder DEAD, replacement took lock, stdout 'watcher: replaced stalled pid 872329 (beacon 843594349s past hard bound 3s)'; also test_live_…
Beacon stale past grace but under the hard bound: re-arm is still refused and the holder is left untouched ✅ pass live test_live_stalled_watch_lock_is_replaced_past_hard_bound first half (FM_WATCHER_STALL_BOUND=9999999999): exit nonzero, 'heartbeat is stale' on stderr, holder stays live
Adversarial - holder survives TERM: refusal and nonzero exit are kept, holder stays alive ✅ pass live Manual drive with holder trapping TERM: exit=1, stderr refusal 'lock held by live pid ... heartbeat is stale', holder ALIVE
Adversarial - recorded identity mismatch (recycled pid): holder is never signalled, re-arm refuses ✅ pass live Manual drive with lock pid-identity set to a non-matching value: exit=1, holder ALIVE (never signalled), refusal on stderr
  • Isolated run of test_live_stale_watch_lock_is_actionable and test_live_stalled_watch_lock_is_replaced_past_hard_bound from tests/fm-watcher-lock.test.sh, 8 consecutive runs after the fix (all pass)
  • Manual live drive of bin/fm-watch.sh with a real fake lock holder: beacon past hard bound -> holder evicted, replacement acquires lock, prints 'watcher: replaced stalled pid ... past hard bound 3s'
  • Manual live drive: holder with trap &#39;&#39; TERM past the hard bound -> exit 1, refusal on stderr, holder still alive
  • Manual live drive: lock pid-identity set to a non-matching value -> exit 1, refusal, holder never signalled

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

  • Live validation: ✅ go - 4 of 5 scenarios driven live against the product
Scenario Result Live Evidence
Live watcher with beacon stale past the hard bound is evicted and the re-arm replaces it ✅ pass live stall-replace-test-10x.log - replacement watcher takes the lock pid, stalled holder no longer alive, prints 'watcher: replaced stalled pid <N> (beacon <A>s past hard bound 3s)'
Live watcher stale past grace but under the hard bound is still refused, holder untouched (adversarial: guard must not over-evict) ✅ pass live First invocation in the test (FM_WATCHER_STALL_BOUND=9999999999) exits non-zero with 'heartbeat is stale' and the holder stays live/non-zombie
Regression: pre-fix binary refuses every re-arm and never replaces the stalled holder ✅ pass live Ran the new test against git show b42d4fa:bin/fm-watch.sh; it fails with the ticket's exact refusal message, proving fail-before / pass-after
Replacement-message assertion no longer races the lock-acquire/echo gap (the flake fix) ✅ pass live 10 consecutive runs all report the replacement message; poll-for-message replaces the single racy grep
A holder that survives TERM keeps the refusal and nonzero exit (survives-TERM fallback) ⏸️ untested no Driving this needs a fixture that installs a TERM trap on a process still matching the lock's recorded identity (starttime+cmdline proof); no such case exists in the suite and building one was out of…
  • Ran test_live_stalled_watch_lock_is_replaced_past_hard_bound 10x in a loop against the fixed bin/fm-watch.sh - 10/10 pass, no flake
  • Ran the same test against git show b42d4fa:bin/fm-watch.sh (pre-fix) - fails with the exact deadlock refusal from the ticket, confirming the regression reproduces before the fix
  • The test's first invocation (FM_WATCHER_STALL_BOUND=9999999999) drives the under-bound boundary: holder stale past grace is still refused and left un-signalled
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…tead of refusing every re-arm

A fleet watcher that is alive but whose liveness beacon has gone stale could
never be replaced: every re-arm was refused because the lock holder was a live
pid, and the holder was never evicted because it was not dead. Add
FM_WATCHER_STALL_BOUND (default 3x the stale grace): below it the refusal is
unchanged; at or past it the arm re-verifies the holder against the lock's
recorded identity, sends TERM, waits boundedly, and takes the lock the normal
way, ledgering a stalled-holder-replaced row. A holder that survives TERM keeps
the old refusal.

Fixes kunchenguid#4400
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Verdict: Diff reviewed (evict_stalled_holder identity-verified TERM once past FM_WATCHER_STALL_BOUND default 3× grace; under-bound refusal unchanged; arm ledger stalled-holder-replaced). Closes ready-for-pr #4400 (Fixes keyword + author intent verified: live-but-stale beacon deadlock). contract-class: restore — existing watcher re-arm/refusal path left homes unsupervised indefinitely; tunable bound only closes that hole, no always-on new surface.

VISION: aligns — One captain one interface: yes (homes stay supervised without captain hand-restart). Authority explicit: yes (env override only; default restores intended recovery). Scripts vs agents: yes. Restart non-event: yes (stalled holder no longer bricks re-arm). Delegation with spine: yes. Fleet outlives vendor: n/a (watcher core). Scope: yes. Closing: peace of mind that a wedged mid-cycle watcher cannot refuse every replacement forever.

Safe review; CI/NM green; MERGEABLE/CLEAN. Squash-merging as restore.

@kunchenguid
kunchenguid merged commit b575497 into kunchenguid:main Sep 25, 2026
19 checks passed
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: this is merged. Thank you @karotkriss — really appreciate you taking the time on this.

kunchenguid pushed a commit to karotkriss/firstmate that referenced this pull request Sep 25, 2026
… + stall eviction

Header comment conflict only after kunchenguid#5594; retain both the stalled-holder
eviction docs from main and this PR's per-poll home/state/code-root exit.
mituso89 pushed a commit to mituso89/firstmate that referenced this pull request Sep 26, 2026
…ad of refusing every re-arm (kunchenguid#5594)

* fix(bin): replace a watcher whose beacon stalls past a hard bound instead of refusing every re-arm

A fleet watcher that is alive but whose liveness beacon has gone stale could
never be replaced: every re-arm was refused because the lock holder was a live
pid, and the holder was never evicted because it was not dead. Add
FM_WATCHER_STALL_BOUND (default 3x the stale grace): below it the refusal is
unchanged; at or past it the arm re-verifies the holder against the lock's
recorded identity, sends TERM, waits boundedly, and takes the lock the normal
way, ledgering a stalled-holder-replaced row. A holder that survives TERM keeps
the old refusal.

Fixes kunchenguid#4400

* no-mistakes(test): poll for replacement message to fix watcher-lock test flake

* no-mistakes(document): document FM_WATCHER_STALL_BOUND in config inventory
mehulbhagwani pushed a commit to mehulbhagwani/firstmate that referenced this pull request Sep 26, 2026
…ad of refusing every re-arm (kunchenguid#5594)

* fix(bin): replace a watcher whose beacon stalls past a hard bound instead of refusing every re-arm

A fleet watcher that is alive but whose liveness beacon has gone stale could
never be replaced: every re-arm was refused because the lock holder was a live
pid, and the holder was never evicted because it was not dead. Add
FM_WATCHER_STALL_BOUND (default 3x the stale grace): below it the refusal is
unchanged; at or past it the arm re-verifies the holder against the lock's
recorded identity, sends TERM, waits boundedly, and takes the lock the normal
way, ledgering a stalled-holder-replaced row. A holder that survives TERM keeps
the old refusal.

Fixes kunchenguid#4400

* no-mistakes(test): poll for replacement message to fix watcher-lock test flake

* no-mistakes(document): document FM_WATCHER_STALL_BOUND in config inventory
mehulbhagwani pushed a commit to mehulbhagwani/firstmate that referenced this pull request Sep 26, 2026
…ad of refusing every re-arm (kunchenguid#5594)

* fix(bin): replace a watcher whose beacon stalls past a hard bound instead of refusing every re-arm

A fleet watcher that is alive but whose liveness beacon has gone stale could
never be replaced: every re-arm was refused because the lock holder was a live
pid, and the holder was never evicted because it was not dead. Add
FM_WATCHER_STALL_BOUND (default 3x the stale grace): below it the refusal is
unchanged; at or past it the arm re-verifies the holder against the lock's
recorded identity, sends TERM, waits boundedly, and takes the lock the normal
way, ledgering a stalled-holder-replaced row. A holder that survives TERM keeps
the old refusal.

Fixes kunchenguid#4400

* no-mistakes(test): poll for replacement message to fix watcher-lock test flake

* no-mistakes(document): document FM_WATCHER_STALL_BOUND in config inventory
ashkonmousavi added a commit to ashkonmousavi/firstmate that referenced this pull request Sep 26, 2026