Skip to content

fix(bin): keep restored second mates supervised on their own home - #8

Merged
babbarc merged 8 commits into
mainfrom
fm/secondmate-reboot-supervision-gap
Sep 19, 2026
Merged

babbarc merged 8 commits into
mainfrom
fm/secondmate-reboot-supervision-gap

Conversation

@babbarc

@babbarc babbarc commented Sep 14, 2026 •

Copy link
Copy Markdown
Owner

Intent

A persistent second mate can come back alive but unsupervised after a host restart, and the fleet does not notice because the agent process genuinely is running. Three occurrences:

  • 2026-09-07 and 2026-09-12 (host reboots). The restored session's agent came back alive and idle but never retook its session lock (state/.lock still named the dead pre-reboot pid), never refreshed .session-start-complete, and never re-armed its watcher - so nothing polled its own checks and its ~3-day fork-sync cadence silently stopped. The main fleet's secondmate liveness sweep did not catch it, because the endpoint read alive (it was).
  • 2026-09-13, not a reboot. The captain restarted pi and herdr around 20:23. The restored herdr server relaunched the bosun pane with FM_HOME=~/firstmate (the MAIN home) still in its environment, inherited from the shell that started the server, while bosun's code root stayed the bosun home. Its watcher could not arm and its turn-end guard evaluated the main home - the same alive-but-unsupervised shape arriving through the pane launcher rather than a reboot. A fix keyed only to reboot paths would miss it.
  • 2026-09-13, ~2 hours unsupervised. After another restart the same leak recurred: the main home became bosun's session, it came back read-only with no watcher, and it was unsupervised for roughly two hours until a relaunch on its own home. This recurred the same day the second occurrence was recorded, so it returns on every restart until a tracked fix lands.

Recovery each time was bin/fm-control.sh <id> relaunch, which sets FM_HOME explicitly for the replacement and reclaims the lock.

Two questions to answer: (1) why does the main home's session take the helm after a restart while the second mate's restored session does not; (2) should the second mate liveness sweep also check the second mate's own supervision (lock ownership and watcher beacon) rather than only endpoint liveness, and if so, how, since endpoint-alive is exactly the misleading signal here.

Related learning: data/learnings.md, entries dated 2026-09-07 and 2026-09-13 on this leak.

What Changed

  • Added bin/fm-home-lib.sh and .pi/extensions/lib/fm-home-resolve.ts, a shared correction that makes a seeded second-mate home (identified by its .fm-secondmate-home marker) always resolve FM_HOME/FM_ROOT_OVERRIDE to its own root, ignoring a disagreeing inherited override; wired into bin/fm-guard.sh, bin/fm-lock.sh, bin/fm-session-start.sh, bin/fm-sessionstart-nudge.sh, bin/fm-sessionstart-run.sh, bin/fm-turnend-guard.sh, bin/fm-turnend-guard-cursor.sh, bin/fm-wake-lib.sh, bin/fm-claude-stop-autoarm.sh, bin/fm-bootstrap.sh, and the .pi/.omp watch and turn-end-guard extensions - this prevents a restored/relaunched pane from inheriting a launcher's FM_HOME and operating on the wrong home.
  • Added fm_supervision_home_verdict to bin/fm-supervision-lib.sh, a cross-home check that judges another home's supervision from the outside (live session-lock pid, matching .session-start-complete record, and fresh watcher beacon) instead of relying on endpoint liveness alone; the secondmate liveness sweep now emits SECONDMATE_LIVENESS: when a live endpoint's own home fails this check, not only when it's skipped or fails to relaunch.
  • Updated docs/configuration.md, docs/herdr-backend.md, docs/scripts.md, docs/sessionstart-nudge.md, AGENTS.md, and the bootstrap-diagnostics skill to document the new home-resolution correction and supervision verdict.
  • Added tests/fm-pi-home-leak.test.sh and extended fm-secondmate-liveness.test.sh, fm-secondmate-sync.test.sh, fm-sessionstart-nudge.test.sh, fm-omp-harness.test.sh, and other extension/watch tests to cover the new behavior; fixed several test fixtures (fm-extension-binding.test.sh, fm-gotmp.test.sh, fm-cursor-primary.test.sh) that built isolated bin/ copies missing the new fm-home-lib.sh dependency.

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The change is a well-bounded, consistently-applied fix: a shared marker-based home-correction helper (bin/fm-home-lib.sh and its TS mirror) is threaded through every entrypoint that resolves FM_HOME, the resume/reload/fork routing now checks actual lock ownership before deferring to the nudge, and a new cross-home supervision verdict lets the liveness sweep report alive-but-unsupervised secondmates instead of endpoint-only checks; all three failure modes described in the user intent (reboot, pane relaunch with leaked FM_HOME, and repeat leak) are covered by new, genuinely behavioral end-to-end tests that drive the real extension/scripts and assert observable state (lock/marker files, child env, digest output), and the previously-flagged missing fm-home-lib.sh fixture symlink was fixed as decided in the prior review round.

