Skip to content

fix(orchestration): refuse and report unreachable send targets - #13639

Closed
artile wants to merge 4 commits into
stablyai:mainfrom
tao-io:fix/orchestration-send-unreachable-targets
Closed

artile wants to merge 4 commits into
stablyai:mainfrom
tao-io:fix/orchestration-send-unreachable-targets

Conversation

@artile

@artile artile commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

orchestration send now refuses a terminal handle that no reader holds, and when it accepts an address that nothing polls it says so on the receipt instead of answering a bare ok: true.

This builds on #13458 by @bbingz, which found the same hole and fixed the clearest part of it. That PR is right about the shape of the check and right about the exception: an active Dispatch assignee whose pane key is not momentarily resolvable must still receive status mail, so getTerminalPaneKey alone cannot be the test. That carve-out is kept here, with its reasoning, and this PR is meant to land instead of #13458 rather than beside it — if #13458 goes in first I will rebase and cut this down to the remainder.

The delta is three things.

1. A live handle is not the same as a reachable handle

I checked the coordinator-handover case from the issue before building on it, and the code does not say what the issue says. run-use rebinds runs.coordinator_handle and runs.coordinator_pane_key and bumps consumer_generation; the previous coordinator's terminal keeps running, and because the pane-to-Run binding lives on runs.coordinator_pane_key, that pane ends up bound to nothing. So mail addressed to the pre-handover handle is readable — the fenced terminal's own orchestration check returns it through the per-handle branch. What breaks is different, and worse to debug: the row is filed under the Run, the Run's mailbox only selects to_handle = 'run:<id>', and the current coordinator never sees it. The sender gets ok: true for a message the coordinator it meant to reach will never read.

That is the general rule, not a handover quirk. A legacy to_handle row is only returned by the per-handle branch of orchestration check. A coordinator reads run:<id>; a worker with an active Dispatch reads dispatch:<id>; neither query returns bare-handle rows. So the useful question is the one the issue asks — is this handle still an addressee anyone reads — and it is answerable from the code. Refusing here would be wrong: those terminals are live and do receive the row, both by push-on-idle and by their own check. The receipt says it instead.

2. Group forms

@all and @worktree:<id> write one legacy-handle row per live terminal, so every recipient that is a coordinator or an active worker gets a row its check will never return — the ~23 invisible rows in the issue. Both options in the issue were open. I chose reporting over rerouting: fan-out exists to give each recipient independent read tracking (§4.5), and folding those rows into the Run mailbox would collapse that and drop group chatter into the fenced-delivery/ack protocol that lifecycle mail depends on, where a new coordinator generation would replay it. The group form also stays accepted — its members come from listTerminals, so they are live by construction. The receipt now names the recipients whose check will not surface the message.

3. Warnings on the receipt

orchestration.send gains an optional warnings array ({ code, recipient, message }), and the CLI prints one Warning: line per entry. Three codes:

  • no_live_terminal — accepted through a carve-out, but nothing holds the handle right now.
  • recipient_reads_other_mailbox — the recipient polls run:<id> or dispatch:<id>, so this row will not appear in its check; the message names the address to use instead.
  • recipient_outside_run — the row is filed under a Run the recipient does not belong to, which is what a stale coordinator handle looks like after a handover.

A new optional result field is wire-safe for older clients per docs/reference/remote-wire-compatibility.md.

One more thing #13458 will hit

The bare refusal on its own breaks orchestration-runtime-update-settlement.test.ts on current main: after a contract update the adopted Run keeps pre-update work for inspection, its coordinator_handle is NULL, and a retained worker's only address is a handle the runtime can no longer resolve. Refusing there drops the report the adopted Run exists to keep. So sends filed under the adopted Run are accepted with a no_live_terminal warning rather than refused. Worth knowing for #13458 as it stands.

Reproduction

Before (from the issue):

$ orca orchestration send --to run:run_deadbeef0000 --subject x --body y --json
{"ok":false,"error":{"code":"run_not_found", …}}

$ orca orchestration send --to dispatch:ctx_deadbeef0000 --subject x --body y --json
{"ok":false,"error":{"code":"dispatch_not_found", …}}

$ orca orchestration send --to term_00000000-0000-0000-0000-000000000000 --subject x --body y --json
{"ok":true,"result":{"message":{"id":"msg_8a5597b2290c", …}}}

After, as asserted by the tests in this PR:

$ orca orchestration send --to term_00000000-0000-0000-0000-000000000000 --subject x --body y --json
{"ok":false,"error":{"code":"terminal_not_found", …}}

