Skip to content

The agent that grows with you

fix(gateway): keep the session's route for a --once alias with no endpoint

David Dudok de Wit97e6f117de5d
diff --git a/gateway/session_cli_turn.py b/gateway/session_cli_turn.py
index 7ccd382b00b..62140b3b70a 100644
--- a/gateway/session_cli_turn.py
+++ b/gateway/session_cli_turn.py
@@ -108,7 +108,10 @@ def one_turn_runtime(override, model, runtime_kwargs, config):
runtime = dict(runtime_kwargs)
current_provider = runtime_kwargs.get('provider') or ''
same_provider = (result.target_provider or '').strip().lower() == current_provider.strip().lower()
- if same_provider and not result.resolved_via_alias:
+ # Only a ``model_aliases`` entry with its own ``base_url`` routes away from the session, as switch_model
+ # applied it to this result (``_apply_direct_alias_endpoint``); a built-in shortcut, an endpointless entry
+ # or a process provider's declared id names a model like a plain pick.
+ if same_provider and not result.alias_endpoint_applied:
# A plain model pick on the session's provider: the explicit provider re-resolved that provider's
# default endpoint and key, and the session already has both (a mirror, a proxy, a pool), so keep
# them. The wire follows the target only where the provider picks it per model (Copilot, Nous
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py
index ae5a820917b..5d364272ce2 100644
--- a/hermes_cli/model_switch.py
+++ b/hermes_cli/model_switch.py
@@ -462,6 +462,10 @@ class ModelSwitchResult:
runtime_capabilities: Optional[dict[str, bool]] = None
model_info: Optional[ModelInfo] = None
is_global: bool = False
+ # The resolved direct alias's own ``base_url`` (and the credential decided for it) replaced the
+ # provider's route. ``resolved_via_alias`` alone is also set by a built-in shortcut, an endpointless
+ # entry or a process provider's declared id, which name a model like a plain pick.
+ alias_endpoint_applied: bool = False
@dataclass(frozen=True)
@@ -1186,6 +1190,7 @@ class _Switch:
new_model: str = ""
target_provider: str = ""
resolved_alias: str = ""
+ alias_endpoint_applied: bool = False
provider_label: str = ""
api_key: str = ""
base_url: str = ""
@@ -1586,6 +1591,7 @@ def _resolve_switch_credentials(st: _Switch) -> Optional[ModelSwitchResult]:
da = DIRECT_ALIASES.get(st.resolved_alias)
if da is not None and da.base_url:
_apply_direct_alias_endpoint(st, da)
+ st.alias_endpoint_applied = True
# Fills an empty mode (alias cleared it) and overrides a STALE mode carried from previous
# session state when the host mandates one wire protocol (e.g. gpt-5.x on api.openai.com
@@ -1738,7 +1744,7 @@ def _build_switch_result(st: _Switch) -> ModelSwitchResult:
provider_label=st.provider_label, resolved_via_alias=st.resolved_alias, capabilities=capabilities,
runtime_capabilities={
k: v for k, v in runtime_capabilities.items() if isinstance(k, str) and isinstance(v, bool)},
- model_info=model_info, is_global=st.is_global)
+ model_info=model_info, is_global=st.is_global, alias_endpoint_applied=st.alias_endpoint_applied)
def switch_model(
diff --git a/tests/gateway/test_one_turn_route_publish.py b/tests/gateway/test_one_turn_route_publish.py
index af457cbd02d..39327d2956d 100644
--- a/tests/gateway/test_one_turn_route_publish.py
+++ b/tests/gateway/test_one_turn_route_publish.py
@@ -224,6 +224,110 @@ def test_a_plain_same_provider_pick_still_keeps_the_session_endpoint(alias_confi
assert (runtime["base_url"], runtime["api_key"]) == (SESSION_URL, "sk-session")
+PROXY = "https://llm-proxy.example/anthropic"
+
+
[email protected]("typed,aliases", [
+ ("claude-haiku-4-5", {}),
+ ("haiku", {}),
+ ("quick", {"quick": {"model": "claude-haiku-4-5", "provider": "anthropic"}}),
+ ("claude-haiku-4-5", {"quick": {"model": "claude-haiku-4-5", "provider": "anthropic"}}),
+ ("claude-haiku-4-5", {"claude-haiku-4-5": {"model": "claude-haiku-4-5", "provider": "anthropic"}}),
+], ids=["model-id", "builtin-alias", "endpointless-alias", "model-id-reverse-matching-it", "alias-named-like-its-model"])
+def test_a_same_provider_alias_without_an_endpoint_keeps_the_session_route(monkeypatch, typed, aliases):
+ """Review of #140969: every alias kind took the "alias runs whole" branch, so ``/model haiku --once`` on
+ a session behind a proxy, or rotating a pool, ran that turn on the provider's default host and key.
+ Only an alias that names its own endpoint leaves the session's route."""
+ cfg = {"model": {"default": "claude-sonnet-4-6", "provider": "anthropic"}, "model_aliases": aliases}
+ monkeypatch.setattr("hermes_cli.config.load_config", lambda *a, **k: cfg)
+ monkeypatch.setattr("hermes_cli.runtime_provider.load_config", lambda *a, **k: cfg)
+ monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", lambda *a, **k: {
+ "accepted": True, "persist": True, "recognized": True, "message": ""})
+ monkeypatch.setattr("hermes_cli.model_switch.DIRECT_ALIASES", {})
+ monkeypatch.setattr("hermes_cli.model_switch.list_provider_models",
+ lambda provider: ["claude-haiku-4-5", "claude-sonnet-4-6"] if provider == "anthropic" else [])
+ monkeypatch.setattr("hermes_cli.models._PROVIDER_MODELS", {})
+ monkeypatch.setattr("agent.models_dev.fetch_models_dev", dict)
+ # The provider's default route, which the explicit provider re-resolves.
+ monkeypatch.setattr("hermes_cli.runtime_provider.resolve_runtime_provider", lambda **_k: {
+ "provider": "anthropic", "api_key": "sk-default", "base_url": "https://api.anthropic.com",
+ "api_mode": "anthropic_messages"})
+ pool = object()
+ model, runtime = one_turn_runtime(
+ {"model": typed, "provider": "anthropic"}, "claude-sonnet-4-6",
+ {"provider": "anthropic", "api_key": "sk-session", "base_url": PROXY, "api_mode": "anthropic_messages",
+ "credential_pool": pool, "request_overrides": {"metadata": {"team": "a"}}},
+ cfg)
+ assert model == "claude-haiku-4-5"
+ assert (runtime["base_url"], runtime["api_key"]) == (PROXY, "sk-session")
+ assert runtime["credential_pool"] is pool
+ assert runtime["request_overrides"] == {"metadata": {"team": "a"}}
+
+
[email protected]("typed", ["opus", "claude-opus-5"], ids=["provider-alias", "declared-id"])
+def test_a_process_providers_declared_model_keeps_the_session_command(monkeypatch, typed):
+ """An ``external_process`` provider's own alias or declared id resolves like an alias, and the turn
+ still runs on the session's command."""
+ from providers.base import ProviderProfile
+ profile = ProviderProfile(name="proc-provider", auth_type="external_process", base_url="process://proc-provider",
+ fallback_models=("claude-sonnet-5", "claude-opus-5"), model_aliases={"opus": "claude-opus-5"})
+ monkeypatch.setattr("providers.get_provider_profile", lambda name: profile if name == profile.name else None)
+ cfg = {"model": {"default": "claude-sonnet-5", "provider": profile.name}}
+ monkeypatch.setattr("hermes_cli.config.load_config", lambda *a, **k: cfg)
+ monkeypatch.setattr("hermes_cli.runtime_provider.load_config", lambda *a, **k: cfg)
+ monkeypatch.setattr("hermes_cli.model_switch.DIRECT_ALIASES", {})
+ monkeypatch.setattr("hermes_cli.model_switch.list_provider_models", lambda provider: [])
+ monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", lambda *a, **k: {
+ "accepted": True, "persist": True, "recognized": True, "message": ""})
+ monkeypatch.setattr("agent.models_dev.fetch_models_dev", dict)
+ monkeypatch.setattr("hermes_cli.model_switch.get_model_info", lambda *a, **k: None)
+ monkeypatch.setattr("hermes_cli.model_switch.get_model_capabilities", lambda *a, **k: None)
+ monkeypatch.setattr("hermes_cli.runtime_provider.resolve_runtime_provider", lambda **_k: {
+ "provider": profile.name, "api_key": "external-process", "base_url": profile.base_url,
+ "api_mode": "chat_completions"})
+ monkeypatch.setattr("hermes_cli.config.get_compatible_custom_providers", lambda _config: [])
+ model, runtime = one_turn_runtime(
+ {"model": typed, "provider": profile.name}, "claude-sonnet-5",
+ {"provider": profile.name, "api_key": "external-process", "base_url": profile.base_url,
+ "api_mode": "chat_completions", "command": "proc-cli", "args": ["--serve"]},
+ cfg)
+ assert model == "claude-opus-5"
+ assert (runtime["command"], runtime["args"]) == ("proc-cli", ["--serve"])
+
+
+def test_an_alias_runs_on_the_endpoint_the_switch_resolved_when_the_config_changes_after(monkeypatch):
+ """The owner re-read the alias table after ``switch_model`` had resolved and validated the alias's
+ endpoint, so a ``config.yaml`` edit in between (its ``base_url`` removed) ran the alias's model on the
+ session's endpoint and key. The route the switch resolved decides."""
+ from hermes_cli import model_switch
+ from hermes_constants import get_config_path
+ path = get_config_path()
+ config = ("model:\n default: session-model\n provider: custom\n base_url: " + SESSION_URL + "\n"
+ "model_aliases:\n fast:\n model: fast-model\n provider: custom\n")
+ path.write_text(config + " base_url: " + ALIAS_URL + "\n api_key: sk-alias\n", encoding="utf-8")
+ edited = []
+
+ def validate(*_a, **_k):
+ path.write_text(config, encoding="utf-8") # the edit lands after the endpoint was applied
+ edited.append(True)
+ return {"accepted": True, "persist": True, "recognized": True, "message": ""}
+
+ monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", validate)
+ monkeypatch.setattr(model_switch, "DIRECT_ALIASES", {})
+ monkeypatch.setattr(model_switch, "_DIRECT_ALIAS_IDENTITY", None)
+ monkeypatch.setattr(model_switch, "_DIRECT_ALIAS_LOADED", None)
+ monkeypatch.setattr("agent.models_dev.fetch_models_dev", dict)
+ model, runtime = one_turn_runtime(
+ {"model": "fast", "provider": "custom"}, "session-model",
+ {"provider": "custom", "api_key": "sk-session", "base_url": SESSION_URL, "api_mode": "chat_completions",
+ "credential_pool": object()},
+ {})
+ assert edited, "the switch never validated the pick"
+ assert model == "fast-model"
+ assert (runtime["base_url"], runtime["api_key"]) == (ALIAS_URL, "sk-alias")
+ assert runtime["credential_pool"] is None
+
+
def test_a_one_turn_override_carries_its_reasoning_level_for_that_admission_only():
from gateway.session_cli_turn import admit_cli_turn, one_turn_reasoning
from hermes_state_runtime import RuntimeStoreError