Skip to content

Re-resolve Orca terminal handles after restart instead of treating them as stable #683

Open

Description

@e-jung

Summary

FirstMate's experimental Orca backend records terminal=<handle> in task metadata and documents that value as a stable handle used for capture, send, current-state reads, and teardown. Orca now has a reproducible open bug where terminal/session handles change across create/list calls and runtime restarts (stablyai/orca#9163), making cached handles stale and breaking message addressing.

Make the FirstMate Orca adapter treat the recorded handle as a hint, re-resolve the current terminal identity before every consequential operation, and fail closed when it cannot prove the replacement refers to the same task terminal.

Current mismatch

FirstMate docs/orca-backend.md currently says:

terminal= carries the stable Orca terminal handle that backend operations use.

stablyai/orca#9163 demonstrates that:

  • orca terminal create and orca terminal list can return different handles for the same tab/PTY;
  • a continuous coordinator or worker session can receive several handles over its lifetime;
  • old handles eventually return terminal_handle_stale or fail to receive injected messages.

This invalidates the adapter's persistence assumption, particularly after Orca restart/reconnect—the exact point where FirstMate recovery needs reliable identity.

Proposed behavior

  • Persist stable identity fields in addition to the transient handle: Orca worktree id, tab id, pane key/leaf id, terminal title/alias, and PTY id where the supported CLI exposes them.
  • Before peek/send/state/close, list terminals within the recorded Orca worktree and resolve exactly one candidate using stable identity.
  • Update local metadata atomically when a replacement handle is proven.
  • Refuse destructive close/teardown on zero or multiple candidates; preserve the worktree and report the ambiguity.
  • Reconcile handles at FirstMate session start and after any terminal_handle_stale response.
  • Keep Orca #9163 as the upstream product fix; this issue is the adapter's defensive compatibility behavior.

Acceptance criteria

  • A fake-Orca test changes the terminal handle after spawn while preserving stable pane/tab identity; peek and send re-resolve and succeed.
  • A restart/reconnect recovery test updates metadata to the current handle.
  • Ambiguous duplicate candidates fail closed without sending input or closing anything.
  • Teardown never trusts a stale handle to identify the terminal being destroyed.
  • Documentation stops calling runtime-issued handles stable.

Related upstream: stablyai/orca#9163

Prepared with AI assistance from current FirstMate/Orca documentation and the upstream reproduction. Reporter: @ericjung.

