Skip to content

feat(orchestration): reading the inbox marks the batch handled (auto-ack) - #34754

Closed
Jinwoo-H wants to merge 9 commits into
mainfrom
orch-auto-ack
Closed

Jinwoo-H wants to merge 9 commits into
mainfrom
orch-auto-ack

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

Reading your orchestration inbox now counts as handling it. A coordinator or worker that forgets --ack no longer gets stuck seeing the same old batch while newer messages pile up behind it.

What Changed

Before: orca orchestration check returned the oldest batch and kept returning that same batch until the caller ran check --ack <delivery_id>. Agents forgot the ack, so newer mail stayed hidden and the wake pointer switched to "…after delivery X, which you have not acknowledged…".

After:

  • A consuming check (with or without --wait) marks the batch it returns as handled before it returns. The next check returns newer mail. This covers Run inboxes, worker Dispatch mailboxes, federated attachments, and the pre-bind residual Dispatch path.
  • --peek and --all still never consume.
  • --ack still works. Acking a batch that is already handled is a no-op, so older CLIs and guides that still ack are fine.
  • Waiters woken by the same notification share one batch. The waiter registry hands every waiter resolved by one notification a shared wake object. The first of them to take a batch stores it there, and the rest return that exact batch (replayed: true) instead of taking more mail. A forgotten background wait therefore cannot split a wake or swallow the batch the live wait was waiting for. A waiter never falls back to a batch from before its own wake. A waiter that receives a shared batch records its own receipt as soon as it has it, so its --retry-request replays that batch too.
  • Receipts with a batch now carry deliveryAcknowledged: true. The CLI uses it for mixed versions: with an older runtime that does not set it, text output adds "This Orca version does not acknowledge batches automatically. After handling every message, rerun this check with --ack ."
  • Updated the worker preamble, the check help text, and the help notes to match.

Mechanism: takeMailboxDelivery / takeRunDelivery select or create the batch, mark it acknowledged, and write the check's complete mutation receipt in one BEGIN IMMEDIATE transaction. They reuse the existing consumer fencing and generation checks and the existing completeMutationReceipt. The held-batch pointer machinery stays, because a batch that an older Orca left outstanding is still served: the next check returns it once and marks it handled.

What "returns it" means (lost responses)

A consuming check is already a durable orchestration mutation keyed by request id. Marking the batch handled and storing the full receipt with that batch now happen in the same database commit. Either both persist or neither does:

  • Interrupted or closed before mail arrives: nothing is consumed. A cancelled wait returns connectionLost and acks nothing.
  • Response lost in transit, or the server crashed after the commit: the CLI already prints a --retry-request <id> recovery command for unknown outcomes. Retrying with it replays the stored receipt with the same batch.
  • Fenced after consuming: a check ends the moment it hands over a batch. Nothing runs after the take, so a later fence check or receipt write can neither withhold the batch nor overwrite its receipt. This covers a nested coordinator whose Run consumer changes after it consumed pre-bind Dispatch mail.
  • Context lost (brand-new session, compaction, a killed CLI that never printed its request id): the rows are marked read, not deleted. orca orchestration inbox --terminal <run:…|dispatch:…> --limit <n> --full shows them. Name the Run mailbox for a coordinator, and the Dispatch mailbox for a worker, including mail it read before binding a helper Run. check --all is only a recent window, so the help no longer offers it as recovery.

I chose this over "ack the previous batch on the caller's next check". That option drops the same mail if the next check comes from a fresh context, and it brings back the stale-batch state this change removes. The request receipt already gives a precise retry for the one window that matters.

Observers

Consuming checks belong to the agent itself. Help text and notes now say that a script that only watches the inbox must use --peek (including --peek --wait) and must never --ack. No observer leases or script detection were added.

Trade-offs to note

  • A worker process that took a batch and was then replaced (Dispatch re-attach) no longer gets that batch again automatically. The new process can find it with orchestration inbox --terminal dispatch:<id> --full.
  • Two plain (non-waiting) checks running at the same moment now split the mail instead of both getting it. Concurrent --wait calls still share a batch.

Why

Missed acks were the main cause of stale-batch bugs. Moving the bookkeeping into Orca removes the "ack until count is 0" loop, the "unacked batch hides newer mail" failure, and the held-batch pointer text in normal use. The design note recommends exactly this (idea 3: reading the inbox advances it).

Linked Issue

Refs #16522, #21081

Visual Proof

Real hidden dev builds (ORCA_BACKGROUND_LAUNCH=1, separate /tmp profiles and CDP ports, isolated CLAUDE_CONFIG_DIR). A coordinator terminal runs run-create, receives first-report, never runs --ack, sits as an idle agent while a worker sends second-report, and then checks again.

