fix(model_switch): route typed configured models off openai-codex (#45006)

A typed `/model <name>` where `<name>` is declared under `providers.<slug>` or
`custom_providers` — but typed while the current provider is a soft-accepting
one (e.g. `openai-codex`) — stayed on the current provider and was swallowed as
an unknown hidden Codex model, instead of routing to the provider that actually
declares it.

Add configured-provider exact-match detection (`_configured_provider_matches`)
and a new Step d.5 in `switch_model`: if the typed model is declared in
user/custom provider config, route to that provider BEFORE
`detect_provider_for_model()` guesses from static catalogs and BEFORE the
common-path validation lets a soft-accepting current provider swallow the name.

- Matching is exact (case-insensitive) against explicitly-declared model
  collections only (`models`, `model`, `default_model`) — never fuzzy/family.
- Same-provider declarer → keep current provider (canonicalize the id).
- Multiple declarers → fail clearly and ask for `--provider <slug>`.
- Single declarer → route there; for `providers.<slug>` user providers, set
  `explicit_provider` so the credential block resolves base_url/key from config.
- Step e (`detect_provider_for_model`) is gated off when `config_routed`.

The deliberately-supported openai-codex / xai-oauth hidden-model soft-accept
(#16172 / #19729) is left untouched: when nothing in config matches, detection
is a no-op.

Salvaged from #45442 by harjothkhara (authorship preserved).

Tests: tests/hermes_cli/test_model_switch_configured_provider_routing.py
(7 tests). Full model_switch suite: 214 passed.

Fixes #45006
This commit is contained in:
harjothkhara 2026-06-23 01:51:35 +05:30
parent 2a58fee1a1
commit 791c992b55
2 changed files with 445 additions and 0 deletions

View file

@ -662,6 +662,88 @@ def resolve_display_context_length(
return None
# ---------------------------------------------------------------------------
# Configured-provider detection for typed model names
# ---------------------------------------------------------------------------
def _configured_provider_matches(
model_name: str,
user_providers: Optional[dict],
custom_providers: Optional[list],
) -> dict[str, str]:
"""Return ``{provider_slug: canonical_model_id}`` for every configured
provider whose declared models contain an exact (case-insensitive) match
for ``model_name``.
Used by :func:`switch_model` to route a *typed* model name to the provider
that actually declares it in user/custom provider config, instead of
leaving it on the current provider. Without this, a model declared under
``providers.<slug>`` / ``custom_providers`` but typed while the current
provider is ``openai-codex`` stays on Codex and is soft-accepted as an
unknown hidden Codex model (#45006).
Matching is exact (case-insensitive); the configured spelling is returned
so the downstream validation/override path sees the canonical id. Only the
explicitly-declared model collections are scanned (``models``, the singular
``model``, and ``default_model``) never fuzzy/family matching.
"""
if not model_name or not model_name.strip():
return {}
target = model_name.strip().lower()
def _match(value) -> Optional[str]:
"""Canonical id if ``value`` (a model collection or scalar) declares
``target``, else None."""
if isinstance(value, str):
return value if value.strip().lower() == target else None
if isinstance(value, dict):
for mid in value:
if isinstance(mid, str) and mid.strip().lower() == target:
return mid
return None
if isinstance(value, (list, tuple)):
for item in value:
if isinstance(item, str) and item.strip().lower() == target:
return item
if isinstance(item, dict):
name = item.get("name")
if isinstance(name, str) and name.strip().lower() == target:
return name
return None
return None
matches: dict[str, str] = {}
if isinstance(user_providers, dict):
for slug, cfg in user_providers.items():
if not isinstance(slug, str) or not isinstance(cfg, dict):
continue
for key in ("models", "model", "default_model"):
hit = _match(cfg.get(key))
if hit:
matches[slug] = hit
break
if isinstance(custom_providers, list):
for entry in custom_providers:
if not isinstance(entry, dict):
continue
name = entry.get("name")
if not isinstance(name, str) or not name.strip():
continue
slug = f"custom:{name}"
if slug in matches:
continue
for key in ("models", "model", "default_model"):
hit = _match(entry.get(key))
if hit:
matches[slug] = hit
break
return matches
# ---------------------------------------------------------------------------
# Core model-switching pipeline
# ---------------------------------------------------------------------------
@ -921,6 +1003,58 @@ def switch_model(
resolved_in_current_catalog = True
break
# --- Step d.5: configured-provider exact-match detection (#45006) ---
# If the typed model is declared in user/custom provider config, route
# to that provider BEFORE detect_provider_for_model() guesses from
# static catalogs and BEFORE the common-path validation can let a
# soft-accepting current provider (e.g. openai-codex) swallow the name
# as an unknown hidden model. Configured matches beat static-catalog
# detection. Unlike step e this is deliberately NOT gated on
# ``not is_custom`` — switching from a local/custom provider A to a
# configured provider B that declares the typed model is the point.
config_routed = False
if (
not resolved_alias
and not resolved_in_current_catalog
and target_provider == current_provider
):
cfg_matches = _configured_provider_matches(
new_model, user_providers, custom_providers
)
if cfg_matches:
if current_provider in cfg_matches:
# The current provider itself declares it — keep current.
new_model = cfg_matches[current_provider]
config_routed = True
else:
match_slugs = sorted(cfg_matches)
if len(match_slugs) > 1:
return ModelSwitchResult(
success=False,
is_global=is_global,
error_message=(
f"'{new_model}' is declared by multiple configured "
f"providers ({', '.join(match_slugs)}). Re-run with "
f"--provider <slug> to choose which one to use."
),
)
target_provider = match_slugs[0]
new_model = cfg_matches[target_provider]
config_routed = True
logger.debug(
"Configured-provider detection routed '%s' to %s",
new_model, target_provider,
)
# User-config providers (providers.<slug>) are resolved in
# the credential block via resolve_user_provider(), which is
# gated on explicit_provider. Mirror the picker so the
# rerouted user provider's base_url/key load from the passed
# config rather than a from-scratch runtime re-resolve that
# doesn't know user-config slugs. custom:* slugs resolve via
# resolve_runtime_provider() directly and need no hint.
if isinstance(user_providers, dict) and target_provider in user_providers:
explicit_provider = target_provider
# --- Step e: detect_provider_for_model() as last resort ---
_base = current_base_url or ""
is_custom = current_provider in {"custom", "local"} or (
@ -932,6 +1066,7 @@ def switch_model(
and not is_custom
and not resolved_alias
and not resolved_in_current_catalog
and not config_routed
):
detected = detect_provider_for_model(new_model, current_provider)
if detected: