Skip to content

fix(orchestration): fail direct dispatches on agent exit - #9001

Open
tianrking wants to merge 2 commits into
stablyai:mainfrom
tianrking:agent/fail-direct-dispatch-exits
Open

tianrking wants to merge 2 commits into
stablyai:mainfrom
tianrking:agent/fail-direct-dispatch-exits

Conversation

@tianrking

@tianrking tianrking commented Jul 16, 2026 •

Copy link
Copy Markdown

Summary

  • mark a task as failed when a directly dispatched worker exits and no coordinator run exists
  • preserve the exit reason on the task instead of leaving it indistinguishable from an untouched ready task
  • add a runtime regression test for the direct-dispatch path

Root cause

failDispatch correctly returns a task to ready for coordinator-managed retries. A direct orca orchestration dispatch has no coordinator loop to consume that retry, so an agent exit could leave work silently stranded in ready forever.

Impact

Coordinator-managed dispatches retain their existing retry and escalation behavior. Directly dispatched tasks now become visible failures that can be deliberately retried by the operator.

Validation

  • node_modules/.bin/vitest.cmd run --config config/vitest.config.ts src/main/runtime/orca-runtime.test.ts --testNamePattern 'marks a direct dispatch failed'
  • node_modules/.bin/oxfmt.cmd --check src/main/runtime/orca-runtime.ts src/main/runtime/orca-runtime.test.ts
  • node_modules/.bin/oxlint.cmd src/main/runtime/orca-runtime.ts src/main/runtime/orca-runtime.test.ts

The full runtime test file passed 710 tests; 4 unrelated tests assume a POSIX /tmp path and fail on this Windows host before exercising this change.

Fixes #8984

ELI5

A directly dispatched orchestration worker that crashed left the task looking like it was still ready. Those tasks are now marked failed with the exit reason so work doesn’t sit forever.

@brennanb2025

Copy link
Copy Markdown
Contributor

Taking a look!

@brennanb2025

Copy link
Copy Markdown
Contributor

Thanks for the clear diagnosis — I pushed a follow-up that keeps the intended behavior while making the dispatch/task failure transition atomic. Direct-dispatch worker exits now terminally fail the task with the exit reason, while coordinator-owned exits remain retryable and still escalate; I also corrected the lifecycle comment and added database/runtime coverage for both paths. The focused and full orchestration/runtime suites, Node typecheck, formatting, lint, and max-lines ratchet all pass.

@brennanb2025
brennanb2025 force-pushed the agent/fail-direct-dispatch-exits branch from ca1dbf9 to 9197ad5 Compare July 21, 2026 00:58
@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Dispatch failure handling now accepts an explicit task requeue policy and applies dispatch and task updates transactionally. PTY exit handling retrieves the active coordinator run before failing a dispatch, requeues tasks when appropriate, and escalates coordinator-owned failures. Tests cover worker and coordinator PTY exits and the non-requeue failure path.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: failing direct dispatches on agent exit.
Description check ✅ Passed The description covers summary, root cause, impact, and validation, though several template sections are omitted.
Linked Issues check ✅ Passed The changes propagate direct-dispatch failures to the task and preserve the exit reason, satisfying #8984's main requirement.
Out of Scope Changes check ✅ Passed The PR stays focused on dispatch-failure handling and regression tests, with no clear unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

🧹 Nitpick comments (1)
src/main/runtime/orchestration/db.ts (1)

834-856: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider using db.transaction() for robust error propagation.

The manual BEGIN IMMEDIATE / COMMIT / ROLLBACK block achieves the atomicity goal. However, if an error naturally aborts the SQLite transaction before the catch block executes, calling this.db.exec('ROLLBACK') will throw a new "cannot rollback - no transaction is active" error. This masks the original cause, making production crashes harder to diagnose.

You can safely sidestep this by wrapping the updates in better-sqlite3's native .transaction() wrapper and invoking it with .immediate(). It handles savepoints, auto-rollbacks, and original error propagation reliably.

♻️ Proposed refactor
-    // Why: the dispatch and its task are one lifecycle transition; committing
-    // only one row would leave the ledger contradictory after an interruption.
-    this.db.exec('BEGIN IMMEDIATE')
-    try {
+    // Why: the dispatch and its task are one lifecycle transition; committing
+    // only one row would leave the ledger contradictory after an interruption.
+    const updateLedger = this.db.transaction(() => {
       this.db
         .prepare(
           'UPDATE dispatch_contexts SET status = ?, failure_count = ?, last_failure = ? WHERE id = ?'
         )
         .run(newStatus, newFailureCount, error, ctxId)
 
       const shouldRetry = options.requeueTask !== false && newStatus !== 'circuit_broken'
       if (shouldRetry) {
         // Why: 'pending' would strand a retry because dependency promotion only
         // runs when a dependency completes; this task is already unblocked.
         this.db.prepare("UPDATE tasks SET status = 'ready' WHERE id = ?").run(ctx.task_id)
       } else {
         this.updateTaskStatus(ctx.task_id, 'failed', error)
       }
-      this.db.exec('COMMIT')
-    } catch (cause) {
-      this.db.exec('ROLLBACK')
-      throw cause
-    }
+    })
+
+    updateLedger.immediate()

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 28cf248d-dac0-43b2-b1bf-18f690bcdf5d

📥 Commits

Reviewing files that changed from the base of the PR and between cf02cc4 and ca1dbf9602232357b66f321f1679f3edb2d2bfdb.

📒 Files selected for processing (4)
  • src/main/runtime/orca-runtime.test.ts
  • src/main/runtime/orca-runtime.ts
  • src/main/runtime/orchestration/db.ts
  • src/main/runtime/orchestration/dispatch-failure-policy.test.ts

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

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: A failed dispatch never propagates to the task — dead agents leave tasks 'ready' forever and work is silently dropped

3 participants