Skip to content

Commit 848fd82

Browse files
committed
fix: prevent context truncation when condensing is disabled (threshold=100%)
- Modified truncateConversationIfNeeded to skip summarization when threshold is 100% - Prevent sliding window truncation when condensing is explicitly disabled - Added test to verify no truncation occurs when threshold is 100% - Updated existing tests to use appropriate thresholds Fixes #7811
1 parent 195f4eb commit 848fd82

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/core/sliding-window/__tests__/sliding-window.spec.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ describe("Sliding Window", () => {
577577
maxTokens: modelInfo.maxTokens,
578578
apiHandler: mockApiHandler,
579579
autoCondenseContext: true,
580-
autoCondenseContextPercent: 100,
580+
autoCondenseContextPercent: 50, // Use a threshold less than 100%
581581
systemPrompt: "System prompt",
582582
taskId,
583583
profileThresholds: {},
@@ -644,7 +644,7 @@ describe("Sliding Window", () => {
644644
maxTokens: modelInfo.maxTokens,
645645
apiHandler: mockApiHandler,
646646
autoCondenseContext: true,
647-
autoCondenseContextPercent: 100,
647+
autoCondenseContextPercent: 50, // Use a threshold less than 100%
648648
systemPrompt: "System prompt",
649649
taskId,
650650
profileThresholds: {},
@@ -821,6 +821,48 @@ describe("Sliding Window", () => {
821821
// Clean up
822822
summarizeSpy.mockRestore()
823823
})
824+
825+
it("should not truncate when autoCondenseContext is true and threshold is 100% even if tokens exceed allowedTokens", async () => {
826+
const modelInfo = createModelInfo(100000, 30000)
827+
const totalTokens = 75000 // This exceeds allowedTokens (60000) but should not truncate when disabled
828+
829+
const messagesWithSmallContent = [
830+
...messages.slice(0, -1),
831+
{ ...messages[messages.length - 1], content: "" },
832+
]
833+
834+
// Spy on summarizeConversation to ensure it's not called
835+
const summarizeSpy = vi.spyOn(condenseModule, "summarizeConversation")
836+
837+
const result = await truncateConversationIfNeeded({
838+
messages: messagesWithSmallContent,
839+
totalTokens,
840+
contextWindow: modelInfo.contextWindow,
841+
maxTokens: modelInfo.maxTokens,
842+
apiHandler: mockApiHandler,
843+
autoCondenseContext: true, // Enabled but with 100% threshold
844+
autoCondenseContextPercent: 100, // 100% threshold means never condense
845+
systemPrompt: "System prompt",
846+
taskId,
847+
profileThresholds: {},
848+
currentProfileId: "default",
849+
})
850+
851+
// Verify summarizeConversation was NOT called when threshold is 100%
852+
expect(summarizeSpy).not.toHaveBeenCalled()
853+
854+
// Should NOT truncate even though tokens exceed allowedTokens when threshold is 100%
855+
expect(result).toEqual({
856+
messages: messagesWithSmallContent,
857+
summary: "",
858+
cost: 0,
859+
prevContextTokens: totalTokens,
860+
error: undefined,
861+
})
862+
863+
// Clean up
864+
summarizeSpy.mockRestore()
865+
})
824866
})
825867

826868
/**

src/core/sliding-window/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export async function truncateConversationIfNeeded({
142142
}
143143
// If no specific threshold is found for the profile, fall back to global setting
144144

145-
if (autoCondenseContext) {
145+
if (autoCondenseContext && effectiveThreshold < 100) {
146146
const contextPercent = (100 * prevContextTokens) / contextWindow
147147
if (contextPercent >= effectiveThreshold || prevContextTokens > allowedTokens) {
148148
// Attempt to intelligently condense the context
@@ -166,7 +166,15 @@ export async function truncateConversationIfNeeded({
166166
}
167167

168168
// Fall back to sliding window truncation if needed
169+
// Exception: When context condensing is explicitly disabled (threshold = 100%), don't truncate
169170
if (prevContextTokens > allowedTokens) {
171+
// Check if condensing is explicitly disabled (threshold is 100% and autoCondenseContext is true)
172+
// This means the user has set the threshold to 100% to disable condensing
173+
if (autoCondenseContext && effectiveThreshold >= 100) {
174+
// Context condensing is explicitly disabled by user, don't truncate
175+
return { messages, summary: "", cost, prevContextTokens, error }
176+
}
177+
// Apply sliding window truncation in all other cases
170178
const truncatedMessages = truncateConversation(messages, 0.5, taskId)
171179
return { messages: truncatedMessages, prevContextTokens, summary: "", cost, error }
172180
}

0 commit comments

Comments
 (0)