Skip to content

Commit 3ee042d

Browse files
authored
fix(condense): summarize only the API-visible history (#2300)
* 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 * test(condense): cover prior summary and stored hidden messages
1 parent 7ecc667 commit 3ee042d

2 files changed

Lines changed: 90 additions & 2 deletions

File tree

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,93 @@ 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+
expect(result.messages[1]).toMatchObject({
1176+
content: "Hidden assistant message",
1177+
truncationParent: truncationId,
1178+
})
1179+
expect(result.messages[2]).toMatchObject({
1180+
content: "Hidden tool result",
1181+
truncationParent: truncationId,
1182+
})
1183+
})
1184+
1185+
it("should build the summarization request from the effective history when a prior summary exists", async () => {
1186+
const condenseId = "condense-1"
1187+
const truncationId = "trunc-2"
1188+
const messages: ApiMessage[] = [
1189+
{ role: "user", content: "Pre-summary task", ts: 1, condenseParent: condenseId },
1190+
{ role: "assistant", content: "Pre-summary assistant", ts: 2, condenseParent: condenseId },
1191+
{ role: "user", content: "## Conversation Summary\nPrior summary", ts: 3, isSummary: true, condenseId },
1192+
{ role: "assistant", content: "Hidden post-summary assistant", ts: 4, truncationParent: truncationId },
1193+
{
1194+
role: "user",
1195+
content: "[Sliding window truncation: 1 messages hidden to reduce context]",
1196+
ts: 5,
1197+
isTruncationMarker: true,
1198+
truncationId,
1199+
},
1200+
{ role: "user", content: "Visible post-summary user message", ts: 6 },
1201+
]
1202+
1203+
await summarizeConversation({
1204+
messages,
1205+
apiHandler: mockApiHandler,
1206+
systemPrompt: defaultSystemPrompt,
1207+
taskId,
1208+
})
1209+
1210+
const requestMessages = vi.mocked(mockApiHandler.createMessage).mock.calls[0][1]
1211+
const requestContents = requestMessages.map(({ content }) =>
1212+
typeof content === "string" ? content : JSON.stringify(content),
1213+
)
1214+
expect(requestContents).toEqual([
1215+
"## Conversation Summary\nPrior summary",
1216+
"[Sliding window truncation: 1 messages hidden to reduce context]",
1217+
"Visible post-summary user message",
1218+
expect.stringContaining("detailed summary of the conversation"),
1219+
])
1220+
})
11341221
})
11351222

11361223
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)