Skip to content

persist sessionId on failure so a failed run stays resumable#47

Open
matt-wright86 wants to merge 1 commit into
mainfrom
fix/resumable-failed-runs
Open

persist sessionId on failure so a failed run stays resumable#47
matt-wright86 wants to merge 1 commit into
mainfrom
fix/resumable-failed-runs

Conversation

@matt-wright86

Copy link
Copy Markdown
Collaborator

Summary

runTaskAgent persisted the engine sessionId only after the success/failure
branch, so a failed run lost its session — a retry had to rebuild context from
scratch. Persist it before the branch (failed runs stay resumable), and
return telemetry on the failure path too.

Type of change

  • Bug fix (non-breaking)
  • Test coverage

Checklist

  • Raw go/npx were not used
  • make gates — N/A: orchestrator-v2 uses its own vp toolchain
  • vp run -r build clean · vp run -r test green
  • Regression test added (engine fails → sessionId persisted + telemetry returned)
  • Commit message short, lowercase, imperative
  • No force-push to main
  • Self-reviewed (/simplify)

Notes for reviewers

First of the orchestrator-v2 hardening pieces (from the earlier experiment branch)
re-expressed onto the work-item model as focused PRs. Next up: I3 (callback
resilience), Route A (telemetry as a terminal outcome), I1 (capability routing).

🤖 Generated with Claude Code

runTaskAgent persisted the engine sessionId only after the success/failure branch, so a failed run lost its session and a retry had to rebuild context from scratch. Persist it before the branch, and return telemetry on the failure path too.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved retry handling for task runs that fail after starting, so session state is saved and can be resumed later.
    • Failure responses now include available execution telemetry, making it easier to diagnose unsuccessful runs.

Walkthrough

The runTaskAgent function now persists engineResult.sessionId to state immediately after engine execution, before checking success or failure, rather than only after success. The failure-path return now includes telemetry from engineResult.stats. A corresponding test was added.

Changes

Session persistence and telemetry on failure

Layer / File(s) Summary
Persist sessionId early and attach telemetry on failure
orchestrator-v2/packages/agent-3-task/src/run-task-agent.ts
ctx.setState now runs before the success/failure branch to persist engineResult.sessionId, and the failure return object now includes telemetry: engineResult.stats ?? undefined.
Test coverage for failure resumability
orchestrator-v2/packages/agent-3-task/src/run-task-agent.spec.ts
New test with failing_engine and valid_state_context asserts session_id_is_persisted and telemetry_is_returned in the outcome.

Possibly related PRs

  • devzeebo/bifrost#20: Related changes to engine execution and sessionId continuation flow that this PR's session persistence logic depends on.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: persisting sessionId on failure so retries remain resumable.
Description check ✅ Passed The description accurately explains the bug fix, telemetry change, and added regression test coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@orchestrator-v2/packages/agent-3-task/src/run-task-agent.ts`:
- Around line 51-58: The unguarded ctx.setState call in runTaskAgent can now
fail the whole function on both success and failure paths, so wrap the
persistence step in try/catch and keep returning a structured WorkItemResult if
state saving rejects. Use the existing runTaskAgent flow and the
engineResult.sessionId / ctx.setState block to locate the change, and make sure
any persistence error is handled without throwing past the function boundary.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b73d428d-7978-42eb-89b0-4759a9fc82f0

📥 Commits

Reviewing files that changed from the base of the PR and between a411816 and 3f30ef5.

📒 Files selected for processing (2)
  • orchestrator-v2/packages/agent-3-task/src/run-task-agent.spec.ts
  • orchestrator-v2/packages/agent-3-task/src/run-task-agent.ts

Comment on lines +51 to 58
// Persist the sessionId before branching on the outcome, so a failed run stays
// resumable — a retry can --resume instead of rebuilding context from scratch.
if (engineResult.sessionId !== undefined) {
await ctx.setState({
...workItem.state,
sessionId: engineResult.sessionId,
});
}

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.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Unhandled ctx.setState failure now impacts both success and failure paths.

This persistence call isn't wrapped in try/catch. If ctx.setState rejects, the function throws instead of returning a structured WorkItemResult, defeating the purpose of making failed runs resumable. Since this block now runs unconditionally (previously only on the success path), the blast radius of an unhandled rejection here has doubled.

🛡️ Suggested guard
   if (engineResult.sessionId !== undefined) {
-    await ctx.setState({
-      ...workItem.state,
-      sessionId: engineResult.sessionId,
-    });
+    try {
+      await ctx.setState({
+        ...workItem.state,
+        sessionId: engineResult.sessionId,
+      });
+    } catch {
+      // Swallow persistence errors so a failing setState doesn't crash the run;
+      // resumability is best-effort and shouldn't mask the underlying engine result.
+    }
   }
📝 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
// Persist the sessionId before branching on the outcome, so a failed run stays
// resumable — a retry can --resume instead of rebuilding context from scratch.
if (engineResult.sessionId !== undefined) {
await ctx.setState({
...workItem.state,
sessionId: engineResult.sessionId,
});
}
// Persist the sessionId before branching on the outcome, so a failed run stays
// resumable — a retry can --resume instead of rebuilding context from scratch.
if (engineResult.sessionId !== undefined) {
try {
await ctx.setState({
...workItem.state,
sessionId: engineResult.sessionId,
});
} catch {
// Swallow persistence errors so a failing setState doesn't crash the run;
// resumability is best-effort and shouldn't mask the underlying engine result.
}
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@orchestrator-v2/packages/agent-3-task/src/run-task-agent.ts` around lines 51
- 58, The unguarded ctx.setState call in runTaskAgent can now fail the whole
function on both success and failure paths, so wrap the persistence step in
try/catch and keep returning a structured WorkItemResult if state saving
rejects. Use the existing runTaskAgent flow and the engineResult.sessionId /
ctx.setState block to locate the change, and make sure any persistence error is
handled without throwing past the function boundary.

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