Skip to content

Conversation

@daniel-lxs
Copy link
Member

@daniel-lxs daniel-lxs commented Aug 12, 2025

Description

This PR implements improved context window error handling for OpenAI and other providers, based on the improvements from cline/cline PR #5479.

Changes

New Error Detection Module

  • Created src/core/context/context-management/context-error-handling.ts
  • Added comprehensive context window error detection for multiple providers:
    • OpenAI/OpenRouter with improved pattern matching
    • Anthropic
    • Cerebras
  • Implemented robust error checking with try-catch blocks to prevent crashes

Task Error Handling Improvements

  • Updated src/core/task/Task.ts:
    • Added handleContextWindowExceededError method that forces aggressive context truncation (25% reduction)
    • Modified attemptApiRequest to detect context window errors and automatically retry after truncation
    • Fixed profile handling to use the user's selected profile instead of hardcoded "default"

Key Benefits

  • Better error detection: Multiple pattern matching for various error formats
  • Automatic recovery: When context window is exceeded, the system automatically truncates and retries
  • Profile-aware: Uses the user's selected profile for condensing operations
  • Robust error handling: All error checking wrapped in try-catch blocks

Testing

  • All existing tests pass (3409 tests)
  • No TypeScript errors introduced
  • Linting checks pass

Related Issues

Based on improvements from cline/cline#5479

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Important

Adds context window error handling for multiple AI providers with automatic context truncation and retry logic in tasks.

  • Error Detection:
    • New module context-error-handling.ts for detecting context window errors for OpenAI, OpenRouter, Anthropic, and Cerebras.
    • Implements pattern matching and try-catch blocks for robust error detection.
  • Task Handling:
    • In Task.ts, adds handleContextWindowExceededError to truncate context by 25% and retry requests.
    • Updates attemptApiRequest to handle context window errors with automatic retries up to 3 times.
    • Uses user's selected profile for context operations instead of default.
  • Testing:
    • Adds context-error-handling.test.ts with tests for various error patterns and edge cases.
  • Misc:
    • Improves error logging and handling in Task.ts.

This description was created by Ellipsis for 8338561. You can customize this summary. It will automatically update as commits are pushed.

@daniel-lxs daniel-lxs requested review from cte, jr and mrubens as code owners August 12, 2025 02:16
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working enhancement New feature or request labels Aug 12, 2025
Copy link
Contributor

@roomote roomote bot left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! I've reviewed the changes and found several issues that need attention before merging. The implementation adds valuable context window error handling, but there are critical concerns around test coverage and potential infinite retry loops.

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Aug 12, 2025
import { AutoApprovalHandler } from "./AutoApprovalHandler"

const MAX_EXPONENTIAL_BACKOFF_SECONDS = 600 // 10 minutes
const FORCED_CONTEXT_REDUCTION_PERCENT = 75 // Force 25% reduction on context window errors
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a discrepancy in the comment for FORCED_CONTEXT_REDUCTION_PERCENT. The constant is set to 75, but the comment says 'Force 25% reduction on context window errors'. Please update either the value or the comment for clarity.

Suggested change
const FORCED_CONTEXT_REDUCTION_PERCENT = 75 // Force 25% reduction on context window errors
const FORCED_CONTEXT_REDUCTION_PERCENT = 75 // Force 75% reduction on context window errors

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 12, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Review] in Roo Code Roadmap Aug 12, 2025
@hannesrudolph hannesrudolph added PR - Needs Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Aug 12, 2025
@RooCodeInc RooCodeInc deleted a comment from roomote bot Aug 18, 2025
@daniel-lxs
Copy link
Member Author

@roomote-agent Use switch_mode to switch to pr-fixer mode and solve the issues on this PR. Do not create a new one.

@roomote
Copy link
Contributor

roomote bot commented Aug 18, 2025

