Skip to content

fix: address review findings for context window management#730

Closed
2witstudios wants to merge 1 commit intoclaude/fix-context-length-errors-WFBfzfrom
ppg/cdx-708-fixes
Closed

fix: address review findings for context window management#730
2witstudios wants to merge 1 commit intoclaude/fix-context-length-errors-WFBfzfrom
ppg/cdx-708-fixes

Conversation

@2witstudios
Copy link
Owner

Summary

Fixes issues identified by both the Claude Code review and Codex review of PR #708.

  • Fix wasTruncated message inversion — the !wasTruncated branch incorrectly told users "Older messages have been trimmed" when no trimming occurred
  • Guard empty messages array — return 413 error when truncation leaves zero messages instead of sending empty context to streamText
  • Fix estimateMessageTokens — match actual Vercel AI SDK tool-{name} part format (input/output/state fields) instead of non-existent tool-call/tool-result types
  • Reconcile context window sizes — extract MODEL_CONTEXT_WINDOWS into shared model-context-windows.ts as single source of truth, fixing divergence (e.g. glm-5: 200k→202,752, minimax m2.5 via OpenRouter: 1M→204,800)
  • Fix isContextLengthError '413' matching — replace broad string check with specific HTTP status pattern (/\b(?:status|http|code|error)\s*413\b/i) to prevent false positives
  • Fix estimateTokens CJK detection — replace regex array allocation with efficient charCodeAt loop
  • Add 42 unit tests covering isContextLengthError, estimateTokens, estimateMessageTokens, getContextWindowSize, determineMessagesToInclude, getAIErrorMessage, isRateLimitError

Test plan

  • 24 tests pass in packages/lib/src/monitoring/__tests__/ai-context-calculator.test.ts
  • 18 tests pass in apps/web/src/lib/ai/shared/__tests__/error-messages.test.ts
  • Verify context truncation works end-to-end with a long conversation
  • Verify 413 error response is shown correctly in UI for context-length errors

🤖 Generated with Claude Code

- Fix wasTruncated message inversion: non-truncated error no longer
  claims messages were trimmed
- Guard empty messages: return 413 when truncation leaves zero messages
  instead of sending empty context to streamText
- Fix estimateMessageTokens: match actual Vercel AI SDK tool-{name}
  part format (input/output/state) instead of legacy tool-call/tool-result
- Reconcile context windows: extract MODEL_CONTEXT_WINDOWS into shared
  model-context-windows.ts as single source of truth (fixes glm-5
  200000→202752, minimax m2.5 via OpenRouter 1M→204800 divergence)
- Fix isContextLengthError: replace broad '413' string match with
  specific HTTP status pattern to prevent false positives
- Fix estimateTokens: replace regex array allocation with efficient
  charCode loop for CJK detection
- Add 42 unit tests for isContextLengthError, estimateTokens,
  estimateMessageTokens, getContextWindowSize, determineMessagesToInclude

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 26, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ppg/cdx-708-fixes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant