Repository navigation
fix(bin): recover Claude auto-arm from hung claims - #3156
Merged
Merged
Conversation
On a Claude primary, a Stop-hook auto-arm process that hung mid-arm held the single-flight owner lock with its epoch ledger frozen at outcome=arming, and the abandonment proof read any live lock holder in arming as legitimately deciding forever. Every later Stop firing exited 0 at the lock, the turn-end guard kept deferring to the hung owner as recovery under way, and the watcher was never auto-re-armed again for the rest of the session - supervision survived only on manual arms and lapsed between them (the 2026-08-26 watcher flap). Corrections layered onto the lock-held-across-arm shape each reopened the same concurrency class one level down, so this replaces the claim machinery wholesale with a generation-based optimistic design: - The epoch ledger's monotonic sequence IS the claim generation; the two-line entry (classic epoch record plus the claimant's MANDATORY pid-identity) is the claim. Every firing defers to a live OPEN claim: outcome arming, owner alive, identity recomputes and matches, and not stuck (entry and watcher beacon both older than the guard grace). - A finished, dead, identity-mismatched, identityless, or stuck claim is superseded by simply taking the next generation - no signalling or revocation of a steady-state predecessor. - No mutex is held across arming or output; the owner lock survives only as a micro-mutex around individual ledger writes. A superseded owner goes completely silent: ownership is re-verified before every arm invocation, episode-state mutation, ledger write, and continuation. - The irrevocable commit point of a translation is the exit status (the harness delivers the collected stderr only on exit 2), so the owned terminal ledger write is the atomic commit: the winning generation exits 2 unconditionally after it, a refused one exits 0 silently even after printing, and the once-per-episode failure notice commits in the same owned critical section as the winning failed write. Two bounded residuals are documented accepted intent: an owner dying between its owned write and its own exit, and a hung old-build owner resuming during the one legacy upgrade window. - The pre-generation lock-holding claim shape keeps defer-or-reclaim behavior through a legacy shim: a live identity-verified stuck owner is retired via TERM (with a queued TERM sufficient when the owner is stopped) before its lock is removed, an unverified or identityless pid is never signalled but never blocks a proven-abandoned reclaim, and the lock's identity evidence is grafted into the ledger (mtime-preserving) so pid-reuse protection survives the lock. - The guard reads the same predicates for recovery ownership and its terminal fail-open (which re-checks for a live open claim under the held locks before committing the attended alarm), with ledger reads anchored to line 1 so the identity line can never confuse them. Behavioral regression coverage exercises all three edge classes through the real hook and guard - a live open claim defers with no lock held, a stuck claim is superseded and the home re-arms, and an end-to-end run with a genuinely hung owner shows a concurrent firing deferring promptly mid-arm, a later firing superseding the stuck owner, and the superseded owner exiting silently without a second translation - plus the identityless/reused-pid loopholes, the superseded-owner arm boundary, and the legacy TERM, SIGSTOP, and signal-free reclaim paths.
Confidence Score: 5/5The PR appears safe to merge with no concrete changed-code defect identified. Generation ownership is revalidated at shared-state commit boundaries, the guard serializes its terminal decision against claim publication, and legacy process signaling requires a matching live PID identity. Reviews (1): Last reviewed commit: "no-mistakes(document): Update auto-arm g..." | Re-trigger Greptile |
This was referenced Aug 27, 2026
prandelicious
added a commit
to prandelicious/firstmate
that referenced
this pull request
Aug 29, 2026
Intent
Find and fix the fundamental cause of the Claude Stop-hook watcher flap in Firstmate: a hung auto-arm hook (bin/fm-claude-stop-autoarm.sh) held the single-flight lock state/.claude-autoarm.lock with its epoch ledger frozen at outcome=arming, and because the reclaim only fired on a terminal outcome or dead owner, every later Stop firing read the live hung owner as legitimate contention and exited 0, silently disabling all future auto-arms while the watcher lapsed between manual arms (the 2026-08-26 watcher flap). This branch is a CAPTAIN-AUTHORIZED CLEAN REBUILD from current main of the captain's accepted generation-based optimistic auto-arm redesign, superseding branch fm/fm-watcher-flap-autoarm-fix-r2 and PR #3150 (preserved read-only; firstmate closes the old PR after this one is green). The accepted design, all details deliberate and settled - none of the following are open questions: each auto-arm claim takes a monotonic generation (the epoch ledger state/.claude-autoarm-epoch's own sequence; the two-line entry - classic epoch record plus the claimant's MANDATORY pid-identity on line 2, read from the ledger alone and never substituted from any lock - IS the claim); no mutex is held across any blocking step (arming or output; the owner lock survives only as a micro-mutex around individual ledger writes with dead-holder self-reclaim); every firing defers to a live OPEN claim (outcome arming, owner alive, recorded identity successfully recomputes AND matches, not stuck - stuck means ledger entry AND watcher beacon state/.last-watcher-beat both older than FM_GUARD_GRACE default 300s; an identityless entry is never open); a finished, dead, identity-mismatched, identityless, or stuck claim is superseded by simply taking the next generation with NO revocation of a steady-state predecessor; a superseded owner goes COMPLETELY silent (cleanup only) - ownership is re-verified before every arm invocation, episode reset, failure-notice, alarm, budget, ledger write, and continuation; the irrevocable commit point of a translation is the EXIT STATUS (the harness delivers the collected stderr banner only on exit 2 and discards it on exit 0), so the owned terminal ledger write is the atomic commit - the winning generation exits 2 unconditionally after it with NO post-write ownership flip, a refused generation exits 0 silently even after printing, and the once-per-episode failure notice commits in the same owned critical section as the winning failed write; the guard (bin/fm-turnend-guard.sh --claude) reads the same predicates for recovery ownership and re-checks for a live open claim under its held locks before committing the attended fail-open alarm, with its ledger reads anchored to line 1. Deliberate accepted decisions: NO predecessor revocation and NO cross-output mutex in steady state; the upgrade-window-only legacy shim (fm_autoarm_claim_abandoned/fm_autoarm_release_abandoned, recognizing pre-generation lock-holding claims by their role file) retires a live identity-VERIFIED proven-stuck old-build owner via TERM before removing its lock - a successfully queued TERM suffices when the owner is stopped (delivery precedes any further user code on continue), an unverified or identityless pid is NEVER signalled, and missing identity evidence never blocks a proven-abandoned reclaim (it only disables the TERM and the mtime-preserving identity graft into the ledger); the stuck threshold reuses FM_GUARD_GRACE rather than adding a knob; TWO bounded residuals are documented accepted intent, each costing at most one extra exit-2 continuation turn absorbed by the durable idempotent wake queue: an owner that dies between its owned terminal write and its own process exit, and a hung old-build owner that resumes during the one legacy upgrade window; a claim misread as stuck in a pathological race (e.g. a beacon read right at system wake) costs at most one extra arm that the watcher singleton dedupes while the superseded owner still goes silent. Behavioral regression coverage is required and present for all three edge classes through the real hook and guard (open-claim deferral with no lock held; stuck-claim supersession and re-arm; an end-to-end genuinely-hung-owner run where a concurrent firing defers promptly mid-arm, a later firing supersedes, and the superseded owner exits silently without a second translation), plus the identityless/reused-pid loopholes, the superseded-owner arm-invocation boundary, and the legacy TERM, SIGSTOP-with-queued-TERM, and signal-free identityless reclaim paths; the legacy lock-shape tests are retained as upgrade-shim coverage. Changed bash stays macOS bash-3.2 compatible and shellcheck-clean; docs updated in their single-owner locations (docs/turnend-guard.md, docs/watcher-continuity.md).
What Changed
Risk Assessment
✅ Low: The generation-based recovery and follow-up fixes preserve the required ownership, lock ordering, refusal, and silent-supersession invariants with no substantiated remaining defect.
Testing
Reproduced the original hung-owner flap on the base commit, then verified the target through both focused behavior suites and an end-to-end hook transcript showing lock-free deferral, generation supersession and re-arm, exactly-once marker recovery, and silent superseded-owner cleanup; the worktree remained clean.
Evidence: Baseline hung-owner watcher-flap reproduction
Source: Baseline hung-owner watcher-flap reproduction
Evidence: Focused end-to-end auto-arm behavior and persisted ledger evidence
Source: Focused end-to-end auto-arm behavior and persisted ledger evidence
Evidence: Claude Stop auto-arm regression transcript
Source: Claude Stop auto-arm regression transcript
Evidence: Turn-end guard regression transcript
Source: Turn-end guard regression transcript
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 1 issue found → auto-fixed (2) ✅
bin/fm-wake-lib.sh:1198- Intent requires that “the once-per-episode failure notice commits in the same owned critical section as the winning failed write,” but marker creation explicitly ignores failure (: > "$marker" ... || true) and still returns success. If the marker cannot be created (for example, it is a dangling symlink), the hook commitsoutcome=failed, delivers exit 2 and the notice, yet leaves no marker; the next firing can deliver the notice again. Treat marker-write failure as a refused commit so that generation exits 0 and a successor retries.🔧 Fix: Refuse auto-arm commits when notice marker creation fails
1 error still open:
bin/fm-claude-stop-autoarm.sh:266- This contradicts the required invariant that a superseded owner “goes COMPLETELY silent (cleanup only).” The ownership check andfm_failure_episode_resetare not atomic. Concrete path: an old long-running claim observes a beacon at age 299s and passes line 262; after the beacon crosses the 300s grace, a successor supersedes it, fails, and commits its notice/alarm/budget state; the old process then resumes at line 266 and deletes that successor state, allowing the episode notice to repeat. The comment that a fresh beacon makes supersession impossible is only momentarily true. Perform the generation check and reset while holding the owner micro-mutex, acquiring the budget lock second, so a successor cannot publish between verification and mutation.🔧 Fix: Make episode reset atomic with generation ownership
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
git diff --stat 524994c8b483513eababb4e791b8b06b3b4ec974..HEADand targeted implementation/test diff inspectionBaseline hung-owner regression against commit524994c8b483513eababb4e791b8b06b3b4ec974using the executable hook test harnessbash tests/fm-claude-stop-autoarm.test.shbash tests/fm-turnend-guard.test.shFocused executable-hook scenarios for open-claim deferral, stuck-claim supersession, failure-marker refusal/retry, and a genuinely hung superseded ownergit status --shortand transient-artifact checks after testing✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.