Skip to content

The agent that grows with you

fix(agent): preserve provider-slug rewrite when host mandate fires

Lavied4658ee6d560
diff --git a/agent/agent_init.py b/agent/agent_init.py
index b0b69b41d5d..c27806080a3 100644
--- a/agent/agent_init.py
+++ b/agent/agent_init.py
@@ -671,10 +671,46 @@ def init_agent(
agent.acp_args = list(acp_args or args or [])
if api_mode in {"chat_completions", "codex_responses", "anthropic_messages", "bedrock_converse", "codex_app_server"}:
agent.api_mode = api_mode
+ elif agent.provider == "openai-codex":
+ agent.api_mode = "codex_responses"
+ elif agent.provider in {"xai", "xai-oauth"}:
+ agent.api_mode = "codex_responses"
+ elif (provider_name is None) and (
+ agent._base_url_hostname == "chatgpt.com"
+ and "/backend-api/codex" in agent._base_url_lower
+ ):
+ agent.api_mode = "codex_responses"
+ agent.provider = "openai-codex"
+ elif (provider_name is None) and agent._base_url_hostname == "api.x.ai":
+ agent.api_mode = "codex_responses"
+ agent.provider = "xai"
+ elif agent.provider == "anthropic" or (provider_name is None and agent._base_url_hostname == "api.anthropic.com"):
+ agent.api_mode = "anthropic_messages"
+ agent.provider = "anthropic"
+ elif agent._base_url_lower.rstrip("/").endswith("/anthropic"):
+ # Third-party Anthropic-compatible endpoints (e.g. MiniMax, DashScope)
+ # use a URL convention ending in /anthropic. Auto-detect these so the
+ # Anthropic Messages API adapter is used instead of chat completions.
+ agent.api_mode = "anthropic_messages"
+ elif agent.provider == "bedrock" or (
+ agent._base_url_hostname.startswith("bedrock-runtime.")
+ and base_url_host_matches(agent._base_url_lower, "amazonaws.com")
+ ):
+ # AWS Bedrock — auto-detect from provider name or base URL
+ # (bedrock-runtime.<region>.amazonaws.com).
+ agent.api_mode = "bedrock_converse"
+ elif agent.provider in {"nous", "nous-portal", "nousresearch"}:
+ # Portal is dual-wire: anthropic/* → Messages, everything else →
+ # chat_completions. Callers that already pass api_mode win above;
+ # this covers direct AIAgent construction without a resolved runtime.
+ from hermes_cli.providers import nous_api_mode
+
+ agent.api_mode = nous_api_mode(agent.model)
else:
- # Host-mandated wire check (single source of truth via providers.host_mandated_api_mode).
- # Covers api.meta.ai → codex_responses for prompt caching (0% on chat vs 93-99% on responses)
- # and any future host mandates. Lazy import to avoid circular imports.
+ # Host-mandated wire check — LAST, so the elif chain's provider-slug
+ # rewrites (e.g. api.anthropic.com → provider="anthropic", #63425)
+ # always run first. Covers api.meta.ai → codex_responses for prompt
+ # caching (0% on chat vs 93-99% on responses) and future mandates.
try:
from hermes_cli.providers import host_mandated_api_mode as _host_mandated_api_mode
@@ -683,41 +719,6 @@ def init_agent(
_mandated = None
if _mandated is not None:
agent.api_mode = _mandated
- elif agent.provider == "openai-codex":
- agent.api_mode = "codex_responses"
- elif agent.provider in {"xai", "xai-oauth"}:
- agent.api_mode = "codex_responses"
- elif (provider_name is None) and (
- agent._base_url_hostname == "chatgpt.com"
- and "/backend-api/codex" in agent._base_url_lower
- ):
- agent.api_mode = "codex_responses"
- agent.provider = "openai-codex"
- elif (provider_name is None) and agent._base_url_hostname == "api.x.ai":
- agent.api_mode = "codex_responses"
- agent.provider = "xai"
- elif agent.provider == "anthropic" or (provider_name is None and agent._base_url_hostname == "api.anthropic.com"):
- agent.api_mode = "anthropic_messages"
- agent.provider = "anthropic"
- elif agent._base_url_lower.rstrip("/").endswith("/anthropic"):
- # Third-party Anthropic-compatible endpoints (e.g. MiniMax, DashScope)
- # use a URL convention ending in /anthropic. Auto-detect these so the
- # Anthropic Messages API adapter is used instead of chat completions.
- agent.api_mode = "anthropic_messages"
- elif agent.provider == "bedrock" or (
- agent._base_url_hostname.startswith("bedrock-runtime.")
- and base_url_host_matches(agent._base_url_lower, "amazonaws.com")
- ):
- # AWS Bedrock — auto-detect from provider name or base URL
- # (bedrock-runtime.<region>.amazonaws.com).
- agent.api_mode = "bedrock_converse"
- elif agent.provider in {"nous", "nous-portal", "nousresearch"}:
- # Portal is dual-wire: anthropic/* → Messages, everything else →
- # chat_completions. Callers that already pass api_mode win above;
- # this covers direct AIAgent construction without a resolved runtime.
- from hermes_cli.providers import nous_api_mode
-
- agent.api_mode = nous_api_mode(agent.model)
else:
agent.api_mode = "chat_completions"
diff --git a/tests/agent/test_meta_agent_init.py b/tests/agent/test_meta_agent_init.py
index de9e790bf5d..6eaf0cbffbe 100644
--- a/tests/agent/test_meta_agent_init.py
+++ b/tests/agent/test_meta_agent_init.py
@@ -81,3 +81,82 @@ def test_agent_init_meta_url_case_insensitive():
skip_memory=True,
)
assert agent.api_mode == "codex_responses"
+
+
+def test_agent_init_anthropic_url_implies_provider_and_api_mode():
+ """Regression for host-mandated api_mode shadowing provider-slug rewrite (#63425).
+
+ When provider=None and base_url is api.anthropic.com, the elif chain must
+ still rewrite provider to "anthropic" before the credential-pool check.
+ The host-mandated fallback moved to the bottom of the cascade (fix/meta-prompt-caching)
+ ensures this; the old top-position mandated branch set api_mode without the slug rewrite
+ and caused credential-pool validation to discard anthropic-scoped pools.
+ """
+ from unittest.mock import MagicMock, patch
+
+ from run_agent import AIAgent
+
+ with patch("agent.anthropic_adapter.build_anthropic_client", return_value=MagicMock()), patch(
+ "agent.anthropic_adapter._is_oauth_token", return_value=False
+ ):
+ agent = AIAgent(
+ provider=None,
+ base_url="https://api.anthropic.com/v1",
+ api_key="sk-test-anthropic",
+ model="claude-sonnet-4-5",
+ api_mode=None,
+ quiet_mode=True,
+ skip_context_files=True,
+ skip_memory=True,
+ )
+ assert agent.provider == "anthropic"
+ assert agent.api_mode == "anthropic_messages"
+
+
+def test_agent_init_anthropic_url_preserves_credential_pool():
+ """Anthropic-scoped credential pool must survive provider auto-detection.
+
+ Mirrors tests/run_agent/test_63425_credential_pool_auto_detect.py at the
+ public AIAgent surface (provider=None URL detection).
+ """
+ from types import SimpleNamespace
+ from unittest.mock import MagicMock, patch
+
+ from run_agent import AIAgent
+
+ pool = SimpleNamespace(provider="anthropic")
+
+ with patch("agent.anthropic_adapter.build_anthropic_client", return_value=MagicMock()), patch(
+ "agent.anthropic_adapter._is_oauth_token", return_value=False
+ ):
+ agent = AIAgent(
+ provider=None,
+ base_url="https://api.anthropic.com/v1",
+ api_key="sk-test-anthropic",
+ model="claude-sonnet-4-5",
+ api_mode=None,
+ credential_pool=pool,
+ quiet_mode=True,
+ skip_context_files=True,
+ skip_memory=True,
+ )
+ assert agent.provider == "anthropic"
+ assert agent.api_mode == "anthropic_messages"
+ assert agent._credential_pool is pool
+
+
+def test_agent_init_meta_url_without_provider_implies_codex_responses():
+ from run_agent import AIAgent
+
+ agent = AIAgent(
+ provider=None,
+ base_url="https://api.meta.ai/v1",
+ api_key="sk-test-meta",
+ model="muse-spark-1.2",
+ api_mode=None,
+ quiet_mode=True,
+ skip_context_files=True,
+ skip_memory=True,
+ )
+ assert agent.api_mode == "codex_responses"
+