Before (main): held-batch pointer, and the second check replays first-report, hiding second-report.

before.png

You have 1 new orchestration message after delivery delivery_9eda02fad8d9, which you have not acknowledged. If you no longer have it, run `orca-dev orchestration check --run run_c6f67cddbb87` to see it again; once it is handled, run `orca-dev orchestration check --run run_c6f67cddbb87 --ack delivery_9eda02fad8d9`.
$ orca-dev orchestration check
Delivery delivery_9eda02fad8d9
msg_3e61bdf22589 [status] from=term_… "first-report"

After (this branch): normal pointer, and the second check returns second-report.

after.png

You have 1 orchestration message. Run `orca-dev orchestration check --run run_e9e659b7ae1f`.
$ orca-dev orchestration check
Delivery delivery_19c85a4c05d5
msg_1f20acbce558 [status] from=term_… "second-report"

Mixed versions (newer CLI, older runtime from the "before" build):

Delivery delivery_9eda02fad8d9
msg_3e61bdf22589 [status] from=term_… "first-report"
This Orca version does not acknowledge batches automatically. After handling every message, rerun this check with --ack delivery_9eda02fad8d9.

Testing

  • I manually tested these changes locally (macOS, hidden dev builds; before/after above)
  • Automated tests added/updated

The old tests encoded "replay until ack", so they now assert the new contract instead:

  • No stale batch hides newer mail: Dispatch mailbox across restart (no --ack in the pointer, next check returns the newer guidance), Run pointer for a never-acking coordinator, residual and Run interleavings.
  • Concurrent waiters (new check-consumption-safety.test.ts, Run and Dispatch): two parked waits woken by one notification over 51 messages both return the same 50-message batch, leaving one unread. A question-filtered waiter whose wake batch was taken by another consumer returns nothing, never an older status batch. Two same-Run waits over the real socket transport share one batch with one delivery row, and two plain concurrent checks get no duplicates. All new tests fail on the previous head.
  • Receipt coupling: a take whose receipt write fails rolls back, leaving the mail unread with no delivery. A nested coordinator fenced after consuming residual Dispatch mail returns that batch, and the receipt recorded with the take carries it.
  • --peek does not consume: peek and filtered peek before a consuming check leave mail unread.
  • Explicit --ack stays harmless: acking a handled batch returns acknowledged with nothing consumed. Covered over RPC, the built CLI, and a federated attachment across restart.
  • Mixed versions: an older CLI's --ack against this server is a no-op. The newer CLI prints the ack hint when the receipt lacks deliveryAcknowledged (formatter test, plus the real-app run above).
  • Lost response: retrying the same request id replays the stored batch.
  • Held-batch chat nudges are still covered for a batch an older Orca left outstanding.

pnpm check:changed --maxWorkers=2 passes.

Not verified: Windows, Linux, and SSH/remote workers by hand. The change is platform-neutral SQLite/RPC, and the remote path goes through the same check RPC with a federated attachment test.

Guides

skill-guides/orchestration.md and the coordinator-loop, messaging-and-gates, and worker-contract references now say:

  • A plain check marks the batch it returns as handled.
  • --ack is gone from the normal loop. It is still accepted, and workers use it only when a batch lacks deliveryAcknowledged (an older Orca).
  • A lost reply is recovered with --retry-request <id>.
  • After lost context, the agent reads orchestration inbox --terminal run:<run_id> (coordinator) or dispatch:<dispatch_id> (worker) with --limit 50 --full.
  • Observer scripts use --peek.

The bundled copies in src/cli/bundled-skill-guides.ts are regenerated.

@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
Walkthrough

Mailbox checks now take and acknowledge returned deliveries. Concurrent waiters can share a delivery take. Responses expose acknowledgement state, and waiting checks return empty results when no batch is available. Tests cover restarts, routing, consumer ownership, residual mail, and receipt rollback. CLI help and formatted output describe compatibility acknowledgement and retry behavior.

Priority: Normal

Merge Risk: Moderate · up to 9a3e1

Mailbox auto-acknowledgement works as intended, but the guidance has gaps. Coordinators on older runtimes can reprocess the same batch and miss newer mail. The documented recovery command can miss the oldest messages when more than 50 are queued. Shared waiters may also lose a recoverable batch if a receipt write fails. Resolve these before merging or explicitly accept them.

