@@ -172,6 +172,7 @@ function queuedResponseForAsk(type: ClineAsk, text?: string): QueuedAskResolutio
172172
173173const FORCED_CONTEXT_REDUCTION_PERCENT = 75 // Keep 75% of context (remove 25%) on context window errors
174174const 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
176177export 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
0 commit comments