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): AutoPilot task queue with 5 running + 15 in-flight caps (#13069)

Zamil Majdye4cc0b882005
diff --git a/.gitignore b/.gitignore
index 5611fd764d..a70415df80 100644
--- a/.gitignore
+++ b/.gitignore
@@ -205,3 +205,9 @@ copilot-session-switch-qa/
tmp/
screenshots/
test-screenshots/
+# Local scratch convention: anything starting with ``.ign`` is gitignored
+# wherever it lives — directories (e.g. ``.ign/``), per-process log tees
+# (``.ign.application.logs``, ``.ign.frontend.logs``), lock files
+# (``.ign.testing.lock``), and any future ``.ign*`` variant.
+.ign*
+**/.ign*
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.py
index b1c809630f..471a459405 100644
--- a/autogpt_platform/backend/backend/api/features/chat/routes.py
+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py
@@ -3,6 +3,7 @@
import asyncio
import logging
from collections.abc import AsyncGenerator
+from datetime import datetime, timezone
from typing import Annotated
from uuid import uuid4
@@ -11,17 +12,21 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Response, Security
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, ConfigDict, Field, field_validator
+from backend.copilot import active_turns
from backend.copilot import service as chat_service
-from backend.copilot import stream_registry
+from backend.copilot import stream_registry, turn_queue
from backend.copilot.active_turns import (
ConcurrentTurnLimitError,
- concurrent_turn_limit_message,
+ get_inflight_turn_limit,
+ inflight_turn_limit_message,
)
from backend.copilot.builder_context import resolve_session_permissions
from backend.copilot.config import ChatConfig, CopilotLlmModel, CopilotMode
from backend.copilot.db import get_chat_messages_paginated
from backend.copilot.executor.utils import enqueue_cancel_task, schedule_chat_turn
from backend.copilot.model import (
+ CHAT_STATUS_IDLE,
+ CHAT_STATUS_RUNNING,
ChatSessionInfo,
ChatSessionMetadata,
create_chat_session,
@@ -38,6 +43,7 @@ from backend.copilot.pending_message_helpers import (
queue_pending_for_http,
)
from backend.copilot.pending_messages import peek_pending_messages
+from backend.copilot.permissions import CopilotPermissions
from backend.copilot.rate_limit import (
CoPilotUsagePublic,
RateLimitExceeded,
@@ -124,6 +130,39 @@ async def _validate_and_get_session(
return session
+# Minimum age before the orphan-reset paths (``get_session`` and
+# ``cancel_session_task``) will touch a ``chatStatus='running'`` session
+# that has no live Redis stream. Lower bound has to clear the
+# ``acquire_turn_slot``→``dispatch_turn.create_session`` window (a few
+# ms in practice). 30s is a generous safety margin — anything still
+# at ``running`` after that without a Redis stream is genuinely an
+# orphan, not an in-flight admit racing this read.
+_ORPHAN_RUNNING_RESET_THRESHOLD_SECONDS = 30
+
+
+async def _try_release_orphan_running(session_id: str, user_id: str) -> bool:
+ """Force-release a session if it's stuck in ``chatStatus='running'``
+ older than ``_ORPHAN_RUNNING_RESET_THRESHOLD_SECONDS`` (= the
+ ``acquire_turn_slot``→``create_session`` race window). Returns
+ True iff a release happened — callers map that into their response
+ so the user can tell ``orphan_released`` apart from
+ ``no_active_session``."""
+ meta = await get_chat_session_metadata(session_id)
+ if meta is None or meta.chat_status != CHAT_STATUS_RUNNING:
+ return False
+ # Prisma returns tz-aware UTC for ``DateTime`` columns today, but
+ # treat a naive value as UTC defensively so an accidental schema
+ # change can't make this subtraction raise ``TypeError``.
+ updated_at = meta.updated_at
+ if updated_at.tzinfo is None:
+ updated_at = updated_at.replace(tzinfo=timezone.utc)
+ age = (datetime.now(timezone.utc) - updated_at).total_seconds()
+ if age <= _ORPHAN_RUNNING_RESET_THRESHOLD_SECONDS:
+ return False
+ await active_turns.release_turn_slot(user_id, session_id)
+ return True
+
+
router = APIRouter(
tags=["chat"],
)
@@ -266,6 +305,7 @@ class SessionDetailResponse(BaseModel):
created_at: str
updated_at: str
user_id: str | None
+ chat_status: str = "idle"
messages: list[dict]
active_stream: ActiveStreamInfo | None = None # Present if stream is still active
has_more_messages: bool = False
@@ -282,6 +322,7 @@ class SessionSummaryResponse(BaseModel):
created_at: str
updated_at: str
title: str | None = None
+ chat_status: str = "idle"
is_processing: bool
@@ -373,6 +414,7 @@ async def list_sessions(
created_at=session.started_at.isoformat(),
updated_at=session.updated_at.isoformat(),
title=session.title,
+ chat_status=session.chat_status,
is_processing=session.session_id in processing_set,
)
for session in sessions
@@ -577,6 +619,13 @@ async def get_session(
last_message_id=last_message_id,
started_at=active_session.created_at.isoformat(),
)
+ elif page.session.chat_status == CHAT_STATUS_RUNNING:
+ # DB says running but Redis has no live stream — either the
+ # executor crashed mid-turn or a fresh admit is racing this
+ # read. ``_try_release_orphan_running`` age-gates the
+ # cleanup so an in-flight ``dispatch_turn`` isn't stomped.
+ if await _try_release_orphan_running(session_id, user_id):
+ page.session.chat_status = CHAT_STATUS_IDLE
# Skip session metadata on "load more" — frontend only needs messages
if before_sequence is not None:
@@ -585,6 +634,7 @@ async def get_session(
created_at=page.session.started_at.isoformat(),
updated_at=page.session.updated_at.isoformat(),
user_id=page.session.user_id or None,
+ chat_status=page.session.chat_status,
messages=messages,
active_stream=None,
has_more_messages=page.has_more,
@@ -601,6 +651,7 @@ async def get_session(
created_at=page.session.started_at.isoformat(),
updated_at=page.session.updated_at.isoformat(),
user_id=page.session.user_id or None,
+ chat_status=page.session.chat_status,
messages=messages,
active_stream=active_stream_info,
has_more_messages=page.has_more,
@@ -831,16 +882,39 @@ async def cancel_session_task(
session_id: str,
user_id: Annotated[str, Security(auth.get_user_id)],
) -> CancelSessionResponse:
- """Cancel the active streaming task for a session.
+ """Cancel an in-flight task for a session.
+
+ Handles both lifecycle states uniformly:
- Publishes a cancel event to the executor via RabbitMQ FANOUT, then
- polls Redis until the task status flips from ``running`` or a timeout
- (5 s) is reached. Returns only after the cancellation is confirmed.
+ * **Queued** (``chatStatus='queued'``) — the dispatcher hasn't
+ claimed the row yet. Flip the session back to ``idle`` and
+ return; no executor cancel event needed.
+ * **Running** (``chatStatus='running'``) — publish a cancel event
+ to the executor via RabbitMQ FANOUT, then poll Redis until the
+ task status flips out of ``running`` or a 5 s timeout is hit.
"""
await _validate_and_get_session(session_id, user_id)
+ # Queued sessions: just flip back to idle. The user clicked X
+ # before any compute was spent; no executor involvement needed.
+ if await turn_queue.cancel_queued_turn(user_id=user_id, session_id=session_id):
+ return CancelSessionResponse(cancelled=True, reason="dequeued")
+
active_session, _ = await stream_registry.get_active_session(session_id, user_id)
if not active_session:
+ # No Redis stream entry. Two possibilities:
+ # (a) Executor crashed mid-turn — Redis meta TTL'd out while DB
+ # ``chatStatus`` stayed ``running`` (the orphan case the user
+ # wants cleaned up).
+ # (b) Sub-millisecond race against a fresh admit: ``acquire_turn_slot``
+ # CAS'd DB ``idle → running`` and the route is about to call
+ # ``dispatch_turn.create_session`` to write Redis. Force-
+ # releasing here would let the turn run while DB says idle,
+ # orphaning the executor work in the opposite direction.
+ # Gate the cleanup on session age so a fresh admit isn't stomped.
+ # Anything older than the threshold is the real orphan.
+ if await _try_release_orphan_running(session_id, user_id):
+ return CancelSessionResponse(cancelled=True, reason="orphan_released")
return CancelSessionResponse(cancelled=True, reason="no_active_session")
await enqueue_cancel_task(session_id)
@@ -1075,10 +1149,42 @@ async def stream_chat_post(
permissions=builder_permissions,
request_arrival_at=request_arrival_at,
)
- except ConcurrentTurnLimitError as exc:
- raise HTTPException(
- status_code=429, detail=concurrent_turn_limit_message()
- ) from exc
+ except ConcurrentTurnLimitError:
+ # Soft running cap (default 5) hit. Fall through to the queue:
+ # if total in-flight (running + queued) is still under the hard
+ # cap (default 15), persist the user's message and flip the
+ # session ``idle`` → ``queued`` so the slot-free hook can promote
+ # it later. Past the hard cap the user is blocked at HTTP 429.
+ inflight_cap = get_inflight_turn_limit()
+ try:
+ await turn_queue.try_enqueue_turn(
+ user_id=user_id,
+ inflight_cap=inflight_cap,
+ session_id=session_id,
+ message=request.message,
+ message_id=request.message_id,
+ is_user_message=request.is_user_message,
+ context=request.context,
+ file_ids=sanitized_file_ids,
+ mode=request.mode,
+ model=request.model,
+ permissions=(
+ builder_permissions.model_dump(exclude_none=True)
+ if builder_permissions
+ else None
+ ),
+ request_arrival_at=request_arrival_at,
+ )
+ except turn_queue.InflightCapExceeded:
+ raise HTTPException(
+ status_code=429,
+ detail=inflight_turn_limit_message(inflight_cap),
+ )
+ logger.info(
+ f"[STREAM] Queued turn for session={session_id} "
+ f"(running cap reached; inflight cap={inflight_cap})"
+ )
+ return _empty_ui_message_stream_response()
if turn_id is None:
logger.info(
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 de26d27cf4..28c273e971 100644
--- a/autogpt_platform/backend/backend/api/features/chat/routes_test.py
+++ b/autogpt_platform/backend/backend/api/features/chat/routes_test.py
@@ -1304,11 +1304,36 @@ def _mock_validate_session(
def test_cancel_session_no_active_task(mocker: pytest_mock.MockerFixture) -> None:
- """Cancel returns cancelled=True with reason when no stream is active."""
+ """Cancel returns ``reason="no_active_session"`` when no Redis
+ stream exists AND the orphan-reset age gate isn't satisfied (e.g.
+ DB ``chatStatus='idle'`` or a fresh admit racing this read). The
+ separate test below covers the orphan-release branch."""
_mock_validate_session(mocker)
+ mocker.patch(
+ "backend.copilot.turn_queue.cancel_queued_turn",
+ new=AsyncMock(return_value=False),
+ )
mock_registry = MagicMock()
mock_registry.get_active_session = AsyncMock(return_value=(None, None))
mocker.patch("backend.api.features.chat.routes.stream_registry", mock_registry)
+ # No orphan release: metadata returns idle, so the age gate yields False.
+ from backend.copilot.model import ChatSessionInfo, ChatSessionMetadata
+
+ now = datetime.now(UTC)
+ mocker.patch(
+ "backend.api.features.chat.routes.get_chat_session_metadata",
+ new=AsyncMock(
+ return_value=ChatSessionInfo(
+ session_id="sess-1",
+ user_id=TEST_USER_ID,
+ usage=[],
+ started_at=now,
+ updated_at=now,
+ metadata=ChatSessionMetadata(),
+ chat_status="idle",
+ )
+ ),
+ )
response = client.post("/sessions/sess-1/cancel")
@@ -1318,6 +1343,119 @@ def test_cancel_session_no_active_task(mocker: pytest_mock.MockerFixture) -> Non
assert data["reason"] == "no_active_session"
+def test_cancel_session_releases_orphan_running(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """When the DB has been stuck at ``chatStatus='running'`` past the
+ age threshold and Redis has no live stream, cancel force-releases
+ the slot and returns ``reason='orphan_released'``."""
+ _mock_validate_session(mocker)
+ mocker.patch(
+ "backend.copilot.turn_queue.cancel_queued_turn",
+ new=AsyncMock(return_value=False),
+ )
+ mock_registry = MagicMock()
+ mock_registry.get_active_session = AsyncMock(return_value=(None, None))
+ mocker.patch("backend.api.features.chat.routes.stream_registry", mock_registry)
+ from backend.copilot.model import ChatSessionInfo, ChatSessionMetadata
+
+ # Session has been running for ~1 hour — past the 30s threshold.
+ stale = datetime.now(UTC) - timedelta(hours=1)
+ mocker.patch(
+ "backend.api.features.chat.routes.get_chat_session_metadata",
+ new=AsyncMock(
+ return_value=ChatSessionInfo(
+ session_id="sess-1",
+ user_id=TEST_USER_ID,
+ usage=[],
+ started_at=stale,
+ updated_at=stale,
+ metadata=ChatSessionMetadata(),
+ chat_status="running",
+ )
+ ),
+ )
+ mock_release = AsyncMock()
+ mocker.patch(
+ "backend.api.features.chat.routes.active_turns.release_turn_slot",
+ new=mock_release,
+ )
+
+ response = client.post("/sessions/sess-1/cancel")
+
+ assert response.status_code == 200
+ data = response.json()
+ assert data["cancelled"] is True
+ assert data["reason"] == "orphan_released"
+ mock_release.assert_awaited_once()
+
+
+def test_cancel_session_skips_orphan_release_within_race_window(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """A session whose DB ``chatStatus='running'`` flip happened
+ sub-second ago is almost certainly a fresh admit racing
+ ``dispatch_turn.create_session`` — NOT an orphan. The age gate
+ must skip the release so we don't stomp the in-flight dispatch."""
+ _mock_validate_session(mocker)
+ mocker.patch(
+ "backend.copilot.turn_queue.cancel_queued_turn",
+ new=AsyncMock(return_value=False),
+ )
+ mock_registry = MagicMock()
+ mock_registry.get_active_session = AsyncMock(return_value=(None, None))
+ mocker.patch("backend.api.features.chat.routes.stream_registry", mock_registry)
+ from backend.copilot.model import ChatSessionInfo, ChatSessionMetadata
+
+ fresh = datetime.now(UTC) - timedelta(seconds=1)
+ mocker.patch(
+ "backend.api.features.chat.routes.get_chat_session_metadata",
+ new=AsyncMock(
+ return_value=ChatSessionInfo(
+ session_id="sess-1",
+ user_id=TEST_USER_ID,
+ usage=[],
+ started_at=fresh,
+ updated_at=fresh,
+ metadata=ChatSessionMetadata(),
+ chat_status="running",
+ )
+ ),
+ )
+ mock_release = AsyncMock()
+ mocker.patch(
+ "backend.api.features.chat.routes.active_turns.release_turn_slot",
+ new=mock_release,
+ )
+
+ response = client.post("/sessions/sess-1/cancel")
+
+ assert response.status_code == 200
+ data = response.json()
+ assert data["reason"] == "no_active_session"
+ mock_release.assert_not_awaited()
+
+
+def test_cancel_session_dequeues_when_queued(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """A queued session is cancelled by flipping chatStatus → idle (no
+ executor cancel event needed). ``reason='dequeued'`` distinguishes
+ this branch from the running-stream cancel."""
+ _mock_validate_session(mocker)
+ mocker.patch(
+ "backend.copilot.turn_queue.cancel_queued_turn",
+ new=AsyncMock(return_value=True),
+ )
+
+ response = client.post("/sessions/sess-1/cancel")
+
+ assert response.status_code == 200
+ data = response.json()
+ assert data["cancelled"] is True
+ assert data["reason"] == "dequeued"
+
+
def test_cancel_session_enqueues_cancel_and_confirms(
mocker: pytest_mock.MockerFixture,
) -> None:
@@ -1325,6 +1463,11 @@ def test_cancel_session_enqueues_cancel_and_confirms(
from backend.copilot.stream_registry import ActiveSession
_mock_validate_session(mocker)
+ # Session isn't queued — fall through to the running-stream path.
+ mocker.patch(
+ "backend.copilot.turn_queue.cancel_queued_turn",
+ new=AsyncMock(return_value=False),
+ )
active_session = ActiveSession(
session_id="sess-1",
user_id=TEST_USER_ID,
@@ -1828,6 +1971,55 @@ def test_get_session_returns_backward_paginated(
assert "newest_sequence" not in data
+def test_get_session_releases_orphan_when_redis_empty_and_db_running(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """A session whose DB says ``chatStatus='running'`` for longer than
+ the race-window threshold AND has no live Redis stream is an orphan
+ (executor crashed mid-turn). Opening the chat must force-release
+ the slot so the sidebar's green dot stops showing."""
+ from backend.copilot.model import ChatSessionInfo, ChatSessionMetadata
+
+ stale = datetime.now(UTC) - timedelta(hours=1)
+ page, _ = _make_paginated_messages(mocker)
+ page.session.chat_status = "running"
+ page.session.updated_at = stale
+ mocker.patch(
+ "backend.api.features.chat.routes.stream_registry.get_active_session",
+ new_callable=AsyncMock,
+ return_value=(None, None),
+ )
+ mocker.patch(
+ "backend.api.features.chat.routes.get_chat_session_metadata",
+ new=AsyncMock(
+ return_value=ChatSessionInfo(
+ session_id="sess-1",
+ user_id=TEST_USER_ID,
+ usage=[],
+ started_at=stale,
+ updated_at=stale,
+ metadata=ChatSessionMetadata(),
+ chat_status="running",
+ )
+ ),
+ )
+ mock_release = AsyncMock()
+ mocker.patch(
+ "backend.api.features.chat.routes.active_turns.release_turn_slot",
+ new=mock_release,
+ )
+
+ response = client.get("/sessions/sess-1")
+
+ assert response.status_code == 200
+ data = response.json()
+ # Local fixup also flips the response's chat_status so the frontend
+ # doesn't render the green dot on the very response that triggered
+ # the reset.
+ assert data["chat_status"] == "idle"
+ mock_release.assert_awaited_once()
+
+
# ─── POST /sessions with builder_graph_id (get-or-create) ──────────────
diff --git a/autogpt_platform/backend/backend/blocks/autopilot.py b/autogpt_platform/backend/backend/blocks/autopilot.py
index 656fa8fbf2..f8445ffddb 100644
--- a/autogpt_platform/backend/backend/blocks/autopilot.py
+++ b/autogpt_platform/backend/backend/blocks/autopilot.py
@@ -19,7 +19,7 @@ from backend.blocks._base import (
)
from backend.copilot.active_turns import (
MAX_TURN_LIFETIME_SECONDS,
- concurrent_turn_limit_message,
+ running_turn_limit_message,
)
from backend.copilot.permissions import (
DISABLED_LEGACY_TOOL_NAMES,
@@ -368,7 +368,7 @@ class AutoPilotBlock(Block):
# cap rejected before ``create_session`` ran. Surface a
# message that points at the actionable cause rather
# than the empty transcript.
- raise RuntimeError(concurrent_turn_limit_message())
+ raise RuntimeError(running_turn_limit_message())
if outcome == "failed":
raise RuntimeError(
"AutoPilot turn failed — see the session's transcript"
diff --git a/autogpt_platform/backend/backend/copilot/active_turns.py b/autogpt_platform/backend/backend/copilot/active_turns.py
index f0a41177b0..0ac45d56b2 100644
--- a/autogpt_platform/backend/backend/copilot/active_turns.py
+++ b/autogpt_platform/backend/backend/copilot/active_turns.py
@@ -1,145 +1,139 @@
-"""Per-user concurrent AutoPilot turn tracking.
+"""Per-user concurrent AutoPilot turn tracking, backed entirely by Postgres.
-Caps how many copilot chat turns a single user can have running
-concurrently so a single API key cannot spawn hundreds of simultaneous
-turns and exhaust shared infrastructure.
-
-This module is the **domain wrapper** over the generic
-:func:`backend.data.redis_helpers.try_acquire_concurrency_slot` primitive
-— it supplies the per-user pool keying, the cap-from-Settings lookup,
-the user-facing error message, and the
-:func:`acquire_turn_slot` context manager that drives the slot's
-admit / release / refresh lifecycle.
+Each :class:`prisma.models.ChatSession` carries a ``chatStatus`` text
+enum: ``"idle"`` (no turn in flight, the 99% case), ``"queued"``
+(waiting for a running slot to free), ``"running"`` (a turn is being
+processed). The cap and queue queries are both ``count`` / ``find_many``
+on ``ChatSession`` by ``chatStatus``.
Public API
----------
-* :func:`acquire_turn_slot` — async context manager every entry point
- (HTTP route, ``run_sub_session`` tool, ``AutoPilotBlock``) wraps around
- the create-session + enqueue dance. Raises
- :class:`ConcurrentTurnLimitError` on rejection.
-* :func:`release_turn_slot` — invoked by ``mark_session_completed``
- when a turn ends, freeing the slot for the next admission.
-* :func:`get_concurrent_turn_limit` /
- :func:`concurrent_turn_limit_message` — operator-tunable cap and the
- matching user-facing 429 detail.
+* :func:`acquire_turn_slot` — async context manager. Counts the user's
+ ``"running"`` sessions, raises :class:`ConcurrentTurnLimitError` at
+ the cap, otherwise flips the session to ``"running"`` and yields a
+ handle whose release transfers to ``mark_session_completed`` via
+ :meth:`TurnSlot.keep`.
+* :func:`release_turn_slot` — flips the session back to ``"idle"``.
+ Called from ``mark_session_completed`` when the turn ends.
+* :func:`count_running_turns` / :func:`get_running_session_ids` —
+ 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.
+
+DB access goes through :func:`backend.data.db_accessors.chat_db` so
+the dispatcher works from both the HTTP server (Prisma directly) and
+the copilot_executor process (RPC via DatabaseManager).
"""
-import logging
-import time
from contextlib import asynccontextmanager
from typing import AsyncIterator
-from redis.exceptions import RedisClusterException, RedisError
-
-from backend.data.redis_client import get_redis_async
-from backend.data.redis_helpers import SlotAdmission, try_acquire_concurrency_slot
+from backend.copilot.model import (
+ CHAT_STATUS_IDLE,
+ CHAT_STATUS_QUEUED,
+ CHAT_STATUS_RUNNING,
+)
+from backend.data.db_accessors import chat_db
from backend.util.settings import Settings
-logger = logging.getLogger(__name__)
+# Upper bound on a single AutoPilot turn's wall-clock duration. Re-exported
+# for callers (e.g. ``backend.blocks.autopilot``) that need a sensible
+# upper-wait timeout. Stale running sessions older than this are an
+# operational concern surfaced via metrics + manual recovery, not
+# enforced at read time.
+MAX_TURN_LIFETIME_SECONDS = 6 * 60 * 60
-# Upper bound on a single AutoPilot turn's wall-clock duration. Beyond
-# this we treat the turn as abandoned: the slot is reclaimed by the
-# stale-cutoff sweep (so a crashed turn doesn't hold a slot forever) and
-# the :class:`AutoPilotBlock` execution wait gives up. Far exceeds typical
-# chat turn duration (seconds-minutes) so legitimate long-running tool
-# calls (E2B sandbox, deep web crawls, etc.) aren't penalised. The normal
-# release path is ``mark_session_completed``; this is the safety net.
-MAX_TURN_LIFETIME_SECONDS = 6 * 60 * 60
+def get_running_turn_limit() -> int:
+ """Configured soft cap on concurrently *running* turns per user."""
+ return Settings().config.max_running_copilot_turns_per_user
-_USER_ACTIVE_TURNS_KEY_PREFIX = "copilot:user_active_turns:"
+def get_inflight_turn_limit() -> int:
+ """Configured hard cap on in-flight (running + queued) turns per user."""
+ return Settings().config.max_inflight_copilot_turns_per_user
-def get_concurrent_turn_limit() -> int:
- """Resolve the configured per-user concurrent-turn cap at call time.
- Reading at call time (rather than module load) lets operators retune
- the cap by editing the env-backed Settings without redeploying the
- code that imports this module.
- """
- return Settings().config.max_concurrent_copilot_turns_per_user
+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
+ return (
+ f"You've reached the limit of {resolved} active tasks (running + queued). "
+ "Please wait for one of your current tasks to finish before starting a new one."
+ )
+
+
+def running_turn_limit_message(limit: int | None = None) -> str:
+ """Default :class:`ConcurrentTurnLimitError` detail when the
+ *running* cap is hit on a path that does not queue (e.g.
+ ``AutoPilotBlock``, ``run_sub_session``). The HTTP route catches
+ the error before it surfaces and replaces the message with the
+ inflight one."""
+ resolved = get_running_turn_limit() if limit is None else limit
+ return (
+ f"You have {resolved} AutoPilot tasks already running. "
+ "Please wait for one of them to finish before starting a new one."
+ )
-def concurrent_turn_limit_message(limit: int | None = None) -> str:
- """User-facing 429 detail string. Pass ``limit`` if you already
- resolved it; otherwise we read the configured value."""
- resolved = get_concurrent_turn_limit() if limit is None else limit
+def queued_turn_message() -> str:
+ """User-facing message rendered when a turn is queued instead of
+ starting immediately because the running cap is full."""
return (
- f"You've reached the limit of {resolved} active tasks. Please wait "
- f"for one of your current tasks to finish before starting a new one."
+ "Your task has been queued and will start automatically when one of "
+ "your current tasks finishes."
)
class ConcurrentTurnLimitError(Exception):
- """User has reached the configured concurrent in-flight AutoPilot
- turn cap. Maps to HTTP 429 in the API layer.
- """
+ """User has reached the configured running AutoPilot turn cap."""
def __init__(self, message: str | None = None) -> None:
- super().__init__(message or concurrent_turn_limit_message())
+ super().__init__(message or running_turn_limit_message())
-def _user_pool_key(user_id: str) -> str:
- # Hash-tag braces ensure all keys for a single user co-locate on the
- # same Redis Cluster slot — required for any future Lua that touches
- # multiple per-user keys atomically.
- return f"{_USER_ACTIVE_TURNS_KEY_PREFIX}{{{user_id}}}"
-
+async def count_running_turns(user_id: str) -> int:
+ """User's current running-turn count."""
+ return await chat_db().count_chat_sessions_by_status(
+ user_id=user_id, status=CHAT_STATUS_RUNNING
+ )
-async def _try_admit_user_turn(user_id: str, session_id: str) -> SlotAdmission:
- """Atomic admit/refresh against the user's active-turn pool.
- Fails open (returns ``ADMITTED``) on Redis errors so a brown-out
- doesn't 429 every user — the cap is a safeguard, not a budget.
- """
- try:
- redis = await get_redis_async()
- now = time.time()
- return await try_acquire_concurrency_slot(
- redis,
- pool_key=_user_pool_key(user_id),
- slot_id=session_id,
- capacity=get_concurrent_turn_limit(),
- score=now,
- stale_before_score=now - MAX_TURN_LIFETIME_SECONDS,
- ttl_seconds=MAX_TURN_LIFETIME_SECONDS,
- )
- except (RedisError, RedisClusterException, ConnectionError, OSError) as exc:
- logger.warning(
- "concurrent-turn cap: Redis unavailable for user=%s; failing open: %s",
- user_id,
- exc,
- )
- return SlotAdmission.ADMITTED
+async def get_running_session_ids(user_id: str) -> set[str]:
+ """Set of the user's session IDs currently running a turn."""
+ rows = await chat_db().list_chat_sessions_by_status(
+ user_id=user_id, status=CHAT_STATUS_RUNNING
+ )
+ return {r.session_id for r in rows}
async def release_turn_slot(user_id: str, session_id: str) -> None:
- """Free ``user_id``'s slot for ``session_id``. Idempotent.
-
- Best-effort — a Redis error only delays release until the next
- stale-cutoff sweep.
- """
- try:
- redis = await get_redis_async()
- await redis.zrem(_user_pool_key(user_id), session_id)
- except (RedisError, RedisClusterException, ConnectionError, OSError) as exc:
- logger.warning(
- "release_turn_slot: Redis unavailable for user=%s session=%s: %s",
- user_id,
- session_id,
- exc,
- )
+ """Flip the session back to ``"idle"``. Idempotent — the CAS on
+ ``chatStatus='running'`` is a no-op when the status has already
+ changed (e.g. a parallel cancel)."""
+ if not user_id:
+ return
+ await chat_db().update_chat_session_status(
+ session_id=session_id,
+ expect_status=CHAT_STATUS_RUNNING,
+ status=CHAT_STATUS_IDLE,
+ user_id=user_id,
+ )
class TurnSlot:
"""Handle yielded by :func:`acquire_turn_slot`.
Call :meth:`keep` once a turn has been successfully scheduled to
- transfer ownership to ``mark_session_completed`` (the release path).
- Without ``keep``, the context manager auto-releases on exit — but
- only when *this* caller admitted the slot. A re-entrant refresh
- leaves the slot alone, since some earlier caller still owns it.
+ transfer release ownership to ``mark_session_completed``. Without
+ ``keep``, the context manager auto-releases on exit — but only when
+ *this* caller admitted the slot.
"""
__slots__ = ("user_id", "session_id", "admitted", "_kept")
@@ -151,9 +145,7 @@ class TurnSlot:
self._kept = False
def keep(self) -> None:
- """Transfer slot ownership out of this context. Caller is now
- responsible for ensuring ``mark_session_completed`` releases the
- slot (or accepts the stale-cutoff fallback)."""
+ """Transfer slot ownership out of this context."""
self._kept = True
@@ -161,34 +153,66 @@ class TurnSlot:
async def acquire_turn_slot(
user_id: str | None,
session_id: str,
+ capacity: int | None = None,
) -> AsyncIterator[TurnSlot]:
"""Reserve a turn slot for the duration of the ``async with`` block.
Three branches on entry:
- * **Admitted** — fresh slot acquired; ``keep()`` transfers ownership
- to ``mark_session_completed``, otherwise the slot is released on
- exit.
- * **Refreshed** — same-``session_id`` re-entry (network retry,
- duplicate request); the existing slot's score is bumped but this
- caller does NOT own its release. Exiting without ``keep`` is a
- no-op.
- * **Rejected** — pool is at the configured cap; raises
- :class:`ConcurrentTurnLimitError` (caller maps to HTTP 429).
-
- Anonymous sessions (``user_id`` falsy) bypass the gate entirely and
- yield a no-op handle.
+ * **Admitted** — user below the cap; ``chatStatus`` flips to
+ ``"running"``. Exit auto-releases unless :meth:`TurnSlot.keep`
+ was called.
+ * **Refreshed** — same ``session_id`` is already ``"running"``
+ (network retry, duplicate request); status stays as-is and this
+ caller does NOT own the release.
+ * **Rejected** — at the cap; raises :class:`ConcurrentTurnLimitError`.
+
+ Anonymous sessions (``user_id`` falsy) bypass the cap entirely.
"""
handle = TurnSlot(user_id or "", session_id)
- if user_id:
- outcome = await _try_admit_user_turn(user_id, session_id)
- if outcome is SlotAdmission.REJECTED:
- raise ConcurrentTurnLimitError()
- if outcome is SlotAdmission.ADMITTED:
- handle.admitted = True
+ if not user_id:
+ yield handle
+ return
+
+ 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(
+ 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)
+ )
+ handle.admitted = True
+ else:
+ # CAS failed: session was not idle. Disambiguate by reading
+ # the current status — running (legitimate SSE-retry refresh)
+ # vs queued (this user already has a pending task for this
+ # session; the route must fall through to the queue path
+ # instead of double-dispatching).
+ current = await db.get_chat_session_status(session_id)
+ if current == CHAT_STATUS_QUEUED:
+ raise ConcurrentTurnLimitError(
+ running_turn_limit_message(resolved_capacity)
+ )
+ # Any other state (running, or unexpectedly idle from a
+ # parallel transition) is treated as refresh: no admit, no
+ # release ownership, no error. Caller's dispatch path is
+ # idempotent on duplicate message_ids via the ChatMessage PK.
try:
yield handle
finally:
if handle.admitted and not handle._kept:
- await release_turn_slot(handle.user_id, handle.session_id)
+ await release_turn_slot(user_id, session_id)
diff --git a/autogpt_platform/backend/backend/copilot/active_turns_test.py b/autogpt_platform/backend/backend/copilot/active_turns_test.py
index b86ad40173..84af6a548b 100644
--- a/autogpt_platform/backend/backend/copilot/active_turns_test.py
+++ b/autogpt_platform/backend/backend/copilot/active_turns_test.py
@@ -1,10 +1,15 @@
-"""Unit tests for active_turns: per-user concurrent AutoPilot turn tracking."""
+"""Unit tests for active_turns: per-user concurrent AutoPilot turn tracking.
-from unittest.mock import AsyncMock, MagicMock
+Backed by ``ChatSession.chatStatus`` accessed through ``chat_db()``;
+tests patch ``backend.copilot.active_turns.chat_db`` to return an
+:class:`unittest.mock.AsyncMock`.
+"""
+
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
-from backend.copilot import active_turns as active_turns_module
+from backend.copilot import active_turns
from backend.copilot.active_turns import (
ConcurrentTurnLimitError,
acquire_turn_slot,
@@ -12,174 +17,153 @@ from backend.copilot.active_turns import (
)
-def _patch_redis(monkeypatch: pytest.MonkeyPatch, redis_mock: MagicMock) -> None:
- monkeypatch.setattr(
- active_turns_module, "get_redis_async", AsyncMock(return_value=redis_mock)
- )
-
-
-def _redis_mock_eval(eval_return: int) -> MagicMock:
- """Build a mock Redis client whose ``eval`` returns the given Lua result.
-
- Lua return values match :class:`SlotAdmission`: 0=REJECTED, 1=ADMITTED, 2=REFRESHED.
+def _mock_db(
+ *,
+ admit_cas_ok: bool = True,
+ running_count: int = 0,
+ 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).
"""
- redis_mock = MagicMock()
- redis_mock.eval = AsyncMock(return_value=eval_return)
- redis_mock.zrem = AsyncMock(return_value=1)
- return redis_mock
+ 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.list_chat_sessions_by_status = AsyncMock(return_value=[])
+ db.get_chat_session_status = AsyncMock(return_value=current_status)
+ return db
# ── release_turn_slot ─────────────────────────────────────────────────
@pytest.mark.asyncio
-async def test_release_zrems_session_from_user_pool(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- redis_mock = MagicMock()
- redis_mock.zrem = AsyncMock(return_value=1)
- _patch_redis(monkeypatch, redis_mock)
-
- await release_turn_slot("user-1", "session-a")
- redis_mock.zrem.assert_called_once()
- args, _ = redis_mock.zrem.call_args
- assert args[1] == "session-a"
- assert "user-1" in args[0]
+async def test_release_flips_running_to_idle_with_userid_guard() -> None:
+ """``release_turn_slot`` calls ``update_chat_session_status`` with
+ a userId guard so a misrouted call can't release another user's row."""
+ db = _mock_db()
+ with patch.object(active_turns, "chat_db", return_value=db):
+ await release_turn_slot("user-1", "session-a")
+ db.update_chat_session_status.assert_awaited_once_with(
+ session_id="session-a",
+ expect_status="running",
+ status="idle",
+ user_id="user-1",
+ )
@pytest.mark.asyncio
-async def test_release_swallows_redis_errors(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- """Redis errors during release are logged, not raised — the slot
- will be swept on the next acquisition's stale-cutoff."""
- monkeypatch.setattr(
- active_turns_module,
- "get_redis_async",
- AsyncMock(side_effect=ConnectionError("down")),
- )
- await release_turn_slot("user-1", "session-a") # must not raise
+async def test_release_anonymous_user_is_noop() -> None:
+ """``release_turn_slot`` with empty user_id makes no DB write."""
+ db = _mock_db()
+ with patch.object(active_turns, "chat_db", return_value=db):
+ await release_turn_slot("", "session-a")
+ db.update_chat_session_status.assert_not_awaited()
# ── acquire_turn_slot lifecycle ───────────────────────────────────────
@pytest.mark.asyncio
-async def test_admitted_slot_releases_on_exit_without_keep(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
+async def test_admitted_slot_releases_on_exit_without_keep() -> None:
"""Forgetting ``keep()`` on a clean exit releases the slot."""
- redis_mock = _redis_mock_eval(1) # ADMITTED
- _patch_redis(monkeypatch, redis_mock)
-
- async with acquire_turn_slot("user-1", "session-a"):
- pass
-
- redis_mock.zrem.assert_called_once()
+ db = _mock_db(admit_cas_ok=True, running_count=1)
+ 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
@pytest.mark.asyncio
-async def test_admitted_slot_releases_on_exception(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
+async def test_admitted_slot_releases_on_exception() -> None:
"""An exception inside the with-block also releases the slot."""
- redis_mock = _redis_mock_eval(1) # ADMITTED
- _patch_redis(monkeypatch, redis_mock)
-
- with pytest.raises(RuntimeError, match="downstream blew up"):
- async with acquire_turn_slot("user-1", "session-a"):
- raise RuntimeError("downstream blew up")
-
- redis_mock.zrem.assert_called_once()
+ db = _mock_db(admit_cas_ok=True, running_count=1)
+ 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
@pytest.mark.asyncio
-async def test_kept_slot_is_not_released_on_exit(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- """``keep()`` transfers ownership; the context manager leaves the slot held."""
- redis_mock = _redis_mock_eval(1) # ADMITTED
- _patch_redis(monkeypatch, redis_mock)
-
- async with acquire_turn_slot("user-1", "session-a") as slot:
- slot.keep()
-
- redis_mock.zrem.assert_not_called()
+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)
+ 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
@pytest.mark.asyncio
-async def test_rejection_raises_concurrent_turn_limit_error(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- redis_mock = _redis_mock_eval(0) # REJECTED
- _patch_redis(monkeypatch, redis_mock)
-
- with pytest.raises(ConcurrentTurnLimitError):
- async with acquire_turn_slot("user-1", "session-a"):
- pytest.fail("body should not run when acquire fails") # pragma: no cover
-
- # Reject path never admitted, so it must not release.
- redis_mock.zrem.assert_not_called()
+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
+ 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
@pytest.mark.asyncio
-async def test_refreshed_slot_is_not_released_on_clean_exit(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- """Refreshed (re-entry) → caller doesn't own the slot, no release on exit."""
- redis_mock = _redis_mock_eval(2) # REFRESHED
- _patch_redis(monkeypatch, redis_mock)
-
- async with acquire_turn_slot("user-1", "session-a"):
- pass
-
- redis_mock.zrem.assert_not_called()
+async def test_queued_session_raises_so_caller_falls_through_to_queue() -> None:
+ """CAS failure + current status == 'queued' means the user already
+ 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")
+ with patch.object(active_turns, "chat_db", return_value=db):
+ with pytest.raises(ConcurrentTurnLimitError):
+ async with acquire_turn_slot("user-1", "session-a"):
+ pytest.fail("body must not run on rejection") # pragma: no cover
@pytest.mark.asyncio
-async def test_refreshed_slot_is_not_released_on_exception(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- """Same-session retry's failure must NOT tear down the original turn's slot."""
- redis_mock = _redis_mock_eval(2) # REFRESHED
- _patch_redis(monkeypatch, redis_mock)
-
- with pytest.raises(RuntimeError, match="boom"):
+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")
+ with patch.object(active_turns, "chat_db", return_value=db):
async with acquire_turn_slot("user-1", "session-a"):
- raise RuntimeError("boom")
-
- redis_mock.zrem.assert_not_called()
+ pass
+ # Only the CAS attempt fires; nothing else.
+ assert db.update_chat_session_status.await_count == 1
@pytest.mark.asyncio
-async def test_anonymous_user_skips_gate(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- """``user_id`` falsy → no slot acquired, no Redis touched, no exception."""
- redis_mock = _redis_mock_eval(0) # would reject if hit
- _patch_redis(monkeypatch, redis_mock)
-
- async with acquire_turn_slot(None, "session-a"):
- pass
-
- redis_mock.eval.assert_not_called()
- redis_mock.zrem.assert_not_called()
+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")
+ 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
@pytest.mark.asyncio
-async def test_redis_brownout_fails_open_admitting_the_turn(
- monkeypatch: pytest.MonkeyPatch,
-) -> None:
- """Cap is a safeguard, not a budget — a Redis brown-out should not
- 429 every user. The context manager admits the turn and continues."""
- monkeypatch.setattr(
- active_turns_module,
- "get_redis_async",
- AsyncMock(side_effect=ConnectionError("down")),
- )
-
- async with acquire_turn_slot("user-1", "session-a") as slot:
- slot.keep() # body runs as if admitted; pretend a turn ran
+async def test_anonymous_user_skips_gate() -> None:
+ """``user_id`` falsy → no DB query, no exception."""
+ db = _mock_db()
+ 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()
# ── default cap pinning ───────────────────────────────────────────────
@@ -191,4 +175,4 @@ def test_schema_default_concurrent_turn_limit_is_15() -> None:
override (e.g. lower cap for development) doesn't break the test."""
from backend.util.settings import Config
- assert Config.model_fields["max_concurrent_copilot_turns_per_user"].default == 15
+ assert Config.model_fields["max_inflight_copilot_turns_per_user"].default == 15
diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.py
index 967cca982c..42df6a7c16 100644
--- a/autogpt_platform/backend/backend/copilot/db.py
+++ b/autogpt_platform/backend/backend/copilot/db.py
@@ -338,8 +338,18 @@ async def add_chat_message(
refusal: str | None = None,
tool_calls: list[dict[str, Any]] | None = None,
function_call: dict[str, Any] | None = None,
+ *,
+ message_id: str | None = None,
+ metadata: dict[str, Any] | None = None,
) -> ChatMessage:
- """Add a message to a chat session."""
+ """Add a message to a chat session.
+
+ Pass ``message_id`` to use an explicit PK (e.g. when the queue
+ layer needs to reference the row before promotion) — otherwise
+ Prisma generates one. ``metadata`` is a generic per-row JSONB bag
+ used by the queue path to stash dispatcher payload on the user
+ row that triggered a queued turn.
+ """
# Build ChatMessageCreateInput with only non-None values
# (Prisma TypedDict rejects optional fields set to None)
data: ChatMessageCreateInput = {
@@ -348,6 +358,9 @@ async def add_chat_message(
"sequence": sequence,
}
+ if message_id is not None:
+ data["id"] = message_id
+
# Add optional string fields — sanitize to strip PostgreSQL-incompatible
# control characters (null bytes etc.) that may appear in tool outputs.
if content is not None:
@@ -364,6 +377,8 @@ async def add_chat_message(
data["toolCalls"] = SafeJson(tool_calls)
if function_call is not None:
data["functionCall"] = SafeJson(function_call)
+ if metadata is not None:
+ data["metadata"] = SafeJson(metadata)
# Run message create and session timestamp update in parallel for lower latency
_, message = await asyncio.gather(
@@ -693,3 +708,83 @@ async def set_turn_duration(session_id: str, duration_ms: int) -> None:
msg.duration_ms = duration_ms
break
await cache_chat_session(session)
+
+
+# Session lifecycle primitives. Callers pass the ``CHAT_STATUS_*``
+# constants from ``model``. Counting and listing by ``chatStatus``
+# covers both the per-user soft running cap (status='running') and the
+# cross-session queue (status='queued'). Adding a new state is a
+# code-only change at call sites.
+
+
+async def count_chat_sessions_by_status(*, user_id: str, status: str) -> int:
+ """Count user's ChatSession rows whose ``chatStatus`` matches."""
+ return await PrismaChatSession.prisma().count(
+ where={"userId": user_id, "chatStatus": status},
+ )
+
+
+async def list_chat_sessions_by_status(
+ *, user_id: str, status: str
+) -> list[ChatSessionInfo]:
+ """User's ChatSession rows with the given status, oldest-first.
+
+ Returns application-model :class:`ChatSessionInfo` instances so the
+ function is RPC-safe — ``mark_session_completed`` invokes the
+ dispatcher from the CoPilotExecutor subprocess (no direct Prisma
+ connection), and ``DatabaseManagerAsyncClient``'s response
+ serializer rejects raw Prisma rows."""
+ rows = await PrismaChatSession.prisma().find_many(
+ where={"userId": user_id, "chatStatus": status},
+ order={"updatedAt": "asc"},
+ )
+ return [ChatSessionInfo.from_db(r) for r in rows]
+
+
+async def get_latest_user_message_in_session(
+ session_id: str,
+) -> ChatMessage | None:
+ """Return the most recent ``role='user'`` ChatMessage in the given
+ session, or ``None`` if there are none. Used by the queue dispatcher
+ to recover the submit-time payload from ``metadata`` when promoting
+ a queued session."""
+ row = await PrismaChatMessage.prisma().find_first(
+ where={"sessionId": session_id, "role": "user"},
+ order={"sequence": "desc"},
+ )
+ return ChatMessage.from_db(row) if row else None
+
+
+async def get_chat_session_status(session_id: str) -> str | None:
+ """Return ``ChatSession.chatStatus`` for ``session_id``, or ``None``
+ if the row doesn't exist. Used by in-flight gates that need to
+ distinguish queued from running (the CAS-only ``update`` path can't
+ tell them apart on its own)."""
+ row = await PrismaChatSession.prisma().find_unique(where={"id": session_id})
+ return row.chatStatus if row else None
+
+
+async def update_chat_session_status(
+ *,
+ session_id: str,
+ status: str,
+ expect_status: str | None = None,
+ user_id: str | None = None,
+) -> bool:
+ """Update ``ChatSession.chatStatus`` to ``status``.
+
+ Pass ``expect_status`` to CAS-gate the update on the current
+ value — required for race-safe cancel/claim/restore (so two
+ concurrent transitions can't both win). Pass ``user_id`` to
+ enforce ownership in the same statement (used by the user-initiated
+ cancel path). Returns True iff the row was matched and updated.
+ """
+ where: ChatSessionWhereInput = {"id": session_id}
+ if expect_status is not None:
+ where["chatStatus"] = expect_status
+ if user_id is not None:
+ where["userId"] = user_id
+ updated = await PrismaChatSession.prisma().update_many(
+ where=where, data={"chatStatus": status}
+ )
+ return updated > 0
diff --git a/autogpt_platform/backend/backend/copilot/db_test.py b/autogpt_platform/backend/backend/copilot/db_test.py
index d86fb43cba..e823113181 100644
--- a/autogpt_platform/backend/backend/copilot/db_test.py
+++ b/autogpt_platform/backend/backend/copilot/db_test.py
@@ -61,6 +61,7 @@ def _make_session(
totalCompletionTokens=0,
title=None,
metadata={},
+ chatStatus="idle",
Messages=messages or [],
)
return session
@@ -615,3 +616,175 @@ async def test_update_message_content_by_sequence_sanitizes_content():
# ``get_chat_messages_paginated`` directly — see ``model._get_session_from_db``.
# Cap-hit + tool-pair boundary behaviour is now covered by the paginated tests
# above and the integration coverage in ``model_test.py``.
+
+
+# ChatSession lifecycle primitives + add_chat_message.
+
+
+async def test_count_chat_sessions_by_status_filters_by_user_and_status() -> None:
+ from backend.copilot.db import count_chat_sessions_by_status
+
+ count = AsyncMock(return_value=3)
+ with patch.object(PrismaChatSession, "prisma", return_value=AsyncMock(count=count)):
+ result = await count_chat_sessions_by_status(user_id="u1", status="running")
+ assert result == 3
+ where = count.call_args.kwargs["where"]
+ assert where == {"userId": "u1", "chatStatus": "running"}
+
+
+async def test_list_chat_sessions_by_status_returns_app_models_oldest_first() -> None:
+ """Returns RPC-safe ``ChatSessionInfo`` rows (not raw Prisma) so the
+ function can serve the dispatcher across the DatabaseManager RPC
+ boundary from the CoPilotExecutor subprocess."""
+ from backend.copilot.db import list_chat_sessions_by_status
+
+ now = datetime.now(UTC)
+ rows = [
+ PrismaChatSession(
+ id="s1",
+ userId="u1",
+ chatStatus="queued",
+ createdAt=now,
+ updatedAt=now,
+ credentials="{}",
+ successfulAgentRuns="{}",
+ successfulAgentSchedules="{}",
+ metadata="{}",
+ totalPromptTokens=0,
+ totalCompletionTokens=0,
+ ),
+ PrismaChatSession(
+ id="s2",
+ userId="u1",
+ chatStatus="queued",
+ createdAt=now,
+ updatedAt=now,
+ credentials="{}",
+ successfulAgentRuns="{}",
+ successfulAgentSchedules="{}",
+ metadata="{}",
+ totalPromptTokens=0,
+ totalCompletionTokens=0,
+ ),
+ ]
+ find_many = AsyncMock(return_value=rows)
+ with patch.object(
+ PrismaChatSession, "prisma", return_value=AsyncMock(find_many=find_many)
+ ):
+ result = await list_chat_sessions_by_status(user_id="u1", status="queued")
+ assert [r.session_id for r in result] == ["s1", "s2"]
+ assert all(r.chat_status == "queued" for r in result)
+ kwargs = find_many.call_args.kwargs
+ assert kwargs["where"] == {"userId": "u1", "chatStatus": "queued"}
+ assert kwargs["order"] == {"updatedAt": "asc"}
+
+
+async def test_update_chat_session_status_owner_gated_returns_true_on_match() -> None:
+ """User-initiated transition (cancel) gates on both ``chatStatus``
+ AND ``userId`` — both guards in one atomic update."""
+ from backend.copilot.db import update_chat_session_status
+
+ update_many = AsyncMock(return_value=1)
+ with patch.object(
+ PrismaChatSession, "prisma", return_value=AsyncMock(update_many=update_many)
+ ):
+ ok = await update_chat_session_status(
+ session_id="s1",
+ expect_status="queued",
+ status="idle",
+ user_id="u1",
+ )
+ assert ok is True
+ kwargs = update_many.call_args.kwargs
+ assert kwargs["where"] == {"id": "s1", "chatStatus": "queued", "userId": "u1"}
+ assert kwargs["data"] == {"chatStatus": "idle"}
+
+
+async def test_update_chat_session_status_returns_false_when_cas_fails() -> None:
+ from backend.copilot.db import update_chat_session_status
+
+ update_many = AsyncMock(return_value=0)
+ with patch.object(
+ PrismaChatSession, "prisma", return_value=AsyncMock(update_many=update_many)
+ ):
+ ok = await update_chat_session_status(
+ session_id="s1", expect_status="queued", status="idle"
+ )
+ assert ok is False
+
+
+async def test_update_chat_session_status_dispatcher_path_omits_user_gate() -> None:
+ """Dispatcher-initiated transitions (claim / restore / release) don't
+ pass ``user_id`` — they act on the session regardless of owner."""
+ from backend.copilot.db import update_chat_session_status
+
+ update_many = AsyncMock(return_value=1)
+ with patch.object(
+ PrismaChatSession, "prisma", return_value=AsyncMock(update_many=update_many)
+ ):
+ await update_chat_session_status(
+ session_id="s1", expect_status="queued", status="running"
+ )
+ where = update_many.call_args.kwargs["where"]
+ assert "userId" not in where
+ assert where == {"id": "s1", "chatStatus": "queued"}
+
+
+async def test_add_chat_message_serialises_metadata_via_safejson() -> None:
+ from backend.copilot.db import add_chat_message
+
+ create = AsyncMock(return_value=_make_msg(sequence=1))
+ session_update = AsyncMock()
+ with (
+ patch.object(
+ PrismaChatMessage, "prisma", return_value=AsyncMock(create=create)
+ ),
+ patch.object(
+ PrismaChatSession, "prisma", return_value=AsyncMock(update=session_update)
+ ),
+ ):
+ await add_chat_message(
+ message_id="m1",
+ session_id="s1",
+ role="user",
+ content="hi",
+ sequence=1,
+ metadata={"mode": "extended_thinking"},
+ )
+ data = create.call_args.kwargs["data"]
+ assert data["id"] == "m1"
+ metadata = data["metadata"]
+ inner = getattr(metadata, "data", metadata)
+ assert inner == {"mode": "extended_thinking"}
+
+
+async def test_add_chat_message_omits_metadata_when_none() -> None:
+ from backend.copilot.db import add_chat_message
+
+ create = AsyncMock(return_value=_make_msg(sequence=1))
+ session_update = AsyncMock()
+ with (
+ patch.object(
+ PrismaChatMessage, "prisma", return_value=AsyncMock(create=create)
+ ),
+ patch.object(
+ PrismaChatSession, "prisma", return_value=AsyncMock(update=session_update)
+ ),
+ ):
+ await add_chat_message(
+ message_id="m1",
+ session_id="s1",
+ role="user",
+ content="hi",
+ sequence=1,
+ metadata=None,
+ )
+ data = create.call_args.kwargs["data"]
+ assert "metadata" not in data
diff --git a/autogpt_platform/backend/backend/copilot/executor/utils.py b/autogpt_platform/backend/backend/copilot/executor/utils.py
index 71cae3eced..1a4c96b3bc 100644
--- a/autogpt_platform/backend/backend/copilot/executor/utils.py
+++ b/autogpt_platform/backend/backend/copilot/executor/utils.py
@@ -9,7 +9,13 @@ import logging
from pydantic import BaseModel
-from backend.copilot.active_turns import TurnSlot, acquire_turn_slot
+from backend.copilot.active_turns import (
+ ConcurrentTurnLimitError,
+ TurnSlot,
+ acquire_turn_slot,
+ get_inflight_turn_limit,
+ inflight_turn_limit_message,
+)
from backend.copilot.config import CopilotLlmModel, CopilotMode
from backend.copilot.permissions import CopilotPermissions
from backend.data.rabbitmq import Exchange, ExchangeType, Queue, RabbitMQConfig
@@ -316,8 +322,28 @@ async def schedule_turn(
a RabbitMQ blip cannot leak a slot until the stale-cutoff sweep.
* On success the slot is *kept*: ownership transfers to
``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.
+
+ The user's queued turns from the chat HTTP route DO count against
+ the inflight cap — pre-check ``running + queued`` here so a user
+ with 5 running + 10 queued can't slip a 16th task through this
+ path; ``acquire_turn_slot`` only sees the running pool.
"""
- async with acquire_turn_slot(user_id, session_id) as slot:
+ if user_id:
+ from backend.copilot.turn_queue import count_inflight_turns
+
+ inflight_cap = get_inflight_turn_limit()
+ 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:
await dispatch_turn(
slot,
session_id=session_id,
@@ -379,20 +405,42 @@ async def dispatch_turn(
tool_name=tool_name,
turn_id=turn_id,
)
- await enqueue_copilot_turn(
- session_id=session_id,
- user_id=user_id,
- message=message,
- turn_id=turn_id,
- is_user_message=is_user_message,
- context=context,
- file_ids=file_ids,
- mode=mode,
- model=model,
- permissions=permissions,
- request_arrival_at=request_arrival_at,
- )
- slot.keep()
+
+ # Once ``create_session`` has written Redis meta, EVERY exit path
+ # from this point on must either (a) commit the turn (``slot.keep()``
+ # + RabbitMQ message enqueued) or (b) tear the Redis meta down — or
+ # a future read will see ``status='running'`` with no consumer.
+ # ``finally`` catches CancelledError as well (which ``except
+ # Exception`` would miss); the ``committed`` flag distinguishes the
+ # happy path from any failure / cancellation.
+ committed = False
+ try:
+ await enqueue_copilot_turn(
+ session_id=session_id,
+ user_id=user_id,
+ message=message,
+ turn_id=turn_id,
+ is_user_message=is_user_message,
+ context=context,
+ file_ids=file_ids,
+ mode=mode,
+ model=model,
+ permissions=permissions,
+ request_arrival_at=request_arrival_at,
+ )
+ slot.keep()
+ committed = True
+ finally:
+ if not committed:
+ try:
+ await stream_registry.delete_session_meta(session_id)
+ except BaseException:
+ # Already in a failure path — log + swallow so the
+ # original exception/cancellation isn't masked.
+ logger.exception(
+ "dispatch_turn: redis meta cleanup failed for session=%s",
+ session_id,
+ )
async def schedule_chat_turn(
diff --git a/autogpt_platform/backend/backend/copilot/model.py b/autogpt_platform/backend/backend/copilot/model.py
index f02a157ce8..04ce7226f3 100644
--- a/autogpt_platform/backend/backend/copilot/model.py
+++ b/autogpt_platform/backend/backend/copilot/model.py
@@ -44,6 +44,15 @@ def _get_session_cache_key(session_id: str) -> str:
return f"{CHAT_SESSION_CACHE_PREFIX}{session_id}"
+# ChatSession.chatStatus lifecycle values. Open enum: future states
+# (e.g. ``"errored"``, ``"paused"``) can be added without a migration.
+# Within a session, turns are serialized so at most one task is in
+# flight at a time — the status sits at session scope, not message.
+CHAT_STATUS_IDLE = "idle"
+CHAT_STATUS_QUEUED = "queued"
+CHAT_STATUS_RUNNING = "running"
+
+
# ===================== Chat data models ===================== #
@@ -83,6 +92,15 @@ class ChatMessage(BaseModel):
duration_ms: int | None = None
created_at: datetime | None = None
+ # Owning session id and generic per-row JSONB bag. Today the
+ # dispatcher uses ``metadata`` to preserve the submit-time payload
+ # (file_ids / mode / model / permissions / context / arrival_at)
+ # on the user row that triggered a queued turn, so a later
+ # promotion can replay the turn faithfully. Generic so future
+ # per-row state can land here without a migration.
+ session_id: str | None = None
+ metadata: dict | None = None
+
@staticmethod
def from_db(prisma_message: PrismaChatMessage) -> "ChatMessage":
"""Convert a Prisma ChatMessage to a Pydantic ChatMessage."""
@@ -98,6 +116,8 @@ class ChatMessage(BaseModel):
sequence=prisma_message.sequence,
duration_ms=prisma_message.durationMs,
created_at=prisma_message.createdAt,
+ session_id=prisma_message.sessionId,
+ metadata=_parse_json_field(prisma_message.metadata),
)
@@ -164,6 +184,8 @@ class ChatSessionInfo(BaseModel):
successful_agent_runs: dict[str, int] = {}
successful_agent_schedules: dict[str, int] = {}
metadata: ChatSessionMetadata = ChatSessionMetadata()
+ # Session lifecycle: "idle" | "queued" | "running" (see CHAT_STATUS_*).
+ chat_status: str = "idle"
@property
def dry_run(self) -> bool:
@@ -212,6 +234,7 @@ class ChatSessionInfo(BaseModel):
successful_agent_runs=successful_agent_runs,
successful_agent_schedules=successful_agent_schedules,
metadata=metadata,
+ chat_status=prisma_session.chatStatus,
)
diff --git a/autogpt_platform/backend/backend/copilot/pending_message_helpers.py b/autogpt_platform/backend/backend/copilot/pending_message_helpers.py
index ca901ae216..bc1c9b18a1 100644
--- a/autogpt_platform/backend/backend/copilot/pending_message_helpers.py
+++ b/autogpt_platform/backend/backend/copilot/pending_message_helpers.py
@@ -14,7 +14,12 @@ from fastapi import HTTPException
from pydantic import BaseModel
from redis.exceptions import RedisClusterException, RedisError
-from backend.copilot.model import ChatMessage, upsert_chat_session
+from backend.copilot.model import (
+ CHAT_STATUS_QUEUED,
+ CHAT_STATUS_RUNNING,
+ ChatMessage,
+ upsert_chat_session,
+)
from backend.copilot.pending_messages import (
MAX_PENDING_MESSAGES,
PendingMessage,
@@ -26,6 +31,7 @@ from backend.copilot.pending_messages import (
)
from backend.copilot.stream_registry import get_session as get_active_session_meta
from backend.copilot.stream_registry import get_session_meta_key
+from backend.data.db_accessors import chat_db
from backend.data.redis_client import get_redis_async
from backend.data.redis_helpers import incr_with_ttl
from backend.data.workspace import resolve_workspace_files
@@ -56,12 +62,21 @@ class StreamRegistryUnavailable(Exception):
async def is_turn_in_flight(session_id: str) -> bool:
- """Return ``True`` when a copilot turn is actively running for *session_id*.
+ """Return ``True`` when a copilot turn is actively running OR
+ waiting in the cross-session queue for *session_id*.
Used by the HTTP pending-message endpoint and the autopilot block so a
- second message arriving while an earlier turn is still executing gets
- queued into the pending buffer instead of racing the in-flight turn on
- the cluster lock.
+ second message arriving while an earlier turn is still executing (or
+ waiting in queue) gets buffered via ``queue_pending_for_http``
+ instead of racing the in-flight turn on the cluster lock.
+
+ Two sources are consulted:
+
+ * Redis stream registry (``status='running'``) — catches actively
+ streaming turns.
+ * ``ChatSession.chatStatus`` — catches both ``"running"`` and
+ ``"queued"`` so a session whose turn hasn't been claimed by the
+ dispatcher yet is still treated as in flight.
Raises :class:`StreamRegistryUnavailable` when the underlying Redis
lookup fails. The HTTP layer must catch this and map it to 503 — letting
@@ -80,7 +95,24 @@ async def is_turn_in_flight(session_id: str) -> bool:
exc,
)
raise StreamRegistryUnavailable() from exc
- return active is not None and active.status == "running"
+ if active is not None and active.status == "running":
+ return True
+ # Fall through to ChatSession.chatStatus: catches the queued case the
+ # Redis registry doesn't know about (dispatcher hasn't claimed yet).
+ # If the DB is unavailable we can't tell whether the session is
+ # queued or running — fail-closed the same way the Redis branch
+ # above does so the HTTP layer maps the unknown state to 503 with a
+ # Retry-After header instead of a raw 500.
+ try:
+ chat_status = await chat_db().get_chat_session_status(session_id)
+ except Exception as exc:
+ logger.warning(
+ "is_turn_in_flight: chat_status lookup failed for session=%s: %s",
+ session_id,
+ exc,
+ )
+ raise StreamRegistryUnavailable() from exc
+ return chat_status in (CHAT_STATUS_QUEUED, CHAT_STATUS_RUNNING)
class QueuePendingMessageResponse(BaseModel):
diff --git a/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py b/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py
index 915a1c4ce6..95f81e2272 100644
--- a/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py
+++ b/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py
@@ -54,18 +54,45 @@ async def test_check_pending_call_rate_fails_open_on_redis_error(
# ── is_turn_in_flight: fail-closed on Redis errors ────────────────────
+def _mock_chat_db(
+ monkeypatch: pytest.MonkeyPatch, *, status: str | None = "idle"
+) -> None:
+ """Stub ``chat_db()`` so the ``is_turn_in_flight`` fallthrough that
+ reads ``ChatSession.chatStatus`` doesn't hit a real DB connection."""
+ db = MagicMock()
+ db.get_chat_session_status = AsyncMock(return_value=status)
+ monkeypatch.setattr(helpers_module, "chat_db", lambda: db)
+
+
@pytest.mark.asyncio
async def test_is_turn_in_flight_returns_false_when_no_active_session(
monkeypatch: pytest.MonkeyPatch,
) -> None:
+ """Redis says no active stream AND ChatSession is idle → not in flight."""
monkeypatch.setattr(
helpers_module,
"get_active_session_meta",
AsyncMock(return_value=None),
)
+ _mock_chat_db(monkeypatch, status="idle")
assert await is_turn_in_flight("sess-1") is False
+async def test_is_turn_in_flight_returns_true_when_queued(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Queued sessions are also in flight even though the Redis stream
+ registry has no entry — the dispatcher hasn't claimed the row yet."""
+ monkeypatch.setattr(
+ helpers_module,
+ "get_active_session_meta",
+ AsyncMock(return_value=None),
+ )
+ _mock_chat_db(monkeypatch, status="queued")
+ assert await is_turn_in_flight("sess-1") is True
+
+
@pytest.mark.asyncio
async def test_is_turn_in_flight_returns_true_when_running(
monkeypatch: pytest.MonkeyPatch,
@@ -77,6 +104,7 @@ async def test_is_turn_in_flight_returns_true_when_running(
"get_active_session_meta",
AsyncMock(return_value=active),
)
+ _mock_chat_db(monkeypatch, status="running")
assert await is_turn_in_flight("sess-1") is True
@@ -115,6 +143,27 @@ async def test_is_turn_in_flight_raises_on_redis_cluster_exception(
await is_turn_in_flight("sess-1")
+async def test_is_turn_in_flight_raises_when_chat_status_lookup_fails(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """DB-side ``get_chat_session_status`` failure must surface as the
+ same typed ``StreamRegistryUnavailable`` the Redis branch raises so
+ the HTTP layer's existing 503 + Retry-After mapping covers it.
+ Without this the Prisma error would bubble as a raw 500."""
+ monkeypatch.setattr(
+ helpers_module,
+ "get_active_session_meta",
+ AsyncMock(return_value=None),
+ )
+ db = MagicMock()
+ db.get_chat_session_status = AsyncMock(side_effect=RuntimeError("db down"))
+ monkeypatch.setattr(helpers_module, "chat_db", lambda: db)
+
+ with pytest.raises(StreamRegistryUnavailable):
+ await is_turn_in_flight("sess-1")
+
+
# ── queue_pending_for_http: gate-then-bump ordering ───────────────────
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 7d63181300..4631c72b50 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py
@@ -11,10 +11,11 @@ Focuses on the queue-on-busy fallback:
fresh dispatch.
"""
-from unittest.mock import AsyncMock, patch
+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
_QR = type(
@@ -135,12 +136,24 @@ async def test_idle_session_enqueues_normally():
create_session = AsyncMock()
enqueue = AsyncMock()
wait_result = AsyncMock(return_value=("completed", SessionResult()))
+ 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.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")
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=0),
+ ),
+ patch.object(active_turns, "chat_db", return_value=idle_db),
patch(
"backend.copilot.sdk.session_waiter.stream_registry.create_session",
new=create_session,
@@ -190,6 +203,10 @@ async def test_idle_session_concurrent_turn_cap_returns_rejected_outcome():
"backend.copilot.executor.utils.acquire_turn_slot",
side_effect=ConcurrentTurnLimitError(),
),
+ patch(
+ "backend.copilot.turn_queue.count_inflight_turns",
+ new=AsyncMock(return_value=0),
+ ),
patch(
"backend.copilot.sdk.session_waiter.stream_registry.create_session",
new=create_session,
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry.py b/autogpt_platform/backend/backend/copilot/stream_registry.py
index cc536821df..c79f6ee78b 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry.py
@@ -27,6 +27,7 @@ from redis.exceptions import RedisError
from backend.api.model import CopilotCompletionPayload
from backend.copilot.active_turns import release_turn_slot
+from backend.copilot.turn_queue import dispatch_next_for_user
from backend.data.db_accessors import chat_db
from backend.data.notification_bus import (
AsyncRedisNotificationEventBus,
@@ -238,6 +239,27 @@ Used by `publish_chunk` to avoid refreshing on every single chunk
_META_TTL_REFRESH_INTERVAL = 60 # seconds
+async def delete_session_meta(session_id: str) -> None:
+ """Delete a session's Redis meta entry — used by the dispatcher's
+ rollback path when ``create_session`` succeeded but the subsequent
+ RabbitMQ enqueue failed. Without this, the session sits with
+ ``status='running'`` in Redis until TTL and ``is_turn_in_flight``
+ keeps reporting True even though no executor will pick the turn up.
+
+ Best-effort: a Redis error here only delays the cleanup to TTL
+ expiry, which is the same window we had before this helper existed.
+ """
+ try:
+ redis = await get_redis_async()
+ await redis.delete(_get_session_meta_key(session_id))
+ except RedisError as exc:
+ logger.warning(
+ "delete_session_meta: redis cleanup failed for session=%s: %s",
+ session_id,
+ exc,
+ )
+
+
async def publish_chunk(
turn_id: str,
chunk: StreamBaseResponse,
@@ -895,6 +917,22 @@ async def mark_session_completed(
except RedisError as e:
logger.warning(f"Failed to release stream lock for session {session_id}: {e}")
+ # Promote the user's oldest queued turn (if any) AFTER the executor
+ # cluster lock and SDK stream lock are cleared — otherwise the
+ # promoted turn races against the just-finished turn's stale locks
+ # and stalls until TTL. Best-effort: dispatcher errors are logged
+ # inside turn_queue; we never let a queue hiccup break the
+ # completion path.
+ if user_id:
+ try:
+ await dispatch_next_for_user(user_id)
+ except Exception as exc:
+ logger.warning(
+ "queue dispatch after session=%s completion failed: %s",
+ session_id,
+ exc,
+ )
+
if error_message and not skip_error_publish:
try:
await publish_chunk(turn_id, StreamError(errorText=error_message))
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry_test.py b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
index e45f96bfd7..965f979cea 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry_test.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
@@ -287,6 +287,75 @@ async def test_mark_session_completed_releases_cluster_lock_on_success():
assert f"{STREAM_LOCK_PREFIX}sess-1" in fake_redis.deleted_keys
+async def test_mark_session_completed_fires_user_queue_dispatcher():
+ """When a turn completes for ``user_id=u1`` (read from the session's
+ Redis meta), the slot-free hook must invoke
+ ``dispatch_next_for_user('u1')`` so the user's next queued session
+ can be promoted. Per-user scoping: we don't dispatch for other
+ users (no global broadcast)."""
+ fake_redis = _FakeRedis({"status": "running", "turn_id": "turn-1", "user_id": "u1"})
+ dispatch_mock = AsyncMock(return_value=True)
+
+ with (
+ patch.object(
+ stream_registry, "get_redis_async", new=AsyncMock(return_value=fake_redis)
+ ),
+ patch.object(
+ stream_registry, "hash_compare_and_set", new=AsyncMock(return_value=True)
+ ),
+ patch.object(stream_registry, "publish_chunk", new=AsyncMock()),
+ patch.object(
+ stream_registry.chat_db(),
+ "set_turn_duration",
+ new=AsyncMock(),
+ create=True,
+ ),
+ patch.object(stream_registry, "release_turn_slot", new=AsyncMock()),
+ patch(
+ "backend.copilot.stream_registry.dispatch_next_for_user",
+ new=dispatch_mock,
+ ),
+ ):
+ await stream_registry.mark_session_completed("sess-1")
+
+ dispatch_mock.assert_awaited_once_with("u1")
+
+
+async def test_mark_session_completed_swallows_dispatcher_errors():
+ """The dispatcher firing after slot-free must not be able to break
+ the completion path — any exception from ``dispatch_next_for_user``
+ is logged and swallowed so a queue hiccup never blocks the turn's
+ cleanup."""
+ fake_redis = _FakeRedis({"status": "running", "turn_id": "turn-1", "user_id": "u1"})
+
+ with (
+ patch.object(
+ stream_registry, "get_redis_async", new=AsyncMock(return_value=fake_redis)
+ ),
+ patch.object(
+ stream_registry, "hash_compare_and_set", new=AsyncMock(return_value=True)
+ ),
+ patch.object(stream_registry, "publish_chunk", new=AsyncMock()),
+ patch.object(
+ stream_registry.chat_db(),
+ "set_turn_duration",
+ new=AsyncMock(),
+ create=True,
+ ),
+ patch.object(stream_registry, "release_turn_slot", new=AsyncMock()),
+ patch(
+ "backend.copilot.stream_registry.dispatch_next_for_user",
+ new=AsyncMock(side_effect=RuntimeError("queue brown-out")),
+ ),
+ ):
+ # No exception should bubble.
+ result = await stream_registry.mark_session_completed("sess-1")
+
+ assert result is True
+
+
@pytest.mark.asyncio
async def test_mark_session_completed_skips_lock_release_when_already_completed():
"""CAS failure = someone else completed the session first; we must not
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 81c94fa3f7..f0c2015dcc 100644
--- a/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
+++ b/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
@@ -27,7 +27,7 @@ import logging
import time
from typing import Any
-from backend.copilot.active_turns import concurrent_turn_limit_message
+from backend.copilot.active_turns import running_turn_limit_message
from backend.copilot.constants import MAX_TOOL_WAIT_SECONDS
from backend.copilot.context import get_current_permissions
from backend.copilot.model import ChatSession, create_chat_session, get_chat_session
@@ -240,7 +240,7 @@ def response_from_outcome(
# Render the actionable message instead of a "see transcript"
# pointer to nothing.
return SubSessionStatusResponse(
- message=concurrent_turn_limit_message(),
+ message=running_turn_limit_message(),
session_id=parent_session_id,
status="error",
sub_session_id=inner_session_id,
diff --git a/autogpt_platform/backend/backend/copilot/transcript.py b/autogpt_platform/backend/backend/copilot/transcript.py
index b2333ea858..11fc01d7af 100644
--- a/autogpt_platform/backend/backend/copilot/transcript.py
+++ b/autogpt_platform/backend/backend/copilot/transcript.py
@@ -1032,6 +1032,10 @@ async def extract_context_messages(
current user turn at ``session_messages[-1]`` is excluded — callers append
it themselves.
"""
+ # Drop reasoning rows — their content lives in transcript paths,
+ # not the public turn list. Queue lifecycle is tracked on the
+ # owning ChatSession, not per message, so no per-row filtering
+ # for queued/running rows is needed here.
session_messages = [m for m in session_messages if m.role != "reasoning"]
prior = session_messages[:-1]
diff --git a/autogpt_platform/backend/backend/copilot/turn_queue.py b/autogpt_platform/backend/backend/copilot/turn_queue.py
new file mode 100644
index 0000000000..ae2afc2e30
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/turn_queue.py
@@ -0,0 +1,368 @@
+"""Per-user FIFO queue for AutoPilot chat turns that exceeded the soft
+running cap.
+
+Queue state lives on :class:`prisma.models.ChatSession`'s ``chatStatus``
+text column:
+
+* ``"idle"`` — DEFAULT, no turn in flight (the 99% case)
+* ``"queued"`` — task waiting for a running slot to free
+* ``"running"`` — turn currently being processed
+
+The user's pending message itself is just a normal ChatMessage row (no
+status of its own). The dispatcher's submit-time payload (``file_ids``,
+``mode``, ``model``, ``permissions``, ``context``, ``request_arrival_at``)
+is stashed in that row's ``metadata`` JSONB so a later promotion can
+replay the turn faithfully.
+
+State transitions live in :func:`backend.copilot.db.update_chat_session_status`:
+
+* (insert + flip) ``"idle"`` → ``"queued"`` via :func:`enqueue_turn`
+* ``"queued"`` → ``"running"`` via :func:`claim_queued_turn` (dispatcher)
+* ``"queued"`` → ``"idle"`` via :func:`cancel_queued_turn` (user)
+* ``"running"`` → ``"idle"`` via :func:`backend.copilot.active_turns.release_turn_slot`
+* ``"running"`` → ``"queued"`` on dispatch-failure restore
+
+If the dispatcher finds the user paywalled / rate-limited at promote
+time, the session stays ``"queued"`` and the next slot-free hook
+re-validates — auto-recovers when eligibility returns, or the user
+cancels manually.
+"""
+
+import logging
+import uuid
+from typing import Any, Mapping
+
+from backend.copilot.active_turns import TurnSlot, count_running_turns
+from backend.copilot.config import ChatConfig
+from backend.copilot.model import (
+ CHAT_STATUS_IDLE,
+ CHAT_STATUS_QUEUED,
+ CHAT_STATUS_RUNNING,
+ ChatMessage,
+ _get_session_lock,
+ invalidate_session_cache,
+)
+from backend.copilot.rate_limit import (
+ RateLimitExceeded,
+ RateLimitUnavailable,
+ check_rate_limit,
+ get_global_rate_limits,
+ is_user_paywalled,
+)
+from backend.data.db_accessors import chat_db
+
+logger = logging.getLogger(__name__)
+
+
+async def count_queued_turns(user_id: str) -> int:
+ """Number of ``chatStatus='queued'`` ChatSession rows for ``user_id``."""
+ return await chat_db().count_chat_sessions_by_status(
+ user_id=user_id, status=CHAT_STATUS_QUEUED
+ )
+
+
+async def count_inflight_turns(user_id: str) -> int:
+ """Running + queued. Hard cap is enforced against this.
+
+ Counts queued first then running so a concurrent queued→running
+ promotion between the two reads can be double-counted (safe — caller
+ rejects one extra task) but never missed. The cap may briefly read
+ high under burst load, never low.
+ """
+ queued = await count_queued_turns(user_id)
+ running = await count_running_turns(user_id)
+ return queued + running
+
+
+async def list_queued_sessions(user_id: str):
+ """User's queued sessions, oldest-first (FIFO order). UX surface
+ for the 'your queued tasks' panel."""
+ return await chat_db().list_chat_sessions_by_status(
+ user_id=user_id, status=CHAT_STATUS_QUEUED
+ )
+
+
+class InflightCapExceeded(Exception):
+ """User's running + queued total has reached the configured hard cap.
+
+ Raised by :func:`try_enqueue_turn` so the route can map to HTTP 429.
+ """
+
+
+async def try_enqueue_turn(
+ *,
+ user_id: str,
+ inflight_cap: int,
+ session_id: str,
+ message: str,
+ message_id: str | None = None,
+ is_user_message: bool = True,
+ context: Mapping[str, str] | None = None,
+ file_ids: list[str] | None = None,
+ mode: str | None = None,
+ model: str | None = None,
+ permissions: Mapping[str, Any] | None = None,
+ request_arrival_at: float = 0.0,
+) -> ChatMessage:
+ """Admit a queued turn against the user's hard cap.
+
+ Non-locked count-then-insert: under burst, two concurrent submits
+ can both pass the count and both insert, leaving the user briefly
+ one or two over the cap. Same trade-off the graph-execution credit
+ rate-limit accepts on its INCRBY path; the cap is a safeguard, not
+ a budget.
+ """
+ if await count_inflight_turns(user_id) >= inflight_cap:
+ raise InflightCapExceeded()
+ return await enqueue_turn(
+ user_id=user_id,
+ session_id=session_id,
+ message=message,
+ message_id=message_id,
+ is_user_message=is_user_message,
+ context=context,
+ file_ids=file_ids,
+ mode=mode,
+ model=model,
+ permissions=permissions,
+ request_arrival_at=request_arrival_at,
+ )
+
+
+async def enqueue_turn(
+ *,
+ user_id: str,
+ session_id: str,
+ message: str,
+ message_id: str | None = None,
+ is_user_message: bool = True,
+ context: Mapping[str, str] | None = None,
+ file_ids: list[str] | None = None,
+ mode: str | None = None,
+ model: str | None = None,
+ permissions: Mapping[str, Any] | None = None,
+ request_arrival_at: float = 0.0,
+) -> ChatMessage:
+ """Persist the user's pending message and flip the session to
+ ``"queued"``. Caller is responsible for the in-flight cap check
+ AND session-ownership check upstream — once the row is committed
+ the dispatcher owns it.
+
+ The user message is a regular ChatMessage row (no special status).
+ The dispatcher's submit-time payload is stashed in the row's
+ ``metadata`` JSONB so a later promotion replays the turn faithfully.
+ """
+ metadata: dict[str, Any] = {}
+ if context is not None:
+ metadata["context"] = dict(context)
+ if file_ids is not None:
+ metadata["file_ids"] = list(file_ids)
+ if mode is not None:
+ metadata["mode"] = mode
+ if model is not None:
+ metadata["model"] = model
+ if permissions is not None:
+ metadata["permissions"] = dict(permissions)
+ if request_arrival_at:
+ metadata["request_arrival_at"] = request_arrival_at
+
+ # The Redis NX session lock serialises with ``append_and_save_message``
+ # so two concurrent submits to the same session can't pick the same
+ # ``sequence`` and PK-collide on ``(sessionId, sequence)``.
+ db = chat_db()
+ async with _get_session_lock(session_id):
+ live_sequence = await db.get_next_sequence(session_id)
+ row = await db.add_chat_message(
+ message_id=message_id or str(uuid.uuid4()),
+ session_id=session_id,
+ role="user" if is_user_message else "assistant",
+ content=message,
+ sequence=live_sequence,
+ metadata=metadata or None,
+ )
+ # Flip the session to ``"queued"``. CAS-gated on ``"idle"`` so a
+ # double-submit (session already queued/running) leaves the state
+ # alone; the second pending message persists as a normal ChatMessage
+ # row. When the session eventually promotes, the dispatcher reads
+ # the most-recent user row via ``get_latest_user_message_in_session``;
+ # earlier pending rows aren't independently scheduled, they sit in
+ # the chat history and the model sees them as context.
+ await db.update_chat_session_status(
+ session_id=session_id,
+ expect_status=CHAT_STATUS_IDLE,
+ status=CHAT_STATUS_QUEUED,
+ user_id=user_id,
+ )
+ # Invalidate the session cache so the next /chat read picks up the
+ # queued row + the session's new status (frontend renders the
+ # 'Queued' badge from ``session.chat_status``).
+ await invalidate_session_cache(session_id)
+ return row
+
+
+async def cancel_queued_turn(*, user_id: str, session_id: str) -> bool:
+ """Flip the user's session from ``"queued"`` → ``"idle"``. Returns
+ True iff the CAS matched AND the session is owned by the user.
+ Cancel/dispatch races resolve in a single atomic update."""
+ ok = await chat_db().update_chat_session_status(
+ session_id=session_id,
+ expect_status=CHAT_STATUS_QUEUED,
+ status=CHAT_STATUS_IDLE,
+ user_id=user_id,
+ )
+ if not ok:
+ return False
+ await invalidate_session_cache(session_id)
+ 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,
+ expect_status=CHAT_STATUS_QUEUED,
+ status=CHAT_STATUS_RUNNING,
+ )
+
+
+async def dispatch_next_for_user(user_id: str) -> bool:
+ """Promote at most one queued session for ``user_id`` from queued →
+ running. Called by ``mark_session_completed`` after every turn
+ ends — the slot-free hook is the only dispatcher trigger.
+
+ Returns ``True`` iff a session was actually promoted.
+
+ Pre-start re-validation runs *before* claiming so a paywalled
+ user's queue head stays queued (rather than consuming a running
+ slot for a turn that would immediately 402). Auto-recovers on
+ the next completion-driven tick once eligibility returns, or the
+ user cancels manually.
+ """
+ # ``executor.utils`` stays a local import: it pulls
+ # ``turn_queue.count_inflight_turns`` lazily back through this module,
+ # so top-leveling it here would deadlock the import graph.
+ from backend.copilot.executor.utils import dispatch_turn
+
+ queued = await list_queued_sessions(user_id)
+ if not queued:
+ return False
+ head = queued[0]
+
+ if await is_user_paywalled(user_id):
+ logger.info(
+ "dispatch_next_for_user: user=%s paywalled, leaving session=%s queued",
+ user_id,
+ head.session_id,
+ )
+ return False
+
+ cfg = ChatConfig()
+ try:
+ daily_limit, weekly_limit, _ = await get_global_rate_limits(
+ user_id,
+ cfg.daily_cost_limit_microdollars,
+ cfg.weekly_cost_limit_microdollars,
+ )
+ await check_rate_limit(
+ user_id=user_id,
+ daily_cost_limit=daily_limit,
+ weekly_cost_limit=weekly_limit,
+ )
+ except RateLimitExceeded as exc:
+ logger.info(
+ "dispatch_next_for_user: user=%s rate-limited (%s), leaving session=%s queued",
+ user_id,
+ exc,
+ head.session_id,
+ )
+ return False
+ except RateLimitUnavailable:
+ logger.warning(
+ "dispatch_next_for_user: rate-limit service degraded for user=%s; "
+ "leaving queue intact for the next tick",
+ user_id,
+ )
+ 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):
+ return False
+
+ # Find the pending user message in this session (the most recent
+ # user-role row with no following assistant rows — i.e. the one
+ # that triggered the queue). Its ``metadata`` carries the
+ # dispatcher payload.
+ pending = await chat_db().get_latest_user_message_in_session(head.session_id)
+ if pending is None or pending.content is None:
+ # Shouldn't happen — enqueue_turn always persists a row before
+ # flipping the session to queued. If it does (corrupted
+ # state), roll back to idle so the next tick doesn't loop.
+ await chat_db().update_chat_session_status(
+ session_id=head.session_id,
+ expect_status=CHAT_STATUS_RUNNING,
+ status=CHAT_STATUS_IDLE,
+ )
+ # Drop the cache so the sidebar doesn't keep showing the
+ # stale ``running`` indicator after the rollback.
+ await invalidate_session_cache(head.session_id)
+ return False
+
+ metadata = pending.metadata or {}
+ turn_id = str(uuid.uuid4())
+ try:
+ # The user's message is already persisted AND the session is
+ # already ``chatStatus='running'`` from claim_queued_session.
+ # Build a TurnSlot directly (no acquire) so we don't re-check
+ # the cap (would over-count our own just-promoted session) and
+ # don't re-flip the status (already running). ``dispatch_turn``
+ # calls ``slot.keep()`` internally; release happens via
+ # ``mark_session_completed`` → ``release_turn_slot``.
+ slot = TurnSlot(user_id, head.session_id)
+ slot.admitted = True
+ await dispatch_turn(
+ slot,
+ session_id=head.session_id,
+ user_id=user_id,
+ turn_id=turn_id,
+ message=pending.content,
+ is_user_message=pending.role == "user",
+ context=metadata.get("context"),
+ file_ids=metadata.get("file_ids"),
+ mode=metadata.get("mode"),
+ model=metadata.get("model"),
+ permissions=metadata.get("permissions"),
+ request_arrival_at=float(metadata.get("request_arrival_at") or 0.0),
+ )
+ except BaseException:
+ # Roll the claim back so a missed-dispatch tick or the next
+ # slot-free event can retry. ``BaseException`` (not just
+ # ``Exception``) so a task cancellation that lands mid-dispatch
+ # still leaves the session in a recoverable ``queued`` state
+ # rather than a stuck ``running``. Redis-side cleanup of the
+ # meta that ``dispatch_turn``'s ``create_session`` wrote is
+ # handled inside ``dispatch_turn`` itself (try/finally on its
+ # ``committed`` flag), so we only restore the DB side here.
+ try:
+ 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)
+ except BaseException as restore_exc:
+ logger.error(
+ "dispatch_next_for_user: failed to restore claim for "
+ "session=%s after dispatch failure; session left in "
+ "chatStatus='running' and will need manual recovery: %s",
+ head.session_id,
+ restore_exc,
+ )
+ raise
+
+ await invalidate_session_cache(head.session_id)
+ return True
diff --git a/autogpt_platform/backend/backend/copilot/turn_queue_test.py b/autogpt_platform/backend/backend/copilot/turn_queue_test.py
new file mode 100644
index 0000000000..78e803949d
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/turn_queue_test.py
@@ -0,0 +1,357 @@
+"""Unit tests for turn_queue: per-user FIFO queue layered over
+ChatSession.chatStatus.
+
+DB access is mocked via the ``backend.copilot.turn_queue.chat_db``
+indirection — same accessor pattern the executor subprocess uses to
+RPC into ``DatabaseManager``. Patching the accessor avoids reaching
+for Prisma directly while still exercising the queue's branching.
+"""
+
+from datetime import datetime, timezone
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from backend.copilot import turn_queue
+from backend.copilot.model import ChatMessage as PydanticChatMessage
+
+
+class _NoopAsyncCM:
+ """Stand-in for the Redis NX session lock context manager."""
+
+ async def __aenter__(self):
+ return True
+
+ async def __aexit__(self, *exc):
+ return None
+
+
+def _pyd_message(**overrides) -> PydanticChatMessage:
+ """Build a Pydantic ChatMessage with sensible defaults."""
+ base = {
+ "id": "msg-1",
+ "role": "user",
+ "content": "hello",
+ "session_id": "s1",
+ "metadata": None,
+ "created_at": datetime.now(timezone.utc),
+ "sequence": 1,
+ }
+ base.update(overrides)
+ 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)
+ return s
+
+
+# ── enqueue_turn payload encoding ──────────────────────────────────────
+
+
+async def test_enqueue_turn_packs_metadata_into_metadata_payload() -> None:
+ """Non-message dispatch params (file_ids, mode, model, permissions,
+ context, request_arrival_at) land in the ChatMessage row's
+ ``metadata`` JSONB so the dispatcher can replay the original turn
+ shape later."""
+ db = MagicMock()
+ db.get_next_sequence = AsyncMock(return_value=42)
+ db.add_chat_message = AsyncMock(return_value=_pyd_message(sequence=42))
+ db.update_chat_session_status = AsyncMock(return_value=True)
+ with (
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch.object(turn_queue, "_get_session_lock", return_value=_NoopAsyncCM()),
+ patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
+ ):
+ await turn_queue.enqueue_turn(
+ user_id="u1",
+ session_id="s1",
+ message="hello",
+ message_id="msg-1",
+ context={"url": "https://example.com"},
+ file_ids=["f1", "f2"],
+ mode="extended_thinking",
+ model="advanced",
+ permissions={"tool_filter": "allow"},
+ request_arrival_at=123.45,
+ )
+ kwargs = db.add_chat_message.call_args.kwargs
+ assert kwargs["session_id"] == "s1"
+ assert kwargs["sequence"] == 42
+ metadata = kwargs["metadata"]
+ assert metadata["context"] == {"url": "https://example.com"}
+ assert metadata["file_ids"] == ["f1", "f2"]
+ assert metadata["mode"] == "extended_thinking"
+ assert metadata["model"] == "advanced"
+ assert metadata["permissions"] == {"tool_filter": "allow"}
+ assert metadata["request_arrival_at"] == 123.45
+ # Session is flipped idle → queued.
+ db.update_chat_session_status.assert_awaited_once_with(
+ session_id="s1", expect_status="idle", status="queued", user_id="u1"
+ )
+
+
+async def test_enqueue_turn_omits_null_fields_from_metadata() -> None:
+ """A turn with no extra params leaves ``metadata`` NULL on the row
+ instead of an empty object."""
+ db = MagicMock()
+ db.get_next_sequence = AsyncMock(return_value=1)
+ db.add_chat_message = AsyncMock(return_value=_pyd_message())
+ db.update_chat_session_status = AsyncMock(return_value=True)
+ with (
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch.object(turn_queue, "_get_session_lock", return_value=_NoopAsyncCM()),
+ patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
+ ):
+ await turn_queue.enqueue_turn(user_id="u1", session_id="s1", message="hello")
+ assert db.add_chat_message.call_args.kwargs["metadata"] is None
+
+
+# ── cancel_queued_turn ─────────────────────────────────────────────────
+
+
+async def test_cancel_queued_turn_returns_true_and_invalidates_cache() -> None:
+ """A successful cancel flips the session ``queued`` → ``idle`` and
+ invalidates the session cache so the frontend drops the badge."""
+ db = MagicMock()
+ db.update_chat_session_status = AsyncMock(return_value=True)
+ invalidate = AsyncMock()
+ with (
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch.object(turn_queue, "invalidate_session_cache", new=invalidate),
+ ):
+ ok = await turn_queue.cancel_queued_turn(user_id="u1", session_id="s1")
+ assert ok is True
+ invalidate.assert_awaited_once_with("s1")
+ db.update_chat_session_status.assert_awaited_once_with(
+ session_id="s1",
+ expect_status="queued",
+ status="idle",
+ user_id="u1",
+ )
+
+
+async def test_cancel_queued_turn_returns_false_when_not_owned_or_not_queued() -> None:
+ db = MagicMock()
+ db.update_chat_session_status = AsyncMock(return_value=False)
+ with patch.object(turn_queue, "chat_db", return_value=db):
+ ok = await turn_queue.cancel_queued_turn(user_id="u1", session_id="s1")
+ assert ok is False
+
+
+# ── try_enqueue_turn ───────────────────────────────────────────────────
+
+
+async def test_try_enqueue_turn_raises_when_at_inflight_cap() -> None:
+ """Pre-check rejects when running + queued already equals the cap."""
+ db = MagicMock()
+ db.count_chat_sessions_by_status = AsyncMock(return_value=10)
+ db.add_chat_message = AsyncMock()
+ with (
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch.object(turn_queue, "count_running_turns", new=AsyncMock(return_value=5)),
+ ):
+ with pytest.raises(turn_queue.InflightCapExceeded):
+ await turn_queue.try_enqueue_turn(
+ user_id="u1",
+ inflight_cap=15,
+ session_id="s1",
+ message="hi",
+ )
+ db.add_chat_message.assert_not_awaited()
+
+
+# ── dispatch_next_for_user ─────────────────────────────────────────────
+
+
+def _patch_queued_list(rows):
+ """Patch ``list_queued_sessions`` (the dispatcher's queue read) to
+ return the given rows. Patching the helper rather than the
+ underlying RPC keeps the test independent of how chat_db()
+ resolves in-process vs. via DatabaseManagerAsyncClient."""
+ return patch.object(
+ turn_queue, "list_queued_sessions", new=AsyncMock(return_value=rows)
+ )
+
+
+async def test_dispatch_returns_false_when_queue_empty() -> None:
+ with _patch_queued_list([]):
+ promoted = await turn_queue.dispatch_next_for_user("u1")
+ assert promoted is False
+
+
+async def test_dispatch_leaves_queued_when_user_paywalled() -> None:
+ """A queued head whose owner has lapsed to NO_TIER stays queued —
+ no transition fires."""
+ db = MagicMock()
+ db.update_chat_session_status = AsyncMock()
+ with (
+ _patch_queued_list([_mock_session()]),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch(
+ "backend.copilot.turn_queue.is_user_paywalled",
+ new=AsyncMock(return_value=True),
+ ),
+ ):
+ promoted = await turn_queue.dispatch_next_for_user("u1")
+ assert promoted is False
+ db.update_chat_session_status.assert_not_awaited()
+
+
+async def test_dispatch_leaves_queued_on_rate_limit_exceeded() -> None:
+ """Mid-queue rate-limit lapse: leave the head queued, the next tick
+ re-validates."""
+ from backend.copilot.rate_limit import RateLimitExceeded
+
+ db = MagicMock()
+ db.update_chat_session_status = AsyncMock()
+ with (
+ _patch_queued_list([_mock_session()]),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ 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=(100, 1000, None)),
+ ),
+ patch(
+ "backend.copilot.turn_queue.check_rate_limit",
+ new=AsyncMock(
+ side_effect=RateLimitExceeded(
+ "daily", resets_at=datetime.now(timezone.utc)
+ )
+ ),
+ ),
+ ):
+ promoted = await turn_queue.dispatch_next_for_user("u1")
+ assert promoted is False
+ db.update_chat_session_status.assert_not_awaited()
+
+
+async def test_dispatch_defers_on_rate_limit_unavailable() -> None:
+ from backend.copilot.rate_limit import RateLimitUnavailable
+
+ db = MagicMock()
+ db.update_chat_session_status = AsyncMock()
+ with (
+ _patch_queued_list([_mock_session()]),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ patch(
+ "backend.copilot.turn_queue.is_user_paywalled",
+ new=AsyncMock(return_value=False),
+ ),
+ patch(
+ "backend.copilot.turn_queue.get_global_rate_limits",
+ new=AsyncMock(side_effect=RateLimitUnavailable()),
+ ),
+ ):
+ promoted = await turn_queue.dispatch_next_for_user("u1")
+ assert promoted is False
+ db.update_chat_session_status.assert_not_awaited()
+
+
+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")
+ pending = _pyd_message(metadata={"mode": "extended_thinking"})
+ db = MagicMock()
+ db.update_chat_session_status = AsyncMock(return_value=True)
+ db.get_latest_user_message_in_session = AsyncMock(return_value=pending)
+ dispatch_turn_mock = AsyncMock()
+ invalidate = AsyncMock()
+ with (
+ _patch_queued_list([head]),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ 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=(100, 1000, None)),
+ ),
+ patch(
+ "backend.copilot.turn_queue.check_rate_limit",
+ new=AsyncMock(),
+ ),
+ patch(
+ "backend.copilot.executor.utils.dispatch_turn",
+ new=dispatch_turn_mock,
+ ),
+ patch.object(turn_queue, "invalidate_session_cache", new=invalidate),
+ ):
+ promoted = await turn_queue.dispatch_next_for_user("u1")
+ 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"
+ )
+
+
+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")
+ 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.get_latest_user_message_in_session = AsyncMock(return_value=pending)
+ dispatch_turn_mock = AsyncMock(side_effect=RuntimeError("RabbitMQ blip"))
+ with (
+ _patch_queued_list([head]),
+ patch.object(turn_queue, "chat_db", return_value=db),
+ 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=(100, 1000, None)),
+ ),
+ patch(
+ "backend.copilot.turn_queue.check_rate_limit",
+ new=AsyncMock(),
+ ),
+ patch(
+ "backend.copilot.executor.utils.dispatch_turn",
+ new=dispatch_turn_mock,
+ ),
+ patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),
+ ):
+ with pytest.raises(RuntimeError, match="RabbitMQ blip"):
+ await turn_queue.dispatch_next_for_user("u1")
+ # Two transitions: claim then restore. Redis-side meta cleanup is
+ # ``dispatch_turn``'s responsibility (its own try/finally on the
+ # ``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(
+ 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 af2919a298..23e060f077 100644
--- a/autogpt_platform/backend/backend/data/db_manager.py
+++ b/autogpt_platform/backend/backend/data/db_manager.py
@@ -397,6 +397,14 @@ class DatabaseManager(AppService):
update_message_content_by_sequence = _(chat_db.update_message_content_by_sequence)
update_chat_session_title = _(chat_db.update_chat_session_title)
set_turn_duration = _(chat_db.set_turn_duration)
+ # ChatSession lifecycle primitives. Three functions cover the
+ # cap-count + cross-session queue (count/list/transition).
+ 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)
+ get_chat_session_status = _(chat_db.get_chat_session_status)
+ get_latest_user_message_in_session = _(chat_db.get_latest_user_message_in_session)
+ add_chat_message = _(chat_db.add_chat_message)
class DatabaseManagerClient(AppServiceClient):
@@ -623,3 +631,9 @@ class DatabaseManagerAsyncClient(AppServiceClient):
update_message_content_by_sequence = d.update_message_content_by_sequence
update_chat_session_title = d.update_chat_session_title
set_turn_duration = d.set_turn_duration
+ 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
+ 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/executor/billing.py b/autogpt_platform/backend/backend/executor/billing.py
index 10d1567bd3..f6369c1c97 100644
--- a/autogpt_platform/backend/backend/executor/billing.py
+++ b/autogpt_platform/backend/backend/executor/billing.py
@@ -24,7 +24,7 @@ from backend.util.clients import (
)
from backend.util.exceptions import InsufficientBalanceError
from backend.util.logging import TruncatedLogger
-from backend.util.metrics import DiscordChannel, discord_send_alert
+from backend.util.metrics import DiscordChannel
from backend.util.settings import Settings
from .utils import LogMetadata, block_usage_cost, execution_usage_cost
diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.py
index e59b6aee9c..8a21ba8160 100644
--- a/autogpt_platform/backend/backend/util/settings.py
+++ b/autogpt_platform/backend/backend/util/settings.py
@@ -174,15 +174,31 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):
le=1000,
description="Maximum number of concurrent graph executions allowed per user per graph.",
)
- max_concurrent_copilot_turns_per_user: int = Field(
+ max_inflight_copilot_turns_per_user: int = Field(
default=15,
ge=1,
le=1000,
description=(
- "Hard cap on concurrent in-flight AutoPilot/CoPilot chat turns "
- "per user. Reached via the chat HTTP route, the AutoPilotBlock, "
- "and run_sub_session — all funnel through schedule_turn() and "
- "share this cap. The user-facing 429 message renders this value."
+ "Hard cap on in-flight (running + queued) AutoPilot/CoPilot "
+ "chat turns per user. Once running >= "
+ "``max_running_copilot_turns_per_user`` and the queue brings the "
+ "total to this number, ``POST /chat/stream`` returns 429. "
+ "Reached from the chat HTTP route, the AutoPilotBlock, and "
+ "run_sub_session — all funnel through schedule_chat_turn / "
+ "schedule_turn and share this cap."
+ ),
+ )
+ max_running_copilot_turns_per_user: int = Field(
+ default=5,
+ ge=1,
+ le=1000,
+ description=(
+ "Soft cap on concurrently *running* AutoPilot/CoPilot chat "
+ "turns per user. Tasks submitted while the user is at this cap "
+ "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."
),
)
diff --git a/autogpt_platform/backend/migrations/20260509120000_add_chat_turn_queue/migration.sql b/autogpt_platform/backend/migrations/20260509120000_add_chat_turn_queue/migration.sql
new file mode 100644
index 0000000000..0e3f2ad309
--- /dev/null
+++ b/autogpt_platform/backend/migrations/20260509120000_add_chat_turn_queue/migration.sql
@@ -0,0 +1,30 @@
+-- Session-level lifecycle for the per-user soft running cap +
+-- cross-session queue. ``idle`` (DEFAULT, no turn in flight),
+-- ``queued`` (waiting for a running slot), ``running`` (a turn is
+-- being processed). A Postgres enum is used (rather than TEXT) so
+-- typos and invalid values are rejected at the DB layer; adding a
+-- future state needs an ``ALTER TYPE ... ADD VALUE`` migration, which
+-- is cheap on PG 12+.
+CREATE TYPE "ChatSessionStatus" AS ENUM ('idle', 'queued', 'running');
+
+ALTER TABLE "ChatSession"
+ ADD COLUMN "chatStatus" "ChatSessionStatus" NOT NULL DEFAULT 'idle';
+
+-- Single compound index covers all three ChatSession queries:
+-- * cap-count (count by userId + chatStatus)
+-- * queue-list (find_many WHERE userId + chatStatus ORDER BY updatedAt)
+-- * sidebar list (find_many WHERE userId ORDER BY updatedAt desc) —
+-- Postgres scans the per-userId index range and sorts in memory.
+-- Drop the prior (userId, updatedAt) index so the 3-col one is the
+-- only path for this table's per-user queries.
+DROP INDEX IF EXISTS "ChatSession_userId_updatedAt_idx";
+
+CREATE INDEX "ChatSession_user_status_idx"
+ ON "ChatSession" ("userId", "chatStatus", "updatedAt");
+
+-- ChatMessage carries an optional per-row JSONB metadata bag for the
+-- dispatcher's submit-time payload on the user row that triggered a
+-- queued turn (file_ids, mode, model, permissions, context,
+-- request_arrival_at). Cleared / unused on every history row.
+ALTER TABLE "ChatMessage"
+ ADD COLUMN "metadata" JSONB;
diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prisma
index 54ec644087..3996b45da5 100644
--- a/autogpt_platform/backend/schema.prisma
+++ b/autogpt_platform/backend/schema.prisma
@@ -68,7 +68,7 @@ model User {
PushSubscriptions PushSubscription[]
PendingHumanReviews PendingHumanReview[]
Workspace UserWorkspace?
- PlatformCostLogs PlatformCostLog[]
+ PlatformCostLogs PlatformCostLog[]
// OAuth Provider relations
OAuthApplications OAuthApplication[]
@@ -81,6 +81,12 @@ model User {
PlatformUserLinks PlatformUserLink[]
}
+enum ChatSessionStatus {
+ idle
+ queued
+ running
+}
+
enum SubscriptionTier {
NO_TIER
BASIC
@@ -156,7 +162,6 @@ model CoPilotUnderstanding {
@@index([userId])
}
-
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//////////////// USER WORKSPACE TABLES /////////////////
@@ -268,9 +273,24 @@ model ChatSession {
totalPromptTokens Int @default(0)
totalCompletionTokens Int @default(0)
+ // Session lifecycle for the per-user soft running cap + cross-session
+ // queue. ``idle`` (DEFAULT, no turn in flight), ``queued`` (waiting
+ // for a running slot to free), ``running`` (a turn is currently
+ // being processed). ``updatedAt`` doubles as the status-change
+ // timestamp (every transition touches the row).
+ chatStatus ChatSessionStatus @default(idle)
+
Messages ChatMessage[]
- @@index([userId, updatedAt])
+ // Single compound index covers (a) the cap-count (count by userId +
+ // chatStatus), (b) the queue-list ORDER BY updatedAt asc, AND (c) the
+ // sidebar list (find_many WHERE userId ORDER BY updatedAt desc).
+ // For (c) Postgres scans the per-userId index range across all
+ // chatStatus sub-buckets and sorts by updatedAt in memory — fine at
+ // typical per-user N (≤100s) and not worth a redundant 2-col index.
+ // The migration drops the previously-existing (userId, updatedAt)
+ // index that this one supersedes.
+ @@index([userId, chatStatus, updatedAt], name: "ChatSession_user_status_idx")
}
model ChatMessage {
@@ -293,6 +313,13 @@ model ChatMessage {
sequence Int
durationMs Int? // Wall-clock milliseconds for this assistant turn
+ // Per-row JSONB metadata bag. Currently holds the dispatcher's
+ // submit-time payload (file_ids, mode, model, permissions, context,
+ // request_arrival_at) on the user row that triggered a queued turn,
+ // so a later promotion can replay the original turn faithfully.
+ // Generic so future per-row state can land here without a migration.
+ metadata Json?
+
@@unique([sessionId, sequence])
}
@@ -627,8 +654,8 @@ model AgentGraphExecution {
ChildExecutions AgentGraphExecution[] @relation("ParentChildExecution")
// Sharing fields
- isShared Boolean @default(false)
- shareToken String? @unique
+ isShared Boolean @default(false)
+ shareToken String? @unique
sharedAt DateTime?
SharedExecutionFiles SharedExecutionFile[]
@@ -871,7 +898,7 @@ model PlatformCostLog {
createdAt DateTime @default(now())
userId String?
- User User? @relation(fields: [userId], references: [id], onDelete: SetNull)
+ User User? @relation(fields: [userId], references: [id], onDelete: SetNull)
graphExecId String?
nodeExecId String?
graphId String?
@@ -884,16 +911,16 @@ model PlatformCostLog {
// Cost in microdollars (1 USD = 1,000,000). Null if unknown.
costMicrodollars BigInt?
- inputTokens Int?
- outputTokens Int?
+ inputTokens Int?
+ outputTokens Int?
cacheReadTokens Int? // Anthropic cache read tokens (billed at 10% of base)
cacheCreationTokens Int? // Anthropic cache write tokens (billed at 125% of base)
- dataSize Int? // bytes
- duration Float? // seconds
- model String?
- trackingType String? // e.g. "cost_usd", "tokens", "characters", "items", "per_run", "sandbox_seconds", "walltime_seconds"
- trackingAmount Float? // Amount in the unit implied by trackingType
- metadata Json?
+ dataSize Int? // bytes
+ duration Float? // seconds
+ model String?
+ trackingType String? // e.g. "cost_usd", "tokens", "characters", "items", "per_run", "sandbox_seconds", "walltime_seconds"
+ trackingAmount Float? // Amount in the unit implied by trackingType
+ metadata Json?
@@index([userId, createdAt])
@@index([provider, createdAt])
@@ -1100,7 +1127,7 @@ model StoreListing {
ActiveVersion StoreListingVersion? @relation("ActiveVersion", fields: [activeVersionId], references: [id])
// The agent link here is only so we can do lookup on agentId
- agentGraphId String @unique
+ agentGraphId String @unique
owningUserId String
OwningUser User @relation(fields: [owningUserId], references: [id])
@@ -1437,12 +1464,12 @@ enum PlatformLinkType {
// "owner" — all usage from that server is attributed to their account.
model PlatformLink {
id String @id @default(uuid())
- userId String // AutoGPT user ID of the owner
+ userId String // AutoGPT user ID of the owner
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
platform PlatformType
- platformServerId String // Server/guild/group ID on that platform
- ownerPlatformUserId String // Platform user ID of the person who set it up
- serverName String? // Display name of the server (best-effort, may go stale)
+ platformServerId String // Server/guild/group ID on that platform
+ ownerPlatformUserId String // Platform user ID of the person who set it up
+ serverName String? // Display name of the server (best-effort, may go stale)
linkedAt DateTime @default(now())
@@unique([platform, platformServerId])
@@ -1455,11 +1482,11 @@ model PlatformLink {
// separately.
model PlatformUserLink {
id String @id @default(uuid())
- userId String // AutoGPT user ID
+ userId String // AutoGPT user ID
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
platform PlatformType
- platformUserId String // Individual's user ID on the platform
- platformUsername String? // Display name at link time (best-effort)
+ platformUserId String // Individual's user ID on the platform
+ platformUsername String? // Display name at link time (best-effort)
linkedAt DateTime @default(now())
@@unique([platform, platformUserId])
@@ -1476,12 +1503,12 @@ model PlatformLinkToken {
platform PlatformType
linkType PlatformLinkType @default(SERVER)
// SERVER token fields (null for USER tokens)
- platformServerId String? // Server/guild/group ID being linked
- serverName String? // Server display name
- channelId String? // Channel to send confirmation back to
+ platformServerId String? // Server/guild/group ID being linked
+ serverName String? // Server display name
+ channelId String? // Channel to send confirmation back to
// Always set — platform user ID of the person who will claim ownership
platformUserId String
- platformUsername String? // Their display name
+ platformUsername String? // Their display name
expiresAt DateTime
usedAt DateTime?
createdAt DateTime @default(now())
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotChatHost.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotChatHost.tsx
index 3529018d8d..31bfad7766 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotChatHost.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotChatHost.tsx
@@ -45,6 +45,7 @@ export function CopilotChatHost({
rateLimitMessage,
dismissRateLimit,
sessionDryRun,
+ sessionChatStatus,
} = useCopilotPage();
return (
@@ -64,6 +65,7 @@ export function CopilotChatHost({
status={status}
error={error}
sessionId={sessionId}
+ sessionChatStatus={sessionChatStatus}
isLoadingSession={isLoadingSession}
isSessionError={isSessionError}
isCreatingSession={isCreatingSession}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
index e3bb7deeec..e900a13cec 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
@@ -24,6 +24,7 @@ export interface ChatContainerProps {
status: string;
error: Error | undefined;
sessionId: string | null;
+ sessionChatStatus?: string;
isLoadingSession: boolean;
isSessionError?: boolean;
isCreatingSession: boolean;
@@ -62,6 +63,7 @@ export const ChatContainer = ({
status,
error,
sessionId,
+ sessionChatStatus,
isLoadingSession,
isSessionError,
isCreatingSession,
@@ -170,6 +172,7 @@ export const ChatContainer = ({
restoreStatusMessage={restoreStatusMessage}
activeStreamStartedAt={activeStreamStartedAt}
sessionID={sessionId}
+ sessionChatStatus={sessionChatStatus}
hasMoreMessages={hasMoreMessages}
isLoadingMore={isLoadingMore}
onLoadMore={onLoadMore}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
index 12da2bb3e9..67635a0b57 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
@@ -34,6 +34,7 @@ import { CopyButton } from "./components/CopyButton";
import { CollapsedToolGroup } from "./components/CollapsedToolGroup";
import { MessageAttachments } from "./components/MessageAttachments";
import { MessagePartRenderer } from "./components/MessagePartRenderer";
+import { QueueBadge } from "./components/QueueBadge";
import { StepsCollapse } from "./components/StepsCollapse";
import { ThinkingIndicator } from "./components/ThinkingIndicator";
@@ -49,6 +50,10 @@ interface Props {
* zero on every fresh mount. */
activeStreamStartedAt?: string | null;
sessionID?: string | null;
+ /** Session-level lifecycle: ``"idle" | "queued" | "running"``.
+ * The Queued badge anchors on the latest user message iff this is
+ * ``"queued"``. */
+ sessionChatStatus?: string;
hasMoreMessages?: boolean;
isLoadingMore?: boolean;
onLoadMore?: () => void;
@@ -267,6 +272,7 @@ export function ChatMessagesContainer({
restoreStatusMessage,
activeStreamStartedAt,
sessionID,
+ sessionChatStatus,
hasMoreMessages,
isLoadingMore,
onLoadMore,
@@ -561,6 +567,22 @@ export function ChatMessagesContainer({
)}
{isLastAssistant && showIndicator && indicator}
</MessageContent>
+ {message.role === "user" &&
+ sessionChatStatus === "queued" &&
+ (() => {
+ const stats = turnStats?.get(message.id);
+ if (!stats?.isLatestUserMessage) {
+ return null;
+ }
+ return (
+ <MessageActions
+ className="mt-1 items-center justify-end gap-1.5"
+ data-testid="queue-status-row"
+ >
+ <QueueBadge sessionID={sessionID ?? null} />
+ </MessageActions>
+ );
+ })()}
{message.role === "user" && textParts.length > 0 && (
<MessageActions className="mt-1 items-center justify-end gap-2 opacity-0 transition-opacity group-focus-within:opacity-100 group-hover:opacity-100">
{(() => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
index 386fde66f2..de3cd643f6 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
@@ -58,6 +58,14 @@ vi.mock("../components/AssistantMessageActions", () => ({
AssistantMessageActions: () => null,
}));
+vi.mock("../components/QueueBadge", () => ({
+ QueueBadge: ({ sessionID }: { sessionID: string | null }) => (
+ <span data-testid="queue-badge" data-session-id={sessionID ?? ""}>
+ QueueBadge
+ </span>
+ ),
+}));
+
vi.mock("../components/CopyButton", () => ({ CopyButton: () => null }));
vi.mock("../components/CollapsedToolGroup", () => ({
CollapsedToolGroup: () => null,
@@ -516,3 +524,101 @@ describe("ChatMessagesContainer — turnStats", () => {
expect(labels.length).toBe(0);
});
});
+
+// ── per-message queue badge ───────────────────────────────────────────────
+
+describe("ChatMessagesContainer — queue badges on user messages", () => {
+ beforeEach(() => {
+ mockScrollEl.scrollHeight = 100;
+ mockScrollEl.scrollTop = 0;
+ mockScrollEl.clientHeight = 500;
+ MockIntersectionObserver.lastCallback = null;
+ vi.stubGlobal("IntersectionObserver", MockIntersectionObserver);
+ });
+
+ afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+ });
+
+ it("renders a QueueBadge when the row is the latest user in a queued session", () => {
+ const userId = "user-q1";
+ const turnStats = new Map([
+ [
+ userId,
+ {
+ isLatestUserMessage: true,
+ rawMessageId: "uuid-q1",
+ },
+ ],
+ ]);
+ const messages = [
+ {
+ id: userId,
+ role: "user" as const,
+ parts: [{ type: "text" as const, text: "queue me", state: "done" }],
+ },
+ ];
+ render(
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ <ChatMessagesContainer
+ {...(baseProps as any)}
+ messages={messages as any}
+ turnStats={turnStats as any}
+ sessionChatStatus="queued"
+ />,
+ );
+ const badge = screen.getByTestId("queue-badge");
+ expect(badge.getAttribute("data-session-id")).toBe("sess-123");
+ });
+
+ it("does NOT render a QueueBadge for normal (non-queued) user messages", () => {
+ const userId = "user-n1";
+ const turnStats = new Map([
+ [userId, { createdAt: "2026-04-23T08:32:09.000Z" }],
+ ]);
+ const messages = [
+ {
+ id: userId,
+ role: "user" as const,
+ parts: [{ type: "text" as const, text: "hi", state: "done" }],
+ },
+ ];
+ render(
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ <ChatMessagesContainer
+ {...(baseProps as any)}
+ messages={messages as any}
+ turnStats={turnStats as any}
+ />,
+ );
+ expect(screen.queryByTestId("queue-badge")).toBeNull();
+ });
+
+ it("does NOT render the badge when isLatestUserMessage but session is idle", () => {
+ // Guards against regressing the AND-gate: even if a row is the
+ // latest user message, the badge should stay hidden unless the
+ // OWNING session is in the queued state.
+ const userId = "user-q2";
+ const turnStats = new Map([
+ [userId, { isLatestUserMessage: true, rawMessageId: "uuid-q2" }],
+ ]);
+ const messages = [
+ {
+ id: userId,
+ role: "user" as const,
+ parts: [{ type: "text" as const, text: "live", state: "done" }],
+ },
+ ];
+ render(
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ <ChatMessagesContainer
+ {...(baseProps as any)}
+ messages={messages as any}
+ turnStats={turnStats as any}
+ sessionChatStatus="idle"
+ />,
+ );
+ expect(screen.queryByTestId("queue-badge")).toBeNull();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/QueueBadge.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/QueueBadge.tsx
new file mode 100644
index 0000000000..7fd7b83134
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/QueueBadge.tsx
@@ -0,0 +1,99 @@
+"use client";
+
+import {
+ getGetV2GetSessionQueryKey,
+ usePostV2CancelSessionTask,
+} from "@/app/api/__generated__/endpoints/chat/chat";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/components/atoms/Tooltip/BaseTooltip";
+import { toast } from "@/components/molecules/Toast/use-toast";
+import * as Sentry from "@sentry/nextjs";
+import { HourglassIcon, XCircleIcon } from "@phosphor-icons/react";
+import { useQueryClient } from "@tanstack/react-query";
+
+interface Props {
+ sessionID: string | null;
+}
+
+const QUEUED_TOOLTIP =
+ "Will start automatically when one of your current tasks finishes.";
+
+export function QueueBadge({ sessionID }: Props) {
+ const queryClient = useQueryClient();
+ const { mutate: cancelTask, isPending: isCancelling } =
+ usePostV2CancelSessionTask({
+ mutation: {
+ onSuccess: () => {
+ if (sessionID) {
+ queryClient.invalidateQueries({
+ queryKey: getGetV2GetSessionQueryKey(sessionID),
+ });
+ }
+ },
+ onError: (error) => {
+ // 404 = session not found / not owned: sync the UI and
+ // suppress the destructive toast.
+ const status = (error as { response?: { status?: number } })?.response
+ ?.status;
+ if (status === 404) {
+ if (sessionID) {
+ queryClient.invalidateQueries({
+ queryKey: getGetV2GetSessionQueryKey(sessionID),
+ });
+ }
+ return;
+ }
+ Sentry.captureException(error);
+ toast({
+ variant: "destructive",
+ title: "Could not cancel queued task",
+ description: "Please try again.",
+ });
+ },
+ },
+ });
+
+ function handleCancel() {
+ if (!sessionID || isCancelling) return;
+ cancelTask({ sessionId: sessionID });
+ }
+
+ return (
+ <span className="inline-flex items-center gap-1">
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span
+ className="inline-flex items-center gap-1 rounded-full bg-purple-100 px-2 py-0.5 text-[11px] font-medium text-purple-800"
+ data-testid="queue-badge-queued"
+ >
+ <HourglassIcon size={12} weight="bold" />
+ Queued
+ </span>
+ </TooltipTrigger>
+ <TooltipContent side="top" className="max-w-xs whitespace-normal">
+ {QUEUED_TOOLTIP}
+ </TooltipContent>
+ </Tooltip>
+ {sessionID ? (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <button
+ type="button"
+ onClick={handleCancel}
+ disabled={isCancelling}
+ aria-label="Cancel queued task"
+ data-testid="queue-cancel-button"
+ className="inline-flex h-4 w-4 items-center justify-center rounded-full text-neutral-500 transition-colors hover:text-red-600 disabled:opacity-50"
+ >
+ <XCircleIcon size={14} weight="fill" />
+ </button>
+ </TooltipTrigger>
+ <TooltipContent side="top">Cancel queued task</TooltipContent>
+ </Tooltip>
+ ) : null}
+ </span>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/QueueBadge.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/QueueBadge.test.tsx
new file mode 100644
index 0000000000..b08660d3bc
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/QueueBadge.test.tsx
@@ -0,0 +1,107 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { fireEvent, screen } from "@testing-library/react";
+import { render } from "@/tests/integrations/test-utils";
+import { QueueBadge } from "../QueueBadge";
+
+const cancelMock = vi.fn();
+const captureExceptionMock = vi.fn();
+const toastMock = vi.fn();
+let isPending = false;
+type ErrorResponse = { response?: { status?: number } } | null;
+let errorResponse: ErrorResponse = null;
+
+vi.mock("@/app/api/__generated__/endpoints/chat/chat", () => ({
+ usePostV2CancelSessionTask: ({
+ mutation,
+ }: {
+ mutation?: {
+ onSuccess?: (response: { status: number }) => void;
+ onError?: (error: unknown) => void;
+ };
+ }) => ({
+ mutate: (args: { sessionId: string }) => {
+ cancelMock(args);
+ if (errorResponse !== null) {
+ mutation?.onError?.(errorResponse);
+ return;
+ }
+ mutation?.onSuccess?.({ status: 200 });
+ },
+ isPending,
+ }),
+ getGetV2GetSessionQueryKey: (sessionId: string) => [
+ `/api/chat/sessions/${sessionId}`,
+ ],
+}));
+
+vi.mock("@sentry/nextjs", () => ({
+ captureException: (...args: unknown[]) => captureExceptionMock(...args),
+}));
+vi.mock("@/components/molecules/Toast/use-toast", () => ({
+ toast: (...args: unknown[]) => toastMock(...args),
+ useToast: () => ({ toast: toastMock, dismiss: vi.fn(), toasts: [] }),
+}));
+
+beforeEach(() => {
+ errorResponse = null;
+});
+
+afterEach(() => {
+ cancelMock.mockClear();
+ captureExceptionMock.mockClear();
+ toastMock.mockClear();
+ isPending = false;
+});
+
+describe("QueueBadge", () => {
+ it("renders the queued badge with the cancel button when a sessionID is present", () => {
+ render(<QueueBadge sessionID="sess-1" />);
+ expect(screen.getByTestId("queue-badge-queued")).toBeDefined();
+ expect(screen.getByTestId("queue-cancel-button")).toBeDefined();
+ });
+
+ it("invokes the cancel mutation with the session id on click", () => {
+ render(<QueueBadge sessionID="sess-42" />);
+ fireEvent.click(screen.getByTestId("queue-cancel-button"));
+ expect(cancelMock).toHaveBeenCalledWith({ sessionId: "sess-42" });
+ });
+
+ it("hides the cancel button when no sessionID is available", () => {
+ render(<QueueBadge sessionID={null} />);
+ expect(screen.getByTestId("queue-badge-queued")).toBeDefined();
+ expect(screen.queryByTestId("queue-cancel-button")).toBeNull();
+ });
+
+ it("treats a 404 cancel response as success (no toast, no Sentry)", () => {
+ // 404 = session already promoted / not owned: not a real failure, the
+ // UI just needs to resync. The destructive toast must NOT fire.
+ errorResponse = { response: { status: 404 } };
+ render(<QueueBadge sessionID="sess-x" />);
+ fireEvent.click(screen.getByTestId("queue-cancel-button"));
+ expect(toastMock).not.toHaveBeenCalled();
+ expect(captureExceptionMock).not.toHaveBeenCalled();
+ });
+
+ it("shows the destructive toast and reports to Sentry on a real cancel error", () => {
+ errorResponse = { response: { status: 500 } };
+ render(<QueueBadge sessionID="sess-y" />);
+ fireEvent.click(screen.getByTestId("queue-cancel-button"));
+ expect(toastMock).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "destructive",
+ title: "Could not cancel queued task",
+ }),
+ );
+ expect(captureExceptionMock).toHaveBeenCalledTimes(1);
+ });
+
+ it("handles cancel errors with no response object (network error)", () => {
+ // No `.response` on the error → the `status === 404` check should
+ // be false, so we fall through to the toast + Sentry path.
+ errorResponse = {};
+ render(<QueueBadge sessionID="sess-z" />);
+ fireEvent.click(screen.getByTestId("queue-cancel-button"));
+ expect(toastMock).toHaveBeenCalled();
+ expect(captureExceptionMock).toHaveBeenCalled();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatSidebar/ChatSidebar.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatSidebar/ChatSidebar.tsx
index 6b9a655fe0..38f3cdd9de 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatSidebar/ChatSidebar.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatSidebar/ChatSidebar.tsx
@@ -28,6 +28,7 @@ import {
CircleNotch,
DownloadSimpleIcon,
DotsThree,
+ HourglassIcon,
PencilSimpleIcon,
PlusCircleIcon,
PlusIcon,
@@ -378,7 +379,31 @@ export function ChatSidebar() {
{formatDate(session.updated_at)}
</Text>
</div>
- {session.is_processing &&
+ {session.chat_status === "running" && (
+ <span
+ aria-label="Session running"
+ title="Running"
+ data-testid="session-status-running"
+ className="inline-flex h-4 w-4 shrink-0 items-center justify-center"
+ >
+ <span className="h-2 w-2 animate-pulse rounded-full bg-emerald-500" />
+ </span>
+ )}
+ {session.chat_status === "queued" && (
+ <span
+ aria-label="Session queued"
+ title="Queued"
+ data-testid="session-status-queued"
+ className="inline-flex h-4 w-4 shrink-0 items-center justify-center text-purple-600"
+ >
+ <HourglassIcon
+ className="h-3.5 w-3.5"
+ weight="bold"
+ />
+ </span>
+ )}
+ {session.chat_status !== "running" &&
+ session.is_processing &&
shouldShowSessionProcessingIndicator({
sessionId: session.id,
currentSessionId: sessionId,
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatSidebar/__tests__/ChatSidebar.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatSidebar/__tests__/ChatSidebar.test.tsx
index 8f4ac14d13..4ab228a35c 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatSidebar/__tests__/ChatSidebar.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatSidebar/__tests__/ChatSidebar.test.tsx
@@ -156,3 +156,78 @@ describe("ChatSidebar — delete flow", () => {
expect(useCopilotUIStore.getState().sessionToDelete).toBeNull();
});
});
+
+describe("ChatSidebar — chat_status indicators", () => {
+ afterEach(() => {
+ server.resetHandlers();
+ });
+
+ it("renders the green running dot on sessions with chat_status='running'", async () => {
+ const runningSessions = [
+ {
+ id: "s1",
+ title: "Running essay",
+ is_processing: false,
+ chat_status: "running",
+ created_at: "2025-01-01T00:00:00Z",
+ updated_at: "2025-01-01T00:00:00Z",
+ },
+ ];
+ server.use(
+ getGetV2ListSessionsMockHandler200({
+ sessions: runningSessions,
+ total: 1,
+ }),
+ );
+ renderSidebar();
+ await screen.findByText("Running essay");
+ expect(screen.getByTestId("session-status-running")).toBeDefined();
+ expect(screen.queryByTestId("session-status-queued")).toBeNull();
+ });
+
+ it("renders the purple hourglass on sessions with chat_status='queued'", async () => {
+ const queuedSessions = [
+ {
+ id: "s1",
+ title: "Robot haiku",
+ is_processing: false,
+ chat_status: "queued",
+ created_at: "2025-01-01T00:00:00Z",
+ updated_at: "2025-01-01T00:00:00Z",
+ },
+ ];
+ server.use(
+ getGetV2ListSessionsMockHandler200({
+ sessions: queuedSessions,
+ total: 1,
+ }),
+ );
+ renderSidebar();
+ await screen.findByText("Robot haiku");
+ expect(screen.getByTestId("session-status-queued")).toBeDefined();
+ expect(screen.queryByTestId("session-status-running")).toBeNull();
+ });
+
+ it("renders no status indicator for chat_status='idle' sessions", async () => {
+ const idleSessions = [
+ {
+ id: "s1",
+ title: "Old chat",
+ is_processing: false,
+ chat_status: "idle",
+ created_at: "2025-01-01T00:00:00Z",
+ updated_at: "2025-01-01T00:00:00Z",
+ },
+ ];
+ server.use(
+ getGetV2ListSessionsMockHandler200({
+ sessions: idleSessions,
+ total: 1,
+ }),
+ );
+ renderSidebar();
+ await screen.findByText("Old chat");
+ expect(screen.queryByTestId("session-status-running")).toBeNull();
+ expect(screen.queryByTestId("session-status-queued")).toBeNull();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts
index e260b43b79..94697e1c7c 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts
@@ -384,3 +384,40 @@ describe("concatWithAssistantMerge", () => {
expect(result).toHaveLength(2);
});
});
+
+describe("convertChatSessionMessagesToUiMessages — latest user marker", () => {
+ it("marks the latest user row as isLatestUserMessage=true", () => {
+ const result = convertChatSessionMessagesToUiMessages(
+ SESSION_ID,
+ [
+ { id: "uuid-user-1", role: "user", content: "first", sequence: 0 },
+ { id: "uuid-asst-1", role: "assistant", content: "reply", sequence: 1 },
+ { id: "uuid-user-2", role: "user", content: "newest", sequence: 2 },
+ ],
+ { isComplete: true },
+ );
+
+ const stats0 = result.stats.get(result.messages[0].id);
+ const stats2 = result.stats.get(
+ result.messages[result.messages.length - 1].id,
+ );
+ // First user row is older — not the latest.
+ expect(stats0?.isLatestUserMessage).toBeFalsy();
+ // Latest user row carries the marker; consumer (ChatMessagesContainer)
+ // decides whether to render the Queued badge by also checking
+ // session.chat_status === 'queued'.
+ expect(stats2?.isLatestUserMessage).toBe(true);
+ expect(stats2?.rawMessageId).toBe("uuid-user-2");
+ });
+
+ it("marks a single user row as the latest", () => {
+ const result = convertChatSessionMessagesToUiMessages(
+ SESSION_ID,
+ [{ id: "u1", role: "user", content: "hi", sequence: 0 }],
+ { isComplete: true },
+ );
+
+ const stats = result.stats.get(result.messages[0].id);
+ expect(stats?.isLatestUserMessage).toBe(true);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts
index dbbe7d1b9c..e29c27c8bd 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts
@@ -4,11 +4,18 @@ import type { FileUIPart, UIMessage, UIDataTypes, UITools } from "ai";
export interface TurnStats {
durationMs?: number;
createdAt?: string;
+ /** Raw ChatMessage.id (UUID). Carried for the badge's cancel handler. */
+ rawMessageId?: string | null;
+ /** True iff this is the latest user message in the session. The
+ * "Queued" badge anchors on this row whenever the OWNING session's
+ * ``chat_status === "queued"`` (checked at render time). */
+ isLatestUserMessage?: boolean;
}
export type TurnStatsMap = Map<string, TurnStats>;
interface SessionChatMessage {
+ id: string | null;
role: string;
content: string | null;
tool_call_id: string | null;
@@ -30,6 +37,7 @@ function coerceSessionChatMessages(
if (!role) return null;
return {
+ id: typeof msg.id === "string" ? msg.id : null,
role,
content:
typeof msg.content === "string"
@@ -215,6 +223,11 @@ export function convertChatSessionMessagesToUiMessages(
stats: TurnStatsMap;
} {
const messages = coerceSessionChatMessages(rawMessages);
+ // Find the most-recent user message — when the session is queued, this
+ // is the message that's waiting and renders the "Queued" badge.
+ const latestUserMessageIndex = messages.findLastIndex(
+ (m) => m.role === "user",
+ );
const toolOutputsByCallId = new Map<string, unknown>();
// Seed with extra tool outputs from adjacent pages first;
@@ -249,6 +262,11 @@ export function convertChatSessionMessagesToUiMessages(
)
return;
+ // Cancelled rows stay visible in the conversation as orphan user
+ // bubbles (no AI follow-up after them). We don't emit a separate
+ // "Cancelled" indicator — the row's lack of a response, combined
+ // with the user remembering they just clicked X, communicates it.
+
// Role=="reasoning" rows carry extended_thinking content. Treat them as
// contributing a reasoning part to the surrounding assistant bubble —
// the consecutive-assistant merge below then folds them into the same
@@ -388,6 +406,14 @@ export function convertChatSessionMessagesToUiMessages(
if (uiRole === "assistant" && msg.duration_ms != null) {
patch.durationMs = msg.duration_ms;
}
+ if (uiRole === "user") {
+ // Queue badge consumes ``rawMessageId`` for its cancel handler and
+ // ``isLatestUserMessage`` to pick the anchor row. The badge's
+ // gating on ``session.chat_status === "queued"`` is the consumer's
+ // (ChatMessagesContainer's) concern.
+ patch.rawMessageId = msg.id;
+ patch.isLatestUserMessage = idx === latestUserMessageIndex;
+ }
if (Object.keys(patch).length > 0) patchStats(msgId, patch);
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts
index fae7bc7485..440e20d1de 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts
@@ -187,6 +187,10 @@ export function useChatSession({ dryRun = false }: UseChatSessionOptions = {}) {
[sessionQuery.data, freshSessionData],
);
+ const sessionChatStatus = (
+ freshSessionData as { chat_status?: string } | undefined
+ )?.chat_status;
+
return {
sessionId,
setSessionId,
@@ -207,5 +211,6 @@ export function useChatSession({ dryRun = false }: UseChatSessionOptions = {}) {
isCreatingSession,
refetchSession: sessionQuery.refetch,
sessionDryRun,
+ sessionChatStatus,
};
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
index fcb1f85376..5e10632e49 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
@@ -56,6 +56,7 @@ export function useCopilotPage() {
isCreatingSession,
refetchSession,
sessionDryRun,
+ sessionChatStatus,
} = useChatSession({ dryRun: isDryRun });
const {
@@ -252,5 +253,6 @@ export function useCopilotPage() {
// used to render the banner. The global `isDryRun` preference (for new
// sessions) lives in the store and is consumed by the toggle button.
sessionDryRun,
+ sessionChatStatus,
};
}
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index d22b7ae0d8..6e546717a4 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -2447,7 +2447,7 @@
"post": {
"tags": ["v2", "chat", "chat"],
"summary": "Cancel Session Task",
- "description": "Cancel the active streaming task for a session.\n\nPublishes a cancel event to the executor via RabbitMQ FANOUT, then\npolls Redis until the task status flips from ``running`` or a timeout\n(5 s) is reached. Returns only after the cancellation is confirmed.",
+ "description": "Cancel an in-flight task for a session.\n\nHandles both lifecycle states uniformly:\n\n* **Queued** (``chatStatus='queued'``) — the dispatcher hasn't\n claimed the row yet. Flip the session back to ``idle`` and\n return; no executor cancel event needed.\n* **Running** (``chatStatus='running'``) — publish a cancel event\n to the executor via RabbitMQ FANOUT, then poll Redis until the\n task status flips out of ``running`` or a 5 s timeout is hit.",
"operationId": "postV2CancelSessionTask",
"security": [{ "HTTPBearerJWT": [] }],
"parameters": [
@@ -15936,6 +15936,11 @@
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "User Id"
},
+ "chat_status": {
+ "type": "string",
+ "title": "Chat Status",
+ "default": "idle"
+ },
"messages": {
"items": { "additionalProperties": true, "type": "object" },
"type": "array",
@@ -15985,6 +15990,11 @@
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "Title"
},
+ "chat_status": {
+ "type": "string",
+ "title": "Chat Status",
+ "default": "idle"
+ },
"is_processing": { "type": "boolean", "title": "Is Processing" }
},
"type": "object",