Skip to content

fix(bin): translate Stop hook timeout signals into durable auto-arm failure - #4474

Merged
kunchenguid merged 2 commits into
kunchenguid:mainfrom
pablontiv:fm/fix-4368-autoarm-timeout
Sep 14, 2026
Merged

kunchenguid merged 2 commits into
kunchenguid:mainfrom
pablontiv:fm/fix-4368-autoarm-timeout

Conversation

@pablontiv

@pablontiv pablontiv commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Captain's intent: convert the incident-driven Firstmate messaging-reliability backlog into shipped fixes now. Fix GitHub issue #4368 in this fork and open a PR through the normal contribution flow, with exact issue linkage (the PR must close #4368).

Issue #4368 substance (observed on main at 269f8fe6; re-verify current line numbers before editing): the Claude Stop hook declares "timeout": 28800 and runs its auto-arm in the foreground, so the harness kills the whole process tree at eight hours. The arm logs exit_code=143 signal=TERM reason=arm-interrupted, never writes a terminal ledger outcome, and never exits 2 — and exit 2 is the only thing that delivers a rewake. With no rewake there is no turn, with no turn there is no Stop event, and with no Stop event there is no arm: nothing inside firstmate can restart it, no failure marker is written, no banner prints, and a person has to type into the session. Durable symptom: state/.claude-autoarm-epoch frozen mid-claim (owner_pid dead, outcome still arming), with neither .claude-autoarm-failure-notified nor .claude-autoarm-failure-alarmed present, sitting unchanged for 14152s across many turn ends in the measured instance. The issue author states: "I think this is the missing cause underneath #4332, and it is a different defect from #4300 and #4236."

What Changed

  • bin/fm-claude-stop-autoarm.sh: added HUP/TERM/INT traps (handle_autoarm_signal) so that when Claude kills the Stop hook's process tree at the configured timeout, the winning generation commits a terminal failed (or failed-suppressed) ledger outcome, writes the failure-notice marker, prints the rewake banner, and exits 2 — instead of leaving the epoch frozen at arming with no rewake.
  • tests/fm-claude-stop-autoarm.test.sh: added test_term_mid_arm_commits_failure_and_rewakes, which sends TERM to the foreground arm owner mid-run and asserts exit code 2, a durable failure marker, a failed ledger outcome, and the interrupted-arm banner.
  • docs/turnend-guard.md and docs/watcher-continuity.md: documented the new host-timeout signal handling and added it to the auto-arm test coverage summary.

Risk Assessment

