diff --git a/config/vitest.performance.config.ts b/config/vitest.performance.config.ts index 26813d404e9..d8fecb02f77 100644 --- a/config/vitest.performance.config.ts +++ b/config/vitest.performance.config.ts @@ -16,6 +16,7 @@ const contracts = [ 'src/main/runtime/rpc/methods/repo.test.ts', 'src/renderer/src/components/tab-bar/use-new-tab-agent-menu-host-preferences.test.tsx', 'src/main/sqlite/sync-database.test.ts', + 'src/main/runtime/orchestration/run-binding-performance.test.ts', 'src/main/runtime/orchestration/db/row-column-lists.test.ts', 'src/shared/native-chat/tools/native-chat-tool-attribution-allocation.test.ts', 'src/shared/native-chat/tools/native-chat-tool-pairing.performance.test.ts', diff --git a/skill-guides/orchestration.md b/skill-guides/orchestration.md index 1ab928de921..a5928f65913 100644 --- a/skill-guides/orchestration.md +++ b/skill-guides/orchestration.md @@ -108,6 +108,12 @@ ORCA orchestration worker-start --spec "" --worktree current --ag ORCA orchestration check --wait --types "worker_done,escalation,question" --timeout-ms 900000 --json ``` +After a restart, resume your own Run with `run-use --id `. A worker +cannot bind the Run it works for or an ancestor, and cannot use +`--takeover-legacy`; an already-bound no-op remains allowed. When its +coordinator directs a handoff, a replacement worker may resume a separate Run +with plain `run-use`. + If `worker-start` exits non-zero, do not relaunch. Read the receipt's `failedStage` and `residualResources`, then load `references/recovery-and-cleanup.md`. diff --git a/skill-guides/orchestration/references/worker-contract.md b/skill-guides/orchestration/references/worker-contract.md index 92cd56e66a8..a7ad813a778 100644 --- a/skill-guides/orchestration/references/worker-contract.md +++ b/skill-guides/orchestration/references/worker-contract.md @@ -51,7 +51,12 @@ run — and once more immediately before `worker_done`, so a redirect or a cancellation lands before the Task settles. `check` names its caller with `--terminal`, never `--from`. Stop checking after `worker_done`. -If `check` returns `consumer_fenced`, this process no longer owns its Dispatch: +Do not use `run-use` to check your Dispatch's mail or bind the Run you work for. +You may resume a separate Run when your coordinator directs a handoff, but you +cannot use `--takeover-legacy`. Plain `check` returns your Dispatch's mail +unless you are coordinating a Run; report to your coordinator with `send`. + +If a plain `check` (no `--run`) returns `consumer_fenced`, this process no longer owns its Dispatch: the Attempt was re-attached to another worker or settled without you. Stop, do not send `worker_done`, and do not retry the check. An empty `check` never means you were replaced; `consumer_fenced` is the only way you learn that. diff --git a/src/cli/bundled-skill-guides.ts b/src/cli/bundled-skill-guides.ts index f99e402944a..5a681a41639 100644 --- a/src/cli/bundled-skill-guides.ts +++ b/src/cli/bundled-skill-guides.ts @@ -72,10 +72,10 @@ const ORCA_PER_WORKSPACE_ENV_SSH_HOST_REFERENCE_MARKDOWN = "# SSH connection mod const ORCA_PER_WORKSPACE_ENV_WINDOWS_SCRIPTS_REFERENCE_MARKDOWN = "# Windows local-side scripts\n\nLoad this when the user's desktop is Windows and you are scaffolding the local-side scripts. A bare\n`.sh` will not execute there. Either require WSL or Git Bash and point `orca.yaml` at a launcher such\nas `bash ./scripts/orca-vm/.sh` through a `.cmd` file, or scaffold PowerShell equivalents.\n\nThe remote-side commands you run inside the Linux environment stay bash regardless of the desktop OS.\n\n```powershell\n#requires -Version 5\n$ErrorActionPreference = 'Stop'\n# resolve env→state→fallback; run the provider CLI / ssh the same way;\n# capture provider output; build the result object for the chosen mode and write ONE line of JSON to stdout.\n# Orca-server mode: @{ schemaVersion=1; pairingCode=$pairingCode; projectRoot=$projectRoot; userData=@{...} }\n# SSH mode: @{ schemaVersion=1; connection=@{ type=\"ssh\"; projectRoot=$projectRoot;\n# target=@{ label=$label; host=$host; port=$port; username=$user } } }\n($result | ConvertTo-Json -Compress -Depth 6)\n# progress/errors → Write-Error / the error stream, never stdout.\n```\n\nThe doctor's executable-bit check is a POSIX concept and is skipped on Windows, so a script that is\nunusable on the user's machine for a different reason still has to be caught by the `--provision`\nself-test.\n" // oxfmt-ignore -const ORCHESTRATION_MARKDOWN = "---\nname: orchestration\ndescription: >-\n Coordinate supervised Orca workers: threaded messages, blocking ask/reply,\n task dispatch, worker_done/escalation waits, task DAGs, decision gates,\n coordinator loops, and decomposing work across agents. Use `orca-cli` for full\n ownership handoffs — \"hand off\", \"handoff\", \"handover\", \"give this to another\n agent\", \"another worktree\" — unless asked to supervise, monitor, or coordinate\n a DAG, and for terminal control, lightweight terminal prompts, shell commands,\n Orca worktree management, and reading or waiting on terminals.\n---\n\n# Orca orchestration\n\nOrchestration is Orca's structured coordination layer. It records who owns work,\nwhich attempt is authoritative, and when supervised work has settled.\n\n## Outcome\n\n**Result:** every in-scope Task has one explicit outcome and every settled worker\nterminal has a next owner or cleanup decision. **Next consumer:** the user who\nrequested supervision. **Done:** all expected Dispatches have settled, every\ndelivered message was processed before acknowledgment, each settled worker was\nreused, explicitly retained, or released, and the turn ends only when the report\nto that user names, per Task, its outcome, the evidence behind it, and any\nunresolved blocker.\n\n**Safe failure:** preserve work and authority and report the state as unknown or\n`unverifiable`. Only positive proof of exit authorizes stop, abandon, or retry,\nand only an accepted settlement authorizes release. Every other observation,\nabsence included, is a checkpoint.\n\n## Classify the role\n\n| Current context | Role | Route |\n| ---------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------ |\n| The user explicitly asks to supervise, monitor, wait for results, track completion, coordinate a DAG, use a decision gate, or manage ask/reply | Coordinator | Use the supervised loop below |\n| The current prompt contains a live injected preamble with Task and Dispatch IDs | Dispatched worker | Follow the preamble and the worker obligations below |\n| The user asks to hand off ownership or start another agent/worktree without supervision | Handoff owner | Use `orca-cli`; create no Run, Task, or Dispatch and do not monitor completion |\n| A message carries a legacy authority label | Compatibility operator | Load the legacy contract reference before any lifecycle mutation |\n| No live preamble and no explicit supervision | Ordinary terminal agent | Do not emit lifecycle messages; use `orca-cli` for terminal/worktree work |\n\nModel or effort selection does not make a handoff supervised: `orca-cli`'s\n`worktree create --agent` and `agent launch` take `--model` and `--effort`, and\nopen a chat or a terminal per the user's Chat UI setting, as `worker-start` does.\nNever substitute a non-Orca subagent tool when Orca orchestration provenance was\nrequested.\n\n## Authority and safety floor\n\n- A Run is a durable namespace and coordinator inbox; it does not schedule or\n place workers. A Task is work. A Dispatch is one authoritative Task attempt.\n- Lifecycle authority comes from the active Dispatch, not a terminal title,\n copied ID, old database row, provider transcript, or visible pane.\n- Workers use the exact executable, handle, Task ID, and Dispatch ID in the live\n preamble, plus any other flag it carries. Never reconstruct, translate, or\n broaden those arguments.\n- After remote start, address the worker by Dispatch ID. The execution host owns\n process, filesystem, transcript, stop, and cleanup facts. Preserve the verdicts\n `live` / `unverifiable` / `exited`; contact loss is not process death.\n- Liveness is layered: `worker-list`'s `projection.liveness` is the fleet verdict\n for the agent; `worker-show`'s `observation.status` is PTY liveness only. A live\n terminal can still hold a dead or stuck agent.\n- Folder workspaces are valid; never require Git or assume a worktree.\n- Clients and remote servers update independently. Treat unknown optional fields\n as absent. A new stream operation requires advertised capability because old\n decoders may silently drop unknown opcodes. Never fall back to local execution\n when remote authority or capability is unproven.\n- Use the executable you used to run `skills get` for the entire run. In the\n examples below, replace `ORCA` with it; do not create a shell variable or run\n `ORCA` literally. If it fails, report that exact error instead of switching.\n- `ORCA status --json` shows your Orca session ID as `caller.orcaSessionId` when you have one.\n- A successful `orchestration send` proves durable enqueue; its wake or nudge is\n best-effort attention only and does not prove the recipient read or accepted it.\n\n## Worker obligations\n\nThe injected preamble is authoritative. A dispatched worker must:\n\n1. Do only the current Task and use the preamble's `ask` command for a blocking\n coordinator question. Never open a local question TUI the coordinator cannot\n answer. Resume the same message ID after an ask timeout.\n2. Send heartbeats only at the cadence in the preamble. A heartbeat proves\n liveness, not completion.\n3. Read coordinator follow-ups at each natural checkpoint — before starting a\n new file, after a test run — and once more immediately before `worker_done`:\n `ORCA orchestration check --terminal --json`.\n4. Send `worker_done` exactly once, from the dispatched terminal, with a\n three-sentence executive summary, both lifecycle IDs, and explicit\n `--outcome succeeded` or `--outcome failed`. Never encode failure only in prose.\n5. Append `--files-modified` and `--report-path` only with real values when\n applicable. After `worker_done`, end the dispatched turn and idle; do not poll\n or start new work.\n\nA direct user instruction after completion starts new user-owned work and takes\nprecedence over the idle rule. Do not reuse the settled lifecycle IDs.\n\n## Canonical supervised loop\n\nConfirm the runtime, bind one Run, and start the full independent wave before\nwaiting. `worker-start --spec` creates the Task and its attempt in one call:\n\n```text\nORCA status --json\nORCA orchestration run-create --objective \"\" --json\nORCA orchestration worker-start --spec \"\" --worktree current --agent codex --json\nORCA orchestration worker-start --spec \"\" --worktree current --agent claude --json\nORCA orchestration check --wait --types \"worker_done,escalation,question\" --timeout-ms 900000 --json\n```\n\nIf `worker-start` exits non-zero, do not relaunch. Read the receipt's\n`failedStage` and `residualResources`, then load\n`references/recovery-and-cleanup.md`.\n\nUse `task-create` plus `worker-start --task ` for planned fan-out with\ndependencies or a retry of a known Task. Use dependencies only for real ordering\nand prefer parallel waves over chains deeper than three or four steps; nested\nworkers obey the depth limit, and a new Run does not reset the caller's depth.\nA dispatched worker starts its own helpers with `worker-start --spec` on the Run\nit works for, without `run-create`; the helpers report to that worker.\n\nA consuming `check` names its caller with `--terminal `, never `--from`;\nomit it inside the coordinator's own Orca terminal. It returns the bound Run's\noldest FIFO Delivery and replays that batch until acknowledged. Process every\nmessage: reply to questions, validate each `worker_done` against the expected\nactive Dispatch, and decide each settled terminal's next owner before the ack:\n\n```text\nORCA orchestration reply --id --body \"\" --json\nORCA orchestration worker-release --dispatch --json\nORCA orchestration check --ack --wait --types \"worker_done,escalation,question\" --timeout-ms 900000 --json\n```\n\nKeep waiting until every expected Dispatch settles. A timeout or empty result is\na checkpoint, not a failure. Do not stop, retry, release, or launch a duplicate\neditor without the positive proof `## Outcome` requires.\n\nAfter three consecutive empty waits, stop waiting blindly and enumerate with\n`ORCA orchestration worker-list --include-remote --json` (defaults to the bound\nRun; `--run ` overrides; the receipt's `scope` names which), acting on\neach row's `projection.attention` categories, `projection.attention.requiresAction`, and literal `projection.nextAction` argv.\nRows come newest first and page at 100: while `page.hasMore`, follow `page.nextCursor` with `--cursor `.\nA `none` `nextAction` has no argv to run: read `liveness.reason` and keep waiting\nwith `check --wait`. Absence never earns an argv; settlement and pending work still do.\nLeave the wait only on positive proof the agent stopped: `exited` liveness, the\nworker's own observation of process exit, or a transcript whose final agent turn\nsent no `worker_done`. Then load `references/recovery-and-cleanup.md` and choose\n`worker-stop` or `worker-abandon` explicitly. `unverifiable` is absence,\nincluding when `worker-show` reports `agentWait` null. Absence never authorizes\nstop, abandon, retry, or release; keep waiting or inspect.\n\n`worker-start` is the normal path, composing placement, terminal readiness,\nprompt injection, and supervised resource ownership. `dispatch --inject` leaves\nan operator-created process unsupervised and is only for an expressiveness gap.\n\n## Task-spec contract\n\nEvery Task spec must be self-contained and name:\n\n- **Target:** the files, component, or environment in scope.\n- **Change:** the concrete result to produce.\n- **Constraints:** invariants, compatibility rules, and do-not-touch boundaries.\n- **Ownership:** what this worker may edit and any coordination boundary.\n- **Observable acceptance:** the test, output, or evidence that proves completion.\n\n## Completion accounting\n\nAfter an accepted success or failure report, immediately do exactly one:\n\n1. Reuse the same proven agent terminal for an immediate follow-up Dispatch.\n2. Record user-requested retention with `worker-retain`.\n3. Run `worker-release`.\n\nRelease is post-settlement cleanup, not cancellation. Only an accepted\nsettlement authorizes it; no other observation does. If release is uncertain,\nfollow its exact recovery receipt and never substitute `terminal close`.\n\nA valid `worker_done` settles the Task and Dispatch automatically; do not follow\nit with `task-update --status completed`. Enumerate the terminals still owing a\ndecision with `worker-list --run --terminal-state reclaimable --json`,\nand do not end the coordinator turn until it returns none.\n\n## Conditional references\n\nThis compact guide is sufficient for the normal local loop. At an action gate\nbelow, run `ORCA skills get orchestration --reference references/.md` and\nread only that document; `--references` lists the names. If the CLI rejects\n`--reference`, run `ORCA skills get orchestration --full` once instead: it\nreturns this exact kernel and every reference, so read only the named one. If an\nolder CLI rejects `--full`, keep this kernel's safety floor, use that command's\n`--help`, and never guess newer flags.\n\n| Action gate | Bundled reference |\n| ------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |\n| Expanded DAG waves, launch model/effort, same-terminal reuse, or review ownership | `references/coordinator-loop.md` |\n| You are a dispatched worker and the live preamble does not answer your question, or `check` returned an error | `references/worker-contract.md` |\n| New worktree, exact workspace, SSH, WSL, or connected-server placement | `references/placement-and-remote.md` |\n| Inbox replay, follow-up messages, group addresses, or decision gates | `references/messaging-and-gates.md` |\n| Failed/stopped/unknown attempts, retry, stop, abandon, retain, or uncertain release | `references/recovery-and-cleanup.md` |\n| Custom argv or terminal topology that `worker-start` cannot express | `references/low-level-topology.md` |\n| Any legacy label, adopted Run, compatibility receipt, or takeover | `references/legacy-contract-migration.md` |\n\nRetired scheduler commands are not aliases for Run creation. Recovery commands\nmust provide their exact next action; follow it with the same selected executable.\n" +const ORCHESTRATION_MARKDOWN = "---\nname: orchestration\ndescription: >-\n Coordinate supervised Orca workers: threaded messages, blocking ask/reply,\n task dispatch, worker_done/escalation waits, task DAGs, decision gates,\n coordinator loops, and decomposing work across agents. Use `orca-cli` for full\n ownership handoffs — \"hand off\", \"handoff\", \"handover\", \"give this to another\n agent\", \"another worktree\" — unless asked to supervise, monitor, or coordinate\n a DAG, and for terminal control, lightweight terminal prompts, shell commands,\n Orca worktree management, and reading or waiting on terminals.\n---\n\n# Orca orchestration\n\nOrchestration is Orca's structured coordination layer. It records who owns work,\nwhich attempt is authoritative, and when supervised work has settled.\n\n## Outcome\n\n**Result:** every in-scope Task has one explicit outcome and every settled worker\nterminal has a next owner or cleanup decision. **Next consumer:** the user who\nrequested supervision. **Done:** all expected Dispatches have settled, every\ndelivered message was processed before acknowledgment, each settled worker was\nreused, explicitly retained, or released, and the turn ends only when the report\nto that user names, per Task, its outcome, the evidence behind it, and any\nunresolved blocker.\n\n**Safe failure:** preserve work and authority and report the state as unknown or\n`unverifiable`. Only positive proof of exit authorizes stop, abandon, or retry,\nand only an accepted settlement authorizes release. Every other observation,\nabsence included, is a checkpoint.\n\n## Classify the role\n\n| Current context | Role | Route |\n| ---------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------ |\n| The user explicitly asks to supervise, monitor, wait for results, track completion, coordinate a DAG, use a decision gate, or manage ask/reply | Coordinator | Use the supervised loop below |\n| The current prompt contains a live injected preamble with Task and Dispatch IDs | Dispatched worker | Follow the preamble and the worker obligations below |\n| The user asks to hand off ownership or start another agent/worktree without supervision | Handoff owner | Use `orca-cli`; create no Run, Task, or Dispatch and do not monitor completion |\n| A message carries a legacy authority label | Compatibility operator | Load the legacy contract reference before any lifecycle mutation |\n| No live preamble and no explicit supervision | Ordinary terminal agent | Do not emit lifecycle messages; use `orca-cli` for terminal/worktree work |\n\nModel or effort selection does not make a handoff supervised: `orca-cli`'s\n`worktree create --agent` and `agent launch` take `--model` and `--effort`, and\nopen a chat or a terminal per the user's Chat UI setting, as `worker-start` does.\nNever substitute a non-Orca subagent tool when Orca orchestration provenance was\nrequested.\n\n## Authority and safety floor\n\n- A Run is a durable namespace and coordinator inbox; it does not schedule or\n place workers. A Task is work. A Dispatch is one authoritative Task attempt.\n- Lifecycle authority comes from the active Dispatch, not a terminal title,\n copied ID, old database row, provider transcript, or visible pane.\n- Workers use the exact executable, handle, Task ID, and Dispatch ID in the live\n preamble, plus any other flag it carries. Never reconstruct, translate, or\n broaden those arguments.\n- After remote start, address the worker by Dispatch ID. The execution host owns\n process, filesystem, transcript, stop, and cleanup facts. Preserve the verdicts\n `live` / `unverifiable` / `exited`; contact loss is not process death.\n- Liveness is layered: `worker-list`'s `projection.liveness` is the fleet verdict\n for the agent; `worker-show`'s `observation.status` is PTY liveness only. A live\n terminal can still hold a dead or stuck agent.\n- Folder workspaces are valid; never require Git or assume a worktree.\n- Clients and remote servers update independently. Treat unknown optional fields\n as absent. A new stream operation requires advertised capability because old\n decoders may silently drop unknown opcodes. Never fall back to local execution\n when remote authority or capability is unproven.\n- Use the executable you used to run `skills get` for the entire run. In the\n examples below, replace `ORCA` with it; do not create a shell variable or run\n `ORCA` literally. If it fails, report that exact error instead of switching.\n- `ORCA status --json` shows your Orca session ID as `caller.orcaSessionId` when you have one.\n- A successful `orchestration send` proves durable enqueue; its wake or nudge is\n best-effort attention only and does not prove the recipient read or accepted it.\n\n## Worker obligations\n\nThe injected preamble is authoritative. A dispatched worker must:\n\n1. Do only the current Task and use the preamble's `ask` command for a blocking\n coordinator question. Never open a local question TUI the coordinator cannot\n answer. Resume the same message ID after an ask timeout.\n2. Send heartbeats only at the cadence in the preamble. A heartbeat proves\n liveness, not completion.\n3. Read coordinator follow-ups at each natural checkpoint — before starting a\n new file, after a test run — and once more immediately before `worker_done`:\n `ORCA orchestration check --terminal --json`.\n4. Send `worker_done` exactly once, from the dispatched terminal, with a\n three-sentence executive summary, both lifecycle IDs, and explicit\n `--outcome succeeded` or `--outcome failed`. Never encode failure only in prose.\n5. Append `--files-modified` and `--report-path` only with real values when\n applicable. After `worker_done`, end the dispatched turn and idle; do not poll\n or start new work.\n\nA direct user instruction after completion starts new user-owned work and takes\nprecedence over the idle rule. Do not reuse the settled lifecycle IDs.\n\n## Canonical supervised loop\n\nConfirm the runtime, bind one Run, and start the full independent wave before\nwaiting. `worker-start --spec` creates the Task and its attempt in one call:\n\n```text\nORCA status --json\nORCA orchestration run-create --objective \"\" --json\nORCA orchestration worker-start --spec \"\" --worktree current --agent codex --json\nORCA orchestration worker-start --spec \"\" --worktree current --agent claude --json\nORCA orchestration check --wait --types \"worker_done,escalation,question\" --timeout-ms 900000 --json\n```\n\nAfter a restart, resume your own Run with `run-use --id `. A worker\ncannot bind the Run it works for or an ancestor, and cannot use\n`--takeover-legacy`; an already-bound no-op remains allowed. When its\ncoordinator directs a handoff, a replacement worker may resume a separate Run\nwith plain `run-use`.\n\nIf `worker-start` exits non-zero, do not relaunch. Read the receipt's\n`failedStage` and `residualResources`, then load\n`references/recovery-and-cleanup.md`.\n\nUse `task-create` plus `worker-start --task ` for planned fan-out with\ndependencies or a retry of a known Task. Use dependencies only for real ordering\nand prefer parallel waves over chains deeper than three or four steps; nested\nworkers obey the depth limit, and a new Run does not reset the caller's depth.\nA dispatched worker starts its own helpers with `worker-start --spec` on the Run\nit works for, without `run-create`; the helpers report to that worker.\n\nA consuming `check` names its caller with `--terminal `, never `--from`;\nomit it inside the coordinator's own Orca terminal. It returns the bound Run's\noldest FIFO Delivery and replays that batch until acknowledged. Process every\nmessage: reply to questions, validate each `worker_done` against the expected\nactive Dispatch, and decide each settled terminal's next owner before the ack:\n\n```text\nORCA orchestration reply --id --body \"\" --json\nORCA orchestration worker-release --dispatch --json\nORCA orchestration check --ack --wait --types \"worker_done,escalation,question\" --timeout-ms 900000 --json\n```\n\nKeep waiting until every expected Dispatch settles. A timeout or empty result is\na checkpoint, not a failure. Do not stop, retry, release, or launch a duplicate\neditor without the positive proof `## Outcome` requires.\n\nAfter three consecutive empty waits, stop waiting blindly and enumerate with\n`ORCA orchestration worker-list --include-remote --json` (defaults to the bound\nRun; `--run ` overrides; the receipt's `scope` names which), acting on\neach row's `projection.attention` categories, `projection.attention.requiresAction`, and literal `projection.nextAction` argv.\nRows come newest first and page at 100: while `page.hasMore`, follow `page.nextCursor` with `--cursor `.\nA `none` `nextAction` has no argv to run: read `liveness.reason` and keep waiting\nwith `check --wait`. Absence never earns an argv; settlement and pending work still do.\nLeave the wait only on positive proof the agent stopped: `exited` liveness, the\nworker's own observation of process exit, or a transcript whose final agent turn\nsent no `worker_done`. Then load `references/recovery-and-cleanup.md` and choose\n`worker-stop` or `worker-abandon` explicitly. `unverifiable` is absence,\nincluding when `worker-show` reports `agentWait` null. Absence never authorizes\nstop, abandon, retry, or release; keep waiting or inspect.\n\n`worker-start` is the normal path, composing placement, terminal readiness,\nprompt injection, and supervised resource ownership. `dispatch --inject` leaves\nan operator-created process unsupervised and is only for an expressiveness gap.\n\n## Task-spec contract\n\nEvery Task spec must be self-contained and name:\n\n- **Target:** the files, component, or environment in scope.\n- **Change:** the concrete result to produce.\n- **Constraints:** invariants, compatibility rules, and do-not-touch boundaries.\n- **Ownership:** what this worker may edit and any coordination boundary.\n- **Observable acceptance:** the test, output, or evidence that proves completion.\n\n## Completion accounting\n\nAfter an accepted success or failure report, immediately do exactly one:\n\n1. Reuse the same proven agent terminal for an immediate follow-up Dispatch.\n2. Record user-requested retention with `worker-retain`.\n3. Run `worker-release`.\n\nRelease is post-settlement cleanup, not cancellation. Only an accepted\nsettlement authorizes it; no other observation does. If release is uncertain,\nfollow its exact recovery receipt and never substitute `terminal close`.\n\nA valid `worker_done` settles the Task and Dispatch automatically; do not follow\nit with `task-update --status completed`. Enumerate the terminals still owing a\ndecision with `worker-list --run --terminal-state reclaimable --json`,\nand do not end the coordinator turn until it returns none.\n\n## Conditional references\n\nThis compact guide is sufficient for the normal local loop. At an action gate\nbelow, run `ORCA skills get orchestration --reference references/.md` and\nread only that document; `--references` lists the names. If the CLI rejects\n`--reference`, run `ORCA skills get orchestration --full` once instead: it\nreturns this exact kernel and every reference, so read only the named one. If an\nolder CLI rejects `--full`, keep this kernel's safety floor, use that command's\n`--help`, and never guess newer flags.\n\n| Action gate | Bundled reference |\n| ------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |\n| Expanded DAG waves, launch model/effort, same-terminal reuse, or review ownership | `references/coordinator-loop.md` |\n| You are a dispatched worker and the live preamble does not answer your question, or `check` returned an error | `references/worker-contract.md` |\n| New worktree, exact workspace, SSH, WSL, or connected-server placement | `references/placement-and-remote.md` |\n| Inbox replay, follow-up messages, group addresses, or decision gates | `references/messaging-and-gates.md` |\n| Failed/stopped/unknown attempts, retry, stop, abandon, retain, or uncertain release | `references/recovery-and-cleanup.md` |\n| Custom argv or terminal topology that `worker-start` cannot express | `references/low-level-topology.md` |\n| Any legacy label, adopted Run, compatibility receipt, or takeover | `references/legacy-contract-migration.md` |\n\nRetired scheduler commands are not aliases for Run creation. Recovery commands\nmust provide their exact next action; follow it with the same selected executable.\n" // oxfmt-ignore -const ORCHESTRATION_FULL_MARKDOWN = "---\nname: orchestration\ndescription: >-\n Coordinate supervised Orca workers: threaded messages, blocking ask/reply,\n task dispatch, worker_done/escalation waits, task DAGs, decision gates,\n coordinator loops, and decomposing work across agents. Use `orca-cli` for full\n ownership handoffs — \"hand off\", \"handoff\", \"handover\", \"give this to another\n agent\", \"another worktree\" — unless asked to supervise, monitor, or coordinate\n a DAG, and for terminal control, lightweight terminal prompts, shell commands,\n Orca worktree management, and reading or waiting on terminals.\n---\n\n# Orca orchestration\n\nOrchestration is Orca's structured coordination layer. It records who owns work,\nwhich attempt is authoritative, and when supervised work has settled.\n\n## Outcome\n\n**Result:** every in-scope Task has one explicit outcome and every settled worker\nterminal has a next owner or cleanup decision. **Next consumer:** the user who\nrequested supervision. **Done:** all expected Dispatches have settled, every\ndelivered message was processed before acknowledgment, each settled worker was\nreused, explicitly retained, or released, and the turn ends only when the report\nto that user names, per Task, its outcome, the evidence behind it, and any\nunresolved blocker.\n\n**Safe failure:** preserve work and authority and report the state as unknown or\n`unverifiable`. Only positive proof of exit authorizes stop, abandon, or retry,\nand only an accepted settlement authorizes release. Every other observation,\nabsence included, is a checkpoint.\n\n## Classify the role\n\n| Current context | Role | Route |\n| ---------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------ |\n| The user explicitly asks to supervise, monitor, wait for results, track completion, coordinate a DAG, use a decision gate, or manage ask/reply | Coordinator | Use the supervised loop below |\n| The current prompt contains a live injected preamble with Task and Dispatch IDs | Dispatched worker | Follow the preamble and the worker obligations below |\n| The user asks to hand off ownership or start another agent/worktree without supervision | Handoff owner | Use `orca-cli`; create no Run, Task, or Dispatch and do not monitor completion |\n| A message carries a legacy authority label | Compatibility operator | Load the legacy contract reference before any lifecycle mutation |\n| No live preamble and no explicit supervision | Ordinary terminal agent | Do not emit lifecycle messages; use `orca-cli` for terminal/worktree work |\n\nModel or effort selection does not make a handoff supervised: `orca-cli`'s\n`worktree create --agent` and `agent launch` take `--model` and `--effort`, and\nopen a chat or a terminal per the user's Chat UI setting, as `worker-start` does.\nNever substitute a non-Orca subagent tool when Orca orchestration provenance was\nrequested.\n\n## Authority and safety floor\n\n- A Run is a durable namespace and coordinator inbox; it does not schedule or\n place workers. A Task is work. A Dispatch is one authoritative Task attempt.\n- Lifecycle authority comes from the active Dispatch, not a terminal title,\n copied ID, old database row, provider transcript, or visible pane.\n- Workers use the exact executable, handle, Task ID, and Dispatch ID in the live\n preamble, plus any other flag it carries. Never reconstruct, translate, or\n broaden those arguments.\n- After remote start, address the worker by Dispatch ID. The execution host owns\n process, filesystem, transcript, stop, and cleanup facts. Preserve the verdicts\n `live` / `unverifiable` / `exited`; contact loss is not process death.\n- Liveness is layered: `worker-list`'s `projection.liveness` is the fleet verdict\n for the agent; `worker-show`'s `observation.status` is PTY liveness only. A live\n terminal can still hold a dead or stuck agent.\n- Folder workspaces are valid; never require Git or assume a worktree.\n- Clients and remote servers update independently. Treat unknown optional fields\n as absent. A new stream operation requires advertised capability because old\n decoders may silently drop unknown opcodes. Never fall back to local execution\n when remote authority or capability is unproven.\n- Use the executable you used to run `skills get` for the entire run. In the\n examples below, replace `ORCA` with it; do not create a shell variable or run\n `ORCA` literally. If it fails, report that exact error instead of switching.\n- `ORCA status --json` shows your Orca session ID as `caller.orcaSessionId` when you have one.\n- A successful `orchestration send` proves durable enqueue; its wake or nudge is\n best-effort attention only and does not prove the recipient read or accepted it.\n\n## Worker obligations\n\nThe injected preamble is authoritative. A dispatched worker must:\n\n1. Do only the current Task and use the preamble's `ask` command for a blocking\n coordinator question. Never open a local question TUI the coordinator cannot\n answer. Resume the same message ID after an ask timeout.\n2. Send heartbeats only at the cadence in the preamble. A heartbeat proves\n liveness, not completion.\n3. Read coordinator follow-ups at each natural checkpoint — before starting a\n new file, after a test run — and once more immediately before `worker_done`:\n `ORCA orchestration check --terminal --json`.\n4. Send `worker_done` exactly once, from the dispatched terminal, with a\n three-sentence executive summary, both lifecycle IDs, and explicit\n `--outcome succeeded` or `--outcome failed`. Never encode failure only in prose.\n5. Append `--files-modified` and `--report-path` only with real values when\n applicable. After `worker_done`, end the dispatched turn and idle; do not poll\n or start new work.\n\nA direct user instruction after completion starts new user-owned work and takes\nprecedence over the idle rule. Do not reuse the settled lifecycle IDs.\n\n## Canonical supervised loop\n\nConfirm the runtime, bind one Run, and start the full independent wave before\nwaiting. `worker-start --spec` creates the Task and its attempt in one call:\n\n```text\nORCA status --json\nORCA orchestration run-create --objective \"\" --json\nORCA orchestration worker-start --spec \"\" --worktree current --agent codex --json\nORCA orchestration worker-start --spec \"\" --worktree current --agent claude --json\nORCA orchestration check --wait --types \"worker_done,escalation,question\" --timeout-ms 900000 --json\n```\n\nIf `worker-start` exits non-zero, do not relaunch. Read the receipt's\n`failedStage` and `residualResources`, then load\n`references/recovery-and-cleanup.md`.\n\nUse `task-create` plus `worker-start --task ` for planned fan-out with\ndependencies or a retry of a known Task. Use dependencies only for real ordering\nand prefer parallel waves over chains deeper than three or four steps; nested\nworkers obey the depth limit, and a new Run does not reset the caller's depth.\nA dispatched worker starts its own helpers with `worker-start --spec` on the Run\nit works for, without `run-create`; the helpers report to that worker.\n\nA consuming `check` names its caller with `--terminal `, never `--from`;\nomit it inside the coordinator's own Orca terminal. It returns the bound Run's\noldest FIFO Delivery and replays that batch until acknowledged. Process every\nmessage: reply to questions, validate each `worker_done` against the expected\nactive Dispatch, and decide each settled terminal's next owner before the ack:\n\n```text\nORCA orchestration reply --id --body \"\" --json\nORCA orchestration worker-release --dispatch --json\nORCA orchestration check --ack --wait --types \"worker_done,escalation,question\" --timeout-ms 900000 --json\n```\n\nKeep waiting until every expected Dispatch settles. A timeout or empty result is\na checkpoint, not a failure. Do not stop, retry, release, or launch a duplicate\neditor without the positive proof `## Outcome` requires.\n\nAfter three consecutive empty waits, stop waiting blindly and enumerate with\n`ORCA orchestration worker-list --include-remote --json` (defaults to the bound\nRun; `--run ` overrides; the receipt's `scope` names which), acting on\neach row's `projection.attention` categories, `projection.attention.requiresAction`, and literal `projection.nextAction` argv.\nRows come newest first and page at 100: while `page.hasMore`, follow `page.nextCursor` with `--cursor `.\nA `none` `nextAction` has no argv to run: read `liveness.reason` and keep waiting\nwith `check --wait`. Absence never earns an argv; settlement and pending work still do.\nLeave the wait only on positive proof the agent stopped: `exited` liveness, the\nworker's own observation of process exit, or a transcript whose final agent turn\nsent no `worker_done`. Then load `references/recovery-and-cleanup.md` and choose\n`worker-stop` or `worker-abandon` explicitly. `unverifiable` is absence,\nincluding when `worker-show` reports `agentWait` null. Absence never authorizes\nstop, abandon, retry, or release; keep waiting or inspect.\n\n`worker-start` is the normal path, composing placement, terminal readiness,\nprompt injection, and supervised resource ownership. `dispatch --inject` leaves\nan operator-created process unsupervised and is only for an expressiveness gap.\n\n## Task-spec contract\n\nEvery Task spec must be self-contained and name:\n\n- **Target:** the files, component, or environment in scope.\n- **Change:** the concrete result to produce.\n- **Constraints:** invariants, compatibility rules, and do-not-touch boundaries.\n- **Ownership:** what this worker may edit and any coordination boundary.\n- **Observable acceptance:** the test, output, or evidence that proves completion.\n\n## Completion accounting\n\nAfter an accepted success or failure report, immediately do exactly one:\n\n1. Reuse the same proven agent terminal for an immediate follow-up Dispatch.\n2. Record user-requested retention with `worker-retain`.\n3. Run `worker-release`.\n\nRelease is post-settlement cleanup, not cancellation. Only an accepted\nsettlement authorizes it; no other observation does. If release is uncertain,\nfollow its exact recovery receipt and never substitute `terminal close`.\n\nA valid `worker_done` settles the Task and Dispatch automatically; do not follow\nit with `task-update --status completed`. Enumerate the terminals still owing a\ndecision with `worker-list --run --terminal-state reclaimable --json`,\nand do not end the coordinator turn until it returns none.\n\n## Conditional references\n\nThis compact guide is sufficient for the normal local loop. At an action gate\nbelow, run `ORCA skills get orchestration --reference references/.md` and\nread only that document; `--references` lists the names. If the CLI rejects\n`--reference`, run `ORCA skills get orchestration --full` once instead: it\nreturns this exact kernel and every reference, so read only the named one. If an\nolder CLI rejects `--full`, keep this kernel's safety floor, use that command's\n`--help`, and never guess newer flags.\n\n| Action gate | Bundled reference |\n| ------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |\n| Expanded DAG waves, launch model/effort, same-terminal reuse, or review ownership | `references/coordinator-loop.md` |\n| You are a dispatched worker and the live preamble does not answer your question, or `check` returned an error | `references/worker-contract.md` |\n| New worktree, exact workspace, SSH, WSL, or connected-server placement | `references/placement-and-remote.md` |\n| Inbox replay, follow-up messages, group addresses, or decision gates | `references/messaging-and-gates.md` |\n| Failed/stopped/unknown attempts, retry, stop, abandon, retain, or uncertain release | `references/recovery-and-cleanup.md` |\n| Custom argv or terminal topology that `worker-start` cannot express | `references/low-level-topology.md` |\n| Any legacy label, adopted Run, compatibility receipt, or takeover | `references/legacy-contract-migration.md` |\n\nRetired scheduler commands are not aliases for Run creation. Recovery commands\nmust provide their exact next action; follow it with the same selected executable.\n\n---\n\n# Bundled references\n\nThese references belong to the version-matched guide above. Read only the documents named by its action gates.\n\n\n\n# Coordinator loop\n\nLoad this reference for expanded DAG waves, per-invocation launch preferences,\nsame-terminal reuse, or review ownership. The compact guide remains the source\nof truth for the loop order and completion boundary.\n\n## Ready waves\n\nCreate independent Tasks before the first wait. Encode only real dependencies,\nthen use the ready view as external memory:\n\n```text\nORCA orchestration task-create --spec \"\" --deps --json\nORCA orchestration task-list --ready --brief --json\n```\n\n`--brief` collapses whitespace and caps echoed specs at 160 characters;\n`spec_truncated` identifies shortened rows. Omit it when full specs are needed or\nwhen an older CLI rejects the flag. A nested worker must respect\n`nested_worker_depth_exceeded`; creating another Run does not reset depth.\nHelpers a worker starts appear in this Run's `worker-list` with their\n`creatorDispatchId`, and report to that worker rather than to you.\n\n## Launch preferences\n\nFor a fresh Claude, Codex, Cursor, Antigravity, or Muse terminal, `--model`\naccepts an opaque provider model ID. Pass it only when the user named a model;\notherwise omit it so the worker inherits the user's configured agent default.\nAdd `--effort` only when that model supports it:\n\n```text\nORCA orchestration worker-start --task --worktree current --agent claude --model opus --effort high --json\nORCA orchestration worker-start --task --worktree current --agent muse --model muse-spark-1.3 --json\n```\n\nOpenCode also accepts `--model` in an existing worktree when the execution host\nverifies its CLI version and model availability. Creating a new worktree with an\nOpenCode model override is unsupported; use `--worktree current` or an existing\nworktree selector, or omit `--model`. OpenCode effort is unsupported. Omit the\nmodel to inherit its configuration; unsupported or unknown hosts refuse the\noverride explicitly.\n\n`--effort` requires `--model`; neither option combines with `--terminal`. A\nconnected worker server must advertise launch-preference support before Orca\nforwards either field. Compare `launch.requested` with `launch.effective`; never\nclaim a model or effort from requested arguments alone.\n\n## Reuse after settlement\n\nChoose the terminal's next owner before acknowledging the Delivery. When the\nsame exact agent has immediate follow-up work, recover the proven handle and\ntransfer cleanup ownership to the new Dispatch:\n\n```text\nORCA orchestration worker-show --dispatch --json\nORCA orchestration worker-start --task --terminal --json\n```\n\nOtherwise explicitly retain or release the settled worker. Do not leave it live\nonly to inspect output; archived output remains available through `worker-read`.\n\n## Review ownership\n\nA review-only `worker_done` authorizes synthesis of findings, not coordinator\nfile edits. Dispatch or hand off fixes unless the user explicitly assigned them\nto the coordinator. If the user's plan names a next owner, post-review fixes and\nPR preparation remain with that owner; the coordinator routes and synthesizes.\n\n\n\n# Legacy contract migration\n\nLoad this reference only for an authority label, adopted Run, compatibility or\nrecovery receipt, or explicit legacy takeover. A newly created attempt always\nuses the current grammar.\n\n## Authority labels\n\n- `[LEGACY COMPATIBILITY]` is live and attested. Run only the exact supported\n command printed with the message, using the same selected executable and\n arguments supplied by the original prompt.\n- `[LEGACY RECOVERY REPLAY — MAY HAVE BEEN SEEN]` is one bounded,\n at-least-once cutover replay. Process it idempotently and acknowledge only\n through the exact displayed guidance.\n- `[LEGACY READ-ONLY]` is inspection-only. It has no reply, acknowledgment, or\n lifecycle mutation.\n- An unlabeled current message uses the current guide and grammar.\n\nAn explicitly selected current Run, attested current binding, current Dispatch,\nor federated attachment takes precedence over legacy fallback. A retained\nadoption record alone does not grant mutation authority. If liveness, principal\nownership, capability, or the exact legacy contract is unproven, degrade to\nread-only inspection and never fall back to local execution.\n\nAdoption preserves the live agent process, PTY/session, terminal handle,\ntab/pane, worktree or folder workspace, Task, and Dispatch. It never restarts or\nreplaces the worker and never revives the retired scheduler. Loss of lifecycle\nauthority does not invalidate the existing process, assignment, or filesystem\nwork. Exact recovery may restore the same PTY once in its original inactive\nbackground tab; it must not spawn, write, signal, stop, switch, focus, split, or\ninject a terminal.\n\n## Compatibility recovery\n\nWhen a compatibility response returns structured next-step arguments, execute\nthose exact arguments with the same selected CLI executable. Do not translate\nfrom memory, broaden the recipient, or retry as a current mutation unless the\nreceipt explicitly authorizes it.\n\nA pending ask, reply, final Dispatch settlement, and consuming check have\ndurable recovery identities. Heartbeat and escalation remain at-least-once\nacross a manual contract-boundary retry. If an ask may already have been\nanswered, run the exact non-consuming recovery check printed by Orca before\ncreating any new question. Never guess among identical question threads.\n\nOn packaged Windows, a legacy ask uses a two-step commit/resume protocol. The\ninitial command commits the question, prints its exact\n`ask --resume ` command, and exits with launcher status `75`. Run\nthat exact resume after the launcher or update boundary. For an attested WSL\nlaunch, preserve the printed `orca-ide` executable and distro route. Older WSL\nworkers without launch proof remain lifecycle read-only even while their\nterminal and filesystem work continue.\n\n## Read-only inspection and takeover\n\nRead-only inspection does not consume mail:\n\n```text\nORCA orchestration run-list --json\nORCA orchestration run-show --id run_legacy_local --json\nORCA orchestration run-show --id --json\nORCA orchestration task-list --run --json\nORCA orchestration inbox --full --json\nORCA orchestration check --terminal --peek --format --json\nORCA terminal read --terminal --json\nORCA terminal wait --terminal --for tui-idle --timeout-ms 60000 --json\n```\n\n`run_legacy_local` is an empty audit tombstone after adoption. Find the ordinary\nRun whose objective is `Recovered orchestration work from a contract update`.\n\nOnly when the original coordinator is unavailable or cannot prove retained\nauthority may a new live coordinator take over from its own terminal:\n\n```text\nORCA orchestration run-use --id --takeover-legacy --json\nORCA orchestration check --run --json\n```\n\nTakeover binds the authenticated invoking terminal; `--from` cannot nominate\nanother coordinator. It fences only the old coordinator and moves pending mail\ninto current Run delivery. It preserves live workers, Tasks, Dispatches, processes, and files.\nNever take over while the original coordinator is actively coordinating.\n\nDo not launch a replacement editor merely because Orca updated or authority is\nunclear. Keep the original worker as the only editor until a stable handoff\npoint, then use a fresh current Dispatch in a conflict-free placement.\n\n\n\n# Low-level topology\n\nLoad this reference only when `worker-start` cannot express required custom argv\nor terminal topology. It is not the normal supervised loop and is never a full\nhandoff recipe. A model or effort is not custom argv: `worker-start --model\n--effort` carries them and keeps the user's Chat UI setting.\n\n```text\nORCA terminal create --worktree active --title --command \"\" --json\nORCA terminal wait --terminal --for tui-idle --timeout-ms 60000 --json\nORCA orchestration dispatch --task --to --inject --json\n```\n\nWait for readiness only when startup could lose injected input. Prefer\nagent-first `worker-start` whenever its argv and topology are sufficient.\n\n`dispatch --inject` creates authoritative Task/Dispatch context but deliberately\nkeeps an operator-created process unsupervised: it creates no supervised worker\nresource row. `worker-show`, `worker-read`, and `worker-list` report the lane as\n`unsupervised`; `worker-stop` and `worker-abandon` do not close that process, and\nsettled retain/release take no process action.\n\nUse `worker-start --terminal ` when lifecycle ownership of an existing\nagent terminal is required. Never imply that low-level dispatch retroactively\nowns a process, never use it to route around the nested-depth limit, and never\nuse it for an ownership handoff.\n\n\n\n# Messaging and gates\n\nLoad this reference for inbox replay, attempt-specific guidance, group\naddresses, blocking questions, or coordinator-managed DAG decisions.\n\nA successful `send` proves durable enqueue. Wake and nudge are best-effort\nattention only: neither proves the recipient read the message, began a turn, or\naccepted steering.\n\n## Coordinator delivery loop\n\n`check` names its caller with `--terminal ` and is the only verb that\nrejects `--from`. Omit `--terminal` inside an Orca terminal, where Orca resolves\nthe caller; pass it explicitly from anywhere else, including a dispatched\nworker reading coordinator follow-ups.\n\nA consuming coordinator `check` returns the bound Run's oldest FIFO Delivery,\nup to 50 messages, and replays that exact batch until acknowledged or all its\nmessages are marked read. Concurrent waits on one Run inbox are allowed; they\nshare the outstanding batch and reserve nothing, and acknowledging the same\nDelivery twice is a no-op. Process every row and required terminal ownership\ndecision before `--ack`. Type filters decide when a waiter wakes; they do not\nauthorize skipping older actionable mail. A Delivery therefore always carries\nthe whole FIFO batch whatever its types, and a `check` without `--wait` hands\nthat batch over unfiltered.\n`--peek` and `--all` are read-only inspection, not progress through the\ncoordinator inbox.\n\nAn empty wait or timeout is a checkpoint. Continue rolling waits until every\nexpected Dispatch settles. Heartbeat or visible activity means alive, not done.\n\n## Addresses\n\nUse a stable Dispatch address for attempt-specific coordinator guidance:\n\n```text\nORCA orchestration send --to dispatch: --subject \"Follow-up\" --body \"\" --json\n```\n\nDo not substitute a remote terminal handle. Omit `--from` for ordinary\ncoordinator calls; a dispatched worker instead copies the exact `--from` and\nother arguments in its preamble. `check` is the exception: it identifies\nits caller with `--terminal`, never `--from`.\n\nGroup addresses include `@all`, `@idle`, `@claude`, `@codex`, `@opencode`,\n`@gemini`, `@droid`, `@grok`, `@cursor`, and `@worktree:`. Every group but\n`@worktree:` means the live Dispatches of the sender's own Run. Mail goes\nto each `dispatch:` mailbox, except a worker coordinating a child Run\nreceives it in that `run:` mailbox. A sender bound to no Run is refused;\n`--run` must match the group audience and never grants membership.\nA Run group excludes its owning coordinator; a worker raising a blocker sends\nto `run:`. A worker that created its own Run addresses that Run's workers,\nnot its siblings. `@worktree:` reaches matching workspace terminals,\nincluding coordinators. Use groups only for intentional fan-out status or\nquestions. `worker_done`, heartbeat, and other\nDispatch lifecycle messages never target groups.\n\n## Questions and gates\n\nA worker uses `ask`; its timeout leaves one durable question pending, which the\nworker resumes by message ID. The coordinator answers that message with `reply`;\na helper's question goes to the worker that started it, which answers it the\nsame way.\n\nUse a gate only for a coordinator-owned Task-DAG decision:\n\n```text\nORCA orchestration gate-create --task --question \"\" --options --json\nORCA orchestration gate-resolve --id --resolution \"\" --json\nORCA orchestration gate-list --task --json\n```\n\nPass `json_array` using the quoting rules of the active shell; do not copy POSIX\nsingle-quote syntax into PowerShell or `cmd.exe`.\n\nDo not create a gate merely to answer a worker's `ask`.\n\n\n\n# Placement and remote execution\n\nLoad this reference before creating a new worktree or placing work through SSH,\nWSL, or another connected Orca server.\n\n## Placement choices\n\nA fresh worker means a fresh agent terminal, not a new Git worktree. Use the\ncurrent or an exact existing workspace by default. Create a worktree only when\nthe user requested one or a concrete checkout or filesystem conflict makes\nsharing unsafe.\n\n```text\n# Current workspace; setup is not rerun.\nORCA orchestration worker-start --task --worktree current --agent codex --json\n\n# Stacked child worktree.\nORCA orchestration worker-start --task --worktree new-child --name --agent codex --setup run --json\n\n# Independent top-level worktree.\nORCA orchestration worker-start --task --worktree new-top-level --name --agent codex --setup run --json\n```\n\nCurrent and exact existing workspaces create a fresh terminal unless\n`--terminal` is explicit. Folder workspaces are first-class; do not invoke Git\nor require worktree lineage when the selected workspace is a folder.\n\nRegister a folder workspace through project setup. `repo add --path `\nrequires a valid Git repository and rejects a plain directory:\n\n```text\nORCA project setup-existing-folder --project --host --path --kind folder --json\n```\n\nThen place work on the returned workspace with an exact selector. A worktree\nselector needs the full `::` value Orca returned, passed as\n`id:`; a bare repo id is not a worktree id. `new-child` and\n`new-top-level` are worktree creation and do not apply to a folder.\n\nNew worktrees use agent-first creation and run setup by default. Preserve the\nrepository's startup policy: `start-immediately` can report setup as `running`,\nwhile `wait-for-setup` gates prompt delivery on success. Orca lineage, Git base,\nfilesystem isolation, coordination parentage, UI grouping, and execution host\nare separate decisions.\n\n## Connected servers\n\nThe Run and Tasks remain authoritative on the current server. `--on` selects\nonly the worker's execution server and appears only on `worker-start`:\n\n```text\nORCA orchestration worker-start --task --on --worktree new-top-level --repo --name --agent codex --setup run --json\n```\n\nRemote `current` and `new-child` are invalid because they are ambiguous across\nservers. Use an exact discovered remote workspace, or `new-top-level` with an\nexact remote repository selector. After start, route every follow-up, read,\nstop, and cleanup by Dispatch ID; never repeat `--on` or substitute a remote\nterminal handle.\n\n```text\nORCA orchestration worker-show --dispatch --json\nORCA orchestration worker-read --dispatch --limit 50 --json\nORCA orchestration send --to dispatch: --subject \"Follow-up\" --body \"\" --json\nORCA orchestration worker-list --run --include-remote --json\n```\n\n`worker-list` reads local fleet state only; enumerate remote workers with\n`--include-remote` or every one of them reads `unverifiable`. Scope every list\nwith `--run `: unscoped, it reports every Dispatch this runtime has\nrecorded, and the workers you are waiting on are lost in that history.\n\n## Execution-host and mixed-version floor\n\nThe execution host owns process, filesystem, transcript, stop, and cleanup\nfacts. Render only `live`, `unverifiable`, or `exited`. Connection loss, relay\nabsence, missing client inventory, or timeout yields `unverifiable`, never\nsynthetic exit and never a client-local substitute action.\n\nClients and servers update independently. Optional response fields may be\nabsent. Forward model/effort, transcript reads, cleanup, or another new remote\noperation only when the peer advertises the relevant capability; unknown stream\nopcodes can be silently dropped. A narrow unsupported response may degrade to a\ndocumented older path, but must not broaden the target or cross the execution\nboundary. Changing host-published content reaches old clients even without a\nwire-shape change, so preserve established semantics or negotiate the behavior.\n\nFor WSL, use the exact executable and arguments returned by Orca so the distro\nand packaged launcher remain bound. Do not translate a printed `orca-ide`\nrecovery command into a PATH-resolved local command.\n\n\n\n# Recovery and cleanup\n\nLoad this reference only after a failed/stopped/unknown attempt, explicit retry\ndecision, stop/abandon request, retention request, or uncertain release.\n\n| Proven state | Safe action |\n| ----------------------- | ------------------------------------------------------------------ |\n| `ready` or active | Keep waiting; optionally read bounded output |\n| `failed` or `stopped` | Start a replacement with `--retry-of`; repeat placement explicitly |\n| `outcome_unknown` | Inspect, then choose `worker-stop` or explicit `worker-abandon` |\n| Accepted `worker_done` | Reuse, retain, or release |\n| Remote contact lost | Preserve `unverifiable`; do not stop or retry from absence alone |\n| `unverifiable` liveness | Keep waiting or inspect; never stop, abandon, retry, or release |\n| Proven `exited` agent | Enumerate with `worker-list`; follow its `nextAction` |\n\n## Inspect before acting\n\n```text\nORCA orchestration worker-list --run --json\nORCA orchestration worker-list --run --include-remote --json\nORCA orchestration worker-show --dispatch --json\nORCA orchestration worker-read --dispatch --limit 50 --json\n```\n\n`worker-list` is the enumerating command and the authority on agent liveness:\neach row carries `projection.liveness`, `projection.attention.categories`,\n`projection.attention.requiresAction`, and a literal `projection.nextAction`\nargv to run. Always scope it with `--run `; an unscoped list reports\nevery Dispatch this runtime has ever recorded and buries the live ones.\n`worker-show`'s `observation.status` is PTY liveness only, so a `live` terminal\nwhose agent died at a trust prompt still reads `live` there.\n\nWhen the two disagree, the fleet verdict decides — unless the fleet row is\n`unverifiable` for a reason that names a gap on this client rather than a fact\nabout the worker. `missing_status`, `host_unavailable`, and\n`capability_unsupported` are such gaps: the first means this runtime holds no\nstatus row, the second that it could not ask the execution host at all, and the\nthird that a stale peer answered but lacks the fleet-snapshot capability.\nAgainst any of them, a `worker-show` verdict sourced from the execution host is\nthe better evidence and outranks the row. Only `host_unavailable` is contact\nloss; the other two mean the host was never asked or answered without the\ncapability.\n\nThis never promotes absence. `unverifiable` from either command still authorizes\nnothing — only a positive `live` or `exited` verdict does.\n\nA worker started with `--on ` reads `unverifiable` until you\nenumerate with `--include-remote`, which asks its execution host for the\nverdict. Rows come newest first and past 100 the response pages, so follow\n`page.nextCursor` with `--cursor ` until `page.hasMore` is false.\n\n## Stall needs positive evidence\n\nLeave the wait only on positive proof the agent stopped: `exited` liveness, the\nworker's own observation of process exit, or a transcript whose final agent turn\nsent no `worker_done`. Only then choose `worker-stop` or `worker-abandon`.\n\n`unverifiable` is always absence — `missing_status`, `stale_status`,\n`restored_unconfirmed`, or a remote worker with no connection — and a null\n`agentWait` or an unchanged `worker-read` tail is that same absence seen again.\nAbsence never authorizes stop, abandon, retry, or release: keep waiting, or\ninspect until you hold one of the positive signals above. A `nextAction` that\nnames an inspecting command is asking for evidence, not for cleanup.\n\n`worker-read --source auto` uses a proven provider transcript when available and\notherwise returns bounded terminal output with a typed `fallbackReason`.\nContinue with its top-level cursor, which is pinned to that source. If Orca\nreports `source_changed`, restart without the old cursor. A bounded initial\ntranscript tail can return an EOF cursor that follows only newly appended records;\nread `contentComplete`, `clipping`, and `warnings` before assuming omitted older\nrecords are pageable. Never guess a provider session ID, transcript path, or\nremote terminal handle.\n\n## Was the mutation applied?\n\nWhen a mutation's response was lost and named no Dispatch, do not replay blind.\nEvery orchestration mutation accepts `--retry-request `, which reuses one\noperation identity so Orca can replay, join, or recover it instead of starting a\nduplicate. Ask what happened first:\n\n```text\nORCA orchestration request-show --request --json\n```\n\n`completed` means the mutation already took effect; read its recorded receipt\ninstead of rerunning. `pending` means the original mutation is still running or\nOrca restarted before recording its outcome; replay the original command with\n`--retry-request `. `absent` means this runtime holds no receipt\nunder your caller identity — that is not proof nothing happened, so inspect the\naffected Task, Dispatch, and terminal before deciding whether to retry.\n\nWhen a worker's terminal accepted input but the submit is unconfirmed, use\n`terminal send --wait-submit `: it observes the accepted prompt for that\nlong and, on timeout, returns the input-accepted receipt without resending.\n\n## Refused starts\n\n`dispatch` and `worker-start` refuse the following preflight cases with a stable\n`error.code`; read it before choosing a recovery, and treat `error.data.nextSteps`\nas the exact recovery text. Older hosts may omit `data`, so treat every field as\noptional.\n\n| Code | Meaning | Recovery |\n| -------------------- | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |\n| `task_not_found` | No Task with that id, or not in the bound Run (`data.taskId`, `data.runId`) | Check `task-list --json`; create the Task with `task-create` if it does not exist |\n| `task_not_startable` | Task cannot start now: not `ready`, or invalid `--retry-of` (`data.status`, `data.unmetDependencies`, `data.retryOf`) | Wait for running dependencies with `check --wait`; retry or unblock failed ones; inspect `dispatch-show` if already dispatched |\n| `inject_rejected` | `--inject` refused because no recognized agent runs in the target (`data.terminal`, `data.reason`) | Start a recognized agent there or pick another terminal; or dispatch without `--inject` and use `terminal send` |\n| `runtime_error` | Any other failure, including a target terminal that already owns an active Dispatch | Read the message, inspect state, and do not retry unchanged |\n\n## Retry, stop, and abandon\n\nRetry only a positively proven failed or stopped attempt. Name the failed Task\nwith `--task`, since `--spec` creates a new one. Placement is never silently\ninherited:\n\n```text\nORCA orchestration worker-start --task --retry-of --worktree --agent --json\n```\n\nAfter three consecutive failures for one Task, its dispatch context\ncircuit-breaks and the Task is failed. Failed `worker-start` attempts count\nacross `--retry-of`; a worker's own `failed` report does not. `--retry-of` a\ncircuit-broken Dispatch is refused with `task_not_startable`: report the failure\nto the user instead. Do not route around that boundary with a new Run, Task, or\nunrelated Dispatch.\n\nFor `outcome_unknown`, inspect first, then make an explicit choice:\n\n```text\nORCA orchestration worker-stop --dispatch --json\nORCA orchestration worker-abandon --dispatch --json\n```\n\n`worker-stop` closes only the exact proven supervised agent terminal. It never\ndeletes the worktree, setup terminal, configured tabs, or unrelated processes.\n`worker-abandon` fences orchestration while accepting that resources may remain\nlive; it performs no remote, process, or filesystem action. When it settles a\nworker, it retains an owned terminal by the same rule as `worker-retain`, so Orca\nstops owing its release; a release already committed (`releasing`,\n`release_unknown`) is left as it is. An already-settled worker is left untouched.\n\nTo cancel a Task, settle its worker with `worker-stop` or `worker-abandon`, then\nrecord the reason. A cancelled Task is `failed`, so its dependents stay blocked\nuntil a `--retry-of` replacement completes it:\n\n```text\nORCA orchestration task-update --id --status failed --result cancelled --json\n```\n\n## Retain and release\n\n```text\nORCA orchestration worker-retain --dispatch --json\nORCA orchestration worker-release --dispatch --json\n```\n\nRetain only when the user explicitly wants the settled terminal kept live.\nRelease works after succeeded and failed reports, archives readable output, and\ncloses only the exact terminal owned by that settled Dispatch. Replays may call\nrelease again safely. Reused, pre-existing, setup, coordinator, active,\nuser-taken-over, and unproven terminals are retained.\n\nA `worker-start` that failed before its agent was ready still owns the terminal\nit created. Its receipt names `worker-release`, and `worker-list` reports that\nrow as `reclaimable`; release it there rather than closing the terminal by hand.\nA `--retry-of` replacement for a `failed` worker releases the terminal that\nworker still owns the same way.\n\nNever release because of timeout, TUI idle, heartbeat, status, question,\nescalation, or stale/rejected completion. If the receipt says `release_pending`\nor `release_unknown`, follow its exact recovery action. Never substitute\n`terminal close`.\n\n`orchestration reset` is destructive recovery. Do not run it during active\ncoordination unless the user explicitly abandons that state.\n\n\n\n# Worker contract\n\nThe injected preamble is authoritative. Copy its command rather than\nreconstructing flags. In particular, preserve the exact executable, worker\nhandle, Task ID, and Dispatch ID, and keep any other flag it carries (an older\nOrca host adds `--dispatch-capability`).\n\n## Heartbeat\n\nSend heartbeats only at the cadence required by the live preamble. Skip them\nwhile blocked inside `ask` or `check --wait`; those calls are liveness signals.\n\n```text\nORCA orchestration send --from --type heartbeat --subject \"alive\" --task-id --dispatch-id --phase \"\"\n```\n\nUse typed lifecycle flags, not a hand-written JSON payload. A heartbeat proves\nliveness, never completion.\n\n## Ask and resume\n\nUse Orca `ask` whenever the coordinator must answer. Never open a local question\nTUI the coordinator cannot answer.\n\n```text\nORCA orchestration ask --from --question \"\" --options \",\" --timeout-ms 600000\n\nORCA orchestration ask --from --resume --timeout-ms 600000\n```\n\nEach wait defaults to 600000 ms (10 minutes). `--timeout-ms` accepts a positive\ninteger up to 1800000 ms (30 minutes); larger values are rejected before creating\nor resuming a question. For decisions that take longer, resume the same question\nafter each timeout.\n\nA timeout or disconnect leaves the original question pending. Resume its\nmessage ID; do not create a duplicate question.\n\n## Reading coordinator follow-ups\n\nThe coordinator steers a running worker with `send --to dispatch:`. That\nenqueue is durable but does not interrupt you, so nothing arrives unless you\nlook:\n\n```text\nORCA orchestration check --terminal --json\n```\n\nRun it at each natural checkpoint — before starting a new file, after a test\nrun — and once more immediately before `worker_done`, so a redirect or a\ncancellation lands before the Task settles. `check` names its caller with\n`--terminal`, never `--from`. Stop checking after `worker_done`.\n\nIf `check` returns `consumer_fenced`, this process no longer owns its Dispatch:\nthe Attempt was re-attached to another worker or settled without you. Stop, do\nnot send `worker_done`, and do not retry the check. An empty `check` never means\nyou were replaced; `consumer_fenced` is the only way you learn that.\nIf `send` or `ask` returns `consumer_fenced`, the command ran from another\nparty's terminal (a coordinator or another worker); run it from your own terminal.\n\n## Helper workers\n\nWhen the preamble has a SUB-DISPATCH section, start helpers on your own\nDispatch's Run. Do not `run-create` or `run-use` for them.\n\n```text\nORCA orchestration worker-start --from --spec \"\" --worktree current --agent --json\nORCA orchestration reply --from --id --body \"\"\n```\n\nTheir questions, escalations, and `worker_done` arrive in your own `check`, next\nto your coordinator's mail. Answer their questions with `reply`, and wait for\nevery helper's `worker_done` before sending yours. After you settle, a helper\nstill running reports to your coordinator's Run.\n\n## Escalation\n\nEscalate only before completion and only when the coordinator must intervene:\n\n```text\nORCA orchestration send --from --type escalation --subject \"Blocked: \" --body \"
\" --task-id --dispatch-id \n```\n\n## Completion\n\nSend exactly one terminal report. `--body` is three sentences: what changed,\nwhat was found, and what remains. Use `--outcome failed` when the requested work\nis not complete; never hide failure in prose or silently exit.\n\nAppend `--files-modified` or `--report-path` only when applicable, using actual\npaths. Do not send documentation placeholders as metadata.\n\n```text\nORCA orchestration send --from --type worker_done --subject \"\" --body \"\" --task-id --dispatch-id --outcome succeeded\n```\n\nAfter `worker_done`, end the dispatched turn and idle. Do not poll, close your\nown terminal, or begin unrelated work. A later direct user instruction is new\nuser-owned work and must not reuse settled lifecycle IDs; a supervised follow-up\narrives with a fresh preamble and Task block.\n" +const ORCHESTRATION_FULL_MARKDOWN = "---\nname: orchestration\ndescription: >-\n Coordinate supervised Orca workers: threaded messages, blocking ask/reply,\n task dispatch, worker_done/escalation waits, task DAGs, decision gates,\n coordinator loops, and decomposing work across agents. Use `orca-cli` for full\n ownership handoffs — \"hand off\", \"handoff\", \"handover\", \"give this to another\n agent\", \"another worktree\" — unless asked to supervise, monitor, or coordinate\n a DAG, and for terminal control, lightweight terminal prompts, shell commands,\n Orca worktree management, and reading or waiting on terminals.\n---\n\n# Orca orchestration\n\nOrchestration is Orca's structured coordination layer. It records who owns work,\nwhich attempt is authoritative, and when supervised work has settled.\n\n## Outcome\n\n**Result:** every in-scope Task has one explicit outcome and every settled worker\nterminal has a next owner or cleanup decision. **Next consumer:** the user who\nrequested supervision. **Done:** all expected Dispatches have settled, every\ndelivered message was processed before acknowledgment, each settled worker was\nreused, explicitly retained, or released, and the turn ends only when the report\nto that user names, per Task, its outcome, the evidence behind it, and any\nunresolved blocker.\n\n**Safe failure:** preserve work and authority and report the state as unknown or\n`unverifiable`. Only positive proof of exit authorizes stop, abandon, or retry,\nand only an accepted settlement authorizes release. Every other observation,\nabsence included, is a checkpoint.\n\n## Classify the role\n\n| Current context | Role | Route |\n| ---------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------ |\n| The user explicitly asks to supervise, monitor, wait for results, track completion, coordinate a DAG, use a decision gate, or manage ask/reply | Coordinator | Use the supervised loop below |\n| The current prompt contains a live injected preamble with Task and Dispatch IDs | Dispatched worker | Follow the preamble and the worker obligations below |\n| The user asks to hand off ownership or start another agent/worktree without supervision | Handoff owner | Use `orca-cli`; create no Run, Task, or Dispatch and do not monitor completion |\n| A message carries a legacy authority label | Compatibility operator | Load the legacy contract reference before any lifecycle mutation |\n| No live preamble and no explicit supervision | Ordinary terminal agent | Do not emit lifecycle messages; use `orca-cli` for terminal/worktree work |\n\nModel or effort selection does not make a handoff supervised: `orca-cli`'s\n`worktree create --agent` and `agent launch` take `--model` and `--effort`, and\nopen a chat or a terminal per the user's Chat UI setting, as `worker-start` does.\nNever substitute a non-Orca subagent tool when Orca orchestration provenance was\nrequested.\n\n## Authority and safety floor\n\n- A Run is a durable namespace and coordinator inbox; it does not schedule or\n place workers. A Task is work. A Dispatch is one authoritative Task attempt.\n- Lifecycle authority comes from the active Dispatch, not a terminal title,\n copied ID, old database row, provider transcript, or visible pane.\n- Workers use the exact executable, handle, Task ID, and Dispatch ID in the live\n preamble, plus any other flag it carries. Never reconstruct, translate, or\n broaden those arguments.\n- After remote start, address the worker by Dispatch ID. The execution host owns\n process, filesystem, transcript, stop, and cleanup facts. Preserve the verdicts\n `live` / `unverifiable` / `exited`; contact loss is not process death.\n- Liveness is layered: `worker-list`'s `projection.liveness` is the fleet verdict\n for the agent; `worker-show`'s `observation.status` is PTY liveness only. A live\n terminal can still hold a dead or stuck agent.\n- Folder workspaces are valid; never require Git or assume a worktree.\n- Clients and remote servers update independently. Treat unknown optional fields\n as absent. A new stream operation requires advertised capability because old\n decoders may silently drop unknown opcodes. Never fall back to local execution\n when remote authority or capability is unproven.\n- Use the executable you used to run `skills get` for the entire run. In the\n examples below, replace `ORCA` with it; do not create a shell variable or run\n `ORCA` literally. If it fails, report that exact error instead of switching.\n- `ORCA status --json` shows your Orca session ID as `caller.orcaSessionId` when you have one.\n- A successful `orchestration send` proves durable enqueue; its wake or nudge is\n best-effort attention only and does not prove the recipient read or accepted it.\n\n## Worker obligations\n\nThe injected preamble is authoritative. A dispatched worker must:\n\n1. Do only the current Task and use the preamble's `ask` command for a blocking\n coordinator question. Never open a local question TUI the coordinator cannot\n answer. Resume the same message ID after an ask timeout.\n2. Send heartbeats only at the cadence in the preamble. A heartbeat proves\n liveness, not completion.\n3. Read coordinator follow-ups at each natural checkpoint — before starting a\n new file, after a test run — and once more immediately before `worker_done`:\n `ORCA orchestration check --terminal --json`.\n4. Send `worker_done` exactly once, from the dispatched terminal, with a\n three-sentence executive summary, both lifecycle IDs, and explicit\n `--outcome succeeded` or `--outcome failed`. Never encode failure only in prose.\n5. Append `--files-modified` and `--report-path` only with real values when\n applicable. After `worker_done`, end the dispatched turn and idle; do not poll\n or start new work.\n\nA direct user instruction after completion starts new user-owned work and takes\nprecedence over the idle rule. Do not reuse the settled lifecycle IDs.\n\n## Canonical supervised loop\n\nConfirm the runtime, bind one Run, and start the full independent wave before\nwaiting. `worker-start --spec` creates the Task and its attempt in one call:\n\n```text\nORCA status --json\nORCA orchestration run-create --objective \"\" --json\nORCA orchestration worker-start --spec \"\" --worktree current --agent codex --json\nORCA orchestration worker-start --spec \"\" --worktree current --agent claude --json\nORCA orchestration check --wait --types \"worker_done,escalation,question\" --timeout-ms 900000 --json\n```\n\nAfter a restart, resume your own Run with `run-use --id `. A worker\ncannot bind the Run it works for or an ancestor, and cannot use\n`--takeover-legacy`; an already-bound no-op remains allowed. When its\ncoordinator directs a handoff, a replacement worker may resume a separate Run\nwith plain `run-use`.\n\nIf `worker-start` exits non-zero, do not relaunch. Read the receipt's\n`failedStage` and `residualResources`, then load\n`references/recovery-and-cleanup.md`.\n\nUse `task-create` plus `worker-start --task ` for planned fan-out with\ndependencies or a retry of a known Task. Use dependencies only for real ordering\nand prefer parallel waves over chains deeper than three or four steps; nested\nworkers obey the depth limit, and a new Run does not reset the caller's depth.\nA dispatched worker starts its own helpers with `worker-start --spec` on the Run\nit works for, without `run-create`; the helpers report to that worker.\n\nA consuming `check` names its caller with `--terminal `, never `--from`;\nomit it inside the coordinator's own Orca terminal. It returns the bound Run's\noldest FIFO Delivery and replays that batch until acknowledged. Process every\nmessage: reply to questions, validate each `worker_done` against the expected\nactive Dispatch, and decide each settled terminal's next owner before the ack:\n\n```text\nORCA orchestration reply --id --body \"\" --json\nORCA orchestration worker-release --dispatch --json\nORCA orchestration check --ack --wait --types \"worker_done,escalation,question\" --timeout-ms 900000 --json\n```\n\nKeep waiting until every expected Dispatch settles. A timeout or empty result is\na checkpoint, not a failure. Do not stop, retry, release, or launch a duplicate\neditor without the positive proof `## Outcome` requires.\n\nAfter three consecutive empty waits, stop waiting blindly and enumerate with\n`ORCA orchestration worker-list --include-remote --json` (defaults to the bound\nRun; `--run ` overrides; the receipt's `scope` names which), acting on\neach row's `projection.attention` categories, `projection.attention.requiresAction`, and literal `projection.nextAction` argv.\nRows come newest first and page at 100: while `page.hasMore`, follow `page.nextCursor` with `--cursor `.\nA `none` `nextAction` has no argv to run: read `liveness.reason` and keep waiting\nwith `check --wait`. Absence never earns an argv; settlement and pending work still do.\nLeave the wait only on positive proof the agent stopped: `exited` liveness, the\nworker's own observation of process exit, or a transcript whose final agent turn\nsent no `worker_done`. Then load `references/recovery-and-cleanup.md` and choose\n`worker-stop` or `worker-abandon` explicitly. `unverifiable` is absence,\nincluding when `worker-show` reports `agentWait` null. Absence never authorizes\nstop, abandon, retry, or release; keep waiting or inspect.\n\n`worker-start` is the normal path, composing placement, terminal readiness,\nprompt injection, and supervised resource ownership. `dispatch --inject` leaves\nan operator-created process unsupervised and is only for an expressiveness gap.\n\n## Task-spec contract\n\nEvery Task spec must be self-contained and name:\n\n- **Target:** the files, component, or environment in scope.\n- **Change:** the concrete result to produce.\n- **Constraints:** invariants, compatibility rules, and do-not-touch boundaries.\n- **Ownership:** what this worker may edit and any coordination boundary.\n- **Observable acceptance:** the test, output, or evidence that proves completion.\n\n## Completion accounting\n\nAfter an accepted success or failure report, immediately do exactly one:\n\n1. Reuse the same proven agent terminal for an immediate follow-up Dispatch.\n2. Record user-requested retention with `worker-retain`.\n3. Run `worker-release`.\n\nRelease is post-settlement cleanup, not cancellation. Only an accepted\nsettlement authorizes it; no other observation does. If release is uncertain,\nfollow its exact recovery receipt and never substitute `terminal close`.\n\nA valid `worker_done` settles the Task and Dispatch automatically; do not follow\nit with `task-update --status completed`. Enumerate the terminals still owing a\ndecision with `worker-list --run --terminal-state reclaimable --json`,\nand do not end the coordinator turn until it returns none.\n\n## Conditional references\n\nThis compact guide is sufficient for the normal local loop. At an action gate\nbelow, run `ORCA skills get orchestration --reference references/.md` and\nread only that document; `--references` lists the names. If the CLI rejects\n`--reference`, run `ORCA skills get orchestration --full` once instead: it\nreturns this exact kernel and every reference, so read only the named one. If an\nolder CLI rejects `--full`, keep this kernel's safety floor, use that command's\n`--help`, and never guess newer flags.\n\n| Action gate | Bundled reference |\n| ------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |\n| Expanded DAG waves, launch model/effort, same-terminal reuse, or review ownership | `references/coordinator-loop.md` |\n| You are a dispatched worker and the live preamble does not answer your question, or `check` returned an error | `references/worker-contract.md` |\n| New worktree, exact workspace, SSH, WSL, or connected-server placement | `references/placement-and-remote.md` |\n| Inbox replay, follow-up messages, group addresses, or decision gates | `references/messaging-and-gates.md` |\n| Failed/stopped/unknown attempts, retry, stop, abandon, retain, or uncertain release | `references/recovery-and-cleanup.md` |\n| Custom argv or terminal topology that `worker-start` cannot express | `references/low-level-topology.md` |\n| Any legacy label, adopted Run, compatibility receipt, or takeover | `references/legacy-contract-migration.md` |\n\nRetired scheduler commands are not aliases for Run creation. Recovery commands\nmust provide their exact next action; follow it with the same selected executable.\n\n---\n\n# Bundled references\n\nThese references belong to the version-matched guide above. Read only the documents named by its action gates.\n\n\n\n# Coordinator loop\n\nLoad this reference for expanded DAG waves, per-invocation launch preferences,\nsame-terminal reuse, or review ownership. The compact guide remains the source\nof truth for the loop order and completion boundary.\n\n## Ready waves\n\nCreate independent Tasks before the first wait. Encode only real dependencies,\nthen use the ready view as external memory:\n\n```text\nORCA orchestration task-create --spec \"\" --deps --json\nORCA orchestration task-list --ready --brief --json\n```\n\n`--brief` collapses whitespace and caps echoed specs at 160 characters;\n`spec_truncated` identifies shortened rows. Omit it when full specs are needed or\nwhen an older CLI rejects the flag. A nested worker must respect\n`nested_worker_depth_exceeded`; creating another Run does not reset depth.\nHelpers a worker starts appear in this Run's `worker-list` with their\n`creatorDispatchId`, and report to that worker rather than to you.\n\n## Launch preferences\n\nFor a fresh Claude, Codex, Cursor, Antigravity, or Muse terminal, `--model`\naccepts an opaque provider model ID. Pass it only when the user named a model;\notherwise omit it so the worker inherits the user's configured agent default.\nAdd `--effort` only when that model supports it:\n\n```text\nORCA orchestration worker-start --task --worktree current --agent claude --model opus --effort high --json\nORCA orchestration worker-start --task --worktree current --agent muse --model muse-spark-1.3 --json\n```\n\nOpenCode also accepts `--model` in an existing worktree when the execution host\nverifies its CLI version and model availability. Creating a new worktree with an\nOpenCode model override is unsupported; use `--worktree current` or an existing\nworktree selector, or omit `--model`. OpenCode effort is unsupported. Omit the\nmodel to inherit its configuration; unsupported or unknown hosts refuse the\noverride explicitly.\n\n`--effort` requires `--model`; neither option combines with `--terminal`. A\nconnected worker server must advertise launch-preference support before Orca\nforwards either field. Compare `launch.requested` with `launch.effective`; never\nclaim a model or effort from requested arguments alone.\n\n## Reuse after settlement\n\nChoose the terminal's next owner before acknowledging the Delivery. When the\nsame exact agent has immediate follow-up work, recover the proven handle and\ntransfer cleanup ownership to the new Dispatch:\n\n```text\nORCA orchestration worker-show --dispatch --json\nORCA orchestration worker-start --task --terminal --json\n```\n\nOtherwise explicitly retain or release the settled worker. Do not leave it live\nonly to inspect output; archived output remains available through `worker-read`.\n\n## Review ownership\n\nA review-only `worker_done` authorizes synthesis of findings, not coordinator\nfile edits. Dispatch or hand off fixes unless the user explicitly assigned them\nto the coordinator. If the user's plan names a next owner, post-review fixes and\nPR preparation remain with that owner; the coordinator routes and synthesizes.\n\n\n\n# Legacy contract migration\n\nLoad this reference only for an authority label, adopted Run, compatibility or\nrecovery receipt, or explicit legacy takeover. A newly created attempt always\nuses the current grammar.\n\n## Authority labels\n\n- `[LEGACY COMPATIBILITY]` is live and attested. Run only the exact supported\n command printed with the message, using the same selected executable and\n arguments supplied by the original prompt.\n- `[LEGACY RECOVERY REPLAY — MAY HAVE BEEN SEEN]` is one bounded,\n at-least-once cutover replay. Process it idempotently and acknowledge only\n through the exact displayed guidance.\n- `[LEGACY READ-ONLY]` is inspection-only. It has no reply, acknowledgment, or\n lifecycle mutation.\n- An unlabeled current message uses the current guide and grammar.\n\nAn explicitly selected current Run, attested current binding, current Dispatch,\nor federated attachment takes precedence over legacy fallback. A retained\nadoption record alone does not grant mutation authority. If liveness, principal\nownership, capability, or the exact legacy contract is unproven, degrade to\nread-only inspection and never fall back to local execution.\n\nAdoption preserves the live agent process, PTY/session, terminal handle,\ntab/pane, worktree or folder workspace, Task, and Dispatch. It never restarts or\nreplaces the worker and never revives the retired scheduler. Loss of lifecycle\nauthority does not invalidate the existing process, assignment, or filesystem\nwork. Exact recovery may restore the same PTY once in its original inactive\nbackground tab; it must not spawn, write, signal, stop, switch, focus, split, or\ninject a terminal.\n\n## Compatibility recovery\n\nWhen a compatibility response returns structured next-step arguments, execute\nthose exact arguments with the same selected CLI executable. Do not translate\nfrom memory, broaden the recipient, or retry as a current mutation unless the\nreceipt explicitly authorizes it.\n\nA pending ask, reply, final Dispatch settlement, and consuming check have\ndurable recovery identities. Heartbeat and escalation remain at-least-once\nacross a manual contract-boundary retry. If an ask may already have been\nanswered, run the exact non-consuming recovery check printed by Orca before\ncreating any new question. Never guess among identical question threads.\n\nOn packaged Windows, a legacy ask uses a two-step commit/resume protocol. The\ninitial command commits the question, prints its exact\n`ask --resume ` command, and exits with launcher status `75`. Run\nthat exact resume after the launcher or update boundary. For an attested WSL\nlaunch, preserve the printed `orca-ide` executable and distro route. Older WSL\nworkers without launch proof remain lifecycle read-only even while their\nterminal and filesystem work continue.\n\n## Read-only inspection and takeover\n\nRead-only inspection does not consume mail:\n\n```text\nORCA orchestration run-list --json\nORCA orchestration run-show --id run_legacy_local --json\nORCA orchestration run-show --id --json\nORCA orchestration task-list --run --json\nORCA orchestration inbox --full --json\nORCA orchestration check --terminal --peek --format --json\nORCA terminal read --terminal --json\nORCA terminal wait --terminal --for tui-idle --timeout-ms 60000 --json\n```\n\n`run_legacy_local` is an empty audit tombstone after adoption. Find the ordinary\nRun whose objective is `Recovered orchestration work from a contract update`.\n\nOnly when the original coordinator is unavailable or cannot prove retained\nauthority may a new live coordinator take over from its own terminal:\n\n```text\nORCA orchestration run-use --id --takeover-legacy --json\nORCA orchestration check --run --json\n```\n\nTakeover binds the authenticated invoking terminal; `--from` cannot nominate\nanother coordinator. It fences only the old coordinator and moves pending mail\ninto current Run delivery. It preserves live workers, Tasks, Dispatches, processes, and files.\nNever take over while the original coordinator is actively coordinating.\n\nDo not launch a replacement editor merely because Orca updated or authority is\nunclear. Keep the original worker as the only editor until a stable handoff\npoint, then use a fresh current Dispatch in a conflict-free placement.\n\n\n\n# Low-level topology\n\nLoad this reference only when `worker-start` cannot express required custom argv\nor terminal topology. It is not the normal supervised loop and is never a full\nhandoff recipe. A model or effort is not custom argv: `worker-start --model\n--effort` carries them and keeps the user's Chat UI setting.\n\n```text\nORCA terminal create --worktree active --title --command \"\" --json\nORCA terminal wait --terminal --for tui-idle --timeout-ms 60000 --json\nORCA orchestration dispatch --task --to --inject --json\n```\n\nWait for readiness only when startup could lose injected input. Prefer\nagent-first `worker-start` whenever its argv and topology are sufficient.\n\n`dispatch --inject` creates authoritative Task/Dispatch context but deliberately\nkeeps an operator-created process unsupervised: it creates no supervised worker\nresource row. `worker-show`, `worker-read`, and `worker-list` report the lane as\n`unsupervised`; `worker-stop` and `worker-abandon` do not close that process, and\nsettled retain/release take no process action.\n\nUse `worker-start --terminal ` when lifecycle ownership of an existing\nagent terminal is required. Never imply that low-level dispatch retroactively\nowns a process, never use it to route around the nested-depth limit, and never\nuse it for an ownership handoff.\n\n\n\n# Messaging and gates\n\nLoad this reference for inbox replay, attempt-specific guidance, group\naddresses, blocking questions, or coordinator-managed DAG decisions.\n\nA successful `send` proves durable enqueue. Wake and nudge are best-effort\nattention only: neither proves the recipient read the message, began a turn, or\naccepted steering.\n\n## Coordinator delivery loop\n\n`check` names its caller with `--terminal ` and is the only verb that\nrejects `--from`. Omit `--terminal` inside an Orca terminal, where Orca resolves\nthe caller; pass it explicitly from anywhere else, including a dispatched\nworker reading coordinator follow-ups.\n\nA consuming coordinator `check` returns the bound Run's oldest FIFO Delivery,\nup to 50 messages, and replays that exact batch until acknowledged or all its\nmessages are marked read. Concurrent waits on one Run inbox are allowed; they\nshare the outstanding batch and reserve nothing, and acknowledging the same\nDelivery twice is a no-op. Process every row and required terminal ownership\ndecision before `--ack`. Type filters decide when a waiter wakes; they do not\nauthorize skipping older actionable mail. A Delivery therefore always carries\nthe whole FIFO batch whatever its types, and a `check` without `--wait` hands\nthat batch over unfiltered.\n`--peek` and `--all` are read-only inspection, not progress through the\ncoordinator inbox.\n\nAn empty wait or timeout is a checkpoint. Continue rolling waits until every\nexpected Dispatch settles. Heartbeat or visible activity means alive, not done.\n\n## Addresses\n\nUse a stable Dispatch address for attempt-specific coordinator guidance:\n\n```text\nORCA orchestration send --to dispatch: --subject \"Follow-up\" --body \"\" --json\n```\n\nDo not substitute a remote terminal handle. Omit `--from` for ordinary\ncoordinator calls; a dispatched worker instead copies the exact `--from` and\nother arguments in its preamble. `check` is the exception: it identifies\nits caller with `--terminal`, never `--from`.\n\nGroup addresses include `@all`, `@idle`, `@claude`, `@codex`, `@opencode`,\n`@gemini`, `@droid`, `@grok`, `@cursor`, and `@worktree:`. Every group but\n`@worktree:` means the live Dispatches of the sender's own Run. Mail goes\nto each `dispatch:` mailbox, except a worker coordinating a child Run\nreceives it in that `run:` mailbox. A sender bound to no Run is refused;\n`--run` must match the group audience and never grants membership.\nA Run group excludes its owning coordinator; a worker raising a blocker sends\nto `run:`. A worker that created its own Run addresses that Run's workers,\nnot its siblings. `@worktree:` reaches matching workspace terminals,\nincluding coordinators. Use groups only for intentional fan-out status or\nquestions. `worker_done`, heartbeat, and other\nDispatch lifecycle messages never target groups.\n\n## Questions and gates\n\nA worker uses `ask`; its timeout leaves one durable question pending, which the\nworker resumes by message ID. The coordinator answers that message with `reply`;\na helper's question goes to the worker that started it, which answers it the\nsame way.\n\nUse a gate only for a coordinator-owned Task-DAG decision:\n\n```text\nORCA orchestration gate-create --task --question \"\" --options --json\nORCA orchestration gate-resolve --id --resolution \"\" --json\nORCA orchestration gate-list --task --json\n```\n\nPass `json_array` using the quoting rules of the active shell; do not copy POSIX\nsingle-quote syntax into PowerShell or `cmd.exe`.\n\nDo not create a gate merely to answer a worker's `ask`.\n\n\n\n# Placement and remote execution\n\nLoad this reference before creating a new worktree or placing work through SSH,\nWSL, or another connected Orca server.\n\n## Placement choices\n\nA fresh worker means a fresh agent terminal, not a new Git worktree. Use the\ncurrent or an exact existing workspace by default. Create a worktree only when\nthe user requested one or a concrete checkout or filesystem conflict makes\nsharing unsafe.\n\n```text\n# Current workspace; setup is not rerun.\nORCA orchestration worker-start --task --worktree current --agent codex --json\n\n# Stacked child worktree.\nORCA orchestration worker-start --task --worktree new-child --name --agent codex --setup run --json\n\n# Independent top-level worktree.\nORCA orchestration worker-start --task --worktree new-top-level --name --agent codex --setup run --json\n```\n\nCurrent and exact existing workspaces create a fresh terminal unless\n`--terminal` is explicit. Folder workspaces are first-class; do not invoke Git\nor require worktree lineage when the selected workspace is a folder.\n\nRegister a folder workspace through project setup. `repo add --path `\nrequires a valid Git repository and rejects a plain directory:\n\n```text\nORCA project setup-existing-folder --project --host --path --kind folder --json\n```\n\nThen place work on the returned workspace with an exact selector. A worktree\nselector needs the full `::` value Orca returned, passed as\n`id:`; a bare repo id is not a worktree id. `new-child` and\n`new-top-level` are worktree creation and do not apply to a folder.\n\nNew worktrees use agent-first creation and run setup by default. Preserve the\nrepository's startup policy: `start-immediately` can report setup as `running`,\nwhile `wait-for-setup` gates prompt delivery on success. Orca lineage, Git base,\nfilesystem isolation, coordination parentage, UI grouping, and execution host\nare separate decisions.\n\n## Connected servers\n\nThe Run and Tasks remain authoritative on the current server. `--on` selects\nonly the worker's execution server and appears only on `worker-start`:\n\n```text\nORCA orchestration worker-start --task --on --worktree new-top-level --repo --name --agent codex --setup run --json\n```\n\nRemote `current` and `new-child` are invalid because they are ambiguous across\nservers. Use an exact discovered remote workspace, or `new-top-level` with an\nexact remote repository selector. After start, route every follow-up, read,\nstop, and cleanup by Dispatch ID; never repeat `--on` or substitute a remote\nterminal handle.\n\n```text\nORCA orchestration worker-show --dispatch --json\nORCA orchestration worker-read --dispatch --limit 50 --json\nORCA orchestration send --to dispatch: --subject \"Follow-up\" --body \"\" --json\nORCA orchestration worker-list --run --include-remote --json\n```\n\n`worker-list` reads local fleet state only; enumerate remote workers with\n`--include-remote` or every one of them reads `unverifiable`. Scope every list\nwith `--run `: unscoped, it reports every Dispatch this runtime has\nrecorded, and the workers you are waiting on are lost in that history.\n\n## Execution-host and mixed-version floor\n\nThe execution host owns process, filesystem, transcript, stop, and cleanup\nfacts. Render only `live`, `unverifiable`, or `exited`. Connection loss, relay\nabsence, missing client inventory, or timeout yields `unverifiable`, never\nsynthetic exit and never a client-local substitute action.\n\nClients and servers update independently. Optional response fields may be\nabsent. Forward model/effort, transcript reads, cleanup, or another new remote\noperation only when the peer advertises the relevant capability; unknown stream\nopcodes can be silently dropped. A narrow unsupported response may degrade to a\ndocumented older path, but must not broaden the target or cross the execution\nboundary. Changing host-published content reaches old clients even without a\nwire-shape change, so preserve established semantics or negotiate the behavior.\n\nFor WSL, use the exact executable and arguments returned by Orca so the distro\nand packaged launcher remain bound. Do not translate a printed `orca-ide`\nrecovery command into a PATH-resolved local command.\n\n\n\n# Recovery and cleanup\n\nLoad this reference only after a failed/stopped/unknown attempt, explicit retry\ndecision, stop/abandon request, retention request, or uncertain release.\n\n| Proven state | Safe action |\n| ----------------------- | ------------------------------------------------------------------ |\n| `ready` or active | Keep waiting; optionally read bounded output |\n| `failed` or `stopped` | Start a replacement with `--retry-of`; repeat placement explicitly |\n| `outcome_unknown` | Inspect, then choose `worker-stop` or explicit `worker-abandon` |\n| Accepted `worker_done` | Reuse, retain, or release |\n| Remote contact lost | Preserve `unverifiable`; do not stop or retry from absence alone |\n| `unverifiable` liveness | Keep waiting or inspect; never stop, abandon, retry, or release |\n| Proven `exited` agent | Enumerate with `worker-list`; follow its `nextAction` |\n\n## Inspect before acting\n\n```text\nORCA orchestration worker-list --run --json\nORCA orchestration worker-list --run --include-remote --json\nORCA orchestration worker-show --dispatch --json\nORCA orchestration worker-read --dispatch --limit 50 --json\n```\n\n`worker-list` is the enumerating command and the authority on agent liveness:\neach row carries `projection.liveness`, `projection.attention.categories`,\n`projection.attention.requiresAction`, and a literal `projection.nextAction`\nargv to run. Always scope it with `--run `; an unscoped list reports\nevery Dispatch this runtime has ever recorded and buries the live ones.\n`worker-show`'s `observation.status` is PTY liveness only, so a `live` terminal\nwhose agent died at a trust prompt still reads `live` there.\n\nWhen the two disagree, the fleet verdict decides — unless the fleet row is\n`unverifiable` for a reason that names a gap on this client rather than a fact\nabout the worker. `missing_status`, `host_unavailable`, and\n`capability_unsupported` are such gaps: the first means this runtime holds no\nstatus row, the second that it could not ask the execution host at all, and the\nthird that a stale peer answered but lacks the fleet-snapshot capability.\nAgainst any of them, a `worker-show` verdict sourced from the execution host is\nthe better evidence and outranks the row. Only `host_unavailable` is contact\nloss; the other two mean the host was never asked or answered without the\ncapability.\n\nThis never promotes absence. `unverifiable` from either command still authorizes\nnothing — only a positive `live` or `exited` verdict does.\n\nA worker started with `--on ` reads `unverifiable` until you\nenumerate with `--include-remote`, which asks its execution host for the\nverdict. Rows come newest first and past 100 the response pages, so follow\n`page.nextCursor` with `--cursor ` until `page.hasMore` is false.\n\n## Stall needs positive evidence\n\nLeave the wait only on positive proof the agent stopped: `exited` liveness, the\nworker's own observation of process exit, or a transcript whose final agent turn\nsent no `worker_done`. Only then choose `worker-stop` or `worker-abandon`.\n\n`unverifiable` is always absence — `missing_status`, `stale_status`,\n`restored_unconfirmed`, or a remote worker with no connection — and a null\n`agentWait` or an unchanged `worker-read` tail is that same absence seen again.\nAbsence never authorizes stop, abandon, retry, or release: keep waiting, or\ninspect until you hold one of the positive signals above. A `nextAction` that\nnames an inspecting command is asking for evidence, not for cleanup.\n\n`worker-read --source auto` uses a proven provider transcript when available and\notherwise returns bounded terminal output with a typed `fallbackReason`.\nContinue with its top-level cursor, which is pinned to that source. If Orca\nreports `source_changed`, restart without the old cursor. A bounded initial\ntranscript tail can return an EOF cursor that follows only newly appended records;\nread `contentComplete`, `clipping`, and `warnings` before assuming omitted older\nrecords are pageable. Never guess a provider session ID, transcript path, or\nremote terminal handle.\n\n## Was the mutation applied?\n\nWhen a mutation's response was lost and named no Dispatch, do not replay blind.\nEvery orchestration mutation accepts `--retry-request `, which reuses one\noperation identity so Orca can replay, join, or recover it instead of starting a\nduplicate. Ask what happened first:\n\n```text\nORCA orchestration request-show --request --json\n```\n\n`completed` means the mutation already took effect; read its recorded receipt\ninstead of rerunning. `pending` means the original mutation is still running or\nOrca restarted before recording its outcome; replay the original command with\n`--retry-request `. `absent` means this runtime holds no receipt\nunder your caller identity — that is not proof nothing happened, so inspect the\naffected Task, Dispatch, and terminal before deciding whether to retry.\n\nWhen a worker's terminal accepted input but the submit is unconfirmed, use\n`terminal send --wait-submit `: it observes the accepted prompt for that\nlong and, on timeout, returns the input-accepted receipt without resending.\n\n## Refused starts\n\n`dispatch` and `worker-start` refuse the following preflight cases with a stable\n`error.code`; read it before choosing a recovery, and treat `error.data.nextSteps`\nas the exact recovery text. Older hosts may omit `data`, so treat every field as\noptional.\n\n| Code | Meaning | Recovery |\n| -------------------- | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |\n| `task_not_found` | No Task with that id, or not in the bound Run (`data.taskId`, `data.runId`) | Check `task-list --json`; create the Task with `task-create` if it does not exist |\n| `task_not_startable` | Task cannot start now: not `ready`, or invalid `--retry-of` (`data.status`, `data.unmetDependencies`, `data.retryOf`) | Wait for running dependencies with `check --wait`; retry or unblock failed ones; inspect `dispatch-show` if already dispatched |\n| `inject_rejected` | `--inject` refused because no recognized agent runs in the target (`data.terminal`, `data.reason`) | Start a recognized agent there or pick another terminal; or dispatch without `--inject` and use `terminal send` |\n| `runtime_error` | Any other failure, including a target terminal that already owns an active Dispatch | Read the message, inspect state, and do not retry unchanged |\n\n## Retry, stop, and abandon\n\nRetry only a positively proven failed or stopped attempt. Name the failed Task\nwith `--task`, since `--spec` creates a new one. Placement is never silently\ninherited:\n\n```text\nORCA orchestration worker-start --task --retry-of --worktree --agent --json\n```\n\nAfter three consecutive failures for one Task, its dispatch context\ncircuit-breaks and the Task is failed. Failed `worker-start` attempts count\nacross `--retry-of`; a worker's own `failed` report does not. `--retry-of` a\ncircuit-broken Dispatch is refused with `task_not_startable`: report the failure\nto the user instead. Do not route around that boundary with a new Run, Task, or\nunrelated Dispatch.\n\nFor `outcome_unknown`, inspect first, then make an explicit choice:\n\n```text\nORCA orchestration worker-stop --dispatch --json\nORCA orchestration worker-abandon --dispatch --json\n```\n\n`worker-stop` closes only the exact proven supervised agent terminal. It never\ndeletes the worktree, setup terminal, configured tabs, or unrelated processes.\n`worker-abandon` fences orchestration while accepting that resources may remain\nlive; it performs no remote, process, or filesystem action. When it settles a\nworker, it retains an owned terminal by the same rule as `worker-retain`, so Orca\nstops owing its release; a release already committed (`releasing`,\n`release_unknown`) is left as it is. An already-settled worker is left untouched.\n\nTo cancel a Task, settle its worker with `worker-stop` or `worker-abandon`, then\nrecord the reason. A cancelled Task is `failed`, so its dependents stay blocked\nuntil a `--retry-of` replacement completes it:\n\n```text\nORCA orchestration task-update --id --status failed --result cancelled --json\n```\n\n## Retain and release\n\n```text\nORCA orchestration worker-retain --dispatch --json\nORCA orchestration worker-release --dispatch --json\n```\n\nRetain only when the user explicitly wants the settled terminal kept live.\nRelease works after succeeded and failed reports, archives readable output, and\ncloses only the exact terminal owned by that settled Dispatch. Replays may call\nrelease again safely. Reused, pre-existing, setup, coordinator, active,\nuser-taken-over, and unproven terminals are retained.\n\nA `worker-start` that failed before its agent was ready still owns the terminal\nit created. Its receipt names `worker-release`, and `worker-list` reports that\nrow as `reclaimable`; release it there rather than closing the terminal by hand.\nA `--retry-of` replacement for a `failed` worker releases the terminal that\nworker still owns the same way.\n\nNever release because of timeout, TUI idle, heartbeat, status, question,\nescalation, or stale/rejected completion. If the receipt says `release_pending`\nor `release_unknown`, follow its exact recovery action. Never substitute\n`terminal close`.\n\n`orchestration reset` is destructive recovery. Do not run it during active\ncoordination unless the user explicitly abandons that state.\n\n\n\n# Worker contract\n\nThe injected preamble is authoritative. Copy its command rather than\nreconstructing flags. In particular, preserve the exact executable, worker\nhandle, Task ID, and Dispatch ID, and keep any other flag it carries (an older\nOrca host adds `--dispatch-capability`).\n\n## Heartbeat\n\nSend heartbeats only at the cadence required by the live preamble. Skip them\nwhile blocked inside `ask` or `check --wait`; those calls are liveness signals.\n\n```text\nORCA orchestration send --from --type heartbeat --subject \"alive\" --task-id --dispatch-id --phase \"\"\n```\n\nUse typed lifecycle flags, not a hand-written JSON payload. A heartbeat proves\nliveness, never completion.\n\n## Ask and resume\n\nUse Orca `ask` whenever the coordinator must answer. Never open a local question\nTUI the coordinator cannot answer.\n\n```text\nORCA orchestration ask --from --question \"\" --options \",\" --timeout-ms 600000\n\nORCA orchestration ask --from --resume --timeout-ms 600000\n```\n\nEach wait defaults to 600000 ms (10 minutes). `--timeout-ms` accepts a positive\ninteger up to 1800000 ms (30 minutes); larger values are rejected before creating\nor resuming a question. For decisions that take longer, resume the same question\nafter each timeout.\n\nA timeout or disconnect leaves the original question pending. Resume its\nmessage ID; do not create a duplicate question.\n\n## Reading coordinator follow-ups\n\nThe coordinator steers a running worker with `send --to dispatch:`. That\nenqueue is durable but does not interrupt you, so nothing arrives unless you\nlook:\n\n```text\nORCA orchestration check --terminal --json\n```\n\nRun it at each natural checkpoint — before starting a new file, after a test\nrun — and once more immediately before `worker_done`, so a redirect or a\ncancellation lands before the Task settles. `check` names its caller with\n`--terminal`, never `--from`. Stop checking after `worker_done`.\n\nDo not use `run-use` to check your Dispatch's mail or bind the Run you work for.\nYou may resume a separate Run when your coordinator directs a handoff, but you\ncannot use `--takeover-legacy`. Plain `check` returns your Dispatch's mail\nunless you are coordinating a Run; report to your coordinator with `send`.\n\nIf a plain `check` (no `--run`) returns `consumer_fenced`, this process no longer owns its Dispatch:\nthe Attempt was re-attached to another worker or settled without you. Stop, do\nnot send `worker_done`, and do not retry the check. An empty `check` never means\nyou were replaced; `consumer_fenced` is the only way you learn that.\nIf `send` or `ask` returns `consumer_fenced`, the command ran from another\nparty's terminal (a coordinator or another worker); run it from your own terminal.\n\n## Helper workers\n\nWhen the preamble has a SUB-DISPATCH section, start helpers on your own\nDispatch's Run. Do not `run-create` or `run-use` for them.\n\n```text\nORCA orchestration worker-start --from --spec \"\" --worktree current --agent --json\nORCA orchestration reply --from --id --body \"\"\n```\n\nTheir questions, escalations, and `worker_done` arrive in your own `check`, next\nto your coordinator's mail. Answer their questions with `reply`, and wait for\nevery helper's `worker_done` before sending yours. After you settle, a helper\nstill running reports to your coordinator's Run.\n\n## Escalation\n\nEscalate only before completion and only when the coordinator must intervene:\n\n```text\nORCA orchestration send --from --type escalation --subject \"Blocked: \" --body \"
\" --task-id --dispatch-id \n```\n\n## Completion\n\nSend exactly one terminal report. `--body` is three sentences: what changed,\nwhat was found, and what remains. Use `--outcome failed` when the requested work\nis not complete; never hide failure in prose or silently exit.\n\nAppend `--files-modified` or `--report-path` only when applicable, using actual\npaths. Do not send documentation placeholders as metadata.\n\n```text\nORCA orchestration send --from --type worker_done --subject \"\" --body \"\" --task-id --dispatch-id --outcome succeeded\n```\n\nAfter `worker_done`, end the dispatched turn and idle. Do not poll, close your\nown terminal, or begin unrelated work. A later direct user instruction is new\nuser-owned work and must not reuse settled lifecycle IDs; a supervised follow-up\narrives with a fresh preamble and Task block.\n" // oxfmt-ignore const ORCHESTRATION_COORDINATOR_LOOP_REFERENCE_MARKDOWN = "# Coordinator loop\n\nLoad this reference for expanded DAG waves, per-invocation launch preferences,\nsame-terminal reuse, or review ownership. The compact guide remains the source\nof truth for the loop order and completion boundary.\n\n## Ready waves\n\nCreate independent Tasks before the first wait. Encode only real dependencies,\nthen use the ready view as external memory:\n\n```text\nORCA orchestration task-create --spec \"\" --deps --json\nORCA orchestration task-list --ready --brief --json\n```\n\n`--brief` collapses whitespace and caps echoed specs at 160 characters;\n`spec_truncated` identifies shortened rows. Omit it when full specs are needed or\nwhen an older CLI rejects the flag. A nested worker must respect\n`nested_worker_depth_exceeded`; creating another Run does not reset depth.\nHelpers a worker starts appear in this Run's `worker-list` with their\n`creatorDispatchId`, and report to that worker rather than to you.\n\n## Launch preferences\n\nFor a fresh Claude, Codex, Cursor, Antigravity, or Muse terminal, `--model`\naccepts an opaque provider model ID. Pass it only when the user named a model;\notherwise omit it so the worker inherits the user's configured agent default.\nAdd `--effort` only when that model supports it:\n\n```text\nORCA orchestration worker-start --task --worktree current --agent claude --model opus --effort high --json\nORCA orchestration worker-start --task --worktree current --agent muse --model muse-spark-1.3 --json\n```\n\nOpenCode also accepts `--model` in an existing worktree when the execution host\nverifies its CLI version and model availability. Creating a new worktree with an\nOpenCode model override is unsupported; use `--worktree current` or an existing\nworktree selector, or omit `--model`. OpenCode effort is unsupported. Omit the\nmodel to inherit its configuration; unsupported or unknown hosts refuse the\noverride explicitly.\n\n`--effort` requires `--model`; neither option combines with `--terminal`. A\nconnected worker server must advertise launch-preference support before Orca\nforwards either field. Compare `launch.requested` with `launch.effective`; never\nclaim a model or effort from requested arguments alone.\n\n## Reuse after settlement\n\nChoose the terminal's next owner before acknowledging the Delivery. When the\nsame exact agent has immediate follow-up work, recover the proven handle and\ntransfer cleanup ownership to the new Dispatch:\n\n```text\nORCA orchestration worker-show --dispatch --json\nORCA orchestration worker-start --task --terminal --json\n```\n\nOtherwise explicitly retain or release the settled worker. Do not leave it live\nonly to inspect output; archived output remains available through `worker-read`.\n\n## Review ownership\n\nA review-only `worker_done` authorizes synthesis of findings, not coordinator\nfile edits. Dispatch or hand off fixes unless the user explicitly assigned them\nto the coordinator. If the user's plan names a next owner, post-review fixes and\nPR preparation remain with that owner; the coordinator routes and synthesizes.\n" @@ -96,7 +96,7 @@ const ORCHESTRATION_PLACEMENT_AND_REMOTE_REFERENCE_MARKDOWN = "# Placement and r const ORCHESTRATION_RECOVERY_AND_CLEANUP_REFERENCE_MARKDOWN = "# Recovery and cleanup\n\nLoad this reference only after a failed/stopped/unknown attempt, explicit retry\ndecision, stop/abandon request, retention request, or uncertain release.\n\n| Proven state | Safe action |\n| ----------------------- | ------------------------------------------------------------------ |\n| `ready` or active | Keep waiting; optionally read bounded output |\n| `failed` or `stopped` | Start a replacement with `--retry-of`; repeat placement explicitly |\n| `outcome_unknown` | Inspect, then choose `worker-stop` or explicit `worker-abandon` |\n| Accepted `worker_done` | Reuse, retain, or release |\n| Remote contact lost | Preserve `unverifiable`; do not stop or retry from absence alone |\n| `unverifiable` liveness | Keep waiting or inspect; never stop, abandon, retry, or release |\n| Proven `exited` agent | Enumerate with `worker-list`; follow its `nextAction` |\n\n## Inspect before acting\n\n```text\nORCA orchestration worker-list --run --json\nORCA orchestration worker-list --run --include-remote --json\nORCA orchestration worker-show --dispatch --json\nORCA orchestration worker-read --dispatch --limit 50 --json\n```\n\n`worker-list` is the enumerating command and the authority on agent liveness:\neach row carries `projection.liveness`, `projection.attention.categories`,\n`projection.attention.requiresAction`, and a literal `projection.nextAction`\nargv to run. Always scope it with `--run `; an unscoped list reports\nevery Dispatch this runtime has ever recorded and buries the live ones.\n`worker-show`'s `observation.status` is PTY liveness only, so a `live` terminal\nwhose agent died at a trust prompt still reads `live` there.\n\nWhen the two disagree, the fleet verdict decides — unless the fleet row is\n`unverifiable` for a reason that names a gap on this client rather than a fact\nabout the worker. `missing_status`, `host_unavailable`, and\n`capability_unsupported` are such gaps: the first means this runtime holds no\nstatus row, the second that it could not ask the execution host at all, and the\nthird that a stale peer answered but lacks the fleet-snapshot capability.\nAgainst any of them, a `worker-show` verdict sourced from the execution host is\nthe better evidence and outranks the row. Only `host_unavailable` is contact\nloss; the other two mean the host was never asked or answered without the\ncapability.\n\nThis never promotes absence. `unverifiable` from either command still authorizes\nnothing — only a positive `live` or `exited` verdict does.\n\nA worker started with `--on ` reads `unverifiable` until you\nenumerate with `--include-remote`, which asks its execution host for the\nverdict. Rows come newest first and past 100 the response pages, so follow\n`page.nextCursor` with `--cursor ` until `page.hasMore` is false.\n\n## Stall needs positive evidence\n\nLeave the wait only on positive proof the agent stopped: `exited` liveness, the\nworker's own observation of process exit, or a transcript whose final agent turn\nsent no `worker_done`. Only then choose `worker-stop` or `worker-abandon`.\n\n`unverifiable` is always absence — `missing_status`, `stale_status`,\n`restored_unconfirmed`, or a remote worker with no connection — and a null\n`agentWait` or an unchanged `worker-read` tail is that same absence seen again.\nAbsence never authorizes stop, abandon, retry, or release: keep waiting, or\ninspect until you hold one of the positive signals above. A `nextAction` that\nnames an inspecting command is asking for evidence, not for cleanup.\n\n`worker-read --source auto` uses a proven provider transcript when available and\notherwise returns bounded terminal output with a typed `fallbackReason`.\nContinue with its top-level cursor, which is pinned to that source. If Orca\nreports `source_changed`, restart without the old cursor. A bounded initial\ntranscript tail can return an EOF cursor that follows only newly appended records;\nread `contentComplete`, `clipping`, and `warnings` before assuming omitted older\nrecords are pageable. Never guess a provider session ID, transcript path, or\nremote terminal handle.\n\n## Was the mutation applied?\n\nWhen a mutation's response was lost and named no Dispatch, do not replay blind.\nEvery orchestration mutation accepts `--retry-request `, which reuses one\noperation identity so Orca can replay, join, or recover it instead of starting a\nduplicate. Ask what happened first:\n\n```text\nORCA orchestration request-show --request --json\n```\n\n`completed` means the mutation already took effect; read its recorded receipt\ninstead of rerunning. `pending` means the original mutation is still running or\nOrca restarted before recording its outcome; replay the original command with\n`--retry-request `. `absent` means this runtime holds no receipt\nunder your caller identity — that is not proof nothing happened, so inspect the\naffected Task, Dispatch, and terminal before deciding whether to retry.\n\nWhen a worker's terminal accepted input but the submit is unconfirmed, use\n`terminal send --wait-submit `: it observes the accepted prompt for that\nlong and, on timeout, returns the input-accepted receipt without resending.\n\n## Refused starts\n\n`dispatch` and `worker-start` refuse the following preflight cases with a stable\n`error.code`; read it before choosing a recovery, and treat `error.data.nextSteps`\nas the exact recovery text. Older hosts may omit `data`, so treat every field as\noptional.\n\n| Code | Meaning | Recovery |\n| -------------------- | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |\n| `task_not_found` | No Task with that id, or not in the bound Run (`data.taskId`, `data.runId`) | Check `task-list --json`; create the Task with `task-create` if it does not exist |\n| `task_not_startable` | Task cannot start now: not `ready`, or invalid `--retry-of` (`data.status`, `data.unmetDependencies`, `data.retryOf`) | Wait for running dependencies with `check --wait`; retry or unblock failed ones; inspect `dispatch-show` if already dispatched |\n| `inject_rejected` | `--inject` refused because no recognized agent runs in the target (`data.terminal`, `data.reason`) | Start a recognized agent there or pick another terminal; or dispatch without `--inject` and use `terminal send` |\n| `runtime_error` | Any other failure, including a target terminal that already owns an active Dispatch | Read the message, inspect state, and do not retry unchanged |\n\n## Retry, stop, and abandon\n\nRetry only a positively proven failed or stopped attempt. Name the failed Task\nwith `--task`, since `--spec` creates a new one. Placement is never silently\ninherited:\n\n```text\nORCA orchestration worker-start --task --retry-of --worktree --agent --json\n```\n\nAfter three consecutive failures for one Task, its dispatch context\ncircuit-breaks and the Task is failed. Failed `worker-start` attempts count\nacross `--retry-of`; a worker's own `failed` report does not. `--retry-of` a\ncircuit-broken Dispatch is refused with `task_not_startable`: report the failure\nto the user instead. Do not route around that boundary with a new Run, Task, or\nunrelated Dispatch.\n\nFor `outcome_unknown`, inspect first, then make an explicit choice:\n\n```text\nORCA orchestration worker-stop --dispatch --json\nORCA orchestration worker-abandon --dispatch --json\n```\n\n`worker-stop` closes only the exact proven supervised agent terminal. It never\ndeletes the worktree, setup terminal, configured tabs, or unrelated processes.\n`worker-abandon` fences orchestration while accepting that resources may remain\nlive; it performs no remote, process, or filesystem action. When it settles a\nworker, it retains an owned terminal by the same rule as `worker-retain`, so Orca\nstops owing its release; a release already committed (`releasing`,\n`release_unknown`) is left as it is. An already-settled worker is left untouched.\n\nTo cancel a Task, settle its worker with `worker-stop` or `worker-abandon`, then\nrecord the reason. A cancelled Task is `failed`, so its dependents stay blocked\nuntil a `--retry-of` replacement completes it:\n\n```text\nORCA orchestration task-update --id --status failed --result cancelled --json\n```\n\n## Retain and release\n\n```text\nORCA orchestration worker-retain --dispatch --json\nORCA orchestration worker-release --dispatch --json\n```\n\nRetain only when the user explicitly wants the settled terminal kept live.\nRelease works after succeeded and failed reports, archives readable output, and\ncloses only the exact terminal owned by that settled Dispatch. Replays may call\nrelease again safely. Reused, pre-existing, setup, coordinator, active,\nuser-taken-over, and unproven terminals are retained.\n\nA `worker-start` that failed before its agent was ready still owns the terminal\nit created. Its receipt names `worker-release`, and `worker-list` reports that\nrow as `reclaimable`; release it there rather than closing the terminal by hand.\nA `--retry-of` replacement for a `failed` worker releases the terminal that\nworker still owns the same way.\n\nNever release because of timeout, TUI idle, heartbeat, status, question,\nescalation, or stale/rejected completion. If the receipt says `release_pending`\nor `release_unknown`, follow its exact recovery action. Never substitute\n`terminal close`.\n\n`orchestration reset` is destructive recovery. Do not run it during active\ncoordination unless the user explicitly abandons that state.\n" // oxfmt-ignore -const ORCHESTRATION_WORKER_CONTRACT_REFERENCE_MARKDOWN = "# Worker contract\n\nThe injected preamble is authoritative. Copy its command rather than\nreconstructing flags. In particular, preserve the exact executable, worker\nhandle, Task ID, and Dispatch ID, and keep any other flag it carries (an older\nOrca host adds `--dispatch-capability`).\n\n## Heartbeat\n\nSend heartbeats only at the cadence required by the live preamble. Skip them\nwhile blocked inside `ask` or `check --wait`; those calls are liveness signals.\n\n```text\nORCA orchestration send --from --type heartbeat --subject \"alive\" --task-id --dispatch-id --phase \"\"\n```\n\nUse typed lifecycle flags, not a hand-written JSON payload. A heartbeat proves\nliveness, never completion.\n\n## Ask and resume\n\nUse Orca `ask` whenever the coordinator must answer. Never open a local question\nTUI the coordinator cannot answer.\n\n```text\nORCA orchestration ask --from --question \"\" --options \",\" --timeout-ms 600000\n\nORCA orchestration ask --from --resume --timeout-ms 600000\n```\n\nEach wait defaults to 600000 ms (10 minutes). `--timeout-ms` accepts a positive\ninteger up to 1800000 ms (30 minutes); larger values are rejected before creating\nor resuming a question. For decisions that take longer, resume the same question\nafter each timeout.\n\nA timeout or disconnect leaves the original question pending. Resume its\nmessage ID; do not create a duplicate question.\n\n## Reading coordinator follow-ups\n\nThe coordinator steers a running worker with `send --to dispatch:`. That\nenqueue is durable but does not interrupt you, so nothing arrives unless you\nlook:\n\n```text\nORCA orchestration check --terminal --json\n```\n\nRun it at each natural checkpoint — before starting a new file, after a test\nrun — and once more immediately before `worker_done`, so a redirect or a\ncancellation lands before the Task settles. `check` names its caller with\n`--terminal`, never `--from`. Stop checking after `worker_done`.\n\nIf `check` returns `consumer_fenced`, this process no longer owns its Dispatch:\nthe Attempt was re-attached to another worker or settled without you. Stop, do\nnot send `worker_done`, and do not retry the check. An empty `check` never means\nyou were replaced; `consumer_fenced` is the only way you learn that.\nIf `send` or `ask` returns `consumer_fenced`, the command ran from another\nparty's terminal (a coordinator or another worker); run it from your own terminal.\n\n## Helper workers\n\nWhen the preamble has a SUB-DISPATCH section, start helpers on your own\nDispatch's Run. Do not `run-create` or `run-use` for them.\n\n```text\nORCA orchestration worker-start --from --spec \"\" --worktree current --agent --json\nORCA orchestration reply --from --id --body \"\"\n```\n\nTheir questions, escalations, and `worker_done` arrive in your own `check`, next\nto your coordinator's mail. Answer their questions with `reply`, and wait for\nevery helper's `worker_done` before sending yours. After you settle, a helper\nstill running reports to your coordinator's Run.\n\n## Escalation\n\nEscalate only before completion and only when the coordinator must intervene:\n\n```text\nORCA orchestration send --from --type escalation --subject \"Blocked: \" --body \"
\" --task-id --dispatch-id \n```\n\n## Completion\n\nSend exactly one terminal report. `--body` is three sentences: what changed,\nwhat was found, and what remains. Use `--outcome failed` when the requested work\nis not complete; never hide failure in prose or silently exit.\n\nAppend `--files-modified` or `--report-path` only when applicable, using actual\npaths. Do not send documentation placeholders as metadata.\n\n```text\nORCA orchestration send --from --type worker_done --subject \"\" --body \"\" --task-id --dispatch-id --outcome succeeded\n```\n\nAfter `worker_done`, end the dispatched turn and idle. Do not poll, close your\nown terminal, or begin unrelated work. A later direct user instruction is new\nuser-owned work and must not reuse settled lifecycle IDs; a supervised follow-up\narrives with a fresh preamble and Task block.\n" +const ORCHESTRATION_WORKER_CONTRACT_REFERENCE_MARKDOWN = "# Worker contract\n\nThe injected preamble is authoritative. Copy its command rather than\nreconstructing flags. In particular, preserve the exact executable, worker\nhandle, Task ID, and Dispatch ID, and keep any other flag it carries (an older\nOrca host adds `--dispatch-capability`).\n\n## Heartbeat\n\nSend heartbeats only at the cadence required by the live preamble. Skip them\nwhile blocked inside `ask` or `check --wait`; those calls are liveness signals.\n\n```text\nORCA orchestration send --from --type heartbeat --subject \"alive\" --task-id --dispatch-id --phase \"\"\n```\n\nUse typed lifecycle flags, not a hand-written JSON payload. A heartbeat proves\nliveness, never completion.\n\n## Ask and resume\n\nUse Orca `ask` whenever the coordinator must answer. Never open a local question\nTUI the coordinator cannot answer.\n\n```text\nORCA orchestration ask --from --question \"\" --options \",\" --timeout-ms 600000\n\nORCA orchestration ask --from --resume --timeout-ms 600000\n```\n\nEach wait defaults to 600000 ms (10 minutes). `--timeout-ms` accepts a positive\ninteger up to 1800000 ms (30 minutes); larger values are rejected before creating\nor resuming a question. For decisions that take longer, resume the same question\nafter each timeout.\n\nA timeout or disconnect leaves the original question pending. Resume its\nmessage ID; do not create a duplicate question.\n\n## Reading coordinator follow-ups\n\nThe coordinator steers a running worker with `send --to dispatch:`. That\nenqueue is durable but does not interrupt you, so nothing arrives unless you\nlook:\n\n```text\nORCA orchestration check --terminal --json\n```\n\nRun it at each natural checkpoint — before starting a new file, after a test\nrun — and once more immediately before `worker_done`, so a redirect or a\ncancellation lands before the Task settles. `check` names its caller with\n`--terminal`, never `--from`. Stop checking after `worker_done`.\n\nDo not use `run-use` to check your Dispatch's mail or bind the Run you work for.\nYou may resume a separate Run when your coordinator directs a handoff, but you\ncannot use `--takeover-legacy`. Plain `check` returns your Dispatch's mail\nunless you are coordinating a Run; report to your coordinator with `send`.\n\nIf a plain `check` (no `--run`) returns `consumer_fenced`, this process no longer owns its Dispatch:\nthe Attempt was re-attached to another worker or settled without you. Stop, do\nnot send `worker_done`, and do not retry the check. An empty `check` never means\nyou were replaced; `consumer_fenced` is the only way you learn that.\nIf `send` or `ask` returns `consumer_fenced`, the command ran from another\nparty's terminal (a coordinator or another worker); run it from your own terminal.\n\n## Helper workers\n\nWhen the preamble has a SUB-DISPATCH section, start helpers on your own\nDispatch's Run. Do not `run-create` or `run-use` for them.\n\n```text\nORCA orchestration worker-start --from --spec \"\" --worktree current --agent --json\nORCA orchestration reply --from --id --body \"\"\n```\n\nTheir questions, escalations, and `worker_done` arrive in your own `check`, next\nto your coordinator's mail. Answer their questions with `reply`, and wait for\nevery helper's `worker_done` before sending yours. After you settle, a helper\nstill running reports to your coordinator's Run.\n\n## Escalation\n\nEscalate only before completion and only when the coordinator must intervene:\n\n```text\nORCA orchestration send --from --type escalation --subject \"Blocked: \" --body \"
\" --task-id --dispatch-id \n```\n\n## Completion\n\nSend exactly one terminal report. `--body` is three sentences: what changed,\nwhat was found, and what remains. Use `--outcome failed` when the requested work\nis not complete; never hide failure in prose or silently exit.\n\nAppend `--files-modified` or `--report-path` only when applicable, using actual\npaths. Do not send documentation placeholders as metadata.\n\n```text\nORCA orchestration send --from --type worker_done --subject \"\" --body \"\" --task-id --dispatch-id --outcome succeeded\n```\n\nAfter `worker_done`, end the dispatched turn and idle. Do not poll, close your\nown terminal, or begin unrelated work. A later direct user instruction is new\nuser-owned work and must not reuse settled lifecycle IDs; a supervised follow-up\narrives with a fresh preamble and Task block.\n" // oxfmt-ignore export const BUNDLED_SKILL_GUIDES = [ diff --git a/src/cli/root-help-text-primary.ts b/src/cli/root-help-text-primary.ts index 8a15f9491e0..ca7fe9dfb3b 100644 --- a/src/cli/root-help-text-primary.ts +++ b/src/cli/root-help-text-primary.ts @@ -109,7 +109,7 @@ export const ROOT_HELP_TEXT_PRIMARY = [ '', 'Orchestration:', ' orchestration run-create Create and bind a lightweight orchestration Run', - ' orchestration run-use Bind this coordinator to an existing Run', + ' orchestration run-use Resume coordinating a Run (workers never use this)', " orchestration run-current Show this coordinator's bound Run", ' orchestration run-list List lightweight orchestration Runs', ' orchestration run-show Show one lightweight orchestration Run', diff --git a/src/cli/specs/orchestration.ts b/src/cli/specs/orchestration.ts index 080e460d136..c4b9b515066 100644 --- a/src/cli/specs/orchestration.ts +++ b/src/cli/specs/orchestration.ts @@ -16,13 +16,13 @@ export const ORCHESTRATION_COMMAND_SPECS: CommandSpec[] = [ }, { path: ['orchestration', 'run-use'], - summary: 'Bind this coordinator to an existing Run', + summary: 'Resume coordinating a Run', usage: 'orca orchestration run-use --id [--from ] [--takeover-legacy] [--retry-request ] [--json]', allowedFlags: [...GLOBAL_FLAGS, 'id', 'from', 'takeover-legacy', 'retry-request'], identityFlagRoles: { from: 'caller' }, notes: [ - '--takeover-legacy must run in the live coordinator agent terminal it binds; it preserves existing worker assignments.' + 'A worker cannot bind its own or ancestor Run or use --takeover-legacy. --takeover-legacy must run in the live coordinator agent terminal it binds; it preserves existing worker assignments.' ] }, { diff --git a/src/main/runtime/orchestration-cli-restart-handoff.test.ts b/src/main/runtime/orchestration-cli-restart-handoff.test.ts new file mode 100644 index 00000000000..2a5244dc8c2 --- /dev/null +++ b/src/main/runtime/orchestration-cli-restart-handoff.test.ts @@ -0,0 +1,279 @@ +import { spawn } from 'node:child_process' +import { mkdtempSync, rmSync } from 'node:fs' +import { createRequire } from 'node:module' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { pathToFileURL } from 'node:url' +import { afterEach, expect, it, vi } from 'vitest' +import { z } from 'zod' +import { OrchestrationDb } from './orchestration/db' + +const cliPath = join(process.cwd(), 'src/cli/index.ts') +const tsxLoader = pathToFileURL(createRequire(import.meta.url).resolve('tsx')).href + +const entity = z.object({ id: z.string() }) +const check = z.object({ + result: z.object({ + runId: z.string(), + deliveryId: z.string(), + messages: z.array(z.object({ id: z.string() }).passthrough()) + }) +}) + +function idOf(response: unknown, key: string): string { + const { result } = z.object({ result: z.record(z.string(), z.unknown()) }).parse(response) + return entity.parse(result[key]).id +} + +afterEach(() => { + vi.unstubAllEnvs() + vi.restoreAllMocks() +}) + +// Sequential source-CLI subprocesses and a disk-backed runtime restart exceed the default test budget. +it("retains CLI work and mail across restart, refuses a worker's own Run, and completes a separate Run handoff", async () => { + const home = mkdtempSync(join(tmpdir(), 'orca-cli-restart-handoff-')) + for (const name of [ + 'HOME', + 'USERPROFILE', + 'APPDATA', + 'LOCALAPPDATA', + 'XDG_CONFIG_HOME', + 'XDG_CACHE_HOME', + 'XDG_DATA_HOME', + 'XDG_STATE_HOME', + 'XDG_RUNTIME_DIR', + 'CLAUDE_CONFIG_DIR', + 'CODEX_HOME', + 'ORCA_E2E_HOME_DIR', + 'ORCA_E2E_USER_DATA_DIR', + 'ORCA_USER_DATA_PATH' + ]) { + vi.stubEnv(name, home) + } + vi.stubEnv('ORCA_BACKGROUND_LAUNCH', '1') + vi.stubEnv('ORCA_DISABLE_MACOS_LOGIN_SHELL', '1') + for (const name of [ + 'ORCA_ENVIRONMENT', + 'ORCA_PAIRING_CODE', + 'ORCA_REMOTE_PAIRING', + 'ORCA_AGENT_SESSION_ID', + 'ORCA_RUNTIME_SOURCE_ID', + 'ORCA_RUNTIME_SOURCE_INCARNATION', + 'ORCA_RUNTIME_SOURCE_PROFILE_PATH' + ]) { + vi.stubEnv(name, '') + } + const { OrcaRuntimeRpcServer } = await import('./runtime-rpc') + const fixture = await import('./orchestration-mailbox-notification-test-harness') + const { registerBoundPty } = await import('./bind-first-pane.test-fixture') + const coord = fixture.TERMINAL_HANDLE + const worker = fixture.SECOND_TERMINAL_HANDLE + const predecessor = 'term_predecessor' + const predecessorTab = '55555555-5555-4555-8555-555555555555' + const predecessorLeaf = '66666666-6666-4666-8666-666666666666' + let db = new OrchestrationDb(join(home, 'orchestration.db')) + + function runtimeForDatabase() { + const { runtime } = fixture.createRuntime(db) + fixture.registerSecondPane(runtime) + registerBoundPty(runtime, 'pty-predecessor', fixture.WORKTREE_ID, null, { + tabId: predecessorTab, + leafId: predecessorLeaf, + incarnationId: 'predecessor-incarnation' + }) + runtime.registerPreAllocatedHandleForPty('pty-predecessor', predecessor) + vi.spyOn(runtime, 'getNestedWorkerMaxDepth').mockReturnValue(2) + return runtime + } + let runtime = runtimeForDatabase() + let server = new OrcaRuntimeRpcServer({ runtime, userDataPath: home }) + async function cli(handle: string, args: string[], exitCode = 0): Promise { + const child = spawn( + process.execPath, + [ + '--import', + tsxLoader, + '--conditions=orca-source', + cliPath, + 'orchestration', + ...args, + '--json' + ], + { + cwd: home, + env: { + ...process.env, + ORCA_TERMINAL_HANDLE: handle, + ORCA_PANE_KEY: runtime.getTerminalPaneKey(handle) ?? '', + ORCA_AGENT_LAUNCH_TOKEN: + handle === coord + ? fixture.LAUNCH_TOKEN + : handle === worker + ? fixture.SECOND_LAUNCH_TOKEN + : '' + }, + stdio: ['ignore', 'pipe', 'pipe'], + windowsHide: true, + timeout: 15_000 + } + ) + let stdout = '' + let stderr = '' + child.stdout.setEncoding('utf8').on('data', (chunk: string) => { + stdout += chunk + }) + child.stderr.setEncoding('utf8').on('data', (chunk: string) => { + stderr += chunk + }) + const code = await new Promise((resolveExit, rejectExit) => { + child.once('close', resolveExit) + child.once('error', rejectExit) + }) + expect(code, JSON.stringify({ args, stdout, stderr })).toBe(exitCode) + const response: unknown = JSON.parse(stdout) + expect(response).toMatchObject({ ok: exitCode === 0 }) + return response + } + + try { + await server.start() + const root = idOf( + await cli(coord, ['run-create', '--objective', 'Retained parent work']), + 'run' + ) + const task = idOf(await cli(coord, ['task-create', '--spec', 'Finish after restart']), 'task') + const dispatch = idOf( + await cli(coord, ['dispatch', '--task', task, '--to', worker]), + 'dispatch' + ) + const separate = idOf( + await cli(predecessor, ['run-create', '--objective', 'Predecessor work']), + 'run' + ) + const continuation = idOf( + await cli(predecessor, ['task-create', '--spec', 'Continue predecessor work']), + 'task' + ) + const retainedMessage = idOf( + await cli(worker, ['send', '--subject', 'Retain this report']), + 'message' + ) + const retainedBatch = check.parse(await cli(coord, ['check'])).result + expect(retainedBatch).toMatchObject({ runId: root, messages: [{ id: retainedMessage }] }) + + await server.stop() + db.close() + db = new OrchestrationDb(join(home, 'orchestration.db')) + runtime = runtimeForDatabase() + server = new OrcaRuntimeRpcServer({ runtime, userDataPath: home }) + await server.start() + expect(await cli(coord, ['run-current'])).toMatchObject({ result: { run: { id: root } } }) + expect(await cli(coord, ['task-list'])).toMatchObject({ + result: { tasks: [{ id: task, status: 'dispatched', dispatch_id: dispatch }] } + }) + expect(check.parse(await cli(coord, ['check'])).result).toEqual(retainedBatch) + + const bindingBefore = db.getRunRaw(root) + expect(await cli(worker, ['run-use', '--id', root], 1)).toMatchObject({ + error: { code: 'worker_cannot_coordinate_own_run', data: { effectsApplied: false } } + }) + expect(db.getRunRaw(root)).toEqual(bindingBefore) + expect(check.parse(await cli(coord, ['check'])).result).toEqual(retainedBatch) + expect(await cli(coord, ['check', '--ack', retainedBatch.deliveryId])).toMatchObject({ + result: { acknowledged: retainedBatch.deliveryId, count: 0 } + }) + expect(await cli(worker, ['run-use', '--id', separate])).toMatchObject({ + result: { run: { id: separate } } + }) + + const question = idOf( + await cli(worker, [ + 'send', + '--to', + `run:${root}`, + '--type', + 'question', + '--subject', + 'Proceed with predecessor work?' + ]), + 'message' + ) + const questionBatch = check.parse(await cli(coord, ['check'])).result + expect(questionBatch).toMatchObject({ + runId: root, + messages: [{ id: question, type: 'question' }] + }) + const reply = idOf( + await cli(coord, ['reply', '--id', question, '--body', 'Proceed with the retained task']), + 'message' + ) + const replyBatch = check.parse(await cli(worker, ['check'])).result + expect(replyBatch).toMatchObject({ + runId: separate, + messages: [ + { + id: reply, + thread_id: question, + to_handle: `run:${separate}`, + run_id: separate, + body: 'Proceed with the retained task' + } + ] + }) + expect(db.getMessageById(reply)?.read).toBe(0) + expect(await cli(worker, ['check', '--ack', replyBatch.deliveryId])).toMatchObject({ + result: { runId: separate, acknowledged: replyBatch.deliveryId, count: 0 } + }) + expect(db.getMessageById(reply)?.read).toBe(1) + await cli(coord, ['check', '--ack', questionBatch.deliveryId]) + + const continuedDispatch = idOf( + await cli(worker, ['dispatch', '--task', continuation, '--to', predecessor]), + 'dispatch' + ) + expect( + await cli(predecessor, [ + 'send', + '--type', + 'worker_done', + '--subject', + 'Predecessor work complete', + '--payload', + JSON.stringify({ + taskId: continuation, + dispatchId: continuedDispatch, + outcome: 'succeeded' + }) + ]) + ).toMatchObject({ result: { lifecycle: { action: 'completed' } } }) + expect(await cli(worker, ['task-list'])).toMatchObject({ + result: { tasks: [{ id: continuation, status: 'completed' }] } + }) + expect(await cli(worker, ['dispatch-show', '--task', continuation])).toMatchObject({ + result: { dispatch: { id: continuedDispatch, status: 'completed' } } + }) + + expect( + await cli(worker, [ + 'send', + '--type', + 'worker_done', + '--subject', + 'Parent work complete', + '--payload', + JSON.stringify({ taskId: task, dispatchId: dispatch, outcome: 'succeeded' }) + ]) + ).toMatchObject({ result: { lifecycle: { action: 'completed' } } }) + expect(await cli(coord, ['task-list'])).toMatchObject({ + result: { tasks: [{ id: task, status: 'completed' }] } + }) + expect(await cli(coord, ['dispatch-show', '--task', task])).toMatchObject({ + result: { dispatch: { id: dispatch, status: 'completed' } } + }) + } finally { + await server.stop() + db.close() + rmSync(home, { recursive: true, force: true }) + } +}, 120_000) diff --git a/src/main/runtime/orchestration-mailbox-notification-consistency.test.ts b/src/main/runtime/orchestration-mailbox-notification-consistency.test.ts index 804798d17fa..6556bdaa4bb 100644 --- a/src/main/runtime/orchestration-mailbox-notification-consistency.test.ts +++ b/src/main/runtime/orchestration-mailbox-notification-consistency.test.ts @@ -281,7 +281,8 @@ describe('orchestration notification mailbox consistency', () => { .map(([sql]) => sql) .find((sql) => sql.includes('FROM run_coordinator_handles AS coordinator')) expect(routingSql).toContain('INDEXED BY idx_messages_undelivered_direct_run') - expect(routingSql).not.toContain('NOT EXISTS') + // An anti-join per coordinator row is fine; one per candidate message would scan the backlog. + expect(routingSql).not.toMatch(/NOT EXISTS \([^)]*candidate\./) expect(db.getMessageById(owned.id)?.to_handle).toBe(`run:${ownedRun.id}`) expect(db.getUnreadDirectMessageTypes(detachedHandle)).toEqual(['status']) db.close() diff --git a/src/main/runtime/orchestration/db/dispatch-depth.ts b/src/main/runtime/orchestration/db/dispatch-depth.ts index 76725593edd..3891b9d857f 100644 --- a/src/main/runtime/orchestration/db/dispatch-depth.ts +++ b/src/main/runtime/orchestration/db/dispatch-depth.ts @@ -54,7 +54,7 @@ export function recordedCreatorIdentity(creator: DispatchCreator): { } /** Self-dispatch is bookkeeping; pre-v37 rows lack creator identity and conservatively count as parents. */ -function isSelfCreatedDispatch(row: DispatchContextRow): boolean { +export function isSelfCreatedDispatch(row: DispatchContextRow): boolean { if (row.creator_pane_key && row.assignee_pane_key) { return isEquivalentPaneKey(row.creator_pane_key, row.assignee_pane_key) } diff --git a/src/main/runtime/orchestration/db/messages/foreign-direct-mailbox-routing.ts b/src/main/runtime/orchestration/db/messages/foreign-direct-mailbox-routing.ts index f8ebfb4762c..2414381a113 100644 --- a/src/main/runtime/orchestration/db/messages/foreign-direct-mailbox-routing.ts +++ b/src/main/runtime/orchestration/db/messages/foreign-direct-mailbox-routing.ts @@ -3,6 +3,7 @@ import type { DispatchContextRow, MessageType } from '../../types' import { DISPATCH_PANE_KEY_MATCH_SUFFIX_SQL, paneKeyMatchSuffix } from '../pane-key-match' import type { OrchestrationDb } from '../orchestration-db' import { DISPATCH_CONTEXT_COLUMN_LIST } from '../row-column-lists' +import { workerSeatGrabSql } from '../runs/run-coordinator-mail-routing' import { ORCHESTRATION_DELIVERY_BATCH_LIMIT, type ForeignDirectMailboxRoutingPage @@ -58,6 +59,7 @@ export function routeForeignDirectMessagesToOwnedMailboxes( ON candidate.run_id = coordinator.run_id AND candidate.to_handle = coordinator.terminal_handle WHERE coordinator.terminal_handle = ?${runExclusion} + AND NOT ${workerSeatGrabSql('coordinator')} AND candidate.read = 0 AND candidate.delivered_at IS NULL AND candidate.delivery_contract = 'current_delivery'`, `SELECT candidate.id, candidate.run_id, candidate.type, candidate.sequence diff --git a/src/main/runtime/orchestration/db/runs/run-binding.ts b/src/main/runtime/orchestration/db/runs/run-binding.ts index dcacfb1f0e2..1f9809ae78f 100644 --- a/src/main/runtime/orchestration/db/runs/run-binding.ts +++ b/src/main/runtime/orchestration/db/runs/run-binding.ts @@ -4,6 +4,7 @@ import { LEGACY_CONTRACT_VERSION } from '../contract-constants' import { isEquivalentPaneKey } from '../pane-key-match' import type { OrchestrationDb } from '../orchestration-db' import type { OrcaSessionId } from '../../../../../shared/orca-session-address' +import { findCallerWorkerDispatches, workerRunBindingRefusal } from './worker-run-binding' import { mailboxAddressOf, runBoundToCoordinator, @@ -41,6 +42,17 @@ export function bindRun( return undefined } const sameBinding = runBoundToCoordinator(run, coordinator) + const explicitTakeover = params.takeoverLegacy === true + // Why: before the legacy gate, so a worker never sees its --takeover-legacy recovery command. + const workerRefusal = sameBinding + ? null + : workerRunBindingRefusal(this, coordinator, params.runId, { + takeover: explicitTakeover, + bind: true + }) + if (workerRefusal) { + throw workerRefusal + } const adoption = this.getLegacyAdoption() const adoptedRun = adoption?.adopted_run_id === params.runId const legacyAuthority = params.legacyCoordinatorAuthority @@ -111,6 +123,13 @@ export function bindRun( } // Only live legacy work requires takeover attestation; settled work has no competing authority. if (activeLegacyAssignment && !sameBinding && !provenLegacyBinding && !params.takeoverLegacy) { + if (findCallerWorkerDispatches(this, coordinator).length > 0) { + throw new OrchestrationError( + 'consumer_fenced', + `Run ${params.runId} still has active legacy work and this caller cannot bind it. Nothing changed.`, + { effectsApplied: false } + ) + } throw new OrchestrationError( 'consumer_fenced', 'This adopted Run still has live legacy work. Its attested coordinator may rebind it, or a current coordinator may explicitly use run-use --takeover-legacy.', diff --git a/src/main/runtime/orchestration/db/runs/run-coordinator-mail-routing.ts b/src/main/runtime/orchestration/db/runs/run-coordinator-mail-routing.ts index b71639acc24..d38cfe5fba9 100644 --- a/src/main/runtime/orchestration/db/runs/run-coordinator-mail-routing.ts +++ b/src/main/runtime/orchestration/db/runs/run-coordinator-mail-routing.ts @@ -1,17 +1,48 @@ import type Database from '../../../../sqlite/sync-database' import type { OrchestrationDb } from '../orchestration-db' import { currentRunCoordinatorSessionAddressSql } from './run-coordinator-orca-session' +import { orcaSessionAddressSql } from '../orca-session-address-sql' /** Mail to an active Dispatch assignee's address in the same Run is that worker's, not coordinator mail. */ export function activeDispatchOwnsAddressSql(runIdSql: string, addressSql: string): string { return `EXISTS ( SELECT 1 FROM dispatch_contexts WHERE dispatch_contexts.run_id = ${runIdSql} - AND dispatch_contexts.assignee_handle = ${addressSql} + AND (dispatch_contexts.assignee_handle = ${addressSql} + OR ${orcaSessionAddressSql('dispatch_contexts.assignee_orca_session_id')} = ${addressSql}) AND dispatch_contexts.status IN ('pending', 'dispatched') )` } +/** + * A remembered address first bound while it worked an unsettled, delegated Dispatch in the same Run + * was a worker grabbing the seat, not a spelling of the coordinator, so it never routes as one. + * Derived from the rows, so addresses older builds remembered stop mattering without editing them. + * julianday() because completed_at is stored in two text formats; whole seconds and a strict > + * because first_bound_at has no milliseconds, so a bind in the second a Dispatch settled is not a grab. + */ +export function workerSeatGrabSql(aliasRow: string): string { + return `EXISTS ( + SELECT 1 FROM dispatch_contexts AS grab + WHERE grab.run_id = ${aliasRow}.run_id + AND (grab.assignee_handle = ${aliasRow}.terminal_handle + OR ${orcaSessionAddressSql('grab.assignee_orca_session_id')} = ${aliasRow}.terminal_handle) + AND NOT (grab.creator_handle IS NOT NULL AND grab.creator_handle = grab.assignee_handle) + AND julianday(grab.created_at) <= julianday(${aliasRow}.first_bound_at) + AND (grab.status IN ('pending', 'dispatched') + OR julianday(datetime(grab.completed_at)) > julianday(${aliasRow}.first_bound_at)) + )` +} + +/** Whether `addressSql` is a coordinator address of the Run `runIdSql`, seat grabs excluded. */ +function coordinatorAliasSql(runIdSql: string, addressSql: string): string { + return `EXISTS ( + SELECT 1 FROM run_coordinator_handles AS alias + WHERE alias.run_id = ${runIdSql} AND alias.terminal_handle = ${addressSql} + AND NOT ${workerSeatGrabSql('alias')} + )` +} + export function rememberRunCoordinatorHandle( this: OrchestrationDb, runId: string, @@ -87,10 +118,7 @@ export function createCoordinatorMailRoutingTrigger(this: OrchestrationDb): void SELECT 1 FROM runs WHERE runs.id = NEW.run_id AND runs.legacy = 0 ) - AND EXISTS ( - SELECT 1 FROM run_coordinator_handles - WHERE run_id = NEW.run_id AND terminal_handle = NEW.to_handle - ) + AND ${coordinatorAliasSql('NEW.run_id', 'NEW.to_handle')} AND NOT ${activeDispatchOwnsAddressSql('NEW.run_id', 'NEW.to_handle')} BEGIN UPDATE messages SET to_handle = 'run:' || NEW.run_id WHERE sequence = NEW.sequence; @@ -113,7 +141,12 @@ export function routeAllUnreadDirectMessagesToRunMailbox( `UPDATE messages SET to_handle = ? WHERE run_id = ? AND to_handle = ? AND read = 0 AND delivery_contract = 'current_delivery' - AND NOT ${activeDispatchOwnsAddressSql('messages.run_id', 'messages.to_handle')}` + AND NOT ${activeDispatchOwnsAddressSql('messages.run_id', 'messages.to_handle')} + AND NOT EXISTS ( + SELECT 1 FROM run_coordinator_handles AS alias + WHERE alias.run_id = messages.run_id AND alias.terminal_handle = messages.to_handle + AND ${workerSeatGrabSql('alias')} + )` ) .run(`run:${runId}`, runId, directHandle) } diff --git a/src/main/runtime/orchestration/db/runs/run-delivery.ts b/src/main/runtime/orchestration/db/runs/run-delivery.ts index f373031bfae..2afac2e4285 100644 --- a/src/main/runtime/orchestration/db/runs/run-delivery.ts +++ b/src/main/runtime/orchestration/db/runs/run-delivery.ts @@ -12,7 +12,7 @@ export function requireCurrentConsumer( if (!run || run.legacy === 1 || run.consumer_generation !== consumerGeneration) { throw new OrchestrationError( 'consumer_fenced', - 'This mailbox consumer has been replaced. Rebind with orchestration run-use.' + `Another session now coordinates Run ${runId}; stop reading this mailbox.` ) } return run diff --git a/src/main/runtime/orchestration/db/runs/run-lookup.ts b/src/main/runtime/orchestration/db/runs/run-lookup.ts index 5f446de18ac..1371430a67e 100644 --- a/src/main/runtime/orchestration/db/runs/run-lookup.ts +++ b/src/main/runtime/orchestration/db/runs/run-lookup.ts @@ -16,6 +16,7 @@ import { encodeRunListCursor, decodeRunListCursor } from '../run-list-cursor' import type { RunListPage } from '../run-list-page' import type { OrchestrationDb } from '../orchestration-db' import { RUN_COLUMN_LIST } from '../row-column-lists' +import { workerSeatGrabSql } from './run-coordinator-mail-routing' export type LegacyAdoptedMailboxOwner = { runId: string @@ -57,17 +58,20 @@ export function getRunMailboxOwnerIdsForHandle( terminalHandle: string, legacyAdoptedMailboxOwner?: LegacyAdoptedMailboxOwner | null ): string[] { - const runIds = ( - this.db - .prepare( - `SELECT coordinator.run_id - FROM run_coordinator_handles AS coordinator - JOIN runs ON runs.id = coordinator.run_id AND runs.legacy = 0 - WHERE coordinator.terminal_handle = ? - ORDER BY coordinator.run_id` - ) - .all(terminalHandle) as { run_id: string }[] - ).map((row) => row.run_id) + const runIds = this.db + .prepare( + `SELECT coordinator.run_id + FROM run_coordinator_handles AS coordinator + JOIN runs ON runs.id = coordinator.run_id AND runs.legacy = 0 + WHERE coordinator.terminal_handle = ? AND NOT ${workerSeatGrabSql('coordinator')} + ORDER BY coordinator.run_id` + ) + .all(terminalHandle) + .flatMap((row) => + typeof row === 'object' && row !== null && 'run_id' in row && typeof row.run_id === 'string' + ? [row.run_id] + : [] + ) const adoptedOwner = legacyAdoptedMailboxOwner === undefined ? this.getLegacyAdoptedRunMailboxOwner() diff --git a/src/main/runtime/orchestration/db/runs/worker-run-binding.ts b/src/main/runtime/orchestration/db/runs/worker-run-binding.ts new file mode 100644 index 00000000000..cbe9beec731 --- /dev/null +++ b/src/main/runtime/orchestration/db/runs/worker-run-binding.ts @@ -0,0 +1,78 @@ +import type { DispatchContextRow } from '../../types' +import { OrchestrationError } from '../../orchestration-error' +import { isSelfCreatedDispatch } from '../dispatch-depth' +import type { OrchestrationDb } from '../orchestration-db' +import { + mailboxAddressOf, + type OrchestrationCoordinatorKey +} from '../../orchestration-caller-identity' + +/** The caller's active delegated Dispatches, matched by address then pane as check and send do. */ +export function findCallerWorkerDispatches( + db: OrchestrationDb, + caller: OrchestrationCoordinatorKey +): DispatchContextRow[] { + const address = mailboxAddressOf(caller) + if (address === null) { + return [] + } + return db + .getActiveDispatchMailboxOwners(address, caller.paneKey ?? undefined) + .filter((dispatch) => !isSelfCreatedDispatch(dispatch)) +} + +/** A delegated Dispatch in this Run, or started from work in this Run. */ +function callerDispatchWorkingForRun( + dispatches: readonly DispatchContextRow[], + db: OrchestrationDb, + runId: string +): DispatchContextRow | undefined { + for (const own of dispatches) { + let row: DispatchContextRow | undefined = own + const visited = new Set() + while (row) { + if (row.run_id === runId || visited.has(row.id)) { + return own + } + visited.add(row.id) + row = row.creator_dispatch_id ? db.getDispatchContextById(row.creator_dispatch_id) : undefined + } + } + return undefined +} + +/** A worker cannot bind its own or ancestor Run, or use a takeover flag on another Run. */ +export function workerRunBindingRefusal( + db: OrchestrationDb, + caller: OrchestrationCoordinatorKey, + runId: string, + attempted: { takeover: boolean; bind: boolean } +): OrchestrationError | null { + const dispatches = findCallerWorkerDispatches(db, caller) + const own = callerDispatchWorkingForRun(dispatches, db, runId) + if (own) { + const role = + own.run_id === runId + ? `You work Dispatch ${own.id} in Run ${runId}, so you cannot coordinate that Run.` + : `You work Dispatch ${own.id} in Run ${own.run_id}, which was started from work in Run ${runId}, so you cannot coordinate Run ${runId}.` + return new OrchestrationError( + 'worker_cannot_coordinate_own_run', + `${role} Your mail arrives through plain \`orchestration check\` (no --run); report with \`orchestration send\`. No effects were applied.`, + { effectsApplied: false, dispatchId: own.id } + ) + } + const first = dispatches[0] + if (first && attempted.bind && attempted.takeover) { + return new OrchestrationError( + 'worker_cannot_take_over_run', + 'A caller working an active Dispatch cannot use a takeover flag. No effects were applied.', + { effectsApplied: false, dispatchId: first.id } + ) + } + return null +} + +/** What a worker with no Run binding is told instead of a Run binding command. */ +export function unboundWorkerMessage(dispatch: DispatchContextRow): string { + return `You work Dispatch ${dispatch.id} in Run ${dispatch.run_id} and are not bound to a Run. Your mail arrives through plain \`orchestration check\` (no --run); report with \`orchestration send\`. No effects were applied.` +} diff --git a/src/main/runtime/orchestration/dispatch-creator-identity-migration.test.ts b/src/main/runtime/orchestration/dispatch-creator-identity-migration.test.ts index 768d920c54a..bfe47373457 100644 --- a/src/main/runtime/orchestration/dispatch-creator-identity-migration.test.ts +++ b/src/main/runtime/orchestration/dispatch-creator-identity-migration.test.ts @@ -39,6 +39,8 @@ describe('OrchestrationDb v36 to v37 migration', () => { seed.close() const raw = new Database(dbPath) + // The current mail-routing trigger reads creator_handle; the next open recreates it. + raw.exec('DROP TRIGGER trg_messages_route_coordinator_mail') for (const column of CREATOR_COLUMNS) { raw.exec(`ALTER TABLE dispatch_contexts DROP COLUMN ${column}`) } diff --git a/src/main/runtime/orchestration/orchestration-adopted-run-binding.test.ts b/src/main/runtime/orchestration/orchestration-adopted-run-binding.test.ts index b34cf06057b..af769b9d4b4 100644 --- a/src/main/runtime/orchestration/orchestration-adopted-run-binding.test.ts +++ b/src/main/runtime/orchestration/orchestration-adopted-run-binding.test.ts @@ -124,6 +124,62 @@ describe('adopted Run binding without --takeover-legacy', () => { }) }) + it('withholds legacy takeover advice and refuses explicit worker takeovers without effects', () => { + const { db, adoptedRunId, recoveryMessageId } = createAdoptedFixture({ settleWork: false }) + const home = db.createRun({ + objective: 'worker home', + coordinatorHandle: CURRENT_COORDINATOR_HANDLE, + coordinatorPaneKey: CURRENT_COORDINATOR_PANE + }) + const worker = createRootDispatch( + db, + db.createTask({ runId: home.id, spec: 'worker' }).id, + 'term_foreign_worker', + 'tab_foreign:22222222-2222-4222-8222-222222222222' + ) + const before = { + adoptedRun: db.getRun(adoptedRunId), + homeRun: db.getRun(home.id), + dispatch: db.getDispatchContextById(worker.id), + mail: db.getMessageById(recoveryMessageId) + } + try { + db.bindRun({ + runId: adoptedRunId, + coordinatorHandle: 'term_foreign_worker', + coordinatorPaneKey: 'tab_foreign:22222222-2222-4222-8222-222222222222' + }) + throw new Error('Expected the legacy bind to be fenced') + } catch (error) { + expect(error).toMatchObject({ code: 'consumer_fenced', data: { effectsApplied: false } }) + expect(JSON.stringify(error)).not.toContain('run-use') + } + for (const [runId, code] of [ + [adoptedRunId, 'worker_cannot_take_over_run'], + [home.id, 'worker_cannot_coordinate_own_run'] + ] as const) { + expect(() => + db.bindRun({ + runId, + coordinatorHandle: 'term_foreign_worker', + coordinatorPaneKey: 'tab_foreign:22222222-2222-4222-8222-222222222222', + takeoverLegacy: true + }) + ).toThrowError( + expect.objectContaining({ + code, + data: { effectsApplied: false, dispatchId: worker.id } + }) + ) + expect({ + adoptedRun: db.getRun(adoptedRunId), + homeRun: db.getRun(home.id), + dispatch: db.getDispatchContextById(worker.id), + mail: db.getMessageById(recoveryMessageId) + }).toEqual(before) + } + }) + it('claims the settled adopted Run and inherits its recovered task graph', () => { const { db, adoptedRunId } = createAdoptedFixture({ settleWork: true }) diff --git a/src/main/runtime/orchestration/orchestration-run-delivery-db.test.ts b/src/main/runtime/orchestration/orchestration-run-delivery-db.test.ts index fdaa5e92a80..8969b55e486 100644 --- a/src/main/runtime/orchestration/orchestration-run-delivery-db.test.ts +++ b/src/main/runtime/orchestration/orchestration-run-delivery-db.test.ts @@ -4,6 +4,7 @@ import { join } from 'node:path' import { afterEach, describe, expect, it } from 'vitest' import { LEGACY_RUN_ID, OrchestrationDb } from './db' import { createRootDispatch } from './db/root-dispatch-test-fixture' +import type { DispatchCreator } from './db/dispatch-depth' describe('OrchestrationDb Run state', () => { let db: OrchestrationDb | undefined @@ -17,11 +18,13 @@ describe('OrchestrationDb Run state', () => { return db } + const COORD_PANE = 'tab_coord:11111111-1111-4111-8111-111111111111' + function createBoundRun(d: OrchestrationDb) { return d.createRun({ objective: 'Mailbox test', coordinatorHandle: 'term_coord', - coordinatorPaneKey: 'tab_coord:11111111-1111-4111-8111-111111111111' + coordinatorPaneKey: COORD_PANE }) } @@ -223,6 +226,49 @@ describe('OrchestrationDb Run state', () => { }) describe('lightweight Run scope', () => { + it('refuses a worker binding an ancestor Run, but not a coordinator with a self-dispatch', () => { + const d = createDb() + const parent = createBoundRun(d) + const coordinator = { kind: 'terminal', handle: 'term_coord', paneKey: COORD_PANE } as const + const dispatchIn = (runId: string, handle: string, creator: DispatchCreator) => + d.createDispatchContext({ + taskId: d.createTask({ spec: 'work', runId }).id, + assigneeHandle: handle, + creator, + maxDepth: Number.MAX_SAFE_INTEGER + }) + // A context-only self-dispatch is bookkeeping, so its coordinator keeps its Run. + dispatchIn(parent.id, 'term_coord', coordinator) + expect( + d.bindRun({ + runId: parent.id, + coordinatorHandle: 'term_coord', + coordinatorPaneKey: COORD_PANE + }) + ).toBeDefined() + dispatchIn(parent.id, 'term_worker', coordinator) + const sub = d.createRun({ + objective: 'sub', + coordinatorHandle: 'term_worker', + coordinatorPaneKey: null + }) + dispatchIn(sub.id, 'term_grand', { kind: 'terminal', handle: 'term_worker' }) + + expect(() => + d.bindRun({ runId: parent.id, coordinatorHandle: 'term_grand', coordinatorPaneKey: null }) + ).toThrow(expect.objectContaining({ code: 'worker_cannot_coordinate_own_run' })) + let creatorHandle = 'term_grand' + for (let depth = 0; depth < 33; depth++) { + const handle = `term_deep_${depth}` + dispatchIn(sub.id, handle, { kind: 'terminal', handle: creatorHandle }) + creatorHandle = handle + } + expect(() => + d.bindRun({ runId: parent.id, coordinatorHandle: creatorHandle, coordinatorPaneKey: null }) + ).toThrow(expect.objectContaining({ code: 'worker_cannot_coordinate_own_run' })) + expect(d.getRun(parent.id)?.coordinator_handle).toBe('term_coord') + }) + it('binds creation to one pane and fences that pane when it creates another Run', () => { const d = createDb() const first = d.createRun({ diff --git a/src/main/runtime/orchestration/run-binding-performance.test.ts b/src/main/runtime/orchestration/run-binding-performance.test.ts new file mode 100644 index 00000000000..f816fd402fc --- /dev/null +++ b/src/main/runtime/orchestration/run-binding-performance.test.ts @@ -0,0 +1,265 @@ +import { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { OrchestrationDb } from './db' +import { createRootDispatch } from './db/root-dispatch-test-fixture' +import { formatOrcaSessionAddress } from '../../../shared/orca-session-address' +import { testOrcaSessionId } from '../../../shared/orca-session-address-test-fixture' + +const databases: OrchestrationDb[] = [] +afterEach(() => { + vi.restoreAllMocks() + for (const db of databases.splice(0)) { + db.close() + } +}) + +function openDatabase(): OrchestrationDb { + const db = new OrchestrationDb(':memory:') + databases.push(db) + return db +} + +// Count real SQLite date evaluations, forwarding to SQLite's unchanged implementation on a second connection. +function historyWork(db: OrchestrationDb) { + const adapter: unknown = db.db + if ( + typeof adapter !== 'object' || + adapter === null || + !('db' in adapter) || + !(adapter.db instanceof DatabaseSync) + ) { + throw new Error('Expected node:sqlite database') + } + const native = adapter.db + const oracle = new DatabaseSync(':memory:') + const date = oracle.prepare('SELECT julianday(?) AS value') + let evaluations = 0 + native.function('julianday', { deterministic: true }, (value) => { + evaluations++ + return date.get(value)?.value ?? null + }) + return { + reset: () => { + evaluations = 0 + }, + count: () => evaluations, + close: () => oracle.close() + } +} + +describe('Run binding scaling contracts', () => { + it('walks to the root with one indexed ancestor read per link at N and 10N', () => { + for (const depth of [4, 40]) { + const db = openDatabase() + const root = db.createRun({ + objective: 'root', + coordinatorHandle: 'root', + coordinatorPaneKey: null + }) + const child = db.createRun({ + objective: 'child', + coordinatorHandle: null, + coordinatorPaneKey: null + }) + let parent = createRootDispatch( + db, + db.createTask({ spec: 'root work', runId: root.id }).id, + 'worker_0' + ) + for (let link = 1; link <= depth; link++) { + parent = db.createDispatchContext({ + taskId: db.createTask({ spec: 'child work', runId: child.id }).id, + assigneeHandle: `worker_${link}`, + creator: { kind: 'terminal', handle: `worker_${link - 1}` }, + maxDepth: Number.MAX_SAFE_INTEGER + }) + } + const separate = db.createRun({ + objective: 'handoff', + coordinatorHandle: null, + coordinatorPaneKey: null + }) + const before = db.getRunRaw(root.id) + const reads: string[] = [] + const observed = new WeakSet() + const originalPrepare = db.db.prepare.bind(db.db) + const prepare = vi.spyOn(db.db, 'prepare').mockImplementation((sql) => { + const statement = originalPrepare(sql) + if (/FROM dispatch_contexts WHERE id = \?/.test(sql) && !observed.has(statement)) { + observed.add(statement) + const get = statement.get.bind(statement) + vi.spyOn(statement, 'get').mockImplementation((...params) => { + reads.push(sql) + return get(...params) + }) + } + return statement + }) + const bind = (runId: string) => + db.bindRun({ + runId, + coordinatorHandle: parent.assignee_handle, + coordinatorPaneKey: null + }) + const assertWalk = () => { + expect(reads).toHaveLength(depth) + expect(prepare.mock.calls.length).toBeLessThanOrEqual(depth + 50) + const sql = reads[0] + expect(sql).toBeDefined() + expect(db.db.prepare(`EXPLAIN QUERY PLAN ${sql}`).all('absent')).toContainEqual( + expect.objectContaining({ + detail: expect.stringMatching(/SEARCH dispatch_contexts USING INDEX .*\(id=\?\)/) + }) + ) + } + expect(() => bind(root.id)).toThrow( + expect.objectContaining({ code: 'worker_cannot_coordinate_own_run' }) + ) + assertWalk() + expect(db.getRunRaw(root.id)).toEqual(before) + prepare.mockClear() + reads.length = 0 + expect(bind(separate.id)?.coordinator_handle).toBe(parent.assignee_handle) + assertWalk() + prepare.mockRestore() + } + }) + + it.each([ + [4, 2], + [40, 2], + [4, 20] + ])( + 'bounds four real alias consumers with %i historical Dispatches and %i unread messages per alias', + (history, messages) => { + const db = openDatabase() + const run = db.createRun({ + objective: 'historical aliases', + coordinatorHandle: 'coord', + coordinatorPaneKey: null + }) + const other = db.createRun({ + objective: 'unrelated history', + coordinatorHandle: null, + coordinatorPaneKey: null + }) + const task = db.createTask({ spec: 'retained work', runId: run.id }) + const otherTask = db.createTask({ spec: 'unrelated retained work', runId: other.id }) + const sessionId = testOrcaSessionId('11111111-1111-4111-8111-111111111111') + const aliases = ['old_coord', 'old_worker', formatOrcaSessionAddress(sessionId)] + const insert = db.db.prepare(`INSERT INTO dispatch_contexts + (id, task_id, run_id, assignee_handle, assignee_orca_session_id, creator_handle, status, created_at, completed_at) + VALUES (?, ?, ?, ?, ?, 'coord', 'completed', '2000-01-01', ?)`) + for (const [index, alias] of aliases.entries()) { + db.rememberRunCoordinatorHandle(run.id, alias) + for (let row = 0; row < history; row++) { + insert.run( + `history_${index}_${row}`, + task.id, + run.id, + index === 0 ? alias : 'old_worker', + index === 0 ? null : sessionId, + index > 0 && row === history - 1 ? '2000-01-04' : '2000-01-02' + ) + } + } + for (let row = 0; row < history * 10; row++) { + insert.run(`unrelated_${row}`, otherTask.id, other.id, 'old_coord', null, '2000-01-04') + } + db.db + .prepare('UPDATE run_coordinator_handles SET first_bound_at = ? WHERE run_id = ?') + .run('2000-01-03', run.id) + const work = historyWork(db) + const prepare = vi.spyOn(db.db, 'prepare') + const ids = new Map() + const assertWork = (messageMultiplier: number) => { + // Each alias history is visited, but no unrelated Run or quadratic history join is needed. + expect(work.count()).toBeGreaterThanOrEqual(history) + expect(work.count()).toBeLessThanOrEqual(24 * history * messageMultiplier) + expect(prepare.mock.calls.length).toBeLessThanOrEqual(12 * messageMultiplier + 12) + for (const [sql] of prepare.mock.calls.filter( + ([query]) => + query.includes('FROM dispatch_contexts AS grab') && !query.startsWith('EXPLAIN') + )) { + const plan = db.db + .prepare(`EXPLAIN QUERY PLAN ${sql}`) + .all(...Array(sql.match(/\?/g)?.length ?? 0).fill(null)) + expect(plan).toContainEqual( + expect.objectContaining({ + detail: expect.stringMatching(/SEARCH grab(?: EXISTS)? USING INDEX .*\(run_id=\?\)/) + }) + ) + expect(plan).not.toContainEqual( + expect.objectContaining({ detail: expect.stringMatching(/SCAN grab/) }) + ) + } + work.reset() + prepare.mockClear() + } + const restoreDirect = () => { + for (const [alias, messageIds] of ids) { + for (const id of messageIds) { + db.db.prepare('UPDATE messages SET to_handle = ? WHERE id = ?').run(alias, id) + } + } + work.reset() + prepare.mockClear() + } + try { + for (const alias of aliases) { + expect(db.getRunMailboxOwnerIdsForHandle(alias)).toEqual( + alias === 'old_coord' ? [run.id] : [] + ) + } + assertWork(1) + + // Production INSERT fires the actual routing trigger, including both address spellings. + for (const alias of aliases) { + const messageIds: string[] = [] + for (let message = 0; message < messages; message++) { + const inserted = db.insertMessage({ + from: 'coord', + to: alias, + runId: run.id, + subject: 'direct' + }) + expect(inserted.to_handle).toBe(alias === 'old_coord' ? `run:${run.id}` : alias) + messageIds.push(inserted.id) + } + ids.set(alias, messageIds) + } + assertWork(messages) + + // Restored pre-upgrade direct mail is swept by binding and foreign-mail reconciliation. + restoreDirect() + for (const alias of aliases) { + db.routeAllUnreadDirectMessagesToRunMailbox(run.id, alias) + } + assertWork(messages) + for (const [alias, messageIds] of ids) { + for (const id of messageIds) { + expect(db.getMessageById(id)?.to_handle).toBe( + alias === 'old_coord' ? `run:${run.id}` : alias + ) + } + } + restoreDirect() + for (const alias of aliases) { + expect(db.routeForeignDirectMessagesToOwnedMailboxes(alias).routedCount).toBe( + alias === 'old_coord' ? messages : 0 + ) + } + assertWork(messages) + for (const [alias, messageIds] of ids) { + for (const id of messageIds) { + expect(db.getMessageById(id)?.to_handle).toBe( + alias === 'old_coord' ? `run:${run.id}` : alias + ) + } + } + } finally { + work.close() + } + } + ) +}) diff --git a/src/main/runtime/orchestration/run-coordinator-handle-migration.test.ts b/src/main/runtime/orchestration/run-coordinator-handle-migration.test.ts index f15ad3c316a..6a7318b6b79 100644 --- a/src/main/runtime/orchestration/run-coordinator-handle-migration.test.ts +++ b/src/main/runtime/orchestration/run-coordinator-handle-migration.test.ts @@ -5,6 +5,7 @@ import { afterEach, describe, expect, it } from 'vitest' import Database from '../../sqlite/sync-database' import { OrchestrationDb } from './db' import { SCHEMA_VERSION } from './db/contract-constants' +import { createRootDispatch } from './db/root-dispatch-test-fixture' describe('Run coordinator handle history migration', () => { let db: OrchestrationDb | undefined @@ -165,4 +166,36 @@ describe('Run coordinator handle history migration', () => { expect(db.getMessageById(message.id)?.to_handle).toBe(`run:${run.id}`) }) + + it('stops routing an address that grabbed the seat while it worked for the Run', () => { + db = new OrchestrationDb(':memory:') + const run = db.createRun({ + objective: 'grabbed', + coordinatorHandle: 'term_coord', + coordinatorPaneKey: 'tab:leaf' + }) + const dispatch = createRootDispatch( + db, + db.createTask({ spec: 'w', runId: run.id }).id, + 'term_worker' + ) + // An older build remembered the worker as a coordinator address when it bound the Run. + db.rememberRunCoordinatorHandle(run.id, 'term_worker') + db.db + .prepare( + `UPDATE dispatch_contexts SET status = 'completed', completed_at = datetime('now', '+1 second') WHERE id = ?` + ) + .run(dispatch.id) + + const mail = db.insertMessage({ + runId: run.id, + from: 'term_coord', + to: 'term_worker', + subject: 'direct' + }) + + expect(db.getMessageById(mail.id)?.to_handle).toBe('term_worker') + expect(db.getRunMailboxOwnerIdsForHandle('term_worker')).toEqual([]) + expect(db.routeForeignDirectMessagesToOwnedMailboxes('term_worker').routedCount).toBe(0) + }) }) diff --git a/src/main/runtime/rpc/errors.ts b/src/main/runtime/rpc/errors.ts index b5314657071..edd41214170 100644 --- a/src/main/runtime/rpc/errors.ts +++ b/src/main/runtime/rpc/errors.ts @@ -144,6 +144,8 @@ const STRUCTURED_RUNTIME_PASSTHROUGH_CODES: ReadonlySet = new Set([ 'worker_prompt_too_large', 'terminal_worktree_mismatch', 'terminal_is_coordinator', + 'worker_cannot_coordinate_own_run', + 'worker_cannot_take_over_run', 'request_mismatch', 'mutation_ledger_full', 'legacy_read_only', diff --git a/src/main/runtime/rpc/methods/orchestration/gates/gate-run-authorization.test.ts b/src/main/runtime/rpc/methods/orchestration/gates/gate-run-authorization.test.ts index 84dcf98417f..1a16bcef7fa 100644 --- a/src/main/runtime/rpc/methods/orchestration/gates/gate-run-authorization.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/gates/gate-run-authorization.test.ts @@ -195,7 +195,7 @@ describe('orchestration gate Run authorization', () => { ok: false, error: { code: 'consumer_fenced', - message: `This coordinator terminal is bound to ${runA}, not ${strangerRun.id}.` + message: `This caller is bound to Run ${runA}, not Run ${strangerRun.id}. Nothing changed.` } }) expect(harness.db.getTask(strangerTask.id)?.status).toBe('ready') diff --git a/src/main/runtime/rpc/methods/orchestration/messaging/check-methods.ts b/src/main/runtime/rpc/methods/orchestration/messaging/check-methods.ts index ebbbae40ff6..6d6f5ba1c5b 100644 --- a/src/main/runtime/rpc/methods/orchestration/messaging/check-methods.ts +++ b/src/main/runtime/rpc/methods/orchestration/messaging/check-methods.ts @@ -104,7 +104,7 @@ export const ORCHESTRATION_CHECK_METHODS = [ if (!hasRunBindingKey(caller) && consumingCheck) { throw new OrchestrationError( 'stable_pane_required', - `Terminal ${handle} has no live pane bound to a Run, so this inbox can never receive Run mail. Rebind this terminal with orchestration run-use, or read the Run mailbox with --run .`, + `Terminal ${handle} has no live pane and no active Dispatch, so a consuming check here can never receive mail. Run check from the agent's own live terminal.`, orchestrationSkillRecoveryData() ) } diff --git a/src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-interleavings.test.ts b/src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-interleavings.test.ts index 4e92d7c35c1..d8ad40d65a0 100644 --- a/src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-interleavings.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-interleavings.test.ts @@ -52,17 +52,22 @@ describe('Run binding during Dispatch checks', () => { subject }) } + // A Run this lead created earlier and that lost its binding: the one kind a worker may resume. + function orphanedOwnRun(): string { + const id = state.db.createRun({ + objective: 'resume', + coordinatorHandle: 'term_lead', + coordinatorPaneKey: LEAD + }).id + state.db.db + .prepare( + 'UPDATE runs SET coordinator_handle = NULL, coordinator_pane_key = NULL WHERE id = ?' + ) + .run(id) + return id + } async function bind(method: 'runCreate' | 'runUse') { - const params = - method === 'runCreate' - ? { objective: 'child' } - : { - id: state.db.createRun({ - objective: 'adopt', - coordinatorHandle: 'term_other', - coordinatorPaneKey: OTHER - }).id - } + const params = method === 'runCreate' ? { objective: 'child' } : { id: orphanedOwnRun() } await h.call(`orchestration.${method}`, { from: 'term_lead', ...params }, state.ctx) const run = state.db.getCurrentRunForPane(LEAD) if (!run) { diff --git a/src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-safety.test.ts b/src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-safety.test.ts index 91ae66ce07f..d665b751ad5 100644 --- a/src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-safety.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-safety.test.ts @@ -503,7 +503,9 @@ describe('Run-bound lead mailbox boundaries', () => { runId: leadRun.id, messages: [{ subject: 'Run report' }] }) - await expect(check({ run: dispatch.run_id })).rejects.toMatchObject({ code: 'consumer_fenced' }) + await expect(check({ run: dispatch.run_id })).rejects.toMatchObject({ + code: 'worker_cannot_coordinate_own_run' + }) expect(state.db.getMessageById(pending.id)?.read).toBe(0) }) diff --git a/src/main/runtime/rpc/methods/orchestration/runs/run-scope.ts b/src/main/runtime/rpc/methods/orchestration/runs/run-scope.ts index f8dddc0d2ac..6fc3e492e67 100644 --- a/src/main/runtime/rpc/methods/orchestration/runs/run-scope.ts +++ b/src/main/runtime/rpc/methods/orchestration/runs/run-scope.ts @@ -8,10 +8,18 @@ import type { } from '../../../../orca-runtime' import { hasRunBindingKey, + mailboxAddressOf, + runCoordinatorKey, type OrchestrationCallerIdentity, type OrchestrationSessionCaller } from '../../../../orchestration/orchestration-caller-identity' import { resolveDeclaredCallerParty } from '../../../../orchestration/orchestration-party' +import { + findCallerWorkerDispatches, + unboundWorkerMessage, + workerRunBindingRefusal +} from '../../../../orchestration/db/runs/worker-run-binding' +import type { OrchestrationDb } from '../../../../orchestration/db' export type RunScopeParams = { runId?: string @@ -109,6 +117,33 @@ export function resolveOrchestrationCaller( return caller } +const COORDINATOR_RUN_REQUIRED = + 'No Run is bound. A coordinator creates one with `orchestration run-create`, or resumes its own with `orchestration run-use --id `.' + +// Why: a worker is told where its own mail is, never to bind a Run. +function unboundCallerError( + db: OrchestrationDb, + caller: OrchestrationCallerIdentity, + explicit: RunRow | undefined +): OrchestrationError { + if (explicit) { + // Why: consumer_fenced, since a reader that lost this Run mid-check must stop, worker or not. + const holder = mailboxAddressOf(runCoordinatorKey(explicit)) + return new OrchestrationError( + 'consumer_fenced', + holder + ? `This caller is not bound to Run ${explicit.id}, which is bound to ${holder}. Nothing changed.` + : `This caller does not coordinate Run ${explicit.id}, which has no coordinator now. Nothing changed.` + ) + } + const worked = findCallerWorkerDispatches(db, caller)[0] + return new OrchestrationError( + 'run_required', + worked ? unboundWorkerMessage(worked) : `${COORDINATOR_RUN_REQUIRED} No effects were applied.`, + orchestrationSkillRecoveryData() + ) +} + // Why: task and gate mutations must share one Run-binding rule. export function resolveRunScope(runtime: OrcaRuntimeService, params: RunScopeParams): RunRow { const db = runtime.getOrchestrationDb() @@ -123,7 +158,7 @@ export function resolveRunScope(runtime: OrcaRuntimeService, params: RunScopePar if (!params.callerTerminalHandle) { throw new OrchestrationError( 'run_required', - 'No Run is bound. Use orchestration run-create or run-use first. No effects were applied.', + `${COORDINATOR_RUN_REQUIRED} A worker never binds a Run: its mail arrives through plain \`orchestration check\`, and it reports with \`orchestration send\`. No effects were applied.`, orchestrationSkillRecoveryData() ) } @@ -146,23 +181,21 @@ export function resolveRunScope(runtime: OrcaRuntimeService, params: RunScopePar ) } const current = db.getCurrentRunForCoordinator(caller) + // A worker naming its own or ancestor Run needs its Dispatch mailbox, not a binding change. + const workerRefusal = + explicit && current?.id !== explicit.id + ? workerRunBindingRefusal(db, caller, explicit.id, { takeover: false, bind: false }) + : null + if (workerRefusal) { + throw workerRefusal + } if (!current) { - if (explicit) { - throw new OrchestrationError( - 'consumer_fenced', - `This coordinator terminal is no longer bound to Run ${explicit.id}.` - ) - } - throw new OrchestrationError( - 'run_required', - 'No Run is bound. Use orchestration run-create or run-use first. No effects were applied.', - orchestrationSkillRecoveryData() - ) + throw unboundCallerError(db, caller, explicit) } if (explicit && current.id !== explicit.id) { throw new OrchestrationError( 'consumer_fenced', - `This coordinator terminal is bound to ${current.id}, not ${explicit.id}.` + `This caller is bound to Run ${current.id}, not Run ${explicit.id}. Nothing changed.` ) } return current diff --git a/src/main/runtime/rpc/methods/orchestration/worker/manual-dispatch-release.test.ts b/src/main/runtime/rpc/methods/orchestration/worker/manual-dispatch-release.test.ts index d2a432864cc..e5055d012e3 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/manual-dispatch-release.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/manual-dispatch-release.test.ts @@ -269,6 +269,50 @@ describe('orchestration.workerAbandon', () => { ) }) + it('refuses self-abandon by a terminal handle, reissued pane, or resolved session before effects', async () => { + const dispatchId = readyWorker() + const pane = 'tab_worker:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa' + const orcaSessionId = parseOrcaSessionAddress('orca_session_id:worker_1')! + const session: OrchestrationSessionCaller = { + address: 'term_worker', + terminalHandle: 'term_worker', + paneKey: pane, + orcaSessionId, + sessionId: orcaSessionId, + workspaceId: 'wt' + } + const callers: Partial[] = [ + { orchestrationCompatibilityEvidence: { terminalHandle: 'term_worker', paneKey: pane } }, + { orchestrationCompatibilityEvidence: { terminalHandle: 'term_reminted', paneKey: pane } }, + { orchestrationCaller: session } + ] + for (const caller of callers) { + await expect(abandon(dispatchId, caller)).rejects.toMatchObject({ + code: 'invalid_argument', + data: { effectsApplied: false, dispatchId } + }) + } + expect(db.getDispatchContextById(dispatchId)?.status).toBe('dispatched') + expect(db.getWorkerDispatch(dispatchId)?.state).toBe('ready') + }) + + it('refuses self-abandon of a context-only Dispatch', async () => { + const task = db.createTask({ runId: 'run_legacy_local', spec: 'context-only worker' }) + const dispatch = db.createDispatchContext({ + taskId: task.id, + assigneeHandle: 'term_worker', + creator: { kind: 'system' }, + maxDepth: 9 + }) + await expect( + abandon(dispatch.id, { + orchestrationCompatibilityEvidence: { terminalHandle: 'term_worker' } + }) + ).rejects.toMatchObject({ code: 'invalid_argument', data: { effectsApplied: false } }) + expect(db.getDispatchContextById(dispatch.id)?.status).toBe('dispatched') + expect(db.getTask(task.id)?.status).toBe('dispatched') + }) + it('reports an already-settled worker as stale and changes nothing', async () => { const dispatchId = readyWorker() db.failDispatch(dispatchId, 'tab closed', { workerProcessExited: true }) diff --git a/src/main/runtime/rpc/methods/orchestration/worker/worker-control.ts b/src/main/runtime/rpc/methods/orchestration/worker/worker-control.ts index 224063f3f63..4e0814f9954 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/worker-control.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/worker-control.ts @@ -201,8 +201,33 @@ export const ORCHESTRATION_WORKER_CONTROL_METHODS = [ name: 'orchestration.workerAbandon', permission: 'workspace', params: WorkerDispatchParams, - handler: (params, { runtime, orchestrationCaller }) => { + handler: (params, { runtime, orchestrationCaller, orchestrationCompatibilityEvidence }) => { const db = runtime.getOrchestrationDb() + // Session identity wins; a terminal-only caller is identified by its launch evidence and pane. + const caller = orchestrationCaller + ? { handle: orchestrationCaller.address, paneKey: orchestrationCaller.paneKey } + : orchestrationCompatibilityEvidence?.terminalHandle + ? { + handle: orchestrationCompatibilityEvidence.terminalHandle, + paneKey: + runtime.getTerminalPaneKey(orchestrationCompatibilityEvidence.terminalHandle) ?? + orchestrationCompatibilityEvidence.paneKey + } + : null + if ( + caller && + db.isDispatchMessageSender({ + dispatchId: params.dispatch, + handle: caller.handle, + paneKey: caller.paneKey + }) + ) { + throw new OrchestrationError( + 'invalid_argument', + `You cannot abandon Dispatch ${params.dispatch} while you are working it. Report to its coordinator instead. No effects were applied.`, + { effectsApplied: false, dispatchId: params.dispatch } + ) + } const abandoned = db.abandonWorkerDispatch( params.dispatch, runtime.getRuntimeId(), diff --git a/src/main/runtime/rpc/orchestration-session-coordinator.test.ts b/src/main/runtime/rpc/orchestration-session-coordinator.test.ts index 7ddd71f244e..1f5b77205c3 100644 --- a/src/main/runtime/rpc/orchestration-session-coordinator.test.ts +++ b/src/main/runtime/rpc/orchestration-session-coordinator.test.ts @@ -250,7 +250,7 @@ describe('a structured chat coordinates through the same verbs as a terminal', ( ok: false, error: { code: 'consumer_fenced', - message: `This coordinator terminal is no longer bound to Run ${runId}.` + message: `This caller is not bound to Run ${runId}, which is bound to ${ADDRESS_Y}. Nothing changed.` } }) expect(await as(SESSION_X, 'orchestration.runCurrent', {})).toMatchObject({ run: null }) @@ -600,6 +600,31 @@ describe('a structured worker that names itself by session id', () => { } }) + it('cannot take or read the Run it works for after making a sub-Run, and keeps its mail', async () => { + // The 10-14 incident: a worker made a sub-Run, `check --run ` said "bound to X, not Y", + // and it ran run-use on the parent, unbinding its coordinator. + const { runId } = dispatchToWorker() + const as = (method: string, params: Row) => + h.dispatch(orchestrationRequest(method, params, { sessionId: workerSession })) + resultOf(await as('orchestration.runCreate', { objective: 'sub' })) + + const read = await as('orchestration.check', { run: runId, peek: true }) + expect(read).toMatchObject({ ok: false, error: { code: 'worker_cannot_coordinate_own_run' } }) + expect(JSON.stringify(read)).not.toContain('run-use') + expect(resultOf(await as('orchestration.check', { peek: true }))).toMatchObject({ + messages: [{ subject: 'instructions' }] + }) + + expect(await as('orchestration.runUse', { id: runId })).toMatchObject({ + ok: false, + error: { code: 'worker_cannot_coordinate_own_run', data: { effectsApplied: false } } + }) + expect(h.db.getRunRaw(runId)).toMatchObject({ + coordinator_handle: 'term_coord', + consumer_generation: 1 + }) + }) + it.each([ ['its handle', handle], ['its session address', ADDRESS_Y] diff --git a/src/main/runtime/rpc/orchestration-session-run-binding.test.ts b/src/main/runtime/rpc/orchestration-session-run-binding.test.ts index 54edde9a03c..51f3b3fcf42 100644 --- a/src/main/runtime/rpc/orchestration-session-run-binding.test.ts +++ b/src/main/runtime/rpc/orchestration-session-run-binding.test.ts @@ -31,6 +31,7 @@ function deliveryId(result: Record): string { describe('structured lead Run binding through the RPC dispatcher', () => { let h: SessionCallerHarness let dispatchId: string + let leadHandle: string function call(sessionId: string, method: string, params: Record = {}) { return h @@ -41,6 +42,7 @@ describe('structured lead Run binding through the RPC dispatcher', () => { beforeEach(async () => { h = createSessionCallerHarness(hostRef) const handle = mintStructuredWorkerHandle() + leadHandle = handle const paneKey = mintStructuredWorkerPaneKey(SESSION_Y) structuredWorkerIdentities.register({ handle, @@ -61,6 +63,16 @@ describe('structured lead Run binding through the RPC dispatcher', () => { maxDepth: Number.MAX_SAFE_INTEGER }).id }) + // A Run this lead created earlier and that lost its binding: the one kind a worker may resume. + function orphanedOwnRun(): string { + const id = h.db.createRun({ + objective: 'resume', + coordinatorHandle: leadHandle, + coordinatorPaneKey: null + }).id + h.db.db.prepare('UPDATE runs SET coordinator_handle = NULL WHERE id = ?').run(id) + return id + } afterEach(() => { h.runtime.cancelMessageWaiters(`dispatch:${dispatchId}`) h.close() @@ -70,16 +82,7 @@ describe('structured lead Run binding through the RPC dispatcher', () => { it.each(['runCreate', 'runUse'] as const)( 'cancels a parked session Dispatch check on %s', async (method) => { - const params = - method === 'runCreate' - ? { objective: 'child' } - : { - id: h.db.createRun({ - objective: 'unbound', - coordinatorHandle: null, - coordinatorPaneKey: null - }).id - } + const params = method === 'runCreate' ? { objective: 'child' } : { id: orphanedOwnRun() } const wait = vi.spyOn(h.runtime, 'waitForMessage') const waiting = call(SESSION_Y, 'check', { wait: true, timeoutMs: 500 }) await vi.waitFor(() => diff --git a/src/shared/cli-argument-boundary.ts b/src/shared/cli-argument-boundary.ts index 795958c38d2..871a42d178e 100644 --- a/src/shared/cli-argument-boundary.ts +++ b/src/shared/cli-argument-boundary.ts @@ -51,6 +51,7 @@ export const CLI_BOOLEAN_FLAGS = new Set([ 'staged', 'supervise', 'tab', + 'takeover-legacy', 'tasks', 'text-stdin', 'unread',