Repository navigation
feat(bin): let firstmate restart itself onto another Claude seat - #18
Merged
Merged
Conversation
A seat switch moves only what the next spawn reads, so it has never moved firstmate: the lead keeps the account its own process launched on while config/claude-seat drifts away from it. No running Claude Code process can change credential store, so the lead changes account only by being replaced. bin/fm-seat.sh lead-restart does that, through bin/fm-lead-restart.sh: another claude starts on the new seat in the same terminal, resuming the same session, and the current process ends. Verified rather than assumed, against the code: - Running workers need nothing. Steering is a durable inbox, status is a durable log, and each task keeps the seat recorded in its own record, so a crew notification step would change nothing and none is added. - The one real worker-facing effect is supervision, and it is handled by not touching it. The watcher is a separate process with its own singleton lock and is never in the kill, so it is orphaned rather than stopped and keeps its beacon; the successor's arm attaches to it instead of starting a second one. The cycle count goes one to one. The guarded pieces: - The persist gate is the one a second mate's restart already applies. The /stow open-record contract now has one owner, bin/fm-persist-request-lib.sh, which bin/fm-secondmate-restart-lib.sh composes its request from and the lead path states verbatim. The command refuses without --persisted. - The lock is handed over through a bounded reservation in bin/fm-lock.sh (state/.lock-handover). It is armed while the outgoing lead still holds the lock, so there is no instant with two holders and none with the home open to a third session while line 1 names a dead pid. The outgoing session is admitted by its session id and keeps confirming its own lock; the successor is admitted by the nonce it is launched with, and that proof is what clears the reservation. A lapsed reservation means nothing, so a failed handover cannot wedge a home. - The successor is relaunched in the same pane with every other flag preserved. The lead's own profile and pane are not derivable elsewhere, so the owning session records both in state/.lock-runtime bound to the lock's pid, and a record naming another pid is no evidence. The launch command is read from the running process and accepted only when it provably round-trips; a flattened argument vector where a multi-word argument is indistinguishable from two arguments refuses and names --launch-command. - The armed threshold watch asks the same question about the lead's own seat and reports that crossing with the exact command rather than acting on it: the poll is a separate process and cannot write the lead's conversation down. It reports nothing unless the whole move passes preflight, so a crossing with no signed-in destination or no established terminal names that blocker instead. - Claude primary only. Any other harness refuses by name. What a failure leaves. Every check runs before anything is touched, so a refusal leaves the previous session running and in charge. After the previous process has ended there is no going back to it: the stage records the outcome in state/.lead-restart.result, drops the reservation, and queues a wake. Recovery is one command in that same terminal, printed in that record and staged as a file to source, and nothing else is affected because only one process was ever replaced. Not proven, and not claimed: that a real signed-in `claude --resume <id>` comes back as that same session. Exercising it needs a credential this work deliberately never touches. The stand-in reproduces the documented contract instead - a resume keeps the session id, which is what --fork-session exists to opt out of - and every other step is proven against real processes. tests/fm-lead-restart.test.sh drives the real scripts: the establishment refusals and the reservation against real claude-named processes, and the whole swap in a real tmux on a private socket, where one lead before and one lead after must be in the same pane, on the new profile, resuming the same session, with a cycle started inside the outgoing lead's tree still alive afterwards.
The lead restart and the seat command both need the seat NAME a profile directory belongs to, which is the inverse of fm_seat_config_dir and belongs beside it rather than in two copies.
…ies out The watch reported the lead's seat crossing and left the move to be asked for. That is report-only: the captain set the threshold, so the threshold firing is the instruction, and firstmate acts on it like any other actionable check result. The watch still hands the move over rather than performing it, and that is about which process can do it, not about who decides: the poll runs in a separate process and cannot write the lead's conversation down, which the persist gate requires first. What changes is that the handover is now binding. - The wake line says so itself and stays self-sufficient with no skill loaded: it states that firstmate moves now, on this wake, without asking the captain, names the skill that owns the handling, and orders the persist step before the command. - .agents/skills/claude-seat-lead-restart owns that handling: the standing authority and its exact scope, the persist step, what to do with a refusal, that nothing follows once the command arms, and the recovery half for a `check: lead-restart` wake whose swap did not complete. It also states that the away and quiet postures do not change it, because a seat runs out of quota whether or not the captain is present. - AGENTS.md section 13 carries only the load trigger; docs/claude-seats.md and docs/configuration.md drop their report-only wording. Also fixed, found while testing this: bin/fm-lock.sh dropped the handover reservation before publishing the new owner's runtime record, so the stage read the swap as complete while that record was still missing. The reservation is now dropped last, which is what makes its disappearance mean the whole acquisition landed. The suite pins the wake as an instruction rather than an option, and bin/fm-test-run.sh maps the new paths so a change to the seat, lock, or lead restart selects this suite.
max-metaplanet
added a commit
that referenced
this pull request
Oct 6, 2026
* feat(bin): let firstmate move itself to another Claude seat A seat switch moves only what the next spawn reads, so it has never moved firstmate: the lead keeps the account its own process launched on while config/claude-seat drifts away from it. No running Claude Code process can change credential store, so the lead changes account only by being replaced. bin/fm-seat.sh lead-restart does that, through bin/fm-lead-restart.sh: another claude starts on the new seat in the same terminal, resuming the same session, and the current process ends. Verified rather than assumed, against the code: - Running workers need nothing. Steering is a durable inbox, status is a durable log, and each task keeps the seat recorded in its own record, so a crew notification step would change nothing and none is added. - The one real worker-facing effect is supervision, and it is handled by not touching it. The watcher is a separate process with its own singleton lock and is never in the kill, so it is orphaned rather than stopped and keeps its beacon; the successor's arm attaches to it instead of starting a second one. The cycle count goes one to one. The guarded pieces: - The persist gate is the one a second mate's restart already applies. The /stow open-record contract now has one owner, bin/fm-persist-request-lib.sh, which bin/fm-secondmate-restart-lib.sh composes its request from and the lead path states verbatim. The command refuses without --persisted. - The lock is handed over through a bounded reservation in bin/fm-lock.sh (state/.lock-handover). It is armed while the outgoing lead still holds the lock, so there is no instant with two holders and none with the home open to a third session while line 1 names a dead pid. The outgoing session is admitted by its session id and keeps confirming its own lock; the successor is admitted by the nonce it is launched with, and that proof is what clears the reservation. A lapsed reservation means nothing, so a failed handover cannot wedge a home. - The successor is relaunched in the same pane with every other flag preserved. The lead's own profile and pane are not derivable elsewhere, so the owning session records both in state/.lock-runtime bound to the lock's pid, and a record naming another pid is no evidence. The launch command is read from the running process and accepted only when it provably round-trips; a flattened argument vector where a multi-word argument is indistinguishable from two arguments refuses and names --launch-command. - The armed threshold watch asks the same question about the lead's own seat and reports that crossing with the exact command rather than acting on it: the poll is a separate process and cannot write the lead's conversation down. It reports nothing unless the whole move passes preflight, so a crossing with no signed-in destination or no established terminal names that blocker instead. - Claude primary only. Any other harness refuses by name. What a failure leaves. Every check runs before anything is touched, so a refusal leaves the previous session running and in charge. After the previous process has ended there is no going back to it: the stage records the outcome in state/.lead-restart.result, drops the reservation, and queues a wake. Recovery is one command in that same terminal, printed in that record and staged as a file to source, and nothing else is affected because only one process was ever replaced. Not proven, and not claimed: that a real signed-in `claude --resume <id>` comes back as that same session. Exercising it needs a credential this work deliberately never touches. The stand-in reproduces the documented contract instead - a resume keeps the session id, which is what --fork-session exists to opt out of - and every other step is proven against real processes. tests/fm-lead-restart.test.sh drives the real scripts: the establishment refusals and the reservation against real claude-named processes, and the whole swap in a real tmux on a private socket, where one lead before and one lead after must be in the same pane, on the new profile, resuming the same session, with a cycle started inside the outgoing lead's tree still alive afterwards. * refactor(bin): give profile-to-seat naming one owner in fm-seat-lib.sh The lead restart and the seat command both need the seat NAME a profile directory belongs to, which is the inverse of fm_seat_config_dir and belongs beside it rather than in two copies. * feat(agents): make the lead's own seat crossing a move firstmate carries out The watch reported the lead's seat crossing and left the move to be asked for. That is report-only: the captain set the threshold, so the threshold firing is the instruction, and firstmate acts on it like any other actionable check result. The watch still hands the move over rather than performing it, and that is about which process can do it, not about who decides: the poll runs in a separate process and cannot write the lead's conversation down, which the persist gate requires first. What changes is that the handover is now binding. - The wake line says so itself and stays self-sufficient with no skill loaded: it states that firstmate moves now, on this wake, without asking the captain, names the skill that owns the handling, and orders the persist step before the command. - .agents/skills/claude-seat-lead-restart owns that handling: the standing authority and its exact scope, the persist step, what to do with a refusal, that nothing follows once the command arms, and the recovery half for a `check: lead-restart` wake whose swap did not complete. It also states that the away and quiet postures do not change it, because a seat runs out of quota whether or not the captain is present. - AGENTS.md section 13 carries only the load trigger; docs/claude-seats.md and docs/configuration.md drop their report-only wording. Also fixed, found while testing this: bin/fm-lock.sh dropped the handover reservation before publishing the new owner's runtime record, so the stage read the swap as complete while that record was still missing. The reservation is now dropped last, which is what makes its disappearance mean the whole acquisition landed. The suite pins the wake as an instruction rather than an option, and bin/fm-test-run.sh maps the new paths so a change to the seat, lock, or lead restart selects this suite. * no-mistakes(review): Harden lead restart handover and notify crew before swap * no-mistakes(document): Clarify lead-restart skill: started outcome queues no wake * no-mistakes(lint): Make fm-lead-restart test shellcheck-clean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Extend Claude seat auto-switching so the primary firstmate itself can move to another seat, not just the new workers it launches.
The design: firstmate tells its crew it is about to restart, starts another claude process on the new seat that becomes the firstmate, then ends the current process - a "kill and resurrect" bootstrap.
Seats are profile directories under the seats root whose contents symlink the shared ~/.claude files, so any seat can resume the same session: the seat is the brain, the sessions and settings are the body.
Crew already launch as CLAUDE_CONFIG_DIR= ... claude.
What Changed
bin/fm-lead-restart.shand abin/fm-seat.sh lead-restart [--to <seat>] [--check] [--persisted] [--launch-command <cmd>]command that moves the primary firstmate to another seat by replacing its process. It checks that the lead is a Claude session and that the destination is signed in. It also works out the session id, pane, and launch command, refusing if any of them can't be established. It sends each live worker a fire-and-forget notice, starts a newclaudeon the new seat in the same terminal that resumes the same session, and then ends the old process. The watcher process is left running. Without--persisted, the command refuses. The outcome and any recovery command are written tostate/.lead-restart.result.bin/fm-lock.shnow records the lock owner's Claude profile and pane instate/.lock-runtime. It also addshandoverandhandover-clearverbs: a handover is a time-limited reservation instate/.lock-handoverthat only the outgoing session or the named successor (proven by a nonce) can use, so no third session can take the home during the swap. The logic for these records lives in the new helpers inbin/fm-session-lock-lib.sh.bin/fm-lock.sh statusshows both the reservation and the lead's profile. Supporting changes: tmux can now check agent state by pane id (%N),fm-send.sh --fire-and-forgetnow accepts crewmate and scout tasks, and the persist-open-work contract moved intobin/fm-persist-request-lib.sh, where secondmate restarts also use it.fm-seat.sh statusnow shows which seat firstmate itself is running on.fm-seat.sh autonow also checks the threshold against firstmate's own seat, runs a--checkpreflight, and emits oneclaude-seat: firstmate itself …instruction (or a line naming what blocks the move). A newclaude-seat-lead-restartskill, updates toAGENTS.md,docs/claude-seats.mdand related docs, shared test fixtures, andtests/fm-lead-restart.test.shcover all of this.🤖 Generated with Claude Code
Risk Assessment
Testing
I ran the change's targeted suite, tests/fm-lead-restart.test.sh: all 18 tests pass, including the full swap and the stranded outcome in a real tmux on a private socket. I then drove the swap twice by hand in a real Herdr lab session (fm-lab-*, created and torn down through bin/fm-herdr-lab.sh), using a claude-named stand-in lead in a real pane. Each time the result was state=done. The lock moved to a new pid on the beta profile in the same pane (fm-lab-…:w1:p1), resuming lead-session-1. The old pid was gone, the reservation was cleared, and one lead was left running. In the second run the live crew member got the fire-and-forget notice before the swap, and the unreachable crew member was named in the result without blocking it. The pane screen captures show the old lead end and
. .lead-restart.launchbring up the successor on seat beta in that same pane. The refusal cases and the automatic trigger passed in the test suite, but only with a faked quota probe and pane probe, so they are not counted as live results. No UI surface is involved; the evidence is CLI and pane transcripts. Only a real signed-inclaude --resumewas not exercised, because no credential was used.claude --resume <id>on the new seat comes back as the same sessionEvidence: fm-lead-restart test suite output (includes the real-tmux swap and stranded tests)
Source: fm-lead-restart test suite output (includes the real-tmux swap and stranded tests)
Evidence: Herdr lab transcript: lead restart alpha→beta, unreachable crew named in result
Source: Herdr lab transcript: lead restart alpha→beta, unreachable crew named in result
Evidence: Herdr lab transcript with live crew pane: notice delivered fire-and-forget, pane screen showing same-pane replacement
Source: Herdr lab transcript with live crew pane: notice delivered fire-and-forget, pane screen showing same-pane replacement
Evidence: Herdr result record
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-lead-restart.sh:489- The handover stage calls fm_backend_agent_alive as if its exit status meant "alive". It doesn't: bin/fm-backend.sh:991 prints alive/dead/unknown and always returns 0, and every other caller (fm-watch.sh, fm-spawn.sh, fm-fleet-snapshot.sh) reads what it prints. Sofm_backend_agent_alive ... && breakpasses on the first iteration, and theif ! fm_backend_agent_alivestranded branch at line 493 can never run. Failure path: the old lead is killed and. launchis typed into the pane, but the successor exits right away (bad flag, auth error, pane closed with the process). The stage skips the start check, waits up to CLAIM_WAIT with the reservation live, then writes state=started ("the replacement is running on seat X") and queues no wake. The captain's terminal is left with no firstmate and the result record says it is running. Fix: compare the printed value, e.g.[ "$(fm_backend_agent_alive "$backend" "$target")" = alive ], at both sites.bin/fm-lead-restart.sh:477- The stage only proves that the lock anchor pid died before it types. <launch-file>+ Enter into the pane. It never checks that the pane went back to a shell. For a trusted Claude session the anchor is CLAUDE_PID, the model-loop process (fm_session_lock_anchor_pid). fm-session-lock-lib.sh itself says this can be a transient helper under a shared daemon, separate from the front-end that owns the pane. If the TUI survives that kill, the launch line goes in as a prompt to the old claude. The (fixed) agent-alive check would then see the old agent and pass. The old session could then reclaim the lock by session id, which the reservation admits. The result would read started/done while firstmate is still on the old seat. Fix: after the kill loop, require the pane's agent state to read dead (a shell) before sending, and recordrefused/strandedotherwise. The launch argv read from that same pid has the same exposure.bin/fm-seat.sh:847- auto_lead_trigger records lead=<seat> before it knows whether the move is available. So a transient refusal suppresses the actionable instruction for the rest of that crossing. Example: on the first poll below threshold, the destination's login probe is undecided (quota-axi rate-limited or slow), sofm-lead-restart.sh --checkrefuses and a one-off "cannot move" line is printed. Every later poll sees reported == seat and returns silently, even once --check would pass. Firstmate then stays on the exhausted seat until it climbs back above the threshold. Fix: record the seat only when the "moves to ... now" instruction is emitted, or key the record on the outcome as well.bin/fm-lead-restart.sh:21- This contradicts the intent. The intent says: "The design: firstmate tells its crew it is about to restart, starts another claude process on the new seat that becomes the firstmate, then ends the current process". The change deliberately does the opposite: "WHAT DELIBERATELY DOES NOT HAPPEN. Running workers are not told anything and are not moved... a crew notification step would be a message that changes nothing". docs/claude-seats.md says the same ("Running workers are untouched and are told nothing"). The reasoning (durable inbox, logs, and watcher continuity) may be sound, but it drops a step the stated design includes. The user needs to confirm the crew notification can be left out.bin/fm-lead-restart.sh:506- The claim loop treats "the reservation is gone" as "the successor took the lock". A reservation that simply reached its deadline looks the same. The window is GRACE+EXIT_WAIT+START_WAIT+CLAIM_WAIT, but the stage can also spend up to 10s on the KILL wait plus per-iteration command time. If the exit and start waits run near their limits and the successor never claims, the reservation lapses inside the claim loop and the stage writes state=done. Checking the lock record directly (a live line-1 pid whose .lock-runtime profile equals to_profile) would make 'done' accurate.🔧 Fix applied.
2 infos still open:
bin/fm-lead-restart.sh:528- The new pane gate is meant to confirm the pane went back to a bare shell before anything is typed into it. It uses fm_backend_agent_alive, but that function prints 'dead' for both a shell pane and a pane that no longer exists (dead|missing) printf 'dead'in bin/fm-backend.sh:993). Failure path: the lead was started as the pane's own command, so the pane closes when the process ends. The gate passes, and the stage types. launchat a target that is gone. With the usual%Ntarget the send fails and the result reads stranded 'could not be delivered', which is honest but not the accurate reason. With asession:windowtarget from FM_SUPERVISOR_TARGET, tmux may fall back to another window in that session. Fix: call fm_backend_agent_state and require exactlydead, and treatmissingas its own stranded outcome.bin/fm-lead-restart.sh:491- notify_crew runs before the grace period and before the kill. If the old lead then refuses to exit (the 'refused' branch), the crew has already been told the lead is restarting onto another seat, and nothing corrects that. The notice says to carry on, so nothing breaks, but the crew now holds a false statement. The prescribed ordering ('before the swap') is followed; this is just a note.✅ **Test** - passed
✅ No issues found.
claude --resume <id>on the new seat comes back as the same sessionbash tests/fm-lead-restart.test.sh(18 behavior tests, including the full swap and the stranded case in a real tmux on a private socket)Manual Herdr lab run 1:bin/fm-herdr-lab.sh name/provision→ stand-in claude lead in its own pane takes the lock through bin/fm-lock.sh →fm-seat.sh lead-restart --to beta --check→fm-seat.sh lead-restart --to beta --persisted→ read .lead-restart.result, .lock, .lock-runtime, the successor's argv, the reservation, and the leads left running →fm-herdr-lab.sh teardownManual Herdr lab run 2: same flow plus a live crew1 worker pane (herdr meta) and a crew2 whose inbox cannot be written; checked crew1's inbox message (delivery=fire-and-forget), the result naming crew2, and aherdr pane readof the lead pane showing the old lead end and the successor start in the same paneChecked after teardown: no leftover fm-lab-leadrestart session inherdr session list, no leftover lab processes, worktree clean✅ **Document** - passed
✅ No issues found.
🔧 **Lint** - 1 issue found → auto-fixed ✅
🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Push** - passed
✅ No issues found.