Repository navigation
fix(bin): stop the Claude turn-end guard raising a false blind-turn alarm while the Stop auto-arm is still starting - #49
Merged
Conversation
…larm while the Stop auto-arm is still starting The guard's 800ms claim window raced the auto-arm's pre-claim work (harness ancestry resolution is a chain of ps forks, seconds under load), so recovery already under way read as absent. The auto-arm now announces itself with a per-pid starting marker before that work and retracts it once its claim role is published; the guard waits, bounded by the marker's age, for a live announced auto-arm before blocking. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…sh. The failure came from the PR's own new test, not from the guard fix itself. After the change, all 72 tests in tests/fm-turnend-guard.test.sh pass locally. I have not re-run CI. **What failed:** CI (portable serial shard 4) failed on `--claude must still block when the starting auto-arm stood down without claiming: expected exit 2, got 0`. **Why:** That test puts a second fake Claude session in the home lock by running `other-claude -c 'sleep 60'`. For a one-command script like that, bash replaces itself with `sleep`. So the lock pid then shows up as `sleep 60` instead of a Claude-named process. The auto-arm's check (`fm_harness_pid_alive`) treats that as a stale lock owner, takes over the lock and claims the home, so the guard correctly allows the stop (exit 0). The test only blocked when the auto-arm checked the lock before bash made that switch. That is a timing race: it passed locally, but on the loaded CI runner it went the other way and waited out the full 15s start window. **Fix:** The fake session now runs `'sleep 60; :'` instead. The trailing `:` stops bash from replacing itself with `sleep`, so the lock pid stays a live Claude-named process. The auto-arm now deterministically stands down, and the test checks what it was meant to: the guard still blocks. No production code changed
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
Fix a race in firstmate's Claude primary turn-end supervision. Under the Claude harness, after almost every handled wake the Stop-hook guard (bin/fm-turnend-guard.sh --claude, registered in .claude/settings.json alongside bin/fm-claude-stop-autoarm.sh) printed a false 'TURN WOULD END BLIND - SUPERVISION IS OFF ... Stop-owned auto-arm did not claim this home either' alarm although the auto-arm was already arming the watcher (epoch outcome=arming, fresh watcher). Root cause reproduced and confirmed: Claude starts both Stop hooks concurrently; the auto-arm's pre-claim work (harness ancestry resolution, a chain of ps forks, seconds under load) outlasts the guard's ~800ms claim window, so the claim lands after the guard blocks. Required: reproduce the race with a test harness starting the two hooks concurrently, fix so the guard recognizes an auto-arm that is starting for this home (bounded wait / early announcement per docs/turnend-guard.md and docs/watcher-continuity.md), while preserving the guard's real safety (still block when no recovery is actually under way), add colocated regression tests, update owning docs. Decisions made: the auto-arm publishes a per-pid marker state/.claude-autoarm-starting. right after its scope check and removes it once its autoarm claim role is published (or on any earlier exit); the guard waits past the claim window while a live-pid marker younger than FM_CLAUDE_AUTOARM_START_WAIT seconds (default 15) exists, sampling the marker before the ownership check; dead/expired markers and an auto-arm that stands down still end in the normal block. Also investigated the guard's '16 task(s) in flight' count: it counts every state/*.meta, including finished-but-not-torn-down tasks; that is stale records reconciled separately, not a count bug, so deliberately left unchanged. Docs updated: docs/turnend-guard.md, docs/watcher-continuity.md, AGENTS.md state list.
What Changed
bin/fm-claude-stop-autoarm.shnow writes a per-pid marker,state/.claude-autoarm-starting.<pid>, right after its scope check. It removes the marker once it has published its autoarm claim role, and also on any earlier exit.bin/fm-turnend-guard.sh --claudechecks for that marker before its ownership check. If the marker's pid is alive and the marker is younger thanFM_CLAUDE_AUTOARM_START_WAITseconds (default 15), the guard keeps waiting past its usual claim window. The normal "TURN WOULD END BLIND" block still fires when the marker's pid is dead, the marker has expired, or the auto-arm stands down.tests/fm-turnend-guard.test.shandtests/fm-claude-stop-autoarm.test.sh. Updateddocs/turnend-guard.md,docs/watcher-continuity.mdand the state list inAGENTS.mdto cover the new marker.Risk Assessment
✅ Low: This is a small, bounded fix. The marker is sampled before the ownership check, which avoids the race with role publication. The extra wait is capped by the marker's age and a live pid. Every exit path in the auto-arm cleans up the marker, and the new tests exercise real behavior.
Testing
I ran the guard and auto-arm test files at HEAD and both pass, including the new tests that start the two hooks concurrently. I then put the pre-fix bin scripts back and reran the guard tests. The slow-starting auto-arm test fails there with the false-alarm block (exit 2), so the tests do reproduce the race. The worktree was restored and is clean.
Evidence: Guard tests at HEAD (fix)
Source: Guard tests at HEAD (fix)
Evidence: Guard tests on pre-fix scripts (race reproduced)
Source: Guard tests on pre-fix scripts (race reproduced)
not ok - --claude must not raise a blind-turn alarm while a slow auto-arm is still starting: expected exit 0, got 2Evidence: Auto-arm tests at HEAD
Source: Auto-arm tests at HEAD
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.
bash tests/fm-turnend-guard.test.shat HEAD: all pass, including the 4 new race tests (slow start recognised, auto-arm stands down still blocks, dead or expired markers ignored, bounded wait then blocks)bash tests/fm-claude-stop-autoarm.test.shat HEAD: all pass (checks the per-pid starting marker)Checked the regression:git checkout 1685d63 -- bin/fm-turnend-guard.sh bin/fm-claude-stop-autoarm.shand rerantests/fm-turnend-guard.test.sh. The slow-start test fails with exit 2, the false blind-turn block. Restored the bin files to HEAD afterwards.✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.