Skip to content
Closed
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
14 changes: 12 additions & 2 deletions src/core/sliding-window/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import { ANTHROPIC_DEFAULT_MAX_TOKENS } from "@roo-code/types"
*/
export const TOKEN_BUFFER_PERCENTAGE = 0.1

/**
* Safety buffer applied when approaching the hard context limit.
* Keep very small so we don't trigger premature condensing on large windows.
*/
const SAFETY_BUFFER_PERCENTAGE = 0.01 // 1% of total context window
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are these constants intentional? They're defined but never used in the implementation. Were they meant to be incorporated into the threshold calculation, or are they placeholders for future enhancements?

const MIN_SAFETY_BUFFER_TOKENS = 2048

/**
* Counts tokens for user content using the provider's token counting implementation.
*
Expand Down Expand Up @@ -118,10 +125,12 @@ export async function truncateConversationIfNeeded({
// Calculate total effective tokens (totalTokens never includes the last message)
const prevContextTokens = totalTokens + lastMessageTokens

// Calculate available tokens for conversation history
// Truncate if we're within TOKEN_BUFFER_PERCENTAGE of the context window
// Calculate effective capacity available for conversation history after reserving output tokens
const allowedTokens = contextWindow * (1 - TOKEN_BUFFER_PERCENTAGE) - reservedTokens
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consider extracting this calculation into a named function like calculateEffectiveTokenCapacity() for better readability and testability. This appears to be the core fix, so giving it a name would make the intent clearer:

Suggested change
const allowedTokens = contextWindow * (1 - TOKEN_BUFFER_PERCENTAGE) - reservedTokens
const allowedTokens = calculateEffectiveTokenCapacity(contextWindow, reservedTokens)


// Note: allowedTokens already includes a dynamic buffer and reserved output tokens
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment mentions keeping 'a single source of truth' but doesn't clarify what that source is or how it differs from the previous approach. Could we make this more explicit? Perhaps something like: 'allowedTokens is now the single threshold for both percentage and absolute checks'?

// Keep a single source of truth for truncation thresholds to align with tests and behavior.

// Determine the effective threshold to use
let effectiveThreshold = autoCondenseContextPercent
const profileThreshold = profileThresholds[currentProfileId]
Expand All @@ -143,6 +152,7 @@ export async function truncateConversationIfNeeded({
// If no specific threshold is found for the profile, fall back to global setting

if (autoCondenseContext) {
// Base percent on effective capacity so visualization and behavior are aligned
Copy link
Contributor Author

Choose a reason for hiding this comment

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

What does 'visualization' refer to here? Is this the UI progress bar, debug output, or something else? A bit more specificity would help future maintainers understand the alignment you're achieving.

const contextPercent = (100 * prevContextTokens) / contextWindow
if (contextPercent >= effectiveThreshold || prevContextTokens > allowedTokens) {
// Attempt to intelligently condense the context
Expand Down
Loading