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): centralise paywall enforcement on all execute routes (NO_TIER bypass) (#13045)
Zamil Majdy555ed4a2d69b
diff --git a/autogpt_platform/backend/backend/api/conftest.py b/autogpt_platform/backend/backend/api/conftest.pyindex 7e8cc1aec6..ecb77bf20f 100644--- a/autogpt_platform/backend/backend/api/conftest.py+++ b/autogpt_platform/backend/backend/api/conftest.py@@ -40,3 +40,29 @@ def mock_jwt_admin(admin_user_id):}return {"get_jwt_payload": override_get_jwt_payload, "user_id": admin_user_id}++[email protected](autouse=True)+def _bypass_paywall(mocker):+ """Make every API test treat the user as paid by default.++ Tests have no real Supabase row backing the JWT, so without this+ bypass every paywalled route would 503 on the tier-lookup failure+ inside ``enforce_payment_paywall`` and every graph execution would+ raise ``UserPaywalledError`` from ``add_graph_execution`` once the+ paywall is wired up. Tests that specifically exercise the paywall+ (e.g. ``TestEnforcePaymentPaywall``, ``TestIsUserPaywalled``) live+ in ``rate_limit_test.py`` and patch ``_fetch_user_tier`` directly,+ bypassing this helper.+ """+ # Patch BOTH bindings: ``rate_limit.is_user_paywalled`` covers the+ # call inside ``enforce_payment_paywall`` (HTTP route dep); the+ # bound name in ``executor.utils.is_user_paywalled`` covers the+ # deep gate inside ``add_graph_execution`` for routes that go all+ # the way through (e.g. graph-execute). Patching only the source+ # module would miss the deep gate because ``utils.py`` does+ # ``from backend.copilot.rate_limit import is_user_paywalled`` at+ # import time.+ paywall_off = mocker.AsyncMock(return_value=False)+ mocker.patch("backend.copilot.rate_limit.is_user_paywalled", new=paywall_off)+ mocker.patch("backend.executor.utils.is_user_paywalled", new=paywall_off)diff --git a/autogpt_platform/backend/backend/api/external/fastapi_app.py b/autogpt_platform/backend/backend/api/external/fastapi_app.pyindex b55c918a74..69e9b6154d 100644--- a/autogpt_platform/backend/backend/api/external/fastapi_app.py+++ b/autogpt_platform/backend/backend/api/external/fastapi_app.py@@ -1,6 +1,9 @@from fastapi import FastAPI+from fastapi.requests import Request+from fastapi.responses import JSONResponsefrom backend.api.middleware.security import SecurityHeadersMiddleware+from backend.copilot.rate_limit import UserPaywalledErrorfrom backend.monitoring.instrumentation import instrument_fastapifrom .v1.routes import v1_router@@ -15,6 +18,15 @@ external_api = FastAPI(external_api.add_middleware(SecurityHeadersMiddleware)external_api.include_router(v1_router, prefix="/v1")++# UserPaywalledError raised at deep enqueue paths (e.g. add_graph_execution)+# maps to HTTP 402 here too — the external API is a separate FastAPI app+# so the handler in rest_api.py doesn't reach it.+@external_api.exception_handler(UserPaywalledError)+async def _user_paywalled_handler(_request: Request, exc: UserPaywalledError):+ return JSONResponse(status_code=402, content={"detail": str(exc)})++# Add Prometheus instrumentationinstrument_fastapi(external_api,diff --git a/autogpt_platform/backend/backend/api/external/v1/routes.py b/autogpt_platform/backend/backend/api/external/v1/routes.pyindex 25f18eb7df..2442c8496d 100644--- a/autogpt_platform/backend/backend/api/external/v1/routes.py+++ b/autogpt_platform/backend/backend/api/external/v1/routes.py@@ -13,6 +13,7 @@ import backend.api.features.store.db as store_dbimport backend.api.features.store.model as store_modelimport backend.blocksfrom backend.api.external.middleware import require_auth, require_permission+from backend.copilot.rate_limit import UserPaywalledError, enforce_payment_paywallfrom backend.data import execution as execution_dbfrom backend.data import graph as graph_dbfrom backend.data import user as user_db@@ -89,6 +90,13 @@ async def execute_graph_block(require_permission(APIKeyPermission.EXECUTE_BLOCK)),) -> CompletedBlockOutput:+ # Sync block exec doesn't pass through ``add_graph_execution`` (no+ # central enqueue), and external API routes use API-key auth instead+ # of JWT so the JWT-based dep doesn't apply either. Inline strict+ # gate with the same fail-closed (503-on-blip) posture as the dep —+ # consistent with chat / internal block / internal graph routes.+ await enforce_payment_paywall(auth.user_id)+obj = backend.blocks.get_block(block_id)if not obj:raise HTTPException(status_code=404, detail=f"Block #{block_id} not found.")@@ -159,6 +167,10 @@ async def execute_graph(require_permission(APIKeyPermission.EXECUTE_GRAPH)),) -> dict[str, Any]:+ # Strict route-level paywall: 503 on tier-lookup failure (rather+ # than fail-open via the deep gate inside add_graph_execution).+ # Consistent with the JWT-gated internal graph-execute route.+ await enforce_payment_paywall(auth.user_id)try:graph_exec = await add_graph_execution(graph_id=graph_id,@@ -167,6 +179,12 @@ async def execute_graph(graph_version=graph_version,)return {"id": graph_exec.id}+ except UserPaywalledError:+ # Defence-in-depth: even though the strict gate above catches+ # paywall before this point, leave the re-raise so the broad+ # ``except Exception`` doesn't accidentally collapse a future+ # deep-gate hit into HTTP 400.+ raiseexcept Exception as e:msg = str(e).encode().decode("unicode_escape")raise HTTPException(status_code=400, detail=msg)diff --git a/autogpt_platform/backend/backend/api/external/v1/routes_test.py b/autogpt_platform/backend/backend/api/external/v1/routes_test.pyindex 39e82b2ea5..7a443ca0bb 100644--- a/autogpt_platform/backend/backend/api/external/v1/routes_test.py+++ b/autogpt_platform/backend/backend/api/external/v1/routes_test.py@@ -6,8 +6,10 @@ import fastapi.testclientimport pytestfrom prisma.enums import APIKeyPermission+import backend.api.external.v1.routes as routes_modfrom backend.api.external.middleware import require_authfrom backend.api.external.v1.routes import v1_router+from backend.copilot.rate_limit import UserPaywalledErrorfrom backend.data.auth.base import APIAuthorizationInfofrom backend.util.exceptions import InsufficientBalanceError@@ -147,3 +149,58 @@ def test_unknown_block_returns_404(monkeypatch: pytest.MonkeyPatch):monkeypatch.setattr("backend.blocks.get_block", lambda _: None)response = client.post("/blocks/00000000-0000-0000-0000-deadbeef/execute", json={})assert response.status_code == 404+++def test_execute_graph_paywall_error_propagates_not_400(+ monkeypatch: pytest.MonkeyPatch,+):+ """The route's broad ``except Exception`` must NOT swallow+ ``UserPaywalledError`` — otherwise the 402 handler registered on+ ``external_api`` never fires and a paywalled user gets a confusing+ 400 instead of 402. Locks the explicit re-raise added for this gap.+ """+ monkeypatch.setattr(+ routes_mod,+ "add_graph_execution",+ AsyncMock(side_effect=UserPaywalledError("subscription required")),+ )++ with pytest.raises(UserPaywalledError):+ client.post(+ "/graphs/00000000-0000-0000-0000-000000000001/execute/1",+ json={"node_input": {}},+ )+++def test_execute_graph_other_errors_still_return_400(monkeypatch: pytest.MonkeyPatch):+ """Non-paywall exceptions retain the existing 400 behaviour — only+ ``UserPaywalledError`` is the surgical exception to the catch-all."""+ monkeypatch.setattr(+ routes_mod,+ "add_graph_execution",+ AsyncMock(side_effect=ValueError("graph missing")),+ )++ response = client.post(+ "/graphs/00000000-0000-0000-0000-000000000001/execute/1",+ json={"node_input": {}},+ )+ assert response.status_code == 400+ assert "graph missing" in response.json()["detail"]+++def test_execute_graph_block_paywall_propagates(monkeypatch: pytest.MonkeyPatch):+ """``execute_graph_block`` must let ``UserPaywalledError`` propagate+ out so the external app's 402 handler fires. There's no surrounding+ catch-all on this route, so we just lock the propagation."""+ monkeypatch.setattr(+ routes_mod,+ "enforce_payment_paywall",+ AsyncMock(side_effect=UserPaywalledError("subscription required")),+ )++ with pytest.raises(UserPaywalledError):+ client.post(+ "/blocks/00000000-0000-0000-0000-000000000001/execute",+ json={},+ )diff --git a/autogpt_platform/backend/backend/api/features/admin/diagnostics_admin_routes.py b/autogpt_platform/backend/backend/api/features/admin/diagnostics_admin_routes.pyindex 4cb8ff0729..cd3823a3f9 100644--- a/autogpt_platform/backend/backend/api/features/admin/diagnostics_admin_routes.py+++ b/autogpt_platform/backend/backend/api/features/admin/diagnostics_admin_routes.py@@ -412,12 +412,15 @@ async def requeue_single_execution(execution = executions[0]- # Use add_graph_execution in requeue mode+ # Use add_graph_execution in requeue mode. ``bypass_paywall=True``+ # because admins are recovering stuck executions on behalf of users+ # who may now be on NO_TIER — the original run was already gated.await add_graph_execution(graph_id=execution.graph_id,user_id=execution.user_id,graph_version=execution.graph_version,graph_exec_id=request.execution_id, # Requeue existing execution+ bypass_paywall=True,)return RequeueExecutionResponse(@@ -466,7 +469,9 @@ async def requeue_multiple_executions(message="No QUEUED executions found to requeue",)- # Requeue all executions in parallel using add_graph_execution+ # Requeue all executions in parallel using add_graph_execution.+ # ``bypass_paywall=True``: admin recovery on behalf of users who may+ # be on NO_TIER — original runs were already gated.async def requeue_one(exec) -> bool:try:await add_graph_execution(@@ -474,6 +479,7 @@ async def requeue_multiple_executions(user_id=exec.user_id,graph_version=exec.graph_version,graph_exec_id=exec.id, # Requeue existing+ bypass_paywall=True,)return Trueexcept Exception as e:@@ -905,7 +911,9 @@ async def requeue_all_stuck_executions(statuses=[AgentExecutionStatus.QUEUED],)- # Requeue all in parallel using add_graph_execution+ # Requeue all in parallel using add_graph_execution.+ # ``bypass_paywall=True``: admin recovery on behalf of users who may+ # be on NO_TIER — original runs were already gated.async def requeue_one(exec) -> bool:try:await add_graph_execution(@@ -913,6 +921,7 @@ async def requeue_all_stuck_executions(user_id=exec.user_id,graph_version=exec.graph_version,graph_exec_id=exec.id, # Requeue existing+ bypass_paywall=True,)return Trueexcept Exception as e:diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.pyindex 023ae22220..e88f706b0c 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py@@ -7,7 +7,7 @@ from typing import Annotatedfrom uuid import uuid4from autogpt_libs import auth-from fastapi import APIRouter, HTTPException, Query, Response, Security+from fastapi import APIRouter, Depends, HTTPException, Query, Response, Securityfrom fastapi.responses import StreamingResponsefrom pydantic import BaseModel, ConfigDict, Field, field_validator@@ -42,6 +42,7 @@ from backend.copilot.rate_limit import (RateLimitUnavailable,acquire_reset_lock,check_rate_limit,+ enforce_payment_paywall,get_daily_reset_count,get_global_rate_limits,get_usage_status,@@ -905,6 +906,7 @@ def _empty_ui_message_stream_response() -> StreamingResponse:@router.post("/sessions/{session_id}/stream",responses={+ 402: {"description": "Subscription required (NO_TIER user, paywall on)"},404: {"description": "Session not found or access denied"},429: {"description": "Cost rate-limit or call-frequency cap exceeded"},503: {@@ -913,6 +915,7 @@ def _empty_ui_message_stream_response() -> StreamingResponse:"header before retrying."},},+ dependencies=[Depends(enforce_payment_paywall)],)async def stream_chat_post(session_id: str,@@ -1002,8 +1005,10 @@ async def stream_chat_post()# Pre-turn rate limit check (cost-based, microdollars).- # check_rate_limit short-circuits internally when both limits are 0.- # Global defaults sourced from LaunchDarkly, falling back to config.+ # Entitlement (NO_TIER + ENABLE_PLATFORM_PAYMENT) is gated upstream by+ # the route-level ``enforce_payment_paywall`` dependency; here we only+ # enforce per-window USD caps. Global defaults sourced from+ # LaunchDarkly, falling back to config.if user_id:try:daily_limit, weekly_limit, _ = await get_global_rate_limits(diff --git a/autogpt_platform/backend/backend/api/features/chat/routes_test.py b/autogpt_platform/backend/backend/api/features/chat/routes_test.pyindex 9862edbaca..f20c044649 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes_test.py@@ -1124,10 +1124,20 @@ def test_stream_chat_accepts_exactly_max_length_message():"""A message exactly at max_length=64_000 must be accepted."""_mock_stream_internals(mocker)+ # Pass generous rate-limits so the rate-limit gate doesn't interfere+ # with the message-length validation under test. Pre-PR convention+ # used 0 for "unlimited"; we now treat 0 as "no spend allowed".mocker.patch("backend.api.features.chat.routes.get_global_rate_limits",new_callable=AsyncMock,- return_value=(0, 0, SubscriptionTier.BASIC),+ return_value=(1_000_000, 5_000_000, SubscriptionTier.BASIC),+ )+ # And mock the redis lookup so check_rate_limit sees zero usage.+ mock_redis = mocker.AsyncMock()+ mock_redis.get = mocker.AsyncMock(side_effect=["0", "0"])+ mocker.patch(+ "backend.copilot.rate_limit.get_redis_async",+ return_value=mock_redis,)response = client.post(diff --git a/autogpt_platform/backend/backend/api/features/library/routes/presets.py b/autogpt_platform/backend/backend/api/features/library/routes/presets.pyindex 98f2cb5f15..89f284f658 100644--- a/autogpt_platform/backend/backend/api/features/library/routes/presets.py+++ b/autogpt_platform/backend/backend/api/features/library/routes/presets.py@@ -2,8 +2,9 @@ import loggingfrom typing import Any, Optionalimport autogpt_libs.auth as autogpt_auth_lib-from fastapi import APIRouter, Body, HTTPException, Query, Security, status+from fastapi import APIRouter, Body, Depends, HTTPException, Query, Security, status+from backend.copilot.rate_limit import enforce_payment_paywallfrom backend.data.execution import GraphExecutionMetafrom backend.data.graph import get_graphfrom backend.data.integrations import get_webhook@@ -367,6 +368,11 @@ async def delete_preset(tags=["presets"],summary="Execute a preset",description="Execute a preset with the given graph and node input for the current user.",+ dependencies=[Depends(enforce_payment_paywall)],+ responses={+ 402: {"description": "Subscription required (NO_TIER user, paywall on)"},+ 503: {"description": "Subscription state temporarily unavailable"},+ },)async def execute_preset(preset_id: str,diff --git a/autogpt_platform/backend/backend/api/features/v1.py b/autogpt_platform/backend/backend/api/features/v1.pyindex 4f3bd283bf..7caeaae5f3 100644--- a/autogpt_platform/backend/backend/api/features/v1.py+++ b/autogpt_platform/backend/backend/api/features/v1.py@@ -15,6 +15,7 @@ from autogpt_libs.auth.jwt_utils import get_jwt_payloadfrom fastapi import (APIRouter,Body,+ Depends,File,HTTPException,Path,@@ -48,7 +49,7 @@ from backend.api.model import (UploadFileResponse,)from backend.blocks import get_block, get_blocks-from backend.copilot.rate_limit import get_tier_multipliers+from backend.copilot.rate_limit import enforce_payment_paywall, get_tier_multipliersfrom backend.data import execution as execution_dbfrom backend.data import graph as graph_dbfrom backend.data.auth import api_key as api_key_db@@ -462,7 +463,10 @@ async def get_graph_blocks() -> Response:path="/blocks/{block_id}/execute",summary="Execute graph block",tags=["blocks"],- dependencies=[Security(requires_user)],+ dependencies=[Security(requires_user), Depends(enforce_payment_paywall)],+ responses={+ 402: {"description": "Subscription required (NO_TIER user, paywall on)"},+ },)async def execute_graph_block(block_id: str, data: BlockInput, user_id: Annotated[str, Security(get_user_id)]@@ -1682,7 +1686,19 @@ async def update_graph_settings(path="/graphs/{graph_id}/execute/{graph_version}",summary="Execute graph agent",tags=["graphs"],- dependencies=[Security(requires_user)],+ dependencies=[Security(requires_user), Depends(enforce_payment_paywall)],+ # The route dep enforces fail-closed (503-on-blip) so a transient+ # Supabase outage surfaces as a retryable error, not a free run+ # for a paywalled user. The deep gate inside ``add_graph_execution``+ # still covers scheduled / webhook / copilot-internal runs that+ # don't pass through this route — those callers prefer fail-open+ # so background work doesn't abandon valid jobs during a blip.+ responses={+ 402: {+ "description": "Payment required: NO_TIER paywall, or insufficient credit balance"+ },+ 503: {"description": "Subscription state temporarily unavailable"},+ },)async def execute_graph(graph_id: str,diff --git a/autogpt_platform/backend/backend/api/rest_api.py b/autogpt_platform/backend/backend/api/rest_api.pyindex 7443dce954..0bb578e00d 100644--- a/autogpt_platform/backend/backend/api/rest_api.py+++ b/autogpt_platform/backend/backend/api/rest_api.py@@ -52,6 +52,7 @@ from backend.api.features.library.exceptions import (FolderValidationError,)from backend.blocks.llm import DEFAULT_LLM_MODEL+from backend.copilot.rate_limit import UserPaywalledErrorfrom backend.data.model import Credentialsfrom backend.integrations.providers import ProviderNamefrom backend.monitoring.instrumentation import instrument_fastapi@@ -307,6 +308,12 @@ app.add_exception_handler(RequestValidationError, validation_error_handler)app.add_exception_handler(pydantic.ValidationError, validation_error_handler)app.add_exception_handler(MissingConfigError, handle_internal_http_error(503))app.add_exception_handler(ValueError, handle_internal_http_error(400))+# UserPaywalledError raised at deep enqueue paths (e.g. add_graph_execution)+# maps to HTTP 402. log_error=False because it's not an error from the+# server's perspective — the user just lacks entitlement.+app.add_exception_handler(+ UserPaywalledError, handle_internal_http_error(402, log_error=False)+)app.add_exception_handler(PreconditionFailed, handle_internal_http_error(428))app.add_exception_handler(Exception, handle_internal_http_error(500))diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.pyindex cf1d00536d..4f066cf846 100644--- a/autogpt_platform/backend/backend/copilot/config.py+++ b/autogpt_platform/backend/backend/copilot/config.py@@ -154,13 +154,13 @@ class ChatConfig(BaseSettings):# the live per-tier values come from the COPILOT_*_COST_LIMIT flags.daily_cost_limit_microdollars: int = Field(default=1_000_000,- description="Max cost per day in microdollars, resets at midnight UTC "- "(0 = unlimited).",+ description="Max cost per day in microdollars, resets at midnight UTC. "+ "0 means no spend allowed (will block); there is no unlimited tier.",)weekly_cost_limit_microdollars: int = Field(default=5_000_000,- description="Max cost per week in microdollars, resets Monday 00:00 UTC "- "(0 = unlimited).",+ description="Max cost per week in microdollars, resets Monday 00:00 UTC. "+ "0 means no spend allowed (will block); there is no unlimited tier.",)# Cost (in credits / cents) to reset the daily rate limit using credits.diff --git a/autogpt_platform/backend/backend/copilot/rate_limit.py b/autogpt_platform/backend/backend/copilot/rate_limit.pyindex 96eae3192a..0d79e42be4 100644--- a/autogpt_platform/backend/backend/copilot/rate_limit.py+++ b/autogpt_platform/backend/backend/copilot/rate_limit.py@@ -56,6 +56,8 @@ import loggingfrom datetime import UTC, datetime, timedeltafrom enum import Enum+import fastapi+from autogpt_libs.auth.dependencies import get_user_idfrom prisma.models import User as PrismaUserfrom pydantic import BaseModel, Fieldfrom redis.exceptions import RedisClusterException, RedisError@@ -64,6 +66,7 @@ from backend.data.db_accessors import user_dbfrom backend.data.redis_client import AsyncRedisClient, get_redis_asyncfrom backend.data.user import get_user_by_idfrom backend.util.cache import cached+from backend.util.feature_flag import Flag, get_feature_flag_value, is_feature_enabledlogger = logging.getLogger(__name__)@@ -152,9 +155,6 @@ async def _fetch_tier_multipliers_flag() -> dict[SubscriptionTier, float] | Nonenon-numeric / non-positive values are skipped; callers merge whateversurvives into :data:`_DEFAULT_TIER_MULTIPLIERS`."""- # Lazy import: rate_limit -> feature_flag -> settings -> ... -> rate_limit.- from backend.util.feature_flag import Flag, get_feature_flag_value-raw = await get_feature_flag_value(Flag.COPILOT_TIER_MULTIPLIERS.value, "system", None)@@ -197,9 +197,6 @@ async def _fetch_cost_limits_flag() -> dict[str, int] | None:are skipped so a broken key degrades to the config default instead ofwiping out the limit."""- # Lazy import: rate_limit -> feature_flag -> settings -> ... -> rate_limit.- from backend.util.feature_flag import Flag, get_feature_flag_value-raw = await get_feature_flag_value(Flag.COPILOT_COST_LIMITS.value, "system", None)if raw is None:return None@@ -273,9 +270,6 @@ async def _fetch_workspace_storage_limits_flag() -> dict[SubscriptionTier, int]negative values are skipped so a broken key degrades to the code defaultinstead of wiping out the limit."""- # Lazy import: rate_limit -> feature_flag -> settings -> ... -> rate_limit.- from backend.util.feature_flag import Flag, get_feature_flag_value-raw = await get_feature_flag_value(Flag.COPILOT_TIER_WORKSPACE_STORAGE_LIMITS.value, "system", None)@@ -343,7 +337,9 @@ class UsageWindow(BaseModel):used: intlimit: int = Field(description="Maximum microdollars of spend allowed in this window. "- "0 means unlimited."+ "0 means no spend allowed (the user is over-cap immediately); there "+ "is no unlimited tier — the public model uses ``None`` for "+ "no-cap-configured.")resets_at: datetime@@ -404,8 +400,20 @@ class CoPilotUsagePublic(BaseModel):"""Project the internal status onto the client-safe schema."""def window(w: UsageWindow) -> UsageWindowPublic | None:- if w.limit <= 0:+ if w.limit < 0:+ # Defensive: nothing produces a negative limit today, but+ # treat it as "no cap configured" → hide the window from+ # the UI rather than divide-by-negative.return None+ if w.limit == 0:+ # Limit of 0 means "no spend allowed" — surface as fully+ # exhausted so the UI shows the user as blocked instead+ # of silently treating it as null/unlimited (which was+ # the source of the original autopilot paywall bypass).+ return UsageWindowPublic(+ percent_used=100.0,+ resets_at=w.resets_at,+ )# When at/over the cap, snap to exactly 100.0 so the UI's# rounded display and its exhaustion check (`percent_used >= 100`)# agree. Without this, e.g. 99.95% would render as "100% used"@@ -459,6 +467,25 @@ 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,@@ -470,8 +497,10 @@ async def get_usage_status(Args:user_id: The user's ID.- daily_cost_limit: Max microdollars of spend per day (0 = unlimited).- weekly_cost_limit: Max microdollars of spend per week (0 = unlimited).+ daily_cost_limit: Max microdollars of spend per day. 0 means no spend+ allowed (over-cap immediately).+ weekly_cost_limit: Max microdollars of spend per week. 0 means no+ spend allowed (over-cap immediately).rate_limit_reset_cost: Credit cost (cents) to reset daily limit (0 = disabled).tier: The user's rate-limit tier (included in the response).@@ -524,28 +553,27 @@ async def get_remaining_usd_budget(user is close to their actual cap, and to feed the baseline path'sper-turn budget hint via :func:`build_budget_ctx`.- A limit of ``0`` is treated as unlimited. Both limits unlimited →- ``float('inf')``.+ A limit of ``0`` is treated as "no spend allowed" — remaining = 0+ on that window. There is no real-world unlimited tier; callers+ should not pass 0 expecting it to mean "no cap".Failure modes:- * Both limits unlimited → ``float('inf')`` (no Redis call).* Redis brown-out → ``floor_usd`` (so callers using the value- as a soft hint don't pretend the user has unlimited budget;- the pre-turn gate has already failed closed at 503 in this- case, so we only land here from observability paths).+ as a soft hint don't pretend the user has full budget; the+ pre-turn gate has already failed closed at 503 in this case,+ so we only land here from observability paths).Args:user_id: The user's ID.- daily_cost_limit: Daily cap in microdollars (0 = unlimited).- weekly_cost_limit: Weekly cap in microdollars (0 = unlimited).+ daily_cost_limit: Daily cap in microdollars. 0 = no spend allowed+ on this window.+ weekly_cost_limit: Weekly cap in microdollars. 0 = no spend allowed+ on this window.floor_usd: Lower bound on the returned value (USD). Avoidshanding the SDK a degenerate ``$0`` budget that would refuseto start a turn. Set to ``0.0`` when the caller wants afaithful "no remaining budget" signal instead of a floor."""- if daily_cost_limit <= 0 and weekly_cost_limit <= 0:- return float("inf")-now = datetime.now(UTC)try:redis = await get_redis_async()@@ -559,12 +587,15 @@ async def get_remaining_usd_budget(logger.warning("Redis unavailable for remaining-budget lookup, returning floor")return floor_usd+ # ``>= 0`` (not ``> 0``): a limit of 0 is "no spend allowed", so the+ # remaining is 0 on that window. Mirrors check_rate_limit's semantics+ # — there is no unlimited tier, so we never short-circuit to float(inf).remaining_microdollars = float("inf")- if daily_cost_limit > 0:+ if daily_cost_limit >= 0:remaining_microdollars = min(remaining_microdollars, max(0, daily_cost_limit - daily_used))- if weekly_cost_limit > 0:+ if weekly_cost_limit >= 0:remaining_microdollars = min(remaining_microdollars, max(0, weekly_cost_limit - weekly_used))@@ -591,7 +622,8 @@ async def build_budget_ctx(Returns ``""`` when:* no ``user_id`` is available,- * the user's tier limits are unlimited (no cap to surface),+ * the user has 0 remaining budget (they shouldn't be here — the+ paywall dep raises 402 first — but defend against drift),* Redis is unavailable (we'd rather emit nothing than amisleading ``$0.00`` hint — the pre-turn gate already failsclosed at 503 in that case).@@ -603,8 +635,6 @@ async def build_budget_ctx(default_daily_cost_limit,default_weekly_cost_limit,)- if daily_limit <= 0 and weekly_limit <= 0:- return ""remaining = await get_remaining_usd_budget(user_id=user_id,daily_cost_limit=daily_limit,@@ -630,10 +660,17 @@ async def check_rate_limit() -> None:"""Check if user is within rate limits.- Raises :class:`RateLimitExceeded` when the user is over their cap, and- :class:`RateLimitUnavailable` when Redis is unreachable so the caller- must fail closed (HTTP 503) — the daily/weekly USD caps are real money- and cannot be bypassed by a Redis brown-out.+ Raises :class:`RateLimitExceeded` when the user is at-or-over their cap+ and :class:`RateLimitUnavailable` when Redis is unreachable so the+ caller must fail closed (HTTP 503) — the daily/weekly USD caps are+ real money and cannot be bypassed by a Redis brown-out.++ A limit of ``0`` means "no spend allowed", not "unlimited" — there is+ no real-world unlimited tier. Routes that want to skip rate-limiting+ entirely should not call this function. Entitlement (``NO_TIER`` ++ ``ENABLE_PLATFORM_PAYMENT``) is enforced upstream by the route+ dependency :func:`enforce_payment_paywall`, so this function is+ purely about per-window USD usage.This is a pre-turn soft check. The authoritative usage counter is updatedby ``record_cost_usage()`` after the turn completes. Under concurrency,@@ -641,11 +678,6 @@ async def check_rate_limit(This is acceptable because cost-based limits are approximate by nature(the exact cost is unknown until after generation)."""- # Short-circuit: when both limits are 0 (unlimited) skip the Redis- # round-trip entirely.- if daily_cost_limit <= 0 and weekly_cost_limit <= 0:- return-now = datetime.now(UTC)try:redis = await get_redis_async()@@ -672,10 +704,15 @@ async def check_rate_limit(logger.warning("Rate limit state unreadable, rejecting request: %s", exc)raise RateLimitUnavailable() from exc- if daily_cost_limit > 0 and daily_used >= daily_cost_limit:+ # ``>= 0`` (not ``> 0``): a limit of 0 means "no spend allowed", and+ # any usage at or above 0 is over-cap. The previous ``> 0`` check+ # silently treated 0 as unlimited, which collided with the multiplier-+ # collapse semantics of :func:`get_global_rate_limits` and produced+ # the autopilot paywall bypass.+ if daily_cost_limit >= 0 and daily_used >= daily_cost_limit:raise RateLimitExceeded("daily", _daily_reset_time(now=now))- if weekly_cost_limit > 0 and weekly_used >= weekly_cost_limit:+ if weekly_cost_limit >= 0 and weekly_used >= weekly_cost_limit:raise RateLimitExceeded("weekly", _weekly_reset_time(now=now))@@ -870,10 +907,18 @@ async def _fetch_user_tier(user_id: str) -> SubscriptionTier:Only successful lookups are cached. Missing users raise``_UserNotFoundError`` so ``@cached`` never stores the fallback.++ Distinguishes "row genuinely missing" (``user_db().get_user_by_id``+ raises ``ValueError``) from "transient DB / Prisma error" (other+ exceptions propagate as-is). Without this distinction a Supabase+ blip would silently degrade every paying user to NO_TIER and+ ``enforce_payment_paywall`` would 402 them — contradicting its+ 503-on-blip contract."""try:user = await user_db().get_user_by_id(user_id)- except Exception:+ except ValueError:+ # ValueError = "User not found" per get_user_by_id's contract.raise _UserNotFoundError(user_id)if user.subscription_tier:return SubscriptionTier(user.subscription_tier)@@ -1059,20 +1104,17 @@ async def get_global_rate_limits(# Apply tier multiplier — resolved through LD (copilot-tier-multipliers)# so multipliers can be tuned without a deploy. Falls back to the defaults- # when LD is unavailable.+ # when LD is unavailable. NO_TIER+paywall is gated upstream by+ # ``enforce_payment_paywall`` (HTTP 402 before this is reached); the+ # only NO_TIER path that gets here is the beta cohort (flag off), which+ # falls back to BASIC limits so testers retain access.tier = await get_user_tier(user_id)multipliers = await get_tier_multipliers()multiplier = multipliers.get(tier.value, 1.0)- # NO_TIER's 0.0 multiplier is the backend half of the paywall — it- # collapses limits to zero so unsubscribed users can't run the chat.- # Only enforce that gate when the platform-payment flag is on for this- # user; in the beta cohort (flag off) NO_TIER falls back to BASIC's- # baseline so the e2e suite and beta testers retain access.- if tier == SubscriptionTier.NO_TIER:- from backend.util.feature_flag import Flag, is_feature_enabled-- if not await is_feature_enabled(Flag.ENABLE_PLATFORM_PAYMENT, user_id):- multiplier = multipliers.get(SubscriptionTier.BASIC.value, 1.0)+ if tier == SubscriptionTier.NO_TIER and not await is_feature_enabled(+ Flag.ENABLE_PLATFORM_PAYMENT, user_id+ ):+ multiplier = multipliers.get(SubscriptionTier.BASIC.value, 1.0)if multiplier != 1.0:# Cast back to int to preserve the microdollar integer contract# downstream — fractional LD multipliers (e.g. 8.5×) truncate at the@@ -1142,3 +1184,103 @@ def _weekly_reset_time(now: datetime | None = None) -> datetime:return now.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=days_until_monday)+++# ---------------------------------------------------------------------------+# Paywall enforcement+# ---------------------------------------------------------------------------+#+# Two primitives. Every gate uses one or the other:+#+# 1. ``is_user_paywalled(user_id) -> bool`` — the check. Lookup errors+# propagate so callers decide their own failure-mode.+# 2. ``enforce_payment_paywall(user_id)`` — HTTP gate. Wraps (1) and+# raises ``UserPaywalledError`` (handler → 402) when paywalled, or+# ``HTTPException(503)`` + Retry-After on lookup error. Doubles as+# JWT route dep AND inline call from non-JWT routes (the explicit+# ``user_id`` arg overrides the ``Security`` default).+#+# Background callers (scheduled cron, webhook handlers, copilot internal+# tools, ``add_graph_execution``) skip (2) and use (1) directly + raise+# ``UserPaywalledError`` inline, so the framework's own retry layer+# catches lookup errors — synthesising an ``HTTPException`` would be+# wrong shape for non-HTTP contexts.+#+++async def is_user_paywalled(user_id: str) -> bool:+ """Return ``True`` if the user has no entitlement to paywalled features.++ A user with no DB tier record (``_UserNotFoundError`` — fresh signup+ that hasn't been provisioned yet, or row missing) is treated as+ ``NO_TIER`` here: paywalled iff ``ENABLE_PLATFORM_PAYMENT`` is on.+ Without this branch the missing-tier case would propagate as a 500+ in any caller that doesn't already have a generic ``except`` (e.g.+ the external API ``execute_graph_block`` route).++ Other tier-lookup errors propagate — callers decide (route → 503,+ background job → fail-open).+ """+ try:+ tier = await _fetch_user_tier(user_id)+ except _UserNotFoundError:+ # No DB row / no subscription_tier set — fresh signup that hasn't+ # been provisioned yet, or row missing entirely. Logged at debug+ # so ops can correlate "402s on fresh signups" with provisioning+ # gaps without spamming the warning level.+ logger.debug(+ "is_user_paywalled: tier lookup empty for %s, treating as NO_TIER",+ user_id[:8],+ )+ tier = SubscriptionTier.NO_TIER+ if tier != SubscriptionTier.NO_TIER:+ return False+ return await is_feature_enabled(Flag.ENABLE_PLATFORM_PAYMENT, user_id)+++async def enforce_payment_paywall(+ user_id: str = fastapi.Security(get_user_id),+) -> None:+ """HTTP paywall gate — fail-closed on lookup error.++ Two call shapes, same behaviour:++ 1. **JWT route dep**:+ ``dependencies=[Depends(enforce_payment_paywall)]`` — FastAPI+ auto-fills ``user_id`` from the JWT via the ``Security``+ default. Mirrors the ``requires_admin_user`` pattern.++ 2. **Non-JWT route (inline)**: ``await enforce_payment_paywall(+ auth.user_id)`` — for API-key-auth routes (external API) where+ the JWT-based ``Security`` default doesn't apply; the explicit+ positional argument wins.++ Raises :class:`UserPaywalledError` (handled by the app-level+ exception handler → HTTP 402) if the user is on NO_TIER with+ ``ENABLE_PLATFORM_PAYMENT`` on. Tier-lookup failures map to+ **HTTP 503 + Retry-After** so the client retries instead of+ treating a transient Supabase / LD blip as a permanent paywall.++ Background callers (scheduled cron, webhook handlers, copilot+ internal tools) skip this function and call :func:`is_user_paywalled`+ directly + raise inline, so the background framework's own error+ handling decides what to do on lookup failure (typically: retry+ on next tick) instead of synthesising an HTTP-shaped exception.+ """+ try:+ paywalled = await is_user_paywalled(user_id)+ except Exception as exc:+ logger.warning(+ "enforce_payment_paywall: tier lookup failed for %s: %s",+ user_id[:8],+ exc,+ )+ raise fastapi.HTTPException(+ status_code=503,+ detail="Subscription state temporarily unavailable, retry shortly.",+ headers={"Retry-After": "30"},+ ) from exc+ if paywalled:+ raise UserPaywalledError(+ "A subscription is required to use this feature. Upgrade to continue."+ )diff --git a/autogpt_platform/backend/backend/copilot/rate_limit_test.py b/autogpt_platform/backend/backend/copilot/rate_limit_test.pyindex c9390a3efd..19ddb504c5 100644--- a/autogpt_platform/backend/backend/copilot/rate_limit_test.py+++ b/autogpt_platform/backend/backend/copilot/rate_limit_test.py@@ -11,6 +11,7 @@ from .rate_limit import (_DEFAULT_TIER_WORKSPACE_STORAGE_MB,DEFAULT_TIER,TIER_MULTIPLIERS,+ CoPilotUsagePublic,CoPilotUsageStatus,RateLimitExceeded,RateLimitUnavailable,@@ -299,28 +300,402 @@ class TestCheckRateLimit:)@pytest.mark.asyncio- async def test_skips_redis_and_does_not_raise_unavailable_when_unlimited(self):- """When both limits are 0 (unlimited) we must not even attempt Redis,- so a brown-out cannot 503 unlimited users."""- with patch(- "backend.copilot.rate_limit.get_redis_async",- side_effect=ConnectionError("Redis down"),- ) as mock_get:- # Should not raise — short-circuited before touching Redis.- await check_rate_limit(_USER, daily_cost_limit=0, weekly_cost_limit=0)- mock_get.assert_not_called()-- @pytest.mark.asyncio- async def test_skips_check_when_limit_is_zero(self):+ async def test_zero_limit_means_blocked_not_unlimited(self):+ """A limit of 0 means "no spend allowed", not "unlimited" — there+ is no real-world unlimited tier. This is the regression behind the+ autopilot paywall bypass: the previous ``> 0`` check let the+ multiplier-collapse path (which returns 0 for paywalled users) slip+ through silently."""mock_redis = AsyncMock()- mock_redis.get = AsyncMock(side_effect=["999999", "999999"])-+ mock_redis.get = AsyncMock(side_effect=["0", "0"])with patch("backend.copilot.rate_limit.get_redis_async",return_value=mock_redis,):- # Should not raise — limits of 0 mean unlimited- await check_rate_limit(_USER, daily_cost_limit=0, weekly_cost_limit=0)+ with pytest.raises(RateLimitExceeded) as exc_info:+ await check_rate_limit(_USER, daily_cost_limit=0, weekly_cost_limit=0)+ assert exc_info.value.window == "daily"+++class TestCoPilotUsagePublicFromStatus:+ """Public-shape projection must surface a 0-limit window as fully+ exhausted (the user is paywalled / blocked), not as `None`. Hiding+ the window would tell the UI "no cap configured" — the same silent+ bypass that motivated this PR."""++ @staticmethod+ def _status(daily_limit: int, weekly_limit: int) -> CoPilotUsageStatus:+ from .rate_limit import UsageWindow++ return CoPilotUsageStatus(+ daily=UsageWindow(+ used=0,+ limit=daily_limit,+ resets_at=_daily_reset_time(),+ ),+ weekly=UsageWindow(+ used=0,+ limit=weekly_limit,+ resets_at=_weekly_reset_time(),+ ),+ tier=DEFAULT_TIER,+ reset_cost=0,+ )++ def test_zero_limit_renders_as_fully_exhausted(self):+ public = CoPilotUsagePublic.from_status(self._status(0, 0))+ assert public.daily is not None+ assert public.daily.percent_used == 100.0+ assert public.weekly is not None+ assert public.weekly.percent_used == 100.0++ def test_positive_limit_renders_normally(self):+ # daily limit $10, used $0 → 0% used+ from .rate_limit import UsageWindow++ status = CoPilotUsageStatus(+ daily=UsageWindow(used=0, limit=10_000_000, resets_at=_daily_reset_time()),+ weekly=UsageWindow(+ used=5_000_000, limit=10_000_000, resets_at=_weekly_reset_time()+ ),+ tier=DEFAULT_TIER,+ reset_cost=0,+ )+ public = CoPilotUsagePublic.from_status(status)+ assert public.daily is not None+ assert public.daily.percent_used == 0.0+ assert public.weekly is not None+ assert public.weekly.percent_used == 50.0+++class TestEnforcePaymentPaywall:+ """The dep bypasses ``get_user_tier``'s fail-open default and goes+ through ``_fetch_user_tier`` directly so a transient DB blip raises+ 503 instead of silently 402-ing every paid user."""++ @pytest.mark.asyncio+ async def test_blocks_no_tier_user_when_flag_on(self, mocker):+ """Headline behaviour: NO_TIER + ENABLE_PLATFORM_PAYMENT on raises+ :class:`UserPaywalledError`. The app-level exception handler in+ ``rest_api.py`` (and the external API's ``fastapi_app.py``) maps+ this to HTTP 402 — the dep itself doesn't construct an+ HTTPException so all gates share the same exception type."""+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(return_value=SubscriptionTier.NO_TIER),+ )+ mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=True),+ )+ from .rate_limit import UserPaywalledError, enforce_payment_paywall++ with pytest.raises(UserPaywalledError) as exc_info:+ await enforce_payment_paywall(_USER)+ assert "subscription" in str(exc_info.value).lower()++ @pytest.mark.asyncio+ async def test_allows_no_tier_user_when_flag_off(self, mocker):+ """Beta cohort: flag off → NO_TIER user passes through (no exception)."""+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(return_value=SubscriptionTier.NO_TIER),+ )+ mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=False),+ )+ from .rate_limit import enforce_payment_paywall++ await enforce_payment_paywall(_USER) # must not raise++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "tier",+ [+ SubscriptionTier.BASIC,+ SubscriptionTier.PRO,+ SubscriptionTier.MAX,+ SubscriptionTier.BUSINESS,+ SubscriptionTier.ENTERPRISE,+ ],+ )+ async def test_allows_paid_tier_even_when_flag_on(self, mocker, tier):+ """Even with the flag on, paid tiers must never be blocked."""+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(return_value=tier),+ )+ is_feature_enabled_mock = mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=True),+ )+ from .rate_limit import enforce_payment_paywall++ await enforce_payment_paywall(_USER) # must not raise+ # Short-circuits on tier check; flag lookup never happens for paid+ # tiers (avoids an unnecessary LD round-trip per paywalled route).+ is_feature_enabled_mock.assert_not_called()+++class TestIsUserPaywalled:+ """``is_user_paywalled`` is the shared core check used by both the+ route-level dep and the function-level gate inside ``add_graph_execution``.+ Strict: propagates lookup failures so the caller decides how to respond+ (route → 503; background job → fail-open)."""++ @pytest.mark.asyncio+ async def test_no_tier_with_flag_on_is_paywalled(self, mocker):+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(return_value=SubscriptionTier.NO_TIER),+ )+ mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=True),+ )+ from .rate_limit import is_user_paywalled++ assert await is_user_paywalled(_USER) is True++ @pytest.mark.asyncio+ async def test_no_tier_with_flag_off_is_not_paywalled(self, mocker):+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(return_value=SubscriptionTier.NO_TIER),+ )+ mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=False),+ )+ from .rate_limit import is_user_paywalled++ assert await is_user_paywalled(_USER) is False++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "tier",+ [+ SubscriptionTier.BASIC,+ SubscriptionTier.PRO,+ SubscriptionTier.MAX,+ SubscriptionTier.BUSINESS,+ SubscriptionTier.ENTERPRISE,+ ],+ )+ async def test_paid_tier_short_circuits_without_flag_lookup(self, mocker, tier):+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(return_value=tier),+ )+ flag_mock = mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=True),+ )+ from .rate_limit import is_user_paywalled++ assert await is_user_paywalled(_USER) is False+ flag_mock.assert_not_called()++ @pytest.mark.asyncio+ async def test_propagates_lookup_failure(self, mocker):+ """The helper does NOT swallow generic tier-lookup errors — callers+ decide. The route-dep maps to 503; background callers+ (add_graph_execution) fail open."""+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(side_effect=RuntimeError("DB down")),+ )+ from .rate_limit import is_user_paywalled++ with pytest.raises(RuntimeError):+ await is_user_paywalled(_USER)++ @pytest.mark.asyncio+ async def test_user_not_found_treated_as_no_tier_when_flag_on(self, mocker):+ """``_UserNotFoundError`` (no DB row, or no ``subscription_tier``+ set yet on a fresh signup) is treated as NO_TIER so callers without+ a generic ``except`` (e.g. the external API ``execute_graph_block``+ route) get a real 402 instead of a leaked 500."""+ from .rate_limit import _UserNotFoundError, is_user_paywalled++ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(side_effect=_UserNotFoundError(_USER)),+ )+ mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=True),+ )+ assert await is_user_paywalled(_USER) is True++ @pytest.mark.asyncio+ async def test_user_not_found_treated_as_no_tier_when_flag_off(self, mocker):+ """Beta cohort: missing tier + flag off → not paywalled (same+ passthrough as a real NO_TIER user with the flag off)."""+ from .rate_limit import _UserNotFoundError, is_user_paywalled++ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(side_effect=_UserNotFoundError(_USER)),+ )+ mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=False),+ )+ assert await is_user_paywalled(_USER) is False+++class TestUserPaywalledError:+ def test_default_message(self):+ from .rate_limit import UserPaywalledError++ assert "subscription" in str(UserPaywalledError()).lower()++ def test_custom_message(self):+ from .rate_limit import UserPaywalledError++ assert str(UserPaywalledError("custom")) == "custom"+++class TestFetchUserTierErrorPropagation:+ """``_fetch_user_tier`` must distinguish "row missing" (raises+ ``ValueError`` via ``get_user_by_id``) from "transient DB error"+ (Prisma connection failure etc). Without this distinction a+ Supabase blip would silently degrade every paying user to NO_TIER+ and ``enforce_payment_paywall`` would 402 them — contradicting its+ 503-on-blip contract. Regression locked here."""++ @pytest.fixture(autouse=True)+ def _clear_cache(self):+ from .rate_limit import _fetch_user_tier++ _fetch_user_tier.cache_clear() # type: ignore[attr-defined]++ @pytest.mark.asyncio+ async def test_value_error_collapses_to_user_not_found(self, mocker):+ """ValueError from get_user_by_id is the documented "missing user"+ signal — it's correct to map this to NO_TIER (via+ _UserNotFoundError → caught in is_user_paywalled)."""+ mock_db = mocker.MagicMock()+ mock_db.get_user_by_id = AsyncMock(side_effect=ValueError("User not found"))+ mocker.patch("backend.copilot.rate_limit.user_db", return_value=mock_db)+ from .rate_limit import _fetch_user_tier, _UserNotFoundError++ with pytest.raises(_UserNotFoundError):+ await _fetch_user_tier(_USER)++ @pytest.mark.asyncio+ async def test_db_outage_propagates(self, mocker):+ """Non-ValueError exceptions (Prisma connection failure, generic+ RuntimeError) propagate as-is so route layer can map to 503."""+ mock_db = mocker.MagicMock()+ mock_db.get_user_by_id = AsyncMock(side_effect=RuntimeError("Prisma down"))+ mocker.patch("backend.copilot.rate_limit.user_db", return_value=mock_db)+ from .rate_limit import _fetch_user_tier++ with pytest.raises(RuntimeError, match="Prisma down"):+ await _fetch_user_tier(_USER)++ @pytest.mark.asyncio+ async def test_db_outage_does_not_collapse_to_402_for_paying_user(self, mocker):+ """End-to-end: a Supabase blip during enforce_payment_paywall+ for any user (paying or not) maps to 503, NOT 402. Locks the+ contract advertised in enforce_payment_paywall's docstring."""+ mock_db = mocker.MagicMock()+ mock_db.get_user_by_id = AsyncMock(side_effect=RuntimeError("Supabase blip"))+ mocker.patch("backend.copilot.rate_limit.user_db", return_value=mock_db)+ from fastapi import HTTPException++ from .rate_limit import enforce_payment_paywall++ with pytest.raises(HTTPException) as exc_info:+ await enforce_payment_paywall(_USER)+ assert exc_info.value.status_code == 503 # not 402+ assert exc_info.value.headers.get("Retry-After") == "30"+++class TestEnforcePaymentPaywallInline:+ """``enforce_payment_paywall`` doubles as both a JWT route dep AND an+ inline call from non-JWT routes (external API graph + block execute).+ These tests cover the inline form — the dep form is exercised+ transitively by the chat / internal-block route tests via FastAPI."""++ @pytest.mark.asyncio+ async def test_blocks_paywalled_user(self, mocker):+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(return_value=SubscriptionTier.NO_TIER),+ )+ mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=True),+ )+ from .rate_limit import UserPaywalledError, enforce_payment_paywall++ with pytest.raises(UserPaywalledError):+ await enforce_payment_paywall(_USER)++ @pytest.mark.asyncio+ async def test_passes_paid_user(self, mocker):+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(return_value=SubscriptionTier.PRO),+ )+ from .rate_limit import enforce_payment_paywall++ await enforce_payment_paywall(_USER) # must not raise++ @pytest.mark.asyncio+ async def test_db_failure_raises_503(self, mocker):+ """Lookup failure → 503 + Retry-After (not 402, not 500). This is+ the asymmetry fix: external API + internal graph routes used to+ fail-open via the deep gate; now they fail-closed at the route."""+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(side_effect=RuntimeError("DB down")),+ )+ from fastapi import HTTPException++ from .rate_limit import enforce_payment_paywall++ with pytest.raises(HTTPException) as exc_info:+ await enforce_payment_paywall(_USER)+ assert exc_info.value.status_code == 503+ assert exc_info.value.headers.get("Retry-After") == "30"+++class TestEnforcePaymentPaywallContinued:+ """Additional ``enforce_payment_paywall`` cases that depend on the+ helpers above. Kept separate so the related ``TestIsUserPaywalled``+ block stays compact above."""++ @pytest.mark.asyncio+ async def test_db_failure_raises_503_not_402(self, mocker):+ """Transient DB outage during tier lookup must NOT 402 every paid+ user (the silent-bypass concern raised by sentry/CodeRabbit on+ the previous round). It must surface as 503 with a Retry-After+ header so the client retries shortly instead of treating the+ outage as a permanent paywall."""+ mocker.patch(+ "backend.copilot.rate_limit._fetch_user_tier",+ new=AsyncMock(side_effect=RuntimeError("DB down")),+ )+ is_feature_enabled_mock = mocker.patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=True),+ )+ from fastapi import HTTPException++ from .rate_limit import enforce_payment_paywall++ with pytest.raises(HTTPException) as exc_info:+ await enforce_payment_paywall(_USER)+ assert exc_info.value.status_code == 503+ assert exc_info.value.headers.get("Retry-After") == "30"+ # Flag lookup never runs when tier is indeterminate — we don't+ # know which cohort to apply.+ is_feature_enabled_mock.assert_not_called()# ---------------------------------------------------------------------------@@ -492,7 +867,7 @@ class TestGetTierMultipliers:async def test_defaults_when_flag_unset(self):"""With no LD override, the resolver returns the default map."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,return_value=None,):@@ -503,7 +878,7 @@ class TestGetTierMultipliers:async def test_ld_override(self):"""LD override populates the targeted tiers; others inherit defaults."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,return_value={"PRO": 7.5, "BUSINESS": 25},):@@ -522,7 +897,7 @@ class TestGetTierMultipliers:async def test_invalid_json_falls_back(self):"""A non-object LD value (string, list, bool) falls back to defaults."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,return_value="broken",):@@ -533,7 +908,7 @@ class TestGetTierMultipliers:async def test_unknown_tier_key_skipped(self):"""Unknown tier keys and non-positive values are silently ignored."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,return_value={"PRO": 3, "BOGUS": 99, "MAX": -1, "BUSINESS": "nope"},):@@ -550,7 +925,7 @@ class TestGetTierMultipliers:async def test_ld_failure_falls_back(self):"""LD lookup raising propagates to defaults, not up the call stack."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,side_effect=RuntimeError("LD SDK not initialized"),):@@ -568,7 +943,7 @@ class TestGetWorkspaceStorageLimits:async def test_defaults_when_flag_unset(self):"""With no LD override, the resolver returns the default map."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,return_value=None,):@@ -581,7 +956,7 @@ class TestGetWorkspaceStorageLimits:async def test_ld_override(self):"""LD override populates targeted tiers; others inherit defaults."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,return_value={"NO_TIER": 300, "PRO": 2048},):@@ -598,7 +973,7 @@ class TestGetWorkspaceStorageLimits:async def test_invalid_json_falls_back(self):"""A non-object LD value falls back to defaults."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,return_value="broken",):@@ -611,7 +986,7 @@ class TestGetWorkspaceStorageLimits:async def test_unknown_tier_key_and_invalid_values_skipped(self):"""Unknown tiers and invalid values degrade to defaults per key."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,return_value={"NO_TIER": 300, "BOGUS": 999, "MAX": -1, "BUSINESS": "nope"},):@@ -627,7 +1002,7 @@ class TestGetWorkspaceStorageLimits:async def test_ld_failure_falls_back(self):"""LD lookup raising propagates to defaults, not up the call stack."""with patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",new_callable=AsyncMock,side_effect=RuntimeError("LD SDK not initialized"),):@@ -665,7 +1040,7 @@ class TestGetGlobalRateLimitsCostLimitsFlag:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=_ld,),):@@ -690,7 +1065,7 @@ class TestGetGlobalRateLimitsCostLimitsFlag:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=_ld,),):@@ -714,7 +1089,7 @@ class TestGetGlobalRateLimitsCostLimitsFlag:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=_ld,),):@@ -738,7 +1113,7 @@ class TestGetGlobalRateLimitsCostLimitsFlag:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=_ld,),caplog.at_level("WARNING"),@@ -767,7 +1142,7 @@ class TestGetGlobalRateLimitsCostLimitsFlag:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=_ld,),):@@ -793,7 +1168,7 @@ class TestGetGlobalRateLimitsCostLimitsFlag:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=_ld,),):@@ -829,7 +1204,7 @@ class TestGetGlobalRateLimitsCostLimitsFlag:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=_ld,),):@@ -1152,7 +1527,7 @@ class TestGetGlobalRateLimitsWithTiers:return_value=SubscriptionTier.PRO,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=_ld,),):@@ -1177,7 +1552,7 @@ class TestGetGlobalRateLimitsWithTiers:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(2_500_000, 12_500_000),),):@@ -1199,7 +1574,7 @@ class TestGetGlobalRateLimitsWithTiers:return_value=SubscriptionTier.PRO,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(2_500_000, 12_500_000),),):@@ -1221,7 +1596,7 @@ class TestGetGlobalRateLimitsWithTiers:return_value=SubscriptionTier.MAX,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(2_500_000, 12_500_000),),):@@ -1243,7 +1618,7 @@ class TestGetGlobalRateLimitsWithTiers:return_value=SubscriptionTier.BUSINESS,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(2_500_000, 12_500_000),),):@@ -1265,7 +1640,7 @@ class TestGetGlobalRateLimitsWithTiers:return_value=SubscriptionTier.ENTERPRISE,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(2_500_000, 12_500_000),),):@@ -1277,6 +1652,44 @@ class TestGetGlobalRateLimitsWithTiers:assert weekly == 750_000_000assert tier == SubscriptionTier.ENTERPRISE+ @pytest.mark.asyncio+ async def test_no_tier_with_payment_disabled_upgrades_to_basic(self):+ """Beta cohort regression test: a NO_TIER user passing through+ the route-level paywall (because ENABLE_PLATFORM_PAYMENT is off)+ must NOT then hit (0, 0) at check_rate_limit. The fallback to+ BASIC's multiplier (1.0) keeps daily/weekly intact so beta+ testers retain access without bypassing usage caps."""++ async def _ld(flag_key: str, _uid: str, default):+ if "cost-limits" in flag_key.lower():+ return {"daily": 1_000_000, "weekly": 5_000_000}+ return default++ with (+ patch(+ "backend.copilot.rate_limit.get_user_tier",+ new=AsyncMock(return_value=SubscriptionTier.NO_TIER),+ ),+ patch(+ "backend.copilot.rate_limit.get_feature_flag_value",+ side_effect=_ld,+ ),+ patch(+ "backend.copilot.rate_limit.is_feature_enabled",+ new=AsyncMock(return_value=False), # beta cohort+ ),+ ):+ daily, weekly, tier = await get_global_rate_limits(+ _USER, 1_000_000, 5_000_000+ )++ # BASIC's multiplier is 1.0 → limits arrive non-zero, not the+ # NO_TIER multiplier-collapsed (0, 0) that would slip past+ # check_rate_limit's pre-PR `> 0` short-circuit.+ assert daily == 1_000_000+ assert weekly == 5_000_000+ assert tier == SubscriptionTier.NO_TIER+# ---------------------------------------------------------------------------# End-to-end: tier limits are respected by check_rate_limit@@ -1320,7 +1733,7 @@ class TestTierLimitsRespected:return_value=SubscriptionTier.PRO,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -1353,7 +1766,7 @@ class TestTierLimitsRespected:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -1387,7 +1800,7 @@ class TestTierLimitsRespected:return_value=SubscriptionTier.ENTERPRISE,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -1534,7 +1947,7 @@ class TestTierLimitsEnforced:return_value=SubscriptionTier.PRO,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -1564,7 +1977,7 @@ class TestTierLimitsEnforced:return_value=SubscriptionTier.PRO,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -1598,7 +2011,7 @@ class TestTierLimitsEnforced:return_value=SubscriptionTier.BUSINESS,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -1629,7 +2042,7 @@ class TestTierLimitsEnforced:return_value=SubscriptionTier.PRO,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -1657,7 +2070,7 @@ class TestTierLimitsEnforced:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -1697,7 +2110,7 @@ class TestTierLimitsEnforced:return_value=SubscriptionTier.BASIC,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -1740,7 +2153,7 @@ class TestTierLimitsEnforced:return_value=SubscriptionTier.BUSINESS,),patch(- "backend.util.feature_flag.get_feature_flag_value",+ "backend.copilot.rate_limit.get_feature_flag_value",side_effect=self._ld_side_effect(self._BASE_DAILY, self._BASE_WEEKLY),),patch(@@ -2141,12 +2554,19 @@ class TestWarnIfStripeSubscriptionDriftsYearly:class TestGetRemainingUsdBudget:@pytest.mark.asyncio- async def test_returns_inf_when_both_limits_unlimited(self):- # No Redis call expected — the function short-circuits on 0/0.- result = await get_remaining_usd_budget(- _USER, daily_cost_limit=0, weekly_cost_limit=0- )- assert result == float("inf")+ async def test_zero_limits_return_floor_not_unlimited(self):+ """With no real-world unlimited tier, both limits at 0 means+ "no spend allowed" — remaining is 0 → floor_usd is returned."""+ mock_redis = AsyncMock()+ mock_redis.get = AsyncMock(side_effect=["0", "0"])+ with patch(+ "backend.copilot.rate_limit.get_redis_async",+ return_value=mock_redis,+ ):+ result = await get_remaining_usd_budget(+ _USER, daily_cost_limit=0, weekly_cost_limit=0, floor_usd=0.5+ )+ assert result == 0.5@pytest.mark.asyncioasync def test_smaller_of_daily_and_weekly_remaining(self):@@ -2167,7 +2587,8 @@ class TestGetRemainingUsdBudget:@pytest.mark.asyncioasync def test_floor_applies_when_user_at_or_over_cap(self):- # daily=$5 used $5 → 0 remaining → floor returned.+ # daily=$5 used $5 → 0 remaining → floor returned. Weekly cap kept+ # well above usage so it doesn't drive the result.mock_redis = AsyncMock()mock_redis.get = AsyncMock(side_effect=["5000000", "0"])with patch(@@ -2177,7 +2598,7 @@ class TestGetRemainingUsdBudget:result = await get_remaining_usd_budget(_USER,daily_cost_limit=5_000_000,- weekly_cost_limit=0,+ weekly_cost_limit=50_000_000,floor_usd=0.5,)assert result == pytest.approx(0.5)@@ -2197,7 +2618,9 @@ class TestGetRemainingUsdBudget:assert result == 0.5@pytest.mark.asyncio- async def test_only_daily_cap_configured(self):+ async def test_daily_drives_when_weekly_is_loose(self):+ """Both limits constrain; whichever leaves less budget wins. Here a+ tight daily ($10 - $1 = $9) wins over a loose weekly."""mock_redis = AsyncMock()mock_redis.get = AsyncMock(side_effect=["1000000", None])with patch(@@ -2207,7 +2630,7 @@ class TestGetRemainingUsdBudget:result = await get_remaining_usd_budget(_USER,daily_cost_limit=10_000_000,- weekly_cost_limit=0,+ weekly_cost_limit=1_000_000_000,)assert result == pytest.approx(9.0)@@ -2233,7 +2656,11 @@ class TestBuildBudgetCtx:assert result == ""@pytest.mark.asyncio- async def test_returns_empty_when_unlimited(self):+ async def test_returns_empty_when_remaining_is_zero(self):+ """No real-world unlimited tier — when the multiplier-collapse path+ returns (0, 0, NO_TIER) the user has $0 remaining → empty hint+ (the paywall dep should already have rejected this turn at 402;+ we just defend against drift)."""with patch("backend.copilot.rate_limit.get_global_rate_limits",new=AsyncMock(return_value=(0, 0, DEFAULT_TIER)),@@ -2247,13 +2674,14 @@ class TestBuildBudgetCtx:@pytest.mark.asyncioasync def test_returns_inner_text_with_remaining_in_dollars(self):- # daily=$10 used $4.50 → $5.50 remaining.+ # daily=$10 used $4.50 → $5.50 daily remaining; weekly cap kept+ # well above usage so daily drives the result.mock_redis = AsyncMock()mock_redis.get = AsyncMock(side_effect=["4500000", "0"])with (patch("backend.copilot.rate_limit.get_global_rate_limits",- new=AsyncMock(return_value=(10_000_000, 0, DEFAULT_TIER)),+ new=AsyncMock(return_value=(10_000_000, 1_000_000_000, DEFAULT_TIER)),),patch("backend.copilot.rate_limit.get_redis_async",@@ -2263,7 +2691,7 @@ class TestBuildBudgetCtx:block = await build_budget_ctx(user_id=_USER,default_daily_cost_limit=10_000_000,- default_weekly_cost_limit=0,+ default_weekly_cost_limit=1_000_000_000,)# No tag wrap — that's inject_user_context's job.assert "<budget_context>" not in block@@ -2278,7 +2706,7 @@ class TestBuildBudgetCtx:with (patch("backend.copilot.rate_limit.get_global_rate_limits",- new=AsyncMock(return_value=(10_000_000, 0, DEFAULT_TIER)),+ new=AsyncMock(return_value=(10_000_000, 1_000_000_000, DEFAULT_TIER)),),patch("backend.copilot.rate_limit.get_redis_async",@@ -2288,12 +2716,12 @@ class TestBuildBudgetCtx:block = await build_budget_ctx(user_id=_USER,default_daily_cost_limit=10_000_000,- default_weekly_cost_limit=0,+ default_weekly_cost_limit=1_000_000_000,)assert block == ""@pytest.mark.asyncio- async def test_returns_empty_when_remaining_is_zero(self):+ async def test_returns_empty_when_at_cap(self):"""At-or-over cap → no hint (the pre-turn gate has alreadyrejected this turn at 429; we only emit a hint when there'sactual headroom to communicate)."""@@ -2302,7 +2730,7 @@ class TestBuildBudgetCtx:with (patch("backend.copilot.rate_limit.get_global_rate_limits",- new=AsyncMock(return_value=(10_000_000, 0, DEFAULT_TIER)),+ new=AsyncMock(return_value=(10_000_000, 1_000_000_000, DEFAULT_TIER)),),patch("backend.copilot.rate_limit.get_redis_async",@@ -2312,6 +2740,6 @@ class TestBuildBudgetCtx:block = await build_budget_ctx(user_id=_USER,default_daily_cost_limit=10_000_000,- default_weekly_cost_limit=0,+ default_weekly_cost_limit=1_000_000_000,)assert block == ""diff --git a/autogpt_platform/backend/backend/data/generate_data_test.py b/autogpt_platform/backend/backend/data/generate_data_test.pyindex 649b57bdda..7f144bc59b 100644--- a/autogpt_platform/backend/backend/data/generate_data_test.py+++ b/autogpt_platform/backend/backend/data/generate_data_test.py@@ -30,6 +30,7 @@ async def _create_test_user(user_id: str) -> None:})except UniqueViolationError:+ # Idempotent test setup: row already exists from a prior run.pass@@ -46,6 +47,7 @@ async def _create_graph(graph_id: str, user_id: str, name: str) -> None:})except UniqueViolationError:+ # Idempotent test setup: row already exists from a prior run.passdiff --git a/autogpt_platform/backend/backend/executor/conftest.py b/autogpt_platform/backend/backend/executor/conftest.pynew file mode 100644index 0000000000..550bcf1e59--- /dev/null+++ b/autogpt_platform/backend/backend/executor/conftest.py@@ -0,0 +1,29 @@+"""Common test fixtures for executor tests.++Note: Common fixtures like ``test_user_id`` are defined in the parent+conftest.py (``backend/conftest.py``) and are available here automatically.+"""++import pytest++[email protected](autouse=True)+def _bypass_paywall(mocker):+ """Make every executor test treat the user as paid by default.++ ``add_graph_execution`` now raises ``UserPaywalledError`` for NO_TIER+ users when ``ENABLE_PLATFORM_PAYMENT`` is on. Tests in this module+ pass synthetic user_ids that don't have a Supabase row, so the tier+ lookup either fails or returns NO_TIER and trips the gate. Bypass it+ here so the executor tests can exercise their own logic without+ concerning themselves with paywall state. Tests that specifically+ exercise the paywall live in ``backend/copilot/rate_limit_test.py``.+ """+ # Patch the *bound* name in executor.utils (where the gate fires).+ # ``utils.py`` does ``from backend.copilot.rate_limit import is_user_paywalled``+ # at import time, so patching ``backend.copilot.rate_limit.is_user_paywalled``+ # alone does not affect the call site here.+ mocker.patch(+ "backend.executor.utils.is_user_paywalled",+ new=mocker.AsyncMock(return_value=False),+ )diff --git a/autogpt_platform/backend/backend/executor/utils.py b/autogpt_platform/backend/backend/executor/utils.pyindex ea3821e308..ff676842b1 100644--- a/autogpt_platform/backend/backend/executor/utils.py+++ b/autogpt_platform/backend/backend/executor/utils.py@@ -11,6 +11,7 @@ from pydantic import BaseModel, JsonValue, ValidationErrorfrom backend.blocks import get_blockfrom backend.blocks._base import Block, BlockCostType, BlockType+from backend.copilot.rate_limit import UserPaywalledError, is_user_paywalledfrom backend.data import execution as execution_dbfrom backend.data import graph as graph_dbfrom backend.data import human_review as human_review_db@@ -1156,6 +1157,8 @@ async def add_graph_execution(execution_context: Optional[ExecutionContext] = None,graph_exec_id: Optional[str] = None,dry_run: bool = False,+ *,+ bypass_paywall: bool = False,) -> GraphExecutionWithNodes:"""Adds a graph execution to the queue and returns the execution entry.@@ -1175,12 +1178,31 @@ async def add_graph_execution(nodes_input_masks: Node inputs to use in the execution.parent_graph_exec_id: The ID of the parent graph execution (for nested executions).graph_exec_id: If provided, resume this existing execution instead of creating a new one.+ bypass_paywall: Skip the per-user paywall check. Set ONLY for admin+ recovery paths (requeueing stuck executions on behalf of a user+ who may be on NO_TIER) — never for user-initiated runs.Returns:GraphExecutionWithNodes: The execution entry.Raises:ValueError: If the graph is not found or if there are validation errors.NotFoundError: If graph_exec_id is provided but execution is not found.+ UserPaywalledError: If the user is on NO_TIER and ``ENABLE_PLATFORM_PAYMENT``+ is on for them, **unless** ``bypass_paywall=True``. Raised here+ so every entry point — HTTP routes, scheduled cron, webhook+ triggers, external API, internal copilot tools — gets the same+ gate without each having to remember a route-level dependency.+ Exception: Tier-lookup errors propagate as-is. The HTTP routes that+ call into ``add_graph_execution`` already wrap with+ ``enforce_payment_paywall`` upstream (which maps lookup failure+ to 503), so by the time we get here those callers have a fresh+ check. Background callers (scheduled jobs, webhook handlers,+ copilot tool runs) catch the exception in their own retry+ framework — failing now is preferable to silently giving a+ paywalled user a free run during an outage."""+ if not bypass_paywall and await is_user_paywalled(user_id):+ raise UserPaywalledError("A subscription is required to run agents.")+if prisma.is_connected():edb = execution_dbudb = user_dbdiff --git a/autogpt_platform/backend/backend/executor/utils_test.py b/autogpt_platform/backend/backend/executor/utils_test.pyindex 9b87a8886f..6235c51089 100644--- a/autogpt_platform/backend/backend/executor/utils_test.py+++ b/autogpt_platform/backend/backend/executor/utils_test.py@@ -1735,3 +1735,68 @@ def test_make_node_credentials_input_map_excludes_auto_creds(# Verify no auto-credential phantom entriesif isinstance(value, dict):assert "_credentials_id" not in value+++# ============================================================================+# Admin-bypass paywall: requeue stuck executions for users on NO_TIER must+# not be blocked by the paywall gate (Sentry bug prediction: admin recovery+# would otherwise raise UserPaywalledError on the original user's behalf).+# ============================================================================+++async def test_add_graph_execution_paywall_blocks_paywalled_user(+ mocker: MockerFixture,+):+ """Sanity: with ``bypass_paywall=False`` (default), a paywalled user+ is rejected before any DB work happens."""+ from backend.copilot.rate_limit import UserPaywalledError++ mocker.patch(+ "backend.executor.utils.is_user_paywalled",+ new=mocker.AsyncMock(return_value=True),+ )+ mock_edb = mocker.patch("backend.executor.utils.execution_db")+ mock_edb.create_graph_execution = mocker.AsyncMock()++ with pytest.raises(UserPaywalledError):+ await add_graph_execution(+ graph_id="g",+ user_id="paywalled-user",+ )++ mock_edb.create_graph_execution.assert_not_called()+++async def test_add_graph_execution_bypass_paywall_skips_check(+ mocker: MockerFixture,+):+ """``bypass_paywall=True`` skips the paywall gate entirely — admin+ requeue paths must succeed even when the original user is NO_TIER."""+ paywall_mock = mocker.patch(+ "backend.executor.utils.is_user_paywalled",+ new=mocker.AsyncMock(return_value=True),+ )+ # Stub everything downstream — we only care that the gate didn't fire.+ mocker.patch("backend.executor.utils.prisma").is_connected.return_value = True+ mocker.patch("backend.executor.utils.user_db")+ mocker.patch("backend.executor.utils.graph_db")+ mocker.patch("backend.executor.utils.workspace_db")+ mocker.patch("backend.executor.utils.onboarding_db")+ mock_edb = mocker.patch("backend.executor.utils.execution_db")+ # Force an early sentinel error AFTER the gate so we can verify the+ # gate was passed without simulating the whole requeue pipeline.+ mock_edb.get_graph_execution = mocker.AsyncMock(+ side_effect=RuntimeError("reached requeue lookup")+ )++ with pytest.raises(RuntimeError, match="reached requeue lookup"):+ await add_graph_execution(+ graph_id="g",+ user_id="paywalled-user",+ graph_exec_id="stuck-exec-id",+ bypass_paywall=True,+ )++ paywall_mock.assert_not_called()diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 7915854522..0bd220e6ba 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -1662,6 +1662,9 @@"401": {"$ref": "#/components/responses/HTTP401NotAuthenticatedError"},+ "402": {+ "description": "Subscription required (NO_TIER user, paywall on)"+ },"422": {"description": "Validation Error","content": {@@ -2594,6 +2597,9 @@"401": {"$ref": "#/components/responses/HTTP401NotAuthenticatedError"},+ "402": {+ "description": "Subscription required (NO_TIER user, paywall on)"+ },"404": { "description": "Session not found or access denied" },"422": {"description": "Validation Error",@@ -4234,6 +4240,9 @@"401": {"$ref": "#/components/responses/HTTP401NotAuthenticatedError"},+ "402": {+ "description": "Payment required: NO_TIER paywall, or insufficient credit balance"+ },"422": {"description": "Validation Error","content": {@@ -4241,7 +4250,8 @@"schema": { "$ref": "#/components/schemas/HTTPValidationError" }}}- }+ },+ "503": { "description": "Subscription state temporarily unavailable" }}}},@@ -6529,6 +6539,9 @@"401": {"$ref": "#/components/responses/HTTP401NotAuthenticatedError"},+ "402": {+ "description": "Subscription required (NO_TIER user, paywall on)"+ },"422": {"description": "Validation Error","content": {@@ -6536,7 +6549,8 @@"schema": { "$ref": "#/components/schemas/HTTPValidationError" }}}- }+ },+ "503": { "description": "Subscription state temporarily unavailable" }}}},