Skip to content

fix(watch): assert watcher liveness during wake drain - #101

Merged
kunchenguid merged 2 commits into
mainfrom
fm/watch-rearm-fix-w3
Jun 26, 2026
Merged

kunchenguid merged 2 commits into
mainfrom
fm/watch-rearm-fix-w3

Conversation

@kunchenguid

Copy link
Copy Markdown
Owner

Intent

Tighten firstmate's watcher re-arm discipline (AGENTS.md section 8) and add a wake-drain liveness assertion so the watcher supervision chain cannot silently lapse. Motivating incident: the watcher (bin/fm-watch.sh, armed via bin/fm-watch-arm.sh as a harness-tracked background task) died ~12 min undetected because re-arms were bundled at the tail of multi-command bashes (where fm-watch-arm silently no-ops when a cycle is briefly still alive, so no fresh cycle got established) and because text-only 'holding' turns ran no supervision script, so fm-guard.sh's liveness banner (which only fires when a supervision script runs) never triggered.

Three deliberate changes:

  1. AGENTS.md section 8: reworded the re-arm discipline into four unambiguous bold-led rules - keep exactly one live cycle while work is in flight; re-arm after each FIRE (a completed arm task carrying a wake reason signal/stale/check/heartbeat) and do NOT churn on a healthy/started no-op; run fm-watch-arm standalone, never bundled in a multi-command bash; never end a turn blind, holds included. This intentionally REPLACES the old misleading 'Re-arming is the primary model: just run fm-watch-arm and let the singleton lock no-op' line that encouraged the churn. Existing correct material (singleton lock, beacon, guard banner, afk-daemon-owns-watcher exception) is preserved verbatim - a tightening, not a rewrite. Also added one line noting fm-wake-drain.sh now runs the same guard.

  2. bin/fm-wake-drain.sh: added an assert_watcher_liveness() helper that REUSES fm-guard.sh's existing graced, beacon-based banner (no duplicated beacon math, per the task constraint) and called it on both success-exit paths. Deliberately called AFTER the queue is emptied so the guard never re-prints its own 'queued wakes pending' notice for the records this run just drained, and wrapped with '|| true' so a guard hiccup never changes the drain's exit status. Because the watcher touches its beacon every poll cycle, a normal fire leaves a recent beacon well inside FM_GUARD_GRACE so the drain stays silent right after a fire; it warns only on a genuine stale-beyond-grace lapse with work in flight. The watcher core (fm-watch.sh, the singleton lock, the beacon-touch, the wake-queue) is deliberately UNTOUCHED.

  3. Tests: the drain's new guard call would otherwise emit a spurious WORKTREE TANGLE banner across the drain-invoking suites because FM_ROOT resolves to the feature-branch test checkout during validation, so tests/wake-helpers.sh now sets a default FM_ROOT_OVERRIDE to a fresh non-git dir to keep the tangle check inert (the same trick the direct fm-guard.sh tests already use); a per-call FM_ROOT_OVERRIDE still wins. Added a regression test (test_drain_asserts_watcher_liveness in tests/fm-wake-queue.test.sh) asserting the drain warns on a lapse and stays silent right after a fire. Verified locally: shellcheck clean, all behavior suites green, and a negative control confirmed the new test genuinely fails if the assertion is removed.

What Changed

  • Tightened watcher supervision rules in AGENTS.md and related docs so captain-facing instructions require one live cycle, standalone re-arms after fired wakes, and supervision before ending holding turns.
  • Added a drain-time watcher liveness assertion to bin/fm-wake-drain.sh by reusing fm-guard.sh after queue drains without changing the drain exit status.
  • Updated wake queue test helpers and regression coverage so drain liveness warnings are exercised without unrelated guard tangle noise.

Risk Assessment

✅ Low: Captain, the change is narrowly scoped to documentation, a successful-drain guard call that reuses existing liveness logic, and focused regression coverage; I did not find material risks in the changed paths.

Testing

I inspected the target diff, ran the focused wake-drain/watcher/daemon suites, ran the full behavior-test loop, captured a manual CLI transcript showing the WATCHER DOWN banner and fresh-beacon silence, verified the AGENTS.md re-arm rules and old line removal, and confirmed the worktree stayed clean with no transient build/test directories left behind.

Evidence: Manual fm-wake-drain liveness transcript
$ FM_STATE_OVERRIDE=/var/folders/5x/4nqprlbx0518k3ybcb1sz6gr0000gn/T/no-mistakes-evidence/01KW31M8H57RN64KXEMCME7WZP/drain-liveness-case/state FM_ROOT_OVERRIDE=/var/folders/5x/4nqprlbx0518k3ybcb1sz6gr0000gn/T/no-mistakes-evidence/01KW31M8H57RN64KXEMCME7WZP/drain-liveness-case/non-git-root bin/fm-wake-drain.sh >/tmp/no-beacon.out 2>/tmp/no-beacon.err
# stderr (no beacon, work in flight)
●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
●  WATCHER DOWN - SUPERVISION IS OFF
●  1 task(s) in flight, but no watcher has a fresh beacon (last beat: never, grace 300s).
●  Trust bin/fm-watch-arm.sh for the true state: it confirms a live watcher and a fresh beacon, or fails loudly.
●  Re-arm it NOW: run bin/fm-watch-arm.sh as the harness-tracked background task (never a shell & that gets reaped).
●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# stdout (no beacon, work in flight)

