Skip to content

Commit 191ccd8

Browse files
committed
fix(condense): summarize only the API-visible history
Condense selected messages from the raw stored history. Sliding window truncation and a prior condense hide messages from normal API requests. The old selection brought those hidden messages back into the summarization request. The request then grew past the model context window, and condense failed with a context size error. summarizeConversation now selects getMessagesSinceLastSummary( getEffectiveApiHistory(messages)). This matches what Task.ts sends for normal requests. The full stored history stays intact, so a rewind can still restore hidden messages. Add a regression test. It asserts the exact request content and the preserved stored history. Fixes #1342
1 parent f8aa881 commit 191ccd8

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎src/core/condense/__tests__/index.spec.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,48 @@ describe("summarizeConversation", () => {
11311131
expect(lastMessage.isSummary).toBe(true)
11321132
expect(lastMessage.role).toBe("user")
11331133
})
1134+
1135+
it("should exclude messages hidden by sliding-window truncation from the summarization request", async () => {
1136+
const truncationId = "trunc-1"
1137+
const messages: ApiMessage[] = [
1138+
{ role: "user", content: "Initial task", ts: 1 },
1139+
{ role: "assistant", content: "Hidden assistant message", ts: 2, truncationParent: truncationId },
1140+
{ role: "user", content: "Hidden tool result", ts: 3, truncationParent: truncationId },
1141+
{
1142+
role: "user",
1143+
content: "[Sliding window truncation: 2 messages hidden to reduce context]",
1144+
ts: 4,
1145+
isTruncationMarker: true,
1146+
truncationId,
1147+
},
1148+
{ role: "assistant", content: "Visible assistant message", ts: 5 },
1149+
{ role: "user", content: "Visible user message", ts: 6 },
1150+
]
1151+
1152+
const result = await summarizeConversation({
1153+
messages,
1154+
apiHandler: mockApiHandler,
1155+
systemPrompt: defaultSystemPrompt,
1156+
taskId,
1157+
})
1158+
1159+
// Exact wire selection: first + truncation marker + visible post-truncation messages +
1160+
// final condense instruction. Hidden messages must not be resurrected; visible ones kept.
1161+
const requestMessages = vi.mocked(mockApiHandler.createMessage).mock.calls[0][1]
1162+
const requestContents = requestMessages.map(({ content }) =>
1163+
typeof content === "string" ? content : JSON.stringify(content),
1164+
)
1165+
expect(requestContents).toEqual([
1166+
"Initial task",
1167+
"[Sliding window truncation: 2 messages hidden to reduce context]",
1168+
"Visible assistant message",
1169+
"Visible user message",
1170+
expect.stringContaining("detailed summary of the conversation"),
1171+
])
1172+
1173+
// Non-destructive: the full stored history (hidden messages included) is preserved for rewind.
1174+
expect(result.messages).toHaveLength(messages.length + 1)
1175+
})
11341176
})
11351177

11361178
describe("summarizeConversation with custom settings", () => {

‎src/core/condense/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ export async function summarizeConversation(options: SummarizeConversationOption
275275

276276
const response: SummarizeResponse = { messages, cost: 0, summary: "" }
277277

278-
// Get messages to summarize (all messages since the last summary, if any)
279-
const messagesToSummarize = getMessagesSinceLastSummary(messages)
278+
// Get effective history to summarize (all messages since the last summary, if any)
279+
const effectiveHistory = getEffectiveApiHistory(messages)
280+
const messagesToSummarize = getMessagesSinceLastSummary(effectiveHistory)
280281

281282
if (messagesToSummarize.length <= 1) {
282283
const error =

0 commit comments

Comments
 (0)