Repository navigation
fix(bin): bound the Claude auto-arm's consecutive failures and own each turn end once - #3163
Valentino-Sole wants to merge 7 commits into
Conversation
…once The Stop-owned auto-arm had no bound on CONSECUTIVE failed re-arm attempts. FM_CLAUDE_AUTOARM_ATTEMPTS bounds retries inside one firing, and the notified and alarmed markers only dedupe one operator notice and the one attended fail-open. An exhausted failure therefore exited 2 on every later firing indefinitely whenever the synchronous guard never reached its attended fail-open, because it is not registered, has no jq, or stood down for another host: the session re-woke itself with no bound and no further record. Give the hook its own durable cap. fm_autoarm_failure_record counts consecutive exhausted failures in state/.claude-autoarm-failure-count, keyed by the auto-arm generation so one generation counts at most once, written under the existing single-flight micro-mutex with the same ownership re-verification as every other ledger write. At FM_CLAUDE_AUTOARM_FAILURE_CAP (default 3) the hook creates the durable stop record state/.claude-autoarm-failure-capped naming the reason, delivers that reason once on its one exit 2, and stops re-arming. Later firings in the same episode start no arm and create no continuation, but still record failed-capped for their own generation: the guard accounts its bounded progression per epoch identity, so a frozen ledger would leave it blocking the same identity forever instead of reaching the one attended fail-open the cap hands the episode over to. Positive watcher recovery ends the episode, and the hook applies that reset itself, so a home whose synchronous guard never runs is not left permanently un-armed by its own cap. A Pi primary can also run its model turns through a nested claude subprocess in this same checkout. Where that subprocess loads project settings, both tracked Stop entries fire inside it while .pi/extensions/fm-primary-turnend-guard.ts is already guarding the same logical turn end from agent_settled, so one turn end gets two independent guards that can each block or re-arm. Make ownership explicit instead: fm_turnend_owner_is_pi_primary reuses the durable evidence the pull guard already trusts - both Pi primary extensions recorded at their current on-disk builds by the live process holding the session lock - and the two Claude-shaped entries stand down for that proven owner. fm-turnend-guard.sh defers in --claude mode only, because the owning extension is itself the caller that runs it with no flags. The claim is a positive proof of an owner, never an assumption of one, so a missing, unloaded, drifted, or crashed owner leaves the Claude entries protecting the turn end exactly as before, and plain claude, plain pi, and Pi on any other provider each keep exactly one working guard. Ownership is deliberately not an environment sniff: Pi exports its variables into every child, so that would also disable a Claude session started by hand from a Pi pane, the GROK_SESSION_ID hazard already recorded in the doc.
Both new turn-end ownership suites had added record_pi_extension_session verbatim, differing only in whether they also claimed state/.lock. Both encode the same off-source contract - the extension-to-marker pairing, the two-line version and pid marker format, and the drift sentinel - so one copy could be updated for a marker-format change while the other silently kept passing against a stale shape. Lift it into tests/lib.sh as fm_record_pi_extension_session, beside the existing cross-suite fixture builders, with the session-lock write behind an optional argument, and have both suites call it. Non-functional: no test changes what it asserts. tests/fm-guard-stale-banner.test.sh keeps its own older helper of the same shape, which takes a different signature over its case_home and case_root fixtures and predates this change.
Confidence Score: 4/5The PR should not merge until Claude stand-down is tied to the Pi-owned logical turn rather than every Claude Stop occurring in a Pi-owned home. A live Pi session's home-wide markers cause both Claude protection layers to exit for an independently launched Claude session, even though Pi's agent_settled guard does not own that session's turn. Files Needing Attention: bin/fm-claude-stop-autoarm.sh, bin/fm-turnend-guard.sh, bin/fm-wake-lib.sh Reviews (1): Last reviewed commit: "no-mistakes(document): document auto-arm..." | Re-trigger Greptile |
| # guard already owns the same logical turn end. Stand down for that proven owner | ||
| # only; without the proof this hook protects the turn end exactly as before | ||
| # (fm_turnend_owner_is_pi_primary in bin/fm-wake-lib.sh owns the contract). | ||
| fm_turnend_owner_is_pi_primary "$STATE" "$FM_ROOT" && exit 0 |
There was a problem hiding this comment.
If a user starts an independent Claude session while a live Pi primary owns the same home, this home-wide predicate makes both Claude protection entries stand down even though Pi's agent_settled guard does not own that session's turn, allowing the independent turn to end without watcher recovery or a guard block.
Summary
Hardens the primary turn-end protection layer in three ways, plus the regressions that pin them.
1. The Claude Stop auto-arm gets its own bound on consecutive failed re-arms.
FM_CLAUDE_AUTOARM_ATTEMPTSbounded retries inside one firing, and the notified/alarmed markers only dedupe one operator notice and the one attended fail-open. An exhausted failure therefore exited 2 on every later firing indefinitely whenever the synchronous guard never reached its attended fail-open (not registered, nojq, or stood down for another host): the session re-woke itself with no bound and no further record.fm_autoarm_failure_recordnow counts consecutive exhausted failures instate/.claude-autoarm-failure-count, keyed by the auto-arm generation so one generation counts at most once, under the existing single-flight micro-mutex. AtFM_CLAUDE_AUTOARM_FAILURE_CAP(default 3) the hook writes the durable stop recordstate/.claude-autoarm-failure-capped, delivers the reason once, and stops re-arming. The count is genuinely consecutive: a successful arm clears it, including the ACTIONABLE close that is this model's ordinary success. Both records are session-scoped, and the session key falls back to the harness pid instate/.lockwhenjqis unavailable, so a new session never inherits a previous session's cap.Capping stops the re-arming, not the escalation: capped firings still record
failed-cappedper generation, so the guard's per-epoch progression still reaches its one loud attended fail-open, and positive watcher recovery ends the episode from the hook itself.2. Exactly one protection layer owns each logical turn end.
A Pi primary can run its model turns through a nested
claudesubprocess in this same checkout. Where that subprocess loads project settings, both trackedStopentries fire inside it while.pi/extensions/fm-primary-turnend-guard.tsalready guards the same logical turn end fromagent_settled.fm_turnend_owner_is_pi_primarymakes ownership explicit, reusing the durable proof the pull guard already trusts. Both Claude-shaped entries stand down for that proven owner;bin/fm-turnend-guard.shdefers in--claudemode only, because the owning extension is itself the caller that runs it with no flags. The claim is a positive proof of an owner and never an assumption of one, so a missing, unloaded, drifted or crashed owner leaves the Claude entries protecting the turn end exactly as before.The proof no longer trusts a bare pid:
fm_pid_identityis recorded in the extension markers and recomputed on every check, exactly asfm_autoarm_claim_openalready does for this failure mode, so a SIGKILLed Pi whose pid is later reused can never pass as a live owner. The identity is mandatory and a marker without one fails closed. The extensions obtain it by calling this repo's ownfm_pid_identityrather than re-implementing a per-platform format that would silently drift.Ownership is deliberately not environment detection: Pi exports its variables into every child, so an env guard would also disable a Claude session started by hand from a Pi pane — the
GROK_SESSION_IDhazard already recorded indocs/turnend-guard.md.3. Documentation.
docs/turnend-guard.mdgains a Turn-end ownership section and owns both contracts;docs/watcher-continuity.mdonly cross-references it.Test plan
tests/fm-claude-stop-autoarm.test.sh— 46 pass, 0 failtests/fm-turnend-guard.test.sh— 75 pass, 0 failtests/fm-guard-stale-banner.test.sh— 27 pass, 0 failtests/fm-session-start.test.sh— 49 pass, 0 failbin/fm-lint.sh— ShellCheck 0.11.0 (pinned), full extended analysis, cleanNew regressions, each written to fail against the previous behaviour:
FM_CLAUDE_AUTOARM_FAILURE_CAP=1)jqNotes
.squish/was added to the tracked.gitignore: agent-local MCP memory databases must never be committed, and a per-clone.git/info/excludedoes not protect other clones.state/.pi-watch-extension-loadedandstate/.pi-turnend-extension-loadedare still absent from theAGENTS.mdstate inventory. That is a pre-existing gap, and deleting either marker fails closed (the ownership proof fails, so both Claude layers keep protecting), unlike the new cap records which were added because deleting the stop record silently un-caps an episode.