Skip to content

fix(routing): auto-map respects explicit [[models]] entries#293

Merged
Destynova2 merged 1 commit intomainfrom
fix/automap-respects-explicit-models
Apr 26, 2026
Merged

fix(routing): auto-map respects explicit [[models]] entries#293
Destynova2 merged 1 commit intomainfrom
fix/automap-respects-explicit-models

Conversation

@Destynova2
Copy link
Copy Markdown
Contributor

Summary

Router::route step 3 (auto-mapping) was rewriting request.model to router.default for any name matching auto_map_regexeven when the user had an explicit [[models]] virtual entry with that name. The virtual entry's fallback chain was therefore bypassed entirely.

The bug surfaces in production with errors like:

Provider API error: 400 - {"error":{"message":"default-model is not a valid model ID","code":400},"user_id":"user_..."}

Repro

  1. Client sends claude-sonnet-4-6.
  2. Auto-map regex ^claude- matches → request.model rewritten to default-model.
  3. The user's explicit [[models]] name = "claude-sonnet-4-6" with fallbacks (anthropic native → OR anthropic/claude-sonnet-4.6deepseek-v4-flash) is never consulted.
  4. default-model chain runs; on exhaustion try_direct_provider_lookup forwards model = "default-model" to a pass-through provider (OpenRouter), which 400s.

This is the failure mode I hit while debugging Anthropic 429 fallback chains: when the primary anthropic/claude-sonnet-4-6 mapping returned 429, the fallback to openrouter/anthropic/claude-sonnet-4.6 should have succeeded (and does succeed when called directly), but auto-map prevented it from ever being tried.

Fix

let has_explicit_virtual = self.config.models.iter().any(|m| m.name == request.model);
if !has_explicit_virtual {
    request.model.clone_from(&self.config.router.default);
}

The check is regex-agnostic: it covers every auto_map_regex configuration (claude, gpt, gemini, mixed, custom) — no provider-specific code path needed.

Auto-map continues to rewrite the model when no explicit virtual exists, preserving the original "catch-all rewrite for unconfigured models" intent.

Other routing steps audited

Step Mutation pattern Same bug?
1 WebSearch decision.model_name = ... direct No
2 Background decision.model_name = ... direct No
3 Auto-map request.model = ... then falls through Yes — fixed here
4 Subagent decision.model_name = ... direct No
5 PromptRule decision.model_name = ... direct No
6 Think decision.model_name = ... direct No

Auto-map is the only step that mutates request.model and falls through; the other steps either return early or set decision.model_name without touching request.model. No other leak path exists.

Tests

Adds two regression tests:

  • test_auto_map_skips_explicit_virtual_model — confirms the new behaviour.
  • test_auto_map_still_rewrites_unmapped_claude — counter-test confirming the original catch-all path is intact.

Existing tests still pass: test_auto_map_claude_models, test_auto_map_custom_regex, test_no_auto_map_non_matching, test_auto_map_routes_to_default.

Test plan

  • cargo nextest run -E 'test(auto_map)' — 9/9 passing
  • cargo fmt --all -- --check clean
  • cargo clippy --all-features -- -D warnings clean
  • CI: full suite

🤖 Generated with Claude Code

`Router::route` step 3 (auto-mapping) was rewriting `request.model` to
`router.default` for any name matching `auto_map_regex` — even when the
user had an explicit `[[models]]` virtual entry with that name. The
virtual entry's fallback chain was therefore bypassed entirely; in
practice the virtual name `default-model` then leaked to pass-through
providers downstream, producing errors like:

  Provider API error: 400 - {"error":{"message":"default-model is not
  a valid model ID","code":400},"user_id":"user_..."}

The error surfaces specifically when:

  1. Client sends `claude-sonnet-4-6`.
  2. Auto-map regex `^claude-` matches → `request.model` rewritten to
     `default-model`.
  3. The user's explicit `[[models]] name = "claude-sonnet-4-6"` with
     fallbacks (anthropic native → OR `anthropic/claude-sonnet-4.6` →
     `deepseek-v4-flash`) is never consulted.
  4. `default-model` chain runs; on exhaustion `try_direct_provider_lookup`
     forwards `model = "default-model"` to a pass-through provider
     (OpenRouter), which 400s.

Fix: skip the auto-map rewrite when `config.models` already contains an
entry whose name equals `request.model`. The check is regex-agnostic, so
it covers every auto_map_regex the user might configure (claude, gpt,
gemini, mixed, etc.) — there is no provider-specific code path here.

Auto-map continues to rewrite the model when no explicit virtual exists,
preserving the original "catch-all rewrite for unconfigured models"
intent that `test_auto_map_claude_models` and `test_auto_map_custom_regex`
exercise.

Adds two regression tests:
- `test_auto_map_skips_explicit_virtual_model` — the new behaviour.
- `test_auto_map_still_rewrites_unmapped_claude` — counter-test confirming
  the original catch-all path is intact.

Tests: 9/9 auto-map tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Destynova2 Destynova2 enabled auto-merge April 26, 2026 18:40
@Destynova2 Destynova2 merged commit dbaf81e into main Apr 26, 2026
42 checks passed
@Destynova2 Destynova2 deleted the fix/automap-respects-explicit-models branch April 26, 2026 18:47
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