From 371334f8f9a6530c1c32da51128644be37612a35 Mon Sep 17 00:00:00 2001 From: Will Sheers Date: Mon, 9 Nov 2026 20:41:41 +1100 Subject: [PATCH 1/3] fix(condense): strip reasoning blocks from condense requests The normal request path drops plain-text reasoning blocks from history (buildCleanConversationHistory), but summarizeConversation sent the raw history including them. For thinking models, plain-text reasoning can be a large share of stored history, so the condense request exceeded the context window that the normal request fits in, and condensing failed repeatedly with "request exceeds the context window". Filter reasoning blocks and standalone reasoning items before summarizing. Messages left empty by the filter are dropped; messages without reasoning are passed through unchanged. Refs #1823 Co-Authored-By: Claude Haiku 5.5 --- .../__tests__/strip-reasoning.spec.ts | 54 +++++++++++++++++++ src/core/condense/index.ts | 41 +++++++++++++- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/core/condense/__tests__/strip-reasoning.spec.ts diff --git a/src/core/condense/__tests__/strip-reasoning.spec.ts b/src/core/condense/__tests__/strip-reasoning.spec.ts new file mode 100644 index 0000000000..d0b92ed4a7 --- /dev/null +++ b/src/core/condense/__tests__/strip-reasoning.spec.ts @@ -0,0 +1,54 @@ +import { describe, it, expect } from "vitest" +import { stripReasoningForCondensing } from "../index" +import type { ApiMessage } from "../../task-persistence/apiMessages" + +describe("stripReasoningForCondensing", () => { + it("removes plain-text and encrypted reasoning blocks but keeps other content", () => { + const messages = [ + { role: "user", content: "hello", ts: 1 }, + { + role: "assistant", + ts: 2, + content: [ + { type: "reasoning", text: "long private thinking" }, + { type: "text", text: "answer" }, + ], + }, + { + role: "assistant", + ts: 3, + content: [{ type: "reasoning", encrypted_content: "opaque" }], + }, + ] as unknown as ApiMessage[] + + const result = stripReasoningForCondensing(messages) + + expect(result).toHaveLength(2) + expect(result[0].content).toBe("hello") + expect(result[1].content).toEqual([{ type: "text", text: "answer" }]) + }) + + it("drops standalone reasoning items", () => { + const messages = [ + { role: "user", content: "question", ts: 1 }, + { type: "reasoning", text: "thinking", ts: 2 }, + ] as unknown as ApiMessage[] + + const result = stripReasoningForCondensing(messages) + + expect(result).toHaveLength(1) + expect(result[0].content).toBe("question") + }) + + it("leaves messages without reasoning unchanged", () => { + const message = { + role: "assistant", + ts: 1, + content: [{ type: "tool_use", id: "t1", name: "read_file", input: {} }], + } as unknown as ApiMessage + + const result = stripReasoningForCondensing([message]) + + expect(result[0]).toBe(message) + }) +}) diff --git a/src/core/condense/index.ts b/src/core/condense/index.ts index d3415c9c8c..0ce402b864 100644 --- a/src/core/condense/index.ts +++ b/src/core/condense/index.ts @@ -108,6 +108,45 @@ export function transformMessagesForCondensing< })) } +/** + * Removes model reasoning blocks from messages before they are sent for summarization. + * + * Reasoning is not needed to summarize the conversation, and plain-text reasoning from + * thinking models can account for a large share of stored history. The normal request path + * drops plain-text reasoning as well (buildCleanConversationHistory in Task.ts); without this + * filter the condense request can exceed the context window that the normal request fits in. + * + * Messages whose content becomes empty after filtering are dropped. + * + * @param messages - The messages to filter + * @returns The messages without reasoning blocks + */ +export function stripReasoningForCondensing(messages: ApiMessage[]): ApiMessage[] { + const result: ApiMessage[] = [] + for (const message of messages) { + // Standalone reasoning items carry no conversational content for the summary. + if ((message as { type?: string }).type === "reasoning") { + continue + } + if (!Array.isArray(message.content)) { + result.push(message) + continue + } + if (!message.content.some((block) => (block as { type?: string }).type === "reasoning")) { + result.push(message) + continue + } + const content = message.content.filter( + (block) => (block as { type?: string }).type !== "reasoning", + ) as ApiMessage["content"] + if (Array.isArray(content) && content.length === 0) { + continue + } + result.push({ ...message, content }) + } + return result +} + export const MIN_CONDENSE_THRESHOLD = 5 // Minimum percentage of context window to trigger condensing export const MAX_CONDENSE_THRESHOLD = 100 // Maximum percentage of context window to trigger condensing @@ -306,7 +345,7 @@ export async function summarizeConversation(options: SummarizeConversationOption // Inject synthetic tool_results for orphan tool_calls to prevent API rejections // (e.g., when user triggers condense after receiving attempt_completion but before responding) - const messagesWithToolResults = injectSyntheticToolResults(messagesToSummarize) + const messagesWithToolResults = injectSyntheticToolResults(stripReasoningForCondensing(messagesToSummarize)) // Transform tool_use and tool_result blocks to text representations. // This is necessary because some providers (like Bedrock via LiteLLM) require the `tools` parameter From d9f97a491ac3629a92f124e13eca71643d1734f6 Mon Sep 17 00:00:00 2001 From: Will Sheers Date: Mon, 9 Nov 2026 23:42:10 +1100 Subject: [PATCH 2/3] fix(condense): keep reasoning-only turns and test the request path Address review feedback on the reasoning filter: - A message whose content is only reasoning is kept with a short placeholder instead of being dropped, so user and assistant turns still alternate for providers such as Bedrock that reject adjacent user messages. - Add a summarizeConversation test that captures the request sent to createMessage and checks that reasoning text is absent while tool use remains. It fails when the filter is removed from the request path. Co-Authored-By: Claude Haiku 5.5 --- .../__tests__/strip-reasoning.spec.ts | 89 +++++++++++++++++-- src/core/condense/index.ts | 13 +-- 2 files changed, 90 insertions(+), 12 deletions(-) diff --git a/src/core/condense/__tests__/strip-reasoning.spec.ts b/src/core/condense/__tests__/strip-reasoning.spec.ts index d0b92ed4a7..ad1489d862 100644 --- a/src/core/condense/__tests__/strip-reasoning.spec.ts +++ b/src/core/condense/__tests__/strip-reasoning.spec.ts @@ -1,6 +1,19 @@ -import { describe, it, expect } from "vitest" -import { stripReasoningForCondensing } from "../index" +import { describe, it, expect, vi } from "vitest" +import { stripReasoningForCondensing, summarizeConversation } from "../index" import type { ApiMessage } from "../../task-persistence/apiMessages" +import type { ApiHandler } from "../../../api" + +vi.mock("../../../api/transform/image-cleaning", () => ({ + maybeRemoveImageBlocks: vi.fn((messages: ApiMessage[]) => [...messages]), +})) + +vi.mock("@roo-code/telemetry", () => ({ + TelemetryService: { + instance: { + captureContextCondensed: vi.fn(), + }, + }, +})) describe("stripReasoningForCondensing", () => { it("removes plain-text and encrypted reasoning blocks but keeps other content", () => { @@ -14,18 +27,30 @@ describe("stripReasoningForCondensing", () => { { type: "text", text: "answer" }, ], }, + ] as unknown as ApiMessage[] + + const result = stripReasoningForCondensing(messages) + + expect(result).toHaveLength(2) + expect(result[0].content).toBe("hello") + expect(result[1].content).toEqual([{ type: "text", text: "answer" }]) + }) + + it("keeps a reasoning-only assistant turn with a placeholder so turns still alternate", () => { + const messages = [ + { role: "user", content: "question", ts: 1 }, { role: "assistant", - ts: 3, + ts: 2, content: [{ type: "reasoning", encrypted_content: "opaque" }], }, + { role: "user", content: "follow-up", ts: 3 }, ] as unknown as ApiMessage[] const result = stripReasoningForCondensing(messages) - expect(result).toHaveLength(2) - expect(result[0].content).toBe("hello") - expect(result[1].content).toEqual([{ type: "text", text: "answer" }]) + expect(result.map((m) => m.role)).toEqual(["user", "assistant", "user"]) + expect(result[1].content).toEqual([{ type: "text", text: "[Reasoning omitted]" }]) }) it("drops standalone reasoning items", () => { @@ -52,3 +77,55 @@ describe("stripReasoningForCondensing", () => { expect(result[0]).toBe(message) }) }) + +describe("summarizeConversation reasoning handling", () => { + it("sends the summarization request without reasoning and with tool use preserved", async () => { + const createMessage = vi.fn((_systemPrompt: string, _messages: unknown[]) => + (async function* () { + yield { type: "text" as const, text: "summary" } + })(), + ) + const apiHandler = { + createMessage, + countTokens: vi.fn(() => Promise.resolve(100)), + getModel: vi.fn(() => ({ id: "test-model", info: { contextWindow: 8000, maxTokens: 4000 } })), + } as unknown as ApiHandler + + const messages = [ + { role: "user", content: "Read the config", ts: 1 }, + { + role: "assistant", + ts: 2, + content: [ + { type: "reasoning", text: "private chain of thought that must not be sent" }, + { type: "tool_use", id: "t1", name: "read_file", input: { path: "config.json" } }, + ], + }, + { + role: "user", + ts: 3, + content: [{ type: "tool_result", tool_use_id: "t1", content: "{}" }], + }, + { role: "assistant", ts: 4, content: [{ type: "reasoning", text: "more thinking" }] }, + { role: "user", ts: 5, content: "Now summarize" }, + ] as unknown as ApiMessage[] + + const result = await summarizeConversation({ + messages, + apiHandler, + systemPrompt: "system", + taskId: "task-1", + }) + + expect(result.error).toBeUndefined() + expect(createMessage).toHaveBeenCalledTimes(1) + const requestMessages = createMessage.mock.calls[0][1] as Array<{ role: string; content: unknown }> + const serialized = JSON.stringify(requestMessages) + + expect(serialized).not.toContain("private chain of thought") + expect(serialized).not.toContain("more thinking") + expect(serialized).toContain("read_file") + expect(serialized).toContain("config.json") + expect(requestMessages.map((m) => m.role)).toEqual(["user", "assistant", "user", "assistant", "user", "user"]) + }) +}) diff --git a/src/core/condense/index.ts b/src/core/condense/index.ts index 0ce402b864..8ad5f042a2 100644 --- a/src/core/condense/index.ts +++ b/src/core/condense/index.ts @@ -116,7 +116,10 @@ export function transformMessagesForCondensing< * drops plain-text reasoning as well (buildCleanConversationHistory in Task.ts); without this * filter the condense request can exceed the context window that the normal request fits in. * - * Messages whose content becomes empty after filtering are dropped. + * Standalone reasoning items are dropped, as the normal request path skips them. A message + * whose content is only reasoning keeps its turn with a short placeholder, so user and + * assistant turns still alternate for providers such as Bedrock that reject adjacent user + * messages. * * @param messages - The messages to filter * @returns The messages without reasoning blocks @@ -136,12 +139,10 @@ export function stripReasoningForCondensing(messages: ApiMessage[]): ApiMessage[ result.push(message) continue } - const content = message.content.filter( - (block) => (block as { type?: string }).type !== "reasoning", + const remaining = message.content.filter((block) => (block as { type?: string }).type !== "reasoning") + const content = ( + remaining.length > 0 ? remaining : [{ type: "text", text: "[Reasoning omitted]" }] ) as ApiMessage["content"] - if (Array.isArray(content) && content.length === 0) { - continue - } result.push({ ...message, content }) } return result From 410bd40b59640777724ddb6ac1c285df66d399de Mon Sep 17 00:00:00 2001 From: Will Sheers Date: Tue, 17 Nov 2026 06:55:57 +1100 Subject: [PATCH 3/3] fix(condense): strip thinking blocks and cover stored history - Also remove Anthropic-style thinking and redacted_thinking blocks from the condense request, not only reasoning blocks. - Assert that the stored history keeps its reasoning blocks and that the caller's input is not mutated. Co-Authored-By: Claude Haiku 5.5 --- .../__tests__/strip-reasoning.spec.ts | 32 +++++++++++++++++++ src/core/condense/index.ts | 10 ++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/core/condense/__tests__/strip-reasoning.spec.ts b/src/core/condense/__tests__/strip-reasoning.spec.ts index ad1489d862..ffbdef4cf2 100644 --- a/src/core/condense/__tests__/strip-reasoning.spec.ts +++ b/src/core/condense/__tests__/strip-reasoning.spec.ts @@ -53,6 +53,32 @@ describe("stripReasoningForCondensing", () => { expect(result[1].content).toEqual([{ type: "text", text: "[Reasoning omitted]" }]) }) + it("removes Anthropic-style thinking and redacted_thinking blocks", () => { + const messages = [ + { role: "user", content: "question", ts: 1 }, + { + role: "assistant", + ts: 2, + content: [ + { type: "thinking", thinking: "thoughts", signature: "sig" }, + { type: "redacted_thinking", data: "opaque" }, + { type: "text", text: "answer" }, + ], + }, + { + role: "assistant", + ts: 3, + content: [{ type: "redacted_thinking", data: "opaque-only" }], + }, + ] as unknown as ApiMessage[] + + const result = stripReasoningForCondensing(messages) + + expect(result).toHaveLength(3) + expect(result[1].content).toEqual([{ type: "text", text: "answer" }]) + expect(result[2].content).toEqual([{ type: "text", text: "[Reasoning omitted]" }]) + }) + it("drops standalone reasoning items", () => { const messages = [ { role: "user", content: "question", ts: 1 }, @@ -110,6 +136,8 @@ describe("summarizeConversation reasoning handling", () => { { role: "user", ts: 5, content: "Now summarize" }, ] as unknown as ApiMessage[] + const inputSnapshot = structuredClone(messages) + const result = await summarizeConversation({ messages, apiHandler, @@ -118,6 +146,10 @@ describe("summarizeConversation reasoning handling", () => { }) expect(result.error).toBeUndefined() + // The stored history keeps its reasoning; only the request copy is filtered. + expect(JSON.stringify(result.messages)).toContain("private chain of thought that must not be sent") + // The caller's input is not mutated. + expect(messages).toEqual(inputSnapshot) expect(createMessage).toHaveBeenCalledTimes(1) const requestMessages = createMessage.mock.calls[0][1] as Array<{ role: string; content: unknown }> const serialized = JSON.stringify(requestMessages) diff --git a/src/core/condense/index.ts b/src/core/condense/index.ts index 8ad5f042a2..fb5e643c30 100644 --- a/src/core/condense/index.ts +++ b/src/core/condense/index.ts @@ -124,6 +124,12 @@ export function transformMessagesForCondensing< * @param messages - The messages to filter * @returns The messages without reasoning blocks */ +const REASONING_BLOCK_TYPES = new Set(["reasoning", "thinking", "redacted_thinking"]) + +function isReasoningBlock(block: unknown): boolean { + return REASONING_BLOCK_TYPES.has((block as { type?: string }).type ?? "") +} + export function stripReasoningForCondensing(messages: ApiMessage[]): ApiMessage[] { const result: ApiMessage[] = [] for (const message of messages) { @@ -135,11 +141,11 @@ export function stripReasoningForCondensing(messages: ApiMessage[]): ApiMessage[ result.push(message) continue } - if (!message.content.some((block) => (block as { type?: string }).type === "reasoning")) { + if (!message.content.some(isReasoningBlock)) { result.push(message) continue } - const remaining = message.content.filter((block) => (block as { type?: string }).type !== "reasoning") + const remaining = message.content.filter((block) => !isReasoningBlock(block)) const content = ( remaining.length > 0 ? remaining : [{ type: "text", text: "[Reasoning omitted]" }] ) as ApiMessage["content"]