AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): surface empty-success ResultMessage as stream error (SECRT-2252) (#12926)
Zamil Majdy5d1cdc2bad44
diff --git a/autogpt_platform/backend/backend/copilot/sdk/interrupted_partial_test.py b/autogpt_platform/backend/backend/copilot/sdk/interrupted_partial_test.pyindex 3c69a8af04..0903af970e 100644--- a/autogpt_platform/backend/backend/copilot/sdk/interrupted_partial_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/interrupted_partial_test.py@@ -22,6 +22,7 @@ from backend.copilot.model import ChatMessage, ChatSessionfrom backend.copilot.response_model import StreamToolOutputAvailablefrom .service import (+ _RETRYABLE_STREAM_ERROR_CODES,_classify_final_failure,_FinalFailure,_flush_orphan_tool_uses_to_session,@@ -320,3 +321,22 @@ class TestRetryRollbackContract:"part-2",f"{COPILOT_ERROR_PREFIX} Boom",]+++class TestRetryableStreamErrorCodes:+ """SECRT-2252: ``_dispatch_response`` consults this set to decide whether+ the StreamError flowing through it should append a retryable marker (UI+ shows a retry button) or a terminal one (UI shows ErrorCard only)."""++ def test_transient_api_error_is_retryable(self):+ assert "transient_api_error" in _RETRYABLE_STREAM_ERROR_CODES++ def test_empty_completion_is_retryable(self):+ # The adapter emits this for ghost-finished SDK turns. The user+ # message ("The model returned an empty response.") only makes sense+ # if the UI offers a retry — otherwise the user sees a dead error.+ assert "empty_completion" in _RETRYABLE_STREAM_ERROR_CODES++ def test_unknown_codes_are_not_retryable(self):+ assert "sdk_error" not in _RETRYABLE_STREAM_ERROR_CODES+ assert "all_attempts_exhausted" not in _RETRYABLE_STREAM_ERROR_CODESdiff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.pyindex ab6a9e7510..9f43f95e80 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py@@ -376,6 +376,31 @@ class SDKResponseAdapter:elif isinstance(sdk_message, ResultMessage):self.flush_unresolved_tool_calls(responses)+ # SECRT-2252: surface ghost-finished sessions as errors instead of silent finishes.+ if sdk_message.subtype == "success" and self._is_empty_completion(+ sdk_message+ ):+ if self.step_open:+ responses.append(StreamFinishStep())+ self.step_open = False+ responses.append(+ StreamError(+ errorText="The model returned an empty response.",+ code="empty_completion",+ )+ )+ # Pair with StreamFinish so ``acc.stream_completed`` flips True+ # in ``_dispatch_response`` — without it the service-layer+ # post-stream branch mis-classifies the turn as "stopped by+ # user" and appends a STOPPED_BY_USER_MARKER on top of the+ # error marker.+ responses.append(StreamFinish())+ logger.warning(+ "[SDK] [%s] Empty-success ResultMessage detected — "+ "emitting stream error instead of silent finish",+ (self.session_id or "?")[:12],+ )+ return responses# Thinking-only final turn guard: when the model's last LLM# call after a tool result produced only a ``ThinkingBlock``# (no ``TextBlock``, no ``ToolUseBlock``) the UI has nothing@@ -437,6 +462,25 @@ class SDKResponseAdapter:return responses+ def _is_empty_completion(self, msg: ResultMessage) -> bool:+ """True when a success ResultMessage carries no content at all.++ Detects the SDK's ghost-finished session: empty ``result``, zero+ ``output_tokens``, and nothing emitted on the wire this turn (no+ text, no reasoning, no tool calls).+ """+ if msg.result:+ return False+ if self.has_started_text or self.has_started_reasoning:+ return False+ if self.current_tool_calls:+ return False+ if self._any_tool_results_seen:+ return False+ usage = msg.usage or {}+ output_tokens = usage.get("output_tokens") or 0+ return output_tokens == 0+def _ensure_text_started(self, responses: list[StreamBaseResponse]) -> None:"""Start (or restart) a text block if needed."""if not self.has_started_text or self.has_ended_text:diff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.pyindex 6d59e21fab..77a0a1fb0e 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py@@ -565,6 +565,105 @@ def test_result_success_does_not_synthesize_when_no_tools_ran():assert text_deltas == []+def test_result_empty_success_emits_error_and_finish():+ """SECRT-2252: a ``subtype="success"`` ResultMessage with empty ``result``,+ no produced content, and ``output_tokens == 0`` is the SDK's ghost-finish+ bug. The adapter surfaces it as a ``StreamError`` *paired with*+ ``StreamFinish`` so the service-layer post-stream flow flips+ ``acc.stream_completed`` and skips the ``STOPPED_BY_USER_MARKER``+ branch. ``SystemMessage(subtype="init")`` opened a step, so the+ empty-completion branch must close it before emitting the error."""+ adapter = _adapter()+ adapter.convert_message(SystemMessage(subtype="init", data={}))+ msg = ResultMessage(+ subtype="success",+ duration_ms=100,+ duration_api_ms=50,+ is_error=False,+ num_turns=1,+ session_id="s1",+ result=None,+ usage={"input_tokens": 5, "output_tokens": 0},+ )+ results = adapter.convert_message(msg)+ types = [type(r).__name__ for r in results]+ assert "StreamFinishStep" in types+ assert "StreamError" in types+ assert "StreamFinish" in types+ # Open step must be closed before the error, and the error must+ # precede StreamFinish on the wire.+ assert types.index("StreamFinishStep") < types.index("StreamError")+ assert types.index("StreamError") < types.index("StreamFinish")+ err = next(r for r in results if isinstance(r, StreamError))+ assert err.code == "empty_completion"+++def test_result_empty_success_with_empty_string_result_treated_as_empty():+ """An empty string (not just None) for ``result`` is also empty."""+ adapter = _adapter()+ adapter.convert_message(SystemMessage(subtype="init", data={}))+ msg = ResultMessage(+ subtype="success",+ duration_ms=100,+ duration_api_ms=50,+ is_error=False,+ num_turns=1,+ session_id="s1",+ result="",+ usage={"output_tokens": 0},+ )+ results = adapter.convert_message(msg)+ err = next(r for r in results if isinstance(r, StreamError))+ assert err.code == "empty_completion"+ assert any(isinstance(r, StreamFinish) for r in results)+++def test_result_success_with_text_emits_finish_not_error():+ """Non-empty success (text was produced) keeps the existing+ ``StreamFinish`` behaviour — no spurious error."""+ adapter = _adapter()+ adapter.convert_message(+ AssistantMessage(content=[TextBlock(text="hello")], model="test")+ )+ msg = ResultMessage(+ subtype="success",+ duration_ms=100,+ duration_api_ms=50,+ is_error=False,+ num_turns=1,+ session_id="s1",+ result="hello",+ usage={"output_tokens": 5},+ )+ results = adapter.convert_message(msg)+ types = [type(r).__name__ for r in results]+ assert "StreamFinish" in types+ assert "StreamError" not in types+++def test_result_success_with_nonzero_output_tokens_not_empty():+ """If ``output_tokens > 0`` but ``result`` is empty, don't classify as+ empty — fall through to the existing success path. No prior+ AssistantMessage so the `output_tokens` guard is the only thing+ keeping `_is_empty_completion()` from firing."""+ adapter = _adapter()+ adapter.convert_message(SystemMessage(subtype="init", data={}))+ msg = ResultMessage(+ subtype="success",+ duration_ms=100,+ duration_api_ms=50,+ is_error=False,+ num_turns=1,+ session_id="s1",+ result="",+ usage={"output_tokens": 50},+ )+ results = adapter.convert_message(msg)+ types = [type(r).__name__ for r in results]+ assert "StreamFinish" in types+ assert "StreamError" not in types++def test_result_error_emits_error_and_finish():adapter = _adapter()msg = ResultMessage(diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 6ab2143825..12a67e9a43 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -193,6 +193,14 @@ _CIRCUIT_BREAKER_ERROR_MSG = (# idle of 2× that genuinely means the SDK itself is stuck._IDLE_TIMEOUT_SECONDS = STREAM_IDLE_TIMEOUT_SECONDS+# StreamError codes that should render as a retryable error in the UI (retry+# button) rather than a terminal ErrorCard. Codes appended via+# ``_append_error_marker`` directly already pass ``retryable=True``; this set+# covers the codes that flow through the adapter -> ``_dispatch_response``.+_RETRYABLE_STREAM_ERROR_CODES: frozenset[str] = frozenset(+ {"transient_api_error", "empty_completion"}+)+# Event types that are ephemeral / cosmetic and must NOT be counted toward# ``events_yielded`` in the transient-retry loop. Counting them would prevent@@ -2026,7 +2034,7 @@ def _dispatch_response(_append_error_marker(ctx.session,response.errorText,- retryable=(response.code == "transient_api_error"),+ retryable=response.code in _RETRYABLE_STREAM_ERROR_CODES,)if isinstance(response, StreamReasoningStart):