Skip to content

fix: use consistent token counting for truncation before/after display#11991

Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/consistent-truncation-token-counting
Draft

fix: use consistent token counting for truncation before/after display#11991
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/consistent-truncation-token-counting

Conversation

@roomote-v0
Copy link
Copy Markdown
Contributor

@roomote-v0 roomote-v0 bot commented Mar 25, 2026

Related GitHub Issue

Closes: #11990

Description

This PR attempts to address Issue #11990 where the truncation UI shows "after" token count larger than "before".

Root cause: In manageContext(), prevContextTokens (the "before" value) was calculated using API-reported token counts (tokensIn + tokensOut + lastMessageTokens), while newContextTokensAfterTruncation (the "after" value) was calculated using local token estimation (system prompt + message content). When API providers (e.g. OpenRouter) report different values than local estimation, the display became inconsistent -- "after" could appear larger than "before".

Fix: Both "before" and "after" values now use the same local token estimation method (estimateTokenCount via the provider's countTokens). The system prompt tokens are computed once and reused for both calculations. The API-reported value is still used for the threshold decision (whether to trigger truncation), but the display values are always consistent.

Key changes:

  • src/core/context-management/index.ts: Calculate localPrevContextTokens using the same method as newContextTokensAfterTruncation in the truncation path
  • src/core/context-management/__tests__/context-management.spec.ts: Updated tests to verify the consistency invariant (prevContextTokens >= newContextTokensAfterTruncation) instead of checking against API-reported values

Feedback and guidance are welcome.

Test Procedure

  • All 40 tests in context-management.spec.ts pass
  • All 33 tests in truncation.spec.ts and getApiMetrics.spec.ts pass
  • Lint and type checks pass across all packages
  • Tests verify the key invariant: prevContextTokens >= newContextTokensAfterTruncation

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue
  • Scope: Changes are focused on the truncation token counting inconsistency
  • Self-Review: Done
  • Testing: Updated existing tests to verify the consistency invariant
  • Documentation Impact: No documentation updates required
  • Contribution Guidelines: Reviewed

Documentation Updates

  • No documentation updates are required.

Additional Notes

The fix only affects the truncation path in manageContext(). The condensation path and non-action path still use the API-reported prevContextTokens value, which is correct for those cases since the condensation result also uses a different token counting approach that is already consistent with its "before" value.

Interactively review PR in Roo Code Cloud

The truncation UI showed "after" token count larger than "before" because
they used different counting methods: prevContextTokens used API-reported
values (tokensIn + tokensOut) while newContextTokensAfterTruncation used
local estimation (system prompt + message content). When providers report
different values than local estimation, the display became inconsistent.

Now both values use the same local estimation method, ensuring the "after"
count is always less than the "before" count.

Fixes #11990
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Strange context bloat and truncation

1 participant