Repository navigation
feat(bin): run secondmates and away mode on the Rex backend - #2
Merged
Merged
Conversation
Secondmates now spawn on the rex backend: each opens as the first tab of its own fm-<id> session, relaunch and reclaim reuse the recorded session, and a dead endpoint is proven by exit 3 and respawned. Primary workers move to a dedicated fm-local session, never the supervisor's own session, and one adapter function owns placement and naming. Remote secondmates can run on a host's own Rex app server in a visible fm-<id> session (fm-spawn/fm-remote-home-seed --backend rex), with the doctor checking the rex CLI and server instead of Herdr. The away daemon supports a Rex supervisor pane through ancestry-proven discovery, runs in a detached block of the captain's session, and keeps the composer gate.
A remote secondmate's host-side record lives under its own home, so retiring the home removes the busy state with it; the later retire step then failed on the missing directory and left the route half retired.
The host control's rex arm now relies on endpoint validation for the session binding, and a fake-rex unit test covers its route, missing, unverified, and cross-backend refusal paths. The verification record gains the live remote second mate run on a real Rex host.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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
I want to add Rex support /rex to firstmate, with full support for first and second mates, at the same level as the herdr support. It should use the Rex capabilties to the maximum, using its multiplexing and pane management and all other things to the maximum for an optimal development experience
Context: a planning investigation split this into three sequential phases. Phase 1, the
rexruntime backend for crewmates and scouts, has merged (#1). This task is Phase 2: secondmates run in their own Rex sessions with launch, relaunch, and recovery, and away mode (/afk) works when firstmate itself runs inside Rex, which it cannot today because the supervisor pane cannot be resolved there. Phase 3 (Rex-native status, events, and notifications) follows separately.to test the second mates, i've connected ds9
Context: ds9 is a second Mac on the tailnet (ssh host
ds9, ds9.bigeye-carat.ts.net) running its own Rex server (Rex tailnet endpoint ds9-rex.bigeye-carat.ts.net, API revision 5,rexonly inside the app bundle, not on PATH), with tmux, herdr, claude, git, treehouse, and no-mistakes installed. Firstmate's remote secondmates currently run only on Herdr.no, nothing is using that rex, use it directly
rex has a concept of sessions, maybe it is a good idea that workers locally, secondmates and their workers run in their own session, so fm-local and fm-remote might be a good idea as session names?
Agreed layout: the primary home's workers run in a dedicated Rex session named fm-local instead of the supervisor's own session; each secondmate gets its own Rex session named fm- on the Rex server of the machine it runs on, with the secondmate as the first tab and its workers as further tabs.
it is importan that the sessions show up in rex, the herdr fm-remote session is not what i want, i want to see the agents in my connected tree
What Changed
fm-localand never to Firstmate's own session. Each second mate gets anfm-<id>session, with the second mate as the first tab and its workers as further tabs.fm_backend_rex_task_session_lockedmakes every placement decision: reuse the recorded session, adopt a single labeled session, or create one, and refuse when the label is ambiguous. Cleanup closes an emptied worker session but never Firstmate's own session or one that still holds a detached block.fm-spawn.sh <id> --secondmate --backend rex), both locally and on remote hosts. When the pane is proven gone (exit 3), relaunch reuses the recorded session; an unreachable server (exit 4) is never treated as dead. Remote routes accept--backend rexinfm-remote-home-seed.shandfm-spawn.shand save it in the parent record asremote_backend=. Switching a live endpoint to the other backend is refused.fm-remote-doctor.sh --backend rexchecks for therexCLI (onPATHor inside the app bundle) and a running Rex server that passes the version gate, and skips the Herdr launch agent. Remote control, liveness, restart, and teardown handle Rex endpoints./afknow works when Firstmate runs in a Rex pane. Supervisor discovery accepts Firstmate's own pane only through structural proof, never from an inheritedREX_BLOCK. The away daemon runs in a detached block of the captain's session, and its record stores the block target and server. Closing that block is verified by exit 3. New tests cover all of this: the fake-rexdoctor test, the real-binary away-mode inject e2e test, and the opt-in Rex secondmate e2e test. The fake-backend and smoke suites gained cases too, and the docs and the runtime-backends verification record now include a ds9 run.🤖 Generated with Claude Code
Risk Assessment
Testing
I re-drove every scenario at HEAD. Locally I ran the real Rex smoke test (
fm-localplacement and second mate session placement), the away-mode test and the Pi second mate test (three runs in a row), all on private Rex lab servers. On ds9 I ran a full remote second mate lifecycle against its own Rex server from a disposable lab home: seed, launch, connected-tree check, route/state, steer and reply, liveness, close and recover, the backend-switch refusal, and teardown. I also ran the fake-binary Rex suite and the remote-doctor suite. Every live scenario passed. The first teardown was refused because the steering reply hadn't been pulled back to the parent yet ('unresolved routed reply'). That guard is meant to block teardown in that state, and teardown succeeded once the reply was pulled. ds9's clone is at 013ec95, so the host-side control cleanup at HEAD was not driven live. Only the fake-binary suite exercised it, and that scenario is recorded as untested. There is no screenshot of the Rex client tree, because the agent process lacks macOS Screen Recording permission (round 1, test-2). Therex client lsandrex session inspecttranscripts stand in for it. The lab home, temp helpers and ds9 remote home are all removed, and the worktree and ds9 clone are clean.Evidence: Real Rex smoke (fm-local session, secondmate fm-<id> session, retirement)
Source: Real Rex smoke (fm-local session, secondmate fm-<id> session, retirement)
Evidence: Away mode inside Rex e2e
Source: Away mode inside Rex e2e
Evidence: Pi/Rex secondmate e2e run 1
Source: Pi/Rex secondmate e2e run 1
Evidence: Pi/Rex secondmate e2e run 2
Source: Pi/Rex secondmate e2e run 2
Evidence: Pi/Rex secondmate e2e run 3
Source: Pi/Rex secondmate e2e run 3
Evidence: ds9 seed
Source: ds9 seed
Evidence: ds9 launch + parent record without remote_rex_session
Source: ds9 launch + parent record without remote_rex_session
spawned nmrex2 ... remote=ds9 backend=rex remote_backend=rex remote_target=01a1626f-...:01a1626f-... count of remote_(rex|herdr)_session keys: 0Evidence: ds9 connected Rex tree (voyager client attached to fm-nmrex2)
Source: ds9 connected Rex tree (voyager client attached to fm-nmrex2)
Evidence: ds9 route/state/send
Source: ds9 route/state/send
Evidence: ds9 steering handled + reply in pane capture
Source: ds9 steering handled + reply in pane capture
Evidence: ds9 liveness, close, recovery into same fm-nmrex2 session
Source: ds9 liveness, close, recovery into same fm-nmrex2 session
Evidence: ds9 backend switch refusal
Source: ds9 backend switch refusal
Evidence: ds9 reply settled on parent
Source: ds9 reply settled on parent
Evidence: ds9 retire (session gone, remote home removed)
Source: ds9 retire (session gone, remote home removed)
Evidence: Fake-binary Rex contract suite
Source: Fake-binary Rex contract suite
Evidence: Remote doctor --backend rex suite
Source: Remote doctor --backend rex suite
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-spawn.sh:1353- Simplification: the parent'sremote_rex_sessioncheck and record field don't verify anything.print_route(bin/fm-remote-secondmate-control.sh:171) never reads Rex; it just prints the constantrex_session=fm-<id>built from the id. The parent then checks that this value equalsfm-$id(bin/fm-spawn.sh:1353), which a current host can never fail. It also writesremote_rex_session=into the parent record (bin/fm-spawn.sh:1395), and nothing in bin/ ever reads that field. The real check that the session is live and labeled fm-<id> already happens on the host incmd_launchthroughrex_session_check. No requirement in the intent needs this second copy. Suggested fix: remove the parent-side check and the unread record field, or, if the route should report the session, haveprint_routeemit the label it actually reads from Rex instead of a synthesized constant.docs/verification/runtime-backends.md:2294- The change's own ds9 evidence records a remote Rex relaunch that stopped the agent and then refused withtask record authorized directory cannot be resolved at <home>/state/parent-route(raised by fm_backlog_record_parent_authorized in bin/fm-backlog-transition-lib.sh:670). That leaves the second mate stopped until someone reruns the relaunch. Reruns succeeded and the refusal did not recur, but no root cause is recorded and no code changed in response. Worth tracking so this stop-then-refuse sequence isn't treated as solved.🔧 Fix applied.
1 info still open:
docs/verification/runtime-backends.md:2294- The change's own ds9 evidence records a remote Rex relaunch that stopped the agent and then refused withtask record authorized directory cannot be resolved at <home>/state/parent-route(raised by fm_backlog_record_parent_authorized in bin/fm-backlog-transition-lib.sh:670). That leaves the second mate stopped until someone reruns the relaunch. Reruns succeeded and the refusal did not recur, but no root cause is recorded and no code changed in response. Worth tracking so this stop-then-refuse sequence isn't treated as solved.🔧 **Test** - 2 issues found → auto-fixed ✅
tests/fm-rex-secondmate-e2e.test.sh:128- The opt-in Pi/Rex second mate e2e failed 4 out of 4 runs here at the doorbell step. A fixedsleep 2sent the doorbell while Pi was still aborting the charter turn, so the doorbell was queued and dropped with it. I replaced the sleep withwait_for_settled, which waits until Pi has rendered the abort and its screen holds steady for four samples. The test then passed 3 out of 3. This was a test timing flake, not an adapter defect: the doorbell text was always delivered to the pane.screencapture -l <Rex window>fails with 'could not create image from window' because the agent's host process lacks macOS Screen Recording permission. To capture it, grant Screen Recording to the terminal or app running the agent (System Settings > Privacy & Security > Screen Recording). ds9'srex client lsandrex lsoutput stands in as the evidence.PATH=<rex bundle>:$PATH REX_BIN_DIR=<rex bundle> tests/fm-backend-rex-smoke.test.sh(real rex binary, private fm-rexlab server)tests/fm-afk-inject-rex-e2e.test.sh(real rex binary, private lab server)FM_REX_SECONDMATE_E2E=1 tests/fm-rex-secondmate-e2e.test.sh: 4 runs failed at the doorbell step before the fix, 3 of 3 passed after itDiagnosed the doorbell failure with an instrumented copy of the test. The doorbell was typed and submitted, but Pi had not finished the charter turn's abort 2s later, so the doorbell was queued and dropped with it; a 10s settle passedRemote ds9 run from a disposable lab home (bin/fm-lab-home.sh create), this worktree as parent:fm-on.sh nmrex fm-remote-doctor.sh --backend rexand without--backend(Herdr gaps only)fm-remote-home-seed.sh --backend rex nmrex ds9 ~/code/firstmate-rex /private/tmp/fm-gate-nmrex-* --no-projectsfm-spawn.sh nmrex --secondmate --backend rex --harness claudethenrex ls,session inspect,block inspect,client lson ds9's app serverfm-on.sh nmrex fm-remote-secondmate-control.sh state|route|observe nmrex,fm-peek.sh nmrexfm-send.sh nmrex '<PONG-REX request>'thenfm-procevent.sh ensure-listening remote-reply-nmrex: the pending reply resolved with PONG-REXfm-send.sh nmrex --key C-lAdversarial:fm-spawn.sh nmrex --secondmate --backend herdrwhile the Rex endpoint is livefm_secondmate_liveness_probelive, thenrex block closeon ds9, probe againfm-spawn.sh nmrex --secondmatewith no--backend(recovery)fm-remote-secondmate-relaunch.sh nmrex claude default defaultfm-teardown.sh nmrex, then checked ds9's server instance, session count, and remote home, and that the ds9 checkout is cleanSupporting checks, not live:tests/fm-backend-rex.test.sh(27 ok) andtests/fm-remote-doctor-rex.test.sh(4 ok) with a fake rex🔧 Fix applied.
✅ Re-checked - no issues remain.
tests/fm-backend-rex-smoke.test.sh(real private Rex lab server; bundled rex CLI put on PATH through a temp symlink)tests/fm-afk-inject-rex-e2e.test.sh(real Rex lab, away mode)FM_REX_SECONDMATE_E2E=1 tests/fm-rex-secondmate-e2e.test.shx3 (real Pi + Rex lab)tests/fm-backend-rex.test.sh(fake-binary contract suite, including the remote host control routing)tests/fm-remote-doctor-rex.test.shbin/fm-remote-home-seed.sh --backend rex nmrex2 ds9 ~/code/firstmate-rex /private/tmp/fm-gate-nmrex2-<pid> --no-projectsagainst a disposable marked lab FM_HOMEbin/fm-spawn.sh nmrex2 --secondmate --backend rex --harness claude-> real Claude second mate on ds9's Rex serverssh ds9 rex client ls / rex ls / rex session inspect fm-nmrex2(connected-tree visibility)bin/fm-on.sh nmrex2 fm-remote-secondmate-control.sh state|route|capture nmrex2bin/fm-send.sh nmrex2 '...PONG-REX2...'+bin/fm-procevent.sh ensure-listening remote-reply-nmrex2(reply settles the pending-reply record)fm_secondmate_liveness_probelive ->rex block closeon ds9 -> probe ->bin/fm-spawn.sh nmrex2 --secondmatewith no --backend (recovery) -> probebin/fm-spawn.sh nmrex2 --secondmate --backend herdr(adversarial backend switch)bin/fm-teardown.sh nmrex2and ds9 post-retirerex ls/ home check✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.