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(backend/copilot): keep the error code in the scheduled-turn alert so a failed routine says why (#15337)

Reinier van der Leere20c57141722
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor.py b/autogpt_platform/backend/backend/copilot/executor/processor.py
index e60423a518..75676b9e7c 100644
--- a/autogpt_platform/backend/backend/copilot/executor/processor.py
+++ b/autogpt_platform/backend/backend/copilot/executor/processor.py
@@ -883,6 +883,9 @@ class CoPilotProcessor:
log.info("Cancel requested, breaking stream")
break
+ # Before the StreamError break: the watch reports its code.
+ if scheduled_watch is not None:
+ scheduled_watch.observe(chunk)
# Capture StreamError so mark_session_completed receives
# the error message (stream_and_publish yields but does
# not publish StreamError — that's done by mark_session_completed).
@@ -890,8 +893,6 @@ class CoPilotProcessor:
error_msg = chunk.errorText
error_code = chunk.code
break
- if scheduled_watch is not None:
- scheduled_watch.observe(chunk)
current_time = time.monotonic()
if current_time - last_refresh >= refresh_interval:
diff --git a/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert.py b/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert.py
index 3a01b68ea2..1c5040f014 100644
--- a/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert.py
+++ b/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert.py
@@ -16,7 +16,11 @@ from pydantic import BaseModel
from backend.copilot import stream_registry
from backend.copilot.permissions import SDK_BUILTIN_TOOL_NAMES
-from backend.copilot.response_model import StreamBaseResponse, StreamToolOutputAvailable
+from backend.copilot.response_model import (
+ StreamBaseResponse,
+ StreamError,
+ StreamToolOutputAvailable,
+)
from backend.copilot.tools.models import ResponseType
from backend.monitoring.instrumentation import COPILOT_SCHEDULED_TURN_FAILURES
@@ -36,13 +40,14 @@ class ToolFailure(BaseModel):
class ScheduledTurnWatch:
- """Collects a scheduled turn's tool errors while it streams, then reports
- once the turn has ended."""
+ """Collects a scheduled turn's error code and tool errors while it streams,
+ then reports once the turn has ended."""
def __init__(self, entry: CoPilotExecutionEntry, origin: ScheduledTurnOrigin):
self._entry = entry
self._origin = origin
self.tool_failures: list[ToolFailure] = []
+ self._turn_error_code: str | None = None
@classmethod
def for_entry(cls, entry: CoPilotExecutionEntry) -> "ScheduledTurnWatch | None":
@@ -51,6 +56,9 @@ class ScheduledTurnWatch:
return cls(entry, entry.scheduled)
def observe(self, chunk: StreamBaseResponse) -> None:
+ if isinstance(chunk, StreamError):
+ self._turn_error_code = chunk.code
+ return
if not isinstance(chunk, StreamToolOutputAvailable):
return
error_type = tool_error_type(chunk.output)
@@ -104,7 +112,9 @@ class ScheduledTurnWatch:
"cron": self._origin.cron,
"session_id": self._entry.session_id,
"turn_id": self._entry.turn_id,
- "turn_error_type": error_type(turn_error) if turn_error else None,
+ "turn_error_type": (
+ error_type(self._turn_error_code or turn_error) if turn_error else None
+ ),
"tool_failures": [f.model_dump() for f in self.tool_failures],
}
failed_tools = ", ".join(
diff --git a/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert_test.py b/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert_test.py
index b08bb4fdfb..f403f43ce2 100644
--- a/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert_test.py
+++ b/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert_test.py
@@ -18,7 +18,11 @@ from backend.copilot.executor.utils import (
ScheduledTurnOrigin,
)
from backend.copilot.model import ChatSession
-from backend.copilot.response_model import StreamTextDelta, StreamToolOutputAvailable
+from backend.copilot.response_model import (
+ StreamError,
+ StreamTextDelta,
+ StreamToolOutputAvailable,
+)
from backend.copilot.tools.models import ErrorResponse, ExecutionStartedResponse
from backend.integrations.codex.transport import CodexCredentialIntegrityError
from backend.monitoring.instrumentation import COPILOT_SCHEDULED_TURN_FAILURES
@@ -304,6 +308,21 @@ async def test_executor_alerts_when_a_scheduled_turns_run_agent_fails():
)
+async def test_executor_alerts_a_scheduled_turns_error_by_its_code():
+ spending_limit = StreamError(
+ errorText="This turn reached its spending limit. Start a new turn.",
+ code="max_budget_exhausted",
+ )
+
+ sentry = await _run_turn(_entry(_weekly()), [spending_limit])
+
+ scope = sentry.new_scope.return_value.__enter__.return_value
+ _, context = scope.set_context.call_args.args
+ assert context["turn_error_type"] == "max_budget_exhausted"
+ assert "spending limit" not in str(sentry.mock_calls)
+
+
@pytest.mark.asyncio
async def test_executor_does_not_alert_for_an_interactive_turn():
sentry = await _run_turn(_entry(None), [_run_agent_failure()])