Testing

Ran the smallest relevant targeted tests for the secondmate-reboot-supervision-gap fix: the two new regression tests directly proving the user intent (fm-pi-home-leak.test.sh for the leaked-FM_HOME reboot/relaunch case, and three new alive-but-unsupervised cases in fm-secondmate-liveness.test.sh for lock/completion/beacon supervision) pass, plus all 20 other test files touched by this branch's diff pass (fm-secondmate-sync.test.sh and a 17-script batch covering the fixture-symlink fixes and related surfaces). I manually reproduced the exact fixture bug described in the already-decided review finding and confirmed the applied one-line fix resolves it. One unrelated test failure (a Herdr classifier unit test) surfaced but was confirmed via a base-commit checkout to be pre-existing and environment-specific, not caused by this branch.

Evidence: fm-home-lib.sh fixture bug repro (before/after)

Source: fm-home-lib.sh fixture bug repro (before/after)

# Repro: fm-sessionstart-hook-live-e2e.test.sh make_lab fixture, missing bin/fm-home-lib.sh

This reproduces exactly the failure described in the decided review finding
'sessionstart-hook-live-e2e-missing-home-lib-symlink': sourcing the real
bin/fm-startup-network.sh dependency chain (via fm-wake-lib.sh) from a fixture
bin/ directory that symlinks only the originally-listed libs (fm-timeout-lib.sh,
fm-wake-lib.sh, fm-session-lock-lib.sh) and omits bin/fm-home-lib.sh, which
bin/fm-wake-lib.sh unconditionally sources.

## BEFORE the fix (fm-home-lib.sh not symlinked into the fixture bin/)
$ bash -c 'set -u; . "/tmp/tmp.NTBC4x90qG/bin/fm-wake-lib.sh"'
/tmp/tmp.NTBC4x90qG/bin/fm-wake-lib.sh: line 7: /tmp/tmp.NTBC4x90qG/bin/fm-home-lib.sh: No such file or directory
/tmp/tmp.NTBC4x90qG/bin/fm-wake-lib.sh: line 8: fm_home_correct: command not found
/tmp/tmp.NTBC4x90qG/bin/fm-wake-lib.sh: line 9: FM_HOME_RESOLVED: unbound variable
(exit 127, matches the reported failure: 'No such file or directory' -> 'fm_home_correct: command not found' -> 'FM_HOME_RESOLVED: unbound variable')

