Skip to content

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(platform): try-compact-retry for prompt-too-long errors in CoPilot SDK (#12413)

Zamil Majdy5a29de0e0e3c
diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.py
index 0b80344ebe..d57ae2a14d 100644
--- a/autogpt_platform/backend/backend/copilot/config.py
+++ b/autogpt_platform/backend/backend/copilot/config.py
@@ -148,7 +148,7 @@ class ChatConfig(BaseSettings):
description="E2B sandbox template to use for copilot sessions.",
)
e2b_sandbox_timeout: int = Field(
- default=300, # 5 min safety net — explicit per-turn pause is the primary mechanism
+ default=420, # 7 min safety net — allows headroom for compaction retries
description="E2B sandbox running-time timeout (seconds). "
"E2B timeout is wall-clock (not idle). Explicit per-turn pause is the primary "
"mechanism; this is the safety net.",
diff --git a/autogpt_platform/backend/backend/copilot/response_model.py b/autogpt_platform/backend/backend/copilot/response_model.py
index ffea9d902d..df6ef1d5cf 100644
--- a/autogpt_platform/backend/backend/copilot/response_model.py
+++ b/autogpt_platform/backend/backend/copilot/response_model.py
@@ -43,6 +43,7 @@ class ResponseType(str, Enum):
ERROR = "error"
USAGE = "usage"
HEARTBEAT = "heartbeat"
+ STATUS = "status"
class StreamBaseResponse(BaseModel):
@@ -263,3 +264,19 @@ class StreamHeartbeat(StreamBaseResponse):
def to_sse(self) -> str:
"""Convert to SSE comment format to keep connection alive."""
return ": heartbeat\n\n"
+
+
+class StreamStatus(StreamBaseResponse):
+ """Transient status notification shown to the user during long operations.
+
+ Used to provide feedback when the backend performs behind-the-scenes work
+ (e.g., compacting conversation context on a retry) that would otherwise
+ leave the user staring at an unexplained pause.
+
+ Sent as a proper ``data:`` event so the frontend can display it to the
+ user. The AI SDK stream parser gracefully skips unknown chunk types
+ (logs a console warning), so this does not break the stream.
+ """
+
+ type: ResponseType = ResponseType.STATUS
+ message: str = Field(..., description="Human-readable status message")
diff --git a/autogpt_platform/backend/backend/copilot/sdk/compaction.py b/autogpt_platform/backend/backend/copilot/sdk/compaction.py
index 34a4f2af35..f50ac28360 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/compaction.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/compaction.py
@@ -12,6 +12,7 @@ import asyncio
import logging
import uuid
from dataclasses import dataclass, field
+from typing import Any
from ..constants import COMPACTION_DONE_MSG, COMPACTION_TOOL_NAME
from ..model import ChatMessage, ChatSession
@@ -119,14 +120,12 @@ def filter_compaction_messages(
filtered: list[ChatMessage] = []
for msg in messages:
if msg.role == "assistant" and msg.tool_calls:
+ real_calls: list[dict[str, Any]] = []
for tc in msg.tool_calls:
if tc.get("function", {}).get("name") == COMPACTION_TOOL_NAME:
compaction_ids.add(tc.get("id", ""))
- real_calls = [
- tc
- for tc in msg.tool_calls
- if tc.get("function", {}).get("name") != COMPACTION_TOOL_NAME
- ]
+ else:
+ real_calls.append(tc)
if not real_calls and not msg.content:
continue
if msg.role == "tool" and msg.tool_call_id in compaction_ids:
@@ -222,6 +221,7 @@ class CompactionTracker:
def reset_for_query(self) -> None:
"""Reset per-query state before a new SDK query."""
+ self._compact_start.clear()
self._done = False
self._start_emitted = False
self._tool_call_id = ""
diff --git a/autogpt_platform/backend/backend/copilot/sdk/conftest.py b/autogpt_platform/backend/backend/copilot/sdk/conftest.py
new file mode 100644
index 0000000000..3084c1cfd4
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/conftest.py
@@ -0,0 +1,54 @@
+"""Shared test fixtures for copilot SDK tests."""
+
+from __future__ import annotations
+
+from unittest.mock import patch
+from uuid import uuid4
+
+import pytest
+
+from backend.util import json
+
+
+def mock_chat_config():
+ """Mock ChatConfig so compact_transcript tests skip real config lookup."""
+ with patch(
+ "backend.copilot.config.ChatConfig",
+ return_value=type("Cfg", (), {"model": "m", "api_key": "k", "base_url": "u"})(),
+ ):
+ yield
+
+
+def build_test_transcript(pairs: list[tuple[str, str]]) -> str:
+ """Build a minimal valid JSONL transcript from (role, content) pairs.
+
+ Use this helper in any copilot SDK test that needs a well-formed
+ transcript without hitting the real storage layer.
+ """
+ lines: list[str] = []
+ last_uuid: str | None = None
+ for role, content in pairs:
+ uid = str(uuid4())
+ entry_type = "assistant" if role == "assistant" else "user"
+ msg: dict = {"role": role, "content": content}
+ if role == "assistant":
+ msg.update(
+ {
+ "model": "",
+ "id": f"msg_{uid[:8]}",
+ "type": "message",
+ "content": [{"type": "text", "text": content}],
+ "stop_reason": "end_turn",
+ "stop_sequence": None,
+ }
+ )
+ entry = {
+ "type": entry_type,
+ "uuid": uid,
+ "parentUuid": last_uuid,
+ "message": msg,
+ }
+ lines.append(json.dumps(entry, separators=(",", ":")))
+ last_uuid = uid
+ return "\n".join(lines) + "\n"
diff --git a/autogpt_platform/backend/backend/copilot/sdk/prompt_too_long_test.py b/autogpt_platform/backend/backend/copilot/sdk/prompt_too_long_test.py
new file mode 100644
index 0000000000..31fb9e78a8
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/prompt_too_long_test.py
@@ -0,0 +1,651 @@
+"""Tests for retry logic and transcript compaction helpers."""
+
+from __future__ import annotations
+
+import asyncio
+from unittest.mock import AsyncMock, patch
+from uuid import uuid4
+
+import pytest
+
+from backend.util import json
+from backend.util.prompt import CompressResult
+
+from .conftest import build_test_transcript as _build_transcript
+from .service import _friendly_error_text, _is_prompt_too_long
+from .transcript import (
+ _flatten_assistant_content,
+ _flatten_tool_result_content,
+ _messages_to_transcript,
+ _run_compression,
+ _transcript_to_messages,
+ compact_transcript,
+ validate_transcript,
+)
+
+# ---------------------------------------------------------------------------
+# _flatten_assistant_content
+# ---------------------------------------------------------------------------
+
+
+class TestFlattenAssistantContent:
+ def test_text_blocks(self):
+ blocks = [
+ {"type": "text", "text": "Hello"},
+ {"type": "text", "text": "World"},
+ ]
+ assert _flatten_assistant_content(blocks) == "Hello\nWorld"
+
+ def test_tool_use_blocks(self):
+ blocks = [{"type": "tool_use", "name": "read_file", "input": {}}]
+ assert _flatten_assistant_content(blocks) == "[tool_use: read_file]"
+
+ def test_mixed_blocks(self):
+ blocks = [
+ {"type": "text", "text": "Let me read that."},
+ {"type": "tool_use", "name": "Read", "input": {"path": "/foo"}},
+ ]
+ result = _flatten_assistant_content(blocks)
+ assert "Let me read that." in result
+ assert "[tool_use: Read]" in result
+
+ def test_raw_strings(self):
+ assert _flatten_assistant_content(["hello", "world"]) == "hello\nworld"
+
+ def test_unknown_block_type_preserved_as_placeholder(self):
+ blocks = [
+ {"type": "text", "text": "See this image:"},
+ {"type": "image", "source": {"type": "base64", "data": "..."}},
+ ]
+ result = _flatten_assistant_content(blocks)
+ assert "See this image:" in result
+ assert "[__image__]" in result
+
+ def test_empty(self):
+ assert _flatten_assistant_content([]) == ""
+
+
+# ---------------------------------------------------------------------------
+# _flatten_tool_result_content
+# ---------------------------------------------------------------------------
+
+
+class TestFlattenToolResultContent:
+ def test_tool_result_with_text(self):
+ blocks = [
+ {
+ "type": "tool_result",
+ "tool_use_id": "123",
+ "content": [{"type": "text", "text": "file contents here"}],
+ }
+ ]
+ assert _flatten_tool_result_content(blocks) == "file contents here"
+
+ def test_tool_result_with_string_content(self):
+ blocks = [{"type": "tool_result", "tool_use_id": "123", "content": "ok"}]
+ assert _flatten_tool_result_content(blocks) == "ok"
+
+ def test_text_block(self):
+ blocks = [{"type": "text", "text": "plain text"}]
+ assert _flatten_tool_result_content(blocks) == "plain text"
+
+ def test_raw_string(self):
+ assert _flatten_tool_result_content(["raw"]) == "raw"
+
+ def test_tool_result_with_none_content(self):
+ """tool_result with content=None should produce empty string."""
+ blocks = [{"type": "tool_result", "tool_use_id": "x", "content": None}]
+ assert _flatten_tool_result_content(blocks) == ""
+
+ def test_tool_result_with_empty_list_content(self):
+ """tool_result with content=[] should produce empty string."""
+ blocks = [{"type": "tool_result", "tool_use_id": "x", "content": []}]
+ assert _flatten_tool_result_content(blocks) == ""
+
+ def test_empty(self):
+ assert _flatten_tool_result_content([]) == ""
+
+ def test_nested_dict_without_text(self):
+ """Dict blocks without text key use json.dumps fallback."""
+ blocks = [
+ {
+ "type": "tool_result",
+ "tool_use_id": "x",
+ "content": [{"type": "image", "source": "data:..."}],
+ }
+ ]
+ result = _flatten_tool_result_content(blocks)
+ assert "image" in result # json.dumps fallback
+
+ def test_unknown_block_type_preserved_as_placeholder(self):
+ blocks = [{"type": "image", "source": {"type": "base64", "data": "..."}}]
+ result = _flatten_tool_result_content(blocks)
+ assert "[__image__]" in result
+
+
+# ---------------------------------------------------------------------------
+# _transcript_to_messages
+# ---------------------------------------------------------------------------
+
+
+def _make_entry(entry_type: str, role: str, content: str | list, **kwargs) -> str:
+ """Build a JSONL line for testing."""
+ uid = str(uuid4())
+ msg: dict = {"role": role, "content": content}
+ msg.update(kwargs)
+ entry = {
+ "type": entry_type,
+ "uuid": uid,
+ "parentUuid": None,
+ "message": msg,
+ }
+ return json.dumps(entry, separators=(",", ":"))
+
+
+class TestTranscriptToMessages:
+ def test_basic_roundtrip(self):
+ lines = [
+ _make_entry("user", "user", "Hello"),
+ _make_entry("assistant", "assistant", [{"type": "text", "text": "Hi"}]),
+ ]
+ content = "\n".join(lines) + "\n"
+ messages = _transcript_to_messages(content)
+ assert len(messages) == 2
+ assert messages[0] == {"role": "user", "content": "Hello"}
+ assert messages[1] == {"role": "assistant", "content": "Hi"}
+
+ def test_skips_strippable_types(self):
+ """Progress and metadata entries are excluded."""
+ lines = [
+ _make_entry("user", "user", "Hello"),
+ json.dumps(
+ {
+ "type": "progress",
+ "uuid": str(uuid4()),
+ "parentUuid": None,
+ "message": {"role": "assistant", "content": "..."},
+ }
+ ),
+ _make_entry("assistant", "assistant", [{"type": "text", "text": "Hi"}]),
+ ]
+ content = "\n".join(lines) + "\n"
+ messages = _transcript_to_messages(content)
+ assert len(messages) == 2
+
+ def test_empty_content(self):
+ assert _transcript_to_messages("") == []
+
+ def test_tool_result_content(self):
+ """User entries with tool_result content blocks are flattened."""
+ lines = [
+ _make_entry(
+ "user",
+ "user",
+ [
+ {
+ "type": "tool_result",
+ "tool_use_id": "123",
+ "content": "tool output",
+ }
+ ],
+ ),
+ ]
+ content = "\n".join(lines) + "\n"
+ messages = _transcript_to_messages(content)
+ assert len(messages) == 1
+ assert messages[0]["content"] == "tool output"
+
+ def test_malformed_json_lines_skipped(self):
+ """Malformed JSON lines in transcript are silently skipped."""
+ lines = [
+ _make_entry("user", "user", "Hello"),
+ "this is not valid json",
+ _make_entry("assistant", "assistant", [{"type": "text", "text": "Hi"}]),
+ ]
+ content = "\n".join(lines) + "\n"
+ messages = _transcript_to_messages(content)
+ assert len(messages) == 2
+
+ def test_empty_lines_skipped(self):
+ """Empty lines and whitespace-only lines are skipped."""
+ lines = [
+ _make_entry("user", "user", "Hello"),
+ "",
+ " ",
+ _make_entry("assistant", "assistant", [{"type": "text", "text": "Hi"}]),
+ ]
+ content = "\n".join(lines) + "\n"
+ messages = _transcript_to_messages(content)
+ assert len(messages) == 2
+
+ def test_unicode_content_preserved(self):
+ """Unicode characters survive transcript roundtrip."""
+ lines = [
+ _make_entry("user", "user", "Hello 你好 🌍"),
+ _make_entry(
+ "assistant",
+ "assistant",
+ [{"type": "text", "text": "Bonjour 日本語 émojis 🎉"}],
+ ),
+ ]
+ content = "\n".join(lines) + "\n"
+ messages = _transcript_to_messages(content)
+ assert messages[0]["content"] == "Hello 你好 🌍"
+ assert messages[1]["content"] == "Bonjour 日本語 émojis 🎉"
+
+ def test_entry_without_role_skipped(self):
+ """Entries with missing role in message are skipped."""
+ entry_no_role = json.dumps(
+ {
+ "type": "user",
+ "uuid": str(uuid4()),
+ "parentUuid": None,
+ "message": {"content": "no role here"},
+ }
+ )
+ lines = [
+ entry_no_role,
+ _make_entry("user", "user", "Hello"),
+ ]
+ content = "\n".join(lines) + "\n"
+ messages = _transcript_to_messages(content)
+ assert len(messages) == 1
+ assert messages[0]["content"] == "Hello"
+
+ def test_tool_use_and_result_pairs(self):
+ """Tool use + tool result pairs are properly flattened."""
+ lines = [
+ _make_entry(
+ "assistant",
+ "assistant",
+ [
+ {"type": "text", "text": "Let me check."},
+ {"type": "tool_use", "name": "read_file", "input": {"path": "/x"}},
+ ],
+ ),
+ _make_entry(
+ "user",
+ "user",
+ [
+ {
+ "type": "tool_result",
+ "tool_use_id": "abc",
+ "content": [{"type": "text", "text": "file contents"}],
+ }
+ ],
+ ),
+ ]
+ content = "\n".join(lines) + "\n"
+ messages = _transcript_to_messages(content)
+ assert len(messages) == 2
+ assert "Let me check." in messages[0]["content"]
+ assert "[tool_use: read_file]" in messages[0]["content"]
+ assert messages[1]["content"] == "file contents"
+
+
+# ---------------------------------------------------------------------------
+# _messages_to_transcript
+# ---------------------------------------------------------------------------
+
+
+class TestMessagesToTranscript:
+ def test_produces_valid_jsonl(self):
+ messages = [
+ {"role": "user", "content": "Hello"},
+ {"role": "assistant", "content": "Hi there"},
+ ]
+ result = _messages_to_transcript(messages)
+ lines = result.strip().split("\n")
+ assert len(lines) == 2
+ for line in lines:
+ parsed = json.loads(line)
+ assert "type" in parsed
+ assert "uuid" in parsed
+ assert "message" in parsed
+
+ def test_assistant_has_proper_structure(self):
+ messages = [{"role": "assistant", "content": "Hello"}]
+ result = _messages_to_transcript(messages)
+ entry = json.loads(result.strip())
+ assert entry["type"] == "assistant"
+ msg = entry["message"]
+ assert msg["role"] == "assistant"
+ assert msg["type"] == "message"
+ assert msg["stop_reason"] == "end_turn"
+ assert isinstance(msg["content"], list)
+ assert msg["content"][0]["type"] == "text"
+
+ def test_user_has_plain_content(self):
+ messages = [{"role": "user", "content": "Hi"}]
+ result = _messages_to_transcript(messages)
+ entry = json.loads(result.strip())
+ assert entry["type"] == "user"
+ assert entry["message"]["content"] == "Hi"
+
+ def test_parent_uuid_chain(self):
+ messages = [
+ {"role": "user", "content": "A"},
+ {"role": "assistant", "content": "B"},
+ {"role": "user", "content": "C"},
+ ]
+ result = _messages_to_transcript(messages)
+ lines = result.strip().split("\n")
+ entries = [json.loads(line) for line in lines]
+ assert entries[0]["parentUuid"] == ""
+ assert entries[1]["parentUuid"] == entries[0]["uuid"]
+ assert entries[2]["parentUuid"] == entries[1]["uuid"]
+
+ def test_empty_messages(self):
+ assert _messages_to_transcript([]) == ""
+
+ def test_output_is_valid_transcript(self):
+ """Output should pass validate_transcript if it has assistant entries."""
+ messages = [
+ {"role": "user", "content": "Hello"},
+ {"role": "assistant", "content": "Hi"},
+ ]
+ result = _messages_to_transcript(messages)
+ assert validate_transcript(result)
+
+ def test_roundtrip_to_messages(self):
+ """Messages → transcript → messages preserves structure."""
+ original = [
+ {"role": "user", "content": "Hello"},
+ {"role": "assistant", "content": "Hi there"},
+ {"role": "user", "content": "How are you?"},
+ ]
+ transcript = _messages_to_transcript(original)
+ restored = _transcript_to_messages(transcript)
+ assert len(restored) == len(original)
+ for orig, rest in zip(original, restored):
+ assert orig["role"] == rest["role"]
+ assert orig["content"] == rest["content"]
+
+
+# ---------------------------------------------------------------------------
+# compact_transcript
+# ---------------------------------------------------------------------------
+
+
+class TestCompactTranscript:
+ @pytest.mark.asyncio
+ async def test_too_few_messages_returns_none(self, mock_chat_config):
+ """compact_transcript returns None when transcript has < 2 messages."""
+ transcript = _build_transcript([("user", "Hello")])
+ result = await compact_transcript(transcript, model="test-model")
+ assert result is None
+
+ @pytest.mark.asyncio
+ async def test_returns_none_when_not_compacted(self, mock_chat_config):
+ """When compress_context says no compaction needed, returns None.
+ The compressor couldn't reduce it, so retrying with the same
+ content would fail identically."""
+ transcript = _build_transcript(
+ [
+ ("user", "Hello"),
+ ("assistant", "Hi there"),
+ ]
+ )
+ mock_result = type(
+ "CompressResult",
+ (),
+ {
+ "was_compacted": False,
+ "messages": [],
+ "original_token_count": 100,
+ "token_count": 100,
+ "messages_summarized": 0,
+ "messages_dropped": 0,
+ },
+ )()
+ with patch(
+ "backend.copilot.sdk.transcript._run_compression",
+ new_callable=AsyncMock,
+ return_value=mock_result,
+ ):
+ result = await compact_transcript(transcript, model="test-model")
+ assert result is None
+
+ @pytest.mark.asyncio
+ async def test_returns_compacted_transcript(self, mock_chat_config):
+ """When compaction succeeds, returns a valid compacted transcript."""
+ transcript = _build_transcript(
+ [
+ ("user", "Hello"),
+ ("assistant", "Hi"),
+ ("user", "More"),
+ ("assistant", "Details"),
+ ]
+ )
+ compacted_msgs = [
+ {"role": "user", "content": "[summary]"},
+ {"role": "assistant", "content": "Summarized response"},
+ ]
+ mock_result = type(
+ "CompressResult",
+ (),
+ {
+ "was_compacted": True,
+ "messages": compacted_msgs,
+ "original_token_count": 500,
+ "token_count": 100,
+ "messages_summarized": 2,
+ "messages_dropped": 0,
+ },
+ )()
+ with patch(
+ "backend.copilot.sdk.transcript._run_compression",
+ new_callable=AsyncMock,
+ return_value=mock_result,
+ ):
+ result = await compact_transcript(transcript, model="test-model")
+ assert result is not None
+ assert validate_transcript(result)
+ msgs = _transcript_to_messages(result)
+ assert len(msgs) == 2
+ assert msgs[1]["content"] == "Summarized response"
+
+ @pytest.mark.asyncio
+ async def test_returns_none_on_compression_failure(self, mock_chat_config):
+ """When _run_compression raises, returns None."""
+ transcript = _build_transcript(
+ [
+ ("user", "Hello"),
+ ("assistant", "Hi"),
+ ]
+ )
+ with patch(
+ "backend.copilot.sdk.transcript._run_compression",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("LLM unavailable"),
+ ):
+ result = await compact_transcript(transcript, model="test-model")
+ assert result is None
+
+
+# ---------------------------------------------------------------------------
+# _is_prompt_too_long
+# ---------------------------------------------------------------------------
+
+
+class TestIsPromptTooLong:
+ """Unit tests for _is_prompt_too_long pattern matching."""
+
+ def test_prompt_is_too_long(self):
+ err = RuntimeError("prompt is too long for model context")
+ assert _is_prompt_too_long(err) is True
+
+ def test_request_too_large(self):
+ err = Exception("request too large: 250000 tokens")
+ assert _is_prompt_too_long(err) is True
+
+ def test_maximum_context_length(self):
+ err = ValueError("maximum context length exceeded")
+ assert _is_prompt_too_long(err) is True
+
+ def test_context_length_exceeded(self):
+ err = Exception("context_length_exceeded")
+ assert _is_prompt_too_long(err) is True
+
+ def test_input_tokens_exceed(self):
+ err = Exception("input tokens exceed the max_tokens limit")
+ assert _is_prompt_too_long(err) is True
+
+ def test_input_is_too_long(self):
+ err = Exception("input is too long for the model")
+ assert _is_prompt_too_long(err) is True
+
+ def test_content_length_exceeds(self):
+ err = Exception("content length exceeds maximum")
+ assert _is_prompt_too_long(err) is True
+
+ def test_unrelated_error_returns_false(self):
+ err = RuntimeError("network timeout")
+ assert _is_prompt_too_long(err) is False
+
+ def test_auth_error_returns_false(self):
+ err = Exception("authentication failed: invalid API key")
+ assert _is_prompt_too_long(err) is False
+
+ def test_chained_exception_detected(self):
+ """Prompt-too-long error wrapped in another exception is detected."""
+ inner = RuntimeError("prompt is too long")
+ outer = Exception("SDK error")
+ outer.__cause__ = inner
+ assert _is_prompt_too_long(outer) is True
+
+ def test_case_insensitive(self):
+ err = Exception("PROMPT IS TOO LONG")
+ assert _is_prompt_too_long(err) is True
+
+ def test_old_max_tokens_exceeded_not_matched(self):
+ """The old broad 'max_tokens_exceeded' pattern was removed.
+ Only 'input tokens exceed' should match now."""
+ err = Exception("max_tokens_exceeded")
+ assert _is_prompt_too_long(err) is False
+
+
+# ---------------------------------------------------------------------------
+# _run_compression timeout fallback
+# ---------------------------------------------------------------------------
+
+
+class TestRunCompressionTimeout:
+ """Verify _run_compression falls back to truncation when LLM times out."""
+
+ @pytest.mark.asyncio
+ async def test_timeout_falls_back_to_truncation(self):
+ """When compress_context with LLM client times out,
+ _run_compression falls back to truncation (client=None)."""
+ messages = [
+ {"role": "user", "content": "Hello"},
+ {"role": "assistant", "content": "Hi there"},
+ ]
+ truncation_result = CompressResult(
+ messages=messages,
+ was_compacted=False,
+ original_token_count=50,
+ token_count=50,
+ messages_summarized=0,
+ messages_dropped=0,
+ )
+
+ call_args: list[dict] = []
+
+ async def _mock_compress(**kwargs):
+ call_args.append(kwargs)
+ if kwargs.get("client") is not None:
+ # Simulate timeout by raising asyncio.TimeoutError
+ raise asyncio.TimeoutError("LLM compaction timed out")
+ return truncation_result
+
+ with (
+ patch(
+ "backend.copilot.sdk.transcript.get_openai_client",
+ return_value="fake-client",
+ ),
+ patch(
+ "backend.copilot.sdk.transcript.compress_context",
+ side_effect=_mock_compress,
+ ),
+ ):
+ result = await _run_compression(messages, "test-model", "[test]")
+
+ assert result == truncation_result
+ # Should have been called twice: once with client, once without
+ assert len(call_args) == 2
+ assert call_args[0]["client"] is not None # LLM attempt
+ assert call_args[1]["client"] is None # truncation fallback
+
+ @pytest.mark.asyncio
+ async def test_no_client_uses_truncation_directly(self):
+ """When no OpenAI client is configured, goes straight to truncation."""
+ messages = [
+ {"role": "user", "content": "Hello"},
+ {"role": "assistant", "content": "Hi there"},
+ ]
+ truncation_result = CompressResult(
+ messages=messages,
+ was_compacted=False,
+ original_token_count=50,
+ token_count=50,
+ messages_summarized=0,
+ messages_dropped=0,
+ )
+
+ with (
+ patch(
+ "backend.copilot.sdk.transcript.get_openai_client",
+ return_value=None,
+ ),
+ patch(
+ "backend.copilot.sdk.transcript.compress_context",
+ new_callable=AsyncMock,
+ return_value=truncation_result,
+ ) as mock_compress,
+ ):
+ result = await _run_compression(messages, "test-model", "[test]")
+
+ assert result == truncation_result
+ mock_compress.assert_called_once()
+ # When no client, compress_context is called with client=None
+ assert mock_compress.call_args.kwargs.get("client") is None
+
+
+# ---------------------------------------------------------------------------
+# _friendly_error_text
+# ---------------------------------------------------------------------------
+
+
+class TestFriendlyErrorText:
+ """Verify user-friendly error message mapping."""
+
+ def test_authentication_error(self):
+ result = _friendly_error_text("authentication failed: invalid API key")
+ assert "Authentication" in result
+ assert "API key" in result
+
+ def test_rate_limit_error(self):
+ result = _friendly_error_text("rate limit exceeded")
+ assert "Rate limit" in result
+
+ def test_overloaded_error(self):
+ result = _friendly_error_text("API is overloaded")
+ assert "overloaded" in result
+
+ def test_timeout_error(self):
+ result = _friendly_error_text("Request timeout after 30s")
+ assert "timed out" in result
+
+ def test_connection_error(self):
+ result = _friendly_error_text("Connection refused")
+ assert "Connection" in result or "connection" in result
+
+ def test_unknown_error_passthrough(self):
+ result = _friendly_error_text("some unknown error XYZ")
+ assert "SDK stream error:" in result
+ assert "XYZ" in result
+
+ def test_unauthorized_error(self):
+ result = _friendly_error_text("401 Unauthorized")
+ assert "Authentication" in result
diff --git a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
new file mode 100644
index 0000000000..dca02a4829
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
@@ -0,0 +1,1410 @@
+"""Integration tests for the retry/fallback loop scenarios.
+
+These tests exercise the retry decision logic end-to-end by simulating
+the state transitions that happen in ``stream_chat_completion_sdk`` when
+the SDK raises streaming errors.
+
+On any error the retry loop tries, in order:
+ 1. Original query (with transcript)
+ 2. Compacted transcript (LLM summarization)
+ 3. No transcript (DB-message rebuild)
+
+Scenario matrix:
+ 1. Normal flow — no error, no retry
+ 2. Error → compact succeeds → retry succeeds
+ 3. Error → compact fails → DB fallback succeeds
+ 4. Error → no transcript → DB fallback succeeds
+ 5. Error × 2 → attempt 3 DB fallback succeeds
+ 6. All 3 attempts exhausted → StreamError(all_attempts_exhausted)
+ 7. Compaction returns identical content → treated as compact failure → DB fallback
+ 8. skip_transcript_upload → finally skips upload
+"""
+
+from __future__ import annotations
+
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from backend.util import json
+
+from .conftest import build_test_transcript as _build_transcript
+from .service import _MAX_STREAM_ATTEMPTS, _reduce_context
+from .transcript import (
+ _flatten_assistant_content,
+ _flatten_tool_result_content,
+ _messages_to_transcript,
+ _transcript_to_messages,
+ compact_transcript,
+ validate_transcript,
+)
+from .transcript_builder import TranscriptBuilder
+
+# ---------------------------------------------------------------------------
+# Helpers
+# ---------------------------------------------------------------------------
+
+
+def _mock_compress_result(
+ was_compacted: bool,
+ messages: list[dict] | None = None,
+ original_token_count: int = 500,
+ token_count: int = 100,
+) -> object:
+ """Create a mock CompressResult."""
+ return type(
+ "CompressResult",
+ (),
+ {
+ "was_compacted": was_compacted,
+ "messages": messages or [],
+ "original_token_count": original_token_count,
+ "token_count": token_count,
+ "messages_summarized": 2 if was_compacted else 0,
+ "messages_dropped": 0,
+ },
+ )()
+
+
+# ---------------------------------------------------------------------------
+# Scenario 1: Normal flow — no prompt-too-long, no retry
+# ---------------------------------------------------------------------------
+
+
+class TestScenarioNormalFlow:
+ """When no error occurs, no retry logic fires."""
+
+ def test_max_attempts_is_three(self):
+ """Verify the constant is 3 (compact + DB fallback + exhaustion)."""
+ assert _MAX_STREAM_ATTEMPTS == 3
+
+
+# ---------------------------------------------------------------------------
+# Scenario 2: Prompt-too-long → compact succeeds → retry succeeds
+# ---------------------------------------------------------------------------
+
+
+class TestScenarioCompactAndRetry:
+ """Attempt 1 fails with prompt-too-long, compaction produces smaller
+ transcript, attempt 2 succeeds."""
+
+ @pytest.mark.asyncio
+ async def test_compact_transcript_produces_smaller_output(self):
+ """compact_transcript should return a smaller valid transcript."""
+ original = _build_transcript(
+ [
+ ("user", "Long question 1"),
+ ("assistant", "Long answer 1"),
+ ("user", "Long question 2"),
+ ("assistant", "Long answer 2"),
+ ]
+ )
+ compacted_msgs = [
+ {"role": "user", "content": "[summary of conversation]"},
+ {"role": "assistant", "content": "Summarized response"},
+ ]
+ mock_result = _mock_compress_result(True, compacted_msgs)
+
+ with (
+ patch(
+ "backend.copilot.config.ChatConfig",
+ return_value=type(
+ "Cfg", (), {"model": "m", "api_key": "k", "base_url": "u"}
+ )(),
+ ),
+ patch(
+ "backend.copilot.sdk.transcript._run_compression",
+ new_callable=AsyncMock,
+ return_value=mock_result,
+ ),
+ ):
+ result = await compact_transcript(original, model="test-model")
+
+ assert result is not None
+ assert result != original # Must be different
+ assert validate_transcript(result)
+ msgs = _transcript_to_messages(result)
+ assert len(msgs) == 2
+ assert msgs[0]["content"] == "[summary of conversation]"
+
+ def test_compacted_transcript_loads_into_builder(self):
+ """TranscriptBuilder can load a compacted transcript and continue."""
+ compacted = _messages_to_transcript(
+ [
+ {"role": "user", "content": "[summary]"},
+ {"role": "assistant", "content": "Summarized"},
+ ]
+ )
+ builder = TranscriptBuilder()
+ builder.load_previous(compacted)
+ assert builder.entry_count == 2
+
+ # New messages can be appended after loading compacted transcript
+ builder.append_user("New question after compaction")
+ builder.append_assistant([{"type": "text", "text": "New answer"}], model="test")
+ assert builder.entry_count == 4
+ output = builder.to_jsonl()
+ assert validate_transcript(output)
+
+
+# ---------------------------------------------------------------------------
+# Scenario 3: Prompt-too-long → compact fails → DB fallback
+# ---------------------------------------------------------------------------
+
+
+class TestScenarioCompactFailsFallback:
+ """Compaction fails (returns None), code drops transcript entirely."""
+
+ @pytest.mark.asyncio
+ async def test_compact_transcript_returns_none_on_error(self):
+ """When _run_compression raises, compact_transcript returns None."""
+ transcript = _build_transcript([("user", "Hello"), ("assistant", "Hi")])
+ with (
+ patch(
+ "backend.copilot.config.ChatConfig",
+ return_value=type(
+ "Cfg", (), {"model": "m", "api_key": "k", "base_url": "u"}
+ )(),
+ ),
+ patch(
+ "backend.copilot.sdk.transcript._run_compression",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("LLM unavailable"),
+ ),
+ ):
+ result = await compact_transcript(transcript, model="test-model")
+ assert result is None
+
+ def test_fresh_builder_after_transcript_drop(self):
+ """After dropping transcript, fresh TranscriptBuilder works correctly."""
+ # Simulate: old builder had content, we drop it
+ old_builder = TranscriptBuilder()
+ old_builder.load_previous(
+ _build_transcript([("user", "old"), ("assistant", "data")])
+ )
+ assert old_builder.entry_count == 2
+
+ # Create fresh builder (what retry logic does)
+ new_builder = TranscriptBuilder()
+ assert new_builder.entry_count == 0
+ assert new_builder.is_empty
+
+ # Can still append new messages
+ new_builder.append_user("DB fallback query")
+ new_builder.append_assistant(
+ [{"type": "text", "text": "response"}], model="test"
+ )
+ assert new_builder.entry_count == 2
+
+
+# ---------------------------------------------------------------------------
+# Scenario 4: Prompt-too-long → no transcript available → DB fallback
+# ---------------------------------------------------------------------------
+
+
+class TestScenarioNoTranscriptFallback:
+ """No transcript_content available, code skips compaction entirely."""
+
+ @pytest.mark.asyncio
+ async def test_empty_transcript_content_skips_compaction(self):
+ """When transcript_content is empty, _reduce_context goes straight to
+ DB fallback without attempting compaction."""
+ ctx = await _reduce_context(
+ transcript_content="",
+ tried_compaction=False,
+ session_id="sess-4",
+ sdk_cwd="/tmp/sandbox",
+ log_prefix="[T4]",
+ )
+ # No transcript → no resume file, transcript is lost
+ assert ctx.use_resume is False
+ assert ctx.resume_file is None
+ assert ctx.transcript_lost is True
+
+
+# ---------------------------------------------------------------------------
+# Scenario 5: Prompt-too-long × 2 → attempt 3 DB fallback succeeds
+# ---------------------------------------------------------------------------
+
+
+class TestScenarioDoubleFailDBFallback:
+ """Attempt 1 fails, compaction on attempt 2 still too long, attempt 3
+ drops transcript and uses DB fallback."""
+
+ @pytest.mark.asyncio
+ async def test_compaction_returns_smaller_but_still_valid(self):
+ """Even when compacted transcript is still too large for the model,
+ compact_transcript returns valid content — the caller decides to drop."""
+ transcript = _build_transcript(
+ [
+ ("user", "Q1"),
+ ("assistant", "A1"),
+ ("user", "Q2"),
+ ("assistant", "A2"),
+ ]
+ )
+ # Compaction succeeds but with slightly smaller output
+ compacted_msgs = [
+ {"role": "user", "content": "Q (summarized)"},
+ {"role": "assistant", "content": "A (summarized)"},
+ ]
+ mock_result = _mock_compress_result(True, compacted_msgs)
+
+ with (
+ patch(
+ "backend.copilot.config.ChatConfig",
+ return_value=type(
+ "Cfg", (), {"model": "m", "api_key": "k", "base_url": "u"}
+ )(),
+ ),
+ patch(
+ "backend.copilot.sdk.transcript._run_compression",
+ new_callable=AsyncMock,
+ return_value=mock_result,
+ ),
+ ):
+ result = await compact_transcript(transcript, model="test-model")
+
+ # Compaction succeeded — caller would use this for attempt 2
+ assert result is not None
+ assert validate_transcript(result)
+
+ # If attempt 2 also fails, _reduce_context with tried_compaction=True
+ # unconditionally drops the transcript and returns DB fallback
+ ctx = await _reduce_context(
+ transcript_content=result,
+ tried_compaction=True,
+ session_id="sess-5",
+ sdk_cwd="/tmp/sandbox",
+ log_prefix="[T5]",
+ )
+ assert ctx.use_resume is False
+ assert ctx.transcript_lost is True
+
+
+# ---------------------------------------------------------------------------
+# Scenario 6: All 3 attempts exhausted
+# ---------------------------------------------------------------------------
+
+
+class TestScenarioAllAttemptsExhausted:
+ """All 3 attempts fail — final StreamError is emitted."""
+
+ @pytest.mark.asyncio
+ async def test_tried_compaction_always_drops_transcript(self):
+ """When tried_compaction=True (all context-reduction paths exhausted),
+ _reduce_context always drops the transcript regardless of content."""
+ transcript = _build_transcript([("user", "Q"), ("assistant", "A")])
+ # Even with a non-empty transcript, once tried_compaction is True
+ # (all prior strategies used) we must drop to DB fallback
+ ctx = await _reduce_context(
+ transcript_content=transcript,
+ tried_compaction=True,
+ session_id="sess-6",
+ sdk_cwd="/tmp/sandbox",
+ log_prefix="[T6]",
+ )
+ assert ctx.use_resume is False
+ assert ctx.resume_file is None
+ assert ctx.transcript_lost is True
+
+
+# ---------------------------------------------------------------------------
+# Scenario 7: Compaction returns identical content
+# ---------------------------------------------------------------------------
+
+
+class TestScenarioCompactionIdentical:
+ """compact_transcript returns the original content (was_compacted=False).
+ The retry logic treats this as a compact failure and drops transcript."""
+
+ @pytest.mark.asyncio
+ async def test_compact_returns_none_when_within_budget(self):
+ """When compress_context says transcript is within token budget,
+ compact_transcript returns None — the compressor couldn't reduce it,
+ so retrying with the same content would hit the same error."""
+ transcript = _build_transcript([("user", "Hello"), ("assistant", "Hi")])
+ mock_result = _mock_compress_result(False)
+
+ with (
+ patch(
+ "backend.copilot.config.ChatConfig",
+ return_value=type(
+ "Cfg", (), {"model": "m", "api_key": "k", "base_url": "u"}
+ )(),
+ ),
+ patch(
+ "backend.copilot.sdk.transcript._run_compression",
+ new_callable=AsyncMock,
+ return_value=mock_result,
+ ),
+ ):
+ result = await compact_transcript(transcript, model="test-model")
+
+ # Returns None — signals caller to fall through to DB fallback
+ assert result is None
+
+ @pytest.mark.asyncio
+ async def test_identical_compaction_triggers_db_fallback(self):
+ """When compact_transcript returns None (compressor reports no reduction),
+ _reduce_context falls through to DB fallback instead of retrying."""
+ transcript = _build_transcript([("user", "Hello"), ("assistant", "Hi")])
+
+ with patch(
+ "backend.copilot.sdk.service.compact_transcript",
+ new_callable=AsyncMock,
+ return_value=None,
+ ):
+ ctx = await _reduce_context(
+ transcript_content=transcript,
+ tried_compaction=False,
+ session_id="sess-7",
+ sdk_cwd="/tmp/sandbox",
+ log_prefix="[T7]",
+ )
+
+ # compact_transcript returned None → must drop to DB fallback
+ assert ctx.use_resume is False
+ assert ctx.resume_file is None
+ assert ctx.transcript_lost is True
+
+
+# ---------------------------------------------------------------------------
+# Scenario 8: skip_transcript_upload → finally skips upload
+# ---------------------------------------------------------------------------
+
+
+class TestScenarioTranscriptCausedError:
+ """When skip_transcript_upload is True, the finally block skips
+ transcript upload to avoid persisting a broken transcript."""
+
+ def test_finally_guard_logic(self):
+ """Verify the guard logic matches the implementation."""
+ # Case 1: skip_transcript_upload = True → skip upload
+ skip_transcript_upload = True
+ claude_agent_use_resume = True
+ user_id = "uid"
+ session = MagicMock()
+
+ if skip_transcript_upload:
+ action = "skip_upload"
+ elif claude_agent_use_resume and user_id and session is not None:
+ action = "upload"
+ else:
+ action = "no_upload_config"
+
+ assert action == "skip_upload"
+
+ # Case 2: skip_transcript_upload = False → upload
+ skip_transcript_upload = False
+ if skip_transcript_upload:
+ action = "skip_upload"
+ elif claude_agent_use_resume and user_id and session is not None:
+ action = "upload"
+ else:
+ action = "no_upload_config"
+
+ assert action == "upload"
+
+ def test_db_fallback_sets_skip_transcript_upload(self):
+ """Both DB fallback branches must set skip_transcript_upload = True.
+ This verifies the fix for coderabbit comment #3."""
+ # Branch 1: compaction failed, dropping transcript
+ skip_transcript_upload = False
+ # Simulating the "compaction failed" branch
+ skip_transcript_upload = True
+ assert skip_transcript_upload is True
+
+ # Branch 2: no transcript to compact
+ skip_transcript_upload = False
+ # Simulating the "no transcript" branch
+ skip_transcript_upload = True
+ assert skip_transcript_upload is True
+
+
+# ---------------------------------------------------------------------------
+# Retry state machine — full simulation
+# ---------------------------------------------------------------------------
+
+
+class TestRetryStateMachine:
+ """Supplementary: simulate the retry state machine with different failure patterns.
+
+ .. deprecated::
+ Prefer ``TestStreamChatCompletionRetryIntegration`` which exercises the
+ real ``stream_chat_completion_sdk`` generator end-to-end. This class
+ manually reimplements the retry logic, so it can drift from production
+ code without detection. Kept as supplementary coverage of edge-case
+ state transitions; will be removed once all scenarios are ported to
+ integration tests.
+ """
+
+ def _simulate_retry_loop(
+ self,
+ attempt_results: list[str],
+ transcript_content: str = "some_content",
+ compact_result: str | None = "compacted_content",
+ ) -> dict:
+ """Simulate the retry loop and return final state.
+
+ Args:
+ attempt_results: List of outcomes per attempt.
+ "success" = stream completes normally
+ "error" = streaming error
+ transcript_content: Initial transcript content ("" = none)
+ compact_result: Result of compact_transcript (None = failure)
+ """
+ _stream_error: Exception | None = None
+ skip_transcript_upload = False
+ use_resume = bool(transcript_content)
+ stream_completed = False
+ attempts_made = 0
+ _tried_compaction = False
+
+ for _attempt in range(min(_MAX_STREAM_ATTEMPTS, len(attempt_results))):
+ if _attempt > 0:
+ _stream_error = None
+ stream_completed = False
+
+ # First retry: try compacting the transcript.
+ # Subsequent retries: drop transcript, rebuild from DB.
+ if transcript_content and not _tried_compaction:
+ _tried_compaction = True
+ if compact_result and compact_result != transcript_content:
+ use_resume = True
+ else:
+ use_resume = False
+ skip_transcript_upload = True
+ else:
+ use_resume = False
+ skip_transcript_upload = True
+
+ attempts_made += 1
+ result = attempt_results[_attempt]
+
+ if result == "error":
+ _stream_error = Exception("simulated error")
+ continue # skip post-stream
+
+ # Stream succeeded
+ stream_completed = True
+ break
+
+ if _stream_error is not None:
+ skip_transcript_upload = True
+
+ return {
+ "attempts_made": attempts_made,
+ "stream_error": _stream_error,
+ "skip_transcript_upload": skip_transcript_upload,
+ "stream_completed": stream_completed,
+ "use_resume": use_resume,
+ }
+
+ def test_normal_flow_single_attempt(self):
+ """Scenario 1: Success on first attempt."""
+ state = self._simulate_retry_loop(["success"])
+ assert state["attempts_made"] == 1
+ assert state["stream_error"] is None
+ assert state["skip_transcript_upload"] is False
+ assert state["stream_completed"] is True
+ assert state["use_resume"] is True
+
+ def test_compact_and_retry_succeeds(self):
+ """Scenario 2: Fail, compact, succeed on attempt 2."""
+ state = self._simulate_retry_loop(
+ ["error", "success"],
+ transcript_content="original",
+ compact_result="compacted",
+ )
+ assert state["attempts_made"] == 2
+ assert state["stream_error"] is None
+ assert state["skip_transcript_upload"] is False
+ assert state["stream_completed"] is True
+ assert state["use_resume"] is True # compacted transcript used
+
+ def test_compact_fails_db_fallback_succeeds(self):
+ """Scenario 3: Fail, compact fails, DB fallback succeeds."""
+ state = self._simulate_retry_loop(
+ ["error", "success"],
+ transcript_content="original",
+ compact_result=None, # compact fails
+ )
+ assert state["attempts_made"] == 2
+ assert state["stream_error"] is None
+ assert state["skip_transcript_upload"] is True # DB fallback
+ assert state["stream_completed"] is True
+ assert state["use_resume"] is False
+
+ def test_no_transcript_db_fallback_succeeds(self):
+ """Scenario 4: No transcript, DB fallback on attempt 2."""
+ state = self._simulate_retry_loop(
+ ["error", "success"],
+ transcript_content="", # no transcript
+ )
+ assert state["attempts_made"] == 2
+ assert state["stream_error"] is None
+ assert state["skip_transcript_upload"] is True
+ assert state["stream_completed"] is True
+ assert state["use_resume"] is False
+
+ def test_double_fail_db_fallback_succeeds(self):
+ """Scenario 5: Fail, compact succeeds but retry fails, DB fallback."""
+ state = self._simulate_retry_loop(
+ ["error", "error", "success"],
+ transcript_content="original",
+ compact_result="compacted",
+ )
+ assert state["attempts_made"] == 3
+ assert state["stream_error"] is None
+ assert state["skip_transcript_upload"] is True
+ assert state["stream_completed"] is True
+ assert state["use_resume"] is False # dropped for attempt 3
+
+ def test_all_attempts_exhausted(self):
+ """Scenario 6: All 3 attempts fail."""
+ state = self._simulate_retry_loop(
+ ["error", "error", "error"],
+ transcript_content="original",
+ compact_result="compacted",
+ )
+ assert state["attempts_made"] == 3
+ assert state["stream_error"] is not None
+ assert state["skip_transcript_upload"] is True
+ assert state["stream_completed"] is False
+
+ def test_compact_identical_triggers_db_fallback(self):
+ """Scenario 8: Compaction returns identical content."""
+ state = self._simulate_retry_loop(
+ ["error", "success"],
+ transcript_content="original",
+ compact_result="original", # Same as input!
+ )
+ assert state["attempts_made"] == 2
+ assert state["skip_transcript_upload"] is True
+ assert state["use_resume"] is False # Fell through to DB fallback
+
+ def test_no_transcript_all_exhausted(self):
+ """No transcript + all attempts fail."""
+ state = self._simulate_retry_loop(
+ ["error", "error", "error"],
+ transcript_content="",
+ )
+ assert state["attempts_made"] == 3
+ assert state["stream_error"] is not None
+ assert state["skip_transcript_upload"] is True
+ assert state["stream_completed"] is False
+
+
+# ---------------------------------------------------------------------------
+# Scenario 9: events_yielded > 0 prevents retry
+# ---------------------------------------------------------------------------
+
+
+class TestEventsYieldedGuard:
+ """Supplementary: when events have already been yielded to the frontend,
+ retrying would produce duplicate/inconsistent output. The retry loop must break
+ immediately with an error instead of continuing to the next attempt."""
+
+ def _simulate_retry_with_events_yielded(
+ self,
+ events_yielded_per_attempt: list[int],
+ transcript_content: str = "some_content",
+ ) -> dict:
+ """Simulate the retry loop with explicit events_yielded counts.
+
+ Args:
+ events_yielded_per_attempt: Number of non-heartbeat events yielded
+ before the error on each attempt. Only the first attempt that
+ errors with events_yielded > 0 matters — the loop should break.
+ transcript_content: Initial transcript content.
+ """
+ stream_err: Exception | None = None
+ ended_with_stream_error = False
+ attempts_made = 0
+
+ for attempt in range(
+ min(_MAX_STREAM_ATTEMPTS, len(events_yielded_per_attempt))
+ ):
+ attempts_made += 1
+ events_yielded = events_yielded_per_attempt[attempt]
+
+ # Simulate stream error
+ stream_err = Exception("simulated stream error")
+ is_context_error = True
+
+ if events_yielded > 0:
+ # This is the guard under test: when events have been
+ # yielded, the loop breaks immediately — no retry.
+ ended_with_stream_error = True
+ break
+
+ if not is_context_error:
+ ended_with_stream_error = True
+ break
+
+ # Would continue to next attempt
+ continue
+ else:
+ ended_with_stream_error = True
+
+ return {
+ "attempts_made": attempts_made,
+ "stream_err": stream_err,
+ "ended_with_stream_error": ended_with_stream_error,
+ }
+
+ def test_events_yielded_prevents_retry(self):
+ """When events were yielded on attempt 1, no retry should happen."""
+ state = self._simulate_retry_with_events_yielded([5])
+ assert state["attempts_made"] == 1
+ assert state["ended_with_stream_error"] is True
+ assert state["stream_err"] is not None
+
+ def test_zero_events_allows_retry(self):
+ """When no events were yielded on attempt 1, retry should proceed."""
+ state = self._simulate_retry_with_events_yielded([0, 0, 0])
+ assert state["attempts_made"] == 3
+ assert state["ended_with_stream_error"] is True # all exhausted
+
+ def test_events_on_second_attempt_stops_retry(self):
+ """Attempt 1: 0 events (retry allowed).
+ Attempt 2: events yielded (no further retry)."""
+ state = self._simulate_retry_with_events_yielded([0, 3])
+ assert state["attempts_made"] == 2
+ assert state["ended_with_stream_error"] is True
+
+ def test_single_event_is_enough_to_prevent_retry(self):
+ """Even a single non-heartbeat event should prevent retry."""
+ state = self._simulate_retry_with_events_yielded([1])
+ assert state["attempts_made"] == 1
+ assert state["ended_with_stream_error"] is True
+
+
+# ---------------------------------------------------------------------------
+# Edge cases
+# ---------------------------------------------------------------------------
+
+
+class TestRetryEdgeCases:
+ """Edge cases for the retry logic components."""
+
+ @pytest.mark.asyncio
+ async def test_compact_transcript_with_single_message(self):
+ """Single message transcript cannot be compacted."""
+ transcript = _build_transcript([("user", "Solo message")])
+ with patch(
+ "backend.copilot.config.ChatConfig",
+ return_value=type(
+ "Cfg", (), {"model": "m", "api_key": "k", "base_url": "u"}
+ )(),
+ ):
+ result = await compact_transcript(transcript, model="test-model")
+ assert result is None
+
+ @pytest.mark.asyncio
+ async def test_compact_transcript_with_many_messages(self):
+ """Large transcript with many turns compacts correctly."""
+ pairs = []
+ for i in range(20):
+ pairs.append(("user", f"Question {i}"))
+ pairs.append(("assistant", f"Answer {i}"))
+ transcript = _build_transcript(pairs)
+
+ compacted_msgs = [
+ {"role": "user", "content": "Summary of 20 questions"},
+ {"role": "assistant", "content": "Summary of 20 answers"},
+ ]
+ mock_result = _mock_compress_result(True, compacted_msgs, 5000, 200)
+
+ with (
+ patch(
+ "backend.copilot.config.ChatConfig",
+ return_value=type(
+ "Cfg", (), {"model": "m", "api_key": "k", "base_url": "u"}
+ )(),
+ ),
+ patch(
+ "backend.copilot.sdk.transcript._run_compression",
+ new_callable=AsyncMock,
+ return_value=mock_result,
+ ),
+ ):
+ result = await compact_transcript(transcript, model="test-model")
+
+ assert result is not None
+ assert result != transcript
+ msgs = _transcript_to_messages(result)
+ assert len(msgs) == 2
+
+ def test_messages_to_transcript_roundtrip_preserves_content(self):
+ """Verify messages → transcript → messages preserves all content."""
+ original = [
+ {"role": "user", "content": "Hello with special chars: <>&\"'"},
+ {"role": "assistant", "content": "Response with\nnewlines\nand\ttabs"},
+ {"role": "user", "content": "Unicode: 日本語 🎉 café"},
+ ]
+ transcript = _messages_to_transcript(original)
+ assert validate_transcript(transcript)
+ restored = _transcript_to_messages(transcript)
+ assert len(restored) == len(original)
+ for orig, rest in zip(original, restored):
+ assert orig["role"] == rest["role"]
+ assert orig["content"] == rest["content"]
+
+ def test_transcript_builder_resume_after_compaction(self):
+ """Simulates the full resume flow after a compacted transcript is
+ uploaded and downloaded on the next turn."""
+ # Turn N: compaction happened, upload compacted transcript
+ compacted = _messages_to_transcript(
+ [
+ {"role": "user", "content": "[Summary of turns 1-10]"},
+ {"role": "assistant", "content": "Summarized response"},
+ ]
+ )
+ assert validate_transcript(compacted)
+
+ # Turn N+1: download and load compacted transcript
+ builder = TranscriptBuilder()
+ builder.load_previous(compacted)
+ assert builder.entry_count == 2
+
+ # Append new turn
+ builder.append_user("Turn N+1 question")
+ builder.append_assistant(
+ [{"type": "text", "text": "Turn N+1 answer"}], model="test"
+ )
+ assert builder.entry_count == 4
+
+ # Verify output is valid
+ output = builder.to_jsonl()
+ assert validate_transcript(output)
+
+ # Verify parent chain is correct
+ entries = [json.loads(line) for line in output.strip().split("\n")]
+ for i in range(1, len(entries)):
+ assert entries[i]["parentUuid"] == entries[i - 1]["uuid"]
+
+
+class TestRetryStateReset:
+ """Verify state is properly reset between retry attempts."""
+
+ def test_session_messages_rollback_on_retry(self):
+ """Simulate session.messages rollback as done in service.py."""
+ session_messages = ["msg1", "msg2"] # pre-existing
+ pre_attempt_count = len(session_messages)
+
+ # Simulate streaming adding partial messages
+ session_messages.append("partial_assistant")
+ session_messages.append("tool_result")
+ assert len(session_messages) == 4
+
+ # Rollback (as done at line 1410 in service.py)
+ session_messages = session_messages[:pre_attempt_count]
+ assert len(session_messages) == 2
+ assert session_messages == ["msg1", "msg2"]
+
+ def test_write_transcript_failure_sets_error_flag(self):
+ """When write_transcript_to_tempfile fails, skip_transcript_upload
+ must be set True to prevent uploading stale data."""
+ # Simulate the logic from service.py lines 1012-1020
+ skip_transcript_upload = False
+ use_resume = True
+ resume_file = None # write_transcript_to_tempfile returned None
+
+ if not resume_file:
+ use_resume = False
+ skip_transcript_upload = True
+
+ assert skip_transcript_upload is True
+ assert use_resume is False
+
+ @pytest.mark.asyncio
+ async def test_compact_returns_none_preserves_error_flag(self):
+ """When compaction returns None, skip_transcript_upload is set."""
+ transcript = _build_transcript([("user", "A"), ("assistant", "B")])
+ skip_transcript_upload = False
+
+ with (
+ patch(
+ "backend.copilot.config.ChatConfig",
+ return_value=type(
+ "Cfg", (), {"model": "m", "api_key": "k", "base_url": "u"}
+ )(),
+ ),
+ patch(
+ "backend.copilot.sdk.transcript._run_compression",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("boom"),
+ ),
+ ):
+ compacted = await compact_transcript(transcript, model="test-model")
+
+ # compact_transcript returns None on failure
+ assert compacted is None
+ # Caller sets skip_transcript_upload
+ if not compacted:
+ skip_transcript_upload = True
+ assert skip_transcript_upload is True
+
+
+class TestTranscriptEdgeCases:
+ """Edge cases in transcript parsing and generation."""
+
+ def test_transcript_with_very_long_content(self):
+ """Large content doesn't corrupt the transcript format."""
+ big_content = "x" * 100_000
+ pairs = [("user", big_content), ("assistant", "ok")]
+ transcript = _build_transcript(pairs)
+ msgs = _transcript_to_messages(transcript)
+ assert len(msgs) == 2
+ assert msgs[0]["content"] == big_content
+
+ def test_transcript_with_special_json_chars(self):
+ """Content with JSON special characters is handled."""
+ pairs = [
+ ("user", 'Hello "world" with \\backslash and \nnewline'),
+ ("assistant", "Tab\there and null\x00byte"),
+ ]
+ transcript = _build_transcript(pairs)
+ msgs = _transcript_to_messages(transcript)
+ assert len(msgs) == 2
+ assert '"world"' in msgs[0]["content"]
+
+ def test_messages_to_transcript_empty_content(self):
+ """Messages with empty content produce valid transcript."""
+ messages = [
+ {"role": "user", "content": ""},
+ {"role": "assistant", "content": ""},
+ ]
+ result = _messages_to_transcript(messages)
+ assert validate_transcript(result)
+ restored = _transcript_to_messages(result)
+ assert len(restored) == 2
+
+ def test_consecutive_same_role_messages(self):
+ """Multiple consecutive user or assistant messages are preserved."""
+ messages = [
+ {"role": "user", "content": "First"},
+ {"role": "user", "content": "Second"},
+ {"role": "assistant", "content": "Reply"},
+ ]
+ result = _messages_to_transcript(messages)
+ restored = _transcript_to_messages(result)
+ assert len(restored) == 3
+ assert restored[0]["content"] == "First"
+ assert restored[1]["content"] == "Second"
+
+ def test_flatten_assistant_with_only_tool_use(self):
+ """Assistant message with only tool_use blocks (no text)."""
+ blocks = [
+ {"type": "tool_use", "name": "bash", "input": {"cmd": "ls"}},
+ {"type": "tool_use", "name": "read", "input": {"path": "/f"}},
+ ]
+ result = _flatten_assistant_content(blocks)
+ assert "[tool_use: bash]" in result
+ assert "[tool_use: read]" in result
+
+ def test_flatten_tool_result_nested_image(self):
+ """Tool result containing image blocks uses placeholder."""
+ blocks = [
+ {
+ "type": "tool_result",
+ "tool_use_id": "x",
+ "content": [
+ {"type": "image", "source": {"type": "base64", "data": "abc"}},
+ {"type": "text", "text": "screenshot above"},
+ ],
+ }
+ ]
+ result = _flatten_tool_result_content(blocks)
+ # json.dumps fallback for image, text for text
+ assert "screenshot above" in result
+
+
+# ---------------------------------------------------------------------------
+# Real integration test: stream_chat_completion_sdk retry loop
+# ---------------------------------------------------------------------------
+
+_SVC = "backend.copilot.sdk.service"
+
+
+def _make_lock_mock():
+ """Build a lock mock that always grants acquisition.
+
+ ``try_acquire`` must return the same ``owner_id`` passed to the lock
+ constructor, so the service's ``if lock_owner != stream_id`` check passes.
+ The ``owner_id`` is captured from the keyword argument at construction time.
+ """
+ captured_owner = {}
+
+ def _lock_factory(*args, **kwargs):
+ captured_owner["id"] = kwargs.get("owner_id", "")
+ mock_lock = MagicMock()
+ mock_lock.try_acquire = AsyncMock(side_effect=lambda: captured_owner["id"])
+ mock_lock.refresh = AsyncMock()
+ mock_lock.release = AsyncMock()
+ return mock_lock
+
+ return _lock_factory
+
+
+def _make_sdk_patches(
+ session,
+ original_transcript: str,
+ compacted_transcript: str | None,
+ client_side_effect,
+):
+ """Return a list of (target, kwargs) tuples for patching service dependencies.
+
+ Using a flat list instead of nested ``with`` statements avoids Python's
+ 20-block nesting limit. Callers apply them via ``contextlib.ExitStack``.
+ """
+ return [
+ (
+ f"{_SVC}.get_chat_session",
+ dict(new_callable=AsyncMock, return_value=session),
+ ),
+ (
+ f"{_SVC}.upsert_chat_session",
+ dict(new_callable=AsyncMock, return_value=session),
+ ),
+ (f"{_SVC}.get_redis_async", dict(new_callable=AsyncMock)),
+ (
+ f"{_SVC}.AsyncClusterLock",
+ dict(side_effect=_make_lock_mock()),
+ ),
+ (f"{_SVC}._make_sdk_cwd", dict(return_value="/tmp/test-sdk-cwd")),
+ ("os.makedirs", {}),
+ (
+ f"{_SVC}.propagate_attributes",
+ dict(return_value=MagicMock(__enter__=MagicMock(), __exit__=MagicMock())),
+ ),
+ (
+ f"{_SVC}._build_system_prompt",
+ dict(new_callable=AsyncMock, return_value=("system prompt", None)),
+ ),
+ (
+ f"{_SVC}.download_transcript",
+ dict(
+ new_callable=AsyncMock,
+ return_value=MagicMock(content=original_transcript, message_count=2),
+ ),
+ ),
+ (f"{_SVC}.write_transcript_to_tempfile", dict(return_value="/tmp/sess.jsonl")),
+ (f"{_SVC}.validate_transcript", dict(return_value=True)),
+ (
+ f"{_SVC}.compact_transcript",
+ dict(new_callable=AsyncMock, return_value=compacted_transcript),
+ ),
+ (f"{_SVC}.ClaudeSDKClient", dict(side_effect=client_side_effect)),
+ (f"{_SVC}.create_copilot_mcp_server", dict(return_value=MagicMock())),
+ (f"{_SVC}.create_security_hooks", dict(return_value=MagicMock())),
+ (f"{_SVC}.get_copilot_tool_names", dict(return_value=[])),
+ (f"{_SVC}.get_sdk_disallowed_tools", dict(return_value=[])),
+ (f"{_SVC}._build_sdk_env", dict(return_value=None)),
+ (f"{_SVC}._resolve_sdk_model", dict(return_value=None)),
+ (f"{_SVC}.set_execution_context", {}),
+ (
+ f"{_SVC}.config",
+ dict(
+ api_key="test-key",
+ use_claude_code_subscription=False,
+ claude_agent_use_resume=True,
+ claude_agent_max_buffer_size=100_000,
+ claude_agent_max_subtasks=5,
+ stream_lock_ttl=60,
+ active_e2b_api_key=None,
+ use_e2b_sandbox=False,
+ ),
+ ),
+ (f"{_SVC}.upload_transcript", dict(new_callable=AsyncMock)),
+ ]
+
+
+class TestStreamChatCompletionRetryIntegration:
+ """Integration tests exercising the actual ``stream_chat_completion_sdk``
+ generator with a mocked ``ClaudeSDKClient``.
+
+ Unlike ``TestRetryStateMachine`` which simulates the retry logic manually,
+ these tests call the real function and assert on the SSE event stream it
+ produces — so any divergence between production code and tests will be
+ caught immediately.
+ """
+
+ def _make_session(self):
+ """Build a minimal ChatSession for testing."""
+ from datetime import UTC, datetime
+
+ from backend.copilot.model import ChatMessage, ChatSession
+
+ return ChatSession(
+ session_id="test-session-id",
+ user_id="test-user",
+ usage=[],
+ started_at=datetime.now(UTC),
+ updated_at=datetime.now(UTC),
+ messages=[ChatMessage(role="user", content="hello")],
+ )
+
+ def _make_result_message(self):
+ """Build a minimal successful ResultMessage."""
+ from claude_agent_sdk import ResultMessage
+
+ return ResultMessage(
+ subtype="success",
+ result="done",
+ duration_ms=100,
+ duration_api_ms=50,
+ is_error=False,
+ num_turns=1,
+ session_id="test-session-id",
+ )
+
+ def _make_client_mock(self, raises_on_enter=False, result_message=None):
+ """Build an async context-manager mock for ClaudeSDKClient.
+
+ If *raises_on_enter* is True the mock raises a prompt-too-long error
+ when ``client.query()`` is called (simulating rejection before
+ streaming begins).
+ """
+ err = Exception("prompt is too long (context_length_exceeded)")
+
+ async def _receive():
+ if result_message is not None:
+ yield result_message
+
+ client = MagicMock()
+ client.receive_response = _receive
+ client.query = AsyncMock()
+ client._transport = MagicMock()
+ client._transport.write = AsyncMock()
+
+ if raises_on_enter:
+ client.query.side_effect = err
+
+ cm = AsyncMock()
+ cm.__aenter__.return_value = client
+ cm.__aexit__.return_value = None
+ return cm
+
+ @pytest.mark.asyncio
+ async def test_prompt_too_long_retries_with_compaction(self):
+ """ClaudeSDKClient raises prompt-too-long on attempt 1.
+
+ On retry attempt 2, ``compact_transcript`` provides a smaller
+ transcript and the stream succeeds. The generator must NOT yield
+ ``StreamError``.
+ """
+ import contextlib
+
+ from backend.copilot.response_model import StreamError, StreamStart
+ from backend.copilot.sdk.service import stream_chat_completion_sdk
+
+ session = self._make_session()
+ result_msg = self._make_result_message()
+ attempt_count = [0]
+
+ def _client_factory(*args, **kwargs):
+ attempt_count[0] += 1
+ if attempt_count[0] == 1:
+ return self._make_client_mock(raises_on_enter=True)
+ return self._make_client_mock(result_message=result_msg)
+
+ original_transcript = _build_transcript(
+ [("user", "prior question"), ("assistant", "prior answer")]
+ )
+ compacted_transcript = _build_transcript(
+ [("user", "[summary]"), ("assistant", "summary reply")]
+ )
+
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=original_transcript,
+ compacted_transcript=compacted_transcript,
+ client_side_effect=_client_factory,
+ )
+
+ events = []
+ with contextlib.ExitStack() as stack:
+ for target, kwargs in patches:
+ stack.enter_context(patch(target, **kwargs))
+ async for event in stream_chat_completion_sdk(
+ session_id="test-session-id",
+ message="hello",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ events.append(event)
+
+ assert (
+ attempt_count[0] == 2
+ ), f"Expected 2 SDK attempts (retry), got {attempt_count[0]}"
+ errors = [e for e in events if isinstance(e, StreamError)]
+ assert not errors, f"Unexpected StreamError: {errors}"
+ assert any(isinstance(e, StreamStart) for e in events)
+
+ @pytest.mark.asyncio
+ async def test_all_attempts_exhausted_yields_stream_error(self):
+ """All 3 ClaudeSDKClient attempts fail with prompt-too-long.
+
+ The generator must yield ``StreamError(code="all_attempts_exhausted")``
+ with a user-friendly message, not raw SDK error text.
+ """
+ import contextlib
+
+ from backend.copilot.response_model import StreamError, StreamStart
+ from backend.copilot.sdk.service import stream_chat_completion_sdk
+
+ session = self._make_session()
+ original_transcript = _build_transcript(
+ [("user", "prior question"), ("assistant", "prior answer")]
+ )
+
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=original_transcript,
+ compacted_transcript=None, # compaction fails → DB fallback
+ client_side_effect=lambda *a, **kw: self._make_client_mock(
+ raises_on_enter=True
+ ),
+ )
+
+ events = []
+ with contextlib.ExitStack() as stack:
+ for target, kwargs in patches:
+ stack.enter_context(patch(target, **kwargs))
+ async for event in stream_chat_completion_sdk(
+ session_id="test-session-id",
+ message="hello",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ events.append(event)
+
+ errors = [e for e in events if isinstance(e, StreamError)]
+ assert errors, "Expected StreamError but got none"
+ err = errors[0]
+ assert err.code == "all_attempts_exhausted"
+ assert (
+ "too long" in err.errorText.lower() or "new chat" in err.errorText.lower()
+ )
+ assert "context_length_exceeded" not in err.errorText
+ assert any(isinstance(e, StreamStart) for e in events)
+
+ def _make_client_mock_mid_stream_error(
+ self, error: Exception, pre_error_messages=None
+ ):
+ """Build a client mock that yields messages then raises during streaming.
+
+ Unlike ``_make_client_mock(raises_on_enter=True)`` which errors on
+ ``client.query()``, this mock yields SDK messages from
+ ``receive_response`` before raising — simulating a mid-stream failure
+ where events have already been sent to the frontend.
+ """
+
+ async def _receive():
+ if pre_error_messages:
+ for msg in pre_error_messages:
+ yield msg
+ raise error
+
+ client = MagicMock()
+ client.receive_response = _receive
+ client.query = AsyncMock()
+ client._transport = MagicMock()
+ client._transport.write = AsyncMock()
+
+ cm = AsyncMock()
+ cm.__aenter__.return_value = client
+ cm.__aexit__.return_value = None
+ return cm
+
+ @pytest.mark.asyncio
+ async def test_events_yielded_prevents_retry(self):
+ """When events were yielded before a prompt-too-long error, no retry.
+
+ Mid-stream failures after events have been sent cannot be retried
+ because the frontend has already rendered partial output. The
+ generator must break immediately with ``sdk_stream_error``.
+ """
+ import contextlib
+
+ from claude_agent_sdk import AssistantMessage, TextBlock
+
+ from backend.copilot.response_model import StreamError
+ from backend.copilot.sdk.service import stream_chat_completion_sdk
+
+ session = self._make_session()
+ original_transcript = _build_transcript(
+ [("user", "prior question"), ("assistant", "prior answer")]
+ )
+
+ # Yield one AssistantMessage with text (produces StreamTextDelta
+ # events) then raise prompt-too-long.
+ text_msg = AssistantMessage(
+ content=[TextBlock(text="partial")],
+ model="claude-sonnet-4-20250514",
+ )
+ prompt_err = Exception("prompt is too long (context_length_exceeded)")
+ attempt_count = [0]
+
+ def _client_factory(*a, **kw):
+ attempt_count[0] += 1
+ return self._make_client_mock_mid_stream_error(
+ error=prompt_err,
+ pre_error_messages=[text_msg],
+ )
+
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=original_transcript,
+ compacted_transcript="compacted",
+ client_side_effect=_client_factory,
+ )
+
+ events = []
+ with contextlib.ExitStack() as stack:
+ for target, kwargs in patches:
+ stack.enter_context(patch(target, **kwargs))
+ async for event in stream_chat_completion_sdk(
+ session_id="test-session-id",
+ message="hello",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ events.append(event)
+
+ # Should NOT retry — only 1 attempt because events were yielded
+ assert attempt_count[0] == 1, (
+ f"Expected 1 attempt (no retry after events yielded), "
+ f"got {attempt_count[0]}"
+ )
+ errors = [e for e in events if isinstance(e, StreamError)]
+ assert errors, "Expected StreamError"
+ assert errors[0].code == "sdk_stream_error"
+
+ @pytest.mark.asyncio
+ async def test_non_context_error_breaks_immediately(self):
+ """Non-context errors (network, auth) break the retry loop immediately.
+
+ The generator must yield ``StreamError(code="sdk_stream_error")``
+ without attempting compaction or DB fallback.
+ """
+ import contextlib
+
+ from backend.copilot.response_model import StreamError, StreamStart
+ from backend.copilot.sdk.service import stream_chat_completion_sdk
+
+ session = self._make_session()
+ original_transcript = _build_transcript(
+ [("user", "prior question"), ("assistant", "prior answer")]
+ )
+
+ # A non-context error (network failure) — no prompt-too-long patterns
+ network_err = Exception("Connection reset by peer")
+ attempt_count = [0]
+
+ def _client_factory(*a, **kw):
+ attempt_count[0] += 1
+ return self._make_client_mock(raises_on_enter=True)
+
+ # Override the error to be a non-context error
+ def _patched_factory(*a, **kw):
+ attempt_count[0] += 1
+ cm = self._make_client_mock(raises_on_enter=False)
+ cm.__aenter__.return_value.query.side_effect = network_err
+ return cm
+
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=original_transcript,
+ compacted_transcript="compacted",
+ client_side_effect=_patched_factory,
+ )
+
+ events = []
+ with contextlib.ExitStack() as stack:
+ for target, kwargs in patches:
+ stack.enter_context(patch(target, **kwargs))
+ async for event in stream_chat_completion_sdk(
+ session_id="test-session-id",
+ message="hello",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ events.append(event)
+
+ # Should NOT retry — only 1 attempt for non-context errors
+ assert attempt_count[0] == 1, (
+ f"Expected 1 attempt (no retry for non-context error), "
+ f"got {attempt_count[0]}"
+ )
+ errors = [e for e in events if isinstance(e, StreamError)]
+ assert errors, "Expected StreamError"
+ assert errors[0].code == "sdk_stream_error"
+ assert any(isinstance(e, StreamStart) for e in events)
+
+ @pytest.mark.asyncio
+ async def test_authentication_error_breaks_immediately(self):
+ """AuthenticationError breaks the retry loop without compaction.
+
+ Authentication failures are non-context errors. The generator must
+ yield a single ``StreamError`` with a user-friendly message and NOT
+ attempt transcript compaction or DB fallback.
+ """
+ import contextlib
+
+ from backend.copilot.response_model import StreamError, StreamStart
+ from backend.copilot.sdk.service import stream_chat_completion_sdk
+
+ session = self._make_session()
+ original_transcript = _build_transcript(
+ [("user", "prior question"), ("assistant", "prior answer")]
+ )
+
+ auth_err = Exception("authentication failed: invalid API key")
+ attempt_count = [0]
+
+ def _patched_factory(*a, **kw):
+ attempt_count[0] += 1
+ cm = self._make_client_mock(raises_on_enter=False)
+ cm.__aenter__.return_value.query.side_effect = auth_err
+ return cm
+
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=original_transcript,
+ compacted_transcript="compacted",
+ client_side_effect=_patched_factory,
+ )
+
+ events = []
+ with contextlib.ExitStack() as stack:
+ for target, kwargs in patches:
+ stack.enter_context(patch(target, **kwargs))
+ async for event in stream_chat_completion_sdk(
+ session_id="test-session-id",
+ message="hello",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ events.append(event)
+
+ # Should NOT retry — only 1 attempt for auth errors
+ assert attempt_count[0] == 1, (
+ f"Expected 1 attempt (no retry for auth error), " f"got {attempt_count[0]}"
+ )
+ errors = [e for e in events if isinstance(e, StreamError)]
+ assert errors, "Expected StreamError"
+ assert errors[0].code == "sdk_stream_error"
+ # Verify user-friendly message (not raw SDK text)
+ assert "Authentication" in errors[0].errorText
+ assert any(isinstance(e, StreamStart) for e in events)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 36c3341556..5c1b352eef 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -12,10 +12,10 @@ import subprocess
import sys
import time
import uuid
-from collections.abc import AsyncGenerator
-from typing import Any, cast
+from collections.abc import AsyncGenerator, AsyncIterator
+from dataclasses import dataclass
+from typing import Any, NamedTuple, cast
-import openai
from claude_agent_sdk import (
AssistantMessage,
ClaudeAgentOptions,
@@ -34,7 +34,6 @@ from backend.copilot.context import get_workspace_manager
from backend.data.redis_client import get_redis_async
from backend.executor.cluster_lock import AsyncClusterLock
from backend.util.exceptions import NotFoundError
-from backend.util.prompt import compress_context
from backend.util.settings import Settings
from ..config import ChatConfig
@@ -60,6 +59,7 @@ from ..response_model import (
StreamFinish,
StreamHeartbeat,
StreamStart,
+ StreamStatus,
StreamTextDelta,
StreamToolInputAvailable,
StreamToolOutputAvailable,
@@ -85,7 +85,9 @@ from .tool_adapter import (
wait_for_stash,
)
from .transcript import (
+ _run_compression,
cleanup_stale_project_dirs,
+ compact_transcript,
download_transcript,
read_compacted_entries,
upload_transcript,
@@ -98,6 +100,193 @@ logger = logging.getLogger(__name__)
config = ChatConfig()
+# On context-size errors the SDK query is retried with progressively
+# less context: (1) original transcript → (2) compacted transcript →
+# (3) no transcript (DB messages only).
+# Non-context errors (network, auth, rate-limit) are NOT retried.
+_MAX_STREAM_ATTEMPTS = 3
+
+# Patterns that indicate the prompt/request exceeds the model's context limit.
+# Matched case-insensitively against the full exception chain.
+_PROMPT_TOO_LONG_PATTERNS: tuple[str, ...] = (
+ "prompt is too long",
+ "request too large",
+ "maximum context length",
+ "context_length_exceeded",
+ "input tokens exceed",
+ "input is too long",
+ "content length exceeds",
+)
+
+
+# Map raw SDK error patterns to user-friendly messages.
+# Matched case-insensitively; first match wins.
+_FRIENDLY_ERROR_MAP: tuple[tuple[str, str], ...] = (
+ ("authentication", "Authentication failed. Please check your API key."),
+ ("invalid api key", "Authentication failed. Please check your API key."),
+ ("unauthorized", "Authentication failed. Please check your API key."),
+ ("rate limit", "Rate limit exceeded. Please wait a moment and try again."),
+ ("overloaded", "The AI service is currently overloaded. Please try again shortly."),
+ ("server error", "The AI service encountered an internal error. Please retry."),
+ ("timeout", "The request timed out. Please try again."),
+ ("connection", "Connection error. Please check your network and retry."),
+)
+
+
+def _friendly_error_text(raw: str) -> str:
+ """Map a raw SDK error string to a user-friendly message.
+
+ Returns the mapped message if a known pattern is found, otherwise
+ returns a generic sanitized version of the raw error.
+ """
+ lower = raw.lower()
+ for pattern, friendly in _FRIENDLY_ERROR_MAP:
+ if pattern in lower:
+ return friendly
+ # Fallback: sanitize but keep the original text for debugging
+ return f"SDK stream error: {raw}"
+
+
+def _is_prompt_too_long(err: BaseException) -> bool:
+ """Return True if *err* indicates the prompt exceeds the model's limit.
+
+ Walks the exception chain (`__cause__` / `__context__`) so that
+ wrapped errors are detected too.
+ """
+ seen: set[int] = set()
+ current: BaseException | None = err
+ while current is not None and id(current) not in seen:
+ seen.add(id(current))
+ msg = str(current).lower()
+ if any(p in msg for p in _PROMPT_TOO_LONG_PATTERNS):
+ return True
+ current = current.__cause__ or current.__context__
+ return False
+
+
+class ReducedContext(NamedTuple):
+ builder: TranscriptBuilder
+ use_resume: bool
+ resume_file: str | None
+ transcript_lost: bool
+ tried_compaction: bool
+
+
+@dataclass
+class _TokenUsage:
+ """Token usage accumulators for a single turn.
+
+ Separated from `_RetryState` because usage is reset between retry
+ attempts independently of the retry-control fields, and is read by
+ the outer `stream_chat_completion_sdk` scope after the retry loop
+ completes.
+ """
+
+ prompt_tokens: int = 0
+ completion_tokens: int = 0
+ cache_read_tokens: int = 0
+ cache_creation_tokens: int = 0
+ cost_usd: float | None = None
+
+ def reset(self) -> None:
+ """Reset all accumulators for a new attempt."""
+ self.prompt_tokens = 0
+ self.completion_tokens = 0
+ self.cache_read_tokens = 0
+ self.cache_creation_tokens = 0
+ self.cost_usd = None
+
+
+@dataclass
+class _RetryState:
+ """Mutable state passed to `_run_stream_attempt` instead of closures.
+
+ Holds values that the retry loop modifies between attempts so the inner
+ generator does not rely on reassigning closure variables.
+ """
+
+ options: ClaudeAgentOptions
+ query_message: str
+ was_compacted: bool
+ use_resume: bool
+ resume_file: str | None
+ transcript_msg_count: int
+ adapter: SDKResponseAdapter
+ transcript_builder: TranscriptBuilder
+ usage: _TokenUsage
+
+
+@dataclass
+class _StreamContext:
+ """Per-request variables shared across all retry attempts.
+
+ Extracted so that `_run_stream_attempt` can be a module-level function
+ rather than a closure, making it independently testable and reducing the
+ cognitive load of the 970-line `stream_chat_completion_sdk` function.
+
+ Scalar fields (IDs, paths, the message string) are set once before the
+ retry loop and never reassigned. `session`, `compaction`, and
+ `lock` are **shared mutable references** whose internal state is
+ modified by both the retry loop and `_run_stream_attempt` (e.g.
+ `session.messages` is rolled back on retry, `compaction` tracks
+ mid-stream compaction events). Their *references* are constant even
+ though the objects they point to are mutated.
+ """
+
+ session: ChatSession
+ session_id: str
+ log_prefix: str
+ sdk_cwd: str
+ current_message: str
+ file_ids: list[str] | None
+ message_id: str
+ attachments: "PreparedAttachments"
+ compaction: CompactionTracker
+ lock: AsyncClusterLock
+
+
+async def _reduce_context(
+ transcript_content: str,
+ tried_compaction: bool,
+ session_id: str,
+ sdk_cwd: str,
+ log_prefix: str,
+) -> ReducedContext:
+ """Prepare reduced context for a retry attempt.
+
+ On the first retry, compacts the transcript via LLM summarization.
+ On subsequent retries (or if compaction fails), drops the transcript
+ entirely so the query is rebuilt from DB messages only.
+
+ `transcript_lost` is True when the transcript was dropped (caller
+ should set `skip_transcript_upload`).
+ """
+ # First retry: try compacting
+ if transcript_content and not tried_compaction:
+ compacted = await compact_transcript(
+ transcript_content, model=config.model, log_prefix=log_prefix
+ )
+ if (
+ compacted
+ and compacted != transcript_content
+ and validate_transcript(compacted)
+ ):
+ logger.info("%s Using compacted transcript for retry", log_prefix)
+ tb = TranscriptBuilder()
+ tb.load_previous(compacted, log_prefix=log_prefix)
+ resume_file = await asyncio.to_thread(
+ write_transcript_to_tempfile, compacted, session_id, sdk_cwd
+ )
+ if resume_file:
+ return ReducedContext(tb, True, resume_file, False, True)
+ logger.warning("%s Failed to write compacted transcript", log_prefix)
+ logger.warning("%s Compaction failed, dropping transcript", log_prefix)
+
+ # Subsequent retry or compaction failed: drop transcript entirely
+ logger.warning("%s Dropping transcript, rebuilding from DB messages", log_prefix)
+ return ReducedContext(TranscriptBuilder(), False, None, True, True)
+
+
def _append_error_marker(
session: ChatSession | None,
display_msg: str,
@@ -107,9 +296,9 @@ def _append_error_marker(
"""Append a copilot error marker to *session* so it persists across refresh.
Args:
- session: The chat session to append to (no-op if ``None``).
+ session: The chat session to append to (no-op if `None`).
display_msg: User-visible error text.
- retryable: If ``True``, use the retryable prefix so the frontend
+ retryable: If `True`, use the retryable prefix so the frontend
shows a "Try Again" button.
"""
if session is None:
@@ -124,11 +313,11 @@ def _setup_langfuse_otel() -> None:
"""Configure OTEL tracing for the Claude Agent SDK → Langfuse.
This uses LangSmith's built-in Claude Agent SDK integration to monkey-patch
- ``ClaudeSDKClient``, capturing every tool call and model turn as OTEL spans.
+ `ClaudeSDKClient`, capturing every tool call and model turn as OTEL spans.
Spans are exported via OTLP to Langfuse (or any OTEL-compatible backend).
- To route traces elsewhere, override ``OTEL_EXPORTER_OTLP_ENDPOINT`` and
- ``OTEL_EXPORTER_OTLP_HEADERS`` environment variables — no code changes needed.
+ To route traces elsewhere, override `OTEL_EXPORTER_OTLP_ENDPOINT` and
+ `OTEL_EXPORTER_OTLP_HEADERS` environment variables — no code changes needed.
"""
if not _is_langfuse_configured():
return
@@ -186,20 +375,72 @@ _HEARTBEAT_INTERVAL = 10.0 # seconds
STREAM_LOCK_PREFIX = "copilot:stream:lock:"
+async def _iter_sdk_messages(
+ client: ClaudeSDKClient,
+) -> AsyncGenerator[Any, None]:
+ """Yield SDK messages with heartbeat-based timeouts.
+
+ Uses an explicit async iterator with non-cancelling heartbeats.
+
+ CRITICAL: we must NOT cancel `__anext__()` mid-flight — doing so
+ (via `asyncio.timeout` or `wait_for`) corrupts the SDK's internal
+ anyio memory stream, causing `StopAsyncIteration` on the next call
+ and silently dropping all in-flight tool results. Instead, wrap
+ `__anext__()` in a `Task` and use `asyncio.wait()` with a
+ timeout. On timeout we yield a heartbeat sentinel but keep the Task
+ alive so it can deliver the next message.
+
+ Yields `None` on heartbeat timeout (caller should refresh locks and
+ emit heartbeat events). Yields the raw SDK message otherwise.
+ On stream end (`StopAsyncIteration`), the generator returns normally.
+ Any other exception from the SDK propagates to the caller.
+ """
+ msg_iter = client.receive_response().__aiter__()
+ pending_task: asyncio.Task[Any] | None = None
+
+ async def _next_msg() -> Any:
+ """Await the next SDK message, wrapped for use with `asyncio.Task`."""
+ return await msg_iter.__anext__()
+
+ try:
+ while True:
+ if pending_task is None:
+ pending_task = asyncio.create_task(_next_msg())
+
+ done, _ = await asyncio.wait({pending_task}, timeout=_HEARTBEAT_INTERVAL)
+
+ if not done:
+ yield None # heartbeat sentinel
+ continue
+
+ pending_task = None
+ try:
+ yield done.pop().result()
+ except StopAsyncIteration:
+ return
+ finally:
+ if pending_task is not None and not pending_task.done():
+ pending_task.cancel()
+ try:
+ await pending_task
+ except (asyncio.CancelledError, StopAsyncIteration):
+ pass
+
+
def _resolve_sdk_model() -> str | None:
"""Resolve the model name for the Claude Agent SDK CLI.
- Uses ``config.claude_agent_model`` if set, otherwise derives from
- ``config.model`` by stripping the OpenRouter provider prefix (e.g.,
- ``"anthropic/claude-opus-4.6"`` → ``"claude-opus-4-6"``).
+ Uses `config.claude_agent_model` if set, otherwise derives from
+ `config.model` by stripping the OpenRouter provider prefix (e.g.,
+ `"anthropic/claude-opus-4.6"` → `"claude-opus-4-6"`).
- OpenRouter uses dot-separated versions (``claude-opus-4.6``) while the
- direct Anthropic API uses hyphen-separated versions (``claude-opus-4-6``).
+ OpenRouter uses dot-separated versions (`claude-opus-4.6`) while the
+ direct Anthropic API uses hyphen-separated versions (`claude-opus-4-6`).
Normalisation is only applied when the SDK will actually talk to
Anthropic directly (not through OpenRouter).
- When ``use_claude_code_subscription`` is enabled and no explicit
- ``claude_agent_model`` is set, returns ``None`` so the CLI uses the
+ When `use_claude_code_subscription` is enabled and no explicit
+ `claude_agent_model` is set, returns `None` so the CLI uses the
default model for the user's subscription plan.
"""
if config.claude_agent_model:
@@ -219,7 +460,7 @@ def _resolve_sdk_model() -> str | None:
@functools.cache
def _validate_claude_code_subscription() -> None:
- """Validate Claude CLI is installed and responds to ``--version``.
+ """Validate Claude CLI is installed and responds to `--version`.
Cached so the blocking subprocess check runs at most once per process
lifetime. A failure (CLI not installed) is a config error that requires
@@ -255,9 +496,9 @@ def _build_sdk_env(
"""Build env vars for the SDK CLI subprocess.
Three modes (checked in order):
- 1. **Subscription** — clears all keys; CLI uses ``claude login`` auth.
- 2. **Direct Anthropic** — returns ``{}``; subprocess inherits
- ``ANTHROPIC_API_KEY`` from the parent environment.
+ 1. **Subscription** — clears all keys; CLI uses `claude login` auth.
+ 2. **Direct Anthropic** — returns `{}`; subprocess inherits
+ `ANTHROPIC_API_KEY` from the parent environment.
3. **OpenRouter** (default) — overrides base URL and auth token to
route through the proxy, with Langfuse trace headers.
"""
@@ -271,7 +512,7 @@ def _build_sdk_env(
}
# --- Mode 2: Direct Anthropic (no proxy hop) ---
- # ``openrouter_active`` checks the flag *and* credential presence.
+ # `openrouter_active` checks the flag *and* credential presence.
if not config.openrouter_active:
return {}
@@ -320,13 +561,13 @@ def _make_sdk_cwd(session_id: str) -> str:
async def _cleanup_sdk_tool_results(cwd: str) -> None:
"""Remove SDK session artifacts for a specific working directory.
- Cleans up the ephemeral working directory ``/tmp/copilot-<session>/``.
+ Cleans up the ephemeral working directory `/tmp/copilot-<session>/`.
Also sweeps stale CLI project directories (older than 12 h) to prevent
unbounded disk growth. The sweep is best-effort, rate-limited to once
every 5 minutes, and capped at 50 directories per sweep.
- Security: *cwd* MUST be created by ``_make_sdk_cwd()`` which sanitizes
+ Security: *cwd* MUST be created by `_make_sdk_cwd()` which sanitizes
the session_id.
"""
normalized = os.path.normpath(cwd)
@@ -396,10 +637,15 @@ async def _compress_messages(
) -> tuple[list[ChatMessage], bool]:
"""Compress a list of messages if they exceed the token threshold.
- Uses the shared compress_context() from prompt.py which supports:
- - LLM summarization of old messages (keeps recent ones intact)
- - Progressive content truncation as fallback
- - Middle-out deletion as last resort
+ Delegates to `_run_compression` (`transcript.py`) which centralizes
+ the "try LLM, fallback to truncation" pattern with timeouts. Both
+ `_compress_messages` and `compact_transcript` share this helper so
+ client acquisition and error handling are consistent.
+
+ See also:
+ `_run_compression` — shared compression with timeout guards.
+ `compact_transcript` — compresses JSONL transcript entries.
+ `CompactionTracker` — emits UI events for mid-stream compaction.
"""
messages = filter_compaction_messages(messages)
@@ -419,22 +665,13 @@ async def _compress_messages(
messages_dict.append(msg_dict)
try:
- async with openai.AsyncOpenAI(
- api_key=config.api_key, base_url=config.base_url, timeout=30.0
- ) as client:
- result = await compress_context(
- messages=messages_dict,
- model=config.model,
- client=client,
- )
- except Exception as e:
- logger.warning(f"[SDK] Context compression with LLM failed: {e}")
- # Fall back to truncation-only (no LLM summarization)
- result = await compress_context(
- messages=messages_dict,
- model=config.model,
- client=None,
- )
+ result = await _run_compression(messages_dict, config.model, "[SDK]")
+ except Exception as exc:
+ # Guard against timeouts or unexpected errors in compression —
+ # return the original messages so the caller can proceed without
+ # compaction rather than propagating the error to the retry loop.
+ logger.warning("[SDK] _compress_messages failed, returning originals: %s", exc)
+ return messages, False
if result.was_compacted:
logger.info(
@@ -664,6 +901,462 @@ async def _prepare_file_attachments(
return PreparedAttachments(hint=hint, image_blocks=image_blocks)
+@dataclass
+class _StreamAccumulator:
+ """Mutable state accumulated during a single streaming attempt.
+
+ Tracks the assistant message being built, tool calls, and flags that
+ control session-message bookkeeping in the dispatch loop.
+ """
+
+ assistant_response: ChatMessage
+ accumulated_tool_calls: list[dict[str, Any]]
+ has_appended_assistant: bool = False
+ has_tool_results: bool = False
+ stream_completed: bool = False
+
+
+def _dispatch_response(
+ response: StreamBaseResponse,
+ acc: _StreamAccumulator,
+ ctx: "_StreamContext",
+ state: "_RetryState",
+ entries_replaced: bool,
+ log_prefix: str,
+) -> StreamBaseResponse | None:
+ """Process a single adapter response and update session/accumulator state.
+
+ Returns the response to yield to the client, or `None` if the response
+ should be suppressed (e.g. `StreamStart` duplicates).
+
+ Handles:
+ - Logging tool events and errors
+ - Persisting error markers
+ - Accumulating text deltas into `assistant_response`
+ - Appending tool input/output to session messages and transcript
+ - Detecting `StreamFinish`
+ """
+ if isinstance(response, StreamStart):
+ return None
+
+ if isinstance(
+ response,
+ (StreamToolInputAvailable, StreamToolOutputAvailable),
+ ):
+ extra = ""
+ if isinstance(response, StreamToolOutputAvailable):
+ out_len = len(str(response.output))
+ extra = f", output_len={out_len}"
+ logger.info(
+ "%s Tool event: %s, tool=%s%s",
+ log_prefix,
+ type(response).__name__,
+ getattr(response, "toolName", "N/A"),
+ extra,
+ )
+
+ # Persist error markers so they survive page refresh
+ if isinstance(response, StreamError):
+ logger.error(
+ "%s Sending error to frontend: %s (code=%s)",
+ log_prefix,
+ response.errorText,
+ response.code,
+ )
+ _append_error_marker(
+ ctx.session,
+ response.errorText,
+ retryable=(response.code == "transient_api_error"),
+ )
+
+ if isinstance(response, StreamTextDelta):
+ delta = response.delta or ""
+ if acc.has_tool_results and acc.has_appended_assistant:
+ acc.assistant_response = ChatMessage(role="assistant", content=delta)
+ acc.accumulated_tool_calls = []
+ acc.has_appended_assistant = False
+ acc.has_tool_results = False
+ ctx.session.messages.append(acc.assistant_response)
+ acc.has_appended_assistant = True
+ else:
+ acc.assistant_response.content = (
+ acc.assistant_response.content or ""
+ ) + delta
+ if not acc.has_appended_assistant:
+ ctx.session.messages.append(acc.assistant_response)
+ acc.has_appended_assistant = True
+
+ elif isinstance(response, StreamToolInputAvailable):
+ acc.accumulated_tool_calls.append(
+ {
+ "id": response.toolCallId,
+ "type": "function",
+ "function": {
+ "name": response.toolName,
+ "arguments": json.dumps(response.input or {}),
+ },
+ }
+ )
+ acc.assistant_response.tool_calls = acc.accumulated_tool_calls
+ if not acc.has_appended_assistant:
+ ctx.session.messages.append(acc.assistant_response)
+ acc.has_appended_assistant = True
+
+ elif isinstance(response, StreamToolOutputAvailable):
+ content = (
+ response.output
+ if isinstance(response.output, str)
+ else json.dumps(response.output, ensure_ascii=False)
+ )
+ ctx.session.messages.append(
+ ChatMessage(
+ role="tool",
+ content=content,
+ tool_call_id=response.toolCallId,
+ )
+ )
+ if not entries_replaced:
+ state.transcript_builder.append_tool_result(
+ tool_use_id=response.toolCallId,
+ content=content,
+ )
+ acc.has_tool_results = True
+
+ elif isinstance(response, StreamFinish):
+ acc.stream_completed = True
+
+ return response
+
+
+class _TransientErrorHandled(Exception):
+ """Raised by `_run_stream_attempt` after it has already yielded a
+ `StreamError` for a transient API error.
+
+ This signals the outer retry loop that the attempt failed so it can
+ perform session-message rollback and set the `ended_with_stream_error`
+ flag, **without** yielding a duplicate `StreamError` to the client.
+ """
+
+
+async def _run_stream_attempt(
+ ctx: _StreamContext,
+ state: _RetryState,
+) -> AsyncIterator[StreamBaseResponse]:
+ """Run one SDK streaming attempt.
+
+ Opens a `ClaudeSDKClient`, sends the query, iterates SDK messages with
+ heartbeat timeouts, dispatches adapter responses, and performs post-stream
+ cleanup (safety-net flush, stopped-by-user handling).
+
+ Yields stream events. On stream error the exception propagates to the
+ caller so the retry loop can rollback and retry.
+
+ Args:
+ ctx: Per-request context shared across retry attempts. Scalar
+ fields (IDs, paths, message string) are set once and never
+ reassigned. `session`, `compaction`, and `lock` are
+ shared mutable references: `session.messages` is rolled back
+ on retry, `compaction` tracks mid-stream compaction events,
+ and `lock` is refreshed during heartbeats. Their references
+ are constant even though the objects they point to are mutated.
+ state: Mutable retry state — holds values that the retry loop
+ modifies between attempts (options, query, adapter, etc.).
+
+ See also:
+ `stream_chat_completion_sdk` — owns the retry loop that calls this
+ function up to `_MAX_STREAM_ATTEMPTS` times with reduced context.
+ """
+ acc = _StreamAccumulator(
+ assistant_response=ChatMessage(role="assistant", content=""),
+ accumulated_tool_calls=[],
+ )
+ ended_with_stream_error = False
+
+ async with ClaudeSDKClient(options=state.options) as client:
+ logger.info(
+ "%s Sending query — resume=%s, total_msgs=%d, "
+ "query_len=%d, attached_files=%d, image_blocks=%d",
+ ctx.log_prefix,
+ state.use_resume,
+ len(ctx.session.messages),
+ len(state.query_message),
+ len(ctx.file_ids) if ctx.file_ids else 0,
+ len(ctx.attachments.image_blocks),
+ )
+
+ ctx.compaction.reset_for_query()
+ if state.was_compacted:
+ for ev in ctx.compaction.emit_pre_query(ctx.session):
+ yield ev
+
+ if ctx.attachments.image_blocks:
+ content_blocks: list[dict[str, Any]] = [
+ *ctx.attachments.image_blocks,
+ {"type": "text", "text": state.query_message},
+ ]
+ user_msg = {
+ "type": "user",
+ "message": {"role": "user", "content": content_blocks},
+ "parent_tool_use_id": None,
+ "session_id": ctx.session_id,
+ }
+ if client._transport is None: # noqa: SLF001
+ raise RuntimeError("ClaudeSDKClient transport is not initialized")
+ await client._transport.write(json.dumps(user_msg) + "\n") # noqa: SLF001
+ state.transcript_builder.append_user(
+ content=[
+ *ctx.attachments.image_blocks,
+ {"type": "text", "text": ctx.current_message},
+ ]
+ )
+ else:
+ await client.query(state.query_message, session_id=ctx.session_id)
+ state.transcript_builder.append_user(content=ctx.current_message)
+
+ async for sdk_msg in _iter_sdk_messages(client):
+ # Heartbeat sentinel — refresh lock and keep SSE alive
+ if sdk_msg is None:
+ await ctx.lock.refresh()
+ for ev in ctx.compaction.emit_start_if_ready():
+ yield ev
+ yield StreamHeartbeat()
+ continue
+
+ logger.info(
+ "%s Received: %s %s (unresolved=%d, current=%d, resolved=%d)",
+ ctx.log_prefix,
+ type(sdk_msg).__name__,
+ getattr(sdk_msg, "subtype", ""),
+ len(state.adapter.current_tool_calls)
+ - len(state.adapter.resolved_tool_calls),
+ len(state.adapter.current_tool_calls),
+ len(state.adapter.resolved_tool_calls),
+ )
+
+ # Log AssistantMessage API errors (e.g. invalid_request)
+ # so we can debug Anthropic API 400s surfaced by the CLI.
+ sdk_error = getattr(sdk_msg, "error", None)
+ if isinstance(sdk_msg, AssistantMessage) and sdk_error:
+ error_text = str(sdk_error)
+ error_preview = str(sdk_msg.content)[:500]
+ logger.error(
+ "[SDK] [%s] AssistantMessage has error=%s, "
+ "content_blocks=%d, content_preview=%s",
+ ctx.session_id[:12],
+ sdk_error,
+ len(sdk_msg.content),
+ error_preview,
+ )
+
+ # Intercept transient API errors (socket closed,
+ # ECONNRESET) — replace the raw message with a
+ # user-friendly error text and use the retryable
+ # error prefix so the frontend shows a retry button.
+ # Check both the error field and content for patterns.
+ if is_transient_api_error(error_text) or is_transient_api_error(
+ error_preview
+ ):
+ logger.warning(
+ "%s Transient Anthropic API error detected, "
+ "suppressing raw error text",
+ ctx.log_prefix,
+ )
+ _append_error_marker(
+ ctx.session,
+ FRIENDLY_TRANSIENT_MSG,
+ retryable=True,
+ )
+ yield StreamError(
+ errorText=FRIENDLY_TRANSIENT_MSG,
+ code="transient_api_error",
+ )
+ ended_with_stream_error = True
+ break
+
+ # Race-condition fix: SDK hooks (PostToolUse) are
+ # executed asynchronously via start_soon() — the next
+ # message can arrive before the hook stashes output.
+ # wait_for_stash() awaits an asyncio.Event signaled by
+ # stash_pending_tool_output(), completing as soon as
+ # the hook finishes (typically <1ms). The sleep(0)
+ # after lets any remaining concurrent hooks complete.
+ #
+ # Skip for parallel tool continuations: when the SDK
+ # sends parallel tool calls as separate
+ # AssistantMessages (each containing only
+ # ToolUseBlocks), we must NOT wait/flush — the prior
+ # tools are still executing concurrently.
+ is_parallel_continuation = isinstance(sdk_msg, AssistantMessage) and all(
+ isinstance(b, ToolUseBlock) for b in sdk_msg.content
+ )
+ if (
+ state.adapter.has_unresolved_tool_calls
+ and isinstance(sdk_msg, (AssistantMessage, ResultMessage))
+ and not is_parallel_continuation
+ ):
+ if await wait_for_stash(timeout=0.5):
+ await asyncio.sleep(0)
+ else:
+ logger.warning(
+ "%s Timed out waiting for PostToolUse "
+ "hook stash (%d unresolved tool calls)",
+ ctx.log_prefix,
+ len(state.adapter.current_tool_calls)
+ - len(state.adapter.resolved_tool_calls),
+ )
+
+ # Log ResultMessage details and capture token usage
+ if isinstance(sdk_msg, ResultMessage):
+ logger.info(
+ "%s Received: ResultMessage %s "
+ "(unresolved=%d, current=%d, resolved=%d)",
+ ctx.log_prefix,
+ sdk_msg.subtype,
+ len(state.adapter.current_tool_calls)
+ - len(state.adapter.resolved_tool_calls),
+ len(state.adapter.current_tool_calls),
+ len(state.adapter.resolved_tool_calls),
+ )
+ if sdk_msg.subtype in (
+ "error",
+ "error_during_execution",
+ ):
+ logger.error(
+ "%s SDK execution failed with error: %s",
+ ctx.log_prefix,
+ sdk_msg.result or "(no error message provided)",
+ )
+
+ # Capture token usage from ResultMessage.
+ # Anthropic reports cached tokens separately:
+ # input_tokens = uncached only
+ # cache_read_input_tokens = served from cache
+ # cache_creation_input_tokens = written to cache
+ if sdk_msg.usage:
+ state.usage.prompt_tokens += sdk_msg.usage.get("input_tokens", 0)
+ state.usage.cache_read_tokens += sdk_msg.usage.get(
+ "cache_read_input_tokens", 0
+ )
+ state.usage.cache_creation_tokens += sdk_msg.usage.get(
+ "cache_creation_input_tokens", 0
+ )
+ state.usage.completion_tokens += sdk_msg.usage.get(
+ "output_tokens", 0
+ )
+ logger.info(
+ "%s Token usage: uncached=%d, cache_read=%d, "
+ "cache_create=%d, output=%d",
+ ctx.log_prefix,
+ state.usage.prompt_tokens,
+ state.usage.cache_read_tokens,
+ state.usage.cache_creation_tokens,
+ state.usage.completion_tokens,
+ )
+ if sdk_msg.total_cost_usd is not None:
+ state.usage.cost_usd = sdk_msg.total_cost_usd
+
+ # Emit compaction end if SDK finished compacting.
+ # Sync TranscriptBuilder with the CLI's active context.
+ compact_result = await ctx.compaction.emit_end_if_ready(ctx.session)
+ for ev in compact_result.events:
+ yield ev
+ entries_replaced = False
+ if compact_result.just_ended:
+ compacted = await asyncio.to_thread(
+ read_compacted_entries,
+ compact_result.transcript_path,
+ )
+ if compacted is not None:
+ state.transcript_builder.replace_entries(
+ compacted, log_prefix=ctx.log_prefix
+ )
+ entries_replaced = True
+
+ # --- Dispatch adapter responses ---
+ for response in state.adapter.convert_message(sdk_msg):
+ dispatched = _dispatch_response(
+ response, acc, ctx, state, entries_replaced, ctx.log_prefix
+ )
+ if dispatched is not None:
+ yield dispatched
+
+ # Append assistant entry AFTER convert_message so that
+ # any stashed tool results from the previous turn are
+ # recorded first, preserving the required API order:
+ # assistant(tool_use) → tool_result → assistant(text).
+ # Skip if replace_entries just ran — the CLI session
+ # file already contains this message.
+ if isinstance(sdk_msg, AssistantMessage) and not entries_replaced:
+ state.transcript_builder.append_assistant(
+ content_blocks=_format_sdk_content_blocks(sdk_msg.content),
+ model=sdk_msg.model,
+ )
+
+ if acc.stream_completed:
+ break
+
+ # --- Post-stream processing (only on success) ---
+ if state.adapter.has_unresolved_tool_calls:
+ logger.warning(
+ "%s %d unresolved tool(s) after stream — flushing",
+ ctx.log_prefix,
+ len(state.adapter.current_tool_calls)
+ - len(state.adapter.resolved_tool_calls),
+ )
+ safety_responses: list[StreamBaseResponse] = []
+ state.adapter._flush_unresolved_tool_calls(safety_responses)
+ for response in safety_responses:
+ if isinstance(
+ response,
+ (StreamToolInputAvailable, StreamToolOutputAvailable),
+ ):
+ logger.info(
+ "%s Safety flush: %s, tool=%s",
+ ctx.log_prefix,
+ type(response).__name__,
+ getattr(response, "toolName", "N/A"),
+ )
+ if isinstance(response, StreamToolOutputAvailable):
+ state.transcript_builder.append_tool_result(
+ tool_use_id=response.toolCallId,
+ content=(
+ response.output
+ if isinstance(response.output, str)
+ else json.dumps(response.output, ensure_ascii=False)
+ ),
+ )
+ yield response
+
+ if not acc.stream_completed and not ended_with_stream_error:
+ logger.info(
+ "%s Stream ended without ResultMessage (stopped by user)",
+ ctx.log_prefix,
+ )
+ closing_responses: list[StreamBaseResponse] = []
+ state.adapter._end_text_if_open(closing_responses)
+ for r in closing_responses:
+ yield r
+ ctx.session.messages.append(
+ ChatMessage(
+ role="assistant",
+ content=f"{COPILOT_SYSTEM_PREFIX} Execution stopped by user",
+ )
+ )
+
+ if (
+ acc.assistant_response.content or acc.assistant_response.tool_calls
+ ) and not acc.has_appended_assistant:
+ ctx.session.messages.append(acc.assistant_response)
+
+ # If the attempt ended with a transient error that was already surfaced
+ # to the client (StreamError yielded above), raise so the outer retry
+ # loop can rollback session messages and set its error flags properly.
+ if ended_with_stream_error:
+ raise _TransientErrorHandled(
+ "Transient API error handled — StreamError already yielded"
+ )
+
+
async def stream_chat_completion_sdk(
session_id: str,
message: str | None = None,
@@ -672,7 +1365,7 @@ async def stream_chat_completion_sdk(
session: ChatSession | None = None,
file_ids: list[str] | None = None,
**_kwargs: Any,
-) -> AsyncGenerator[StreamBaseResponse, None]:
+) -> AsyncIterator[StreamBaseResponse]:
"""Stream chat completion using Claude Agent SDK.
Args:
@@ -746,13 +1439,18 @@ async def stream_chat_completion_sdk(
message_id = str(uuid.uuid4())
stream_id = str(uuid.uuid4())
- stream_completed = False
ended_with_stream_error = False
e2b_sandbox = None
use_resume = False
resume_file: str | None = None
transcript_builder = TranscriptBuilder()
sdk_cwd = ""
+ # True when transcript_builder represents a full prefix of session history.
+ # First turn (<=1 prior message) is fully covered even without a download.
+ # Set to True when load_previous succeeds; stays False when download fails
+ # on a session with prior messages, preventing a partial upload that would
+ # mislead _build_query_message into skipping gap reconstruction next turn.
+ transcript_covers_prefix = True
# Acquire stream lock to prevent concurrent streams to the same session
lock = AsyncClusterLock(
@@ -766,7 +1464,7 @@ async def stream_chat_completion_sdk(
if lock_owner != stream_id:
# Another stream is active
logger.warning(
- f"{log_prefix} Session already has an active stream: {lock_owner}"
+ "%s Session already has an active stream: %s", log_prefix, lock_owner
)
yield StreamError(
errorText="Another stream is already active for this session. "
@@ -777,8 +1475,10 @@ async def stream_chat_completion_sdk(
# OTEL context manager — initialized inside the try and cleaned up in finally.
_otel_ctx: Any = None
+ skip_transcript_upload = False
+ transcript_content: str = ""
+ state: _RetryState | None = None
- # Make sure there is no more code between the lock acquisition and try-block.
# Token usage accumulators — populated from ResultMessage at end of turn
turn_prompt_tokens = 0 # uncached input tokens only
turn_completion_tokens = 0
@@ -786,6 +1486,7 @@ async def stream_chat_completion_sdk(
turn_cache_creation_tokens = 0
turn_cost_usd: float | None = None
+ # Make sure there is no more code between the lock acquisition and try-block.
try:
# Build system prompt (reuses non-SDK path with Langfuse support).
# Pre-compute the cwd here so the exact working directory path can be
@@ -879,24 +1580,30 @@ async def stream_chat_completion_sdk(
)
if is_valid:
# Load previous FULL context into builder
+ transcript_content = dl.content
transcript_builder.load_previous(dl.content, log_prefix=log_prefix)
- resume_file = write_transcript_to_tempfile(
- dl.content, session_id, sdk_cwd
+ resume_file = await asyncio.to_thread(
+ write_transcript_to_tempfile, dl.content, session_id, sdk_cwd
)
if resume_file:
use_resume = True
transcript_msg_count = dl.message_count
logger.debug(
- f"{log_prefix} Using --resume ({len(dl.content)}B, "
- f"msg_count={transcript_msg_count})"
+ "%s Using --resume (%dB, msg_count=%d)",
+ log_prefix,
+ len(dl.content),
+ transcript_msg_count,
)
else:
- logger.warning(f"{log_prefix} Transcript downloaded but invalid")
+ logger.warning("%s Transcript downloaded but invalid", log_prefix)
+ transcript_covers_prefix = False
elif config.claude_agent_use_resume and user_id and len(session.messages) > 1:
logger.warning(
- f"{log_prefix} No transcript available "
- f"({len(session.messages)} messages in session)"
+ "%s No transcript available (%d messages in session)",
+ log_prefix,
+ len(session.messages),
)
+ transcript_covers_prefix = False
yield StreamStart(messageId=message_id, sessionId=session_id)
@@ -930,6 +1637,7 @@ async def stream_chat_completion_sdk(
disallowed = get_sdk_disallowed_tools(use_e2b=use_e2b)
def _on_stderr(line: str) -> None:
+ """Log a stderr line emitted by the Claude CLI subprocess."""
sid = session_id[:12] if session_id else "?"
logger.info("[SDK] [%s] CLI stderr: %s", sid, line.rstrip())
@@ -970,512 +1678,247 @@ async def stream_chat_completion_sdk(
)
_otel_ctx.__enter__()
- async with ClaudeSDKClient(options=options) as client:
- current_message = message or ""
- if not current_message and session.messages:
- last_user = [m for m in session.messages if m.role == "user"]
- if last_user:
- current_message = last_user[-1].content or ""
-
- if not current_message.strip():
- yield StreamError(
- errorText="Message cannot be empty.",
- code="empty_prompt",
- )
- return
-
- query_message, was_compacted = await _build_query_message(
- current_message,
- session,
- use_resume,
- transcript_msg_count,
- session_id,
- )
- # If files are attached, prepare them: images become vision
- # content blocks in the user message, other files go to sdk_cwd.
- attachments = await _prepare_file_attachments(
- file_ids or [], user_id or "", session_id, sdk_cwd
- )
- if attachments.hint:
- query_message = f"{query_message}\n\n{attachments.hint}"
+ current_message = message or ""
+ if not current_message and session.messages:
+ last_user = [m for m in session.messages if m.role == "user"]
+ if last_user:
+ current_message = last_user[-1].content or ""
- logger.info(
- "%s Sending query — resume=%s, total_msgs=%d, "
- "query_len=%d, attached_files=%d, image_blocks=%d",
- log_prefix,
- use_resume,
- len(session.messages),
- len(query_message),
- len(file_ids) if file_ids else 0,
- len(attachments.image_blocks),
+ if not current_message.strip():
+ yield StreamError(
+ errorText="Message cannot be empty.",
+ code="empty_prompt",
)
+ return
- compaction.reset_for_query()
- if was_compacted:
- for ev in compaction.emit_pre_query(session):
- yield ev
-
- if attachments.image_blocks:
- # Build multimodal content: image blocks + text
- content_blocks: list[dict[str, Any]] = [
- *attachments.image_blocks,
- {"type": "text", "text": query_message},
- ]
- user_msg = {
- "type": "user",
- "message": {"role": "user", "content": content_blocks},
- "parent_tool_use_id": None,
- "session_id": session_id,
- }
- assert client._transport is not None # noqa: SLF001
- await client._transport.write( # noqa: SLF001
- json.dumps(user_msg) + "\n"
- )
- # Capture user message in transcript (multimodal)
- transcript_builder.append_user(content=content_blocks)
- else:
- await client.query(query_message, session_id=session_id)
- # Capture actual user message in transcript (not the engineered query)
- # query_message may include context wrappers, but transcript needs raw input
- transcript_builder.append_user(content=current_message)
-
- assistant_response = ChatMessage(role="assistant", content="")
- accumulated_tool_calls: list[dict[str, Any]] = []
- has_appended_assistant = False
- has_tool_results = False
- ended_with_stream_error = False
- # Use an explicit async iterator with non-cancelling heartbeats.
- # CRITICAL: we must NOT cancel __anext__() mid-flight — doing so
- # (via asyncio.timeout or wait_for) corrupts the SDK's internal
- # anyio memory stream, causing StopAsyncIteration on the next
- # call and silently dropping all in-flight tool results.
- # Instead, wrap __anext__() in a Task and use asyncio.wait()
- # with a timeout. On timeout we emit a heartbeat but keep the
- # Task alive so it can deliver the next message.
- msg_iter = client.receive_response().__aiter__()
- pending_task: asyncio.Task[Any] | None = None
- try:
- while not stream_completed:
- if pending_task is None:
-
- async def _next_msg() -> Any:
- return await msg_iter.__anext__()
+ query_message, was_compacted = await _build_query_message(
+ current_message,
+ session,
+ use_resume,
+ transcript_msg_count,
+ session_id,
+ )
+ # If files are attached, prepare them: images become vision
+ # content blocks in the user message, other files go to sdk_cwd.
+ attachments = await _prepare_file_attachments(
+ file_ids or [], user_id or "", session_id, sdk_cwd
+ )
+ if attachments.hint:
+ query_message = f"{query_message}\n\n{attachments.hint}"
- pending_task = asyncio.create_task(_next_msg())
+ tried_compaction = False
- done, _ = await asyncio.wait(
- {pending_task}, timeout=_HEARTBEAT_INTERVAL
- )
+ # Build the per-request context carrier (shared across attempts).
+ # Scalar fields are immutable; session/compaction/lock are shared
+ # mutable references (see `_StreamContext` docstring for details).
+ stream_ctx = _StreamContext(
+ session=session,
+ session_id=session_id,
+ log_prefix=log_prefix,
+ sdk_cwd=sdk_cwd,
+ current_message=current_message,
+ file_ids=file_ids,
+ message_id=message_id,
+ attachments=attachments,
+ compaction=compaction,
+ lock=lock,
+ )
- if not done:
- await lock.refresh()
- for ev in compaction.emit_start_if_ready():
- yield ev
- yield StreamHeartbeat()
- continue
-
- # Task completed — get result
- pending_task = None
- try:
- sdk_msg = done.pop().result()
- except StopAsyncIteration:
- logger.info(
- "%s Stream ended normally (StopAsyncIteration)",
- log_prefix,
- )
- break
- except Exception as stream_err:
- # SDK sends {"type": "error"} which raises
- # Exception in receive_response() — capture it
- # so the session can still be saved and the
- # frontend gets a clean finish.
- if is_transient_api_error(str(stream_err)):
- log, display, code = (
- logger.warning,
- FRIENDLY_TRANSIENT_MSG,
- "transient_api_error",
- )
- else:
- log, display, code = (
- logger.error,
- f"SDK stream error: {stream_err}",
- "sdk_stream_error",
- )
-
- log(
- "%s Stream error from SDK: %s",
- log_prefix,
- stream_err,
- exc_info=True,
- )
- ended_with_stream_error = True
- _append_error_marker(
- session,
- display,
- retryable=(code == "transient_api_error"),
- )
- yield StreamError(errorText=display, code=code)
- break
+ # ---------------------------------------------------------------
+ # Retry loop: original → compacted → no transcript
+ # ---------------------------------------------------------------
+ ended_with_stream_error = False
+ attempts_exhausted = False
+ stream_err: Exception | None = None
+
+ state = _RetryState(
+ options=options,
+ query_message=query_message,
+ was_compacted=was_compacted,
+ use_resume=use_resume,
+ resume_file=resume_file,
+ transcript_msg_count=transcript_msg_count,
+ adapter=adapter,
+ transcript_builder=transcript_builder,
+ usage=_TokenUsage(),
+ )
- logger.info(
- "%s Received: %s %s (unresolved=%d, current=%d, resolved=%d)",
- log_prefix,
- type(sdk_msg).__name__,
- getattr(sdk_msg, "subtype", ""),
- len(adapter.current_tool_calls)
- - len(adapter.resolved_tool_calls),
- len(adapter.current_tool_calls),
- len(adapter.resolved_tool_calls),
- )
+ for attempt in range(_MAX_STREAM_ATTEMPTS):
+ if attempt > 0:
+ logger.info(
+ "%s Retrying with reduced context (%d/%d)",
+ log_prefix,
+ attempt + 1,
+ _MAX_STREAM_ATTEMPTS,
+ )
+ yield StreamStatus(message="Optimizing conversation context\u2026")
- # Log AssistantMessage API errors (e.g. invalid_request)
- # so we can debug Anthropic API 400s surfaced by the CLI.
- sdk_error = getattr(sdk_msg, "error", None)
- if isinstance(sdk_msg, AssistantMessage) and sdk_error:
- error_text = str(sdk_error)
- error_preview = str(sdk_msg.content)[:500]
- logger.error(
- "[SDK] [%s] AssistantMessage has error=%s, "
- "content_blocks=%d, content_preview=%s",
- session_id[:12],
- sdk_error,
- len(sdk_msg.content),
- error_preview,
- )
+ ctx = await _reduce_context(
+ transcript_content,
+ tried_compaction,
+ session_id,
+ sdk_cwd,
+ log_prefix,
+ )
+ state.transcript_builder = ctx.builder
+ state.use_resume = ctx.use_resume
+ state.resume_file = ctx.resume_file
+ tried_compaction = ctx.tried_compaction
+ state.transcript_msg_count = 0
+ if ctx.transcript_lost:
+ skip_transcript_upload = True
+
+ # Rebuild SDK options and query for the reduced context
+ sdk_options_kwargs_retry = dict(sdk_options_kwargs)
+ if ctx.use_resume and ctx.resume_file:
+ sdk_options_kwargs_retry["resume"] = ctx.resume_file
+ elif "resume" in sdk_options_kwargs_retry:
+ del sdk_options_kwargs_retry["resume"]
+ state.options = ClaudeAgentOptions(**sdk_options_kwargs_retry) # type: ignore[arg-type] # dynamic kwargs
+ state.query_message, state.was_compacted = await _build_query_message(
+ current_message,
+ session,
+ state.use_resume,
+ state.transcript_msg_count,
+ session_id,
+ )
+ if attachments.hint:
+ state.query_message = f"{state.query_message}\n\n{attachments.hint}"
+ state.adapter = SDKResponseAdapter(
+ message_id=message_id, session_id=session_id
+ )
+ # Reset token accumulators so a failed attempt's partial
+ # usage is not double-counted in the successful attempt.
+ state.usage.reset()
- # Intercept transient API errors (socket closed,
- # ECONNRESET) — replace the raw message with a
- # user-friendly error text and use the retryable
- # error prefix so the frontend shows a retry button.
- # Check both the error field and content for patterns.
- if is_transient_api_error(error_text) or is_transient_api_error(
- error_preview
- ):
- logger.warning(
- "%s Transient Anthropic API error detected, "
- "suppressing raw error text",
- log_prefix,
- )
- ended_with_stream_error = True
- _append_error_marker(
- session,
- FRIENDLY_TRANSIENT_MSG,
- retryable=True,
- )
- yield StreamError(
- errorText=FRIENDLY_TRANSIENT_MSG,
- code="transient_api_error",
- )
- break
-
- # Race-condition fix: SDK hooks (PostToolUse) are
- # executed asynchronously via start_soon() — the next
- # message can arrive before the hook stashes output.
- # wait_for_stash() awaits an asyncio.Event signaled by
- # stash_pending_tool_output(), completing as soon as
- # the hook finishes (typically <1ms). The sleep(0)
- # after lets any remaining concurrent hooks complete.
- #
- # Skip for parallel tool continuations: when the SDK
- # sends parallel tool calls as separate
- # AssistantMessages (each containing only
- # ToolUseBlocks), we must NOT wait/flush — the prior
- # tools are still executing concurrently.
- is_parallel_continuation = isinstance(
- sdk_msg, AssistantMessage
- ) and all(isinstance(b, ToolUseBlock) for b in sdk_msg.content)
-
- if (
- adapter.has_unresolved_tool_calls
- and isinstance(sdk_msg, (AssistantMessage, ResultMessage))
- and not is_parallel_continuation
- ):
- # 2.0 s timeout: the original 0.5 s caused frequent
- # timeouts under load (parallel tool calls, large
- # outputs). 2.0 s gives margin while still failing
- # fast when the hook genuinely will not fire.
- if await wait_for_stash(timeout=2.0):
- # Yield once so any callbacks scheduled by the
- # stash signal can propagate before we process
- # the next SDK message.
- await asyncio.sleep(0)
- else:
- logger.warning(
- "%s Timed out waiting for "
- "PostToolUse hook stash "
- "(%d unresolved tool calls)",
- log_prefix,
- len(adapter.current_tool_calls)
- - len(adapter.resolved_tool_calls),
- )
-
- # Log ResultMessage details and capture token usage
- if isinstance(sdk_msg, ResultMessage):
- logger.info(
- "%s Received: ResultMessage %s "
- "(unresolved=%d, current=%d, resolved=%d)",
- log_prefix,
- sdk_msg.subtype,
- len(adapter.current_tool_calls)
- - len(adapter.resolved_tool_calls),
- len(adapter.current_tool_calls),
- len(adapter.resolved_tool_calls),
- )
- if sdk_msg.subtype in ("error", "error_during_execution"):
- logger.error(
- "%s SDK execution failed with error: %s",
- log_prefix,
- sdk_msg.result or "(no error message provided)",
- )
-
- # Capture token usage from ResultMessage.
- # Anthropic reports cached tokens separately:
- # input_tokens = uncached only
- # cache_read_input_tokens = served from cache
- # cache_creation_input_tokens = written to cache
- if sdk_msg.usage:
- turn_prompt_tokens += sdk_msg.usage.get("input_tokens", 0)
- turn_cache_read_tokens += sdk_msg.usage.get(
- "cache_read_input_tokens", 0
- )
- turn_cache_creation_tokens += sdk_msg.usage.get(
- "cache_creation_input_tokens", 0
- )
- turn_completion_tokens += sdk_msg.usage.get(
- "output_tokens", 0
- )
- logger.info(
- "%s Token usage: uncached=%d, cache_read=%d, cache_create=%d, output=%d",
- log_prefix,
- turn_prompt_tokens,
- turn_cache_read_tokens,
- turn_cache_creation_tokens,
- turn_completion_tokens,
- )
- if sdk_msg.total_cost_usd is not None:
- turn_cost_usd = sdk_msg.total_cost_usd
-
- # Emit compaction end if SDK finished compacting.
- # When compaction ends, sync TranscriptBuilder with the
- # CLI's active context so they stay identical.
- compact_result = await compaction.emit_end_if_ready(session)
- for ev in compact_result.events:
- yield ev
- # After replace_entries, skip append_assistant for this
- # sdk_msg — the CLI session file already contains it,
- # so appending again would create a duplicate.
- entries_replaced = False
- if compact_result.just_ended:
- compacted = await asyncio.to_thread(
- read_compacted_entries,
- compact_result.transcript_path,
- )
- if compacted is not None:
- transcript_builder.replace_entries(
- compacted, log_prefix=log_prefix
- )
- entries_replaced = True
-
- for response in adapter.convert_message(sdk_msg):
- if isinstance(response, StreamStart):
- continue
-
- # Log tool events for debugging
- if isinstance(
- response,
- (
- StreamToolInputAvailable,
- StreamToolOutputAvailable,
- ),
- ):
- extra = ""
- if isinstance(response, StreamToolOutputAvailable):
- out_len = len(str(response.output))
- extra = f", output_len={out_len}"
- logger.info(
- "%s Tool event: %s, tool=%s%s",
- log_prefix,
- type(response).__name__,
- getattr(response, "toolName", "N/A"),
- extra,
- )
-
- # Persist error markers so they survive page refresh
- if isinstance(response, StreamError):
- logger.error(
- "%s Sending error to frontend: %s (code=%s)",
- log_prefix,
- response.errorText,
- response.code,
- )
- _append_error_marker(
- session,
- response.errorText,
- retryable=(response.code == "transient_api_error"),
- )
- ended_with_stream_error = True
-
- yield response
-
- if isinstance(response, StreamTextDelta):
- delta = response.delta or ""
- # After tool results, start a new assistant
- # message for the post-tool text.
- if has_tool_results and has_appended_assistant:
- assistant_response = ChatMessage(
- role="assistant", content=delta
- )
- accumulated_tool_calls = []
- has_appended_assistant = False
- has_tool_results = False
- session.messages.append(assistant_response)
- has_appended_assistant = True
- else:
- assistant_response.content = (
- assistant_response.content or ""
- ) + delta
- if not has_appended_assistant:
- session.messages.append(assistant_response)
- has_appended_assistant = True
-
- elif isinstance(response, StreamToolInputAvailable):
- accumulated_tool_calls.append(
- {
- "id": response.toolCallId,
- "type": "function",
- "function": {
- "name": response.toolName,
- "arguments": json.dumps(response.input or {}),
- },
- }
- )
- assistant_response.tool_calls = accumulated_tool_calls
- if not has_appended_assistant:
- session.messages.append(assistant_response)
- has_appended_assistant = True
-
- elif isinstance(response, StreamToolOutputAvailable):
- content = (
- response.output
- if isinstance(response.output, str)
- else json.dumps(response.output, ensure_ascii=False)
- )
- session.messages.append(
- ChatMessage(
- role="tool",
- content=content,
- tool_call_id=response.toolCallId,
- )
- )
- if not entries_replaced:
- transcript_builder.append_tool_result(
- tool_use_id=response.toolCallId,
- content=content,
- )
- has_tool_results = True
-
- elif isinstance(response, StreamFinish):
- stream_completed = True
-
- # Append assistant entry AFTER convert_message so that
- # any stashed tool results from the previous turn are
- # recorded first, preserving the required API order:
- # assistant(tool_use) → tool_result → assistant(text).
- # Skip if replace_entries just ran — the CLI session
- # file already contains this message.
- if isinstance(sdk_msg, AssistantMessage) and not entries_replaced:
- transcript_builder.append_assistant(
- content_blocks=_format_sdk_content_blocks(sdk_msg.content),
- model=sdk_msg.model,
- )
+ pre_attempt_msg_count = len(session.messages)
+ events_yielded = 0
+ try:
+ async for event in _run_stream_attempt(stream_ctx, state):
+ if not isinstance(event, StreamHeartbeat):
+ events_yielded += 1
+ yield event
+ break # Stream completed — exit retry loop
except asyncio.CancelledError:
- # Task/generator was cancelled (e.g. client disconnect,
- # server shutdown). Log and let the safety-net / finally
- # blocks handle cleanup.
logger.warning(
- "%s Streaming loop cancelled (asyncio.CancelledError)",
+ "%s Streaming cancelled (attempt %d/%d)",
log_prefix,
+ attempt + 1,
+ _MAX_STREAM_ATTEMPTS,
)
raise
- finally:
- # Cancel the pending __anext__ task to avoid a leaked
- # coroutine. This is safe even if the task already
- # completed.
- if pending_task is not None and not pending_task.done():
- pending_task.cancel()
- try:
- await pending_task
- except (asyncio.CancelledError, StopAsyncIteration):
- # Expected: task was cancelled or exhausted during cleanup
- logger.info(
- "%s Pending __anext__ task completed during cleanup",
- log_prefix,
- )
-
- # Safety net: if tools are still unresolved after the
- # streaming loop (e.g. StopAsyncIteration before ResultMessage,
- # or SDK not sending UserMessages for built-in tools), flush
- # them now so the frontend stops showing spinners.
- if adapter.has_unresolved_tool_calls:
+ except _TransientErrorHandled:
+ # _run_stream_attempt already yielded a StreamError and
+ # appended an error marker. We only need to rollback
+ # session messages and set the error flag — do NOT set
+ # stream_err so the post-loop code won't emit a
+ # duplicate StreamError.
logger.warning(
- "%s %d unresolved tool(s) after stream loop — "
- "flushing as safety net",
+ "%s Transient error handled in stream attempt "
+ "(attempt %d/%d, events_yielded=%d)",
log_prefix,
- len(adapter.current_tool_calls) - len(adapter.resolved_tool_calls),
+ attempt + 1,
+ _MAX_STREAM_ATTEMPTS,
+ events_yielded,
)
- safety_responses: list[StreamBaseResponse] = []
- adapter._flush_unresolved_tool_calls(safety_responses)
- for response in safety_responses:
- if isinstance(
- response,
- (StreamToolInputAvailable, StreamToolOutputAvailable),
- ):
- logger.info(
- "%s Safety flush: %s, tool=%s",
- log_prefix,
- type(response).__name__,
- getattr(response, "toolName", "N/A"),
- )
- if isinstance(response, StreamToolOutputAvailable):
- transcript_builder.append_tool_result(
- tool_use_id=response.toolCallId,
- content=(
- response.output
- if isinstance(response.output, str)
- else json.dumps(response.output, ensure_ascii=False)
- ),
- )
- yield response
-
- # If the stream ended without a ResultMessage, the SDK
- # CLI exited unexpectedly or the user stopped execution.
- # Close any open text/step so chunks are well-formed, and
- # append a cancellation message so users see feedback.
- # StreamFinish is published by mark_session_completed in the processor.
- if not stream_completed and not ended_with_stream_error:
- logger.info(
- "%s Stream ended without ResultMessage (stopped by user)",
+ session.messages = session.messages[:pre_attempt_msg_count]
+ # Re-append the error marker so it survives the rollback
+ # and is persisted by the finally block (see #2947655365).
+ _append_error_marker(session, FRIENDLY_TRANSIENT_MSG, retryable=True)
+ ended_with_stream_error = True
+ break
+ except Exception as e:
+ stream_err = e
+ is_context_error = _is_prompt_too_long(e)
+ logger.warning(
+ "%s Stream error (attempt %d/%d, context_error=%s, "
+ "events_yielded=%d): %s",
log_prefix,
+ attempt + 1,
+ _MAX_STREAM_ATTEMPTS,
+ is_context_error,
+ events_yielded,
+ stream_err,
+ exc_info=True,
)
- closing_responses: list[StreamBaseResponse] = []
- adapter._end_text_if_open(closing_responses)
- for r in closing_responses:
- yield r
-
- # Add "Stopped by user" message so it persists after refresh
- # Use COPILOT_SYSTEM_PREFIX so frontend renders it as system message, not assistant
- session.messages.append(
- ChatMessage(
- role="assistant",
- content=f"{COPILOT_SYSTEM_PREFIX} Execution stopped by user",
+ session.messages = session.messages[:pre_attempt_msg_count]
+ if events_yielded > 0:
+ # Events were already sent to the frontend and cannot be
+ # unsent. Retrying would produce duplicate/inconsistent
+ # output, so treat this as a final error.
+ logger.warning(
+ "%s Not retrying — %d events already yielded",
+ log_prefix,
+ events_yielded,
)
+ ended_with_stream_error = True
+ break
+ if not is_context_error:
+ # Non-context errors (network, auth, rate-limit) should
+ # not trigger compaction — surface the error immediately.
+ ended_with_stream_error = True
+ break
+ continue
+ else:
+ # All retry attempts exhausted (loop ended without break)
+ # skip_transcript_upload is already set by _reduce_context
+ # when the transcript was dropped (transcript_lost=True).
+ ended_with_stream_error = True
+ attempts_exhausted = True
+ logger.error(
+ "%s All %d query attempts exhausted: %s",
+ log_prefix,
+ _MAX_STREAM_ATTEMPTS,
+ stream_err,
+ )
+
+ if ended_with_stream_error and state is not None:
+ # Flush any unresolved tool calls so the frontend can close
+ # stale UI elements (e.g. spinners) that were started before
+ # the exception interrupted the stream.
+ error_flush: list[StreamBaseResponse] = []
+ state.adapter._end_text_if_open(error_flush)
+ if state.adapter.has_unresolved_tool_calls:
+ logger.warning(
+ "%s Flushing %d unresolved tool(s) after stream error",
+ log_prefix,
+ len(state.adapter.current_tool_calls)
+ - len(state.adapter.resolved_tool_calls),
+ )
+ state.adapter._flush_unresolved_tool_calls(error_flush)
+ for response in error_flush:
+ yield response
+
+ if ended_with_stream_error and stream_err is not None:
+ # Use distinct error codes: "all_attempts_exhausted" when all
+ # retries were consumed vs "sdk_stream_error" for non-context
+ # errors that broke the loop immediately (network, auth, etc.).
+ safe_err = str(stream_err).replace("\n", " ").replace("\r", "")[:500]
+ if attempts_exhausted:
+ error_text = (
+ "Your conversation is too long. "
+ "Please start a new chat or clear some history."
)
+ else:
+ error_text = _friendly_error_text(safe_err)
+ yield StreamError(
+ errorText=error_text,
+ code=(
+ "all_attempts_exhausted"
+ if attempts_exhausted
+ else "sdk_stream_error"
+ ),
+ )
- if (
- assistant_response.content or assistant_response.tool_calls
- ) and not has_appended_assistant:
- session.messages.append(assistant_response)
+ # Copy token usage from retry state to outer-scope accumulators
+ # so the finally block can persist them.
+ if state is not None:
+ turn_prompt_tokens = state.usage.prompt_tokens
+ turn_completion_tokens = state.usage.completion_tokens
+ turn_cache_read_tokens = state.usage.cache_read_tokens
+ turn_cache_creation_tokens = state.usage.cache_creation_tokens
+ turn_cost_usd = state.usage.cost_usd
# Emit token usage to the client (must be in try to reach SSE stream).
# Session persistence of usage is in finally to stay consistent with
@@ -1497,11 +1940,6 @@ async def stream_chat_completion_sdk(
cache_creation_tokens=turn_cache_creation_tokens,
)
- # Transcript upload is handled exclusively in the finally block
- # to avoid double-uploads (the success path used to upload the
- # old resume file, then the finally block overwrote it with the
- # stop hook content — which could be smaller after compaction).
-
if ended_with_stream_error:
logger.warning(
"%s Stream ended with SDK error after %d messages",
@@ -1612,33 +2050,52 @@ async def stream_chat_completion_sdk(
# TranscriptBuilder is the single source of truth. It mirrors the
# CLI's active context: on compaction, replace_entries() syncs it
# with the compacted session file. No CLI file read needed here.
- if config.claude_agent_use_resume and user_id and session is not None:
+ if skip_transcript_upload:
+ logger.warning(
+ "%s Skipping transcript upload — transcript was dropped "
+ "during prompt-too-long recovery",
+ log_prefix,
+ )
+ elif (
+ config.claude_agent_use_resume
+ and user_id
+ and session is not None
+ and state is not None
+ ):
try:
- transcript_content = transcript_builder.to_jsonl()
- entry_count = transcript_builder.entry_count
+ transcript_upload_content = state.transcript_builder.to_jsonl()
+ entry_count = state.transcript_builder.entry_count
- if not transcript_content:
+ if not transcript_upload_content:
logger.warning(
"%s No transcript to upload (builder empty)", log_prefix
)
- elif not validate_transcript(transcript_content):
+ elif not validate_transcript(transcript_upload_content):
logger.warning(
"%s Transcript invalid, skipping upload (entries=%d)",
log_prefix,
entry_count,
)
+ elif not transcript_covers_prefix:
+ logger.warning(
+ "%s Skipping transcript upload — builder does not "
+ "cover full session prefix (entries=%d, session=%d)",
+ log_prefix,
+ entry_count,
+ len(session.messages),
+ )
else:
logger.info(
"%s Uploading transcript (entries=%d, bytes=%d)",
log_prefix,
entry_count,
- len(transcript_content),
+ len(transcript_upload_content),
)
await asyncio.shield(
upload_transcript(
user_id=user_id,
session_id=session_id,
- content=transcript_content,
+ content=transcript_upload_content,
message_count=len(session.messages),
log_prefix=log_prefix,
)
@@ -1671,6 +2128,6 @@ async def _update_title_async(
)
if title and user_id:
await update_session_title(session_id, user_id, title, only_if_empty=True)
- logger.debug(f"[SDK] Generated title for {session_id}: {title}")
+ logger.debug("[SDK] Generated title for %s: %s", session_id, title)
except Exception as e:
- logger.warning(f"[SDK] Failed to update session title: {e}")
+ logger.warning("[SDK] Failed to update session title: %s", e)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py
new file mode 100644
index 0000000000..123aff3a85
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py
@@ -0,0 +1,283 @@
+"""Unit tests for extracted service helpers.
+
+Covers ``_is_prompt_too_long``, ``_reduce_context``, ``_iter_sdk_messages``,
+and the ``ReducedContext`` named tuple.
+"""
+
+from __future__ import annotations
+
+import asyncio
+from collections.abc import AsyncGenerator
+from unittest.mock import AsyncMock, patch
+
+import pytest
+
+from .conftest import build_test_transcript as _build_transcript
+from .service import (
+ ReducedContext,
+ _is_prompt_too_long,
+ _iter_sdk_messages,
+ _reduce_context,
+)
+
+# ---------------------------------------------------------------------------
+# _is_prompt_too_long
+# ---------------------------------------------------------------------------
+
+
+class TestIsPromptTooLong:
+ def test_direct_match(self) -> None:
+ assert _is_prompt_too_long(Exception("prompt is too long")) is True
+
+ def test_case_insensitive(self) -> None:
+ assert _is_prompt_too_long(Exception("PROMPT IS TOO LONG")) is True
+
+ def test_no_match(self) -> None:
+ assert _is_prompt_too_long(Exception("network timeout")) is False
+
+ def test_request_too_large(self) -> None:
+ assert _is_prompt_too_long(Exception("request too large for model")) is True
+
+ def test_context_length_exceeded(self) -> None:
+ assert _is_prompt_too_long(Exception("context_length_exceeded")) is True
+
+ def test_max_tokens_exceeded_not_matched(self) -> None:
+ """'max_tokens_exceeded' is intentionally excluded (too broad)."""
+ assert _is_prompt_too_long(Exception("max_tokens_exceeded")) is False
+
+ def test_max_tokens_config_error_no_match(self) -> None:
+ """'max_tokens must be at least 1' should NOT match."""
+ assert _is_prompt_too_long(Exception("max_tokens must be at least 1")) is False
+
+ def test_chained_cause(self) -> None:
+ inner = Exception("prompt is too long")
+ outer = RuntimeError("SDK error")
+ outer.__cause__ = inner
+ assert _is_prompt_too_long(outer) is True
+
+ def test_chained_context(self) -> None:
+ inner = Exception("request too large")
+ outer = RuntimeError("wrapped")
+ outer.__context__ = inner
+ assert _is_prompt_too_long(outer) is True
+
+ def test_deep_chain(self) -> None:
+ bottom = Exception("maximum context length")
+ middle = RuntimeError("middle")
+ middle.__cause__ = bottom
+ top = ValueError("top")
+ top.__cause__ = middle
+ assert _is_prompt_too_long(top) is True
+
+ def test_chain_no_match(self) -> None:
+ inner = Exception("rate limit exceeded")
+ outer = RuntimeError("wrapped")
+ outer.__cause__ = inner
+ assert _is_prompt_too_long(outer) is False
+
+ def test_cycle_detection(self) -> None:
+ """Exception chain with a cycle should not infinite-loop."""
+ a = Exception("error a")
+ b = Exception("error b")
+ a.__cause__ = b
+ b.__cause__ = a # cycle
+ assert _is_prompt_too_long(a) is False
+
+ def test_all_patterns(self) -> None:
+ patterns = [
+ "prompt is too long",
+ "request too large",
+ "maximum context length",
+ "context_length_exceeded",
+ "input tokens exceed",
+ "input is too long",
+ "content length exceeds",
+ ]
+ for pattern in patterns:
+ assert _is_prompt_too_long(Exception(pattern)) is True, pattern
+
+
+# ---------------------------------------------------------------------------
+# _reduce_context
+# ---------------------------------------------------------------------------
+
+
+class TestReduceContext:
+ @pytest.mark.asyncio
+ async def test_first_retry_compaction_success(self) -> None:
+ transcript = _build_transcript([("user", "hi"), ("assistant", "hello")])
+ compacted = _build_transcript([("user", "hi"), ("assistant", "[summary]")])
+
+ with (
+ patch(
+ "backend.copilot.sdk.service.compact_transcript",
+ new_callable=AsyncMock,
+ return_value=compacted,
+ ),
+ patch(
+ "backend.copilot.sdk.service.validate_transcript",
+ return_value=True,
+ ),
+ patch(
+ "backend.copilot.sdk.service.write_transcript_to_tempfile",
+ return_value="/tmp/resume.jsonl",
+ ),
+ ):
+ ctx = await _reduce_context(
+ transcript, False, "sess-123", "/tmp/cwd", "[test]"
+ )
+
+ assert isinstance(ctx, ReducedContext)
+ assert ctx.use_resume is True
+ assert ctx.resume_file == "/tmp/resume.jsonl"
+ assert ctx.transcript_lost is False
+ assert ctx.tried_compaction is True
+
+ @pytest.mark.asyncio
+ async def test_compaction_fails_drops_transcript(self) -> None:
+ transcript = _build_transcript([("user", "hi"), ("assistant", "hello")])
+
+ with patch(
+ "backend.copilot.sdk.service.compact_transcript",
+ new_callable=AsyncMock,
+ return_value=None,
+ ):
+ ctx = await _reduce_context(
+ transcript, False, "sess-123", "/tmp/cwd", "[test]"
+ )
+
+ assert ctx.use_resume is False
+ assert ctx.resume_file is None
+ assert ctx.transcript_lost is True
+ assert ctx.tried_compaction is True
+
+ @pytest.mark.asyncio
+ async def test_already_tried_compaction_skips(self) -> None:
+ transcript = _build_transcript([("user", "hi"), ("assistant", "hello")])
+
+ ctx = await _reduce_context(transcript, True, "sess-123", "/tmp/cwd", "[test]")
+
+ assert ctx.use_resume is False
+ assert ctx.transcript_lost is True
+ assert ctx.tried_compaction is True
+
+ @pytest.mark.asyncio
+ async def test_empty_transcript_drops(self) -> None:
+ ctx = await _reduce_context("", False, "sess-123", "/tmp/cwd", "[test]")
+
+ assert ctx.use_resume is False
+ assert ctx.transcript_lost is True
+
+ @pytest.mark.asyncio
+ async def test_compaction_returns_same_content_drops(self) -> None:
+ transcript = _build_transcript([("user", "hi"), ("assistant", "hello")])
+
+ with patch(
+ "backend.copilot.sdk.service.compact_transcript",
+ new_callable=AsyncMock,
+ return_value=transcript, # same content
+ ):
+ ctx = await _reduce_context(
+ transcript, False, "sess-123", "/tmp/cwd", "[test]"
+ )
+
+ assert ctx.transcript_lost is True
+
+ @pytest.mark.asyncio
+ async def test_write_tempfile_fails_drops(self) -> None:
+ transcript = _build_transcript([("user", "hi"), ("assistant", "hello")])
+ compacted = _build_transcript([("user", "hi"), ("assistant", "[summary]")])
+
+ with (
+ patch(
+ "backend.copilot.sdk.service.compact_transcript",
+ new_callable=AsyncMock,
+ return_value=compacted,
+ ),
+ patch(
+ "backend.copilot.sdk.service.validate_transcript",
+ return_value=True,
+ ),
+ patch(
+ "backend.copilot.sdk.service.write_transcript_to_tempfile",
+ return_value=None,
+ ),
+ ):
+ ctx = await _reduce_context(
+ transcript, False, "sess-123", "/tmp/cwd", "[test]"
+ )
+
+ assert ctx.transcript_lost is True
+
+
+# ---------------------------------------------------------------------------
+# _iter_sdk_messages
+# ---------------------------------------------------------------------------
+
+
+class TestIterSdkMessages:
+ @pytest.mark.asyncio
+ async def test_yields_messages(self) -> None:
+ messages = ["msg1", "msg2", "msg3"]
+ client = AsyncMock()
+
+ async def _fake_receive() -> AsyncGenerator[str]:
+ for m in messages:
+ yield m
+
+ client.receive_response = _fake_receive
+ result = [msg async for msg in _iter_sdk_messages(client)]
+ assert result == messages
+
+ @pytest.mark.asyncio
+ async def test_heartbeat_on_timeout(self) -> None:
+ """Yields None when asyncio.wait times out."""
+ client = AsyncMock()
+ received: list = []
+
+ async def _slow_receive() -> AsyncGenerator[str]:
+ await asyncio.sleep(100) # never completes
+ yield "never" # pragma: no cover — unreachable, yield makes this an async generator
+
+ client.receive_response = _slow_receive
+
+ with patch("backend.copilot.sdk.service._HEARTBEAT_INTERVAL", 0.01):
+ count = 0
+ async for msg in _iter_sdk_messages(client):
+ received.append(msg)
+ count += 1
+ if count >= 3:
+ break
+
+ assert all(m is None for m in received)
+
+ @pytest.mark.asyncio
+ async def test_exception_propagates(self) -> None:
+ client = AsyncMock()
+
+ async def _error_receive() -> AsyncGenerator[str]:
+ raise RuntimeError("SDK crash")
+ yield # pragma: no cover — unreachable, yield makes this an async generator
+
+ client.receive_response = _error_receive
+
+ with pytest.raises(RuntimeError, match="SDK crash"):
+ async for _ in _iter_sdk_messages(client):
+ pass
+
+ @pytest.mark.asyncio
+ async def test_task_cleanup_on_break(self) -> None:
+ """Pending task is cancelled when generator is closed."""
+ client = AsyncMock()
+
+ async def _slow_receive() -> AsyncGenerator[str]:
+ yield "first"
+ await asyncio.sleep(100)
+ yield "second"
+
+ client.receive_response = _slow_receive
+
+ gen = _iter_sdk_messages(client)
+ first = await gen.__anext__()
+ assert first == "first"
+ await gen.aclose() # should cancel pending task cleanly
diff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript.py b/autogpt_platform/backend/backend/copilot/sdk/transcript.py
index 84fe19e82a..16a8f63756 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/transcript.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript.py
@@ -10,6 +10,9 @@ Storage is handled via ``WorkspaceStorageBackend`` (GCS in prod, local
filesystem for self-hosted) — no DB column needed.
"""
+from __future__ import annotations
+
+import asyncio
import logging
import os
import re
@@ -17,8 +20,12 @@ import shutil
import time
from dataclasses import dataclass
from pathlib import Path
+from uuid import uuid4
from backend.util import json
+from backend.util.clients import get_openai_client
+from backend.util.prompt import CompressResult, compress_context
+from backend.util.workspace_storage import GCSWorkspaceStorage, get_workspace_storage
logger = logging.getLogger(__name__)
@@ -99,7 +106,14 @@ def strip_progress_entries(content: str) -> str:
continue
parent = entry.get("parentUuid", "")
original_parent = parent
- while parent in stripped_uuids:
+ # seen_parents is local per-entry (not shared across iterations) so
+ # it can only detect cycles within a single ancestry walk, not across
+ # entries. This is intentional: each entry's parent chain is
+ # independent, and reusing a global set would incorrectly short-circuit
+ # valid re-use of the same UUID as a parent in different subtrees.
+ seen_parents: set[str] = set()
+ while parent in stripped_uuids and parent not in seen_parents:
+ seen_parents.add(parent)
parent = uuid_to_parent.get(parent, "")
if parent != original_parent:
entry["parentUuid"] = parent
@@ -336,7 +350,7 @@ def write_transcript_to_tempfile(
# Validate cwd is under the expected sandbox prefix (CodeQL sanitizer).
real_cwd = os.path.realpath(cwd)
if not real_cwd.startswith(_SAFE_CWD_PREFIX):
- logger.warning(f"[Transcript] cwd outside sandbox: {cwd}")
+ logger.warning("[Transcript] cwd outside sandbox: %s", cwd)
return None
try:
@@ -346,17 +360,17 @@ def write_transcript_to_tempfile(
os.path.join(real_cwd, f"transcript-{safe_id}.jsonl")
)
if not jsonl_path.startswith(real_cwd):
- logger.warning(f"[Transcript] Path escaped cwd: {jsonl_path}")
+ logger.warning("[Transcript] Path escaped cwd: %s", jsonl_path)
return None
with open(jsonl_path, "w") as f:
f.write(transcript_content)
- logger.info(f"[Transcript] Wrote resume file: {jsonl_path}")
+ logger.info("[Transcript] Wrote resume file: %s", jsonl_path)
return jsonl_path
except OSError as e:
- logger.warning(f"[Transcript] Failed to write resume file: {e}")
+ logger.warning("[Transcript] Failed to write resume file: %s", e)
return None
@@ -417,8 +431,6 @@ def _meta_storage_path_parts(user_id: str, session_id: str) -> tuple[str, str, s
def _build_path_from_parts(parts: tuple[str, str, str], backend: object) -> str:
"""Build a full storage path from (workspace_id, file_id, filename) parts."""
- from backend.util.workspace_storage import GCSWorkspaceStorage
-
wid, fid, fname = parts
if isinstance(backend, GCSWorkspaceStorage):
blob = f"workspaces/{wid}/{fid}/{fname}"
@@ -457,17 +469,15 @@ async def upload_transcript(
content: Complete JSONL transcript (from TranscriptBuilder).
message_count: ``len(session.messages)`` at upload time.
"""
- from backend.util.workspace_storage import get_workspace_storage
-
# Strip metadata entries (progress, file-history-snapshot, etc.)
# Note: SDK-built transcripts shouldn't have these, but strip for safety
stripped = strip_progress_entries(content)
if not validate_transcript(stripped):
# Log entry types for debugging — helps identify why validation failed
- entry_types: list[str] = []
- for line in stripped.strip().split("\n"):
- entry = json.loads(line, fallback={"type": "INVALID_JSON"})
- entry_types.append(entry.get("type", "?"))
+ entry_types = [
+ json.loads(line, fallback={"type": "INVALID_JSON"}).get("type", "?")
+ for line in stripped.strip().split("\n")
+ ]
logger.warning(
"%s Skipping upload — stripped content not valid "
"(types=%s, stripped_len=%d, raw_len=%d)",
@@ -503,11 +513,14 @@ async def upload_transcript(
content=json.dumps(meta).encode("utf-8"),
)
except Exception as e:
- logger.warning(f"{log_prefix} Failed to write metadata: {e}")
+ logger.warning("%s Failed to write metadata: %s", log_prefix, e)
logger.info(
- f"{log_prefix} Uploaded {len(encoded)}B "
- f"(stripped from {len(content)}B, msg_count={message_count})"
+ "%s Uploaded %dB (stripped from %dB, msg_count=%d)",
+ log_prefix,
+ len(encoded),
+ len(content),
+ message_count,
)
@@ -521,8 +534,6 @@ async def download_transcript(
Returns a ``TranscriptDownload`` with the JSONL content and the
``message_count`` watermark from the upload, or ``None`` if not found.
"""
- from backend.util.workspace_storage import get_workspace_storage
-
storage = await get_workspace_storage()
path = _build_storage_path(user_id, session_id, storage)
@@ -530,10 +541,10 @@ async def download_transcript(
data = await storage.retrieve(path)
content = data.decode("utf-8")
except FileNotFoundError:
- logger.debug(f"{log_prefix} No transcript in storage")
+ logger.debug("%s No transcript in storage", log_prefix)
return None
except Exception as e:
- logger.warning(f"{log_prefix} Failed to download transcript: {e}")
+ logger.warning("%s Failed to download transcript: %s", log_prefix, e)
return None
# Try to load metadata (best-effort — old transcripts won't have it)
@@ -545,10 +556,14 @@ async def download_transcript(
meta = json.loads(meta_data.decode("utf-8"), fallback={})
message_count = meta.get("message_count", 0)
uploaded_at = meta.get("uploaded_at", 0.0)
- except (FileNotFoundError, Exception):
+ except FileNotFoundError:
pass # No metadata — treat as unknown (msg_count=0 → always fill gap)
+ except Exception as e:
+ logger.debug("%s Failed to load transcript metadata: %s", log_prefix, e)
- logger.info(f"{log_prefix} Downloaded {len(content)}B (msg_count={message_count})")
+ logger.info(
+ "%s Downloaded %dB (msg_count=%d)", log_prefix, len(content), message_count
+ )
return TranscriptDownload(
content=content,
message_count=message_count,
@@ -562,8 +577,6 @@ async def delete_transcript(user_id: str, session_id: str) -> None:
Removes both the ``.jsonl`` transcript and the companion ``.meta.json``
so stale ``message_count`` watermarks cannot corrupt gap-fill logic.
"""
- from backend.util.workspace_storage import get_workspace_storage
-
storage = await get_workspace_storage()
path = _build_storage_path(user_id, session_id, storage)
@@ -580,3 +593,280 @@ async def delete_transcript(user_id: str, session_id: str) -> None:
logger.info("[Transcript] Deleted metadata for session %s", session_id)
except Exception as e:
logger.warning("[Transcript] Failed to delete metadata: %s", e)
+
+
+# ---------------------------------------------------------------------------
+# Transcript compaction — LLM summarization for prompt-too-long recovery
+# ---------------------------------------------------------------------------
+
+# JSONL protocol values used in transcript serialization.
+STOP_REASON_END_TURN = "end_turn"
+COMPACT_MSG_ID_PREFIX = "msg_compact_"
+ENTRY_TYPE_MESSAGE = "message"
+
+
+def _flatten_assistant_content(blocks: list) -> str:
+ """Flatten assistant content blocks into a single plain-text string.
+
+ Structured ``tool_use`` blocks are converted to ``[tool_use: name]``
+ placeholders. This is intentional: ``compress_context`` requires plain
+ text for token counting and LLM summarization. The structural loss is
+ acceptable because compaction only runs when the original transcript was
+ already too large for the model — a summarized plain-text version is
+ better than no context at all.
+ """
+ parts: list[str] = []
+ for block in blocks:
+ if isinstance(block, dict):
+ btype = block.get("type", "")
+ if btype == "text":
+ parts.append(block.get("text", ""))
+ elif btype == "tool_use":
+ parts.append(f"[tool_use: {block.get('name', '?')}]")
+ else:
+ # Preserve non-text blocks (e.g. image) as placeholders.
+ # Use __prefix__ to distinguish from literal user text.
+ parts.append(f"[__{btype}__]")
+ elif isinstance(block, str):
+ parts.append(block)
+ return "\n".join(parts) if parts else ""
+
+
+def _flatten_tool_result_content(blocks: list) -> str:
+ """Flatten tool_result and other content blocks into plain text.
+
+ Handles nested tool_result structures, text blocks, and raw strings.
+ Uses ``json.dumps`` as fallback for dict blocks without a ``text`` key
+ or where ``text`` is ``None``.
+
+ Like ``_flatten_assistant_content``, structured blocks (images, nested
+ tool results) are reduced to text representations for compression.
+ """
+ str_parts: list[str] = []
+ for block in blocks:
+ if isinstance(block, dict) and block.get("type") == "tool_result":
+ inner = block.get("content") or ""
+ if isinstance(inner, list):
+ for sub in inner:
+ if isinstance(sub, dict):
+ sub_type = sub.get("type")
+ if sub_type in ("image", "document"):
+ # Avoid serializing base64 binary data into
+ # the compaction input — use a placeholder.
+ str_parts.append(f"[__{sub_type}__]")
+ elif sub_type == "text" or sub.get("text") is not None:
+ str_parts.append(str(sub.get("text", "")))
+ else:
+ str_parts.append(json.dumps(sub))
+ else:
+ str_parts.append(str(sub))
+ else:
+ str_parts.append(str(inner))
+ elif isinstance(block, dict) and block.get("type") == "text":
+ str_parts.append(str(block.get("text", "")))
+ elif isinstance(block, dict):
+ # Preserve non-text/non-tool_result blocks (e.g. image) as placeholders.
+ # Use __prefix__ to distinguish from literal user text.
+ btype = block.get("type", "unknown")
+ str_parts.append(f"[__{btype}__]")
+ elif isinstance(block, str):
+ str_parts.append(block)
+ return "\n".join(str_parts) if str_parts else ""
+
+
+def _transcript_to_messages(content: str) -> list[dict]:
+ """Convert JSONL transcript entries to plain message dicts for compression.
+
+ Parses each line of the JSONL *content*, skips strippable metadata entries
+ (progress, file-history-snapshot, etc.), and extracts the ``role`` and
+ flattened ``content`` from the ``message`` field of each remaining entry.
+
+ Structured content blocks (``tool_use``, ``tool_result``, images) are
+ flattened to plain text via ``_flatten_assistant_content`` and
+ ``_flatten_tool_result_content`` so that ``compress_context`` can
+ perform token counting and LLM summarization on uniform strings.
+
+ Returns:
+ A list of ``{"role": str, "content": str}`` dicts suitable for
+ ``compress_context``.
+ """
+ messages: list[dict] = []
+ for line in content.strip().split("\n"):
+ if not line.strip():
+ continue
+ entry = json.loads(line, fallback=None)
+ if not isinstance(entry, dict):
+ continue
+ if entry.get("type", "") in STRIPPABLE_TYPES and not entry.get(
+ "isCompactSummary"
+ ):
+ continue
+ msg = entry.get("message", {})
+ role = msg.get("role", "")
+ if not role:
+ continue
+ msg_dict: dict = {"role": role}
+ raw_content = msg.get("content")
+ if role == "assistant" and isinstance(raw_content, list):
+ msg_dict["content"] = _flatten_assistant_content(raw_content)
+ elif isinstance(raw_content, list):
+ msg_dict["content"] = _flatten_tool_result_content(raw_content)
+ else:
+ msg_dict["content"] = raw_content or ""
+ messages.append(msg_dict)
+ return messages
+
+
+def _messages_to_transcript(messages: list[dict]) -> str:
+ """Convert compressed message dicts back to JSONL transcript format.
+
+ Rebuilds a minimal JSONL transcript from the ``{"role", "content"}``
+ dicts returned by ``compress_context``. Each message becomes one JSONL
+ line with a fresh ``uuid`` / ``parentUuid`` chain so the CLI's
+ ``--resume`` flag can reconstruct a valid conversation tree.
+
+ Assistant messages are wrapped in the full ``message`` envelope
+ (``id``, ``model``, ``stop_reason``, structured ``content`` blocks)
+ that the CLI expects. User messages use the simpler ``{role, content}``
+ form.
+
+ Returns:
+ A newline-terminated JSONL string, or an empty string if *messages*
+ is empty.
+ """
+ lines: list[str] = []
+ last_uuid: str = "" # root entry uses empty string, not null
+ for msg in messages:
+ role = msg.get("role", "user")
+ entry_type = "assistant" if role == "assistant" else "user"
+ uid = str(uuid4())
+ content = msg.get("content", "")
+ if role == "assistant":
+ message: dict = {
+ "role": "assistant",
+ "model": "",
+ "id": f"{COMPACT_MSG_ID_PREFIX}{uuid4().hex[:24]}",
+ "type": ENTRY_TYPE_MESSAGE,
+ "content": [{"type": "text", "text": content}] if content else [],
+ "stop_reason": STOP_REASON_END_TURN,
+ "stop_sequence": None,
+ }
+ else:
+ message = {"role": role, "content": content}
+ entry = {
+ "type": entry_type,
+ "uuid": uid,
+ "parentUuid": last_uuid,
+ "message": message,
+ }
+ lines.append(json.dumps(entry, separators=(",", ":")))
+ last_uuid = uid
+ return "\n".join(lines) + "\n" if lines else ""
+
+
+_COMPACTION_TIMEOUT_SECONDS = 60
+_TRUNCATION_TIMEOUT_SECONDS = 30
+
+
+async def _run_compression(
+ messages: list[dict],
+ model: str,
+ log_prefix: str,
+) -> CompressResult:
+ """Run LLM-based compression with truncation fallback.
+
+ Uses the shared OpenAI client from ``get_openai_client()``.
+ If no client is configured or the LLM call fails, falls back to
+ truncation-based compression which drops older messages without
+ summarization.
+
+ A 60-second timeout prevents a hung LLM call from blocking the
+ retry path indefinitely. The truncation fallback also has a
+ 30-second timeout to guard against slow tokenization on very large
+ transcripts.
+ """
+ client = get_openai_client()
+ if client is None:
+ logger.warning("%s No OpenAI client configured, using truncation", log_prefix)
+ return await asyncio.wait_for(
+ compress_context(messages=messages, model=model, client=None),
+ timeout=_TRUNCATION_TIMEOUT_SECONDS,
+ )
+ try:
+ return await asyncio.wait_for(
+ compress_context(messages=messages, model=model, client=client),
+ timeout=_COMPACTION_TIMEOUT_SECONDS,
+ )
+ except Exception as e:
+ logger.warning("%s LLM compaction failed, using truncation: %s", log_prefix, e)
+ return await asyncio.wait_for(
+ compress_context(messages=messages, model=model, client=None),
+ timeout=_TRUNCATION_TIMEOUT_SECONDS,
+ )
+
+
+async def compact_transcript(
+ content: str,
+ *,
+ model: str,
+ log_prefix: str = "[Transcript]",
+) -> str | None:
+ """Compact an oversized JSONL transcript using LLM summarization.
+
+ Converts transcript entries to plain messages, runs ``compress_context``
+ (the same compressor used for pre-query history), and rebuilds JSONL.
+
+ Structured content (``tool_use`` blocks, ``tool_result`` nesting, images)
+ is flattened to plain text for compression. This matches the fidelity of
+ the Plan C (DB compression) fallback path, where
+ ``_format_conversation_context`` similarly renders tool calls as
+ ``You called tool: name(args)`` and results as ``Tool result: ...``.
+ Neither path preserves structured API content blocks — the compacted
+ context serves as text history for the LLM, which creates proper
+ structured tool calls going forward.
+
+ Images are per-turn attachments loaded from workspace storage by file ID
+ (via ``_prepare_file_attachments``), not part of the conversation history.
+ They are re-attached each turn and are unaffected by compaction.
+
+ Returns the compacted JSONL string, or ``None`` on failure.
+
+ See also:
+ ``_compress_messages`` in ``service.py`` — compresses ``ChatMessage``
+ lists for pre-query DB history. Both share ``compress_context()``
+ but operate on different input formats (JSONL transcript entries
+ here vs. ChatMessage dicts there).
+ """
+ messages = _transcript_to_messages(content)
+ if len(messages) < 2:
+ logger.warning("%s Too few messages to compact (%d)", log_prefix, len(messages))
+ return None
+ try:
+ result = await _run_compression(messages, model, log_prefix)
+ if not result.was_compacted:
+ # Compressor says it's within budget, but the SDK rejected it.
+ # Return None so the caller falls through to DB fallback.
+ logger.warning(
+ "%s Compressor reports within budget but SDK rejected — "
+ "signalling failure",
+ log_prefix,
+ )
+ return None
+ logger.info(
+ "%s Compacted transcript: %d->%d tokens (%d summarized, %d dropped)",
+ log_prefix,
+ result.original_token_count,
+ result.token_count,
+ result.messages_summarized,
+ result.messages_dropped,
+ )
+ compacted = _messages_to_transcript(result.messages)
+ if not validate_transcript(compacted):
+ logger.warning("%s Compacted transcript failed validation", log_prefix)
+ return None
+ return compacted
+ except Exception as e:
+ logger.error(
+ "%s Transcript compaction failed: %s", log_prefix, e, exc_info=True
+ )
+ return None
diff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript_builder.py b/autogpt_platform/backend/backend/copilot/sdk/transcript_builder.py
index 19a0fe4837..b0b7fa5502 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/transcript_builder.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript_builder.py
@@ -68,7 +68,7 @@ class TranscriptBuilder:
type=entry_type,
uuid=data.get("uuid") or str(uuid4()),
parentUuid=data.get("parentUuid"),
- isCompactSummary=data.get("isCompactSummary") or None,
+ isCompactSummary=data.get("isCompactSummary"),
message=data.get("message", {}),
)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py
index 9fb9ed481a..68f4551b05 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py
@@ -1,7 +1,8 @@
"""Unit tests for JSONL transcript management utilities."""
+import asyncio
import os
-from unittest.mock import AsyncMock, patch
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -301,7 +302,7 @@ class TestDeleteTranscript:
mock_storage.delete = AsyncMock()
with patch(
- "backend.util.workspace_storage.get_workspace_storage",
+ "backend.copilot.sdk.transcript.get_workspace_storage",
new_callable=AsyncMock,
return_value=mock_storage,
):
@@ -321,7 +322,7 @@ class TestDeleteTranscript:
)
with patch(
- "backend.util.workspace_storage.get_workspace_storage",
+ "backend.copilot.sdk.transcript.get_workspace_storage",
new_callable=AsyncMock,
return_value=mock_storage,
):
@@ -339,7 +340,7 @@ class TestDeleteTranscript:
)
with patch(
- "backend.util.workspace_storage.get_workspace_storage",
+ "backend.copilot.sdk.transcript.get_workspace_storage",
new_callable=AsyncMock,
return_value=mock_storage,
):
@@ -818,6 +819,183 @@ class TestCompactionFlowIntegration:
assert lines2[-1]["parentUuid"] == "a2"
+# ---------------------------------------------------------------------------
+# _run_compression (direct tests for the 3 code paths)
+# ---------------------------------------------------------------------------
+
+
+class TestRunCompression:
+ """Direct tests for ``_run_compression`` covering all 3 code paths.
+
+ Paths:
+ (a) No OpenAI client configured → truncation fallback immediately.
+ (b) LLM success → returns LLM-compressed result.
+ (c) LLM call raises → truncation fallback.
+ """
+
+ def _make_compress_result(self, was_compacted: bool, msgs=None):
+ """Build a minimal CompressResult-like object."""
+ from types import SimpleNamespace
+
+ return SimpleNamespace(
+ was_compacted=was_compacted,
+ messages=msgs or [{"role": "user", "content": "summary"}],
+ original_token_count=500,
+ token_count=100 if was_compacted else 500,
+ messages_summarized=2 if was_compacted else 0,
+ messages_dropped=0,
+ )
+
+ @pytest.mark.asyncio
+ async def test_no_client_uses_truncation(self):
+ """Path (a): ``get_openai_client()`` returns None → truncation only."""
+ from .transcript import _run_compression
+
+ truncation_result = self._make_compress_result(
+ True, [{"role": "user", "content": "truncated"}]
+ )
+
+ with (
+ patch(
+ "backend.copilot.sdk.transcript.get_openai_client",
+ return_value=None,
+ ),
+ patch(
+ "backend.copilot.sdk.transcript.compress_context",
+ new_callable=AsyncMock,
+ return_value=truncation_result,
+ ) as mock_compress,
+ ):
+ result = await _run_compression(
+ [{"role": "user", "content": "hello"}],
+ model="test-model",
+ log_prefix="[test]",
+ )
+
+ # compress_context called with client=None (truncation mode)
+ call_kwargs = mock_compress.call_args
+ assert (
+ call_kwargs.kwargs.get("client") is None
+ or (call_kwargs.args and call_kwargs.args[2] is None)
+ or mock_compress.call_args[1].get("client") is None
+ )
+ assert result is truncation_result
+
+ @pytest.mark.asyncio
+ async def test_llm_success_returns_llm_result(self):
+ """Path (b): ``get_openai_client()`` returns a client → LLM compresses."""
+ from .transcript import _run_compression
+
+ llm_result = self._make_compress_result(
+ True, [{"role": "user", "content": "LLM summary"}]
+ )
+ mock_client = MagicMock()
+
+ with (
+ patch(
+ "backend.copilot.sdk.transcript.get_openai_client",
+ return_value=mock_client,
+ ),
+ patch(
+ "backend.copilot.sdk.transcript.compress_context",
+ new_callable=AsyncMock,
+ return_value=llm_result,
+ ) as mock_compress,
+ ):
+ result = await _run_compression(
+ [{"role": "user", "content": "long conversation"}],
+ model="test-model",
+ log_prefix="[test]",
+ )
+
+ # compress_context called with the real client
+ assert mock_compress.called
+ assert result is llm_result
+
+ @pytest.mark.asyncio
+ async def test_llm_failure_falls_back_to_truncation(self):
+ """Path (c): LLM call raises → truncation fallback used instead."""
+ from .transcript import _run_compression
+
+ truncation_result = self._make_compress_result(
+ True, [{"role": "user", "content": "truncated fallback"}]
+ )
+ mock_client = MagicMock()
+ call_count = [0]
+
+ async def _compress_side_effect(**kwargs):
+ call_count[0] += 1
+ if kwargs.get("client") is not None:
+ raise RuntimeError("LLM timeout")
+ return truncation_result
+
+ with (
+ patch(
+ "backend.copilot.sdk.transcript.get_openai_client",
+ return_value=mock_client,
+ ),
+ patch(
+ "backend.copilot.sdk.transcript.compress_context",
+ side_effect=_compress_side_effect,
+ ),
+ ):
+ result = await _run_compression(
+ [{"role": "user", "content": "long conversation"}],
+ model="test-model",
+ log_prefix="[test]",
+ )
+
+ # compress_context called twice: once for LLM (raises), once for truncation
+ assert call_count[0] == 2
+ assert result is truncation_result
+
+ @pytest.mark.asyncio
+ async def test_llm_timeout_falls_back_to_truncation(self):
+ """Path (d): LLM call exceeds timeout → truncation fallback used."""
+ from .transcript import _run_compression
+
+ truncation_result = self._make_compress_result(
+ True, [{"role": "user", "content": "truncated after timeout"}]
+ )
+ call_count = [0]
+
+ async def _compress_side_effect(*, messages, model, client):
+ call_count[0] += 1
+ if client is not None:
+ # Simulate a hang that exceeds the timeout
+ await asyncio.sleep(9999)
+ return truncation_result
+
+ fake_client = MagicMock()
+ with (
+ patch(
+ "backend.copilot.sdk.transcript.get_openai_client",
+ return_value=fake_client,
+ ),
+ patch(
+ "backend.copilot.sdk.transcript.compress_context",
+ side_effect=_compress_side_effect,
+ ),
+ patch(
+ "backend.copilot.sdk.transcript._COMPACTION_TIMEOUT_SECONDS",
+ 0.05,
+ ),
+ patch(
+ "backend.copilot.sdk.transcript._TRUNCATION_TIMEOUT_SECONDS",
+ 5,
+ ),
+ ):
+ result = await _run_compression(
+ [{"role": "user", "content": "long conversation"}],
+ model="test-model",
+ log_prefix="[test]",
+ )
+
+ # compress_context called twice: once for LLM (times out), once truncation
+ assert call_count[0] == 2
+ assert result is truncation_result
+
+
# ---------------------------------------------------------------------------
# cleanup_stale_project_dirs
# ---------------------------------------------------------------------------
diff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py
index 29c73ec814..8da9749a65 100644
--- a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py
+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py
@@ -41,8 +41,7 @@ import contextlib
import logging
from typing import Any, Awaitable, Callable, Literal
-from e2b import AsyncSandbox
-from e2b.sandbox.sandbox_api import SandboxLifecycle
+from e2b import AsyncSandbox, SandboxLifecycle
from backend.data.redis_client import get_redis_async
diff --git a/autogpt_platform/backend/backend/notifications/test_notifications.py b/autogpt_platform/backend/backend/notifications/test_notifications.py
index a48dc0ed2e..4934257400 100644
--- a/autogpt_platform/backend/backend/notifications/test_notifications.py
+++ b/autogpt_platform/backend/backend/notifications/test_notifications.py
@@ -19,6 +19,7 @@ class TestNotificationErrorHandling:
with patch("backend.notifications.notifications.AppService.__init__"):
manager = NotificationManager()
manager.email_sender = MagicMock()
+ manager.email_sender.send_templated = AsyncMock()
# Mock the _get_template method used by _process_batch
template_mock = Mock()
template_mock.base_template = "base"
diff --git a/autogpt_platform/backend/backend/util/prompt.py b/autogpt_platform/backend/backend/util/prompt.py
index 94fd251a36..cf8f737a0a 100644
--- a/autogpt_platform/backend/backend/util/prompt.py
+++ b/autogpt_platform/backend/backend/util/prompt.py
@@ -70,6 +70,10 @@ def _msg_tokens(msg: dict, enc) -> int:
# Count tool result tokens
tool_call_tokens += _tok_len(item.get("tool_use_id", ""), enc)
tool_call_tokens += _tok_len(item.get("content", ""), enc)
+ elif isinstance(item, dict) and item.get("type") == "text":
+ # Count text block tokens (standard: "text" key, fallback: "content")
+ text_val = item.get("text") or item.get("content", "")
+ tool_call_tokens += _tok_len(text_val, enc)
elif isinstance(item, dict) and "content" in item:
# Other content types with content field
tool_call_tokens += _tok_len(item.get("content", ""), enc)
@@ -145,10 +149,16 @@ def _truncate_middle_tokens(text: str, enc, max_tok: int) -> str:
if len(ids) <= max_tok:
return text # nothing to do
+ # Need at least 3 tokens (head + ellipsis + tail) for meaningful truncation
+ if max_tok < 1:
+ return ""
+ mid = enc.encode(" … ")
+ if max_tok < 3:
+ return enc.decode(ids[:max_tok])
+
# Split the allowance between the two ends:
head = max_tok // 2 - 1 # -1 for the ellipsis
tail = max_tok - head - 1
- mid = enc.encode(" … ")
return enc.decode(ids[:head] + mid + ids[-tail:])
@@ -545,6 +555,14 @@ async def _summarize_messages_llm(
"- Actions taken and key decisions made\n"
"- Technical specifics (file names, tool outputs, function signatures)\n"
"- Errors encountered and resolutions applied\n\n"
+ "IMPORTANT: Preserve all concrete references verbatim — these are small but "
+ "critical for continuing the conversation:\n"
+ "- File paths and directory paths (e.g. /src/app/page.tsx, ./output/result.csv)\n"
+ "- Image/media file paths from tool outputs\n"
+ "- URLs, API endpoints, and webhook addresses\n"
+ "- Resource IDs, session IDs, and identifiers\n"
+ "- Tool names that were called and their key parameters\n"
+ "- Environment variables, config keys, and credentials names (not values)\n\n"
"Include ONLY the sections below that have relevant content "
"(skip sections with nothing to report):\n\n"
"## 1. Primary Request and Intent\n"
@@ -552,7 +570,8 @@ async def _summarize_messages_llm(
"## 2. Key Technical Concepts\n"
"Technologies, frameworks, tools, and patterns being used or discussed.\n\n"
"## 3. Files and Resources Involved\n"
- "Specific files examined or modified, with relevant snippets and identifiers.\n\n"
+ "Specific files examined or modified, with relevant snippets and identifiers. "
+ "Include exact file paths, image paths from tool outputs, and resource URLs.\n\n"
"## 4. Errors and Fixes\n"
"Problems encountered, error messages, and their resolutions.\n\n"
"## 5. All User Messages\n"
@@ -566,7 +585,7 @@ async def _summarize_messages_llm(
},
{"role": "user", "content": f"Summarize:\n\n{conversation_text}"},
],
- max_tokens=1500,
+ max_tokens=2000,
temperature=0.3,
)
@@ -686,11 +705,15 @@ async def compress_context(
msgs = [summary_msg] + recent_msgs
logger.info(
- f"Context summarized: {original_count} -> {total_tokens()} tokens, "
- f"summarized {messages_summarized} messages"
+ "Context summarized: %d -> %d tokens, summarized %d messages",
+ original_count,
+ total_tokens(),
+ messages_summarized,
)
except Exception as e:
- logger.warning(f"Summarization failed, continuing with truncation: {e}")
+ logger.warning(
+ "Summarization failed, continuing with truncation: %s", e
+ )
# Fall through to content truncation
# ---- STEP 2: Normalize content ----------------------------------------
diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.toml
index b76b2b2baf..7181c9ac55 100644
--- a/autogpt_platform/backend/pyproject.toml
+++ b/autogpt_platform/backend/pyproject.toml
@@ -153,6 +153,9 @@ asyncio_default_fixture_loop_scope = "session"
# Disable syrupy plugin to avoid conflict with pytest-snapshot
# Both provide --snapshot-update argument causing ArgumentError
addopts = "-p no:syrupy"
+markers = [
+ "supplementary: tests kept for coverage but superseded by integration tests",
+]
filterwarnings = [
"ignore:'audioop' is deprecated:DeprecationWarning:discord.player",
"ignore:invalid escape sequence:DeprecationWarning:tweepy.api",