# handle of a coordinator that run-use replaced
$ orca orchestration send --to term_<pre-handover> --subject x --body y --json
{"ok":true,"result":{"message":{…},"warnings":[{"code":"recipient_outside_run","recipient":"term_<pre-handover>", …}]}}

$ orca orchestration send --to @all --subject x --body y
Sent 2 messages to 2 recipients
Warning: term_coord reads run:run_…, and that mailbox never returns messages addressed to a bare terminal handle. Address run:run_… instead.
Warning: term_worker reads dispatch:ctx_…, and that mailbox never returns messages addressed to a bare terminal handle. Address dispatch:ctx_… instead.

I could not exercise the packaged CLI against a live runtime from this environment, so the after block is the RPC behaviour the tests assert, not a transcript. The invisibility claims behind it were verified against the real OrchestrationDb and OrcaRuntimeService, not mocks of them: the tests send through the RPC handler and then read back through orchestration.check.

Test plan

  • vitest run src/main/runtime/rpc/methods/orchestration.test.ts — 160 passed. New cases: unknown handle refused with terminal_not_found and no row stored; active Dispatch assignee without a pane accepted with no_live_terminal; live worker warned with recipient_reads_other_mailbox and its check proven empty; coordinator warned with the run: mailbox named; handle replaced by run-use warned with recipient_outside_run, with the Run mailbox proven empty and the fenced terminal proven still able to read; plain live terminal outside any Run gets no warnings; @all names both unreachable recipients and both checks proven empty.
  • vitest run src/cli/handlers/orchestration-send-receipt-warnings.test.ts — 3 passed (single receipt, fan-out receipt, clean receipt unchanged).
  • vitest run src/main/runtime/rpc src/cli/handlers — 157 files, 1552 passed, 1 skipped.
  • pnpm typecheck — clean.
  • pnpm lint — clean.
  • pnpm test (full suite) — 4594 files passed, 49271 passed, 99 skipped. Two failures remain, both in src/main/agent-hooks/ WSL relay tests; they fail identically on unmodified upstream/main in this environment (no WSL host).
  • src/main/ssh/ssh-remote-orca-cli.test.ts — its fixture resolved every handle to a null pane, so the new validation refused its recipient. The fixture now resolves the recipient the way a host runtime does; second commit.
  • Not run: manual CLI verification against a live runtime.

Fixes #13363

Follow-up: unreachable fan-out recipients (review round 2)

CodeRabbit found a hole this PR had left open: group fan-out resolved recipient reach only
after every row was inserted and notified, so an unreachable member still got a row nobody
can read. It is reachable in practice — terminal.list reports a connected PTY that owns no
pane (buildPtyTerminalSummary, leaf id pty:<ptyId>), and getTerminalPaneKey returns null
for it because the id is not a pane UUID. Reach now resolves before db.insertMessage.

The judgement call, stated on purpose. The bot asked for all-or-nothing: refuse the whole
fan-out if any member is unreachable. I chose per-recipient reporting instead. Group membership
is discovered rather than chosen — the sender picks @all, not who is in it — so one orphaned
pane would block a broadcast to every live agent, with no way for the sender to exclude the
offender. That trades a storage bug for an availability bug.

Unreachable recipients are therefore dropped before insertion and named on the receipt under a
new warning code recipient_unreachable, distinct from no_live_terminal, which reports a row
that was stored (an in-flight Dispatch assignee whose pane is momentarily unresolvable).
recipients counts delivered rows rather than resolved handles. The strict rule survives where
it still earns its keep: a fan-out in which no recipient is reachable stores nothing readable at
all, so it throws terminal_not_found and writes no rows.

$ orca orchestration send --to @all --subject x --body y
Sent 1 messages to 1 recipients
Warning: No live terminal holds term_<orphan> and no active Dispatch names it, so no message was created for that recipient.

Added tests: skips a fan-out member nothing can reach and names it on the receipt,
refuses a fan-out when no resolved recipient can be reached, and
keeps delivering to a worktree group when one member has no pane (the @worktree:<id> form),
plus a CLI receipt case for the partial-delivery line. vitest run over the four touched
suites: 194 passed. pnpm lint and pnpm typecheck clean.

artile added 2 commits August 10, 2026 22:22
`orchestration send --to <handle>` accepted any bare terminal handle and
answered ok:true, storing a legacy `to_handle` row that no reader returns.
Refuse a handle with no live pane and no active Dispatch with
`terminal_not_found`, and, when an address is accepted but no reader polls
it, name that recipient in a `warnings` entry on the receipt.

