-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: respect condensation percentage threshold when saving settings #6859
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -144,7 +144,17 @@ export async function truncateConversationIfNeeded({ | |
|
|
||
| if (autoCondenseContext) { | ||
| const contextPercent = (100 * prevContextTokens) / contextWindow | ||
| if (contextPercent >= effectiveThreshold || prevContextTokens > allowedTokens) { | ||
| // Check if we should trigger condensation based on percentage threshold | ||
| const shouldCondenseByPercentage = contextPercent >= effectiveThreshold | ||
| // Check if we're over the hard token limit | ||
| const isOverHardLimit = prevContextTokens > allowedTokens | ||
|
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. The variable naming here is mostly clear, but I'm wondering if |
||
|
|
||
| // Only trigger automatic condensation when the percentage threshold is met | ||
| // If we're over the hard limit but below percentage, we'll use sliding window instead | ||
| if (shouldCondenseByPercentage) { | ||
| console.log( | ||
| `[Condensation] Triggering automatic condensation: ${contextPercent.toFixed(1)}% >= ${effectiveThreshold}% threshold`, | ||
|
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. I notice we're adding several console.log statements here for debugging condensation decisions. While these are helpful for understanding the flow, should we consider using a proper logging framework or removing these before merging to avoid cluttering production logs? We could potentially use the existing logging infrastructure that's already in place. |
||
| ) | ||
| // Attempt to intelligently condense the context | ||
| const result = await summarizeConversation( | ||
| messages, | ||
|
|
@@ -159,14 +169,29 @@ export async function truncateConversationIfNeeded({ | |
| if (result.error) { | ||
| error = result.error | ||
| cost = result.cost | ||
| console.log(`[Condensation] Condensation failed: ${result.error}`) | ||
| // If condensation fails and we're over the hard limit, fall through to sliding window | ||
| } else { | ||
| console.log( | ||
| `[Condensation] Successfully condensed context from ${prevContextTokens} to ${result.newContextTokens} tokens`, | ||
| ) | ||
| return { ...result, prevContextTokens } | ||
| } | ||
| } else if (isOverHardLimit) { | ||
| // If we're over the hard limit but haven't reached the percentage threshold, | ||
| // log this condition - we'll use sliding window truncation below | ||
| console.log( | ||
| `[Condensation] Context tokens (${prevContextTokens}) exceed allowed (${allowedTokens}), but percentage (${contextPercent.toFixed(1)}%) < threshold (${effectiveThreshold}%). Will use sliding window truncation.`, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // Fall back to sliding window truncation if needed | ||
|
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. This comment says "This happens when we're over the hard token limit regardless of percentage" but that's not entirely accurate - this code path is also reached when condensation fails (see line 173 where we check for result.error). Could we update this comment to reflect both scenarios? |
||
| // This happens when we're over the hard token limit regardless of percentage | ||
| if (prevContextTokens > allowedTokens) { | ||
| console.log( | ||
| `[Condensation] Using sliding window truncation: ${prevContextTokens} tokens > ${allowedTokens} allowed`, | ||
| ) | ||
| const truncatedMessages = truncateConversation(messages, 0.5, taskId) | ||
| return { messages: truncatedMessages, prevContextTokens, summary: "", cost, error } | ||
| } | ||
|
|
||
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.
The test modifications look comprehensive! I'm wondering though - should we add a test case for when tokens are exactly at the percentage threshold (e.g., exactly 100% when threshold is 100%)? This would help ensure boundary conditions are properly handled.