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.

Revert "fix(backend/copilot): detect prompt-too-long in AssistantMessage content and ResultMessage success subtype"

Zamil Majdyd61a2c6cd05d
diff --git a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
index 5a5bc2a432..9c9d5e4772 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
@@ -1487,191 +1487,3 @@ class TestStreamChatCompletionRetryIntegration:
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_result_message_success_subtype_prompt_too_long_triggers_compaction(
- self,
- ):
- """CLI returns ResultMessage(subtype="success") with result="Prompt is too long".
-
- The SDK internally compacts but the transcript is still too long. It
- returns subtype="success" (process completed) with result="Prompt is
- too long" (the actual rejection message). The retry loop must detect
- this as a context-length error and trigger compaction — the subtype
- "success" must not fool it into treating this as a real response.
- """
- import contextlib
-
- from claude_agent_sdk import AssistantMessage, ResultMessage, TextBlock
-
- from backend.copilot.response_model import StreamError, StreamStart
- from backend.copilot.sdk.service import stream_chat_completion_sdk
-
- session = self._make_session()
- success_result = self._make_result_message()
- attempt_count = [0]
-
- error_result = ResultMessage(
- subtype="success",
- result="Prompt is too long",
- duration_ms=100,
- duration_api_ms=0,
- is_error=False,
- num_turns=1,
- session_id="test-session-id",
- )
-
- def _client_factory(*args, **kwargs):
- attempt_count[0] += 1
-
- async def _receive_error():
- yield AssistantMessage(
- content=[TextBlock(text="Prompt is too long")],
- model="<synthetic>",
- error="invalid_request",
- )
- yield error_result
-
- async def _receive_success():
- yield success_result
-
- client = MagicMock()
- client._transport = MagicMock()
- client._transport.write = AsyncMock()
- client.query = AsyncMock()
- if attempt_count[0] == 1:
- client.receive_response = _receive_error
- else:
- client.receive_response = _receive_success
- cm = AsyncMock()
- cm.__aenter__.return_value = client
- cm.__aexit__.return_value = None
- return cm
-
- 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 (subtype='success' with 'Prompt is too long' "
- f"result should trigger compaction 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_assistant_message_error_content_prompt_too_long_triggers_compaction(
- self,
- ):
- """AssistantMessage.error="invalid_request" with content "Prompt is too long".
-
- The SDK returns error type "invalid_request" but puts the actual
- rejection message ("Prompt is too long") in the content blocks.
- The retry loop must detect this via content inspection (sdk_error
- being set confirms it's an error message, not user content).
- """
- import contextlib
-
- from claude_agent_sdk import AssistantMessage, ResultMessage, TextBlock
-
- from backend.copilot.response_model import StreamError, StreamStart
- from backend.copilot.sdk.service import stream_chat_completion_sdk
-
- session = self._make_session()
- success_result = self._make_result_message()
- attempt_count = [0]
-
- def _client_factory(*args, **kwargs):
- attempt_count[0] += 1
-
- async def _receive_error():
- # SDK returns invalid_request with "Prompt is too long" in content
- yield AssistantMessage(
- content=[TextBlock(text="Prompt is too long")],
- model="<synthetic>",
- error="invalid_request",
- )
- yield ResultMessage(
- subtype="success",
- result="Prompt is too long",
- duration_ms=100,
- duration_api_ms=0,
- is_error=False,
- num_turns=1,
- session_id="test-session-id",
- )
-
- async def _receive_success():
- yield success_result
-
- client = MagicMock()
- client._transport = MagicMock()
- client._transport.write = AsyncMock()
- client.query = AsyncMock()
- if attempt_count[0] == 1:
- client.receive_response = _receive_error
- else:
- client.receive_response = _receive_success
- cm = AsyncMock()
- cm.__aenter__.return_value = client
- cm.__aexit__.return_value = None
- return cm
-
- 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 (AssistantMessage error content 'Prompt is "
- f"too long' should trigger compaction 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)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 79c6591e6e..1c98da46c6 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -1310,16 +1310,10 @@ async def _run_stream_attempt(
# AssistantMessage.error (not as a Python exception).
# Re-raise so the outer retry loop can compact the
# transcript and retry with reduced context.
- # Check both error_text and error_preview: sdk_error
- # being set confirms this is an error message (not user
- # content), so checking content is safe. The actual
- # error description (e.g. "Prompt is too long") may be
- # in the content, not the error type field
- # (e.g. error="invalid_request", content="Prompt is
- # too long").
- if _is_prompt_too_long(Exception(error_text)) or _is_prompt_too_long(
- Exception(error_preview)
- ):
+ # Only check error_text (the error field), not the
+ # content preview — content may contain arbitrary text
+ # that false-positives the pattern match.
+ if _is_prompt_too_long(Exception(error_text)):
logger.warning(
"%s Prompt-too-long detected via AssistantMessage "
"error — raising for retry",
@@ -1420,16 +1414,13 @@ async def _run_stream_attempt(
ctx.log_prefix,
sdk_msg.result or "(no error message provided)",
)
-
- # Check for prompt-too-long regardless of subtype — the
- # SDK may return subtype="success" with result="Prompt is
- # too long" when the CLI rejects the prompt before calling
- # the API (cost_usd=0, no tokens consumed). If we only
- # check the "error" subtype path, the stream appears to
- # complete normally, the synthetic error text is stored
- # in the transcript, and the session grows without bound.
- if _is_prompt_too_long(RuntimeError(sdk_msg.result or "")):
- raise RuntimeError("Prompt is too long")
+ # If the CLI itself rejected the prompt as too long
+ # (pre-API check, duration_api_ms=0), re-raise as an
+ # exception so the retry loop can trigger compaction.
+ # Without this, the ResultMessage is silently consumed
+ # and the retry/compaction mechanism is never invoked.
+ if _is_prompt_too_long(RuntimeError(sdk_msg.result or "")):
+ raise RuntimeError("Prompt is too long")
# Capture token usage from ResultMessage.
# Anthropic reports cached tokens separately: