Repository navigation
Conversation
This was referenced Oct 1, 2026
feat(orca): add agent-state classifier, secondmate spawns, and Escape delivery
flight505/firstmate#1
Merged
This was referenced Oct 16, 2026
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
Implement all planned phases of the Firstmate + Orca integration in one closed loop, correcting bugs and rerunning tests until the complete migration is finished. The words authorizing this scope were: "you may complete all phases in a closed loop, fixing bugs until completion". The completed capability spike established that Orca 1.4.206 exposes real worker-start, worker-read, worker-abandon, worker-show, worker-list, worker-release, terminal wait, worktree ps, mailbox/ask, and durable Run/Task/Dispatch identities. The final result must make native supervision usable without losing Firstmate\x27s isolation, steering, validation, recovery, delivery, cleanup, and persistent Secondmate guarantees.
What Changed
bin/backends/orca-supervised.shplus a JSON helper).fm-spawn.sh --orca-mode supervisedruns the full launch in the task's Orca terminal first, then attaches it withorchestration worker-start --terminal. Before treating the launch as consumed, it checks that the returned Run/Task/Dispatch/worker/worktree identities match and reads a completeworker-readtranscript. The mode is capability-gated: agent-context schema 1, the required command shapes, a Claude/Codex/Cursor harness, and a liveORCA_TERMINAL_HANDLE. If any check fails, ordinary ships and scouts fall back to terminal mode.--secondmatespawns, but only through native supervision bound to an exact existing home workspace fromorca worktree ps. A recorded native task never falls back to terminal mode on relaunch or restart: it relaunches in a fresh terminal with--task/--retry-of. Meta records the durable native identities, and routing (fm-backend,fm-peek,fm-send,fm-crew-state,fm-control,fm-classify-lib) goes throughdispatch:<id>. It uses transcript-first reads, mailbox doorbells with a terminal fallback,worker-stop/worker-abandon/worker-releasecontrol, and it refuses on any identity mismatch.cleanup_recovery=orcarecord, plusorca_retry_dispatch_idfor a partial retry, covers attaches or relaunches that cannot settle. The persistent Secondmate home is kept, the Secondmate restart gate and liveness sweep skip recovery records, and the Orca docs, skill, and backend tests are updated to match.Risk Assessment
Testing
I ran the three targeted regression suites for this change with the live flag set: tests/fm-backend-orca.test.sh, tests/fm-control.test.sh and tests/fm-secondmate-restart.test.sh. All passed; they cover the fake-Orca lifecycle, the recovery records and partial receipts. I then set up a throwaway Orca lab: a scratch repo already registered in Orca, a coordinator worktree and terminal, and isolated Firstmate state/data/config under /tmp. In it I ran the real Firstmate CLIs against the running Orca app. The capability gate accepted native mode only inside a real Orca terminal; a missing or stale handle fell back, and Pi was excluded. A persistent Secondmate refused outside Orca and refused a raw launch, with no mutation. A raw-launch scout asking for supervised mode fell back to the terminal adapter on a real Orca worktree and terminal, and was peeked and torn down, which released the worktree. Forged native records and
cleanup_recovery=orcarecords were refused by peek, crew-state, send and teardown, and every record was kept. A native spawn run inside the Orca coordinator hit a realagent_unconfiguredrefusal; spawn closed the launched terminal, released the worktree, wrote no record, and left no Dispatch in the Run. The one remaining gap is a successful attach, which needs a real logged-in agent. The lab was torn down afterwards. There is no UI change, so there are no screenshots; the evidence is CLI transcripts.Evidence: Native gate inside a live Orca terminal
Source: Native gate inside a live Orca terminal
Evidence: Native gate outside Orca (no handle / stale handle)
Source: Native gate outside Orca (no handle / stale handle)
Evidence: Persistent Secondmate refusal outside Orca
Source: Persistent Secondmate refusal outside Orca
Evidence: Raw-launch scout supervised→terminal fallback spawn
Source: Raw-launch scout supervised→terminal fallback spawn
Evidence: Terminal-mode scout peek
Source: Terminal-mode scout peek
Evidence: Terminal-mode scout teardown and Orca release
Source: Terminal-mode scout teardown and Orca release
Evidence: Forged native and recovery records fail closed
Source: Forged native and recovery records fail closed
Evidence: Forged native record steer keeps durable inbox
Source: Forged native record steer keeps durable inbox
Evidence: Native spawn inside Orca: refused attach abort cleanup
Source: Native spawn inside Orca: refused attach abort cleanup
Evidence: Direct worker-start attach probe (agent_unconfigured, 0 workers)
Source: Direct worker-start attach probe (agent_unconfigured, 0 workers)
Evidence: Targeted regression suites log
Source: Targeted regression suites log
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 5 issues found → auto-fixed ✅
bin/backends/orca-supervised.sh:185- The native attach loses the Dispatch identity on any failed or outcome_unknown worker-start.out=$("${args[@]}") || return 1(and thenfm_backend_orca_json_ok ... || return 1) returns beforefm_backend_orca_supervised_set_from_jsonruns.orca orchestration worker-start --helpsays 'Failed or outcome_unknown exits 1 and JSON includes stage/failedStage, setup, effects, residualResources'. So when Orca creates the Dispatch and then reports outcome_unknown, FM_ORCA_SUPERVISED_DISPATCH_ID stays empty and fm-spawn.sh:5153-5160 copies nothing. The abort trap sees no ORCA_DISPATCH_ID and skipsorca_supervised_abort_reconcile. It closes the terminal and, on a fresh ship, removes the worktree with nocleanup_recovery=orcarecord. The Dispatch then leaks untracked. The spawn comment at fm-spawn.sh:5162-5165 ('A nonzero receipt is not proof that no worker was created. The abort trap reconciles any Dispatch it did return') says the opposite of what happens, and the user asked to 'verify no Dispatch leak'. Fix: capture the exit status, runfm_backend_orca_supervised_set_from_json "$out" startwhenever stdout is parseable JSON (ok:false included), then return 1, so the existing trap and recovery-record path see the Dispatch.bin/fm-spawn.sh:5162- When worker-start returns a different terminal handle, the abort trap closes that handle and leaves the terminal Firstmate launched running.fm_backend_orca_supervised_worker_startstores the receipt's handle in FM_ORCA_SUPERVISED_TERMINAL and only then returns 1 on[ "$FM_ORCA_SUPERVISED_TERMINAL" = "$terminal" ](orca-supervised.sh:192). Line 5162 still copies that value into ORCA_TERMINAL. The trap then runsfm_backend_kill orca "$ORCA_TERMINAL", andorca_abort_recovery_recordwrites it asterminal=, so teardown's recovery path closes it a second time. The launched terminal$Tkeeps its harness running outside task control, which is the case the new 'close the launched terminal' contract is meant to prevent. The terminal that does get closed was never verified as Firstmate's; it could be any handle Orca put in the receipt, including another task's terminal or the coordinator's. Now that spawn passes--terminal "$T",$Tis authoritative. Fix: drop the ORCA_TERMINAL assignment from the receipt at line 5162 (or assign it only after the equality check passes).bin/fm-spawn.sh:1306- A failed native relaunch can still leave a record that no lifecycle command accepts. On--relaunch, suppose worker-start returns a retry Dispatch D2 but omitsterminalIncarnationorpaneKey. worker_start returns 1 at orca-supervised.sh:193-194, and lines 5153-5160 copy D2 and the other returned ids while ORCA_PANE_KEY/ORCA_TERMINAL_INCAR stay empty (they are initialized empty, not from the prior record). Iforca_supervised_abort_reconcilethen fails, the trap reaches theelifat 1306 because SPAWN_FRESH_COMMIT_PENDING=0 on relaunch. The reconcile fails when the worker is ambiguous and gets abandoned (rc 2), or when the stop/poll doesn't reach dead within FM_ORCA_EXIT_POLLS.orca_supervised_meta_publishthen writesorca_pane_key=/orca_terminal_incarnation=with empty values and nocleanup_recoverykey.fm_backend_validate_task_endpoint(fm-backend.sh:546-551) refuses that record, so relaunch, teardown, fm-control and procevent all lock out, and D2 is left unsettled. This is the relaunch sibling of the round-5 and round-6 findings. Any honest remedy needs a policy decision: a recovery-typed relaunch record must not let teardown's ship recovery branch delete preserved relaunch work, and keeping the prior identities would leave D2 untracked. That remedy, not the defect, is what needs authorization.bin/fm-spawn.sh:3316- A native relaunch opens replacement terminal T2 here, but ORCA_ABORT_CLEANUP stays 0 until line 5134. The published relaunch record also names the prior terminal (terminal=${ORCA_PRIOR_TERMINAL:-...}at 4671). Any exit between 3316 and 5134 therefore leaves T2 open in the task workspace, and no record, trap or teardown will ever close it. Such exits include harness hook install failures, record publish failures, and the launch-dir/launch-file refusals at 5026-5050. A second relaunch opens another terminal on top of it. There is a related gap: when attach succeeds and records T2, and then worker-show or the first-turn proof fails, the trap closes T2 but never closes the prior T1. The record no longer names T1, so it is orphaned too. Fix: have the trap close ORCA_TERMINAL when ORCA_PRIOR_TERMINAL is set and differs from it, for example by arming a relaunch-only cleanup flag right after T2 is created, and close ORCA_PRIOR_TERMINAL once the record has moved to T2.bin/fm-teardown.sh:436- This commit deletesFM_LOCK_LOG_PREFIX=teardown, which has nothing to do with the Orca work. fm-teardown.sh still sources bin/fm-lock-lib.sh (line 299), andfm_lock_logprints${FM_LOCK_LOG_PREFIX:-fm-lock}: .... Teardown's stale-lock diagnostics now come out asfm-lock:instead ofteardown:, so they no longer identify their caller the way fm-lock-lib documents ('so each caller's output stays recognizable'). No intent requirement needs this change. Restore the line.🔧 Fix applied.
✅ Re-checked - no issues remain.
bin/fm-teardown.sh:3591- Observed live: for an ordinary native supervised task, teardown runsreap_task_worktree_processes(and detaches or deletes the branch) beforesettle_native_orca_metaproves Dispatch ownership. When ownership can't be proven, teardown prints 'preserving its durable records', but it has already killed the processes in that worktree (live run: 'reaping leaked worktree process(es) ... 10587 11025 13293', then REFUSED). A later teardown rerun can still settle a real Dispatch, so nothing leaks, but the refusal isn't side-effect free. Informational only; the ordering predates this change for other backends.FM_ORCA_SUPERVISED_LIVE=1 bin/fm-test-run.sh tests/fm-backend-orca.test.sh(exit 0)bin/fm-test-run.sh tests/fm-control.test.sh(exit 0)bin/fm-test-run.sh tests/fm-secondmate-restart.test.sh(exit 0)Live gate probe viafm_backend_orca_supervised_capability_checkfor claude/codex/pi inside a real Orca coordinator terminal (sent withorca terminal send)Live gate probe outside Orca with ORCA_TERMINAL_HANDLE unset and with a stale handlefm-spawn.sh smlive1 <home> --harness claude --backend orca --secondmateoutside Orca (no handle and stale handle), plus a raw-launch Secondmate; Orca worktree count checked before and afterfm-spawn.sh fmlivescout1 <proj> --scout --harness 'bash -c ...' --backend orca --orca-mode supervisedagainst real Orca, thenfm-peek.shandfm-crew-state.shForgedorca_mode=supervisedrecord and forgedcleanup_recovery=orcarecord on a real Orca worktree:fm-peek.sh,fm-crew-state.sh,fm-send.sh,fm-teardown.shfm-captain-hold.sh complete fmlivescout1 --nonethenfm-teardown.sh fmlivescout1; Orca worktree row and terminal state checked afterwardsfm-spawn.sh fmlivenat1 <proj> --scout --harness claude --backend orca --orca-mode supervisedrun inside the real Orca coordinator terminal; thenorca orchestration worker-list --run <lab run>,orca worktree ps,orca terminal showDirectorca orchestration worker-start --terminalprobe to name the attach refusal (agent_unconfigured)✅ **Document** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.