diff --git a/.review/pr-8274 b/.review/pr-8274 new file mode 160000 index 0000000000..e46929b8d8 --- /dev/null +++ b/.review/pr-8274 @@ -0,0 +1 @@ +Subproject commit e46929b8d8add0cd3c412d69f8ac882c405a4ba9 diff --git a/src/core/condense/__tests__/condense.spec.ts b/src/core/condense/__tests__/condense.spec.ts index 5eb97b3e8a..396f9e56f2 100644 --- a/src/core/condense/__tests__/condense.spec.ts +++ b/src/core/condense/__tests__/condense.spec.ts @@ -3,6 +3,7 @@ import { Anthropic } from "@anthropic-ai/sdk" import type { ModelInfo } from "@roo-code/types" import { TelemetryService } from "@roo-code/telemetry" +import { vi } from "vitest" import { BaseProvider } from "../../../api/providers/base-provider" import { ApiMessage } from "../../task-persistence/apiMessages" @@ -10,7 +11,7 @@ import { summarizeConversation, getMessagesSinceLastSummary, N_MESSAGES_TO_KEEP // Create a mock ApiHandler for testing class MockApiHandler extends BaseProvider { - createMessage(): any { + createMessage(systemPrompt?: string, messages?: any[]): any { // Mock implementation for testing - returns an async iterable stream const mockStream = { async *[Symbol.asyncIterator]() { @@ -176,7 +177,7 @@ describe("Condense", () => { it("should handle empty summary from API gracefully", async () => { // Mock handler that returns empty summary class EmptyMockApiHandler extends MockApiHandler { - override createMessage(): any { + override createMessage(systemPrompt?: string, messages?: any[]): any { const mockStream = { async *[Symbol.asyncIterator]() { yield { type: "text", text: "" } @@ -204,6 +205,87 @@ describe("Condense", () => { expect(result.messages).toEqual(messages) expect(result.cost).toBeGreaterThan(0) }) + + it("should include the initial ask in the summarization input", async () => { + const initialAsk = "Please help me implement a new authentication system" + const messages: ApiMessage[] = [ + { role: "user", content: initialAsk }, + { role: "assistant", content: "I'll help you implement an authentication system" }, + { role: "user", content: "Let's start with JWT tokens" }, + { role: "assistant", content: "Setting up JWT authentication" }, + { role: "user", content: "Add refresh token support" }, + { role: "assistant", content: "Adding refresh token logic" }, + { role: "user", content: "Include rate limiting" }, + { role: "assistant", content: "Implementing rate limiting" }, + { role: "user", content: "Add tests" }, + ] + + // Create a spy to capture what's sent to createMessage + let capturedMessages: any[] = [] + class SpyApiHandler extends MockApiHandler { + override createMessage(systemPrompt?: string, messages?: any[]): any { + capturedMessages = messages || [] + return super.createMessage(systemPrompt, messages) + } + } + + const spyHandler = new SpyApiHandler() + await summarizeConversation(messages, spyHandler, "System prompt", taskId, 5000, false) + + // Verify the initial ask is included in the messages sent for summarization + expect(capturedMessages.length).toBeGreaterThan(0) + + // The first user message in the captured messages should be the initial ask + const firstUserMessage = capturedMessages.find((msg) => msg.role === "user") + expect(firstUserMessage).toBeDefined() + expect(firstUserMessage.content).toBe(initialAsk) + + // Verify all messages except the last N are included + const expectedMessagesToSummarize = messages.slice(0, -N_MESSAGES_TO_KEEP) + // The last message in capturedMessages is the summarization request, so we exclude it + const actualSummarizedMessages = capturedMessages.slice(0, -1) + + // Check that we have the right number of messages + expect(actualSummarizedMessages.length).toBe(expectedMessagesToSummarize.length) + + // Verify the content matches + for (let i = 0; i < expectedMessagesToSummarize.length; i++) { + expect(actualSummarizedMessages[i].role).toBe(expectedMessagesToSummarize[i].role) + expect(actualSummarizedMessages[i].content).toBe(expectedMessagesToSummarize[i].content) + } + }) + + it("should include initial ask with slash command in summarization", async () => { + const slashCommand = "/prr #456 - Implement feature X" + const messages: ApiMessage[] = [ + { role: "user", content: slashCommand }, + { role: "assistant", content: "Working on PR #456" }, + { role: "user", content: "Add error handling" }, + { role: "assistant", content: "Adding error handling" }, + { role: "user", content: "Include logging" }, + { role: "assistant", content: "Adding logging" }, + { role: "user", content: "Write documentation" }, + { role: "assistant", content: "Writing docs" }, + { role: "user", content: "Final review" }, + ] + + // Spy on the API handler to verify what's being sent + let capturedMessages: any[] = [] + class SpyApiHandler extends MockApiHandler { + override createMessage(systemPrompt?: string, messages?: any[]): any { + capturedMessages = messages || [] + return super.createMessage(systemPrompt, messages) + } + } + + const spyHandler = new SpyApiHandler() + await summarizeConversation(messages, spyHandler, "System prompt", taskId, 5000, false) + + // Verify the slash command is in the summarization input + const firstMessage = capturedMessages[0] + expect(firstMessage.role).toBe("user") + expect(firstMessage.content).toBe(slashCommand) + }) }) describe("getMessagesSinceLastSummary", () => { diff --git a/src/core/condense/index.ts b/src/core/condense/index.ts index 166a8ba4ca..01561f58ed 100644 --- a/src/core/condense/index.ts +++ b/src/core/condense/index.ts @@ -15,25 +15,30 @@ const SUMMARY_PROMPT = `\ Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions. This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing with the conversation and supporting any continuing tasks. +IMPORTANT: Always capture and preserve the initial user request or task that started this conversation. This is critical for maintaining context when resuming work after condensation. + Your summary should be structured as follows: Context: The context to continue the conversation with. If applicable based on the current task, this should include: - 1. Previous Conversation: High level details about what was discussed throughout the entire conversation with the user. This should be written to allow someone to be able to follow the general overarching conversation flow. - 2. Current Work: Describe in detail what was being worked on prior to this request to summarize the conversation. Pay special attention to the more recent messages in the conversation. - 3. Key Technical Concepts: List all important technical concepts, technologies, coding conventions, and frameworks discussed, which might be relevant for continuing with this work. - 4. Relevant Files and Code: If applicable, enumerate specific files and code sections examined, modified, or created for the task continuation. Pay special attention to the most recent messages and changes. - 5. Problem Solving: Document problems solved thus far and any ongoing troubleshooting efforts. - 6. Pending Tasks and Next Steps: Outline all pending tasks that you have explicitly been asked to work on, as well as list the next steps you will take for all outstanding work, if applicable. Include code snippets where they add clarity. For any next steps, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no information loss in context between tasks. + 1. Initial Request: The original user request or task that started this conversation. This should be captured verbatim or as close to the original as possible to preserve the initial intent. + 2. Previous Conversation: High level details about what was discussed throughout the entire conversation with the user. This should be written to allow someone to be able to follow the general overarching conversation flow. + 3. Current Work: Describe in detail what was being worked on prior to this request to summarize the conversation. Pay special attention to the more recent messages in the conversation. + 4. Key Technical Concepts: List all important technical concepts, technologies, coding conventions, and frameworks discussed, which might be relevant for continuing with this work. + 5. Relevant Files and Code: If applicable, enumerate specific files and code sections examined, modified, or created for the task continuation. Pay special attention to the most recent messages and changes. + 6. Problem Solving: Document problems solved thus far and any ongoing troubleshooting efforts. + 7. Pending Tasks and Next Steps: Outline all pending tasks that you have explicitly been asked to work on, as well as list the next steps you will take for all outstanding work, if applicable. Include code snippets where they add clarity. For any next steps, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no information loss in context between tasks. Example summary structure: -1. Previous Conversation: +1. Initial Request: + [The original user request that started this conversation] +2. Previous Conversation: [Detailed description] -2. Current Work: +3. Current Work: [Detailed description] -3. Key Technical Concepts: +4. Key Technical Concepts: - [Concept 1] - [Concept 2] - [...] -4. Relevant Files and Code: +5. Relevant Files and Code: - [File Name 1] - [Summary of why this file is important] - [Summary of the changes made to this file, if any] @@ -41,9 +46,9 @@ Example summary structure: - [File Name 2] - [Important Code Snippet] - [...] -5. Problem Solving: +6. Problem Solving: [Detailed description] -6. Pending Tasks and Next Steps: +7. Pending Tasks and Next Steps: - [Task 1 details & next steps] - [Task 2 details & next steps] - [...] @@ -103,8 +108,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 to preserve initial context + const messagesToSummarize = getMessagesSinceLastSummary(messages.slice(0, -N_MESSAGES_TO_KEEP)) if (messagesToSummarize.length <= 1) { const error = diff --git a/tmp/pr-8287-Roo-Code b/tmp/pr-8287-Roo-Code new file mode 160000 index 0000000000..88a473b017 --- /dev/null +++ b/tmp/pr-8287-Roo-Code @@ -0,0 +1 @@ +Subproject commit 88a473b017af37091c85ce3056e444e856f80d6e