Skip to content

Commit d631aa6

Browse files
authored
fix(task): auto-retry on empty assistant response (#9076) (#9083)
1 parent bf04849 commit d631aa6

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

src/core/task/Task.ts

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,15 +2429,69 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
24292429
// If there's no assistant_responses, that means we got no text
24302430
// or tool_use content blocks from API which we should assume is
24312431
// an error.
2432-
await this.say(
2433-
"error",
2434-
"Unexpected API Response: The language model did not provide any assistant messages. This may indicate an issue with the API or the model's output.",
2435-
)
24362432

2437-
await this.addToApiConversationHistory({
2438-
role: "assistant",
2439-
content: [{ type: "text", text: "Failure: I did not provide a response." }],
2440-
})
2433+
// Check if we should auto-retry or prompt the user
2434+
const state = await this.providerRef.deref()?.getState()
2435+
if (state?.autoApprovalEnabled && state?.alwaysApproveResubmit) {
2436+
// Auto-retry with backoff - don't persist failure message when retrying
2437+
const errorMsg =
2438+
"Unexpected API Response: The language model did not provide any assistant messages. This may indicate an issue with the API or the model's output."
2439+
2440+
await this.backoffAndAnnounce(
2441+
currentItem.retryAttempt ?? 0,
2442+
new Error("Empty assistant response"),
2443+
errorMsg,
2444+
)
2445+
2446+
// Check if task was aborted during the backoff
2447+
if (this.abort) {
2448+
console.log(
2449+
`[Task#${this.taskId}.${this.instanceId}] Task aborted during empty-assistant retry backoff`,
2450+
)
2451+
break
2452+
}
2453+
2454+
// Push the same content back onto the stack to retry, incrementing the retry attempt counter
2455+
stack.push({
2456+
userContent: currentUserContent,
2457+
includeFileDetails: false,
2458+
retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
2459+
})
2460+
2461+
// Continue to retry the request
2462+
continue
2463+
} else {
2464+
// Prompt the user for retry decision
2465+
const { response } = await this.ask(
2466+
"api_req_failed",
2467+
"The model returned no assistant messages. This may indicate an issue with the API or the model's output.",
2468+
)
2469+
2470+
if (response === "yesButtonClicked") {
2471+
await this.say("api_req_retried")
2472+
2473+
// Push the same content back to retry
2474+
stack.push({
2475+
userContent: currentUserContent,
2476+
includeFileDetails: false,
2477+
retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
2478+
})
2479+
2480+
// Continue to retry the request
2481+
continue
2482+
} else {
2483+
// User declined to retry - persist error and failure message
2484+
await this.say(
2485+
"error",
2486+
"Unexpected API Response: The language model did not provide any assistant messages. This may indicate an issue with the API or the model's output.",
2487+
)
2488+
2489+
await this.addToApiConversationHistory({
2490+
role: "assistant",
2491+
content: [{ type: "text", text: "Failure: I did not provide a response." }],
2492+
})
2493+
}
2494+
}
24412495
}
24422496

24432497
// If we reach here without continuing, return false (will always be false for now)

0 commit comments

Comments
 (0)