Skip to content

Commit 79bf036

Browse files
committed
fix: bound mid-stream API retries and surface failures after retry limit
1 parent cc9c0af commit 79bf036

2 files changed

Lines changed: 183 additions & 17 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ function queuedResponseForAsk(type: ClineAsk, text?: string): QueuedAskResolutio
172172

173173
const FORCED_CONTEXT_REDUCTION_PERCENT = 75 // Keep 75% of context (remove 25%) on context window errors
174174
const MAX_CONTEXT_WINDOW_RETRIES = 3 // Maximum retries for context window errors
175+
const MAX_MID_STREAM_RETRIES = 3 // Maximum automatic retries when a provider stream fails mid-stream
175176

176177
export interface TaskOptions extends CreateTaskOptions {
177178
provider: ClineProvider
@@ -3655,34 +3656,98 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
36553656
this.abortReason = cancelReason
36563657
await this.abortTask()
36573658
} else {
3659+
// Stream failed - retry with the same content, but only up to
3660+
// MAX_MID_STREAM_RETRIES automatic attempts. Every attempt
3661+
// re-bills the full input context, so retries must be both
3662+
// bounded and visible to the user.
3663+
const midStreamRetryAttempt = currentItem.retryAttempt ?? 0
3664+
3665+
if (midStreamRetryAttempt >= MAX_MID_STREAM_RETRIES) {
3666+
// Automatic retries exhausted - surface the failure instead of
3667+
// retrying (and re-billing the request) silently forever.
3668+
console.error(
3669+
`[Task#${this.taskId}.${this.instanceId}] Stream failed, automatic retry limit (${MAX_MID_STREAM_RETRIES}) reached: ${streamingFailedMessage}`,
3670+
)
3671+
3672+
const { response } = await this.ask(
3673+
"api_req_failed",
3674+
streamingFailedMessage ?? rawErrorMessage,
3675+
)
3676+
3677+
if (response === "yesButtonClicked") {
3678+
await this.say("api_req_retried")
3679+
3680+
// The user approved another round of retries, so reset the
3681+
// automatic retry budget. Remove the user message this request
3682+
// added first so it is not duplicated in history on retry.
3683+
let removedCurrentUserMessage = false
3684+
if (this.apiConversationHistory.length > 0) {
3685+
const lastMessage =
3686+
this.apiConversationHistory[this.apiConversationHistory.length - 1]
3687+
if (lastMessage.role === "user") {
3688+
this.apiConversationHistory.pop()
3689+
this.messageCounts.user--
3690+
removedCurrentUserMessage = true
3691+
}
3692+
}
3693+
3694+
stack.push({
3695+
userContent: currentUserContent,
3696+
includeFileDetails: false,
3697+
retryAttempt: 0,
3698+
userMessageWasRemoved: removedCurrentUserMessage,
3699+
})
3700+
3701+
// Continue to retry the request
3702+
continue
3703+
}
3704+
3705+
// User declined to retry - record the failure visibly and stop.
3706+
await this.say("error", streamingFailedMessage ?? rawErrorMessage)
3707+
3708+
// Synthetic assistant message recording the failure -- increment
3709+
// messageCounts.assistant to match, same as the normal
3710+
// assistant-message-saved path.
3711+
await this.addToApiConversationHistory({
3712+
role: "assistant",
3713+
content: [
3714+
{
3715+
type: "text",
3716+
text: "Failure: The API request failed mid-stream and the retry was declined.",
3717+
},
3718+
],
3719+
})
3720+
this.messageCounts.assistant++
3721+
3722+
return false
3723+
}
3724+
36583725
// Stream failed - log the error and retry with the same content
3659-
// The existing rate limiting will prevent rapid retries
36603726
console.error(
3661-
`[Task#${this.taskId}.${this.instanceId}] Stream failed, will retry: ${streamingFailedMessage}`,
3727+
`[Task#${this.taskId}.${this.instanceId}] Stream failed, will retry (attempt ${midStreamRetryAttempt + 1}/${MAX_MID_STREAM_RETRIES}): ${streamingFailedMessage}`,
36623728
)
36633729

3664-
// Apply exponential backoff similar to first-chunk errors when auto-resubmit is enabled
3665-
const stateForBackoff = await this.providerRef.deref()?.getState()
3666-
if (stateForBackoff?.autoApprovalEnabled) {
3667-
await this.backoffAndAnnounce(currentItem.retryAttempt ?? 0, error)
3730+
// Announce every automatic retry with the shared exponential
3731+
// backoff countdown (api_req_retry_delayed) so no retry - and its
3732+
// associated token cost - happens silently.
3733+
await this.backoffAndAnnounce(midStreamRetryAttempt, error)
36683734

3669-
// Check if task was aborted during the backoff
3670-
if (this.abort) {
3671-
console.log(
3672-
`[Task#${this.taskId}.${this.instanceId}] Task aborted during mid-stream retry backoff`,
3673-
)
3674-
// Abort the entire task
3675-
this.abortReason = "user_cancelled"
3676-
await this.abortTask()
3677-
break
3678-
}
3735+
// Check if task was aborted during the backoff
3736+
if (this.abort) {
3737+
console.log(
3738+
`[Task#${this.taskId}.${this.instanceId}] Task aborted during mid-stream retry backoff`,
3739+
)
3740+
// Abort the entire task
3741+
this.abortReason = "user_cancelled"
3742+
await this.abortTask()
3743+
break
36793744
}
36803745

36813746
// Push the same content back onto the stack to retry, incrementing the retry attempt counter
36823747
stack.push({
36833748
userContent: currentUserContent,
36843749
includeFileDetails: false,
3685-
retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
3750+
retryAttempt: midStreamRetryAttempt + 1,
36863751
})
36873752

36883753
// Continue to retry the request

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,107 @@ describe("Cline", () => {
647647
})
648648
})
649649

