Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions src/core/condense/__tests__/strip-reasoning.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
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", () => {
const messages = [
{ role: "user", content: "hello", ts: 1 },
{
role: "assistant",
ts: 2,
content: [
{ type: "reasoning", text: "long private thinking" },
{ 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: 2,
content: [{ type: "reasoning", encrypted_content: "opaque" }],
},
{ role: "user", content: "follow-up", ts: 3 },
] as unknown as ApiMessage[]

const result = stripReasoningForCondensing(messages)

expect(result.map((m) => m.role)).toEqual(["user", "assistant", "user"])
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 },
{ 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)
})
})

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 inputSnapshot = structuredClone(messages)

const result = await summarizeConversation({
messages,
apiHandler,
systemPrompt: "system",
taskId: "task-1",
})

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)

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"])
})
})
48 changes: 47 additions & 1 deletion src/core/condense/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,52 @@
}))
}

/**
* 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.
*
* 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
*/
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 ?? "")

Check warning on line 130 in src/core/condense/index.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/condense/index.ts:130: NoCoverage StringLiteral mutant (replacement: "Stryker was here!"). See the job summary for the complete list and resolution guidance.
}

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(isReasoningBlock)) {
result.push(message)
continue
}
const remaining = message.content.filter((block) => !isReasoningBlock(block))
const content = (
remaining.length > 0 ? remaining : [{ type: "text", text: "[Reasoning omitted]" }]
) as ApiMessage["content"]
result.push({ ...message, content })
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return result
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

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

Expand Down Expand Up @@ -306,7 +352,7 @@

// 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
Expand Down
Loading