Skip to content

The agent that grows with you

refactor(opencode): fold the family-path heal into normalize_opencode_base_url as a table

teknium19b0c53b43abf
diff --git a/hermes_cli/models.py b/hermes_cli/models.py
index 864cc234413..e5789c6d488 100644
--- a/hermes_cli/models.py
+++ b/hermes_cli/models.py
@@ -2279,30 +2279,8 @@ 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}"
+# Relay path per OpenCode family on opencode.ai hosts. The free tier is served by the Zen relay.
+_OPENCODE_FAMILY_PATHS = {"opencode-zen": "/zen", "opencode-free": "/zen", "opencode-go": "/zen/go"}
def normalize_opencode_base_url(
@@ -2310,23 +2288,25 @@ def normalize_opencode_base_url(
"""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. 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``)."""
+ ``OPENCODE_*_BASE_URL`` proxies are left alone. On those hosts the relay path segment follows
+ the resolved family too (``/zen`` vs ``/zen/go``): the two relays serve different model sets,
+ so a ``model.base_url`` carried over from the other family 401s ("Model ... is not supported")."""
url = str(base_url or "").strip().rstrip("/")
family = opencode_provider_family(provider_id)
if not url or family is None:
return url
- url = _heal_opencode_family_path(url, family)
+ try:
+ parsed = urllib.parse.urlparse(url)
+ except Exception:
+ parsed = None
+ official = parsed is not None and (parsed.netloc.lower() == "opencode.ai" or parsed.netloc.lower().endswith(".opencode.ai"))
+ if official and re.fullmatch(r"/zen(/go)?(/v1)?", parsed.path):
+ url = f"{parsed.scheme}://{parsed.netloc}{_OPENCODE_FAMILY_PATHS[family]}{'/v1' if parsed.path.endswith('/v1') else ''}"
if api_mode == "anthropic_messages":
return re.sub(r"/v1$", "", url)
if url.endswith("/v1"):
return url
- try:
- host = urllib.parse.urlparse(url).netloc.lower()
- except Exception:
- host = ""
- return url + "/v1" if host == "opencode.ai" or host.endswith(".opencode.ai") else url
+ return url + "/v1" if official else url
def github_model_reasoning_efforts(
diff --git a/tests/hermes_cli/test_model_validation.py b/tests/hermes_cli/test_model_validation.py
index 1ccb5b84923..5af4e111981 100644
--- a/tests/hermes_cli/test_model_validation.py
+++ b/tests/hermes_cli/test_model_validation.py
@@ -341,49 +341,26 @@ class TestNormalizeOpencodeBaseUrl:
class TestNormalizeOpencodeBaseUrlFamilyPath:
- """A carried-over base_url must also be healed on the FAMILY path segment, not just /v1.
+ """A carried-over base_url is healed on the FAMILY path segment (``/zen`` vs ``/zen/go``), not
+ just ``/v1`` (#112600): ``model.base_url`` pinned to the Zen relay survived a switch to
+ ``opencode-go`` and every request 401'd ("Model mimo-v2.5 is not supported")."""
- 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
+ @pytest.mark.parametrize("provider, api_mode, url, expected", [
+ ("opencode-go", "chat_completions", "https://opencode.ai/zen/v1", "https://opencode.ai/zen/go/v1"),
+ ("opencode-zen", "chat_completions", "https://opencode.ai/zen/go/v1", "https://opencode.ai/zen/v1"),
# 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
+ ("opencode-free", "chat_completions", "https://opencode.ai/zen/go/v1", "https://opencode.ai/zen/v1"),
+ # Family healed first, then the /v1 strip for the Anthropic SDK — both apply.
+ ("opencode-go", "anthropic_messages", "https://opencode.ai/zen/v1", "https://opencode.ai/zen/go"),
+ ("opencode-zen", "anthropic_messages", "https://opencode.ai/zen/go", "https://opencode.ai/zen"),
# 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"
+ ("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"
+ ("opencode-go", "chat_completions", "https://opencode.ai/api", "https://opencode.ai/api/v1"),
+ ])
+ def test_family_path_follows_the_resolved_provider(self, provider, api_mode, url, expected):
+ from hermes_cli.models import normalize_opencode_base_url
+ assert normalize_opencode_base_url(provider, api_mode, url) == expected
class TestAzureFoundryModelApiMode:
diff --git a/tests/hermes_cli/test_runtime_provider_resolution.py b/tests/hermes_cli/test_runtime_provider_resolution.py
index 6a802401c05..1315cfb6f25 100644
--- a/tests/hermes_cli/test_runtime_provider_resolution.py
+++ b/tests/hermes_cli/test_runtime_provider_resolution.py
@@ -1107,26 +1107,6 @@ def test_opencode_go_resolution_heals_a_stale_zen_config_base_url(monkeypatch):
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"
# ------------------------------------------------------------------