A bare-handle row is only returned by the per-handle branch of
`orchestration check`. A coordinator reads `run:<id>` and a worker reads
`dispatch:<id>`, so a live recipient can still be unable to pull the
message. Group fan-out writes the same kind of row per recipient, which is
why `@all` traffic never shows up in a check.

Two addresses stay accepted on purpose: an active Dispatch assignee whose
pane key is not momentarily resolvable, and the retained pre-update Run,
whose handles are expected to be dead and whose mail is kept for
inspection. Both carry a warning instead of an error.

Fixes stablyai#13363
The remote CLI fixture resolved every handle to a null pane, so the new
send validation refused its recipient. Resolve the recipient the way the
host runtime does and stub the active-Dispatch lookup.
@coderabbitai

coderabbitai Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 80eeed04-d10c-4d16-8155-cda56c57d4fc

📥 Commits

Reviewing files that changed from the base of the PR and between cf679b3 and 3c96d6c.

📒 Files selected for processing (2)
  • src/main/runtime/rpc/methods/orchestration.test.ts
  • src/main/runtime/rpc/methods/orchestration.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/runtime/rpc/methods/orchestration.test.ts
  • src/main/runtime/rpc/methods/orchestration.ts

📝 Walkthrough

Walkthrough

Orchestration sends now resolve terminal recipient reachability before delivery. Missing terminals produce terminal_not_found. Deliverable messages can include warnings for inactive terminals, alternate mailboxes, and recipients outside the filed Run. Group sends skip unreachable recipients and aggregate warnings. RPC error mapping preserves terminal_not_found, and the CLI formats warnings for single-recipient and fan-out results. Tests cover routing, warning states, formatting, and updated runtime fixtures.

Merge Risk: ⚪ Minimal · up to 3c96d

The change improves reporting and refusal behavior for unreachable orchestration targets, with the supplied checks passing for the touched areas; no actionable merge-blocking risk remains beyond normal review and checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary change: refusing and reporting unreachable orchestration send targets.
Description check ✅ Passed The description clearly explains the problem, implementation, linked issue, behavior examples, testing, and known environment failures.
Linked Issues check ✅ Passed The changes satisfy issue #13363 by rejecting unknown handles, reporting warnings, and handling unreachable fan-out recipients.
Out of Scope Changes check ✅ Passed The implementation, warning types, RPC mapping, CLI formatting, fixtures, and tests are related to the linked issue objectives.
✨ Finishing Touches 💡 1
⚔️ Resolve merge conflicts 💡
  • Resolve merge conflict in branch fix/orchestration-send-unreachable-targets

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 26897f06-598e-4599-bd6f-7bfa80d69fc7

📥 Commits

Reviewing files that changed from the base of the PR and between c0c893d and fbd3d74.

📒 Files selected for processing (8)
  • src/cli/handlers/orchestration-send-receipt-warnings.test.ts
  • src/cli/handlers/orchestration.ts
  • src/main/runtime/rpc/errors.ts
  • src/main/runtime/rpc/methods/orchestration-recipient-reachability.ts
  • src/main/runtime/rpc/methods/orchestration.test.ts
  • src/main/runtime/rpc/methods/orchestration.ts
  • src/main/runtime/rpc/orchestration-runtime-update-settlement.test.ts
  • src/main/ssh/ssh-remote-orca-cli.test.ts

Group fan-out resolved recipient reach only after every row was inserted and
notified, so an @ALL or @WorkTree: member with no live pane and no active
Dispatch still got a legacy to_handle row that no reader can surface. An
orphaned PTY is the concrete case: terminal.list reports it, but its leaf id is
`pty:<id>` rather than a pane UUID, so getTerminalPaneKey returns null.

Resolve reach for every recipient before db.insertMessage, deliver to the
reachable ones, and name each dropped recipient on the receipt with a new
`recipient_unreachable` warning. Group membership is discovered rather than
chosen, so refusing the whole broadcast for one dead member would let a single
orphaned pane block a live fleet. A fan-out where no recipient is reachable
stores nothing readable at all and still fails with terminal_not_found.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/runtime/rpc/methods/orchestration.ts (1)

648-648: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Return sendWarnings from each direct lifecycle receipt.

When a direct lifecycle send returns at lines 624, 639, or 644, it bypasses line 648. A bare recipient with an active Dispatch and no live pane is deliverable and receives no_live_terminal, but these receipts omit that warning. Add sendWarnings to these return values and add a regression test.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3517837b-6660-4235-8398-bcc9ddcfcd6c

📥 Commits

Reviewing files that changed from the base of the PR and between fbd3d74 and cf679b3.

