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): report provider failures on the engine that actually runs (#14103)
Nicholas Tindle59ec8439c920
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex 23e8498ca3..a365e1460c 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -42,7 +42,7 @@ from backend.copilot.builder_context import (build_builder_context_turn_prefix,build_builder_system_prompt_suffix,)-from backend.copilot.config import CopilotLLMModel+from backend.copilot.config import CopilotLlmAuthProvider, CopilotLLMModelfrom backend.copilot.context import get_workspace_manager, set_execution_contextfrom backend.copilot.expert_context import build_expert_identity_suffixfrom backend.copilot.graphiti.config import is_enabled_for_user@@ -52,6 +52,7 @@ from backend.copilot.local_context_probe import (compaction_target_for_window,probe_local_context_window,)+from backend.copilot.markers import append_error_markerfrom backend.copilot.model import (ChatMessage,ChatSession,@@ -79,6 +80,7 @@ from backend.copilot.prompting import (get_delegation_supplement,get_graphiti_supplement,)+from backend.copilot.provider_failure import classify as classify_provider_failurefrom backend.copilot.rate_limit import build_budget_ctxfrom backend.copilot.response_model import (StreamBaseResponse,@@ -86,6 +88,7 @@ from backend.copilot.response_model import (StreamFinish,StreamFinishStep,StreamModeChanged,+ StreamProviderFailure,StreamStart,StreamStartStep,StreamStatus,@@ -445,6 +448,11 @@ class _BaselineStreamState:# ever produces "ld" | "catalog" | "env" ("fallback" is SDK-only, marking# a CLI 529-overload swap); typed as the shared RoutingSource superset.routing_source: RoutingSource = "env"+ # The connection this turn runs on, stamped onto each assistant row as+ # it is built. Recorded per turn so a later route change cannot rewrite+ # what already ran; see backend/copilot/segments.py.+ llm_auth_provider: CopilotLlmAuthProvider | None = None+ llm_credential_id: str | None = None# Live delivery channel drained concurrently by ``stream_chat_completion_baseline``# so reasoning / text / tool events reach the SSE wire **during** the upstream# LLM stream, not after ``_baseline_llm_caller`` returns. Before this was a@@ -1264,6 +1272,8 @@ def _baseline_conversation_updater(role="assistant",model=state.model,routing_source=state.routing_source,+ llm_auth_provider=state.llm_auth_provider,+ llm_credential_id=state.llm_credential_id,content=response.response_text or "",tool_calls=[{@@ -2151,7 +2161,16 @@ async def stream_chat_completion_baseline(logger.warning("[Baseline] Langfuse trace context setup failed")_stream_error = False # Track whether an error occurred during streaming- state = _BaselineStreamState(model=active_model, routing_source=routing_source)+ state = _BaselineStreamState(+ model=active_model,+ routing_source=routing_source,+ llm_auth_provider=(+ session.metadata.llm_auth_provider if session is not None else None+ ),+ llm_credential_id=(+ session.metadata.llm_credential_id if session is not None else None+ ),+ )# Bind extracted module-level callbacks to this request's state/session# using functools.partial so they satisfy the Protocol signatures.@@ -2309,6 +2328,8 @@ async def stream_chat_completion_baseline(content=text_only_text,model=state.model,routing_source=state.routing_source,+ llm_auth_provider=state.llm_auth_provider,+ llm_credential_id=state.llm_credential_id,))for _buffered in state.session_messages:@@ -2435,8 +2456,41 @@ async def stream_chat_completion_baseline(evt = state.pending_events.get_nowait()if evt is not None:yield evt- yield StreamError(errorText=error_msg, code="baseline_error")- # Still persist whatever we got+ failure = classify_provider_failure(+ e,+ auth_provider=session.metadata.llm_auth_provider if session else None,+ credential_id=session.metadata.llm_credential_id if session else None,+ message=error_msg,+ )+ # Written before the error is yielded, and deliberately not in+ # ``finally``. The consumer breaks out of its loop the moment it sees+ # a StreamError and closes this generator, so the tail of ``finally``+ # -- including its session upsert -- is cut short at the first await.+ # A marker appended there is built correctly and then thrown away.+ #+ # ``_session_holder[0]`` rather than ``session``: the inner task can+ # replace the binding mid-turn, and the marker has to land on the+ # object that is actually current.+ _failed_session = _session_holder[0]+ if append_error_marker(+ _failed_session,+ error_msg,+ retryable=failure.retryable if failure is not None else True,+ failure=failure.as_part() if failure is not None else None,+ ):+ try:+ await upsert_chat_session(_failed_session)+ except Exception as marker_err:+ logger.error(+ "[Baseline] Failed to persist the error marker: %s", marker_err+ )+ if failure is not None:+ # Before the error, so a client that acts on the envelope has it+ # in hand by the time the turn is reported failed.+ yield StreamProviderFailure(failure=failure.as_part())+ yield StreamError(errorText=error_msg, code=failure.kind.value)+ else:+ yield StreamError(errorText=error_msg, code="baseline_error")finally:# Cancel the inner task if we're unwinding early (client disconnect,# unexpected error in the consumer) so it doesn't keep streaming@@ -2585,6 +2639,8 @@ async def stream_chat_completion_baseline(content=final_text,model=state.model,routing_source=state.routing_source,+ llm_auth_provider=state.llm_auth_provider,+ llm_credential_id=state.llm_credential_id,))try:diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.pyindex f984d16714..521b980273 100644--- a/autogpt_platform/backend/backend/copilot/db.py+++ b/autogpt_platform/backend/backend/copilot/db.py@@ -12,6 +12,7 @@ from prisma.models import ChatMessage as PrismaChatMessagefrom prisma.models import ChatSession as PrismaChatSessionfrom prisma.types import (ChatMessageCreateInput,+ ChatMessageUpdateInput,ChatMessageWhereInput,ChatSessionCreateInput,ChatSessionUpdateInput,@@ -654,6 +655,17 @@ async def add_chat_messages_batch(if msg.get("routing_source") is not None:data["routingSource"] = msg["routing_source"]+ if msg.get("llm_auth_provider") is not None:+ data["llmAuthProvider"] = msg["llm_auth_provider"]+ if msg.get("llm_credential_id") is not None:+ data["llmCredentialId"] = msg["llm_credential_id"]++ # Per-row bag. The single-message path already persisted+ # this; the batch path silently dropped it, so anything+ # written here by a turn never survived the insert.+ if msg.get("metadata") is not None:+ data["metadata"] = SafeJson(msg["metadata"])+messages_data.append(data)# Run create_many and session update in parallel within transaction@@ -1172,17 +1184,31 @@ async def update_chat_message_stamps(sequence: int,model: str | None,routing_source: str | None,+ llm_auth_provider: str | None = None,+ llm_credential_id: str | None = None,) -> bool:- """Back-fill model/routingSource on an already-persisted message row.+ """Back-fill the execution stamps on an already-persisted message row.Mid-turn flushes persist assistant rows (assigning sequences) BEFOREthe end-of-turn stamping runs; this repairs those rows so theanalytics columns survive in the DB. Same mechanism and authorizationreasoning as ``update_chat_message_tool_calls``.++ The route is written only when known. Passing None for it would blank a+ row that a previous stamp already got right, which is precisely the+ rewriting of history per-turn segments exist to prevent."""+ data: ChatMessageUpdateInput = {+ "model": model,+ "routingSource": routing_source,+ }+ if llm_auth_provider is not None:+ data["llmAuthProvider"] = llm_auth_provider+ if llm_credential_id is not None:+ data["llmCredentialId"] = llm_credential_idresult = await PrismaChatMessage.prisma().update(where={"sessionId_sequence": {"sessionId": session_id, "sequence": sequence}},- data={"model": model, "routingSource": routing_source},+ data=data,)if not result:logger.warning(diff --git a/autogpt_platform/backend/backend/copilot/executor/processor.py b/autogpt_platform/backend/backend/copilot/executor/processor.pyindex 521bc38136..ecaf06b946 100644--- a/autogpt_platform/backend/backend/copilot/executor/processor.py+++ b/autogpt_platform/backend/backend/copilot/executor/processor.py@@ -550,7 +550,14 @@ class CoPilotProcessor:))except CodexCredentialBusyError:- raise RuntimeError("codex_credential_busy") from None+ # Re-raised as-is rather than flattened into a bare+ # RuntimeError: a ChatGPT connection serves one chat at a+ # time, so this is the single most likely thing a user+ # with two chats open will hit. Keeping the type is what+ # lets the failure say so, and say that waiting works --+ # ``RuntimeError("codex_credential_busy")`` reaches the+ # chat as an unexplained error with no advice attached.+ raiseexcept asyncio.CancelledError:raiseexcept Exception:diff --git a/autogpt_platform/backend/backend/copilot/markers.py b/autogpt_platform/backend/backend/copilot/markers.pynew file mode 100644index 0000000000..f10c7bbb68--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/markers.py@@ -0,0 +1,102 @@+"""Error markers persisted into a chat so a failure survives the stream.++A stream error lives only on the wire. When the SSE connection ends the+message is gone, and a reloaded chat shows the user's question followed by+nothing at all -- no answer, no error, no sign anything happened. The SDK+path solved this by appending a marker row; the baseline path never did.++The prefixes are a frontend rendering contract, not decoration:+``COPILOT_ERROR_PREFIX`` renders an error card, and+``COPILOT_RETRYABLE_ERROR_PREFIX`` renders one that also offers Try Again.+Choosing between them is a claim about whether trying again can work, so it+is made from the typed failure rather than assumed.+"""++from typing import Any++from backend.copilot.constants import (+ COPILOT_ERROR_PREFIX,+ COPILOT_RETRYABLE_ERROR_PREFIX,+ COPILOT_SYSTEM_PREFIX,+ STREAM_ERROR_MARKER,+ STREAM_INCOMPLETE_MARKER,+)+from backend.copilot.model import ChatMessage, ChatSession+++def is_error_marker(message: ChatMessage) -> bool:+ """True when this row is a marker rather than something the model said."""+ if message.role != "assistant" or not message.content:+ return False+ return message.content.startswith(+ (COPILOT_ERROR_PREFIX, COPILOT_RETRYABLE_ERROR_PREFIX)+ )+++def has_trailing_marker(session: ChatSession | None) -> bool:+ """True when the last row is already a marker.++ Several guards can fire on one failed turn. Without this check a single+ failure stacks two or three error cards on the same chat.+ """+ if session is None or not session.messages:+ return False+ last = session.messages[-1]+ if last.role != "assistant" or not last.content:+ return False+ return (+ is_error_marker(last)+ or last.content.startswith(COPILOT_SYSTEM_PREFIX)+ or last.content in (STREAM_ERROR_MARKER, STREAM_INCOMPLETE_MARKER)+ )+++# Key under which the typed failure rides on the marker row's metadata bag.+# Namespaced because the bag is shared with the dispatcher's submit-time+# payload and anything else that lands there later.+PROVIDER_FAILURE_KEY = "provider_failure"+++def append_error_marker(+ session: ChatSession | None,+ display_message: str,+ *,+ retryable: bool,+ failure: dict[str, Any] | None = None,+) -> bool:+ """Record a failure in the chat itself. Returns whether a row was added.++ ``retryable`` decides which card the user sees, so it must come from+ something that actually knows -- offering Try Again on an expired login+ or a spent quota costs the user a retry to learn it cannot work.++ ``failure`` is the typed envelope, stored on the row so a chat reopened+ tomorrow can still say *which* connection failed and what would fix it.+ Without it the prefix is all that survives, which distinguishes only+ "retry" from "do not retry" -- enough for a button, not enough to offer+ the switch-connection or reconnect the failure actually calls for.+ """+ if session is None or has_trailing_marker(session):+ return False+ prefix = COPILOT_RETRYABLE_ERROR_PREFIX if retryable else COPILOT_ERROR_PREFIX+ session.messages.append(+ ChatMessage(+ role="assistant",+ content=f"{prefix} {display_message}",+ metadata={PROVIDER_FAILURE_KEY: failure} if failure else None,+ )+ )+ return True+++def provider_failure_of(message: ChatMessage) -> dict[str, Any] | None:+ """The typed failure recorded on a marker row, if it carries one.++ ``None`` for every marker written before this existed, and for a failure+ the classifier declined to name. Callers fall back to the prefix, which+ is what they had before.+ """+ if not is_error_marker(message) or not message.metadata:+ return None+ failure = message.metadata.get(PROVIDER_FAILURE_KEY)+ return failure if isinstance(failure, dict) else Nonediff --git a/autogpt_platform/backend/backend/copilot/markers_test.py b/autogpt_platform/backend/backend/copilot/markers_test.pynew file mode 100644index 0000000000..068225c2bb--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/markers_test.py@@ -0,0 +1,257 @@+from datetime import UTC, datetime+from unittest.mock import AsyncMock, MagicMock++import pytest+import pytest_mock++from backend.copilot.constants import (+ COPILOT_ERROR_PREFIX,+ COPILOT_RETRYABLE_ERROR_PREFIX,+ STREAM_ERROR_MARKER,+)+from backend.copilot.markers import (+ append_error_marker,+ has_trailing_marker,+ is_error_marker,+ provider_failure_of,+)+from backend.copilot.model import ChatMessage, ChatSession+++def _session(messages: list[ChatMessage] | None = None) -> ChatSession:+ now = datetime(2026, 8, 20, tzinfo=UTC)+ return ChatSession(+ session_id="s1",+ user_id="u1",+ usage=[],+ started_at=now,+ updated_at=now,+ messages=messages or [],+ )+++class TestAFailureSurvivesTheStream:+ def test_a_failed_turn_leaves_something_behind(self) -> None:+ # Without this the chat shows the question and nothing after it.+ session = _session([ChatMessage(role="user", content="hi")])++ assert append_error_marker(session, "boom", retryable=True) is True+ assert len(session.messages) == 2+ assert session.messages[-1].role == "assistant"+ assert "boom" in (session.messages[-1].content or "")++ def test_the_provider_s_own_words_are_kept(self) -> None:+ session = _session()+ append_error_marker(+ session, "Can't reach the local LLM backend at :8099.", retryable=True+ )+ assert "8099" in (session.messages[-1].content or "")+++class TestRetryIsOnlyOfferedWhenItCanWork:+ def test_a_transient_failure_offers_try_again(self) -> None:+ session = _session()+ append_error_marker(session, "hiccup", retryable=True)+ assert (session.messages[-1].content or "").startswith(+ COPILOT_RETRYABLE_ERROR_PREFIX+ )++ def test_a_failure_retrying_cannot_fix_does_not(self) -> None:+ # An expired login or a spent quota costs the user a retry to learn+ # the retry was never going to work.+ session = _session()+ append_error_marker(session, "Your plan does not include this", retryable=False)+ content = session.messages[-1].content or ""+ assert content.startswith(COPILOT_ERROR_PREFIX)+ assert not content.startswith(COPILOT_RETRYABLE_ERROR_PREFIX)+++class TestOneFailureIsOneCard:+ @pytest.mark.parametrize(+ "existing",+ [+ f"{COPILOT_ERROR_PREFIX} already failed",+ f"{COPILOT_RETRYABLE_ERROR_PREFIX} already failed",+ STREAM_ERROR_MARKER,+ ],+ )+ def test_a_second_guard_does_not_stack_another_card(self, existing: str) -> None:+ # Several guards can fire on one failed turn.+ session = _session([ChatMessage(role="assistant", content=existing)])++ assert append_error_marker(session, "boom", retryable=True) is False+ assert len(session.messages) == 1++ def test_a_normal_reply_is_not_mistaken_for_a_marker(self) -> None:+ session = _session([ChatMessage(role="assistant", content="Here you go.")])++ assert has_trailing_marker(session) is False+ assert append_error_marker(session, "boom", retryable=True) is True++ def test_a_user_row_never_blocks_the_marker(self) -> None:+ session = _session([ChatMessage(role="user", content="hi")])+ assert has_trailing_marker(session) is False+++class TestRecognisingMarkers:+ def test_both_error_prefixes_count(self) -> None:+ for prefix in (COPILOT_ERROR_PREFIX, COPILOT_RETRYABLE_ERROR_PREFIX):+ assert is_error_marker(ChatMessage(role="assistant", content=f"{prefix} x"))++ def test_ordinary_content_does_not(self) -> None:+ assert not is_error_marker(ChatMessage(role="assistant", content="hello"))++ def test_a_user_row_carrying_the_text_does_not(self) -> None:+ # Only the assistant writes markers; a user pasting the string in+ # must not make their own message render as an error card.+ assert not is_error_marker(+ ChatMessage(role="user", content=f"{COPILOT_ERROR_PREFIX} spoof")+ )++ def test_an_empty_session_has_no_trailing_marker(self) -> None:+ assert has_trailing_marker(_session()) is False+ assert has_trailing_marker(None) is False+++class TestTheMarkerReachesTheDatabase:+ """Building the marker is not the same as saving it.++ The baseline yields the error and its consumer immediately breaks and+ closes the generator, so the tail of its ``finally`` -- including the+ session upsert -- never runs. A marker appended there is constructed+ correctly and then discarded, which is exactly what happened: the append+ reported success while the row never appeared in Postgres.++ The failure path therefore has to persist the marker itself, before it+ yields the error that ends the stream.++ These two are structural guards, not behavioural proof: they assert the+ ordering in the source rather than driving a real turn, because standing+ up that generator needs a provider, a session, tools and an execution+ context. The behavioural evidence is a live run against a deployment --+ a 404 leaves a non-retryable card and a dead endpoint leaves a retryable+ one -- and these keep the ordering from silently regressing afterwards.+ """++ def test_the_error_path_persists_before_it_yields(self) -> None:+ import inspect++ from backend.copilot.baseline import service++ source = inspect.getsource(service.stream_chat_completion_baseline)+ marker_at = source.find("append_error_marker(")+ assert marker_at != -1, "the failure path no longer records a marker"++ upsert_at = source.find("upsert_chat_session", marker_at)+ error_yield_at = source.find("yield StreamError(", marker_at)+ assert upsert_at != -1, "the marker is appended but never persisted"+ assert error_yield_at != -1++ # Persisting after the error is yielded is the bug this guards:+ # the consumer closes the generator on that yield.+ assert upsert_at < error_yield_at, (+ "the marker must be persisted before StreamError is yielded -- "+ "the consumer closes the generator on it, so anything after is "+ "not guaranteed to run"+ )++ def test_the_marker_is_not_left_to_the_finally_block(self) -> None:+ import inspect++ from backend.copilot.baseline import service++ source = inspect.getsource(service.stream_chat_completion_baseline)+ finally_at = source.rfind("\n finally:")+ marker_at = source.find("append_error_marker(")+ assert marker_at != -1+ assert finally_at != -1+ assert marker_at < finally_at, (+ "the marker moved into finally, where generator teardown cuts it "+ "short at the first await"+ )+++class TestTheMarkerCarriesTheFailure:+ """The prefix says retry-or-not. The envelope says what would fix it.++ A chat reopened tomorrow has only the row: without the failure on it,+ the most a card can offer is Try Again, which is the wrong advice for+ an expired login, a spent quota or a plan that excludes the connection.+ """++ def test_the_failure_rides_on_the_row(self) -> None:+ session = _session()+ append_error_marker(+ session,+ "You've hit this connection's limit",+ retryable=False,+ failure={"kind": "usage_limit", "authProvider": "codex", "resetsAt": None},+ )++ recorded = provider_failure_of(session.messages[-1])+ assert recorded is not None+ assert recorded["kind"] == "usage_limit"+ assert recorded["authProvider"] == "codex"++ def test_a_marker_without_one_reads_as_none(self) -> None:+ # Every marker written before this existed, and every failure the+ # classifier declined to name. Callers fall back to the prefix.+ session = _session()+ append_error_marker(session, "boom", retryable=True)+ assert provider_failure_of(session.messages[-1]) is None++ def test_an_ordinary_reply_is_never_read_as_a_failure(self) -> None:+ assert (+ provider_failure_of(ChatMessage(role="assistant", content="Here you go."))+ is None+ )++ def test_a_non_dict_payload_is_refused(self) -> None:+ # The bag is shared; a collision on the key must not crash a render.+ msg = ChatMessage(+ role="assistant",+ content=f"{COPILOT_ERROR_PREFIX} boom",+ metadata={"provider_failure": "not-a-dict"},+ )+ assert provider_failure_of(msg) is None+++class TestTheFailureReachesTheDatabase:+ """Setting metadata on the row is not the same as saving it.++ ``_save_session_to_db`` builds each row field by field and+ ``add_chat_messages_batch`` maps them one by one, so a field is only+ persisted if it is named in both. ``metadata`` was named in neither --+ the single-message path persisted it, the batch path silently dropped it.+ """++ @pytest.mark.asyncio+ async def test_a_marker_reaches_the_database_layer_with_its_failure(+ self, mocker: pytest_mock.MockerFixture+ ) -> None:+ from backend.copilot import model as model_module++ captured: dict[str, object] = {}++ async def _capture(session_id: str, messages: list, start_sequence: int) -> int:+ captured["messages"] = messages+ return start_sequence++ fake_db = MagicMock()+ fake_db.add_chat_messages_batch = AsyncMock(side_effect=_capture)+ fake_db.get_chat_session_metadata = AsyncMock(return_value=None)+ fake_db.create_chat_session = AsyncMock(return_value=None)+ fake_db.update_chat_session = AsyncMock(return_value=None)+ mocker.patch.object(model_module, "chat_db", return_value=fake_db)++ session = _session()+ append_error_marker(+ session, "boom", retryable=False, failure={"kind": "auth_expired"}+ )+ await model_module._save_session_to_db(+ session, existing_message_count=0, skip_existence_check=True+ )++ rows = captured.get("messages")+ assert rows, "no rows reached the database layer"+ assert rows[0]["metadata"] == {"provider_failure": {"kind": "auth_expired"}}diff --git a/autogpt_platform/backend/backend/copilot/model.py b/autogpt_platform/backend/backend/copilot/model.pyindex 2181a3edf2..73eae9392d 100644--- a/autogpt_platform/backend/backend/copilot/model.py+++ b/autogpt_platform/backend/backend/copilot/model.py@@ -198,6 +198,14 @@ class ChatMessage(BaseModel):model: str | None = Nonerouting_source: RoutingSource | None = Field(default=None, exclude=True)+ # Which connection served this turn. Unlike routing_source this is the+ # user's own choice, not an internal cohort, so it is safe to serialize+ # and is what lets a chat show what each turn actually ran on.+ # None on user/tool rows and on rows written before this existed --+ # ``segment_of`` resolves those against the session's own route.+ llm_auth_provider: CopilotLlmAuthProvider | None = None+ llm_credential_id: str | None = None+stamps_pending_save: bool = Field(default=False, exclude=True)"""True when model/routing_source were stamped after this row was alreadypersisted (mid-turn flush assigned its sequence before end-of-turn@@ -261,6 +269,10 @@ class ChatMessage(BaseModel):# DB column is a plain string; the values are always ones we wrote# (this PR owns the column) and Pydantic re-validates on construct.routing_source=cast("RoutingSource | None", prisma_message.routingSource),+ llm_auth_provider=cast(+ "CopilotLlmAuthProvider | None", prisma_message.llmAuthProvider+ ),+ llm_credential_id=prisma_message.llmCredentialId,)@@ -1102,6 +1114,9 @@ async def _save_session_to_db("function_call": msg.function_call,"model": msg.model,"routing_source": msg.routing_source,+ "llm_auth_provider": msg.llm_auth_provider,+ "llm_credential_id": msg.llm_credential_id,+ "metadata": msg.metadata,})logger.info(@@ -1152,6 +1167,8 @@ async def _save_session_to_db(sequence=msg.sequence,model=msg.model,routing_source=msg.routing_source,+ llm_auth_provider=msg.llm_auth_provider,+ llm_credential_id=msg.llm_credential_id,)except Exception as e:logger.error(diff --git a/autogpt_platform/backend/backend/copilot/model_test.py b/autogpt_platform/backend/backend/copilot/model_test.pyindex e7e3a37260..b3a2974fa1 100644--- a/autogpt_platform/backend/backend/copilot/model_test.py+++ b/autogpt_platform/backend/backend/copilot/model_test.py@@ -1914,6 +1914,8 @@ async def test_save_session_to_db_backfills_stamps_on_flushed_rows(sequence=7,model="claude-sonnet-4-6",routing_source="env",+ llm_auth_provider=None,+ llm_credential_id=None,)assert flushed.stamps_pending_save is Falsediff --git a/autogpt_platform/backend/backend/copilot/offers.py b/autogpt_platform/backend/backend/copilot/offers.pyindex dda5bb8638..a7978fc0ba 100644--- a/autogpt_platform/backend/backend/copilot/offers.py+++ b/autogpt_platform/backend/backend/copilot/offers.py@@ -16,14 +16,20 @@ from pydantic import BaseModelfrom backend.copilot.config import ChatConfig, CopilotLlmAuthProvider, CopilotLLMModelfrom backend.copilot.engine import resolve_use_sdk-from backend.copilot.model_router import resolve_model_route+from backend.copilot.model_router import (+ ROUTE_SURFACE_CODEX,+ ModelMode,+ resolve_model_route,+)from backend.copilot.transports import (ChatTransportResponse,get_chat_transports,is_deployment_chat_available,settings,)+from backend.data import llm_registryfrom backend.integrations.codex.access import CODEX_MINIMUM_PLAN_ERROR, has_codex_access+from backend.util.entitlements import Entitlement, has_entitlementfrom backend.util.feature_flag import Flag, is_feature_enabledfrom backend.util.settings import BehaveAs@@ -44,16 +50,22 @@ class ConnectionTier(BaseModel):same router the turn will use — LaunchDarkly cell, then registry, thenconfig — so it cannot drift from what actually answers.- It is ``None`` on a ChatGPT connection. Naming that model means asking- the account what it advertises, which takes a runtime lease against the- provider; doing that once per credential to render a list is the cost- this endpoint exists to avoid.+ On a ChatGPT connection it is the model the catalog pins for that cell,+ read straight from the registry -- no lease, no call to the account.+ What a lease would add is confirmation that this particular account still+ advertises it; the router checks that when the turn runs and falls back+ if not, so the name here is the routed model rather than a guarantee.+ ``None`` only when the catalog pins nothing for the cell."""tier: CopilotLLMModellabel: strselectable: booldisplay_model: str | None = None+ # Why this tier cannot be picked, when it cannot. Advanced is a paid+ # tier on hosted, so the row stays visible and says what unlocks it --+ # hiding it would remove the upgrade reason it exists to create.+ lock_reason: str | None = Noneclass AIConnectionOffer(BaseModel):@@ -85,16 +97,30 @@ async def get_connection_offers(user_id: str) -> list[AIConnectionOffer]:pick."""config = ChatConfig()- models = await _platform_tier_models(user_id, config)+ # One engine decision for the whole response: it is a property of the+ # deployment and the user, not of which connection they pick.+ use_sdk = await resolve_use_sdk(+ user_id,+ use_claude_code_subscription=config.use_claude_code_subscription,+ config_default=config.use_claude_agent_sdk,+ thinking_available=config.thinking_available,+ )+ mode = "thinking" if use_sdk else "fast"+ models = await _platform_tier_models(mode, user_id, config)+ codex_models = _codex_tier_models(mode)+ advanced_allowed = await _advanced_tier_allowed(user_id)offers = [- _offer(transport, models) for transport in await get_chat_transports(user_id)+ _offer(transport, models, codex_models, advanced_allowed)+ for transport in await get_chat_transports(user_id)]- locked = await _locked_codex_offer(user_id, offers)+ locked = await _locked_codex_offer(user_id, offers, codex_models)return offers + ([locked] if locked else [])async def _locked_codex_offer(- user_id: str, offers: list[AIConnectionOffer]+ user_id: str,+ offers: list[AIConnectionOffer],+ codex_models: dict[CopilotLLMModel, str | None],) -> AIConnectionOffer | None:"""The ChatGPT connection a plan does not include, shown rather than hidden.@@ -144,7 +170,19 @@ async def _locked_codex_offer(state="locked",selectable=False,is_default=False,- tiers=[],+ # Named even though nothing here can be picked: "what you get" is the+ # whole argument for connecting, and a surface that cannot say which+ # models is asking the user to take it on faith. Not selectable, so+ # naming them cannot be mistaken for offering them.+ tiers=[+ ConnectionTier(+ tier=tier,+ label=label,+ selectable=False,+ display_model=codex_models.get(tier),+ )+ for tier, label in TIER_LABELS.items()+ ],limitations=[],lock_reason=CODEX_MINIMUM_PLAN_ERROR,unlock_href="/settings/billing",@@ -152,7 +190,7 @@ async def _locked_codex_offer(async def _platform_tier_models(- user_id: str, config: ChatConfig+ mode: ModelMode, user_id: str, config: ChatConfig) -> dict[CopilotLLMModel, str | None]:"""Resolve each tier against the engine this user's turns will run on.@@ -160,18 +198,11 @@ async def _platform_tier_models(any more — the decision is the server's, so it can be made before a turnexists rather than during one."""- use_sdk = await resolve_use_sdk(- user_id,- use_claude_code_subscription=config.use_claude_code_subscription,- config_default=config.use_claude_agent_sdk,- thinking_available=config.thinking_available,- )- mode = "thinking" if use_sdk else "fast"resolved: dict[CopilotLLMModel, str | None] = {}for tier in TIER_LABELS:try:route = await resolve_model_route(mode, tier, user_id, config=config)- resolved[tier] = route.model+ resolved[tier] = _display_name(route.model)except Exception:# A tier that cannot be resolved is described without a name# rather than failing the whole list.@@ -193,9 +224,58 @@ def offer_id_for(transport: ChatTransportResponse) -> str:return f"{transport.auth_provider}:{transport.credential_id or 'deployment'}"+ADVANCED_TIER_LOCK_REASON = "A Max plan or higher is required for Advanced."+++async def _advanced_tier_allowed(user_id: str) -> bool:+ """Whether this user may pick the Advanced tier.++ A failure to resolve the entitlement leaves the tier open rather than+ locked: a billing hiccup should not silently downgrade someone's model.+ """+ try:+ return await has_entitlement(user_id, Entitlement.ADVANCED_MODEL_TIER)+ except Exception:+ logger.warning(+ "Could not resolve the Advanced tier entitlement; leaving it open",+ exc_info=True,+ )+ return True+++def _display_name(slug: str | None) -> str | None:+ """What the catalog calls this model, rather than its slug.++ The PRD labels tiers "Balanced · 5.6 Terra", not+ "Balanced · gpt-5.6-terra": the slug is a routing key and reads as one.+ Falls back to the slug when the registry has no entry, which is better+ than showing nothing.+ """+ if not slug:+ return None+ model = llm_registry.get_model(slug)+ return model.display_name if model else slug+++def _codex_tier_models(mode: str) -> dict[CopilotLLMModel, str | None]:+ """The models the catalog pins for the Codex cells of this engine.++ A registry read, so it costs nothing and needs no credential. The router+ validates the pinned slug against what the account actually advertises+ when the turn runs, and falls back if it is gone -- so this names the+ routed model, not a promise about the account.+ """+ return {+ tier: _display_name(llm_registry.get_route(ROUTE_SURFACE_CODEX, mode, tier))+ for tier in TIER_LABELS+ }++def _offer(transport: ChatTransportResponse,platform_models: dict[CopilotLLMModel, str | None],+ codex_models: dict[CopilotLLMModel, str | None],+ advanced_allowed: bool,) -> AIConnectionOffer:return AIConnectionOffer(offer_id=offer_id_for(transport),@@ -212,11 +292,18 @@ def _offer(ConnectionTier(tier=tier,label=label,- selectable=transport.available,+ selectable=(+ transport.available and (advanced_allowed or tier != "advanced")+ ),display_model=(platform_models.get(tier)if transport.auth_provider == "platform"- else None+ else codex_models.get(tier)+ ),+ lock_reason=(+ None+ if advanced_allowed or tier != "advanced"+ else ADVANCED_TIER_LOCK_REASON),)for tier, label in TIER_LABELS.items()diff --git a/autogpt_platform/backend/backend/copilot/offers_test.py b/autogpt_platform/backend/backend/copilot/offers_test.pyindex f5be6f5384..7d39572a50 100644--- a/autogpt_platform/backend/backend/copilot/offers_test.py+++ b/autogpt_platform/backend/backend/copilot/offers_test.py@@ -30,6 +30,14 @@ def _transport()[email protected](autouse=True)+def advanced_allowed(mocker: pytest_mock.MockerFixture):+ """Advanced is open unless a test says otherwise."""+ return mocker.patch.object(+ offers, "has_entitlement", new=AsyncMock(return_value=True)+ )++@pytest.fixture(autouse=True)def hosted(mocker: pytest_mock.MockerFixture):mocker.patch.object(offers.settings.config, "behave_as", BehaveAs.CLOUD)@@ -114,6 +122,51 @@ async def test_offer_ids_are_stable_and_per_account(assert offer_id_for(first) == offer_id_for(first)+async def test_a_chatgpt_tier_names_the_model_the_catalog_pins(+ mocker: pytest_mock.MockerFixture,+) -> None:+ # A registry read, not a call to the account: the router validates the+ # pinned slug against what the account advertises when the turn runs.+ mocker.patch.object(+ offers.llm_registry,+ "get_route",+ side_effect=lambda surface, mode, tier: f"{surface}:{mode}:{tier}",+ )+ _mock_transports(+ mocker, [_transport("platform", None), _transport("codex", "cred-1")]+ )++ codex = [+ o+ for o in await get_connection_offers("user")+ if o.auth_method == "chatgpt_oauth"+ ][0]++ assert [t.display_model for t in codex.tiers] == [+ "copilot_codex:fast:standard",+ "copilot_codex:fast:advanced",+ ]+++async def test_a_chatgpt_tier_the_catalog_pins_nothing_for_stays_unnamed(+ mocker: pytest_mock.MockerFixture,+) -> None:+ mocker.patch.object(offers.llm_registry, "get_route", return_value=None)+ _mock_transports(+ mocker, [_transport("platform", None), _transport("codex", "cred-1")]+ )++ codex = [+ o+ for o in await get_connection_offers("user")+ if o.auth_method == "chatgpt_oauth"+ ][0]++ assert all(t.display_model is None for t in codex.tiers)++@pytest.mark.asyncioasync def test_tiers_name_the_model_they_resolve_to(mocker: pytest_mock.MockerFixture,@@ -329,3 +382,71 @@ async def test_self_host_sells_nothing_because_it_grants_everything(_upsell(mocker)assert _locked(await get_connection_offers("user")) == []+++async def test_advanced_is_locked_for_a_plan_that_does_not_include_it(+ mocker: pytest_mock.MockerFixture, advanced_allowed+) -> None:+ # Visible and locked, not hidden: the row is the upgrade reason.+ advanced_allowed.return_value = False+ _mock_transports(mocker, [_transport("platform", None)])++ tiers = {t.tier: t for t in (await get_connection_offers("user"))[0].tiers}++ assert tiers["standard"].selectable is True+ assert tiers["advanced"].selectable is False+ assert tiers["advanced"].lock_reason+ # Still named, so the user can see what they would be getting.+ assert "advanced" in tiers+++async def test_advanced_is_open_when_the_plan_includes_it(+ mocker: pytest_mock.MockerFixture,+) -> None:+ _mock_transports(mocker, [_transport("platform", None)])++ tiers = {t.tier: t for t in (await get_connection_offers("user"))[0].tiers}++ assert all(t.selectable for t in tiers.values())+ assert all(t.lock_reason is None for t in tiers.values())+++async def test_an_unresolvable_entitlement_leaves_advanced_open(+ mocker: pytest_mock.MockerFixture, advanced_allowed+) -> None:+ # A billing hiccup must not silently downgrade someone's model.+ advanced_allowed.side_effect = RuntimeError("billing down")+ _mock_transports(mocker, [_transport("platform", None)])++ tiers = {t.tier: t for t in (await get_connection_offers("user"))[0].tiers}++ assert tiers["advanced"].selectable is True+ assert tiers["advanced"].lock_reason is None+++async def test_a_locked_chatgpt_offer_still_names_the_models(+ mocker: pytest_mock.MockerFixture,+) -> None:+ # "What you get" is the argument for connecting; a locked row that+ # cannot name the models asks the user to take it on faith.+ mocker.patch.object(+ offers.llm_registry, "get_route", side_effect=lambda s, m, t: f"{m}-{t}"+ )+ mocker.patch.object(offers.llm_registry, "get_model", return_value=None)+ _mock_transports(mocker, [_transport("platform", None)])+ _upsell(mocker)++ locked = _locked(await get_connection_offers("user"))[0]++ # The fixture pins the engine to the baseline, so the cells are "fast".+ assert [t.display_model for t in locked.tiers] == [+ "fast-standard",+ "fast-advanced",+ ]+ # Naming them must not read as offering them.+ assert all(t.selectable is False for t in locked.tiers)+ assert locked.selectable is Falsediff --git a/autogpt_platform/backend/backend/copilot/provider_failure.py b/autogpt_platform/backend/backend/copilot/provider_failure.pynew file mode 100644index 0000000000..0fed31b737--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/provider_failure.py@@ -0,0 +1,298 @@+"""What went wrong with the provider behind a turn, said precisely.++Every provider failure currently reaches the client as one opaque+``baseline_error`` carrying a stringified exception. An expired ChatGPT+login, a monthly usage limit, a retired model and a policy refusal are all+the same event to the UI, so it can only offer the same useless advice --+"Press Try Again" -- to three failures where trying again cannot work.++This module names the difference once, at the boundary where the provider+exception is still intact, and hands the caller something a UI can act on:+whether retrying is pointless, whether reconnecting would help, and when+the limit actually lifts.++``resets_at`` is deliberately ``None`` unless the provider reported a real+timestamp. An invented "try again in an hour" that turns out to be wrong is+worse than saying nothing, because the user plans around it.+"""++import logging+import re+import time+from enum import Enum+from typing import Any++from openai import (+ APIConnectionError,+ APIStatusError,+ APITimeoutError,+ AuthenticationError,+ InternalServerError,+ NotFoundError,+ PermissionDeniedError,+ RateLimitError,+)+from pydantic import BaseModel, Field++from backend.copilot.rate_limit import UserPaywalledError+from backend.integrations.codex.transport import (+ CodexCredentialBusyError,+ CodexCredentialIntegrityError,+ CodexInvocationTimeoutError,+ CodexTransportOverloadedError,+)++logger = logging.getLogger(__name__)+++class ProviderFailureKind(str, Enum):+ """Why a provider refused, in terms that imply what to do about it."""++ # The credential was understood and rejected -- reconnecting fixes it.+ AUTH_EXPIRED = "auth_expired"+ # The credential is malformed or unusable; reconnecting fixes it.+ INVALID_CREDENTIAL = "invalid_credential"+ # A quota is spent. Waiting fixes it; retrying now does not.+ USAGE_LIMIT = "usage_limit"+ # The model is gone or not available to this account.+ MODEL_UNAVAILABLE = "model_unavailable"+ # The provider refused on policy grounds. Retrying is pointless.+ POLICY_DENIED = "policy_denied"+ # The user's plan does not include this route.+ #+ # Shares its value with ``ExecutionFailureReason.ENTITLEMENT_REQUIRED``+ # on purpose: the same denial can terminate a graph run or a chat turn,+ # and two names for it would split every downstream count in half.+ ENTITLEMENT_REQUIRED = "entitlement_required"+ # Anything that might genuinely succeed on a second attempt.+ TRANSIENT = "transient"+++# Retrying the same turn is only honest for a failure that could resolve+# itself. Everything else needs the user to do something first.+_RETRYABLE = frozenset({ProviderFailureKind.TRANSIENT})++# The kinds a reconnect would actually clear.+_RECONNECTABLE = frozenset(+ {+ ProviderFailureKind.AUTH_EXPIRED,+ ProviderFailureKind.INVALID_CREDENTIAL,+ }+)+++class ProviderFailure(BaseModel):+ """One provider refusal, carried whole from the runtime to the client."""++ kind: ProviderFailureKind+ message: str = Field(description="What to tell the user, already humanized")+ auth_provider: str | None = Field(+ default=None, description="Which connection failed: platform or codex"+ )+ credential_id: str | None = None+ resets_at: int | None = Field(+ default=None,+ description=(+ "Unix seconds, only when the provider reported one. Never guessed."+ ),+ )++ @property+ def retryable(self) -> bool:+ return self.kind in _RETRYABLE++ @property+ def reconnect_fixes_it(self) -> bool:+ return self.kind in _RECONNECTABLE++ def as_part(self) -> dict[str, Any]:+ """The payload carried on the stream, including derived advice.++ ``retryable`` is computed here rather than left to the client so a+ second implementation of "is this worth retrying" cannot drift from+ this one.+ """+ return {+ "kind": self.kind.value,+ "message": self.message,+ "authProvider": self.auth_provider,+ "credentialId": self.credential_id,+ "resetsAt": self.resets_at,+ "retryable": self.retryable,+ "reconnectFixesIt": self.reconnect_fixes_it,+ }+++# Codex failures arrive carrying an internal code as their message+# ("codex_credential_busy"), which is a log line, not something to show a+# person. These say the same thing in the user's terms, and say what to do.+_CODEX_MESSAGES: dict[type[BaseException], str] = {+ # Deliberately does not say "only one chat at a time". Measured against a+ # live ChatGPT connection, concurrent chats do NOT contend: three+ # simultaneous turns all completed in ~8s, and a short turn fired six+ # seconds into a 42s one answered in 4s, with no busy error in either+ # case. The five-second timeout is on the *credential* lease, which is+ # contended when something needs to write the credential -- a token+ # refresh -- not by ordinary turns sharing it.+ #+ # So this is rarer than "you have two chats open", and wording it that+ # way would send the user to close a chat that is not the problem.+ CodexCredentialBusyError: (+ "This ChatGPT connection is briefly unavailable. " "Try again in a moment."+ ),+ CodexCredentialIntegrityError: (+ "This ChatGPT connection can't be used. Reconnect the account in "+ "Settings, then send this again."+ ),+ CodexTransportOverloadedError: (+ "ChatGPT is busy right now. Try again in a moment."+ ),+ CodexInvocationTimeoutError: ("ChatGPT took too long to respond. Try again."),+}+++def _humanize(exc: BaseException) -> str | None:+ """A user-facing sentence for a failure whose own message is a code."""+ return _CODEX_MESSAGES.get(type(exc))+++def classify(+ exc: BaseException,+ *,+ auth_provider: str | None = None,+ credential_id: str | None = None,+ message: str | None = None,+) -> ProviderFailure | None:+ """Name the failure, or return ``None`` if it is not the provider's.++ ``None`` means "not recognisably a provider refusal" and leaves the+ caller's existing behaviour untouched. That matters: a bug in our own+ code dressed up as a provider failure would send the user to reconnect+ an account that was never broken.+ """+ kind = _kind_of(exc)+ if kind is None:+ return None+ return ProviderFailure(+ kind=kind,+ message=message or _humanize(exc) or str(exc) or type(exc).__name__,+ auth_provider=auth_provider,+ credential_id=credential_id,+ resets_at=_resets_at(exc),+ )+++# HTTP status the compat gateway should answer with for each kind, so the+# CLI upstream of it can tell "stop asking" from "try again". A blanket 502+# reads as a server fault and invites a retry that cannot succeed.+_STATUS_BY_KIND: dict[ProviderFailureKind, int] = {+ ProviderFailureKind.AUTH_EXPIRED: 401,+ ProviderFailureKind.INVALID_CREDENTIAL: 401,+ ProviderFailureKind.ENTITLEMENT_REQUIRED: 402,+ ProviderFailureKind.POLICY_DENIED: 403,+ ProviderFailureKind.MODEL_UNAVAILABLE: 404,+ ProviderFailureKind.USAGE_LIMIT: 429,+ ProviderFailureKind.TRANSIENT: 503,+}+++def status_for(failure: "ProviderFailure") -> int:+ """The status a gateway should return for this failure."""+ return _STATUS_BY_KIND.get(failure.kind, 502)+++def _kind_of(exc: BaseException) -> ProviderFailureKind | None:+ if isinstance(exc, UserPaywalledError):+ return ProviderFailureKind.ENTITLEMENT_REQUIRED+ # Codex runs behind a CLI, so its failures arrive as transport+ # exceptions rather than HTTP errors. Only the ones whose meaning is+ # unambiguous are named; a bare CodexTransportError could be anything.+ if isinstance(exc, CodexCredentialIntegrityError):+ return ProviderFailureKind.INVALID_CREDENTIAL+ if isinstance(+ exc,+ (+ CodexInvocationTimeoutError,+ CodexTransportOverloadedError,+ CodexCredentialBusyError,+ ),+ ):+ return ProviderFailureKind.TRANSIENT+ if isinstance(exc, AuthenticationError):+ return ProviderFailureKind.AUTH_EXPIRED+ if isinstance(exc, PermissionDeniedError):+ return ProviderFailureKind.POLICY_DENIED+ if isinstance(exc, RateLimitError):+ return ProviderFailureKind.USAGE_LIMIT+ if isinstance(exc, NotFoundError):+ return ProviderFailureKind.MODEL_UNAVAILABLE+ if isinstance(exc, (APIConnectionError, APITimeoutError, InternalServerError)):+ return ProviderFailureKind.TRANSIENT+ if isinstance(exc, APIStatusError):+ # Any other 5xx is worth another attempt; a 4xx we have not named+ # above is a request we built wrong, which is ours, not the+ # provider's, and must not be dressed up as a provider refusal.+ return ProviderFailureKind.TRANSIENT if exc.status_code >= 500 else None+ return None+++def _resets_at(exc: BaseException) -> int | None:+ """A reset time only if the provider actually sent one.++ Read from the standard ``x-ratelimit-reset-*`` response headers. Anything+ unparseable is dropped rather than approximated -- a wrong time is worse+ than no time, because the user schedules around it.+ """+ response = getattr(exc, "response", None)+ headers = getattr(response, "headers", None)+ if headers is None:+ return None+ raw = headers.get("x-ratelimit-reset-requests") or headers.get(+ "x-ratelimit-reset-tokens"+ )+ if not raw:+ return None+ try:+ return _to_unix_seconds(str(raw))+ except (ValueError, TypeError):+ logger.debug("Unparseable rate-limit reset header: %r", raw)+ return None+++def _to_unix_seconds(raw: str) -> int | None:+ """Absolute epoch seconds, or ``None`` for a shape we do not understand.++ OpenAI sends a duration like ``60s`` / ``1m30s`` / ``6m0s`` / ``2h``;+ some compatible servers send an absolute epoch instead. A duration is+ still the provider reporting a reset -- "60 seconds from now" is a fact,+ not a guess -- so it is anchored to the current clock. What is refused is+ inventing a reset the provider never mentioned at all.+ """+ value = raw.strip().lower()+ if not value:+ return None+ if value.isdigit():+ seconds = int(value)+ # Below this, it is a duration in seconds rather than an epoch:+ # 10**9 seconds is 2001, so no real reset epoch is ever smaller.+ return seconds if seconds > 10**9 else int(time.time()) + seconds+ total = _duration_seconds(value)+ return int(time.time() + total) if total is not None else None+++_DURATION_UNITS = {"ms": 0.001, "s": 1.0, "m": 60.0, "h": 3600.0, "d": 86400.0}+_DURATION_PART = re.compile(r"(\d+(?:\.\d+)?)(ms|s|m|h|d)")+++def _duration_seconds(value: str) -> float | None:+ """Seconds in a compound duration, or ``None`` if it is not one.++ Whole-string match, so a value carrying anything we did not parse is+ rejected rather than silently half-read.+ """+ parts = _DURATION_PART.findall(value)+ if not parts:+ return None+ if "".join(number + unit for number, unit in parts) != value:+ return None+ return sum(float(number) * _DURATION_UNITS[unit] for number, unit in parts)diff --git a/autogpt_platform/backend/backend/copilot/provider_failure_test.py b/autogpt_platform/backend/backend/copilot/provider_failure_test.pynew file mode 100644index 0000000000..a8717dd3ff--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/provider_failure_test.py@@ -0,0 +1,185 @@+import time++import httpx+import pytest+from openai import (+ APIConnectionError,+ AuthenticationError,+ BadRequestError,+ InternalServerError,+ NotFoundError,+ PermissionDeniedError,+ RateLimitError,+)++from backend.copilot.provider_failure import ProviderFailureKind, classify+from backend.copilot.rate_limit import UserPaywalledError+from backend.util.exceptions import ExecutionFailureReason+++def _api_error(cls, status: int, headers: dict[str, str] | None = None):+ request = httpx.Request("POST", "https://api.example.com/v1/chat/completions")+ response = httpx.Response(status, headers=headers or {}, request=request)+ return cls("boom", response=response, body=None)+++class TestWhatTheFailureIs:+ @pytest.mark.parametrize(+ "exc, expected",+ [+ (_api_error(AuthenticationError, 401), ProviderFailureKind.AUTH_EXPIRED),+ (_api_error(PermissionDeniedError, 403), ProviderFailureKind.POLICY_DENIED),+ (_api_error(RateLimitError, 429), ProviderFailureKind.USAGE_LIMIT),+ (_api_error(NotFoundError, 404), ProviderFailureKind.MODEL_UNAVAILABLE),+ (_api_error(InternalServerError, 500), ProviderFailureKind.TRANSIENT),+ (_api_error(BadRequestError, 502), ProviderFailureKind.TRANSIENT),+ ],+ )+ def test_each_provider_refusal_gets_its_own_name(self, exc, expected) -> None:+ failure = classify(exc)+ assert failure is not None+ assert failure.kind is expected++ def test_a_connection_failure_is_worth_retrying(self) -> None:+ exc = APIConnectionError(request=httpx.Request("POST", "https://x"))+ failure = classify(exc)+ assert failure is not None+ assert failure.kind is ProviderFailureKind.TRANSIENT+ assert failure.retryable is True++ def test_a_paywall_is_the_same_denial_a_graph_run_reports(self) -> None:+ # Two names for one denial would split every downstream count in half.+ failure = classify(UserPaywalledError("Max plan required"))+ assert failure is not None+ assert failure.kind.value == ExecutionFailureReason.ENTITLEMENT_REQUIRED.value+++class TestWhatIsNotTheProvidersFault:+ def test_our_own_bad_request_is_not_dressed_up_as_a_provider_refusal(self) -> None:+ # A 4xx we did not name is a request we built wrong. Calling it a+ # provider failure would send the user to reconnect a working account.+ assert classify(_api_error(BadRequestError, 400)) is None++ def test_an_ordinary_bug_is_left_alone(self) -> None:+ assert classify(ValueError("our bug")) is None+++class TestWhatTheUserShouldDo:+ @pytest.mark.parametrize(+ "exc, retryable, reconnect",+ [+ (_api_error(AuthenticationError, 401), False, True),+ (_api_error(RateLimitError, 429), False, False),+ (_api_error(NotFoundError, 404), False, False),+ (_api_error(InternalServerError, 500), True, False),+ ],+ )+ def test_the_advice_matches_the_failure(self, exc, retryable, reconnect) -> None:+ failure = classify(exc)+ assert failure is not None+ assert failure.retryable is retryable+ assert failure.reconnect_fixes_it is reconnect++ def test_the_advice_travels_with_the_envelope(self) -> None:+ # Computed server-side so a second "is this retryable" cannot drift.+ part = classify(_api_error(AuthenticationError, 401)).as_part()+ assert part["retryable"] is False+ assert part["reconnectFixesIt"] is True+ assert part["kind"] == "auth_expired"+++class TestWhenTheLimitLifts:+ def test_a_reported_duration_becomes_a_real_timestamp(self) -> None:+ exc = _api_error(RateLimitError, 429, {"x-ratelimit-reset-requests": "6m0s"})+ failure = classify(exc)+ assert failure is not None+ assert failure.resets_at is not None+ assert 300 < failure.resets_at - int(time.time()) <= 360++ def test_a_reported_epoch_is_passed_through(self) -> None:+ exc = _api_error(+ RateLimitError, 429, {"x-ratelimit-reset-requests": "1900000000"}+ )+ assert classify(exc).resets_at == 1900000000++ def test_token_resets_are_read_when_request_resets_are_absent(self) -> None:+ exc = _api_error(RateLimitError, 429, {"x-ratelimit-reset-tokens": "30s"})+ assert classify(exc).resets_at is not None++ @pytest.mark.parametrize("raw", ["", "soon", "12x", "tomorrow"])+ def test_an_unreadable_reset_is_dropped_rather_than_approximated(+ self, raw: str+ ) -> None:+ # A wrong time is worse than no time: the user schedules around it.+ exc = _api_error(RateLimitError, 429, {"x-ratelimit-reset-requests": raw})+ assert classify(exc).resets_at is None++ def test_no_reset_is_invented_when_the_provider_reported_none(self) -> None:+ assert classify(_api_error(RateLimitError, 429)).resets_at is None+++class TestWhichConnectionFailed:+ def test_the_envelope_names_the_connection_that_refused(self) -> None:+ failure = classify(+ _api_error(AuthenticationError, 401),+ auth_provider="codex",+ credential_id="cred-1",+ )+ assert failure is not None+ assert failure.as_part()["authProvider"] == "codex"+ assert failure.as_part()["credentialId"] == "cred-1"++ def test_a_humanized_message_survives_classification(self) -> None:+ # The baseline path already turns a bare connection error into+ # operator-useful advice; classifying must not throw that away.+ failure = classify(+ APIConnectionError(request=httpx.Request("POST", "https://x")),+ message="Can't reach the local LLM backend at http://x/v1.",+ )+ assert failure is not None+ assert "local LLM backend" in failure.message+++class TestCodexFailuresSpeakToThePerson:+ """Codex failures carry an internal code as their message.++ ``codex_credential_busy`` is a log line. Shown to a user it explains+ nothing and suggests nothing.+ """++ def test_a_busy_connection_says_so_and_says_waiting_works(self) -> None:+ from backend.integrations.codex.transport import CodexCredentialBusyError++ failure = classify(+ CodexCredentialBusyError("codex_credential_busy"), auth_provider="codex"+ )+ assert failure is not None+ assert "codex_credential_busy" not in failure.message+ assert "briefly unavailable" in failure.message+ # Waiting is what clears it, so the retry affordance is honest.+ assert failure.retryable is True++ def test_the_message_does_not_blame_concurrent_chats(self) -> None:+ # Measured: concurrent chats on one credential do not contend. The+ # lease is contended by credential writes (token refresh), so telling+ # the user to close another chat would point at the wrong thing.+ from backend.integrations.codex.transport import CodexCredentialBusyError++ message = classify(CodexCredentialBusyError("x"), auth_provider="codex").message+ assert "only one" not in message.lower()++ def test_an_unusable_credential_points_at_reconnecting(self) -> None:+ from backend.integrations.codex.transport import CodexCredentialIntegrityError++ failure = classify(CodexCredentialIntegrityError("bad"), auth_provider="codex")+ assert failure is not None+ assert "Reconnect" in failure.message+ assert failure.reconnect_fixes_it is True++ def test_an_explicit_message_still_wins(self) -> None:+ from backend.integrations.codex.transport import CodexCredentialBusyError++ failure = classify(+ CodexCredentialBusyError("x"), auth_provider="codex", message="caller says"+ )+ assert failure.message == "caller says"diff --git a/autogpt_platform/backend/backend/copilot/response_model.py b/autogpt_platform/backend/backend/copilot/response_model.pyindex a5256a7f46..f1500b971b 100644--- a/autogpt_platform/backend/backend/copilot/response_model.py+++ b/autogpt_platform/backend/backend/copilot/response_model.py@@ -66,6 +66,7 @@ class ResponseType(str, Enum):# deltas. Transient (never persisted); the durable record is the# ``context_compaction`` tool row's JSON output.COMPACTION = "data-compaction"+ PROVIDER_FAILURE = "data-provider-failure"class StreamBaseResponse(BaseModel):@@ -363,6 +364,28 @@ class StreamModeChanged(StreamBaseResponse):return f"data: {json.dumps(data)}\n\n"+class StreamProviderFailure(StreamBaseResponse):+ """The typed reason a provider refused this turn.++ Rides alongside ``StreamError`` rather than replacing it. The AI SDK+ pins error frames to ``z.strictObject({type, errorText})``, which is why+ ``StreamError.details`` never reaches the client and ``code`` has to+ travel disguised as a ``[code:x]`` text prefix. A data part has no such+ ceiling, so the envelope arrives whole -- and every existing consumer of+ the error frame keeps working untouched.+ """++ type: ResponseType = ResponseType.PROVIDER_FAILURE+ failure: dict[str, Any] = Field(+ ..., description="ProviderFailure.as_part() payload"+ )++ def to_sse(self) -> str:+ """Emit as an AI SDK v5 data part."""+ data = {"type": self.type.value, "data": self.failure}+ return f"data: {json_dumps(data)}\n\n"++class StreamStatus(StreamBaseResponse):"""Transient status notification shown to the user during long operations.diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.pyindex b70e91dc01..83144bc87c 100644--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_gateway.py@@ -16,6 +16,9 @@ from openai_codex.generated.v2_all import AgentMessageDeltaNotificationfrom openai_codex.models import Notificationfrom pydantic import BaseModel, ConfigDict, Field+from backend.copilot.provider_failure import ProviderFailure+from backend.copilot.provider_failure import classify as classify_provider_failure+from backend.copilot.provider_failure import status_forfrom backend.integrations.codex.models import (CodexDynamicToolCall,CodexDynamicToolResult,@@ -141,6 +144,11 @@ class CodexAnthropicGateway:"Codex gateway requires a credential lease or agent session")self._credential_lease = credential_lease+ # The last failure this gateway named, kept so the service layer can+ # report *what* went wrong. By the time a failure reaches the CLI it+ # is an HTTP status and a sentence; the typed exception only exists+ # in here.+ self.last_failure: ProviderFailure | None = Noneself.model = modelself.effort: CodexReasoningEffort | None = effortself._transport = cast(@@ -478,6 +486,35 @@ class CodexAnthropicGateway:)await conversation.queue.put(_Failed(error=exc))+ def _failure_response(self, failed: "_Failed") -> web.Response:+ """Answer a failed conversation with a status the CLI can act on.++ Every failure used to become ``502 api_error``, which reads as a+ server fault and invites the CLI to retry something that cannot+ succeed -- an expired credential retried three times is still+ expired. Naming the failure lets the status say "stop asking".++ Unrecognised failures keep the old 502: a wrong specific status+ would be worse than an honest generic one.+ """+ failure = classify_provider_failure(+ failed.error,+ auth_provider="codex",+ credential_id=(+ self._credential_lease.credentials.id+ if self._credential_lease is not None+ else None+ ),+ )+ self.last_failure = failure+ if failure is None:+ return _anthropic_error(502, "api_error", "Codex model transport failed")+ return _anthropic_error(+ status_for(failure),+ failure.kind.value,+ failure.message,+ )+async def _streaming_response(self,request: web.Request,@@ -499,11 +536,7 @@ class CodexAnthropicGateway:) -> web.StreamResponse:first = await conversation.queue.get()if isinstance(first, _Failed):- return _anthropic_error(- 502,- "api_error",- "Codex model transport failed",- )+ return self._failure_response(first)response = web.StreamResponse(status=200,@@ -618,6 +651,28 @@ class CodexAnthropicGateway:)await _write_message_end(response, "end_turn", output_tokens)return+ elif isinstance(event, _Failed):+ # The HTTP status can no longer change once streaming starts,+ # but classification is still required: the service reads+ # ``last_failure`` after the gateway closes to persist the+ # provider-specific reason on the turn.+ self._failure_response(event)+ failure = self.last_failure+ await _write_sse(+ response,+ {+ "type": "error",+ "error": {+ "type": failure.kind.value if failure else "api_error",+ "message": (+ failure.message+ if failure+ else "Codex model transport failed"+ ),+ },+ },+ )+ returnelse:await _write_sse(response,diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_gateway_failure_test.py b/autogpt_platform/backend/backend/copilot/sdk/codex_gateway_failure_test.pynew file mode 100644index 0000000000..52cd21e45b--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_gateway_failure_test.py@@ -0,0 +1,81 @@+from unittest.mock import AsyncMock, MagicMock++import pytest++from backend.copilot.provider_failure import ProviderFailureKind+from backend.copilot.sdk.codex_compat_gateway import (+ CodexAnthropicGateway,+ _Conversation,+ _Failed,+ _TextDelta,+)+from backend.integrations.codex.transport import (+ CodexCredentialIntegrityError,+ CodexTransportError,+ CodexTransportOverloadedError,+)+++def _gateway() -> CodexAnthropicGateway:+ return CodexAnthropicGateway(+ agent_session=object(), # type: ignore[arg-type]+ model="gpt-5.6-luna",+ transport=object(), # type: ignore[arg-type]+ )+++class TestTheGatewayNamesItsFailures:+ """A blanket 502 tells the CLI to try again. Most failures cannot."""++ @pytest.mark.parametrize(+ "exc, status, kind",+ [+ (+ CodexCredentialIntegrityError("bad"),+ 401,+ ProviderFailureKind.INVALID_CREDENTIAL,+ ),+ (CodexTransportOverloadedError("busy"), 503, ProviderFailureKind.TRANSIENT),+ ],+ )+ def test_a_named_failure_gets_a_status_that_means_something(+ self, exc, status, kind+ ) -> None:+ gw = _gateway()+ response = gw._failure_response(_Failed(error=exc))+ assert response.status == status+ assert gw.last_failure is not None+ assert gw.last_failure.kind is kind++ def test_an_unrecognised_failure_keeps_the_honest_generic(self) -> None:+ # A wrong specific status would be worse than a generic one.+ gw = _gateway()+ response = gw._failure_response(_Failed(error=CodexTransportError("?")))+ assert response.status == 502+ assert gw.last_failure is None++ def test_the_failure_is_left_where_the_service_layer_can_read_it(self) -> None:+ # By the time this reaches the CLI it is a status and a sentence;+ # the typed exception only exists inside the gateway.+ gw = _gateway()+ assert gw.last_failure is None+ gw._failure_response(_Failed(error=CodexCredentialIntegrityError("bad")))+ assert gw.last_failure is not None+ assert gw.last_failure.auth_provider == "codex"++ @pytest.mark.asyncio+ async def test_a_midstream_failure_is_classified_for_the_service(self) -> None:+ gw = _gateway()+ conversation = _Conversation(id="conversation-1")+ await conversation.queue.put(+ _Failed(error=CodexCredentialIntegrityError("expired credential"))+ )+ response = MagicMock()+ response.write = AsyncMock()++ await gw._write_boundary(response, conversation, _TextDelta(text="partial"))++ assert gw.last_failure is not None+ assert gw.last_failure.kind is ProviderFailureKind.INVALID_CREDENTIAL+ wire = b"".join(call.args[0] for call in response.write.await_args_list)+ assert b'"type":"invalid_credential"' in wirediff --git a/autogpt_platform/backend/backend/copilot/sdk/provider_failure_parity_test.py b/autogpt_platform/backend/backend/copilot/sdk/provider_failure_parity_test.pynew file mode 100644index 0000000000..92121e6b7b--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/provider_failure_parity_test.py@@ -0,0 +1,96 @@+"""The SDK path reports provider failures the way the baseline does.++Most turns run on the SDK engine, and the codex route always does. Until+this, a provider failure there reached the chat as CLI text: the marker+carried no envelope and the retry affordance came from a list of+engine-level codes that knows nothing about expired logins or spent quotas.+"""++from datetime import UTC, datetime++from backend.copilot.markers import provider_failure_of+from backend.copilot.model import ChatSession+from backend.copilot.provider_failure import ProviderFailureKind, classify+from backend.copilot.sdk.service import (+ _append_error_marker,+ _InterruptedAttempt,+ _provider_failure_for,+)+from backend.integrations.codex.transport import CodexCredentialIntegrityError+++class _Gateway:+ def __init__(self, failure=None):+ self.last_failure = failure+++class _Ctx:+ def __init__(self, gateway=None):+ self.codex_gateway = gateway+++def _session() -> ChatSession:+ now = datetime(2026, 8, 21, tzinfo=UTC)+ return ChatSession(+ session_id="s1",+ user_id="u1",+ usage=[],+ started_at=now,+ updated_at=now,+ messages=[],+ )+++class TestReadingTheGateway:+ def test_a_named_codex_failure_is_found(self) -> None:+ failure = classify(CodexCredentialIntegrityError("bad"), auth_provider="codex")+ assert _provider_failure_for(_Ctx(_Gateway(failure))) is failure++ def test_the_platform_route_has_no_gateway_to_ask(self) -> None:+ assert _provider_failure_for(_Ctx(None)) is None++ def test_a_codex_turn_the_gateway_could_not_name_reports_nothing(self) -> None:+ # Caller keeps its existing behaviour rather than inventing a kind.+ assert _provider_failure_for(_Ctx(_Gateway(None))) is None+++class TestTheMarkerMatchesTheBaseline:+ def test_the_sdk_marker_carries_the_envelope(self) -> None:+ # Same row shape both engines produce, so one reader serves both.+ session = _session()+ failure = classify(CodexCredentialIntegrityError("bad"), auth_provider="codex")+ _append_error_marker(+ session,+ failure.message,+ retryable=failure.retryable,+ failure=failure.as_part(),+ )++ recorded = provider_failure_of(session.messages[-1])+ assert recorded is not None+ assert recorded["kind"] == ProviderFailureKind.INVALID_CREDENTIAL.value+ assert recorded["reconnectFixesIt"] is True++ def test_an_unclassified_sdk_error_still_writes_a_marker(self) -> None:+ session = _session()+ _append_error_marker(session, "the assistant stopped", retryable=True)++ assert session.messages, "an unclassified failure must still be recorded"+ assert provider_failure_of(session.messages[-1]) is None++ def test_retry_finalizer_keeps_the_provider_envelope(self) -> None:+ session = _session()+ failure = classify(CodexCredentialIntegrityError("bad"), auth_provider="codex")++ _InterruptedAttempt().finalize(+ session,+ state=None,+ display_msg=failure.message,+ retryable=failure.retryable,+ failure=failure.as_part(),+ )++ recorded = provider_failure_of(session.messages[-1])+ assert recorded is not None+ assert recorded["kind"] == ProviderFailureKind.INVALID_CREDENTIAL.value+ assert recorded["reconnectFixesIt"] is Truediff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex b183a026de..7dc540a3aa 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -48,6 +48,9 @@ from backend.copilot.model_router import (resolve_model_route,)from backend.copilot.graphiti.context import fetch_warm_context+from backend.copilot.markers import append_error_marker+from backend.copilot.provider_failure import ProviderFailure+from backend.copilot.segments import Segment, stamp_segmentfrom backend.copilot.graphiti.ingest import enqueue_conversation_turnfrom backend.copilot.sdk.codex_compat_gateway import CodexAnthropicGatewayfrom backend.data.db_accessors import chat_db@@ -124,6 +127,7 @@ from ..response_model import (StreamCompactionProgress,StreamError,StreamFinish,+ StreamProviderFailure,StreamFinishStep,StreamHeartbeat,StreamReasoningDelta,@@ -796,6 +800,13 @@ async def _consume_sdk_until_done(),):continue+ # The envelope goes out just ahead of the error it explains,+ # so a client acting on it has it in hand before the turn is+ # reported failed. Same contract as the baseline path.+ if isinstance(dispatched, StreamError):+ codex_failure = _provider_failure_for(ctx)+ if codex_failure is not None:+ yield StreamProviderFailure(failure=codex_failure.as_part())yield dispatched# Mid-turn follow-up persistence: the MCP tool wrapper drains@@ -1354,6 +1365,11 @@ class _StreamContext:attachments: "PreparedAttachments"compaction: CompactionTrackerlock: AsyncClusterLock+ # The Codex gateway for this turn, when the route is a ChatGPT+ # subscription. Carried here so the error path can ask it what actually+ # failed: by the time a provider failure reaches this layer it is CLI+ # text, and the gateway holds the last point at which it was typed.+ codex_gateway: "CodexAnthropicGateway | None" = None# Per-retry token budgets for the no-transcript (use_resume=False) path.@@ -1499,14 +1515,30 @@ def _append_error_marker(display_msg: str,*,retryable: bool = False,+ failure: dict[str, Any] | None = None,) -> None:- """Append a copilot error marker to *session* so it persists across refresh."""- if session is None:- return- prefix = COPILOT_RETRYABLE_ERROR_PREFIX if retryable else COPILOT_ERROR_PREFIX- session.messages.append(- ChatMessage(role="assistant", content=f"{prefix} {display_msg}")- )+ """Append a copilot error marker to *session* so it persists across refresh.++ Delegates to the shared writer so both engines produce the same row: the+ frontend's rendering contract lives in one place, and a failure recorded+ on a Codex turn carries the same envelope a baseline turn would.+ """+ append_error_marker(session, display_msg, retryable=retryable, failure=failure)+++def _provider_failure_for(ctx: "_StreamContext") -> ProviderFailure | None:+ """What the Codex gateway last named, if it named anything.++ The gateway is the last point where a provider failure is still a typed+ exception; downstream it is an HTTP status, then CLI text. Reading it+ here is what lets a Codex turn say "your ChatGPT login expired" rather+ than "the assistant ran into an error".++ ``None`` on the platform route, and on a Codex turn whose failure the+ gateway declined to name -- the caller keeps its existing behaviour.+ """+ gateway = ctx.codex_gateway+ return gateway.last_failure if gateway is not None else Nonedef _is_error_marker(msg: ChatMessage) -> bool:@@ -1598,6 +1630,7 @@ class _InterruptedAttempt:display_msg: str,*,retryable: bool,+ failure: dict[str, Any] | None = None,) -> list[StreamBaseResponse]:"""Re-attach partial + synthetic tool_result rows + error marker.@@ -1616,7 +1649,12 @@ class _InterruptedAttempt:session.messages.extend(self.partial)self.partial = []events = _flush_orphan_tool_uses_to_session(session, state)- _append_error_marker(session, display_msg, retryable=retryable)+ _append_error_marker(+ session,+ display_msg,+ retryable=retryable,+ failure=failure,+ )return events@@ -3603,10 +3641,16 @@ def _dispatch_response(response.errorText,response.code,)+ failure = _provider_failure_for(ctx)_append_error_marker(ctx.session,- response.errorText,- retryable=response.code in _RETRYABLE_STREAM_ERROR_CODES,+ failure.message if failure else response.errorText,+ retryable=(+ failure.retryable+ if failure+ else response.code in _RETRYABLE_STREAM_ERROR_CODES+ ),+ failure=failure.as_part() if failure else None,)if isinstance(response, StreamReasoningStart):@@ -4590,6 +4634,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuescodex_gateway: CodexAnthropicGateway | None = Nonedeferred_codex_cleanup_error: BaseException | None = Noneis_codex_transport = credential_lease is not None+ turn_segment = _sdk_serving_segment(credential_lease)# 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@@ -5244,6 +5289,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesattachments=attachments,compaction=compaction,lock=lock,+ codex_gateway=codex_gateway,)# ---------------------------------------------------------------@@ -5632,6 +5678,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesinterrupted, attempts_exhausted, transient_exhausted, stream_err)if failure is not None:+ provider_failure = _provider_failure_for(stream_ctx)cleanup_events: list[StreamBaseResponse] = []if state is not None:state.adapter._end_text_if_open(cleanup_events)@@ -5641,6 +5688,9 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesstate,failure.display_msg,retryable=failure.retryable,+ failure=(+ provider_failure.as_part() if provider_failure else None+ ),))for response in cleanup_events:@@ -5738,7 +5788,17 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# so this is a no-op for those and only kicks in for unhandled errors# that bypass the retry-loop handlers entirely.if not ended_with_stream_error:- interrupted.finalize(session, state, display_msg, retryable=is_transient)+ interrupted.finalize(+ session,+ state,+ display_msg,+ retryable=is_transient,+ failure=(+ codex_gateway.last_failure.as_part()+ if codex_gateway and codex_gateway.last_failure+ else None+ ),+ )logger.debug("%s Appended error marker, will be persisted in finally",log_prefix,@@ -5967,6 +6027,13 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesactual_model=state.observed_model if state is not None else None,routing_source=routing_source,)+ # What this turn ran on, recorded on the turn rather than read+ # back off the session later, so a route change cannot rewrite it.+ stamp_segment(+ session.messages,+ pre_turn_message_count,+ turn_segment,+ )try:await asyncio.shield(upsert_chat_session(session))logger.info(@@ -6294,6 +6361,19 @@ def _same_model(a: str, b: str | None) -> bool:return b is not None and _canonical_model(a) == _canonical_model(b)+def _sdk_serving_segment(+ credential_lease: CredentialLease | PooledCodexRuntimeLease | None,+) -> Segment:+ """The immutable route that actually serves this SDK turn."""+ if credential_lease is None:+ return Segment("platform", None, is_segment_zero=False)+ return Segment(+ "codex",+ credential_lease.credentials.id,+ is_segment_zero=False,+ )++def _stamp_turn_messages(messages: list[ChatMessage],*,diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex 96cf507f6b..8dd7ca07cc 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -1690,6 +1690,40 @@ class TestStampTurnMessages:), "pre_turn_message_count must be captured AFTER error-marker cleanup"+class TestSdkServingSegment:+ def test_platform_turn_is_not_read_from_mutable_session_metadata(self):+ from backend.copilot.sdk.service import _sdk_serving_segment++ segment = _sdk_serving_segment(None)++ assert segment.auth_provider == "platform"+ assert segment.credential_id is None+ assert segment.is_segment_zero is False++ def test_codex_turn_is_attributed_to_the_credential_that_served_it(self):+ from backend.copilot.sdk.service import _sdk_serving_segment++ lease = MagicMock()+ lease.credentials.id = "cred-that-served-the-turn"++ segment = _sdk_serving_segment(lease)++ assert segment.auth_provider == "codex"+ assert segment.credential_id == "cred-that-served-the-turn"+ assert segment.is_segment_zero is False++ def test_finalizer_stamps_the_captured_serving_segment(self):+ import inspect++ from backend.copilot.sdk.service import stream_chat_completion_sdk++ source = inspect.getsource(stream_chat_completion_sdk)++ assert "turn_segment = _sdk_serving_segment(credential_lease)" in source+ assert "pre_turn_message_count,\n turn_segment," in source+ assert "session_segment(session)" not in source++class TestChatMessageStampRoundTrip:"""model survives client-facing serialization; routing_source isdeliberately EXCLUDED from payloads (clients could infer LD cohortdiff --git a/autogpt_platform/backend/backend/copilot/segments.py b/autogpt_platform/backend/backend/copilot/segments.pynew file mode 100644index 0000000000..2072fc391d--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/segments.py@@ -0,0 +1,121 @@+"""Which connection a given turn actually ran on.++A session's metadata says where a chat *starts*. For most of this codebase's+life that was the same thing as where every turn in it ran, because the+connection was chosen at creation and could never change. Once it can --+after a usage limit, on a user's switch -- the two stop being the same, and+a session-level answer starts lying about history: every past turn would+appear to have run on whatever the connection happens to be now.++So each assistant turn records its own route, and the session's metadata+becomes the answer for turns that have none -- segment zero. That covers+every row written before the columns existed, without a backfill that would+have to invent what it could not know.++Nothing here rewrites a stamped turn. A route change applies forward.+"""++from backend.copilot.config import CopilotLlmAuthProvider+from backend.copilot.model import ChatMessage, ChatSession+++class Segment:+ """The connection one turn ran on, and where that answer came from."""++ __slots__ = ("auth_provider", "credential_id", "is_segment_zero")+ auth_provider: CopilotLlmAuthProvider+ credential_id: str | None+ is_segment_zero: bool++ def __init__(+ self,+ auth_provider: CopilotLlmAuthProvider,+ credential_id: str | None,+ *,+ is_segment_zero: bool,+ ) -> None:+ self.auth_provider = auth_provider+ self.credential_id = credential_id+ # True when this came from the session rather than the turn itself:+ # the turn predates per-turn stamping, or is the first of the chat.+ self.is_segment_zero = is_segment_zero++ def __eq__(self, other: object) -> bool:+ if not isinstance(other, Segment):+ return NotImplemented+ return (+ self.auth_provider == other.auth_provider+ and self.credential_id == other.credential_id+ )++ def __repr__(self) -> str:+ return (+ f"Segment({self.auth_provider!r}, {self.credential_id!r}, "+ f"is_segment_zero={self.is_segment_zero})"+ )+++def segment_of(message: ChatMessage, session: ChatSession) -> Segment:+ """The route this turn ran on, falling back to the session's own.++ A stamp is only trusted when the turn carries an auth provider. A+ credential id without one cannot be interpreted -- it names an account+ without saying whose -- so it is ignored rather than guessed at.+ """+ if message.llm_auth_provider is None:+ return session_segment(session)+ return Segment(+ message.llm_auth_provider,+ message.llm_credential_id,+ is_segment_zero=False,+ )+++def session_segment(session: ChatSession) -> Segment:+ """Segment zero: where this chat started."""+ return Segment(+ session.metadata.llm_auth_provider,+ session.metadata.llm_credential_id,+ is_segment_zero=True,+ )+++def segment_boundaries(messages: list[ChatMessage], session: ChatSession) -> list[int]:+ """Indexes where the connection changed, in order.++ Only assistant turns carry a route, so user and tool rows are skipped+ rather than treated as a change back to segment zero. The first assistant+ turn is a boundary only when it ran somewhere other than where the chat+ started.+ """+ boundaries: list[int] = []+ current = session_segment(session)+ for index, message in enumerate(messages):+ if message.role != "assistant" or message.llm_auth_provider is None:+ continue+ segment = segment_of(message, session)+ if segment != current:+ boundaries.append(index)+ current = segment+ return boundaries+++def stamp_segment(+ messages: list[ChatMessage],+ start_index: int,+ segment: Segment,+) -> None:+ """Record the connection on the assistant turns this run produced.++ Mirrors how ``model`` / ``routing_source`` are stamped: only rows that+ have none are written, so a re-stamp cannot overwrite what an earlier+ run recorded. Rows already flushed to the database mid-turn are flagged+ for back-fill by the save path.+ """+ for message in messages[start_index:]:+ if message.role != "assistant" or message.llm_auth_provider is not None:+ continue+ message.llm_auth_provider = segment.auth_provider+ message.llm_credential_id = segment.credential_id+ if message.sequence is not None:+ message.stamps_pending_save = Truediff --git a/autogpt_platform/backend/backend/copilot/segments_test.py b/autogpt_platform/backend/backend/copilot/segments_test.pynew file mode 100644index 0000000000..7ce058c9a9--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/segments_test.py@@ -0,0 +1,237 @@+from datetime import UTC, datetime+from unittest.mock import AsyncMock, MagicMock++import pytest+import pytest_mock++from backend.copilot.config import CopilotLlmAuthProvider+from backend.copilot.model import ChatMessage, ChatSession, ChatSessionMetadata+from backend.copilot.segments import (+ Segment,+ segment_boundaries,+ segment_of,+ session_segment,+ stamp_segment,+)+++def _session(+ auth_provider: CopilotLlmAuthProvider = "platform",+ credential_id: str | None = None,+ messages: list[ChatMessage] | None = None,+) -> ChatSession:+ now = datetime(2026, 8, 20, tzinfo=UTC)+ return ChatSession(+ session_id="s1",+ user_id="u1",+ usage=[],+ started_at=now,+ updated_at=now,+ messages=messages or [],+ metadata=ChatSessionMetadata(+ llm_auth_provider=auth_provider,+ llm_credential_id=credential_id,+ ),+ )+++def _assistant(+ auth_provider: CopilotLlmAuthProvider | None = None,+ credential_id: str | None = None,+ sequence: int | None = None,+) -> ChatMessage:+ return ChatMessage(+ role="assistant",+ content="hi",+ llm_auth_provider=auth_provider,+ llm_credential_id=credential_id,+ sequence=sequence,+ )+++class TestSegmentZero:+ def test_a_turn_that_recorded_nothing_reads_as_the_session_route(self) -> None:+ # Every row written before these columns existed. A backfill would+ # have had to invent what it could not know.+ session = _session("codex", "cred-1")+ segment = segment_of(_assistant(), session)+ assert segment == Segment("codex", "cred-1", is_segment_zero=True)+ assert segment.is_segment_zero is True++ def test_a_turn_that_recorded_its_route_is_believed(self) -> None:+ session = _session("platform", None)+ segment = segment_of(_assistant("codex", "cred-9"), session)+ assert segment == Segment("codex", "cred-9", is_segment_zero=False)+ assert segment.is_segment_zero is False++ def test_a_credential_without_a_provider_is_not_guessed_at(self) -> None:+ # It names an account without saying whose. Falling back is honest;+ # pairing it with the session's provider would fabricate a route.+ session = _session("platform", None)+ segment = segment_of(_assistant(None, "cred-9"), session)+ assert segment == Segment("platform", None, is_segment_zero=True)+++class TestHistoryIsNotRewritten:+ def test_a_route_change_leaves_earlier_turns_alone(self) -> None:+ earlier = _assistant("codex", "cred-1")+ session = _session("platform", None, [earlier])++ # The session now points somewhere else entirely.+ assert segment_of(earlier, session) == Segment(+ "codex", "cred-1", is_segment_zero=False+ )++ def test_stamping_never_overwrites_a_turn_that_already_has_a_route(self) -> None:+ already = _assistant("codex", "cred-1")+ stamp_segment([already], 0, Segment("platform", None, is_segment_zero=True))+ assert already.llm_auth_provider == "codex"+ assert already.llm_credential_id == "cred-1"++ def test_only_this_run_s_turns_are_stamped(self) -> None:+ history = _assistant()+ fresh = _assistant()+ stamp_segment(+ [history, fresh], 1, Segment("codex", "cred-1", is_segment_zero=False)+ )+ assert history.llm_auth_provider is None+ assert fresh.llm_auth_provider == "codex"+++class TestStamping:+ def test_a_user_row_is_never_given_a_route(self) -> None:+ user = ChatMessage(role="user", content="hi")+ stamp_segment([user], 0, Segment("codex", "cred-1", is_segment_zero=False))+ assert user.llm_auth_provider is None++ def test_a_row_already_flushed_is_flagged_for_backfill(self) -> None:+ # Mid-turn flush assigned a sequence before end-of-turn stamping.+ flushed = _assistant(sequence=4)+ stamp_segment([flushed], 0, Segment("codex", "cred-1", is_segment_zero=False))+ assert flushed.stamps_pending_save is True++ def test_an_unflushed_row_needs_no_backfill(self) -> None:+ fresh = _assistant()+ stamp_segment([fresh], 0, Segment("codex", "cred-1", is_segment_zero=False))+ assert fresh.stamps_pending_save is False+++class TestBoundaries:+ def test_a_chat_that_never_moved_has_none(self) -> None:+ session = _session("platform", None)+ messages = [_assistant("platform"), _assistant("platform")]+ assert segment_boundaries(messages, session) == []++ def test_the_turn_the_route_changed_on_is_a_boundary(self) -> None:+ session = _session("platform", None)+ messages = [+ _assistant("platform"),+ _assistant("codex", "cred-1"),+ _assistant("codex", "cred-1"),+ ]+ assert segment_boundaries(messages, session) == [1]++ def test_moving_back_is_a_boundary_too(self) -> None:+ session = _session("platform", None)+ messages = [+ _assistant("codex", "cred-1"),+ _assistant("platform"),+ ]+ assert segment_boundaries(messages, session) == [0, 1]++ def test_switching_between_two_accounts_of_one_provider_counts(self) -> None:+ # Same provider, different subscription -- a real change of who pays.+ session = _session("codex", "cred-1")+ messages = [_assistant("codex", "cred-1"), _assistant("codex", "cred-2")]+ assert segment_boundaries(messages, session) == [1]++ def test_unstamped_rows_do_not_read_as_a_change(self) -> None:+ # User and tool rows carry no route, and neither do turns written+ # before the columns existed. Treating either as "back to segment+ # zero" would invent boundaries that never happened.+ session = _session("codex", "cred-1")+ messages = [+ _assistant("codex", "cred-1"),+ ChatMessage(role="user", content="next"),+ ChatMessage(role="tool", content="{}", tool_call_id="t1"),+ _assistant("codex", "cred-1"),+ ]+ assert segment_boundaries(messages, session) == []+++class TestSessionSegment:+ def test_it_reports_where_the_chat_started(self) -> None:+ segment = session_segment(_session("codex", "cred-1"))+ assert segment.auth_provider == "codex"+ assert segment.credential_id == "cred-1"+ assert segment.is_segment_zero is True+++ "a, b, equal",+ [+ (+ Segment("platform", None, is_segment_zero=True),+ Segment("platform", None, is_segment_zero=False),+ True,+ ),+ (+ Segment("codex", "c1", is_segment_zero=False),+ Segment("codex", "c2", is_segment_zero=False),+ False,+ ),+ (+ Segment("platform", None, is_segment_zero=False),+ Segment("codex", None, is_segment_zero=False),+ False,+ ),+ ],+)+def test_two_segments_match_on_the_route_not_on_where_it_was_read(+ a: Segment, b: Segment, equal: bool+) -> None:+ # Whether the answer came from the turn or the session is provenance,+ # not identity -- otherwise the first stamped turn of an unchanged chat+ # would read as a boundary.+ assert (a == b) is equal+++class TestTheRouteSurvivesTheSavePath:+ """The stamp is worthless if it is dropped on the way to the database.++ ``_save_session_to_db`` builds each row's dict field by field rather than+ dumping the model, so a new field on ``ChatMessage`` reaches Postgres only+ if it is named there too. Stamping worked and the column existed, and the+ route still landed as NULL because that one list had not been updated.+ """++ @pytest.mark.asyncio+ async def test_a_stamped_turn_reaches_the_database_layer_with_its_route(+ self, mocker: pytest_mock.MockerFixture+ ) -> None:+ from backend.copilot import model as model_module++ captured: dict[str, object] = {}++ async def _capture(session_id: str, messages: list, start_sequence: int) -> int:+ captured["messages"] = messages+ return start_sequence++ fake_db = MagicMock()+ fake_db.add_chat_messages_batch = AsyncMock(side_effect=_capture)+ fake_db.get_chat_session_metadata = AsyncMock(return_value=None)+ fake_db.create_chat_session = AsyncMock(return_value=None)+ fake_db.update_chat_session = AsyncMock(return_value=None)+ mocker.patch.object(model_module, "chat_db", return_value=fake_db)++ session = _session("codex", "cred-1", [_assistant("codex", "cred-1")])+ await model_module._save_session_to_db(+ session, existing_message_count=0, skip_existence_check=True+ )++ rows = captured.get("messages")+ assert rows, "no rows reached the database layer"+ assistant_rows = [r for r in rows if r["role"] == "assistant"]+ assert assistant_rows, "the assistant row never reached the database layer"+ assert assistant_rows[0]["llm_auth_provider"] == "codex"+ assert assistant_rows[0]["llm_credential_id"] == "cred-1"diff --git a/autogpt_platform/backend/backend/copilot/stream_registry.py b/autogpt_platform/backend/backend/copilot/stream_registry.pyindex 7874ca2dcc..8f983fa3dc 100644--- a/autogpt_platform/backend/backend/copilot/stream_registry.py+++ b/autogpt_platform/backend/backend/copilot/stream_registry.py@@ -50,6 +50,7 @@ from .response_model import (StreamHeartbeat,StreamModeChanged,StreamPendingDrained,+ StreamProviderFailure,StreamReasoningDelta,StreamReasoningEnd,StreamReasoningStart,@@ -1141,6 +1142,37 @@ async def get_active_session(return session, last_id+# Every stream part the registry can rebuild on replay.+#+# Turns run in the executor and reach the browser by replay, so a part+# missing from this map is emitted, published, and then silently dropped --+# visible only as an "Unknown chunk type" warning. Add new parts here.+CHUNK_TYPE_TO_CLASS: dict[str, type[StreamBaseResponse]] = {+ ResponseType.START.value: StreamStart,+ ResponseType.FINISH.value: StreamFinish,+ ResponseType.START_STEP.value: StreamStartStep,+ ResponseType.FINISH_STEP.value: StreamFinishStep,+ ResponseType.TEXT_START.value: StreamTextStart,+ ResponseType.TEXT_DELTA.value: StreamTextDelta,+ ResponseType.TEXT_END.value: StreamTextEnd,+ ResponseType.REASONING_START.value: StreamReasoningStart,+ ResponseType.REASONING_DELTA.value: StreamReasoningDelta,+ ResponseType.REASONING_END.value: StreamReasoningEnd,+ ResponseType.TOOL_INPUT_START.value: StreamToolInputStart,+ ResponseType.TOOL_INPUT_AVAILABLE.value: StreamToolInputAvailable,+ ResponseType.TOOL_OUTPUT_AVAILABLE.value: StreamToolOutputAvailable,+ ResponseType.ERROR.value: StreamError,+ ResponseType.USAGE.value: StreamUsage,+ ResponseType.HEARTBEAT.value: StreamHeartbeat,+ ResponseType.STATUS.value: StreamStatus,+ ResponseType.DREAM_OPERATIONS.value: StreamDreamOperations,+ ResponseType.PENDING_DRAINED.value: StreamPendingDrained,+ ResponseType.MODE_CHANGED.value: StreamModeChanged,+ ResponseType.PROVIDER_FAILURE.value: StreamProviderFailure,+ ResponseType.COMPACTION.value: StreamCompactionProgress,+}++def _reconstruct_chunk(chunk_data: dict) -> StreamBaseResponse | None:"""Reconstruct a StreamBaseResponse from JSON data.@@ -1150,33 +1182,8 @@ def _reconstruct_chunk(chunk_data: dict) -> StreamBaseResponse | None:Returns:Reconstructed response object, or None if unknown type"""- # Map response types to their corresponding classes- type_to_class: dict[str, type[StreamBaseResponse]] = {- ResponseType.START.value: StreamStart,- ResponseType.FINISH.value: StreamFinish,- ResponseType.START_STEP.value: StreamStartStep,- ResponseType.FINISH_STEP.value: StreamFinishStep,- ResponseType.TEXT_START.value: StreamTextStart,- ResponseType.TEXT_DELTA.value: StreamTextDelta,- ResponseType.TEXT_END.value: StreamTextEnd,- ResponseType.REASONING_START.value: StreamReasoningStart,- ResponseType.REASONING_DELTA.value: StreamReasoningDelta,- ResponseType.REASONING_END.value: StreamReasoningEnd,- ResponseType.TOOL_INPUT_START.value: StreamToolInputStart,- ResponseType.TOOL_INPUT_AVAILABLE.value: StreamToolInputAvailable,- ResponseType.TOOL_OUTPUT_AVAILABLE.value: StreamToolOutputAvailable,- ResponseType.ERROR.value: StreamError,- ResponseType.USAGE.value: StreamUsage,- ResponseType.HEARTBEAT.value: StreamHeartbeat,- ResponseType.STATUS.value: StreamStatus,- ResponseType.DREAM_OPERATIONS.value: StreamDreamOperations,- ResponseType.PENDING_DRAINED.value: StreamPendingDrained,- ResponseType.MODE_CHANGED.value: StreamModeChanged,- ResponseType.COMPACTION.value: StreamCompactionProgress,- }-- chunk_type = chunk_data.get("type")- chunk_class = type_to_class.get(chunk_type) # type: ignore[arg-type]+ chunk_type = chunk_data.get("type", "")+ chunk_class = CHUNK_TYPE_TO_CLASS.get(chunk_type)if chunk_class is None:logger.warning(f"Unknown chunk type: {chunk_type}")diff --git a/autogpt_platform/backend/backend/copilot/stream_registry_test.py b/autogpt_platform/backend/backend/copilot/stream_registry_test.pyindex 236c60ae4f..891ecee3f1 100644--- a/autogpt_platform/backend/backend/copilot/stream_registry_test.py+++ b/autogpt_platform/backend/backend/copilot/stream_registry_test.py@@ -594,13 +594,13 @@ def test_every_response_type_survives_the_relay():stays green — this has now happened twice (MODE_CHANGED, COMPACTION).Adding a ``ResponseType`` should force a decision here."""- import inspect- import re-from backend.copilot.response_model import ResponseType- source = inspect.getsource(stream_registry._reconstruct_chunk)- mapped = set(re.findall(r"ResponseType\.(\w+)\.value", source))+ mapped = {+ member.name+ for member in ResponseType+ if member.value in stream_registry.CHUNK_TYPE_TO_CLASS+ }expected = {m.name for m in ResponseType} - _UNRELAYED_RESPONSE_TYPESmissing = sorted(expected - mapped)@@ -608,3 +608,61 @@ def test_every_response_type_survives_the_relay():f"ResponseType(s) {missing} are not registered in _reconstruct_chunk — ""they would be dropped on the executor → Redis → rest_server relay")+++class TestEveryPartSurvivesReplay:+ """A part the registry cannot reconstruct never reaches the client.++ Turns run in the executor and reach the browser by replay through this+ registry, so a stream part is only real if ``_reconstruct_chunk`` knows+ its type. ``StreamProviderFailure`` was emitted correctly, published+ correctly, and dropped here with "Unknown chunk type" -- invisible except+ as a warning in the logs.+ """++ def test_provider_failure_is_reconstructed(self) -> None:+ from backend.copilot.response_model import ResponseType, StreamProviderFailure+ from backend.copilot.stream_registry import _reconstruct_chunk++ chunk = _reconstruct_chunk(+ {+ "type": ResponseType.PROVIDER_FAILURE.value,+ "failure": {"kind": "usage_limit", "retryable": False},+ }+ )++ assert isinstance(chunk, StreamProviderFailure)+ assert chunk.failure["kind"] == "usage_limit"++ def test_no_emittable_part_is_missing_from_the_registry(self) -> None:+ # The map is hand-maintained, so a new part is only one forgotten+ # line away from being emitted, published, and silently undelivered.+ import inspect++ from backend.copilot import response_model+ from backend.copilot.response_model import StreamBaseResponse+ from backend.copilot.stream_registry import CHUNK_TYPE_TO_CLASS++ # Parts nothing can emit, so nothing can fail to deliver:+ # StreamHeartbeat rides as an SSE comment, never as a chunk.+ # StreamCursor is deprecated and no longer constructed anywhere.+ # (Its docstring says it is kept so older stored chunks can be+ # reconstructed, which the missing map entry does not actually+ # deliver -- harmless while nothing emits it, but not what the+ # docstring claims.)+ never_published = {"StreamHeartbeat", "StreamCursor"}++ missing = []+ for name, cls in inspect.getmembers(response_model, inspect.isclass):+ if not issubclass(cls, StreamBaseResponse) or cls is StreamBaseResponse:+ continue+ if name in never_published:+ continue+ field = cls.model_fields.get("type")+ default = getattr(field, "default", None)+ if default is None or not hasattr(default, "value"):+ continue+ if default.value not in CHUNK_TYPE_TO_CLASS:+ missing.append(f"{name} ({default.value})")++ assert not missing, f"parts that cannot survive replay: {missing}"diff --git a/autogpt_platform/backend/backend/util/entitlements.py b/autogpt_platform/backend/backend/util/entitlements.pyindex d776ff571f..e62141bfb3 100644--- a/autogpt_platform/backend/backend/util/entitlements.py+++ b/autogpt_platform/backend/backend/util/entitlements.py@@ -11,6 +11,10 @@ from backend.util.settings import BehaveAs, Settingsclass Entitlement(str, Enum):CODEX_SUBSCRIPTION_TRANSPORT = "codex_subscription_transport"+ # The Advanced model tier. Gated on hosted so it is a reason to upgrade+ # rather than a reason not to; granted outright on self-host, where+ # there is no plan to sell and the operator pays for their own tokens.+ ADVANCED_MODEL_TIER = "advanced_model_tier"class EntitlementPolicy(BaseModel):@@ -26,6 +30,10 @@ ENTITLEMENT_POLICIES: Mapping[Entitlement, EntitlementPolicy] = MappingProxyTypeminimum_tier=SubscriptionTier.MAX,allow_local=True,),+ Entitlement.ADVANCED_MODEL_TIER: EntitlementPolicy(+ minimum_tier=SubscriptionTier.MAX,+ allow_local=True,+ ),})diff --git a/autogpt_platform/backend/migrations/20260820030000_add_chat_message_llm_route/migration.sql b/autogpt_platform/backend/migrations/20260820030000_add_chat_message_llm_route/migration.sqlnew file mode 100644index 0000000000..58012d550c--- /dev/null+++ b/autogpt_platform/backend/migrations/20260820030000_add_chat_message_llm_route/migration.sql@@ -0,0 +1,6 @@+-- AlterTable+-- Nullable with no default: Postgres records this in the catalog only, so+-- it does not rewrite the table. Existing rows read as NULL and fall back+-- to the session's own route.+ALTER TABLE "ChatMessage" ADD COLUMN "llmAuthProvider" TEXT,+ADD COLUMN "llmCredentialId" TEXT;diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prismaindex 2bfcdd2ca3..ae19b03a1b 100644--- a/autogpt_platform/backend/schema.prisma+++ b/autogpt_platform/backend/schema.prisma@@ -636,6 +636,14 @@ model ChatMessage {model String?routingSource String?+ // Which connection served this assistant turn. The session's metadata is+ // the route a chat STARTS on; these record the route a turn actually RAN+ // on, so a later route change cannot rewrite what already happened.+ // Null on user/tool rows and on rows predating this column, where the+ // session's own route is the answer (see `segment_of`).+ llmAuthProvider String?+ llmCredentialId String?+// Per-row JSONB metadata bag. Currently holds the dispatcher's// submit-time payload (file_ids, mode, model, permissions, context,// request_arrival_at) on the user row that triggered a queued turn,diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/providerFailure.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/providerFailure.test.tsnew file mode 100644index 0000000000..064696a966--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/providerFailure.test.ts@@ -0,0 +1,111 @@+import { describe, expect, it } from "vitest";++import {+ describeProviderFailure,+ parseProviderFailurePart,+ type ProviderFailure,+} from "../providerFailure";++function part(data: Record<string, unknown>) {+ return { type: "data-provider-failure", data };+}++function failure(over: Partial<ProviderFailure> = {}): ProviderFailure {+ return {+ kind: "usage_limit",+ message: "",+ authProvider: "codex",+ credentialId: "cred-1",+ resetsAt: null,+ retryable: false,+ reconnectFixesIt: false,+ ...over,+ };+}++describe("parseProviderFailurePart", () => {+ it("reads a failure the server sent", () => {+ const parsed = parseProviderFailurePart(+ part({+ kind: "auth_expired",+ message: "token expired",+ authProvider: "codex",+ credentialId: "cred-1",+ resetsAt: null,+ retryable: false,+ reconnectFixesIt: true,+ }),+ );++ expect(parsed?.kind).toBe("auth_expired");+ expect(parsed?.reconnectFixesIt).toBe(true);+ });++ it("ignores other data parts", () => {+ expect(+ parseProviderFailurePart({+ type: "data-mode-changed",+ data: { mode: "fast" },+ }),+ ).toBeNull();+ });++ it("refuses a kind it does not know", () => {+ // A kind added server-side without client copy would otherwise render+ // as an empty toast.+ expect(parseProviderFailurePart(part({ kind: "invented" }))).toBeNull();+ });++ it("does not infer advice the server withheld", () => {+ // Absent means "not retryable", never "probably fine to retry".+ const parsed = parseProviderFailurePart(part({ kind: "policy_denied" }));+ expect(parsed?.retryable).toBe(false);+ expect(parsed?.reconnectFixesIt).toBe(false);+ });+});++describe("describeProviderFailure", () => {+ it("does not tell someone to retry what cannot succeed", () => {+ // The whole point of the envelope: three failures that used to get+ // "Press Try Again" now say what would actually help.+ for (const kind of [+ "auth_expired",+ "usage_limit",+ "model_unavailable",+ ] as const) {+ const copy = describeProviderFailure(failure({ kind }));+ expect(copy.description.toLowerCase()).not.toContain("try again");+ }+ });++ it("names the reset when the provider reported one", () => {+ const copy = describeProviderFailure(+ failure({ resetsAt: Math.floor(Date.now() / 1000) + 20 * 60 }),+ );+ expect(copy.description).toContain("about 20 minutes");+ });++ it("names no time when the provider reported no reset", () => {+ // The kind's own copy may mention resetting in general; what must not+ // appear is a specific time nobody reported.+ const copy = describeProviderFailure(failure({ resetsAt: null }));+ expect(copy.description).not.toMatch(/Resets in/i);+ });++ it("drops a reset that has already passed", () => {+ const copy = describeProviderFailure(+ failure({ resetsAt: Math.floor(Date.now() / 1000) - 60 }),+ );+ expect(copy.description).not.toMatch(/Resets in/i);+ });++ it("prefers the provider's own words for the detail", () => {+ const copy = describeProviderFailure(+ failure({+ kind: "policy_denied",+ message: "This request was blocked by the provider's safety policy.",+ }),+ );+ expect(copy.description).toContain("safety policy");+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ChoiceRow.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ChoiceRow.tsxindex 372fa44e48..b7ec8bae7e 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ChoiceRow.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ChoiceRow.tsx@@ -16,6 +16,8 @@ interface Props {* its own line, so the name a screen reader announces stays complete.*/label?: string;+ /** Shown beside the title, e.g. "Connected". */+ badge?: string;/** Why this cannot be chosen, and where to go to change that. */lock?: { reason: string; href: string | null };}@@ -28,9 +30,12 @@ export function ChoiceRow({onSelect,label,lock,+ badge,}: Props) {if (lock) {- return <LockedRow title={title} subtitle={subtitle} lock={lock} />;+ return (+ <LockedRow title={title} subtitle={subtitle} notes={notes} lock={lock} />+ );}return (@@ -58,7 +63,14 @@ export function ChoiceRow({)}</span><span className="flex min-w-0 flex-col">- <span className="text-xs font-medium text-foreground">{title}</span>+ <span className="flex items-center gap-1.5">+ <span className="text-xs font-medium text-foreground">{title}</span>+ {badge && (+ <span className="rounded-full bg-green-500/10 px-1.5 py-px text-[10px] font-medium text-green-700">+ {badge}+ </span>+ )}+ </span>{subtitle && (<span className="break-words text-[11px] leading-snug text-muted-foreground">{subtitle}@@ -80,6 +92,7 @@ export function ChoiceRow({interface LockedProps {title: string;subtitle?: string;+ notes?: string[];lock: { reason: string; href: string | null };}@@ -88,7 +101,7 @@ interface LockedProps {* click that cannot do anything. It states what the connection is, why it is* unavailable, and the one action that changes that.*/-function LockedRow({ title, subtitle, lock }: LockedProps) {+function LockedRow({ title, subtitle, notes, lock }: LockedProps) {return (<div className="flex items-start gap-2.5 px-3 py-2"><LockIcon@@ -105,6 +118,14 @@ function LockedRow({ title, subtitle, lock }: LockedProps) {{subtitle}</span>)}+ {notes?.map((note) => (+ <span+ key={note}+ className="mt-0.5 text-[11px] leading-snug text-muted-foreground/80"+ >+ {note}+ </span>+ ))}<span className="mt-0.5 text-[11px] leading-snug text-muted-foreground/80">{lock.reason}</span>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ConnectionPicker.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ConnectionPicker.tsxindex 98af38d794..ebed8e4f4d 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ConnectionPicker.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/ConnectionPicker.tsx@@ -13,7 +13,15 @@ import {} from "react-icons/pi";import { ChoiceRow } from "./ChoiceRow";-import { offerSubtitle, tierLabel, tierModel, tierName } from "./helpers";+import {+ isLinkedAccount,+ offerSubtitle,+ tierLabel,+ tierModel,+ tierName,+ tierLock,+ tierSummary,+} from "./helpers";import { useConnectionPicker } from "./useConnectionPicker";const TIERS = ["standard", "advanced"] as const;@@ -120,7 +128,10 @@ export function ConnectionPicker({ connectionLocked = false }: Props) {key={offer.offer_id}title={offer.display_name}subtitle={offerSubtitle(offer)}- notes={offer.limitations}+ badge={isLinkedAccount(offer) ? "Connected" : undefined}+ notes={[tierSummary(offer), ...offer.limitations].filter(+ Boolean,+ )}isSelected={offer.offer_id === active?.offer_id}onSelect={() => chooseConnection(offer)}lock={@@ -149,6 +160,7 @@ export function ConnectionPicker({ connectionLocked = false }: Props) {label={tierLabel(active, candidate)}isSelected={tier === candidate}onSelect={() => setTier(candidate)}+ lock={tierLock(active, candidate)}/>))}</div>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/helpers.tsindex d3f0e13d0d..5346b645d9 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ConnectionPicker/helpers.ts@@ -105,3 +105,45 @@ export function tierLabel(export function offerSubtitle(offer: AIConnectionOffer): string {return offer.lock_reason ? offer.description : offer.backed_by_label;}++/**+ * "Balanced: Sonnet 5 · Advanced: Opus 5" for a connection row.+ *+ * The PRD's mock puts both models under each route so the choice can be made+ * before switching to it -- otherwise you have to select a connection to+ * discover what it runs, which is the wrong order.+ *+ * Empty when the server named no models, so the row simply omits the line+ * rather than rendering "Balanced: · Advanced:".+ */+export function tierSummary(offer: AIConnectionOffer): string {+ const named = offer.tiers+ .filter((tier) => tier.display_model)+ .map((tier) => `${tier.label}: ${tier.display_model}`);+ return named.join(" · ");+}++/**+ * Whether this connection is one the user linked themselves.+ *+ * Drives the "Connected" badge: a deployment route is not something anyone+ * connected, so badging it would be meaningless.+ */+export function isLinkedAccount(offer: AIConnectionOffer): boolean {+ return offer.auth_method !== "deployment" && Boolean(offer.credential_id);+}++/**+ * The lock on a tier, when the plan does not include it.+ *+ * Advanced stays visible and locked rather than hidden: the row is the+ * upgrade reason, and hiding it would remove the thing it exists to create.+ */+export function tierLock(+ offer: AIConnectionOffer | undefined,+ tier: string,+): { reason: string; href: string | null } | undefined {+ const match = tierOf(offer, tier);+ if (!match?.lock_reason) return undefined;+ return { reason: match.lock_reason, href: "/settings/billing" };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ConnectionPicker.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ConnectionPicker.test.tsxindex d36456ce5d..133fd5deb7 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ConnectionPicker.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ConnectionPicker.test.tsx@@ -5,7 +5,12 @@ import {import type { AIConnectionOffer } from "@/app/api/__generated__/models/aIConnectionOffer";import type { ConnectionTier } from "@/app/api/__generated__/models/connectionTier";import { server } from "@/mocks/mock-server";-import { render, screen, waitFor } from "@/tests/integrations/test-utils";+import {+ render,+ screen,+ waitFor,+ within,+} from "@/tests/integrations/test-utils";import userEvent from "@testing-library/user-event";import { beforeEach, describe, expect, it } from "vitest";@@ -398,6 +403,29 @@ describe("ConnectionPicker", () => {expect(screen.queryByText("Your ChatGPT plan")).toBeNull();});+ it("keeps named models visible on a locked connection", async () => {+ mockOffers([+ offer(),+ locked({+ tiers: [+ tier("standard", "Balanced", "gpt-5.6-terra"),+ tier("advanced", "Advanced", "gpt-5.6-sol"),+ ],+ }),+ ]);++ render(<ConnectionPicker />);+ await userEvent.click(+ await screen.findByRole("button", { name: /Runs on/ }),+ );++ expect(+ await screen.findByText(+ "Balanced: gpt-5.6-terra · Advanced: gpt-5.6-sol",+ ),+ ).toBeDefined();+ });+it("does not offer a locked connection as something to pick", async () => {// Rendering it as a radio would invite a click that cannot take effect.mockOffers([offer(), locked()]);@@ -427,6 +455,69 @@ describe("ConnectionPicker", () => {expect(useCopilotUIStore.getState().copilotLlmAuth).toBeNull();});+ it("names both models on a connection before you switch to it", async () => {+ // Otherwise you have to select a connection to discover what it runs,+ // which is the wrong order.+ mockOffers([offer(), chatgpt()]);++ render(<ConnectionPicker />);+ await userEvent.click(+ await screen.findByRole("button", { name: /Runs on/ }),+ );++ expect(+ await screen.findByText("Balanced: sonnet-5 · Advanced: opus-5"),+ ).toBeDefined();+ });++ it("badges a connection the user linked themselves", async () => {+ mockOffers([offer(), chatgpt()]);++ render(<ConnectionPicker />);+ await userEvent.click(+ await screen.findByRole("button", { name: /Runs on/ }),+ );++ expect(await screen.findByText("Connected")).toBeDefined();+ // The deployment route is not something anyone connected.+ expect(screen.getAllByText("Connected")).toHaveLength(1);+ });++ it("shows a tier the plan excludes as locked rather than hiding it", async () => {+ // The row is the upgrade reason; hiding it removes what it exists for.+ mockOffers([+ offer({+ tiers: [+ tier("standard", "Balanced", "sonnet-5"),+ {+ ...tier("advanced", "Advanced", "opus-5"),+ selectable: false,+ lock_reason: "A Max plan or higher is required for Advanced.",+ },+ ],+ }),+ chatgpt(),+ ]);++ render(<ConnectionPicker />);+ await userEvent.click(+ await screen.findByRole("button", { name: /Runs on/ }),+ );++ expect(+ await screen.findByText("A Max plan or higher is required for Advanced."),+ ).toBeDefined();+ // Still named, so the user sees what they would get.+ expect(screen.getByText("opus-5")).toBeDefined();+ // Scoped to the tier group: the connection row's own name now contains+ // "Advanced: opus-5" from its tier summary, so a page-wide query matches it.+ const tierGroup = screen.getByRole("radiogroup", { name: "Model tier" });+ expect(+ within(tierGroup).queryByRole("radio", { name: /Advanced/ }),+ ).toBeNull();+ expect(within(tierGroup).getAllByRole("radio")).toHaveLength(1);+ });+it("reports a failure rather than inventing a connection", async () => {server.use(getGetV2ListChatConnectionsMockHandler401());diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamErrorHandlers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamErrorHandlers.tsindex 3d4beb65dd..ecacddef2c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamErrorHandlers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamErrorHandlers.ts@@ -1,5 +1,10 @@import { toast } from "@/components/molecules/Toast/use-toast";+import {+ describeProviderFailure,+ type ProviderFailure,+} from "./providerFailure";+/*** Parses a backend-encoded error code from an `errorText` payload.*@@ -97,6 +102,12 @@ interface HandleStreamErrorArgs {onRateLimit: (message: string) => void;onReconnect: () => void;isUserStoppingRef: React.MutableRefObject<boolean>;+ /**+ * The typed envelope, when this turn sent one. It is the only source here+ * that actually knows what happened; every branch below it is inference+ * from error text.+ */+ providerFailure?: ProviderFailure | null;}/**@@ -104,6 +115,8 @@ interface HandleStreamErrorArgs {* (or rate-limit UI), and decides whether to retry via reconnect.** Dispatch order (exclusive branches):+ * 0. A typed provider-failure envelope → its own copy. Preferred over+ * everything below, which is guesswork from error text.* 1. `usage limit` substring → rate-limit UI via `onRateLimit`.* 2. 401 / auth failure → auth-error toast.* 3. `[code:<id>]` backend prefix → curated or generic backend toast.@@ -117,9 +130,28 @@ export function handleStreamError({onRateLimit,onReconnect,isUserStoppingRef,+ providerFailure,}: HandleStreamErrorArgs): void {const errorDetail = extractErrorDetail(error);+ // 0. The server said what went wrong, so stop guessing.+ if (providerFailure) {+ const copy = describeProviderFailure(providerFailure);+ if (providerFailure.kind === "usage_limit") {+ // Still routed through the rate-limit path: it restores the composer+ // text for a message the backend refused before persisting, which a+ // toast alone would lose.+ onRateLimit(`${copy.title}. ${copy.description}`);+ return;+ }+ toast({+ title: copy.title,+ description: copy.description,+ variant: "destructive",+ });+ return;+ }+// 1. Rate limit (FastAPI 429 body contains "usage limit")if (errorDetail.toLowerCase().includes("usage limit")) {onRateLimit(diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/providerFailure.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/providerFailure.tsnew file mode 100644index 0000000000..5a9c253365--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/providerFailure.ts@@ -0,0 +1,129 @@+/**+ * The typed reason a provider refused a turn, as it arrives on the stream.+ *+ * Until this existed the client had to sniff substrings out of error text --+ * `"usage limit"`, `"401"` -- to guess what happened, and everything it+ * could not place got the same advice: press Try Again. That advice is+ * wrong for most provider failures. Retrying an expired login, a spent+ * quota or a retired model cannot succeed, and telling someone to retry+ * costs them the time it takes to find out.+ *+ * The server decides `retryable` and `reconnectFixesIt` so a second+ * implementation of that judgement cannot drift from the one that classified+ * the failure.+ */++export type ProviderFailureKind =+ | "auth_expired"+ | "invalid_credential"+ | "usage_limit"+ | "model_unavailable"+ | "policy_denied"+ | "entitlement_required"+ | "transient";++export interface ProviderFailure {+ kind: ProviderFailureKind;+ message: string;+ authProvider: string | null;+ credentialId: string | null;+ resetsAt: number | null;+ retryable: boolean;+ reconnectFixesIt: boolean;+}++const KINDS = new Set<string>([+ "auth_expired",+ "invalid_credential",+ "usage_limit",+ "model_unavailable",+ "policy_denied",+ "entitlement_required",+ "transient",+]);++export function parseProviderFailurePart(dataPart: {+ type: string;+ data?: unknown;+}): ProviderFailure | null {+ if (dataPart.type !== "data-provider-failure") return null;+ const data = dataPart.data as Partial<ProviderFailure> | undefined;+ if (!data || typeof data.kind !== "string" || !KINDS.has(data.kind)) {+ return null;+ }+ return {+ kind: data.kind as ProviderFailureKind,+ message: typeof data.message === "string" ? data.message : "",+ authProvider: data.authProvider ?? null,+ credentialId: data.credentialId ?? null,+ resetsAt: typeof data.resetsAt === "number" ? data.resetsAt : null,+ retryable: data.retryable === true,+ reconnectFixesIt: data.reconnectFixesIt === true,+ };+}++interface FailureCopy {+ title: string;+ description: string;+}++const COPY_BY_KIND: Record<ProviderFailureKind, FailureCopy> = {+ auth_expired: {+ title: "Your connection needs signing in again",+ description: "Reconnect the account in Settings, then send this again.",+ },+ invalid_credential: {+ title: "That connection can't be used",+ description: "Reconnect the account in Settings, then send this again.",+ },+ usage_limit: {+ title: "You've hit this connection's limit",+ description: "Switch connection to keep going, or wait for it to reset.",+ },+ model_unavailable: {+ title: "That model isn't available",+ description: "Pick another model tier, or switch connection.",+ },+ policy_denied: {+ title: "The provider declined this request",+ description: "Rewording it may help. Retrying it unchanged won't.",+ },+ entitlement_required: {+ title: "Your plan doesn't include this connection",+ description: "Switch connection, or see plans to unlock it.",+ },+ transient: {+ title: "Connection hiccup",+ description: "Something went wrong on the way to the model. Try again.",+ },+};++/**+ * What to tell the user, preferring the provider's own words for the detail.+ *+ * The reset time is appended only when the provider reported one -- an+ * invented "try again in an hour" is worse than silence, because people plan+ * around it.+ */+export function describeProviderFailure(failure: ProviderFailure): FailureCopy {+ const base = COPY_BY_KIND[failure.kind];+ const reset = formatResetHint(failure.resetsAt);+ const detail = failure.message.trim() || base.description;+ return {+ title: base.title,+ description: reset ? `${base.description} ${reset}` : detail,+ };+}++function formatResetHint(resetsAt: number | null): string | null {+ if (resetsAt === null) return null;+ const seconds = resetsAt - Math.floor(Date.now() / 1000);+ if (seconds <= 0) return null;+ if (seconds < 90) return "Resets in under a minute.";+ const minutes = Math.round(seconds / 60);+ if (minutes < 60) return `Resets in about ${minutes} minutes.`;+ const hours = Math.round(minutes / 60);+ return hours === 1+ ? "Resets in about an hour."+ : `Resets in about ${hours} hours.`;+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.tsindex 3648828ef8..a5dffbc61e 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts@@ -34,6 +34,10 @@ import {} from "./helpers";import { extractDbSequence } from "./helpers/convertChatSessionToUiMessages";import { getLatestAssistantStatusMessage } from "./messageParts";+import {+ parseProviderFailurePart,+ type ProviderFailure,+} from "./providerFailure";import { useCopilotUIStore } from "./store";import type { CopilotLlmModel } from "./store";import { useCopilotReconnect } from "./useCopilotReconnect";@@ -118,6 +122,9 @@ export function useCopilotStream({// plain boolean can't bleed state across sessions.const isUserStoppingRef = useRef(false);const pendingEngineSwitchRef = useRef(false);+ // Cleared once consumed, so a later failure without an envelope cannot+ // inherit the explanation of an earlier one.+ const providerFailureRef = useRef<ProviderFailure | null>(null);// State mirror of ``isUserStoppingRef`` — the ref is read synchronously// inside SDK callbacks, the state drives UI so a click on the stop button// immediately overrides ``isStreaming`` regardless of whether AI SDK has@@ -183,6 +190,7 @@ export function useCopilotStream({? FINISH_REFETCH_ATTEMPTS_PENDING_SWITCH: FINISH_REFETCH_ATTEMPTS_DEFAULT;pendingEngineSwitchRef.current = false;+ providerFailureRef.current = null;setIsFinishProbing(true);try {for (let attempt = 0; attempt < attempts; attempt++) {@@ -211,8 +219,11 @@ export function useCopilotStream({clearKickoffPending(userId, kickoffExpertId, kickoffAttemptToken);}).catch(() => undefined);}+ const failureForThisTurn = providerFailureRef.current;+ providerFailureRef.current = null;handleStreamError({error,+ providerFailure: failureForThisTurn,onRateLimit: (message) => {// Backend raises 429 BEFORE persisting the user message, so the// optimistic user bubble added by useChat is a lie. Restore the text@@ -287,6 +298,12 @@ export function useCopilotStream({if (isEngineSwitchPart(dataPart)) {pendingEngineSwitchRef.current = true;}+ // The envelope always precedes the error frame it explains, so+ // stashing it here means handleError has it in hand.+ const failure = parseProviderFailurePart(dataPart);+ if (failure) {+ providerFailureRef.current = failure;+ }}chatRuntime.onFinish = handleFinish;diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex eded303186..b0e236793e 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -17499,12 +17499,16 @@"display_model": {"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "Display Model"+ },+ "lock_reason": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Lock Reason"}},"type": "object","required": ["tier", "label", "selectable"],"title": "ConnectionTier",- "description": "One quality level on a connection.\n\n``display_model`` is the model this tier resolves to, run through the\nsame router the turn will use — LaunchDarkly cell, then registry, then\nconfig — so it cannot drift from what actually answers.\n\nIt is ``None`` on a ChatGPT connection. Naming that model means asking\nthe account what it advertises, which takes a runtime lease against the\nprovider; doing that once per credential to render a list is the cost\nthis endpoint exists to avoid."+ "description": "One quality level on a connection.\n\n``display_model`` is the model this tier resolves to, run through the\nsame router the turn will use — LaunchDarkly cell, then registry, then\nconfig — so it cannot drift from what actually answers.\n\nOn a ChatGPT connection it is the model the catalog pins for that cell,\nread straight from the registry -- no lease, no call to the account.\nWhat a lease would add is confirmation that this particular account still\nadvertises it; the router checks that when the turn runs and falls back\nif not, so the name here is the routed model rather than a guarantee.\n``None`` only when the catalog pins nothing for the cell."},"ContentType": {"type": "string",