Skip to content
Closed
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
1 change: 1 addition & 0 deletions .review/pr-8274
Submodule pr-8274 added at e46929
86 changes: 84 additions & 2 deletions src/core/condense/__tests__/condense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import { Anthropic } from "@anthropic-ai/sdk"
import type { ModelInfo } from "@roo-code/types"
import { TelemetryService } from "@roo-code/telemetry"
import { vi } from "vitest"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Unused import vi from vitest; remove to satisfy lint rules.


import { BaseProvider } from "../../../api/providers/base-provider"
import { ApiMessage } from "../../task-persistence/apiMessages"
import { summarizeConversation, getMessagesSinceLastSummary, N_MESSAGES_TO_KEEP } from "../index"

// 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]() {
Expand Down Expand Up @@ -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: "" }
Expand Down Expand Up @@ -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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Prefer asserting ordering explicitly: validate that capturedMessages[0] is the initial ask to ensure it is first in the summarization input.

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", () => {
Expand Down
33 changes: 19 additions & 14 deletions src/core/condense/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,40 @@ 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]
- [Important Code Snippet]
- [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]
- [...]
Expand Down Expand Up @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions tmp/pr-8287-Roo-Code
Submodule pr-8287-Roo-Code added at 88a473
Loading