Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ describe("runTaskAgent", () => {
then: { engine_failure_returned },
});

test("persists sessionId and returns telemetry when the engine fails (resumable retry)", {
given: { failing_engine, valid_state_context },
when: { task_agent_run },
then: {
session_id_is_persisted,
telemetry_is_returned,
},
});

test("fails when engine throws", {
given: { throwing_engine, valid_state_context },
when: { task_agent_run },
Expand Down
17 changes: 10 additions & 7 deletions orchestrator-v2/packages/agent-3-task/src/run-task-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,23 @@ export async function runTaskAgent(
return { outcome: "failed", message };
}

if (!engineResult.success) {
return {
outcome: "failed",
message: engineResult.lastMessage ?? "Engine execution failed",
};
}

// 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,
});
}
Comment on lines +51 to 58

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.


if (!engineResult.success) {
return {
outcome: "failed",
message: engineResult.lastMessage ?? "Engine execution failed",
telemetry: engineResult.stats ?? undefined,
};
}

return {
outcome: "completed",
message: engineResult.lastMessage ?? undefined,
Expand Down