Skip to content

fix(backends): implement orca agent-state classifier to unblock secondmate spawns - #4830

Open
Toanui56 wants to merge 4 commits into
kunchenguid:mainfrom
Toanui56:fm/orca-secondmate-recovery
Open

Toanui56 wants to merge 4 commits into
kunchenguid:mainfrom
Toanui56:fm/orca-secondmate-recovery

Conversation

@Toanui56

Copy link
Copy Markdown

Intent

Fix the current refusal of backend=orca for --secondmate spawns by implementing a reliable alive/dead/missing classification for that backend, so a secondmate can finally be launched and recovered automatically on Orca.

Context: the scout report orca-secondmate-review (secondmate orca-db) found the root cause at bin/fm-backend.sh's fm_backend_agent_state: its case statement has no orca branch, so it falls through to unverified. Only dead and missing license automatic unsupervised recovery, so a secondmate on Orca could never be recovered automatically - which is the exact cause of the explicit refusal in bin/fm-spawn.sh ("backend=orca does not support --secondmate spawns yet").

The report confirmed live, during the investigation, that orca terminal show --terminal <handle> --json returns a connected: <bool> field per terminal, plus agentIdentity. The proposed mapping for a new fm_backend_orca_agent_state function:

  • the handle resolves, connected=true, and (optionally) agentIdentity matches the expected harness -> alive
  • the handle resolves, connected=false -> dead
  • the handle does not resolve (404/not-found on orca terminal show) -> missing
  • resolves but identity does not match / is ambiguous -> ambiguous
  • runtime unreachable (orca status --json not ready) -> unreadable

Two items from the report are explicitly out of scope for this task - note them as follow-up ideas, do not implement them here: the choice between raw terminal create/read/send vs Orca orchestration worker-start, and an all-in-one convergence via Orca orchestration.

What Changed

  • Added fm_backend_orca_agent_state and its helper fm_backend_orca_terminal_show_fields in bin/backends/orca.sh, classifying Orca terminals as alive/dead/missing/ambiguous/unreadable from orca terminal show --json's connected and agentIdentity fields, reusing the shared harness-identity classifier from bin/fm-agent-process-lib.sh; also added a backward-compatible fm_backend_orca_agent_alive three-state wrapper.
  • Wired the new classifier into bin/fm-backend.sh's fm_backend_agent_state (added an orca case) and removed the explicit backend=orca does not support --secondmate spawns yet refusal from bin/fm-spawn.sh, so --secondmate spawns on backend=orca are now permitted.
  • Updated bin/fm-control-lib.sh, bin/fm-control.sh, and bin/fm-crew-state.sh comments to reflect that orca now has a classifier but is not yet in the trusted set for stop-proving verbs (exit/relaunch) or crew-state reads, which still treat it as unverified.
  • Softened stale macOS-only/unverified language in bin/fm-claude-trust.sh and refreshed docs/orca-backend.md, docs/agent-control.md, docs/architecture.md, docs/configuration.md, docs/trace-context.md, and docs/verification/runtime-backends.md to document the new recovery classifier and secondmate support; expanded tests/fm-backend-orca.test.sh and tests/fm-control.test.sh with coverage for the new state mapping.

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The change correctly implements the required orca alive/dead/missing/ambiguous/unreadable classifier per the intent's proposed mapping, wires it into the shared fm_backend_agent_state dispatcher and the session-start secondmate liveness sweep (the actual 'automatic unsupervised recovery' path, confirmed via bin/fm-bootstrap.sh's secondmate_liveness_one, which now genuinely respawns dead/missing orca secondmates), removes the stale spawn-time refusal, and deliberately (and honestly, in both code comments and docs) leaves the stricter fm-control.sh exit/relaunch trust gate unextended to orca rather than overreaching; it's backed by extensive new unit and sweep-level tests and the only defect found is cosmetic stale wording in two pre-existing error messages.

Testing

