Skip to content

fix(bin): recognize launchd-reparented Claude sessions - #9

Merged
Hozzy02 merged 2 commits into
mainfrom
fm/fm-autoarm-reparented-ancestry
Sep 16, 2026
Merged

Hozzy02 merged 2 commits into
mainfrom
fm/fm-autoarm-reparented-ancestry

Conversation

@Hozzy02

@Hozzy02 Hozzy02 commented Sep 16, 2026

Copy link
Copy Markdown
Owner

Intent

Make firstmate's Claude session-lock identity check recognize its own session when Claude Code's bg-pty-host worker has been reparented to launchd, so the Claude Stop auto-arm (bin/fm-claude-stop-autoarm.sh) stops going silently inert, without weakening the refusal for a genuinely different live session.

Observed defect (2026-09-15, live on this machine): state/.lock in the primary home named a live claude pid (20434) that was the only firstmate primary on the machine. Its bg-pty-host/bg-spare worker chain had been reparented to launchd (ppid 1) after the owning bg-daemon restarted, so fm_harness_ancestry_pids in bin/fm-session-lock-lib.sh stopped at pid 1 and never reached the outer session pid, so fm_session_lock_owned_by_self failed closed. Consequence: the Stop auto-arm treated the lock as another session's and never armed for about 24 hours.

Fix: reproduced the failure with a fake process tree, then confirmed the real shape on this machine via read-only ps inspection (a live 'claude bg-pty-host' process with ppid 1 was found live on this machine, matching the incident exactly). Evaluated several candidate identity proofs empirically (Stop hook payload's session_id, an env var Claude Code exports to hooks, the pty host's daemon/socket relationship) by instrumenting real live one-shot 'claude -p' sessions with SessionStart/PreToolUse/Stop hooks. Found that Claude Code exports CLAUDE_PID to every hook and tool-call child, naming the true top-level session pid, stable and identical across SessionStart/PreToolUse/Stop within one session, and exactly matching what the existing ps ancestry walk finds in the healthy (non-reparented) case - so it is a structural, harness-provided signal (not a rendered/vendor string) that is immune to the ppid change reparenting causes.

Changed bin/fm-session-lock-lib.sh's fm_harness_ancestry_pids to widen a genuinely still-extending Claude ancestry (one that already found a live claude match but hit the ps walk's gap) with CLAUDE_PID, but only once independently confirmed alive and harness-shaped via the existing fm_harness_pid_alive predicate - so a stray or stale CLAUDE_PID inherited from an unrelated shell can never manufacture ownership on its own, and a lock genuinely held by a different live session is still refused exactly as before. This is the single shared library used by bin/fm-lock.sh (acquire/status), the Stop auto-arm, and bin/fm-turnend-guard-cursor.sh, so every caller is kept consistent; bin/fm-turnend-guard.sh --claude cooperates with the auto-arm's own epoch/lock state rather than resolving identity itself, so it needed no direct change.

Hard constraints honored: never signaled, killed, restarted, or modified any running claude process, the primary home's state/.lock, or its watcher; never ran bin/fm-claude-stop-autoarm.sh, bin/fm-watch-arm.sh, or bin/fm-lock.sh against the primary home /Users/randalwade/Repos/firstmate; all reproduction used temporary homes, fixtures, and isolated lab directories under a scratchpad or throwaway git worktrees. Did not change the auto-arm hook's multi-hour timeout (out of scope). A lock held by a different live session is still refused, and unresolvable/ambiguous identity still fails closed - both covered by new tests.

