Skip to content

The agent that grows with you

fix(runtime): compare against the catalog default path, keep declared on empty input

kshitijk4poordbdcb83d7786
diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.py
index a81d92f03cd..8b2e43c89b4 100644
--- a/hermes_cli/runtime_provider.py
+++ b/hermes_cli/runtime_provider.py
@@ -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 = urlparse(normalized).path.rstrip("/")
+ 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
@@ -140,7 +140,8 @@ def _fallback_api_mode(provider: str, base_url: str, model: str = "") -> str:
"""api_mode when no explicit/persisted mode applies: URL detection (host-mandated wire shapes)
first, then the transport the provider overlay declares via ``providers.determine_api_mode``
(``openai-api`` pointed at us.api.openai.com 400'd on every tool call without it), then
- ``chat_completions``."""
+ ``chat_completions``. A declared ``anthropic_messages`` is kept only on the provider's own
+ endpoint (#76836)."""
if is_actual_route(provider, base_url):
return "chat_completions"
detected = _detect_api_mode_for_url(base_url)
@@ -158,15 +159,25 @@ 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 a path under ``/anthropic``. Unknown default ⇒ True
- (nothing to compare against, keep the declared transport)."""
- default = (getattr(get_provider(provider), "base_url", "") or "").strip()
- if not default:
+ 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."""
+ pdef = get_provider(provider, allow_network=False)
+ default = (pdef.base_url if pdef else "").strip()
+ if not default or not (base_url or "").strip():
return True
if not base_url_host_matches(base_url, base_url_hostname(default)):
return False
- path = urlparse((base_url or "").strip().lower()).path.rstrip("/")
- return path == "" or path.startswith("/anthropic")
+ path = _base_url_path(base_url)
+ default_path = _base_url_path(default).removesuffix("/v1")
+ 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:
diff --git a/tests/hermes_cli/test_runtime_transport_precedence.py b/tests/hermes_cli/test_runtime_transport_precedence.py
index 653ea251005..c598d5e476b 100644
--- a/tests/hermes_cli/test_runtime_transport_precedence.py
+++ b/tests/hermes_cli/test_runtime_transport_precedence.py
@@ -8,8 +8,9 @@ the provider overlay itself declares.
Contract pinned here: when URL detection has no opinion, the runtime falls
back to ``providers.determine_api_mode(provider, base_url, model)`` (the
-provider's declared transport), and only lands on ``chat_completions`` for
-genuinely unknown providers/endpoints. Covers the explicit-runtime path and
+provider's declared transport), and lands on ``chat_completions`` for
+genuinely unknown providers/endpoints and for a declared ``anthropic_messages``
+off the provider's own endpoint (#76836). Covers the explicit-runtime path and
the API-key-provider path; the pool-entry path shares the same helper.
"""
@@ -43,10 +44,33 @@ class TestFallbackApiMode:
== "codex_responses"
)
+ @pytest.fixture
+ def catalog_defaults(self, monkeypatch):
+ """Pin the models.dev default endpoints the predicate compares against, so the contract
+ holds without the network (a cold cache leaves ``ProviderDef.base_url`` empty)."""
+ import dataclasses
+ from hermes_cli import runtime_provider
+ defaults = {
+ "minimax": "https://api.minimax.io/anthropic/v1",
+ "minimax-cn": "https://api.minimaxi.com/anthropic/v1",
+ "tencent-tokenplan": "https://api.lkeap.cloud.tencent.com/plan/anthropic",
+ }
+ real = runtime_provider.get_provider
+
+ def seeded(name, *, allow_network=True):
+ pdef = real(name, allow_network=False)
+ if pdef is not None and not pdef.base_url and name in defaults:
+ pdef = dataclasses.replace(pdef, base_url=defaults[name])
+ return pdef
+
+ monkeypatch.setattr(runtime_provider, "get_provider", seeded)
+
+ @pytest.mark.usefixtures("catalog_defaults")
@pytest.mark.parametrize("provider, base_url", [
("minimax", "http://127.0.0.1:8787/v1"), # OpenAI-compatible relay in front of MiniMax
("minimax-cn", "https://egress.corp.test/v1"), # corporate egress gateway
("minimax", "https://api.minimax.io/v1"), # provider's own OpenAI-compatible path
+ ("minimax", "https://api.minimax.io/anthropic-compat/v1"), # lookalike path, not /anthropic/
])
def test_anthropic_transport_is_not_assumed_off_the_provider_endpoint(self, provider, base_url):
# #76836: the declared anthropic_messages transport is a statement about the provider's
@@ -54,10 +78,14 @@ class TestFallbackApiMode:
# shaped requests with x-api-key there 401 on every turn (or silently reroute to a fallback).
assert _fallback_api_mode(provider, base_url, "MiniMax-M3") == "chat_completions"
+ @pytest.mark.usefixtures("catalog_defaults")
@pytest.mark.parametrize("provider, base_url", [
("minimax", "https://api.minimax.io"), # bare host, no /anthropic hint (#53054 kept)
("minimax", "https://api.minimax.io/anthropic"), # URL-detected native path
("minimax-cn", "https://api.minimaxi.com/anthropic/v1"),
+ ("minimax", "https://api.minimax.io/anthropic/v1/messages"), # under the default path
+ ("tencent-tokenplan", "https://api.lkeap.cloud.tencent.com/plan/anthropic/v2"), # non-/anthropic default
+ ("minimax", ""), # nothing to compare: keep the declared transport
])
def test_anthropic_transport_holds_on_the_provider_endpoint(self, provider, base_url):
assert _fallback_api_mode(provider, base_url, "MiniMax-M3") == "anthropic_messages"