$ touch /var/folders/5x/4nqprlbx0518k3ybcb1sz6gr0000gn/T/no-mistakes-evidence/01KW31M8H57RN64KXEMCME7WZP/drain-liveness-case/state/.last-watcher-beat
$ FM_STATE_OVERRIDE=/var/folders/5x/4nqprlbx0518k3ybcb1sz6gr0000gn/T/no-mistakes-evidence/01KW31M8H57RN64KXEMCME7WZP/drain-liveness-case/state FM_ROOT_OVERRIDE=/var/folders/5x/4nqprlbx0518k3ybcb1sz6gr0000gn/T/no-mistakes-evidence/01KW31M8H57RN64KXEMCME7WZP/drain-liveness-case/non-git-root FM_GUARD_GRACE=300 bin/fm-wake-drain.sh >/tmp/fresh.out 2>/tmp/fresh.err
# stderr bytes (fresh beacon)
       0
# stdout bytes (fresh beacon)
       0

# Final queue file bytes
       0

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • git status --short --branch
  • git diff --stat fe3c867788fe389bbcaddbf9500c27cceb99504a..eb6897bb28b25df74b6dc2ab7d966904c16b9b25
  • git diff --name-only fe3c867788fe389bbcaddbf9500c27cceb99504a..eb6897bb28b25df74b6dc2ab7d966904c16b9b25
  • Reviewed base-to-target diffs for AGENTS.md, bin/fm-wake-drain.sh, tests/fm-wake-queue.test.sh, and tests/wake-helpers.sh.
  • tests/fm-wake-queue.test.sh
  • tests/fm-watcher-lock.test.sh
  • tests/fm-daemon.test.sh
  • tests/fm-wake-daemon-lifecycle-e2e.test.sh
  • for test_script in tests/*.test.sh; do "$test_script"; done
  • Manual bin/fm-wake-drain.sh check with an in-flight meta file and no watcher beacon, then with a fresh .last-watcher-beat, writing evidence to /var/folders/5x/4nqprlbx0518k3ybcb1sz6gr0000gn/T/no-mistakes-evidence/01KW31M8H57RN64KXEMCME7WZP.
  • rg -n "Keep exactly one live cycle|Standalone, never bundled|Re-arm after each FIRE|No turn ends blind|fm-wake-drain.sh.*same guard" AGENTS.md
  • if rg -q "Re-arming is the primary model" AGENTS.md; then printf 'old misleading re-arm line still present\n' >&2; exit 1; fi
  • git status --short
  • find . -maxdepth 2 -type d \( -name '.pytest_cache' -o -name 'node_modules' -o -name 'coverage' -o -name 'dist' -o -name 'build' \) -print
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…drain

The watcher supervision chain could silently lapse: re-arms were bundled
at the tail of multi-command calls (where fm-watch-arm no-ops when a cycle
is briefly still alive, so no fresh cycle got established), and text-only
"holding" turns ran no supervision script, so fm-guard's liveness banner -
which only fires when a guarded script runs - never triggered.

AGENTS.md section 8: make the re-arm rules unambiguous - keep exactly one
live cycle while work is in flight; re-arm after each FIRE (a completed arm
task carrying a wake reason) and never churn on a healthy/started no-op; run
fm-watch-arm standalone, never bundled; never end a turn blind, holds
included. Existing material (singleton lock, beacon, guard banner, afk
exception) is preserved.

fm-wake-drain.sh: assert watcher liveness after draining by reusing
fm-guard.sh's existing graced, beacon-based banner, so a lapse also surfaces
on a plain drain-and-handle turn. Called after the queue is emptied so the
guard never re-prints its own queued-wakes notice, and best-effort so it
never changes the drain's exit status. The grace beacon keeps it silent right
after a normal fire and warns only on a stale-beyond-grace lapse with work in
flight. Watcher core (fm-watch.sh, lock, beacon-touch, wake-queue) untouched.

tests: keep the worktree-tangle check inert across the drain-invoking suites
(wake-helpers points FM_ROOT at a non-git dir, the same trick the direct
fm-guard.sh tests use), and add a regression test asserting the drain warns on
a lapse and stays silent right after a fire.
@kunchenguid
kunchenguid merged commit 9391e90 into main Jun 26, 2026
4 checks passed
@kunchenguid
kunchenguid deleted the fm/watch-rearm-fix-w3 branch June 26, 2026 23:07
leo1oel added a commit to leo1oel/nemo that referenced this pull request Jun 27, 2026
…liveness on drain (kunchenguid#101) (#12)

Port two upstream fixes; both are harness-agnostic supervision/contract changes.

kunchenguid#97 harden no-mistakes validation contract:
- bin/fm-brief.sh: rewrite the ship brief's validation block - the pipeline owns
  every fix (incl. the fix for a real bug review finds in the crewmate's own
  code); respond, never self-implement/abort/re-run; process every return
  (backgrounding ok, never idle-wait for auto-advance); review findings always
  gate; avoid --yes (it auto-resolves ask-user with zero escalation).
- AGENTS.md section 7: supervisor heuristic - judge a validating crewmate by the
  run step status (running/fixing/ci = working; awaiting_approval/fix_review =
  parked), not shell liveness, plus a self-fix-duplication red flag.
- CONTRIBUTING.md: same crewmate-validation contract.

kunchenguid#101 assert watcher liveness during wake drain:
- bin/fm-wake-drain.sh: after draining, call fm-guard.sh (best-effort) so a lapsed
  supervision chain also surfaces on a plain drain-and-handle turn, not only when
  a guarded script runs. Graced/beacon-based, so silent right after a normal fire.
- AGENTS.md section 8: unambiguous re-arm rules - keep exactly one live cycle,
  re-arm after each FIRE and never churn on a healthy/started no-op, run
  fm-watch-arm standalone (never bundled), never end a turn blind (holds included).
- fm-guard.sh / fm-watch-arm.sh: comment clarifications; docs synced.
- tests/fm-wake-queue.test.sh: pin FM_ROOT (drain now runs the tangle check) and
  add a regression test (drain warns on a lapse, silent after a fire).

shellcheck clean; full suite green (96 checks).
@nikzorz

nikzorz commented Oct 15, 2026

Copy link
Copy Markdown

Local run evidence for tests/fm-remote-secondmate-lifecycle-e2e.test.sh

Diagnosis: reproduced on unchanged main at load ~10 on 12 cores. A timestamped trace of the spawn showed about 31s spent inside fm-remote-inherit-push.sh: 13 inherited items before data/captain-shared.md, each a full fm-on -> ssh -> remote entrypoint round trip, with no sleep or lock wait on the path. At load ~5 the spawn reached its blocked write after 1463 of the old 1500 x 0.02s polls. That is load-driven throughput exhausting a fixed poll count, not an ordering race or a bin/ bug.

run1.log

uptime before:  10:23:40 up 1 day, 10:53,  1 user,  load average: 2.51, 4.30, 6.04
ok - remote spawn serializes inheritance through launch publication
ok - config push and bootstrap serialize remote inheritance convergence
not ok - the watcher did not exit on its auto-relaunch wake within the bound
rc=1 elapsed=719s
uptime after:  10:35:39 up 1 day, 11:05,  1 user,  load average: 1.01, 2.65, 4.56

run2.log

uptime before:  10:35:39 up 1 day, 11:05,  1 user,  load average: 1.01, 2.65, 4.56
ok - remote spawn serializes inheritance through launch publication
ok - config push and bootstrap serialize remote inheritance convergence
not ok - the watcher did not exit on its auto-relaunch wake within the bound
rc=1 elapsed=652s
uptime after:  10:46:31 up 1 day, 11:16,  1 user,  load average: 0.66, 1.23, 2.84

run3.log

uptime before:  10:46:31 up 1 day, 11:16,  1 user,  load average: 0.66, 1.23, 2.84
ok - remote spawn serializes inheritance through launch publication
ok - config push and bootstrap serialize remote inheritance convergence
not ok - the watcher did not exit on its auto-relaunch wake within the bound
rc=1 elapsed=730s
uptime after:  10:58:41 up 1 day, 11:28,  1 user,  load average: 7.63, 4.99, 4.29

run4-siblings.log

uptime before:  11:30:46 up 1 day, 12:01,  1 user,  load average: 1.31, 12.71, 14.15
ok - remote spawn serializes inheritance through launch publication
ok - config push and bootstrap serialize remote inheritance convergence
not ok - the watcher did not exit on its auto-relaunch wake within the bound
rc=1 elapsed=769s
uptime after:  11:43:35 up 1 day, 12:13,  1 user,  load average: 4.11, 6.03, 9.57

Runs 1-3 are on the first commit (spawn wait only); run 4 is on the final head (sibling waits too). In every run the inheritance legs pass. Each run then stops at the separately ticketed watcher auto-relaunch bound, so the file never reaches the respawn sibling wait locally; CI ran the whole file green.

fritznoff pushed a commit to fritznoff/starship that referenced this pull request Nov 2, 2026
…henguid#101)

* fix(bin): hold a macOS sleep assertion for the watcher lifetime

Per-run caffeinate gaps let the machine sleep mid-fleet-work. The lock-owning watcher now keeps caffeinate -dims bound to its pid for the whole process, so the assertion dies with the watcher and does not pile up across successors.

* no-mistakes(review): drop display-wake flag from watcher lifetime caffeinate assertion

* no-mistakes(ci): declare the caffeinate watcher test in shards and fork surface

tests/fm-watch-caffeinate.test.sh joins the watcher-wake-lock family and
portable serial shard 1 so the coverage guard's shard partition stays
complete, and a watcher-sleep-assertion capability claims the fork-only
test path so the declared-fork-surface gate passes.

* fix(tests): reap background children from the real test shell on Linux

BASHPID inside command substitution is the subshell, so pgrep never saw the suite's background jobs on bash 4+.

* no-mistakes: apply CI fixes
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