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.

feat(backend/copilot): check the AutoPilot allowance wherever a turn starts, bill AutoPilot block turns to credits past it, and say which limit decides (#15396)

Nicholas Tindle406dcd16443e
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes_test.py b/autogpt_platform/backend/backend/api/features/chat/routes_test.py
index 9fa1b491a6..25ca52b8d4 100644
--- a/autogpt_platform/backend/backend/api/features/chat/routes_test.py
+++ b/autogpt_platform/backend/backend/api/features/chat/routes_test.py
@@ -1644,6 +1644,34 @@ def test_usage_uses_config_limits(
)
+def test_usage_reports_the_limit_that_blocks_the_next_turn(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """Both windows used up: the client is told which one it waits for and
+ when that refills, computed server-side from the meters' own status."""
+ _mock_usage(mocker, daily_used=10000, weekly_used=50000)
+
+ response = client.get("/usage")
+
+ assert response.status_code == 200
+ data = response.json()
+ assert data["state"] == "exhausted"
+ assert data["limiting_window"] == "weekly"
+ assert data["resets_at"] == data["weekly"]["resets_at"]
+
+
+def test_usage_state_is_ok_with_room_left(mocker: pytest_mock.MockerFixture) -> None:
+ _mock_usage(mocker, daily_used=500, weekly_used=2000)
+
+ data = client.get("/usage").json()
+
+ assert (data["state"], data["limiting_window"], data["resets_at"]) == (
+ "ok",
+ None,
+ None,
+ )
+
+
def test_usage_rejects_unauthenticated_request() -> None:
"""GET /usage should return 401 when no valid JWT is provided."""
unauthenticated_app = fastapi.FastAPI()
diff --git a/autogpt_platform/backend/backend/blocks/autopilot.py b/autogpt_platform/backend/backend/blocks/autopilot.py
index 435fd8483f..c06fa499c1 100644
--- a/autogpt_platform/backend/backend/blocks/autopilot.py
+++ b/autogpt_platform/backend/backend/blocks/autopilot.py
@@ -5,6 +5,7 @@ import contextvars
import json
import logging
import uuid
+from datetime import UTC
from enum import Enum
from typing import TYPE_CHECKING, Any, Literal
@@ -22,6 +23,11 @@ from backend.copilot.active_turns import (
MAX_TURN_LIFETIME_SECONDS,
running_turn_limit_message,
)
+from backend.copilot.constants import (
+ AUTOPILOT_BLOCK_ID,
+ AUTOPILOT_BLOCK_TOOL_NAME,
+ PER_TURN_SPEND_FLOOR_USD,
+)
from backend.copilot.permissions import (
DISABLED_LEGACY_TOOL_NAMES,
CopilotPermissions,
@@ -32,20 +38,21 @@ from backend.copilot.permissions import (
from backend.data.model import CredentialsField, CredentialsMetaInput, SchemaField
from backend.integrations.providers import ProviderName
from backend.util.exceptions import BlockExecutionError
+from backend.util.settings import Settings
if TYPE_CHECKING:
+ from backend.copilot.turn_pause import TurnPause
from backend.data.execution import ExecutionContext
logger = logging.getLogger(__name__)
-# Block ID shared between autopilot.py and copilot prompting.py.
-AUTOPILOT_BLOCK_ID = "c069dc6b-c3ed-4c12-b6e5-d47361e64ce6"
-
# Identifiers used when registering an AutoPilotBlock turn with the
# stream registry — distinguishes block-originated turns from sub-session
-# or HTTP SSE turns in logs / observability.
+# or HTTP SSE turns in logs / observability. The tool name also rides the
+# queue entry, so the executor can bill the turn to credits once the plan's
+# AutoPilot allowance is used up.
_AUTOPILOT_TOOL_CALL_ID = "autopilot_block"
-_AUTOPILOT_TOOL_NAME = "autopilot_block"
+_AUTOPILOT_TOOL_NAME = AUTOPILOT_BLOCK_TOOL_NAME
# Ceiling on how long AutoPilotBlock.execute_copilot will wait for the
# enqueued turn's terminal event. Sourced from the shared turn-lifetime
@@ -72,6 +79,21 @@ class SubAgentRecursionError(BlockExecutionError):
)
+class AutoPilotOutOfBudgetError(BlockExecutionError):
+ """The block's turn was refused when picked up: no AutoPilot budget left.
+
+ A deliberate stop like the recursion limit, so it skips the recovery
+ re-enqueue, which would only be refused again.
+ """
+
+ def __init__(self, pause: "TurnPause") -> None:
+ super().__init__(
+ message=_out_of_budget_message(pause),
+ block_name="AutoPilotBlock",
+ block_id=AUTOPILOT_BLOCK_ID,
+ )
+
+
class ToolCallEntry(TypedDict):
"""A single tool invocation record from an autopilot execution."""
@@ -458,6 +480,9 @@ class AutoPilotBlock(Block):
f"{_AUTOPILOT_BLOCK_MAX_WAIT_SECONDS}s — session "
f"{session_id}"
)
+ if outcome == "refused" and result.pause is not None:
+ # The executor refused the turn for want of budget.
+ raise AutoPilotOutOfBudgetError(result.pause)
if outcome == "refused":
# Terminal, like the branches above: nothing ran, so the
# SessionResult carries only the refusal and its empty
@@ -664,7 +689,7 @@ class AutoPilotBlock(Block):
yield "session_id", sid
yield "error", "AutoPilot execution was cancelled."
raise
- except SubAgentRecursionError as exc:
+ except (SubAgentRecursionError, AutoPilotOutOfBudgetError) as exc:
# Deliberate block — re-enqueueing would immediately hit the limit
# again, so skip recovery and just surface the error.
yield "session_id", sid
@@ -706,6 +731,29 @@ class AutoPilotBlock(Block):
# Helpers – placed after the block class for top-down readability.
# ---------------------------------------------------------------------------
+
+def _out_of_budget_message(pause: "TurnPause") -> str:
+ """What a graph run shows for a turn refused for want of budget.
+
+ The executor runs a block turn on the credit balance once the plan's
+ allowance is used up, so a refusal means the balance can't pay for one
+ either: the message says both, how to add credits, and when the
+ allowance resets, if it does.
+ """
+ allowance = "the trial's" if pause.reason == "trial" else "your plan's"
+ credits_url = f"{Settings().config.frontend_base_url.rstrip('/')}/profile/credits"
+ message = (
+ f"Out of AutoPilot budget: {allowance} AutoPilot allowance is used up, "
+ "and your credit balance doesn't cover a run (it needs at least "
+ f"${PER_TURN_SPEND_FLOOR_USD:.2f}). Add credits at {credits_url} to "
+ "run it on credits."
+ )
+ if pause.resets_at is None:
+ return message
+ resets_at = pause.resets_at.astimezone(UTC)
+ return f"{message} The allowance resets {resets_at:%Y-%m-%d %H:%M} UTC."
+
+
# Task-scoped recursion depth counter & chain-wide limit.
# contextvars are scoped to the current asyncio task, so concurrent
# graph executions each get independent counters.
@@ -880,6 +928,8 @@ async def _enqueue_for_recovery(
# executor being a separate process.
# TODO(#14244-f5): revisit together with run_agent's tree reset.
envelope=root_envelope(recovery_turn_id, session_id=session_id),
+ # Still the block's turn: it may run on credits as the first did.
+ tool_name=_AUTOPILOT_TOOL_NAME,
),
timeout=10,
)
diff --git a/autogpt_platform/backend/backend/blocks/autopilot_refusal_test.py b/autogpt_platform/backend/backend/blocks/autopilot_refusal_test.py
index ae1207e668..369e294582 100644
--- a/autogpt_platform/backend/backend/blocks/autopilot_refusal_test.py
+++ b/autogpt_platform/backend/backend/blocks/autopilot_refusal_test.py
@@ -5,14 +5,27 @@
turn in flight, and when the tree ledger refuses the spawn. Without a branch for
it the block falls through and renders an empty ``SessionResult`` as a
successful turn, dropping the refusal.
+
+The executor can refuse a turn too, when the account is out of AutoPilot
+budget as it picks the turn up and its credit balance can't pay for the turn
+either. The waiter reports that as ``refused`` with the not-started pause,
+which the block turns into an out-of-budget error that skips recovery.
"""
-from unittest.mock import AsyncMock, patch
+from datetime import UTC, datetime
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
-from backend.blocks.autopilot import AutoPilotBlock
+from backend.blocks.autopilot import (
+ AutoPilotBlock,
+ AutoPilotOutOfBudgetError,
+ AutoPilotTransport,
+ _enqueue_for_recovery,
+)
from backend.copilot.sdk.session_waiter import SessionResult
+from backend.copilot.turn_pause import TurnPause
+from backend.data.execution import ExecutionContext
@pytest.mark.asyncio
@@ -49,3 +62,136 @@ async def test_a_refusal_with_no_message_still_raises():
max_recursion_depth=3,
user_id="u1",
)
+
+
+_OUT_OF_TODAY = TurnPause(
+ reason="daily",
+ resets_at=datetime(2026, 10, 12, tzinfo=UTC),
+ checkpoint_saved=False,
+ started=False,
+)
+
+
+async def _execute():
+ return await AutoPilotBlock().execute_copilot(
+ prompt="do the thing",
+ system_context="",
+ session_id="sess-1",
+ max_recursion_depth=3,
+ user_id="u1",
+ )
+
+
+def _completed(result: SessionResult):
+ return _waited("completed", result)
+
+
+def _refused_for_budget(pause: TurnPause):
+ return _waited("refused", SessionResult(refusal=pause.summary, pause=pause))
+
+
+def _waited(outcome: str, result: SessionResult):
+ return patch(
+ "backend.copilot.sdk.session_waiter.run_copilot_turn_via_queue",
+ new=AsyncMock(return_value=(outcome, result)),
+ )
+
+
+_OUT_OF_TODAY_MESSAGE = (
+ "Out of AutoPilot budget: your plan's AutoPilot allowance is used up, and "
+ "your credit balance doesn't cover a run (it needs at least $0.50). Add "
+ "credits at https://platform.agpt.co/profile/credits to run it on credits. "
+ "The allowance resets 2026-10-12 00:00 UTC."
+)
+
+
+def frontend(mocker):
+ settings = MagicMock()
+ settings.config.frontend_base_url = "https://platform.agpt.co/"
+ mocker.patch("backend.blocks.autopilot.Settings", return_value=settings)
+
+
+async def test_a_turn_refused_for_budget_says_why_how_to_top_up_and_when_it_resets(
+ frontend,
+):
+ """The executor only refuses a block turn the credit balance can't cover."""
+ with _refused_for_budget(_OUT_OF_TODAY):
+ with pytest.raises(AutoPilotOutOfBudgetError) as raised:
+ await _execute()
+ assert str(raised.value) == _OUT_OF_TODAY_MESSAGE
+
+
+async def test_a_spent_trial_says_its_allowance_is_used_up(frontend):
+ pause = TurnPause(reason="trial", checkpoint_saved=False, started=False)
+ with _refused_for_budget(pause):
+ with pytest.raises(AutoPilotOutOfBudgetError) as raised:
+ await _execute()
+ assert str(raised.value) == (
+ "Out of AutoPilot budget: the trial's AutoPilot allowance is used up, and "
+ "your credit balance doesn't cover a run (it needs at least $0.50). Add "
+ "credits at https://platform.agpt.co/profile/credits to run it on credits."
+ )
+
+
+async def test_a_recovery_run_is_still_the_blocks_turn():
+ """So it may run on the credit balance as the first run could."""
+ session = MagicMock()
+ session.metadata.llm_auth_provider = "platform"
+ session.metadata.llm_credential_id = None
+ enqueue = AsyncMock()
+
+ with (
+ patch(
+ "backend.copilot.model.get_chat_session", AsyncMock(return_value=session)
+ ),
+ patch("backend.copilot.executor.utils.enqueue_copilot_turn", enqueue),
+ ):
+ await _enqueue_for_recovery("sess-1", "user-1", "do the thing", dry_run=False)
+
+ enqueue.assert_awaited_once()
+ assert enqueue.await_args.kwargs["tool_name"] == "autopilot_block"
+
+
+async def test_a_turn_that_paused_after_running_returns_its_checkpoint():
+ landed = _OUT_OF_TODAY.model_copy(update={"started": True})
+ checkpoint = "Where I got to: half the inbox."
+ with _completed(SessionResult(response_text=checkpoint, pause=landed)):
+ response, *_ = await _execute()
+ assert response == checkpoint
+
+
+async def test_a_budget_refusal_reaches_the_graph_without_a_recovery_run(frontend):
+ """Re-enqueueing it for recovery would only be refused again."""
+ block = AutoPilotBlock()
+ recovery = AsyncMock()
+ context = ExecutionContext(
+ user_id="user-1",
+ graph_id="graph-1",
+ graph_exec_id="graph-exec-1",
+ node_id="node-1",
+ node_exec_id="node-exec-1",
+ )
+
+ with (
+ patch.object(block, "create_session", AsyncMock(return_value="sess-1")),
+ _refused_for_budget(_OUT_OF_TODAY),
+ patch("backend.blocks.autopilot._enqueue_for_recovery", recovery),
+ ):
+ outputs = [
+ item
+ async for item in block.run(
+ AutoPilotBlock.Input(
+ prompt="go", transport=AutoPilotTransport.PLATFORM
+ ),
+ execution_context=context,
+ )
+ ]
+
+ assert outputs == [("session_id", "sess-1"), ("error", _OUT_OF_TODAY_MESSAGE)]
+ recovery.assert_not_awaited()
diff --git a/autogpt_platform/backend/backend/copilot/at_reset.py b/autogpt_platform/backend/backend/copilot/at_reset.py
index 62dbdba3ce..a80da84e9e 100644
--- a/autogpt_platform/backend/backend/copilot/at_reset.py
+++ b/autogpt_platform/backend/backend/copilot/at_reset.py
@@ -192,7 +192,10 @@ async def _blocking_reset(user_id: str) -> datetime:
raise NoResetAhead("no_reset")
if tier == SubscriptionTier.TRIAL and await _trial_allowance_spent(user_id):
raise NoResetAhead("no_reset")
- status = await get_usage_status(user_id, daily_limit, weekly_limit, tier=tier)
+ # No usage, read off a Redis failure, would say the budget is already back.
+ status = await get_usage_status(
+ user_id, daily_limit, weekly_limit, tier=tier, fail_closed=True
+ )
# As in ``check_rate_limit``: a negative limit means no cap at all.
exhausted = [w for w in (status.daily, status.weekly) if 0 <= w.limit <= w.used]
if not exhausted:
diff --git a/autogpt_platform/backend/backend/copilot/at_reset_test.py b/autogpt_platform/backend/backend/copilot/at_reset_test.py
index 028f365566..b3f0a7248f 100644
--- a/autogpt_platform/backend/backend/copilot/at_reset_test.py
+++ b/autogpt_platform/backend/backend/copilot/at_reset_test.py
@@ -127,6 +127,16 @@ async def test_out_of_both_waits_for_the_one_that_resets_last(limits):
assert timedelta(0) <= after_reset < timedelta(seconds=600)
+async def test_unreadable_usage_is_unavailable_not_a_budget_back(limits):
+ # Read as no usage, an outage would tell the client to send right away.
+ _, status, _ = limits
+ status.side_effect = RateLimitUnavailable("Redis down")
+ with pytest.raises(RateLimitUnavailable):
+ await next_reset_run_at("user-1", SESSION, "send")
+ assert status.await_args.kwargs["fail_closed"] is True
+
+
@pytest.mark.asyncio
async def test_a_resume_fires_a_minute_after_a_send_on_the_same_chat(limits):
_, status, _ = limits
diff --git a/autogpt_platform/backend/backend/copilot/budget_gate.py b/autogpt_platform/backend/backend/copilot/budget_gate.py
new file mode 100644
index 0000000000..af7fb1ae48
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/budget_gate.py
@@ -0,0 +1,136 @@
+"""The allowance check a platform-billed turn passes before it starts.
+
+The chat route checks the allowance before it accepts a message, but much of
+what runs never passes through it: scheduled follow-ups and routines, the
+AutoPilot block, sub-sessions, approval wakes, chat bots, and the turn an
+engine starts by itself to answer messages queued while it ran. So the
+executor checks every platform turn it picks up, and the SDK engine checks
+before it continues. A turn the allowance can't cover doesn't start: it ends
+as a "not started" pause naming the limit and when it refills, recorded in
+the chat like any other pause. The one exception is the AutoPilot block's
+turn, which the executor runs on the credit balance instead when the balance
+can pay for it (see ``backend.copilot.credit_billing``).
+
+ChatGPT and Microsoft 365 turns are paid for by the user's own subscription,
+so they never come here.
+"""
+
+import logging
+
+from pydantic import BaseModel
+
+from backend.copilot.config import ChatConfig
+from backend.copilot.model import ChatSession, upsert_chat_session
+from backend.copilot.pending_messages import peek_pending_count
+from backend.copilot.rate_limit import (
+ RateLimitExceeded,
+ SubscriptionTier,
+ check_rate_limit,
+ get_global_rate_limits,
+)
+from backend.copilot.turn_pause import TurnPause, append_pause_marker
+from backend.copilot.usage_activation import get_ready_usage_state
+
+logger = logging.getLogger(__name__)
+
+config = ChatConfig()
+
+
+class AutoContinueGate(BaseModel):
+ """Whether a finished turn may go on to the messages queued during it."""
+
+ proceed: bool
+ # Set when the allowance refused them: the pause to send in their place.
+ pause: TurnPause | None = None
+
+
+async def refusal_for(user_id: str | None) -> TurnPause | None:
+ """The not-started pause for a platform turn starting now, or None to run.
+
+ Raises ``RateLimitUnavailable`` when the allowance can't be read: callers
+ fail closed rather than run a turn nobody metered.
+ """
+ if not user_id:
+ return None
+ daily, weekly, tier = await get_global_rate_limits(
+ user_id,
+ config.daily_cost_limit_microdollars,
+ config.weekly_cost_limit_microdollars,
+ )
+ if daily == 0 and weekly == 0:
+ # No plan, or the trial is over: nothing to spend, and nothing that
+ # refills. Settled here because an ended trial's usage state still
+ # reads TRIAL, which check_rate_limit's tier guard would take for a
+ # subscription change and report as unavailable -- and which is
+ # what says the pause is the trial's rather than a missing plan's.
+ state = await get_ready_usage_state(user_id)
+ ended_trial = state.tier == SubscriptionTier.TRIAL
+ return TurnPause(
+ reason="trial" if ended_trial else "weekly",
+ resets_at=None,
+ checkpoint_saved=False,
+ started=False,
+ )
+ try:
+ await check_rate_limit(
+ user_id=user_id,
+ daily_cost_limit=daily,
+ weekly_cost_limit=weekly,
+ expected_tier=tier,
+ )
+ except RateLimitExceeded as exc:
+ # A trial's allowance doesn't refill; its end is not a reset. Nor
+ # does a window that allows nothing (no plan, or the trial is over).
+ limit = {"daily": daily, "weekly": weekly}.get(exc.window, 0)
+ return TurnPause(
+ reason=exc.window,
+ resets_at=exc.resets_at if limit > 0 else None,
+ checkpoint_saved=False,
+ started=False,
+ )
+ return None
+
+
+async def record_refusal(session: ChatSession, pause: TurnPause) -> None:
+ """Leave the pause in the chat so its card survives a reload.
+
+ Best effort: the live stream carries the pause even if this fails.
+ """
+ if not append_pause_marker(session, pause):
+ return
+ try:
+ await upsert_chat_session(session)
+ except Exception:
+ logger.warning(
+ f"Could not save the not-started marker in session "
+ f"{session.session_id[:12]}",
+ exc_info=True,
+ )
+
+
+async def gate_auto_continue(
+ session: ChatSession, user_id: str | None, *, platform_billed: bool
+) -> AutoContinueGate:
+ """Check the allowance before a finished turn answers queued messages.
+
+ Only when something is queued. Refused, or with the allowance unreadable,
+ the messages stay queued for the next turn instead of being drained; a
+ refusal is also recorded in the chat, as the executor records its own.
+ """
+ if not platform_billed or not user_id:
+ return AutoContinueGate(proceed=True)
+ try:
+ if not await peek_pending_count(session.session_id):
+ return AutoContinueGate(proceed=True)
+ pause = await refusal_for(user_id)
+ except Exception:
+ logger.warning(
+ f"Queued messages in session {session.session_id[:12]} wait for "
+ "the next turn: the allowance couldn't be checked",
+ exc_info=True,
+ )
+ return AutoContinueGate(proceed=False)
+ if pause is None:
+ return AutoContinueGate(proceed=True)
+ await record_refusal(session, pause)
+ return AutoContinueGate(proceed=False, pause=pause)
diff --git a/autogpt_platform/backend/backend/copilot/budget_gate_test.py b/autogpt_platform/backend/backend/copilot/budget_gate_test.py
new file mode 100644
index 0000000000..e484c54324
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/budget_gate_test.py
@@ -0,0 +1,217 @@
+from datetime import UTC, datetime
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from backend.copilot.budget_gate import (
+ AutoContinueGate,
+ gate_auto_continue,
+ record_refusal,
+ refusal_for,
+)
+from backend.copilot.model import ChatMessage, ChatSession
+from backend.copilot.rate_limit import (
+ RateLimitExceeded,
+ RateLimitUnavailable,
+ SubscriptionTier,
+)
+from backend.copilot.turn_pause import TurnPause, pause_of
+
+_MOD = "backend.copilot.budget_gate"
+WEEKLY_RESET = datetime(2026, 10, 19, tzinfo=UTC)
+
+
+def allowance():
+ """The rate-limit reads; tests set what ``check_rate_limit`` does."""
+ with (
+ patch(
+ f"{_MOD}.get_global_rate_limits",
+ AsyncMock(return_value=(3_000_000, 6_000_000, SubscriptionTier.PRO)),
+ ) as limits,
+ patch(f"{_MOD}.check_rate_limit", AsyncMock()) as check,
+ ):
+ yield limits, check
+
+
+def _usage_state(tier: SubscriptionTier):
+ return patch(
+ f"{_MOD}.get_ready_usage_state",
+ AsyncMock(return_value=MagicMock(tier=tier)),
+ )
+
+
+def _session() -> ChatSession:
+ session = ChatSession.new("u-1", dry_run=False)
+ session.messages = [
+ ChatMessage(role="user", content="summarise my inbox", sequence=0),
+ ChatMessage(role="assistant", content="Done.", sequence=1),
+ ]
+ return session
+
+
+async def test_a_turn_with_allowance_left_runs(allowance):
+ _, check = allowance
+ assert await refusal_for("u-1") is None
+ check.assert_awaited_once_with(
+ user_id="u-1",
+ daily_cost_limit=3_000_000,
+ weekly_cost_limit=6_000_000,
+ expected_tier=SubscriptionTier.PRO,
+ )
+
+
+async def test_a_turn_without_a_user_is_not_checked(allowance):
+ limits, _ = allowance
+ assert await refusal_for(None) is None
+ limits.assert_not_awaited()
+
+
+async def test_a_used_up_window_refuses_the_turn_until_it_refills(allowance):
+ _, check = allowance
+ check.side_effect = RateLimitExceeded("weekly", WEEKLY_RESET)
+ assert await refusal_for("u-1") == TurnPause(
+ reason="weekly",
+ resets_at=WEEKLY_RESET,
+ checkpoint_saved=False,
+ started=False,
+ )
+
+
+async def test_a_spent_trial_refuses_the_turn_with_no_reset(allowance):
+ # The trial's end rides on the exception, but it is not a refill.
+ _, check = allowance
+ check.side_effect = RateLimitExceeded("trial", WEEKLY_RESET)
+ pause = await refusal_for("u-1")
+ assert pause is not None
+ assert (pause.reason, pause.resets_at, pause.started) == ("trial", None, False)
+
+
+async def test_a_window_that_allows_nothing_refuses_with_no_reset(allowance):
+ # No plan, or a trial that is over: zero limits never refill on their own,
+ # so the pause can't offer to pick the task up at the next reset.
+ limits, check = allowance
+ limits.return_value = (0, 0, SubscriptionTier.NO_TIER)
+ check.side_effect = RateLimitExceeded("weekly", WEEKLY_RESET)
+ with _usage_state(SubscriptionTier.NO_TIER):
+ pause = await refusal_for("u-1")
+ assert pause is not None
+ assert (pause.reason, pause.resets_at, pause.started) == ("weekly", None, False)
+
+
+async def test_an_ended_trial_is_refused_without_reading_its_stale_tier(allowance):
+ # get_global_rate_limits reports an ended trial as NO_TIER with no
+ # allowance while its usage state still says TRIAL; check_rate_limit's
+ # tier guard would call that unavailable and fail the turn with "try
+ # again in a moment" instead of saying it can't run.
+ limits, check = allowance
+ limits.return_value = (0, 0, SubscriptionTier.NO_TIER)
+ check.side_effect = RateLimitUnavailable("Subscription changed")
+ with _usage_state(SubscriptionTier.TRIAL):
+ pause = await refusal_for("u-1")
+ # Named as the trial, which is what ended: Upgrade, not a reset.
+ assert pause == TurnPause(
+ reason="trial", resets_at=None, checkpoint_saved=False, started=False
+ )
+ check.assert_not_awaited()
+
+
+async def test_an_unreadable_allowance_is_raised_for_the_caller(allowance):
+ _, check = allowance
+ check.side_effect = RateLimitUnavailable()
+ with pytest.raises(RateLimitUnavailable):
+ await refusal_for("u-1")
+
+
+async def test_a_refusal_is_saved_as_a_marker_row():
+ session = _session()
+ pause = TurnPause(reason="daily", started=False, checkpoint_saved=False)
+ with patch(f"{_MOD}.upsert_chat_session", AsyncMock()) as save:
+ await record_refusal(session, pause)
+ save.assert_awaited_once_with(session)
+ assert pause_of(session.messages[-1]) == pause
+
+
+async def test_a_failed_save_does_not_fail_the_refusal():
+ session = _session()
+ pause = TurnPause(reason="daily", started=False, checkpoint_saved=False)
+ with patch(f"{_MOD}.upsert_chat_session", AsyncMock(side_effect=OSError)):
+ await record_refusal(session, pause)
+ assert pause_of(session.messages[-1]) == pause
+
+
+async def test_a_chat_already_ending_in_a_marker_gets_no_second_one():
+ session = _session()
+ first = TurnPause(reason="daily", started=False, checkpoint_saved=False)
+ with patch(f"{_MOD}.upsert_chat_session", AsyncMock()) as save:
+ await record_refusal(session, first)
+ await record_refusal(session, first.model_copy(update={"reason": "weekly"}))
+ save.assert_awaited_once()
+ assert pause_of(session.messages[-1]) == first
+
+
+def queued():
+ with patch(f"{_MOD}.peek_pending_count", AsyncMock(return_value=2)) as peek:
+ yield peek
+
+
+async def test_a_chatgpt_turn_continues_unchecked(allowance, queued):
+ limits, _ = allowance
+ gate = await gate_auto_continue(_session(), "u-1", platform_billed=False)
+ assert gate == AutoContinueGate(proceed=True)
+ queued.assert_not_awaited()
+ limits.assert_not_awaited()
+
+
+async def test_nothing_queued_needs_no_check(allowance, queued):
+ limits, _ = allowance
+ queued.return_value = 0
+ gate = await gate_auto_continue(_session(), "u-1", platform_billed=True)
+ assert gate == AutoContinueGate(proceed=True)
+ limits.assert_not_awaited()
+
+
+async def test_queued_messages_continue_while_the_allowance_lasts(allowance, queued):
+ gate = await gate_auto_continue(_session(), "u-1", platform_billed=True)
+ assert gate == AutoContinueGate(proceed=True)
+
+
+async def test_queued_messages_wait_when_the_allowance_is_used_up(allowance, queued):
+ _, check = allowance
+ check.side_effect = RateLimitExceeded("weekly", WEEKLY_RESET)
+ session = _session()
+ with patch(f"{_MOD}.upsert_chat_session", AsyncMock()) as save:
+ gate = await gate_auto_continue(session, "u-1", platform_billed=True)
+
+ assert not gate.proceed
+ assert gate.pause is not None and not gate.pause.started
+ assert (gate.pause.reason, gate.pause.resets_at) == ("weekly", WEEKLY_RESET)
+ # Recorded in the chat; the queue itself was only looked at, never drained.
+ assert pause_of(session.messages[-1]) == gate.pause
+ save.assert_awaited_once_with(session)
+ queued.assert_awaited_once_with(session.session_id)
+
+
+async def test_queued_messages_wait_when_the_allowance_is_unreadable(allowance, queued):
+ _, check = allowance
+ check.side_effect = RateLimitUnavailable()
+ session = _session()
+ gate = await gate_auto_continue(session, "u-1", platform_billed=True)
+ assert gate == AutoContinueGate(proceed=False)
+ assert pause_of(session.messages[-1]) is None
diff --git a/autogpt_platform/backend/backend/copilot/constants.py b/autogpt_platform/backend/backend/copilot/constants.py
index 8163afff53..d540c8dd67 100644
--- a/autogpt_platform/backend/backend/copilot/constants.py
+++ b/autogpt_platform/backend/backend/copilot/constants.py
@@ -5,6 +5,18 @@
AUTOPILOT_NAME = "Otto"
AUTOPILOT_ROLE = "Head of AI"
+# The AutoPilot block, and the tool name its turns carry from dispatch to the
+# executor -- which bills them to the credit balance once the plan's AutoPilot
+# allowance is used up (see ``backend.copilot.credit_billing``).
+AUTOPILOT_BLOCK_ID = "c069dc6b-c3ed-4c12-b6e5-d47361e64ce6"
+AUTOPILOT_BLOCK_TOOL_NAME = "autopilot_block"
+
+# The per-turn spend floor, the one the old per-query budget was topped up to.
+# A turn's budget has none, but a paid plan's CLI backstop never sits below
+# it, and a credit balance has to cover it before a turn may bill to credits.
+# The per-turn ceiling is ``ChatConfig.claude_agent_max_budget_usd``.
+PER_TURN_SPEND_FLOOR_USD = 0.5
+
# Special message prefixes for text-based markers (parsed by frontend).
# The hex suffix makes accidental LLM generation of these strings virtually
# impossible, avoiding false-positive marker detection in normal conversation.
diff --git a/autogpt_platform/backend/backend/copilot/credit_billing.py b/autogpt_platform/backend/backend/copilot/credit_billing.py
new file mode 100644
index 0000000000..d52a75924f
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/credit_billing.py
@@ -0,0 +1,270 @@
+"""AutoPilot block turns billed to the credit balance once the allowance is out.
+
+A graph running the AutoPilot block shouldn't stop because the plan's AutoPilot
+allowance is used up while the account's automation credits could pay for the
+turn. So before the executor refuses a block turn for budget (see
+``backend.copilot.budget_gate``), it checks the credit balance: if the balance
+covers the per-turn spend floor, the turn runs billed to credits instead. Only
+the block's turns do, its recovery re-run included. Chat, scheduled follow-ups
+and routines, sub-sessions, approval wakes, chat bots and auto-continue are
+refused as before.
+
+A credit-billed turn:
+
+- may spend what the balance covered when it started, and never more than the
+ per-turn ceiling (``claude_agent_max_budget_usd``). Its budget is a hard cap,
+ so its CLI backstop sits exactly on it;
+- runs on the SDK engine, the one that enforces a budget;
+- records its cost here rather than in the plan's daily and weekly windows;
+- is charged once when it ends: the cost it recorded, rounded up to whole
+ credits, never past its cap. A turn that ended without a result is charged
+ what it recorded too, through the same accounting as any other turn.
+"""
+
+import asyncio
+import logging
+import math
+from collections.abc import AsyncIterator
+from contextlib import asynccontextmanager
+from contextvars import ContextVar
+
+from backend.copilot.config import ChatConfig
+from backend.copilot.constants import (
+ AUTOPILOT_BLOCK_ID,
+ AUTOPILOT_BLOCK_TOOL_NAME,
+ PER_TURN_SPEND_FLOOR_USD,
+)
+from backend.copilot.rate_limit import RateLimitUnavailable
+from backend.copilot.tree import MICRODOLLARS_PER_CREDIT
+from backend.copilot.turn_budget import BudgetWindow, TurnBudget
+from backend.copilot.turn_pause import PauseReason, TurnPause
+from backend.data.credit import UsageTransactionMetadata
+from backend.data.db_accessors import credit_db
+from backend.util.exceptions import InsufficientBalanceError
+from backend.util.settings import Settings
+
+logger = logging.getLogger(__name__)
+config = ChatConfig()
+settings = Settings()
+
+CREDITS_PER_USD = 100
+# The OpenRouter cost reconcile retries its lookups for up to ~30s after the
+# stream ends (see ``sdk.openrouter_cost``). The charge waits that long for it;
+# a cost recorded later still is charged when it lands.
+_COST_SETTLE_TIMEOUT_SECONDS = 45.0
+_BUDGET_WINDOW: dict[PauseReason, BudgetWindow] = {
+ "daily": "daily",
+ "weekly": "weekly",
+ "trial": "trial",
+ "task": "task",
+ "steps": "task",
+}
+
+_credit_billed_turn: ContextVar["CreditBilledTurn | None"] = ContextVar(
+ "credit_billed_turn", default=None
+)
+
+
+class CreditBilledTurn:
+ """One AutoPilot block turn running on the credit balance."""
+
+ def __init__(
+ self,
+ *,
+ user_id: str,
+ session_id: str,
+ cap_credits: int,
+ refusal: TurnPause,
+ organization_id: str | None = None,
+ team_id: str | None = None,
+ ) -> None:
+ self.user_id = user_id
+ self.session_id = session_id
+ # In an organization's run the organization pays, as for the graph's
+ # other blocks.
+ self.organization_id = organization_id
+ self.team_id = team_id
+ # What the balance covered when the turn started, within the ceiling.
+ self.cap_credits = cap_credits
+ # The refusal this turn runs instead of: the limit that's used up.
+ self.refusal = refusal
+ self.spent_microdollars = 0
+ self.charged_credits = 0
+ self._ended = False
+ self._recordings: set[asyncio.Task] = set()
+ self._charging = asyncio.Lock()
+
+ @property
+ def budget(self) -> TurnBudget:
+ """A hard cap at what the balance covers: the turn mustn't overdraw."""
+ return TurnBudget(
+ usd=self.cap_credits / CREDITS_PER_USD,
+ limiting=_BUDGET_WINDOW[self.refusal.reason],
+ resets_at=self.refusal.resets_at,
+ hard_cap=True,
+ )
+
+ def wait_for(self, recording: asyncio.Task) -> None:
+ """Hold the charge for cost a task records after the stream ends."""
+ self._recordings.add(recording)
+
+ async def record(self, cost_microdollars: int) -> None:
+ self.spent_microdollars += cost_microdollars
+ if self._ended:
+ # Recorded after the turn was charged: charged as it lands.
+ await self._charge_due()
+
+ async def end(self) -> None:
+ """Charge what the turn recorded, once its late recordings are in.
+
+ A cancel during the wait doesn't skip the charge: the turn still pays
+ for what it spent, and cost recorded later is charged as it lands.
+ """
+ try:
+ if self._recordings:
+ await asyncio.wait(
+ self._recordings, timeout=_COST_SETTLE_TIMEOUT_SECONDS
+ )
+ finally:
+ self._ended = True
+ await asyncio.shield(self._charge_due())
+
+ async def _charge_due(self) -> None:
+ async with self._charging:
+ owed = min(credits_for(self.spent_microdollars), self.cap_credits)
+ due = owed - self.charged_credits
+ if due <= 0:
+ return
+ self.charged_credits += await self._spend(due)
+
+ async def _spend(self, credits: int) -> int:
+ """Debit up to ``credits``, never below zero; returns what was debited."""
+ try:
+ await self._debit(credits)
+ return credits
+ except InsufficientBalanceError:
+ # Other runs spent the balance while this turn ran: it takes what
+ # is left rather than overdrawing the account.
+ try:
+ left = min(credits, await _balance(self.user_id, self.organization_id))
+ if left > 0:
+ await self._debit(left)
+ except Exception:
+ left = 0
+ logger.warning(
+ f"Credit-billed turn in session {self.session_id[:12]} owed "
+ f"{credits} credits; the balance covered {max(left, 0)}"
+ )
+ return max(left, 0)
+ except Exception:
+ logger.error(
+ f"Billing leak: could not charge {credits} credits for the "
+ f"credit-billed turn in session {self.session_id[:12]}",
+ exc_info=True,
+ )
+ return 0
+
+ async def _debit(self, credits: int) -> None:
+ metadata = UsageTransactionMetadata(
+ chat_session_id=self.session_id,
+ block_id=AUTOPILOT_BLOCK_ID,
+ block="AutoPilotBlock",
+ reason="AutoPilot turn billed to credits: plan allowance used up",
+ )
+ if self.organization_id:
+ await credit_db().spend_org_credits(
+ org_id=self.organization_id,
+ user_id=self.user_id,
+ cost=credits,
+ metadata=metadata,
+ team_id=self.team_id,
+ )
+ else:
+ await credit_db().spend_credits(
+ user_id=self.user_id, cost=credits, metadata=metadata
+ )
+
+
+async def bill_to_credits(
+ *,
+ user_id: str | None,
+ session_id: str,
+ tool_name: str | None,
+ refusal: TurnPause,
+ organization_id: str | None = None,
+ team_id: str | None = None,
+) -> CreditBilledTurn | None:
+ """The credit-billed turn to run instead of ``refusal``, or None to refuse.
+
+ Only the AutoPilot block's turns, and only when the balance covers the
+ per-turn spend floor: the organization's balance in an organization's
+ run. None where credits are off, leaving no balance to bill, or where
+ the SDK engine that caps a turn's spend can't run. Raises
+ ``RateLimitUnavailable`` when the balance can't be read: like an
+ unreadable allowance, that fails the turn closed.
+ """
+ if user_id is None or tool_name != AUTOPILOT_BLOCK_TOOL_NAME:
+ return None
+ if not settings.config.enable_credit or not config.thinking_available:
+ return None
+ try:
+ balance = await _balance(user_id, organization_id)
+ except Exception as exc:
+ raise RateLimitUnavailable("Credit balance unavailable") from exc
+ if balance < math.ceil(PER_TURN_SPEND_FLOOR_USD * CREDITS_PER_USD):
+ return None
+ ceiling = int(config.claude_agent_max_budget_usd * CREDITS_PER_USD)
+ return CreditBilledTurn(
+ user_id=user_id,
+ session_id=session_id,
+ cap_credits=min(balance, ceiling),
+ refusal=refusal,
+ organization_id=organization_id,
+ team_id=team_id,
+ )
+
+
+async def _balance(user_id: str, organization_id: str | None) -> int:
+ if organization_id:
+ return await credit_db().get_org_credits(org_id=organization_id)
+ return await credit_db().get_credits(user_id)
+
+
+@asynccontextmanager
+async def credit_billing(turn: CreditBilledTurn) -> AsyncIterator[None]:
+ """Run what's inside billed to credits, and charge it when it ends."""
+ token = _credit_billed_turn.set(turn)
+ try:
+ yield
+ finally:
+ _credit_billed_turn.reset(token)
+ await turn.end()
+
+
+def credit_billed_turn(user_id: str | None) -> CreditBilledTurn | None:
+ """The credit-billed turn the running work belongs to, if any."""
+ turn = _credit_billed_turn.get()
+ if turn is None or turn.user_id != user_id:
+ return None
+ return turn
+
+
+async def record_credit_billed_cost(user_id: str, cost_microdollars: int) -> bool:
+ """Put a cost on its credit-billed turn; False when there is none."""
+ turn = credit_billed_turn(user_id)
+ if turn is None:
+ return False
+ await turn.record(cost_microdollars)
+ return True
+
+
+def wait_for_cost(recording: asyncio.Task) -> None:
+ """Have a credit-billed turn's charge wait for a cost recorded later."""
+ turn = _credit_billed_turn.get()
+ if turn is not None:
+ turn.wait_for(recording)
+
+
+def credits_for(microdollars: int) -> int:
+ """Whole credits, rounded up: part of a cent is still charged."""
+ return -(-microdollars // MICRODOLLARS_PER_CREDIT)
diff --git a/autogpt_platform/backend/backend/copilot/credit_billing_test.py b/autogpt_platform/backend/backend/copilot/credit_billing_test.py
new file mode 100644
index 0000000000..7e95b3069b
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/credit_billing_test.py
@@ -0,0 +1,300 @@
+"""AutoPilot block turns billed to credits once the plan's allowance is out.
+
+The executor runs a block turn on the credit balance instead of refusing it
+when the balance covers the per-turn spend floor. The turn is capped at what
+the balance covers and charged the cost it recorded when it ends.
+"""
+
+import asyncio
+from datetime import UTC, datetime
+from unittest.mock import AsyncMock, MagicMock, PropertyMock
+
+import pytest
+from claude_agent_sdk import AssistantMessage, TextBlock
+
+from backend.copilot.config import ChatConfig
+from backend.copilot.constants import AUTOPILOT_BLOCK_ID
+from backend.copilot.credit_billing import (
+ CreditBilledTurn,
+ bill_to_credits,
+ credit_billing,
+ record_credit_billed_cost,
+ wait_for_cost,
+)
+from backend.copilot.rate_limit import RateLimitUnavailable
+from backend.copilot.sdk.governor import TurnGovernor
+from backend.copilot.sdk.usage_meter import UsageMeter, cli_price_ratio
+from backend.copilot.turn_pause import TurnPause
+from backend.util.exceptions import InsufficientBalanceError
+
+OUT_OF_TODAY = TurnPause(
+ reason="daily",
+ resets_at=datetime(2026, 10, 15, tzinfo=UTC),
+ checkpoint_saved=False,
+ started=False,
+)
+KIMI = "moonshotai/kimi-k2.6"
+
+_CREDITS_ON = "backend.copilot.credit_billing.settings.config.enable_credit"
+
+
+def credits(mocker) -> MagicMock:
+ db = MagicMock()
+ db.get_credits = AsyncMock(return_value=500)
+ db.spend_credits = AsyncMock(return_value=0)
+ db.get_org_credits = AsyncMock(return_value=0)
+ db.spend_org_credits = AsyncMock(return_value=0)
+ mocker.patch("backend.copilot.credit_billing.credit_db", return_value=db)
+ # A deployment that bills credits, on a route the SDK engine can take.
+ mocker.patch(_CREDITS_ON, True)
+ mocker.patch.object(
+ ChatConfig, "thinking_available", new_callable=PropertyMock, return_value=True
+ )
+ return db
+
+
+async def _billed(credits: MagicMock, balance: int) -> CreditBilledTurn:
+ credits.get_credits.return_value = balance
+ turn = await bill_to_credits(
+ user_id="user-1",
+ session_id="sess-1",
+ tool_name="autopilot_block",
+ refusal=OUT_OF_TODAY,
+ )
+ assert turn is not None
+ return turn
+
+
+def _charged(credits: MagicMock) -> list[int]:
+ return [call.kwargs["cost"] for call in credits.spend_credits.await_args_list]
+
+
+class _Meter(UsageMeter):
+ def __init__(self, spent: float, last: float) -> None:
+ super().__init__()
+ self.spent = spent
+ self.last = last
+
+ def observe(self, message: AssistantMessage) -> None:
+ pass
+
+ @property
+ def spent_usd(self) -> float:
+ return self.spent
+
+ @property
+ def last_main_call_usd(self) -> float:
+ return self.last
+
+
+async def test_a_block_turn_runs_when_the_balance_covers_the_floor(credits):
+ turn = await _billed(credits, balance=50)
+ assert turn.cap_credits == 50
+
+ credits.get_credits.return_value = 49
+ assert (
+ await bill_to_credits(
+ user_id="user-1",
+ session_id="sess-1",
+ tool_name="autopilot_block",
+ refusal=OUT_OF_TODAY,
+ )
+ is None
+ )
+
+
[email protected]("tool_name", ["chat", "run_sub_session", None])
+async def test_only_the_blocks_turns_bill_to_credits(credits, tool_name):
+ turn = await bill_to_credits(
+ user_id="user-1", session_id="sess-1", tool_name=tool_name, refusal=OUT_OF_TODAY
+ )
+
+ assert turn is None
+ credits.get_credits.assert_not_awaited()
+
+
+async def test_no_turn_bills_to_credits_where_credits_are_off(credits, mocker):
+ # The disabled credit model reads a balance and charges nothing: a turn
+ # billed there would run free.
+ mocker.patch(_CREDITS_ON, False)
+
+ assert await _bill(organization_id=None) is None
+ credits.get_credits.assert_not_awaited()
+
+
+async def test_no_turn_bills_to_credits_without_the_engine_that_caps_it(
+ credits, mocker
+):
+ mocker.patch.object(
+ ChatConfig, "thinking_available", new_callable=PropertyMock, return_value=False
+ )
+
+ assert await _bill(organization_id=None) is None
+
+
+async def test_an_organizations_run_bills_the_organizations_balance(credits):
+ credits.get_credits.return_value = 0
+ credits.get_org_credits.return_value = 300
+
+ turn = await _bill(organization_id="org-1", team_id="team-1")
+ assert turn is not None and turn.cap_credits == 300
+ await turn.record(1_000_000)
+ await turn.end()
+
+ credits.get_org_credits.assert_awaited_once_with(org_id="org-1")
+ credits.spend_credits.assert_not_awaited()
+ charge = credits.spend_org_credits.await_args.kwargs
+ assert (charge["org_id"], charge["user_id"], charge["team_id"]) == (
+ "org-1",
+ "user-1",
+ "team-1",
+ )
+ assert charge["cost"] == 100
+
+
+async def test_a_turn_cancelled_while_its_charge_waits_is_still_charged(credits):
+ turn = await _billed(credits, balance=500)
+ await turn.record(1_000_000)
+ reconcile = asyncio.create_task(asyncio.sleep(3600))
+ turn.wait_for(reconcile)
+
+ ending = asyncio.create_task(turn.end())
+ await asyncio.sleep(0)
+ ending.cancel()
+ with pytest.raises(asyncio.CancelledError):
+ await ending
+ reconcile.cancel()
+
+ assert _charged(credits) == [100]
+
+
+async def _bill(**tenancy) -> CreditBilledTurn | None:
+ return await bill_to_credits(
+ user_id="user-1",
+ session_id="sess-1",
+ tool_name="autopilot_block",
+ refusal=OUT_OF_TODAY,
+ **tenancy,
+ )
+
+
+async def test_an_unreadable_balance_fails_closed(credits):
+ credits.get_credits.side_effect = ConnectionError("db down")
+
+ with pytest.raises(RateLimitUnavailable):
+ await bill_to_credits(
+ user_id="user-1",
+ session_id="sess-1",
+ tool_name="autopilot_block",
+ refusal=OUT_OF_TODAY,
+ )
+
+
+async def test_the_budget_is_the_balance_within_the_per_turn_ceiling(credits):
+ budget = (await _billed(credits, balance=75)).budget
+ assert (budget.usd, budget.hard_cap) == (0.75, True)
+ assert (budget.limiting, budget.resets_at) == ("daily", OUT_OF_TODAY.resets_at)
+
+ assert (await _billed(credits, balance=5_000)).budget.usd == 10.0
+
+
+async def test_a_turn_costing_more_than_the_balance_stops_at_the_cap(credits):
+ turn = await _billed(credits, balance=75)
+ meter = _Meter(spent=0.70, last=0.01)
+ governor = TurnGovernor(budget=turn.budget, step_checkin=450)
+ governor.meter = meter
+
+ # The CLI's own stop sits on the balance, with none of a paid plan's
+ # headroom past it.
+ assert governor.cli_budget_usd(KIMI) == pytest.approx(0.75 * cli_price_ratio(KIMI))
+ governor.observe(AssistantMessage(content=[TextBlock(text="x")], model="m"))
+ assert governor.landing is None
+ meter.spent = 0.74
+ governor.observe(AssistantMessage(content=[TextBlock(text="x")], model="m"))
+ assert governor.landing == "daily"
+
+ # Spend the CLI let past the cap before it stopped is never charged.
+ async with credit_billing(turn):
+ assert await record_credit_billed_cost("user-1", 900_000)
+ assert _charged(credits) == [75]
+
+
+async def test_the_turn_is_charged_its_recorded_cost_when_it_ends(credits):
+ turn = await _billed(credits, balance=500)
+
+ async with credit_billing(turn):
+ assert await record_credit_billed_cost("user-1", 123_400)
+ assert await record_credit_billed_cost("user-1", 1)
+ credits.spend_credits.assert_not_awaited()
+
+ # 123,401 microdollars, rounded up to whole credits, in one charge.
+ credits.spend_credits.assert_awaited_once()
+ charge = credits.spend_credits.await_args.kwargs
+ assert (charge["user_id"], charge["cost"]) == ("user-1", 13)
+ assert charge["metadata"].chat_session_id == "sess-1"
+ assert charge["metadata"].block_id == AUTOPILOT_BLOCK_ID
+ assert charge["metadata"].block == "AutoPilotBlock"
+
+
+async def test_the_charge_waits_for_cost_recorded_after_the_stream(credits):
+ turn = await _billed(credits, balance=500)
+
+ async def reconcile() -> None:
+ await asyncio.sleep(0.05)
+ await record_credit_billed_cost("user-1", 50_000)
+
+ async with credit_billing(turn):
+ wait_for_cost(asyncio.create_task(reconcile()))
+
+ assert _charged(credits) == [5]
+
+
+async def test_cost_recorded_after_the_charge_is_charged_when_it_lands(credits):
+ turn = await _billed(credits, balance=500)
+ async with credit_billing(turn):
+ await record_credit_billed_cost("user-1", 25_000)
+
+ await turn.record(30_000)
+
+ # 55,000 microdollars in all: 6 credits, not 3 + 3.
+ assert _charged(credits) == [3, 3]
+ assert turn.charged_credits == 6
+
+
+async def test_a_balance_spent_meanwhile_is_charged_only_what_is_left(credits):
+ turn = await _billed(credits, balance=500)
+ credits.spend_credits.side_effect = [
+ InsufficientBalanceError("too low", "user-1", 20, 40),
+ 0,
+ ]
+
+ async with credit_billing(turn):
+ await record_credit_billed_cost("user-1", 400_000)
+ credits.get_credits.return_value = 20
+
+ assert _charged(credits) == [40, 20]
+ assert turn.charged_credits == 20
+
+
+async def test_another_users_cost_stays_off_the_turn(credits):
+ turn = await _billed(credits, balance=500)
+
+ async with credit_billing(turn):
+ assert not await record_credit_billed_cost("user-2", 90_000)
+
+ credits.spend_credits.assert_not_awaited()
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor.py b/autogpt_platform/backend/backend/copilot/executor/processor.py
index 8580c66316..e60423a518 100644
--- a/autogpt_platform/backend/backend/copilot/executor/processor.py
+++ b/autogpt_platform/backend/backend/copilot/executor/processor.py
@@ -11,14 +11,21 @@ import os
import subprocess
import threading
import time
+import uuid
from contextlib import AsyncExitStack
from typing import TYPE_CHECKING, Callable, cast
from backend.copilot import stream_registry
from backend.copilot.baseline import stream_chat_completion_baseline
+from backend.copilot.budget_gate import record_refusal, refusal_for
from backend.copilot.config import ChatConfig
from backend.copilot.context import set_turn_unattended
from backend.copilot.credential_selection import set_turn_credential_pins
+from backend.copilot.credit_billing import (
+ CreditBilledTurn,
+ bill_to_credits,
+ credit_billing,
+)
from backend.copilot.engine import resolve_use_sdk
from backend.copilot.expert_context import (
EXPERT_SESSION_MISSING_MESSAGE,
@@ -26,12 +33,20 @@ from backend.copilot.expert_context import (
ExpertSessionUnavailableError,
)
from backend.copilot.llm_attribution import set_turn_impersonated
-from backend.copilot.response_model import StreamError, StreamStatus
+from backend.copilot.model import maybe_append_user_message
+from backend.copilot.rate_limit import RateLimitUnavailable
+from backend.copilot.response_model import (
+ StreamError,
+ StreamStart,
+ StreamStatus,
+ StreamTurnPaused,
+)
from backend.copilot.sdk import service as sdk_service
from backend.copilot.sdk.dummy import stream_chat_completion_dummy
from backend.copilot.stream_heartbeat import wrap_stream_with_heartbeat
from backend.copilot.tools.agent_browser import close_browser_daemon
from backend.copilot.trial_cost_context import trial_cost_context
+from backend.copilot.turn_pause import TurnPause
from backend.copilot.turn_tracing import TurnTrace, turn_transaction
from backend.data.model import OAuth2Credentials
from backend.executor.cluster_lock import ClusterLock
@@ -67,6 +82,12 @@ logger = TruncatedLogger(logging.getLogger(__name__), prefix="[CoPilotExecutor]"
SHUTDOWN_ERROR_MESSAGE = (
"Copilot executor shut down before this turn finished. Please retry."
)
+# A platform turn whose allowance can't be read doesn't run unmetered.
+RATE_LIMIT_UNAVAILABLE_CODE = "rate_limit_unavailable"
+RATE_LIMIT_UNAVAILABLE_MESSAGE = (
+ "Usage limits can't be checked right now, so this didn't start. "
+ "Please try again in a moment."
+)
# Max time execute() blocks after requesting async turn cancellation. The worker
# waits for normal cleanup so late stream writes do not race the manager, but it
@@ -244,6 +265,28 @@ async def _wait_for_expert_setup(session: "ChatSession") -> None:
await asyncio.sleep(EXPERT_SETUP_POLL_SECONDS)
+async def _refuse_turn(
+ entry: CoPilotExecutionEntry, session: "ChatSession", pause: TurnPause
+) -> None:
+ """End a turn the allowance can't cover without running it.
+
+ The chat keeps the turn's message, as a turn that ran would, followed by
+ a not-started marker; the live stream carries the same pause part the
+ engines end a landed turn with.
+ """
+ maybe_append_user_message(
+ session, entry.message or None, entry.is_user_message, entry.message_metadata
+ )
+ await record_refusal(session, pause)
+ for chunk in (
+ StreamStart(messageId=str(uuid.uuid4()), sessionId=entry.session_id),
+ StreamTurnPaused(pause=pause.as_part()),
+ ):
+ await stream_registry.publish_chunk(
+ entry.turn_id, chunk, session_id=entry.session_id
+ )
+
+
def execute_copilot_turn(
entry: CoPilotExecutionEntry,
cancel: threading.Event,
@@ -573,6 +616,7 @@ class CoPilotProcessor:
credential_lease = None
cost_context_stack = AsyncExitStack()
scheduled_watch = ScheduledTurnWatch.for_entry(entry)
+ credit_turn: CreditBilledTurn | None = None
try:
from backend.copilot.model import get_chat_session
@@ -599,6 +643,48 @@ class CoPilotProcessor:
raise RuntimeError("codex_session_route_mismatch")
session = await _normalize_private_expert_session_tenancy(session)
+ if entry.llm_auth_provider == "platform":
+ # Every platform-billed turn passes the allowance here, from
+ # whichever path queued it (see backend.copilot.budget_gate).
+ # An AutoPilot block turn the allowance can't cover may run on
+ # the credit balance instead (see backend.copilot.credit_billing).
+ try:
+ pause = await refusal_for(entry.user_id)
+ if pause is not None:
+ # Billed to the tenancy the turn runs under (as
+ # streamed below), as the graph's other blocks are.
+ is_expert_session = session.expert_id is not None
+ credit_turn = await bill_to_credits(
+ user_id=entry.user_id,
+ session_id=entry.session_id,
+ tool_name=entry.tool_name,
+ refusal=pause,
+ organization_id=(
+ session.organization_id
+ if is_expert_session
+ else entry.organization_id
+ ),
+ team_id=(
+ session.team_id if is_expert_session else entry.team_id
+ ),
+ )
+ except RateLimitUnavailable:
+ error_msg = RATE_LIMIT_UNAVAILABLE_MESSAGE
+ error_code = RATE_LIMIT_UNAVAILABLE_CODE
+ return
+ if pause is not None and credit_turn is None:
+ log.info(f"Turn not started: the {pause.reason} limit is used up")
+ await _refuse_turn(entry, session, pause)
+ return
+ if credit_turn is not None:
+ log.info(
+ f"The {credit_turn.refusal.reason} limit is used up: "
+ f"billing this block turn to credits, up to "
+ f"{credit_turn.cap_credits} credits"
+ )
+ await cost_context_stack.enter_async_context(
+ credit_billing(credit_turn)
+ )
await _wait_for_expert_setup(session)
if entry.llm_auth_provider == "codex":
@@ -726,6 +812,11 @@ class CoPilotProcessor:
log.info(
"Forcing SDK engine: session is in agent building mode"
)
+ if credit_turn is not None and not use_sdk:
+ # Only the SDK engine caps a turn's spend, and a turn
+ # on the credit balance mustn't overdraw it.
+ use_sdk = True
+ log.info("Forcing SDK engine: turn is billed to credits")
stream_fn = (
sdk_service.stream_chat_completion_sdk
if use_sdk
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor_budget_gate_test.py b/autogpt_platform/backend/backend/copilot/executor/processor_budget_gate_test.py
new file mode 100644
index 0000000000..b5eacbf5ef
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/executor/processor_budget_gate_test.py
@@ -0,0 +1,396 @@
+"""The executor checks the allowance of every platform turn it picks up.
+
+Whatever queued the turn -- the chat route, a schedule, the AutoPilot block, a
+sub-session, a chat bot -- a turn the allowance can't cover never runs: it
+ends as a not-started pause, live and on a marker row, without an error. The
+one exception is the AutoPilot block's turn, which runs on the credit balance
+instead when the balance covers it.
+"""
+
+import logging
+import threading
+from datetime import UTC, datetime
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
+
+import pytest
+
+from backend.copilot.config import ChatConfig
+from backend.copilot.executor.processor import (
+ RATE_LIMIT_UNAVAILABLE_CODE,
+ RATE_LIMIT_UNAVAILABLE_MESSAGE,
+ CoPilotProcessor,
+)
+from backend.copilot.executor.utils import CoPilotExecutionEntry, CoPilotLogMetadata
+from backend.copilot.model import ChatMessage, ChatSession
+from backend.copilot.rate_limit import RateLimitUnavailable
+from backend.copilot.response_model import StreamStart, StreamTurnPaused
+from backend.copilot.token_tracking import persist_and_record_usage
+from backend.copilot.turn_pause import TurnPause, pause_of
+
+_PROC = "backend.copilot.executor.processor"
+PAUSE = TurnPause(
+ reason="daily",
+ resets_at=datetime(2026, 10, 15, tzinfo=UTC),
+ checkpoint_saved=False,
+ started=False,
+)
+
+
[email protected](autouse=True)
+def no_trial_attribution(mocker):
+ store = MagicMock()
+ store.get_usage_activation_state = AsyncMock(
+ side_effect=lambda user_id: SimpleNamespace(
+ user_id=user_id, generation=None, trial_id=None, ready=True
+ )
+ )
+ mocker.patch(
+ "backend.copilot.usage_activation.pro_activation_db", return_value=store
+ )
+
+
+class _Turn:
+ """Runs ``_execute_async`` with the registry, session store and engine
+ replaced, recording what the turn published and how it ended."""
+
+ def __init__(self, session: ChatSession) -> None:
+ self.session = session
+ self.publish = AsyncMock()
+ self.complete = AsyncMock()
+ self.save = AsyncMock()
+ self.engine = MagicMock(return_value=MagicMock())
+ self.stream = MagicMock(return_value=_EmptyStream())
+
+ async def run(self, entry: CoPilotExecutionEntry, refusal: AsyncMock) -> None:
+ with (
+ patch(f"{_PROC}.refusal_for", refusal),
+ patch(f"{_PROC}.stream_registry.publish_chunk", self.publish),
+ patch(f"{_PROC}.stream_registry.mark_session_completed", self.complete),
+ patch(f"{_PROC}.stream_registry.stream_and_publish", self.stream),
+ patch(f"{_PROC}.ChatConfig", return_value=MagicMock(test_mode=True)),
+ patch(f"{_PROC}.stream_chat_completion_dummy", self.engine),
+ patch(
+ "backend.copilot.model.get_chat_session",
+ AsyncMock(return_value=self.session),
+ ),
+ patch("backend.copilot.budget_gate.upsert_chat_session", self.save),
+ ):
+ await CoPilotProcessor()._execute_async(
+ entry,
+ threading.Event(),
+ MagicMock(),
+ CoPilotLogMetadata(logger=logging.getLogger("test-copilot")),
+ )
+
+ def published(self) -> list:
+ return [call.args[1] for call in self.publish.await_args_list]
+
+
+class _EmptyStream:
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ raise StopAsyncIteration
+
+ async def aclose(self) -> None:
+ return None
+
+
+def _entry(**update) -> CoPilotExecutionEntry:
+ entry = CoPilotExecutionEntry(
+ session_id="sess-1",
+ turn_id="turn-1",
+ user_id="user-1",
+ message="summarise my inbox",
+ )
+ return entry.model_copy(update=update)
+
+
+def _session(*messages: ChatMessage) -> ChatSession:
+ session = ChatSession.new("user-1", dry_run=False)
+ session.session_id = "sess-1"
+ session.messages = list(messages)
+ return session
+
+
+async def test_a_refused_turn_ends_paused_without_running():
+ turn = _Turn(_session(ChatMessage(role="assistant", content="Earlier.")))
+ refusal = AsyncMock(return_value=PAUSE)
+
+ await turn.run(_entry(), refusal)
+
+ refusal.assert_awaited_once_with("user-1")
+ turn.engine.assert_not_called()
+ turn.stream.assert_not_called()
+ paused = [c for c in turn.published() if isinstance(c, StreamTurnPaused)]
+ assert [c.pause for c in paused] == [PAUSE.as_part()]
+ assert any(isinstance(c, StreamStart) for c in turn.published())
+ for call in turn.publish.await_args_list:
+ assert call.args[0] == "turn-1"
+ # The chat keeps what was asked and why it didn't run.
+ asked, marker = turn.session.messages[-2:]
+ assert (asked.role, asked.content) == ("user", "summarise my inbox")
+ assert pause_of(marker) == PAUSE
+ turn.save.assert_awaited_once_with(turn.session)
+ turn.complete.assert_awaited_once_with(
+ "sess-1", error_message=None, error_code=None, turn_id="turn-1"
+ )
+
+
+async def test_a_message_the_route_already_saved_is_not_saved_twice():
+ turn = _Turn(_session(ChatMessage(role="user", content="summarise my inbox")))
+
+ await turn.run(_entry(), AsyncMock(return_value=PAUSE))
+
+ assert [m.content for m in turn.session.messages if m.role == "user"] == [
+ "summarise my inbox"
+ ]
+ assert pause_of(turn.session.messages[-1]) == PAUSE
+
+
+async def test_an_unreadable_allowance_fails_the_turn_closed():
+ turn = _Turn(_session())
+
+ await turn.run(_entry(), AsyncMock(side_effect=RateLimitUnavailable()))
+
+ turn.engine.assert_not_called()
+ turn.stream.assert_not_called()
+ assert not any(isinstance(c, StreamTurnPaused) for c in turn.published())
+ turn.complete.assert_awaited_once_with(
+ "sess-1",
+ error_message=RATE_LIMIT_UNAVAILABLE_MESSAGE,
+ error_code=RATE_LIMIT_UNAVAILABLE_CODE,
+ turn_id="turn-1",
+ )
+
+
+async def test_a_turn_the_allowance_covers_runs():
+ turn = _Turn(_session())
+
+ await turn.run(_entry(), AsyncMock(return_value=None))
+
+ turn.engine.assert_called_once()
+ turn.stream.assert_called_once()
+ turn.save.assert_not_awaited()
+
+
+async def test_a_turn_on_the_users_own_subscription_is_not_checked():
+ session = ChatSession.new(
+ "user-1",
+ dry_run=False,
+ llm_auth_provider="microsoft_365_copilot",
+ llm_credential_id="cred-microsoft",
+ )
+ session.session_id = "sess-1"
+ turn = _Turn(session)
+ refusal = AsyncMock(return_value=PAUSE)
+ manager = MagicMock(acquire_lease=AsyncMock(side_effect=ValueError))
+
+ with patch(f"{_PROC}.IntegrationCredentialsManager", return_value=manager):
+ with pytest.raises(RuntimeError):
+ await turn.run(
+ _entry(
+ llm_auth_provider="microsoft_365_copilot",
+ llm_credential_id="cred-microsoft",
+ ),
+ refusal,
+ )
+
+ refusal.assert_not_awaited()
+ manager.acquire_lease.assert_awaited_once()
+
+
+class _SpendingStream:
+ """A turn that records its cost the way an engine does, then ends."""
+
+ def __init__(self, cost_usd: float) -> None:
+ self.cost_usd = cost_usd
+ self.done = False
+
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ if not self.done:
+ self.done = True
+ await persist_and_record_usage(
+ session=None,
+ user_id="user-1",
+ prompt_tokens=1_000,
+ completion_tokens=100,
+ cost_usd=self.cost_usd,
+ )
+ raise StopAsyncIteration
+
+ async def aclose(self) -> None:
+ return None
+
+
+_CREDITS_ON = "backend.copilot.credit_billing.settings.config.enable_credit"
+
+
+def credits(mocker) -> MagicMock:
+ db = MagicMock()
+ db.get_credits = AsyncMock(return_value=500)
+ db.spend_credits = AsyncMock(return_value=0)
+ db.get_org_credits = AsyncMock(return_value=0)
+ db.spend_org_credits = AsyncMock(return_value=0)
+ mocker.patch("backend.copilot.credit_billing.credit_db", return_value=db)
+ # A deployment that bills credits, on a route the SDK engine can take.
+ mocker.patch(_CREDITS_ON, True)
+ mocker.patch.object(
+ ChatConfig, "thinking_available", new_callable=PropertyMock, return_value=True
+ )
+ return db
+
+
+def windows(mocker) -> AsyncMock:
+ """What records spend on the plan's daily and weekly windows."""
+ mocker.patch("backend.copilot.token_tracking._schedule_cost_log")
+ return mocker.patch(
+ "backend.copilot.token_tracking.record_cost_usage", new=AsyncMock()
+ )
+
+
+def _paused(turn: _Turn) -> list:
+ return [c.pause for c in turn.published() if isinstance(c, StreamTurnPaused)]
+
+
+async def test_a_block_turn_past_its_allowance_runs_on_credits_and_pays_its_cost(
+ credits, windows
+):
+ turn = _Turn(_session())
+ turn.stream = MagicMock(return_value=_SpendingStream(cost_usd=0.1234))
+
+ await turn.run(_entry(tool_name="autopilot_block"), AsyncMock(return_value=PAUSE))
+
+ turn.engine.assert_called_once()
+ assert _paused(turn) == []
+ # $0.1234 is 1,234 tenths of a cent: 13 credits, rounded up.
+ credits.spend_credits.assert_awaited_once()
+ charge = credits.spend_credits.await_args.kwargs
+ assert (charge["user_id"], charge["cost"]) == ("user-1", 13)
+ assert charge["metadata"].chat_session_id == "sess-1"
+ assert charge["metadata"].block == "AutoPilotBlock"
+ windows.assert_not_awaited()
+ turn.complete.assert_awaited_once_with(
+ "sess-1", error_message=None, error_code=None, turn_id="turn-1"
+ )
+
+
+async def test_an_organizations_block_turn_is_charged_to_the_organization(
+ credits, windows
+):
+ credits.get_org_credits.return_value = 300
+ turn = _Turn(_session())
+ turn.stream = MagicMock(return_value=_SpendingStream(cost_usd=0.1234))
+
+ await turn.run(
+ _entry(tool_name="autopilot_block", organization_id="org-1", team_id="team-1"),
+ AsyncMock(return_value=PAUSE),
+ )
+
+ credits.get_org_credits.assert_awaited_once_with(org_id="org-1")
+ credits.spend_credits.assert_not_awaited()
+ charge = credits.spend_org_credits.await_args.kwargs
+ assert (charge["org_id"], charge["team_id"], charge["cost"]) == (
+ "org-1",
+ "team-1",
+ 13,
+ )
+
+
+async def test_a_turn_the_allowance_covers_still_spends_from_its_windows(
+ credits, windows
+):
+ turn = _Turn(_session())
+ turn.stream = MagicMock(return_value=_SpendingStream(cost_usd=0.1234))
+
+ await turn.run(_entry(tool_name="autopilot_block"), AsyncMock(return_value=None))
+
+ windows.assert_awaited_once()
+ assert windows.await_args.kwargs["cost_microdollars"] == 123_400
+ credits.get_credits.assert_not_awaited()
+ credits.spend_credits.assert_not_awaited()
+
+
+async def test_a_block_turn_the_balance_cant_cover_is_refused_and_charged_nothing(
+ credits, windows
+):
+ credits.get_credits.return_value = 49
+ turn = _Turn(_session())
+
+ await turn.run(_entry(tool_name="autopilot_block"), AsyncMock(return_value=PAUSE))
+
+ turn.engine.assert_not_called()
+ turn.stream.assert_not_called()
+ assert _paused(turn) == [PAUSE.as_part()]
+ credits.spend_credits.assert_not_awaited()
+ windows.assert_not_awaited()
+
+
[email protected]("tool_name", [None, "chat", "run_sub_session"])
+async def test_a_turn_the_block_didnt_start_is_refused_whatever_the_balance(
+ credits, windows, tool_name
+):
+ credits.get_credits.return_value = 5_000
+ turn = _Turn(_session())
+
+ await turn.run(_entry(tool_name=tool_name), AsyncMock(return_value=PAUSE))
+
+ turn.engine.assert_not_called()
+ assert _paused(turn) == [PAUSE.as_part()]
+ credits.get_credits.assert_not_awaited()
+ credits.spend_credits.assert_not_awaited()
+
+
+async def test_an_unreadable_allowance_fails_a_block_turn_closed_too(credits):
+ turn = _Turn(_session())
+
+ await turn.run(
+ _entry(tool_name="autopilot_block"),
+ AsyncMock(side_effect=RateLimitUnavailable()),
+ )
+
+ turn.engine.assert_not_called()
+ assert _paused(turn) == []
+ credits.get_credits.assert_not_awaited()
+ turn.complete.assert_awaited_once_with(
+ "sess-1",
+ error_message=RATE_LIMIT_UNAVAILABLE_MESSAGE,
+ error_code=RATE_LIMIT_UNAVAILABLE_CODE,
+ turn_id="turn-1",
+ )
+
+
+async def test_an_unreadable_balance_fails_a_block_turn_closed(credits):
+ credits.get_credits.side_effect = ConnectionError("db down")
+ turn = _Turn(_session())
+
+ await turn.run(_entry(tool_name="autopilot_block"), AsyncMock(return_value=PAUSE))
+
+ turn.engine.assert_not_called()
+ assert _paused(turn) == []
+ credits.spend_credits.assert_not_awaited()
+ turn.complete.assert_awaited_once_with(
+ "sess-1",
+ error_message=RATE_LIMIT_UNAVAILABLE_MESSAGE,
+ error_code=RATE_LIMIT_UNAVAILABLE_CODE,
+ turn_id="turn-1",
+ )
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor_test.py b/autogpt_platform/backend/backend/copilot/executor/processor_test.py
index ea06ea1b9f..0f9fb0d1ed 100644
--- a/autogpt_platform/backend/backend/copilot/executor/processor_test.py
+++ b/autogpt_platform/backend/backend/copilot/executor/processor_test.py
@@ -67,6 +67,15 @@ def no_trial_attribution(mocker):
)
[email protected](autouse=True)
+def allowance_admits_every_turn(mocker):
+ """The pickup budget gate has its own tests (processor_budget_gate_test)."""
+ mocker.patch(
+ "backend.copilot.executor.processor.refusal_for",
+ AsyncMock(return_value=None),
+ )
+
+
class TestResolveUseSdk:
"""Which engine runs a turn — entirely the server's call."""
diff --git a/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert_test.py b/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert_test.py
index ea4c94f86a..b08bb4fdfb 100644
--- a/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert_test.py
+++ b/autogpt_platform/backend/backend/copilot/executor/scheduled_turn_alert_test.py
@@ -40,6 +40,15 @@ def mock_stream_publication(mocker):
)
[email protected](autouse=True)
+def allowance_admits_every_turn(mocker):
+ """The pickup budget gate has its own tests (processor_budget_gate_test)."""
+ mocker.patch(
+ "backend.copilot.executor.processor.refusal_for",
+ AsyncMock(return_value=None),
+ )
+
+
def _entry(scheduled: ScheduledTurnOrigin | None) -> CoPilotExecutionEntry:
return CoPilotExecutionEntry(
session_id="sess-weekly-report",
diff --git a/autogpt_platform/backend/backend/copilot/executor/trial_cost_test.py b/autogpt_platform/backend/backend/copilot/executor/trial_cost_test.py
index 1101d5aefa..d68f8e1723 100644
--- a/autogpt_platform/backend/backend/copilot/executor/trial_cost_test.py
+++ b/autogpt_platform/backend/backend/copilot/executor/trial_cost_test.py
@@ -59,6 +59,11 @@ async def test_delayed_trial_cost_and_later_paid_turn_are_separate(use_sdk):
patch("backend.copilot.rate_limit._fetch_user_tier", tier),
patch("backend.copilot.rate_limit.get_redis_async", AsyncMock()),
patch("backend.copilot.rate_limit._incr_counter_atomic", AsyncMock()),
+ # The pickup budget gate has its own tests (processor_budget_gate_test).
+ patch(
+ "backend.copilot.executor.processor.refusal_for",
+ AsyncMock(return_value=None),
+ ),
patch("backend.copilot.token_tracking._schedule_cost_log") as cost_log,
patch(
"backend.copilot.model.get_chat_session",
diff --git a/autogpt_platform/backend/backend/copilot/executor/utils.py b/autogpt_platform/backend/backend/copilot/executor/utils.py
index c9b59ef822..59fe8f96a4 100644
--- a/autogpt_platform/backend/backend/copilot/executor/utils.py
+++ b/autogpt_platform/backend/backend/copilot/executor/utils.py
@@ -363,6 +363,11 @@ class CoPilotExecutionEntry(BaseModel):
"""Set when the scheduler fired this turn. ``None`` for every other
caller, and for entries queued before the field existed."""
+ tool_name: str | None = None
+ """What started the turn, as registered with the stream registry
+ (``"autopilot_block"`` for the AutoPilot block, whose turns may run on the
+ credit balance once the plan's allowance is used up). ``None`` for entries
+ queued before the field existed."""
trace_headers: dict[str, str] = Field(default_factory=dict)
"""``sentry-trace``/``baggage`` of the transaction that dispatched the turn,
which the turn's own transaction continues. Empty when the dispatcher was
@@ -404,6 +409,7 @@ async def enqueue_copilot_turn(
impersonated: bool = False,
credential_pins: CredentialPins | None = None,
scheduled: ScheduledTurnOrigin | None = None,
+ tool_name: str | None = None,
trace_headers: Mapping[str, str] | None = None,
) -> None:
"""Enqueue a CoPilot task for processing by the executor service.
@@ -462,6 +468,7 @@ async def enqueue_copilot_turn(
),
enqueued_at=time.time(),
cost_context=await capture_cost_context(user_id),
+ tool_name=tool_name,
)
queue_client = await get_async_copilot_queue()
@@ -691,6 +698,7 @@ async def dispatch_turn(
impersonated=impersonated,
credential_pins=credential_pins,
scheduled=scheduled,
+ tool_name=tool_name,
trace_headers=trace_headers,
)
slot.keep()
diff --git a/autogpt_platform/backend/backend/copilot/rate_limit.py b/autogpt_platform/backend/backend/copilot/rate_limit.py
index 6e5fb9f59b..337f93ca7b 100644
--- a/autogpt_platform/backend/backend/copilot/rate_limit.py
+++ b/autogpt_platform/backend/backend/copilot/rate_limit.py
@@ -56,7 +56,7 @@ import asyncio
import logging
from datetime import UTC, datetime, timedelta
from enum import Enum
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Literal
import fastapi
from autogpt_libs.auth.dependencies import get_user_id
@@ -71,6 +71,12 @@ from backend.copilot.usage_activation import (
get_ready_usage_state,
usage_keys,
)
+from backend.copilot.usage_state import (
+ LimitWindow,
+ UsageState,
+ WindowReading,
+ usage_verdict,
+)
from backend.data.db_accessors import credit_db, user_db
from backend.data.redis_client import AsyncRedisClient, get_redis_async
from backend.data.subscription_trial import TrialState
@@ -379,6 +385,12 @@ class CoPilotUsageStatus(BaseModel):
default=0,
description="Credit cost (in cents) to reset the daily limit. 0 = feature disabled.",
)
+ # A lifetime trial already shows a spent allowance as a full weekly
+ # window; a trial whose period caps are tighter than its allowance doesn't.
+ trial_spent: bool = False
+ # The weekly window holds the trial's lifetime allowance rather than a
+ # weekly cap: it doesn't refill, and ``resets_at`` is the trial's end.
+ weekly_is_trial_allowance: bool = False
class UsageWindowPublic(BaseModel):
@@ -413,6 +425,24 @@ class CoPilotUsagePublic(BaseModel):
default=0,
description="Credit cost (in cents) to reset the daily limit. 0 = feature disabled.",
)
+ state: UsageState = Field(
+ default="ok",
+ description="'exhausted' when a turn can't start now (a capped window "
+ "or the trial allowance is used up), 'low' from 80% of the tightest "
+ "window (where the meters turn amber), otherwise 'ok'.",
+ )
+ limiting_window: LimitWindow | None = Field(
+ default=None,
+ description="The limit behind a 'low' or 'exhausted' state: the "
+ "used-up window that refills last, 'trial' for the trial allowance, "
+ "or the fullest window when 'low'. Null when 'ok'.",
+ )
+ resets_at: datetime | None = Field(
+ default=None,
+ description="When the limiting window refills. Null when 'ok', for "
+ "the trial allowance, and for a window that allows nothing (no plan), "
+ "none of which refill.",
+ )
@classmethod
def from_status(cls, status: CoPilotUsageStatus) -> "CoPilotUsagePublic":
@@ -449,19 +479,53 @@ class CoPilotUsagePublic(BaseModel):
resets_at=w.resets_at,
)
+ daily, weekly = window(status.daily), window(status.weekly)
+ readings = [
+ reading
+ for reading in (
+ _reading("daily", status.daily, daily),
+ _reading("weekly", status.weekly, weekly),
+ )
+ if reading is not None
+ ]
+ verdict = usage_verdict(
+ readings,
+ weekly_is_trial_allowance=status.weekly_is_trial_allowance,
+ trial_spent=status.trial_spent,
+ )
return cls(
- daily=window(status.daily),
- weekly=window(status.weekly),
+ daily=daily,
+ weekly=weekly,
tier=status.tier,
reset_cost=status.reset_cost,
+ state=verdict.state,
+ limiting_window=verdict.limiting_window,
+ resets_at=verdict.resets_at,
)
+def _reading(
+ name: Literal["daily", "weekly"],
+ window: UsageWindow,
+ public: UsageWindowPublic | None,
+) -> WindowReading | None:
+ """A capped window as its meter shows it; None when it has no cap."""
+ if public is None:
+ return None
+ return WindowReading(
+ window=name,
+ percent_used=public.percent_used,
+ exhausted=window.used >= window.limit,
+ resets_at=window.resets_at,
+ refills=window.limit > 0,
+ )
+
+
class RateLimitExceeded(Exception):
"""Raised when a user exceeds their CoPilot usage limit."""
- def __init__(self, window: str, resets_at: datetime):
- self.window = window
+ def __init__(self, window: LimitWindow, resets_at: datetime):
+ self.window: LimitWindow = window
self.resets_at = resets_at
delta = resets_at - datetime.now(UTC)
total_secs = delta.total_seconds()
@@ -485,6 +549,7 @@ async def get_usage_status(
weekly_cost_limit: int,
rate_limit_reset_cost: int = 0,
tier: SubscriptionTier = DEFAULT_TIER,
+ fail_closed: bool = False,
) -> CoPilotUsageStatus:
"""Get current usage status for a user.
@@ -496,12 +561,22 @@ async def get_usage_status(
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).
+ fail_closed: Raise ``RateLimitUnavailable`` when the counters can't be
+ read, instead of reporting no usage. For callers that act on the
+ figures rather than show them.
Returns:
CoPilotUsageStatus with current usage and limits in microdollars.
"""
now = datetime.now(UTC)
state = await get_ready_usage_state(user_id)
+ if daily_cost_limit == 0 and weekly_cost_limit == 0:
+ # No plan, or the trial is over: nothing can be spent, so the
+ # counters don't matter. An ended trial still has its stale TRIAL
+ # state here, which reports it as a spent trial rather than no plan.
+ return _no_allowance_status(
+ now, tier, trial_ended=state.tier == SubscriptionTier.TRIAL
+ )
if await _effective_usage_tier(user_id, state.tier) != tier:
raise RateLimitUnavailable("Subscription changed while resolving usage limits")
d_key, w_key = usage_keys(user_id, state.generation, now)
@@ -522,13 +597,16 @@ async def get_usage_status(
OSError,
ValueError,
) as exc:
- if state.generation is not None:
+ if state.generation is not None or fail_closed:
raise RateLimitUnavailable("Paid usage state is unavailable") from exc
logger.warning("Redis unavailable for usage status, returning zeros")
weekly_reset = _weekly_reset_time(now=now)
+ trial_spent = False
+ weekly_is_trial_allowance = False
if tier == SubscriptionTier.TRIAL:
trial = await credit_db().get_subscription_trial(user_id)
+ trial_spent = _trial_spent(trial)
if trial and _trial_uses_total_budget(
trial, daily_cost_limit, weekly_cost_limit
):
@@ -538,6 +616,7 @@ async def get_usage_status(
weekly_used = trial.cost_microdollars
weekly_cost_limit = trial.offer.total_cost_limit
weekly_reset = trial.ends_at or now
+ weekly_is_trial_allowance = True
return CoPilotUsageStatus(
daily=UsageWindow(
@@ -552,6 +631,32 @@ async def get_usage_status(
),
tier=tier,
reset_cost=0 if tier == SubscriptionTier.TRIAL else rate_limit_reset_cost,
+ trial_spent=trial_spent,
+ weekly_is_trial_allowance=weekly_is_trial_allowance,
+ )
+
+
+def _no_allowance_status(
+ now: datetime, tier: SubscriptionTier, *, trial_ended: bool
+) -> CoPilotUsageStatus:
+ return CoPilotUsageStatus(
+ daily=UsageWindow(used=0, limit=0, resets_at=_daily_reset_time(now=now)),
+ weekly=UsageWindow(used=0, limit=0, resets_at=_weekly_reset_time(now=now)),
+ tier=tier,
+ reset_cost=0,
+ trial_spent=trial_ended,
+ )
+
+
+def _trial_spent(trial: "TrialState | None") -> bool:
+ """No trial turn may start: the trial is over or its allowance is used.
+
+ The same conditions :func:`check_rate_limit` refuses a trial turn on.
+ """
+ return (
+ trial is None
+ or not trial.active
+ or trial.cost_microdollars >= trial.offer.total_cost_limit
)
@@ -788,12 +893,16 @@ async def check_rate_limit(
# collapse semantics of :func:`get_global_rate_limits` and produced
# the autopilot paywall bypass. Only an explicitly negative limit — the
# self-hosted "no cap" sentinel — skips a window.
- if not skip_daily and daily_cost_limit >= 0 and daily_used >= daily_cost_limit:
- raise RateLimitExceeded("daily", _daily_reset_time(now=now))
-
+ #
+ # Weekly first: with both windows used up, the user is blocked until the
+ # one that refills last, and the weekly window never refills before the
+ # daily one (on Sundays they refill together).
if weekly_cost_limit >= 0 and weekly_used >= weekly_cost_limit:
raise RateLimitExceeded("weekly", _weekly_reset_time(now=now))
+ if not skip_daily and daily_cost_limit >= 0 and daily_used >= daily_cost_limit:
+ raise RateLimitExceeded("daily", _daily_reset_time(now=now))
+
def _trial_uses_total_budget(
trial: "TrialState", daily_cost_limit: int, weekly_cost_limit: int
diff --git a/autogpt_platform/backend/backend/copilot/rate_limit_test.py b/autogpt_platform/backend/backend/copilot/rate_limit_test.py
index 20c030c63e..80e4cb7c5a 100644
--- a/autogpt_platform/backend/backend/copilot/rate_limit_test.py
+++ b/autogpt_platform/backend/backend/copilot/rate_limit_test.py
@@ -162,6 +162,22 @@ class TestGetUsageStatus:
assert status.daily.used == 0
assert status.weekly.used == 0
+ @pytest.mark.asyncio
+ async def test_fails_closed_when_asked_and_redis_unavailable(self):
+ # A caller that acts on the figures mustn't read "no usage" off an
+ # outage.
+ with patch(
+ "backend.copilot.rate_limit.get_redis_async",
+ side_effect=ConnectionError("Redis down"),
+ ):
+ with pytest.raises(RateLimitUnavailable):
+ await get_usage_status(
+ _USER,
+ daily_cost_limit=10000,
+ weekly_cost_limit=50000,
+ fail_closed=True,
+ )
+
@pytest.mark.asyncio
async def test_partial_none_daily_counter(self):
"""Daily counter is None (new day), weekly has usage."""
@@ -307,6 +323,24 @@ class TestCheckRateLimit:
)
assert exc_info.value.window == "weekly"
+ @pytest.mark.asyncio
+ async def test_both_windows_used_up_names_the_one_that_refills_last(self):
+ """Regression: with both caps hit the user is blocked until the weekly
+ reset, not the next midnight the daily check used to report."""
+ mock_redis = AsyncMock()
+ mock_redis.get = AsyncMock(side_effect=["10000", "50000"])
+
+ with patch(
+ "backend.copilot.rate_limit.get_redis_async",
+ return_value=mock_redis,
+ ):
+ with pytest.raises(RateLimitExceeded) as exc_info:
+ await check_rate_limit(
+ _USER, daily_cost_limit=10000, weekly_cost_limit=50000
+ )
+ assert exc_info.value.window == "weekly"
+ assert exc_info.value.resets_at == _weekly_reset_time()
+
@pytest.mark.asyncio
async def test_raises_unavailable_when_redis_connection_error(self):
"""Fail-closed: ConnectionError must surface as RateLimitUnavailable so
@@ -402,7 +436,8 @@ class TestCheckRateLimit:
):
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"
+ # Both windows are used up, so the one that refills last is named.
+ assert exc_info.value.window == "weekly"
@pytest.mark.asyncio
async def test_negative_limit_disables_the_window(self):
diff --git a/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py b/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py
index 63bf8ac580..b637517405 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py
@@ -32,6 +32,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from claude_agent_sdk import ResultMessage
+from backend.copilot.budget_gate import AutoContinueGate
from backend.copilot.model import ChatMessage, ChatSession, ChatSessionMetadata
from backend.copilot.response_model import StreamStart
from backend.copilot.turn_budget import TurnBudget
@@ -164,6 +165,10 @@ def _make_patches(*, hire_experts_enabled: bool):
return_value=TurnBudget(usd=100.0, limiting="task"),
),
),
+ (
+ f"{_SVC}.gate_auto_continue",
+ dict(new_callable=AsyncMock, return_value=AutoContinueGate(proceed=True)),
+ ),
(f"{_SVC}.drain_pending_safe", dict(new_callable=AsyncMock, return_value=[])),
]
return patches, mcp_server_mock, is_feature_enabled_mock
diff --git a/autogpt_platform/backend/backend/copilot/sdk/governor.py b/autogpt_platform/backend/backend/copilot/sdk/governor.py
index 375ba08b3f..e7fafe540d 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/governor.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/governor.py
@@ -15,6 +15,7 @@ didn't: ``max_budget_usd`` above the budget (in the CLI's price units) and
from claude_agent_sdk import AssistantMessage, Message, StreamEvent
+from backend.copilot.constants import PER_TURN_SPEND_FLOOR_USD
from backend.copilot.turn_budget import TurnBudget
from backend.copilot.turn_pause import PauseReason, TurnPause
@@ -22,8 +23,6 @@ from .usage_meter import UsageMeter, cli_price_ratio
# Model calls past the check-in before the CLI's own max_turns stops the turn.
STEP_BACKSTOP_MARGIN = 50
-# The CLI refuses to start a query whose budget is very small.
-_CLI_MIN_BUDGET_USD = 0.5
# Spend held back for the checkpoint: about two calls at the latest call's
# price (the round whose tools are refused, then the reply), within bounds.
_RESERVE_MIN_USD = 0.02
@@ -142,7 +141,7 @@ class TurnGovernor:
return self.budget.usd * cli_price_ratio(model)
headroom = max(_BACKSTOP_MIN_USD, _BACKSTOP_SHARE * self.budget.usd)
cli_usd = (self.budget.usd + headroom) * cli_price_ratio(model)
- return max(_CLI_MIN_BUDGET_USD, cli_usd)
+ return max(PER_TURN_SPEND_FLOOR_USD, cli_usd)
def _update(self) -> None:
if self.landing is not None:
diff --git a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
index 7eaf640307..0f497b326b 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
@@ -31,6 +31,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from claude_agent_sdk import ResultMessage
+from backend.copilot.budget_gate import AutoContinueGate
from backend.copilot.constants import COMPACTION_TOOL_NAME
from backend.copilot.model import ChatMessage, ChatSession
from backend.copilot.response_model import (
@@ -1092,6 +1093,12 @@ def _make_sdk_patches(
return_value=TurnBudget(usd=100.0, limiting="task"),
),
),
+ # The auto-continue allowance check peeks the pending queue in Redis
+ # (covered in budget_gate_test).
+ (
+ f"{_SVC}.gate_auto_continue",
+ dict(new_callable=AsyncMock, return_value=AutoContinueGate(proceed=True)),
+ ),
# Stub pending-message drain so retry tests don't hit Redis.
# Returns an empty list → no mid-turn injection happens.
(
@@ -2120,6 +2127,56 @@ class TestStreamChatCompletionRetryIntegration:
)
assert any(isinstance(e, StreamStart) for e in events)
+ @pytest.mark.asyncio
+ async def test_queued_messages_wait_when_the_allowance_is_used_up(self):
+ """Auto-continue asks the allowance first. Refused, it drains nothing
+ and starts no second query: the turn ends on a not-started pause and
+ the queued messages stay queued for the next turn."""
+ from backend.copilot.response_model import StreamTurnPaused
+ from backend.copilot.turn_pause import TurnPause
+
+ session = self._make_session()
+ result_msg = self._make_result_message()
+ queries: list[int] = []
+
+ def _client_factory(*args, **kwargs):
+ queries.append(1)
+ return self._make_client_mock(result_message=result_msg)
+
+ pause = TurnPause(reason="daily", checkpoint_saved=False, started=False)
+ refused = AsyncMock(return_value=AutoContinueGate(proceed=False, pause=pause))
+ drain = AsyncMock(return_value=[])
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=_build_transcript(
+ [("user", "prior question"), ("assistant", "prior answer")]
+ ),
+ compacted_transcript=None,
+ client_side_effect=_client_factory,
+ )
+
+ events = []
+ with contextlib.ExitStack() as stack:
+ for target, kwargs in patches:
+ stack.enter_context(patch(target, **kwargs))
+ stack.enter_context(patch(f"{_SVC}.gate_auto_continue", new=refused))
+ stack.enter_context(patch(f"{_SVC}.drain_pending_safe", new=drain))
+ async for event in stream_chat_completion_sdk(
+ session_id="test-session-id",
+ message="hello",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ events.append(event)
+
+ refused.assert_awaited_once_with(session, "test-user", platform_billed=True)
+ assert [e.pause for e in events if isinstance(e, StreamTurnPaused)] == [
+ pause.as_part()
+ ]
+ assert len(queries) == 1, "a refused auto-continue must not query again"
+ drain.assert_awaited_once() # the turn-start drain only
+
# ---------------------------------------------------------------------------
# Pre-query compaction row — timing and lifecycle at generator level
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 1418ea2a77..629308b6c4 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -52,6 +52,7 @@ from backend.copilot.model_router import (
resolve_codex_model_route,
resolve_model_route,
)
+from backend.copilot.budget_gate import gate_auto_continue
from backend.copilot.budget_signal import build_turn_budget_block
from backend.copilot.continuation import as_model_prompt
from backend.copilot.feedback_db import RATEABLE_ROLES
@@ -67,6 +68,7 @@ from backend.copilot.sdk.cost_tracking import (
read_cli_session_usage,
)
from backend.copilot.tool_display import tool_calls_for_provider
+from backend.copilot.credit_billing import credit_billed_turn, wait_for_cost
from backend.copilot.turn_budget import resolve_turn_budget
from backend.copilot.turn_pause import TurnPause, append_pause_marker
from backend.data.db_accessors import chat_db
@@ -5286,9 +5288,18 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
# Lands the turn with a checkpoint near its budget or step check-in.
# A ChatGPT subscription pays for its own turns, so it gets the
- # check-in only.
+ # check-in only. A turn billed to credits gets what its balance covers.
+ credit_turn = credit_billed_turn(user_id)
governor = TurnGovernor(
- budget=None if is_codex_transport else await resolve_turn_budget(user_id),
+ budget=(
+ None
+ if is_codex_transport
+ else (
+ credit_turn.budget
+ if credit_turn is not None
+ else await resolve_turn_budget(user_id)
+ )
+ ),
step_checkin=config.sdk_step_checkin,
)
@@ -6466,6 +6477,8 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
)
_background_tasks.add(cost_reconcile_task)
cost_reconcile_task.add_done_callback(_background_tasks.discard)
+ # A turn billed to credits is charged once this cost is in.
+ wait_for_cost(cost_reconcile_task)
else:
# Reconcile disabled, OpenRouter inactive, or subscription
# path (no gen-IDs). Record the SDK CLI's
@@ -6732,9 +6745,22 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
# A paused turn doesn't continue either: it stopped because it ran out of
# budget or reached its check-in, and the user picks it back up from the
# Paused card. Anything queued meanwhile stays queued for that turn.
+ #
+ # Nor does a platform turn the allowance can no longer cover: nobody
+ # pressed send for the continuation, so nothing else would check it. The
+ # queued messages stay queued and a not-started pause takes their place.
# -------------------------------------------------------------------------
if not ended_with_stream_error and not (governor is not None and governor.landed):
- _auto_pending_messages = await drain_pending_safe(session_id, log_prefix)
+ auto_continue = await gate_auto_continue(
+ session, user_id, platform_billed=not is_codex_transport
+ )
+ if auto_continue.pause is not None:
+ yield StreamTurnPaused(pause=auto_continue.pause.as_part())
+ _auto_pending_messages = (
+ await drain_pending_safe(session_id, log_prefix)
+ if auto_continue.proceed
+ else []
+ )
if _auto_pending_messages:
logger.info(
"%s Auto-continuing with %d pending message(s) queued after turn start",
diff --git a/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py b/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py
index 621ffb61c4..e3610840a3 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py
@@ -22,6 +22,7 @@ import logging
import time
import uuid
from dataclasses import dataclass, field
+from datetime import UTC
from typing import TYPE_CHECKING, Any, Literal
from backend.copilot import stream_registry
@@ -40,6 +41,7 @@ from backend.copilot.pending_message_helpers import (
)
from backend.copilot.response_model import StreamError, StreamFinish
from backend.copilot.tree import SpawnRequest, TreeRefusal
+from backend.copilot.turn_pause import TurnPause
from backend.copilot.turn_queue import SessionNotIdle, queued_turn_refusal
from backend.data.db_accessors import chat_db
@@ -92,6 +94,10 @@ class SessionResult:
# Why the turn never started, when the outcome is ``refused``. Written
# for the model: it names the bound that was hit.
refusal: str = ""
+ # Set when the turn ended as a pause. A turn the executor refused at
+ # pickup for want of allowance (``started=False``) comes back as
+ # ``refused``, with a ``refusal`` naming the limit: nothing ran.
+ pause: TurnPause | None = None
async def wait_for_session_result(
@@ -148,9 +154,35 @@ async def wait_for_session_result(
result.prompt_tokens = acc.prompt_tokens
result.completion_tokens = acc.completion_tokens
result.total_tokens = acc.total_tokens
+ result.pause = pause = acc.pause
+ if (
+ outcome == "completed"
+ and pause is not None
+ and not pause.started
+ and not result.response_text
+ and not result.tool_calls
+ ):
+ # Only the executor's not-started pause: the turn never ran, and
+ # reported as done the caller would take the empty answer for the
+ # task's result. A turn that ran and then couldn't continue into
+ # queued messages ends with the same pause after its own output,
+ # and did complete.
+ outcome = "refused"
+ result.refusal = _budget_refusal(pause)
return outcome, result
+def _budget_refusal(pause: TurnPause) -> str:
+ """What the caller's model is told about a turn refused for budget."""
+ if pause.resets_at is None:
+ return f"{pause.summary} The AutoPilot budget doesn't reset on its own."
+ resets_at = pause.resets_at.astimezone(UTC)
+ return (
+ f"{pause.summary} The AutoPilot budget resets "
+ f"{resets_at:%Y-%m-%d %H:%M} UTC."
+ )
+
+
async def run_copilot_turn_via_queue(
*,
session_id: str,
diff --git a/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py b/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py
index 26c67b839b..562069b89f 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py
@@ -11,12 +11,25 @@ Focuses on the queue-on-busy fallback:
fresh dispatch.
"""
+import asyncio
+from datetime import UTC, datetime
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from backend.copilot import active_turns
-from backend.copilot.sdk.session_waiter import SessionResult, run_copilot_turn_via_queue
+from backend.copilot.response_model import (
+ StreamFinish,
+ StreamStart,
+ StreamTextDelta,
+ StreamTurnPaused,
+)
+from backend.copilot.sdk.session_waiter import (
+ SessionResult,
+ run_copilot_turn_via_queue,
+ wait_for_session_result,
+)
+from backend.copilot.turn_pause import TurnPause
_QR = type(
"QR",
@@ -442,6 +455,92 @@ async def test_a_tree_refusal_becomes_the_refused_outcome():
assert result.refusal == "This task's tree has closed."
+async def _wait_on(*events):
+ queue: asyncio.Queue = asyncio.Queue()
+ for event in events:
+ queue.put_nowait(("0-0", event))
+ with (
+ patch(
+ "backend.copilot.sdk.session_waiter.stream_registry.subscribe_to_session",
+ new=AsyncMock(return_value=queue),
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.stream_registry.unsubscribe_from_session",
+ new=AsyncMock(),
+ ),
+ ):
+ return await wait_for_session_result(
+ session_id="sess-1", user_id="u1", timeout=5
+ )
+
+
+async def test_a_turn_refused_at_pickup_is_refused_not_completed():
+ """The executor ends a turn the allowance can't cover as a not-started
+ pause and the stream completes; reported as done, the caller would take
+ the empty answer for the task's result."""
+ pause = TurnPause(
+ reason="weekly",
+ resets_at=datetime(2026, 10, 19, tzinfo=UTC),
+ checkpoint_saved=False,
+ started=False,
+ )
+ outcome, result = await _wait_on(
+ StreamStart(messageId="msg-1"),
+ StreamTurnPaused(pause=pause.as_part()),
+ StreamFinish(),
+ )
+
+ assert outcome == "refused"
+ assert result.pause == pause
+ assert result.response_text == ""
+ assert result.refusal == (
+ "Not started: out of this week's budget. "
+ "The AutoPilot budget resets 2026-10-19 00:00 UTC."
+ )
+
+
+async def test_a_refusal_with_nothing_to_wait_for_says_so():
+ pause = TurnPause(reason="trial", checkpoint_saved=False, started=False)
+ outcome, result = await _wait_on(
+ StreamTurnPaused(pause=pause.as_part()), StreamFinish()
+ )
+ assert outcome == "refused"
+ assert result.refusal == (
+ "Not started: the trial allowance is used up. "
+ "The AutoPilot budget doesn't reset on its own."
+ )
+
+
+async def test_a_turn_that_paused_after_running_still_completes():
+ pause = TurnPause(reason="daily", checkpoint_saved=True, started=True)
+ outcome, result = await _wait_on(
+ StreamTextDelta(id="t-1", delta="Where I got to: half the inbox."),
+ StreamTurnPaused(pause=pause.as_part()),
+ StreamFinish(),
+ )
+ assert outcome == "completed"
+ assert result.pause == pause
+ assert result.refusal == ""
+
+
+async def test_a_turn_that_ran_and_could_not_continue_still_completes():
+ """Out of budget before queued messages, the engine ends a turn that ran
+ with a not-started pause after its output; that turn's answer stands."""
+ pause = TurnPause(reason="weekly", checkpoint_saved=False, started=False)
+ outcome, result = await _wait_on(
+ StreamTextDelta(id="t-1", delta="Here is the summary."),
+ StreamTurnPaused(pause=pause.as_part()),
+ StreamFinish(),
+ )
+ assert outcome == "completed"
+ assert result.response_text == "Here is the summary."
+ assert result.refusal == ""
+
+
def _status_db(status: str) -> MagicMock:
db = MagicMock()
db.get_chat_session_status = AsyncMock(return_value=status)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/stream_accumulator.py b/autogpt_platform/backend/backend/copilot/sdk/stream_accumulator.py
index 9cd66b104d..fc8d5868a4 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/stream_accumulator.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/stream_accumulator.py
@@ -17,13 +17,16 @@ from __future__ import annotations
import logging
from typing import Any
-from pydantic import BaseModel, Field
+from pydantic import BaseModel, Field, ValidationError
+
+from backend.copilot.turn_pause import TurnPause
from ..response_model import (
StreamError,
StreamTextDelta,
StreamToolInputAvailable,
StreamToolOutputAvailable,
+ StreamTurnPaused,
StreamUsage,
)
@@ -49,6 +52,8 @@ class EventAccumulator(BaseModel):
prompt_tokens: int = 0
completion_tokens: int = 0
total_tokens: int = 0
+ # Why the turn paused -- or, when ``started`` is False, never ran.
+ pause: TurnPause | None = None
def process_event(event: object, acc: EventAccumulator) -> str | None:
@@ -80,6 +85,11 @@ def process_event(event: object, acc: EventAccumulator) -> str | None:
acc.prompt_tokens += e.prompt_tokens
acc.completion_tokens += e.completion_tokens
acc.total_tokens += e.total_tokens
+ case StreamTurnPaused(pause=pause):
+ try:
+ acc.pause = TurnPause.model_validate(pause)
+ except ValidationError:
+ logger.warning(f"Ignoring an unreadable turn pause: {pause}")
case StreamError(errorText=err):
return err
return None
diff --git a/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py b/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py
index df023edee7..fcc445b62e 100644
--- a/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py
+++ b/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py
@@ -12,6 +12,7 @@ from claude_agent_sdk import Message
from openai.types.chat import ChatCompletionChunk
from backend.copilot.baseline import service as baseline
+from backend.copilot.budget_gate import AutoContinueGate
from backend.copilot.model import ChatMessage, ChatSession
from backend.copilot.pending_messages import PendingMessage
from backend.copilot.response_model import StreamBaseResponse, StreamStart
@@ -197,6 +198,7 @@ async def sdk_service_turn(
"resolve_turn_budget": AsyncMock(
return_value=TurnBudget(usd=100.0, limiting="task")
),
+ "gate_auto_continue": AsyncMock(return_value=AutoContinueGate(proceed=True)),
"drain_pending_safe": AsyncMock(
side_effect=[[], queued_after_first] + [[]] * len(queries)
),
diff --git a/autogpt_platform/backend/backend/copilot/token_tracking.py b/autogpt_platform/backend/backend/copilot/token_tracking.py
index 87098119e3..4eb331d8af 100644
--- a/autogpt_platform/backend/backend/copilot/token_tracking.py
+++ b/autogpt_platform/backend/backend/copilot/token_tracking.py
@@ -17,6 +17,7 @@ import threading
from openai.types.completion_usage import PromptTokensDetails
+from backend.copilot.credit_billing import credit_billed_turn, record_credit_billed_cost
from backend.copilot.trial_cost_context import get_trial_cost_context
from backend.data.db_accessors import platform_cost_db
from backend.data.platform_cost import PlatformCostEntry, usd_to_microdollars
@@ -226,7 +227,9 @@ async def persist_and_record_usage(
cost_microdollars = usd_to_microdollars(cost_float)
if cost_microdollars is not None and cost_microdollars > 0:
- if user_id:
+ # A turn billed to credits is charged to the credit balance when it
+ # ends, and spends nothing from the plan's daily and weekly windows.
+ if user_id and not await record_credit_billed_cost(user_id, cost_microdollars):
# record_cost_usage() owns its fail-open handling for Redis/network
# errors. Don't wrap with a broad except here — unexpected accounting
# bugs should surface instead of being silently logged as warnings.
@@ -284,6 +287,8 @@ async def persist_and_record_usage(
if trial_context is not None:
metadata["subscription_trial_id"] = trial_context.trial_id
metadata["usage_generation"] = trial_context.generation
+ if credit_billed_turn(user_id) is not None:
+ metadata["billing_mode"] = "credits"
_schedule_cost_log(
PlatformCostEntry(
diff --git a/autogpt_platform/backend/backend/copilot/trial_usage_limits_test.py b/autogpt_platform/backend/backend/copilot/trial_usage_limits_test.py
index c5e0ea6677..bc16971eb3 100644
--- a/autogpt_platform/backend/backend/copilot/trial_usage_limits_test.py
+++ b/autogpt_platform/backend/backend/copilot/trial_usage_limits_test.py
@@ -251,6 +251,10 @@ async def test_public_trial_exhaustion_matches_lifetime_budget_across_weekly_res
assert public.daily is not None and public.daily.percent_used < 100
assert public.weekly is not None
assert (public.weekly.percent_used >= 100) == (spent >= 20_000_000)
+ # The weekly window holds the allowance: named as the trial, no reset.
+ assert status.weekly_is_trial_allowance
+ assert public.limiting_window in (None, "trial")
+ assert public.resets_at is None
assert usage == before
@@ -277,6 +281,60 @@ async def test_legacy_trial_with_tighter_windows_retains_periodic_limits(
assert status.weekly.resets_at == _SUNDAY + timedelta(minutes=1)
+async def test_legacy_trial_weekly_cap_is_a_window_that_refills(
+ trial: TrialState, usage: dict[str, int]
+):
+ # Not the allowance: a used-up weekly cap refills on Monday, so the verdict
+ # names the week and its reset (Send at reset can wait for it).
+ trial.offer = trial.offer.model_copy(
+ update={"daily_cost_limit": 2_000_000, "weekly_cost_limit": 10_000_000}
+ )
+ trial.cost_microdollars = 10_000_000
+ usage[rate_limit._daily_key(_USER)] = 500_000
+ usage[rate_limit._weekly_key(_USER)] = 10_000_000
+ daily, weekly, tier = await rate_limit.get_global_rate_limits(_USER, 1, 1)
+
+ with pytest.raises(rate_limit.RateLimitExceeded) as exc:
+ await rate_limit.check_rate_limit(_USER, daily, weekly)
+ status = await rate_limit.get_usage_status(_USER, daily, weekly, tier=tier)
+ public = rate_limit.CoPilotUsagePublic.from_status(status)
+
+ assert exc.value.window == "weekly"
+ assert not status.weekly_is_trial_allowance
+ assert (public.state, public.limiting_window, public.resets_at) == (
+ "exhausted",
+ "weekly",
+ _SUNDAY + timedelta(minutes=1),
+ )
+
+
+async def test_legacy_trial_reports_a_spent_allowance_its_windows_dont_show(
+ trial: TrialState, usage: dict[str, int]
+):
+ trial.offer = trial.offer.model_copy(
+ update={"daily_cost_limit": 2_000_000, "weekly_cost_limit": 10_000_000}
+ )
+ trial.cost_microdollars = 20_000_000
+ usage[rate_limit._daily_key(_USER)] = 500_000
+ usage[rate_limit._weekly_key(_USER)] = 1_000_000
+ daily, weekly, tier = await rate_limit.get_global_rate_limits(_USER, 1, 1)
+
+ with pytest.raises(rate_limit.RateLimitExceeded) as exc:
+ await rate_limit.check_rate_limit(_USER, daily, weekly)
+ status = await rate_limit.get_usage_status(_USER, daily, weekly, tier=tier)
+ public = rate_limit.CoPilotUsagePublic.from_status(status)
+
+ assert exc.value.window == "trial"
+ assert status.trial_spent
+ assert (public.state, public.limiting_window, public.resets_at) == (
+ "exhausted",
+ "trial",
+ None,
+ )
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize("spent", [5_000_000, 20_000_000])
async def test_lifetime_trial_budget_remains_authoritative_without_redis(
@@ -304,3 +362,26 @@ async def test_lifetime_trial_budget_remains_authoritative_without_redis(
status = await rate_limit.get_usage_status(_USER, daily, weekly, tier=tier)
assert status.weekly.used == spent
assert status.weekly.resets_at == trial.ends_at
+
+
+async def test_an_ended_trial_reads_as_used_up_not_unavailable(
+ trial: TrialState, usage: dict[str, int]
+):
+ # The trial is over but the usage state still says TRIAL until the
+ # downgrade; get_global_rate_limits already reports NO_TIER with nothing
+ # to spend. The tier guard took that for a subscription change and failed
+ # every /usage read; there is nothing to count, so it answers directly.
+ trial.ends_at = _SUNDAY - timedelta(days=1)
+ daily, weekly, tier = await rate_limit.get_global_rate_limits(_USER, 1, 1)
+ assert (daily, weekly, tier) == (0, 0, rate_limit.SubscriptionTier.NO_TIER)
+
+ status = await rate_limit.get_usage_status(_USER, daily, weekly, tier=tier)
+ public = rate_limit.CoPilotUsagePublic.from_status(status)
+
+ assert status.trial_spent
+ assert (public.state, public.limiting_window, public.resets_at) == (
+ "exhausted",
+ "trial",
+ None,
+ )
diff --git a/autogpt_platform/backend/backend/copilot/usage_state.py b/autogpt_platform/backend/backend/copilot/usage_state.py
new file mode 100644
index 0000000000..9a27ad100b
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/usage_state.py
@@ -0,0 +1,83 @@
+"""Whether the account can start a turn now, and which limit decides that.
+
+The usage meters say how full each window is. This is the verdict the client
+keys its banners on, worked out once on the server from the same status:
+``exhausted`` when a turn would be refused now, ``low`` from the point the
+meters turn amber, ``ok`` otherwise -- with the limit behind it and when that
+limit refills.
+"""
+
+from datetime import datetime
+from typing import Literal
+
+from pydantic import BaseModel
+
+UsageState = Literal["ok", "low", "exhausted"]
+LimitWindow = Literal["daily", "weekly", "trial"]
+
+# Where the usage meters turn amber.
+LOW_PERCENT_USED = 80.0
+
+
+class WindowReading(BaseModel):
+ """One capped window as the meters show it."""
+
+ window: Literal["daily", "weekly"]
+ percent_used: float
+ exhausted: bool
+ resets_at: datetime
+ # False for a window that allows nothing (no plan, or the trial is over):
+ # its reset brings no budget back.
+ refills: bool = True
+
+
+class UsageVerdict(BaseModel):
+ state: UsageState = "ok"
+ limiting_window: LimitWindow | None = None
+ # None for a trial: its allowance doesn't refill.
+ resets_at: datetime | None = None
+
+
+def usage_verdict(
+ readings: list[WindowReading],
+ *,
+ weekly_is_trial_allowance: bool,
+ trial_spent: bool,
+) -> UsageVerdict:
+ """The state of the account's allowance, from its capped windows.
+
+ When several windows are used up, the one that refills last is the one
+ the account waits for. A weekly window that holds a trial's lifetime
+ allowance is named as the trial, with no reset; a trial with its own
+ daily and weekly caps keeps them, and they refill like anyone's.
+ """
+ if trial_spent:
+ return UsageVerdict(state="exhausted", limiting_window="trial")
+ exhausted = [reading for reading in readings if reading.exhausted]
+ if exhausted:
+ limiting = max(exhausted, key=_refill_order)
+ return _verdict("exhausted", limiting, weekly_is_trial_allowance)
+ if not readings:
+ return UsageVerdict()
+ tightest = max(readings, key=lambda r: (r.percent_used, _refill_order(r)))
+ if tightest.percent_used >= LOW_PERCENT_USED:
+ return _verdict("low", tightest, weekly_is_trial_allowance)
+ return UsageVerdict()
+
+
+def _refill_order(reading: WindowReading) -> tuple[bool, datetime, bool]:
+ # A window that never refills outlasts any reset; the weekly window wins
+ # a tie on time, being the larger of the two.
+ return not reading.refills, reading.resets_at, reading.window == "weekly"
+
+
+def _verdict(
+ state: UsageState, reading: WindowReading, weekly_is_trial_allowance: bool
+) -> UsageVerdict:
+ if weekly_is_trial_allowance and reading.window == "weekly":
+ return UsageVerdict(state=state, limiting_window="trial")
+ return UsageVerdict(
+ state=state,
+ limiting_window=reading.window,
+ resets_at=reading.resets_at if reading.refills else None,
+ )
diff --git a/autogpt_platform/backend/backend/copilot/usage_state_test.py b/autogpt_platform/backend/backend/copilot/usage_state_test.py
new file mode 100644
index 0000000000..521d209b52
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/usage_state_test.py
@@ -0,0 +1,171 @@
+"""The usage API's verdict: ok, low or exhausted, the limit behind it and when
+it refills, computed from the same status the meters show."""
+
+from datetime import UTC, datetime
+
+from backend.copilot.rate_limit import (
+ CoPilotUsagePublic,
+ CoPilotUsageStatus,
+ SubscriptionTier,
+ UsageWindow,
+)
+from backend.copilot.usage_state import UsageVerdict, WindowReading, usage_verdict
+
+DAILY_RESET = datetime(2026, 10, 15, tzinfo=UTC)
+WEEKLY_RESET = datetime(2026, 10, 19, tzinfo=UTC)
+TRIAL_END = datetime(2026, 10, 25, tzinfo=UTC)
+
+
+def _usage(
+ daily_used: int,
+ weekly_used: int,
+ *,
+ daily_limit: int = 1_000_000,
+ weekly_limit: int = 5_000_000,
+ tier: SubscriptionTier = SubscriptionTier.PRO,
+ weekly_reset: datetime = WEEKLY_RESET,
+ trial_spent: bool = False,
+ weekly_is_trial_allowance: bool = False,
+) -> CoPilotUsagePublic:
+ return CoPilotUsagePublic.from_status(
+ CoPilotUsageStatus(
+ daily=UsageWindow(
+ used=daily_used, limit=daily_limit, resets_at=DAILY_RESET
+ ),
+ weekly=UsageWindow(
+ used=weekly_used, limit=weekly_limit, resets_at=weekly_reset
+ ),
+ tier=tier,
+ trial_spent=trial_spent,
+ weekly_is_trial_allowance=weekly_is_trial_allowance,
+ )
+ )
+
+
+def _verdict(usage: CoPilotUsagePublic) -> tuple:
+ return usage.state, usage.limiting_window, usage.resets_at
+
+
+def test_room_in_every_window_is_ok():
+ assert _verdict(_usage(100_000, 1_000_000)) == ("ok", None, None)
+
+
+def test_low_from_where_the_meters_turn_amber():
+ assert _verdict(_usage(800_000, 1_000_000)) == ("low", "daily", DAILY_RESET)
+ assert _verdict(_usage(799_000, 1_000_000)) == ("ok", None, None)
+
+
+def test_low_names_the_fullest_window():
+ # 81% of the day, 90% of the week.
+ usage = _usage(810_000, 4_500_000)
+ assert _verdict(usage) == ("low", "weekly", WEEKLY_RESET)
+
+
+def test_a_used_up_window_is_exhausted_until_it_refills():
+ assert _verdict(_usage(1_000_000, 2_000_000)) == (
+ "exhausted",
+ "daily",
+ DAILY_RESET,
+ )
+
+
+def test_with_both_used_up_the_window_that_refills_last_decides():
+ assert _verdict(_usage(1_000_000, 5_000_000)) == (
+ "exhausted",
+ "weekly",
+ WEEKLY_RESET,
+ )
+
+
+def test_a_tie_on_refill_time_goes_to_the_weekly_window():
+ # On a Sunday both windows refill at the same midnight.
+ readings = [
+ WindowReading(
+ window=name, percent_used=100.0, exhausted=True, resets_at=WEEKLY_RESET
+ )
+ for name in ("daily", "weekly")
+ ]
+ verdict = usage_verdict(
+ readings, weekly_is_trial_allowance=False, trial_spent=False
+ )
+ assert verdict == UsageVerdict(
+ state="exhausted", limiting_window="weekly", resets_at=WEEKLY_RESET
+ )
+
+
+def test_a_zero_limit_is_exhausted_not_unlimited():
+ # No plan, or a trial that is over: the window's reset brings nothing
+ # back, so there is no reset to wait for.
+ usage = _usage(0, 0, daily_limit=0, weekly_limit=0)
+ assert _verdict(usage) == ("exhausted", "weekly", None)
+
+
+def test_a_window_that_allows_nothing_outlasts_one_that_refills():
+ usage = _usage(1_000_000, 0, weekly_limit=0)
+ assert _verdict(usage) == ("exhausted", "weekly", None)
+
+
+def test_uncapped_windows_are_ok():
+ usage = _usage(9_000_000, 9_000_000, daily_limit=-1, weekly_limit=-1)
+ assert (usage.daily, usage.weekly) == (None, None)
+ assert _verdict(usage) == ("ok", None, None)
+
+
+def test_a_meter_rounded_up_to_100_is_low_while_a_turn_can_still_start():
+ # 99.96% displays as 100.0, but the cap isn't reached, so the turn runs.
+ usage = _usage(999_600, 0)
+ assert usage.daily is not None and usage.daily.percent_used == 100.0
+ assert _verdict(usage) == ("low", "daily", DAILY_RESET)
+
+
+def test_a_trials_weekly_window_is_its_allowance_without_a_reset():
+ # A lifetime trial's allowance is folded into the weekly window, which
+ # "resets" when the trial ends.
+ folded = {
+ "tier": SubscriptionTier.TRIAL,
+ "weekly_reset": TRIAL_END,
+ "weekly_is_trial_allowance": True,
+ }
+ assert _verdict(_usage(0, 4_500_000, **folded)) == ("low", "trial", None)
+ spent = _usage(0, 5_000_000, trial_spent=True, **folded)
+ assert _verdict(spent) == ("exhausted", "trial", None)
+
+
+def test_a_trials_own_weekly_cap_refills_like_anyones():
+ # A trial whose period caps are tighter than its allowance keeps a real
+ # weekly window: it refills on Monday, and Send at reset can wait for it.
+ trial = SubscriptionTier.TRIAL
+ assert _verdict(_usage(0, 4_500_000, tier=trial)) == (
+ "low",
+ "weekly",
+ WEEKLY_RESET,
+ )
+ assert _verdict(_usage(0, 5_000_000, tier=trial)) == (
+ "exhausted",
+ "weekly",
+ WEEKLY_RESET,
+ )
+
+
+def test_a_spent_trial_allowance_is_exhausted_with_room_in_the_windows():
+ # A trial whose period caps are tighter than its allowance doesn't fold
+ # the allowance into its windows, so the status flags it.
+ usage = _usage(100_000, 1_000_000, tier=SubscriptionTier.TRIAL, trial_spent=True)
+ assert _verdict(usage) == ("exhausted", "trial", None)
+
+
+def test_a_trials_used_up_day_still_refills_at_midnight():
+ usage = _usage(1_000_000, 1_000_000, tier=SubscriptionTier.TRIAL)
+ assert _verdict(usage) == ("exhausted", "daily", DAILY_RESET)
+
+
+def test_the_verdict_leaves_the_trial_meter_clamp_alone():
+ usage = _usage(
+ 0,
+ 4_999_999,
+ tier=SubscriptionTier.TRIAL,
+ weekly_reset=TRIAL_END,
+ weekly_is_trial_allowance=True,
+ )
+ assert usage.weekly is not None and usage.weekly.percent_used == 99.9
+ assert _verdict(usage) == ("low", "trial", None)
diff --git a/autogpt_platform/backend/backend/data/db_manager.py b/autogpt_platform/backend/backend/data/db_manager.py
index 7ee00ff6c1..f713ed3d90 100644
--- a/autogpt_platform/backend/backend/data/db_manager.py
+++ b/autogpt_platform/backend/backend/data/db_manager.py
@@ -296,6 +296,11 @@ async def _get_org_credits(org_id: str) -> int:
return await _get_org_credits_raw(org_id)
+# Public aliases used by db_accessors.credit_db() when Prisma is connected
+get_org_credits = _get_org_credits
+spend_org_credits = _spend_org_credits
+
+
class DatabaseManager(AppService):
"""Database connection pooling service.
diff --git a/autogpt_platform/backend/backend/executor/scheduler.py b/autogpt_platform/backend/backend/executor/scheduler.py
index f7b399a370..1491b5be44 100644
--- a/autogpt_platform/backend/backend/executor/scheduler.py
+++ b/autogpt_platform/backend/backend/executor/scheduler.py
@@ -1128,7 +1128,10 @@ def _put_back_onto(
newer: "CopilotTurnJobInfo", message: str
) -> tuple[str, datetime | None]:
"""A send put back after the user wrote another while it ran: both go,
- this one first, as the newer job the user set."""
+ this one first, as the newer job the user set. One already put back there
+ (a repeated call whose first try landed) is left as it is."""
+ if newer.message.startswith(f"{message}\n\n"):
+ return newer.message, newer.created_at
return f"{message}\n\n{newer.message}", newer.created_at
@@ -2968,6 +2971,7 @@ class Scheduler(AppService):
if job is None:
return False
job.remove()
+ self._delete_schedule_index_row(job_id)
self._invalidate_jobs_cache()
logger.info(f"Removed {kind}-at-reset job for session {session_id[:12]}")
return True
diff --git a/autogpt_platform/backend/backend/executor/scheduler_at_reset_test.py b/autogpt_platform/backend/backend/executor/scheduler_at_reset_test.py
index 5d59f66aef..e269b7286b 100644
--- a/autogpt_platform/backend/backend/executor/scheduler_at_reset_test.py
+++ b/autogpt_platform/backend/backend/executor/scheduler_at_reset_test.py
@@ -183,6 +183,25 @@ def test_setting_it_again_replaces_the_earlier_job(live_scheduler):
assert jobs[0].next_run_time == first + timedelta(days=1)
+def test_cancel_drops_the_jobs_schedule_index_row(live_scheduler):
+ # Otherwise the schedule list shows it until the next reconcile.
+ live_scheduler._schedule_index = MagicMock()
+ live_scheduler.add_copilot_turn_schedule(
+ user_id=USER,
+ session_id=SESSION,
+ message="Resume.",
+ run_at=datetime.now(UTC) + timedelta(hours=3),
+ user_timezone="UTC",
+ purpose="resume",
+ schedule_id=at_reset_schedule_id("resume", SESSION),
+ )
+
+ assert live_scheduler.cancel_at_reset_turn(USER, SESSION, "resume") is True
+ live_scheduler._schedule_index.delete_many.assert_called_once_with(
+ [at_reset_schedule_id("resume", SESSION)]
+ )
+
+
def test_cancel_removes_only_the_owners_job_and_is_idempotent(live_scheduler):
live_scheduler.add_copilot_turn_schedule(
user_id=USER,
@@ -398,6 +417,19 @@ def test_a_send_put_back_joins_one_written_while_it_ran(live_scheduler):
assert args.message == "draft the launch post\n\nalso the changelog"
+def test_a_put_back_repeated_after_its_first_try_landed_adds_nothing(
+ live_scheduler,
+):
+ # The put-back call is retried when its answer is lost in transit, which
+ # can happen after the scheduler already put it back.
+ _add(live_scheduler, "send", "also the changelog")
+ set_at = datetime.now(UTC) - timedelta(minutes=1)
+ for _ in range(2):
+ _add(live_scheduler, "send", "draft the launch post", created_at=set_at)
+ args = _job_args(live_scheduler, "send")
+ assert args.message == "draft the launch post\n\nalso the changelog"
+
+
def test_a_resume_put_back_leaves_one_set_while_it_ran(live_scheduler):
_add(live_scheduler, "resume", "Resume.")
newer = _job_args(live_scheduler, "resume").created_at
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index 99c5145fab..bd03af3cde 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -20955,6 +20955,29 @@
"title": "Reset Cost",
"description": "Credit cost (in cents) to reset the daily limit. 0 = feature disabled.",
"default": 0
+ },
+ "state": {
+ "type": "string",
+ "enum": ["ok", "low", "exhausted"],
+ "title": "State",
+ "description": "'exhausted' when a turn can't start now (a capped window or the trial allowance is used up), 'low' from 80% of the tightest window (where the meters turn amber), otherwise 'ok'.",
+ "default": "ok"
+ },
+ "limiting_window": {
+ "anyOf": [
+ { "type": "string", "enum": ["daily", "weekly", "trial"] },
+ { "type": "null" }
+ ],
+ "title": "Limiting Window",
+ "description": "The limit behind a 'low' or 'exhausted' state: the used-up window that refills last, 'trial' for the trial allowance, or the fullest window when 'low'. Null when 'ok'."
+ },
+ "resets_at": {
+ "anyOf": [
+ { "type": "string", "format": "date-time" },
+ { "type": "null" }
+ ],
+ "title": "Resets At",
+ "description": "When the limiting window refills. Null when 'ok', for the trial allowance, and for a window that allows nothing (no plan), none of which refill."
}
},
"type": "object",