Skip to content

Commit b9d1e28

Browse files
committed
fix: prevent consecutive user messages on streaming retry
When a streaming error occurs after a tool use, the retry logic was adding a duplicate user message to the API conversation history, causing 400 errors. The fix skips adding the user message on retry attempts since it was already added in the first attempt.
1 parent 8d14e90 commit b9d1e28

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/core/task/Task.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,8 +1867,13 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
18671867
// results.
18681868
const finalUserContent = [...parsedUserContent, { type: "text" as const, text: environmentDetails }]
18691869

1870-
await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
1871-
TelemetryService.instance.captureConversationMessage(this.taskId, "user")
1870+
// Only add user message to conversation history if this is NOT a retry attempt
1871+
// On retries, the user message was already added in the previous attempt
1872+
// This prevents consecutive user messages (including tool->user sequences) which cause 400 errors
1873+
if ((currentItem.retryAttempt ?? 0) === 0) {
1874+
await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
1875+
TelemetryService.instance.captureConversationMessage(this.taskId, "user")
1876+
}
18721877

18731878
// Since we sent off a placeholder api_req_started message to update the
18741879
// webview while waiting to actually start the API request (to load

0 commit comments

Comments
 (0)