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
41 changes: 41 additions & 0 deletions src/core/condense/__tests__/condense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,47 @@ Line 2
}
})

it("should not send messages hidden by sliding-window truncation to the summarizer", async () => {
const truncationId = "trunc-1"
const messages: ApiMessage[] = [
{ role: "user", content: "Hidden first", truncationParent: truncationId },
{ role: "assistant", content: "Hidden second", truncationParent: truncationId },
{ role: "assistant", content: "Truncation marker", isTruncationMarker: true, truncationId },
{ role: "user", content: "Visible third" },
{ role: "assistant", content: "Visible fourth" },
{ role: "user", content: "Visible fifth" },
]
const original = structuredClone(messages)
const sentMessages: unknown[] = []
const createMessageSpy = vi
.spyOn(mockApiHandler, "createMessage")
.mockImplementation((...args: unknown[]) => {
sentMessages.push(args[1])
return (async function* () {
yield { type: "text", text: "Mock summary" }
})()
})

const result = await summarizeConversation({
messages,
apiHandler: mockApiHandler,
systemPrompt: "System prompt",
taskId,
isAutomaticTrigger: false,
})

const sentText = JSON.stringify(sentMessages[0])
expect(sentText).not.toContain("Hidden first")
expect(sentText).not.toContain("Hidden second")
expect(sentText).toContain("Visible third")

// Complete stored history is preserved (non-destructive) and the input is not mutated
expect(result.messages.filter((msg) => !msg.isSummary)).toHaveLength(messages.length)
expect(messages).toEqual(original)

createMessageSpy.mockRestore()
})

it("should preserve <command> blocks in the summary", async () => {
const messages: ApiMessage[] = [
{
Expand Down
6 changes: 4 additions & 2 deletions src/core/condense/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ export async function summarizeConversation(options: SummarizeConversationOption

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

// Get messages to summarize (all messages since the last summary, if any)
const messagesToSummarize = getMessagesSinceLastSummary(messages)
// Summarize the effective history (what normal requests send): excludes messages already hidden
// by sliding-window truncation or a previous condense, and starts at the last summary if any.
// The stored history in `messages` is left intact and tagged below.
const messagesToSummarize = getEffectiveApiHistory(messages)

if (messagesToSummarize.length <= 1) {
const error =
Expand Down
Loading