Skip to content

fix(orchestration): preserve completed prompt dispatches - #15941

Open
haoliangli wants to merge 1 commit into
stablyai:mainfrom
haoliangli:fix/orchestration-settle-completed-prompt
Open

haoliangli wants to merge 1 commit into
stablyai:mainfrom
haoliangli:fix/orchestration-settle-completed-prompt

Conversation

@haoliangli

Copy link
Copy Markdown

Summary

Prevent orchestration dispatch --inject from reporting agent_prompt_stalled after the dispatched worker has already completed the task through worker_done.

Problem

Prompt submission verification can time out while the worker has already received the lifecycle preamble, executed the task, and durably completed the Dispatch. The injection catch path currently calls failDispatch and rethrows the stale prompt error without re-reading settlement state. This produces a contradictory result: the CLI reports failure even though the Task and Dispatch are completed. A coordinator may then retry work that already ran.

Approach

  • Re-read the Dispatch after prompt submission throws.
  • If the Dispatch is already completed, treat the durable settlement as authoritative and return injected: true.
  • Preserve the existing rollback behavior for every unsettled prompt failure.
  • Add a regression test for the worker_done / prompt-verification race.

Verification

  • orchestration-tasks-dispatch.test.ts: 31/31 passed
  • New regression test fails with the production change reverted (agent_prompt_stalled) and passes with the fix
  • typecheck:node: passed
  • oxfmt --check on changed files: passed
  • oxlint on changed files: passed
  • git diff --check: passed

Scope

This change is limited to the low-level local orchestration.dispatch --inject settlement race. It does not change agent recognition, prompt submission timing, worker-start launch behavior, or terminal ownership/release semantics.

Related: #15920

A worker can report completion before prompt verification times out. Treat that durable completion as authoritative instead of overwriting it with a stale prompt-delivery failure.
Copilot AI lite review requested due to automatic review settings August 22, 2026 12:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The orchestration.dispatch handler now checks whether a dispatch completed concurrently after terminal prompt injection fails. Completed dispatches return success with injected: true. Incomplete dispatches are marked failed and rethrow the original error. An integration test covers task completion before prompt sending raises an error and verifies completed task and dispatch statuses.

Merge Risk: 🟡 Moderate · up to 4b3f3

The fix preserves completed work after a prompt-verification race, but the completed response can still contain a stale dispatch status, causing callers to receive inconsistent settlement information. This bounded correctness issue should be fixed before merge; the regression test should also exercise the actual worker_done path.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes preserving completed orchestration prompt dispatches.
Description check ✅ Passed The description explains the problem, approach, scope, linked issue, and verification, but omits several template sections and checklist items.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.
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.

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

🧹 Nitpick comments (1)
src/main/runtime/rpc/methods/orchestration-tasks-dispatch.test.ts (1)

320-324: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Exercise the actual worker_done path.

If this test is intended to cover the race named in the PR objective, the mock should call lifecycle reconciliation instead of updating the task directly. Direct db.updateTaskStatus skips worker_done payload validation, Dispatch identity checks, and message settlement. The existing lifecycle-reconciliation.test.ts uses a worker_done message containing taskId and dispatchId; reuse that path before throwing the prompt error.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3207852e-78d4-423e-917e-adb80f7a678b

📥 Commits

Reviewing files that changed from the base of the PR and between 5651662 and 4b3f3de.

📒 Files selected for processing (2)
  • src/main/runtime/rpc/methods/orchestration-tasks-dispatch.test.ts
  • src/main/runtime/rpc/methods/orchestration.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment on lines +1702 to +1708
const settled = db.getDispatchContextById(ctx.id)
if (settled?.status === 'completed') {
injected = true
} else {
db.failDispatch(ctx.id, err instanceof Error ? err.message : String(err))
throw err
}

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.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Return the settled Dispatch row.

The code reads settled at Line 1702, but the response still returns ctx at Lines 1714-1716. ctx was created before the race and can still contain status: 'dispatched' while the durable row is completed. Return settled on this branch so the response matches the authoritative state.

Proposed fix
           const settled = db.getDispatchContextById(ctx.id)
           if (settled?.status === 'completed') {
-            injected = true
+            if (params.returnPreamble) {
+              return { dispatch: settled, injected: true, preamble }
+            }
+            return { dispatch: settled, injected: true }
           } else {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const settled = db.getDispatchContextById(ctx.id)
if (settled?.status === 'completed') {
injected = true
} else {
db.failDispatch(ctx.id, err instanceof Error ? err.message : String(err))
throw err
}
const settled = db.getDispatchContextById(ctx.id)
if (settled?.status === 'completed') {
if (params.returnPreamble) {
return { dispatch: settled, injected: true, preamble }
}
return { dispatch: settled, injected: true }
} else {
db.failDispatch(ctx.id, err instanceof Error ? err.message : String(err))
throw err
}

This branch has not been deployed

No deployments
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.

2 participants