The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): heal the OpenCode family path in a carried-over base_url
KeyArgo723b462b12b3
diff --git a/hermes_cli/models.py b/hermes_cli/models.pyindex 64d0137ea3e..864cc234413 100644--- a/hermes_cli/models.py+++ b/hermes_cli/models.py@@ -2279,15 +2279,45 @@ def opencode_model_api_mode(provider_id: Optional[str], model_id: Optional[str])return "chat_completions"+def _heal_opencode_family_path(url: str, family: str) -> str:+ """Rewrite a Zen/Go relay URL's FAMILY path segment (``/zen`` vs ``/zen/go``) to ``family``.++ A base URL is persisted to ``model.base_url`` and carried into the next session, so a switch+ between the two relays (e.g. Zen pinned, then a Go-only model selected) would otherwise keep+ asking the wrong relay, which serves a different model set and 401s ("Model ... is not+ supported"). Only ``/zen``-rooted paths on opencode.ai hosts are touched — custom+ ``OPENCODE_*_BASE_URL`` proxies and unrelated paths pass through untouched.+ """+ try:+ parsed = urllib.parse.urlparse(url)+ except Exception:+ return url+ host = parsed.netloc.lower()+ if not (host == "opencode.ai" or host.endswith(".opencode.ai")):+ return url+ path = parsed.path.rstrip("/")+ if not path.startswith("/zen"):+ return url+ tail = path[len("/zen/go"):] if path.startswith("/zen/go") else path[len("/zen"):]+ if tail not in ("", "/v1"): # an unexpected shape is not a family alias — leave it alone+ return url+ root = "/zen/go" if family == "opencode-go" else "/zen" # opencode-free lives on the Zen relay+ return f"{parsed.scheme}://{parsed.netloc}{root}{tail}"++def normalize_opencode_base_url(provider_id: Optional[str], api_mode: Optional[str], base_url: Optional[str]) -> str:"""Normalize an OpenCode Zen / Go base URL for the API mode. Must be SYMMETRIC: the anthropic-stripped URL gets persisted to ``model.base_url`` after switching into an anthropic-routed model,and chat/codex modes heal it by re-adding ``/v1`` — but only on opencode.ai hosts, so custom- ``OPENCODE_*_BASE_URL`` proxies are left alone."""+ ``OPENCODE_*_BASE_URL`` proxies are left alone. The family path segment is healed the same way:+ ``/zen`` and ``/zen/go`` are different relays serving different model sets, so a carried-over+ URL must follow the resolved provider family (see ``_heal_opencode_family_path``)."""url = str(base_url or "").strip().rstrip("/")- if not url or opencode_provider_family(provider_id) is None:+ family = opencode_provider_family(provider_id)+ if not url or family is None:return url+ url = _heal_opencode_family_path(url, family)if api_mode == "anthropic_messages":return re.sub(r"/v1$", "", url)if url.endswith("/v1"):diff --git a/tests/hermes_cli/test_model_validation.py b/tests/hermes_cli/test_model_validation.pyindex b2daf7070ef..1ccb5b84923 100644--- a/tests/hermes_cli/test_model_validation.py+++ b/tests/hermes_cli/test_model_validation.py@@ -340,6 +340,52 @@ class TestNormalizeOpencodeBaseUrl:) == "https://openrouter.ai/api"+class TestNormalizeOpencodeBaseUrlFamilyPath:+ """A carried-over base_url must also be healed on the FAMILY path segment, not just /v1.++ Regression for #112600: ``model.base_url`` pinned to the Zen relay+ (``https://opencode.ai/zen/v1``) survived a switch to ``opencode-go``; the relays serve+ different model sets, so every request went to Zen and 401'd+ (``Model mimo-v2.5 is not supported``). ``normalize_opencode_base_url`` is the one healing+ step every resolution path funnels through, so it owns the family-path rewrite.+ """++ def test_chat_completions_go_provider_heals_a_zen_base_url(self):+ from hermes_cli.models import normalize_opencode_base_url+ assert normalize_opencode_base_url(+ "opencode-go", "chat_completions", "https://opencode.ai/zen/v1"+ ) == "https://opencode.ai/zen/go/v1"++ def test_anthropic_messages_go_provider_heals_a_zen_base_url(self):+ from hermes_cli.models import normalize_opencode_base_url+ # Family healed first, then the /v1 strip for the Anthropic SDK — both apply.+ assert normalize_opencode_base_url(+ "opencode-go", "anthropic_messages", "https://opencode.ai/zen/v1"+ ) == "https://opencode.ai/zen/go"++ def test_zen_and_free_providers_heal_a_go_base_url(self):+ from hermes_cli.models import normalize_opencode_base_url+ # opencode-free is served on the Zen relay, so it maps back to /zen (not /zen/go).+ for provider in ("opencode-zen", "opencode-free"):+ assert normalize_opencode_base_url(+ provider, "chat_completions", "https://opencode.ai/zen/go/v1"+ ) == "https://opencode.ai/zen/v1"+ assert normalize_opencode_base_url(+ "opencode-zen", "anthropic_messages", "https://opencode.ai/zen/go"+ ) == "https://opencode.ai/zen"++ def test_custom_proxy_host_and_unrelated_paths_untouched(self):+ from hermes_cli.models import normalize_opencode_base_url+ # A self-hosted OPENCODE_*_BASE_URL proxy has no family path to rewrite.+ assert normalize_opencode_base_url(+ "opencode-go", "chat_completions", "https://gateway.internal.example/zen/v1"+ ) == "https://gateway.internal.example/zen/v1"+ # Non-/zen paths on the real host keep the pre-existing /v1 behaviour.+ assert normalize_opencode_base_url(+ "opencode-go", "chat_completions", "https://opencode.ai/api"+ ) == "https://opencode.ai/api/v1"++class TestAzureFoundryModelApiMode:"""Azure Foundry deploys GPT-5.x / codex / o-series as Responses-API-only.diff --git a/tests/hermes_cli/test_runtime_provider_resolution.py b/tests/hermes_cli/test_runtime_provider_resolution.pyindex 57e9abcf1a9..6a802401c05 100644--- a/tests/hermes_cli/test_runtime_provider_resolution.py+++ b/tests/hermes_cli/test_runtime_provider_resolution.py@@ -1082,6 +1082,51 @@ def test_opencode_go_model_derivation_beats_stale_persisted_api_mode(monkeypatchassert resolved["api_mode"] == "anthropic_messages"+def test_opencode_go_resolution_heals_a_stale_zen_config_base_url(monkeypatch):+ """End-to-end for #112600: a ``model.base_url`` pinned to the Zen relay must follow the+ resolved provider family. The CLI inherits that pinned override across a switch, and the Zen+ relay does not serve Go-subscription models, so the client was built against+ ``https://opencode.ai/zen/v1`` and every request 401'd ("Model mimo-v2.5 is not supported").+ """+ monkeypatch.setattr(rp, "resolve_provider", lambda *a, **k: "opencode-go")+ monkeypatch.setattr(+ rp,+ "_get_model_config",+ lambda: {+ "provider": "opencode-go",+ "default": "mimo-v2.5",+ "base_url": "https://opencode.ai/zen/v1",+ },+ )+ monkeypatch.setenv("OPENCODE_GO_API_KEY", "test-opencode-go-key")+ monkeypatch.delenv("OPENCODE_GO_BASE_URL", raising=False)++ resolved = rp.resolve_runtime_provider(requested="opencode-go")++ assert resolved["api_mode"] == "chat_completions"+ assert resolved["base_url"] == "https://opencode.ai/zen/go/v1"+++def test_opencode_zen_resolution_heals_a_stale_go_config_base_url(monkeypatch):+ """The mirror direction: a Go URL must not survive a switch back to the Zen relay."""+ monkeypatch.setattr(rp, "resolve_provider", lambda *a, **k: "opencode-zen")+ monkeypatch.setattr(+ rp,+ "_get_model_config",+ lambda: {+ "provider": "opencode-zen",+ "default": "deepseek-v4-flash",+ "base_url": "https://opencode.ai/zen/go/v1",+ },+ )+ monkeypatch.setenv("OPENCODE_ZEN_API_KEY", "test-opencode-zen-key")+ monkeypatch.delenv("OPENCODE_ZEN_BASE_URL", raising=False)++ resolved = rp.resolve_runtime_provider(requested="opencode-zen")++ assert resolved["base_url"] == "https://opencode.ai/zen/v1"++# ------------------------------------------------------------------# fix #2562 — resolve_provider("custom") must not remap to "openrouter"# ------------------------------------------------------------------