Skip to content

fix(bin): bound the Claude auto-arm's consecutive failures and own each turn end once - #3163

Open
Valentino-Sole wants to merge 7 commits into
kunchenguid:mainfrom
Valentino-Sole:fm/fundament-autoarm-haerten
Open

Valentino-Sole wants to merge 7 commits into
kunchenguid:mainfrom
Valentino-Sole:fm/fundament-autoarm-haerten

Conversation

@Valentino-Sole

Copy link
Copy Markdown

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_ATTEMPTS bounded 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, no jq, or stood down for another host): the session re-woke itself with no bound and no further record.

fm_autoarm_failure_record now counts consecutive exhausted failures in state/.claude-autoarm-failure-count, keyed by the auto-arm generation so one generation counts at most once, under the existing single-flight micro-mutex. At FM_CLAUDE_AUTOARM_FAILURE_CAP (default 3) the hook writes the durable stop record state/.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 in state/.lock when jq is 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-capped per 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 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 already guards the same logical turn end from agent_settled.

fm_turnend_owner_is_pi_primary makes 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.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 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_identity is recorded in the extension markers and recomputed on every check, exactly as fm_autoarm_claim_open already 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 own fm_pid_identity rather 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_ID hazard already recorded in docs/turnend-guard.md.

3. Documentation. docs/turnend-guard.md gains a Turn-end ownership section and owns both contracts; docs/watcher-continuity.md only cross-references it.

Test plan

  • tests/fm-claude-stop-autoarm.test.sh — 46 pass, 0 fail
  • tests/fm-turnend-guard.test.sh — 75 pass, 0 fail
  • tests/fm-guard-stale-banner.test.sh — 27 pass, 0 fail
  • tests/fm-session-start.test.sh — 49 pass, 0 fail
  • bin/fm-lint.sh — ShellCheck 0.11.0 (pinned), full extended analysis, clean

New regressions, each written to fail against the previous behaviour:

  • a capped firing's open arming claim never allows a blind stop — the concurrent same-Stop-event case the earlier sequential tests could not see
  • a capped auto-arm stops retrying while the guard still reaches one attended fail-open
  • a cap reached before any failure notice still reaches one attended fail-open (FM_CLAUDE_AUTOARM_FAILURE_CAP=1)
  • a new session gets an honest arm attempt after a previous session's cap, and the same without jq
  • positive watcher recovery ends a capped episode and re-arms
  • with both layers present exactly one blocks per logical turn end, and the standing-down entry consumes no block budget
  • an absent, crashed, or version-drifted owner leaves no protection gap
  • a reused pid does not prove ownership

Notes

  • .squish/ was added to the tracked .gitignore: agent-local MCP memory databases must never be committed, and a per-clone .git/info/exclude does not protect other clones.
  • Deliberately deferred: state/.pi-watch-extension-loaded and state/.pi-turnend-extension-loaded are still absent from the AGENTS.md state 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.

…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.
@greptile-apps

greptile-apps Bot commented Aug 27, 2026

Copy link
Copy Markdown

Confidence Score: 4/5

The 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Turn ownership is overbroad

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant