diff --git a/src/core/condense/__tests__/index.spec.ts b/src/core/condense/__tests__/index.spec.ts index d86b500f90..6a03298aa6 100644 --- a/src/core/condense/__tests__/index.spec.ts +++ b/src/core/condense/__tests__/index.spec.ts @@ -283,6 +283,32 @@ describe("summarizeConversation", () => { const mockCallArgs = (maybeRemoveImageBlocks as Mock).mock.calls[0][0] as any[] expect(mockCallArgs[mockCallArgs.length - 1]).toEqual(expectedFinalMessage) }) + it("should include the original first user message in summarization input", async () => { + const messages: ApiMessage[] = [ + { role: "user", content: "Initial ask", ts: 1 }, + { role: "assistant", content: "Ack", ts: 2 }, + { role: "user", content: "Follow-up", ts: 3 }, + { role: "assistant", content: "Response", ts: 4 }, + { role: "user", content: "More", ts: 5 }, + { role: "assistant", content: "Later", ts: 6 }, + { role: "user", content: "Newest", ts: 7 }, + ] + + await summarizeConversation(messages, mockApiHandler, defaultSystemPrompt, taskId, DEFAULT_PREV_CONTEXT_TOKENS) + + const mockCallArgs = (maybeRemoveImageBlocks as Mock).mock.calls[0][0] as any[] + + // Expect the original first user message to be present in the messages sent to the summarizer + const hasInitialAsk = mockCallArgs.some( + (m) => + m.role === "user" && + (typeof m.content === "string" + ? m.content === "Initial ask" + : Array.isArray(m.content) && + m.content.some((b: any) => b.type === "text" && b.text === "Initial ask")), + ) + expect(hasInitialAsk).toBe(true) + }) it("should calculate newContextTokens correctly with systemPrompt", async () => { const messages: ApiMessage[] = [ diff --git a/src/core/condense/index.ts b/src/core/condense/index.ts index 166a8ba4ca..86cfa7ab1e 100644 --- a/src/core/condense/index.ts +++ b/src/core/condense/index.ts @@ -103,8 +103,8 @@ export async function summarizeConversation( // Always preserve the first message (which may contain slash command content) const firstMessage = messages[0] - // Get messages to summarize, excluding the first message and last N messages - const messagesToSummarize = getMessagesSinceLastSummary(messages.slice(1, -N_MESSAGES_TO_KEEP)) + // Get messages to summarize, including the first message and excluding the last N messages + const messagesToSummarize = getMessagesSinceLastSummary(messages.slice(0, -N_MESSAGES_TO_KEEP)) if (messagesToSummarize.length <= 1) { const error =