From a28d32661f820bad7e33505b9534e66ddb6dbb04 Mon Sep 17 00:00:00 2001 From: daniel-lxs Date: Mon, 29 Dec 2025 14:40:14 -0500 Subject: [PATCH] fix: flush pending tool results before condensing context Fixes ROO-329: Race condition in condenseContext causing tool_use/tool_result protocol violation When a user triggered manual context condensation while a tool was executing, the tool_use block was already saved to apiConversationHistory but the corresponding tool_result was still pending in userMessageContent. The condensation operated on incomplete history, causing the API to reject with a protocol violation error. This fix adds flushPendingToolResultsToHistory() at the start of condenseContext() to ensure tool_use/tool_result pairs are complete before any condensation operation. --- src/core/task/Task.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/task/Task.ts b/src/core/task/Task.ts index 1bdbb55ba94..a588d7db1b1 100644 --- a/src/core/task/Task.ts +++ b/src/core/task/Task.ts @@ -1424,6 +1424,10 @@ export class Task extends EventEmitter implements TaskLike { } public async condenseContext(): Promise { + // CRITICAL: Flush any pending tool results before condensing + // to ensure tool_use/tool_result pairs are complete in history + await this.flushPendingToolResultsToHistory() + const systemPrompt = await this.getSystemPrompt() // Get condensing configuration