Testing added, following the firstmate-coding-guidelines 'Harness-dependent checks' rule (this verdict comes from Claude Code's process names/env, so it needs a portable regression plus live-harness proof and a dated verification record): tests/fm-session-lock-ancestry.test.sh gained a unit case (fake process table) and three real-process e2e cases via a genuinely orphaned bg-pty-host/bg-spare chain - CLAUDE_PID naming the true session claims ownership and arms, no CLAUDE_PID reproduces the original defect as inert (not mis-owned, not corrupting the lock), and CLAUDE_PID naming a different live claude pid still refuses ownership. tests/lib.sh now unsets CLAUDE_PID at suite start, because this test suite is itself frequently run from inside a real live Claude Code session whose own CLAUDE_PID would otherwise leak into fixtures that deliberately fake a different session identity. A new opt-in live-harness-optin test, tests/fm-claude-pid-identity-live-e2e.test.sh (gated on FM_CLAUDE_LIVE_E2E=1, self-skipping otherwise), proves against the real installed Claude Code that CLAUDE_PID reaches SessionStart/PreToolUse/Stop hook children with a stable value; it was run twice live and passed both times. It is deliberately a separate, narrower test from the existing tests/fm-claude-stop-autoarm-live-e2e.test.sh, because that suite's multi-turn rapid-rewake cycle count proved sensitive to live model turn-taking and Claude Code version drift unrelated to this identity signal (observed during this work, on the currently installed 2.1.272, independent of this change - reproduced identically against the pre-edit baseline library) - entangling the two would let that unrelated flakiness mask a real regression in this fix. docs/verification/supervision.md is the single owner of this identity contract's description and dated verification evidence (established via existing cross-references from docs/scripts.md and docs/watcher-continuity.md); it was updated in place with the CLAUDE_PID fallback description and a new dated 2026-09-15 verification entry with exact commands and observed output, rather than duplicating the contract into docs/turnend-guard.md or docs/watcher-continuity.md, which only reference it and do not own it.

Verification performed: tests/fm-session-lock-ancestry.test.sh, tests/fm-claude-stop-autoarm.test.sh, tests/fm-turnend-guard.test.sh, and tests/fm-watcher-lock.test.sh all pass cleanly (re-run multiple times for reliability, including after ruling out unrelated machine-load-induced flakiness in tests/fm-session-start.test.sh and tests/fm-startup-network.test.sh, which passed cleanly on direct reruns once system load settled - those two suites are unrelated to this change's code paths). bin/fm-lint.sh passes with no findings.

What Changed

  • Extend Claude session ancestry only when a verified Claude worker chain terminates at launchd/init, using a live harness-shaped CLAUDE_PID to recover the owning session without weakening fail-closed lock checks.
  • Add deterministic and live-harness regression coverage for valid, missing, stale, and different-session CLAUDE_PID values, while preventing ambient Claude identity from leaking into test fixtures.
  • Document the reparented bg-pty-host identity contract and its live verification evidence.

Risk Assessment

✅ Low: Captain, the corrected change is narrowly bounded to the verified PID-1 reparenting boundary, preserves fail-closed behavior for every other ancestry termination, and adds regression coverage for both accepted and refused paths.

Testing

No separate baseline command was supplied to this phase; the focused public-interface regression and live Claude 2.1.272 harness check passed, directly demonstrating successful reparented auto-arm, the absent-signal counterfactual, different-session refusal, and stable hook identity. This is a CLI/process change, so reviewer-visible command transcripts are the appropriate evidence rather than screenshots.

Evidence: Reparented auto-arm evidence

Reparented chain armed with the correct CLAUDE_PID, stayed inert without it, and refused a different live Claude session.

ok - session-lock: a version-named Claude Code session is identified from its install path and argv[0]
ok - session-lock: ordinary script paths under a harness directory are not harness processes
ok - session-lock: ownership stops at the first non-harness gap above the contiguous run
ok - session-lock: a live version-named session holding the lock is not mistaken for a stale owner
ok - session-lock: CLAUDE_PID recognizes this session's own lock across a reparented bg-pty-host gap, without weakening refusal for a different pid
ok - session-lock e2e: a version-named session claims the home and arms supervision
ok - session-lock e2e: a session parented by a harness-named daemon claims the home and arms supervision
ok - session-lock e2e: a version-named session under a harness-named daemon keeps its own lock
ok - session-lock e2e: a session reparented past bg-pty-host claims its home via CLAUDE_PID and arms supervision
ok - session-lock e2e: a reparented chain with no CLAUDE_PID reproduces the original defect (inert, not mis-owned)
ok - session-lock e2e: CLAUDE_PID naming a different live session still refuses ownership of a reparented chain
Evidence: Live Claude identity evidence

Claude 2.1.272 exported one stable CLAUDE_PID across SessionStart, PreToolUse, and Stop hooks.

ok - Claude 2.1.272 (Claude Code) live E2E: CLAUDE_PID=4812 (session_id=2e034278-a7b5-4d41-bb98-96921d45049e) reached SessionStart, PreToolUse, and Stop hook children with a stable value

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • 🚨 bin/fm-session-lock-lib.sh:143 - The required refusal across a non-harness gap is no longer guaranteed. After finding Claude, the walk can break on a non-harness parent at line 138 while extending remains 1; lines 143-150 then append an inherited live CLAUDE_PID. Concrete path: current shell -> Claude PID 900 -> bash PID 910 -> different Claude PID 920, with CLAUDE_PID=920 and the lock held by 920; ownership now returns true despite the gap. This contradicts the intent that “a lock genuinely held by a different live session is still refused” and that ambiguous identity fails closed. Admit the fallback only when the walk explicitly reaches PID 1 immediately above the matched Claude chain—not after a non-harness terminus, ps failure, or hop limit.

🔧 Fix: Restrict Claude fallback to launchd reparenting
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • tests/fm-session-lock-ancestry.test.sh | tee /var/folders/t1/8htjz3j91cqcfslff4w76zqc0000gn/T/no-mistakes-evidence/01M2K4XMFDVDGCCGKR47BPGKRS/session-lock-ancestry.txt
  • FM_CLAUDE_LIVE_E2E=1 tests/fm-claude-pid-identity-live-e2e.test.sh | tee /var/folders/t1/8htjz3j91cqcfslff4w76zqc0000gn/T/no-mistakes-evidence/01M2K4XMFDVDGCCGKR47BPGKRS/claude-pid-live-e2e.txt
  • git status --short and transient live-test directory inspection confirmed cleanup
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

state/.lock ownership was decided purely from a ps ancestry walk, which
Claude Code's own bg-pty-host can outrun: when its owning bg-daemon
restarts, the pty host and its bg-spare hook children keep running but
get reparented to launchd (ppid 1), opening a gap the walk cannot cross
back to the outer session pid. The Stop-owned auto-arm then treated its
own live session's lock as another session's and went silently inert.

fm_harness_ancestry_pids now widens a genuinely still-extending Claude
ancestry with CLAUDE_PID, the env var Claude Code sets in every hook and
tool-call child to name its own top-level session pid - unaffected by
the ppid change reparenting causes, and only trusted once the walk has
already proven a live claude ancestor and the named pid is independently
alive and harness-shaped. A different live session's lock is still
refused.

Portable regressions cover the reparented chain (with and without
CLAUDE_PID) and the competing-session refusal against both a fake
process table and a genuinely orphaned real process tree. A new opt-in
live test and a dated verification record confirm the env var against
the real installed Claude Code.
@Hozzy02
Hozzy02 merged commit 6ffd781 into main Sep 16, 2026
13 checks passed
@Hozzy02
Hozzy02 deleted the fm/fm-autoarm-reparented-ancestry branch September 26, 2026 00:41
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