The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: explicit fallback api_mode always wins; clean up dead-code guard
Tekniumd0be93bd9a6f
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex 44fd782fa65..e618488e765 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -2035,10 +2035,16 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> boolfb_base_url_hint = (fb.get("base_url") or "").strip() or Nonefb_api_key_hint = resolve_entry_api_key(fb)# Determine api_mode from the ORIGINAL base_url (before URL transformation).- # resolve_provider_client() calls _to_openai_base_url() which rewrites- # /anthropic to /v1, losing the anthropic wire signal. Pre-compute here.+ # resolve_provider_client() calls _to_openai_base_url() which can rewrite+ # a dual-surface /anthropic base to /v1, losing the Anthropic wire signal+ # from the client's post-rewrite base_url. Pre-compute here so detection+ # sees the URL the user actually configured. (#79787)+ #+ # An explicit ``api_mode`` on the fallback entry always wins — including+ # an explicit "chat_completions" — and suppresses all re-detection below.+ fb_api_mode_explicit = bool(str(fb.get("api_mode") or "").strip())fb_api_mode = "chat_completions"- if fb.get("api_mode"):+ if fb_api_mode_explicit:fb_api_mode = str(fb.get("api_mode")).strip()elif fb_provider == "anthropic":# Provider-name check must not be gated on fb_base_url_hint:@@ -2082,15 +2088,14 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> boolfb_model, fb_provider, _norm_err,)- # Determine api_mode from provider / base URL / model- # If fb_api_mode was already pre-computed from original URL, preserve it- if not locals().get('fb_api_mode'):- fb_api_mode = "chat_completions"+ # Re-determine api_mode from provider / resolved base URL / model when+ # the pre-computed pass above landed on the default and the user did+ # not pin api_mode explicitly. An explicit fb.api_mode (even+ # "chat_completions") must never be overridden here.fb_base_url = str(fb_client.base_url)_fb_is_azure = agent._is_azure_openai_url(fb_base_url)-- # Only re-determine if not already set from original URL- if fb_api_mode == "chat_completions":++ if not fb_api_mode_explicit and fb_api_mode == "chat_completions":if fb_provider == "openai-codex":fb_api_mode = "codex_responses"elif fb_provider in {"nous", "nous-portal", "nousresearch"}:@@ -2101,6 +2106,16 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> boolfrom hermes_cli.providers import nous_api_modefb_api_mode = nous_api_mode(fb_model)+ elif (+ fb_base_url.rstrip("/").lower().endswith("/anthropic")+ or base_url_hostname(fb_base_url) == "api.anthropic.com"+ ):+ # Named custom providers (e.g. cron-anthropic) resolve their+ # base_url from config rather than the fallback entry, so the+ # pre-resolve hint check above never sees it. Match the host+ # the same way determine_api_mode() and _detect_api_mode_for_url()+ # do on the primary path. (#32243, #49247)+ fb_api_mode = "anthropic_messages"elif _fb_is_azure:# Azure OpenAI serves gpt-5.x on /chat/completions — does NOT# support the Responses API. Stay on chat_completions.diff --git a/tests/run_agent/test_fallback_api_mode_preservation.py b/tests/run_agent/test_fallback_api_mode_preservation.pynew file mode 100644index 00000000000..ccda8fac3a8--- /dev/null+++ b/tests/run_agent/test_fallback_api_mode_preservation.py@@ -0,0 +1,182 @@+"""Fallback activation must preserve the Anthropic wire signal (PR #79787).++Three behaviors salvaged from PR #79787:++1. An explicit ``api_mode`` on the fallback entry is honored — and always+ wins, including an explicit ``chat_completions`` that would otherwise be+ overridden by codex_responses / bedrock re-detection.+2. ``fb_api_mode`` is detected from the ORIGINAL ``base_url`` hint before+ ``resolve_provider_client`` / ``_to_openai_base_url`` can rewrite a+ dual-surface ``/anthropic`` base to ``/v1``.+3. ``api_mode`` is passed into ``resolve_provider_client`` at the fallback+ call site so the resolver keeps the Anthropic wire for custom bases.+"""++from unittest.mock import MagicMock, patch++from run_agent import AIAgent+++def _make_agent(fallback_model=None):+ with (+ patch("run_agent.get_tool_definitions", return_value=[]),+ patch("run_agent.check_toolset_requirements", return_value={}),+ patch("run_agent.OpenAI"),+ ):+ agent = AIAgent(+ api_key="test-key",+ base_url="https://openrouter.ai/api/v1",+ quiet_mode=True,+ skip_context_files=True,+ skip_memory=True,+ fallback_model=fallback_model,+ )+ agent.client = MagicMock()+ return agent+++def _mock_client(base_url="https://openrouter.ai/api/v1", api_key="fb-key"):+ mock = MagicMock()+ mock.base_url = base_url+ mock.api_key = api_key+ return mock+++def _activate(agent, resolved_base_url, resolved_model, build_anthropic=None):+ """Run _try_activate_fallback with the standard mock stack.++ Returns the mock for resolve_provider_client so callers can assert on+ the api_mode kwarg passed at the fallback call site.+ """+ patches = [+ patch(+ "agent.chat_completion_helpers._fallback_entry_unavailable_without_network",+ return_value=None,+ ),+ patch(+ "agent.auxiliary_client.resolve_provider_client",+ return_value=(+ _mock_client(base_url=resolved_base_url),+ resolved_model,+ ),+ ),+ patch(+ "hermes_cli.model_normalize.normalize_model_for_provider",+ side_effect=lambda m, p: m,+ ),+ patch(+ "agent.anthropic_adapter.build_anthropic_client",+ side_effect=build_anthropic+ or (lambda api_key, base_url, timeout=None, **kw: MagicMock()),+ ),+ ]+ with patches[0], patches[1] as mock_rpc, patches[2], patches[3]:+ assert agent._try_activate_fallback() is True+ return mock_rpc+++class TestExplicitApiModeHonored:+ def test_explicit_anthropic_messages_honored(self):+ fbs = [{+ "provider": "custom",+ "model": "claude-opus-4-6",+ "base_url": "https://gateway.example.com/v1",+ "api_key": "k",+ "api_mode": "anthropic_messages",+ }]+ agent = _make_agent(fallback_model=fbs)+ mock_rpc = _activate(agent, "https://gateway.example.com/v1", "claude-opus-4-6")+ assert agent.api_mode == "anthropic_messages"+ # Behavior (3): api_mode forwarded to the resolver.+ assert mock_rpc.call_args.kwargs["api_mode"] == "anthropic_messages"++ def test_explicit_chat_completions_not_overridden_by_redetection(self):+ """An explicit chat_completions must survive re-detection.++ api.openai.com would normally re-detect to codex_responses via+ _is_direct_openai_url; the explicit config field must win.+ """+ fbs = [{+ "provider": "custom",+ "model": "gpt-5.2",+ "base_url": "https://api.openai.com/v1",+ "api_key": "k",+ "api_mode": "chat_completions",+ }]+ agent = _make_agent(fallback_model=fbs)+ _activate(agent, "https://api.openai.com/v1", "gpt-5.2")+ assert agent.api_mode == "chat_completions"++ def test_explicit_chat_completions_not_overridden_by_bedrock_redetection(self):+ fbs = [{+ "provider": "bedrock",+ "model": "anthropic.claude-3-5-sonnet",+ "api_key": "k",+ "api_mode": "chat_completions",+ }]+ agent = _make_agent(fallback_model=fbs)+ _activate(+ agent,+ "https://bedrock-runtime.us-east-1.amazonaws.com",+ "anthropic.claude-3-5-sonnet",+ )+ assert agent.api_mode == "chat_completions"+++class TestOriginalUrlDetection:+ def test_anthropic_suffix_hint_survives_rewrite(self):+ """Dual-surface /anthropic base rewritten to /v1 by the resolver:+ detection must run on the ORIGINAL hint, not the rewritten client URL.+ """+ fbs = [{+ "provider": "custom",+ "model": "MiniMax-M2.5",+ "base_url": "https://api.minimax.io/anthropic",+ "api_key": "k",+ }]+ agent = _make_agent(fallback_model=fbs)+ mock_rpc = _activate(agent, "https://api.minimax.io/v1", "MiniMax-M2.5")+ assert agent.api_mode == "anthropic_messages"+ assert mock_rpc.call_args.kwargs["api_mode"] == "anthropic_messages"++ def test_anthropic_host_hint_detected(self):+ fbs = [{+ "provider": "custom",+ "model": "claude-opus-4-6",+ "base_url": "https://api.anthropic.com",+ "api_key": "k",+ }]+ agent = _make_agent(fallback_model=fbs)+ _activate(agent, "https://api.anthropic.com", "claude-opus-4-6")+ assert agent.api_mode == "anthropic_messages"++ def test_provider_anthropic_without_base_url(self):+ """provider: anthropic with no explicit base_url must still resolve+ to anthropic_messages (follow-up commit 38303343 in PR #79787)."""+ fbs = [{"provider": "anthropic", "model": "claude-opus-4-6", "api_key": "k"}]+ agent = _make_agent(fallback_model=fbs)+ mock_rpc = _activate(agent, "https://api.anthropic.com", "claude-opus-4-6")+ assert agent.api_mode == "anthropic_messages"+ assert mock_rpc.call_args.kwargs["api_mode"] == "anthropic_messages"+++class TestPlainFallbackUnchanged:+ def test_plain_openrouter_fallback_stays_chat_completions(self):+ fbs = [{+ "provider": "openrouter",+ "model": "z-ai/glm-5",+ "api_key": "k",+ }]+ agent = _make_agent(fallback_model=fbs)+ mock_rpc = _activate(agent, "https://openrouter.ai/api/v1", "z-ai/glm-5")+ assert agent.api_mode == "chat_completions"+ assert mock_rpc.call_args.kwargs["api_mode"] == "chat_completions"++ def test_post_resolve_anthropic_host_still_detected(self):+ """Named custom providers resolve api.anthropic.com from config, not+ the fallback entry — post-resolve detection must still catch it+ (#32243, #49247)."""+ fbs = [{"provider": "cron-anthropic", "model": "claude-opus-4-6", "api_key": "k"}]+ agent = _make_agent(fallback_model=fbs)+ _activate(agent, "https://api.anthropic.com/v1", "claude-opus-4-6")+ assert agent.api_mode == "anthropic_messages"