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): recover budget-limited turns without resending work (#13432)
C1-BA-B1-F3b8c84a873003
diff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.pyindex 1fae4c59c2..3d03a1ec63 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py@@ -603,9 +603,10 @@ class SDKResponseAdapter:responses.append(StreamError(errorText=(- "The turn ended because it exceeded the budget. "- "Try a smaller scope, or wait for the next "- "billing window."+ "This turn reached its spending limit. "+ "Send a follow-up to continue with a smaller scope. "+ "If your account usage limit is also reached, "+ "wait for it to reset."),code="max_budget_exhausted",)diff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.pyindex d549452b27..47a1a4d3c6 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py@@ -2250,7 +2250,10 @@ def test_error_max_budget_usd_surfaces_specific_error_not_empty_overlay():errors = [r for r in results if isinstance(r, StreamError)]assert len(errors) == 1assert errors[0].code == "max_budget_exhausted"- assert "budget" in errors[0].errorText.lower()+ assert "spending limit" in errors[0].errorText.lower()+ assert "follow-up" in errors[0].errorText+ assert "account" in errors[0].errorText+ assert "billing window" not in errors[0].errorText# Must NOT shadow with empty_completion overlay.assert all(e.code != "empty_completion" for e in errors)diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamErrorHandlers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamErrorHandlers.test.tsindex a10eced46a..4de9c2285f 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamErrorHandlers.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamErrorHandlers.test.ts@@ -114,6 +114,25 @@ describe("handleStreamError", () => {expect(arg.title).toBe("Your expert ran into a problem");});+ it("keeps a turn budget error distinct from account admission limits", () => {+ const onRateLimit = vi.fn();+ const onReconnect = vi.fn();+ handleStreamError({+ error: new Error(+ "[code:max_budget_exhausted] Send a follow-up. If your account usage limit is also reached, wait for its reset.",+ ),+ onRateLimit,+ onReconnect,+ isUserStoppingRef: makeRef(false),+ });++ expect(mockToast).toHaveBeenCalledWith(+ expect.objectContaining({ title: "Turn budget reached" }),+ );+ expect(onRateLimit).not.toHaveBeenCalled();+ expect(onReconnect).not.toHaveBeenCalled();+ });+it("uses fallbackDescription when the backend message is empty", () => {handleStreamError({error: new Error("[code:tool_stalled]"),diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessagePartRenderer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessagePartRenderer.tsxindex a65e56c1ea..96187e28f6 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessagePartRenderer.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessagePartRenderer.tsx@@ -197,7 +197,7 @@ export function MessagePartRenderer({<ErrorCardkey={key}responseError={{ message: markerText }}- context="execution"+ context="the response"onRetry={markerType === "retryable_error" ? onRetry : undefined}/>);diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamErrorHandlers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamErrorHandlers.tsindex 592b6d7da8..f68ae39f53 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamErrorHandlers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamErrorHandlers.ts@@ -67,6 +67,11 @@ const TOAST_BY_BACKEND_CODE: Record<fallbackDescription:"The assistant couldn't complete this turn. Press Try Again to retry.",},+ max_budget_exhausted: {+ title: "Turn budget reached",+ fallbackDescription:+ "This turn reached its spending limit. Send a follow-up to continue with a smaller scope. If your account usage limit is also reached, wait for it to reset.",+ },};/** Fallback toast shown for any `[code:X]` we don't have specific copy for. */@@ -160,11 +165,10 @@ interface HandleStreamErrorArgs {* (or rate-limit UI), and decides whether to retry via reconnect.** Dispatch order (exclusive branches):- * 0. A typed provider-failure envelope → its own copy. Preferred over- * everything below, which is guesswork from error text.- * 1. `usage limit` substring → rate-limit UI via `onRateLimit`.- * 2. 401 / auth failure → auth-error toast.- * 3. `[code:<id>]` backend prefix → curated or generic backend toast.+ * 0. A typed provider-failure envelope → its own copy.+ * 1. `[code:<id>]` backend prefix → curated or generic backend toast.+ * 2. Legacy `usage limit` substring → rate-limit UI via `onRateLimit`.+ * 3. Legacy 401 / auth failure → auth-error toast.* 4. TypeError / AbortError / "connection interrupted" → reconnect.* 5. Anything else silently falls through (the AI-SDK also surfaces* `error` into the hook's `error` return, which drives the inline@@ -215,7 +219,21 @@ export function handleStreamError({return;}- // 1. Rate limit (FastAPI 429 body contains "usage limit")+ // 1. Coded backend errors take precedence over message-text heuristics.+ const { code: backendCode, message: backendMessage } =+ parseBackendErrorCode(errorDetail);+ if (backendCode) {+ const userToast =+ TOAST_BY_BACKEND_CODE[backendCode] ?? GENERIC_BACKEND_TOAST;+ toast({+ title: userToast.title,+ description: backendMessage || userToast.fallbackDescription,+ variant: "destructive",+ });+ return;+ }++ // 2. Rate limit (FastAPI 429 body contains "usage limit")if (errorDetail.toLowerCase().includes("usage limit")) {onRateLimit(errorDetail || "You've reached your usage limit. Please try again later.",@@ -223,7 +241,7 @@ export function handleStreamError({return;}- // 2. Authentication failures (from getCopilotAuthHeaders or 401 responses)+ // 3. Authentication failures (from getCopilotAuthHeaders or 401 responses)const isAuthError =errorDetail.includes("Authentication failed") ||errorDetail.includes("Unauthorized") ||@@ -238,21 +256,6 @@ export function handleStreamError({return;}- // 3. Coded backend error — show curated or generic toast so backend hangs- // are never silent.- const { code: backendCode, message: backendMessage } =- parseBackendErrorCode(errorDetail);- if (backendCode) {- const userToast =- TOAST_BY_BACKEND_CODE[backendCode] ?? GENERIC_BACKEND_TOAST;- toast({- title: userToast.title,- description: backendMessage || userToast.fallbackDescription,- variant: "destructive",- });- return;- }-// 4. Transient network / abort — reconnect so the "Try Again" affordance// (persisted retryable-error marker on the session) lights up.if (isUserStoppingRef.current) return;