Skip to content

feat: Add model auto-switch functionality for improved error handling#18383

Closed
freezed-corpse-143 wants to merge 1 commit intoanomalyco:devfrom
freezed-corpse-143:feature/model-auto-switch
Closed

feat: Add model auto-switch functionality for improved error handling#18383
freezed-corpse-143 wants to merge 1 commit intoanomalyco:devfrom
freezed-corpse-143:feature/model-auto-switch

Conversation

@freezed-corpse-143
Copy link
Copy Markdown

PR introduces automatic model switching capability to enhance the reliability and user experience of the opencode system. When a model encounters specific types of errors, the system will automatically switch to an alternative model, with opencode/zen as the final fallback.
Key Features

  • Automatic Model Fallback: When a model fails due to authentication errors or non-retryable API errors, the system automatically selects an alternative model from available providers
  • Intelligent Error Handling: The system distinguishes between different error types:
    • ✅ Triggers fallback: AuthError, non-retryable APIError
    • ✅ Does not trigger fallback: ContextOverflowError, retryable APIError
  • Graceful Degradation: If no alternative providers are available, the system falls back to the free opencode/zen model
  • User Notification: TUI displays informative notifications when model switching occurs
  • Comprehensive Testing: Full test coverage for both core functions and error scenarios
    Implementation Details
  1. src/provider/provider.ts: Added getFallbackModel() function that:
    • Excludes the failed model's provider from alternatives
    • Selects the first available alternative provider
    • Chooses the best model from that provider using existing sorting logic
    • Falls back to opencode/zen when no alternatives exist
  2. src/session/retry.ts: Added shouldFallbackToAlternativeModel() function that:
    • Evaluates error types to determine if fallback should be triggered
    • Returns true for AuthError and non-retryable APIError
    • Returns false for ContextOverflowError and retryable errors
  3. src/session/processor.ts: Integrated fallback logic into the session processor:
    • Checks for fallback eligibility after retry logic
    • Updates message metadata with the new model
    • Publishes ModelFallback event for TUI notifications
    • Continues processing with the fallback model
  4. src/session/index.ts: Added ModelFallback bus event definition with:
    • sessionID: The affected session
    • fromModel: Original model that failed
    • toModel: New model being used
    • reason: Error message that triggered the fallback
  5. Test Coverage: Added comprehensive tests in:
    • test/provider/provider.test.ts: Tests getFallbackModel with and without alternatives
    • test/session/retry.test.ts: Tests shouldFallbackToAlternativeModel with all error scenarios
      Usage
      The feature is enabled by default and requires no configuration changes. Users will see TUI notifications when model switching occurs, providing transparency about the automatic recovery process.
      Backward Compatibility
  • Fully backward compatible with existing sessions and configurations
  • No breaking changes to existing APIs
  • Integrates seamlessly with current error handling mechanisms
    Performance Impact
  • Minimal performance overhead (fallback logic only executes on errors)
  • Debug logs only output at DEBUG level
  • No impact on normal operation flow
    Fixes #model-auto-switch-feature

- Add getFallbackModel function to provider module for automatic model switching
- Add shouldFallbackToAlternativeModel function to determine when to switch models
- Integrate model fallback logic in session processor error handling
- Add ModelFallback event definition for TUI notifications
- Add comprehensive tests for both new functions
- Models switch on AuthError and non-retryable APIError, fallback to opencode/zen when no alternatives
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found one potentially related PR:

PR #13189: feat: add model fallback support with TTFT-based timeout
#13189

This PR appears related because it also implements model fallback functionality, though it uses a different approach (TTFT-based timeout vs. error-triggered auto-switching). You may want to review it to ensure there's no overlap or conflict between the two implementations, and to understand if the existing fallback mechanism should be enhanced or replaced by the auto-switch feature in PR #18383.

@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 20, 2026
@github-actions github-actions bot closed this Mar 20, 2026
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