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/executor): fail a run that was denied by entitlement (#14060)

Nicholas Tindlea160f7e74424
diff --git a/autogpt_platform/backend/backend/copilot/rate_limit.py b/autogpt_platform/backend/backend/copilot/rate_limit.py
index 12147a54da..9ee1fc786e 100644
--- a/autogpt_platform/backend/backend/copilot/rate_limit.py
+++ b/autogpt_platform/backend/backend/copilot/rate_limit.py
@@ -66,6 +66,7 @@ from backend.data.db_accessors import credit_db, user_db
from backend.data.redis_client import AsyncRedisClient, get_redis_async
from backend.data.user import get_user_by_id
from backend.util.cache import cached
+from backend.util.exceptions import UserPaywalledError
from backend.util.feature_flag import Flag, get_feature_flag_value, is_feature_enabled
logger = logging.getLogger(__name__)
@@ -467,25 +468,6 @@ class RateLimitUnavailable(Exception):
"""
-class UserPaywalledError(Exception):
- """User has no entitlement to run a paywalled feature (NO_TIER tier
- + ``ENABLE_PLATFORM_PAYMENT`` on).
-
- Raised by ``add_graph_execution`` and other deep enqueue paths so
- that *every* execution entry point (HTTP route, scheduled cron,
- webhook trigger, external API, internal copilot tool) gets the same
- gate without each one having to remember a route-level dependency.
- Routes wrap this into HTTP 402; background tasks log and abandon
- the run.
- """
-
- def __init__(
- self,
- message: str = "A subscription is required to run this feature.",
- ) -> None:
- super().__init__(message)
-
-
async def get_usage_status(
user_id: str,
daily_cost_limit: int,
diff --git a/autogpt_platform/backend/backend/executor/activity_status_generator.py b/autogpt_platform/backend/backend/executor/activity_status_generator.py
index c45e65c75f..0e3f8f2226 100644
--- a/autogpt_platform/backend/backend/executor/activity_status_generator.py
+++ b/autogpt_platform/backend/backend/executor/activity_status_generator.py
@@ -50,6 +50,13 @@ INSUFFICIENT_BALANCE_SUMMARY = (
"This run couldn't complete because there weren't enough credits available. "
f"{INSUFFICIENT_BALANCE_GUIDANCE}"
)
+ENTITLEMENT_REQUIRED_GUIDANCE = (
+ "Upgrade the plan, or switch the step to an option your plan includes."
+)
+ENTITLEMENT_REQUIRED_SUMMARY = (
+ "This run couldn't complete because it used a feature the current plan "
+ f"doesn't include. {ENTITLEMENT_REQUIRED_GUIDANCE}"
+)
# Default system prompt template for activity status generation
@@ -222,6 +229,12 @@ def _get_deterministic_failure_response(
"correctness_score": 0.0,
}
+ if execution_stats.failure_reason == ExecutionFailureReason.ENTITLEMENT_REQUIRED:
+ return {
+ "activity_status": ENTITLEMENT_REQUIRED_SUMMARY,
+ "correctness_score": 0.0,
+ }
+
return None
diff --git a/autogpt_platform/backend/backend/executor/activity_status_generator_test.py b/autogpt_platform/backend/backend/executor/activity_status_generator_test.py
index b3d722f227..c97b68c2c3 100644
--- a/autogpt_platform/backend/backend/executor/activity_status_generator_test.py
+++ b/autogpt_platform/backend/backend/executor/activity_status_generator_test.py
@@ -1112,6 +1112,20 @@ class TestDeterministicFailureResponse:
assert "enough credits" in result["activity_status"].lower()
assert "billing owner" not in result["activity_status"].lower()
+ def test_entitlement_failure_skips_llm_analysis(self):
+ """A plan-gated denial is as deterministic as an empty wallet. Without
+ a handler the run still reaches the LLM for a summary of something
+ already known — the exact cost this module exists to avoid."""
+ stats = GraphExecutionStats(
+ failure_reason=ExecutionFailureReason.ENTITLEMENT_REQUIRED
+ )
+
+ result = _get_deterministic_failure_response(stats, ExecutionStatus.FAILED)
+
+ assert result is not None
+ assert result["correctness_score"] == 0.0
+ assert "plan" in result["activity_status"].lower()
+
@pytest.mark.parametrize(
"status",
[ExecutionStatus.COMPLETED, ExecutionStatus.TERMINATED, None],
@@ -1234,3 +1248,15 @@ class TestDeterministicFailureResponse:
else:
assert result is None
mock_get_openai_client.assert_called_once()
+
+
+def test_paywall_is_a_known_graph_execution_error():
+ """Anything outside this set pages via Discord as an unexpected failure.
+ A plan-gated denial is a known business outcome, so leaving it out produced
+ a spurious "Unknown Graph Execution Error" alert with a stack trace — the
+ same reasoning that keeps it out of Sentry in util/metrics."""
+ from backend.executor.manager import KNOWN_GRAPH_EXECUTION_ERRORS
+ from backend.util.exceptions import UserPaywalledError
+
+ assert issubclass(UserPaywalledError, Exception)
+ assert UserPaywalledError in KNOWN_GRAPH_EXECUTION_ERRORS
diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.py
index a21dee4eb2..646171e852 100644
--- a/autogpt_platform/backend/backend/executor/manager.py
+++ b/autogpt_platform/backend/backend/executor/manager.py
@@ -65,11 +65,11 @@ from backend.util.decorator import (
time_measured,
)
from backend.util.exceptions import (
- ExecutionFailureReason,
GraphNotFoundError,
InsufficientBalanceError,
ModerationError,
NotFoundError,
+ UserPaywalledError,
get_execution_failure_reason,
)
from backend.util.file import clean_exec_files
@@ -139,9 +139,10 @@ def _record_execution_failure(
``error`` always reflects the *latest* failure, so the message a user sees
describes what actually terminated the run.
- ``failure_reason`` is *sticky*: once a typed failure (currently only
- :class:`InsufficientBalanceError`) has been promoted from a node via
- :func:`_propagate_node_failure`, a later untyped error cannot clear it.
+ ``failure_reason`` is *sticky*: once a typed failure
+ (:class:`InsufficientBalanceError` or :class:`UserPaywalledError`) has been
+ promoted from a node via :func:`_propagate_node_failure`, a later untyped
+ error cannot clear it.
An exhausted wallet is the root cause of whatever fails next, and losing
that reason would send the run back to LLM analysis — the exact cost this
module exists to avoid. Only another typed classification may replace it.
@@ -156,6 +157,17 @@ def _record_execution_failure(
execution_stats.error = str(error) or type(error).__name__
+# Terminal conditions the platform already understands. Anything outside this
+# set is treated as an unexpected failure and pages via Discord, so a known
+# business outcome left out of it produces a spurious "Unknown Graph Execution
+# Error" alert with a stack trace.
+KNOWN_GRAPH_EXECUTION_ERRORS = (
+ InsufficientBalanceError,
+ ModerationError,
+ UserPaywalledError,
+)
+
+
def _propagate_node_failure(
graph_stats: GraphExecutionStats,
node_error: BaseException,
@@ -1326,11 +1338,14 @@ class ExecutionProcessor:
# Determine final execution status based on whether there was an error or termination
if cancel.is_set():
execution_status = ExecutionStatus.TERMINATED
- elif (
- error is not None
- or execution_stats.failure_reason
- == ExecutionFailureReason.INSUFFICIENT_BALANCE
- ):
+ elif error is not None or execution_stats.failure_reason is not None:
+ # Any typed failure reason means the run is terminally broken,
+ # not merely that a node errored. Untyped node errors stay
+ # non-fatal, since a graph may deliberately wire an error
+ # output onward; only classified, trusted failures promote to
+ # graph stats via `_propagate_node_failure`. Before this, a
+ # paywalled run reported COMPLETED with error=null while the
+ # node inside carried the real denial.
execution_status = ExecutionStatus.FAILED
else:
if db_client.has_pending_reviews_for_graph_exec(
@@ -1353,8 +1368,7 @@ class ExecutionProcessor:
)
_record_execution_failure(execution_stats, error)
- known_errors = (InsufficientBalanceError, ModerationError)
- if isinstance(error, known_errors):
+ if isinstance(error, KNOWN_GRAPH_EXECUTION_ERRORS):
return ExecutionStatus.FAILED
execution_status = ExecutionStatus.FAILED
diff --git a/autogpt_platform/backend/backend/util/exceptions.py b/autogpt_platform/backend/backend/util/exceptions.py
index 77396d1275..cdccd54c2d 100644
--- a/autogpt_platform/backend/backend/util/exceptions.py
+++ b/autogpt_platform/backend/backend/util/exceptions.py
@@ -112,10 +112,30 @@ class InsufficientBalanceError(ValueError):
return self.message
+class UserPaywalledError(Exception):
+ """User has no entitlement to run a paywalled feature (NO_TIER tier
+ + ``ENABLE_PLATFORM_PAYMENT`` on).
+
+ Raised by ``add_graph_execution`` and other deep enqueue paths so
+ that *every* execution entry point (HTTP route, scheduled cron,
+ webhook trigger, external API, internal copilot tool) gets the same
+ gate without each one having to remember a route-level dependency.
+ Routes wrap this into HTTP 402; background tasks log and abandon
+ the run.
+ """
+
+ def __init__(
+ self,
+ message: str = "A subscription is required to run this feature.",
+ ) -> None:
+ super().__init__(message)
+
+
class ExecutionFailureReason(str, Enum):
"""Structured reasons for terminal graph execution failures."""
INSUFFICIENT_BALANCE = "insufficient_balance"
+ ENTITLEMENT_REQUIRED = "entitlement_required"
# Keep this legacy-only fallback synchronized with the equivalent predicates in
@@ -143,6 +163,8 @@ def get_execution_failure_reason(
"""Classify trusted exceptions, with an opt-in fallback for persisted text."""
if isinstance(error, InsufficientBalanceError):
return ExecutionFailureReason.INSUFFICIENT_BALANCE
+ if isinstance(error, UserPaywalledError):
+ return ExecutionFailureReason.ENTITLEMENT_REQUIRED
if (
allow_legacy_text
and isinstance(error, str)
diff --git a/autogpt_platform/backend/backend/util/exceptions_test.py b/autogpt_platform/backend/backend/util/exceptions_test.py
index 208abda730..b8bb645389 100644
--- a/autogpt_platform/backend/backend/util/exceptions_test.py
+++ b/autogpt_platform/backend/backend/util/exceptions_test.py
@@ -11,6 +11,7 @@ from backend.util.exceptions import (
BlockUnknownError,
ExecutionFailureReason,
InsufficientBalanceError,
+ UserPaywalledError,
get_execution_failure_reason,
)
@@ -105,6 +106,37 @@ def test_typed_insufficient_balance_error_is_classified_by_type():
)
+def test_paywall_error_is_classified_by_type():
+ """A paywalled node failure is a trusted, terminal, user-actionable
+ failure — the same class as an exhausted wallet. Without a typed reason it
+ never reached graph stats, so a denied run reported COMPLETED with a null
+ error while the node inside carried the real denial."""
+ error = UserPaywalledError("A Max plan or higher is required to use ChatGPT.")
+
+ assert (
+ get_execution_failure_reason(error)
+ == ExecutionFailureReason.ENTITLEMENT_REQUIRED
+ )
+
+
+def test_paywall_error_is_still_importable_from_rate_limit():
+ """It moved to break an import cycle; every existing call site imports it
+ from its old home."""
+ from backend.copilot.rate_limit import UserPaywalledError as ReExported
+
+ assert ReExported is UserPaywalledError
+
+
+def test_untyped_entitlement_wording_is_not_classified():
+ """Classification is by type, never by message text."""
+ assert (
+ get_execution_failure_reason(
+ RuntimeError("A Max plan or higher is required to use ChatGPT.")
+ )
+ is None
+ )
+
+
def test_analytics_sql_gates_structured_credit_failures_on_failed_status():
sql = _read_graph_execution_sql()
diff --git a/autogpt_platform/backend/backend/util/metrics.py b/autogpt_platform/backend/backend/util/metrics.py
index 953f5a62ad..fb29e33986 100644
--- a/autogpt_platform/backend/backend/util/metrics.py
+++ b/autogpt_platform/backend/backend/util/metrics.py
@@ -196,7 +196,8 @@ def _before_send(event, hint):
if any(kw in exc_msg for kw in _USER_AUTH_KEYWORDS):
return None
- # Expected business logic — insufficient balance
+ # Expected business logic — exhausted wallet, or a plan that does
+ # not include the feature. Neither is a platform bug.
if get_execution_failure_reason(exc_value) is not None:
return None
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index 215543fd59..9121733272 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -18802,7 +18802,7 @@
},
"ExecutionFailureReason": {
"type": "string",
- "enum": ["insufficient_balance"],
+ "enum": ["insufficient_balance", "entitlement_required"],
"title": "ExecutionFailureReason",
"description": "Structured reasons for terminal graph execution failures."
},