Activity

  1. str0203 commented on Aug 10, 2026

    @str0203

    The upstream side of this dependency is now explicitly linked from stablyai/orca#9163, and the supervised-worker migration is tracked in #1813.

    I suggest preserving two implementation horizons here:

    Immediate adapter defense

    • Treat the recorded terminal handle as transient.
    • On terminal_handle_stale or session start, re-resolve within the recorded worktree using supported stable fields.
    • Atomically replace metadata only when exactly one candidate is proven.
    • Zero or multiple candidates refuse send/control/teardown and preserve state.

    Durable identity migration

    The restart smoke proposed in #2084 should exercise both modes: rotate the terminal handle after spawn, verify non-destructive read/send recovery, then verify ambiguous duplicates refuse cleanup. This keeps the current adapter safe without blocking on the upstream canonical-identity fix, while giving a clean deletion path once supervised-worker identity is adopted.

  2. kunchenguid commented on Aug 24, 2026

    @kunchenguid
    Owner

    Speaking as Kun's firstmate: inspected on current main 7b88520c0554.

    VISION.md (each rule):

    • One captain, one interface: aligns. Stale handles drop injects after restart.
    • Authority is explicit: n/a.
    • Scripts own the mechanics: aligns. Re-resolve or fail closed is a script.
    • A restart is a non-event: aligns. Treating handles as stable is the opposite of a restart-safe fleet.
    • Delegation with a spine: aligns.
    • The fleet outlives any vendor: aligns. Bind to identity Orca actually keeps, not today's handle pixels. Upstream stablyai/orca#9163 does not block a fail-closed re-resolve here.
    • Scope: aligns.

    Evidence: docs/orca-backend.md still says terminal= is the stable handle. bin/backends/orca.sh still records and uses terminal-handle with no re-resolve-before-send. Defect still present. Related #1813. Labeling ready-for-pr.

  3. added
    ready-for-pr
    on Aug 24, 2026
  4. kunchenguid commented on Aug 24, 2026

    @kunchenguid
    Owner

    Speaking as Kun's firstmate:

    VISION.md read in full from current main 7b88520c055408a18f1476ecce08be60b2885fc9. Per-rule, inspected bin/backends/orca.sh send/read/close, bin/fm-spawn.sh terminal= persistence, docs/orca-backend.md, tests/fm-backend-orca.test.sh, thread + #1813:

    • One captain, one interface: aligns. Sending or tearing down against a stale handle hides a failure as a missing pane.
    • Authority is explicit: aligns. Fail closed on zero or many candidates; never infer which terminal to destroy.
    • Scripts own the mechanics: aligns. Re-resolve-or-refuse is exact. Treating a recorded handle as stable is a script guessing a vendor pixel.
    • A restart is a non-event: aligns. This is the Orca-restart identity hole. Recovery must rebind from durable fields, not from chat memory of the spawn-time handle.
    • Delegation with a spine: aligns. Adapter defense, not a new task shape.
    • The fleet outlives any vendor: aligns. Bind to identity Orca actually keeps stable (worktree / tab / pane / PTY, once proven), not to today's handle string. Upstream [Bug]: Terminal/session handle instability breaks orchestration message addressing and injection stablyai/orca#9163 remains the product fix; this issue is defensive compatibility.
    • Scope: aligns. Command-layer adapter. Durable dispatch identity is Orca backend: adopt Orca's supervised-worker primitives instead of raw worktree + terminal #1813; this ticket is the immediate re-resolve/fail-closed behavior on the current adapter.

    Contributor report (e-jung): Orca handles change across create/list and runtime restarts (stablyai/orca#9163). Firstmate records terminal= and uses it as a stable handle for capture, send, current-state, and teardown. Proposed: persist additional identity fields; re-resolve to exactly one candidate before peek/send/state/close; atomically update metadata when proven; refuse destructive close on 0 or N>1; reconcile at session start and on terminal_handle_stale. str0203 (2026-08-10): keep two horizons — immediate adapter defense here, durable dispatch identity once #1813 lands; #2084 should exercise both.

    Inspected current main 7b88520c055408a18f1476ecce08be60b2885fc9:

    • bin/fm-spawn.sh:758-759,2686-2688: spawn writes terminal=$ORCA_TERMINAL as durable metadata. docs/orca-backend.md:38-44 still names terminal= (with orca_worktree_id=) as backend authority for operation and cleanup.
    • bin/backends/orca.sh:157-205,256-290: terminal create/read/send/close take the recorded handle as --terminal with no list-and-re-resolve, no terminal_handle_stale handling, no candidate proof, no metadata refresh. fm_backend_orca_json_get still extracts only result.terminal.handle.
    • tests/fm-backend-orca.test.sh: no re-resolve fixture, no restart/reconnect handle rotation, no ambiguous-duplicate fail-closed case (0 hits for re-resolve).
    • No open covering PR. Orca backend: adopt Orca's supervised-worker primitives instead of raw worktree + terminal #1813 is the supervised-worker identity migration, not a substitute for this adapter defense. Do not close this waiting on Orca backend: adopt Orca's supervised-worker primitives instead of raw worktree + terminal #1813.

    Still present. Constraints for a patch: treat the recorded handle as a hint; re-resolve within the recorded worktree using supported stable fields before peek/send/state/close; replace metadata only when exactly one candidate is proven; zero or multiple candidates refuse send/control/teardown and preserve the worktree; keep Orca #9163 as the upstream product fix. Fake-Orca tests: handle rotates after spawn, peek/send succeed; restart updates metadata; duplicates fail closed; teardown never trusts a stale handle.

    ready-for-pr is a queue label, not a merge vote.

  5. added
    bug
    on Sep 24, 2026
  6. devin-ai-integration commented on Sep 24, 2026

    @devin-ai-integration
    No description provided.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugready-for-pr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions