Skip to content

The agent that grows with you

fix(runtime): share URL path parsing with the host check; pin catalog defaults in tests

kshitijk4poorda2b6a47b935
diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.py
index 8b2e43c89b4..2d67899281b 100644
--- a/hermes_cli/runtime_provider.py
+++ b/hermes_cli/runtime_provider.py
@@ -33,7 +33,7 @@ from hermes_cli import config as _config_mod
from hermes_cli import models as _models # attribute access keeps ``hermes_cli.models.<name>`` patches effective
from hermes_constants import OPENROUTER_BASE_URL
from hermes_cli.providers import determine_api_mode, get_provider, is_actual_route, is_official_openai_host, nous_api_mode
-from utils import base_url_host_matches, base_url_hostname, env_int
+from utils import base_url_host_matches, base_url_hostname, base_url_path, env_int
# Late-bound delegates, deliberately NOT module-level from-imports: this module is often imported
@@ -118,7 +118,7 @@ def _detect_api_mode_for_url(base_url: str) -> Optional[str]:
mandated = _HOST_MANDATED_API_MODES.get(hostname) or ("codex_responses" if is_official_openai_host(base_url) else None)
if mandated:
return mandated
- path = _base_url_path(normalized)
+ path = base_url_path(normalized)
if path.endswith(("/anthropic", "/anthropic/v1")) or (hostname == "api.kimi.com" and "/coding" in normalized):
# Direct native Anthropic host: realign with providers.determine_api_mode, which already maps this
# host to anthropic_messages. The exact-hostname match rejects lookalike subdomains
@@ -147,7 +147,7 @@ def _fallback_api_mode(provider: str, base_url: str, model: str = "") -> str:
detected = _detect_api_mode_for_url(base_url)
if detected:
return detected
- declared = determine_api_mode(provider, base_url, model) or "chat_completions"
+ declared = determine_api_mode(provider, base_url, model)
if declared == "anthropic_messages" and not _on_declared_anthropic_endpoint(provider, base_url):
# The declared Anthropic transport describes the provider's own endpoint. A base_url
# override at a foreign host (OpenAI-compatible relay, LiteLLM, egress proxy) or at the
@@ -158,28 +158,24 @@ def _fallback_api_mode(provider: str, base_url: str, model: str = "") -> str:
def _on_declared_anthropic_endpoint(provider: str, base_url: str) -> bool:
- """True when ``base_url`` is the provider's own Anthropic-protocol endpoint: same host as the
- catalog default and either the bare host or the default's path (``/anthropic`` for MiniMax,
- ``/plan/anthropic`` for Tencent) with or without a ``/v1`` tail. With nothing to compare —
- no URL, or an overlay-only provider whose models.dev default is not cached (offline) — keep
- the declared transport: demoting the provider's own default endpoint would be the worse
- failure."""
+ """True when ``base_url`` is the provider's own Anthropic-protocol endpoint: the catalog
+ default's host (or a subdomain of it) and either the bare host — the provider's own host
+ with no path still means the native endpoint (#53054) — or the default's path
+ (``/anthropic`` for MiniMax, ``/plan/anthropic`` for Tencent) with or without a ``/v1``
+ tail. With nothing to compare — no URL, no path on the default, or an overlay-only
+ provider whose models.dev default is not cached (offline) — keep the declared transport:
+ demoting the provider's own default endpoint would be the worse failure."""
pdef = get_provider(provider, allow_network=False)
default = (pdef.base_url if pdef else "").strip()
- if not default or not (base_url or "").strip():
+ default_path = base_url_path(default).removesuffix("/v1")
+ if not default_path or not (base_url or "").strip():
return True
if not base_url_host_matches(base_url, base_url_hostname(default)):
return False
- path = _base_url_path(base_url)
- default_path = _base_url_path(default).removesuffix("/v1")
+ path = base_url_path(base_url)
return path == "" or path == default_path or path.startswith(default_path + "/")
-def _base_url_path(base_url: str) -> str:
- """Lower-cased URL path without the trailing slash (``""`` for a bare host)."""
- return urlparse((base_url or "").strip().lower()).path.rstrip("/")
-
-
def _resolve_plain_custom_api_mode(model_cfg: Dict[str, Any], base_url: str) -> str:
"""api_mode for legacy/plain ``provider: custom`` endpoints — conservative by default: only
direct OpenAI/xAI/Meta URLs imply Responses; named custom providers opt in via ``api_mode``."""
diff --git a/tests/hermes_cli/test_runtime_transport_precedence.py b/tests/hermes_cli/test_runtime_transport_precedence.py
index c598d5e476b..667d868947c 100644
--- a/tests/hermes_cli/test_runtime_transport_precedence.py
+++ b/tests/hermes_cli/test_runtime_transport_precedence.py
@@ -58,8 +58,12 @@ class TestFallbackApiMode:
real = runtime_provider.get_provider
def seeded(name, *, allow_network=True):
+ # allow_network is dropped so the fixture can never reach models.dev; replace, not
+ # assign, because get_provider hands back the cached ProviderDef other tests share.
+ # Seed unconditionally: a warm disk cache must not swap in whatever models.dev
+ # publishes today.
pdef = real(name, allow_network=False)
- if pdef is not None and not pdef.base_url and name in defaults:
+ if pdef is not None and name in defaults:
pdef = dataclasses.replace(pdef, base_url=defaults[name])
return pdef
@@ -104,7 +108,8 @@ class TestFallbackApiMode:
def test_minimax_declared_anthropic_transport_honored(self):
# Same latent bug class: minimax declares an Anthropic-compatible
# transport but previously fell back to chat_completions when the
- # URL carried no /anthropic hint.
+ # URL carried no /anthropic hint. The bare catalog host is still
+ # honored (#53054); non-default paths now are not (#76836).
from hermes_cli.providers import determine_api_mode
expected = determine_api_mode("minimax", "https://api.minimax.io")
diff --git a/utils.py b/utils.py
index 36d9651e792..3ca74d4b110 100644
--- a/utils.py
+++ b/utils.py
@@ -583,6 +583,13 @@ def base_url_hostname(base_url: str) -> str:
return _hostname_of(_parse_base_url(base_url))
+def base_url_path(base_url: str) -> str:
+ """Lowercased URL path without the trailing slash (``""`` for a bare host); same scheme
+ tolerance as :func:`base_url_hostname` so host and path checks agree on one URL."""
+ parsed = _parse_base_url(base_url)
+ return (parsed.path if parsed else "").lower().rstrip("/")
+
+
def model_forces_max_completion_tokens(model: str) -> bool:
"""True for OpenAI families that reject ``max_tokens`` (HTTP 400 ``unsupported_parameter``)."""
m = (model or "").strip().lower().rsplit("/", 1)[-1]