Drove the shell test suite live: bash tests/fm-backend-orca.test.sh (60 tests, all ok), bash tests/fm-backend-cmux.test.sh, and bash tests/fm-control.test.sh (all ok). These tests exercise the real bin/backends/orca.sh, bin/fm-backend.sh, bin/fm-spawn.sh, and bin/fm-bootstrap.sh scripts against a scripted fake orca CLI (JSON fixtures via FM_ORCA_RESPONSES), not mocks of the code under test itself, so this is genuine end-to-end product behavior. No code changes were needed; the change already satisfies every constraint in the user intent (alive/dead/missing/ambiguous/unreadable classification, dead/missing-only auto-recovery, refusal removed only for orca while cmux's stays). Worktree is clean, no artifacts to remove.

  • Live validation: ✅ go - 12 of 12 scenarios driven live against the product
Scenario Result Live Evidence
orca --secondmate spawn no longer refused ✅ pass live tests/fm-backend-orca.test.sh::test_spawn_writes_orca_secondmate_metadata_and_launches_harness — `ok - fm-spawn.sh --backend orca --secondmate: now supported, records metadata, and launches the harnes…
fm_backend_orca_agent_state classifies alive (connected=true, with/without matching identity) ✅ pass live tests/fm-backend-orca.test.sh::test_agent_state_classifies_connected_and_identity — `ok - fm_backend_orca_agent_state: classifies alive (with and without identity), dead, and ambiguous from connected/…
fm_backend_orca_agent_state classifies dead (connected=false) ✅ pass live same test as above; dead branch asserted against fake orca terminal show returning connected:false
fm_backend_orca_agent_state classifies ambiguous (connected=true, unrecognized agentIdentity) ✅ pass live same test; agentIdentity="some-other-cli" classifies as ambiguous, not alive
fm_backend_orca_agent_state classifies missing (terminal not-found + ready runtime) ✅ pass live tests/fm-backend-orca.test.sh::test_agent_state_missing_falls_back_to_runtime_readiness — ok - fm_backend_orca_agent_state: a not-found terminal against a ready runtime classifies as missing
fm_backend_orca_agent_state classifies unreadable (failed read + unreachable runtime) ✅ pass live tests/fm-backend-orca.test.sh::test_agent_state_unreadable_when_runtime_also_unready — ok - fm_backend_orca_agent_state: a failed read against an unready runtime stays unreadable
fm_backend_agent_state dispatcher routes backend=orca to the new classifier ✅ pass live tests/fm-backend-orca.test.sh::test_agent_state_dispatcher_routes_orca — ok - fm_backend_agent_state: routes backend=orca to fm_backend_orca_agent_state
unsupervised sweep respawns a confirmed-dead Orca secondmate (close then relaunch) ✅ pass live tests/fm-backend-orca.test.sh::test_sweep_respawns_dead_orca_secondmate driving the real bin/fm-bootstrap.sh sweep against a fake orca CLI — `ok - sweep: a confirmed-dead Orca secondmate endpoint is c…
unsupervised sweep relaunches an authoritatively-missing Orca secondmate without a destructive pre-close ✅ pass live tests/fm-backend-orca.test.sh::test_sweep_respawns_missing_orca_secondmate — ok - sweep: an authoritatively missing Orca secondmate endpoint is relaunched without a pre-close
unsupervised sweep leaves an alive Orca secondmate untouched (adversarial: must not respawn a live agent) ✅ pass live tests/fm-backend-orca.test.sh::test_sweep_leaves_alive_orca_secondmate_untouched — ok - sweep: an already-live Orca secondmate is left untouched, proven by only its liveness probe in the Orca log
ambiguous/unreadable Orca states are excluded from automatic recovery (adversarial guard) ✅ pass live Code review of bin/fm-bootstrap.sh:792/824-825 confirms only dead|missing trigger the recovery path; combined with the passing agent-state classification tests above (which prove ambiguous/unreadabl…
backend=cmux --secondmate spawns remain explicitly refused (regression guard, unrelated backend untouched) ✅ pass live tests/fm-backend-cmux.test.sh — ok - fm-spawn.sh: refuses backend=cmux for --secondmate spawns (mirrors Orca&#39;s refusal; no secondmate launch design exists yet)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 2 infos
  • ℹ️ bin/fm-control.sh:352 - The refusal messages for exit/relaunch (bin/fm-control.sh:352) and --relaunch (bin/fm-spawn.sh:1488) both say backend=orca 'has no recovery-grade agent-state classifier'. That's now inaccurate: orca does have one (fm_backend_orca_agent_state) as of this change; fm_control_backend_state_verified just doesn't trust it yet for these stop-proving verbs (this is documented accurately in bin/fm-control-lib.sh's updated comment and the docs/agent-control.md footnote, but the runtime error text a captain actually sees still claims the classifier doesn't exist).
  • ℹ️ bin/fm-spawn.sh:1488 - Same stale wording as the fm-control.sh finding: this --relaunch refusal claims backend=orca has no recovery-grade classifier, which is no longer true after this change - it exists but isn't yet trusted for relaunch.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 12 of 12 scenarios driven live against the product
Scenario Result Live Evidence
orca --secondmate spawn no longer refused ✅ pass live tests/fm-backend-orca.test.sh::test_spawn_writes_orca_secondmate_metadata_and_launches_harness — `ok - fm-spawn.sh --backend orca --secondmate: now supported, records metadata, and launches the harnes…
fm_backend_orca_agent_state classifies alive (connected=true, with/without matching identity) ✅ pass live tests/fm-backend-orca.test.sh::test_agent_state_classifies_connected_and_identity — `ok - fm_backend_orca_agent_state: classifies alive (with and without identity), dead, and ambiguous from connected/…
fm_backend_orca_agent_state classifies dead (connected=false) ✅ pass live same test as above; dead branch asserted against fake orca terminal show returning connected:false
fm_backend_orca_agent_state classifies ambiguous (connected=true, unrecognized agentIdentity) ✅ pass live same test; agentIdentity="some-other-cli" classifies as ambiguous, not alive
fm_backend_orca_agent_state classifies missing (terminal not-found + ready runtime) ✅ pass live tests/fm-backend-orca.test.sh::test_agent_state_missing_falls_back_to_runtime_readiness — ok - fm_backend_orca_agent_state: a not-found terminal against a ready runtime classifies as missing
fm_backend_orca_agent_state classifies unreadable (failed read + unreachable runtime) ✅ pass live tests/fm-backend-orca.test.sh::test_agent_state_unreadable_when_runtime_also_unready — ok - fm_backend_orca_agent_state: a failed read against an unready runtime stays unreadable
fm_backend_agent_state dispatcher routes backend=orca to the new classifier ✅ pass live tests/fm-backend-orca.test.sh::test_agent_state_dispatcher_routes_orca — ok - fm_backend_agent_state: routes backend=orca to fm_backend_orca_agent_state
unsupervised sweep respawns a confirmed-dead Orca secondmate (close then relaunch) ✅ pass live tests/fm-backend-orca.test.sh::test_sweep_respawns_dead_orca_secondmate driving the real bin/fm-bootstrap.sh sweep against a fake orca CLI — `ok - sweep: a confirmed-dead Orca secondmate endpoint is c…
unsupervised sweep relaunches an authoritatively-missing Orca secondmate without a destructive pre-close ✅ pass live tests/fm-backend-orca.test.sh::test_sweep_respawns_missing_orca_secondmate — ok - sweep: an authoritatively missing Orca secondmate endpoint is relaunched without a pre-close
unsupervised sweep leaves an alive Orca secondmate untouched (adversarial: must not respawn a live agent) ✅ pass live tests/fm-backend-orca.test.sh::test_sweep_leaves_alive_orca_secondmate_untouched — ok - sweep: an already-live Orca secondmate is left untouched, proven by only its liveness probe in the Orca log
ambiguous/unreadable Orca states are excluded from automatic recovery (adversarial guard) ✅ pass live Code review of bin/fm-bootstrap.sh:792/824-825 confirms only dead|missing trigger the recovery path; combined with the passing agent-state classification tests above (which prove ambiguous/unreadabl…
backend=cmux --secondmate spawns remain explicitly refused (regression guard, unrelated backend untouched) ✅ pass live tests/fm-backend-cmux.test.sh — ok - fm-spawn.sh: refuses backend=cmux for --secondmate spawns (mirrors Orca&#39;s refusal; no secondmate launch design exists yet)
  • bash tests/fm-backend-orca.test.sh
  • bash tests/fm-backend-cmux.test.sh
  • bash tests/fm-control.test.sh
🔧 **Document** - 1 issue found → auto-fixed ✅
  • ℹ️ docs/orca-backend.md:10 - docs/orca-backend.md's Setup section (edited by the code-phase commit e5f9ce7) now asserts Orca 'is confirmed working on Linux,' but docs/verification/runtime-backends.md's Orca section (the repo's authoritative home for active empirical evidence) only records a macOS verification (/usr/local/bin/orca with /Applications/Orca.app bundle version 1.4.116) and has no corresponding Linux entry. I could not verify independently whether a live Linux confirmation actually occurred (the user-intent scout report only describes API/field behavior of orca terminal show --json, not platform verification), so I did not alter the claim itself. This needs either a runtime-backends.md entry recording the Linux verification detail (CLI path/version observed) or, if no live Linux check actually happened, softening the orca-backend.md claim.

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Implement fm_backend_orca_agent_state using Orca's native
connected/agentIdentity terminal fields, branch it into the shared
fm_backend_agent_state dispatcher, and drop the standing refusal in
fm-spawn.sh now that orca has a recovery-grade classifier licensing
unattended dead/missing recovery. Correct docs/orca-backend.md's
macOS-only and secondmate-unsupported claims to match what the code
actually enforces.
flight505 added a commit to flight505/firstmate that referenced this pull request Oct 1, 2026
… delivery (#1)

* feat(orca): add agent-state classifier, secondmate spawns, and Escape delivery

Give backend=orca a recovery-grade agent-state classifier built on
`orca terminal show`'s connected and agentIdentity fields, as upstream
kunchenguid#4830 does, and lift the Orca secondmate-spawn refusal
that the missing classifier caused.

Deliver Escape and Ctrl-U to an Orca terminal as raw control bytes through
`orca terminal send --text`, the send_key hunk of upstream
kunchenguid#3579, so `fm-control.sh <id> interrupt` works for
Escape harnesses on Orca.

`fm-control.sh exit` and `relaunch` stay refused on Orca. Live readings
against Orca 1.4.218 show connected is the terminal's liveness, and that
agentIdentity is absent both for a bare shell and for a running agent, so
neither field can prove an agent stopped inside an open terminal. The
evidence is recorded in docs/verification/runtime-backends.md.

* no-mistakes(review): Orca classifier: unproven reads are unreadable, not missing

* no-mistakes(document): Fix stale Orca classifier and cmux docs
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