Pre-merge checks: Passed 3 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Description check Warning The description is detailed and covers the user impact, implementation, rationale, testing, visual proof, compatibility, and trade-offs. However, the required Linked Issue section uses `Refs #16522, #… Add an actual linked issue using the required Fixes #<issue-number> format, and complete the omitted template sections or explicitly mark them as not applicable.
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: consuming an orchestration inbox batch marks it handled automatically.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is detailed and covers the user impact, implementation, rationale, testing, visual proof, compatibility, and trade-offs. However, the required Linked Issue section uses Refs #16522, #21081 instead of an actual linked issue in the required Fixes # format. Several template sections are also omitted, including Review, Agent skill upstream boundary, Notes, and Checklist.

Resolution

Add an actual linked issue using the required Fixes #&lt;issue-number&gt; format, and complete the omitted template sections or explicitly mark them as not applicable.

Full details: Docstring Coverage

Explanation

Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 16 functions across 37 files. (4 skipped: 4 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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: 2


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4f207240-a06c-4cac-b652-dab564b7fc60
Commits

Reviewing files that changed from the base of the PR and between ca21549 and de3feb7.

Files ignored due to path filters: 1
  • src/main/runtime/orchestration/__snapshots__/preamble.test.ts.snap is excluded by !**/*.snap
Files selected for processing: 22
  • src/cli/specs/orchestration.ts
  • src/main/runtime/orchestration-cli-restart-handoff.test.ts
  • src/main/runtime/orchestration-dispatch-mailbox-delivery.spec.ts
  • src/main/runtime/orchestration-mailbox-detached-routing.spec.ts
  • src/main/runtime/orchestration-mailbox-notification-consistency.test.ts
  • src/main/runtime/orchestration-message-delivery-identity.test.ts
  • src/main/runtime/orchestration/db/messages/role-mailbox-delivery.ts
  • src/main/runtime/orchestration/db/runs/run-delivery.ts
  • src/main/runtime/orchestration/preamble.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-run.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-worker-consumer-fencing.test.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-worker-federated-attachment.test.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-worker.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check.test.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-interleavings.test.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/run-bound-mailbox-safety.test.ts
  • src/main/runtime/rpc/orchestration-current-authority-precedence.test.ts
  • src/main/runtime/rpc/orchestration-session-coordinator.test.ts
  • src/main/runtime/runtime-rpc-long-poll-transport.test.ts
  • src/main/runtime/structured-chat-coordinator-mail.test.ts
  • src/shared/orchestration-check-output.test.ts
  • src/shared/orchestration-check-output.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Comment thread src/cli/specs/orchestration.ts Resolved

@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
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 93de6eb6-5b2b-4bf3-abea-051f44f5ffb8
Commits

Reviewing files that changed from the base of the PR and between de3feb7 and b8d49c8.

Files selected for processing: 10
  • src/cli/specs/orchestration.ts
  • src/main/runtime/construction/runtime-service-state.ts
  • src/main/runtime/orchestration/db/messages/role-mailbox-delivery.ts
  • src/main/runtime/orchestration/db/runs/run-delivery.ts
  • src/main/runtime/orchestration/mailbox-notification-coordinator.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-consumption-safety.test.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-run-pending-mail.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-run.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-worker.ts
  • src/main/runtime/runtime-message-waiters.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@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
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 23050fad-f17c-4122-86f4-fc3777973443
Commits

Reviewing files that changed from the base of the PR and between 7ac3a65 and b8ee4f8.

Files selected for processing: 4
  • src/main/runtime/rpc/methods/orchestration/messaging/check-consumption-safety.test.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-run-pending-mail.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-run.ts
  • src/main/runtime/rpc/methods/orchestration/messaging/check-worker.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

const key = `${address}:${workerMailbox.generation}`
const batch = waitResult.share(key, () => takeDelivery(typeFilter))
// A waiter that shares another's take has no take transaction, so it receipts the batch on arrival.
args.recordMutationReceipt?.(result(batch))

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.

Major · Data Integrity & Integration · Heavy lift

Preserve the receipt when a shared waiter receives a batch.

If another waiter takes the batch first, share returns that committed batch to this waiter. Line 289 then writes this waiter's receipt outside the take transaction. If the receipt write fails, this waiter gets an error, but the batch remains acknowledged and a retry cannot recover its messages. The Run wait path has the same sequence at Lines 200–202 of check-run.ts. Coordinate the shared waiters' receipts with the take, or provide a durable recovery path for each waiter. (raw.githubusercontent.com)

@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: 2


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 07490f56-87ed-4bc3-a215-35b2ec94ab77
Commits

Reviewing files that changed from the base of the PR and between b8ee4f8 and 9a3e16b.

Files selected for processing: 5
  • skill-guides/orchestration.md
  • skill-guides/orchestration/references/coordinator-loop.md
  • skill-guides/orchestration/references/messaging-and-gates.md
  • skill-guides/orchestration/references/worker-contract.md
  • src/cli/bundled-skill-guides.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Comment on lines +137 to +138
oldest FIFO Delivery and marks it handled; `--ack` is still accepted but not
needed. Process every message: reply to questions, validate each `worker_done`

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.

Major · Functional Correctness · Quick win

Preserve the coordinator compatibility acknowledgement.

If the runtime is older, it omits deliveryAcknowledged and replays the batch until --ack. The documented --json loop will otherwise process the same batch repeatedly and hide newer mail. Add the conditional acknowledgement used in the worker guidance, including in the messaging reference and bundled guide. (raw.githubusercontent.com)


If a `check` reply was lost, rerun it with the `--retry-request <id>` it printed.
If you lost context, read the handled mail with
`ORCA orchestration inbox --terminal run:<run_id> --limit 50 --full --json`.

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.

Major · Data Integrity & Integration · Quick win

Supported by static analysis

Script executed:

sed -n '1,200p' src/main/runtime/orchestration/db/messages/message-inbox.ts
sed -n '134,156p' skill-guides/orchestration.md

Repository: stablyai/orca

Length of output: 8058


Script executed:

set -o pipefail
printf '%s\n' '--- relevant references ---'
rg -n -F --glob '*.ts' --glob '*.md' -e 'getAllMessages(' -e 'orchestration inbox' -e '--limit 50' -e 'truncated' -e 'cursor' -e 'check' src/main skill-guides | head -240
printf '%s\n' '--- inbox implementation callers and command output ---'
rg -n -F --glob '*.ts' -e 'getAllMessages(' -e 'inbox' src/main/runtime src/main/cli src/main | head -240
printf '%s\n' '--- check batch selection ---'
rg -n -F --glob '*.ts' -e 'getUnreadMessages(' -e 'getUndeliveredUnreadMessages(' -e 'Delivery' -e 'limit:' src/main/runtime/orchestration | head -260
printf '%s\n' '--- guide recovery references ---'
rg -n -C 4 -F --glob '*.md' -e 'inbox --terminal' -e 'Inbox recovery' -e 'Dispatch recovery' -e '--retry-request' skill-guides src/main

Repository: stablyai/orca

Length of output: 41400


Script executed:

set -o pipefail
printf '%s\n' '--- guide context ---'
nl -ba skill-guides/orchestration.md | sed -n '130,170p'
nl -ba skill-guides/orchestration/references/messaging-and-gates.md | sed -n '1,35p'
nl -ba skill-guides/orchestration/references/worker-contract.md | sed -n '44,62p'
printf '%s\n' '--- inbox command bindings ---'
rg -n -F --glob '*.ts' -e 'getAllMessages' -e 'inbox' src/main/cli src/main/runtime/rpc src/main/runtime/orchestration | grep -vE 'test|message-inbox.ts' | head -220
printf '%s\n' '--- delivery batch definition and selection ---'
rg -n -F --glob '*.ts' -e 'ORCHESTRATION_DELIVERY_BATCH_LIMIT' -e 'getOrCreateMailboxDelivery' -e 'getUndeliveredUnreadMessages' src/main/runtime/orchestration | head -180

Repository: stablyai/orca

Length of output: 17233


Script executed:

set -o pipefail
nl -ba src/main/runtime/rpc/methods/orchestration/messaging/message-methods.ts | sed -n '145,205p'
rg -n -F --glob '*.ts' 'getAllMessagesForHandle' src/main/runtime/orchestration src/main/runtime/rpc
nl -ba src/main/runtime/orchestration/db/messages/message-inbox.ts | sed -n '115,135p;270,310p'
nl -ba src/main/runtime/orchestration/db/messages/mailbox-routing-page.ts | sed -n '1,20p'
nl -ba src/main/runtime/orchestration/mailbox-pointer-eligibility.ts | sed -n '45,65p'

Repository: stablyai/orca

Length of output: 7286


Script executed:

nl -ba src/main/runtime/orchestration/db/messages/message-inbox.ts | sed -n '135,265p'

Repository: stablyai/orca

Length of output: 5491


Do not treat 50 inbox rows as complete recovery.

A consuming check returns the oldest FIFO batch of up to 50 messages. The terminal-scoped inbox command returns only the newest rows and provides no cursor or truncation indicator. With more than 50 messages, it can omit messages from the consumed batch.

Update the Run and Dispatch recovery guidance to require a limit known to exceed the mailbox history, or provide pagination/truncation metadata before presenting the command as complete recovery.

@Jinwoo-H

Copy link
Copy Markdown
Contributor Author

Superseded by #36137, which combines the orchestration v3 changes into one PR.

@Jinwoo-H Jinwoo-H closed this Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant