Repository navigation
fix(bin): keep the Claude turn-end guard waiting while a supervision-host auto-arm is starting - #68
Merged
Conversation
The Claude Stop guard treated an in-flight host re-arm as supervision being off when the generation claim landed after its 800ms window. Keep waiting while that auto-arm's starting marker names a live matching process, and still block when the arm is absent, dead, or past the bound.
…ion quietly waiting... just kidding captain only above enough
…he two behavior failures don't come from this PR's code, and Full suite failed only because those other lanes did. - **Lint 2 (fixed):** ShellCheck reported SC2329 ("this function is never invoked") for the `sleep()` override at `tests/fm-turnend-guard.test.sh:2556`, in the host-starting-bound test. The function isn't dead: `export -f sleep` passes it to the guard process so each claim-wait pass runs slowly. ShellCheck can't see that, so I added a scoped `# shellcheck disable=SC2329` with a comment explaining the export. This is the same approach as the earlier SC2016 lint fix. No other file has the problem. Running `bin/fm-lint.sh` on the file with ShellCheck 0.11.0 and full extended analysis now passes. - **Behavior portable serial 6:** The failure was in `tests/fm-extension-binding.test.sh` ("recovered source orphan-second never handled its private result"). - **Behavior portable serial 16:** The failure was in `tests/fm-watcher-lock.test.sh` ("the successor never delivered its recovery wake and exited"). - Neither test file uses anything this PR changes: the turn-end guard, the Claude Stop auto-arm script, or the starting marker. - Both files pass locally with no failing tests, so these look like timing flakes on the CI runners. - **Full suite:** It's only a roll-up that fails when any other lane fails ("A full-suite job failed or was cancelled"). It should pass once the lanes above pass. `tests/fm-turnend-guard.test.sh` also passes in full locally. The change is left uncommitted in the worktree
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
Turn-end guard says supervision is off while the supervision host is re-arming
Resolution recorded by fm-captain-hold.
Decision digest: 5bdc02baef9afb174011b9518ace85ceccc9553f92fc3327f30be8528c439697
Resolution mode: released
Captain decision:
Captain 2026-11-15 (Jobs page note 1794703235-3uxISq): "The 11 firstmate fixes - do all of these". Start now instead of the staggered dates; Grok builds, DeepSeek checks, firstmate repo reviews one at a time (fleet 133), captain merges each.
Seen 3 times on 2026-11-09 (about 07:00Z, 07:09Z, 07:10Z): at the end of a long wake-handling turn, the Claude Stop guard (bin/fm-turnend-guard.sh --claude) prints "TURN WOULD END BLIND - SUPERVISION IS OFF ... last beat 69-428s ago ... the Stop-owned auto-arm did not claim this home", yet seconds later fm-supervision-host.sh park plus fm-watch-arm.sh/fm-watch.sh are live with a fresh beat. Supervision host is on for this home. Find whether the guard misses the host-owned arm path (false alarm) or there is a real gap between host cycles; fix the guard or the gap. Firstmate repo ship, upkeep side job (Grok + DeepSeek).
"The 11 firstmate fixes - do all of these" (2026-11-15)
What Changed
bin/fm-claude-stop-autoarm.shnow writes its pid tostate/.claude-autoarm-startingbefore it sources any libraries. An EXIT trap removes the file, but only if it still holds this process's pid. This lets the synchronous Stop guard see an auto-arm that is running but has not made its claim yet.bin/fm-turnend-guard.sh --claudestill uses the shortFM_CLAUDE_AUTOARM_SYNC_WAIT_MSwait. On a home where the supervision host is enabled, it now keeps waiting after that window for as long as the starting marker names a live process runningfm-claude-stop-autoarm.sh. It stops waiting when the claim or epoch proof appears, when that process exits, or whenFM_CLAUDE_AUTOARM_HOST_CLAIM_WAIT_MS(default 15000) has passed since the wait began. After the loop it checks recovery once more. In these cases it keeps the short wait and still shows the "supervision is off" banner: no marker, a symlinked or non-numeric marker, a dead or reused pid, and a home with the host turned off. The allow path is now a sharedallow_autoarm_recoveryhelper.docs/configuration.mddocuments the new setting,docs/turnend-guard.mddescribes the extended wait, and the operational home layout skill lists the new marker file.tests/fm-turnend-guard.test.shadds tests for the extended wait and for each case that still blocks.🤖 Generated with Claude Code
Risk Assessment
✅ Low: The change is narrow. It extends the --claude claim wait only while a host-enabled home's starting marker names a live fm-claude-stop-autoarm.sh process, and an elapsed-time deadline caps that wait (default 15s). The round-4 fix added a recheck after the loop, so a rewake, claim, or healthy watcher that lands as the starter exits still allows the stop. The marker is published atomically and its owner-only release is correct. I found no reachable path that gives a wrong verdict.
Testing
I ran the targeted turnend-guard test file, which passed. I also drove the real Stop auto-arm and the real
--claudeguard together in disposable primary homes. On base, the real auto-arm published itsarmingclaim, but the guard still printed the "TURN WOULD END BLIND ... Stop-owned auto-arm did not claim" banner. That is the reported false alarm, reproduced live. On the fixed HEAD, the same race allowed the stop silently every time, waiting about 700-870ms for the real claim. HEAD also used no continuation and left no starting marker behind. A home that opted out of the host still showed the banner, which matches the recorded decision. A genuinely missing auto-arm with a stale dead-pid marker still blocked in about 1.6s, not the 15s ceiling. The auto-arm does create a missing state/ dir, but base does the same: fm-wake-lib.sh makes it when sourced. The new marker publish correctly skipped it. Everything ran as real scripts in the hook-firing order. No real Claude TUI was run, because the gate worktree is a linked worktree where these hooks stay inert by design. The lab was torn down afterward.claudefrom the gate worktree puts FM_ROOT in a linked worktree, and fm_primary_scope_matches deliberately keeps both hooks inert there. The race also depends on how long the auto-arm takes…Evidence: Live HEAD vs base vs host-off guard decisions with the real auto-arm
Source: Live HEAD vs base vs host-off guard decisions with the real auto-arm
Evidence: Live guard blocks quickly with no auto-arm and a stale marker
Source: Live guard blocks quickly with no auto-arm and a stale marker
Evidence: Auto-arm state/ creation is the same on HEAD and base
Source: Auto-arm state/ creation is the same on HEAD and base
Evidence: Targeted turnend guard test log
Source: Targeted turnend guard test log
Pipeline
Updates from git push no-mistakes
✅ **Intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-claude-stop-autoarm.sh:237- publish_starting_marker runsmkdir -p "$STATE"on every Stop, before the scope check at line 309. Before this change, the hook exited without writing anything in a checkout that is not a primary home. Now, every Claude Stop in a linked worktree of the firstmate repo (crewmate/scout worktrees, gate worktrees like this one) leaves behind a state/ directory that persists. It also briefly creates the starting marker and its lock directory there. That is a new side effect in scopes the hook is documented to ignore. The mkdir is also unnecessary for primary homes: fm-sessionstart-run.sh:93-99 already creates state/ for a primary root, and the guard stands down when state/ is missing (fm-turnend-guard.sh:214 via fm_primary_scope_matches). Fix: replace the mkdir with[ -d "$STATE" ] || return 0so the marker is only published into a state dir that already exists.bin/fm-turnend-guard.sh:592- The longer claim wait only applies when fm_supervision_host_enabled is true. The slow pre-claim work in fm-claude-stop-autoarm.sh (sourcing six libraries, ps-based ancestry checks, fm-lock.sh, fm_supervision_needed, lines 260-399) is the same whether or not the home runs the supervision host. So a Claude home that opted out of the host can still hit the same false 'did not claim' banner when its claim lands after 800ms. The tests (host-off case) and docs show this restriction is deliberate and it matches the stated intent, which is about a host home. Asking only to confirm that non-host homes should keep the short window.docs/configuration.md:3483- The docs say the guard 'keeps waiting past FM_CLAUDE_AUTOARM_SYNC_WAIT_MS', which reads as extra time on top of the sync wait. The code (fm-turnend-guard.sh host_ticks = max(HOST_CLAIM_WAIT_MS, SYNC_WAIT_MS)/100, counted from the start of the loop) treats it as the total wait. The guard header comment also describes it as the total. Reword it as 'total milliseconds the --claude guard may wait (counted from the start of the claim wait, at least FM_CLAUDE_AUTOARM_SYNC_WAIT_MS) while ...'.bin/fm-claude-stop-autoarm.sh:251- release_starting_marker checks[ -f "$STARTING_MARKER" ]before taking the starting lock, then reads the file without silencing errors. Race: firing A sees B's marker, B removes it under the lock, then A'sread < "$STARTING_MARKER"fails. Bash then prints 'No such file or directory' to stderr from A's EXIT trap. If A is exiting 2, its stderr is the model-visible rewake banner, so that error line gets appended to the banner. Fix: add2>/dev/nullto the redirect, or repeat the -f check after acquiring the lock.🔧 Fix applied.
6 issues (2 warnings, 4 infos) still open:
bin/fm-claude-stop-autoarm.sh:237- publish_starting_marker runsmkdir -p "$STATE"on every Stop, before the scope check at line 309. Before this change, the hook exited without writing anything in a checkout that is not a primary home. Now, every Claude Stop in a linked worktree of the firstmate repo (crewmate/scout worktrees, gate worktrees like this one) leaves behind a state/ directory that persists. It also briefly creates the starting marker and its lock directory there. That is a new side effect in scopes the hook is documented to ignore. The mkdir is also unnecessary for primary homes: fm-sessionstart-run.sh:93-99 already creates state/ for a primary root, and the guard stands down when state/ is missing (fm-turnend-guard.sh:214 via fm_primary_scope_matches). Fix: replace the mkdir with[ -d "$STATE" ] || return 0so the marker is only published into a state dir that already exists.bin/fm-turnend-guard.sh:592- The longer claim wait only applies when fm_supervision_host_enabled is true. The slow pre-claim work in fm-claude-stop-autoarm.sh (sourcing six libraries, ps-based ancestry checks, fm-lock.sh, fm_supervision_needed, lines 260-399) is the same whether or not the home runs the supervision host. So a Claude home that opted out of the host can still hit the same false 'did not claim' banner when its claim lands after 800ms. The tests (host-off case) and docs show this restriction is deliberate and it matches the stated intent, which is about a host home. Asking only to confirm that non-host homes should keep the short window.bin/fm-claude-stop-autoarm.sh:251- release_starting_marker checks[ -f "$STARTING_MARKER" ]before taking the starting lock, then reads the file without silencing errors. Race: firing A sees B's marker, B removes it under the lock, then A'sread < "$STARTING_MARKER"fails. Bash then prints 'No such file or directory' to stderr from A's EXIT trap. If A is exiting 2, its stderr is the model-visible rewake banner, so that error line gets appended to the banner. Fix: add2>/dev/nullto the redirect, or repeat the -f check after acquiring the lock.bin/fm-claude-stop-autoarm.sh:184- Simplification: the directory lock around the starting marker (starting_lock_remove / starting_lock_acquire / starting_lock_release, lines 184-232, plus its uses at 238/242 in publish_starting_marker and 250/256 in release_starting_marker) is about 50 lines of extra machinery. It has its own stale-holder recovery and re-entry handling. The intent only requires that the guard stop reporting a false 'did not claim' while a host auto-arm is still starting. The marker plus a compare-before-remove on exit already gives that. The lock only covers the microsecond window between release reading the marker and running rm, and that window matters only when two auto-arm firings overlap. Even in that case the guard rechecks the named pid's liveness and command, and the wait is bounded. The lock also does not fully deliver its guarantee: two waiters that both see the same dead holder can each run starting_lock_remove, and one of them can remove the other's freshly made lock. That defect sits inside this component, so the smallest fix is to remove it. Recommendation: remove the lock and keep the atomic tmp+mv publish and the read/compare/rm release. The existing 'drops only its own' test still covers that narrower form.bin/fm-claude-stop-autoarm.sh:240- Round 1's fix for review-4 changed the read redirects to2>/dev/null <file. It left two write redirects in the older order, where>filecomes before2>/dev/null. Bash applies redirections left to right, so when the open fails, the error ('No such file or directory' or 'Permission denied') is printed to the hook's real stderr before 2>/dev/null takes effect. I checked this withprintf x > /nonexistent/f 2>/dev/null. On an exit-2 path, that stderr is the model-visible rewake banner. Sites: bin/fm-claude-stop-autoarm.sh:240 (printf ... > "$tmp" 2>/dev/nullin publish, for example when state/ exists but is not writable) and bin/fm-claude-stop-autoarm.sh:201 (the lock pid write; it fails if a concurrent stale-steal removes the lock directory just after mkdir). The EXIT trap can reach line 201 after the banner is printed. Fix: write these as2>/dev/null > file. Line 201 goes away if the starting lock is removed as recommended above.bin/fm-claude-stop-autoarm.sh:1- The pipeline fix commit b608427 has a garbled subject: 'no-mistakes(review): Fix auto-arm marker handling and wait documentation quietly waiting... just kidding captain only above enough'. The trailing words are unrelated noise, the kind of thing fix: keep greetings out of no-mistakes commit subjects #66 ('keep greetings out of no-mistakes commit subjects') was meant to stop. This is not a code defect. Squash or reword the commit before merge so it does not land in main's history.🔧 Fix applied.
3 issues (1 warning, 2 infos) still open:
bin/fm-turnend-guard.sh:592- The longer claim wait only applies when fm_supervision_host_enabled is true. The slow pre-claim work in fm-claude-stop-autoarm.sh (sourcing six libraries, ps-based ancestry checks, fm-lock.sh, fm_supervision_needed, lines 260-399) is the same whether or not the home runs the supervision host. So a Claude home that opted out of the host can still hit the same false 'did not claim' banner when its claim lands after 800ms. The tests (host-off case) and docs show this restriction is deliberate and it matches the stated intent, which is about a host home. Asking only to confirm that non-host homes should keep the short window.bin/fm-claude-stop-autoarm.sh:1- The pipeline fix commit b608427 has a garbled subject: 'no-mistakes(review): Fix auto-arm marker handling and wait documentation quietly waiting... just kidding captain only above enough'. The trailing words are unrelated noise, the kind of thing fix: keep greetings out of no-mistakes commit subjects #66 ('keep greetings out of no-mistakes commit subjects') was meant to stop. This is not a code defect. Squash or reword the commit before merge so it does not land in main's history.bin/fm-turnend-guard.sh:596- The extended host wait limits loop iterations, not elapsed time. host_ticks = HOST_CLAIM_WAIT_MS/100, and each pass addssleep 0.1to the real cost of autoarm_owns_recovery and host_arm_pending. That cost is several forks per pass: cat of the watch lock pid, the ledger read, the epoch sed, fm_pid_identity, and thepscall in fm_proc_info. The old loop had the same drift, but it only ran 8 passes. Now it can run up to 150 passes while a live auto-arm is still starting (default 15000 ms). On Linux the real wait goes somewhat past 15s. On Git Bash/MSYS, which the repo supports and where each fork costs tens of milliseconds or more, 150 passes can take well over a minute. The --claude guard entry in .claude/settings.json:54 sets notimeout, so it gets Claude Code's default command-hook timeout. If the harness kills the guard there, the Stop ends with no banner and no block, which is the blind turn this guard exists to catch. It also contradicts the round-1 docs fix in docs/configuration.md:3483, which calls the value the 'total milliseconds the --claude guard may wait'. Fix: record a deadline once before the loop and break on elapsed time instead of oni >= host_ticks. Use $SECONDS, ordate +%scaptured before the loop, because macOS bash 3.2 has no EPOCHREALTIME. The sync window can keep its tick count. The same tick-for-time assumption is the bound described in the header comment at bin/fm-turnend-guard.sh:82-90.🔧 Fix applied.
4 issues (2 warnings, 2 infos) still open:
bin/fm-turnend-guard.sh:592- The longer claim wait only applies when fm_supervision_host_enabled is true. The slow pre-claim work in fm-claude-stop-autoarm.sh (sourcing six libraries, ps-based ancestry checks, fm-lock.sh, fm_supervision_needed, lines 260-399) is the same whether or not the home runs the supervision host. So a Claude home that opted out of the host can still hit the same false 'did not claim' banner when its claim lands after 800ms. The tests (host-off case) and docs show this restriction is deliberate and it matches the stated intent, which is about a host home. Asking only to confirm that non-host homes should keep the short window.bin/fm-claude-stop-autoarm.sh:1- The pipeline fix commit b608427 has a garbled subject: 'no-mistakes(review): Fix auto-arm marker handling and wait documentation quietly waiting... just kidding captain only above enough'. The trailing words are unrelated noise, the kind of thing fix: keep greetings out of no-mistakes commit subjects #66 ('keep greetings out of no-mistakes commit subjects') was meant to stop. This is not a code defect. Squash or reword the commit before merge so it does not land in main's history.bin/fm-turnend-guard.sh:596- The extended host wait limits loop iterations, not elapsed time. host_ticks = HOST_CLAIM_WAIT_MS/100, and each pass addssleep 0.1to the real cost of autoarm_owns_recovery and host_arm_pending. That cost is several forks per pass: cat of the watch lock pid, the ledger read, the epoch sed, fm_pid_identity, and thepscall in fm_proc_info. The old loop had the same drift, but it only ran 8 passes. Now it can run up to 150 passes while a live auto-arm is still starting (default 15000 ms). On Linux the real wait goes somewhat past 15s. On Git Bash/MSYS, which the repo supports and where each fork costs tens of milliseconds or more, 150 passes can take well over a minute. The --claude guard entry in .claude/settings.json:54 sets notimeout, so it gets Claude Code's default command-hook timeout. If the harness kills the guard there, the Stop ends with no banner and no block, which is the blind turn this guard exists to catch. It also contradicts the round-1 docs fix in docs/configuration.md:3483, which calls the value the 'total milliseconds the --claude guard may wait'. Fix: record a deadline once before the loop and break on elapsed time instead of oni >= host_ticks. Use $SECONDS, ordate +%scaptured before the loop, because macOS bash 3.2 has no EPOCHREALTIME. The sync window can keep its tick count. The same tick-for-time assumption is the bound described in the header comment at bin/fm-turnend-guard.sh:82-90.bin/fm-turnend-guard.sh:596- The rewritten claim-wait loop dropped the old post-loopautoarm_owns_recoveryrecheck. Each pass now runsautoarm_owns_recoveryfirst and thenhost_arm_pending, and it breaks straight to the block path when the starter is no longer pending. The usual reason the starter stops pending is that the auto-arm just finished: it published its claim, armed, recordedoutcome=rewakeor made the watcher healthy, exited, and removed.claude-autoarm-starting. That kind of proof is still accepted after the process exits (lines 428 and 448-454). Concrete sequence on a host-enabled home after the sync window: (1) the pass'sautoarm_owns_recoverysees no claim, no fresh epoch, and an unhealthy watcher; (2) while that call and its forks are still running, the auto-arm claims, finishes, writesoutcome=rewakeand exits; (3)host_arm_pendingsees no marker or a dead pid and returns 1; (4) the loop breaks,budget_account_current_epoch blockuses up one continuation, and the user gets the false 'Stop-owned auto-arm did not claim' banner. That is the exact symptom the intent asks to remove. The window is only as long as oneautoarm_owns_recoverycall, which is several forks: tens of ms on Linux and more on Git Bash/MSYS. A host-path auto-arm that hands off quickly can fit inside it. Before this change, the old code always ran one more ownership check after its loop. The round-3 elapsed-time fix kept the same break structure and did not change this. Fix: run one final ownership check before blocking, e.g.autoarm_owns_recovery && allow_autoarm_recoveryright after the loop. Alternatively, recheck ownership only when the break happened becausehost_arm_pendingfailed.🔧 Fix applied.
2 infos still open:
bin/fm-turnend-guard.sh:592- The longer claim wait only applies when fm_supervision_host_enabled is true. The slow pre-claim work in fm-claude-stop-autoarm.sh (sourcing six libraries, ps-based ancestry checks, fm-lock.sh, fm_supervision_needed, lines 260-399) is the same whether or not the home runs the supervision host. So a Claude home that opted out of the host can still hit the same false 'did not claim' banner when its claim lands after 800ms. The tests (host-off case) and docs show this restriction is deliberate and it matches the stated intent, which is about a host home. Asking only to confirm that non-host homes should keep the short window.bin/fm-claude-stop-autoarm.sh:1- The pipeline fix commit b608427 has a garbled subject: 'no-mistakes(review): Fix auto-arm marker handling and wait documentation quietly waiting... just kidding captain only above enough'. The trailing words are unrelated noise, the kind of thing fix: keep greetings out of no-mistakes commit subjects #66 ('keep greetings out of no-mistakes commit subjects') was meant to stop. This is not a code defect. Squash or reword the commit before merge so it does not land in main's history.✅ **Test** - passed
✅ No issues found.
claudefrom the gate worktree puts FM_ROOT in a linked worktree, and fm_primary_scope_matches deliberately keeps both hooks inert there. The race also depends on how long the auto-arm takes…bash tests/fm-turnend-guard.test.sh(targeted file, exit 0; includes the new host-starter, starter-exit, marker-publish and missing-state cases)Live: disposable plain-checkout primary homes extracted from HEAD and from base 6d7e171 with in-flight task state and a 400s-stale watcher beat. Inside each home, a parent process namedclaudeholds state/.lock and starts the realbin/fm-claude-stop-autoarm.shand the realbin/fm-turnend-guard.sh --claudeconcurrently on the same synthetic Stop payload.Live A/B: on HEAD with the host on (the default) and FM_CLAUDE_AUTOARM_SYNC_WAIT_MS=0, twice; on HEAD with the default 800ms window; on base with the host on and a 0ms window, twice; on HEAD with config/supervision-host-offLive adversarial: the real guard on a host-on HEAD home with no auto-arm running and a stale.claude-autoarm-startingmarker naming dead pid 999999Live side effect: ran the real auto-arm against an FM_HOME with no state/ on HEAD and on base, under a bash -x trace✅ **Document** - passed
✅ No issues found.
🔧 **Lint** - 1 issue found → auto-fixed ✅
🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Push** - passed
✅ No issues found.