Skip to content

The agent that grows with you

fix: fallback activation picks the OpenCode per-model wire (muse-spark → Responses)

Aniruddha Adak6eef5ea0bbe2
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 10807d84f94..cd0bf13e471 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -1711,6 +1711,14 @@ def _fallback_api_mode_resolved(agent, fb_provider: str, fb_model: str, fb_base_
landed on the chat_completions default (never called for an explicit api_mode)."""
if fb_provider == "openai-codex":
return "codex_responses"
+ from hermes_cli.models import opencode_model_api_mode
+ from hermes_cli.runtime_provider_custom import _opencode_family_for_custom
+ opencode_family = _opencode_family_for_custom(fb_provider, fb_base_url)
+ if opencode_family is not None:
+ # OpenCode Zen/Go/free serve Responses-only (muse-spark, gpt-*, grok-*), anthropic_messages
+ # (minimax, qwen) and chat_completions models behind one provider; the primary /model path
+ # already re-derives per model — the fallback wire must agree (#102148).
+ return opencode_model_api_mode(opencode_family, fb_model)
if fb_provider in {"nous", "nous-portal", "nousresearch"}:
# Portal is dual-wire: anthropic/* must land on /v1/messages (the swap rebuilds the native client).
from hermes_cli.providers import nous_api_mode
diff --git a/tests/agent/test_fallback_api_mode_preservation.py b/tests/agent/test_fallback_api_mode_preservation.py
index 05ad206bcd9..71e651d5d0e 100644
--- a/tests/agent/test_fallback_api_mode_preservation.py
+++ b/tests/agent/test_fallback_api_mode_preservation.py
@@ -14,6 +14,8 @@ Three behaviors salvaged from PR #79787:
from unittest.mock import MagicMock, patch
+import pytest
+
from run_agent import AIAgent
@@ -196,3 +198,28 @@ class TestPlainFallbackUnchanged:
agent = _make_agent(fallback_model=fbs)
_activate(agent, "https://api.anthropic.com/v1", "claude-opus-4-6")
assert agent.api_mode == "anthropic_messages"
+
+
+class TestOpenCodeFamilyPerModelWire:
+ """OpenCode Zen/Go/free serve Responses-only, Anthropic-wire and chat-completions models behind
+ one provider; a fallback entry must land on the same wire the primary /model path picks
+ (#102148: muse-spark on opencode-go was sent to /chat/completions → deterministic 500)."""
+
+ @pytest.mark.parametrize(
+ ("entry", "resolved_base_url", "expected_mode"),
+ [
+ ({"provider": "opencode-go", "model": "muse-spark-1.3-contributor"}, "https://opencode.ai/zen/go/v1", "codex_responses"),
+ ({"provider": "opencode-free", "model": "muse-spark-1.3-contributor-free"}, "https://opencode.ai/zen/v1", "codex_responses"),
+ ({"provider": "opencode-go", "model": "minimax-m2.7"}, "https://opencode.ai/zen/go/v1", "anthropic_messages"),
+ ({"provider": "custom", "model": "muse-spark-1.3-contributor", "base_url": "https://opencode.ai/zen/go/v1", "api_key": "k"},
+ "https://opencode.ai/zen/go/v1", "codex_responses"),
+ # controls: a chat-completions model stays put; an explicit pin always wins.
+ ({"provider": "opencode-go", "model": "deepseek-v4-flash"}, "https://opencode.ai/zen/go/v1", "chat_completions"),
+ ({"provider": "opencode-go", "model": "muse-spark-1.3-contributor", "api_mode": "chat_completions"},
+ "https://opencode.ai/zen/go/v1", "chat_completions"),
+ ],
+ )
+ def test_fallback_wire_matches_opencode_model_table(self, entry, resolved_base_url, expected_mode):
+ agent = _make_agent(fallback_model=[entry])
+ _activate(agent, resolved_base_url, entry["model"])
+ assert agent.api_mode == expected_mode