📒 Files selected for processing (4)
  • src/cli/handlers/orchestration-send-receipt-warnings.test.ts
  • src/main/runtime/rpc/methods/orchestration-recipient-reachability.ts
  • src/main/runtime/rpc/methods/orchestration.test.ts
  • src/main/runtime/rpc/methods/orchestration.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/cli/handlers/orchestration-send-receipt-warnings.test.ts
  • src/main/runtime/rpc/methods/orchestration-recipient-reachability.ts

…ts too

Only the ordinary direct receipt carried sendWarnings. The three lifecycle
returns — a capability rejection, a suppressed heartbeat and a rejected worker
report — returned before it, so a sender that addressed a bare handle with an
active Dispatch and no live pane got a clean-looking receipt and never learned
that nothing was listening. The warning describes the address, not the outcome,
so every direct receipt owes it.

A withSendWarnings helper now attaches it at all four returns, so a later return
added to this path cannot silently drop it again.

Two regression tests, both verified failing on the parent commit: a suppressed
heartbeat and a rejected worker_done, each addressed to a handle that warns
no_live_terminal. The heartbeat case runs without a bound Run on purpose — a
heartbeat naming a dispatchId is readdressed to the sender's Run when it has
one, and a run: address carries no bare-handle warning to lose.
@artile

artile commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Picking up the outside-diff comment from the 2026-08-10 review, on orchestration.ts:648 — "Return sendWarnings from each direct lifecycle receipt." It went unanswered when I handled the fan-out finding; it was correct, and it is fixed in 3c96d6c.

The diagnosis holds exactly as written. Three returns sat before line 648 — the dispatch_capability_invalid rejection, the suppressed heartbeat, and the rejected lifecycle report — and each returned a receipt with no warnings. A sender addressing a bare handle that has an active Dispatch and no live pane is deliverable, earns no_live_terminal, and on those three paths got a clean-looking receipt instead. That warning is the only signal it has, since the send itself succeeds.

The remedy is a withSendWarnings helper applied at all four returns rather than a spread repeated four times, because the reason this was missed is that the warning lives at the top of the handler and the returns are ~120 lines below it. A fifth return added to this path now gets the same treatment by default.

Two regression tests in orchestration.test.ts, both verified red against cf679b31bd:

  • a suppressed heartbeat (unknown dispatchId) to a warned handle;
  • a rejected worker_done (missing_dispatch_id) to the same, asserting the lifecycle verdict and the warning together.

One detail worth recording, since it shaped the first test. A heartbeat that names a dispatchId is readdressed to the sender's Run whenever it has one, so the bare-handle warning never exists on that path. The suppressed case therefore runs without a bound Run, which is the only shape where a lifecycle heartbeat keeps a bare recipient. The capability-rejection return is in the same position: it needs routing.dispatchId, and with a Run present the address is rewritten to run:, so it is covered by the helper rather than by a test.

pnpm typecheck and pnpm lint clean; orchestration.test.ts plus orchestration-send-receipt-warnings.test.ts are 169 passed.

@Jinwoo-H

Copy link
Copy Markdown
Contributor

Superseded by #14964. The original branch predates #13717's canonical Run/Dispatch mailbox architecture and its fork rejects maintainer pushes despite the editable-maintainer flag, so the current-main rewrite could not be pushed here. The replacement preserves the issue's intent, documents the narrowed semantics, and carries deterministic receipt + DB/check red/green/revert proof. This PR was not merged.

@Jinwoo-H Jinwoo-H closed this Aug 16, 2026
@artile

artile commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

Understood, and no objection to the supersede — #14964 is built on the architecture this branch predates, and that is the right way round. Thanks for carrying the intent rather than dropping it.

I read the replacement. It covers both holes that only surfaced during review here, and covers one of them better than this branch did: withSendWarnings is applied to five returns in orchestration.ts, including the non-rejected worker_done lifecycle return and the final plain return, where this branch reached only four. The group path carries groupWarnings too. Nothing to flag from my side.

On the push failure — you are right, and the cause is not the flag. maintainer_can_modify reads true on this PR and on every other one of ours; I checked all thirteen. The blocker is that tao-io/orca is owned by an organization, and "allow edits by maintainers" does not grant push access on org-owned forks regardless of what the flag reports. So the API says yes and the push is rejected, which is exactly the confusing behaviour you hit.

That is ours to fix, not yours, and it affects every open PR we have here — so the same rewrite-and-close is the only path available to you on any of them. I am raising it on our side today. Until it is resolved, the practical workaround is to tell me what you want changed and I will push it within the day; I am watching these daily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: orchestration send accepts an unknown terminal handle silently, creating a message nobody can read

4 participants