Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/copilot): keep one running slot for the user's own message (#15347)

Reinier van der Leerd56ac481b0d3
diff --git a/autogpt_platform/backend/backend/copilot/active_turns.py b/autogpt_platform/backend/backend/copilot/active_turns.py
index e5eb26d551..8d6864646d 100644
--- a/autogpt_platform/backend/backend/copilot/active_turns.py
+++ b/autogpt_platform/backend/backend/copilot/active_turns.py
@@ -20,11 +20,9 @@ Public API
used by the queue layer (in-flight = running + queued) and the
dispatcher's busy-session check.
-Cap admission is a *non-locked* count-then-update. Two concurrent
-submits from the same user can both pass the count and both update,
-leaving the user briefly one or two over the cap. This is the same
-trade-off the graph-execution credit rate-limit accepts on its
-``INCRBY`` path: the cap is a safeguard, not a budget.
+Cap admission counts and flips under one per-user database lock
+(``admit_chat_session_turn``), so concurrent submits fill exactly the free
+slots: none over the cap, and none refused while a slot is free.
DB access goes through :func:`backend.data.db_accessors.chat_db` so
the dispatcher works from both the HTTP server (Prisma directly) and
@@ -60,6 +58,12 @@ def get_inflight_turn_limit() -> int:
return Settings().config.max_inflight_copilot_turns_per_user
+def get_delegated_turn_limit() -> int:
+ """Running cap for a turn another session started: one below the user's, so
+ sub-work never takes the last slot (at a cap of 1 there is none to keep)."""
+ return max(get_running_turn_limit() - 1, 1)
+
+
def inflight_turn_limit_message(limit: int | None = None) -> str:
"""User-facing 429 detail when the in-flight cap is hit."""
resolved = get_inflight_turn_limit() if limit is None else limit
@@ -82,6 +86,16 @@ def running_turn_limit_message(limit: int | None = None) -> str:
)
+def delegated_turn_limit_message() -> str:
+ """What a spawn tool tells the model when its turn hit the delegated cap."""
+ return (
+ f"Sub-work may use {get_delegated_turn_limit()} of the user's "
+ f"{get_running_turn_limit()} task slots, and those are all taken (the "
+ "last slot is kept for the user's own messages). Wait for a running "
+ "task to finish before starting another."
+ )
+
+
def queued_turn_message() -> str:
"""User-facing message rendered when a turn is queued instead of
starting immediately because the running cap is full."""
@@ -177,23 +191,15 @@ async def acquire_turn_slot(
resolved_capacity = capacity if capacity is not None else get_running_turn_limit()
db = chat_db()
- # Try fresh admit: promote idle → running in one CAS-gated update.
- if await db.update_chat_session_status(
+ admit = await db.admit_chat_session_turn(
session_id=session_id,
- expect_status=CHAT_STATUS_IDLE,
- status=CHAT_STATUS_RUNNING,
user_id=user_id,
- ):
- # Fresh admit: enforce the cap by counting AFTER the flip.
- # Reading after-write is OK because over-admit just briefly
- # exceeds the cap — the user gets one extra slot at most under
- # burst, same trade-off as the prior count-then-update path.
- if await count_running_turns(user_id) > resolved_capacity:
- # Roll back our flip; the caller falls through to the queue.
- await release_turn_slot(user_id, session_id)
- raise ConcurrentTurnLimitError(
- running_turn_limit_message(resolved_capacity)
- )
+ expect_status=CHAT_STATUS_IDLE,
+ capacity=resolved_capacity,
+ )
+ if admit == "full":
+ raise ConcurrentTurnLimitError(running_turn_limit_message(resolved_capacity))
+ if admit == "admitted":
handle.admitted = True
else:
# CAS failed: session was not idle. Disambiguate by reading
diff --git a/autogpt_platform/backend/backend/copilot/active_turns_db_test.py b/autogpt_platform/backend/backend/copilot/active_turns_db_test.py
new file mode 100644
index 0000000000..5d8fd638f8
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/active_turns_db_test.py
@@ -0,0 +1,199 @@
+"""The running cap against the database: what the spawn tools persist is
+what the cap reads."""
+
+import asyncio
+import uuid
+from unittest.mock import AsyncMock, patch
+
+import pytest
+import pytest_asyncio
+from prisma.models import User
+
+from backend.copilot import active_turns
+from backend.copilot import db as copilot_db
+from backend.copilot.active_turns import (
+ ConcurrentTurnLimitError,
+ TurnSlot,
+ acquire_turn_slot,
+)
+from backend.copilot.model import (
+ CHAT_STATUS_IDLE,
+ CHAT_STATUS_QUEUED,
+ CHAT_STATUS_RUNNING,
+ create_chat_session,
+)
+from backend.copilot.sdk.session_waiter import SessionResult, run_copilot_turn_via_queue
+from backend.data.db import prisma as db_client
+from backend.data.db_accessors import chat_db
+from backend.util.test import SpinTestServer
+
+
[email protected](loop_scope="session")
+async def test_a_chat_fanning_out_six_sub_sessions_leaves_the_user_a_slot():
+ """The 18:12Z shape: one chat's turn starts six sub-sessions, then the user
+ sends a message in another chat."""
+ user_id = str(uuid.uuid4())
+ await User.prisma().create(
+ data={"id": user_id, "email": f"active-turns-{user_id}@example.com"}
+ )
+ try:
+ spawner = await create_chat_session(user_id, dry_run=False)
+ assert await chat_db().update_chat_session_status(
+ session_id=spawner.session_id,
+ expect_status=CHAT_STATUS_IDLE,
+ status=CHAT_STATUS_RUNNING,
+ user_id=user_id,
+ )
+ subs = [
+ await create_chat_session(
+ user_id,
+ dry_run=False,
+ origin="automation",
+ delegated_by_session_id=spawner.session_id,
+ )
+ for _ in range(6)
+ ]
+ chat = await create_chat_session(user_id, dry_run=False)
+
+ with (
+ patch.object(active_turns, "get_running_turn_limit", return_value=5),
+ patch(
+ "backend.copilot.executor.utils.get_inflight_turn_limit",
+ return_value=15,
+ ),
+ patch("backend.copilot.executor.utils.dispatch_turn", new=_keep_slot),
+ patch(
+ "backend.copilot.sdk.session_waiter.wait_for_session_result",
+ new=AsyncMock(return_value=("running", SessionResult())),
+ ),
+ ):
+ outcomes = [
+ (
+ await run_copilot_turn_via_queue(
+ session_id=sub.session_id,
+ user_id=user_id,
+ message="test a stream",
+ timeout=0,
+ tool_call_id=f"sub:{spawner.session_id}",
+ tool_name="run_sub_session",
+ allow_queue=False,
+ )
+ )[0]
+ for sub in subs
+ ]
+ async with acquire_turn_slot(user_id, chat.session_id) as slot:
+ assert slot.admitted
+
+ assert outcomes == ["running"] * 3 + ["rejected_concurrent_turn_cap"] * 3
+ finally:
+ await User.prisma().delete(where={"id": user_id})
+
+
+ "capacity, free",
+ [
+ # Six sub-sessions spawned at once with three running: one fits.
+ (4, 1),
+ # Six messages at once with three running: two fit.
+ (5, 2),
+ ],
+)
[email protected](loop_scope="session")
+async def test_concurrent_admits_fill_exactly_the_free_slots(capacity: int, free: int):
+ user_id = str(uuid.uuid4())
+ await User.prisma().create(
+ data={"id": user_id, "email": f"active-turns-{user_id}@example.com"}
+ )
+ try:
+ for _ in range(3):
+ running = await create_chat_session(user_id, dry_run=False)
+ assert await chat_db().update_chat_session_status(
+ session_id=running.session_id,
+ expect_status=CHAT_STATUS_IDLE,
+ status=CHAT_STATUS_RUNNING,
+ user_id=user_id,
+ )
+ waiting = [await create_chat_session(user_id, dry_run=False) for _ in range(6)]
+
+ admitted = await asyncio.gather(
+ *(_admit(user_id, s.session_id, capacity) for s in waiting)
+ )
+
+ assert sum(admitted) == free
+ assert (
+ await chat_db().count_chat_sessions_by_status(
+ user_id=user_id, status=CHAT_STATUS_RUNNING
+ )
+ ) == 3 + free
+ finally:
+ await User.prisma().delete(where={"id": user_id})
+
+
[email protected](loop_scope="session")
+async def test_a_claim_on_a_row_that_moved_after_it_was_read_is_busy():
+ """A cancel outside the lock can land between the read and the update."""
+ user_id = str(uuid.uuid4())
+ await User.prisma().create(
+ data={"id": user_id, "email": f"active-turns-{user_id}@example.com"}
+ )
+ try:
+ session = await create_chat_session(user_id, dry_run=False)
+ real = copilot_db.PrismaChatSession.prisma
+
+ with patch.object(
+ copilot_db.PrismaChatSession,
+ "prisma",
+ side_effect=lambda client=None: _ReadBeforeTheCancel(real(client)),
+ ):
+ admit = await copilot_db.admit_chat_session_turn(
+ session_id=session.session_id,
+ user_id=user_id,
+ expect_status=CHAT_STATUS_QUEUED,
+ capacity=5,
+ )
+
+ assert admit == "busy"
+ assert (
+ await chat_db().get_chat_session_status(session.session_id)
+ ) == CHAT_STATUS_IDLE
+ finally:
+ await User.prisma().delete(where={"id": user_id})
+
+
+class _ReadBeforeTheCancel:
+ """The session's actions, with its read taken just before a cancel moved it
+ from queued to idle."""
+
+ def __init__(self, actions) -> None:
+ self._actions = actions
+
+ def __getattr__(self, name: str):
+ return getattr(self._actions, name)
+
+ async def find_unique(self, **kwargs):
+ row = await self._actions.find_unique(**kwargs)
+ return row.model_copy(update={"chatStatus": CHAT_STATUS_QUEUED})
+
+
+async def _admit(user_id: str, session_id: str, capacity: int) -> bool:
+ try:
+ async with acquire_turn_slot(user_id, session_id, capacity=capacity) as slot:
+ slot.keep()
+ return slot.admitted
+ except ConcurrentTurnLimitError:
+ return False
+
+
+async def _keep_slot(slot: TurnSlot, **_) -> None:
+ slot.keep()
+
+
+@pytest_asyncio.fixture(autouse=True)
+async def absorb_a_stale_event_loop(server: SpinTestServer):
+ """An earlier test can leave the shared Prisma client bound to a closed
+ loop; only the first query on the new loop fails. Spend it here."""
+ try:
+ await db_client.execute_raw("SELECT 1")
+ except RuntimeError as error:
+ if "Event loop is closed" not in str(error):
+ raise
diff --git a/autogpt_platform/backend/backend/copilot/active_turns_test.py b/autogpt_platform/backend/backend/copilot/active_turns_test.py
index 7f798614a0..c050a48371 100644
--- a/autogpt_platform/backend/backend/copilot/active_turns_test.py
+++ b/autogpt_platform/backend/backend/copilot/active_turns_test.py
@@ -17,26 +17,18 @@ from backend.copilot.active_turns import (
)
-def _mock_db(
- *,
- admit_cas_ok: bool = True,
- running_count: int = 0,
- current_status: str = "idle",
-) -> MagicMock:
+def _mock_db(*, admit: str = "admitted", current_status: str = "idle") -> MagicMock:
"""Mock ``chat_db()`` return value.
- * ``admit_cas_ok`` — return value of ``update_chat_session_status``
- for the idle→running CAS in ``acquire_turn_slot``. True simulates
- a successful fresh admit; False simulates the CAS failing (session
- was not idle), in which case ``current_status`` is consulted.
- * ``running_count`` — return value of
- ``count_chat_sessions_by_status(status='running')`` after the flip.
- * ``current_status`` — return value of ``get_chat_session_status``
- after a CAS failure (the disambiguation read).
+ * ``admit`` — what ``admit_chat_session_turn`` decides under its lock:
+ ``"admitted"``, ``"full"`` (at the cap) or ``"busy"`` (not idle), in
+ which case ``current_status`` is consulted.
+ * ``current_status`` — return value of ``get_chat_session_status``.
"""
db = MagicMock()
- db.update_chat_session_status = AsyncMock(return_value=admit_cas_ok)
- db.count_chat_sessions_by_status = AsyncMock(return_value=running_count)
+ db.admit_chat_session_turn = AsyncMock(return_value=admit)
+ db.update_chat_session_status = AsyncMock(return_value=True)
+ db.count_chat_sessions_by_status = AsyncMock(return_value=0)
db.list_chat_sessions_by_status = AsyncMock(return_value=[])
db.get_chat_session_status = AsyncMock(return_value=current_status)
return db
@@ -75,48 +67,47 @@ async def test_release_anonymous_user_is_noop() -> None:
@pytest.mark.asyncio
async def test_admitted_slot_releases_on_exit_without_keep() -> None:
"""Forgetting ``keep()`` on a clean exit releases the slot."""
- db = _mock_db(admit_cas_ok=True, running_count=1)
+ db = _mock_db()
with patch.object(active_turns, "chat_db", return_value=db):
async with acquire_turn_slot("user-1", "session-a"):
pass
- # First flip: admit (idle → running). Second flip: release (running → idle).
- assert db.update_chat_session_status.await_count == 2
+ # The admit flipped under its lock; the exit releases (running → idle).
+ assert db.update_chat_session_status.await_count == 1
@pytest.mark.asyncio
async def test_admitted_slot_releases_on_exception() -> None:
"""An exception inside the with-block also releases the slot."""
- db = _mock_db(admit_cas_ok=True, running_count=1)
+ db = _mock_db()
with patch.object(active_turns, "chat_db", return_value=db):
with pytest.raises(RuntimeError, match="downstream blew up"):
async with acquire_turn_slot("user-1", "session-a"):
raise RuntimeError("downstream blew up")
- assert db.update_chat_session_status.await_count == 2
+ assert db.update_chat_session_status.await_count == 1
@pytest.mark.asyncio
async def test_kept_slot_is_not_released_on_exit() -> None:
"""``keep()`` transfers ownership; the context manager leaves the
slot held for ``mark_session_completed`` to clean up."""
- db = _mock_db(admit_cas_ok=True, running_count=1)
+ db = _mock_db()
with patch.object(active_turns, "chat_db", return_value=db):
async with acquire_turn_slot("user-1", "session-a") as slot:
slot.keep()
- # Only the admit fires; release is the caller's responsibility now.
- assert db.update_chat_session_status.await_count == 1
+ # Nothing released; that is the caller's responsibility now.
+ db.update_chat_session_status.assert_not_awaited()
@pytest.mark.asyncio
-async def test_rejection_rolls_back_admit_when_over_cap() -> None:
- """Admit count-after-flip exceeds the cap → roll back to idle and
- raise ConcurrentTurnLimitError."""
- db = _mock_db(admit_cas_ok=True, running_count=6) # 6 > capacity=5
+async def test_a_full_cap_raises_without_flipping() -> None:
+ """The cap is checked before the flip, under the same lock."""
+ db = _mock_db(admit="full")
with patch.object(active_turns, "chat_db", return_value=db):
with pytest.raises(ConcurrentTurnLimitError):
async with acquire_turn_slot("user-1", "session-a", capacity=5):
pytest.fail("body must not run on rejection") # pragma: no cover
- # Two flips: admit then rollback.
- assert db.update_chat_session_status.await_count == 2
+ assert db.admit_chat_session_turn.await_args.kwargs["capacity"] == 5
+ db.update_chat_session_status.assert_not_awaited()
@pytest.mark.asyncio
@@ -125,7 +116,7 @@ async def test_queued_session_raises_so_caller_falls_through_to_queue() -> None:
has a pending task for this session. Raise ConcurrentTurnLimitError
so the route falls through to ``try_enqueue_turn`` instead of
double-dispatching."""
- db = _mock_db(admit_cas_ok=False, current_status="queued")
+ db = _mock_db(admit="busy", current_status="queued")
with patch.object(active_turns, "chat_db", return_value=db):
with pytest.raises(ConcurrentTurnLimitError):
async with acquire_turn_slot("user-1", "session-a"):
@@ -136,23 +127,22 @@ async def test_queued_session_raises_so_caller_falls_through_to_queue() -> None:
async def test_refreshed_slot_is_not_released_on_clean_exit() -> None:
"""CAS failure + current status == 'running' is the SSE-retry
refresh path: no admit, no release ownership, no error."""
- db = _mock_db(admit_cas_ok=False, current_status="running")
+ db = _mock_db(admit="busy", current_status="running")
with patch.object(active_turns, "chat_db", return_value=db):
async with acquire_turn_slot("user-1", "session-a"):
pass
- # Only the CAS attempt fires; nothing else.
- assert db.update_chat_session_status.await_count == 1
+ db.update_chat_session_status.assert_not_awaited()
@pytest.mark.asyncio
async def test_refreshed_slot_is_not_released_on_exception() -> None:
"""Same-session retry's failure must NOT tear down the original turn."""
- db = _mock_db(admit_cas_ok=False, current_status="running")
+ db = _mock_db(admit="busy", current_status="running")
with patch.object(active_turns, "chat_db", return_value=db):
with pytest.raises(RuntimeError, match="boom"):
async with acquire_turn_slot("user-1", "session-a"):
raise RuntimeError("boom")
- assert db.update_chat_session_status.await_count == 1
+ db.update_chat_session_status.assert_not_awaited()
@pytest.mark.asyncio
@@ -162,8 +152,7 @@ async def test_anonymous_user_skips_gate() -> None:
with patch.object(active_turns, "chat_db", return_value=db):
async with acquire_turn_slot(None, "session-a"):
pass
- db.update_chat_session_status.assert_not_awaited()
- db.count_chat_sessions_by_status.assert_not_awaited()
+ db.admit_chat_session_turn.assert_not_awaited()
# ── default cap pinning ───────────────────────────────────────────────
diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.py
index 4ed9fc64bb..55df12a837 100644
--- a/autogpt_platform/backend/backend/copilot/db.py
+++ b/autogpt_platform/backend/backend/copilot/db.py
@@ -4,7 +4,7 @@ import asyncio
import logging
import uuid
from datetime import UTC, datetime
-from typing import Any
+from typing import Any, Literal
import sentry_sdk
from prisma.errors import UniqueViolationError
@@ -27,6 +27,7 @@ from backend.util.exceptions import ExpertNotFoundError
from backend.util.json import SafeJson, dumps, sanitize_string
from .model import (
+ CHAT_STATUS_RUNNING,
ChatMessage,
ChatSessionInfo,
ChatSessionMetadata,
@@ -1448,6 +1449,44 @@ async def get_chat_session_status(session_id: str) -> str | None:
return row.chatStatus if row else None
+async def admit_chat_session_turn(
+ *, session_id: str, user_id: str, expect_status: str, capacity: int
+) -> Literal["admitted", "full", "busy"]:
+ """Flip the session ``expect_status`` → running while the user has fewer
+ than ``capacity`` running: ``"full"`` at the cap, ``"busy"`` when the
+ session is not in ``expect_status``.
+
+ The count precedes the flip inside one per-user lock, and relies on READ
+ COMMITTED (Postgres's default) to see every flip committed before the lock.
+ """
+ async with db.transaction() as tx:
+ # execute_raw, not query_raw: pg_advisory_xact_lock returns void.
+ await tx.execute_raw(
+ "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))",
+ f"copilot-turns:{user_id}",
+ )
+ sessions = PrismaChatSession.prisma(tx)
+ current = await sessions.find_unique(where={"id": session_id})
+ if (
+ current is None
+ or current.userId != user_id
+ or current.chatStatus != expect_status
+ ):
+ return "busy"
+ running = await sessions.count(
+ where={"userId": user_id, "chatStatus": CHAT_STATUS_RUNNING}
+ )
+ if running >= capacity:
+ return "full"
+ # Conditional: a transition outside this lock (a cancel) may have
+ # moved the row since it was read.
+ updated = await sessions.update_many(
+ where={"id": session_id, "userId": user_id, "chatStatus": expect_status},
+ data={"chatStatus": CHAT_STATUS_RUNNING},
+ )
+ return "admitted" if updated else "busy"
+
+
async def update_chat_session_status(
*,
session_id: str,
diff --git a/autogpt_platform/backend/backend/copilot/executor/utils.py b/autogpt_platform/backend/backend/copilot/executor/utils.py
index 8b0d0b4bf5..21c1b7989b 100644
--- a/autogpt_platform/backend/backend/copilot/executor/utils.py
+++ b/autogpt_platform/backend/backend/copilot/executor/utils.py
@@ -16,6 +16,7 @@ from backend.copilot.active_turns import (
ConcurrentTurnLimitError,
TurnSlot,
acquire_turn_slot,
+ get_delegated_turn_limit,
get_inflight_turn_limit,
inflight_turn_limit_message,
)
@@ -462,6 +463,7 @@ async def schedule_turn(
unattended: bool = False,
credential_pins: CredentialPins | None = None,
scheduled: ScheduledTurnOrigin | None = None,
+ delegated: bool = False,
) -> None:
"""End-to-end "start a copilot turn": reserve a per-user concurrency
slot, register the session in the stream registry, then publish the
@@ -489,10 +491,12 @@ async def schedule_turn(
``mark_session_completed``, which releases it when the turn ends.
Capacity is the *inflight* cap (default 15) rather than the running
- cap (default 5): non-HTTP callers (``run_sub_session``,
- ``AutoPilotBlock``) have no FIFO queue fallback, so applying the
- soft running cap here would regress concurrency below the prior
- SECRT-2335 hotfix behaviour.
+ cap (default 5): non-HTTP callers (``AutoPilotBlock``, scheduled
+ turns) have no FIFO queue fallback, so applying the soft running cap
+ here would regress concurrency below the prior SECRT-2335 hotfix
+ behaviour. A ``delegated`` turn (one another session started) is
+ admitted only below the running cap minus one instead, so it never
+ takes the slot the user's own next message needs.
The user's queued turns from the chat HTTP route DO count against
the inflight cap — pre-check ``running + queued`` here so a user
@@ -506,9 +510,8 @@ async def schedule_turn(
if await count_inflight_turns(user_id) >= inflight_cap:
raise ConcurrentTurnLimitError(inflight_turn_limit_message(inflight_cap))
- async with acquire_turn_slot(
- user_id, session_id, capacity=get_inflight_turn_limit()
- ) as slot:
+ capacity = get_delegated_turn_limit() if delegated else get_inflight_turn_limit()
+ async with acquire_turn_slot(user_id, session_id, capacity=capacity) as slot:
await dispatch_turn(
slot,
session_id=session_id,
diff --git a/autogpt_platform/backend/backend/copilot/gate/held.py b/autogpt_platform/backend/backend/copilot/gate/held.py
index 53a9dd5864..f63f6ab661 100644
--- a/autogpt_platform/backend/backend/copilot/gate/held.py
+++ b/autogpt_platform/backend/backend/copilot/gate/held.py
@@ -208,6 +208,7 @@ async def wake(
from backend.copilot.active_turns import (
ConcurrentTurnLimitError,
acquire_turn_slot,
+ get_delegated_turn_limit,
get_inflight_turn_limit,
)
from backend.copilot.executor.utils import dispatch_turn
@@ -220,6 +221,7 @@ async def wake(
is_users_own_chat,
post_refusal,
try_enqueue_turn,
+ wakes_sub_work,
)
try:
@@ -247,8 +249,11 @@ async def wake(
return
permissions = resolve_session_permissions(info)
metadata = {_WAKE_KEY: True}
+ capacity = get_delegated_turn_limit() if wakes_sub_work(info) else None
try:
- async with acquire_turn_slot(user_id, session_id) as slot:
+ async with acquire_turn_slot(
+ user_id, session_id, capacity=capacity
+ ) as slot:
# Not admitted: a turn is already running, and its end wakes us.
if not slot.admitted:
return
diff --git a/autogpt_platform/backend/backend/copilot/gate/held_db_test.py b/autogpt_platform/backend/backend/copilot/gate/held_db_test.py
index 1e16d32c84..7e2e6a1f04 100644
--- a/autogpt_platform/backend/backend/copilot/gate/held_db_test.py
+++ b/autogpt_platform/backend/backend/copilot/gate/held_db_test.py
@@ -7,13 +7,14 @@ exactly what the approval ran it with.
import asyncio
import json
+import uuid
from datetime import UTC, datetime, timedelta
from typing import Any
from unittest.mock import AsyncMock, patch
import pytest
from prisma.enums import ReviewStatus
-from prisma.models import PendingHumanReview
+from prisma.models import PendingHumanReview, User
from backend.copilot import stream_registry
from backend.copilot.context import set_execution_context
@@ -21,10 +22,14 @@ from backend.copilot.gate import chat_rules, check_action, held
from backend.copilot.gate import review as review_store
from backend.copilot.gate.classifier import Judgement
from backend.copilot.model import (
+ CHAT_STATUS_IDLE,
+ CHAT_STATUS_QUEUED,
+ CHAT_STATUS_RUNNING,
AutopilotMode,
ChatMessage,
ChatSession,
append_and_save_message,
+ create_chat_session,
get_chat_session,
update_session_autopilot_mode,
upsert_chat_session,
@@ -35,7 +40,7 @@ from backend.copilot.tools.base import BaseTool
from backend.copilot.tools.models import ResponseType, ToolResponseBase
from backend.copilot.tree import TurnEnvelope, get_tree_ledger, root_envelope
from backend.copilot.turn_queue import UNRECORDED_WAKE, WAKE_LATER
-from backend.data.db_accessors import review_db
+from backend.data.db_accessors import chat_db, review_db
from backend.data.redis_client import get_redis_async
_TOOL = "post_to_chat_platform"
@@ -221,6 +226,44 @@ async def test_a_wake_starts_under_the_envelope_its_call_was_held_under(
assert dispatch.await_args.kwargs["envelope"] == _turn_envelope(session)
+ "delegated_by, starts", [("parent-session", False), (None, True)]
+)
[email protected](loop_scope="session")
+async def test_a_wake_in_a_sub_session_leaves_the_users_last_slot(
+ server, gate_on, delegated_by: str | None, starts: bool
+):
+ """Four already run: a wake in a session another one opened is its sub-work,
+ so it waits in the queue as it would had it been queued; in the user's own
+ chat it takes the fifth slot."""
+ user_id = str(uuid.uuid4())
+ await User.prisma().create(
+ data={"id": user_id, "email": f"held-{user_id}@example.com"}
+ )
+ try:
+ for _ in range(4):
+ running = await create_chat_session(user_id, dry_run=False)
+ assert await chat_db().update_chat_session_status(
+ session_id=running.session_id,
+ expect_status=CHAT_STATUS_IDLE,
+ status=CHAT_STATUS_RUNNING,
+ user_id=user_id,
+ )
+ session = await _new_session(user_id, delegated_by=delegated_by)
+ review_id = await _hold(session, user_id, "one slot short")
+ await _answer(review_id, ReviewStatus.APPROVED)
+ dispatch = AsyncMock()
+
+ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):
+ await held.wake(user_id, session.session_id)
+
+ assert dispatch.await_count == (1 if starts else 0)
+ status = await chat_db().get_chat_session_status(session.session_id)
+ assert status == (CHAT_STATUS_IDLE if starts else CHAT_STATUS_QUEUED)
+ finally:
+ await User.prisma().delete(where={"id": user_id})
+
+
@pytest.mark.asyncio(loop_scope="session")
async def test_a_call_rebuilt_from_its_card_wakes_as_a_root_in_the_users_own_chat(
setup_test_user, test_user_id, gate_on, post_tool
diff --git a/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py b/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py
index 139e96e690..3aae035925 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py
@@ -255,6 +255,7 @@ async def run_copilot_turn_via_queue(
permissions=permissions,
spawn=spawn,
message_metadata=message_metadata,
+ delegated=session.metadata.delegated_by_session_id is not None,
)
except TreeRefusal as refused:
return "refused", SessionResult(refusal=refused.message)
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 99db61cee0..6ca24f8841 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py
@@ -30,6 +30,7 @@ def mock_session_lookup():
session = MagicMock()
session.metadata.llm_auth_provider = "platform"
session.metadata.llm_credential_id = None
+ session.metadata.delegated_by_session_id = None
with patch(
"backend.copilot.sdk.session_waiter.get_chat_session",
new=AsyncMock(return_value=session),
@@ -188,8 +189,8 @@ async def test_idle_session_enqueues_normally():
idle_db = MagicMock()
# Session is idle → CAS idle → running succeeds; running count is 1
# after the flip (this caller is the only running session).
+ idle_db.admit_chat_session_turn = AsyncMock(return_value="admitted")
idle_db.update_chat_session_status = AsyncMock(return_value=True)
- idle_db.count_chat_sessions_by_status = AsyncMock(return_value=1)
idle_db.list_chat_sessions_by_status = AsyncMock(return_value=[])
idle_db.get_chat_session_status = AsyncMock(return_value="idle")
@@ -231,6 +232,62 @@ async def test_idle_session_enqueues_normally():
enqueue.assert_awaited_once()
+ "delegated_by, inflight, expected",
+ [
+ # A sub-session's turn with four running may not take the fifth slot.
+ ("parent-session", 4, "rejected_concurrent_turn_cap"),
+ # An AutoPilotBlock turn keeps the inflight cap...
+ (None, 4, "completed"),
+ # ...which still refuses at its number.
+ (None, 15, "rejected_concurrent_turn_cap"),
+ ],
+)
+async def test_a_delegated_turn_leaves_the_users_last_slot_free(
+ mock_session_lookup, delegated_by: str | None, inflight: int, expected: str
+):
+ mock_session_lookup.return_value.metadata.delegated_by_session_id = delegated_by
+ db = MagicMock()
+ # Four already running: the admit decides by the capacity it is given.
+ db.admit_chat_session_turn = AsyncMock(
+ side_effect=lambda **kw: "full" if 4 >= kw["capacity"] else "admitted"
+ )
+ db.get_chat_session_status = AsyncMock(return_value="idle")
+
+ with (
+ patch(
+ "backend.copilot.sdk.session_waiter.is_turn_in_flight",
+ new=AsyncMock(return_value=False),
+ ),
+ patch(
+ "backend.copilot.turn_queue.count_inflight_turns",
+ new=AsyncMock(return_value=inflight),
+ ),
+ patch.object(active_turns, "chat_db", return_value=db),
+ patch.object(active_turns, "get_running_turn_limit", return_value=5),
+ patch(
+ "backend.copilot.executor.utils.get_inflight_turn_limit", return_value=15
+ ),
+ patch("backend.copilot.sdk.session_waiter.stream_registry.create_session"),
+ patch("backend.copilot.executor.utils.enqueue_copilot_turn"),
+ patch(
+ "backend.copilot.sdk.session_waiter.wait_for_session_result",
+ new=AsyncMock(return_value=("completed", SessionResult())),
+ ),
+ ):
+ outcome, _ = await run_copilot_turn_via_queue(
+ session_id="sess-sub",
+ user_id="u1",
+ message="kick off",
+ timeout=0.1,
+ tool_call_id="sub:parent-session",
+ tool_name="run_sub_session",
+ )
+
+ assert outcome == expected
+
+
@pytest.mark.asyncio
async def test_idle_session_concurrent_turn_cap_returns_rejected_outcome():
"""Slot-cap rejection in ``schedule_turn`` surfaces as the dedicated
diff --git a/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert.py b/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert.py
index ecd951721b..a1c1bf48df 100644
--- a/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert.py
+++ b/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert.py
@@ -33,6 +33,7 @@ import time
from typing import Any
from backend.api.features.experts.models import Expert
+from backend.copilot.active_turns import delegated_turn_limit_message
from backend.copilot.budget_signal import build_spawn_state_note
from backend.copilot.context import get_current_permissions
from backend.copilot.model import (
@@ -218,7 +219,11 @@ class DelegateToExpertTool(BaseTool):
# The thread is gone; handing back its id would send the model to
# poll a delegation that no longer exists.
return self._error(
- result.refusal or f"{target.name} could not start this task.",
+ (
+ delegated_turn_limit_message()
+ if outcome == "rejected_concurrent_turn_cap"
+ else result.refusal or f"{target.name} could not start this task."
+ ),
session,
)
workspace_files = (
diff --git a/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert_test.py b/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert_test.py
index df4adaf1f2..953a6d2c24 100644
--- a/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/delegate_to_expert_test.py
@@ -13,6 +13,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
+from backend.copilot.active_turns import delegated_turn_limit_message
from backend.copilot.sdk.session_waiter import SessionResult
from .delegate_to_expert import DelegateToExpertTool
@@ -924,6 +925,26 @@ class TestRefusedDelegationCleanup:
assert isinstance(result, ErrorResponse)
assert "inner-1" not in result.model_dump_json()
+ async def test_a_delegation_refused_at_the_reserve_says_to_wait(
+ self, roster, mock_turn, mock_sessions, monkeypatch
+ ):
+ """The thread is discarded, but the model still learns the last slot is
+ the user's and to wait for a running task, as the other spawn tools say."""
+ monkeypatch.setattr(
+ "backend.copilot.tools.run_sub_session.delete_chat_session", AsyncMock()
+ )
+ mock_turn.return_value = ("rejected_concurrent_turn_cap", SessionResult())
+
+ result = await DelegateToExpertTool()._execute(
+ user_id="alice",
+ session=_session(expert_id="expert-a"),
+ expert_id="expert-b",
+ prompt="draft the ops update",
+ )
+
+ assert isinstance(result, ErrorResponse)
+ assert result.message == delegated_turn_limit_message()
+
async def test_a_refused_resume_keeps_the_prior_thread(
self, roster, mock_turn, mock_sessions, monkeypatch
):
diff --git a/autogpt_platform/backend/backend/copilot/tools/handoff_to_expert.py b/autogpt_platform/backend/backend/copilot/tools/handoff_to_expert.py
index 7ac5959c97..ddb17ca6c6 100644
--- a/autogpt_platform/backend/backend/copilot/tools/handoff_to_expert.py
+++ b/autogpt_platform/backend/backend/copilot/tools/handoff_to_expert.py
@@ -25,7 +25,7 @@ import logging
from typing import Any
from backend.api.features.experts.models import Expert
-from backend.copilot.active_turns import running_turn_limit_message
+from backend.copilot.active_turns import delegated_turn_limit_message
from backend.copilot.budget_signal import build_spawn_state_note
from backend.copilot.context import get_current_permissions
from backend.copilot.model import (
@@ -349,7 +349,7 @@ def _refused_transfer_message(
if outcome == "rejected_concurrent_turn_cap":
return (
f"The handoff to {target_name} did not happen — the task is still "
- f"yours. {running_turn_limit_message()}"
+ f"yours. {delegated_turn_limit_message()}"
)
if outcome == "refused" and refusal:
return (
diff --git a/autogpt_platform/backend/backend/copilot/tools/handoff_to_expert_test.py b/autogpt_platform/backend/backend/copilot/tools/handoff_to_expert_test.py
index 680403dfa5..b33aa0b806 100644
--- a/autogpt_platform/backend/backend/copilot/tools/handoff_to_expert_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/handoff_to_expert_test.py
@@ -626,7 +626,7 @@ class TestFailedTransfer:
self, roster, mock_turn, mock_sessions
):
r = await self._handoff(mock_turn, "rejected_concurrent_turn_cap")
- assert "already running" in r.message
+ assert "kept for the user's own messages" in r.message
@pytest.mark.parametrize("outcome", ["rejected_concurrent_turn_cap", "failed"])
@pytest.mark.asyncio
diff --git a/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py b/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
index f6d41da80d..495d68fb4c 100644
--- a/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
+++ b/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
@@ -28,7 +28,7 @@ import logging
import time
from typing import Any
-from backend.copilot.active_turns import running_turn_limit_message
+from backend.copilot.active_turns import delegated_turn_limit_message
from backend.copilot.budget_signal import build_spawn_state_note
from backend.copilot.constants import MAX_TOOL_WAIT_SECONDS
from backend.copilot.context import get_current_permissions, get_workspace_manager
@@ -552,7 +552,7 @@ def response_from_outcome(
# Render the actionable message instead of a "see transcript"
# pointer to nothing.
return SubSessionStatusResponse(
- message=running_turn_limit_message(),
+ message=delegated_turn_limit_message(),
session_id=parent_session_id,
status="error",
sub_session_id=inner_session_id,
diff --git a/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py b/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py
index a12e41f13c..ba7fd7b8b5 100644
--- a/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py
@@ -15,6 +15,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
+from backend.copilot import active_turns
from backend.copilot.permissions import CopilotPermissions
from backend.copilot.sdk.session_waiter import SessionResult
from backend.copilot.sdk.stream_accumulator import ToolCallEntry
@@ -1107,6 +1108,19 @@ class TestHollowResponseRepro:
# ---------------------------------------------------------------------------
+def test_a_sub_refused_at_the_cap_is_told_the_last_slot_is_the_users(monkeypatch):
+ monkeypatch.setattr(active_turns, "get_running_turn_limit", lambda: 5)
+ r = response_from_outcome(
+ outcome="rejected_concurrent_turn_cap",
+ result=SessionResult(),
+ inner_session_id="inner-1",
+ parent_session_id="parent-1",
+ elapsed=1.0,
+ )
+ assert r.message is not None
+ assert "Sub-work may use 4 of the user's 5 task slots" in r.message
+
+
class TestActorParameter:
def test_default_actor_is_sub_autopilot(self):
r = response_from_outcome(
diff --git a/autogpt_platform/backend/backend/copilot/turn_queue.py b/autogpt_platform/backend/backend/copilot/turn_queue.py
index 47314792ca..d6ea807d8c 100644
--- a/autogpt_platform/backend/backend/copilot/turn_queue.py
+++ b/autogpt_platform/backend/backend/copilot/turn_queue.py
@@ -30,12 +30,17 @@ cancels manually.
import logging
import uuid
-from typing import Any, Mapping
+from typing import Any, Literal, Mapping
from prisma.errors import UniqueViolationError
from pydantic import BaseModel, ValidationError
-from backend.copilot.active_turns import TurnSlot, count_running_turns
+from backend.copilot.active_turns import (
+ TurnSlot,
+ count_running_turns,
+ get_delegated_turn_limit,
+ get_running_turn_limit,
+)
from backend.copilot.config import ChatConfig, CopilotLlmAuthProvider
from backend.copilot.db import is_duplicate_chat_message_id_error
from backend.copilot.model import (
@@ -258,15 +263,17 @@ async def cancel_queued_turn(*, user_id: str, session_id: str) -> bool:
return True
-async def claim_queued_session(session_id: str) -> bool:
- """Atomically claim a queued session by transitioning ``chatStatus``
- ``"queued"`` → ``"running"``. Returns True iff the CAS matched
- (i.e. the session was still queued; not cancelled / claimed by a
- concurrent dispatcher)."""
- return await chat_db().update_chat_session_status(
- session_id=session_id,
+async def claim_queued_session(
+ session: ChatSessionInfo, *, sub_work: bool
+) -> Literal["admitted", "full", "busy"]:
+ """Claim a queued session, ``"queued"`` → ``"running"``, if the user has a
+ slot for it: sub-work below the reserve, their own message below the cap.
+ ``"busy"`` when it was cancelled or claimed elsewhere since it was read."""
+ return await chat_db().admit_chat_session_turn(
+ session_id=session.session_id,
+ user_id=session.user_id,
expect_status=CHAT_STATUS_QUEUED,
- status=CHAT_STATUS_RUNNING,
+ capacity=get_delegated_turn_limit() if sub_work else get_running_turn_limit(),
)
@@ -301,26 +308,34 @@ async def _promote_head(user_id: str) -> bool | None:
# Local for the same reason: the gate imports this module back.
from backend.copilot.gate.held import is_answer_row
+ # The user's own turns go before sub-work, oldest first in each, so sub-work
+ # that does not fit means nothing behind it does; one that may not start yet
+ # does not hold up the rest.
queued = await list_queued_sessions(user_id)
- if not queued:
- return False
- head = queued[0]
-
- route_provider = head.metadata.llm_auth_provider
- refusal = await turn_refusal(user_id, route_provider)
- if refusal is not None:
- logger.log(
- logging.WARNING if refusal.transient else logging.INFO,
- f"dispatch_next_for_user: user={user_id} {refusal.reason}, "
- f"leaving session={head.session_id} queued",
+ sub_work = {s.session_id: await _is_sub_work(s) for s in queued}
+ candidates = sorted(queued, key=lambda s: sub_work[s.session_id])
+ gates = _UserGates(user_id)
+ head = None
+ try:
+ for candidate in candidates:
+ if await _may_start(gates, candidate):
+ head = candidate
+ break
+ except _QueueOnHold as hold:
+ logger.warning(
+ f"dispatch_next_for_user: user={user_id} {hold}; "
+ "leaving queue intact for the next tick"
)
return False
+ if head is None:
+ return False
- # Claim by transitioning the session ``queued`` → ``running``. A
- # parallel cancel between validation and claim rejects this
- # dispatch via the CAS returning False.
- if not await claim_queued_session(head.session_id):
+ claim = await claim_queued_session(head, sub_work=sub_work[head.session_id])
+ if claim == "full":
return False
+ if claim == "busy":
+ # Cancelled or claimed elsewhere since it was listed: try the next.
+ return None
try:
# Find the pending user message in this session (the most recent
@@ -354,34 +369,6 @@ async def _promote_head(user_id: str) -> bool | None:
await _refuse_queued_turn(head, UNRECORDED_WAKE)
return None
- # A turn can sit in the queue long enough for the plan that bought it to
- # lapse. The tier was checked when the turn was accepted, but promoting it
- # is a second, later decision to spend, so it gets its own check --
- # otherwise a downgrade between the two buys a free Advanced run. The turn
- # goes back to queued rather than quietly re-running on Standard: nothing
- # in this feature changes what a turn runs on without being asked. It
- # promotes itself once entitlement returns, and can be cancelled meanwhile.
- if route_provider == "platform" and metadata.get("model") == "advanced":
- try:
- entitled = await advanced_tier_entitled(user_id)
- except EntitlementUnavailable:
- entitled = False
- logger.warning(
- "dispatch_next_for_user: could not resolve the Advanced "
- "entitlement for user=%s; leaving session=%s queued",
- user_id,
- head.session_id,
- exc_info=True,
- )
- if not entitled:
- await chat_db().update_chat_session_status(
- session_id=head.session_id,
- expect_status=CHAT_STATUS_RUNNING,
- status=CHAT_STATUS_QUEUED,
- )
- await invalidate_session_cache(head.session_id)
- return False
-
turn_id = str(uuid.uuid4())
# The user's message is already persisted AND the session is
# already ``chatStatus='running'`` from claim_queued_session.
@@ -464,6 +451,84 @@ async def _promote_head(user_id: str) -> bool | None:
return True
+async def _is_sub_work(session: ChatSessionInfo) -> bool:
+ """A message the user typed is theirs whatever session it is in; what an
+ approval wakes there is sub-work if :func:`wakes_sub_work` says so."""
+ if not wakes_sub_work(session):
+ return False
+ # Local: the gate imports this module back.
+ from backend.copilot.gate.held import is_answer_row
+
+ waiting = await chat_db().get_latest_user_message_in_session(session.session_id)
+ return waiting is not None and is_answer_row(waiting)
+
+
+async def _may_start(gates: "_UserGates", head: ChatSessionInfo) -> bool:
+ """Whether ``head`` may start now: :func:`turn_refusal` for its route and,
+ on the platform route, an Advanced turn's tier."""
+ route_provider = head.metadata.llm_auth_provider
+ if not await gates.route_open(route_provider):
+ return False
+ if route_provider != "platform":
+ return True
+ # A turn can sit in the queue long enough for the plan that bought it to
+ # lapse. The tier was checked when the turn was accepted, but promoting it
+ # is a second, later decision to spend, so it gets its own check --
+ # otherwise a downgrade between the two buys a free Advanced run. The turn
+ # stays queued rather than quietly re-running on Standard: nothing in this
+ # feature changes what a turn runs on without being asked. It promotes
+ # itself once entitlement returns, and can be cancelled meanwhile.
+ pending = await chat_db().get_latest_user_message_in_session(head.session_id)
+ model = (pending.metadata or {}).get("model") if pending else None
+ if model == "advanced" and not await gates.advanced_tier():
+ logger.info(
+ f"dispatch_next_for_user: user={gates.user_id} lacks the Advanced tier, "
+ f"leaving session={head.session_id} queued"
+ )
+ return False
+ return True
+
+
+class _UserGates:
+ """The per-user checks, made once per pass over the queue however many
+ queued sessions are tried."""
+
+ def __init__(self, user_id: str) -> None:
+ self.user_id = user_id
+ self._refusals: dict[CopilotLlmAuthProvider, TurnRefusal | None] = {}
+ self._advanced: bool | None = None
+
+ async def route_open(self, route: CopilotLlmAuthProvider) -> bool:
+ """Raises :class:`_QueueOnHold` when the gate could not read the user's
+ state, which leaves the whole queue for the next tick."""
+ if route not in self._refusals:
+ self._refusals[route] = await turn_refusal(self.user_id, route)
+ refusal = self._refusals[route]
+ if refusal is None:
+ return True
+ if refusal.transient:
+ raise _QueueOnHold(refusal.reason)
+ logger.info(f"dispatch_next_for_user: user={self.user_id} {refusal.reason}")
+ return False
+
+ async def advanced_tier(self) -> bool:
+ if self._advanced is None:
+ try:
+ self._advanced = await advanced_tier_entitled(self.user_id)
+ except EntitlementUnavailable:
+ logger.warning(
+ "dispatch_next_for_user: could not resolve the Advanced "
+ f"entitlement for user={self.user_id}",
+ exc_info=True,
+ )
+ self._advanced = False
+ return self._advanced
+
+
+class _QueueOnHold(Exception):
+ """Nothing is promoted this tick: the user's state could not be read."""
+
+
def is_users_own_chat(session: ChatSessionInfo) -> bool:
"""A chat the user opened themselves, not one another session or a graph
started. Its turns are roots, so a wake there whose envelope was lost can
@@ -474,6 +539,12 @@ def is_users_own_chat(session: ChatSessionInfo) -> bool:
)
+def wakes_sub_work(session: ChatSessionInfo) -> bool:
+ """An approval wake here is the sub-work of the session that opened this
+ one, so it is admitted within the user's reserve, direct or queued."""
+ return session.metadata.delegated_by_session_id is not None
+
+
async def _refuse_queued_turn(head: ChatSessionInfo, reason: str) -> None:
"""Close a promoted turn that may not start: say why in its thread and
free its slot, which a failed post must not keep."""
diff --git a/autogpt_platform/backend/backend/copilot/turn_queue_db_test.py b/autogpt_platform/backend/backend/copilot/turn_queue_db_test.py
index 9b217da7d3..b4e1f7f3d5 100644
--- a/autogpt_platform/backend/backend/copilot/turn_queue_db_test.py
+++ b/autogpt_platform/backend/backend/copilot/turn_queue_db_test.py
@@ -89,6 +89,8 @@ async def test_a_queued_wake_with_no_recorded_envelope_in_a_sub_session_is_not_s
message_metadata={held._WAKE_KEY: True},
expect_refusal=turn_queue.UNRECORDED_WAKE,
sub_work=True,
+ # Three left running: below the reserve, so the wake is claimed.
+ running=4,
)
assert promoted is None
@@ -103,11 +105,42 @@ async def test_a_queued_wake_whose_envelope_no_longer_parses_is_not_started():
message_metadata={held._WAKE_KEY: True, "envelope": {"depth": "deep"}},
expect_refusal=turn_queue.UNRECORDED_WAKE,
sub_work=True,
+ # Three left running: below the reserve, so the wake is claimed.
+ running=4,
)
assert promoted is None
[email protected](loop_scope="session")
+async def test_queued_sub_work_is_not_promoted_into_the_users_last_slot():
+ """Four still run after the turn ends: an approval wake in a session
+ another one opened is that session's sub-work, so the fifth slot stays."""
+ held_under = _envelope(1)
+ await (await get_tree_ledger()).open(
+ held_under.tree_id, ceiling_microdollars=1_000_000, max_nodes=10
+ )
+ promoted, _, _ = await _promote_after(
+ _envelope(1),
+ message=held.WAKE_MESSAGE,
+ message_metadata={held._WAKE_KEY: True},
+ envelope=held_under,
+ sub_work=True,
+ )
+
+ assert promoted is None
+
+
[email protected](loop_scope="session")
+async def test_a_message_typed_into_a_delegated_session_takes_the_users_slot():
+ """It is the user's own turn wherever they typed it."""
+ promoted, _, _ = await _promote_after(
+ _envelope(1), message="typed by the user", message_metadata=None, sub_work=True
+ )
+
+ assert promoted is not None
+
+
async def _promote_after(
finished: TurnEnvelope,
*,
@@ -116,6 +149,7 @@ async def _promote_after(
envelope: TurnEnvelope | None = None,
expect_refusal: str | None = None,
sub_work: bool = False,
+ running: int = 5,
) -> tuple[TurnEnvelope | None, int, int]:
"""Queue a turn behind a full cap, then end a turn carrying ``finished``.
@@ -126,7 +160,9 @@ async def _promote_after(
data={"id": user_id, "email": f"turn-queue-{user_id}@example.com"}
)
try:
- sessions = [await create_chat_session(user_id, dry_run=False) for _ in range(5)]
+ sessions = [
+ await create_chat_session(user_id, dry_run=False) for _ in range(running)
+ ]
for session in sessions:
assert await chat_db().update_chat_session_status(
session_id=session.session_id,
diff --git a/autogpt_platform/backend/backend/copilot/turn_queue_test.py b/autogpt_platform/backend/backend/copilot/turn_queue_test.py
index 29d67bb2f9..92925e5590 100644
--- a/autogpt_platform/backend/backend/copilot/turn_queue_test.py
+++ b/autogpt_platform/backend/backend/copilot/turn_queue_test.py
@@ -18,8 +18,9 @@ from prisma.errors import UniqueViolationError
from backend.copilot import turn_queue
from backend.copilot.gate import held
from backend.copilot.model import ChatMessage as PydanticChatMessage
+from backend.copilot.model import ChatSessionInfo
from backend.copilot.permissions import CopilotPermissions
-from backend.copilot.tree import TreeRefusal
+from backend.copilot.tree import TreeRefusal, TurnEnvelope
class _NoopAsyncCM:
@@ -47,16 +48,17 @@ def _pyd_message(**overrides) -> PydanticChatMessage:
return PydanticChatMessage(**base)
-def _mock_session(session_id: str = "s1", title: str | None = "T") -> MagicMock:
- """Build a ChatSessionInfo-ish mock for list_chat_sessions_by_status
- return values (the function returns app-model rows, not raw Prisma,
- so the RPC serializer can pass them through)."""
- s = MagicMock()
- s.session_id = session_id
- s.title = title
- s.updated_at = datetime.now(timezone.utc)
- s.metadata.llm_auth_provider = "platform"
- return s
+def _queued_row(session_id: str = "s1", title: str | None = "T") -> ChatSessionInfo:
+ """A row as ``list_chat_sessions_by_status`` returns it to the dispatcher."""
+ now = datetime.now(timezone.utc)
+ return ChatSessionInfo(
+ session_id=session_id,
+ user_id="u1",
+ title=title,
+ usage=[],
+ started_at=now,
+ updated_at=now,
+ )
@pytest.fixture(autouse=True)
@@ -258,7 +260,7 @@ async def test_promoted_turn_tracking_preserves_session_attribution(
) -> None:
if tracking_fails:
tracked_message.side_effect = RuntimeError("tracking failed")
- head = _mock_session()
+ head = _queued_row()
head.expert_id = expert_id
head.metadata.origin = origin
head.metadata.source_platform = source_platform
@@ -275,7 +277,9 @@ async def test_promoted_turn_tracking_preserves_session_attribution(
patch.object(turn_queue, "chat_db", return_value=db),
patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=True)),
patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=claim)
+ turn_queue,
+ "claim_queued_session",
+ new=AsyncMock(return_value="admitted" if claim else "full"),
),
patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
patch("backend.copilot.executor.utils.dispatch_turn", new=dispatched),
@@ -315,7 +319,7 @@ async def test_a_long_run_of_wakes_that_may_not_start_drains_without_recursing()
):
"""Each is closed and the next tried; more of them than the recursion limit
must still drain rather than overflow inside the completion hook."""
- queue = [_mock_session(f"s{i}") for i in range(sys.getrecursionlimit() + 100)]
+ queue = [_queued_row(f"s{i}") for i in range(sys.getrecursionlimit() + 100)]
for row in queue:
row.metadata.llm_auth_provider = "codex"
db = MagicMock()
@@ -335,7 +339,7 @@ async def test_a_long_run_of_wakes_that_may_not_start_drains_without_recursing()
patch.object(turn_queue, "chat_db", return_value=db),
patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=True)),
patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=True)
+ turn_queue, "claim_queued_session", new=AsyncMock(return_value="admitted")
),
patch.object(turn_queue, "_refuse_queued_turn", new=closed),
):
@@ -345,34 +349,84 @@ async def test_a_long_run_of_wakes_that_may_not_start_drains_without_recursing()
@pytest.mark.asyncio
-async def test_the_drain_stops_at_a_head_the_access_gate_refuses() -> None:
- """A wake that may never start is closed and the next head tried; one the
- shared access gate refuses stays queued, unclaimed, for a later tick."""
- closed, refused = _mock_session("closed"), _mock_session("refused")
- closed.metadata.llm_auth_provider = "microsoft_365_copilot"
+async def test_a_head_taken_since_it_was_listed_does_not_stop_promotion() -> None:
+ """Cancelled or claimed elsewhere between the listing and the claim: the
+ next queued session is tried rather than the dispatch giving up."""
+ gone, waiting = _queued_row("gone"), _queued_row("waiting")
+ for row in (gone, waiting):
+ row.metadata.llm_auth_provider = "codex"
+ db = MagicMock()
+ db.get_latest_user_message_in_session = AsyncMock(return_value=_pyd_message())
+ dispatched = AsyncMock()
+
+ with (
+ patch.object(
+ turn_queue,
+ "list_queued_sessions",
+ new=AsyncMock(side_effect=[[gone, waiting], [waiting]]),
+ ),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=True)),
+ patch.object(
+ turn_queue,
+ "claim_queued_session",
+ new=AsyncMock(side_effect=["busy", "admitted"]),
+ ),
+ patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
+ patch("backend.copilot.executor.utils.dispatch_turn", new=dispatched),
+ ):
+ assert await turn_queue.dispatch_next_for_user("u1") is True
+
+ assert dispatched.await_args.kwargs["session_id"] == "waiting"
+
+
+async def test_a_turn_the_access_gate_refuses_does_not_hold_up_the_next() -> None:
+ """A wake that may never start is closed; a turn the shared access gate
+ refuses stays queued, unclaimed, and the next one that may start does."""
+ closed, refused, admissible = (
+ _queued_row("closed"),
+ _queued_row("refused"),
+ _queued_row("admissible"),
+ )
+ # An automation's session: not the user's own chat, and not sub-work.
+ closed.metadata.origin = "automation"
+ for row in (closed, admissible):
+ row.metadata.llm_auth_provider = "microsoft_365_copilot"
refused.metadata.llm_auth_provider = "codex"
+ waiting = {
+ "closed": _pyd_message(metadata={held._WAKE_KEY: True}),
+ "refused": _pyd_message(),
+ "admissible": _pyd_message(),
+ }
db = MagicMock()
db.update_chat_session_status = AsyncMock(return_value=True)
- db.get_latest_user_message_in_session = AsyncMock(
- return_value=_pyd_message(metadata={held._WAKE_KEY: True})
- )
- claim = AsyncMock(return_value=True)
+ db.get_latest_user_message_in_session = AsyncMock(side_effect=waiting.get)
+ claim = AsyncMock(return_value="admitted")
+ dispatched = AsyncMock()
with (
patch.object(
turn_queue,
"list_queued_sessions",
- new=AsyncMock(side_effect=[[closed, refused], [refused]]),
+ new=AsyncMock(
+ side_effect=[[closed, refused, admissible], [refused, admissible]]
+ ),
),
patch.object(turn_queue, "chat_db", return_value=db),
patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=False)),
patch.object(turn_queue, "claim_queued_session", new=claim),
patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
patch.object(turn_queue, "append_and_save_message", new=AsyncMock()),
+ patch("backend.copilot.executor.utils.dispatch_turn", new=dispatched),
):
- assert await turn_queue.dispatch_next_for_user("u1") is False
+ assert await turn_queue.dispatch_next_for_user("u1") is True
- claim.assert_awaited_once_with("closed")
+ assert [c.args[0].session_id for c in claim.await_args_list] == [
+ "closed",
+ "admissible",
+ ]
+ assert dispatched.await_args.kwargs["session_id"] == "admissible"
db.update_chat_session_status.assert_awaited_once_with(
session_id="closed", expect_status="running", status="idle"
)
@@ -381,7 +435,7 @@ async def test_the_drain_stops_at_a_head_the_access_gate_refuses() -> None:
@pytest.mark.asyncio
async def test_unparseable_stored_permissions_read_the_sessions_current_ones() -> None:
"""Not left stuck at the head of the queue, and never read as none."""
- head = _mock_session()
+ head = _queued_row()
head.metadata.llm_auth_provider = "codex"
db = MagicMock()
db.update_chat_session_status = AsyncMock(return_value=True)
@@ -396,7 +450,7 @@ async def test_unparseable_stored_permissions_read_the_sessions_current_ones() -
patch.object(turn_queue, "chat_db", return_value=db),
patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=True)),
patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=True)
+ turn_queue, "claim_queued_session", new=AsyncMock(return_value="admitted")
),
patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
patch.object(turn_queue, "resolve_session_permissions", return_value=today),
@@ -407,6 +461,41 @@ async def test_unparseable_stored_permissions_read_the_sessions_current_ones() -
assert dispatched.await_args.kwargs["permissions"] == today
+async def test_a_typed_message_behind_sub_work_that_does_not_fit_still_starts() -> None:
+ """Four running: an older approval wake in a delegated session waits for the
+ reserve, while a newer message the user typed into another one fits."""
+ wake, typed = _queued_row("wake"), _queued_row("typed")
+ for row in (wake, typed):
+ row.metadata.delegated_by_session_id = "parent"
+ row.metadata.llm_auth_provider = "codex"
+ waiting = {
+ "wake": _pyd_message(metadata={held._WAKE_KEY: True}),
+ "typed": _pyd_message(),
+ }
+ db = MagicMock()
+ db.get_latest_user_message_in_session = AsyncMock(side_effect=waiting.get)
+ dispatched = AsyncMock()
+
+ with (
+ _patch_queued_list([wake, typed]),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=True)),
+ patch.object(
+ turn_queue,
+ "claim_queued_session",
+ new=AsyncMock(
+ side_effect=lambda _row, *, sub_work: "full" if sub_work else "admitted"
+ ),
+ ),
+ patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
+ patch("backend.copilot.executor.utils.dispatch_turn", new=dispatched),
+ ):
+ assert await turn_queue.dispatch_next_for_user("u1") is True
+
+ assert dispatched.await_args.kwargs["session_id"] == "typed"
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize(
"stored_envelope",
@@ -417,7 +506,8 @@ async def test_a_refusal_that_cannot_be_posted_still_frees_the_slot(
stored_envelope: dict | None,
) -> None:
"""The session leaves ``running`` and the next queued one is tried."""
- head = _mock_session()
+ head = _queued_row()
+ head.metadata.delegated_by_session_id = "parent"
head.metadata.llm_auth_provider = "codex"
metadata: dict = {held._WAKE_KEY: True}
if stored_envelope:
@@ -435,7 +525,7 @@ async def test_a_refusal_that_cannot_be_posted_still_frees_the_slot(
patch.object(turn_queue, "chat_db", return_value=db),
patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=True)),
patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=True)
+ turn_queue, "claim_queued_session", new=AsyncMock(return_value="admitted")
),
patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
patch.object(turn_queue, "resolve_session_permissions", return_value=None),
@@ -464,7 +554,7 @@ async def test_a_failure_between_the_claim_and_the_dispatch_requeues_the_turn(
failure: BaseException,
) -> None:
"""Not left ``running`` with no turn to end it: the next tick retries."""
- head = _mock_session()
+ head = _queued_row()
head.metadata.llm_auth_provider = "codex"
db = MagicMock()
db.update_chat_session_status = AsyncMock(return_value=True)
@@ -475,7 +565,7 @@ async def test_a_failure_between_the_claim_and_the_dispatch_requeues_the_turn(
patch.object(turn_queue, "chat_db", return_value=db),
patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=True)),
patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=True)
+ turn_queue, "claim_queued_session", new=AsyncMock(return_value="admitted")
),
patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
):
@@ -501,7 +591,7 @@ async def test_dispatch_leaves_queued_when_user_paywalled() -> None:
db = MagicMock()
db.update_chat_session_status = AsyncMock()
with (
- _patch_queued_list([_mock_session()]),
+ _patch_queued_list([_queued_row()]),
patch.object(turn_queue, "chat_db", return_value=db),
patch(
"backend.copilot.turn_queue.is_user_paywalled",
@@ -515,11 +605,12 @@ async def test_dispatch_leaves_queued_when_user_paywalled() -> None:
@pytest.mark.asyncio
async def test_codex_dispatch_skips_platform_billing_gates() -> None:
- head = _mock_session()
+ head = _queued_row()
head.metadata.llm_auth_provider = "codex"
head.metadata.llm_credential_id = "cred-1"
pending = _pyd_message()
db = MagicMock()
+ db.admit_chat_session_turn = AsyncMock(return_value="admitted")
db.update_chat_session_status = AsyncMock(return_value=True)
db.get_latest_user_message_in_session = AsyncMock(return_value=pending)
dispatch_turn_mock = AsyncMock()
@@ -577,14 +668,16 @@ async def test_promotion_rechecks_the_advanced_tier_before_spending() -> None:
of them to change -- but not the tier. A user who queued Advanced turns
and then downgraded had them dispatched anyway.
"""
- head = _mock_session()
+ head = _queued_row()
head.metadata.llm_auth_provider = "platform"
pending = _pyd_message(metadata={"model": "advanced"})
db = MagicMock()
+ db.admit_chat_session_turn = AsyncMock(return_value="admitted")
db.update_chat_session_status = AsyncMock(return_value=True)
db.get_latest_user_message_in_session = AsyncMock(return_value=pending)
dispatch_turn_mock = AsyncMock()
entitled = AsyncMock(return_value=False)
+ claim = AsyncMock(return_value="admitted")
with (
_patch_queued_list([head]),
@@ -598,9 +691,7 @@ async def test_promotion_rechecks_the_advanced_tier_before_spending() -> None:
new=AsyncMock(return_value=(1, 1, None)),
),
patch("backend.copilot.turn_queue.check_rate_limit", new=AsyncMock()),
- patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=True)
- ),
+ patch.object(turn_queue, "claim_queued_session", new=claim),
patch.object(turn_queue, "advanced_tier_entitled", new=entitled),
patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
patch("backend.copilot.executor.utils.dispatch_turn", new=dispatch_turn_mock),
@@ -610,21 +701,23 @@ async def test_promotion_rechecks_the_advanced_tier_before_spending() -> None:
assert promoted is False
entitled.assert_awaited_once_with("u1")
dispatch_turn_mock.assert_not_awaited()
- # Back to queued rather than quietly re-run on Standard: nothing here
+ # Left queued rather than quietly re-run on Standard: nothing here
# changes what a turn runs on without being asked.
- assert db.update_chat_session_status.await_args.kwargs["status"] == "queued"
+ claim.assert_not_awaited()
@pytest.mark.asyncio
async def test_promotion_refuses_when_the_entitlement_cannot_be_resolved() -> None:
"""An outage is not permission to spend, and not a reason to lose the turn."""
- head = _mock_session()
+ head = _queued_row()
head.metadata.llm_auth_provider = "platform"
pending = _pyd_message(metadata={"model": "advanced"})
db = MagicMock()
+ db.admit_chat_session_turn = AsyncMock(return_value="admitted")
db.update_chat_session_status = AsyncMock(return_value=True)
db.get_latest_user_message_in_session = AsyncMock(return_value=pending)
dispatch_turn_mock = AsyncMock()
+ claim = AsyncMock(return_value="admitted")
with (
_patch_queued_list([head]),
@@ -638,9 +731,7 @@ async def test_promotion_refuses_when_the_entitlement_cannot_be_resolved() -> No
new=AsyncMock(return_value=(1, 1, None)),
),
patch("backend.copilot.turn_queue.check_rate_limit", new=AsyncMock()),
- patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=True)
- ),
+ patch.object(turn_queue, "claim_queued_session", new=claim),
patch.object(
turn_queue,
"advanced_tier_entitled",
@@ -653,16 +744,152 @@ async def test_promotion_refuses_when_the_entitlement_cannot_be_resolved() -> No
assert promoted is False
dispatch_turn_mock.assert_not_awaited()
- assert db.update_chat_session_status.await_args.kwargs["status"] == "queued"
+ claim.assert_not_awaited()
+
+
+ "queue, promoted",
+ [
+ # The user's own message goes first, though sub-work queued earlier.
+ ([("sub", "parent", "platform"), ("chat", None, "platform")], "chat"),
+ # One that may not start (no Codex access) does not hold up the rest.
+ ([("chat", None, "codex"), ("sub", "parent", "platform")], "sub"),
+ # With nothing of the user's queued, sub-work is promoted.
+ ([("sub", "parent", "platform")], "sub"),
+ # Within each group the oldest goes first.
+ ([("older", None, "platform"), ("newer", None, "platform")], "older"),
+ ([("older", "parent", "platform"), ("newer", "parent", "platform")], "older"),
+ ],
+)
+async def test_promotion_order(
+ queue: list[tuple[str, str | None, str]], promoted: str
+) -> None:
+ rows = [_queued_row(session_id) for session_id, _, _ in queue]
+ for row, (_, delegated_by, route) in zip(rows, queue):
+ row.metadata.delegated_by_session_id = delegated_by
+ row.metadata.llm_auth_provider = route
+ # A delegated row's turn is sub-work: an approval wake held under its tree.
+ sub_work = {
+ held._WAKE_KEY: True,
+ "envelope": TurnEnvelope(tree_id="tree", depth=1).model_dump(mode="json"),
+ }
+ waiting = {
+ session_id: _pyd_message(
+ metadata=sub_work if delegated_by else {"model": "standard"}
+ )
+ for session_id, delegated_by, _ in queue
+ }
+ db = MagicMock()
+ db.get_latest_user_message_in_session = AsyncMock(side_effect=waiting.get)
+ claim = AsyncMock(return_value="admitted")
+ dispatched = AsyncMock()
+
+ with (
+ _patch_queued_list(rows),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=False)),
+ patch(
+ "backend.copilot.turn_queue.is_user_paywalled",
+ new=AsyncMock(return_value=False),
+ ),
+ patch(
+ "backend.copilot.turn_queue.get_global_rate_limits",
+ new=AsyncMock(return_value=(1, 1, None)),
+ ),
+ patch("backend.copilot.turn_queue.check_rate_limit", new=AsyncMock()),
+ patch.object(turn_queue, "claim_queued_session", new=claim),
+ patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
+ patch("backend.copilot.executor.utils.dispatch_turn", new=dispatched),
+ ):
+ assert await turn_queue.dispatch_next_for_user("u1") is True
+
+ assert claim.await_args.args[0].session_id == promoted
+ assert dispatched.await_args.kwargs["session_id"] == promoted
+
+
+async def test_an_advanced_turn_without_the_tier_does_not_hold_up_the_queue() -> None:
+ """Skipped like any session that may not start yet, and the per-user checks
+ are made once however many sessions are tried."""
+ chat, sub = _queued_row("chat"), _queued_row("sub")
+ sub.metadata.delegated_by_session_id = "parent"
+ models = {"chat": "advanced", "sub": "standard"}
+ db = MagicMock()
+ db.get_latest_user_message_in_session = AsyncMock(
+ side_effect=lambda sid: _pyd_message(metadata={"model": models[sid]})
+ )
+ paywalled = AsyncMock(return_value=False)
+ claim = AsyncMock(return_value="admitted")
+
+ with (
+ _patch_queued_list([chat, sub]),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch.object(turn_queue, "is_user_paywalled", new=paywalled),
+ patch.object(
+ turn_queue,
+ "get_global_rate_limits",
+ new=AsyncMock(return_value=(1, 1, None)),
+ ),
+ patch.object(turn_queue, "check_rate_limit", new=AsyncMock()),
+ patch.object(
+ turn_queue, "advanced_tier_entitled", new=AsyncMock(return_value=False)
+ ),
+ patch.object(turn_queue, "claim_queued_session", new=claim),
+ patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
+ patch("backend.copilot.executor.utils.dispatch_turn", new=AsyncMock()),
+ ):
+ assert await turn_queue.dispatch_next_for_user("u1") is True
+
+ assert claim.await_args.args[0].session_id == "sub"
+ paywalled.assert_awaited_once_with("u1")
+
+
+async def test_a_degraded_rate_limit_service_leaves_the_whole_queue() -> None:
+ chat, sub = _queued_row("chat"), _queued_row("sub")
+ sub.metadata.delegated_by_session_id = "parent"
+ sub.metadata.llm_auth_provider = "codex"
+ codex = AsyncMock(return_value=True)
+ db = MagicMock()
+ db.get_latest_user_message_in_session = AsyncMock(return_value=_pyd_message())
+ claim = AsyncMock(return_value="admitted")
+
+ with (
+ _patch_queued_list([chat, sub]),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ # A dispatch that scanned on would start the turn: fail, not hang.
+ patch("backend.copilot.executor.utils.dispatch_turn", new=AsyncMock()),
+ patch.object(
+ turn_queue, "is_user_paywalled", new=AsyncMock(return_value=False)
+ ),
+ patch.object(
+ turn_queue,
+ "get_global_rate_limits",
+ new=AsyncMock(return_value=(1, 1, None)),
+ ),
+ patch.object(
+ turn_queue,
+ "check_rate_limit",
+ new=AsyncMock(side_effect=turn_queue.RateLimitUnavailable()),
+ ),
+ patch.object(turn_queue, "has_codex_access", new=codex),
+ patch.object(turn_queue, "claim_queued_session", new=claim),
+ ):
+ assert await turn_queue.dispatch_next_for_user("u1") is False
+
+ codex.assert_not_awaited()
+ claim.assert_not_awaited()
@pytest.mark.asyncio
async def test_promotion_does_not_recheck_the_tier_for_a_standard_turn() -> None:
"""The gate is on the paid tier only; Balanced promotes as before."""
- head = _mock_session()
+ head = _queued_row()
head.metadata.llm_auth_provider = "platform"
pending = _pyd_message(metadata={"model": "standard"})
db = MagicMock()
+ db.admit_chat_session_turn = AsyncMock(return_value="admitted")
db.update_chat_session_status = AsyncMock(return_value=True)
db.get_latest_user_message_in_session = AsyncMock(return_value=pending)
entitled = AsyncMock(side_effect=AssertionError("tier checked for Balanced"))
@@ -680,7 +907,7 @@ async def test_promotion_does_not_recheck_the_tier_for_a_standard_turn() -> None
),
patch("backend.copilot.turn_queue.check_rate_limit", new=AsyncMock()),
patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=True)
+ turn_queue, "claim_queued_session", new=AsyncMock(return_value="admitted")
),
patch.object(turn_queue, "advanced_tier_entitled", new=entitled),
patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
@@ -700,11 +927,12 @@ async def test_promotion_uses_current_codex_route_not_stale_platform_tier() -> N
session route is deliberately mutable so a blocked turn can continue on a
subscription-backed connection.
"""
- head = _mock_session()
+ head = _queued_row()
head.metadata.llm_auth_provider = "codex"
head.metadata.llm_credential_id = "cred-1"
pending = _pyd_message(metadata={"model": "advanced"})
db = MagicMock()
+ db.admit_chat_session_turn = AsyncMock(return_value="admitted")
db.update_chat_session_status = AsyncMock(return_value=True)
db.get_latest_user_message_in_session = AsyncMock(return_value=pending)
dispatch_turn_mock = AsyncMock()
@@ -715,7 +943,7 @@ async def test_promotion_uses_current_codex_route_not_stale_platform_tier() -> N
patch.object(turn_queue, "chat_db", return_value=db),
patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=True)),
patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=True)
+ turn_queue, "claim_queued_session", new=AsyncMock(return_value="admitted")
),
patch.object(turn_queue, "advanced_tier_entitled", new=entitled),
patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
@@ -731,11 +959,12 @@ async def test_promotion_uses_current_codex_route_not_stale_platform_tier() -> N
@pytest.mark.asyncio
async def test_microsoft_promotion_skips_platform_billing_gates() -> None:
- head = _mock_session()
+ head = _queued_row()
head.metadata.llm_auth_provider = "microsoft_365_copilot"
head.metadata.llm_credential_id = "cred-microsoft"
pending = _pyd_message(metadata={"model": "advanced"})
db = MagicMock()
+ db.admit_chat_session_turn = AsyncMock(return_value="admitted")
db.update_chat_session_status = AsyncMock(return_value=True)
db.get_latest_user_message_in_session = AsyncMock(return_value=pending)
dispatch_turn_mock = AsyncMock()
@@ -749,7 +978,7 @@ async def test_microsoft_promotion_skips_platform_billing_gates() -> None:
patch.object(turn_queue, "is_user_paywalled", new=platform_gate),
patch.object(turn_queue, "advanced_tier_entitled", new=platform_gate),
patch.object(
- turn_queue, "claim_queued_session", new=AsyncMock(return_value=True)
+ turn_queue, "claim_queued_session", new=AsyncMock(return_value="admitted")
),
patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
patch("backend.copilot.executor.utils.dispatch_turn", new=dispatch_turn_mock),
@@ -766,7 +995,7 @@ async def test_microsoft_promotion_skips_platform_billing_gates() -> None:
@pytest.mark.asyncio
async def test_codex_dispatch_leaves_queued_when_user_lacks_access() -> None:
- head = _mock_session()
+ head = _queued_row()
head.metadata.llm_auth_provider = "codex"
head.metadata.llm_credential_id = "cred-1"
db = MagicMock()
@@ -800,7 +1029,7 @@ async def test_dispatch_leaves_queued_on_rate_limit_exceeded() -> None:
db = MagicMock()
db.update_chat_session_status = AsyncMock()
with (
- _patch_queued_list([_mock_session()]),
+ _patch_queued_list([_queued_row()]),
patch.object(turn_queue, "chat_db", return_value=db),
patch(
"backend.copilot.turn_queue.is_user_paywalled",
@@ -831,7 +1060,7 @@ async def test_dispatch_defers_on_rate_limit_unavailable() -> None:
db = MagicMock()
db.update_chat_session_status = AsyncMock()
with (
- _patch_queued_list([_mock_session()]),
+ _patch_queued_list([_queued_row()]),
patch.object(turn_queue, "chat_db", return_value=db),
patch(
"backend.copilot.turn_queue.is_user_paywalled",
@@ -851,9 +1080,10 @@ async def test_dispatch_defers_on_rate_limit_unavailable() -> None:
async def test_dispatch_happy_path_claims_and_dispatches() -> None:
"""All gates pass → claim session queued → running, build a TurnSlot,
dispatch_turn, invalidate cache, return True."""
- head = _mock_session(session_id="s1")
+ head = _queued_row(session_id="s1")
pending = _pyd_message(metadata={"mode": "extended_thinking"})
db = MagicMock()
+ db.admit_chat_session_turn = AsyncMock(return_value="admitted")
db.update_chat_session_status = AsyncMock(return_value=True)
db.get_latest_user_message_in_session = AsyncMock(return_value=pending)
dispatch_turn_mock = AsyncMock()
@@ -883,21 +1113,25 @@ async def test_dispatch_happy_path_claims_and_dispatches() -> None:
assert promoted is True
dispatch_turn_mock.assert_awaited_once()
invalidate.assert_awaited_once_with("s1")
- # Single claim transition fired (no restore).
- db.update_chat_session_status.assert_awaited_once_with(
- session_id="s1", expect_status="queued", status="running"
- )
+ # Claimed queued → running under the user's cap; nothing restored.
+ assert db.admit_chat_session_turn.await_args.kwargs == {
+ "session_id": "s1",
+ "user_id": "u1",
+ "expect_status": "queued",
+ "capacity": turn_queue.get_running_turn_limit(),
+ }
+ db.update_chat_session_status.assert_not_awaited()
@pytest.mark.asyncio
async def test_dispatch_rolls_claim_back_on_dispatch_failure() -> None:
"""If ``dispatch_turn`` raises after claim, restore the session
``running`` → ``queued`` so the next tick can retry."""
- head = _mock_session(session_id="s1")
+ head = _queued_row(session_id="s1")
pending = _pyd_message(metadata={"mode": "extended_thinking"})
db = MagicMock()
- # First call (claim) returns True; second call (restore) also True.
- db.update_chat_session_status = AsyncMock(side_effect=[True, True])
+ db.admit_chat_session_turn = AsyncMock(return_value="admitted")
+ db.update_chat_session_status = AsyncMock(return_value=True)
db.get_latest_user_message_in_session = AsyncMock(return_value=pending)
dispatch_turn_mock = AsyncMock(side_effect=RuntimeError("RabbitMQ blip"))
with (
@@ -928,10 +1162,7 @@ async def test_dispatch_rolls_claim_back_on_dispatch_failure() -> None:
# ``committed`` flag), not the dispatcher's — see the
# ``test_dispatch_turn_cleans_redis_on_enqueue_failure`` test in
# ``executor/utils_test`` for that contract.
- assert db.update_chat_session_status.await_count == 2
- db.update_chat_session_status.assert_any_await(
- session_id="s1", expect_status="queued", status="running"
- )
- db.update_chat_session_status.assert_any_await(
+ db.admit_chat_session_turn.assert_awaited_once()
+ db.update_chat_session_status.assert_awaited_once_with(
session_id="s1", expect_status="running", status="queued"
)
diff --git a/autogpt_platform/backend/backend/data/db_manager.py b/autogpt_platform/backend/backend/data/db_manager.py
index 2168a0448d..c38d878920 100644
--- a/autogpt_platform/backend/backend/data/db_manager.py
+++ b/autogpt_platform/backend/backend/data/db_manager.py
@@ -675,6 +675,7 @@ class DatabaseManager(AppService):
count_chat_sessions_by_status = _(chat_db.count_chat_sessions_by_status)
list_chat_sessions_by_status = _(chat_db.list_chat_sessions_by_status)
update_chat_session_status = _(chat_db.update_chat_session_status)
+ admit_chat_session_turn = _(chat_db.admit_chat_session_turn)
get_chat_session_status = _(chat_db.get_chat_session_status)
get_latest_user_message_in_session = _(chat_db.get_latest_user_message_in_session)
@@ -1101,6 +1102,7 @@ class DatabaseManagerAsyncClient(AppServiceClient):
count_chat_sessions_by_status = d.count_chat_sessions_by_status
list_chat_sessions_by_status = d.list_chat_sessions_by_status
update_chat_session_status = d.update_chat_session_status
+ admit_chat_session_turn = d.admit_chat_session_turn
get_chat_session_status = d.get_chat_session_status
get_latest_user_message_in_session = d.get_latest_user_message_in_session
add_chat_message = d.add_chat_message
diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.py
index 7c5a157d5d..232932f03d 100644
--- a/autogpt_platform/backend/backend/util/settings.py
+++ b/autogpt_platform/backend/backend/util/settings.py
@@ -338,7 +338,10 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):
"are queued in ``CopilotTaskQueue`` (FIFO) up to "
"``max_inflight_copilot_turns_per_user`` total in-flight. "
"Must be <= the in-flight cap; default 5 keeps shared-infra "
- "concurrency predictable while letting users batch-submit."
+ "concurrency predictable while letting users batch-submit. "
+ "Turns another session started (sub-sessions, delegations, "
+ "handoffs) are admitted only below this cap minus one, so one "
+ "slot is always left for the user's own message."
),
)