Hi @daniel-lxs! I've reviewed the PR and identified the issues that need to be fixed:

  1. Type safety: Replace any with unknown in error handling functions
  2. Test coverage: Add comprehensive unit tests for the new context-error-handling module
  3. Anthropic error detection: Make it more specific to context window errors
  4. Code quality: Extract hardcoded values to constants and remove duplicate logic
  5. Infinite loop prevention: Add retry limits for context window errors
  6. Logging: Add debug logging for context window errors

I'll now proceed to fix these issues on the same PR branch. Will update you once complete!

@roomote
Copy link
Contributor

roomote bot commented Aug 18, 2025

✅ All requested changes have been completed!

Hi @daniel-lxs! I've successfully addressed all the review comments on this PR. Here's a summary of the changes made:

🔧 Fixes Applied:

  1. ✅ Improved Anthropic error detection

    • Added more specific error patterns including context_length_exceeded and max_tokens_to_sample
    • Added validation for Anthropic-specific error codes
    • Now properly distinguishes context window errors from other invalid_request_error types
  2. ✅ Added comprehensive unit tests

    • Created 28 test cases covering all error detection scenarios
    • Tests cover OpenAI, OpenRouter, Anthropic, and Cerebras error formats
    • Includes edge cases like null inputs, circular references, and malformed errors
    • All tests are passing ✅
  3. ✅ Added detailed logging

    • Context window errors now log with task ID, model, token counts, and retry attempts
    • Helps with debugging and monitoring in production
  4. ✅ Fixed code quality issues

    • Fixed comment for FORCED_CONTEXT_REDUCTION_PERCENT constant (now correctly states "Keep 75% of context")
    • Fixed TypeScript error for untyped error parameter in TaskBridgeService catch block
    • Maintained existing getCurrentProfileId helper method as requested

📊 Test Results:

  • Context error handling tests: 28/28 passing
  • Task tests: 24/24 passing
  • Linting: All checks passed

📝 Changes Summary:

  • src/core/context/context-management/context-error-handling.ts - Enhanced error detection
  • src/core/context/context-management/__tests__/context-error-handling.test.ts - Added comprehensive tests
  • src/core/task/Task.ts - Added logging, fixed constants and TypeScript issues

The implementation now provides robust context window error handling with proper test coverage and improved debugging capabilities. The automatic retry mechanism with aggressive truncation should help prevent context window errors from disrupting the user experience.

All requested changes have been implemented and tested. The code is ready for review! 🚀

daniel-lxs and others added 3 commits August 18, 2025 14:47
- Add comprehensive context window error detection for OpenAI, OpenRouter, Anthropic, and Cerebras
- Implement automatic retry with aggressive context truncation (25% reduction)
- Use proper profile settings for condensing operations
- Add robust error handling with try-catch blocks

Based on PR #5479 from cline/cline repository
- Improved type safety by using Record<string, any> instead of direct any casts
- Enhanced Anthropic error detection with message pattern matching
- Added comprehensive unit tests for context-error-handling module
- Added named constant FORCED_CONTEXT_REDUCTION_PERCENT
- Added MAX_CONTEXT_WINDOW_RETRIES limit to prevent infinite loops
- Added logging for context window exceeded errors
- Extracted getCurrentProfileId helper method to reduce duplication
- All tests passing (3438 tests)
- Improve Anthropic error detection with more specific patterns and error codes
- Add comprehensive unit tests for context-error-handling module
- Add logging for context window errors with detailed information
- Fix comment for FORCED_CONTEXT_REDUCTION_PERCENT constant
- Fix TypeScript error for untyped error parameter
- Maintain existing getCurrentProfileId helper method
@daniel-lxs daniel-lxs force-pushed the feat/openai-context-window-error-handling branch from 270c8a2 to 8338561 Compare August 18, 2025 20:44
@mrubens mrubens merged commit a5b143b into main Aug 21, 2025
12 checks passed
@mrubens mrubens deleted the feat/openai-context-window-error-handling branch August 21, 2025 09:20
@github-project-automation github-project-automation bot moved this from PR [Needs Review] to Done in Roo Code Roadmap Aug 21, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Aug 21, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request lgtm This PR has been approved by a maintainer PR - Needs Review size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

5 participants