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.
feat(backend/copilot): SDK fast tier defaults to Kimi K2.6 via OpenRouter + vendor-aware cost + cross-model fix (#12878)
Zamil Majdyb98bcf31c8e3
diff --git a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.pyindex 808b06eb32..ad87114959 100644--- a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py@@ -108,17 +108,16 @@ class TestResolveBaselineModel:== "anthropic/claude-opus-4.7")- def test_standard_cells_diverge_across_paths(self):- """The whole point of the split: baseline cheap (Kimi) vs SDK- Anthropic-only (Sonnet). If the shipped standard defaults ever- collapse to the same value someone lost the cost savings.- Checked against ``Field`` defaults, not the env-backed singleton."""+ def test_standard_cells_share_kimi_default_across_paths(self):+ """After PR #12878 both paths default to the same cheap model+ (``moonshotai/kimi-k2.6``). The split exists for *override*+ flexibility, not for forcing a price gap — guard against an+ accidental regression that pins the SDK path back to Sonnet."""from backend.copilot.config import ChatConfig- assert (- ChatConfig.model_fields["thinking_standard_model"].default- != ChatConfig.model_fields["fast_standard_model"].default- )+ kimi = "moonshotai/kimi-k2.6"+ assert ChatConfig.model_fields["fast_standard_model"].default == kimi+ assert ChatConfig.model_fields["thinking_standard_model"].default == kimidef test_standard_and_advanced_cells_differ_on_fast(self):"""Advanced tier defaults to a different model than standard ondiff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.pyindex 08dcaf8898..f9799ecb22 100644--- a/autogpt_platform/backend/backend/copilot/config.py+++ b/autogpt_platform/backend/backend/copilot/config.py@@ -3,7 +3,7 @@import osfrom typing import Literal-from pydantic import AliasChoices, Field, field_validator+from pydantic import AliasChoices, Field, field_validator, model_validatorfrom pydantic_settings import BaseSettingsfrom backend.util.clients import OPENROUTER_BASE_URL@@ -54,7 +54,7 @@ class ChatConfig(BaseSettings):"``reasoning`` + ``include_reasoning`` extension params on the ""Moonshot endpoints — so the baseline reasoning plumbing lights up ""without provider-specific code. Roll back to the Anthropic route "- "via ``CHAT_FAST_STANDARD_MODEL=anthropic/claude-sonnet-4-6`` (then "+ "via ``CHAT_FAST_STANDARD_MODEL=anthropic/claude-sonnet-4.6`` (then ""``cache_control`` breakpoints reactivate via ""``_is_anthropic_model``).",)@@ -65,17 +65,20 @@ class ChatConfig(BaseSettings):"Override via ``CHAT_FAST_ADVANCED_MODEL``.",)thinking_standard_model: str = Field(- default="anthropic/claude-sonnet-4-6",+ default="moonshotai/kimi-k2.6",validation_alias=AliasChoices("CHAT_THINKING_STANDARD_MODEL","CHAT_MODEL",),description="SDK (extended-thinking) path, 'standard' / ``None`` "- "tier. Sonnet by default: the Claude Agent SDK CLI only speaks to "- "Anthropic endpoints, so the standard SDK tier has to stay on an "- "Anthropic model regardless of what the baseline path runs. "- "Override via ``CHAT_THINKING_STANDARD_MODEL`` (legacy "- "``CHAT_MODEL`` still honored).",+ "tier. Kimi K2.6 by default: routed via OpenRouter's Anthropic-"+ "compatible ``/v1/messages`` endpoint, which the Claude Agent SDK "+ "CLI accepts as a drop-in ``ANTHROPIC_BASE_URL`` target. The same "+ "cost/capability rationale as the baseline path applies — ~5x "+ "cheaper than Sonnet at SWE-Bench parity. Roll back to Sonnet via "+ "``CHAT_THINKING_STANDARD_MODEL=anthropic/claude-sonnet-4.6`` (then "+ "the SDK ``cache_control`` markers reactivate). Direct-Anthropic "+ "deployments (no OpenRouter) must override to an Anthropic model.",)thinking_advanced_model: str = Field(default="anthropic/claude-opus-4.7",@@ -269,9 +272,10 @@ class ChatConfig(BaseSettings):Field(default=None,description="Thinking effort level: 'low', 'medium', 'high', 'max', or None. "- "Only applies to models with extended thinking (Opus). "- "Sonnet doesn't have extended thinking — setting effort on Sonnet "- "can cause <internal_reasoning> tag leaks. "+ "Applies to models that emit a reasoning channel — Opus (extended "+ "thinking) and Kimi K2.6 (OpenRouter ``reasoning`` extension lit "+ "up by #12871). Sonnet does not have extended thinking — setting "+ "effort on Sonnet can cause <internal_reasoning> tag leaks. ""None = let the model decide. Override via CHAT_CLAUDE_AGENT_THINKING_EFFORT.",))@@ -303,6 +307,41 @@ class ChatConfig(BaseSettings):"(24h, permanent) TTL option — see ""https://platform.claude.com/docs/en/build-with-claude/prompt-caching.",)+ sdk_include_partial_messages: bool = Field(+ default=False,+ description="Enable per-token streaming on the SDK path by setting "+ "``include_partial_messages=True`` on ``ClaudeAgentOptions``. The "+ "CLI then emits raw Anthropic ``content_block_delta`` events as "+ "``StreamEvent`` messages ahead of each summary "+ "``AssistantMessage``, so long answers and extended-thinking "+ "reasoning land on the wire token-by-token instead of popping in "+ "as a lump at ``content_block_stop``. Matches the perceptual "+ "progress the baseline path has shipped since #12873. Off by "+ "default to keep the rollout staged; the adapter falls back to "+ "summary-only emission when this flag is False. See "+ "``docs/sdk-per-token-streaming-followup.md`` for the diff-based "+ "reconcile logic that prevents partial/summary double-emission "+ "and truncation when the two views disagree.",+ )+ sdk_reconcile_openrouter_cost: bool = Field(+ default=True,+ description="Query OpenRouter's ``/api/v1/generation?id=`` after each "+ "SDK turn and record the authoritative ``total_cost`` instead of the "+ "Claude Agent SDK CLI's estimate. Covers every OpenRouter-routed "+ "SDK turn regardless of vendor — the CLI's static Anthropic pricing "+ "table is accurate for Anthropic models (Sonnet/Opus via OpenRouter "+ "bill at Anthropic's own rates, penny-for-penny), but the reconcile "+ "catches any future rate change the CLI hasn't picked up and makes "+ "non-Anthropic cost (Kimi et al) correct — real billed amount, "+ "matching the baseline path's ``usage.cost`` read since #12864. "+ "Kill-switch for emergencies: set ``CHAT_SDK_RECONCILE_OPENROUTER_COST"+ "=false`` to fall back to the CLI's ``total_cost_usd`` reported "+ "synchronously (accurate-for-Anthropic / over-billed-for-Kimi). "+ "Tradeoff: 0.5-2s window between turn end and cost write; rate-limit "+ "counter briefly unaware, back-to-back turns in that window see "+ "stale state. The alternative (writing an estimate sync then a "+ "correction delta) would double-count the rate limit.",+ )claude_agent_cli_path: str | None = Field(default=None,description="Optional explicit path to a Claude Code CLI binary. "@@ -473,6 +512,59 @@ class ChatConfig(BaseSettings):)return v+ @model_validator(mode="after")+ def _validate_sdk_model_vendor_compatibility(self) -> "ChatConfig":+ """Fail at config load when an SDK model slug is incompatible with+ explicit direct-Anthropic mode.++ The SDK path's ``_normalize_model_name`` raises ``ValueError`` when+ a non-Anthropic vendor slug (e.g. ``moonshotai/kimi-k2.6``) is paired+ with direct-Anthropic mode — but that fires inside the request loop,+ so a misconfigured deployment would surface a 500 to every user+ instead of failing visibly at boot.++ Only the **explicit** opt-out (``use_openrouter=False``) is checked+ here, not the credential-missing path. Build environments and+ OpenAPI-schema export jobs construct ``ChatConfig()`` without any+ OpenRouter credentials in the env — that's not a misconfiguration,+ it's "config loads ok, but no SDK turn will succeed until creds are+ wired". The runtime guard in ``_normalize_model_name`` still+ catches the credential-missing path on the first SDK turn.++ Covers all three SDK fields that flow through+ ``_normalize_model_name``: primary tier+ (``thinking_standard_model``), advanced tier+ (``thinking_advanced_model``), and fallback model+ (``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).+ """+ if self.use_claude_code_subscription:+ return self+ if self.use_openrouter:+ return self+ for field_name in (+ "thinking_standard_model",+ "thinking_advanced_model",+ "claude_agent_fallback_model",+ ):+ value: str = getattr(self, field_name)+ if not value or "/" not in value:+ continue+ if value.split("/", 1)[0] != "anthropic":+ raise ValueError(+ f"Direct-Anthropic mode (use_openrouter=False) "+ f"requires an Anthropic model for {field_name}, got "+ f"{value!r}. Set CHAT_THINKING_STANDARD_MODEL / "+ f"CHAT_THINKING_ADVANCED_MODEL / "+ f"CHAT_CLAUDE_AGENT_FALLBACK_MODEL to an anthropic/* "+ f"slug, or set CHAT_USE_OPENROUTER=true."+ )+ return self+# Prompt paths for different contextsPROMPT_PATHS: dict[str, str] = {"default": "prompts/chat_system.md",diff --git a/autogpt_platform/backend/backend/copilot/config_test.py b/autogpt_platform/backend/backend/copilot/config_test.pyindex 25f9f477f2..7279061447 100644--- a/autogpt_platform/backend/backend/copilot/config_test.py+++ b/autogpt_platform/backend/backend/copilot/config_test.py@@ -5,12 +5,17 @@ import pytestfrom .config import ChatConfig# Env vars that the ChatConfig validators read — must be cleared so they don't-# override the explicit constructor values we pass in each test.+# override the explicit constructor values we pass in each test. Includes the+# SDK/baseline model aliases so a leftover ``CHAT_MODEL=...`` in the developer+# or CI environment can't change whether+# ``_validate_sdk_model_vendor_compatibility`` raises._ENV_VARS_TO_CLEAR = ("CHAT_USE_E2B_SANDBOX","CHAT_E2B_API_KEY","E2B_API_KEY","CHAT_USE_OPENROUTER",+ "CHAT_USE_CLAUDE_AGENT_SDK",+ "CHAT_USE_CLAUDE_CODE_SUBSCRIPTION","CHAT_API_KEY","OPEN_ROUTER_API_KEY","OPENAI_API_KEY",@@ -19,6 +24,14 @@ _ENV_VARS_TO_CLEAR = ("OPENAI_BASE_URL","CHAT_CLAUDE_AGENT_CLI_PATH","CLAUDE_AGENT_CLI_PATH",+ "CHAT_FAST_STANDARD_MODEL",+ "CHAT_FAST_MODEL",+ "CHAT_FAST_ADVANCED_MODEL",+ "CHAT_THINKING_STANDARD_MODEL",+ "CHAT_THINKING_ADVANCED_MODEL",+ "CHAT_MODEL",+ "CHAT_ADVANCED_MODEL",+ "CHAT_CLAUDE_AGENT_FALLBACK_MODEL","CHAT_RENDER_REASONING_IN_UI","CHAT_STREAM_REPLAY_COUNT",)@@ -30,6 +43,22 @@ def _clean_env(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.delenv(var, raising=False)+def _make_direct_safe_config(**kwargs) -> ChatConfig:+ """Build a ``ChatConfig`` for tests that pass ``use_openrouter=False``+ but aren't exercising the SDK vendor-compatibility validator.++ Pins ``thinking_standard_model``/``thinking_advanced_model`` to anthropic/*+ so the construction passes ``_validate_sdk_model_vendor_compatibility``+ without each test having to repeat the override.+ """+ defaults: dict = {+ "thinking_standard_model": "anthropic/claude-sonnet-4-6",+ "thinking_advanced_model": "anthropic/claude-opus-4-7",+ }+ defaults.update(kwargs)+ return ChatConfig(**defaults)++class TestOpenrouterActive:"""Tests for the openrouter_active property."""@@ -50,7 +79,7 @@ class TestOpenrouterActive:assert cfg.openrouter_active is Falsedef test_disabled_returns_false_despite_credentials(self):- cfg = ChatConfig(+ cfg = _make_direct_safe_config(use_openrouter=False,api_key="or-key",base_url="https://openrouter.ai/api/v1",@@ -168,6 +197,98 @@ class TestClaudeAgentCliPathEnvFallback:ChatConfig()+class TestSdkModelVendorCompatibility:+ """``model_validator`` that fails fast on SDK model vs routing-mode+ mismatch — see PR #12878 iteration-2 review. Mirrors the runtime+ guard in ``_normalize_model_name`` so misconfig surfaces at boot+ instead of as a 500 on the first SDK turn."""++ def test_direct_anthropic_with_kimi_default_raises(self):+ """The ``moonshotai/kimi-k2.6`` default must fail at config load+ when the deployment has no OpenRouter credentials."""+ with pytest.raises(Exception, match="requires an Anthropic model"):+ ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ )++ def test_direct_anthropic_with_anthropic_override_succeeds(self):+ """Direct-Anthropic mode is fine when both SDK slugs are anthropic/*."""+ cfg = ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ thinking_standard_model="anthropic/claude-sonnet-4-6",+ )+ assert cfg.thinking_standard_model == "anthropic/claude-sonnet-4-6"++ def test_openrouter_with_kimi_default_succeeds(self):+ """Default Kimi slug round-trips cleanly when OpenRouter is on."""+ cfg = ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ use_claude_code_subscription=False,+ )+ assert cfg.thinking_standard_model == "moonshotai/kimi-k2.6"++ def test_subscription_mode_skips_check(self):+ """Subscription path resolves the model to None and bypasses+ ``_normalize_model_name``, so the slug check is skipped."""+ cfg = ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=True,+ )+ assert cfg.use_claude_code_subscription is True++ def test_advanced_tier_also_validated(self):+ """Both standard and advanced SDK slugs are checked."""+ with pytest.raises(Exception, match="thinking_advanced_model"):+ 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="moonshotai/kimi-k2.6",+ )++ def test_fallback_model_also_validated(self):+ """``claude_agent_fallback_model`` flows through+ ``_normalize_model_name`` via ``_resolve_fallback_model`` so the+ same direct-Anthropic guard applies."""+ with pytest.raises(Exception, match="claude_agent_fallback_model"):+ 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",+ claude_agent_fallback_model="moonshotai/kimi-k2.6",+ )++ def test_empty_fallback_skipped(self):+ """Empty ``claude_agent_fallback_model`` (no fallback configured)+ must not trip the validator — the fallback-disabled state is+ intentional and shouldn't require a placeholder anthropic/* slug."""+ 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",+ claude_agent_fallback_model="",+ )+ assert cfg.claude_agent_fallback_model == ""++class TestRenderReasoningInUi:"""``render_reasoning_in_ui`` gates reasoning wire events globally."""diff --git a/autogpt_platform/backend/backend/copilot/sdk/env_test.py b/autogpt_platform/backend/backend/copilot/sdk/env_test.pyindex e61908081c..36f3dc32cb 100644--- a/autogpt_platform/backend/backend/copilot/sdk/env_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/env_test.py@@ -13,12 +13,19 @@ from backend.copilot.config import ChatConfigdef _make_config(**overrides) -> ChatConfig:- """Create a ChatConfig with safe defaults, applying *overrides*."""+ """Create a ChatConfig with safe defaults, applying *overrides*.++ SDK model fields are pinned to anthropic/* so the+ ``_validate_sdk_model_vendor_compatibility`` model_validator allows+ construction with ``use_openrouter=False`` (the default here).+ """defaults = {"use_claude_code_subscription": False,"use_openrouter": False,"api_key": None,"base_url": None,+ "thinking_standard_model": "anthropic/claude-sonnet-4-6",+ "thinking_advanced_model": "anthropic/claude-opus-4-7",}defaults.update(overrides)return ChatConfig(**defaults)diff --git a/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost.py b/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost.pynew file mode 100644index 0000000000..ee0f02de44--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost.py@@ -0,0 +1,399 @@+"""Authoritative per-turn cost for OpenRouter-routed SDK generations.++The Claude Agent SDK CLI's ``ResultMessage.total_cost_usd`` is computed+from a static Anthropic pricing table baked into the binary. For+non-Anthropic models routed through OpenRouter (e.g. Kimi K2.6) the CLI+silently falls back to Sonnet rates — empirically ~5x too high. Even+after a rate-card override the estimate is still ~37% off in practice+because OpenRouter's own tokenizer counts, reasoning-token rollup, and+dated-snapshot pricing tiers can't be reconstructed from what the SDK+exposes locally.++This module provides :func:`record_turn_cost_from_openrouter` — an+``asyncio.create_task``-able coroutine that:++1. Queries ``https://openrouter.ai/api/v1/generation?id=<gen-id>`` for+ each generation ID captured during the turn.+2. Sums the authoritative ``total_cost`` across all rounds.+3. Calls :func:`persist_and_record_usage` **once** with the real number,+ updating both the cost-analytics row and the rate-limit counter.++If every lookup fails (404 / timeout / parse error), the caller's+``fallback_cost_usd`` is recorded instead — keeps the rate-limit counter+populated with the best available estimate rather than leaving the turn+uncharged.+"""++from __future__ import annotations++import asyncio+import logging+import os+from pathlib import Path+from typing import TYPE_CHECKING++import httpx++from backend.copilot.token_tracking import persist_and_record_usage+from backend.util import json++if TYPE_CHECKING:+ from backend.copilot.model import ChatSession++logger = logging.getLogger(__name__)++# OpenRouter docs:+# https://openrouter.ai/docs/api-reference/get-a-generation+_GENERATION_URL = "https://openrouter.ai/api/v1/generation"++# OpenRouter's generation endpoint indexes the billing row a few seconds+# after the SSE stream closes — observed ~8-12s in practice. Retry with+# progressive backoff for up to ~30s total before giving up, so the typical+# indexing window (~10s) fits inside the retry envelope. Backoff values+# in seconds summed: 0.5 + 1 + 2 + 4 + 8 + 15 = 30.5.+_MAX_RETRIES = 7+_BACKOFF_SECONDS = (0.5, 1.0, 2.0, 4.0, 8.0, 15.0)+_REQUEST_TIMEOUT = 10.0+++async def _fetch_generation_cost(+ client: httpx.AsyncClient,+ gen_id: str,+ api_key: str,+ log_prefix: str,+) -> float | None:+ """Fetch the ``total_cost`` for one generation, with retries.++ Retries only on transient conditions:++ * HTTP 404 — row not yet indexed server-side (typical ~5-10s lag+ after the SSE stream closes)+ * HTTP 408 / 429 — timeout / rate limit+ * HTTP 5xx — transient OpenRouter outage+ * Network / ``httpx`` exceptions — transport-level retryable++ Fails fast on permanent client errors (401 Unauthorized,+ 403 Forbidden, 400 Bad Request, etc.) since they can't recover+ within the retry window and would just burn API quota.++ Returns ``None`` when the endpoint reports no data, on a permanent+ failure, or when every retry attempt hits a transient error.+ """+ headers = {"Authorization": f"Bearer {api_key}"}+ params = {"id": gen_id}+ last_error: Exception | None = None+ for attempt in range(_MAX_RETRIES):+ if attempt > 0:+ await asyncio.sleep(_BACKOFF_SECONDS[attempt - 1])+ try:+ resp = await client.get(+ _GENERATION_URL,+ params=params,+ headers=headers,+ timeout=_REQUEST_TIMEOUT,+ )+ status = resp.status_code+ # Fast-fail on permanent client errors — retrying 401/403/400+ # just burns API quota and delays the fallback.+ if status in (400, 401, 403):+ logger.warning(+ "%s OpenRouter /generation permanent error %d for %s — "+ "not retrying (check API key / request shape)",+ log_prefix,+ status,+ gen_id,+ )+ return None+ # Transient retryable: 404 (indexing lag), 408 (timeout),+ # 429 (rate limit), 5xx (server error).+ if status == 404 or status == 408 or status == 429 or status >= 500:+ last_error = RuntimeError(f"HTTP {status} on attempt {attempt + 1}")+ continue+ # Any other 4xx — treat as permanent.+ if status >= 400:+ logger.warning(+ "%s OpenRouter /generation unexpected status %d for %s — "+ "not retrying",+ log_prefix,+ status,+ gen_id,+ )+ return None+ payload = resp.json().get("data")+ if not isinstance(payload, dict):+ logger.warning(+ "%s OpenRouter /generation returned no data for %s",+ log_prefix,+ gen_id,+ )+ return None+ cost = payload.get("total_cost")+ if cost is None:+ logger.warning(+ "%s OpenRouter /generation response missing total_cost "+ "for %s (keys=%s)",+ log_prefix,+ gen_id,+ sorted(payload.keys())[:10],+ )+ return None+ return float(cost)+ except Exception as exc: # noqa: BLE001+ # Network / transport errors are retryable.+ last_error = exc+ continue+ logger.warning(+ "%s OpenRouter /generation lookup failed for %s after %d attempts: %s",+ log_prefix,+ gen_id,+ _MAX_RETRIES,+ last_error,+ )+ return None+++def _gen_ids_from_jsonl(path: Path) -> set[str]:+ """Extract ``gen-`` message IDs from every assistant entry in a+ Claude CLI JSONL file.++ Tolerant of malformed lines: single bad JSON object doesn't block+ the whole file. Also reads ``redacted_thinking`` / ``thinking``+ entries that share an ID with their parent (via ``jq -u`` in the+ CLI) and dedups by caller.+ """+ ids: set[str] = set()+ try:+ with path.open("r", encoding="utf-8") as f:+ for line in f:+ line = line.strip()+ if not line:+ continue+ entry = json.loads(line, fallback=None)+ if not isinstance(entry, dict):+ continue+ if entry.get("type") != "assistant":+ continue+ message = entry.get("message")+ if not isinstance(message, dict):+ continue+ msg_id = message.get("id")+ if isinstance(msg_id, str) and msg_id.startswith("gen-"):+ ids.add(msg_id)+ except (OSError, UnicodeDecodeError) as exc:+ logger.debug(+ "Failed to scan JSONL for gen-IDs: path=%s err=%s",+ path,+ exc,+ )+ return ids+++def _discover_turn_subagent_gen_ids(+ project_dir: Path,+ session_id: str,+ turn_start_ts: float,+ known: list[str],+) -> list[str]:+ """Gen-IDs from this session's subagents created during this turn.++ Main-turn LLM rounds (incl. fallback retries) arrive on the live+ stream as ``AssistantMessage`` and land on ``known`` via+ ``message_id``. What's NOT on ``known`` is the CLI's subagent LLM+ calls — chiefly auto-compaction, which spawns a fresh JSONL under+ ``<project_dir>/<session_id>/subagents/agent-acompact-*.jsonl``+ whose gen-IDs never touch our main adapter. OpenRouter bills them+ anyway, so without this sweep compaction turns under-report cost.++ Scoping: ONLY the current session's subagent dir+ (``<project_dir>/<session_id>/subagents/agent-*.jsonl``) and ONLY+ files whose ``mtime >= turn_start_ts``. Without both guards we'd+ merge prior turns' gen-IDs (main JSONL accumulates forever) and+ foreign sessions' gen-IDs (the project dir contains every session+ for this cwd), double-billing the user.++ Also covers non-compaction subagents (Task tool etc.) when the CLI+ spawns them — their live-stream visibility depends on SDK version,+ so the sweep is a safety net. The dedup against ``known`` means+ anything already captured live doesn't double count.++ Preserves ``known`` ordering so main-turn IDs stay first; only+ appends truly new IDs from the sweep.+ """+ merged: list[str] = list(known)+ seen = set(merged)+ subagents_dir = project_dir / session_id / "subagents"+ if not subagents_dir.exists():+ return merged+ try:+ for jsonl in subagents_dir.glob("agent-*.jsonl"):+ try:+ if jsonl.stat().st_mtime < turn_start_ts:+ continue+ except OSError:+ continue+ for gen_id in _gen_ids_from_jsonl(jsonl):+ if gen_id not in seen:+ seen.add(gen_id)+ merged.append(gen_id)+ except OSError as exc:+ logger.debug("Failed to walk subagents dir=%s: %s", subagents_dir, exc)+ return merged+++async def record_turn_cost_from_openrouter(+ *,+ session: "ChatSession",+ user_id: str | None,+ model: str | None,+ prompt_tokens: int,+ completion_tokens: int,+ cache_read_tokens: int,+ cache_creation_tokens: int,+ generation_ids: list[str],+ cli_project_dir: str | None,+ cli_session_id: str | None,+ turn_start_ts: float | None,+ fallback_cost_usd: float | None,+ api_key: str | None,+ log_prefix: str,+) -> None:+ """Persist turn cost from OpenRouter's authoritative ``/generation``.++ Writes a single cost-analytics row via :func:`persist_and_record_usage`+ — same method used for the Anthropic-direct sync path — so the+ cost-log append and rate-limit counter stay consistent. No double+ counting: the caller skips its own sync persist for non-Anthropic+ OpenRouter turns and defers entirely to this task.++ Launched via ``asyncio.create_task`` from the stream ``finally`` block+ so the ~500-2000ms ``/generation`` indexing delay doesn't add latency+ to the turn. During that window the rate-limit counter is briefly+ unaware of the turn's cost; back-to-back turns in that sub-second+ gap see a stale counter. Acceptable tradeoff — the alternative+ (writing a possibly-wrong estimate synchronously) creates a+ double-count when the reconcile delta arrives.++ Fallback semantics: if every generation lookup fails, records+ ``fallback_cost_usd`` instead so the rate-limit counter isn't left+ completely empty. Keeps behaviour at-worst equivalent to the+ rate-card estimate that came before this task existed.+ """+ if not api_key:+ logger.debug(+ "%s OpenRouter cost record skipped: no API key available",+ log_prefix,+ )+ return++ # Merge in any gen-IDs from CLI subagent JSONLs the live stream+ # didn't surface — chiefly SDK-internal compaction, which spawns a+ # summarisation LLM call under+ # ``<project_dir>/<cli_session_id>/subagents/...`` that OpenRouter+ # bills but doesn't emit via our main adapter. Safe no-op when no+ # compaction happened (no subagent files created this turn) or the+ # CLI wrote nothing there.+ #+ # The sweep is SESSION-scoped (``<cli_session_id>/subagents/``, not+ # the whole project dir) and TURN-scoped (mtime >= turn_start_ts).+ # Both guards are load-bearing: the project dir contains every+ # session for this cwd, and subagent files persist across turns,+ # so an unscoped sweep would re-bill prior turns and foreign+ # sessions' gen-IDs.+ if cli_project_dir and cli_session_id and turn_start_ts is not None:+ merged_ids = _discover_turn_subagent_gen_ids(+ Path(os.path.expanduser(cli_project_dir)),+ cli_session_id,+ turn_start_ts,+ generation_ids,+ )+ if len(merged_ids) != len(generation_ids):+ logger.info(+ "%s[cost-record] discovered %d additional gen-IDs in "+ "session subagents (compaction / Task) — reconcile "+ "covers all",+ log_prefix,+ len(merged_ids) - len(generation_ids),+ )+ generation_ids = merged_ids++ if not generation_ids:+ return++ try:+ async with httpx.AsyncClient() as client:+ tasks = [+ _fetch_generation_cost(client, gen_id, api_key, log_prefix)+ for gen_id in generation_ids+ ]+ results = await asyncio.gather(*tasks, return_exceptions=False)+ except Exception as exc: # noqa: BLE001+ logger.warning(+ "%s OpenRouter cost record failed to fetch any generation "+ "(falling back to rate-card estimate): %s",+ log_prefix,+ exc,+ )+ results = []++ fetched = [r for r in results if isinstance(r, (int, float))]+ if fetched and len(fetched) == len(generation_ids):+ real_cost: float | None = sum(fetched)+ # Log real (OpenRouter billed) vs CLI rate-card estimate so an+ # operator can spot divergence without querying OpenRouter by+ # hand. Under-count typically means a gen-ID source we don't+ # capture live (e.g. title model, background LLM calls running+ # outside the main stream); over-count means the CLI's rate+ # table is stale vs. OpenRouter's current pricing.+ delta_pct: float | None = None+ if fallback_cost_usd and fallback_cost_usd > 0:+ delta_pct = (real_cost - fallback_cost_usd) / fallback_cost_usd * 100+ logger.info(+ "%s[cost-record] OpenRouter real=$%.6f cli_estimate=$%s "+ "delta=%s (gen_ids=%d)",+ log_prefix,+ real_cost,+ f"{fallback_cost_usd:.6f}" if fallback_cost_usd is not None else "?",+ f"{delta_pct:+.1f}%" if delta_pct is not None else "n/a",+ len(generation_ids),+ )+ else:+ real_cost = fallback_cost_usd+ if fetched:+ # Partial success: some lookups returned a cost, others didn't.+ # Trusting the partial sum would under-report; fall back to the+ # estimate so rate-limit enforcement stays conservative.+ logger.warning(+ "%s[cost-record] OpenRouter partial lookup (%d/%d) — "+ "using fallback estimate=$%s",+ log_prefix,+ len(fetched),+ len(generation_ids),+ real_cost,+ )+ else:+ logger.warning(+ "%s[cost-record] OpenRouter lookup failed for all gens — "+ "using fallback estimate=$%s",+ log_prefix,+ real_cost,+ )++ try:+ await persist_and_record_usage(+ session=session,+ user_id=user_id,+ prompt_tokens=prompt_tokens,+ completion_tokens=completion_tokens,+ cache_read_tokens=cache_read_tokens,+ cache_creation_tokens=cache_creation_tokens,+ log_prefix=f"{log_prefix}[cost-record]",+ cost_usd=real_cost,+ model=model,+ provider="open_router",+ )+ except Exception as exc: # noqa: BLE001+ logger.warning(+ "%s[cost-record] failed to persist: %s",+ log_prefix,+ exc,+ )diff --git a/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost_test.py b/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost_test.pynew file mode 100644index 0000000000..442e858c0a--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost_test.py@@ -0,0 +1,520 @@+"""Unit tests for SDK-path OpenRouter cost recording."""++from __future__ import annotations++from datetime import UTC, datetime+from unittest.mock import AsyncMock, patch++import httpx+import pytest++from backend.copilot.model import ChatSession+from backend.copilot.sdk.openrouter_cost import record_turn_cost_from_openrouter+++def _session() -> ChatSession:+ now = datetime.now(UTC)+ return ChatSession(+ session_id="sess-1",+ user_id="user-1",+ usage=[],+ started_at=now,+ updated_at=now,+ messages=[],+ )+++def _mock_generation_response(cost: float) -> dict:+ return {+ "data": {+ "total_cost": cost,+ "native_tokens_prompt": 1000,+ "native_tokens_completion": 50,+ "tokens_prompt": 1100,+ "tokens_completion": 60,+ }+ }+++class TestRecordTurnCostFromOpenRouter:+ """Single-write semantics: the cost + rate-limit counter is updated+ exactly once per turn via this background task. The sync path at+ the call site is already skipped for non-Anthropic OpenRouter turns,+ so there's no double-counting path even on partial failure."""++ @pytest.mark.asyncio+ async def test_empty_generation_ids_no_op(self):+ """Direct-Anthropic turn produces no gen-IDs — task is a no-op."""+ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new_callable=AsyncMock) as mock_get,+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="anthropic/claude-sonnet-4.6",+ prompt_tokens=10,+ completion_tokens=5,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=[],+ cli_project_dir=None,+ cli_session_id=None,+ turn_start_ts=None,+ fallback_cost_usd=0.05,+ api_key="sk-or-test",+ log_prefix="[test]",+ )+ mock_persist.assert_not_called()+ mock_get.assert_not_called()++ @pytest.mark.asyncio+ async def test_missing_api_key_no_op(self):+ """Without an OpenRouter API key we can't query the endpoint — skip."""+ with patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist:+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="moonshotai/kimi-k2.6",+ prompt_tokens=10,+ completion_tokens=5,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-1"],+ cli_project_dir=None,+ cli_session_id=None,+ turn_start_ts=None,+ fallback_cost_usd=0.02,+ api_key=None,+ log_prefix="[test]",+ )+ mock_persist.assert_not_called()++ @pytest.mark.asyncio+ async def test_single_generation_records_real_cost(self):+ """Authoritative cost from OpenRouter is the value recorded — no+ reliance on the fallback estimate."""+ real_cost = 0.02900595++ async def _get(self, url, **kwargs): # noqa: ARG001+ return httpx.Response(200, json=_mock_generation_response(real_cost))++ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new=_get),+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="moonshotai/kimi-k2.6",+ prompt_tokens=29669,+ completion_tokens=280,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-1776842410"],+ cli_project_dir=None,+ cli_session_id=None,+ turn_start_ts=None,+ fallback_cost_usd=0.01858, # rate-card estimate, deliberately wrong+ api_key="sk-or-test",+ log_prefix="[test]",+ )+ mock_persist.assert_called_once()+ kwargs = mock_persist.call_args.kwargs+ assert kwargs["cost_usd"] == pytest.approx(real_cost, rel=1e-9)+ assert kwargs["prompt_tokens"] == 29669+ assert kwargs["completion_tokens"] == 280+ assert kwargs["provider"] == "open_router"+ assert kwargs["model"] == "moonshotai/kimi-k2.6"++ @pytest.mark.asyncio+ async def test_multi_round_turn_sums_costs(self):+ """Tool-use turn has N generation IDs; the real cost is the sum+ of ``total_cost`` across all rounds — recorded in a single row."""+ costs_by_id = {"gen-a": 0.029, "gen-b": 0.030}++ async def _get(self, url, **kwargs): # noqa: ARG001+ gen_id = kwargs.get("params", {}).get("id")+ return httpx.Response(+ 200, json=_mock_generation_response(costs_by_id[gen_id])+ )++ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new=_get),+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="moonshotai/kimi-k2.6",+ prompt_tokens=60000,+ completion_tokens=600,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-a", "gen-b"],+ cli_project_dir=None,+ cli_session_id=None,+ turn_start_ts=None,+ fallback_cost_usd=0.037,+ api_key="sk-or-test",+ log_prefix="[test]",+ )+ mock_persist.assert_called_once()+ cost = mock_persist.call_args.kwargs["cost_usd"]+ assert cost == pytest.approx(sum(costs_by_id.values()), rel=1e-9)++ @pytest.mark.asyncio+ async def test_partial_lookup_falls_back_to_estimate(self):+ """If only some gen-IDs resolve, summing them would under-report.+ Fall back to the caller's estimate and log — the rate-limit+ counter stays populated with the best available number."""+ fallback = 0.05+ seq = iter(+ [+ httpx.Response(200, json=_mock_generation_response(0.03)),+ httpx.Response(404, text="not found"),+ httpx.Response(404, text="not found"),+ httpx.Response(404, text="not found"),+ httpx.Response(404, text="not found"),+ ]+ )++ async def _get(self, *args, **kwargs): # noqa: ARG001+ return next(seq)++ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new=_get),+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="moonshotai/kimi-k2.6",+ prompt_tokens=1000,+ completion_tokens=10,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-a", "gen-b"],+ cli_project_dir=None,+ cli_session_id=None,+ turn_start_ts=None,+ fallback_cost_usd=fallback,+ api_key="sk-or-test",+ log_prefix="[test]",+ )+ mock_persist.assert_called_once()+ assert mock_persist.call_args.kwargs["cost_usd"] == fallback++ @pytest.mark.asyncio+ async def test_fast_fail_on_401_no_retries(self):+ """Permanent client errors (401/403/400) must not retry — burning+ the 30s retry window on an unauthenticated request wastes API+ quota and delays the fallback."""+ call_count = {"n": 0}++ async def _get(self, *args, **kwargs): # noqa: ARG001+ call_count["n"] += 1+ return httpx.Response(401, text="unauthorized")++ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new=_get),+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="moonshotai/kimi-k2.6",+ prompt_tokens=1000,+ completion_tokens=10,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-a"],+ cli_project_dir=None,+ cli_session_id=None,+ turn_start_ts=None,+ fallback_cost_usd=0.02,+ api_key="sk-bad",+ log_prefix="[test]",+ )+ # Only one call — no retries.+ assert call_count["n"] == 1+ # Fallback was recorded (lookup failed → keep rate-limit counter live).+ mock_persist.assert_called_once()+ assert mock_persist.call_args.kwargs["cost_usd"] == 0.02++ @pytest.mark.asyncio+ async def test_retries_on_404_then_succeeds(self):+ """Indexing lag: endpoint returns 404 initially, then 200 once the+ billing row is indexed. Retry budget should exhaust transient+ states rather than giving up on first 404."""+ seq = iter(+ [+ httpx.Response(404, text="not found"),+ httpx.Response(200, json=_mock_generation_response(0.025)),+ ]+ )++ async def _get(self, *args, **kwargs): # noqa: ARG001+ return next(seq)++ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new=_get),+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="moonshotai/kimi-k2.6",+ prompt_tokens=1000,+ completion_tokens=10,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-a"],+ cli_project_dir=None,+ cli_session_id=None,+ turn_start_ts=None,+ fallback_cost_usd=0.05,+ api_key="sk-or-test",+ log_prefix="[test]",+ )+ mock_persist.assert_called_once()+ assert mock_persist.call_args.kwargs["cost_usd"] == pytest.approx(0.025)++ @pytest.mark.asyncio+ async def test_complete_lookup_failure_falls_back_to_estimate(self):+ """Every lookup fails → record the estimate so the rate-limit+ counter isn't left empty. At-worst parity with the pre-task+ behaviour."""+ fallback = 0.02++ async def _get(self, *args, **kwargs): # noqa: ARG001+ raise httpx.ConnectError("no network")++ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new=_get),+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="moonshotai/kimi-k2.6",+ prompt_tokens=1000,+ completion_tokens=10,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-a"],+ cli_project_dir=None,+ cli_session_id=None,+ turn_start_ts=None,+ fallback_cost_usd=fallback,+ api_key="sk-or-test",+ log_prefix="[test]",+ )+ mock_persist.assert_called_once()+ assert mock_persist.call_args.kwargs["cost_usd"] == fallback++ @pytest.mark.asyncio+ async def test_compaction_subagent_gen_ids_are_swept(self, tmp_path):+ """CLI-internal compaction spawns a subagent JSONL under+ ``<project_dir>/<session_id>/subagents/agent-acompact-*.jsonl``+ whose gen-IDs the live adapter never surfaces. When+ ``cli_project_dir`` + ``cli_session_id`` + ``turn_start_ts``+ are supplied the reconcile walks only THIS session's subagents+ and discovers the compaction IDs."""+ session_id = "sess-abc"+ sub_dir = tmp_path / session_id / "subagents"+ sub_dir.mkdir(parents=True)+ (sub_dir / "agent-acompact-xyz.jsonl").write_text(+ '{"type":"assistant","message":{"id":"gen-compact-1","content":[]}}\n'+ '{"type":"assistant","message":{"id":"gen-compact-2","content":[]}}\n'+ )++ costs_by_id = {+ "gen-main-1": 0.020,+ "gen-compact-1": 0.005,+ "gen-compact-2": 0.003,+ }++ async def _get(self, *args, **kwargs): # noqa: ARG001+ gen_id = kwargs.get("params", {}).get("id")+ return httpx.Response(+ 200, json=_mock_generation_response(costs_by_id[gen_id])+ )++ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new=_get),+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="anthropic/claude-opus-4.7",+ prompt_tokens=1000,+ completion_tokens=10,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-main-1"],+ cli_project_dir=str(tmp_path),+ cli_session_id=session_id,+ turn_start_ts=0.0,+ fallback_cost_usd=0.05,+ api_key="sk-or-test",+ log_prefix="[test]",+ )+ mock_persist.assert_called_once()+ assert mock_persist.call_args.kwargs["cost_usd"] == pytest.approx(+ sum(costs_by_id.values()), rel=1e-9+ )++ @pytest.mark.asyncio+ async def test_compaction_sweep_no_subagents_is_noop(self, tmp_path):+ """No compaction happened → reconcile uses only the caller's+ gen-IDs, same as when cli_project_dir is None."""+ session_id = "sess-none"+ (tmp_path / session_id).mkdir()++ async def _get(self, *args, **kwargs): # noqa: ARG001+ return httpx.Response(200, json=_mock_generation_response(0.02))++ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new=_get),+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="moonshotai/kimi-k2.6",+ prompt_tokens=1000,+ completion_tokens=10,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-main-1"],+ cli_project_dir=str(tmp_path),+ cli_session_id=session_id,+ turn_start_ts=0.0,+ fallback_cost_usd=0.05,+ api_key="sk-or-test",+ log_prefix="[test]",+ )+ mock_persist.assert_called_once()+ assert mock_persist.call_args.kwargs["cost_usd"] == pytest.approx(0.02)++ @pytest.mark.asyncio+ async def test_compaction_sweep_ignores_prior_turn_and_foreign_sessions(+ self, tmp_path+ ):+ """Scoping guards the sweep from double-billing: a stale subagent+ file from a prior turn (mtime before ``turn_start_ts``) and any+ subagent from a foreign session (different session_id folder)+ must BOTH be skipped. Without either guard, a long-running+ session with past compactions would re-bill every prior turn,+ and a second chat session in the same cwd would inherit the+ first session's compaction cost."""+ import os+ import time++ this_session = "sess-current"+ other_session = "sess-other"++ this_subagents = tmp_path / this_session / "subagents"+ this_subagents.mkdir(parents=True)+ other_subagents = tmp_path / other_session / "subagents"+ other_subagents.mkdir(parents=True)++ # Prior-turn compaction file — same session, stale mtime.+ stale_file = this_subagents / "agent-acompact-stale.jsonl"+ stale_file.write_text(+ '{"type":"assistant","message":{"id":"gen-stale-1","content":[]}}\n'+ )+ # Foreign session's compaction file.+ foreign_file = other_subagents / "agent-acompact-foreign.jsonl"+ foreign_file.write_text(+ '{"type":"assistant","message":{"id":"gen-foreign-1","content":[]}}\n'+ )+ # Current-turn compaction file — fresh.+ fresh_file = this_subagents / "agent-acompact-fresh.jsonl"+ fresh_file.write_text(+ '{"type":"assistant","message":{"id":"gen-fresh-1","content":[]}}\n'+ )++ # turn_start_ts lies between the stale and fresh mtimes.+ past = time.time() - 3600+ os.utime(stale_file, (past, past))+ os.utime(foreign_file, (past, past))+ turn_start_ts = time.time() - 60 # 1 min ago+ fresh_now = time.time()+ os.utime(fresh_file, (fresh_now, fresh_now))++ costs_by_id = {+ "gen-main-1": 0.010,+ "gen-fresh-1": 0.004,+ }++ async def _get(self, *args, **kwargs): # noqa: ARG001+ gen_id = kwargs.get("params", {}).get("id")+ # If the sweep leaks a stale/foreign ID, the test fails here+ # with a KeyError rather than silently over-billing.+ assert gen_id in costs_by_id, f"sweep leaked out-of-scope gen_id {gen_id}"+ return httpx.Response(+ 200, json=_mock_generation_response(costs_by_id[gen_id])+ )++ with (+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ ) as mock_persist,+ patch("httpx.AsyncClient.get", new=_get),+ ):+ await record_turn_cost_from_openrouter(+ session=_session(),+ user_id="u1",+ model="moonshotai/kimi-k2.6",+ prompt_tokens=1000,+ completion_tokens=10,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ generation_ids=["gen-main-1"],+ cli_project_dir=str(tmp_path),+ cli_session_id=this_session,+ turn_start_ts=turn_start_ts,+ fallback_cost_usd=0.05,+ api_key="sk-or-test",+ log_prefix="[test]",+ )+ mock_persist.assert_called_once()+ # Exactly the current-turn main + fresh compaction — no stale,+ # no foreign.+ assert mock_persist.call_args.kwargs["cost_usd"] == pytest.approx(+ sum(costs_by_id.values()), rel=1e-9+ )diff --git a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.pyindex 829e511f7e..070e6992be 100644--- a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py@@ -10,12 +10,19 @@ from backend.copilot.constants import is_transient_api_errordef _make_config(**overrides) -> ChatConfig:- """Create a ChatConfig with safe defaults, applying *overrides*."""+ """Create a ChatConfig with safe defaults, applying *overrides*.++ SDK model fields are pinned to anthropic/* so the+ ``_validate_sdk_model_vendor_compatibility`` model_validator allows+ construction with ``use_openrouter=False`` (the default here).+ """defaults = {"use_claude_code_subscription": False,"use_openrouter": False,"api_key": None,"base_url": None,+ "thinking_standard_model": "anthropic/claude-sonnet-4-6",+ "thinking_advanced_model": "anthropic/claude-opus-4-7",}defaults.update(overrides)return ChatConfig(**defaults)@@ -39,8 +46,11 @@ class TestResolveFallbackModel:assert _resolve_fallback_model() is None- def test_strips_provider_prefix(self):- """OpenRouter-style 'anthropic/claude-sonnet-4-...' is stripped."""+ def test_keeps_full_slug_when_openrouter_active(self):+ """OpenRouter routes by ``vendor/model`` slug — _normalize_model_name+ now preserves the prefix when openrouter_active is True so non-+ Anthropic vendors stay routable. Anthropic slugs are passed+ through unchanged in this mode (PR #12878)."""cfg = _make_config(claude_agent_fallback_model="anthropic/claude-sonnet-4-20250514",use_openrouter=True,@@ -52,8 +62,7 @@ class TestResolveFallbackModel:result = _resolve_fallback_model()- assert result == "claude-sonnet-4-20250514"- assert "/" not in result+ assert result == "anthropic/claude-sonnet-4-20250514"def test_dots_replaced_for_direct_anthropic(self):"""Direct Anthropic requires hyphen-separated versions."""diff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.pyindex 6db4615062..bd26db5d5f 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py@@ -7,12 +7,15 @@ the frontend expects.import jsonimport logging+import timeimport uuid+from typing import Anyfrom claude_agent_sdk import (AssistantMessage,Message,ResultMessage,+ StreamEvent,SystemMessage,TextBlock,ThinkingBlock,@@ -46,6 +49,16 @@ from .tool_adapter import MCP_TOOL_PREFIX, pop_pending_tool_outputlogger = logging.getLogger(__name__)+# Coalescing thresholds for ``thinking_delta`` events on the SDK partial+# stream — matches the baseline window (see+# ``baseline/reasoning.py::_COALESCE_MIN_CHARS``). Anthropic's extended-+# thinking channel emits ~1 event per token (~4,700 per Kimi K2.6 turn);+# a 64-char / 50 ms window halves the event rate vs 32/40 while staying+# well under the ~100 ms perceptual threshold.+_THINKING_COALESCE_MIN_CHARS = 64+_THINKING_COALESCE_MAX_INTERVAL_MS = 50.0++class SDKResponseAdapter:"""Adapter for converting Claude Agent SDK messages to Vercel AI SDK format.@@ -81,6 +94,36 @@ class SDKResponseAdapter:# case so the turn renders as cleanly complete.self._text_since_last_tool_result = Falseself._any_tool_results_seen = False+ # --- Partial-message streaming state (CHAT_SDK_INCLUDE_PARTIAL_MESSAGES)+ # When ``include_partial_messages=True`` is set on+ # ``ClaudeAgentOptions``, the CLI emits raw Anthropic streaming+ # events (``content_block_start`` / ``content_block_delta`` /+ # ``content_block_stop``) as ``StreamEvent`` messages ahead of+ # each summary ``AssistantMessage``. We consume those for+ # per-token wire emission and reconcile against the summary to+ # catch any tail content the partial stream missed (short blocks+ # the CLI emits summary-only, OpenRouter proxy quirks, encrypted+ # thinking).+ #+ self._block_types_by_index: dict[int, str] = {}+ # Running partial-stream buffers. Summary AssistantMessages can+ # arrive *before* the corresponding ``content_block_stop`` event+ # (the CLI flushes the summary as soon as the block is complete+ # on the provider side, with the stop event following as a+ # separate frame). Reconcile-by-index therefore can't rely on+ # completed-block queues — instead we maintain running buffers+ # of all partial output of each type, and each summary block of+ # that type consumes its prefix. This also trivially handles+ # Kimi K2.6's pattern of emitting each content block as its own+ # summary AssistantMessage: Python list indices don't align+ # with Anthropic content_block indices, but per-type order does.+ self._partial_text_buffer: str = ""+ self._partial_thinking_buffer: str = ""+ # Coalescing buffer for ``thinking_delta`` — text_delta is+ # naturally coarser so we let it through unbuffered.+ self._pending_thinking_delta: str = ""+ self._pending_thinking_index: int | None = None+ self._last_thinking_flush_monotonic: float = 0.0@propertydef has_unresolved_tool_calls(self) -> bool:@@ -106,6 +149,15 @@ class SDKResponseAdapter:# produced (task_progress events were previously silent).responses.append(StreamHeartbeat())+ elif isinstance(sdk_message, StreamEvent):+ # Raw Anthropic streaming events — only delivered when+ # ``include_partial_messages=True`` is set on+ # ``ClaudeAgentOptions`` (gated by+ # ``config.sdk_include_partial_messages``). Drives per-token+ # emission of text + thinking; tool_use and other structural+ # events stay on the ``AssistantMessage`` path.+ self._handle_stream_event(sdk_message, responses)+elif isinstance(sdk_message, AssistantMessage):# Flush any SDK built-in tool calls that didn't get a UserMessage# result (e.g. WebSearch, Read handled internally by the CLI).@@ -122,18 +174,43 @@ class SDKResponseAdapter:responses.append(StreamStartStep())self.step_open = True- for block in sdk_message.content:+ # Hoist ThinkingBlocks to the front of the iteration so the UI+ # sees reasoning *before* the answer it produced — that's the+ # natural reading order and the way Anthropic models emit them.+ # OpenRouter passthrough providers (Moonshot/Kimi, DeepSeek)+ # often place ``reasoning`` after the visible text in the+ # response, which would make ``ReasoningCollapse`` render under+ # the assistant message instead of above it. ToolUse and other+ # block types stay in their original relative order so tool+ # call sequences remain coherent.+ #+ # Note: when ``include_partial_messages=True`` is active the+ # per-token stream already emitted reasoning + text in their+ # natural on-the-wire order via ``_handle_stream_event``. The+ # summary walk below falls through to ``_emit_text_tail`` /+ # ``_emit_thinking_tail`` which emit only the diff, preserving+ # that ordering without duplicating content.+ blocks_with_idx = sorted(+ enumerate(sdk_message.content),+ key=lambda pair: 0 if isinstance(pair[1], ThinkingBlock) else 1,+ )++ for block_index, block in blocks_with_idx:if isinstance(block, TextBlock):- if block.text:- # Reasoning and text are distinct UI parts; close- # any open reasoning block before opening text so- # the AI SDK transport doesn't merge them.+ # Reasoning and text are distinct UI parts; close any+ # open reasoning block before opening text so the AI+ # SDK transport doesn't merge them.+ tail = self._text_tail_for_summary_block(block.text)+ if tail:self._end_reasoning_if_open(responses)self._ensure_text_started(responses)responses.append(- StreamTextDelta(id=self.text_block_id, delta=block.text)+ StreamTextDelta(id=self.text_block_id, delta=tail))self._text_since_last_tool_result = True+ elif block.text:+ # Partial stream already emitted the full text.+ self._text_since_last_tool_result = Trueelif isinstance(block, ThinkingBlock):# Stream extended_thinking content as a reasoning@@ -160,13 +237,27 @@ class SDKResponseAdapter:# into the SDK transcript via# ``_format_sdk_content_blocks`` is unaffected — that# feeds ``--resume`` continuity, not the UI.- if block.thinking:+ #+ # Flush any pending coalesce buffer to the wire BEFORE+ # computing the tail — otherwise a summary that+ # arrives between the last partial delta and the+ # ``content_block_stop`` event (race: summary is+ # flushed by the CLI as soon as the block is complete+ # provider-side, with stop lagging as a separate+ # frame) would see ``_partial_thinking_buffer``+ # missing the pending prefix, and+ # ``_thinking_tail_for_summary_block`` would emit the+ # full block — duplicating the tail that+ # ``_end_reasoning_if_open`` still drains on stop.+ self._flush_pending_thinking(responses)+ tail = self._thinking_tail_for_summary_block(block.thinking)+ if tail:self._end_text_if_open(responses)self._ensure_reasoning_started(responses)responses.append(StreamReasoningDelta(id=self.reasoning_block_id,- delta=block.thinking,+ delta=tail,))@@ -380,11 +471,238 @@ class SDKResponseAdapter:self.has_started_reasoning = Truedef _end_reasoning_if_open(self, responses: list[StreamBaseResponse]) -> None:- """End the current reasoning block if one is open."""+ """End the current reasoning block if one is open.++ Drains any buffered thinking_delta text so the tail isn't lost+ when the block closes before the coalesce window elapses.+ """if self.has_started_reasoning and not self.has_ended_reasoning:+ if self._pending_thinking_delta:+ responses.append(+ StreamReasoningDelta(+ id=self.reasoning_block_id,+ delta=self._pending_thinking_delta,+ )+ )+ self._partial_thinking_buffer += self._pending_thinking_delta+ self._pending_thinking_delta = ""+ self._pending_thinking_index = Noneresponses.append(StreamReasoningEnd(id=self.reasoning_block_id))self.has_ended_reasoning = True+ # ------------------------------------------------------------------+ # Partial-message streaming (CHAT_SDK_INCLUDE_PARTIAL_MESSAGES)+ # ------------------------------------------------------------------++ def _reset_partial_stream_state(self) -> None:+ """Clear per-message partial-stream state.++ Anthropic's ``content_block`` indices are scoped to a single+ message — when a fresh ``message_start`` event arrives (new+ ``AssistantMessage`` turn) the maps must reset so indices from+ the previous message don't suppress genuine content in the new+ one.++ Also clears ``_partial_*_buffer``: multi-round turns (tool use)+ emit a ``message_start`` per LLM round, and leftover prefix+ content from round N would cause the summary walk in round N+1+ to either match the wrong prefix (silently dropping new content)+ or diverge and fall back to re-emitting the whole block.+ """+ self._block_types_by_index = {}+ self._partial_text_buffer = ""+ self._partial_thinking_buffer = ""+ self._pending_thinking_delta = ""+ self._pending_thinking_index = None++ def _text_tail_for_summary_block(self, full_text: str) -> str:+ """Reconcile the next summary ``TextBlock`` against the running+ partial-stream buffer.++ The CLI can emit the summary ``AssistantMessage`` before the+ matching ``content_block_stop`` event, so we can't rely on a+ queue of completed blocks. Instead we maintain+ ``_partial_text_buffer`` — the concatenation of every+ ``text_delta`` chunk that hasn't been claimed by a summary+ block yet — and consume ``full_text`` as a prefix from it.+ Summary blocks that have no partial backing (buffer empty)+ emit their full text; blocks that partial covered wholly are+ silent; blocks with a partial prefix + a summary tail emit+ only the tail. Kimi K2.6's pattern of emitting each content+ block as its own summary ``AssistantMessage`` is handled+ automatically because block order is preserved across both+ streams.+ """+ if not full_text:+ return ""+ if not self._partial_text_buffer:+ return full_text+ if full_text.startswith(self._partial_text_buffer):+ tail = full_text[len(self._partial_text_buffer) :]+ self._partial_text_buffer = ""+ return tail+ if self._partial_text_buffer.startswith(full_text):+ # Partial already emitted this whole block plus more — the+ # "more" belongs to a later summary block. Consume only the+ # prefix matching this block and leave the rest buffered.+ self._partial_text_buffer = self._partial_text_buffer[len(full_text) :]+ return ""+ logger.warning(+ "SDK partial/summary text diverged "+ "(partial_buf=%d chars, summary=%d chars) — emitting summary, "+ "clearing partial buffer to recover",+ len(self._partial_text_buffer),+ len(full_text),+ )+ self._partial_text_buffer = ""+ return full_text++ def _thinking_tail_for_summary_block(self, full_thinking: str) -> str:+ """Same as :meth:`_text_tail_for_summary_block` for reasoning."""+ if not full_thinking:+ return ""+ if not self._partial_thinking_buffer:+ return full_thinking+ if full_thinking.startswith(self._partial_thinking_buffer):+ tail = full_thinking[len(self._partial_thinking_buffer) :]+ self._partial_thinking_buffer = ""+ return tail+ if self._partial_thinking_buffer.startswith(full_thinking):+ self._partial_thinking_buffer = self._partial_thinking_buffer[+ len(full_thinking) :+ ]+ return ""+ logger.warning(+ "SDK partial/summary thinking diverged "+ "(partial_buf=%d chars, summary=%d chars) — emitting summary, "+ "clearing partial buffer to recover",+ len(self._partial_thinking_buffer),+ len(full_thinking),+ )+ self._partial_thinking_buffer = ""+ return full_thinking++ def _handle_stream_event(+ self, evt: StreamEvent, responses: list[StreamBaseResponse]+ ) -> None:+ """Translate raw Anthropic streaming events into wire events.++ Handles four event types; everything else (``message_delta``+ stop reasons, ``signature_delta``, ``input_json_delta``,+ ``ping``, ...) is ignored because the summary ``AssistantMessage``+ carries their effects.++ * ``message_start`` — new message boundary, reset per-index maps+ * ``content_block_start`` — open text / reasoning block on the+ wire and remember the block type at that index+ * ``content_block_delta`` — forward ``text_delta`` immediately+ and coalesce ``thinking_delta`` (64-char / 50 ms window)+ * ``content_block_stop`` — drain any buffered thinking and close+ the corresponding wire block+ """+ raw: dict[str, Any] = evt.event or {}+ event_type = raw.get("type")++ if event_type == "message_start":+ self._reset_partial_stream_state()+ return++ if event_type == "content_block_start":+ block = raw.get("content_block") or {}+ index = raw.get("index")+ block_type = block.get("type")+ if not isinstance(index, int) or not isinstance(block_type, str):+ return+ self._block_types_by_index[index] = block_type+ if block_type == "text":+ self._end_reasoning_if_open(responses)+ self._ensure_text_started(responses)+ # Seed any preamble the block_start carries.+ seed = block.get("text") or ""+ if seed:+ responses.append(StreamTextDelta(id=self.text_block_id, delta=seed))+ self._partial_text_buffer += seed+ self._text_since_last_tool_result = True+ elif block_type == "thinking":+ self._end_text_if_open(responses)+ self._ensure_reasoning_started(responses)+ self._last_thinking_flush_monotonic = time.monotonic()+ # tool_use / server_tool_use / redacted_thinking blocks stay+ # on the ``AssistantMessage`` path — the frontend widgets+ # need the final ``input`` payload which only arrives in the+ # summary.+ return++ if event_type == "content_block_delta":+ index = raw.get("index")+ if not isinstance(index, int):+ return+ delta = raw.get("delta") or {}+ delta_type = delta.get("type")+ if delta_type == "text_delta":+ chunk = delta.get("text") or ""+ if not chunk:+ return+ self._ensure_text_started(responses)+ responses.append(StreamTextDelta(id=self.text_block_id, delta=chunk))+ self._partial_text_buffer += chunk+ self._text_since_last_tool_result = True+ elif delta_type == "thinking_delta":+ chunk = delta.get("thinking") or ""+ if not chunk:+ return+ self._ensure_reasoning_started(responses)+ # Flush the coalesce buffer if the index changed — shouldn't+ # happen in practice but guard against interleaved indices.+ if (+ self._pending_thinking_index is not None+ and self._pending_thinking_index != index+ ):+ self._flush_pending_thinking(responses)+ self._pending_thinking_delta += chunk+ self._pending_thinking_index = index+ now = time.monotonic()+ elapsed_ms = (now - self._last_thinking_flush_monotonic) * 1000.0+ if (+ len(self._pending_thinking_delta) >= _THINKING_COALESCE_MIN_CHARS+ or elapsed_ms >= _THINKING_COALESCE_MAX_INTERVAL_MS+ ):+ self._flush_pending_thinking(responses)+ self._last_thinking_flush_monotonic = now+ # Other delta types (``signature_delta``, ``input_json_delta``)+ # are CLI / tool-dispatch plumbing — not surfaced on the wire.+ return++ if event_type == "content_block_stop":+ index = raw.get("index")+ if not isinstance(index, int):+ return+ block_type = self._block_types_by_index.pop(index, None)+ if block_type == "text":+ self._end_text_if_open(responses)+ elif block_type == "thinking":+ self._end_reasoning_if_open(responses)+ return++ def _flush_pending_thinking(self, responses: list[StreamBaseResponse]) -> None:+ """Drain the coalesce buffer into a ``StreamReasoningDelta``.++ Separate from ``_end_reasoning_if_open`` because the coalesce+ window can flush mid-block (threshold hit) without closing the+ reasoning block.+ """+ if not self._pending_thinking_delta:+ return+ responses.append(+ StreamReasoningDelta(+ id=self.reasoning_block_id,+ delta=self._pending_thinking_delta,+ )+ )+ self._partial_thinking_buffer += self._pending_thinking_delta+ self._pending_thinking_delta = ""+ self._pending_thinking_index = None+def _flush_unresolved_tool_calls(self, responses: list[StreamBaseResponse]) -> None:"""Emit outputs for tool calls that didn't receive a UserMessage result.diff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.pyindex b9a4237792..d61349542b 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py@@ -6,6 +6,7 @@ import pytestfrom claude_agent_sdk import (AssistantMessage,ResultMessage,+ StreamEvent,SystemMessage,TextBlock,ThinkingBlock,@@ -21,6 +22,7 @@ from backend.copilot.response_model import (StreamFinishStep,StreamHeartbeat,StreamReasoningDelta,+ StreamReasoningEnd,StreamStart,StreamStartStep,StreamTextDelta,@@ -298,6 +300,36 @@ def test_text_after_thinking_closes_reasoning_and_opens_text():assert re_idx < ts_idx+def test_thinking_after_text_in_same_message_renders_reasoning_first():+ """Kimi K2.6 (and other non-Anthropic OpenRouter providers) place+ ``reasoning`` AFTER the visible text in the response, so the SDK+ builds an ``AssistantMessage`` with content = [TextBlock, ThinkingBlock].+ Without reordering, the UI would show the answer first and the+ reasoning panel below it — the opposite of the natural reading+ order Anthropic models produce. response_adapter must hoist+ ThinkingBlocks to the front so ``reasoning-start/delta/end`` events+ hit the SSE stream BEFORE the ``text-*`` events."""+ adapter = _adapter()+ msg = AssistantMessage(+ content=[+ TextBlock(text="63"),+ ThinkingBlock(thinking="7 times 9 is 63", signature=""),+ ],+ model="test",+ )+ results = adapter.convert_message(msg)+ types = [type(r).__name__ for r in results]+ # ReasoningStart must land before TextStart in the emitted stream+ assert "StreamReasoningStart" in types+ assert "StreamTextStart" in types+ assert types.index("StreamReasoningStart") < types.index("StreamTextStart")+ # ReasoningDelta payload is intact+ assert any(+ isinstance(r, StreamReasoningDelta) and r.delta == "7 times 9 is 63"+ for r in results+ )++def test_tool_use_after_thinking_closes_reasoning():"""Opening a tool also closes an open reasoning block."""adapter = _adapter()@@ -1055,3 +1087,318 @@ def test_end_text_if_open_no_op_after_text_already_ended():second: list[StreamBaseResponse] = []adapter._end_text_if_open(second)assert second == []+++# ---------------------------------------------------------------------------+# Partial-message streaming (CHAT_SDK_INCLUDE_PARTIAL_MESSAGES)+# Covers the 10 scenarios in docs/sdk-per-token-streaming-followup.md+# ---------------------------------------------------------------------------+++def _stream_event(payload: dict) -> StreamEvent:+ """Convenience constructor for a raw Anthropic StreamEvent payload."""+ return StreamEvent(+ uuid="stream-evt",+ session_id="session-1",+ parent_tool_use_id=None,+ event=payload,+ )+++def _message_start() -> StreamEvent:+ return _stream_event({"type": "message_start"})+++def _text_block_start(index: int) -> StreamEvent:+ return _stream_event(+ {+ "type": "content_block_start",+ "index": index,+ "content_block": {"type": "text", "text": ""},+ }+ )+++def _text_delta(index: int, text: str) -> StreamEvent:+ return _stream_event(+ {+ "type": "content_block_delta",+ "index": index,+ "delta": {"type": "text_delta", "text": text},+ }+ )+++def _thinking_block_start(index: int) -> StreamEvent:+ return _stream_event(+ {+ "type": "content_block_start",+ "index": index,+ "content_block": {"type": "thinking", "thinking": ""},+ }+ )+++def _thinking_delta(index: int, text: str) -> StreamEvent:+ return _stream_event(+ {+ "type": "content_block_delta",+ "index": index,+ "delta": {"type": "thinking_delta", "thinking": text},+ }+ )+++def _block_stop(index: int) -> StreamEvent:+ return _stream_event({"type": "content_block_stop", "index": index})+++def _collect_text_deltas(responses):+ return "".join(r.delta for r in responses if isinstance(r, StreamTextDelta))+++def _collect_reasoning_deltas(responses):+ return "".join(r.delta for r in responses if isinstance(r, StreamReasoningDelta))+++class TestPartialMessageStreaming:+ """Scenarios 1-10 from sdk-per-token-streaming-followup.md.++ The adapter runs unconditionally in partial-aware mode — when the+ flag ``CHAT_SDK_INCLUDE_PARTIAL_MESSAGES`` is off the CLI simply+ never emits ``StreamEvent`` messages and the diff maps stay empty+ (so the tail logic degrades to "emit the full summary content"+ which is the pre-partial behaviour).+ """++ def test_partial_and_summary_agree_no_duplicate(self):+ """Scenario 1: partial streams full text, summary matches exactly.+ No duplicate emission, no truncation — full content reaches the+ wire once."""+ adapter = _adapter()+ full = "Hello world"+ responses: list[StreamBaseResponse] = []+ adapter._handle_stream_event(_text_block_start(0), responses)+ for chunk in ("Hello", " ", "world"):+ adapter._handle_stream_event(_text_delta(0, chunk), responses)+ adapter._handle_stream_event(_block_stop(0), responses)+ # Summary arrives with the same full text+ summary = adapter.convert_message(+ AssistantMessage(content=[TextBlock(text=full)], model="test")+ )+ combined = responses + summary+ assert _collect_text_deltas(combined) == full++ def test_partial_short_summary_long_tail_emitted(self):+ """Scenario 2 (the truncation bug we saw): partial emitted a+ prefix of the real answer; summary has the full text. The+ adapter must emit only the tail so no content is lost."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ adapter._handle_stream_event(_text_block_start(0), responses)+ for chunk in ("The user ", "seems confused. They sent"):+ adapter._handle_stream_event(_text_delta(0, chunk), responses)+ # Summary has the full, un-truncated content+ full = (+ "The user seems confused. They sent a short greeting. "+ "Let me offer them concrete options."+ )+ summary = adapter.convert_message(+ AssistantMessage(content=[TextBlock(text=full)], model="test")+ )+ combined = responses + summary+ assert _collect_text_deltas(combined) == full++ def test_partial_empty_summary_only(self):+ """Scenario 3: no partial deltas (CLI emitted the block entirely+ in the summary — short blocks, proxy buffering, encrypted+ content). Summary carries the full text."""+ adapter = _adapter()+ summary = adapter.convert_message(+ AssistantMessage(content=[TextBlock(text="short answer")], model="test")+ )+ assert _collect_text_deltas(summary) == "short answer"++ def test_partial_long_summary_matches_no_double_emit(self):+ """Scenario 4 (most common): partial streams everything, summary+ repeats the same content. No duplication on the wire."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ full = "Here is a long paragraph with several words in it."+ adapter._handle_stream_event(_text_block_start(0), responses)+ # Partition into chunks that *exactly* reconstruct ``full`` — a+ # word-split with trailing spaces would emit more content than+ # the summary carries and the reconcile would correctly flag+ # divergence.+ chunks = [full[:13], full[13:25], full[25:]]+ assert "".join(chunks) == full+ for chunk in chunks:+ adapter._handle_stream_event(_text_delta(0, chunk), responses)+ adapter._handle_stream_event(_block_stop(0), responses)+ assert _collect_text_deltas(responses) == full++ summary = adapter.convert_message(+ AssistantMessage(content=[TextBlock(text=full)], model="test")+ )+ # Summary must not add any TextDelta since partial already covered it+ assert _collect_text_deltas(summary) == ""++ def test_partial_diverges_summary_wins(self):+ """Scenario 5: partial content isn't a prefix of the summary.+ Defensive path emits the full summary content — content must+ not silently disappear."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ adapter._handle_stream_event(_text_block_start(0), responses)+ adapter._handle_stream_event(_text_delta(0, "first draft"), responses)+ # Summary has totally different content (proxy rewrote it)+ summary = adapter.convert_message(+ AssistantMessage(+ content=[TextBlock(text="final polished answer")],+ model="test",+ )+ )+ # The summary's text must reach the wire even though partial+ # already emitted "first draft" (which was the proxy's draft).+ assert "final polished answer" in _collect_text_deltas(responses + summary)++ def test_thinking_only_partial_coalesced(self):+ """Scenario 6a (thinking-only permutation): a run of+ ``thinking_delta`` events below the coalesce threshold flushes+ at ``content_block_stop`` so the reasoning tail isn't lost."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ adapter._handle_stream_event(_thinking_block_start(0), responses)+ # Each chunk is well under the 64-char threshold+ for chunk in ("Let ", "me ", "think"):+ adapter._handle_stream_event(_thinking_delta(0, chunk), responses)+ # At stop, the pending buffer drains+ adapter._handle_stream_event(_block_stop(0), responses)+ assert _collect_reasoning_deltas(responses) == "Let me think"+ # Block closed+ assert any(isinstance(r, StreamReasoningEnd) for r in responses)++ def test_text_only_via_partial_and_summary(self):+ """Scenario 6b (text-only permutation): partial fills a block,+ summary matches — see scenario 4 for no-double-emit assertion."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ adapter._handle_stream_event(_text_block_start(0), responses)+ adapter._handle_stream_event(_text_delta(0, "hi"), responses)+ adapter._handle_stream_event(_block_stop(0), responses)+ assert _collect_text_deltas(responses) == "hi"++ def test_mixed_text_then_thinking_partial_preserves_order(self):+ """Scenario 6c (mixed, Anthropic order — reasoning then text).+ When partial emits blocks in natural order and summary matches,+ the wire order is identical to emission order."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ # Anthropic-shape: thinking index 0, text index 1+ adapter._handle_stream_event(_thinking_block_start(0), responses)+ adapter._handle_stream_event(+ _thinking_delta(0, "X" * 80), responses+ ) # over threshold+ adapter._handle_stream_event(_block_stop(0), responses)+ adapter._handle_stream_event(_text_block_start(1), responses)+ adapter._handle_stream_event(_text_delta(1, "answer"), responses)+ adapter._handle_stream_event(_block_stop(1), responses)+ types = [type(r).__name__ for r in responses]+ # ReasoningStart must come before TextStart — partial streams in+ # the CLI's natural order, which is also the UI's desired order.+ assert types.index("StreamReasoningStart") < types.index("StreamTextStart")++ def test_multi_message_turn_resets_per_index_maps(self):+ """Scenario 7: tool-use loop creates multiple AssistantMessages+ per turn. Anthropic content-block indices are scoped to a single+ message — ``message_start`` must reset the diff maps so the next+ message's index-0 text isn't silently suppressed."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ # First message at index 0 = "first"+ adapter._handle_stream_event(_message_start(), responses)+ adapter._handle_stream_event(_text_block_start(0), responses)+ adapter._handle_stream_event(_text_delta(0, "first"), responses)+ adapter._handle_stream_event(_block_stop(0), responses)+ # New message starts — index 0 now refers to a fresh block+ adapter._handle_stream_event(_message_start(), responses)+ adapter._handle_stream_event(_text_block_start(0), responses)+ adapter._handle_stream_event(_text_delta(0, "second"), responses)+ adapter._handle_stream_event(_block_stop(0), responses)+ # Both texts must land on the wire+ assert _collect_text_deltas(responses) == "firstsecond"++ def test_empty_thinking_with_signature_emits_nothing(self):+ """Scenario 8: encrypted / empty thinking block. Partial emits+ nothing, summary carries ``block.thinking == ""`` with a+ signature — the adapter must not open a reasoning block."""+ adapter = _adapter()+ summary = adapter.convert_message(+ AssistantMessage(+ content=[ThinkingBlock(thinking="", signature="sig")],+ model="test",+ )+ )+ # No reasoning events should be emitted for empty thinking+ reasoning_events = [+ r+ for r in summary+ if isinstance(r, StreamReasoningDelta)+ or type(r).__name__ in ("StreamReasoningStart", "StreamReasoningEnd")+ ]+ assert reasoning_events == []++ def test_thinking_tail_drains_on_block_stop(self):+ """Scenario 10: a thinking_delta chunk smaller than the 64-char+ threshold arrives, then ``content_block_stop``. The tail text+ must emit in a final ``StreamReasoningDelta`` BEFORE+ ``StreamReasoningEnd``."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ adapter._handle_stream_event(_thinking_block_start(0), responses)+ # One small chunk well under 64 chars+ adapter._handle_stream_event(_thinking_delta(0, "tiny chunk"), responses)+ # Block stop must flush the pending buffer+ adapter._handle_stream_event(_block_stop(0), responses)+ types = [type(r).__name__ for r in responses]+ # The final ReasoningDelta must precede ReasoningEnd+ rd_idx = types.index("StreamReasoningDelta")+ re_idx = types.index("StreamReasoningEnd")+ assert rd_idx < re_idx+ assert _collect_reasoning_deltas(responses) == "tiny chunk"++ def test_thinking_coalesces_on_char_threshold(self):+ """Extra: thinking_delta accumulating past 64 chars flushes+ mid-block without waiting for block_stop (coalesce threshold)."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ adapter._handle_stream_event(_thinking_block_start(0), responses)+ # One 80-char chunk trips the threshold on a single event+ adapter._handle_stream_event(_thinking_delta(0, "x" * 80), responses)+ # A ReasoningDelta must already have been emitted (not buffered+ # until block_stop).+ assert any(isinstance(r, StreamReasoningDelta) for r in responses)+++# ---------------------------------------------------------------------------+# Partial/summary reconcile — summary walk must not duplicate partial content+# ---------------------------------------------------------------------------+++def test_summary_walk_skips_fully_streamed_text():+ """If the partial stream delivered the entire TextBlock, the summary+ walk must not emit a second ``StreamTextDelta`` for the same block."""+ adapter = _adapter()+ responses: list[StreamBaseResponse] = []+ adapter._handle_stream_event(_text_block_start(0), responses)+ adapter._handle_stream_event(_text_delta(0, "complete answer"), responses)+ adapter._handle_stream_event(_block_stop(0), responses)+ # Summary arrives with matching content+ summary = adapter.convert_message(+ AssistantMessage(content=[TextBlock(text="complete answer")], model="test")+ )+ # Partial path emitted exactly one StreamTextDelta+ partial_deltas = [r for r in responses if isinstance(r, StreamTextDelta)]+ summary_deltas = [r for r in summary if isinstance(r, StreamTextDelta)]+ assert len(partial_deltas) == 1+ assert summary_deltas == []diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex eb6babba2a..d62ba2afff 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -27,6 +27,7 @@ from claude_agent_sdk import (ClaudeAgentOptions,ClaudeSDKClient,ResultMessage,+ StreamEvent,TextBlock,ThinkingBlock,ToolResultBlock,@@ -130,6 +131,7 @@ from ..transcript import (from ..transcript_builder import TranscriptBuilderfrom .compaction import CompactionTracker, filter_compaction_messagesfrom .env import build_sdk_env # noqa: F401 — re-export for backward compat+from .openrouter_cost import record_turn_cost_from_openrouterfrom .response_adapter import SDKResponseAdapterfrom .security_hooks import create_security_hooksfrom .tool_adapter import (@@ -365,6 +367,14 @@ class _RetryState:# ``detect_gap`` picks them up as gap-fill entries instead of assuming the# JSONL already covers them.midturn_user_rows: int = 0+ # OpenRouter generation IDs collected across all attempts of this turn.+ # Populated from ``AssistantMessage.message_id`` when routed via+ # OpenRouter (``gen-...`` prefix). Consumed by the finally block to+ # fire ``record_turn_cost_from_openrouter`` for non-Anthropic models —+ # the CLI's static-Anthropic-priced estimate is replaced with the+ # authoritative ``/generation`` total_cost. Lives on ``_RetryState``+ # (not per-attempt ``_StreamAccumulator``) so it survives retries.+ generation_ids: list[str] = dataclass_field(default_factory=list)@dataclass@@ -679,24 +689,87 @@ async def _iter_sdk_messages(def _normalize_model_name(raw_model: str) -> str:"""Normalize a model name for the current routing configuration.- Applies two transformations shared by both the primary and fallback- model resolution paths:-- 1. **Strip provider prefix** — OpenRouter-style names like- ``"anthropic/claude-opus-4.6"`` are reduced to ``"claude-opus-4.6"``.- 2. **Dot-to-hyphen conversion** — when *not* routing through OpenRouter- the direct Anthropic API requires hyphen-separated versions- (``"claude-opus-4-6"``), so dots are replaced with hyphens.+ Two routing modes:++ 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+ unchanged so OpenRouter can route to the correct provider. Anthropic+ names happen to also resolve when stripped, but non-Anthropic vendors+ (Moonshot, Google, etc.) do not — keeping the prefix is the only form+ that 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."""+ if config.openrouter_active:+ return raw_modelmodel = raw_modelif "/" in model:- model = model.split("/", 1)[1]- # OpenRouter uses dots in versions (claude-opus-4.6) but the direct- # Anthropic API requires hyphens (claude-opus-4-6). Only normalise- # when NOT routing through OpenRouter.- if not config.openrouter_active:- model = model.replace(".", "-")- return 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."+ )+ return model.replace(".", "-")+++# Per-million-token rates ($USD) for non-Anthropic OpenRouter slugs that+# the Claude Agent SDK CLI doesn't recognise. The CLI's bundled pricing+# table only knows Anthropic models — for anything else its+# ``ResultMessage.total_cost_usd`` silently falls back to Sonnet rates,+# over-billing by ~5x for cheaper models like Kimi K2.6. Values are taken+# directly from each provider's published rate card and must be kept in+# sync when prices change. Cache discounts are not applied — Kimi via+# OpenRouter does not currently expose a separate cached-input price.+_NON_ANTHROPIC_RATES_USD_PER_MTOK: dict[str, tuple[float, float]] = {+ # vendor/model: (input_per_mtok, output_per_mtok)+ "moonshotai/kimi-k2.6": (0.60, 2.80),+ "moonshotai/kimi-k2-thinking": (0.60, 2.80),+ "moonshotai/kimi-k2.5": (0.60, 2.80),+ "moonshotai/kimi-k2": (0.60, 2.80),+}+++def _override_cost_for_non_anthropic(+ raw_model: str | None,+ sdk_reported_usd: float,+ prompt_tokens: int,+ completion_tokens: int,+ cache_read_tokens: int,+ cache_creation_tokens: int,+) -> float:+ """Recompute turn cost from a known rate card for non-Anthropic models.++ The Claude Agent SDK CLI's ``total_cost_usd`` is computed from a+ static Anthropic pricing table baked into the binary — it doesn't+ know Kimi/DeepSeek/etc rates and silently bills at Sonnet prices,+ which would over-charge a Kimi-default deployment by ~5x. Mirror+ the baseline path's behaviour by computing the real cost from the+ token counts whenever we recognise the slug; otherwise trust the+ SDK number (correct for Anthropic models, best-effort for unknown+ providers).+ """+ if raw_model is None:+ return sdk_reported_usd+ rates = _NON_ANTHROPIC_RATES_USD_PER_MTOK.get(raw_model)+ if rates is None:+ return sdk_reported_usd+ input_rate, output_rate = rates+ # Treat cache reads/creation as plain prompt tokens since OpenRouter+ # does not currently report a discounted cached-input price for the+ # tracked Moonshot endpoints.+ total_prompt = prompt_tokens + cache_read_tokens + cache_creation_tokens+ return (total_prompt * input_rate + completion_tokens * output_rate) / 1_000_000def _resolve_sdk_model() -> str | None:@@ -2089,6 +2162,24 @@ async def _run_stream_attempt(len(state.adapter.resolved_tool_calls),)+ # Capture OpenRouter generation IDs from each+ # ``AssistantMessage.message_id`` — when routed via OpenRouter+ # these are ``gen-...`` slugs we can use post-turn to query+ # ``/api/v1/generation?id=`` for the authoritative per-turn+ # cost and token counts (the CLI's ``total_cost_usd`` is+ # computed from a static Anthropic pricing table that+ # silently over-bills non-Anthropic routes). Direct-Anthropic+ # turns produce ``msg_...`` IDs which the generation endpoint+ # doesn't know about — harmlessly ignored at reconcile time.+ if isinstance(sdk_msg, AssistantMessage):+ msg_id = sdk_msg.message_id+ if (+ msg_id is not None+ and msg_id.startswith("gen-")+ and msg_id not in state.generation_ids+ ):+ state.generation_ids.append(msg_id)+# Log AssistantMessage API errors (e.g. invalid_request)# so we can debug Anthropic API 400s surfaced by the CLI.sdk_error = getattr(sdk_msg, "error", None)@@ -2257,7 +2348,23 @@ async def _run_stream_attempt(state.usage.completion_tokens,)if sdk_msg.total_cost_usd is not None:- state.usage.cost_usd = sdk_msg.total_cost_usd+ # The SDK CLI's ``total_cost_usd`` is computed from a+ # static Anthropic pricing table baked into the CLI+ # binary. When we route through OpenRouter to a non-+ # Anthropic model (e.g. Kimi K2.6) the CLI doesn't+ # know the real per-token price and silently falls+ # back to Sonnet rates — over-billing the user ~5x.+ # Recompute from a known rate card for non-Anthropic+ # OpenRouter slugs so the cost row, the rate-limit+ # counter, and the UI cost display reflect reality.+ state.usage.cost_usd = _override_cost_for_non_anthropic(+ raw_model=getattr(state.options, "model", None),+ sdk_reported_usd=sdk_msg.total_cost_usd,+ prompt_tokens=state.usage.prompt_tokens,+ completion_tokens=state.usage.completion_tokens,+ cache_read_tokens=state.usage.cache_read_tokens,+ cache_creation_tokens=state.usage.cache_creation_tokens,+ )# Emit compaction end if SDK finished compacting.# Sync TranscriptBuilder with the CLI's active context.@@ -2451,16 +2558,36 @@ async def _run_stream_attempt(# flush the assistant message before tool_calls are set on it# (text and tool_use arrive as separate SDK events), the# tool_calls update is lost — the next flush starts past it.- _msgs_since_flush += 1+ #+ # With ``include_partial_messages=True`` the CLI delivers+ # hundreds of ``StreamEvent`` messages per turn — incrementing+ # ``_msgs_since_flush`` on each one trips the threshold long+ # before the assistant text is complete, saving a truncated+ # prefix that subsequent deltas can never extend (append-only).+ # Count only messages that produce a persisted row boundary+ # (AssistantMessage, UserMessage, ResultMessage) and skip+ # raw StreamEvents. Also skip when text or reasoning is+ # still in-flight on the adapter: the row is live and a flush+ # would lock it at its current length.+ if not isinstance(sdk_msg, StreamEvent):+ _msgs_since_flush += 1now = time.monotonic()has_pending_tools = (acc.has_appended_assistantand acc.accumulated_tool_callsand not acc.has_tool_results)- if not has_pending_tools and (- _msgs_since_flush >= _FLUSH_MESSAGE_THRESHOLD- or (now - _last_flush_time) >= _FLUSH_INTERVAL_SECONDS+ adapter = state.adapter+ has_open_block = (+ adapter.has_started_text and not adapter.has_ended_text+ ) or (adapter.has_started_reasoning and not adapter.has_ended_reasoning)+ if (+ not has_pending_tools+ and not has_open_block+ and (+ _msgs_since_flush >= _FLUSH_MESSAGE_THRESHOLD+ or (now - _last_flush_time) >= _FLUSH_INTERVAL_SECONDS+ )):try:await asyncio.shield(upsert_chat_session(ctx.session))@@ -2937,6 +3064,12 @@ async def stream_chat_completion_sdk(# Defaults ensure the finally block can always reference these safely even when# an early return (e.g. sdk_cwd error) skips their normal assignment below.sdk_model: str | None = None+ # Wall-clock timestamp captured before the CLI runs so the+ # OpenRouter reconcile can filter subagent JSONLs by mtime — only+ # files created during THIS turn contribute gen-IDs. Without this+ # the sweep would pick up prior turns' compaction files that persist+ # under ``<session_id>/subagents/``, double-billing the user.+ turn_start_ts = time.time()# Make sure there is no more code between the lock acquisition and try-block.try:@@ -3154,6 +3287,17 @@ async def stream_chat_completion_sdk(sdk_options_kwargs["effort"] = config.claude_agent_thinking_effortif sdk_model:sdk_options_kwargs["model"] = sdk_model+ if config.sdk_include_partial_messages:+ # Opt into per-token streaming — the CLI emits raw Anthropic+ # ``content_block_delta`` events as ``StreamEvent`` messages+ # ahead of each summary ``AssistantMessage`` so reasoning and+ # text land on the wire token-by-token (matching the baseline+ # path's UX shipped in #12873). ``SDKResponseAdapter`` consumes+ # the partial stream via ``_handle_stream_event`` and emits+ # only the tail diff from the subsequent summary, so content+ # never double-emits and a summary-only short block still+ # reaches the UI.+ sdk_options_kwargs["include_partial_messages"] = Trueif sdk_env:sdk_options_kwargs["env"] = sdk_env@@ -3885,19 +4029,104 @@ async def stream_chat_completion_sdk(# --- Persist token usage to session + rate-limit counters ---# Both must live in finally so they stay consistent even when an# exception interrupts the try block after StreamUsage was yielded.- await persist_and_record_usage(- session=session,- user_id=user_id,- prompt_tokens=turn_prompt_tokens,- completion_tokens=turn_completion_tokens,- cache_read_tokens=turn_cache_read_tokens,- cache_creation_tokens=turn_cache_creation_tokens,- log_prefix=log_prefix,- cost_usd=turn_cost_usd,- model=sdk_model or config.thinking_standard_model,- provider="anthropic",+ effective_model = sdk_model or config.thinking_standard_model+ # ``state`` is populated lazily inside the retry loop; when the+ # turn exits before the first attempt runs (e.g. very early+ # validation error) it's still None, so ``generation_ids`` is+ # empty by definition.+ collected_gen_ids: list[str] = (+ list(state.generation_ids) if state is not None else []+ )+ _use_openrouter_reconcile = bool(+ config.openrouter_active+ and config.sdk_reconcile_openrouter_cost+ and collected_gen_ids)+ # CLI project dir — used by the reconcile task to sweep for+ # compaction subagents' gen-IDs. ``sdk_cwd`` is the per-session+ # CLI working directory; the CLI encodes it into the project-dir+ # name the same way ``encode_cwd_for_cli`` does, and writes+ # the main transcript + any ``subagents/`` alongside it under+ # ``~/.claude/projects/<encoded>/``. Empty when sdk_cwd isn't+ # set (shouldn't happen in practice for SDK turns).+ cli_project_dir: str | None = None+ if sdk_cwd:+ cli_project_dir = os.path.join(+ os.path.expanduser("~/.claude/projects"),+ encode_cwd_for_cli(sdk_cwd),+ )++ if _use_openrouter_reconcile:+ # Defer the single cost-and-rate-limit write to a background+ # task that queries OpenRouter's authoritative+ # ``/generation?id=`` for every round in this turn. Covers+ # all vendors:+ #+ # * Non-Anthropic (Kimi et al): the CLI's ``total_cost_usd``+ # is computed from a static Anthropic rate table that+ # doesn't know the model — silently over-bills by ~5x.+ # The reconcile replaces it with OpenRouter's real bill.+ # * Anthropic via OpenRouter: the CLI's number matches+ # Anthropic's own rates penny-for-penny in the common+ # case, but the reconcile catches any rate change the+ # CLI binary hasn't picked up and any OpenRouter-side+ # divergence (cache-discount accounting, promo pricing).+ #+ # The task calls ``persist_and_record_usage`` exactly once+ # per turn — same method as the sync path, so append-only+ # cost-log + rate-limit counter update together. The sync+ # path below is skipped entirely when the reconcile fires,+ # so no double-counting. Kill-switch:+ # ``CHAT_SDK_RECONCILE_OPENROUTER_COST=false``.+ #+ # Brief window (~0.5-2s) where the rate-limit counter is+ # unaware of this turn — back-to-back turns in that window+ # see a stale counter.+ asyncio.create_task(+ record_turn_cost_from_openrouter(+ session=session,+ user_id=user_id,+ model=effective_model,+ prompt_tokens=turn_prompt_tokens,+ completion_tokens=turn_completion_tokens,+ cache_read_tokens=turn_cache_read_tokens,+ cache_creation_tokens=turn_cache_creation_tokens,+ generation_ids=collected_gen_ids,+ cli_project_dir=cli_project_dir,+ cli_session_id=session_id,+ turn_start_ts=turn_start_ts,+ fallback_cost_usd=turn_cost_usd,+ api_key=config.api_key,+ log_prefix=log_prefix,+ )+ )+ else:+ # Reconcile disabled, OpenRouter inactive, or subscription+ # path (no gen-IDs). Record the SDK CLI's+ # ``total_cost_usd`` synchronously: accurate for Anthropic+ # (same rate card as billing); for non-Anthropic it's the+ # rate-card estimate that ``_override_cost_for_non_anthropic``+ # caps (still 1.5-2x off vs real OpenRouter bill, but much+ # closer than the ~5x Sonnet-rate fallback).+ await persist_and_record_usage(+ session=session,+ user_id=user_id,+ prompt_tokens=turn_prompt_tokens,+ completion_tokens=turn_completion_tokens,+ cache_read_tokens=turn_cache_read_tokens,+ cache_creation_tokens=turn_cache_creation_tokens,+ log_prefix=log_prefix,+ cost_usd=turn_cost_usd,+ model=effective_model,+ # ``provider`` labels the cost-analytics row; the cost+ # value still comes from the SDK-reported number.+ # Tracks the actual upstream so the row matches reality:+ # OpenRouter when ``openrouter_active``, Anthropic+ # otherwise.+ provider=("open_router" if config.openrouter_active else "anthropic"),+ )+# --- Persist session messages ---# This MUST run in finally to persist messages even when the generator# is stopped early (e.g., user clicks stop, processor breaks stream loop).diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex 4eb5bc4ac2..0146fe53f1 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -366,38 +366,84 @@ class TestNormalizeModelName:The per-request model toggle calls _normalize_model_name with either``config.thinking_advanced_model`` (for 'advanced') or``config.thinking_standard_model`` (for 'standard'). These tests verify- the OpenRouter/provider-prefix stripping that keeps the value compatible- with the Claude CLI.+ the OpenRouter/direct-Anthropic split: OpenRouter routes by full+ ``vendor/model`` slug, while direct-Anthropic strips the prefix and+ converts dots to hyphens."""- def test_strips_anthropic_prefix(self):- assert _normalize_model_name("anthropic/claude-opus-4-6") == "claude-opus-4-6"+ @pytest.fixture+ def _direct_anthropic_config(self, monkeypatch: pytest.MonkeyPatch):+ """Force ``config.openrouter_active = False`` for prefix-strip tests.- def test_strips_openai_prefix(self):- assert _normalize_model_name("openai/gpt-4o") == "gpt-4o"+ Pins the SDK model fields to anthropic/* so the new+ ``_validate_sdk_model_vendor_compatibility`` model_validator+ permits ChatConfig construction.+ """+ from backend.copilot import config as cfg_mod++ cfg = cfg_mod.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",+ )+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)++ @pytest.fixture+ def _openrouter_config(self, monkeypatch: pytest.MonkeyPatch):+ """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",+ base_url="https://openrouter.ai/api/v1",+ use_claude_code_subscription=False,+ )+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)- def test_strips_google_prefix(self):- assert _normalize_model_name("google/gemini-2.5-flash") == "gemini-2.5-flash"+ def test_strips_anthropic_prefix(self, _direct_anthropic_config):+ assert _normalize_model_name("anthropic/claude-opus-4-6") == "claude-opus-4-6"- def test_already_normalized_unchanged(self):+ def test_rejects_non_anthropic_vendor_in_direct_mode(+ self, _direct_anthropic_config+ ):+ """Direct-Anthropic mode must fail loudly on non-Anthropic vendor+ slugs — silent strip would send e.g. ``gpt-4o`` to the Anthropic+ API and produce an opaque model_not_found error."""+ with pytest.raises(ValueError, match="requires an Anthropic model"):+ _normalize_model_name("openai/gpt-4o")+ with pytest.raises(ValueError, match="requires an Anthropic model"):+ _normalize_model_name("moonshotai/kimi-k2.6")+ with pytest.raises(ValueError, match="requires an Anthropic model"):+ _normalize_model_name("google/gemini-2.5-flash")++ def test_already_normalized_unchanged(self, _direct_anthropic_config):assert (_normalize_model_name("claude-sonnet-4-20250514")== "claude-sonnet-4-20250514")- def test_empty_string_unchanged(self):+ def test_empty_string_unchanged(self, _direct_anthropic_config):assert _normalize_model_name("") == ""- def test_opus_model_roundtrip(self):- """The exact string used for the 'opus' toggle strips correctly."""- assert _normalize_model_name("anthropic/claude-opus-4-6") == "claude-opus-4-6"+ def test_opus_model_dot_to_hyphen(self, _direct_anthropic_config):+ """Direct-Anthropic mode: dots in versions become hyphens."""+ assert _normalize_model_name("anthropic/claude-opus-4.6") == "claude-opus-4-6"- def test_sonnet_openrouter_model(self):- """Sonnet model as stored in config (OpenRouter-prefixed) strips cleanly."""+ def test_openrouter_keeps_anthropic_slug(self, _openrouter_config):+ """OpenRouter routes by full slug — keep prefix and dots intact."""assert (- _normalize_model_name("anthropic/claude-sonnet-4-6") == "claude-sonnet-4-6"+ _normalize_model_name("anthropic/claude-sonnet-4.6")+ == "anthropic/claude-sonnet-4.6")+ def test_openrouter_keeps_kimi_slug(self, _openrouter_config):+ """Non-Anthropic vendors (Moonshot) require the prefix to route."""+ assert _normalize_model_name("moonshotai/kimi-k2.6") == "moonshotai/kimi-k2.6"+# ---------------------------------------------------------------------------# _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 619fce3017..7f53cb67b5 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_test.py@@ -15,6 +15,7 @@ from .service import (_build_system_prompt_value,_is_sdk_disconnect_error,_normalize_model_name,+ _override_cost_for_non_anthropic,_prepare_file_attachments,_resolve_sdk_model,_safe_close_sdk_client,@@ -355,11 +356,16 @@ class TestNormalizeModelName:api_key=None,base_url=None,use_claude_code_subscription=False,+ # Pin SDK slugs to anthropic/* so the new+ # _validate_sdk_model_vendor_compatibility allows construction.+ thinking_standard_model="anthropic/claude-sonnet-4-6",+ thinking_advanced_model="anthropic/claude-opus-4-7",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)assert _normalize_model_name("anthropic/claude-opus-4.6") == "claude-opus-4-6"- def test_dots_preserved_for_openrouter(self, monkeypatch, _clean_config_env):+ def test_openrouter_keeps_full_slug(self, monkeypatch, _clean_config_env):+ """OpenRouter routes by ``vendor/model`` slug — keep prefix and dots."""from backend.copilot import config as cfg_modcfg = cfg_mod.ChatConfig(@@ -369,7 +375,11 @@ class TestNormalizeModelName:use_claude_code_subscription=False,)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)- assert _normalize_model_name("anthropic/claude-opus-4.6") == "claude-opus-4.6"+ assert (+ _normalize_model_name("anthropic/claude-opus-4.6")+ == "anthropic/claude-opus-4.6"+ )+ assert _normalize_model_name("moonshotai/kimi-k2.6") == "moonshotai/kimi-k2.6"def test_no_prefix_no_dots(self, monkeypatch, _clean_config_env):from backend.copilot import config as cfg_mod@@ -379,6 +389,8 @@ class TestNormalizeModelName: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",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)assert (@@ -390,8 +402,9 @@ class TestNormalizeModelName:class TestResolveSdkModel:"""Tests for _resolve_sdk_model — model ID resolution for the SDK CLI."""- def test_openrouter_active_keeps_dots(self, monkeypatch, _clean_config_env):- """When OpenRouter is fully active, model keeps dot-separated version."""+ def test_openrouter_active_keeps_full_slug(self, monkeypatch, _clean_config_env):+ """When OpenRouter is fully active, the canonical vendor/model slug+ is preserved so OpenRouter can route to the correct provider."""from backend.copilot import config as cfg_modcfg = cfg_mod.ChatConfig(@@ -403,7 +416,23 @@ class TestResolveSdkModel:use_claude_code_subscription=False,)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)- assert _resolve_sdk_model() == "claude-opus-4.6"+ assert _resolve_sdk_model() == "anthropic/claude-opus-4.6"++ def test_openrouter_active_kimi_slug(self, monkeypatch, _clean_config_env):+ """Non-Anthropic models (Kimi via Moonshot) require the prefix to+ survive OpenRouter routing — strip would leave an unroutable slug."""+ from backend.copilot import config as cfg_mod++ cfg = cfg_mod.ChatConfig(+ thinking_standard_model="moonshotai/kimi-k2.6",+ claude_agent_model=None,+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ use_claude_code_subscription=False,+ )+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)+ assert _resolve_sdk_model() == "moonshotai/kimi-k2.6"def test_openrouter_disabled_normalizes_to_hyphens(self, monkeypatch, _clean_config_env@@ -651,6 +680,8 @@ class TestSystemPromptPreset: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.claude_agent_cross_user_prompt_cache is True@@ -662,6 +693,8 @@ class TestSystemPromptPreset: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.claude_agent_cross_user_prompt_cache is False@@ -674,3 +707,79 @@ class TestIdleTimeoutConstant:def test_idle_timeout_is_10_min(self):assert _IDLE_TIMEOUT_SECONDS == 10 * 60+++class TestOverrideCostForNonAnthropic:+ """Verifies that turn costs routed through OpenRouter to non-Anthropic+ vendors use the platform's per-model rate card instead of the SDK+ CLI's static Anthropic pricing table — which silently falls back to+ Sonnet rates for unknown models and over-bills by ~5x."""++ def test_kimi_cost_recomputed_from_rate_card(self):+ """Kimi K2.6 @ $0.60 input / $2.80 output per MTok. 29564 prompt+ tokens + 78 completion should land at ~$0.018, not $0.09 (Sonnet)."""+ recomputed = _override_cost_for_non_anthropic(+ raw_model="moonshotai/kimi-k2.6",+ sdk_reported_usd=0.089862, # what the SDK CLI reported (Sonnet price)+ prompt_tokens=29564,+ completion_tokens=78,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ )+ expected = (29564 * 0.60 + 78 * 2.80) / 1_000_000+ assert recomputed == pytest.approx(expected, rel=1e-9)+ # Sanity-check against a hand-computed magnitude.+ assert 0.017 < recomputed < 0.019++ def test_anthropic_cost_unchanged(self):+ """Anthropic slugs pass through the SDK-reported value since the+ CLI's pricing table is correct for them."""+ result = _override_cost_for_non_anthropic(+ raw_model="anthropic/claude-sonnet-4.6",+ sdk_reported_usd=0.089862,+ prompt_tokens=29564,+ completion_tokens=78,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ )+ assert result == 0.089862++ def test_unknown_non_anthropic_vendor_passes_through(self):+ """A non-Anthropic slug not in the rate card falls back to the+ SDK-reported value — best-effort rather than misleading zero."""+ result = _override_cost_for_non_anthropic(+ raw_model="deepseek/some-new-model",+ sdk_reported_usd=0.05,+ prompt_tokens=10000,+ completion_tokens=500,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ )+ assert result == 0.05++ def test_none_model_passes_through(self):+ """Subscription mode / no-model case returns the SDK value."""+ result = _override_cost_for_non_anthropic(+ raw_model=None,+ sdk_reported_usd=0.07,+ prompt_tokens=100,+ completion_tokens=10,+ cache_read_tokens=0,+ cache_creation_tokens=0,+ )+ assert result == 0.07++ def test_cache_tokens_folded_into_prompt(self):+ """Since the Moonshot endpoints don't report discounted cached-+ input pricing, cache_read/creation tokens are priced at the same+ rate as regular prompt tokens."""+ recomputed = _override_cost_for_non_anthropic(+ raw_model="moonshotai/kimi-k2.6",+ sdk_reported_usd=0.5,+ prompt_tokens=1000,+ completion_tokens=0,+ cache_read_tokens=5000,+ cache_creation_tokens=2000,+ )+ expected = (1000 + 5000 + 2000) * 0.60 / 1_000_000+ assert recomputed == pytest.approx(expected, rel=1e-9)diff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.pyindex 01f3540c28..4b8849fb57 100644--- a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py@@ -1249,7 +1249,14 @@ class TestStripStaleThinkingBlocks:new_asst = self._asst_entry("msg_new",[- {"type": "thinking", "thinking": "latest thoughts"},+ # Anthropic-shape thinking block (has signature) — preserved+ # on the last turn. Signature-less variant covered by+ # ``test_strips_signatureless_last_turn_thinking``.+ {+ "type": "thinking",+ "thinking": "latest thoughts",+ "signature": "anthropic-sig",+ },{"type": "text", "text": "world"},],uuid="a2",@@ -1271,11 +1278,16 @@ class TestStripStaleThinkingBlocks:assert new_content[1]["type"] == "text"def test_preserves_last_assistant_thinking(self) -> None:- """The last assistant entry's thinking blocks must be preserved."""+ """The last assistant entry's thinking blocks must be preserved+ when they carry an Anthropic ``signature``. Signature-less+ blocks (e.g. from Kimi K2.6 via OpenRouter) are stripped to+ prevent ``Invalid `signature` in `thinking` block`` errors on+ a subsequent Anthropic-model turn — covered by+ ``test_strips_signatureless_last_turn_thinking``."""entry = self._asst_entry("msg_only",[- {"type": "thinking", "thinking": "must keep"},+ {"type": "thinking", "thinking": "must keep", "signature": "sig"},{"type": "text", "text": "response"},],)@@ -1284,6 +1296,47 @@ class TestStripStaleThinkingBlocks:lines = [json.loads(ln) for ln in result.strip().split("\n")]assert len(lines[0]["message"]["content"]) == 2+ def test_strips_signatureless_last_turn_thinking(self) -> None:+ """Cross-model fix (PR #12878): a signature-less thinking block+ on the last assistant turn must be stripped before a subsequent+ Anthropic-model dispatch tries to replay it."""+ entry = self._asst_entry(+ "msg_kimi",+ [+ # No signature → non-Anthropic provider+ {"type": "thinking", "thinking": "kimi reasoning"},+ {"type": "text", "text": "answer"},+ ],+ )+ content = _make_jsonl(entry)+ result = strip_stale_thinking_blocks(content)+ lines = [json.loads(ln) for ln in result.strip().split("\n")]+ types = [b["type"] for b in lines[0]["message"]["content"]]+ assert "thinking" not in types+ assert "text" in types++ def test_preserves_redacted_thinking_on_last_turn(self) -> None:+ """``redacted_thinking`` blocks are signature-less by design+ (encrypted ``data`` field instead). Stripping them on the last+ turn would violate Anthropic's value-identity requirement for+ multi-turn replay. The signature rule only applies to plain+ ``thinking`` blocks."""+ entry = self._asst_entry(+ "msg_anthropic",+ [+ # Anthropic-emitted redacted_thinking: has ``data``,+ # never has ``signature``.+ {"type": "redacted_thinking", "data": "encrypted_blob"},+ {"type": "text", "text": "response"},+ ],+ )+ content = _make_jsonl(entry)+ result = strip_stale_thinking_blocks(content)+ lines = [json.loads(ln) for ln in result.strip().split("\n")]+ types = [b["type"] for b in lines[0]["message"]["content"]]+ assert "redacted_thinking" in types+ assert "text" in types+def test_no_assistant_entries_returns_unchanged(self) -> None:"""Transcripts with only user entries should pass through unchanged."""user = self._user_entry("hello")@@ -1294,12 +1347,13 @@ class TestStripStaleThinkingBlocks:assert strip_stale_thinking_blocks("") == ""def test_multiple_turns_strips_all_but_last(self) -> None:- """With 3 assistant turns, only the last keeps thinking blocks."""+ """With 3 assistant turns, only the last keeps thinking blocks+ (and only when those blocks carry an Anthropic ``signature``)."""entries = [self._asst_entry("msg_1",[- {"type": "thinking", "thinking": "t1"},+ {"type": "thinking", "thinking": "t1", "signature": "s1"},{"type": "text", "text": "a1"},],uuid="a1",@@ -1308,7 +1362,7 @@ class TestStripStaleThinkingBlocks:self._asst_entry("msg_2",[- {"type": "thinking", "thinking": "t2"},+ {"type": "thinking", "thinking": "t2", "signature": "s2"},{"type": "text", "text": "a2"},],uuid="a2",@@ -1318,7 +1372,7 @@ class TestStripStaleThinkingBlocks:self._asst_entry("msg_3",[- {"type": "thinking", "thinking": "t3"},+ {"type": "thinking", "thinking": "t3", "signature": "s3"},{"type": "text", "text": "a3"},],uuid="a3",@@ -1339,16 +1393,18 @@ class TestStripStaleThinkingBlocks:assert lines[4]["message"]["content"][0]["type"] == "thinking"def test_same_msg_id_multi_entry_turn(self) -> None:- """Multiple entries sharing the same message.id (same turn) are preserved."""+ """Multiple entries sharing the same message.id (same turn) are+ preserved when their thinking blocks carry an Anthropic+ ``signature``."""entries = [self._asst_entry("msg_old",- [{"type": "thinking", "thinking": "old"}],+ [{"type": "thinking", "thinking": "old", "signature": "old_sig"}],uuid="a1",),self._asst_entry("msg_last",- [{"type": "thinking", "thinking": "t_part1"}],+ [{"type": "thinking", "thinking": "t_part1", "signature": "p1_sig"}],uuid="a2",parent="a1",),diff --git a/autogpt_platform/backend/backend/copilot/transcript.py b/autogpt_platform/backend/backend/copilot/transcript.pyindex 5a46760dfd..468a02f796 100644--- a/autogpt_platform/backend/backend/copilot/transcript.py+++ b/autogpt_platform/backend/backend/copilot/transcript.py@@ -195,16 +195,17 @@ def strip_stale_thinking_blocks(content: str) -> str:is_last_turn = (last_asst_msg_id is not None and msg.get("id") == last_asst_msg_id) or (last_asst_msg_id is None and i == last_asst_idx)- if (- msg.get("role") == "assistant"- and not is_last_turn- and isinstance(msg.get("content"), list)- ):+ if msg.get("role") == "assistant" and isinstance(msg.get("content"), list):content_blocks = msg["content"]+ producing_model = msg.get("model") if isinstance(msg, dict) else Nonefiltered = [bfor b in content_blocks- if not (isinstance(b, dict) and b.get("type") in _THINKING_BLOCK_TYPES)+ if not _should_strip_thinking_block(+ b,+ is_last_turn=is_last_turn,+ producing_model=producing_model,+ )]if len(filtered) < len(content_blocks):stripped_count += len(content_blocks) - len(filtered)@@ -310,23 +311,30 @@ def strip_for_upload(content: str) -> str:if uid in reparented:needs_reserialize = True- # Strip stale thinking blocks from non-last assistant entries+ # Strip stale thinking blocks from non-last assistant entries.+ # Also strip *signature-less* thinking blocks from the last entry —+ # those come from non-Anthropic providers (e.g. Kimi K2.6 via+ # OpenRouter) and are rejected with ``Invalid `signature` in+ # `thinking` block`` if a subsequent turn is dispatched to an+ # Anthropic model that re-validates them. Anthropic-emitted+ # thinking blocks always carry a non-empty ``signature`` field, so+ # this filter is a no-op on Sonnet/Opus turns and only kicks in+ # when the prior turn ran on a non-Anthropic vendor.if last_asst_idx is not None:msg = entry.get("message", {})is_last_turn = (last_asst_msg_id is not None and msg.get("id") == last_asst_msg_id) or (last_asst_msg_id is None and i == last_asst_idx)- if (- msg.get("role") == "assistant"- and not is_last_turn- and isinstance(msg.get("content"), list)- ):+ if msg.get("role") == "assistant" and isinstance(msg.get("content"), list):content_blocks = msg["content"]+ producing_model = msg.get("model") if isinstance(msg, dict) else Nonefiltered = [bfor b in content_blocks- if not (- isinstance(b, dict) and b.get("type") in _THINKING_BLOCK_TYPES+ if not _should_strip_thinking_block(+ b,+ is_last_turn=is_last_turn,+ producing_model=producing_model,)]if len(filtered) < len(content_blocks):@@ -951,6 +959,92 @@ ENTRY_TYPE_MESSAGE = "message"_THINKING_BLOCK_TYPES = frozenset({"thinking", "redacted_thinking"})+def _is_anthropic_model(model: str | None) -> bool:+ """True when *model* is an Anthropic-issued slug.++ Used to decide whether a thinking block's signature is+ cryptographically valid for Anthropic replay. Non-Anthropic vendors+ routed through OpenRouter's Anthropic-compat shim (Kimi K2.6,+ DeepSeek, GPT-OSS) sometimes emit thinking blocks with a+ placeholder signature — it passes a non-empty string check but+ fails Anthropic's cryptographic validation, producing the opaque+ ``Invalid signature in thinking block`` 400 on the next turn+ whenever the model toggle switches to Sonnet/Opus.+ """+ return isinstance(model, str) and model.startswith("anthropic/")+++def _should_strip_thinking_block(+ block: object,+ *,+ is_last_turn: bool,+ producing_model: str | None = None,+) -> bool:+ """Return True when *block* is a thinking block that should be removed+ from a transcript entry before upload.++ Strip only when the block CAN'T be replayed safely. Never strip a+ valid Anthropic-issued thinking block — it carries real reasoning+ state that preserves context continuity on ``--resume``.++ Strip rules (first match wins):++ 1. **Non-Anthropic producer (any position)** — thinking blocks from+ Kimi / DeepSeek / GPT-OSS via OpenRouter's Anthropic-compat shim+ carry either no signature or a placeholder string that passes a+ non-empty check but fails Anthropic's cryptographic validation.+ Strip unconditionally; they also add low-value tokens to the+ replay context.+ 2. **Malformed ``thinking`` (any position, Anthropic producer,+ empty signature)** — shouldn't happen in practice, but if the+ signature is missing / empty the block can't be validated.+ Safer to drop than to 400 the next turn.+ 3. **Stale non-last entry with unknown producer** — when the+ caller doesn't wire ``producing_model`` through (legacy paths /+ older tests) we can't tell if the block is safe to keep; fall+ back to the old behaviour of dropping non-last thinking blocks+ to avoid replaying an unverifiable block to Anthropic.++ Preserved:++ * Anthropic ``thinking`` with non-empty signature — at any+ position, last OR non-last. Keeping prior-turn reasoning+ chains helps continuity on multi-round SDK resumes without any+ risk of signature rejection.+ * Anthropic ``redacted_thinking`` — carries an encrypted ``data``+ payload instead of a ``signature``; by design signature-less,+ but Anthropic-issued and safely replayable.+ """+ if not isinstance(block, dict):+ return False+ btype = block.get("type")+ if btype not in _THINKING_BLOCK_TYPES:+ return False+ # Legacy call sites pass producing_model=None — preserve the old+ # "strip-all-non-last-thinking" heuristic for those so we don't+ # regress callers that haven't been updated.+ if producing_model is None:+ if not is_last_turn:+ return True+ if btype != "thinking":+ return False+ signature = block.get("signature")+ return not (isinstance(signature, str) and signature)+ # Non-Anthropic producer — strip at any position. These blocks+ # CAN'T be cryptographically validated by Anthropic on replay.+ if not _is_anthropic_model(producing_model):+ return True+ # Anthropic producer, redacted_thinking: always preserve — the+ # ``data`` field is the signature analog.+ if btype == "redacted_thinking":+ return False+ # Anthropic producer, ``thinking``: keep iff it has a real+ # (non-empty) signature. Empty-signature Anthropic thinking+ # shouldn't happen but guard against it anyway.+ signature = block.get("signature")+ return not (isinstance(signature, str) and signature)++def _flatten_assistant_content(blocks: list) -> str:"""Flatten assistant content blocks into a single plain-text string.diff --git a/autogpt_platform/backend/backend/copilot/transcript_test.py b/autogpt_platform/backend/backend/copilot/transcript_test.pyindex dde07a063e..96c7b3fc70 100644--- a/autogpt_platform/backend/backend/copilot/transcript_test.py+++ b/autogpt_platform/backend/backend/copilot/transcript_test.py@@ -591,7 +591,16 @@ class TestStripForUpload:"role": "assistant","id": "msg_new","content": [- {"type": "thinking", "thinking": "fresh thinking"},+ # Anthropic-style thinking block — has a signature so+ # ``_should_strip_thinking_block`` preserves it on the+ # last turn. Without the signature (e.g. emitted by+ # Kimi K2.6 via OpenRouter) it would be stripped — see+ # ``test_strips_signatureless_thinking_from_last_turn``.+ {+ "type": "thinking",+ "thinking": "fresh thinking",+ "signature": "anthropic-signed-blob",+ },{"type": "text", "text": "new answer"},],},@@ -624,6 +633,224 @@ class TestStripForUpload:new_types = [b["type"] for b in new_content if isinstance(b, dict)]assert "thinking" in new_types # last assistant preserved+ def test_strips_signatureless_thinking_from_last_turn(self):+ """Kimi K2.6 (and other non-Anthropic OpenRouter providers) emit+ thinking blocks without the Anthropic ``signature`` field. When+ a subsequent advanced-tier toggle replays the transcript to Opus,+ Anthropic's API rejects the signature-less block with ``Invalid+ `signature` in `thinking` block`` — so strip_for_upload must drop+ them from the LAST assistant entry too, not just stale ones."""+ user = {+ "type": "user",+ "uuid": "u1",+ "parentUuid": "",+ "message": {"role": "user", "content": "hi"},+ }+ # Last (and only) assistant entry with a Kimi-shape thinking block+ asst = {+ "type": "assistant",+ "uuid": "a1",+ "parentUuid": "u1",+ "message": {+ "role": "assistant",+ "id": "msg_kimi",+ "content": [+ # No ``signature`` field → non-Anthropic provider+ {"type": "thinking", "thinking": "kimi reasoning"},+ {"type": "text", "text": "answer"},+ ],+ },+ }+ content = _make_jsonl(user, asst)+ result = strip_for_upload(content)+ entries = [json.loads(line) for line in result.strip().split("\n")]+ asst_entry = next(+ e for e in entries if e.get("message", {}).get("id") == "msg_kimi"+ )+ types = [+ b["type"] for b in asst_entry["message"]["content"] if isinstance(b, dict)+ ]+ assert "thinking" not in types, (+ "Signature-less thinking block on last turn must be stripped "+ "to prevent Anthropic API rejection on model-switch replay"+ )+ assert "text" in types, "Text content must survive stripping"++ def test_strips_non_anthropic_thinking_with_placeholder_signature(self):+ """OpenRouter's Anthropic-compat shim can emit thinking blocks+ from non-Anthropic producers (Kimi K2.6, DeepSeek) with a+ PLACEHOLDER signature string that passes the "non-empty string"+ check but fails Anthropic's cryptographic validation on replay.++ Observed in session 864a55ba after model-toggle from standard+ (Kimi) to advanced (Opus): the CLI session upload included a+ thinking block with ``signature="ANTHROPIC_SHIM_PLACEHOLDER"``+ (or similar), Opus 4.7 rejected with 400 ``Invalid `signature`+ in `thinking` block``. Fix: strip thinking blocks from the+ LAST assistant turn whenever the producing ``model`` isn't an+ ``anthropic/*`` slug, regardless of signature presence."""+ user = {+ "type": "user",+ "uuid": "u1",+ "parentUuid": "",+ "message": {"role": "user", "content": "hi"},+ }+ asst = {+ "type": "assistant",+ "uuid": "a1",+ "parentUuid": "u1",+ "message": {+ "role": "assistant",+ "id": "msg_kimi_shim",+ "model": "moonshotai/kimi-k2.6-20260420",+ "content": [+ # Placeholder signature — non-empty but cryptographically+ # invalid for Anthropic. Legacy strip (signature-only)+ # would KEEP this block.+ {+ "type": "thinking",+ "thinking": "shimmed reasoning",+ "signature": "PLACEHOLDER_SHIM_SIG_abc123",+ },+ {"type": "text", "text": "answer"},+ ],+ },+ }+ content = _make_jsonl(user, asst)+ result = strip_for_upload(content)+ entries = [json.loads(line) for line in result.strip().split("\n")]+ asst_entry = next(+ e for e in entries if e.get("message", {}).get("id") == "msg_kimi_shim"+ )+ types = [+ b["type"] for b in asst_entry["message"]["content"] if isinstance(b, dict)+ ]+ assert "thinking" not in types, (+ "Non-Anthropic thinking block must be stripped even when it "+ "carries a placeholder signature — replay-to-Opus otherwise "+ "400s with Invalid signature"+ )+ assert "text" in types++ def test_preserves_anthropic_thinking_on_non_last_turn(self):+ """Anthropic ``thinking`` blocks on NON-last turns carry real+ reasoning state that helps context continuity on ``--resume``.+ Keep them when the producing model is known-Anthropic with a+ valid signature; strip only when we can't validate safely+ (legacy callers with no model info — falls through to the+ old stale-strip rule).+ """+ user = {+ "type": "user",+ "uuid": "u1",+ "parentUuid": "",+ "message": {"role": "user", "content": "first"},+ }+ asst1 = {+ "type": "assistant",+ "uuid": "a1",+ "parentUuid": "u1",+ "message": {+ "role": "assistant",+ "id": "msg_opus_prev",+ "model": "anthropic/claude-4.7-opus-20260416",+ "content": [+ {+ "type": "thinking",+ "thinking": "first-turn reasoning",+ "signature": "ANTHROPIC_SIG_1",+ },+ {"type": "text", "text": "first answer"},+ ],+ },+ }+ user2 = {+ "type": "user",+ "uuid": "u2",+ "parentUuid": "a1",+ "message": {"role": "user", "content": "second"},+ }+ asst2 = {+ "type": "assistant",+ "uuid": "a2",+ "parentUuid": "u2",+ "message": {+ "role": "assistant",+ "id": "msg_opus_last",+ "model": "anthropic/claude-4.7-opus-20260416",+ "content": [+ {+ "type": "thinking",+ "thinking": "last-turn reasoning",+ "signature": "ANTHROPIC_SIG_2",+ },+ {"type": "text", "text": "last answer"},+ ],+ },+ }+ content = _make_jsonl(user, asst1, user2, asst2)+ result = strip_for_upload(content)+ entries = [json.loads(line) for line in result.strip().split("\n")]++ # Prior Opus turn's thinking must survive — valid Anthropic+ # block with signature.+ prev = next(+ e for e in entries if e.get("message", {}).get("id") == "msg_opus_prev"+ )+ prev_types = [b["type"] for b in prev["message"]["content"]]+ assert "thinking" in prev_types, (+ "Anthropic thinking block on a non-last turn must be "+ "preserved — it carries real reasoning state"+ )+ # Last turn's thinking also preserved.+ last = next(+ e for e in entries if e.get("message", {}).get("id") == "msg_opus_last"+ )+ last_types = [b["type"] for b in last["message"]["content"]]+ assert "thinking" in last_types++ def test_preserves_anthropic_thinking_with_valid_signature(self):+ """Sanity: an Anthropic-issued thinking block with a real+ signature on the last turn must NOT be stripped — Anthropic+ requires value-identity on replay."""+ user = {+ "type": "user",+ "uuid": "u1",+ "parentUuid": "",+ "message": {"role": "user", "content": "hi"},+ }+ asst = {+ "type": "assistant",+ "uuid": "a1",+ "parentUuid": "u1",+ "message": {+ "role": "assistant",+ "id": "msg_opus",+ "model": "anthropic/claude-4.7-opus-20260416",+ "content": [+ {+ "type": "thinking",+ "thinking": "reasoning",+ "signature": "REAL_ANTHROPIC_SIG_blob",+ },+ {"type": "text", "text": "answer"},+ ],+ },+ }+ content = _make_jsonl(user, asst)+ result = strip_for_upload(content)+ entries = [json.loads(line) for line in result.strip().split("\n")]+ asst_entry = next(+ e for e in entries if e.get("message", {}).get("id") == "msg_opus"+ )+ types = [+ b["type"] for b in asst_entry["message"]["content"] if isinstance(b, dict)+ ]+ assert (+ "thinking" in types+ ), "Anthropic-signed thinking on last turn must survive strip"+ assert "text" in types+def test_empty_content(self):result = strip_for_upload("")# Empty string produces a single empty line after split, resulting in "\n"