✅ Low: The change is a well-scoped, self-contained addition (a signal trap plus a matching test and doc line) that reuses existing, already-tested primitives (fm_autoarm_write_owned's ownership re-check, the same failed/failed-suppressed/FAILURE_NOTICE/FAILURE_ALARM semantics used elsewhere in the script, and the same 'kill -TERM a foreground child' trap pattern already validated in fm-watch-arm.test.sh), covers the exact failure sequence described in issue #4368 (foreground 8h timeout kill leaving the ledger frozen at 'arming' with no exit 2), and ships a real behavioral regression test that fails without the fix and passes with it.

Testing

Ran the tracked regression suite plus targeted, self-removed scratch tests that drove the real fm-claude-stop-autoarm.sh script under a fake Claude harness with a slow arm fixture, sending TERM/HUP/INT mid-arm; also reproduced the pre-fix failure (exit 143, no marker) against base commit 80556bc to prove the new test is a genuine regression test. All derived scenarios passed; the one apparent INT anomaly traced to a bash job-control quirk in my own ad hoc reproduction, not a product defect, confirmed by re-driving via direct process spawn.

  • Live validation: ✅ go - 6 of 6 scenarios driven live against the product
Scenario Result Live Evidence
TERM mid-arm commits durable failure and rewakes (primary issue #4368 symptom) ✅ pass live Ran bash tests/fm-claude-stop-autoarm.test.sh (target commit) - test_term_mid_arm_commits_failure_and_rewakes passes: hook TERM'd mid-arm exits 2, writes .claude-autoarm-failure-notified, ledger…
Regression proof: same test fails without the fix (reproduces reported bug) ✅ pass live Extracted base commit 80556bc (via git archive) into a scratch dir, ran the target's new test against the pre-fix script: got `not ok - TERM mid-arm must preserve Claude's rewake-triggering hook ex…
TERM after attended alarm already consumed stays silent (must not restart automatic continuation) ✅ pass live Manual scratch test (tests/fm-claude-stop-autoarm.scratch.test.sh, run then removed) pre-seeded .claude-autoarm-failure-alarmed before TERM: hook exited 0, produced no stderr output, ledger outcom…
TERM after failure notice already sent still exits 2 without duplicating the notice banner ✅ pass live Same scratch run, pre-seeded .claude-autoarm-failure-notified before TERM: hook exited 2 (keeps the Stop-owned retry alive), ledger outcome=failed-suppressed, no duplicate 'INTERRUPTED' banner text.
HUP mid-arm behaves identically to TERM ✅ pass live Scratch parameterized test driving the real hook with kill -HUP: exit 2, outcome=failed, failure-notice marker written, INTERRUPTED banner present - matches TERM behavior.
INT mid-arm behaves identically to TERM/HUP when the hook is spawned the way the real host spawns it ✅ pass live A naive scratch test using bash cmd & backgrounding to send SIGINT initially looked like a failure (natural completion, no trap fired). Root-caused to a POSIX/bash rule that force-ignores SIGINT for…

Related Issues

Closes #4368

This PR leaves #4332 and #4439 unresolved.
It may resolve their shared root cause, but both issues require re-diagnosis after this change lands.

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ bin/fm-claude-stop-autoarm.sh:221 - handle_autoarm_signal() (bin/fm-claude-stop-autoarm.sh:221-236) duplicates the FAILURE_ALARM/FAILURE_NOTICE decision tree already implemented at the tail of the script (lines 331-380): check FAILURE_ALARM -> record failed-suppressed and exit 0; else if !FAILURE_NOTICE -> print banner, commit 'failed' with the marker, exit 2; else -> commit 'failed-suppressed', exit 2. This could be factored into one shared helper used by both the signal trap and the normal exhausted-failure path, reducing the chance the two copies drift.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 6 of 6 scenarios driven live against the product
Scenario Result Live Evidence
TERM mid-arm commits durable failure and rewakes (primary issue #4368 symptom) ✅ pass live Ran bash tests/fm-claude-stop-autoarm.test.sh (target commit) - test_term_mid_arm_commits_failure_and_rewakes passes: hook TERM'd mid-arm exits 2, writes .claude-autoarm-failure-notified, ledger…
Regression proof: same test fails without the fix (reproduces reported bug) ✅ pass live Extracted base commit 80556bc (via git archive) into a scratch dir, ran the target's new test against the pre-fix script: got `not ok - TERM mid-arm must preserve Claude's rewake-triggering hook ex…
TERM after attended alarm already consumed stays silent (must not restart automatic continuation) ✅ pass live Manual scratch test (tests/fm-claude-stop-autoarm.scratch.test.sh, run then removed) pre-seeded .claude-autoarm-failure-alarmed before TERM: hook exited 0, produced no stderr output, ledger outcom…
TERM after failure notice already sent still exits 2 without duplicating the notice banner ✅ pass live Same scratch run, pre-seeded .claude-autoarm-failure-notified before TERM: hook exited 2 (keeps the Stop-owned retry alive), ledger outcome=failed-suppressed, no duplicate 'INTERRUPTED' banner text.
HUP mid-arm behaves identically to TERM ✅ pass live Scratch parameterized test driving the real hook with kill -HUP: exit 2, outcome=failed, failure-notice marker written, INTERRUPTED banner present - matches TERM behavior.
INT mid-arm behaves identically to TERM/HUP when the hook is spawned the way the real host spawns it ✅ pass live A naive scratch test using bash cmd & backgrounding to send SIGINT initially looked like a failure (natural completion, no trap fired). Root-caused to a POSIX/bash rule that force-ignores SIGINT for…
  • bash tests/fm-claude-stop-autoarm.test.sh on target commit 86561e5 (all 41 cases pass, including new test_term_mid_arm_commits_failure_and_rewakes)
  • Same test file run against base commit 80556bca via git archive extraction - reproduces exit 143 failure, confirming genuine regression coverage
  • Scratch-appended adversarial tests (post-alarm TERM, post-notice TERM, HUP mid-arm) run against the target script live, then removed from the worktree
  • Direct python3 subprocess.Popen spawn of the hook with SIGINT sent mid-arm, to isolate a bash &-backgrounding job-control artifact from real hook behavior
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.