-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix: Add skipContextCompression flag to prevent unwanted compression - Fixes #4430 #6896
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
shirishgone
wants to merge
2
commits into
RooCodeInc:main
from
shirishgone:fix/context-compression-skip-flag-4430
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
196 changes: 196 additions & 0 deletions
196
src/core/sliding-window/__tests__/context-compression-fix.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest" | ||
| import { truncateConversationIfNeeded } from "../index" | ||
| import { ApiHandler } from "../../../api" | ||
| import { ApiMessage } from "../../task-persistence/apiMessages" | ||
|
|
||
| // Mock dependencies | ||
| vi.mock("@roo-code/telemetry", () => ({ | ||
| TelemetryService: { | ||
| instance: { | ||
| captureSlidingWindowTruncation: vi.fn(), | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock("../../condense", () => ({ | ||
| MAX_CONDENSE_THRESHOLD: 100, | ||
| MIN_CONDENSE_THRESHOLD: 50, | ||
| summarizeConversation: vi.fn().mockResolvedValue({ | ||
| messages: [], | ||
| summary: "Test summary", | ||
| cost: 0.01, | ||
| newContextTokens: 100, | ||
| }), | ||
| })) | ||
|
|
||
| describe("Context Compression Fix for Issue #4430", () => { | ||
| let mockApiHandler: ApiHandler | ||
| let testMessages: ApiMessage[] | ||
| let mockSummarizeConversation: any | ||
|
|
||
| beforeEach(async () => { | ||
| // Reset all mocks before each test | ||
| vi.clearAllMocks() | ||
|
|
||
| // Get the mocked function | ||
| const { summarizeConversation } = await import("../../condense") | ||
| mockSummarizeConversation = summarizeConversation | ||
|
|
||
| mockApiHandler = { | ||
| countTokens: vi.fn().mockResolvedValue(1000), | ||
| } as any | ||
|
|
||
| testMessages = [ | ||
| { | ||
| role: "user", | ||
| content: [{ type: "text", text: "Test message 1" }], | ||
| ts: Date.now(), | ||
| }, | ||
| { | ||
| role: "assistant", | ||
| content: [{ type: "text", text: "Test response 1" }], | ||
| ts: Date.now(), | ||
| }, | ||
| ] as ApiMessage[] | ||
| }) | ||
|
|
||
| it("should skip context compression when skipContextCompression flag is true", async () => { | ||
| const result = await truncateConversationIfNeeded({ | ||
| messages: testMessages, | ||
| totalTokens: 8000, // High token count to trigger compression | ||
| contextWindow: 10000, | ||
| maxTokens: 1000, | ||
| apiHandler: mockApiHandler, | ||
| autoCondenseContext: true, | ||
| autoCondenseContextPercent: 50, // Low threshold to trigger compression | ||
| systemPrompt: "Test system prompt", | ||
| taskId: "test-task-id", | ||
| profileThresholds: {}, | ||
| currentProfileId: "default", | ||
| skipContextCompression: true, // This should prevent compression | ||
| }) | ||
|
|
||
| // Should return original messages without compression | ||
| expect(result.messages).toBe(testMessages) | ||
| expect(result.summary).toBe("") | ||
| expect(result.cost).toBe(0) | ||
| }) | ||
|
|
||
| it("should perform normal compression when skipContextCompression flag is false", async () => { | ||
| const result = await truncateConversationIfNeeded({ | ||
| messages: testMessages, | ||
| totalTokens: 8000, // High token count to trigger compression | ||
| contextWindow: 10000, | ||
| maxTokens: 1000, | ||
| apiHandler: mockApiHandler, | ||
| autoCondenseContext: true, | ||
| autoCondenseContextPercent: 50, // Low threshold to trigger compression | ||
| systemPrompt: "Test system prompt", | ||
| taskId: "test-task-id", | ||
| profileThresholds: {}, | ||
| currentProfileId: "default", | ||
| skipContextCompression: false, // Normal compression should occur | ||
| }) | ||
|
|
||
| // Should call summarizeConversation for compression | ||
| expect(mockSummarizeConversation).toHaveBeenCalled() | ||
| expect(result.summary).toBe("Test summary") | ||
| expect(result.cost).toBe(0.01) | ||
| }) | ||
|
|
||
| it("should not trigger compression when context is below threshold", async () => { | ||
| const result = await truncateConversationIfNeeded({ | ||
| messages: testMessages, | ||
| totalTokens: 1000, // Low token count, below threshold | ||
| contextWindow: 10000, | ||
| maxTokens: 1000, | ||
| apiHandler: mockApiHandler, | ||
| autoCondenseContext: true, | ||
| autoCondenseContextPercent: 80, // High threshold | ||
| systemPrompt: "Test system prompt", | ||
| taskId: "test-task-id", | ||
| profileThresholds: {}, | ||
| currentProfileId: "default", | ||
| skipContextCompression: false, | ||
| }) | ||
|
|
||
| // Should not call summarizeConversation | ||
| expect(mockSummarizeConversation).not.toHaveBeenCalled() | ||
| expect(result.messages).toBe(testMessages) | ||
| expect(result.summary).toBe("") | ||
| }) | ||
|
|
||
| it("should handle multi-file read scenario correctly", async () => { | ||
| // Simulate the scenario from issue #4430: | ||
| // - Multi-file read is enabled (maxConcurrentFileReads > 1) | ||
| // - Context usage is at 100% threshold | ||
| // - Settings save operation triggers compression check | ||
|
|
||
| const result = await truncateConversationIfNeeded({ | ||
| messages: testMessages, | ||
| totalTokens: 10000, // Exactly at 100% of context window | ||
| contextWindow: 10000, | ||
| maxTokens: 1000, | ||
| apiHandler: mockApiHandler, | ||
| autoCondenseContext: true, | ||
| autoCondenseContextPercent: 100, // 100% threshold as in the issue | ||
| systemPrompt: "Test system prompt", | ||
| taskId: "test-task-id", | ||
| profileThresholds: {}, | ||
| currentProfileId: "default", | ||
| skipContextCompression: true, // Skip flag set by settings save | ||
| }) | ||
|
|
||
| // Should skip compression despite being at threshold | ||
| expect(result.messages).toBe(testMessages) | ||
| expect(result.summary).toBe("") | ||
| expect(result.cost).toBe(0) | ||
| }) | ||
|
|
||
| it("should clear the skip flag after use to prevent side effects", async () => { | ||
| // First call with skip flag set to true | ||
| const firstResult = await truncateConversationIfNeeded({ | ||
| messages: testMessages, | ||
| totalTokens: 8000, // High token count to trigger compression | ||
| contextWindow: 10000, | ||
| maxTokens: 1000, | ||
| apiHandler: mockApiHandler, | ||
| autoCondenseContext: true, | ||
| autoCondenseContextPercent: 50, // Low threshold to trigger compression | ||
| systemPrompt: "Test system prompt", | ||
| taskId: "test-task-id", | ||
| profileThresholds: {}, | ||
| currentProfileId: "default", | ||
| skipContextCompression: true, // Skip compression on first call | ||
| }) | ||
|
|
||
| // Should skip compression | ||
| expect(firstResult.messages).toBe(testMessages) | ||
| expect(firstResult.summary).toBe("") | ||
| expect(mockSummarizeConversation).not.toHaveBeenCalled() | ||
|
|
||
| // Reset mock call count | ||
| vi.clearAllMocks() | ||
|
|
||
| // Second call without skip flag (simulating next API request) | ||
| const secondResult = await truncateConversationIfNeeded({ | ||
| messages: testMessages, | ||
| totalTokens: 8000, // Same high token count | ||
| contextWindow: 10000, | ||
| maxTokens: 1000, | ||
| apiHandler: mockApiHandler, | ||
| autoCondenseContext: true, | ||
| autoCondenseContextPercent: 50, // Same low threshold | ||
| systemPrompt: "Test system prompt", | ||
| taskId: "test-task-id", | ||
| profileThresholds: {}, | ||
| currentProfileId: "default", | ||
| skipContextCompression: false, // Normal compression should occur | ||
| }) | ||
|
|
||
| // Should perform compression this time | ||
| expect(mockSummarizeConversation).toHaveBeenCalled() | ||
| expect(secondResult.summary).toBe("Test summary") | ||
| expect(secondResult.cost).toBe(0.01) | ||
| }) | ||
| }) | ||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.