Repository navigation
fix: add turn-end supervision guard - #249
Merged
Merged
Conversation
fm-guard.sh is pull-based: it only warns when some other supervision script happens to run, so a primary session that ends a turn without re-arming the watcher and then runs no further fleet-touching command can sit blind for hours (the 2026-07-04 incident this fixes). Add bin/fm-turnend-guard.sh, a Claude Code Stop hook registered in the tracked .claude/settings.json, that fires on every primary turn end and blocks (exit 2, verified empirically to force continuation) when work is in flight with no fresh watcher beacon. It never blocks more than once per turn, using Claude Code's own stop_hook_active loop-guard field, and scopes itself to the actual primary checkout only (inert in crewmate/scout worktrees and secondmate homes). Factor the shared "in-flight but no live watcher" predicate out of fm-guard.sh into bin/fm-supervision-lib.sh so the pull-based banner and the push-based hook can never drift on what "unhealthy" means. Document the verified Stop-hook mechanism and scoping in docs/turnend-guard.md, add a harness-adapters note, and cover the predicate and hook with tests/fm-turnend-guard.test.sh.
kunchenguid
force-pushed
the
fm/fm-turnend-guard-g4
branch
from
July 4, 2026 19:29
b8bee3c to
f71393e
Compare
leo1oel
added a commit
to leo1oel/nemo
that referenced
this pull request
Jul 5, 2026
…kunchenguid#256) (#26) Port upstream kunchenguid#249 with the kunchenguid#256 path-anchoring fix. fm-guard.sh is pull-based - it warns only when another supervision script happens to run - so a primary session that ends a turn without re-arming the watcher can sit blind for hours. bin/fm-turnend-guard.sh is push-based: a Claude Code Stop hook (tracked .claude/settings.json, anchored via $CLAUDE_PROJECT_DIR since hooks run from the session cwd) that blocks the turn end (exit 2, at most once per turn via stop_hook_active) when tasks are in flight with no fresh watcher beacon. Fork adaptations: python3 instead of jq for the payload (fail-open), and an afk branch that judges the sub-supervisor daemon pid instead of the beacon, since the daemon owns the watcher while state/.afk exists. Scoped to the primary checkout; secondmate homes and linked worktrees no-op.
mielyemitchell
added a commit
to mielyemitchell/firstmate
that referenced
this pull request
Jul 6, 2026
Intent
Build a turn-end supervision guard for the PRIMARY firstmate session so a turn that ends blind (fleet work in flight but no live watcher) raises an immediate alarm instead of waiting for the next fleet-touching command. Motivated by a 2026-07-04 incident where the primary ended a turn without re-arming the watcher and a parked pipeline gate sat unnoticed for ~9 hours, since fm-guard.sh is pull-based (only warns when some other supervision script runs).
Design deliberately chosen after empirical investigation (verified live against a real claude -p session, not just docs):
Also added docs/turnend-guard.md recording the verified empirical Stop-hook findings (exact JSON schema fields, the exit-code-2 block mechanism, the stop_hook_active loop-guard, and that a project-level .claude/settings.json does not take effect when launched from a subdirectory of the project - verified live), added a harness-adapters skill note, an AGENTS.md section 8 note, docs/scripts.md and README.md doc-index entries, and a new tests/fm-turnend-guard.test.sh (14 cases) covering the predicate lib directly and the hook script's scoping/loop-guard/fail-open behavior end to end. Confirmed no regressions: shellcheck clean and the full existing 41-file test suite passes unchanged.
What Changed
fm-guard.sh, watcher arming, and the new turn-end guard use consistent state and watcher predicates.Risk Assessment
Testing
The provided full-suite baseline was already green; I then ran the focused turn-end guard, tangle guard, and watcher lock suites, plus a manual end-to-end Stop-hook transcript showing the configured hook blocks with exit 2 and the exact required reason when in-flight work has no live watcher, allows the loop-guard retry, allows a real live watcher, and stays inert in a secondmate home.
Evidence: Manual turn-end guard transcript
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 1 issue found → auto-fixed (3) ✅
bin/fm-turnend-guard.sh:66- The hook hard-codes$FM_ROOT/stateinstead of the establishedFM_HOME/FM_STATE_OVERRIDEstate resolution used by the rest of firstmate. In a supported primary session withFM_HOMEset, it can miss the real in-flight tasks or block on stale repo-root state; computeSTATEthe same way asfm-guard.shand run the predicate against that.🔧 Fix: Respect active home in turnend guard
1 error still open:
bin/fm-turnend-guard.sh:68- The Stop hook reuses the grace-basedfm_supervision_unhealthypredicate, so a watcher that just fired and exited still looks healthy for up toFM_GUARD_GRACEseconds because.last-watcher-beatis fresh. That lets the exact failure mode through: after handling a wake, ending the turn without re-arming the watcher will usually happen inside the 300s grace window, so the hook exits 0 and the fleet can go blind until some later fleet command. The hook needs an end-of-turn predicate that verifies a live watcher/arm is actually present, or otherwise treats a missing watcher lock/process as unhealthy even when the last beacon is recent.🔧 Fix: Require live watcher for turn-end guard
1 warning still open:
tests/fm-turnend-guard.test.sh:342-date +%s%Nis GNU-specific. On macOS/BSDdate,%Nis left literal, soelapsed_ms=$(( (end - start) / 1000000 ))can abort the new test underset -uinstead of running the suite on a supported local platform. Use a portable seconds-based check or an existing portable timing helper.🔧 Fix: Captain: portable turn-end timing
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
command -v tmux >/dev/null || { echo "tmux is required for e2e tests" >&2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo "== $t =="; bash "$t" || rc=1; done; exit "$rc"Baseline already ran successfully before this validation:command -v tmux >/dev/null || { echo "tmux is required for e2e tests" >&2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo "== $t =="; bash "$t" || rc=1; done; exit "$rc"bash tests/fm-turnend-guard.test.shbash tests/fm-tangle-guard.test.shbash tests/fm-watcher-lock.test.shManual Stop-hook evidence: created primary-shaped and secondmate-shaped fixtures in the evidence directory, invokedFM_ROOT_OVERRIDE=<fixture> bash bin/fm-turnend-guard.shwith Claude Stop payloads, and startedbin/fm-watch.shonce to verify the live-watcher allow path; captured transcript at/var/folders/5x/4nqprlbx0518k3ybcb1sz6gr0000gn/T/no-mistakes-evidence/01KWQ6E50HEJGKADE8WARXP26Z/turnend-guard-manual-transcript.txt✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.