fix: prevent consecutive user messages on streaming retry (#9249)

This commit is contained in:
Daniel 2025-11-13 22:31:26 -05:00 committed by GitHub
parent 38e3529c1e
commit d139eff9fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1768,6 +1768,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
userContent: Anthropic.Messages.ContentBlockParam[]
includeFileDetails: boolean
retryAttempt?: number
userMessageWasRemoved?: boolean // Track if user message was removed due to empty response
}
const stack: StackItem[] = [{ userContent, includeFileDetails, retryAttempt: 0 }]
@ -1868,8 +1869,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
// results.
const finalUserContent = [...parsedUserContent, { type: "text" as const, text: environmentDetails }]
await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
TelemetryService.instance.captureConversationMessage(this.taskId, "user")
// Only add user message to conversation history if:
// 1. This is the first attempt (retryAttempt === 0), OR
// 2. The message was removed in a previous iteration (userMessageWasRemoved === true)
// This prevents consecutive user messages while allowing re-add when needed
if ((currentItem.retryAttempt ?? 0) === 0 || currentItem.userMessageWasRemoved) {
await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
TelemetryService.instance.captureConversationMessage(this.taskId, "user")
}
// Since we sent off a placeholder api_req_started message to update the
// webview while waiting to actually start the API request (to load
@ -2553,10 +2560,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
}
// Push the same content back onto the stack to retry, incrementing the retry attempt counter
// Mark that user message was removed so it gets re-added on retry
stack.push({
userContent: currentUserContent,
includeFileDetails: false,
retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
userMessageWasRemoved: true,
})
// Continue to retry the request