AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): normalize model name based on actual transport, not config shape (#12932)
Zamil Majdy932ecd3a079f
diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.pyindex c844a8ac45..2dad177e44 100644--- a/autogpt_platform/backend/backend/copilot/config.py+++ b/autogpt_platform/backend/backend/copilot/config.py@@ -395,11 +395,17 @@ class ChatConfig(BaseSettings):@propertydef openrouter_active(self) -> bool:- """True when OpenRouter is enabled AND credentials are usable.+ """True when OpenRouter config is shape-valid (flag + credentials).- Single source of truth for "will the SDK route through OpenRouter?".- Checks the flag *and* that ``api_key`` + a valid ``base_url`` are- present — mirrors the fallback logic in ``build_sdk_env``.+ Indicates whether OpenRouter settings are present and usable —+ ``use_openrouter`` set, plus ``api_key`` + a valid ``base_url``,+ mirroring the fallback logic in ``build_sdk_env``.++ Note: this is a **config-shape check only**. Runtime SDK routing+ is governed by ``effective_transport`` — subscription mode+ bypasses OpenRouter entirely even when these fields are set, so+ callers asking "will the SDK actually route through OpenRouter+ for this turn?" should use ``effective_transport`` instead."""if not self.use_openrouter:return False@@ -408,6 +414,34 @@ class ChatConfig(BaseSettings):base = base[:-3]return bool(self.api_key and base and base.startswith("http"))+ @property+ def effective_transport(+ self,+ ) -> Literal["subscription", "openrouter", "direct_anthropic"]:+ """The transport the SDK CLI subprocess actually uses for this turn.++ Detection order:++ 1. ``subscription`` — when ``use_claude_code_subscription`` is True+ the CLI uses OAuth from the keychain or+ ``CLAUDE_CODE_OAUTH_TOKEN`` and ignores ``CHAT_BASE_URL`` /+ ``CHAT_API_KEY`` entirely (see ``build_sdk_env`` mode 1).+ 2. ``openrouter`` — when ``openrouter_active`` (use_openrouter ++ api_key + a valid base_url).+ 3. ``direct_anthropic`` — fallback (CLI talks to api.anthropic.com+ with ``ANTHROPIC_API_KEY`` from parent env).++ Use this when the question is "which model-name format will the+ CLI accept?" — the OpenRouter slug ``anthropic/claude-opus-4.7``+ works through the proxy but is rejected by the subscription /+ direct-Anthropic transports.+ """+ if self.use_claude_code_subscription:+ return "subscription"+ if self.openrouter_active:+ return "openrouter"+ return "direct_anthropic"+@propertydef e2b_active(self) -> bool:"""True when E2B is enabled and the API key is present.@@ -532,9 +566,13 @@ class ChatConfig(BaseSettings):(``claude_agent_fallback_model`` via ``_resolve_fallback_model``).Skipped when ``use_claude_code_subscription=True`` because the- subscription path resolves the model to ``None`` (CLI default)- and never calls ``_normalize_model_name``. Empty fallback strings- are also skipped (no fallback configured).+ subscription path normally resolves the static config to ``None``+ (CLI default). An LD-served override under subscription does+ flow through ``_normalize_model_name``; the runtime guard first+ falls back to the tier default, and only avoids a request error+ when that default is itself valid (otherwise the original LD+ ValueError is re-raised — see ``_resolve_sdk_model_for_request``).+ Empty fallback strings are also skipped (no fallback configured)."""if self.use_claude_code_subscription:return selfdiff --git a/autogpt_platform/backend/backend/copilot/sdk/conftest.py b/autogpt_platform/backend/backend/copilot/sdk/conftest.pyindex 879e7b346e..724034469a 100644--- a/autogpt_platform/backend/backend/copilot/sdk/conftest.py+++ b/autogpt_platform/backend/backend/copilot/sdk/conftest.py@@ -11,6 +11,33 @@ import pytest_asynciofrom backend.util import json+# ---------------------------------------------------------------------------+# Env vars that ``ChatConfig`` validators read — must be cleared so explicit+# constructor values are used. Centralised here so adding a new env-backed+# field only needs one update across the SDK test suite.+# ---------------------------------------------------------------------------+_CONFIG_ENV_VARS = (+ "CHAT_USE_OPENROUTER",+ "CHAT_API_KEY",+ "OPEN_ROUTER_API_KEY",+ "OPENAI_API_KEY",+ "CHAT_BASE_URL",+ "OPENROUTER_BASE_URL",+ "OPENAI_BASE_URL",+ "CHAT_USE_CLAUDE_CODE_SUBSCRIPTION",+ "CHAT_USE_CLAUDE_AGENT_SDK",+ "CHAT_CLAUDE_AGENT_CROSS_USER_PROMPT_CACHE",+ "CHAT_CLAUDE_AGENT_CLI_PATH",+ "CLAUDE_AGENT_CLI_PATH",+)+++def _clean_config_env(monkeypatch: pytest.MonkeyPatch) -> None:+ """Clear env-backed CHAT_* settings so ChatConfig uses constructor values."""+ for var in _CONFIG_ENV_VARS:+ monkeypatch.delenv(var, raising=False)+@pytest_asyncio.fixture(scope="session", loop_scope="session", name="server")async def _server_noop() -> None:diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex a1220658f0..929ddb9b3c 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -919,38 +919,46 @@ async def _iter_sdk_messages(def _normalize_model_name(raw_model: str) -> str:- """Normalize a model name for the current routing configuration.+ """Normalize a model name for the **actual** SDK CLI transport.- Two routing modes:+ Three transports (see ``ChatConfig.effective_transport``):- 1. **OpenRouter active** — the canonical OpenRouter slug is- ``"<vendor>/<model>"`` (e.g. ``"anthropic/claude-opus-4.6"``,- ``"moonshotai/kimi-k2.6"``). Pass the prefixed name through+ 1. **OpenRouter** — the canonical OpenRouter slug is+ ``"<vendor>/<model>"`` (e.g. ``"anthropic/claude-opus-4-6"``,+ ``"moonshotai/kimi-k2-6"``). Pass the prefixed name throughunchanged so OpenRouter can route to the correct provider. Anthropicnames happen to also resolve when stripped, but non-Anthropic vendors(Moonshot, Google, etc.) do not — keeping the prefix is the only formthat works for every model in the catalog.- 2. **Direct Anthropic** — strip the OpenRouter ``anthropic/`` prefix- and convert dots to hyphens (``"claude-opus-4.6"`` →- ``"claude-opus-4-6"``) since the Anthropic Messages API rejects- both the prefix and dot-separated versions. Raises ``ValueError``- when a non-Anthropic vendor slug is paired with direct-Anthropic- mode — silently stripping ``moonshotai/`` would send ``kimi-k2.6``- to the Anthropic API and produce an opaque ``model_not_found``- error far from the misconfiguration source.+ 2. **Subscription / Direct Anthropic** — strip the OpenRouter+ ``anthropic/`` prefix and convert dots to hyphens+ (``"claude-opus-4.6"`` → ``"claude-opus-4-6"``). The CLI subprocess+ (subscription mode) and the Anthropic Messages API both reject the+ prefix and dot-separated versions. Raises ``ValueError`` when a+ non-Anthropic vendor slug is paired with these transports — silently+ stripping ``moonshotai/`` would send ``kimi-k2-6`` to the Anthropic+ API / CLI and produce an opaque ``model_not_found`` error far from+ the misconfiguration source.++ Gating on the **actual transport** (not just config shape) matters+ because subscription mode and OpenRouter config can coexist —+ ``CHAT_USE_CLAUDE_CODE_SUBSCRIPTION=true`` paired with a populated+ ``CHAT_BASE_URL`` / ``CHAT_API_KEY`` (left over from an earlier+ OpenRouter setup) used to incorrectly pass ``anthropic/claude-opus-4-7``+ to the CLI subprocess, which the CLI rejects."""- if config.openrouter_active:+ if config.effective_transport == "openrouter":return raw_modelmodel = raw_modelif "/" in model:vendor, model = model.split("/", 1)if vendor != "anthropic":raise ValueError(- f"Direct-Anthropic mode (use_openrouter=False or missing "- f"OpenRouter credentials) requires an Anthropic model, got "- f"vendor={vendor!r} from model={raw_model!r}. Set "- f"CHAT_THINKING_STANDARD_MODEL/CHAT_THINKING_ADVANCED_MODEL "- f"to an anthropic/* slug, or enable OpenRouter."+ f"{config.effective_transport!r} transport requires an "+ f"Anthropic model, got vendor={vendor!r} from "+ f"model={raw_model!r}. Set CHAT_THINKING_STANDARD_MODEL/"+ f"CHAT_THINKING_ADVANCED_MODEL to an anthropic/* slug, or "+ f"enable OpenRouter.")return model.replace(".", "-")diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex 09a31dbedd..8683ca7504 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -13,6 +13,9 @@ from unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom claude_agent_sdk import AssistantMessage, TextBlock, ToolUseBlock+from backend.copilot import config as cfg_mod+from backend.copilot.config import ChatConfig+from .conftest import build_test_transcript as _build_transcriptfrom .service import (_RETRY_TARGET_TOKENS,@@ -23,6 +26,7 @@ from .service import (_iter_sdk_messages,_normalize_model_name,_reduce_context,+ _resolve_sdk_model_for_request,_restore_cli_session_for_turn,_TokenUsage,)@@ -373,15 +377,15 @@ class TestNormalizeModelName:"""@pytest.fixture- def _direct_anthropic_config(self, monkeypatch: pytest.MonkeyPatch):+ def _direct_anthropic_config(+ self, monkeypatch: pytest.MonkeyPatch, _clean_config_env: None+ ):"""Force ``config.openrouter_active = False`` for prefix-strip tests.Pins the SDK model fields to anthropic/* so the new``_validate_sdk_model_vendor_compatibility`` model_validatorpermits ChatConfig construction."""- from backend.copilot import config as cfg_mod-cfg = cfg_mod.ChatConfig(use_openrouter=False,api_key=None,@@ -393,10 +397,10 @@ class TestNormalizeModelName:monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)@pytest.fixture- def _openrouter_config(self, monkeypatch: pytest.MonkeyPatch):+ def _openrouter_config(+ self, monkeypatch: pytest.MonkeyPatch, _clean_config_env: None+ ):"""Force ``config.openrouter_active = True`` for slug-preservation tests."""- from backend.copilot import config as cfg_mod-cfg = cfg_mod.ChatConfig(use_openrouter=True,api_key="or-key",@@ -445,6 +449,172 @@ class TestNormalizeModelName:"""Non-Anthropic vendors (Moonshot) require the prefix to route."""assert _normalize_model_name("moonshotai/kimi-k2.6") == "moonshotai/kimi-k2.6"+ @pytest.fixture+ def _subscription_with_openrouter_config(+ self, monkeypatch: pytest.MonkeyPatch, _clean_config_env: None+ ):+ """Subscription mode with leftover OpenRouter base_url + api_key.++ Reproduces the bug: ``CHAT_USE_CLAUDE_CODE_SUBSCRIPTION=true`` plus+ a populated ``CHAT_BASE_URL`` (e.g. left over from an earlier+ OpenRouter setup) used to incorrectly preserve the OpenRouter slug+ because the gate checked config shape (``openrouter_active``) not+ actual transport. The CLI subprocess uses OAuth here and rejects+ the OpenRouter format.+ """+ cfg = cfg_mod.ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ use_claude_code_subscription=True,+ )+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)++ def test_subscription_strips_anthropic_prefix_despite_openrouter_config(+ self, _subscription_with_openrouter_config+ ):+ """Subscription transport must produce the CLI-friendly form even+ when OpenRouter base_url + api_key are set — the CLI uses OAuth+ and ignores those fields, so the OpenRouter slug would be rejected."""+ assert _normalize_model_name("anthropic/claude-opus-4.7") == "claude-opus-4-7"++ def test_subscription_rejects_non_anthropic_vendor(+ self, _subscription_with_openrouter_config+ ):+ """The CLI subprocess can only talk to Anthropic models — Kimi via+ Moonshot must raise so the resolver falls back to a tier default+ instead of feeding an unroutable slug to the CLI."""+ with pytest.raises(ValueError, match="requires an Anthropic model"):+ _normalize_model_name("moonshotai/kimi-k2.6")+++# ---------------------------------------------------------------------------+# ChatConfig.effective_transport — single source of truth for "which+# transport will the SDK CLI actually use?"+# ---------------------------------------------------------------------------+++class TestEffectiveTransport:+ """Subscription mode wins over OpenRouter even when OpenRouter+ base_url + api_key are set, because the CLI subprocess uses OAuth and+ ignores ``CHAT_BASE_URL`` / ``CHAT_API_KEY`` (see ``build_sdk_env``+ mode 1). Picking the right transport here is what lets+ ``_normalize_model_name`` produce the correct model-name format.+ """++ def test_subscription_wins_over_openrouter_config(self, _clean_config_env):+ cfg = ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ use_claude_code_subscription=True,+ )+ assert cfg.effective_transport == "subscription"+ # ``openrouter_active`` is still True (config-shape check) but+ # the actual transport is subscription.+ assert cfg.openrouter_active is True++ def test_openrouter_when_subscription_disabled(self, _clean_config_env):+ cfg = ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ use_claude_code_subscription=False,+ )+ assert cfg.effective_transport == "openrouter"++ def test_direct_anthropic_when_no_openrouter_no_subscription(+ self, _clean_config_env+ ):+ cfg = ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ thinking_standard_model="anthropic/claude-sonnet-4-6",+ thinking_advanced_model="anthropic/claude-opus-4-7",+ )+ assert cfg.effective_transport == "direct_anthropic"++ def test_subscription_alone_is_subscription(self, _clean_config_env):+ cfg = ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=True,+ )+ assert cfg.effective_transport == "subscription"+++# ---------------------------------------------------------------------------+# _resolve_sdk_model_for_request — transport-aware LD-override normalisation+# ---------------------------------------------------------------------------+++class TestResolveSdkModelForRequestTransportAware:+ """When subscription mode is on but the deployment also has OpenRouter+ config populated (e.g. ``CHAT_BASE_URL`` left over from a previous+ setup), an LD-served override must be normalised for the **subscription+ CLI**, not passed through as the OpenRouter slug. The CLI subprocess+ uses OAuth and rejects ``anthropic/claude-opus-4.7`` with the model+ error reproduced in local debugging:++ ``There's an issue with the selected model+ (anthropic/claude-opus-4.7). It may not exist or you may not have+ access to it.``+ """++ @pytest.mark.asyncio+ async def test_subscription_advanced_override_normalised_for_cli(+ self, monkeypatch: pytest.MonkeyPatch, _clean_config_env: None+ ):+ cfg = cfg_mod.ChatConfig(+ thinking_standard_model="anthropic/claude-sonnet-4-6",+ thinking_advanced_model="anthropic/claude-opus-4.7",+ claude_agent_model=None,+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ use_claude_code_subscription=True,+ )+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)++ with patch(+ "backend.copilot.sdk.service._resolve_thinking_model_for_user",+ new=AsyncMock(return_value="anthropic/claude-opus-4.7"),+ ):+ resolved = await _resolve_sdk_model_for_request(+ model="advanced", session_id="sess-adv", user_id="user-1"+ )+ # NOT the OpenRouter slug, NOT None — the CLI-friendly hyphenated form.+ assert resolved == "claude-opus-4-7"++ @pytest.mark.asyncio+ async def test_subscription_standard_no_override_returns_none(+ self, monkeypatch: pytest.MonkeyPatch, _clean_config_env: None+ ):+ """When LD agrees with the config default, subscription mode still+ wins on the standard tier — returns ``None`` so the CLI picks the+ subscription default model."""+ cfg = cfg_mod.ChatConfig(+ thinking_standard_model="anthropic/claude-sonnet-4-6",+ claude_agent_model=None,+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ use_claude_code_subscription=True,+ )+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)++ with patch(+ "backend.copilot.sdk.service._resolve_thinking_model_for_user",+ new=AsyncMock(return_value="anthropic/claude-sonnet-4-6"),+ ):+ resolved = await _resolve_sdk_model_for_request(+ model="standard", session_id="sess-std", user_id="user-1"+ )+ assert resolved is None+# ---------------------------------------------------------------------------# _TokenUsage — null-safe accumulation (OpenRouter initial-stream-event bug)diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_test.pyindex 3e02e99fee..180219db4b 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_test.py@@ -325,27 +325,11 @@ class TestCleanupSdkToolResults:# ----------------------------------------------------------------------------# Env vars that ChatConfig validators read — must be cleared so explicit-# constructor values are used.+# Env-cleanup fixture is shared via ``conftest._clean_config_env``. This+# file exposes a re-export for callers that don't rely on conftest discovery+# (kept for backwards compatibility — pytest finds the conftest fixture+# automatically without an explicit import).# ----------------------------------------------------------------------------_CONFIG_ENV_VARS = (- "CHAT_USE_OPENROUTER",- "CHAT_API_KEY",- "OPEN_ROUTER_API_KEY",- "OPENAI_API_KEY",- "CHAT_BASE_URL",- "OPENROUTER_BASE_URL",- "OPENAI_BASE_URL",- "CHAT_USE_CLAUDE_CODE_SUBSCRIPTION",- "CHAT_USE_CLAUDE_AGENT_SDK",- "CHAT_CLAUDE_AGENT_CROSS_USER_PROMPT_CACHE",-)---def _clean_config_env(monkeypatch: pytest.MonkeyPatch) -> None:- for var in _CONFIG_ENV_VARS:- monkeypatch.delenv(var, raising=False)class TestNormalizeModelName:@@ -619,7 +603,13 @@ class TestResolveSdkModelForRequestLdFallback:on ``copilot-model-routing[thinking][standard]`` returned``None`` (CLI picked subscription default Opus), silentlyignoring the LD override. An LD value different from the- config default is an explicit admin decision and must win."""+ config default is an explicit admin decision and must win.++ Subscription transport rejects non-Anthropic vendors (the CLI+ subprocess can't talk to Moonshot), so the resolver fails soft+ to the tier default normalised for the subscription transport+ (``claude-sonnet-4-6``) — not ``None``, which would silently+ re-introduce the old subscription-default bypass."""cfg = cfg_mod.ChatConfig(thinking_standard_model="anthropic/claude-sonnet-4-6",claude_agent_model=None,@@ -637,8 +627,9 @@ class TestResolveSdkModelForRequestLdFallback:resolved = await _resolve_sdk_model_for_request(model="standard", session_id="sess-std-sub", user_id="user-1")- # Expect LD-served Kimi, NOT None (the old subscription-default bypass)- assert resolved == "moonshotai/kimi-k2.6"+ # Kimi can't be served by the subscription CLI; fail-soft to+ # the tier default normalised for the active transport.+ assert resolved == "claude-sonnet-4-6"@pytest.mark.asyncioasync def test_standard_subscription_survives_trailing_whitespace_in_env(@@ -705,7 +696,10 @@ class TestResolveSdkModelForRequestLdFallback:"""Subscription mode bypasses LD only on the standard tier —the advanced tier always consults LD because the user explicitlyasked for the premium path. A subscription + advanced request- with LD-served Opus must return Opus (not ``None``)."""+ with LD-served Opus must return Opus normalised for the+ subscription CLI (``claude-opus-4-7``), not the OpenRouter slug+ ``anthropic/claude-opus-4.7`` which the CLI subprocess rejects+ even when ``CHAT_BASE_URL`` is set to the OpenRouter proxy."""cfg = cfg_mod.ChatConfig(thinking_standard_model="anthropic/claude-sonnet-4-6",thinking_advanced_model="anthropic/claude-opus-4.7",@@ -724,7 +718,7 @@ class TestResolveSdkModelForRequestLdFallback:resolved = await _resolve_sdk_model_for_request(model="advanced", session_id="sess-adv-sub", user_id="user-1")- assert resolved == "anthropic/claude-opus-4.7"+ assert resolved == "claude-opus-4-7"# ---------------------------------------------------------------------------