Skip to content

The agent that grows with you

fix(model_switch): treat a fall-through to OpenRouter's default as "no custom endpoint configured"

kshitijk4poor0cb3760383dc
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py
index 5642a0b0f4c..030996ff6c1 100644
--- a/hermes_cli/model_switch.py
+++ b/hermes_cli/model_switch.py
@@ -7,9 +7,9 @@ exclusively; colons are reserved for OpenRouter variant suffixes (``:free``, ``:
from __future__ import annotations
import logging
-from contextlib import suppress
import os
import re
+from contextlib import suppress
from dataclasses import dataclass, field
from typing import Any, NamedTuple, Optional
@@ -1382,14 +1382,16 @@ def _creds_for_switched_provider(st: _Switch) -> Optional[ModelSwitchResult]:
# A bare-custom session switching models stays on its endpoint (#45597). Arriving from
# ANOTHER provider (the per-turn config sync adopting ``provider: custom``), the configured
# custom endpoint wins: keeping the session's URL pairs the new model with the previous
- # provider's host and key (#73680). No configured endpoint ⇒ keep the current one (an alias
- # may still supply its own below).
- st.api_key, st.base_url = st.current_api_key, st.current_base_url
+ # provider's host and key (#73680). The resolver raises with no endpoint and no key, and
+ # lands on the OpenRouter DEFAULT with a key but no ``model.base_url`` (#74143) — both mean
+ # "nothing configured": keep the current endpoint.
+ key, url = st.current_api_key, st.current_base_url
if st.current_provider != "custom":
with suppress(Exception):
st.resolve_runtime(requested="custom")
- if not st.base_url:
- st.api_key, st.base_url = st.current_api_key, st.current_base_url
+ if st.base_url and not _fell_back_to_openrouter_default(st):
+ key, url = st.api_key, st.base_url
+ st.api_key, st.base_url = key, url
st.api_mode = determine_api_mode(st.target_provider, st.base_url)
else:
# A URL-bearing LOCAL direct alias (ollama, vllm — labels that resolve to `custom`)
@@ -1448,13 +1450,19 @@ def _creds_for_current_provider(st: _Switch) -> None:
# key/endpoint rotation is not pinned to a stale session.
if (
st.current_provider in {"custom", "local"} and st.current_base_url
- and (not st.base_url or base_url_host_matches(st.base_url, "openrouter.ai"))
- and not base_url_host_matches(st.current_base_url, "openrouter.ai")
+ and (not st.base_url or _fell_back_to_openrouter_default(st))
):
st.base_url, st.api_key = st.current_base_url, st.current_api_key
st.api_mode = determine_api_mode(st.current_provider, st.base_url)
+def _fell_back_to_openrouter_default(st: _Switch) -> bool:
+ """The bare-``custom`` resolver ended on OpenRouter's default host while the session was
+ elsewhere: no trusted ``model.base_url`` existed, so the URL is one the user never picked."""
+ return (base_url_host_matches(st.base_url, "openrouter.ai")
+ and not base_url_host_matches(st.current_base_url, "openrouter.ai"))
+
+
def _resolve_switch_credentials(st: _Switch) -> Optional[ModelSwitchResult]:
"""COMMON PATH part 1: credentials, direct-alias endpoint override, and the api_mode for the
final (provider, base_url) before validation."""
diff --git a/tests/hermes_cli/test_model_switch_custom_providers.py b/tests/hermes_cli/test_model_switch_custom_providers.py
index d8224b41407..1e62aa45d91 100644
--- a/tests/hermes_cli/test_model_switch_custom_providers.py
+++ b/tests/hermes_cli/test_model_switch_custom_providers.py
@@ -508,7 +508,8 @@ def test_switch_to_bare_custom_from_another_provider_resolves_the_configured_end
home = tmp_path / "hermes-home"
home.mkdir()
(home / "config.yaml").write_text(
- "model:\n default: qwen3:8b\n provider: custom\n base_url: http://127.0.0.1:11434/v1\n"
+ "model:\n default: qwen3:8b\n provider: custom\n base_url: http://127.0.0.1:11434/v1\n",
+ encoding="utf-8",
)
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", lambda *a, **k: _MOCK_VALIDATION)
@@ -527,8 +528,37 @@ def test_switch_to_bare_custom_from_another_provider_resolves_the_configured_end
)
assert result.success is True
+ assert result.target_provider == "custom"
assert result.base_url == "http://127.0.0.1:11434/v1"
- assert result.api_key != "sk-openrouter"
+ assert result.api_key == "no-key-required"
+
+
+def test_switch_to_bare_custom_with_no_configured_endpoint_keeps_the_current_one(monkeypatch, tmp_path):
+ """#74143 shape on the switched-provider path: with no ``model.base_url`` the bare-custom
+ resolver lands on OpenRouter's default whenever an OpenRouter key exists — a host the user
+ never picked. An Anthropic session must stay on its own endpoint instead."""
+ home = tmp_path / "hermes-home"
+ home.mkdir()
+ (home / "config.yaml").write_text("model:\n default: m\n provider: anthropic\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-env")
+ monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", lambda *a, **k: _MOCK_VALIDATION)
+ 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)
+
+ result = switch_model(
+ raw_input="m2",
+ current_provider="anthropic",
+ current_model="m",
+ current_base_url="https://api.anthropic.com",
+ current_api_key="sk-ant",
+ explicit_provider="custom",
+ user_providers={},
+ custom_providers=[],
+ )
+
+ assert result.success is True
+ assert (result.base_url, result.api_key) == ("https://api.anthropic.com", "sk-ant")
def test_is_aggregator_recognizes_named_custom_provider():