Skip to content

Commit d139eff

Browse files
authored
fix: prevent consecutive user messages on streaming retry (#9249)
1 parent 38e3529 commit d139eff

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/core/task/Task.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
17681768
userContent: Anthropic.Messages.ContentBlockParam[]
17691769
includeFileDetails: boolean
17701770
retryAttempt?: number
1771+
userMessageWasRemoved?: boolean // Track if user message was removed due to empty response
17711772
}
17721773

17731774
const stack: StackItem[] = [{ userContent, includeFileDetails, retryAttempt: 0 }]
@@ -1868,8 +1869,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
18681869
// results.
18691870
const finalUserContent = [...parsedUserContent, { type: "text" as const, text: environmentDetails }]
18701871

1871-
await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
1872-
TelemetryService.instance.captureConversationMessage(this.taskId, "user")
1872+
// Only add user message to conversation history if:
1873+
// 1. This is the first attempt (retryAttempt === 0), OR
1874+
// 2. The message was removed in a previous iteration (userMessageWasRemoved === true)
1875+
// This prevents consecutive user messages while allowing re-add when needed
1876+
if ((currentItem.retryAttempt ?? 0) === 0 || currentItem.userMessageWasRemoved) {
1877+
await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
1878+
TelemetryService.instance.captureConversationMessage(this.taskId, "user")
1879+
}
18731880

18741881
// Since we sent off a placeholder api_req_started message to update the
18751882
// webview while waiting to actually start the API request (to load
@@ -2553,10 +2560,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
25532560
}
25542561

25552562
// Push the same content back onto the stack to retry, incrementing the retry attempt counter
2563+
// Mark that user message was removed so it gets re-added on retry
25562564
stack.push({
25572565
userContent: currentUserContent,
25582566
includeFileDetails: false,
25592567
retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
2568+
userMessageWasRemoved: true,
25602569
})
25612570

25622571
// Continue to retry the request

0 commit comments

Comments
 (0)