-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: include initial ask in condense summarization to preserve context #8296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule pr-8274
added at
e46929
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
||
| 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]() { | ||
|
|
@@ -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") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Prefer asserting ordering explicitly: validate that |
||
| 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", () => { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule pr-8287-Roo-Code
added at
88a473
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Unused import
vifromvitest; remove to satisfy lint rules.