650+
describe("mid-stream retries", () => {
651+
function midStreamFailingRequest(error: Error) {
652+
return (async function* () {
653+
yield { type: "text", text: "partial response" } as ApiStreamChunk
654+
throw error
655+
})()
656+
}
657+
658+
async function createMidStreamRetryTask() {
659+
const task = new Task({
660+
provider: mockProvider,
661+
apiConfiguration: mockApiConfig,
662+
task: "test task",
663+
startTask: false,
664+
})
665+
const state = await mockProvider.getState()
666+
vi.spyOn(mockProvider, "getState").mockResolvedValue({
667+
...state,
668+
apiConfiguration: mockApiConfig,
669+
autoApprovalEnabled: false,
670+
})
671+
vi.spyOn(task.diffViewProvider, "reset").mockResolvedValue(undefined)
672+
return task
673+
}
674+
675+
it("stops auto-retrying and surfaces the failure after the mid-stream retry limit", async () => {
676+
const task = await createMidStreamRetryTask()
677+
const streamError = new Error("Overloaded")
678+
const askSpy = vi
679+
.spyOn(task, "ask")
680+
.mockResolvedValue({ response: "noButtonClicked" } satisfies TaskAskResult)
681+
const saySpy = vi.spyOn(task, "say")
682+
const attemptSpy = vi
683+
.spyOn(task, "attemptApiRequest")
684+
.mockImplementation(() => midStreamFailingRequest(streamError))
685+
686+
const result = await task.recursivelyMakeClineRequests([{ type: "text", text: "original user request" }])
687+
688+
// 1 initial attempt + MAX_MID_STREAM_RETRIES (3) automatic retries, then it stops
689+
// instead of looping (and re-billing the request) forever.
690+
expect(attemptSpy).toHaveBeenCalledTimes(4)
691+
692+
// Every automatic retry is announced through the shared backoff countdown so
693+
// no retry happens silently.
694+
const retryAnnouncements = saySpy.mock.calls.filter((call) => call[0] === "api_req_retry_delayed")
695+
expect(retryAnnouncements.length).toBeGreaterThan(0)
696+
697+
// Once the cap is exhausted the failure is surfaced to the user.
698+
expect(askSpy).toHaveBeenCalledTimes(1)
699+
expect(askSpy).toHaveBeenCalledWith("api_req_failed", expect.stringContaining("Overloaded"))
700+
expect(saySpy).toHaveBeenCalledWith("error", expect.stringContaining("Overloaded"))
701+
702+
expect(result).toBe(false)
703+
expect(task.apiConversationHistory).toMatchObject([
704+
{
705+
role: "user",
706+
content: expect.arrayContaining([expect.objectContaining({ text: "original user request" })]),
707+
},
708+
{ role: "assistant", content: [{ type: "text", text: expect.stringContaining("Failure") }] },
709+
])
710+
})
711+
712+
it("resets the retry budget when the user approves another retry round", async () => {
713+
const task = await createMidStreamRetryTask()
714+
const streamError = new Error("Overloaded")
715+
const askSpy = vi
716+
.spyOn(task, "ask")
717+
.mockResolvedValue({ response: "yesButtonClicked" } satisfies TaskAskResult)
718+
const saySpy = vi.spyOn(task, "say")
719+
720+
let callCount = 0
721+
const attemptSpy = vi.spyOn(task, "attemptApiRequest").mockImplementation(() => {
722+
callCount++
723+
if (callCount <= 4) {
724+
return midStreamFailingRequest(streamError)
725+
}
726+
if (callCount === 5) {
727+
return (async function* () {
728+
yield { type: "text", text: "recovered response" } as ApiStreamChunk
729+
})()
730+
}
731+
throw new Error("stop after recovered response")
732+
})
733+
734+
await task.recursivelyMakeClineRequests([{ type: "text", text: "original user request" }])
735+
736+
expect(askSpy).toHaveBeenCalledTimes(1)
737+
expect(saySpy).toHaveBeenCalledWith("api_req_retried")
738+
// 4 failed attempts (initial + 3 automatic retries), 1 user-approved retry that
739+
// succeeded, and 1 follow-up request for the no-tool-use continuation.
740+
expect(attemptSpy).toHaveBeenCalledTimes(6)
741+
742+
// The user-approved retry must not duplicate the user message in history.
743+
const userMessages = task.apiConversationHistory.filter((message) => message.role === "user")
744+
expect(userMessages).toHaveLength(2) // original request (re-added once) + no-tools-used follow-up
745+
expect(userMessages[0]).toMatchObject({
746+
content: expect.arrayContaining([expect.objectContaining({ text: "original user request" })]),
747+
})
748+
})
749+
})
750+
650751
describe("constructor", () => {
651752
it.each([{ apiConfigName: "parent-local-profile" }, { apiConfigName: undefined }])(
652753
"uses an explicit delegated-child context without shared state or startup persistence",

0 commit comments

Comments
 (0)