## AFTER the fix (bin/fm-home-lib.sh symlinked in, as applied at tests/fm-sessionstart-hook-live-e2e.test.sh:125)
$ FM_HOME="/tmp/tmp.NTBC4x90qG" bash -c 'set -u; . "/tmp/tmp.NTBC4x90qG/bin/fm-wake-lib.sh"; echo "sourced ok, FM_HOME=$FM_HOME"'
sourced ok, FM_HOME=/tmp/tmp.NTBC4x90qG
(exit 0)
- Outcome: ⚠️ 1 warning across 1 run (9m40s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

⚠️ **Test** - 1 warning
  • ⚠️ tests/fm-secondmate-liveness.test.sh:191 - tests/fm-secondmate-liveness.test.sh's unit test "Herdr pane state unknown should map to unreadable" fails in this sandbox: it stubs fm_backend_herdr_pane_agent_state but not fm_backend_herdr_server_running_state, so the fallback path invokes the real installed herdr (0.9.0) CLI against a fictitious "sess" session, which answers definitively "stopped" (server.running=false) instead of erroring, causing the classifier to correctly report "missing" per its own documented contract rather than the test's expected "unreadable". I confirmed this is pre-existing and unrelated to this branch: bin/backends/herdr.sh and this exact test block are untouched by the diff, and the same failure reproduces identically when checked out at the base commit de532b5 before any of this branch's changes.
  • Manual repro: sourced bin/fm-wake-lib.sh from a scratch fixture dir missing bin/fm-home-lib.sh (reproduces the reported 'No such file or directory' -> 'fm_home_correct: command not found' -> 'FM_HOME_RESOLVED: unbound variable' failure), then repeated with fm-home-lib.sh symlinked in (sources cleanly) - confirms the fix applied at tests/fm-sessionstart-hook-live-e2e.test.sh:125
  • env -u FM_BACKEND ./bin/fm-test-run.sh tests/fm-pi-home-leak.test.sh tests/fm-secondmate-liveness.test.sh tests/fm-secondmate-sync.test.sh
  • env -u FM_BACKEND ./bin/fm-test-run.sh tests/fm-secondmate-liveness.test.sh (isolated rerun of the single failing test to rule out cross-test interference)
  • git worktree add /tmp/fm-base-check de532b5a2e34c02e7d0b49ae8e07b02e923e31f5 && env -u FM_BACKEND ./bin/fm-test-run.sh tests/fm-secondmate-liveness.test.sh (confirmed the same failure exists at the base commit, pre-existing and unrelated to this branch)
  • env -u FM_BACKEND ./bin/fm-test-run.sh tests/fm-afk-return.test.sh tests/fm-calm-pi-extension.test.sh tests/fm-claude-stop-autoarm.test.sh tests/fm-cursor-primary.test.sh tests/fm-extension-binding.test.sh tests/fm-gotmp.test.sh tests/fm-omp-harness.test.sh tests/fm-pi-branch-extension.test.sh tests/fm-pi-primary-types.test.sh tests/fm-pi-watch-extension.test.sh tests/fm-pi-windows-shell-invocation.test.sh tests/fm-remote-backlog-handoff.test.sh tests/fm-remote-transport-lanes.test.sh tests/fm-session-lock-ancestry.test.sh tests/fm-sessionstart-nudge.test.sh tests/fm-turnend-guard.test.sh tests/fm-watch-recovery-loop.test.sh (all 17 passed, 4 expected environment gate-skips: missing optional pi-coding-agent package, no native Windows Node)
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@babbarc babbarc changed the title fix(bin): detect and recover a second mate that comes back alive but unsupervised fix(bin): keep a restored second mate on its own home and detect lost supervision Sep 14, 2026
yeti added 8 commits September 14, 2026 09:31
…rvision

A persistent second mate could come back alive but unsupervised after a
host reboot or a backend-server restart: the restored pane kept the
launcher's FM_HOME (the main home) while its code root stayed the
second mate home, so its session start ran the wrong home's digest,
never retook its own lock, and never re-armed its watcher - and the
liveness sweep saw nothing because the agent process was genuinely alive.

- Resolve a seeded second mate home from its own .fm-secondmate-home
  marker in both layers: bin/fm-home-lib.sh for the shell session-open,
  lock, watcher, guard, and auto-arm scripts, and
  .pi/extensions/lib/fm-home-resolve.ts for the tracked Pi and omp
  extensions, whose spawned children now receive the corrected
  environment.
- Take the helm on resume/reload/fork when the session does not already
  own the home lock, instead of leaving an idle restored agent to defer
  an advisory nudge.
- Report an alive-but-unsupervised secondmate: the liveness sweep now
  checks the home's own session lock, startup completion, and watcher
  beacon and emits an actionable SECONDMATE_LIVENESS line rather than
  stopping at endpoint-alive.

Tests cover the restored-home resolution end to end, the run-wrapper
routing change, and the new liveness diagnostic; existing extension and
session-start fixtures copy the new shared resolver.
…ards. Commit e856523 added a new shared library bin/fm-home-lib.sh and wired it into bin/fm-wake-lib.sh (and other scripts) as part of the secondmate-reboot-supervision fix. Two test fixtures build a fake FM_HOME by copying/symlinking an explicit hand-maintained list of bin/*.sh files rather than the whole bin/ directory, and neither list was updated to include the new fm-home-lib.sh dependency, so any subprocess that transitively sources fm-wake-lib.sh inside the fake fixture failed with "No such file or directory": - tests/fm-gotmp.test.sh (Behavior portable serial 2: "teardown exited non-zero with a valid tasktmp") - tests/fm-cursor-primary.test.sh (Behavior portable serial 1: "a Cursor-delivered Stop payload must not block through the Claude-settings duplicate") Fix: added bin/fm-home-lib.sh to both fixture builders (both occurrences in fm-gotmp.test.sh's make_fake_root and inline duplicate, and fm-cursor-primary.test.sh's install_scripts file list). Both tests now pass in isolation. For Lint: local fm-lint.sh runs in "changed-file mode" by default outside CI, which isn't representative — CI runs full ShellCheck analysis (GITHUB_ACTIONS=true). Reran full-mode lint in the background to check for a real regression from the new bin/fm-home-lib.sh and other touched files; only SC2034 "appears unused" warnings were observed on a prior run whose true exit code was masked by a `| tail` pipe, so a clean rerun (capturing the real exit code) is in progress. Also found tests/fm-watcher-lock.test.sh and tests/fm-session-start.test.sh failing locally, but confirmed both belong to shard 5 (not shards 1/2, the reported failing checks) and are caused by FM_BACKEND=herdr being pre-set in this session's own ambient shell environment, not a code regression — out of scope. Verification status: full portable-serial-1of5 and portable-serial-2of5 lane reruns (with FM_BACKEND unset) and a full-mode fm-lint.sh rerun (with real exit code captured) are still running in the background (each full shard takes ~17-20 minutes based on CI history). I have not yet confirmed the complete shards are green end-to-end or that Lint's true exit code is 0 — only the two specific previously-failing test files have been verified fixed in isolation. Next step once those background runs complete: confirm exit codes are 0, and if Lint shows a real (non-pipe-masked) failure, diagnose and fix that separately
CI runs fm-lint.sh in full dataflow analysis, which local changed-file
mode does not, so the SC2034 "appears unused" warnings on the new
caller-read result variables only surfaced after the branch pushed.

- Mark FM_HOME_RESOLVED/FM_ROOT_RESOLVED and FM_SUP_HOME_OK/
  FM_SUP_HOME_REASON as intentionally read by callers so full ShellCheck
  accepts them.
- Drop the unused local in the new omp leaked-home test.

Verified with shellcheck --norc --external-sources over every shell file
this branch changes, and by rerunning the two CI-shard fixture tests and
the new regression tests.
…"Behavior portable serial 1" failing. tests/fm-extension-binding.test.sh builds an isolated REMOTE_ROOT/bin by copying an explicit script list (including fm-procevent.sh and fm-wake-lib.sh) but omitted fm-home-lib.sh, which fm-wake-lib.sh unconditionally sources - same fixture bug class as prior commits e667f66/f0e715f fixed elsewhere, just missed in this file. Added fm-home-lib.sh to that copy list (single-line fix). Audited all other shard-1 test fixtures that copy/symlink bin/*.sh files; all others already include fm-home-lib.sh where required. Did not touch "PR must be raised via no-mistakes" - that failure is a stale pipeline attestation bound to an older commit (b372a94) than the current PR head (ab1c15d); it is expected to self-resolve once the outer no-mistakes executor re-attests against the new head, which is outside this CI-phase's scope (no push/PR control permitted here). Verification: running the full portable-serial-1of5 shard locally (env -u FM_BACKEND ./bin/fm-test-run.sh --lane portable-serial-1of5 --json /tmp/portable-serial-1of5.json), matching CI's "Behavior portable serial 1" check. This run takes ~15-20 minutes and was still in progress (on the longest script, tests/fm-watch-triage.test.sh, with 71+ ok lines and zero failures so far) when this report was generated. No failures observed yet. The outer executor should confirm this background run reaches FM_TEST_SUMMARY with failed=0 before treating the check as green, or rerun the shard, since local completion was not yet observed at report time. File changed: tests/fm-extension-binding.test.sh (one line: added fm-home-lib.sh to the REMOTE_ROOT/bin copy list around line 1822)
…ures

The new alive-but-unsupervised liveness check reports a secondmate whose
endpoint reads alive but whose home holds no live session lock. The sync
test fixtures modelled a running secondmate without any supervision
record, so the new, correct diagnostic broke the nudge-idempotency test's
assertion that a settled retry converges to silence.

Give add_sm_worktree the same healthy supervision record the liveness
fixtures use - a lock naming a live fake harness plus matching startup
completion - so the fixture represents a genuinely supervised secondmate
and the alive-branch check stays silent.
@babbarc
babbarc force-pushed the fm/secondmate-reboot-supervision-gap branch from ab1c15d to 1a79099 Compare September 14, 2026 08:50
@babbarc babbarc changed the title fix(bin): keep a restored second mate on its own home and detect lost supervision fix(bin): keep restored second mates supervised on their own home Sep 14, 2026
@babbarc
babbarc merged commit 768ccba into main Sep 19, 2026
14 of 15 checks passed
babbarc pushed a commit that referenced this pull request Nov 22, 2026
…ed (offer #8, final)

Refreshed local patch carrying the final reviewed version of upstream offer #8
(kunchenguid#7382), squashed onto current upstream main.

It replaces the two local variants this stack carried - 783c0ecc (the older
home-resolution variant) and b9ece53e (explicit fm_home_correct calls in five
entry points) - with the single mechanism upstream reviewed:

- bin/fm-home-lib.sh corrects an inherited FM_HOME/FM_ROOT_OVERRIDE/FM_ROOT at
  source time when the checkout is a seeded second mate root, and 26 directly
  invoked bin entry points source it before deriving any home-scoped path, so a
  restored pane's own tool shell can no longer read or write the launcher's
  main home (backlog, state, checks).
- bin/fm-supervision-lib.sh gains fm_supervision_home_verdict; bootstrap and the
  watcher tick re-judge alive second mate homes, report "alive but unsupervised"
  once via a check wake, and teardown clears the marker.
- Test and tooling hardening the offer carries: the home-leak regression case,
  fixture list updates, lint source-directive count, and host-determinism fixes.

Content is identical to the offer's final head 0190e10, minus the
coverage-guard collation slice that offer #17 carries instead.

Refreshed onto upstream bb3e690: the correction now also covers the entry
points and the Pi read-repetition extension added upstream since the offer,
and the cross-home verdict reads the session lock through the shared
fm_session_pid_valid predicate, so a tagged win:<pid> holder is usable.
@babbarc
babbarc deleted the fm/secondmate-reboot-supervision-gap branch November 22, 2026 17:14
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