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(copilot): queue follow-up messages on busy sessions (UI + run_sub_session + AutoPilot block) (#12737)

Zamil Majdyb1c043c2d8cc
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.py
index cbde6a40fe..eceedb828c 100644
--- a/autogpt_platform/backend/backend/api/features/chat/routes.py
+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py
@@ -2,15 +2,13 @@
import asyncio
import logging
-import re
from collections.abc import AsyncGenerator
from typing import Annotated
from uuid import uuid4
from autogpt_libs import auth
from fastapi import APIRouter, HTTPException, Query, Response, Security
-from fastapi.responses import StreamingResponse
-from prisma.models import UserWorkspaceFile
+from fastapi.responses import JSONResponse, StreamingResponse
from pydantic import BaseModel, ConfigDict, Field, field_validator
from backend.copilot import service as chat_service
@@ -29,6 +27,12 @@ from backend.copilot.model import (
get_user_sessions,
update_session_title,
)
+from backend.copilot.pending_message_helpers import (
+ QueuePendingMessageResponse,
+ is_turn_in_flight,
+ queue_pending_for_http,
+)
+from backend.copilot.pending_messages import peek_pending_messages
from backend.copilot.rate_limit import (
CoPilotUsageStatus,
RateLimitExceeded,
@@ -75,7 +79,7 @@ from backend.copilot.tracking import track_user_message
from backend.data.credit import UsageTransactionMetadata, get_user_credit_model
from backend.data.redis_client import get_redis_async
from backend.data.understanding import get_business_understanding
-from backend.data.workspace import get_or_create_workspace
+from backend.data.workspace import build_files_block, resolve_workspace_files
from backend.util.exceptions import InsufficientBalanceError, NotFoundError
from backend.util.settings import Settings
@@ -85,10 +89,6 @@ logger = logging.getLogger(__name__)
config = ChatConfig()
-_UUID_RE = re.compile(
- r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.I
-)
-
async def _validate_and_get_session(
session_id: str,
@@ -151,6 +151,19 @@ class StreamChatRequest(BaseModel):
)
+class PeekPendingMessagesResponse(BaseModel):
+ """Response for the pending-message peek (GET) endpoint.
+
+ Returns a read-only view of the pending buffer — messages are NOT
+ consumed. The frontend uses this to restore the queued-message
+ indicator after a page refresh and to decide when to clear it once
+ a turn has ended.
+ """
+
+ messages: list[str]
+ count: int
+
+
class CreateSessionRequest(BaseModel):
"""Request model for creating a new chat session.
@@ -764,36 +777,52 @@ async def cancel_session_task(
@router.post(
"/sessions/{session_id}/stream",
+ responses={
+ 202: {
+ "model": QueuePendingMessageResponse,
+ "description": (
+ "Session has a turn in flight — message queued into the pending "
+ "buffer and will be picked up between tool-call rounds by the "
+ "executor currently processing the turn."
+ ),
+ },
+ 404: {"description": "Session not found or access denied"},
+ 429: {"description": "Token rate-limit or call-frequency cap exceeded"},
+ },
)
async def stream_chat_post(
session_id: str,
request: StreamChatRequest,
user_id: str = Security(auth.get_user_id),
):
- """
- Stream chat responses for a session (POST with context support).
+ """Start a new turn OR queue a follow-up — decided server-side.
- Streams the AI/completion responses in real time over Server-Sent Events (SSE), including:
- - Text fragments as they are generated
- - Tool call UI elements (if invoked)
- - Tool execution results
+ - **Session idle**: starts a turn. Returns an SSE stream (``text/event-stream``)
+ with Vercel AI SDK chunks (text fragments, tool-call UI, tool results).
+ The generation runs in a background task that survives client disconnects;
+ reconnect via ``GET /sessions/{session_id}/stream`` to resume.
- The AI generation runs in a background task that continues even if the client disconnects.
- All chunks are written to a per-turn Redis stream for reconnection support. If the client
- disconnects, they can reconnect using GET /sessions/{session_id}/stream to resume.
+ - **Session has a turn in flight**: pushes the message into the per-session
+ pending buffer and returns ``202 application/json`` with
+ ``QueuePendingMessageResponse``. The executor running the current turn
+ drains the buffer between tool-call rounds (baseline) or at the start of
+ the next turn (SDK). Clients should detect the 202 and surface the
+ message as a queued-chip in the UI.
Args:
- session_id: The chat session identifier to associate with the streamed messages.
- request: Request body containing message, is_user_message, and optional context.
+ session_id: The chat session identifier.
+ request: Request body with message, is_user_message, and optional context.
user_id: Authenticated user ID.
- Returns:
- StreamingResponse: SSE-formatted response chunks.
-
"""
import asyncio
import time
stream_start_time = time.perf_counter()
+ # Wall-clock arrival time, propagated to the executor so the turn-start
+ # drain can order pending messages relative to this request (pending
+ # pushed BEFORE this instant were typed earlier; pending pushed AFTER
+ # are race-path follow-ups typed while /stream was still processing).
+ request_arrival_at = time.time()
log_meta = {"component": "ChatStream", "session_id": session_id, "user_id": user_id}
logger.info(
@@ -802,6 +831,26 @@ async def stream_chat_post(
extra={"json_fields": log_meta},
)
await _validate_and_get_session(session_id, user_id)
+
+ # Self-defensive queue-fallback: if a turn is already running, don't race
+ # it on the cluster lock — drop the message into the pending buffer and
+ # return 202 so the caller can render a chip. Both UI chips and autopilot
+ # block follow-ups route through this path; keeping the decision on the
+ # server means every caller gets uniform behaviour.
+ if (
+ request.is_user_message
+ and request.message
+ and await is_turn_in_flight(session_id)
+ ):
+ response = await queue_pending_for_http(
+ session_id=session_id,
+ user_id=user_id,
+ message=request.message,
+ context=request.context,
+ file_ids=request.file_ids,
+ )
+ return JSONResponse(status_code=202, content=response.model_dump())
+
logger.info(
f"[TIMING] session validated in {(time.perf_counter() - stream_start_time) * 1000:.1f}ms",
extra={
@@ -832,33 +881,10 @@ async def stream_chat_post(
# Also sanitise file_ids so only validated, workspace-scoped IDs are
# forwarded downstream (e.g. to the executor via enqueue_copilot_turn).
sanitized_file_ids: list[str] | None = None
- if request.file_ids and user_id:
- # Filter to valid UUIDs only to prevent DB abuse
- valid_ids = [fid for fid in request.file_ids if _UUID_RE.match(fid)]
-
- if valid_ids:
- workspace = await get_or_create_workspace(user_id)
- # Batch query instead of N+1
- files = await UserWorkspaceFile.prisma().find_many(
- where={
- "id": {"in": valid_ids},
- "workspaceId": workspace.id,
- "isDeleted": False,
- }
- )
- # Only keep IDs that actually exist in the user's workspace
- sanitized_file_ids = [wf.id for wf in files] or None
- file_lines: list[str] = [
- f"- {wf.name} ({wf.mimeType}, {round(wf.sizeBytes / 1024, 1)} KB), file_id={wf.id}"
- for wf in files
- ]
- if file_lines:
- files_block = (
- "\n\n[Attached files]\n"
- + "\n".join(file_lines)
- + "\nUse read_workspace_file with the file_id to access file contents."
- )
- request.message += files_block
+ if request.file_ids:
+ files = await resolve_workspace_files(user_id, request.file_ids)
+ sanitized_file_ids = [wf.id for wf in files] or None
+ request.message += build_files_block(files)
# Atomically append user message to session BEFORE creating task to avoid
# race condition where GET_SESSION sees task as "running" but message isn't
@@ -917,6 +943,7 @@ async def stream_chat_post(
file_ids=sanitized_file_ids,
mode=request.mode,
model=request.model,
+ request_arrival_at=request_arrival_at,
)
else:
logger.info(
@@ -1067,6 +1094,31 @@ async def stream_chat_post(
)
+ "/sessions/{session_id}/messages/pending",
+ response_model=PeekPendingMessagesResponse,
+ responses={
+ 404: {"description": "Session not found or access denied"},
+ },
+)
+async def get_pending_messages(
+ session_id: str,
+ user_id: str = Security(auth.get_user_id),
+):
+ """Peek at the pending-message buffer without consuming it.
+
+ Returns the current contents of the session's pending message buffer
+ so the frontend can restore the queued-message indicator after a page
+ refresh and clear it correctly once a turn drains the buffer.
+ """
+ await _validate_and_get_session(session_id, user_id)
+ pending = await peek_pending_messages(session_id)
+ return PeekPendingMessagesResponse(
+ messages=[m.content for m in pending],
+ count=len(pending),
+ )
+
+
@router.get(
"/sessions/{session_id}/stream",
)
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 011dd05053..4dc6547515 100644
--- a/autogpt_platform/backend/backend/api/features/chat/routes_test.py
+++ b/autogpt_platform/backend/backend/api/features/chat/routes_test.py
@@ -175,7 +175,7 @@ def test_stream_chat_accepts_20_file_ids(mocker: pytest_mock.MockerFixture):
_mock_stream_internals(mocker)
# Patch workspace lookup as imported by the routes module
mocker.patch(
- "backend.api.features.chat.routes.get_or_create_workspace",
+ "backend.data.workspace.get_or_create_workspace",
return_value=type("W", (), {"id": "ws-1"})(),
)
mock_prisma = mocker.MagicMock()
@@ -227,7 +227,7 @@ def test_file_ids_filters_invalid_uuids(mocker: pytest_mock.MockerFixture):
and NOT passed to the database query."""
_mock_stream_internals(mocker)
mocker.patch(
- "backend.api.features.chat.routes.get_or_create_workspace",
+ "backend.data.workspace.get_or_create_workspace",
return_value=type("W", (), {"id": "ws-1"})(),
)
@@ -265,7 +265,7 @@ def test_file_ids_scoped_to_workspace(mocker: pytest_mock.MockerFixture):
"""The batch query should scope to the user's workspace."""
_mock_stream_internals(mocker)
mocker.patch(
- "backend.api.features.chat.routes.get_or_create_workspace",
+ "backend.data.workspace.get_or_create_workspace",
return_value=type("W", (), {"id": "my-workspace-id"})(),
)
@@ -617,6 +617,246 @@ class TestStreamChatRequestModeValidation:
assert req.mode is None
+# ─── POST /stream queue-fallback (when a turn is already in flight) ──
+
+
+def _mock_stream_queue_internals(
+ mocker: pytest_mock.MockerFixture,
+ *,
+ session_exists: bool = True,
+ turn_in_flight: bool = True,
+ call_count: int = 1,
+):
+ """Mock dependencies for the POST /stream queue-fallback path.
+
+ When ``turn_in_flight`` is True the handler takes the 202 queue branch.
+ """
+ if session_exists:
+ mock_session = mocker.MagicMock()
+ mock_session.id = "sess-1"
+ mocker.patch(
+ "backend.api.features.chat.routes._validate_and_get_session",
+ new_callable=AsyncMock,
+ return_value=mock_session,
+ )
+ else:
+ mocker.patch(
+ "backend.api.features.chat.routes._validate_and_get_session",
+ side_effect=fastapi.HTTPException(
+ status_code=404, detail="Session not found."
+ ),
+ )
+ mocker.patch(
+ "backend.api.features.chat.routes.is_turn_in_flight",
+ new_callable=AsyncMock,
+ return_value=turn_in_flight,
+ )
+ mocker.patch(
+ "backend.api.features.chat.routes.get_global_rate_limits",
+ new_callable=AsyncMock,
+ return_value=(0, 0, None),
+ )
+ mocker.patch(
+ "backend.api.features.chat.routes.check_rate_limit",
+ new_callable=AsyncMock,
+ return_value=None,
+ )
+ mocker.patch(
+ "backend.copilot.pending_message_helpers.get_redis_async",
+ new_callable=AsyncMock,
+ return_value=mocker.MagicMock(),
+ )
+ mocker.patch(
+ "backend.copilot.pending_message_helpers.incr_with_ttl",
+ new_callable=AsyncMock,
+ return_value=call_count,
+ )
+ mocker.patch(
+ "backend.copilot.pending_message_helpers.push_pending_message",
+ new_callable=AsyncMock,
+ return_value=1,
+ )
+ # queue_user_message re-runs is_turn_in_flight via the helper module —
+ # stub that path out too so we don't need a fake stream_registry.
+ mocker.patch(
+ "backend.copilot.pending_message_helpers.get_active_session_meta",
+ new_callable=AsyncMock,
+ return_value=None,
+ )
+
+
+def test_stream_queue_returns_202_when_turn_in_flight(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """Happy path: POST /stream to a session with a live turn → 202 queue."""
+ _mock_stream_queue_internals(mocker)
+
+ response = client.post(
+ "/sessions/sess-1/stream",
+ json={"message": "follow-up", "is_user_message": True},
+ )
+
+ assert response.status_code == 202
+ data = response.json()
+ assert data["buffer_length"] == 1
+ assert "turn_in_flight" in data
+
+
+def test_stream_queue_session_not_found_returns_404(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """If the session doesn't exist or belong to the user, returns 404."""
+ _mock_stream_queue_internals(mocker, session_exists=False)
+
+ response = client.post(
+ "/sessions/bad-sess/stream",
+ json={"message": "hi", "is_user_message": True},
+ )
+ assert response.status_code == 404
+
+
+def test_stream_queue_call_frequency_limit_returns_429(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """Per-user call-frequency cap rejects rapid-fire queued pushes."""
+ from backend.copilot.pending_message_helpers import PENDING_CALL_LIMIT
+
+ _mock_stream_queue_internals(mocker, call_count=PENDING_CALL_LIMIT + 1)
+
+ response = client.post(
+ "/sessions/sess-1/stream",
+ json={"message": "hi", "is_user_message": True},
+ )
+ assert response.status_code == 429
+ assert "Too many queued message requests this minute" in response.json()["detail"]
+
+
+def test_stream_queue_converts_context_dict_to_pending_context(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """StreamChatRequest.context is a raw dict; must be coerced to the
+ typed PendingMessageContext before being pushed onto the buffer."""
+ _mock_stream_queue_internals(mocker)
+ queue_spy = mocker.patch(
+ "backend.copilot.pending_message_helpers.queue_user_message",
+ new_callable=AsyncMock,
+ )
+ from backend.copilot.pending_message_helpers import QueuePendingMessageResponse
+
+ queue_spy.return_value = QueuePendingMessageResponse(
+ buffer_length=1, max_buffer_length=10, turn_in_flight=True
+ )
+
+ response = client.post(
+ "/sessions/sess-1/stream",
+ json={
+ "message": "hi",
+ "is_user_message": True,
+ "context": {"url": "https://example.test", "content": "body"},
+ },
+ )
+
+ assert response.status_code == 202
+ queue_spy.assert_awaited_once()
+ kwargs = queue_spy.await_args.kwargs
+ from backend.copilot.pending_messages import PendingMessageContext
+
+ assert isinstance(kwargs["context"], PendingMessageContext)
+ assert kwargs["context"].url == "https://example.test"
+ assert kwargs["context"].content == "body"
+
+
+def test_stream_queue_passes_none_context_when_omitted(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """When request.context is omitted, the queue call receives context=None."""
+ _mock_stream_queue_internals(mocker)
+ queue_spy = mocker.patch(
+ "backend.copilot.pending_message_helpers.queue_user_message",
+ new_callable=AsyncMock,
+ )
+ from backend.copilot.pending_message_helpers import QueuePendingMessageResponse
+
+ queue_spy.return_value = QueuePendingMessageResponse(
+ buffer_length=1, max_buffer_length=10, turn_in_flight=True
+ )
+
+ response = client.post(
+ "/sessions/sess-1/stream",
+ json={"message": "hi", "is_user_message": True},
+ )
+
+ assert response.status_code == 202
+ queue_spy.assert_awaited_once()
+ assert queue_spy.await_args.kwargs["context"] is None
+
+
+# ─── get_pending_messages (GET /sessions/{session_id}/messages/pending) ─────
+
+
+def test_get_pending_messages_returns_200_with_empty_buffer(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """Happy path: no pending messages returns 200 with empty list."""
+ mocker.patch(
+ "backend.api.features.chat.routes._validate_and_get_session",
+ new_callable=AsyncMock,
+ return_value=mocker.MagicMock(),
+ )
+ mocker.patch(
+ "backend.api.features.chat.routes.peek_pending_messages",
+ new_callable=AsyncMock,
+ return_value=[],
+ )
+
+ response = client.get("/sessions/sess-1/messages/pending")
+
+ assert response.status_code == 200
+ data = response.json()
+ assert data["messages"] == []
+ assert data["count"] == 0
+
+
+def test_get_pending_messages_returns_queued_messages(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """Returns pending messages from buffer without consuming them."""
+ mocker.patch(
+ "backend.api.features.chat.routes._validate_and_get_session",
+ new_callable=AsyncMock,
+ return_value=mocker.MagicMock(),
+ )
+ mocker.patch(
+ "backend.api.features.chat.routes.peek_pending_messages",
+ new_callable=AsyncMock,
+ return_value=[
+ MagicMock(content="first message"),
+ MagicMock(content="second message"),
+ ],
+ )
+
+ response = client.get("/sessions/sess-1/messages/pending")
+
+ assert response.status_code == 200
+ data = response.json()
+ assert data["count"] == 2
+ assert data["messages"] == ["first message", "second message"]
+
+
+def test_get_pending_messages_session_not_found_returns_404(
+ mocker: pytest_mock.MockerFixture,
+) -> None:
+ """If session does not exist or belongs to another user, returns 404."""
+ mocker.patch(
+ "backend.api.features.chat.routes._validate_and_get_session",
+ side_effect=fastapi.HTTPException(status_code=404, detail="Session not found."),
+ )
+
+ response = client.get("/sessions/bad-sess/messages/pending")
+
+ assert response.status_code == 404
+
+
class TestStripInjectedContext:
"""Unit tests for `_strip_injected_context` — the GET-side helper that
hides the server-injected `<user_context>` block from API responses.
diff --git a/autogpt_platform/backend/backend/blocks/autopilot.py b/autogpt_platform/backend/backend/blocks/autopilot.py
index 3a556d286c..ff7c3784ac 100644
--- a/autogpt_platform/backend/backend/blocks/autopilot.py
+++ b/autogpt_platform/backend/backend/blocks/autopilot.py
@@ -356,6 +356,10 @@ class AutoPilotBlock(Block):
)
# Build a lightweight conversation summary from the aggregated data.
+ # When ``result.queued`` is True the prompt rode on an already-
+ # in-flight turn (``run_copilot_turn_via_queue`` queued it and
+ # waited on the existing turn's stream); the aggregated result
+ # is still valid, so the same rendering path applies.
turn_messages: list[dict[str, Any]] = [
{"role": "user", "content": effective_prompt},
]
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.py
index a2813ad881..df52f38d22 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/service.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py
@@ -35,6 +35,17 @@ from backend.copilot.model import (
maybe_append_user_message,
upsert_chat_session,
)
+from backend.copilot.pending_message_helpers import (
+ combine_pending_with_current,
+ drain_pending_safe,
+ pending_texts_from,
+ persist_pending_as_user_rows,
+ persist_session_safe,
+)
+from backend.copilot.pending_messages import (
+ drain_pending_messages,
+ format_pending_as_user_message,
+)
from backend.copilot.prompting import get_baseline_supplement, get_graphiti_supplement
from backend.copilot.response_model import (
StreamBaseResponse,
@@ -75,6 +86,7 @@ from backend.copilot.transcript import (
validate_transcript,
)
from backend.copilot.transcript_builder import TranscriptBuilder
+from backend.data.db_accessors import chat_db
from backend.util import json as util_json
from backend.util.exceptions import NotFoundError
from backend.util.prompt import (
@@ -257,6 +269,11 @@ class _BaselineStreamState:
cost_usd: float | None = None
thinking_stripper: _ThinkingStripper = field(default_factory=_ThinkingStripper)
session_messages: list[ChatMessage] = field(default_factory=list)
+ # Tracks how much of ``assistant_text`` has already been flushed to
+ # ``session.messages`` via mid-loop pending drains, so the ``finally``
+ # block only appends the *new* assistant text (avoiding duplication of
+ # round-1 text when round-1 entries were cleared from session_messages).
+ _flushed_assistant_text_len: int = 0
async def _baseline_llm_caller(
@@ -911,6 +928,7 @@ async def stream_chat_completion_baseline(
permissions: "CopilotPermissions | None" = None,
context: dict[str, str] | None = None,
mode: CopilotMode | None = None,
+ request_arrival_at: float = 0.0,
**_kwargs: Any,
) -> AsyncGenerator[StreamBaseResponse, None]:
"""Baseline LLM with tool calling via OpenAI-compatible API.
@@ -942,7 +960,57 @@ async def stream_chat_completion_baseline(
message_length=len(message or ""),
)
- session = await upsert_chat_session(session)
+ # Capture count *before* the pending drain so is_first_turn and the
+ # transcript staleness check are not skewed by queued messages.
+ _pre_drain_msg_count = len(session.messages)
+
+ # Drain any messages the user queued via POST /messages/pending
+ # while this session was idle (or during a previous turn whose
+ # mid-loop drains missed them).
+ # The drained content is appended after ``message`` so the user's submitted
+ # message remains the leading context (better UX: the user sent their primary
+ # message first, queued follow-ups second). The already-saved user message
+ # in the DB is updated via update_message_content_by_sequence rather than
+ # inserting a new row, because routes.py has already saved the user message
+ # before the executor picks up the turn (using insert_pending_before_last +
+ # persist_session_safe would add a duplicate row at sequence N+1).
+ drained_at_start_pending = await drain_pending_safe(session_id, "[Baseline]")
+ if drained_at_start_pending:
+ logger.info(
+ "[Baseline] Draining %d pending message(s) at turn start for session %s",
+ len(drained_at_start_pending),
+ session_id,
+ )
+ drained_at_start_content = pending_texts_from(drained_at_start_pending)
+ # Chronological combine: pending typed BEFORE this /stream
+ # request's arrival go ahead of ``message``; race-path follow-ups
+ # typed AFTER (queued while /stream was still processing) go
+ # after. See ``combine_pending_with_current`` for details.
+ message = combine_pending_with_current(
+ drained_at_start_pending,
+ message,
+ request_arrival_at=request_arrival_at,
+ )
+ # Update the in-memory content of the already-saved user message
+ # and persist that update by sequence number.
+ last_user_msg = next(
+ (m for m in reversed(session.messages) if m.role == "user"), None
+ )
+ if last_user_msg is None or last_user_msg.sequence is None:
+ # Defensive: routes.py always pre-saves the user message with a
+ # sequence before dispatch, so this is unreachable under normal
+ # flow. Raising instead of a warning-and-continue avoids silent
+ # data loss (in-memory message diverges from the DB row, so the
+ # queued chip would disappear from the UI after refresh without
+ # a corresponding bubble).
+ raise RuntimeError(
+ f"[Baseline] Cannot persist turn-start pending injection: "
+ f"last_user_msg={'missing' if last_user_msg is None else 'has no sequence'}"
+ )
+ last_user_msg.content = message
+ await chat_db().update_message_content_by_sequence(
+ session_id, last_user_msg.sequence, message
+ )
# Select model based on the per-request mode. 'fast' downgrades to
# the cheaper/faster model; everything else keeps the default.
@@ -971,7 +1039,9 @@ async def stream_chat_completion_baseline(
# Build system prompt only on the first turn to avoid mid-conversation
# changes from concurrent chats updating business understanding.
- is_first_turn = len(session.messages) <= 1
+ # Use the pre-drain count so queued pending messages don't incorrectly
+ # flip is_first_turn to False on an actual first turn.
+ is_first_turn = _pre_drain_msg_count <= 1
# Gate context fetch on both first turn AND user message so that assistant-
# role calls (e.g. tool-result submissions) on the first turn don't trigger
# a needless DB lookup for user understanding.
@@ -983,9 +1053,11 @@ async def stream_chat_completion_baseline(
prompt_task = _build_system_prompt(None)
# Run download + prompt build concurrently — both are independent I/O
- # on the request critical path.
+ # on the request critical path. Use the pre-drain count so pending
+ # messages drained at turn start don't spuriously trigger a transcript
+ # load on an actual first turn.
transcript_download: TranscriptDownload | None = None
- if user_id and len(session.messages) > 1:
+ if user_id and _pre_drain_msg_count > 1:
(
(transcript_upload_safe, transcript_download),
(base_system_prompt, understanding),
@@ -1004,6 +1076,17 @@ async def stream_chat_completion_baseline(
# Append user message to transcript after context injection below so the
# transcript receives the prefixed message when user context is available.
+ # NOTE: drained pending messages are folded into the current user
+ # message's content (see the turn-start drain above), so the single
+ # ``transcript_builder.append_user`` call below (covered by the
+ # ``if message and is_user_message`` branch that appends
+ # ``user_message_for_transcript or message``) already records the
+ # combined text in the transcript. Do NOT also append drained items
+ # individually here — on the ``transcript_download is None`` path
+ # that would produce N separate pending entries plus the combined
+ # entry, duplicating the pending content in the JSONL uploaded for
+ # the next turn's ``--resume``.
+
# Generate title for new sessions
if is_user_message and not session.title:
user_messages = [m for m in session.messages if m.role == "user"]
@@ -1025,10 +1108,12 @@ async def stream_chat_completion_baseline(
system_prompt = base_system_prompt + get_baseline_supplement() + graphiti_supplement
# Warm context: pre-load relevant facts from Graphiti on first turn.
+ # Use the pre-drain count so pending messages drained at turn start
+ # don't prevent warm context injection on an actual first turn.
# Stored here but injected into the user message (not the system prompt)
# after openai_messages is built — keeps system prompt static for caching.
warm_ctx: str | None = None
- if graphiti_enabled and user_id and len(session.messages) <= 1:
+ if graphiti_enabled and user_id and _pre_drain_msg_count <= 1:
from backend.copilot.graphiti.context import fetch_warm_context
warm_ctx = await fetch_warm_context(user_id, message or "")
@@ -1078,7 +1163,9 @@ async def stream_chat_completion_baseline(
understanding, message or "", session_id, session.messages
)
if prefixed is not None:
- for msg in openai_messages:
+ # Reverse scan so we update the current turn's user message, not
+ # the first (oldest) one when pending messages were drained.
+ for msg in reversed(openai_messages):
if msg["role"] == "user":
msg["content"] = prefixed
break
@@ -1086,12 +1173,14 @@ async def stream_chat_completion_baseline(
else:
logger.warning("[Baseline] No user message found for context injection")
- # Inject Graphiti warm context into the first user message (not the
- # system prompt) so the system prompt stays static and cacheable.
+ # Inject Graphiti warm context into the current turn's user message (not
+ # the system prompt) so the system prompt stays static and cacheable.
# warm_ctx is already wrapped in <temporal_context>.
# Appended AFTER user_context so <user_context> stays at the very start.
+ # Reverse scan so we update the current turn's user message, not the
+ # oldest one when pending messages were drained.
if warm_ctx:
- for msg in openai_messages:
+ for msg in reversed(openai_messages):
if msg["role"] == "user":
existing = msg.get("content", "")
if isinstance(existing, str):
@@ -1197,9 +1286,26 @@ async def stream_chat_completion_baseline(
# Bind extracted module-level callbacks to this request's state/session
# using functools.partial so they satisfy the Protocol signatures.
_bound_llm_caller = partial(_baseline_llm_caller, state=state)
- _bound_tool_executor = partial(
- _baseline_tool_executor, state=state, user_id=user_id, session=session
- )
+
+ # ``session`` is reassigned after each mid-turn ``persist_session_safe``
+ # call (``upsert_chat_session`` returns a fresh ``model_copy``). Holding
+ # the object via ``partial(session=session)`` would pin tool executions
+ # to the *original* object — any post-persist ``session.successful_agent_runs``
+ # mutation from a run_agent tool call would then land on the stale copy
+ # and be lost on the final persist. Wrap in a 1-element holder and read
+ # the current binding lazily so the executor always sees the latest session.
+ _session_holder: list[ChatSession] = [session]
+
+ async def _bound_tool_executor(
+ tool_call: LLMToolCall, tools: Sequence[Any]
+ ) -> ToolCallResult:
+ return await _baseline_tool_executor(
+ tool_call,
+ tools,
+ state=state,
+ user_id=user_id,
+ session=_session_holder[0],
+ )
_bound_conversation_updater = partial(
_baseline_conversation_updater,
@@ -1223,6 +1329,124 @@ async def stream_chat_completion_baseline(
yield evt
state.pending_events.clear()
+ # Inject any messages the user queued while the turn was
+ # running. ``tool_call_loop`` mutates ``openai_messages``
+ # in-place, so appending here means the model sees the new
+ # messages on its next LLM call.
+ #
+ # IMPORTANT: skip when the loop has already finished (no
+ # more LLM calls are coming). ``tool_call_loop`` yields
+ # a final ``ToolCallLoopResult`` on both paths:
+ # - natural finish: ``finished_naturally=True``
+ # - hit max_iterations: ``finished_naturally=False``
+ # and ``iterations >= max_iterations``
+ # In either case the loop is about to return on the next
+ # ``async for`` step, so draining here would silently
+ # lose the message (the user sees 202 but the model never
+ # reads the text). Those messages stay in the buffer and
+ # get picked up at the start of the next turn.
+ is_final_yield = (
+ loop_result.finished_naturally
+ or loop_result.iterations >= _MAX_TOOL_ROUNDS
+ )
+ if is_final_yield:
+ continue
+ try:
+ pending = await drain_pending_messages(session_id)
+ except Exception:
+ logger.warning(
+ "[Baseline] mid-loop drain_pending_messages failed for session %s",
+ session_id,
+ exc_info=True,
+ )
+ pending = []
+ if pending:
+ # Flush any buffered assistant/tool messages from completed
+ # rounds into session.messages BEFORE appending the pending
+ # user message. ``_baseline_conversation_updater`` only
+ # records assistant+tool rounds into ``state.session_messages``
+ # — they are normally batch-flushed in the finally block.
+ # Without this in-order flush, the mid-loop pending user
+ # message lands before the preceding round's assistant/tool
+ # entries, producing chronologically-wrong session.messages
+ # on persist (user interposed between an assistant tool_call
+ # and its tool-result), which breaks OpenAI tool-call ordering
+ # invariants on the next turn's replay.
+ #
+ # Also persist any assistant text from text-only rounds (rounds
+ # with no tool calls, which ``_baseline_conversation_updater``
+ # does NOT record in session_messages). If we only update
+ # ``_flushed_assistant_text_len`` without persisting the text,
+ # that text is silently lost: the finally block only appends
+ # assistant_text[_flushed_assistant_text_len:], so text generated
+ # before this drain never reaches session.messages.
+ recorded_text = "".join(
+ m.content or ""
+ for m in state.session_messages
+ if m.role == "assistant"
+ )
+ unflushed_text = state.assistant_text[
+ state._flushed_assistant_text_len :
+ ]
+ text_only_text = (
+ unflushed_text[len(recorded_text) :]
+ if unflushed_text.startswith(recorded_text)
+ else unflushed_text
+ )
+ if text_only_text.strip():
+ session.messages.append(
+ ChatMessage(role="assistant", content=text_only_text)
+ )
+ for _buffered in state.session_messages:
+ session.messages.append(_buffered)
+ state.session_messages.clear()
+ # Record how much assistant_text has been covered by the
+ # structured entries just flushed, so the finally block's
+ # final-text dedup doesn't re-append rounds already persisted.
+ state._flushed_assistant_text_len = len(state.assistant_text)
+
+ # Persist the assistant/tool flush BEFORE the pending append
+ # so a later pending-persist failure can roll back the
+ # pending rows without also discarding LLM output.
+ session = await persist_session_safe(session, "[Baseline]")
+ # ``upsert_chat_session`` may return a *new* ``ChatSession``
+ # instance (e.g. when a concurrent title update has written a
+ # newer title to Redis, it returns ``session.model_copy``).
+ # Keep ``_session_holder`` in sync so subsequent tool rounds
+ # executed via ``_bound_tool_executor`` see the fresh session
+ # — any tool-side mutations on the stale object would be
+ # discarded when the new one is persisted in the ``finally``.
+ _session_holder[0] = session
+
+ # ``format_pending_as_user_message`` embeds file attachments
+ # and context URL/page content into the content string so
+ # the in-session transcript is a faithful copy of what the
+ # model actually saw. We also mirror each push into
+ # ``openai_messages`` so the model's next LLM round sees it.
+ #
+ # Pre-compute the formatted dicts once so both the openai
+ # messages append and the content_of lookup inside the
+ # shared helper use the same string — and so ``on_rollback``
+ # can trim ``openai_messages`` to the recorded anchor.
+ formatted_by_pm = {
+ id(pm): format_pending_as_user_message(pm) for pm in pending
+ }
+ _openai_anchor = len(openai_messages)
+ for pm in pending:
+ openai_messages.append(formatted_by_pm[id(pm)])
+
+ def _trim_openai_on_rollback(_session_anchor: int) -> None:
+ del openai_messages[_openai_anchor:]
+
+ await persist_pending_as_user_rows(
+ session,
+ transcript_builder,
+ pending,
+ log_prefix="[Baseline]",
+ content_of=lambda pm: formatted_by_pm[id(pm)]["content"],
+ on_rollback=_trim_openai_on_rollback,
+ )
+
if loop_result and not loop_result.finished_naturally:
limit_msg = (
f"Exceeded {_MAX_TOOL_ROUNDS} tool-call rounds "
@@ -1263,6 +1487,11 @@ async def stream_chat_completion_baseline(
yield StreamError(errorText=error_msg, code="baseline_error")
# Still persist whatever we got
finally:
+ # Pending messages are drained atomically at turn start and
+ # between tool rounds, so there's nothing to clear in finally.
+ # Any message pushed after the final drain window stays in the
+ # buffer and gets picked up at the start of the next turn.
+
# Set cost attributes on OTEL span before closing
if _trace_ctx is not None:
try:
@@ -1338,7 +1567,11 @@ async def stream_chat_completion_baseline(
# no tool calls, i.e. the natural finish). Only add it if the
# conversation updater didn't already record it as part of a
# tool-call round (which would have empty response_text).
- final_text = state.assistant_text
+ # Only consider assistant text produced AFTER the last mid-loop
+ # flush. ``_flushed_assistant_text_len`` tracks the prefix already
+ # persisted via structured session_messages during mid-loop pending
+ # drains; including it here would duplicate those rounds.
+ final_text = state.assistant_text[state._flushed_assistant_text_len :]
if state.session_messages:
# Strip text already captured in tool-call round messages
recorded = "".join(
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py b/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py
index 881018175f..a0e55d843f 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py
@@ -1010,3 +1010,204 @@ class TestBaselineCostExtraction:
assert state.cost_usd is None
assert state.turn_prompt_tokens == 1000
assert state.turn_completion_tokens == 500
+
+
+class TestMidLoopPendingFlushOrdering:
+ """Regression test for the mid-loop pending drain ordering invariant.
+
+ ``_baseline_conversation_updater`` records assistant+tool entries from
+ each tool-call round into ``state.session_messages``; the finally block
+ of ``stream_chat_completion_baseline`` batch-flushes them into
+ ``session.messages`` at the end of the turn.
+
+ The mid-loop pending drain appends pending user messages directly to
+ ``session.messages``. Without flushing ``state.session_messages`` first,
+ the pending user message lands BEFORE the preceding round's assistant+
+ tool entries in the final persisted ``session.messages`` — which
+ produces a malformed tool-call/tool-result ordering on the next turn's
+ replay.
+
+ This test documents the invariant by replaying the production flush
+ sequence against an in-memory state.
+ """
+
+ def test_flush_then_append_preserves_chronological_order(self):
+ """Mid-loop drain must flush state.session_messages before appending
+ the pending user message, so the final order matches the
+ chronological execution order.
+ """
+ # Initial state: user turn already appended by maybe_append_user_message
+ session_messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="original user turn"),
+ ]
+ state = _BaselineStreamState()
+
+ # Round 1 completes: conversation_updater buffers assistant+tool
+ # entries into state.session_messages (but does NOT write to
+ # session.messages yet).
+ builder = TranscriptBuilder()
+ builder.append_user("original user turn")
+ response = LLMLoopResponse(
+ response_text="calling search",
+ tool_calls=[LLMToolCall(id="tc_1", name="search", arguments="{}")],
+ raw_response=None,
+ prompt_tokens=0,
+ completion_tokens=0,
+ )
+ tool_results = [
+ ToolCallResult(
+ tool_call_id="tc_1", tool_name="search", content="search output"
+ ),
+ ]
+ openai_messages: list = []
+ _baseline_conversation_updater(
+ openai_messages,
+ response,
+ tool_results=tool_results,
+ transcript_builder=builder,
+ state=state,
+ model="test-model",
+ )
+ # state.session_messages should now hold the round-1 assistant + tool
+ assert len(state.session_messages) == 2
+ assert state.session_messages[0].role == "assistant"
+ assert state.session_messages[1].role == "tool"
+
+ # --- Mid-loop pending drain (production code pattern) ---
+ # Flush first, THEN append pending. This is the ordering fix.
+ for _buffered in state.session_messages:
+ session_messages.append(_buffered)
+ state.session_messages.clear()
+ session_messages.append(
+ ChatMessage(role="user", content="pending mid-loop message")
+ )
+
+ # Round 2 completes: new assistant+tool entries buffer again.
+ response2 = LLMLoopResponse(
+ response_text="another call",
+ tool_calls=[LLMToolCall(id="tc_2", name="calc", arguments="{}")],
+ raw_response=None,
+ prompt_tokens=0,
+ completion_tokens=0,
+ )
+ tool_results2 = [
+ ToolCallResult(
+ tool_call_id="tc_2", tool_name="calc", content="calc output"
+ ),
+ ]
+ _baseline_conversation_updater(
+ openai_messages,
+ response2,
+ tool_results=tool_results2,
+ transcript_builder=builder,
+ state=state,
+ model="test-model",
+ )
+
+ # --- Finally-block flush (end of turn) ---
+ for msg in state.session_messages:
+ session_messages.append(msg)
+
+ # Assert chronological order: original user, round-1 assistant,
+ # round-1 tool, pending user, round-2 assistant, round-2 tool.
+ assert [m.role for m in session_messages] == [
+ "user",
+ "assistant",
+ "tool",
+ "user",
+ "assistant",
+ "tool",
+ ]
+ assert session_messages[0].content == "original user turn"
+ assert session_messages[3].content == "pending mid-loop message"
+ # The assistant message carrying tool_call tc_1 must be immediately
+ # followed by its tool result — no user message interposed.
+ assert session_messages[1].role == "assistant"
+ assert session_messages[1].tool_calls is not None
+ assert session_messages[1].tool_calls[0]["id"] == "tc_1"
+ assert session_messages[2].role == "tool"
+ assert session_messages[2].tool_call_id == "tc_1"
+ # Same invariant for the round after the pending user.
+ assert session_messages[4].tool_calls is not None
+ assert session_messages[4].tool_calls[0]["id"] == "tc_2"
+ assert session_messages[5].tool_call_id == "tc_2"
+
+ def test_flushed_assistant_text_len_prevents_duplicate_final_text(self):
+ """After mid-loop drain clears state.session_messages, the finally
+ block must not re-append assistant text from rounds already flushed.
+
+ ``state.assistant_text`` accumulates ALL rounds' text, but
+ ``state.session_messages`` only holds entries from rounds AFTER the
+ last mid-loop flush. Without ``_flushed_assistant_text_len``, the
+ ``finally`` block's ``startswith(recorded)`` check fails because
+ ``recorded`` only covers post-flush rounds, and the full
+ ``assistant_text`` is appended — duplicating pre-flush rounds.
+ """
+ state = _BaselineStreamState()
+ session_messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="user turn"),
+ ]
+
+ # Simulate round 1 text accumulation (as _bound_llm_caller does)
+ state.assistant_text += "calling search"
+
+ # Round 1 conversation_updater buffers structured entries
+ builder = TranscriptBuilder()
+ builder.append_user("user turn")
+ response1 = LLMLoopResponse(
+ response_text="calling search",
+ tool_calls=[LLMToolCall(id="tc_1", name="search", arguments="{}")],
+ raw_response=None,
+ prompt_tokens=0,
+ completion_tokens=0,
+ )
+ _baseline_conversation_updater(
+ [],
+ response1,
+ tool_results=[
+ ToolCallResult(
+ tool_call_id="tc_1", tool_name="search", content="result"
+ )
+ ],
+ transcript_builder=builder,
+ state=state,
+ model="test-model",
+ )
+
+ # Mid-loop drain: flush + clear + record flushed text length
+ for _buffered in state.session_messages:
+ session_messages.append(_buffered)
+ state.session_messages.clear()
+ state._flushed_assistant_text_len = len(state.assistant_text)
+ session_messages.append(ChatMessage(role="user", content="pending message"))
+
+ # Simulate round 2 text accumulation
+ state.assistant_text += "final answer"
+
+ # Round 2: natural finish (no tool calls → no session_messages entry)
+
+ # --- Finally block logic (production code) ---
+ for msg in state.session_messages:
+ session_messages.append(msg)
+
+ final_text = state.assistant_text[state._flushed_assistant_text_len :]
+ if state.session_messages:
+ recorded = "".join(
+ m.content or "" for m in state.session_messages if m.role == "assistant"
+ )
+ if final_text.startswith(recorded):
+ final_text = final_text[len(recorded) :]
+ if final_text.strip():
+ session_messages.append(ChatMessage(role="assistant", content=final_text))
+
+ # The final assistant message should only contain round-2 text,
+ # not the round-1 text that was already flushed mid-loop.
+ assistant_msgs = [m for m in session_messages if m.role == "assistant"]
+ # Round-1 structured assistant (from mid-loop flush)
+ assert assistant_msgs[0].content == "calling search"
+ assert assistant_msgs[0].tool_calls is not None
+ # Round-2 final text (from finally block)
+ assert assistant_msgs[1].content == "final answer"
+ assert assistant_msgs[1].tool_calls is None
+ # Crucially: only 2 assistant messages, not 3 (no duplicate)
+ assert len(assistant_msgs) == 2
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor.py b/autogpt_platform/backend/backend/copilot/executor/processor.py
index adcb35a3dd..8a25e1a1d9 100644
--- a/autogpt_platform/backend/backend/copilot/executor/processor.py
+++ b/autogpt_platform/backend/backend/copilot/executor/processor.py
@@ -346,7 +346,9 @@ class CoPilotProcessor:
# Stream chat completion and publish chunks to Redis.
# stream_and_publish wraps the raw stream with registry
- # publishing (shared with collect_copilot_response).
+ # publishing so subscribers on the session Redis stream
+ # (e.g. wait_for_session_result, SSE clients) receive the
+ # same events as they are produced.
raw_stream = stream_fn(
session_id=entry.session_id,
message=entry.message if entry.message else None,
@@ -357,6 +359,7 @@ class CoPilotProcessor:
mode=effective_mode,
model=entry.model,
permissions=entry.permissions,
+ request_arrival_at=entry.request_arrival_at,
)
async for chunk in stream_registry.stream_and_publish(
session_id=entry.session_id,
diff --git a/autogpt_platform/backend/backend/copilot/executor/utils.py b/autogpt_platform/backend/backend/copilot/executor/utils.py
index 29ef58a7be..b96e1821a1 100644
--- a/autogpt_platform/backend/backend/copilot/executor/utils.py
+++ b/autogpt_platform/backend/backend/copilot/executor/utils.py
@@ -169,6 +169,15 @@ class CoPilotExecutionEntry(BaseModel):
forwards its parent's permissions so the sub can't escalate). ``None``
means the worker applies no filter."""
+ request_arrival_at: float = 0.0
+ """Unix-epoch seconds (server clock) when the originating HTTP
+ ``/stream`` request arrived. The executor's turn-start drain uses
+ this to decide whether each pending message was typed BEFORE or AFTER
+ the turn's ``current`` message, and orders the combined user bubble
+ chronologically. Defaults to ``0.0`` for backward compatibility with
+ queue messages written before this field existed (they sort as "all
+ pending before current" — the pre-fix behaviour)."""
+
class CancelCoPilotEvent(BaseModel):
"""Event to cancel a CoPilot operation."""
@@ -191,6 +200,7 @@ async def enqueue_copilot_turn(
mode: CopilotMode | None = None,
model: CopilotLlmModel | None = None,
permissions: CopilotPermissions | None = None,
+ request_arrival_at: float = 0.0,
) -> None:
"""Enqueue a CoPilot task for processing by the executor service.
@@ -220,6 +230,7 @@ async def enqueue_copilot_turn(
mode=mode,
model=model,
permissions=permissions,
+ request_arrival_at=request_arrival_at,
)
queue_client = await get_async_copilot_queue()
diff --git a/autogpt_platform/backend/backend/copilot/pending_message_helpers.py b/autogpt_platform/backend/backend/copilot/pending_message_helpers.py
new file mode 100644
index 0000000000..4340b743f0
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/pending_message_helpers.py
@@ -0,0 +1,384 @@
+"""Shared helpers for draining and injecting pending messages.
+
+Used by both the baseline and SDK copilot paths to avoid duplicating
+the try/except drain, format, insert, and persist patterns.
+
+Also provides the call-rate-limit check for the queue endpoint so
+routes.py stays free of Redis/Lua details.
+"""
+
+import logging
+from typing import TYPE_CHECKING, Callable
+
+from fastapi import HTTPException
+from pydantic import BaseModel
+
+from backend.copilot.model import ChatMessage, upsert_chat_session
+from backend.copilot.pending_messages import (
+ MAX_PENDING_MESSAGES,
+ PendingMessage,
+ PendingMessageContext,
+ drain_pending_messages,
+ format_pending_as_user_message,
+ push_pending_message,
+)
+from backend.copilot.stream_registry import get_session as get_active_session_meta
+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
+
+if TYPE_CHECKING:
+ from backend.copilot.model import ChatSession
+ from backend.copilot.transcript_builder import TranscriptBuilder
+
+logger = logging.getLogger(__name__)
+
+# Call-frequency cap for the pending-message endpoint. The token-budget
+# check guards against overspend but not rapid-fire pushes from a client
+# with a large budget.
+PENDING_CALL_LIMIT = 30
+PENDING_CALL_WINDOW_SECONDS = 60
+_PENDING_CALL_KEY_PREFIX = "copilot:pending:calls:"
+
+
+async def is_turn_in_flight(session_id: str) -> bool:
+ """Return ``True`` when a copilot turn is actively running for *session_id*.
+
+ Used by the unified POST /stream entry point 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.
+ """
+ active = await get_active_session_meta(session_id)
+ return active is not None and active.status == "running"
+
+
+class QueuePendingMessageResponse(BaseModel):
+ """Response returned by ``POST /stream`` with status 202 when a message
+ is queued because the session already has a turn in flight.
+
+ - ``buffer_length``: how many messages are now in the session's
+ pending buffer (after this push)
+ - ``max_buffer_length``: the per-session cap (server-side constant)
+ - ``turn_in_flight``: ``True`` if a copilot turn was running when
+ we checked — purely informational for UX feedback. Always ``True``
+ for responses from ``POST /stream`` with status 202.
+ """
+
+ buffer_length: int
+ max_buffer_length: int
+ turn_in_flight: bool
+
+
+async def queue_user_message(
+ *,
+ session_id: str,
+ message: str,
+ context: PendingMessageContext | None = None,
+ file_ids: list[str] | None = None,
+) -> QueuePendingMessageResponse:
+ """Push *message* into the per-session pending buffer.
+
+ The shared primitive for "a message arrived while a turn is in flight" —
+ called from the unified POST /stream handler and the autopilot block.
+ Call-frequency rate limiting is the caller's responsibility (HTTP path
+ enforces it; internal block callers skip it).
+ """
+ pending = PendingMessage(
+ content=message,
+ file_ids=file_ids or [],
+ context=context,
+ )
+ new_len = await push_pending_message(session_id, pending)
+ return QueuePendingMessageResponse(
+ buffer_length=new_len,
+ max_buffer_length=MAX_PENDING_MESSAGES,
+ turn_in_flight=await is_turn_in_flight(session_id),
+ )
+
+
+async def queue_pending_for_http(
+ *,
+ session_id: str,
+ user_id: str,
+ message: str,
+ context: dict[str, str] | None,
+ file_ids: list[str] | None,
+) -> QueuePendingMessageResponse:
+ """HTTP-facing wrapper around :func:`queue_user_message`.
+
+ Owns the HTTP-only concerns that sat inline in ``stream_chat_post``:
+
+ 1. Per-user call-rate cap (429 on overflow).
+ 2. File-ID sanitisation against the user's own workspace.
+ 3. ``{url, content}`` dict → ``PendingMessageContext`` coercion.
+ 4. Push via ``queue_user_message``.
+
+ Raises :class:`HTTPException` with status 429 if the rate cap is hit;
+ otherwise returns the ``QueuePendingMessageResponse`` the handler can
+ serialise 1:1 into the 202 body.
+ """
+ call_count = await check_pending_call_rate(user_id)
+ if call_count > PENDING_CALL_LIMIT:
+ raise HTTPException(
+ status_code=429,
+ detail=(
+ f"Too many queued message requests this minute: limit is "
+ f"{PENDING_CALL_LIMIT} per {PENDING_CALL_WINDOW_SECONDS}s "
+ "across all sessions"
+ ),
+ )
+
+ sanitized_file_ids: list[str] | None = None
+ if file_ids:
+ files = await resolve_workspace_files(user_id, file_ids)
+ sanitized_file_ids = [wf.id for wf in files] or None
+
+ # ``PendingMessageContext`` uses the default ``extra='ignore'`` so
+ # unknown keys in the loose HTTP-level ``context`` dict are silently
+ # dropped rather than raising ``ValidationError`` + 500ing (sentry
+ # r3105553772). The strict mode would only help protect against
+ # typos, but the upstream ``StreamChatRequest.context: dict[str, str]``
+ # is already schemaless, so the strict mode adds no real safety.
+ queue_context = PendingMessageContext.model_validate(context) if context else None
+ return await queue_user_message(
+ session_id=session_id,
+ message=message,
+ context=queue_context,
+ file_ids=sanitized_file_ids,
+ )
+
+
+async def check_pending_call_rate(user_id: str) -> int:
+ """Increment and return the per-user push counter for the current window.
+
+ The counter is **user-global**: it counts pushes across ALL sessions
+ belonging to the user, not per-session. This prevents a client from
+ bypassing the cap by spreading rapid pushes across many sessions.
+
+ Returns the new call count. Raises nothing — callers compare the
+ return value against ``PENDING_CALL_LIMIT`` and decide what to do.
+ Fails open (returns 0) if Redis is unavailable so the endpoint stays
+ usable during Redis hiccups.
+ """
+ try:
+ redis = await get_redis_async()
+ key = f"{_PENDING_CALL_KEY_PREFIX}{user_id}"
+ return await incr_with_ttl(redis, key, PENDING_CALL_WINDOW_SECONDS)
+ except Exception:
+ logger.warning(
+ "pending_message_helpers: call-rate check failed for user=%s, failing open",
+ user_id,
+ )
+ return 0
+
+
+async def drain_pending_safe(
+ session_id: str, log_prefix: str = ""
+) -> list[PendingMessage]:
+ """Drain the pending buffer and return the full ``PendingMessage`` objects.
+
+ Returns ``[]`` on any Redis error so callers can always treat the
+ result as a plain list. Callers that only need the rendered string
+ (turn-start injection, auto-continue combined prompt) wrap this with
+ :func:`pending_texts_from` — we return the structured objects so the
+ re-queue rollback path can preserve ``file_ids`` / ``context`` that
+ would otherwise be stripped by a text-only conversion.
+ """
+ try:
+ return await drain_pending_messages(session_id)
+ except Exception:
+ logger.warning(
+ "%s drain_pending_messages failed, skipping",
+ log_prefix or "pending_messages",
+ exc_info=True,
+ )
+ return []
+
+
+def pending_texts_from(pending: list[PendingMessage]) -> list[str]:
+ """Render a list of ``PendingMessage`` objects into plain text strings.
+
+ Shared helper for the two callers that need the rendered form:
+ turn-start injection (bundles the pending block into the user prompt)
+ and the auto-continue combined-message path.
+ """
+ return [format_pending_as_user_message(pm)["content"] for pm in pending]
+
+
+def combine_pending_with_current(
+ pending: list[PendingMessage],
+ current_message: str | None,
+ *,
+ request_arrival_at: float,
+) -> str:
+ """Order pending messages around *current_message* by typing time.
+
+ Pending messages whose ``enqueued_at`` is strictly greater than
+ ``request_arrival_at`` were typed AFTER the user hit enter to start
+ the current turn (the "race" path: queued into the pending buffer
+ while ``/stream`` was still processing on the server). They belong
+ chronologically AFTER the current message.
+
+ Pending messages whose ``enqueued_at`` is less than or equal to
+ ``request_arrival_at`` were typed BEFORE the current turn — usually
+ from a prior in-flight window that auto-continue didn't consume.
+ They belong BEFORE the current message.
+
+ Stable-sort within each bucket preserves enqueue order for messages
+ typed in the same phase. Legacy ``PendingMessage`` objects with no
+ ``enqueued_at`` (written by older workers, defaulted to 0.0) sort as
+ "before everything" — the pre-fix behaviour, which is a safe default
+ for the rare queue entries that outlived a deploy.
+ """
+ before: list[PendingMessage] = []
+ after: list[PendingMessage] = []
+ for pm in pending:
+ if request_arrival_at > 0 and pm.enqueued_at > request_arrival_at:
+ after.append(pm)
+ else:
+ before.append(pm)
+ parts = pending_texts_from(before)
+ if current_message and current_message.strip():
+ parts.append(current_message)
+ parts.extend(pending_texts_from(after))
+ return "\n\n".join(parts)
+
+
+def insert_pending_before_last(session: "ChatSession", texts: list[str]) -> None:
+ """Insert pending messages into *session* just before the last message.
+
+ Pending messages were queued during the previous turn, so they belong
+ chronologically before the current user message that was already
+ appended via ``maybe_append_user_message``. Inserting at ``len-1``
+ preserves that order: [...history, pending_1, pending_2, current_msg].
+
+ The caller must have already appended the current user message before
+ calling this function. If ``session.messages`` is unexpectedly empty,
+ a warning is logged and the messages are appended at index 0 so they
+ are not silently lost.
+ """
+ if not texts:
+ return
+ if not session.messages:
+ logger.warning(
+ "insert_pending_before_last: session.messages is empty — "
+ "current user message was not appended before drain; "
+ "inserting pending messages at index 0"
+ )
+ insert_idx = max(0, len(session.messages) - 1)
+ for i, content in enumerate(texts):
+ session.messages.insert(
+ insert_idx + i, ChatMessage(role="user", content=content)
+ )
+
+
+async def persist_session_safe(
+ session: "ChatSession", log_prefix: str = ""
+) -> "ChatSession":
+ """Persist *session* to the DB, returning the (possibly updated) session.
+
+ Swallows transient DB errors so a failing persist doesn't discard
+ messages already popped from Redis — the turn continues from memory.
+ """
+ try:
+ return await upsert_chat_session(session)
+ except Exception as err:
+ logger.warning(
+ "%s Failed to persist pending messages: %s",
+ log_prefix or "pending_messages",
+ err,
+ )
+ return session
+
+
+async def persist_pending_as_user_rows(
+ session: "ChatSession",
+ transcript_builder: "TranscriptBuilder",
+ pending: list[PendingMessage],
+ *,
+ log_prefix: str,
+ content_of: Callable[[PendingMessage], str] = lambda pm: pm.content,
+ on_rollback: Callable[[int], None] | None = None,
+) -> bool:
+ """Append ``pending`` as user rows to *session* + *transcript_builder*,
+ persist, and roll back + re-queue if the persist silently failed.
+
+ This is the shared mid-turn follow-up persist used by both the baseline
+ and SDK paths — they differ only in (a) how they derive the displayed
+ string from a ``PendingMessage`` and (b) what extra per-path state
+ (e.g. ``openai_messages``) needs trimming on rollback. Those variance
+ points are exposed as ``content_of`` and ``on_rollback``.
+
+ Flow:
+ 1. Snapshot transcript + record the session.messages length.
+ 2. Append one user row per pending message to both stores.
+ 3. ``persist_session_safe`` — swallowed errors mean no sequences get
+ back-filled, which we use as the failure signal.
+ 4. If any newly-appended row has ``sequence is None`` → rollback:
+ delete the appended rows, restore the transcript snapshot, call
+ ``on_rollback(anchor)`` for the caller's own state, then re-push
+ each ``PendingMessage`` into the primary pending buffer so the
+ next turn-start drain picks them up.
+
+ Returns ``True`` when the rows were persisted with sequences, ``False``
+ when the rollback path fired. Callers can use this to decide whether
+ to log success or continue a retry loop.
+ """
+ if not pending:
+ return True
+
+ session_anchor = len(session.messages)
+ transcript_snapshot = transcript_builder.snapshot()
+
+ for pm in pending:
+ content = content_of(pm)
+ session.messages.append(ChatMessage(role="user", content=content))
+ transcript_builder.append_user(content=content)
+
+ # ``persist_session_safe`` may return a ``model_copy`` of *session* (e.g.
+ # when ``upsert_chat_session`` patches a concurrently-updated title).
+ # Do NOT reassign the caller's reference — the caller already pushed the
+ # rows into its own ``session.messages`` above, and rollback below MUST
+ # delete from that same list. Inspect the returned object only to learn
+ # whether sequences were back-filled; if so, copy them onto the caller's
+ # objects so the session stays internally consistent for downstream
+ # ``append_and_save_message`` calls.
+ persisted = await persist_session_safe(session, log_prefix)
+ persisted_tail = persisted.messages[session_anchor:]
+ if len(persisted_tail) == len(pending) and all(
+ m.sequence is not None for m in persisted_tail
+ ):
+ for caller_msg, persisted_msg in zip(
+ session.messages[session_anchor:], persisted_tail
+ ):
+ caller_msg.sequence = persisted_msg.sequence
+ newly_appended = session.messages[session_anchor:]
+
+ if any(m.sequence is None for m in newly_appended):
+ logger.warning(
+ "%s Mid-turn follow-up persist did not back-fill sequences; "
+ "rolling back %d row(s) and re-queueing into the primary buffer",
+ log_prefix,
+ len(pending),
+ )
+ del session.messages[session_anchor:]
+ transcript_builder.restore(transcript_snapshot)
+ if on_rollback is not None:
+ on_rollback(session_anchor)
+ for pm in pending:
+ try:
+ await push_pending_message(session.session_id, pm)
+ except Exception:
+ logger.exception(
+ "%s Failed to re-queue mid-turn follow-up on rollback",
+ log_prefix,
+ )
+ return False
+
+ logger.info(
+ "%s Persisted %d mid-turn follow-up user row(s)",
+ log_prefix,
+ len(pending),
+ )
+ return True
diff --git a/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py b/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py
new file mode 100644
index 0000000000..b3ddc6269f
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py
@@ -0,0 +1,472 @@
+"""Unit tests for pending_message_helpers."""
+
+from typing import Any
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from backend.copilot import pending_message_helpers as helpers_module
+from backend.copilot.pending_message_helpers import (
+ PENDING_CALL_LIMIT,
+ check_pending_call_rate,
+ combine_pending_with_current,
+ drain_pending_safe,
+ insert_pending_before_last,
+ persist_session_safe,
+)
+from backend.copilot.pending_messages import PendingMessage
+
+# ── check_pending_call_rate ────────────────────────────────────────────
+
+
+async def test_check_pending_call_rate_returns_count(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(
+ helpers_module, "get_redis_async", AsyncMock(return_value=MagicMock())
+ )
+ monkeypatch.setattr(helpers_module, "incr_with_ttl", AsyncMock(return_value=3))
+
+ result = await check_pending_call_rate("user-1")
+ assert result == 3
+
+
+async def test_check_pending_call_rate_fails_open_on_redis_error(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(
+ helpers_module,
+ "get_redis_async",
+ AsyncMock(side_effect=ConnectionError("down")),
+ )
+
+ result = await check_pending_call_rate("user-1")
+ assert result == 0
+
+
+async def test_check_pending_call_rate_at_limit(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(
+ helpers_module, "get_redis_async", AsyncMock(return_value=MagicMock())
+ )
+ monkeypatch.setattr(
+ helpers_module,
+ "incr_with_ttl",
+ AsyncMock(return_value=PENDING_CALL_LIMIT + 1),
+ )
+
+ result = await check_pending_call_rate("user-1")
+ assert result > PENDING_CALL_LIMIT
+
+
+# ── drain_pending_safe ─────────────────────────────────────────────────
+
+
+async def test_drain_pending_safe_returns_pending_messages(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """``drain_pending_safe`` now returns the structured ``PendingMessage``
+ objects (not pre-formatted strings) so the auto-continue re-queue path
+ can preserve ``file_ids`` / ``context`` on rollback."""
+ msgs = [
+ PendingMessage(content="hello", file_ids=["f1"]),
+ PendingMessage(content="world"),
+ ]
+ monkeypatch.setattr(
+ helpers_module, "drain_pending_messages", AsyncMock(return_value=msgs)
+ )
+
+ result = await drain_pending_safe("sess-1")
+ assert result == msgs
+ # Structured metadata survives — the bug r3105523410 guard.
+ assert result[0].file_ids == ["f1"]
+
+
+async def test_drain_pending_safe_returns_empty_on_error(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(
+ helpers_module,
+ "drain_pending_messages",
+ AsyncMock(side_effect=RuntimeError("redis down")),
+ )
+
+ result = await drain_pending_safe("sess-1", "[Test]")
+ assert result == []
+
+
+async def test_drain_pending_safe_empty_buffer(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(
+ helpers_module, "drain_pending_messages", AsyncMock(return_value=[])
+ )
+
+ result = await drain_pending_safe("sess-1")
+ assert result == []
+
+
+# ── combine_pending_with_current ───────────────────────────────────────
+
+
+def test_combine_before_current_when_pending_older() -> None:
+ """Pending typed before the /stream request → goes ahead of current
+ (prior-turn / inter-turn case)."""
+ pending = [
+ PendingMessage(content="older_a", enqueued_at=100.0),
+ PendingMessage(content="older_b", enqueued_at=110.0),
+ ]
+ result = combine_pending_with_current(
+ pending, "current_msg", request_arrival_at=120.0
+ )
+ assert result == "older_a\n\nolder_b\n\ncurrent_msg"
+
+
+def test_combine_after_current_when_pending_newer() -> None:
+ """Pending queued AFTER the /stream request arrived → goes after
+ current. This is the race path where user hits enter twice in quick
+ succession (second press goes through the queue endpoint while the
+ first /stream is still processing)."""
+ pending = [
+ PendingMessage(content="race_followup", enqueued_at=125.0),
+ ]
+ result = combine_pending_with_current(
+ pending, "current_msg", request_arrival_at=120.0
+ )
+ assert result == "current_msg\n\nrace_followup"
+
+
+def test_combine_mixed_before_and_after() -> None:
+ """Mixed bucket: older items first, current, then newer race items."""
+ pending = [
+ PendingMessage(content="way_older", enqueued_at=50.0),
+ PendingMessage(content="race_fast_follow", enqueued_at=125.0),
+ PendingMessage(content="also_older", enqueued_at=80.0),
+ ]
+ result = combine_pending_with_current(
+ pending, "current_msg", request_arrival_at=120.0
+ )
+ # Enqueue order preserved within each bucket (stable partition).
+ assert result == "way_older\n\nalso_older\n\ncurrent_msg\n\nrace_fast_follow"
+
+
+def test_combine_no_current_joins_pending() -> None:
+ """Auto-continue case: no current message, just drained pending."""
+ pending = [PendingMessage(content="a"), PendingMessage(content="b")]
+ result = combine_pending_with_current(pending, None, request_arrival_at=0.0)
+ assert result == "a\n\nb"
+
+
+def test_combine_legacy_zero_timestamp_sorts_before() -> None:
+ """A ``PendingMessage`` from before this field existed (default 0.0)
+ should sort as "before everything" — safe pre-fix behaviour."""
+ pending = [PendingMessage(content="legacy", enqueued_at=0.0)]
+ result = combine_pending_with_current(
+ pending, "current_msg", request_arrival_at=120.0
+ )
+ assert result == "legacy\n\ncurrent_msg"
+
+
+def test_combine_missing_request_arrival_falls_back_to_before() -> None:
+ """If the HTTP handler didn't stamp ``request_arrival_at`` (0.0
+ default — older queue entries) the combine degrades gracefully to
+ the pre-fix behaviour: all pending goes before current."""
+ pending = [
+ PendingMessage(content="a", enqueued_at=500.0),
+ PendingMessage(content="b", enqueued_at=1000.0),
+ ]
+ result = combine_pending_with_current(pending, "current", request_arrival_at=0.0)
+ assert result == "a\n\nb\n\ncurrent"
+
+
+# ── insert_pending_before_last ─────────────────────────────────────────
+
+
+def _make_session(*contents: str) -> Any:
+ session = MagicMock()
+ session.messages = [MagicMock(role="user", content=c) for c in contents]
+ return session
+
+
+def test_insert_pending_before_last_single_existing_message() -> None:
+ session = _make_session("current")
+ insert_pending_before_last(session, ["queued"])
+ assert session.messages[0].content == "queued"
+ assert session.messages[1].content == "current"
+
+
+def test_insert_pending_before_last_multiple_pending() -> None:
+ session = _make_session("current")
+ insert_pending_before_last(session, ["p1", "p2"])
+ contents = [m.content for m in session.messages]
+ assert contents == ["p1", "p2", "current"]
+
+
+def test_insert_pending_before_last_empty_session() -> None:
+ session = _make_session()
+ insert_pending_before_last(session, ["queued"])
+ assert session.messages[0].content == "queued"
+
+
+def test_insert_pending_before_last_no_texts_is_noop() -> None:
+ session = _make_session("current")
+ insert_pending_before_last(session, [])
+ assert len(session.messages) == 1
+
+
+# ── persist_session_safe ───────────────────────────────────────────────
+
+
+async def test_persist_session_safe_returns_updated_session(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ original = MagicMock()
+ updated = MagicMock()
+ monkeypatch.setattr(
+ helpers_module, "upsert_chat_session", AsyncMock(return_value=updated)
+ )
+
+ result = await persist_session_safe(original, "[Test]")
+ assert result is updated
+
+
+async def test_persist_session_safe_returns_original_on_error(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ original = MagicMock()
+ monkeypatch.setattr(
+ helpers_module,
+ "upsert_chat_session",
+ AsyncMock(side_effect=Exception("db error")),
+ )
+
+ result = await persist_session_safe(original, "[Test]")
+ assert result is original
+
+
+# ── persist_pending_as_user_rows ───────────────────────────────────────
+
+
+class _FakeTranscript:
+ """Minimal TranscriptBuilder shim — records append_user + snapshot/restore."""
+
+ def __init__(self) -> None:
+ self.entries: list[str] = []
+
+ def append_user(self, content: str, uuid: str | None = None) -> None:
+ self.entries.append(content)
+
+ def snapshot(self) -> list[str]:
+ return list(self.entries)
+
+ def restore(self, snap: list[str]) -> None:
+ self.entries = list(snap)
+
+
+def _make_chat_message_class(
+ monkeypatch: pytest.MonkeyPatch,
+) -> Any:
+ """Return a simple ChatMessage stand-in that tracks sequence."""
+
+ class _Msg:
+ def __init__(self, role: str, content: str) -> None:
+ self.role = role
+ self.content = content
+ self.sequence: int | None = None
+
+ monkeypatch.setattr(helpers_module, "ChatMessage", _Msg)
+ return _Msg
+
+
+async def test_persist_pending_empty_list_is_noop(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
+
+ _make_chat_message_class(monkeypatch)
+ session = MagicMock()
+ session.messages = []
+ tb = _FakeTranscript()
+ monkeypatch.setattr(helpers_module, "upsert_chat_session", AsyncMock())
+ monkeypatch.setattr(helpers_module, "push_pending_message", AsyncMock())
+
+ ok = await persist_pending_as_user_rows(session, tb, [], log_prefix="[T]")
+ assert ok is True
+ assert session.messages == []
+ assert tb.entries == []
+
+
+async def test_persist_pending_happy_path_appends_and_returns_true(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
+ from backend.copilot.pending_messages import PendingMessage as PM
+
+ _make_chat_message_class(monkeypatch)
+ session = MagicMock()
+ session.session_id = "sess"
+ session.messages = []
+ tb = _FakeTranscript()
+
+ async def _fake_upsert(sess: Any) -> Any:
+ # Simulate the DB back-filling sequence numbers on success.
+ for i, m in enumerate(sess.messages):
+ m.sequence = i
+ return sess
+
+ monkeypatch.setattr(helpers_module, "upsert_chat_session", _fake_upsert)
+ push_mock = AsyncMock()
+ monkeypatch.setattr(helpers_module, "push_pending_message", push_mock)
+
+ pending = [PM(content="a"), PM(content="b")]
+ ok = await persist_pending_as_user_rows(session, tb, pending, log_prefix="[T]")
+ assert ok is True
+ assert [m.content for m in session.messages] == ["a", "b"]
+ assert tb.entries == ["a", "b"]
+ push_mock.assert_not_awaited()
+
+
+async def test_persist_pending_rollback_when_sequence_missing(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
+ from backend.copilot.pending_messages import PendingMessage as PM
+
+ _make_chat_message_class(monkeypatch)
+ session = MagicMock()
+ session.session_id = "sess"
+ # Prior state — anchor point is len(messages) before the helper runs.
+ session.messages = []
+ tb = _FakeTranscript()
+ tb.entries = ["earlier-entry"]
+
+ async def _fake_upsert_fails_silently(sess: Any) -> Any:
+ # Simulate the "persist swallowed the error" branch — sequences stay None.
+ return sess
+
+ monkeypatch.setattr(
+ helpers_module, "upsert_chat_session", _fake_upsert_fails_silently
+ )
+ push_mock = AsyncMock()
+ monkeypatch.setattr(helpers_module, "push_pending_message", push_mock)
+
+ pending = [PM(content="a"), PM(content="b")]
+ ok = await persist_pending_as_user_rows(session, tb, pending, log_prefix="[T]")
+
+ assert ok is False
+ # Rollback: session.messages trimmed to anchor, transcript restored.
+ assert session.messages == []
+ assert tb.entries == ["earlier-entry"]
+ # Both pending messages re-queued.
+ assert push_mock.await_count == 2
+ assert push_mock.await_args_list[0].args[1] is pending[0]
+ assert push_mock.await_args_list[1].args[1] is pending[1]
+
+
+async def test_persist_pending_rollback_calls_on_rollback_hook(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Baseline's openai_messages trim runs via the on_rollback hook."""
+ from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
+ from backend.copilot.pending_messages import PendingMessage as PM
+
+ _make_chat_message_class(monkeypatch)
+ session = MagicMock()
+ session.session_id = "sess"
+ session.messages = []
+ tb = _FakeTranscript()
+
+ async def _fails(sess: Any) -> Any:
+ return sess
+
+ monkeypatch.setattr(helpers_module, "upsert_chat_session", _fails)
+ monkeypatch.setattr(helpers_module, "push_pending_message", AsyncMock())
+
+ on_rollback_calls: list[int] = []
+
+ def _on_rollback(anchor: int) -> None:
+ on_rollback_calls.append(anchor)
+
+ await persist_pending_as_user_rows(
+ session,
+ tb,
+ [PM(content="x")],
+ log_prefix="[T]",
+ on_rollback=_on_rollback,
+ )
+ assert on_rollback_calls == [0]
+
+
+async def test_persist_pending_uses_custom_content_of(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
+ from backend.copilot.pending_messages import PendingMessage as PM
+
+ _make_chat_message_class(monkeypatch)
+ session = MagicMock()
+ session.session_id = "sess"
+ session.messages = []
+ tb = _FakeTranscript()
+
+ async def _ok(sess: Any) -> Any:
+ for i, m in enumerate(sess.messages):
+ m.sequence = i
+ return sess
+
+ monkeypatch.setattr(helpers_module, "upsert_chat_session", _ok)
+ monkeypatch.setattr(helpers_module, "push_pending_message", AsyncMock())
+
+ await persist_pending_as_user_rows(
+ session,
+ tb,
+ [PM(content="raw")],
+ log_prefix="[T]",
+ content_of=lambda pm: f"FORMATTED:{pm.content}",
+ )
+ assert session.messages[0].content == "FORMATTED:raw"
+ assert tb.entries == ["FORMATTED:raw"]
+
+
+async def test_persist_pending_swallows_requeue_errors(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A broken push_pending_message on rollback must not raise upward —
+ the rollback still needs to trim state even if re-queue fails."""
+ from backend.copilot.pending_message_helpers import persist_pending_as_user_rows
+ from backend.copilot.pending_messages import PendingMessage as PM
+
+ _make_chat_message_class(monkeypatch)
+ session = MagicMock()
+ session.session_id = "sess"
+ session.messages = []
+ tb = _FakeTranscript()
+
+ async def _fails(sess: Any) -> Any:
+ return sess
+
+ monkeypatch.setattr(helpers_module, "upsert_chat_session", _fails)
+ monkeypatch.setattr(
+ helpers_module,
+ "push_pending_message",
+ AsyncMock(side_effect=RuntimeError("redis down")),
+ )
+
+ ok = await persist_pending_as_user_rows(
+ session, tb, [PM(content="x")], log_prefix="[T]"
+ )
+ # Still returns False (rolled back) — exception was logged + swallowed.
+ assert ok is False
diff --git a/autogpt_platform/backend/backend/copilot/pending_messages.py b/autogpt_platform/backend/backend/copilot/pending_messages.py
new file mode 100644
index 0000000000..ff6eed8b59
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/pending_messages.py
@@ -0,0 +1,450 @@
+"""Pending-message buffer for in-flight copilot turns.
+
+When a user sends a new message while a copilot turn is already executing,
+instead of blocking the frontend (or queueing a brand-new turn after the
+current one finishes), we want the new message to be *injected into the
+running turn* — appended between tool-call rounds so the model sees it
+before its next LLM call.
+
+This module provides the cross-process buffer that makes that possible:
+
+- **Producer** (chat API route): pushes a pending message to Redis and
+ publishes a notification on a pub/sub channel.
+- **Consumer** (executor running the turn): on each tool-call round,
+ drains the buffer and appends the pending messages to the conversation.
+
+The Redis list is the durable store; the pub/sub channel is a fast
+wake-up hint for long-idle consumers (not used by default, but available
+for future blocking-wait semantics).
+
+A hard cap of ``MAX_PENDING_MESSAGES`` per session prevents abuse. The
+buffer is trimmed to the latest ``MAX_PENDING_MESSAGES`` on every push.
+"""
+
+import json
+import logging
+import time
+from typing import Any, cast
+
+from pydantic import BaseModel, Field, ValidationError
+
+from backend.data.redis_client import get_redis_async
+from backend.data.redis_helpers import capped_rpush
+
+logger = logging.getLogger(__name__)
+
+# Per-session cap. Higher values risk a runaway consumer; lower values
+# risk dropping user input under heavy typing. 10 was chosen as a
+# reasonable ceiling — a user typing faster than the copilot can drain
+# between tool rounds is already an unusual usage pattern.
+MAX_PENDING_MESSAGES = 10
+
+# Redis key + TTL. The buffer is ephemeral: if a turn completes or the
+# executor dies, the pending messages should either have been drained
+# already or are safe to drop (the user can resend).
+_PENDING_KEY_PREFIX = "copilot:pending:"
+_PENDING_CHANNEL_PREFIX = "copilot:pending:notify:"
+_PENDING_TTL_SECONDS = 3600 # 1 hour — matches stream_ttl default
+
+# Secondary queue that carries drained-but-awaiting-persist PendingMessages
+# from the MCP tool wrapper (which drains the primary buffer and injects
+# into tool output for the LLM) to sdk/service.py's _dispatch_response
+# handler for StreamToolOutputAvailable, which pops and persists them as a
+# separate user row chronologically after the tool_result row. This is the
+# hand-off between "Claude saw the follow-up mid-turn" (wrapper) and "UI
+# renders a user bubble for it" (service). Rollback path re-queues into
+# the PRIMARY buffer so the next turn-start drain picks them up if the
+# user-row persist fails.
+_PERSIST_QUEUE_KEY_PREFIX = "copilot:pending-persist:"
+
+# Payload sent on the pub/sub notify channel. Subscribers treat any
+# message as a wake-up hint; the value itself is not meaningful.
+_NOTIFY_PAYLOAD = "1"
+
+
+class PendingMessageContext(BaseModel):
+ """Structured page context attached to a pending message.
+
+ Default ``extra='ignore'`` (pydantic's default): unknown keys from
+ the loose HTTP-level ``StreamChatRequest.context: dict[str, str]``
+ are silently dropped rather than raising ``ValidationError`` on
+ forward-compat additions. The strict ``extra='forbid'`` mode was
+ removed after sentry r3105553772 — strict validation at this
+ boundary only added a 500 footgun; the upstream request model is
+ already schemaless so strict mode protects nothing.
+ """
+
+ url: str | None = Field(default=None, max_length=2_000)
+ content: str | None = Field(default=None, max_length=32_000)
+
+
+class PendingMessage(BaseModel):
+ """A user message queued for injection into an in-flight turn."""
+
+ content: str = Field(min_length=1, max_length=32_000)
+ file_ids: list[str] = Field(default_factory=list, max_length=20)
+ context: PendingMessageContext | None = None
+ # Wall-clock time (unix seconds, float) the message was queued by the
+ # user. Used by the turn-start drain to order pending relative to the
+ # turn's ``current`` message: items typed *before* the current's
+ # /stream arrival go ahead of it; items typed *after* (race path,
+ # queued while the /stream HTTP request was still processing) go
+ # after. Defaults to 0.0 for backward compatibility with entries
+ # written before this field existed — those sort as "before everything"
+ # which matches the pre-fix behaviour.
+ enqueued_at: float = Field(default_factory=time.time)
+
+
+def _buffer_key(session_id: str) -> str:
+ return f"{_PENDING_KEY_PREFIX}{session_id}"
+
+
+def _notify_channel(session_id: str) -> str:
+ return f"{_PENDING_CHANNEL_PREFIX}{session_id}"
+
+
+def _decode_redis_item(item: Any) -> str:
+ """Decode a redis-py list item to a str.
+
+ redis-py returns ``bytes`` when ``decode_responses=False`` and ``str``
+ when ``decode_responses=True``. This helper handles both so callers
+ don't have to repeat the isinstance guard.
+ """
+ return item.decode("utf-8") if isinstance(item, bytes) else str(item)
+
+
+async def push_pending_message(
+ session_id: str,
+ message: PendingMessage,
+) -> int:
+ """Append a pending message to the session's buffer.
+
+ Returns the new buffer length. Enforces ``MAX_PENDING_MESSAGES`` by
+ trimming from the left (oldest) — the newest message always wins if
+ the user has been typing faster than the copilot can drain.
+
+ Delegates to :func:`backend.data.redis_helpers.capped_rpush` so RPUSH
+ + LTRIM + EXPIRE + LLEN run atomically (MULTI/EXEC) in one round
+ trip; a concurrent drain (LPOP) can no longer observe the list
+ temporarily over ``MAX_PENDING_MESSAGES``.
+
+ Note on durability: if the executor turn crashes after a push but before
+ the drain window runs, the message remains in Redis until the TTL expires
+ (``_PENDING_TTL_SECONDS``, currently 1 hour). It is delivered on the
+ next turn that drains the buffer. If no turn runs within the TTL the
+ message is silently dropped; the user may resend it.
+ """
+ redis = await get_redis_async()
+ key = _buffer_key(session_id)
+ payload = message.model_dump_json()
+
+ new_length = await capped_rpush(
+ redis,
+ key,
+ payload,
+ max_len=MAX_PENDING_MESSAGES,
+ ttl_seconds=_PENDING_TTL_SECONDS,
+ )
+
+ # Fire-and-forget notify. Subscribers use this as a wake-up hint;
+ # the buffer itself is authoritative so a lost notify is harmless.
+ try:
+ await redis.publish(_notify_channel(session_id), _NOTIFY_PAYLOAD)
+ except Exception as e: # pragma: no cover
+ logger.warning("pending_messages: publish failed for %s: %s", session_id, e)
+
+ logger.info(
+ "pending_messages: pushed message to session=%s (buffer_len=%d)",
+ session_id,
+ new_length,
+ )
+ return new_length
+
+
+async def drain_pending_messages(session_id: str) -> list[PendingMessage]:
+ """Atomically pop all pending messages for *session_id*.
+
+ Returns them in enqueue order (oldest first). Uses ``LPOP`` with a
+ count so the read+delete is a single Redis round trip. If the list
+ is empty or missing, returns ``[]``.
+ """
+ redis = await get_redis_async()
+ key = _buffer_key(session_id)
+
+ # Redis LPOP with count (Redis 6.2+) returns None for missing key,
+ # empty list if we somehow race an empty key, or the popped items.
+ # Draining MAX_PENDING_MESSAGES at once is safe because the push side
+ # uses RPUSH + LTRIM(-MAX_PENDING_MESSAGES, -1) to cap the list to that
+ # same value, so the list can never hold more items than we drain here.
+ # If the cap is raised on the push side, raise the drain count here too
+ # (or switch to a loop drain).
+ lpop_result = await redis.lpop(key, MAX_PENDING_MESSAGES) # type: ignore[assignment]
+ if not lpop_result:
+ return []
+ raw_popped: list[Any] = list(lpop_result)
+
+ # redis-py may return bytes or str depending on decode_responses.
+ decoded: list[str] = [_decode_redis_item(item) for item in raw_popped]
+
+ messages: list[PendingMessage] = []
+ for payload in decoded:
+ try:
+ messages.append(PendingMessage.model_validate(json.loads(payload)))
+ except (json.JSONDecodeError, ValidationError, TypeError, ValueError) as e:
+ logger.warning(
+ "pending_messages: dropping malformed entry for %s: %s",
+ session_id,
+ e,
+ )
+
+ if messages:
+ logger.info(
+ "pending_messages: drained %d messages for session=%s",
+ len(messages),
+ session_id,
+ )
+ return messages
+
+
+async def peek_pending_count(session_id: str) -> int:
+ """Return the current buffer length without consuming it."""
+ redis = await get_redis_async()
+ length = await cast("Any", redis.llen(_buffer_key(session_id)))
+ return int(length)
+
+
+async def peek_pending_messages(session_id: str) -> list[PendingMessage]:
+ """Return pending messages without consuming them.
+
+ Uses LRANGE 0 -1 to read all items in enqueue order (oldest first)
+ without removing them. Returns an empty list if the buffer is empty
+ or the session has no pending messages.
+ """
+ redis = await get_redis_async()
+ key = _buffer_key(session_id)
+ items = await cast("Any", redis.lrange(key, 0, -1))
+ if not items:
+ return []
+ messages: list[PendingMessage] = []
+ for item in items:
+ try:
+ messages.append(
+ PendingMessage.model_validate(json.loads(_decode_redis_item(item)))
+ )
+ except (json.JSONDecodeError, ValidationError, TypeError, ValueError) as e:
+ logger.warning(
+ "pending_messages: dropping malformed peek entry for %s: %s",
+ session_id,
+ e,
+ )
+ return messages
+
+
+async def _clear_pending_messages_unsafe(session_id: str) -> None:
+ """Drop the session's pending buffer — **not** the normal turn cleanup.
+
+ Named ``_unsafe`` because reaching for this at turn end drops queued
+ follow-ups on the floor instead of running them (the bug fixed by
+ commit b64be73). The atomic ``LPOP`` drain at turn start is the
+ primary consumer; anything pushed after the drain window belongs to
+ the next turn by definition. Retained only as an operator/debug
+ escape hatch for manually clearing a stuck session and as a fixture
+ in the unit tests.
+ """
+ redis = await get_redis_async()
+ await redis.delete(_buffer_key(session_id))
+
+
+# Per-message and total-block caps for inline tool-boundary injection.
+# Per-message keeps a single long paste from dominating; the total cap
+# keeps the follow-up block small relative to the 100 KB MCP truncation
+# boundary so tool output always stays the larger share of the wrapper
+# return value.
+_FOLLOWUP_CONTENT_MAX_CHARS = 2_000
+_FOLLOWUP_TOTAL_MAX_CHARS = 6_000
+
+
+def _persist_queue_key(session_id: str) -> str:
+ return f"{_PERSIST_QUEUE_KEY_PREFIX}{session_id}"
+
+
+async def stash_pending_for_persist(
+ session_id: str,
+ messages: list[PendingMessage],
+) -> None:
+ """Enqueue drained PendingMessages for UI-row persistence.
+
+ Writes each message as a JSON payload to
+ ``copilot:pending-persist:{session_id}``. The SDK service's
+ tool-result dispatch handler LPOPs this queue right after appending
+ the tool_result row to ``session.messages``, so the resulting user
+ row lands at the correct chronological position (after the tool
+ output the follow-up was drained against).
+
+ Fire-and-forget on Redis failures: a stash failure means Claude
+ still saw the follow-up in tool output (the injection step ran
+ first), so the only consequence is a missing UI bubble. Logged
+ so it can be spotted.
+ """
+ if not messages:
+ return
+ try:
+ redis = await get_redis_async()
+ key = _persist_queue_key(session_id)
+ payloads = [m.model_dump_json() for m in messages]
+ await redis.rpush(key, *payloads) # type: ignore[misc]
+ await redis.expire(key, _PENDING_TTL_SECONDS) # type: ignore[misc]
+ except Exception:
+ logger.warning(
+ "pending_messages: failed to stash %d message(s) for persist "
+ "(session=%s); UI will miss the follow-up bubble but Claude "
+ "already saw the content in tool output",
+ len(messages),
+ session_id,
+ exc_info=True,
+ )
+
+
+async def drain_pending_for_persist(session_id: str) -> list[PendingMessage]:
+ """Atomically drain the persist queue for *session_id*.
+
+ Returns the queued ``PendingMessage`` objects in enqueue order (oldest
+ first). Returns ``[]`` on any error so the service-layer caller can
+ always treat the result as a plain list. Called by sdk/service.py
+ after appending a tool_result row to ``session.messages``.
+ """
+ try:
+ redis = await get_redis_async()
+ key = _persist_queue_key(session_id)
+ lpop_result = await redis.lpop( # type: ignore[assignment]
+ key, MAX_PENDING_MESSAGES
+ )
+ except Exception:
+ logger.warning(
+ "pending_messages: drain_pending_for_persist failed for session=%s",
+ session_id,
+ exc_info=True,
+ )
+ return []
+ if not lpop_result:
+ return []
+ raw_popped: list[Any] = list(lpop_result)
+ messages: list[PendingMessage] = []
+ for item in raw_popped:
+ try:
+ messages.append(
+ PendingMessage.model_validate(json.loads(_decode_redis_item(item)))
+ )
+ except (json.JSONDecodeError, ValidationError, TypeError, ValueError) as e:
+ logger.warning(
+ "pending_messages: dropping malformed persist-queue entry "
+ "for %s: %s",
+ session_id,
+ e,
+ )
+ return messages
+
+
+def format_pending_as_followup(pending: list[PendingMessage]) -> str:
+ """Render drained pending messages as a ``<user_follow_up>`` block.
+
+ Used by the SDK tool-boundary injection path to surface queued user
+ text inside a tool result so the model reads it on the next LLM round,
+ without starting a separate turn. Wrapped in a stable XML-style tag so
+ the shared system-prompt supplement can teach the model to treat the
+ contents as the user's continuation of their request, not as tool
+ output. Each message is capped to keep the block bounded even if the
+ user pastes long content.
+ """
+ if not pending:
+ return ""
+ rendered: list[str] = []
+ total_chars = 0
+ dropped = 0
+ for idx, pm in enumerate(pending, start=1):
+ text = pm.content
+ if len(text) > _FOLLOWUP_CONTENT_MAX_CHARS:
+ text = text[:_FOLLOWUP_CONTENT_MAX_CHARS] + "… [truncated]"
+ entry = f"Message {idx}:\n{text}"
+ if pm.context and pm.context.url:
+ entry += f"\n[Page URL: {pm.context.url}]"
+ if pm.file_ids:
+ entry += "\n[Attached files: " + ", ".join(pm.file_ids) + "]"
+ if total_chars + len(entry) > _FOLLOWUP_TOTAL_MAX_CHARS:
+ dropped = len(pending) - idx + 1
+ break
+ rendered.append(entry)
+ total_chars += len(entry)
+ if dropped:
+ rendered.append(f"… [{dropped} more message(s) truncated]")
+ body = "\n\n".join(rendered)
+ return (
+ "<user_follow_up>\n"
+ "The user sent the following message(s) while this tool was running. "
+ "Treat them as a continuation of their current request — acknowledge "
+ "and act on them in your next response. Do not echo these tags back.\n\n"
+ f"{body}\n"
+ "</user_follow_up>"
+ )
+
+
+async def drain_and_format_for_injection(
+ session_id: str,
+ *,
+ log_prefix: str,
+) -> str:
+ """Drain the pending buffer and produce a ``<user_follow_up>`` block.
+
+ Shared entry point for every mid-turn injection site (``PostToolUse``
+ hook for MCP + built-in tools, baseline between-rounds drain, etc.).
+ Also stashes the drained messages on the persist queue so the service
+ layer appends a real user row after the tool_result it rode in on —
+ giving the UI a correctly-ordered bubble.
+
+ Returns an empty string if nothing was queued or Redis failed; callers
+ can pass the result straight to ``additionalContext``.
+ """
+ if not session_id:
+ return ""
+ try:
+ pending = await drain_pending_messages(session_id)
+ except Exception:
+ logger.warning(
+ "%s drain_pending_messages failed (session=%s); skipping injection",
+ log_prefix,
+ session_id,
+ exc_info=True,
+ )
+ return ""
+ if not pending:
+ return ""
+ logger.info(
+ "%s Injected %d user follow-up(s) into tool output (session=%s)",
+ log_prefix,
+ len(pending),
+ session_id,
+ )
+ await stash_pending_for_persist(session_id, pending)
+ return format_pending_as_followup(pending)
+
+
+def format_pending_as_user_message(message: PendingMessage) -> dict[str, Any]:
+ """Shape a ``PendingMessage`` into the OpenAI-format user message dict.
+
+ Used by the baseline tool-call loop when injecting the buffered
+ message into the conversation. Context/file metadata (if any) is
+ embedded into the content so the model sees everything in one block.
+ """
+ parts: list[str] = [message.content]
+ if message.context:
+ if message.context.url:
+ parts.append(f"\n\n[Page URL: {message.context.url}]")
+ if message.context.content:
+ parts.append(f"\n\n[Page content]\n{message.context.content}")
+ if message.file_ids:
+ parts.append(
+ "\n\n[Attached files]\n"
+ + "\n".join(f"- file_id={fid}" for fid in message.file_ids)
+ + "\nUse read_workspace_file with the file_id to access file contents."
+ )
+ return {"role": "user", "content": "".join(parts)}
diff --git a/autogpt_platform/backend/backend/copilot/pending_messages_test.py b/autogpt_platform/backend/backend/copilot/pending_messages_test.py
new file mode 100644
index 0000000000..06f809579f
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/pending_messages_test.py
@@ -0,0 +1,614 @@
+"""Tests for the copilot pending-messages buffer.
+
+Uses a fake async Redis client so the tests don't require a real Redis
+instance (the backend test suite's DB/Redis fixtures are heavyweight
+and pull in the full app startup).
+"""
+
+import asyncio
+import json
+from typing import Any
+
+import pytest
+
+from backend.copilot import pending_messages as pm_module
+from backend.copilot.pending_messages import (
+ MAX_PENDING_MESSAGES,
+ PendingMessage,
+ PendingMessageContext,
+ _clear_pending_messages_unsafe,
+ drain_and_format_for_injection,
+ drain_pending_for_persist,
+ drain_pending_messages,
+ format_pending_as_followup,
+ format_pending_as_user_message,
+ peek_pending_count,
+ peek_pending_messages,
+ push_pending_message,
+ stash_pending_for_persist,
+)
+
+# ── Fake Redis ──────────────────────────────────────────────────────
+
+
+class _FakeRedis:
+ def __init__(self) -> None:
+ # Values are ``str | bytes`` because real redis-py returns
+ # bytes when ``decode_responses=False``; the drain path must
+ # handle both and our tests exercise both.
+ self.lists: dict[str, list[str | bytes]] = {}
+ self.published: list[tuple[str, str]] = []
+
+ async def rpush(self, key: str, *values: Any) -> int:
+ lst = self.lists.setdefault(key, [])
+ lst.extend(values)
+ return len(lst)
+
+ async def ltrim(self, key: str, start: int, stop: int) -> None:
+ lst = self.lists.get(key, [])
+ # Redis LTRIM stop is inclusive; -1 means the last element.
+ if stop == -1:
+ self.lists[key] = lst[start:]
+ else:
+ self.lists[key] = lst[start : stop + 1]
+
+ async def expire(self, key: str, seconds: int) -> int:
+ # Fake doesn't enforce TTL — just acknowledge.
+ return 1
+
+ async def publish(self, channel: str, payload: str) -> int:
+ self.published.append((channel, payload))
+ return 1
+
+ async def lpop(self, key: str, count: int) -> list[str | bytes] | None:
+ lst = self.lists.get(key)
+ if not lst:
+ return None
+ popped = lst[:count]
+ self.lists[key] = lst[count:]
+ return popped
+
+ async def llen(self, key: str) -> int:
+ return len(self.lists.get(key, []))
+
+ async def lrange(self, key: str, start: int, stop: int) -> list[str | bytes]:
+ lst = self.lists.get(key, [])
+ # Redis LRANGE stop is inclusive; -1 means the last element.
+ if stop == -1:
+ return list(lst[start:])
+ return list(lst[start : stop + 1])
+
+ async def delete(self, key: str) -> int:
+ if key in self.lists:
+ del self.lists[key]
+ return 1
+ return 0
+
+ def pipeline(self, transaction: bool = True) -> "_FakePipeline":
+ # Returns a fake pipeline that records ops and replays them in
+ # order on ``execute()``. Used by ``capped_rpush`` (push_pending_message)
+ # and ``incr_with_ttl`` (call-rate check) via MULTI/EXEC.
+ return _FakePipeline(self)
+
+ async def incr(self, key: str) -> int:
+ # Used by incr_with_ttl's pipeline.
+ current = int(self.lists.get(key, [0])[0]) if self.lists.get(key) else 0
+ current += 1
+ # We abuse the same lists dict for simple counters — store [count].
+ self.lists[key] = [str(current)]
+ return current
+
+
+class _FakePipeline:
+ """Async pipeline shim matching the redis-py MULTI/EXEC surface."""
+
+ def __init__(self, parent: "_FakeRedis") -> None:
+ self._parent = parent
+ self._ops: list[tuple[str, tuple[Any, ...], dict[str, Any]]] = []
+
+ # Each method just records the op; dispatching happens in execute().
+ def rpush(self, key: str, *values: Any) -> "_FakePipeline":
+ self._ops.append(("rpush", (key, *values), {}))
+ return self
+
+ def ltrim(self, key: str, start: int, stop: int) -> "_FakePipeline":
+ self._ops.append(("ltrim", (key, start, stop), {}))
+ return self
+
+ def expire(self, key: str, seconds: int, **kw: Any) -> "_FakePipeline":
+ self._ops.append(("expire", (key, seconds), kw))
+ return self
+
+ def llen(self, key: str) -> "_FakePipeline":
+ self._ops.append(("llen", (key,), {}))
+ return self
+
+ def incr(self, key: str) -> "_FakePipeline":
+ self._ops.append(("incr", (key,), {}))
+ return self
+
+ async def execute(self) -> list[Any]:
+ results: list[Any] = []
+ for name, args, _kw in self._ops:
+ fn = getattr(self._parent, name)
+ results.append(await fn(*args))
+ return results
+
+ # Support `async with pipeline() as pipe:` too.
+ async def __aenter__(self) -> "_FakePipeline":
+ return self
+
+ async def __aexit__(self, *a: Any) -> None:
+ return None
+
+
+def fake_redis(monkeypatch: pytest.MonkeyPatch) -> _FakeRedis:
+ redis = _FakeRedis()
+
+ async def _get_redis_async() -> _FakeRedis:
+ return redis
+
+ monkeypatch.setattr(pm_module, "get_redis_async", _get_redis_async)
+ return redis
+
+
+# ── Basic push / drain ──────────────────────────────────────────────
+
+
+async def test_push_and_drain_single_message(fake_redis: _FakeRedis) -> None:
+ length = await push_pending_message("sess1", PendingMessage(content="hello"))
+ assert length == 1
+ assert await peek_pending_count("sess1") == 1
+
+ drained = await drain_pending_messages("sess1")
+ assert len(drained) == 1
+ assert drained[0].content == "hello"
+ assert await peek_pending_count("sess1") == 0
+
+
+async def test_push_and_drain_preserves_order(fake_redis: _FakeRedis) -> None:
+ for i in range(3):
+ await push_pending_message("sess2", PendingMessage(content=f"msg {i}"))
+
+ drained = await drain_pending_messages("sess2")
+ assert [m.content for m in drained] == ["msg 0", "msg 1", "msg 2"]
+
+
+async def test_drain_empty_returns_empty_list(fake_redis: _FakeRedis) -> None:
+ assert await drain_pending_messages("nope") == []
+
+
+# ── Buffer cap ──────────────────────────────────────────────────────
+
+
+async def test_cap_drops_oldest_when_exceeded(fake_redis: _FakeRedis) -> None:
+ # Push MAX_PENDING_MESSAGES + 3 messages
+ for i in range(MAX_PENDING_MESSAGES + 3):
+ await push_pending_message("sess3", PendingMessage(content=f"m{i}"))
+
+ # Buffer should be clamped to MAX
+ assert await peek_pending_count("sess3") == MAX_PENDING_MESSAGES
+
+ drained = await drain_pending_messages("sess3")
+ assert len(drained) == MAX_PENDING_MESSAGES
+ # Oldest 3 dropped — we should only see m3..m(MAX+2)
+ assert drained[0].content == "m3"
+ assert drained[-1].content == f"m{MAX_PENDING_MESSAGES + 2}"
+
+
+# ── Clear ───────────────────────────────────────────────────────────
+
+
+async def test_clear_removes_buffer(fake_redis: _FakeRedis) -> None:
+ await push_pending_message("sess4", PendingMessage(content="x"))
+ await push_pending_message("sess4", PendingMessage(content="y"))
+ await _clear_pending_messages_unsafe("sess4")
+ assert await peek_pending_count("sess4") == 0
+
+
+async def test_clear_is_idempotent(fake_redis: _FakeRedis) -> None:
+ # Clearing an already-empty buffer should not raise
+ await _clear_pending_messages_unsafe("sess_empty")
+ await _clear_pending_messages_unsafe("sess_empty")
+
+
+# ── Publish hook ────────────────────────────────────────────────────
+
+
+async def test_push_publishes_notification(fake_redis: _FakeRedis) -> None:
+ await push_pending_message("sess5", PendingMessage(content="hi"))
+ assert ("copilot:pending:notify:sess5", "1") in fake_redis.published
+
+
+# ── Format helper ───────────────────────────────────────────────────
+
+
+def test_format_pending_plain_text() -> None:
+ msg = PendingMessage(content="just text")
+ out = format_pending_as_user_message(msg)
+ assert out == {"role": "user", "content": "just text"}
+
+
+def test_format_pending_with_context_url() -> None:
+ msg = PendingMessage(
+ content="see this page",
+ context=PendingMessageContext(url="https://example.com"),
+ )
+ out = format_pending_as_user_message(msg)
+ content = out["content"]
+ assert out["role"] == "user"
+ assert "see this page" in content
+ # The URL should appear verbatim in the [Page URL: ...] block.
+ assert "[Page URL: https://example.com]" in content
+
+
+def test_format_pending_with_file_ids() -> None:
+ msg = PendingMessage(content="look here", file_ids=["a", "b"])
+ out = format_pending_as_user_message(msg)
+ assert "file_id=a" in out["content"]
+ assert "file_id=b" in out["content"]
+
+
+def test_format_pending_with_all_fields() -> None:
+ """All fields (content + context url/content + file_ids) should all appear."""
+ msg = PendingMessage(
+ content="summarise this",
+ context=PendingMessageContext(
+ url="https://example.com/page",
+ content="headline text",
+ ),
+ file_ids=["f1", "f2"],
+ )
+ out = format_pending_as_user_message(msg)
+ body = out["content"]
+ assert out["role"] == "user"
+ assert "summarise this" in body
+ assert "[Page URL: https://example.com/page]" in body
+ assert "[Page content]\nheadline text" in body
+ assert "file_id=f1" in body
+ assert "file_id=f2" in body
+
+
+# ── Followup block caps ────────────────────────────────────────────
+
+
+def test_format_followup_single_message() -> None:
+ out = format_pending_as_followup([PendingMessage(content="hello")])
+ assert "<user_follow_up>" in out
+ assert "</user_follow_up>" in out
+ assert "Message 1:\nhello" in out
+
+
+def test_format_followup_total_cap_drops_overflow() -> None:
+ """10 × 2 KB messages must truncate past the total cap (~6 KB) with a
+ marker indicating how many were dropped."""
+ messages = [PendingMessage(content="A" * 2_000) for _ in range(10)]
+ out = format_pending_as_followup(messages)
+ # Block stays within the total cap (plus a little wrapper overhead).
+ # The body alone is capped at 6 KB; we allow generous overhead for the
+ # <user_follow_up> wrapper + headers.
+ assert len(out) < 8_000
+ assert "more message(s) truncated" in out
+ # The first message at least must be present.
+ assert "Message 1:" in out
+
+
+def test_format_followup_total_cap_marker_counts_dropped() -> None:
+ """The marker should name the exact number of dropped messages."""
+ # Each 3 KB message gets capped to 2 KB first; with ~2 KB per entry and a
+ # 6 KB total cap, roughly two entries fit and the rest are dropped.
+ messages = [PendingMessage(content="X" * 3_000) for _ in range(5)]
+ out = format_pending_as_followup(messages)
+ assert "Message 1:" in out
+ assert "Message 2:" in out
+ # Message 3 would push total past 6 KB; marker should report exactly how
+ # many were left out (here: messages 3, 4, 5 → 3 dropped).
+ assert "[3 more message(s) truncated]" in out
+
+
+def test_format_followup_empty_returns_empty_string() -> None:
+ assert format_pending_as_followup([]) == ""
+
+
+# ── Malformed payload handling ──────────────────────────────────────
+
+
+async def test_drain_skips_malformed_entries(
+ fake_redis: _FakeRedis,
+) -> None:
+ # Seed the fake with a mix of valid and malformed payloads
+ fake_redis.lists["copilot:pending:bad"] = [
+ json.dumps({"content": "valid"}),
+ "{not valid json",
+ json.dumps({"content": "also valid", "file_ids": ["a"]}),
+ ]
+ drained = await drain_pending_messages("bad")
+ assert len(drained) == 2
+ assert drained[0].content == "valid"
+ assert drained[1].content == "also valid"
+
+
+async def test_drain_decodes_bytes_payloads(
+ fake_redis: _FakeRedis,
+) -> None:
+ """Real redis-py returns ``bytes`` when ``decode_responses=False``.
+
+ Seed the fake with bytes values to exercise the ``decode("utf-8")``
+ branch in ``drain_pending_messages`` so a regression there doesn't
+ slip past CI.
+ """
+ fake_redis.lists["copilot:pending:bytes_sess"] = [
+ json.dumps({"content": "from bytes"}).encode("utf-8"),
+ ]
+ drained = await drain_pending_messages("bytes_sess")
+ assert len(drained) == 1
+ assert drained[0].content == "from bytes"
+
+
+async def test_peek_decodes_bytes_payloads(
+ fake_redis: _FakeRedis,
+) -> None:
+ """``peek_pending_messages`` uses the same ``_decode_redis_item`` helper
+ as the drain path. Seed with bytes to guard against regression.
+ """
+ fake_redis.lists["copilot:pending:peek_bytes_sess"] = [
+ json.dumps({"content": "peeked from bytes"}).encode("utf-8"),
+ ]
+ peeked = await peek_pending_messages("peek_bytes_sess")
+ assert len(peeked) == 1
+ assert peeked[0].content == "peeked from bytes"
+ # peek must NOT consume the item
+ assert fake_redis.lists["copilot:pending:peek_bytes_sess"] != []
+
+
+# ── Concurrency ─────────────────────────────────────────────────────
+
+
+async def test_concurrent_push_and_drain(fake_redis: _FakeRedis) -> None:
+ """Two pushes fired concurrently should both land; a concurrent drain
+ should see at least one of them (the fake serialises, so it will
+ always see both, but we exercise the code path either way)."""
+ await asyncio.gather(
+ push_pending_message("sess_conc", PendingMessage(content="a")),
+ push_pending_message("sess_conc", PendingMessage(content="b")),
+ )
+ drained = await drain_pending_messages("sess_conc")
+ assert len(drained) >= 1
+ contents = {m.content for m in drained}
+ assert contents <= {"a", "b"}
+
+
+# ── Publish error path ──────────────────────────────────────────────
+
+
+async def test_push_survives_publish_failure(
+ fake_redis: _FakeRedis, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """A publish error must not propagate — the buffer is still authoritative."""
+
+ async def _fail_publish(channel: str, payload: str) -> int:
+ raise RuntimeError("redis publish down")
+
+ monkeypatch.setattr(fake_redis, "publish", _fail_publish)
+
+ length = await push_pending_message("sess_pub_err", PendingMessage(content="ok"))
+ assert length == 1
+ drained = await drain_pending_messages("sess_pub_err")
+ assert len(drained) == 1
+ assert drained[0].content == "ok"
+
+
+# ── peek_pending_messages ────────────────────────────────────────────
+
+
+async def test_peek_pending_messages_returns_all_without_consuming(
+ fake_redis: _FakeRedis,
+) -> None:
+ """Peek returns all queued messages and leaves the buffer intact."""
+ await push_pending_message("peek1", PendingMessage(content="first"))
+ await push_pending_message("peek1", PendingMessage(content="second"))
+
+ peeked = await peek_pending_messages("peek1")
+ assert len(peeked) == 2
+ assert peeked[0].content == "first"
+ assert peeked[1].content == "second"
+
+ # Buffer must not be consumed — count still 2
+ assert await peek_pending_count("peek1") == 2
+ drained = await drain_pending_messages("peek1")
+ assert len(drained) == 2
+
+
+async def test_peek_pending_messages_empty_buffer(fake_redis: _FakeRedis) -> None:
+ """Peek on a missing key returns an empty list without raising."""
+ result = await peek_pending_messages("no_such_session")
+ assert result == []
+
+
+async def test_peek_pending_messages_decodes_bytes_payloads(
+ fake_redis: _FakeRedis,
+) -> None:
+ """peek_pending_messages decodes bytes entries the same way drain does."""
+ fake_redis.lists["copilot:pending:peek_bytes"] = [
+ json.dumps({"content": "from bytes"}).encode("utf-8"),
+ ]
+ peeked = await peek_pending_messages("peek_bytes")
+ assert len(peeked) == 1
+ assert peeked[0].content == "from bytes"
+
+
+async def test_peek_pending_messages_skips_malformed_entries(
+ fake_redis: _FakeRedis,
+) -> None:
+ """Malformed entries are skipped and valid ones are returned."""
+ fake_redis.lists["copilot:pending:peek_bad"] = [
+ json.dumps({"content": "valid peek"}),
+ "{bad json",
+ json.dumps({"content": "also valid peek"}),
+ ]
+ peeked = await peek_pending_messages("peek_bad")
+ assert len(peeked) == 2
+ assert peeked[0].content == "valid peek"
+ assert peeked[1].content == "also valid peek"
+
+
+# ── Persist queue (mid-turn follow-up UI bubble hand-off) ───────────
+
+
+async def test_stash_for_persist_enqueues_and_drain_pops_in_order(
+ fake_redis: _FakeRedis,
+) -> None:
+ """stash_pending_for_persist writes messages under the persist key;
+ drain_pending_for_persist LPOPs them in enqueue order."""
+ msgs = [
+ PendingMessage(content="first mid-turn follow-up"),
+ PendingMessage(content="second"),
+ ]
+ await stash_pending_for_persist("sess-persist", msgs)
+
+ # Stored under the distinct persist key, NOT the primary buffer.
+ assert "copilot:pending-persist:sess-persist" in fake_redis.lists
+ assert "copilot:pending:sess-persist" not in fake_redis.lists
+
+ drained = await drain_pending_for_persist("sess-persist")
+ assert len(drained) == 2
+ assert drained[0].content == "first mid-turn follow-up"
+ assert drained[1].content == "second"
+
+ # Queue is empty after drain.
+ assert await drain_pending_for_persist("sess-persist") == []
+
+
+async def test_stash_for_persist_empty_list_is_noop(
+ fake_redis: _FakeRedis,
+) -> None:
+ """Passing an empty list must NOT create a Redis key (would leak
+ empty persist entries and require a drain for no reason)."""
+ await stash_pending_for_persist("sess-noop", [])
+ assert "copilot:pending-persist:sess-noop" not in fake_redis.lists
+
+
+async def test_drain_pending_for_persist_missing_key_returns_empty(
+ fake_redis: _FakeRedis,
+) -> None:
+ assert await drain_pending_for_persist("never-stashed") == []
+
+
+async def test_drain_pending_for_persist_skips_malformed(
+ fake_redis: _FakeRedis,
+) -> None:
+ fake_redis.lists["copilot:pending-persist:bad"] = [
+ json.dumps({"content": "good one"}),
+ "not json",
+ json.dumps({"content": "another good one"}),
+ ]
+ result = await drain_pending_for_persist("bad")
+ assert [m.content for m in result] == ["good one", "another good one"]
+
+
+async def test_persist_queue_isolated_from_primary_buffer(
+ fake_redis: _FakeRedis,
+) -> None:
+ """Draining the persist queue must NOT touch the primary pending
+ buffer (and vice versa) — they serve different lifecycles."""
+ # Seed the primary buffer with one entry.
+ await push_pending_message("sess-iso", PendingMessage(content="primary"))
+ # Stash a separate entry on the persist queue.
+ await stash_pending_for_persist("sess-iso", [PendingMessage(content="persist")])
+
+ drained_persist = await drain_pending_for_persist("sess-iso")
+ assert [m.content for m in drained_persist] == ["persist"]
+
+ # Primary buffer untouched.
+ assert await peek_pending_count("sess-iso") == 1
+ drained_primary = await drain_pending_messages("sess-iso")
+ assert [m.content for m in drained_primary] == ["primary"]
+
+
+async def test_stash_for_persist_swallows_redis_failure(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A broken Redis during stash must not raise — Claude has already
+ seen the follow-up via tool output; the only fallout is a missing
+ UI bubble, which we log and move on."""
+
+ async def _broken_redis() -> Any:
+ raise ConnectionError("redis down")
+
+ monkeypatch.setattr(pm_module, "get_redis_async", _broken_redis)
+
+ # Must NOT raise.
+ await stash_pending_for_persist("sess-broken", [PendingMessage(content="lost")])
+
+
+# ── drain_and_format_for_injection: shared entry point ─────────────────
+
+
+async def test_drain_and_format_for_injection_happy_path(
+ fake_redis: _FakeRedis,
+) -> None:
+ """Queued messages drain into a ready-to-inject <user_follow_up> block
+ AND are stashed on the persist queue for UI row hand-off."""
+ await push_pending_message("sess-share", PendingMessage(content="do X also"))
+
+ result = await drain_and_format_for_injection("sess-share", log_prefix="[TEST]")
+
+ assert "<user_follow_up>" in result
+ assert "do X also" in result
+ # Primary buffer drained.
+ assert await peek_pending_count("sess-share") == 0
+ # Persist queue got a copy for the UI.
+ persisted = await drain_pending_for_persist("sess-share")
+ assert len(persisted) == 1
+ assert persisted[0].content == "do X also"
+
+
+async def test_drain_and_format_for_injection_empty_returns_empty(
+ fake_redis: _FakeRedis,
+) -> None:
+ assert await drain_and_format_for_injection("sess-empty", log_prefix="[TEST]") == ""
+
+
+async def test_drain_and_format_for_injection_swallows_redis_error(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ async def _broken() -> Any:
+ raise ConnectionError("down")
+
+ monkeypatch.setattr(pm_module, "get_redis_async", _broken)
+
+ # Must NOT raise — broken Redis becomes "nothing to inject".
+ assert (
+ await drain_and_format_for_injection("sess-broken", log_prefix="[TEST]") == ""
+ )
+
+
+async def test_drain_and_format_for_injection_missing_session_id() -> None:
+ assert await drain_and_format_for_injection("", log_prefix="[TEST]") == ""
diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.py
index 3ec75e8bbe..1274302eee 100644
--- a/autogpt_platform/backend/backend/copilot/prompting.py
+++ b/autogpt_platform/backend/backend/copilot/prompting.py
@@ -341,6 +341,29 @@ def _generate_tool_documentation() -> str:
return docs
+_USER_FOLLOW_UP_NOTE = """
+# `<user_follow_up>` blocks in tool output
+
+A `<user_follow_up>…</user_follow_up>` block at the head of a tool result is a
+message the user sent while the tool was running — not tool output. The user is
+watching the chat live and waiting for confirmation their message landed.
+
+Every time you see one:
+
+1. **Ack immediately.** Your very next emission must be a short visible line,
+ before any more tool calls:
+ *"Got your follow-up: {paraphrase}. {what I'll do}."*
+
+2. **Then act on it:**
+ - Question/input request → stop the tool chain and answer/ask back.
+ - New requirement → fold into the current plan.
+ - Correction → update the plan and continue with the revised target.
+
+Never echo the `<user_follow_up>` tags back. The block holds only the user's
+words — the rest of the tool result is the real data.
+"""
+
+
@cache
def get_sdk_supplement(use_e2b: bool) -> str:
"""Get the supplement for SDK mode (Claude Agent SDK).
@@ -363,9 +386,12 @@ def get_sdk_supplement(use_e2b: bool) -> str:
Returns:
The supplement string to append to the system prompt
"""
- if use_e2b:
- return _get_cloud_sandbox_supplement()
- return _get_local_storage_supplement("/tmp/copilot-<session-id>")
+ base = (
+ _get_cloud_sandbox_supplement()
+ if use_e2b
+ else _get_local_storage_supplement("/tmp/copilot-<session-id>")
+ )
+ return base + _USER_FOLLOW_UP_NOTE
def get_graphiti_supplement() -> str:
diff --git a/autogpt_platform/backend/backend/copilot/sdk/__init__.py b/autogpt_platform/backend/backend/copilot/sdk/__init__.py
index 905e5351cc..db97fdd8fb 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/__init__.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/__init__.py
@@ -24,14 +24,10 @@ from typing import TYPE_CHECKING, Any
# Static imports for type checkers so they can resolve __all__ entries
# without executing the lazy-import machinery at runtime.
if TYPE_CHECKING:
- from .collect import CopilotResult as CopilotResult
- from .collect import collect_copilot_response as collect_copilot_response
from .service import stream_chat_completion_sdk as stream_chat_completion_sdk
from .tool_adapter import create_copilot_mcp_server as create_copilot_mcp_server
__all__ = [
- "CopilotResult",
- "collect_copilot_response",
"stream_chat_completion_sdk",
"create_copilot_mcp_server",
]
@@ -39,8 +35,6 @@ __all__ = [
# Dispatch table for PEP 562 lazy imports. Each entry is a (module, attr)
# pair so new exports can be added without touching __getattr__ itself.
_LAZY_IMPORTS: dict[str, tuple[str, str]] = {
- "CopilotResult": (".collect", "CopilotResult"),
- "collect_copilot_response": (".collect", "collect_copilot_response"),
"stream_chat_completion_sdk": (".service", "stream_chat_completion_sdk"),
"create_copilot_mcp_server": (".tool_adapter", "create_copilot_mcp_server"),
}
diff --git a/autogpt_platform/backend/backend/copilot/sdk/collect.py b/autogpt_platform/backend/backend/copilot/sdk/collect.py
deleted file mode 100644
index 175b344d78..0000000000
--- a/autogpt_platform/backend/backend/copilot/sdk/collect.py
+++ /dev/null
@@ -1,171 +0,0 @@
-"""Public helpers for consuming a copilot stream as a simple request-response.
-
-This module exposes :class:`CopilotResult` and :func:`collect_copilot_response`
-so that callers (e.g. the AutoPilot block) can consume the copilot stream
-without implementing their own event loop.
-"""
-
-from __future__ import annotations
-
-import logging
-import uuid
-from collections.abc import AsyncIterator
-from contextlib import asynccontextmanager
-from typing import TYPE_CHECKING, Any
-
-if TYPE_CHECKING:
- from backend.copilot.permissions import CopilotPermissions
-
-from pydantic import BaseModel
-from redis.exceptions import RedisError
-
-from .. import stream_registry
-from .service import stream_chat_completion_sdk
-from .stream_accumulator import EventAccumulator, process_event
-
-logger = logging.getLogger(__name__)
-
-# Identifiers used when registering AutoPilot-originated streams in the
-# stream registry. Distinct from "chat_stream"/"chat" used by the HTTP SSE
-# endpoint, making it easy to filter AutoPilot streams in logs/observability.
-AUTOPILOT_TOOL_CALL_ID = "autopilot_stream"
-AUTOPILOT_TOOL_NAME = "autopilot"
-
-
-class CopilotResult:
- """Aggregated result from consuming a copilot stream.
-
- Returned by :func:`collect_copilot_response` so callers don't need to
- implement their own event-loop over the raw stream events.
- """
-
- __slots__ = (
- "response_text",
- "tool_calls",
- "prompt_tokens",
- "completion_tokens",
- "total_tokens",
- )
-
- def __init__(self) -> None:
- self.response_text: str = ""
- self.tool_calls: list[dict[str, Any]] = []
- self.prompt_tokens: int = 0
- self.completion_tokens: int = 0
- self.total_tokens: int = 0
-
-
-class _RegistryHandle(BaseModel):
- """Tracks stream registry session state for cleanup."""
-
- publish_turn_id: str = ""
- error_msg: str | None = None
- error_already_published: bool = False
-
-
-@asynccontextmanager
-async def _registry_session(
- session_id: str, user_id: str, turn_id: str
-) -> AsyncIterator[_RegistryHandle]:
- """Create a stream registry session and ensure it is finalized."""
- handle = _RegistryHandle(publish_turn_id=turn_id)
- try:
- await stream_registry.create_session(
- session_id=session_id,
- user_id=user_id,
- tool_call_id=AUTOPILOT_TOOL_CALL_ID,
- tool_name=AUTOPILOT_TOOL_NAME,
- turn_id=turn_id,
- )
- except (RedisError, ConnectionError, OSError):
- logger.warning(
- "[collect] Failed to create stream registry session for %s, "
- "frontend will not receive real-time updates",
- session_id[:12],
- exc_info=True,
- )
- # Disable chunk publishing but keep finalization enabled so
- # mark_session_completed can clean up any partial registry state.
- handle.publish_turn_id = ""
-
- try:
- yield handle
- finally:
- try:
- await stream_registry.mark_session_completed(
- session_id,
- error_message=handle.error_msg,
- skip_error_publish=handle.error_already_published,
- )
- except (RedisError, ConnectionError, OSError):
- logger.warning(
- "[collect] Failed to mark stream completed for %s",
- session_id[:12],
- exc_info=True,
- )
-
-
-async def collect_copilot_response(
- *,
- session_id: str,
- message: str,
- user_id: str,
- is_user_message: bool = True,
- permissions: "CopilotPermissions | None" = None,
-) -> CopilotResult:
- """Consume :func:`stream_chat_completion_sdk` and return aggregated results.
-
- Registers with the stream registry so the frontend can connect via SSE
- and receive real-time updates while the AutoPilot block is executing.
-
- Args:
- session_id: Chat session to use.
- message: The user message / prompt.
- user_id: Authenticated user ID.
- is_user_message: Whether this is a user-initiated message.
- permissions: Optional capability filter. When provided, restricts
- which tools and blocks the copilot may use during this execution.
-
- Returns:
- A :class:`CopilotResult` with the aggregated response text,
- tool calls, and token usage.
-
- Raises:
- RuntimeError: If the stream yields a ``StreamError`` event.
- """
- turn_id = str(uuid.uuid4())
- async with _registry_session(session_id, user_id, turn_id) as handle:
- try:
- raw_stream = stream_chat_completion_sdk(
- session_id=session_id,
- message=message,
- is_user_message=is_user_message,
- user_id=user_id,
- permissions=permissions,
- )
- published_stream = stream_registry.stream_and_publish(
- session_id=session_id,
- turn_id=handle.publish_turn_id,
- stream=raw_stream,
- )
-
- acc = EventAccumulator()
- async for event in published_stream:
- if err := process_event(event, acc):
- handle.error_msg = err
- # stream_and_publish skips StreamError events, so
- # mark_session_completed must publish the error to Redis.
- handle.error_already_published = False
- raise RuntimeError(f"Copilot error: {err}")
- except Exception:
- if handle.error_msg is None:
- handle.error_msg = "AutoPilot execution failed"
- raise
-
- result = CopilotResult()
- result.response_text = "".join(acc.response_parts)
- result.tool_calls = [tc.model_dump() for tc in acc.tool_calls]
- result.prompt_tokens = acc.prompt_tokens
- result.completion_tokens = acc.completion_tokens
- result.total_tokens = acc.total_tokens
- return result
diff --git a/autogpt_platform/backend/backend/copilot/sdk/collect_test.py b/autogpt_platform/backend/backend/copilot/sdk/collect_test.py
deleted file mode 100644
index d86d1f4fb3..0000000000
--- a/autogpt_platform/backend/backend/copilot/sdk/collect_test.py
+++ /dev/null
@@ -1,177 +0,0 @@
-"""Tests for collect_copilot_response stream registry integration."""
-
-from unittest.mock import AsyncMock, patch
-
-import pytest
-
-from backend.copilot.response_model import (
- StreamError,
- StreamFinish,
- StreamTextDelta,
- StreamToolInputAvailable,
- StreamToolOutputAvailable,
- StreamUsage,
-)
-from backend.copilot.sdk.collect import collect_copilot_response
-
-
-def _mock_stream_fn(*events):
- """Return a callable that returns an async generator."""
-
- async def _gen(**_kwargs):
- for e in events:
- yield e
-
- return _gen
-
-
-def mock_registry():
- """Patch stream_registry module used by collect."""
- with patch("backend.copilot.sdk.collect.stream_registry") as m:
- m.create_session = AsyncMock()
- m.publish_chunk = AsyncMock()
- m.mark_session_completed = AsyncMock()
-
- # stream_and_publish: pass-through that also publishes (real logic)
- # We re-implement the pass-through here so the event loop works,
- # but still track publish_chunk calls via the mock.
- async def _stream_and_publish(session_id, turn_id, stream):
- async for event in stream:
- if turn_id and not isinstance(event, (StreamFinish, StreamError)):
- await m.publish_chunk(turn_id, event)
- yield event
-
- m.stream_and_publish = _stream_and_publish
- yield m
-
-
-def stream_fn_patch():
- """Helper to patch stream_chat_completion_sdk."""
-
- def _patch(events):
- return patch(
- "backend.copilot.sdk.collect.stream_chat_completion_sdk",
- new=_mock_stream_fn(*events),
- )
-
- return _patch
-
-
-async def test_stream_registry_called_on_success(mock_registry, stream_fn_patch):
- """Stream registry create/publish/complete are called correctly on success."""
- events = [
- StreamTextDelta(id="t1", delta="Hello "),
- StreamTextDelta(id="t1", delta="world"),
- StreamUsage(prompt_tokens=10, completion_tokens=5, total_tokens=15),
- StreamFinish(),
- ]
-
- with stream_fn_patch(events):
- result = await collect_copilot_response(
- session_id="test-session",
- message="hi",
- user_id="user-1",
- )
-
- assert result.response_text == "Hello world"
- assert result.total_tokens == 15
-
- mock_registry.create_session.assert_awaited_once()
- # StreamFinish should NOT be published (mark_session_completed does it)
- published_types = [
- type(call.args[1]).__name__
- for call in mock_registry.publish_chunk.call_args_list
- ]
- assert "StreamFinish" not in published_types
- assert "StreamTextDelta" in published_types
-
- mock_registry.mark_session_completed.assert_awaited_once()
- _, kwargs = mock_registry.mark_session_completed.call_args
- assert kwargs.get("error_message") is None
-
-
-async def test_stream_registry_error_on_stream_error(mock_registry, stream_fn_patch):
- """mark_session_completed receives error message when StreamError occurs."""
- events = [
- StreamTextDelta(id="t1", delta="partial"),
- StreamError(errorText="something broke"),
- ]
-
- with stream_fn_patch(events):
- with pytest.raises(RuntimeError, match="something broke"):
- await collect_copilot_response(
- session_id="test-session",
- message="hi",
- user_id="user-1",
- )
-
- _, kwargs = mock_registry.mark_session_completed.call_args
- assert kwargs.get("error_message") == "something broke"
- # stream_and_publish skips StreamError, so mark_session_completed must
- # publish it (skip_error_publish=False).
- assert kwargs.get("skip_error_publish") is False
-
- # StreamError should NOT be published via publish_chunk — mark_session_completed
- # handles it to avoid double-publication.
- published_types = [
- type(call.args[1]).__name__
- for call in mock_registry.publish_chunk.call_args_list
- ]
- assert "StreamError" not in published_types
-
-
-async def test_graceful_degradation_when_create_session_fails(
- mock_registry, stream_fn_patch
-):
- """AutoPilot still works when stream registry create_session raises."""
- events = [
- StreamTextDelta(id="t1", delta="works"),
- StreamFinish(),
- ]
- mock_registry.create_session = AsyncMock(side_effect=ConnectionError("Redis down"))
-
- with stream_fn_patch(events):
- result = await collect_copilot_response(
- session_id="test-session",
- message="hi",
- user_id="user-1",
- )
-
- assert result.response_text == "works"
- # publish_chunk should NOT be called because turn_id was cleared
- mock_registry.publish_chunk.assert_not_awaited()
- # mark_session_completed IS still called to clean up any partial state
- mock_registry.mark_session_completed.assert_awaited_once()
-
-
-async def test_tool_calls_published_and_collected(mock_registry, stream_fn_patch):
- """Tool call events are both published to registry and collected in result."""
- events = [
- StreamToolInputAvailable(
- toolCallId="tc-1", toolName="read_file", input={"path": "/tmp"}
- ),
- StreamToolOutputAvailable(
- toolCallId="tc-1", output="file contents", success=True
- ),
- StreamTextDelta(id="t1", delta="done"),
- StreamFinish(),
- ]
-
- with stream_fn_patch(events):
- result = await collect_copilot_response(
- session_id="test-session",
- message="hi",
- user_id="user-1",
- )
-
- assert len(result.tool_calls) == 1
- assert result.tool_calls[0]["tool_name"] == "read_file"
- assert result.tool_calls[0]["output"] == "file contents"
- assert result.tool_calls[0]["success"] is True
- assert result.response_text == "done"
diff --git a/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py b/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py
index a6e88889c3..da9477121d 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py
@@ -255,6 +255,111 @@ async def test_build_query_no_resume_multi_message(monkeypatch):
assert was_compacted is False # mock returns False
+async def test_build_query_session_msg_ceiling_prevents_pending_duplication():
+ """session_msg_ceiling stops pending messages from leaking into the gap.
+
+ Scenario: transcript covers 2 messages, session has 2 historical + 1 current
+ + 2 pending drained at turn start. Without the ceiling the gap would include
+ the pending messages AND current_message already has them → duplication.
+ With session_msg_ceiling=3 (pre-drain count) the gap slice is empty and
+ only current_message carries the pending content.
+ """
+ # session.messages after drain: [hist1, hist2, current_msg, pending1, pending2]
+ session = _make_session(
+ [
+ ChatMessage(role="user", content="hist1"),
+ ChatMessage(role="assistant", content="hist2"),
+ ChatMessage(role="user", content="current msg with pending1 pending2"),
+ ChatMessage(role="user", content="pending1"),
+ ChatMessage(role="user", content="pending2"),
+ ]
+ )
+ # transcript covers hist1+hist2 (2 messages); pre-drain count was 3 (includes current_msg)
+ result, was_compacted = await _build_query_message(
+ "current msg with pending1 pending2",
+ session,
+ use_resume=True,
+ transcript_msg_count=2,
+ session_id="test-session",
+ session_msg_ceiling=3, # len(session.messages) before drain
+ )
+ # Gap should be empty (transcript_msg_count == ceiling - 1), so no history prepended
+ assert result == "current msg with pending1 pending2"
+ assert was_compacted is False
+ # Pending messages must NOT appear in gap context
+ assert "pending1" not in result.split("current msg")[0]
+
+
+async def test_build_query_session_msg_ceiling_preserves_real_gap():
+ """session_msg_ceiling still surfaces a genuine stale-transcript gap.
+
+ Scenario: transcript covers 2 messages, session has 4 historical + 1 current
+ + 2 pending. Ceiling = 5 (pre-drain). Real gap = messages 2-3 (hist3, hist4).
+ """
+ session = _make_session(
+ [
+ ChatMessage(role="user", content="hist1"),
+ ChatMessage(role="assistant", content="hist2"),
+ ChatMessage(role="user", content="hist3"),
+ ChatMessage(role="assistant", content="hist4"),
+ ChatMessage(role="user", content="current"),
+ ChatMessage(role="user", content="pending1"),
+ ChatMessage(role="user", content="pending2"),
+ ]
+ )
+ result, was_compacted = await _build_query_message(
+ "current",
+ session,
+ use_resume=True,
+ transcript_msg_count=2,
+ session_id="test-session",
+ session_msg_ceiling=5, # pre-drain: [hist1..hist4, current]
+ )
+ # Gap = session.messages[2:4] = [hist3, hist4]
+ assert "<conversation_history>" in result
+ assert "hist3" in result
+ assert "hist4" in result
+ assert "Now, the user says:\ncurrent" in result
+ # Pending messages must NOT appear in gap
+ assert "pending1" not in result
+ assert "pending2" not in result
+
+
+async def test_build_query_session_msg_ceiling_suppresses_spurious_no_resume_fallback():
+ """session_msg_ceiling prevents the no-resume compression fallback from
+ firing on the first turn of a session when pending messages inflate msg_count.
+
+ Scenario: fresh session (1 message) + 1 pending message drained at turn start.
+ Without the ceiling: msg_count=2 > 1 → fallback triggers → pending message
+ leaked into history → wrong context sent to model.
+ With session_msg_ceiling=1 (pre-drain count): effective_count=1, 1 > 1 is False
+ → fallback does not trigger → current_message returned as-is.
+ """
+ # session.messages after drain: [current_msg, pending_msg]
+ session = _make_session(
+ [
+ ChatMessage(role="user", content="What is 2 plus 2?"),
+ ChatMessage(role="user", content="What is 7 plus 7?"), # pending
+ ]
+ )
+ result, was_compacted = await _build_query_message(
+ "What is 2 plus 2?\n\nWhat is 7 plus 7?",
+ session,
+ use_resume=False,
+ transcript_msg_count=0,
+ session_id="test-session",
+ session_msg_ceiling=1, # pre-drain: only 1 message existed
+ )
+ # Should return current_message directly without wrapping in history context
+ assert result == "What is 2 plus 2?\n\nWhat is 7 plus 7?"
+ assert was_compacted is False
+ # Pending question must NOT appear in a spurious history section
+ assert "<conversation_history>" not in result
+
+
@pytest.mark.asyncio
async def test_build_query_no_resume_multi_message_compacted(monkeypatch):
"""When compression actually compacts, was_compacted should be True."""
diff --git a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
index 60c65f00ce..5b3919c2aa 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
@@ -1040,6 +1040,12 @@ def _make_sdk_patches(
),
),
(f"{_SVC}.get_user_tier", dict(new_callable=AsyncMock, return_value=None)),
+ # Stub pending-message drain so retry tests don't hit Redis.
+ # Returns an empty list → no mid-turn injection happens.
+ (
+ f"{_SVC}.drain_pending_safe",
+ dict(new_callable=AsyncMock, return_value=[]),
+ ),
]
diff --git a/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py b/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
index 666e55fbba..6401dfade4 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
@@ -10,7 +10,12 @@ import re
from collections.abc import Callable
from typing import Any, cast
-from backend.copilot.context import is_allowed_local_path, is_sdk_tool_path
+from backend.copilot.context import (
+ get_execution_context,
+ is_allowed_local_path,
+ is_sdk_tool_path,
+)
+from backend.copilot.pending_messages import drain_and_format_for_injection
from .tool_adapter import (
BLOCKED_TOOLS,
@@ -327,6 +332,30 @@ def create_security_hooks(
tool_name,
)
+ # Mid-turn drain: after ANY tool finishes (MCP or built-in), pull
+ # any queued user follow-up messages and attach them to the
+ # tool_result as ``additionalContext``. This is the
+ # protocol-legal mid-turn injection slot — Claude reads the
+ # follow-up on the next LLM round without starting a new turn.
+ # The drain helper also stashes a persist-queue copy so
+ # ``sdk/service.py`` can append a matching user row to the UI.
+ _, session = get_execution_context()
+ followup = ""
+ if session is not None and session.session_id:
+ followup = await drain_and_format_for_injection(
+ session.session_id,
+ log_prefix="[SDK][PostToolUse]",
+ )
+ if followup:
+ return cast(
+ SyncHookJSONOutput,
+ {
+ "hookSpecificOutput": {
+ "hookEventName": "PostToolUse",
+ "additionalContext": followup,
+ }
+ },
+ )
return cast(SyncHookJSONOutput, {})
async def post_tool_failure_hook(
diff --git a/autogpt_platform/backend/backend/copilot/sdk/security_hooks_test.py b/autogpt_platform/backend/backend/copilot/sdk/security_hooks_test.py
index 033bcf1494..fbb650a9f4 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/security_hooks_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/security_hooks_test.py
@@ -699,3 +699,160 @@ async def test_subagent_hooks_sanitize_inputs(_subagent_hooks, caplog):
assert "\u202a" not in record.message
assert "\u200b" not in record.message
assert "/tmp/maliciouspath" in caplog.text
+
+
+# -- PostToolUse: mid-turn pending-message drain ------------------------------
+
+
[email protected](not _sdk_available(), reason="claude_agent_sdk not installed")
+async def test_post_tool_use_injects_followup_additional_context(
+ monkeypatch,
+):
+ """Queued messages drain into ``additionalContext`` for any tool."""
+ from unittest.mock import MagicMock
+
+ from backend.copilot import context as ctx_mod
+ from backend.copilot import pending_messages as pm_module
+
+ session = MagicMock()
+ session.session_id = "sess-post-inject"
+ ctx_mod.set_execution_context(
+ user_id="u1",
+ session=session,
+ sandbox=None,
+ sdk_cwd=SDK_CWD,
+ )
+
+ async def fake_drain(_session_id: str):
+ assert _session_id == "sess-post-inject"
+ return [pm_module.PendingMessage(content="please also do X")]
+
+ async def fake_stash(_session_id, _messages):
+ return None
+
+ monkeypatch.setattr(
+ "backend.copilot.pending_messages.drain_pending_messages", fake_drain
+ )
+ monkeypatch.setattr(
+ "backend.copilot.pending_messages.stash_pending_for_persist", fake_stash
+ )
+
+ hooks = create_security_hooks(user_id="u1", sdk_cwd=SDK_CWD, max_subtasks=2)
+ post = hooks["PostToolUse"][0].hooks[0]
+
+ result = await post(
+ {
+ "tool_name": "WebSearch", # built-in — the path the old wrapper missed
+ "tool_response": "search results here",
+ },
+ tool_use_id="tu-web-1",
+ context={},
+ )
+
+ injected = result.get("hookSpecificOutput", {})
+ assert injected.get("hookEventName") == "PostToolUse"
+ assert "<user_follow_up>" in injected.get("additionalContext", "")
+ assert "please also do X" in injected.get("additionalContext", "")
+
+
[email protected](not _sdk_available(), reason="claude_agent_sdk not installed")
+async def test_post_tool_use_no_pending_returns_empty(monkeypatch):
+ from unittest.mock import MagicMock
+
+ from backend.copilot import context as ctx_mod
+
+ session = MagicMock()
+ session.session_id = "sess-post-empty"
+ ctx_mod.set_execution_context(
+ user_id="u1", session=session, sandbox=None, sdk_cwd=SDK_CWD
+ )
+
+ async def fake_drain(_session_id: str):
+ return []
+
+ monkeypatch.setattr(
+ "backend.copilot.pending_messages.drain_pending_messages", fake_drain
+ )
+
+ hooks = create_security_hooks(user_id="u1", sdk_cwd=SDK_CWD, max_subtasks=2)
+ post = hooks["PostToolUse"][0].hooks[0]
+
+ result = await post(
+ {"tool_name": "mcp__copilot__run_block", "tool_response": "ok"},
+ tool_use_id="tu-mcp-1",
+ context={},
+ )
+
+ # No additionalContext means Claude gets the tool_result verbatim.
+ assert "hookSpecificOutput" not in result
+
+
[email protected](not _sdk_available(), reason="claude_agent_sdk not installed")
+async def test_post_tool_use_drain_failure_returns_empty(monkeypatch):
+ """A Redis blip must not corrupt the hook response."""
+ from unittest.mock import MagicMock
+
+ from backend.copilot import context as ctx_mod
+
+ session = MagicMock()
+ session.session_id = "sess-post-fail"
+ ctx_mod.set_execution_context(
+ user_id="u1", session=session, sandbox=None, sdk_cwd=SDK_CWD
+ )
+
+ async def failing_drain(_session_id: str):
+ raise RuntimeError("redis down")
+
+ monkeypatch.setattr(
+ "backend.copilot.pending_messages.drain_pending_messages", failing_drain
+ )
+
+ hooks = create_security_hooks(user_id="u1", sdk_cwd=SDK_CWD, max_subtasks=2)
+ post = hooks["PostToolUse"][0].hooks[0]
+
+ result = await post(
+ {"tool_name": "Read", "tool_response": "file body"},
+ tool_use_id="tu-read-1",
+ context={},
+ )
+
+ assert "hookSpecificOutput" not in result
+
+
[email protected](not _sdk_available(), reason="claude_agent_sdk not installed")
+async def test_post_tool_use_no_session_skips_drain(monkeypatch):
+ from backend.copilot import context as ctx_mod
+
+ ctx_mod.set_execution_context(
+ user_id=None,
+ session=None, # type: ignore[arg-type]
+ sandbox=None,
+ sdk_cwd=SDK_CWD,
+ )
+
+ drain_called = False
+
+ async def fake_drain(_session_id: str):
+ nonlocal drain_called
+ drain_called = True
+ return []
+
+ monkeypatch.setattr(
+ "backend.copilot.pending_messages.drain_pending_messages", fake_drain
+ )
+
+ hooks = create_security_hooks(user_id=None, sdk_cwd=SDK_CWD, max_subtasks=2)
+ post = hooks["PostToolUse"][0].hooks[0]
+
+ result = await post(
+ {"tool_name": "WebSearch", "tool_response": "x"},
+ tool_use_id="tu-x",
+ context={},
+ )
+
+ assert drain_called is False
+ assert "hookSpecificOutput" not in result
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index a670249f72..c27480651f 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -38,6 +38,7 @@ from langsmith.integrations.claude_agent_sdk import configure_claude_agent_sdk
from opentelemetry import trace as otel_trace
from pydantic import BaseModel
+from backend.data.db_accessors import chat_db
from backend.data.redis_client import get_redis_async
from backend.executor.cluster_lock import AsyncClusterLock
from backend.util.exceptions import NotFoundError
@@ -61,6 +62,18 @@ from ..model import (
maybe_append_user_message,
upsert_chat_session,
)
+from ..pending_message_helpers import (
+ combine_pending_with_current,
+ drain_pending_safe,
+ pending_texts_from,
+ persist_pending_as_user_rows,
+ persist_session_safe,
+)
+from ..pending_messages import (
+ PendingMessage,
+ drain_pending_for_persist,
+ push_pending_message,
+)
from ..permissions import apply_tool_permissions
from ..prompting import get_graphiti_supplement, get_sdk_supplement
from ..rate_limit import get_user_tier
@@ -340,6 +353,15 @@ class _RetryState:
# None = model-aware default. Halved each retry for progressively more
# aggressive compression (LLM summarize → truncate → middle-out → trim).
target_tokens: int | None = None
+ # Count of user rows inserted MID-TURN by the follow-up persist path
+ # (``StreamToolOutputAvailable`` handler). The CLI JSONL does NOT contain
+ # these as separate user entries — they are embedded inside tool_result
+ # text via the MCP wrapper injection, which the CLI may even strip when
+ # the tool output exceeds its internal size cap. Tracking them separately
+ # lets the upload path subtract from ``message_count`` so the next turn's
+ # ``detect_gap`` picks them up as gap-fill entries instead of assuming the
+ # JSONL already covers them.
+ midturn_user_rows: int = 0
@dataclass
@@ -1294,6 +1316,8 @@ async def _build_query_message(
use_resume: bool,
transcript_msg_count: int,
session_id: str,
+ *,
+ session_msg_ceiling: int | None = None,
target_tokens: int | None = None,
prior_messages: "list[ChatMessage] | None" = None,
) -> tuple[str, bool]:
@@ -1310,11 +1334,29 @@ async def _build_query_message(
progressively more aggressive compression when the first attempt exceeds
context limits.
+ Args:
+ session_msg_ceiling: If provided, treat ``session.messages`` as if it
+ only has this many entries when computing the gap slice. Pass
+ ``len(session.messages)`` captured *before* appending any pending
+ messages so that mid-turn drains do not skew the gap calculation
+ and cause pending messages to be duplicated in both the gap context
+ and ``current_message``.
+
Returns:
Tuple of (query_message, was_compacted).
"""
msg_count = len(session.messages)
- prior = session.messages[:-1] # all turns except the current user message
+ # Use the ceiling if supplied (prevents pending-message duplication when
+ # messages were appended to session.messages after the drain but before
+ # this function is called).
+ effective_count = (
+ session_msg_ceiling if session_msg_ceiling is not None else msg_count
+ )
+ # Exclude the current user message and any pending messages appended after
+ # the ceiling snapshot — only history up to effective_count-1 is in scope.
+ # max(0, ...) guards against a theoretical 0-message ceiling (brand-new
+ # session) where -1 would select all-but-last instead of an empty slice.
+ prior = session.messages[: max(0, effective_count - 1)]
logger.info(
"[SDK] [%s] Context path: use_resume=%s, transcript_msg_count=%d,"
@@ -1327,7 +1369,7 @@ async def _build_query_message(
)
if use_resume and transcript_msg_count > 0:
- if transcript_msg_count < msg_count - 1:
+ if transcript_msg_count < effective_count - 1:
# Sanity-check the watermark: the last covered position should be
# an assistant turn. A user-role message here means the count is
# misaligned (e.g. a message was deleted and DB positions shifted).
@@ -1378,7 +1420,7 @@ async def _build_query_message(
)
return current_message, False
- elif not use_resume and msg_count > 1:
+ elif not use_resume and effective_count > 1:
# No --resume: the CLI starts a fresh session with no prior context.
# Injecting only the post-transcript gap would omit the transcript-covered
# prefix entirely, so always compress the full prior session here.
@@ -1676,6 +1718,29 @@ def _dispatch_response(
acc.has_appended_assistant = True
elif isinstance(response, StreamToolOutputAvailable):
+ # Dedupe: the response adapter can emit the same tool_use_id more than
+ # once when the CLI re-delivers a ToolResultBlock (e.g. after a retry
+ # or when a parallel-tool UserMessage is processed alongside a flush).
+ # Guard at persistence time — the first emission already wrote the row
+ # (via the pop_pending_tool_output stash, so it has clean text), and a
+ # duplicate would land a second row with the raw MCP list fallback
+ # content (breaking frontend widgets and inflating conversation tokens).
+ already_persisted = any(
+ m.role == "tool" and m.tool_call_id == response.toolCallId
+ for m in ctx.session.messages
+ )
+ if already_persisted:
+ logger.info(
+ "%s Skipping duplicate tool_result for toolCallId=%s",
+ log_prefix,
+ response.toolCallId,
+ )
+ # Return None so the caller's ``if dispatched is not None: yield``
+ # short-circuits — the duplicate event stays off the SSE stream
+ # (so the frontend doesn't render a second widget) and the
+ # mid-turn follow-up persist doesn't double-fire (its guard is
+ # ``dispatched is not None``).
+ return None
content = (
response.output
if isinstance(response.output, str)
@@ -2266,6 +2331,42 @@ async def _run_stream_attempt(
if dispatched is not None:
yield dispatched
+ # Mid-turn follow-up persistence: the MCP tool wrapper drains
+ # the primary pending buffer and stashes the drained
+ # PendingMessages into the per-session persist queue. Claude
+ # has already seen them via the <user_follow_up> block
+ # injected into the tool output. Now — right after the
+ # tool_result row has been appended to session.messages — we
+ # pop the persist queue and append a real user ChatMessage
+ # so the UI renders a proper user bubble in the correct
+ # chronological position (after the tool_result, before the
+ # assistant's continuing response). Rollback re-queues into
+ # the PRIMARY pending buffer so the next turn-start drain
+ # picks them up if this persist silently fails.
+ # Only run the follow-up persist if the tool_result row was
+ # actually appended by _dispatch_response (currently always
+ # true for this variant, but we guard so a future refactor
+ # that conditionally skips the append can't silently land
+ # a user row before a missing tool_result).
+ if (
+ isinstance(response, StreamToolOutputAvailable)
+ and dispatched is not None
+ and acc.has_tool_results
+ ):
+ followup_drained = await drain_pending_for_persist(
+ ctx.session.session_id
+ )
+ if followup_drained and await persist_pending_as_user_rows(
+ ctx.session,
+ state.transcript_builder,
+ followup_drained,
+ log_prefix=ctx.log_prefix,
+ ):
+ # Track CLI-JSONL-invisible rows so the upload
+ # watermark excludes them and the next turn's
+ # detect_gap picks them up as gap-fill.
+ state.midturn_user_rows += len(followup_drained)
+
# Append assistant entry AFTER convert_message so that
# any stashed tool results from the previous turn are
# recorded first, preserving the required API order:
@@ -2596,6 +2697,7 @@ async def stream_chat_completion_sdk(
permissions: "CopilotPermissions | None" = None,
mode: CopilotMode | None = None,
model: CopilotLlmModel | None = None,
+ request_arrival_at: float = 0.0,
**_kwargs: Any,
) -> AsyncIterator[StreamBaseResponse]:
"""Stream chat completion using Claude Agent SDK.
@@ -2646,20 +2748,47 @@ async def stream_chat_completion_sdk(
if message:
message = strip_user_context_tags(message)
- if maybe_append_user_message(session, message, is_user_message):
- if is_user_message:
- track_user_message(
- user_id=user_id,
- session_id=session_id,
- message_length=len(message or ""),
- )
+ _user_message_appended = maybe_append_user_message(
+ session, message, is_user_message
+ )
+ if _user_message_appended and is_user_message:
+ track_user_message(
+ user_id=user_id,
+ session_id=session_id,
+ message_length=len(message or ""),
+ )
# Structured log prefix: [SDK][<session>][T<turn>]
# Turn = number of user messages (1-based), computed AFTER appending the new message.
turn = sum(1 for m in session.messages if m.role == "user")
log_prefix = f"[SDK][{session_id[:12]}][T{turn}]"
- session = await upsert_chat_session(session)
+ # Persist the appended user message to DB immediately so page refreshes
+ # during a long-running turn (e.g. auto-continue whose sleep/bash call
+ # blocks for minutes) show the user bubble. routes.py pre-saves the
+ # user message before direct POSTs so maybe_append_user_message returns
+ # False there (duplicate) — this branch only fires for internal callers
+ # that did NOT pre-save, most notably the auto-continue recursive call
+ # below.
+ #
+ # If the persist fails, roll back the in-memory append: otherwise
+ # session.messages[-1] carries a ``sequence=None`` ghost row, and a
+ # later turn-start drain (from a pending message queued during this
+ # turn) would trip the "no sequence" RuntimeError and crash the turn.
+ if _user_message_appended and is_user_message:
+ session = await persist_session_safe(session, log_prefix)
+ if session.messages and session.messages[-1].sequence is None:
+ # Eager persist swallowed a transient DB failure and left the
+ # in-memory append without a sequence. Roll back so the session
+ # stays consistent with the DB and raise so the caller can
+ # re-queue any drained content. Without this, a later
+ # turn-start drain would trip the "no sequence" RuntimeError
+ # and lose the fresh pending messages it just LPOPed.
+ session.messages.pop()
+ raise RuntimeError(
+ f"{log_prefix} Eager persist of user message failed; "
+ f"in-memory append rolled back"
+ )
# Generate title for new sessions (first user message)
if is_user_message and not session.title:
@@ -2985,6 +3114,77 @@ async def stream_chat_completion_sdk(
if last_user:
current_message = last_user[-1].content or ""
+ # Capture the message count *before* draining so _build_query_message
+ # can compute the gap slice without including the newly-drained pending
+ # messages. Pending messages are both appended to session.messages AND
+ # concatenated into current_message; without the ceiling the gap slice
+ # would extend into the pending messages and duplicate them in the
+ # model's input context (gap_context + current_message both containing
+ # them).
+ _pre_drain_msg_count = len(session.messages)
+
+ # Drain any messages the user queued via POST /messages/pending
+ # while the previous turn was running (or since the session was
+ # idle). Messages are drained ATOMICALLY — one LPOP with count
+ # removes them all at once, so a concurrent push lands *after*
+ # the drain and stays queued for the next turn instead of being
+ # lost between LPOP and clear. File IDs and context are
+ # preserved via format_pending_as_user_message.
+ #
+ # The drained content is combined in chronological (typing) order:
+ # pending messages were queued DURING the previous turn, so they
+ # were typed BEFORE the current /stream message. Putting pending
+ # first — ``pending → current`` — matches the order the user
+ # actually sent them and avoids the "I typed A then B but it shows
+ # up as B then A" confusion. The already-saved user message in
+ # the DB is updated via update_message_content_by_sequence to
+ # include the pending texts, avoiding a duplicate INSERT that
+ # would occur if we used insert_pending_before_last +
+ # persist_session_safe (routes.py has already saved the user
+ # message at sequence N before the executor runs, so an
+ # incremental upsert would write a second copy at N+1).
+ pending_messages = await drain_pending_safe(session_id, log_prefix)
+ if pending_messages:
+ logger.info(
+ "%s Draining %d pending message(s) at turn start",
+ log_prefix,
+ len(pending_messages),
+ )
+ # Chronological combine: items typed BEFORE this request
+ # arrived go ahead of ``current_message``; items typed AFTER
+ # (race path, queued while /stream was still processing) go
+ # after. ``pending_texts`` is kept around because downstream
+ # code (the executor's update_message_content_by_sequence
+ # call) needs the pre-combine list.
+ pending_texts = pending_texts_from(pending_messages)
+ current_message = combine_pending_with_current(
+ pending_messages,
+ current_message,
+ request_arrival_at=request_arrival_at,
+ )
+ # Update the in-memory content of the already-saved user message
+ # and persist that update to the DB by sequence number. This
+ # avoids inserting an extra row — the user message was already
+ # written at its sequence by append_and_save_message in routes.py.
+ last_user_msg = next(
+ (m for m in reversed(session.messages) if m.role == "user"), None
+ )
+ if last_user_msg is None or last_user_msg.sequence is None:
+ # Defensive: routes.py always pre-saves the user message with
+ # a sequence before dispatch, so this is unreachable under
+ # normal flow. Raising instead of a warning-and-continue
+ # avoids silent data loss (in-memory diverges from DB row,
+ # so the queued chip would disappear from the UI after
+ # refresh without a corresponding bubble).
+ raise RuntimeError(
+ f"{log_prefix} Cannot persist turn-start pending injection: "
+ f"last_user_msg={'missing' if last_user_msg is None else 'has no sequence'}"
+ )
+ last_user_msg.content = current_message
+ await chat_db().update_message_content_by_sequence(
+ session_id, last_user_msg.sequence, current_message
+ )
+
if not current_message.strip():
yield StreamError(
errorText="Message cannot be empty.",
@@ -3036,6 +3236,7 @@ async def stream_chat_completion_sdk(
use_resume,
transcript_msg_count,
session_id,
+ session_msg_ceiling=_pre_drain_msg_count,
prior_messages=restore_context_messages,
)
# If files are attached, prepare them: images become vision
@@ -3199,6 +3400,7 @@ async def stream_chat_completion_sdk(
state.use_resume,
state.transcript_msg_count,
session_id,
+ session_msg_ceiling=_pre_drain_msg_count,
target_tokens=state.target_tokens,
)
if attachments.hint:
@@ -3531,6 +3733,11 @@ async def stream_chat_completion_sdk(
raise
finally:
+ # Pending messages are drained atomically at the start of each
+ # turn (see drain_pending_messages call above), so there's
+ # nothing to clean up here — any message pushed after that
+ # point belongs to the next turn.
+
# --- Close OTEL context (with cost attributes) ---
if _otel_ctx is not None:
try:
@@ -3689,7 +3896,19 @@ async def stream_chat_completion_sdk(
# That concern was addressed by the inflated-watermark fix
# (using the GCS watermark as the anchor for gap detection),
# which makes len(session.messages) safe to use here.
- _jsonl_covered = len(session.messages)
+ #
+ # Mid-turn follow-up user rows (persisted via the
+ # StreamToolOutputAvailable handler) are NOT part of the CLI
+ # JSONL — the CLI only knows them as embedded text inside a
+ # tool_result, and even that embedding can be stripped by
+ # the CLI's internal tool_result size cap. Deduct them
+ # from the watermark so detect_gap on the next turn
+ # treats them as gap-fill entries and the model sees them
+ # as real user messages instead of missing text.
+ _midturn_offset = (
+ state.midturn_user_rows if state is not None else 0
+ )
+ _jsonl_covered = len(session.messages) - _midturn_offset
await asyncio.shield(
upload_transcript(
user_id=user_id,
@@ -3715,3 +3934,86 @@ async def stream_chat_completion_sdk(
finally:
# Release stream lock to allow new streams for this session
await lock.release()
+
+ # -------------------------------------------------------------------------
+ # Auto-continue: drain any messages the user queued AFTER the turn-start
+ # drain window and process them as a new turn automatically.
+ #
+ # This code only executes on NORMAL turn completion. GeneratorExit and
+ # BaseException both re-raise inside their except blocks, so the generator
+ # closes before reaching here — messages queued during a cancelled turn are
+ # preserved in Redis for the next manual turn.
+ # -------------------------------------------------------------------------
+ if not ended_with_stream_error:
+ _auto_pending_messages = await drain_pending_safe(session_id, log_prefix)
+ if _auto_pending_messages:
+ logger.info(
+ "%s Auto-continuing with %d pending message(s) queued after turn start",
+ log_prefix,
+ len(_auto_pending_messages),
+ )
+ # Combine all pending messages into one turn so they are processed
+ # together rather than sequentially. The recursive call may itself
+ # drain further messages queued while this turn runs.
+ _auto_combined = "\n\n".join(pending_texts_from(_auto_pending_messages))
+ # Race guard: drain_pending_safe has already LPOPed the messages
+ # from Redis. If another request acquires the session lock in the
+ # window between our lock.release() above and the recursive call's
+ # try_acquire() below, that recursive call exits with
+ # "stream_already_active" and the drained messages would be
+ # permanently lost. Detect that sentinel on the first yielded
+ # event and push the drained messages back to Redis so the
+ # competing stream's turn-start drain picks them up — preserving
+ # the original ``file_ids`` / ``context`` metadata (sentry
+ # r3105523410 — text-only requeue silently stripped it).
+ _auto_requeued = False
+ _first_auto_event = True
+
+ async def _requeue_drained(reason: str) -> None:
+ logger.warning(
+ "%s Auto-continue %s; re-queueing %d drained message(s)",
+ log_prefix,
+ reason,
+ len(_auto_pending_messages),
+ )
+ for _pm in _auto_pending_messages:
+ try:
+ await push_pending_message(session_id, _pm)
+ except Exception:
+ logger.exception(
+ "%s Failed to re-queue auto-continue message",
+ log_prefix,
+ )
+
+ try:
+ async for event in stream_chat_completion_sdk(
+ session_id=session_id,
+ message=_auto_combined,
+ is_user_message=True,
+ user_id=user_id,
+ file_ids=None,
+ permissions=permissions,
+ mode=mode,
+ model=model,
+ ):
+ if _first_auto_event:
+ _first_auto_event = False
+ if (
+ isinstance(event, StreamError)
+ and getattr(event, "code", None) == "stream_already_active"
+ ):
+ await _requeue_drained("lost lock race")
+ _auto_requeued = True
+ # Suppress the stale "already active" error —
+ # the competing stream will emit its own events.
+ continue
+ yield event
+ except Exception:
+ # Eager-persist rollback or any other failure inside the
+ # recursive call before messages were consumed. Push the
+ # drained texts back so the next turn picks them up.
+ if not _auto_requeued:
+ await _requeue_drained("raised during recursive call")
+ raise
+ if _auto_requeued:
+ return
diff --git a/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py b/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py
index 847d4d4970..69ec687c0b 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/session_waiter.py
@@ -7,20 +7,12 @@ and ``AutoPilotBlock`` all delegate a copilot turn to the
centralised primitive so every caller agrees on the dispatch shape,
the event aggregation, and the cleanup contract.
-Two wait modes:
-
-* :func:`wait_for_session_completion` — cheap "did it finish?" when the
- caller only needs a ``SessionOutcome`` (``running`` / ``completed`` /
- ``failed``). Used by ``get_sub_session_result`` when it only needs to
- decide between returning the final ChatSession state or "still busy".
-* :func:`wait_for_session_result` — accumulates stream events into an
- :class:`EventAccumulator` so the caller also gets back
- ``response_text`` / ``tool_calls`` / token usage in memory, without
- an extra DB round-trip. Used by the full-result callers
- (``run_sub_session`` completed path, ``AutoPilotBlock.execute_copilot``).
-
-Plus :func:`run_copilot_turn_via_queue` — the one-shot "create session
-meta → enqueue → wait for result" sequence that every caller uses.
+:func:`wait_for_session_result` accumulates stream events into an
+:class:`EventAccumulator` so callers get back ``response_text`` /
+``tool_calls`` / token usage in memory without an extra DB round-trip.
+
+:func:`run_copilot_turn_via_queue` is the one-shot "create session meta
+→ enqueue → wait for result" sequence every caller uses.
"""
from __future__ import annotations
@@ -33,6 +25,10 @@ from typing import TYPE_CHECKING, Literal
from backend.copilot import stream_registry
from backend.copilot.executor.utils import enqueue_copilot_turn
+from backend.copilot.pending_message_helpers import (
+ is_turn_in_flight,
+ queue_user_message,
+)
from backend.copilot.response_model import StreamError, StreamFinish
from .stream_accumulator import EventAccumulator, ToolCallEntry, process_event
@@ -43,42 +39,28 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
-SessionOutcome = Literal["completed", "failed", "running"]
+SessionOutcome = Literal["completed", "failed", "running", "queued"]
@dataclass
class SessionResult:
"""Aggregated result from a copilot session turn observed via
- ``stream_registry``. Mirrors :class:`collect.CopilotResult` so both
- in-process and cross-process consumers get the same shape."""
+ ``stream_registry``.
+
+ When ``queued`` is set, :func:`run_copilot_turn_via_queue` detected an
+ in-flight turn on the target session and pushed the message onto the
+ pending buffer instead of starting a new turn. ``response_text`` is
+ empty and the aggregate counts are zero in that case; the executor
+ running the earlier turn drains the buffer on its next round.
+ """
response_text: str = ""
tool_calls: list[ToolCallEntry] = field(default_factory=list)
prompt_tokens: int = 0
completion_tokens: int = 0
total_tokens: int = 0
- error_text: str | None = None
-
-
-async def wait_for_session_completion(
- *,
- session_id: str,
- user_id: str | None,
- timeout: float,
-) -> SessionOutcome:
- """Return the outcome of the latest turn on *session_id* within *timeout*.
-
- Light-weight variant of :func:`wait_for_session_result` — drops the
- event aggregation so callers that only need to decide between "still
- running" and "terminal" don't pay for building an accumulator.
- """
- outcome, _ = await _drain_until_terminal(
- session_id=session_id,
- user_id=user_id,
- timeout=timeout,
- accumulate=False,
- )
- return outcome
+ queued: bool = False
+ pending_buffer_length: int = 0
async def wait_for_session_result(
@@ -87,70 +69,56 @@ async def wait_for_session_result(
user_id: str | None,
timeout: float,
) -> tuple[SessionOutcome, SessionResult]:
- """Drain the session's stream events, aggregate them into a result.
+ """Drain the session's stream events and aggregate them into a result.
Returns whatever has been observed at the cap (``running`` + partial
result) or at the terminal event (``completed`` / ``failed`` + full
result). Cleans up the subscriber listener on every exit path so
long-running polls don't leak listeners (sentry r3105348640).
"""
- outcome, acc = await _drain_until_terminal(
- session_id=session_id,
- user_id=user_id,
- timeout=timeout,
- accumulate=True,
- )
- result = SessionResult()
- if acc is not None:
- result.response_text = "".join(acc.response_parts)
- result.tool_calls = list(acc.tool_calls)
- result.prompt_tokens = acc.prompt_tokens
- result.completion_tokens = acc.completion_tokens
- result.total_tokens = acc.total_tokens
- return outcome, result
-
-
-async def _drain_until_terminal(
- *,
- session_id: str,
- user_id: str | None,
- timeout: float,
- accumulate: bool,
-) -> tuple[SessionOutcome, EventAccumulator | None]:
- """Shared drain loop used by both wait helpers."""
queue = await stream_registry.subscribe_to_session(
session_id=session_id,
user_id=user_id,
)
+ result = SessionResult()
if queue is None:
# Session meta not in Redis yet, or the caller doesn't own it.
# ``subscribe_to_session`` already retried with backoff before
# returning None.
- return "running", (EventAccumulator() if accumulate else None)
+ return "running", result
- acc = EventAccumulator() if accumulate else None
+ acc = EventAccumulator()
+ outcome: SessionOutcome = "running"
try:
loop = asyncio.get_event_loop()
deadline = loop.time() + max(timeout, 0)
while True:
remaining = deadline - loop.time()
if remaining <= 0:
- return "running", acc
+ break
event = await asyncio.wait_for(queue.get(), timeout=remaining)
- if accumulate and acc is not None:
- process_event(event, acc)
+ process_event(event, acc)
if isinstance(event, StreamFinish):
- return "completed", acc
+ outcome = "completed"
+ break
if isinstance(event, StreamError):
- return "failed", acc
+ outcome = "failed"
+ break
except asyncio.TimeoutError:
- return "running", acc
+ pass
finally:
await stream_registry.unsubscribe_from_session(
session_id=session_id,
subscriber_queue=queue,
)
+ result.response_text = "".join(acc.response_parts)
+ result.tool_calls = list(acc.tool_calls)
+ result.prompt_tokens = acc.prompt_tokens
+ result.completion_tokens = acc.completion_tokens
+ result.total_tokens = acc.total_tokens
+ return outcome, result
+
async def run_copilot_turn_via_queue(
*,
@@ -178,7 +146,55 @@ async def run_copilot_turn_via_queue(
``tool_call_id`` / ``tool_name`` disambiguate who originated the
turn in observability / replay (e.g. ``"sub:<parent>"`` for a
sub-session, ``"autopilot_block"`` for an AutoPilotBlock run).
+
+ Self-defensive queue-fallback: if the target session already has a
+ turn running (another ``run_sub_session`` / AutoPilot block / UI
+ chat), don't race it on the cluster lock. Push the message onto the
+ pending buffer so the existing turn drains it at its next round
+ boundary, then:
+
+ * ``timeout == 0`` — return immediately with
+ ``("queued", SessionResult(queued=True, ...))``. Callers that
+ explicitly opted into fire-and-forget (``run_sub_session`` with
+ ``wait_for_result=0``) use this to bail without waiting.
+ * ``timeout > 0`` — **subscribe to the in-flight turn's stream and
+ return its aggregated result** (exactly the same shape as a
+ normally-dispatched turn, but with ``result.queued=True`` so
+ callers can tell we rode on someone else's turn). Semantically
+ identical to "I asked the session to do something and here is
+ what happened next"; no separate deferred-state branch needed in
+ ``run_sub_session`` / ``AutoPilotBlock``.
"""
+ if await is_turn_in_flight(session_id):
+ logger.info(
+ "[queue] session=%s has a turn in flight; queueing message "
+ "(tool=%s) into pending buffer instead of starting a new turn",
+ session_id[:12],
+ tool_name,
+ )
+ state = await queue_user_message(session_id=session_id, message=message)
+ if timeout <= 0:
+ # Fire-and-forget: caller explicitly asked not to wait.
+ return "queued", SessionResult(
+ queued=True, pending_buffer_length=state.buffer_length
+ )
+ # Ride the in-flight turn: subscribe to its stream and return the
+ # same aggregated result shape as a fresh dispatch. The model
+ # drains the pending buffer between tool rounds (baseline) or at
+ # the next tool boundary via the PostToolUse hook (SDK), so the
+ # response we observe will reflect our queued follow-up (or be
+ # the terminal result if the in-flight turn finishes before the
+ # buffer drains — in that case ``result.queued=True`` is still
+ # the correct signal for the caller).
+ outcome, observed = await wait_for_session_result(
+ session_id=session_id,
+ user_id=user_id,
+ timeout=timeout,
+ )
+ observed.queued = True
+ observed.pending_buffer_length = state.buffer_length
+ return outcome, observed
+
turn_id = str(uuid.uuid4())
await stream_registry.create_session(
session_id=session_id,
diff --git a/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py b/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py
new file mode 100644
index 0000000000..dfdafa87ce
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/session_waiter_test.py
@@ -0,0 +1,169 @@
+"""Tests for the shared queue primitive in ``session_waiter``.
+
+Focuses on the queue-on-busy fallback:
+
+* ``timeout == 0`` — push into the buffer and return immediately with
+ ``("queued", SessionResult(queued=True, ...))``; skip registry +
+ RabbitMQ entirely.
+* ``timeout > 0`` — push into the buffer, then subscribe to the
+ in-flight turn's stream and return its aggregated result (with
+ ``queued=True`` annotation) so callers get the same shape as a
+ fresh dispatch.
+"""
+
+from unittest.mock import AsyncMock, patch
+
+import pytest
+
+from backend.copilot.sdk.session_waiter import SessionResult, run_copilot_turn_via_queue
+
+_QR = type(
+ "QR",
+ (),
+ {"buffer_length": 4, "max_buffer_length": 10, "turn_in_flight": True},
+)
+
+
+async def test_queue_branch_timeout_zero_returns_immediately():
+ """Busy + timeout=0 → no registry, no enqueue, no wait, queued result."""
+ queue_mock = AsyncMock(return_value=_QR())
+ create_session = AsyncMock()
+ enqueue = AsyncMock()
+ wait_result = AsyncMock()
+
+ with (
+ patch(
+ "backend.copilot.sdk.session_waiter.is_turn_in_flight",
+ new=AsyncMock(return_value=True),
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.queue_user_message",
+ new=queue_mock,
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.stream_registry.create_session",
+ new=create_session,
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.enqueue_copilot_turn",
+ new=enqueue,
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.wait_for_session_result",
+ new=wait_result,
+ ),
+ ):
+ outcome, result = await run_copilot_turn_via_queue(
+ session_id="sess-busy",
+ user_id="u1",
+ message="follow-up",
+ timeout=0,
+ tool_call_id="sub:parent",
+ tool_name="run_sub_session",
+ )
+
+ assert outcome == "queued"
+ assert isinstance(result, SessionResult)
+ assert result.queued is True
+ assert result.pending_buffer_length == 4
+ create_session.assert_not_awaited()
+ enqueue.assert_not_awaited()
+ wait_result.assert_not_awaited()
+ queue_mock.assert_awaited_once_with(session_id="sess-busy", message="follow-up")
+
+
+async def test_queue_branch_positive_timeout_rides_inflight_turn():
+ """Busy + timeout>0 → push buffer, subscribe to in-flight turn, return
+ its aggregated result with ``queued=True`` annotation."""
+ queue_mock = AsyncMock(return_value=_QR())
+ create_session = AsyncMock()
+ enqueue = AsyncMock()
+ observed = SessionResult()
+ observed.response_text = "final answer from in-flight turn"
+ wait_result = AsyncMock(return_value=("completed", observed))
+
+ with (
+ patch(
+ "backend.copilot.sdk.session_waiter.is_turn_in_flight",
+ new=AsyncMock(return_value=True),
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.queue_user_message",
+ new=queue_mock,
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.stream_registry.create_session",
+ new=create_session,
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.enqueue_copilot_turn",
+ new=enqueue,
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.wait_for_session_result",
+ new=wait_result,
+ ),
+ ):
+ outcome, result = await run_copilot_turn_via_queue(
+ session_id="sess-busy",
+ user_id="u1",
+ message="follow-up",
+ timeout=30.0,
+ tool_call_id="autopilot_block",
+ tool_name="autopilot_block",
+ )
+
+ # We rode on the existing turn — its outcome + aggregate propagate up.
+ assert outcome == "completed"
+ assert result.response_text == "final answer from in-flight turn"
+ # Marker so callers can tell we didn't start a fresh turn.
+ assert result.queued is True
+ assert result.pending_buffer_length == 4
+ # Still no new registry entry / no new RabbitMQ job — that was the point.
+ create_session.assert_not_awaited()
+ enqueue.assert_not_awaited()
+ # Subscribed to the session stream (not a new turn_id).
+ wait_result.assert_awaited_once()
+ assert wait_result.await_args.kwargs["session_id"] == "sess-busy"
+
+
+async def test_idle_session_enqueues_normally():
+ """Idle session → registry session created, enqueued, drain waits."""
+ create_session = AsyncMock()
+ enqueue = AsyncMock()
+ wait_result = AsyncMock(return_value=("completed", SessionResult()))
+
+ with (
+ patch(
+ "backend.copilot.sdk.session_waiter.is_turn_in_flight",
+ new=AsyncMock(return_value=False),
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.stream_registry.create_session",
+ new=create_session,
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.enqueue_copilot_turn",
+ new=enqueue,
+ ),
+ patch(
+ "backend.copilot.sdk.session_waiter.wait_for_session_result",
+ new=wait_result,
+ ),
+ ):
+ outcome, result = await run_copilot_turn_via_queue(
+ session_id="sess-idle",
+ user_id="u1",
+ message="kick off",
+ timeout=0.1,
+ tool_call_id="autopilot_block",
+ tool_name="autopilot_block",
+ )
+
+ assert outcome == "completed"
+ assert result.queued is False
+ create_session.assert_awaited_once()
+ enqueue.assert_awaited_once()
diff --git a/autogpt_platform/backend/backend/copilot/sdk/stream_accumulator.py b/autogpt_platform/backend/backend/copilot/sdk/stream_accumulator.py
index a61e0827e0..9cd66b104d 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/stream_accumulator.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/stream_accumulator.py
@@ -2,17 +2,14 @@
Consumes the same ``StreamBaseResponse`` events that fly over
``stream_registry`` (text deltas, tool i/o, usage, errors) and folds
-them into a single :class:`EventAccumulator` state. Two consumers:
-
-* :func:`collect.collect_copilot_response` — drives the SDK stream
- directly on the worker that's running the turn.
-* :func:`session_waiter.wait_for_session_result` — reads the same
- events from a Redis Stream subscription, so a different process can
- obtain the same aggregated result for a session it didn't run.
-
-Keeping the dispatch in one place means both entry points agree on
-what "response_text", "tool_calls", and token counts mean without
-drifting apart as new event types are added.
+them into a single :class:`EventAccumulator` state. Used by
+:func:`session_waiter.wait_for_session_result` to read events from a
+Redis Stream subscription so a different process can obtain the
+aggregated result for a session it didn't run.
+
+Keeping the dispatch in one place means new event types can be added
+without drifting callers apart on what "response_text", "tool_calls",
+or token counts mean.
"""
from __future__ import annotations
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
index 5b2a80fb5b..d97937da23 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
@@ -62,11 +62,24 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
-# Max MCP response size in chars. 100K chars ≈ 25K tokens. The SDK writes oversized results to tool-results/ files.
-# Set to 100K (down from a previous 500K) because the SDK already reads back large results from disk via
-# tool-results/ — sending 500K chars inline bloated the context window and caused cache-miss thrashing.
-# 100K keeps the common case (block output, API responses) in-band without punishing the context budget.
-_MCP_MAX_CHARS = 100_000
+# Max MCP response size in chars — sized to the Claude CLI's internal cap.
+#
+# The CLI has a default ``maxResultSizeChars = 1e5`` (100K chars) annotation
+# for MCP tool results, but the actual trigger is TOKEN-based (see
+# ``sizeEstimateTokens`` in the bundled CLI at ``tengu_mcp_large_result_handled``)
+# and fires around 20–25K tokens. For JSON-heavy tool output (~3–4 chars/token)
+# that lands anywhere from ~60K to ~100K chars in practice; we've observed the
+# error path at 81K chars in production. When it fires, the CLI persists the
+# full output to disk and REPLACES the returned content with a synthetic
+# ``"Error: result (N characters) exceeds maximum allowed tokens. Output has
+# been saved to …"`` message — which destroys any `<user_follow_up>` block
+# we injected.
+#
+# 70K gives us headroom below the observed 81K trigger and leaves ~6K for the
+# follow-up injection plus CLI wire overhead. Oversized content is still
+# reachable via ``read_tool_result`` against the persisted disk file; only
+# the inline reply to this specific call is truncated.
+_MCP_MAX_CHARS = 70_000
# MCP server naming - the SDK prefixes tool names as "mcp__{server_name}__{tool}"
MCP_SERVER_NAME = "copilot"
@@ -619,8 +632,12 @@ def _make_truncating_wrapper(
else:
_clear_tool_failures(tool_name)
- # Stash BEFORE stripping so the frontend SSE stream receives
- # the full output including _STRIP_FROM_LLM fields (e.g. is_dry_run).
+ # Stash the raw tool output for the frontend SSE stream so widgets
+ # (bash, tool viewers) receive clean JSON. Mid-turn user follow-up
+ # injection for MCP + built-in tools is now handled uniformly by
+ # the ``PostToolUse`` hook via ``additionalContext`` so Claude sees
+ # the follow-up attached to the tool_result without mutating the
+ # frontend-facing payload.
if not truncated.get("isError"):
text = _text_from_mcp_result(truncated)
if text:
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
index a9a9671b9f..679ec234c2 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
@@ -968,3 +968,53 @@ class TestStripLlmFields:
stashed = pop_pending_tool_output("fake_tool_normal")
assert stashed is not None
assert '"is_dry_run": true' in stashed
+
+
+class TestTruncatingWrapperLeavesOutputUntouched:
+ """Mid-turn drain moved to the shared ``PostToolUse`` hook path so every
+ tool (MCP + built-in) is covered uniformly. The wrapper must therefore
+ forward tool output verbatim and never touch ``<user_follow_up>``."""
+
+ @pytest.mark.asyncio
+ async def test_wrapper_does_not_inject_followup(self):
+ session = MagicMock()
+ session.dry_run = False
+ session.session_id = "sess-no-inject"
+ set_execution_context(user_id="u", session=session, sandbox=None, sdk_cwd="/tmp/test") # type: ignore[arg-type]
+
+ async def fake_tool_fn(_args: dict) -> dict:
+ return {
+ "content": [{"type": "text", "text": "CLEAN_OUTPUT"}],
+ "isError": False,
+ }
+
+ wrapper = _make_truncating_wrapper(fake_tool_fn, "fake_tool_clean")
+ result = await wrapper({})
+
+ text = result["content"][0]["text"]
+ assert text == "CLEAN_OUTPUT"
+ assert "<user_follow_up>" not in text
+
+ @pytest.mark.asyncio
+ async def test_stash_stays_clean(self):
+ """The frontend-facing stash must be a byte-for-byte copy of the
+ raw tool output (needed for JSON.parse in the bash widget)."""
+ session = MagicMock()
+ session.dry_run = False
+ session.session_id = "sess-stash"
+ set_execution_context(user_id="u", session=session, sandbox=None, sdk_cwd="/tmp/test") # type: ignore[arg-type]
+
+ clean_json = '{"stdout": "hello\\n", "exit_code": 0}'
+
+ async def fake_tool_fn(_args: dict) -> dict:
+ return {
+ "content": [{"type": "text", "text": clean_json}],
+ "isError": False,
+ }
+
+ wrapper = _make_truncating_wrapper(fake_tool_fn, "fake_tool_stash_pure")
+ await wrapper({})
+
+ stashed = pop_pending_tool_output("fake_tool_stash_pure")
+ assert stashed == clean_json
+ assert "<user_follow_up>" not in (stashed or "")
diff --git a/autogpt_platform/backend/backend/copilot/service.py b/autogpt_platform/backend/backend/copilot/service.py
index 3372cd1ddb..135d08a44a 100644
--- a/autogpt_platform/backend/backend/copilot/service.py
+++ b/autogpt_platform/backend/backend/copilot/service.py
@@ -446,7 +446,9 @@ async def inject_user_context(
+ final_message
)
- for session_msg in session_messages:
+ # Scan in reverse so we target the current turn's user message, not
+ # an older one that may exist when pending messages have been drained.
+ for session_msg in reversed(session_messages):
if session_msg.role == "user":
# Only touch the DB / in-memory state when the content actually
# needs to change — avoids an unnecessary write on the common
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry.py b/autogpt_platform/backend/backend/copilot/stream_registry.py
index 02fa21b574..e669e79dd1 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry.py
@@ -32,6 +32,7 @@ from backend.data.notification_bus import (
NotificationEvent,
)
from backend.data.redis_client import get_redis_async
+from backend.data.redis_helpers import hash_compare_and_set
from .config import ChatConfig
from .executor.utils import COPILOT_CONSUMER_TIMEOUT_SECONDS
@@ -68,17 +69,6 @@ _listener_sessions: dict[int, tuple[str, asyncio.Task]] = {}
# If the queue is full and doesn't drain within this time, send an overflow error
QUEUE_PUT_TIMEOUT = 5.0
-# Lua script for atomic compare-and-swap status update (idempotent completion)
-# Returns 1 if status was updated, 0 if already completed/failed
-COMPLETE_SESSION_SCRIPT = """
-local current = redis.call("HGET", KEYS[1], "status")
-if current == "running" then
- redis.call("HSET", KEYS[1], "status", ARGV[1])
- return 1
-end
-return 0
-"""
-
@dataclass
class ActiveSession:
@@ -839,12 +829,14 @@ async def mark_session_completed(
turn_id = _parse_session_meta(meta, session_id).turn_id if meta else session_id
# Atomic compare-and-swap: only update if status is "running"
- result = await redis.eval(COMPLETE_SESSION_SCRIPT, 1, meta_key, status) # type: ignore[misc]
+ swapped = await hash_compare_and_set(
+ redis, meta_key, "status", expected="running", new=status
+ )
# Clean up the in-memory TTL refresh tracker to prevent unbounded growth.
_meta_ttl_refresh_at.pop(session_id, None)
- if result == 0:
+ if not swapped:
logger.debug(f"Session {session_id} already completed/failed, skipping")
return False
diff --git a/autogpt_platform/backend/backend/copilot/tools/models.py b/autogpt_platform/backend/backend/copilot/tools/models.py
index 645d6468ef..d6606e5d4b 100644
--- a/autogpt_platform/backend/backend/copilot/tools/models.py
+++ b/autogpt_platform/backend/backend/copilot/tools/models.py
@@ -287,8 +287,13 @@ class SubSessionStatusResponse(ToolResponseBase):
"""
type: ResponseType = ResponseType.MCP_TOOL_OUTPUT
- status: Literal["running", "completed", "cancelled", "error"] = Field(
- description="Current state of the sub-AutoPilot run.",
+ status: Literal["running", "completed", "cancelled", "error", "queued"] = Field(
+ description=(
+ "Current state of the sub-AutoPilot run. ``queued`` means the "
+ "target session already had a turn in flight, so the message was "
+ "pushed onto its pending buffer and will be picked up by the "
+ "existing turn on its next drain."
+ ),
)
sub_session_id: str = Field(
description=(
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 a972335606..82bbc5e53b 100644
--- a/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
+++ b/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
@@ -8,7 +8,7 @@ Mirror-image of ``run_agent`` + ``view_agent_output`` for copilot turns:
``CoPilotExecutionEntry``, and waits on the Redis stream until the
terminal event arrives or the cap fires.
2. Any available ``copilot_executor`` worker claims the job, runs
- ``collect_copilot_response`` to completion, and publishes the final
+ the SDK stream to completion, and publishes the final
``StreamFinish`` event on the session's Redis stream.
3. If the terminal event arrives in the wait window, the aggregated
:class:`SessionResult` (response text, tool calls, usage) comes back
@@ -85,9 +85,7 @@ class RunSubSessionTool(BaseTool):
},
"sub_autopilot_session_id": {
"type": "string",
- "description": (
- "Continue a prior sub via its session_id; empty = new."
- ),
+ "description": ("Continue/queue-into a prior sub; empty = new."),
"default": "",
},
"wait_for_result": {
@@ -197,8 +195,28 @@ def response_from_outcome(
``completed`` surfaces the aggregated response text + tool calls.
``failed`` returns the error marker with the same handles.
``running`` returns just the polling handles so the agent can resume.
+ ``queued`` means the target session already had a turn in flight; the
+ message was appended to its pending buffer and will be processed by
+ the existing turn on its next drain.
"""
link = _sub_session_link(inner_session_id)
+ if outcome == "queued":
+ return SubSessionStatusResponse(
+ message=(
+ f"Target session already had a turn in flight; the message "
+ f"was queued ({result.pending_buffer_length} now pending) and "
+ "will be processed by the existing turn on its next drain. "
+ f"Call get_sub_session_result to poll progress"
+ f"{f' or watch live at {link}' if link else ''}."
+ ),
+ session_id=parent_session_id,
+ status="queued",
+ sub_session_id=inner_session_id,
+ sub_autopilot_session_id=inner_session_id,
+ sub_autopilot_session_link=link,
+ elapsed_seconds=round(elapsed, 2),
+ )
+
if outcome == "running":
return SubSessionStatusResponse(
message=(
diff --git a/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py b/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py
index 7ff2b60f50..8c67ca0008 100644
--- a/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py
@@ -4,7 +4,7 @@ Sub-AutoPilots are enqueued on the copilot_execution RabbitMQ queue and
executed by any copilot_executor worker. The tools wait for completion
by subscribing to ``stream_registry`` for the sub's ChatSession. These
tests patch the three integration seams — ``enqueue_copilot_turn``,
-``wait_for_session_completion``, and ``stream_registry.create_session``
+``wait_for_session_result``, and ``stream_registry.create_session``
— to exercise the tool logic without needing RabbitMQ or Redis.
"""
@@ -261,6 +261,29 @@ class TestRunSubSession:
assert r.tool_calls[0]["tool_name"] == "foo"
mock_waiter.assert_awaited_once()
+ @pytest.mark.asyncio
+ async def test_queued_outcome_surfaces_queued_status(
+ self, mock_queue, mock_waiter, mock_model
+ ):
+ """When the shared primitive reports the target session already has
+ a turn running, the tool surfaces ``status='queued'`` so the LLM can
+ decide whether to poll or move on."""
+ from backend.copilot.sdk.session_waiter import SessionResult
+
+ queued_res = SessionResult(queued=True, pending_buffer_length=2)
+ mock_waiter.return_value = ("queued", queued_res)
+
+ r = await RunSubSessionTool()._execute(
+ user_id="alice",
+ session=_session("alice"),
+ prompt="please do another thing",
+ wait_for_result=0,
+ )
+ assert isinstance(r, SubSessionStatusResponse)
+ assert r.status == "queued"
+ assert r.sub_session_id == "inner-1"
+ assert "queued" in (r.message or "").lower()
+
@pytest.mark.asyncio
async def test_wait_clamps_above_maximum(self, mock_queue, mock_waiter, mock_model):
"""wait_for_result values above the cap are clamped before being
diff --git a/autogpt_platform/backend/backend/data/redis_helpers.py b/autogpt_platform/backend/backend/data/redis_helpers.py
new file mode 100644
index 0000000000..fd6bebb313
--- /dev/null
+++ b/autogpt_platform/backend/backend/data/redis_helpers.py
@@ -0,0 +1,154 @@
+"""Shared Redis helpers for patterns that need client-side atomicity.
+
+Redis is a wonderful key-value store but has ergonomic gaps that every
+app ends up papering over the same way — usually as ad-hoc Lua EVALs or
+raw pipelines scattered across the codebase. This module collects the
+patterns we actually use into a single place:
+
+- :func:`incr_with_ttl` — atomic INCR + set TTL (Redis has no native
+ "increment and set TTL on first bump" command). Implemented with
+ ``pipeline(transaction=True)`` (MULTI/EXEC) — no Lua needed.
+- :func:`capped_rpush` — push to a bounded list (RPUSH + LTRIM + EXPIRE +
+ LLEN) atomically. Pipeline-based.
+- :func:`hash_compare_and_set` — set a hash field only if its current
+ value matches an expected one. Genuinely needs Lua because the
+ condition depends on the current value (pipeline can't branch).
+
+Everything sharable lives here. If a new Lua script is tempting in
+application code, add a helper here first — callers should not touch
+``redis.eval`` / ``pipeline(transaction=True)`` directly for anything
+this module can cover.
+"""
+
+from typing import Any, cast
+
+from redis import Redis
+from redis.asyncio import Redis as AsyncRedis
+
+# ---------------------------------------------------------------------------
+# Lua scripts — registered centrally so there is exactly ONE authoritative
+# copy per pattern and ``SCRIPT LOAD`` can be amortised in future if needed.
+# ---------------------------------------------------------------------------
+
+# Compare-and-set on a hash field. Returns 1 if swapped, 0 if the current
+# value didn't match. Needs Lua because the SET is conditional on a GET
+# result (MULTI/EXEC cannot branch on intermediate replies).
+#
+# KEYS[1] hash key
+# ARGV[1] hash field
+# ARGV[2] expected current value
+# ARGV[3] new value
+_HASH_CAS_LUA = """
+local current = redis.call('HGET', KEYS[1], ARGV[1])
+if current == ARGV[2] then
+ redis.call('HSET', KEYS[1], ARGV[1], ARGV[3])
+ return 1
+end
+return 0
+"""
+
+
+async def incr_with_ttl(
+ redis: AsyncRedis,
+ key: str,
+ ttl_seconds: int,
+ *,
+ reset_ttl_on_bump: bool = False,
+) -> int:
+ """Atomically increment *key* and set its TTL.
+
+ Returns the new counter value.
+
+ Args:
+ redis: AsyncRedis client.
+ key: Counter key.
+ ttl_seconds: TTL to apply to the key.
+ reset_ttl_on_bump: When ``False`` (default, fixed-window), the TTL is
+ only set on the first bump in a window — subsequent bumps leave
+ the existing TTL alone so the window genuinely expires
+ ``ttl_seconds`` after the first push. When ``True``
+ (sliding-window), every bump refreshes the TTL.
+
+ Atomicity: uses MULTI/EXEC so the counter can never end up without a
+ TTL (the classic "process dies between INCR and EXPIRE" orphan).
+ """
+ pipe = redis.pipeline(transaction=True)
+ pipe.incr(key)
+ # EXPIRE ... NX = "only set TTL if none exists" (Redis 7+). In
+ # reset_ttl_on_bump mode, unconditional EXPIRE refreshes every bump.
+ if reset_ttl_on_bump:
+ pipe.expire(key, ttl_seconds)
+ else:
+ pipe.expire(key, ttl_seconds, nx=True)
+ results = await pipe.execute()
+ return int(results[0])
+
+
+def incr_with_ttl_sync(
+ redis: Redis,
+ key: str,
+ ttl_seconds: int,
+ *,
+ reset_ttl_on_bump: bool = False,
+) -> int:
+ """Sync variant of :func:`incr_with_ttl` — same semantics."""
+ pipe = redis.pipeline(transaction=True)
+ pipe.incr(key)
+ if reset_ttl_on_bump:
+ pipe.expire(key, ttl_seconds)
+ else:
+ pipe.expire(key, ttl_seconds, nx=True)
+ results = pipe.execute()
+ return int(results[0])
+
+
+async def capped_rpush(
+ redis: AsyncRedis,
+ key: str,
+ value: str,
+ *,
+ max_len: int,
+ ttl_seconds: int,
+) -> int:
+ """Atomically RPUSH *value*, trim to *max_len*, set TTL, and return LLEN.
+
+ Returns the list length after the push+trim.
+
+ Atomicity: MULTI/EXEC so a concurrent LPOP can never observe the
+ list transiently over ``max_len``.
+
+ Use this for bounded producer/consumer buffers where the newest
+ entries matter most (LTRIM from the left, keeping the tail).
+ """
+ pipe = redis.pipeline(transaction=True)
+ pipe.rpush(key, value)
+ pipe.ltrim(key, -max_len, -1)
+ pipe.expire(key, ttl_seconds)
+ pipe.llen(key)
+ results = cast("list[Any]", await pipe.execute())
+ return int(results[-1])
+
+
+async def hash_compare_and_set(
+ redis: AsyncRedis,
+ key: str,
+ field: str,
+ *,
+ expected: str,
+ new: str,
+) -> bool:
+ """Atomically set ``HSET key field new`` iff current value == *expected*.
+
+ Returns ``True`` if the swap happened, ``False`` otherwise.
+
+ Use this for idempotent state transitions (e.g. mark a task as
+ ``completed`` only when it is still ``running``, so a late retry
+ cannot clobber an earlier terminal state). Genuinely needs Lua
+ because the write is conditional on the read result — MULTI/EXEC
+ cannot branch on intermediate replies.
+ """
+ result = await cast(
+ "Any",
+ redis.eval(_HASH_CAS_LUA, 1, key, field, expected, new),
+ )
+ return int(result) == 1
diff --git a/autogpt_platform/backend/backend/data/redis_helpers_test.py b/autogpt_platform/backend/backend/data/redis_helpers_test.py
new file mode 100644
index 0000000000..c45028dbac
--- /dev/null
+++ b/autogpt_platform/backend/backend/data/redis_helpers_test.py
@@ -0,0 +1,223 @@
+"""Unit tests for :mod:`backend.data.redis_helpers`.
+
+Uses a minimal in-memory fake Redis that only implements the surface
+exercised by the helpers: pipeline(transaction=True) with
+incr/expire/rpush/ltrim/llen, and eval() for the CAS helper.
+"""
+
+from typing import Any
+
+import pytest
+
+from backend.data.redis_helpers import (
+ capped_rpush,
+ hash_compare_and_set,
+ incr_with_ttl,
+ incr_with_ttl_sync,
+)
+
+# ── Fake Redis + pipeline ──────────────────────────────────────────────
+
+
+class _Fake:
+ """Async-only fake. Enough for ``incr_with_ttl`` + ``capped_rpush`` + CAS."""
+
+ def __init__(self) -> None:
+ self.counters: dict[str, int] = {}
+ self.lists: dict[str, list[str]] = {}
+ self.hashes: dict[str, dict[str, str]] = {}
+ self.ttls: dict[str, int] = {}
+ self.expire_calls: list[tuple[str, int, bool]] = []
+
+ # --- primitives ---
+ async def incr(self, key: str) -> int:
+ self.counters[key] = self.counters.get(key, 0) + 1
+ return self.counters[key]
+
+ async def expire(self, key: str, seconds: int, nx: bool = False) -> int:
+ self.expire_calls.append((key, seconds, nx))
+ if nx and key in self.ttls:
+ return 0
+ self.ttls[key] = seconds
+ return 1
+
+ async def rpush(self, key: str, *values: Any) -> int:
+ self.lists.setdefault(key, []).extend(str(v) for v in values)
+ return len(self.lists[key])
+
+ async def ltrim(self, key: str, start: int, stop: int) -> None:
+ lst = self.lists.get(key, [])
+ if stop == -1:
+ self.lists[key] = lst[start:]
+ else:
+ self.lists[key] = lst[start : stop + 1]
+
+ async def llen(self, key: str) -> int:
+ return len(self.lists.get(key, []))
+
+ async def eval(self, script: str, numkeys: int, *args: Any) -> int:
+ # Shim for hash-CAS only.
+ key, field, expected, new = args[0], args[1], args[2], args[3]
+ h = self.hashes.setdefault(key, {})
+ if h.get(field) == expected:
+ h[field] = new
+ return 1
+ return 0
+
+ # --- pipeline ---
+ def pipeline(self, transaction: bool = True) -> "_FakePipe":
+ return _FakePipe(self)
+
+
+class _FakePipe:
+ def __init__(self, parent: _Fake) -> None:
+ self._parent = parent
+ self._ops: list[tuple[str, tuple[Any, ...], dict[str, Any]]] = []
+
+ def incr(self, key: str) -> "_FakePipe":
+ self._ops.append(("incr", (key,), {}))
+ return self
+
+ def expire(self, key: str, seconds: int, **kw: Any) -> "_FakePipe":
+ self._ops.append(("expire", (key, seconds), kw))
+ return self
+
+ def rpush(self, key: str, value: Any) -> "_FakePipe":
+ self._ops.append(("rpush", (key, value), {}))
+ return self
+
+ def ltrim(self, key: str, start: int, stop: int) -> "_FakePipe":
+ self._ops.append(("ltrim", (key, start, stop), {}))
+ return self
+
+ def llen(self, key: str) -> "_FakePipe":
+ self._ops.append(("llen", (key,), {}))
+ return self
+
+ async def execute(self) -> list[Any]:
+ out: list[Any] = []
+ for name, args, kw in self._ops:
+ out.append(await getattr(self._parent, name)(*args, **kw))
+ return out
+
+
+class _SyncFake:
+ """Sync fake for :func:`incr_with_ttl_sync`."""
+
+ def __init__(self) -> None:
+ self.counters: dict[str, int] = {}
+ self.ttls: dict[str, int] = {}
+ self.expire_calls: list[tuple[str, int, bool]] = []
+
+ def incr(self, key: str) -> int:
+ self.counters[key] = self.counters.get(key, 0) + 1
+ return self.counters[key]
+
+ def expire(self, key: str, seconds: int, nx: bool = False) -> int:
+ self.expire_calls.append((key, seconds, nx))
+ if nx and key in self.ttls:
+ return 0
+ self.ttls[key] = seconds
+ return 1
+
+ def pipeline(self, transaction: bool = True) -> "_SyncPipe":
+ return _SyncPipe(self)
+
+
+class _SyncPipe:
+ def __init__(self, parent: _SyncFake) -> None:
+ self._parent = parent
+ self._ops: list[tuple[str, tuple[Any, ...], dict[str, Any]]] = []
+
+ def incr(self, key: str) -> "_SyncPipe":
+ self._ops.append(("incr", (key,), {}))
+ return self
+
+ def expire(self, key: str, seconds: int, **kw: Any) -> "_SyncPipe":
+ self._ops.append(("expire", (key, seconds), kw))
+ return self
+
+ def execute(self) -> list[Any]:
+ return [
+ getattr(self._parent, name)(*args, **kw) for name, args, kw in self._ops
+ ]
+
+
+# ── incr_with_ttl ──────────────────────────────────────────────────────
+
+
+async def test_incr_with_ttl_returns_count_and_sets_ttl_once() -> None:
+ """Fixed-window: TTL is set only on first bump, subsequent calls keep it."""
+ r = _Fake()
+ assert await incr_with_ttl(r, "k", 60) == 1 # type: ignore[arg-type]
+ assert await incr_with_ttl(r, "k", 60) == 2 # type: ignore[arg-type]
+ assert await incr_with_ttl(r, "k", 60) == 3 # type: ignore[arg-type]
+ # EXPIRE is always called, but with nx=True so only first succeeds.
+ assert all(nx for _, _, nx in r.expire_calls)
+ assert r.expire_calls == [("k", 60, True)] * 3
+
+
+async def test_incr_with_ttl_sliding_window_refreshes_every_bump() -> None:
+ r = _Fake()
+ await incr_with_ttl(r, "k", 10, reset_ttl_on_bump=True) # type: ignore[arg-type]
+ await incr_with_ttl(r, "k", 20, reset_ttl_on_bump=True) # type: ignore[arg-type]
+ # nx=False on every call → TTL is refreshed each bump.
+ assert r.expire_calls == [("k", 10, False), ("k", 20, False)]
+ assert r.ttls["k"] == 20
+
+
+def test_incr_with_ttl_sync_behaves_the_same() -> None:
+ r = _SyncFake()
+ assert incr_with_ttl_sync(r, "k", 5) == 1 # type: ignore[arg-type]
+ assert incr_with_ttl_sync(r, "k", 5) == 2 # type: ignore[arg-type]
+ assert r.expire_calls == [("k", 5, True), ("k", 5, True)]
+
+
+# ── capped_rpush ───────────────────────────────────────────────────────
+
+
+async def test_capped_rpush_returns_length_and_trims() -> None:
+ r = _Fake()
+ for i in range(5):
+ length = await capped_rpush(
+ r, "buf", f"item-{i}", max_len=3, ttl_seconds=30 # type: ignore[arg-type]
+ )
+ # After 5 pushes capped at 3, only the newest 3 remain.
+ assert length == 3
+ assert r.lists["buf"] == ["item-2", "item-3", "item-4"]
+
+
+async def test_capped_rpush_first_push_returns_one() -> None:
+ r = _Fake()
+ length = await capped_rpush(r, "buf", "only", max_len=10, ttl_seconds=60) # type: ignore[arg-type]
+ assert length == 1
+ assert r.lists["buf"] == ["only"]
+
+
+# ── hash_compare_and_set ───────────────────────────────────────────────
+
+
+async def test_hash_cas_swaps_when_expected_matches() -> None:
+ r = _Fake()
+ r.hashes["meta"] = {"status": "running"}
+ swapped = await hash_compare_and_set(
+ r, "meta", "status", expected="running", new="completed" # type: ignore[arg-type]
+ )
+ assert swapped is True
+ assert r.hashes["meta"]["status"] == "completed"
+
+
+async def test_hash_cas_no_swap_when_expected_differs() -> None:
+ r = _Fake()
+ r.hashes["meta"] = {"status": "completed"}
+ swapped = await hash_compare_and_set(
+ r, "meta", "status", expected="running", new="failed" # type: ignore[arg-type]
+ )
+ assert swapped is False
+ assert r.hashes["meta"]["status"] == "completed"
diff --git a/autogpt_platform/backend/backend/data/workspace.py b/autogpt_platform/backend/backend/data/workspace.py
index 8697b07b15..43e328813b 100644
--- a/autogpt_platform/backend/backend/data/workspace.py
+++ b/autogpt_platform/backend/backend/data/workspace.py
@@ -5,6 +5,7 @@ This module provides functions for managing user workspaces and workspace files.
"""
import logging
+import re
from datetime import datetime, timezone
from typing import Optional
@@ -15,6 +16,10 @@ from prisma.types import UserWorkspaceFileWhereInput
from backend.util.json import SafeJson
+_UUID_RE = re.compile(
+ r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.I
+)
+
logger = logging.getLogger(__name__)
@@ -336,6 +341,48 @@ async def soft_delete_workspace_file(
return WorkspaceFile.from_db(updated) if updated else None
+async def resolve_workspace_files(
+ user_id: str,
+ file_ids: list[str],
+) -> list[UserWorkspaceFile]:
+ """Return workspace-scoped file records for the given IDs.
+
+ Filters out non-UUID entries, then queries only IDs that belong to the
+ caller's workspace and are not soft-deleted. Safe to call with
+ untrusted input — invalid IDs and cross-user IDs are silently dropped.
+ """
+ valid_ids = [fid for fid in file_ids if _UUID_RE.fullmatch(fid)]
+ if not valid_ids:
+ return []
+ workspace = await get_or_create_workspace(user_id)
+ return await UserWorkspaceFile.prisma().find_many(
+ where={
+ "id": {"in": valid_ids},
+ "workspaceId": workspace.id,
+ "isDeleted": False,
+ }
+ )
+
+
+def build_files_block(files: list[UserWorkspaceFile]) -> str:
+ """Return a formatted ``[Attached files]`` block for injection into a message.
+
+ Returns an empty string when *files* is empty so callers can do a simple
+ ``message += build_files_block(files)`` without an extra ``if`` check.
+ """
+ if not files:
+ return ""
+ lines = [
+ f"- {f.name} ({f.mimeType}, {round(f.sizeBytes / 1024, 1)} KB), file_id={f.id}"
+ for f in files
+ ]
+ return (
+ "\n\n[Attached files]\n"
+ + "\n".join(lines)
+ + "\nUse read_workspace_file with the file_id to access file contents."
+ )
+
+
async def get_workspace_total_size(workspace_id: str) -> int:
"""
Get the total size of all files in a workspace.
diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.py
index 2af3ce784e..87ee3cbc44 100644
--- a/autogpt_platform/backend/backend/executor/manager.py
+++ b/autogpt_platform/backend/backend/executor/manager.py
@@ -38,6 +38,7 @@ from backend.data.execution import (
from backend.data.graph import Link, Node
from backend.data.model import GraphExecutionStats, NodeExecutionStats
from backend.data.rabbitmq import SyncRabbitMQ
+from backend.data.redis_helpers import incr_with_ttl_sync
from backend.executor.cost_tracking import (
drain_pending_cost_logs,
log_system_credential_cost,
@@ -1964,10 +1965,12 @@ def increment_execution_count(user_id: str) -> int:
"""
Increment the execution count for a given user,
this will be used to charge the user for the execution cost.
+
+ Uses :func:`incr_with_ttl_sync` so INCR and EXPIRE run atomically via
+ MULTI/EXEC — previously this was a bare INCR followed by a separate
+ EXPIRE which could orphan the counter (no TTL) if the process died
+ between the two commands.
"""
r = redis.get_redis()
k = f"uec:{user_id}" # User Execution Count global key
- counter = cast(int, r.incr(k))
- if counter == 1:
- r.expire(k, settings.config.execution_counter_expiration_time)
- return counter
+ return incr_with_ttl_sync(r, k, settings.config.execution_counter_expiration_time)
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx
index 88f70c75d8..158d0b2392 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx
@@ -83,6 +83,8 @@ export function CopilotPage() {
isSyncing,
createSession,
onSend,
+ onEnqueue,
+ queuedMessages,
isLoadingSession,
isSessionError,
isCreatingSession,
@@ -213,6 +215,8 @@ export function CopilotPage() {
onCreateSession={createSession}
onSend={onSend}
onStop={stop}
+ onEnqueue={onEnqueue}
+ queuedMessages={queuedMessages}
isUploadingFiles={isUploadingFiles}
hasMoreMessages={hasMoreMessages}
isLoadingMore={isLoadingMore}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPage.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPage.test.ts
index d9519dda0c..093648d407 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPage.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPage.test.ts
@@ -1,10 +1,12 @@
-import { renderHook } from "@testing-library/react";
+import { act, renderHook, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { useCopilotPage } from "../useCopilotPage";
const mockUseChatSession = vi.fn();
const mockUseCopilotStream = vi.fn();
const mockUseLoadMoreMessages = vi.fn();
+const mockQueueFollowUpMessage = vi.fn();
+const mockToast = vi.fn();
vi.mock("../useChatSession", () => ({
useChatSession: (...args: unknown[]) => mockUseChatSession(...args),
@@ -35,13 +37,24 @@ vi.mock("../store", () => ({
vi.mock("../helpers/convertChatSessionToUiMessages", () => ({
concatWithAssistantMerge: (a: unknown[], b: unknown[]) => [...a, ...b],
}));
+vi.mock("../helpers", () => ({
+ deduplicateMessages: (msgs: unknown[]) => msgs,
+}));
vi.mock("@/app/api/__generated__/endpoints/chat/chat", () => ({
useDeleteV2DeleteSession: () => ({ mutate: vi.fn(), isPending: false }),
useGetV2ListSessions: () => ({ data: undefined, isLoading: false }),
getGetV2ListSessionsQueryKey: () => ["sessions"],
+ getV2GetPendingMessages: vi.fn().mockResolvedValue({
+ status: 200,
+ data: { count: 0, messages: [] },
+ }),
+}));
+vi.mock("../helpers/queueFollowUpMessage", () => ({
+ queueFollowUpMessage: (...args: unknown[]) =>
+ mockQueueFollowUpMessage(...args),
}));
vi.mock("@/components/molecules/Toast/use-toast", () => ({
- toast: vi.fn(),
+ toast: (...args: unknown[]) => mockToast(...args),
}));
vi.mock("@/lib/direct-upload", () => ({
uploadFileDirect: vi.fn(),
@@ -129,3 +142,107 @@ describe("useCopilotPage — backward pagination message ordering", () => {
expect(result.current.messages[1]).toEqual(currentMsg);
});
});
+
+describe("useCopilotPage — onSend queue-in-flight path", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("rejects attaching files while a turn is in flight", async () => {
+ mockUseChatSession.mockReturnValue(makeBaseChatSession());
+ mockUseCopilotStream.mockReturnValue(
+ makeBaseCopilotStream({ status: "streaming" }),
+ );
+ mockUseLoadMoreMessages.mockReturnValue(makeBaseLoadMore());
+
+ const { result } = renderHook(() => useCopilotPage());
+
+ const bigFile = new File(["x"], "doc.txt", { type: "text/plain" });
+ await act(async () => {
+ await result.current.onSend("hello", [bigFile]);
+ });
+
+ expect(mockQueueFollowUpMessage).not.toHaveBeenCalled();
+ expect(mockToast).toHaveBeenCalledWith(
+ expect.objectContaining({ title: "Please wait to attach files" }),
+ );
+ });
+
+ it("queues a text-only message via queueFollowUpMessage when in flight", async () => {
+ mockUseChatSession.mockReturnValue(makeBaseChatSession());
+ mockUseCopilotStream.mockReturnValue(
+ makeBaseCopilotStream({ status: "streaming" }),
+ );
+ mockUseLoadMoreMessages.mockReturnValue(makeBaseLoadMore());
+ mockQueueFollowUpMessage.mockResolvedValue({
+ kind: "queued",
+ buffer_length: 1,
+ max_buffer_length: 10,
+ turn_in_flight: true,
+ });
+
+ const { result } = renderHook(() => useCopilotPage());
+
+ await act(async () => {
+ await result.current.onSend("follow-up");
+ });
+
+ expect(mockQueueFollowUpMessage).toHaveBeenCalledWith(
+ "sess-1",
+ "follow-up",
+ );
+ // appendChip should have been called, bringing the chip into queuedMessages.
+ await waitFor(() => {
+ expect(result.current.queuedMessages).toContain("follow-up");
+ });
+ });
+
+ it("does not append a chip or toast when the server raced and started a new turn", async () => {
+ mockUseChatSession.mockReturnValue(makeBaseChatSession());
+ mockUseCopilotStream.mockReturnValue(
+ makeBaseCopilotStream({ status: "streaming" }),
+ );
+ mockUseLoadMoreMessages.mockReturnValue(makeBaseLoadMore());
+ mockQueueFollowUpMessage.mockResolvedValue({
+ kind: "raced_started_turn",
+ status: 200,
+ });
+
+ const { result } = renderHook(() => useCopilotPage());
+
+ await act(async () => {
+ await result.current.onSend("follow-up");
+ });
+
+ expect(mockQueueFollowUpMessage).toHaveBeenCalledWith(
+ "sess-1",
+ "follow-up",
+ );
+ // No chip should appear — the server already started a new turn,
+ // useHydrateOnStreamEnd will surface the response.
+ expect(result.current.queuedMessages).not.toContain("follow-up");
+ // No misleading error toast either.
+ expect(mockToast).not.toHaveBeenCalledWith(
+ expect.objectContaining({ title: "Could not queue message" }),
+ );
+ });
+
+ it("surfaces a toast and rethrows when the queue POST fails", async () => {
+ mockUseChatSession.mockReturnValue(makeBaseChatSession());
+ mockUseCopilotStream.mockReturnValue(
+ makeBaseCopilotStream({ status: "streaming" }),
+ );
+ mockUseLoadMoreMessages.mockReturnValue(makeBaseLoadMore());
+ mockQueueFollowUpMessage.mockRejectedValue(new Error("boom"));
+
+ const { result } = renderHook(() => useCopilotPage());
+
+ await act(async () => {
+ await expect(result.current.onSend("follow-up")).rejects.toThrow(/boom/);
+ });
+
+ expect(mockToast).toHaveBeenCalledWith(
+ expect.objectContaining({ title: "Could not queue message" }),
+ );
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPendingChips.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPendingChips.test.ts
new file mode 100644
index 0000000000..b4120f878f
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPendingChips.test.ts
@@ -0,0 +1,351 @@
+import { act, renderHook, waitFor } from "@testing-library/react";
+import type { UIMessage } from "ai";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+// ── Mock the generated API endpoint so we can control peek responses. ──
+const peekMock = vi.fn();
+vi.mock("@/app/api/__generated__/endpoints/chat/chat", () => ({
+ getV2GetPendingMessages: (...args: unknown[]) => peekMock(...args),
+}));
+
+// Import AFTER mocks are declared.
+import { useCopilotPendingChips } from "../useCopilotPendingChips";
+
+function user(id: string, text?: string): UIMessage {
+ return {
+ id,
+ role: "user",
+ parts: [
+ {
+ type: "text" as const,
+ text: text ?? `user-${id}`,
+ state: "done" as const,
+ },
+ ],
+ };
+}
+
+function assistant(id: string, text?: string): UIMessage {
+ return {
+ id,
+ role: "assistant",
+ parts: [
+ {
+ type: "text" as const,
+ text: text ?? `assistant-${id}`,
+ state: "done" as const,
+ },
+ ],
+ };
+}
+
+describe("useCopilotPendingChips", () => {
+ beforeEach(() => {
+ peekMock.mockReset();
+ peekMock.mockResolvedValue({
+ status: 200,
+ data: { count: 0, messages: [] },
+ });
+ });
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ it("restores chips from backend peek on session load", async () => {
+ peekMock.mockResolvedValueOnce({
+ status: 200,
+ data: { count: 2, messages: ["first", "second"] },
+ });
+
+ const setMessages = vi.fn();
+ const { result } = renderHook(() =>
+ useCopilotPendingChips({
+ sessionId: "sess-1",
+ status: "ready",
+ messages: [],
+ setMessages,
+ }),
+ );
+
+ await waitFor(() => {
+ expect(result.current.queuedMessages).toEqual(["first", "second"]);
+ });
+ });
+
+ it("clears chips when the session changes", async () => {
+ // Initial session returns 2 chips.
+ peekMock.mockResolvedValueOnce({
+ status: 200,
+ data: { count: 2, messages: ["a", "b"] },
+ });
+ const setMessages = vi.fn();
+ const { result, rerender } = renderHook(
+ ({ sessionId }: { sessionId: string | null }) =>
+ useCopilotPendingChips({
+ sessionId,
+ status: "ready",
+ messages: [],
+ setMessages,
+ }),
+ { initialProps: { sessionId: "sess-1" } },
+ );
+
+ await waitFor(() => {
+ expect(result.current.queuedMessages).toHaveLength(2);
+ });
+
+ // Next session's peek returns no chips.
+ peekMock.mockResolvedValueOnce({
+ status: 200,
+ data: { count: 0, messages: [] },
+ });
+ rerender({ sessionId: "sess-2" });
+
+ await waitFor(() => {
+ expect(result.current.queuedMessages).toEqual([]);
+ });
+ });
+
+ it("appendChip adds a chip locally without hitting backend", () => {
+ const setMessages = vi.fn();
+ const { result } = renderHook(() =>
+ useCopilotPendingChips({
+ sessionId: "s",
+ status: "ready",
+ messages: [],
+ setMessages,
+ }),
+ );
+
+ act(() => {
+ result.current.appendChip("hello");
+ });
+ expect(result.current.queuedMessages).toEqual(["hello"]);
+ });
+
+ it("promotes chips to a bubble when a second new assistant appears (auto-continue)", () => {
+ const setMessages = vi.fn();
+
+ // Initial: only a user msg (no assistants yet). The hook's internal
+ // "seen" set starts empty so the FIRST new assistant in a streaming
+ // chain is treated as Turn 1's opener.
+ const { result, rerender } = renderHook(
+ ({ messages }: { messages: UIMessage[] }) =>
+ useCopilotPendingChips({
+ sessionId: "s",
+ status: "streaming",
+ messages,
+ setMessages,
+ }),
+ { initialProps: { messages: [user("u1")] } },
+ );
+
+ // First assistant appears → opener; no promotion (correctly).
+ rerender({
+ messages: [user("u1"), assistant("a1", "turn1")],
+ });
+
+ // User queues a chip while turn 1 is still streaming.
+ act(() => {
+ result.current.appendChip("followup");
+ });
+
+ // Clear any prior setMessages calls so we only inspect the promotion.
+ setMessages.mockClear();
+
+ // A SECOND new assistant id appears → auto-continue detected.
+ rerender({
+ messages: [
+ user("u1"),
+ assistant("a1", "turn1"),
+ assistant("a2", "turn2"),
+ ],
+ });
+
+ // setMessages was called with an updater that inserts the promoted bubble.
+ const promoteCall = setMessages.mock.calls.find(
+ ([fn]) => typeof fn === "function",
+ );
+ expect(promoteCall).toBeDefined();
+
+ const updater = promoteCall![0] as (prev: UIMessage[]) => UIMessage[];
+ const after = updater([user("u1"), assistant("a1"), assistant("a2")]);
+
+ const a2Idx = after.findIndex((m) => m.id === "a2");
+ const promotedIdx = after.findIndex((m) =>
+ m.id.startsWith("promoted-auto-continue-"),
+ );
+ expect(promotedIdx).toBeGreaterThanOrEqual(0);
+ expect(promotedIdx).toBeLessThan(a2Idx);
+ expect(after[promotedIdx].role).toBe("user");
+ expect((after[promotedIdx].parts![0] as { text?: string }).text).toBe(
+ "followup",
+ );
+
+ // And chips have been cleared.
+ expect(result.current.queuedMessages).toEqual([]);
+ });
+
+ it("turn-start drain: peek count=0 clears chips once submitted→streaming", async () => {
+ const setMessages = vi.fn();
+ // Seed with chips via the idle-state peek so local state has them by
+ // the time we kick the submitted→streaming transition.
+ peekMock.mockResolvedValueOnce({
+ status: 200,
+ data: { count: 1, messages: ["local-chip"] },
+ });
+
+ type StatusProps = {
+ status: "ready" | "submitted" | "streaming";
+ };
+ const { result, rerender } = renderHook<
+ ReturnType<typeof useCopilotPendingChips>,
+ StatusProps
+ >(
+ ({ status }) =>
+ useCopilotPendingChips({
+ sessionId: "s",
+ status,
+ messages: [],
+ setMessages,
+ }),
+ { initialProps: { status: "ready" } },
+ );
+
+ await waitFor(() => {
+ expect(result.current.queuedMessages).toEqual(["local-chip"]);
+ });
+
+ rerender({ status: "submitted" });
+ // Backend really drained — count is 0.
+ peekMock.mockResolvedValue({
+ status: 200,
+ data: { count: 0, messages: [] },
+ });
+ rerender({ status: "streaming" });
+
+ await waitFor(() => {
+ expect(result.current.queuedMessages).toEqual([]);
+ });
+ });
+
+ it("turn-start drain: non-200 peek response does not clear chips", async () => {
+ const setMessages = vi.fn();
+ // Seed chips from the session-load peek so we don't race with a
+ // separate appendChip after idle-peek has already overwritten state.
+ peekMock.mockResolvedValueOnce({
+ status: 200,
+ data: { count: 1, messages: ["keep-me"] },
+ });
+
+ type StatusProps = {
+ status: "ready" | "submitted" | "streaming";
+ };
+ const { result, rerender } = renderHook<
+ ReturnType<typeof useCopilotPendingChips>,
+ StatusProps
+ >(
+ ({ status }) =>
+ useCopilotPendingChips({
+ sessionId: "s",
+ status,
+ messages: [],
+ setMessages,
+ }),
+ { initialProps: { status: "ready" } },
+ );
+
+ await waitFor(() => {
+ expect(result.current.queuedMessages).toEqual(["keep-me"]);
+ });
+
+ rerender({ status: "submitted" });
+ // Turn-start peek returns an error status — the condition `count === 0`
+ // only fires on `status === 200`, so chips must stay.
+ peekMock.mockResolvedValue({ status: 500, data: undefined });
+ rerender({ status: "streaming" });
+
+ await act(async () => {
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+
+ expect(result.current.queuedMessages).toEqual(["keep-me"]);
+ });
+
+ it("mid-turn poll: peek error is swallowed and chips are preserved", async () => {
+ vi.useFakeTimers();
+ const setMessages = vi.fn();
+ const { result } = renderHook(() =>
+ useCopilotPendingChips({
+ sessionId: "s",
+ status: "streaming",
+ messages: [user("u1"), assistant("a1")],
+ setMessages,
+ }),
+ );
+
+ act(() => {
+ result.current.appendChip("survives");
+ });
+
+ // Simulate a transient network failure on the peek.
+ peekMock.mockRejectedValue(new Error("network blip"));
+
+ await act(async () => {
+ vi.advanceTimersByTime(2_000);
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+
+ // No promotion happened, chips remain intact.
+ expect(result.current.queuedMessages).toEqual(["survives"]);
+ const promoteCall = setMessages.mock.calls.find(([arg]) => {
+ if (typeof arg !== "function") return false;
+ const updated = (arg as (p: UIMessage[]) => UIMessage[])([]);
+ return updated.some((m) => m.id.startsWith("promoted-"));
+ });
+ expect(promoteCall).toBeUndefined();
+ });
+
+ it("mid-turn poll promotes drained chips when backend count drops", async () => {
+ vi.useFakeTimers();
+ const setMessages = vi.fn();
+ const { result } = renderHook(() =>
+ useCopilotPendingChips({
+ sessionId: "s",
+ status: "streaming",
+ messages: [user("u1"), assistant("a1")],
+ setMessages,
+ }),
+ );
+
+ act(() => {
+ result.current.appendChip("chipA");
+ result.current.appendChip("chipB");
+ });
+
+ // Backend now reports count=0 (drained by MCP wrapper).
+ peekMock.mockResolvedValue({
+ status: 200,
+ data: { count: 0, messages: [] },
+ });
+
+ await act(async () => {
+ vi.advanceTimersByTime(2_000);
+ // Let the awaited promise resolve.
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+
+ // The poll should have promoted the two chips to a bubble.
+ const promotedCall = setMessages.mock.calls.find(([arg]) => {
+ if (typeof arg !== "function") return false;
+ const updated = (arg as (p: UIMessage[]) => UIMessage[])([]);
+ return updated.some((m) => m.id.startsWith("promoted-midturn-"));
+ });
+ expect(promotedCall).toBeDefined();
+ // And remaining chips cleared.
+ expect(result.current.queuedMessages).toEqual([]);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts
new file mode 100644
index 0000000000..19a52735d7
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts
@@ -0,0 +1,132 @@
+import { renderHook } from "@testing-library/react";
+import type { UIMessage } from "ai";
+import { describe, expect, it, vi } from "vitest";
+
+import { useHydrateOnStreamEnd } from "../useHydrateOnStreamEnd";
+
+/** Use distinct default text per id so the assistant dedup doesn't collapse them. */
+function msg(id: string, text?: string): UIMessage {
+ return {
+ id,
+ role: "assistant",
+ parts: [
+ {
+ type: "text" as const,
+ text: text ?? `text-${id}`,
+ state: "done" as const,
+ },
+ ],
+ };
+}
+
+describe("useHydrateOnStreamEnd", () => {
+ it("does nothing while streaming", () => {
+ const setMessages = vi.fn();
+ renderHook(() =>
+ useHydrateOnStreamEnd({
+ status: "streaming",
+ hydratedMessages: [msg("a")],
+ isReconnectScheduled: false,
+ setMessages,
+ }),
+ );
+ expect(setMessages).not.toHaveBeenCalled();
+ });
+
+ it("does nothing while a reconnect is scheduled", () => {
+ const setMessages = vi.fn();
+ renderHook(() =>
+ useHydrateOnStreamEnd({
+ status: "ready",
+ hydratedMessages: [msg("a")],
+ isReconnectScheduled: true,
+ setMessages,
+ }),
+ );
+ expect(setMessages).not.toHaveBeenCalled();
+ });
+
+ it("length-gated top-up when there is no pending force-hydrate", () => {
+ // Fresh mount at "ready" → force-hydrate was never armed.
+ // The hook should top up only if the fresh data is strictly larger.
+ const setMessages = vi.fn();
+ const fresh = [msg("a"), msg("b"), msg("c")];
+ renderHook(() =>
+ useHydrateOnStreamEnd({
+ status: "ready",
+ hydratedMessages: fresh,
+ isReconnectScheduled: false,
+ setMessages,
+ }),
+ );
+ expect(setMessages).toHaveBeenCalledTimes(1);
+ // setMessages was called with an updater — invoke with a short prev.
+ const updater = setMessages.mock.calls[0][0] as (
+ prev: UIMessage[],
+ ) => UIMessage[];
+ expect(updater([msg("x")])).toHaveLength(3);
+ // And with a longer prev, no-op.
+ expect(updater([msg("x"), msg("y"), msg("z"), msg("w")])).toHaveLength(4);
+ });
+
+ it("waits for fresh reference after streaming→ready, then force-hydrates", () => {
+ const setMessages = vi.fn();
+ const staleSnapshot = [msg("s1")];
+
+ const { rerender } = renderHook(
+ ({
+ status,
+ hydratedMessages,
+ }: {
+ status: "streaming" | "ready";
+ hydratedMessages: UIMessage[];
+ }) =>
+ useHydrateOnStreamEnd({
+ status,
+ hydratedMessages,
+ isReconnectScheduled: false,
+ setMessages,
+ }),
+ {
+ initialProps: {
+ status: "streaming",
+ hydratedMessages: staleSnapshot,
+ },
+ },
+ );
+
+ // While streaming, no hydration activity.
+ expect(setMessages).not.toHaveBeenCalled();
+
+ // Flip to ready with the SAME hydrated reference (stale) — the force-
+ // hydrate flag is armed, but we must not overwrite yet.
+ rerender({ status: "ready", hydratedMessages: staleSnapshot });
+ expect(setMessages).not.toHaveBeenCalled();
+
+ // New reference arrives → force-hydrate runs once.
+ const fresh = [msg("s1"), msg("s2")];
+ rerender({ status: "ready", hydratedMessages: fresh });
+ expect(setMessages).toHaveBeenCalledTimes(1);
+ const arg = setMessages.mock.calls[0][0];
+ // Force-hydrate replaces unconditionally (not an updater fn).
+ expect(Array.isArray(arg)).toBe(true);
+ expect((arg as UIMessage[]).map((m) => m.id)).toEqual(["s1", "s2"]);
+
+ // Subsequent rerender with same fresh ref → no additional call.
+ rerender({ status: "ready", hydratedMessages: fresh });
+ expect(setMessages).toHaveBeenCalledTimes(1);
+ });
+
+ it("ignores undefined or empty hydratedMessages", () => {
+ const setMessages = vi.fn();
+ renderHook(() =>
+ useHydrateOnStreamEnd({
+ status: "ready",
+ hydratedMessages: undefined,
+ isReconnectScheduled: false,
+ setMessages,
+ }),
+ );
+ expect(setMessages).not.toHaveBeenCalled();
+ });
+});
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 7f3c1d0328..dc01eba286 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
@@ -26,6 +26,10 @@ export interface ChatContainerProps {
onCreateSession: () => void | Promise<string>;
onSend: (message: string, files?: File[]) => void | Promise<void>;
onStop: () => void;
+ /** Called to enqueue a message while streaming (bypasses normal send flow). */
+ onEnqueue?: (message: string) => void | Promise<void>;
+ /** Pending queued messages waiting to be injected, shown at the end of chat. */
+ queuedMessages?: string[];
isUploadingFiles?: boolean;
hasMoreMessages?: boolean;
isLoadingMore?: boolean;
@@ -50,6 +54,8 @@ export const ChatContainer = ({
onCreateSession,
onSend,
onStop,
+ onEnqueue,
+ queuedMessages,
isUploadingFiles,
hasMoreMessages,
isLoadingMore,
@@ -65,13 +71,14 @@ export const ChatContainer = ({
// state would otherwise shrink the chat column with no panel rendered.
const isArtifactOpen = isArtifactsEnabled && isArtifactPanelOpen;
useAutoOpenArtifacts({ sessionId });
- const isBusy =
- status === "streaming" ||
- status === "submitted" ||
- !!isReconnecting ||
- !!isSyncing ||
- isLoadingSession ||
- !!isSessionError;
+ // isStreaming controls the stop-button UI and routes submits to the queue
+ // endpoint — the input itself must NOT be disabled during streaming so users
+ // can type and queue their next message.
+ const isStreaming = status === "streaming" || status === "submitted";
+ // The input is only truly disabled when the session isn't ready at all
+ // (reconnecting, syncing, loading, or errored) — NOT during normal streaming.
+ const isInputDisabled =
+ !!isReconnecting || !!isSyncing || isLoadingSession || !!isSessionError;
const inputLayoutId = "copilot-2-chat-input";
// Retry: re-send the last user message (used by ErrorCard on transient errors)
@@ -110,6 +117,7 @@ export const ChatContainer = ({
onLoadMore={onLoadMore}
onRetry={handleRetry}
historicalDurations={historicalDurations}
+ queuedMessages={queuedMessages}
/>
<motion.div
initial={{ opacity: 0 }}
@@ -121,10 +129,11 @@ export const ChatContainer = ({
<ChatInput
inputId="chat-input-session"
onSend={onSend}
- disabled={isBusy}
- isStreaming={isBusy}
+ disabled={isInputDisabled}
+ isStreaming={isStreaming}
isUploadingFiles={isUploadingFiles}
onStop={onStop}
+ onEnqueue={onEnqueue}
placeholder="What else can I help with?"
droppedFiles={droppedFiles}
onDroppedFilesConsumed={onDroppedFilesConsumed}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
index ae24800142..95066cfc9d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
@@ -1,5 +1,6 @@
import {
PromptInputBody,
+ PromptInputButton,
PromptInputFooter,
PromptInputSubmit,
PromptInputTextarea,
@@ -9,6 +10,7 @@ import { toast } from "@/components/molecules/Toast/use-toast";
import { InputGroup } from "@/components/ui/input-group";
import { cn } from "@/lib/utils";
import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
+import { Tray } from "@phosphor-icons/react";
import { ChangeEvent, useEffect, useState } from "react";
import { AttachmentMenu } from "./components/AttachmentMenu";
import { DryRunToggleButton } from "./components/DryRunToggleButton";
@@ -27,6 +29,8 @@ interface Props {
isStreaming?: boolean;
isUploadingFiles?: boolean;
onStop?: () => void;
+ /** Called to enqueue a message when copilot is streaming and user has typed text. */
+ onEnqueue?: (message: string) => void | Promise<void>;
placeholder?: string;
className?: string;
inputId?: string;
@@ -44,6 +48,7 @@ export function ChatInput({
isStreaming = false,
isUploadingFiles = false,
onStop,
+ onEnqueue,
placeholder = "Type your message...",
className,
inputId = "chat-input",
@@ -114,7 +119,12 @@ export function ChatInput({
}, [droppedFiles, onDroppedFilesConsumed]);
const hasFiles = files.length > 0;
+ // isBusy disables non-essential interactions (attachment menu, voice recording)
+ // but must not disable the textarea itself — streaming allows queued messages.
const isBusy = disabled || isStreaming || isUploadingFiles;
+ // The textarea is only truly disabled when the session is unavailable, not
+ // during normal streaming (users can type and queue the next message).
+ const isTextareaDisabled = disabled || isUploadingFiles;
const {
value,
@@ -127,11 +137,13 @@ export function ChatInput({
// Only clear files after successful send (onSend throws on failure)
setFiles([]);
},
- disabled: isBusy,
+ disabled: isTextareaDisabled,
canSendEmpty: hasFiles,
inputId,
});
+ const [isEnqueueing, setIsEnqueueing] = useState(false);
+
const {
isRecording,
isTranscribing,
@@ -143,10 +155,10 @@ export function ChatInput({
audioStream,
} = useVoiceRecording({
setValue,
- disabled: isBusy,
- isStreaming,
+ disabled: isTextareaDisabled,
value,
inputId,
+ isStreaming,
});
function handleChange(e: ChangeEvent<HTMLTextAreaElement>) {
@@ -255,6 +267,29 @@ export function ChatInput({
onClick={toggleRecording}
/>
)}
+ {isStreaming && canSend && onEnqueue && (
+ <PromptInputButton
+ aria-label="Queue message"
+ tooltip="Queue message"
+ disabled={isEnqueueing}
+ onClick={async () => {
+ if (isEnqueueing) return;
+ const trimmed = value.trim();
+ if (trimmed) {
+ setIsEnqueueing(true);
+ try {
+ await onEnqueue(trimmed);
+ setValue("");
+ } finally {
+ setIsEnqueueing(false);
+ }
+ }
+ }}
+ className="size-[2.625rem] rounded-full border-zinc-800 bg-zinc-800 text-white hover:border-zinc-900 hover:bg-zinc-900"
+ >
+ <Tray className="size-4" weight="bold" />
+ </PromptInputButton>
+ )}
{isStreaming ? (
<PromptInputSubmit status="streaming" onStop={onStop} />
) : (
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx
index 5bac773deb..506df99c22 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx
@@ -3,6 +3,8 @@ import {
screen,
fireEvent,
cleanup,
+ act,
+ waitFor,
} from "@/tests/integrations/test-utils";
import { afterEach, describe, expect, it, vi } from "vitest";
import { ChatInput } from "../ChatInput";
@@ -19,6 +21,8 @@ const mockSetCopilotLlmModel = vi.fn((model: string) => {
vi.mock("@/app/(platform)/copilot/store", () => ({
useCopilotUIStore: () => ({
+ copilotMode: mockCopilotMode,
+ setCopilotMode: mockSetCopilotChatMode,
copilotChatMode: mockCopilotMode,
setCopilotChatMode: mockSetCopilotChatMode,
copilotLlmModel: mockCopilotLlmModel,
@@ -85,6 +89,19 @@ vi.mock("@/components/ai-elements/prompt-input", () => ({
PromptInputTools: ({ children }: { children: React.ReactNode }) => (
<div data-testid="tools">{children}</div>
),
+ PromptInputButton: ({
+ children,
+ onClick,
+ "aria-label": ariaLabel,
+ }: {
+ children?: React.ReactNode;
+ onClick?: React.MouseEventHandler<HTMLButtonElement>;
+ "aria-label"?: string;
+ }) => (
+ <button aria-label={ariaLabel} onClick={onClick} data-testid="queue-btn">
+ {children}
+ </button>
+ ),
}));
vi.mock("@/components/ui/input-group", () => ({
@@ -109,6 +126,20 @@ vi.mock("../components/RecordingButton", () => ({
vi.mock("../components/RecordingIndicator", () => ({
RecordingIndicator: () => null,
}));
+vi.mock("../components/DryRunToggleButton", () => ({
+ DryRunToggleButton: ({
+ onToggle,
+ }: {
+ isDryRun: boolean;
+ isStreaming: boolean;
+ readOnly: boolean;
+ onToggle: () => void;
+ }) => (
+ <button data-testid="dry-run-toggle" onClick={onToggle}>
+ Dry Run
+ </button>
+ ),
+}));
const mockOnSend = vi.fn();
@@ -212,6 +243,99 @@ describe("ChatInput mode toggle", () => {
});
});
+describe("ChatInput queue button", () => {
+ it("does not render queue button when not streaming", () => {
+ render(<ChatInput onSend={mockOnSend} onEnqueue={vi.fn()} />);
+ expect(screen.queryByLabelText(/queue message/i)).toBeNull();
+ });
+
+ it("does not render queue button when streaming but no text typed", () => {
+ render(<ChatInput onSend={mockOnSend} onEnqueue={vi.fn()} isStreaming />);
+ expect(screen.queryByLabelText(/queue message/i)).toBeNull();
+ });
+
+ it("renders queue button when streaming with text and onEnqueue provided", () => {
+ render(<ChatInput onSend={mockOnSend} onEnqueue={vi.fn()} isStreaming />);
+ const textarea = screen.getByTestId("textarea");
+ fireEvent.change(textarea, { target: { value: "follow-up question" } });
+ expect(screen.getByLabelText(/queue message/i)).toBeDefined();
+ });
+
+ it("calls onEnqueue with trimmed text when queue button clicked", async () => {
+ const mockOnEnqueue = vi.fn().mockResolvedValue(undefined);
+ render(
+ <ChatInput onSend={mockOnSend} onEnqueue={mockOnEnqueue} isStreaming />,
+ );
+ const textarea = screen.getByTestId("textarea");
+ fireEvent.change(textarea, { target: { value: " hello " } });
+ await act(async () => {
+ fireEvent.click(screen.getByLabelText(/queue message/i));
+ });
+ expect(mockOnEnqueue).toHaveBeenCalledWith("hello");
+ });
+
+ it("clears textarea after successful enqueue", async () => {
+ const mockOnEnqueue = vi.fn().mockResolvedValue(undefined);
+ render(
+ <ChatInput onSend={mockOnSend} onEnqueue={mockOnEnqueue} isStreaming />,
+ );
+ const textarea = screen.getByTestId("textarea");
+ fireEvent.change(textarea, { target: { value: "my message" } });
+ fireEvent.click(screen.getByLabelText(/queue message/i));
+ await waitFor(() => {
+ expect((textarea as HTMLTextAreaElement).value).toBe("");
+ });
+ });
+
+ it("preserves textarea text if queue button clicked with empty input", async () => {
+ const mockOnEnqueue = vi.fn().mockResolvedValue(undefined);
+ render(
+ <ChatInput onSend={mockOnSend} onEnqueue={mockOnEnqueue} isStreaming />,
+ );
+ const textarea = screen.getByTestId("textarea");
+ // No text typed — queue button should not render
+ expect(screen.queryByLabelText(/queue message/i)).toBeNull();
+ // onEnqueue must not be called
+ expect(mockOnEnqueue).not.toHaveBeenCalled();
+ // textarea stays empty
+ expect((textarea as HTMLTextAreaElement).value).toBe("");
+ });
+});
+
+describe("ChatInput dry-run toggle", () => {
+ it("does not render dry-run toggle when flag is disabled", () => {
+ mockFlagValue = false;
+ render(<ChatInput onSend={mockOnSend} />);
+ expect(screen.queryByTestId("dry-run-toggle")).toBeNull();
+ });
+
+ it("renders dry-run toggle when flag is enabled and no session", () => {
+ mockFlagValue = true;
+ render(<ChatInput onSend={mockOnSend} hasSession={false} />);
+ expect(screen.getByTestId("dry-run-toggle")).toBeDefined();
+ });
+
+ it("hides dry-run toggle when session is active and isDryRun is false", () => {
+ mockFlagValue = true;
+ render(<ChatInput onSend={mockOnSend} hasSession />);
+ // isDryRun is false in mock, hasSession is true → toggle hidden
+ expect(screen.queryByTestId("dry-run-toggle")).toBeNull();
+ });
+
+ it("calls setIsDryRun and shows toast when dry-run toggled", async () => {
+ const { toast } = await import("@/components/molecules/Toast/use-toast");
+ mockFlagValue = true;
+ render(<ChatInput onSend={mockOnSend} />);
+ fireEvent.click(screen.getByTestId("dry-run-toggle"));
+ // isDryRun was false → next is true
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ title: "Test mode enabled",
+ }),
+ );
+ });
+});
+
describe("ChatInput model toggle", () => {
it("renders model toggle button when flag is enabled", () => {
mockFlagValue = true;
@@ -265,14 +389,13 @@ describe("ChatInput model toggle", () => {
// flag is immutable and shown via the CopilotPage banner, not this button.
mockFlagValue = true;
render(<ChatInput onSend={mockOnSend} hasSession />);
- expect(screen.queryByLabelText(/test mode/i)).toBeNull();
- expect(screen.queryByLabelText(/enable test mode/i)).toBeNull();
+ expect(screen.queryByTestId("dry-run-toggle")).toBeNull();
});
it("shows dry-run toggle when no session", () => {
mockFlagValue = true;
render(<ChatInput onSend={mockOnSend} />);
- expect(screen.getByLabelText(/test mode|enable test mode/i)).toBeTruthy();
+ expect(screen.getByTestId("dry-run-toggle")).toBeTruthy();
});
it("shows a toast when switching to advanced", async () => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useVoiceRecording.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useVoiceRecording.ts
index 209c252236..655ec6df1d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useVoiceRecording.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useVoiceRecording.ts
@@ -12,17 +12,17 @@ const MAX_RECORDING_DURATION = 2 * 60 * 1000; // 2 minutes in ms
interface Args {
setValue: React.Dispatch<React.SetStateAction<string>>;
disabled?: boolean;
- isStreaming?: boolean;
value: string;
inputId?: string;
+ isStreaming?: boolean;
}
export function useVoiceRecording({
setValue,
disabled = false,
- isStreaming = false,
value,
inputId,
+ isStreaming = false,
}: Args) {
const [isRecording, setIsRecording] = useState(false);
const [isTranscribing, setIsTranscribing] = useState(false);
@@ -223,8 +223,9 @@ export function useVoiceRecording({
event.preventDefault();
stopRecording();
return;
- } else if (!value.trim()) {
- // Start recording on space when input is empty
+ } else if (!value.trim() && !isStreaming) {
+ // Start recording on space when input is empty and not streaming
+ // (mirrors the visual disabled state of the mic button during streaming)
event.preventDefault();
void startRecording();
return;
@@ -237,13 +238,15 @@ export function useVoiceRecording({
}
// Let PromptInputTextarea handle remaining keys (Enter → submit, etc.)
},
- [value, isTranscribing, stopRecording, startRecording],
+ [value, isTranscribing, isStreaming, stopRecording, startRecording],
);
const showMicButton = isSupported;
// Don't include isRecording in disabled state - we need key events to work
- // Text input is blocked via handleKeyDown instead
- const isInputDisabled = disabled || isStreaming || isTranscribing;
+ // Text input is blocked via handleKeyDown instead.
+ // isStreaming is intentionally excluded: users can type and queue messages
+ // while a stream is in-flight; the stop button handles the streaming state.
+ const isInputDisabled = disabled || isTranscribing;
// Cleanup on unmount
useEffect(() => {
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 d12f97106b..357f1dfc1f 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
@@ -11,6 +11,7 @@ import {
} from "@/components/ai-elements/message";
import { Button } from "@/components/atoms/Button/Button";
import { LoadingSpinner } from "@/components/atoms/LoadingSpinner/LoadingSpinner";
+import { Clock } from "@phosphor-icons/react";
import { FileUIPart, UIDataTypes, UIMessage, UITools } from "ai";
import { useEffect, useLayoutEffect, useRef } from "react";
import { useStickToBottomContext } from "use-stick-to-bottom";
@@ -45,6 +46,8 @@ interface Props {
onLoadMore?: () => void;
onRetry?: () => void;
historicalDurations?: Map<string, number>;
+ /** Pending queued messages waiting to be injected, shown at the end of chat. */
+ queuedMessages?: string[];
}
function renderSegments(
@@ -254,6 +257,7 @@ export function ChatMessagesContainer({
onLoadMore,
onRetry,
historicalDurations,
+ queuedMessages,
}: Props) {
// Hide the container for one frame when messages first load so
// StickToBottom can scroll to the bottom before the user sees it.
@@ -478,6 +482,17 @@ export function ChatMessagesContainer({
</Message>
)}
{graphExecId && <CopilotPendingReviews graphExecId={graphExecId} />}
+ {queuedMessages?.map((msg, idx) => (
+ <Message key={idx} from="user">
+ <MessageContent className="flex flex-col gap-1 rounded-xl border border-dashed border-purple-400 bg-purple-100 px-3 py-2.5 text-[1rem] leading-relaxed text-slate-900 opacity-60 [border-bottom-right-radius:0]">
+ <span>{msg}</span>
+ <span className="flex items-center gap-1 text-xs text-slate-500">
+ <Clock className="size-3" weight="bold" />
+ Queued
+ </span>
+ </MessageContent>
+ </Message>
+ ))}
{error && (
<details className="rounded-lg bg-red-50 p-4 text-sm text-red-700">
<summary className="cursor-pointer font-medium">
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 333896ec07..2162e49fbf 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
@@ -21,29 +21,42 @@ vi.mock("use-stick-to-bottom", () => ({
vi.mock("@/components/ai-elements/conversation", () => ({
Conversation: ({ children }: { children: React.ReactNode }) => (
- <div>{children}</div>
+ <div data-testid="conversation">{children}</div>
),
ConversationContent: ({ children }: { children: React.ReactNode }) => (
- <div>{children}</div>
+ <div data-testid="conversation-content">{children}</div>
),
ConversationScrollButton: () => null,
}));
vi.mock("@/components/ai-elements/message", () => ({
- Message: ({ children }: { children: React.ReactNode }) => (
- <div>{children}</div>
- ),
- MessageContent: ({ children }: { children: React.ReactNode }) => (
- <div>{children}</div>
+ Message: ({
+ children,
+ from,
+ }: {
+ children: React.ReactNode;
+ from?: string;
+ }) => (
+ <div data-testid={`message-${from ?? "unknown"}`} data-from={from}>
+ {children}
+ </div>
),
MessageActions: ({ children }: { children: React.ReactNode }) => (
<div>{children}</div>
),
+ MessageContent: ({
+ children,
+ className,
+ }: {
+ children: React.ReactNode;
+ className?: string;
+ }) => <div className={className}>{children}</div>,
}));
vi.mock("../components/AssistantMessageActions", () => ({
AssistantMessageActions: () => null,
}));
+
vi.mock("../components/CopyButton", () => ({ CopyButton: () => null }));
vi.mock("../components/CollapsedToolGroup", () => ({
CollapsedToolGroup: () => null,
@@ -79,6 +92,18 @@ vi.mock("../helpers", () => ({
}),
}));
+vi.mock("@/components/atoms/LoadingSpinner/LoadingSpinner", () => ({
+ LoadingSpinner: () => <div data-testid="loading-spinner" />,
+}));
+
+vi.mock("@phosphor-icons/react", () => ({
+ Clock: () => <span data-testid="clock-icon" />,
+ ArrowDown: () => null,
+ ArrowUp: () => null,
+}));
+
+// ── helpers ───────────────────────────────────────────────────────────────
+
type ObserverCallback = (entries: { isIntersecting: boolean }[]) => void;
class MockIntersectionObserver {
static lastCallback: ObserverCallback | null = null;
@@ -98,18 +123,112 @@ class MockIntersectionObserver {
thresholds = [];
}
-const BASE_PROPS = {
- messages: [],
+const baseProps = {
+ messages: [] as any[],
status: "ready" as const,
error: undefined,
isLoading: false,
- sessionID: "sess-1",
+ sessionID: "sess-123",
+ queuedMessages: [] as string[],
hasMoreMessages: true,
isLoadingMore: false,
onLoadMore: vi.fn(),
onRetry: vi.fn(),
};
+// ── queued-messages rendering ─────────────────────────────────────────────
+
+describe("ChatMessagesContainer — queuedMessages", () => {
+ beforeEach(() => {
+ mockScrollEl.scrollHeight = 100;
+ mockScrollEl.scrollTop = 0;
+ mockScrollEl.clientHeight = 500;
+ MockIntersectionObserver.lastCallback = null;
+ vi.stubGlobal("IntersectionObserver", MockIntersectionObserver);
+ });
+
+ afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+ });
+
+ it("renders nothing extra when queuedMessages is empty", () => {
+ render(<ChatMessagesContainer {...baseProps} queuedMessages={[]} />);
+ expect(screen.queryByText("Queued")).toBeNull();
+ });
+
+ it("renders a single queued message with Queued label", () => {
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ queuedMessages={["What about section 3?"]}
+ />,
+ );
+ expect(screen.getByText("What about section 3?")).toBeDefined();
+ expect(screen.getByText("Queued")).toBeDefined();
+ });
+
+ it("renders multiple queued messages as separate bubbles", () => {
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ queuedMessages={["First follow-up", "Second follow-up"]}
+ />,
+ );
+ expect(screen.getByText("First follow-up")).toBeDefined();
+ expect(screen.getByText("Second follow-up")).toBeDefined();
+ const queuedLabels = screen.getAllByText("Queued");
+ expect(queuedLabels.length).toBe(2);
+ });
+
+ it("renders queued messages even when status is streaming", () => {
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ status="streaming"
+ queuedMessages={["queued during stream"]}
+ />,
+ );
+ expect(screen.getByText("queued during stream")).toBeDefined();
+ expect(screen.getByText("Queued")).toBeDefined();
+ });
+
+ it("renders no queued messages when prop is undefined", () => {
+ const { queuedMessages: _, ...propsWithoutQueued } = baseProps;
+ render(<ChatMessagesContainer {...propsWithoutQueued} />);
+ expect(screen.queryByText("Queued")).toBeNull();
+ });
+});
+
+// ── loading state ─────────────────────────────────────────────────────────
+
+describe("ChatMessagesContainer — loading", () => {
+ beforeEach(() => {
+ mockScrollEl.scrollHeight = 100;
+ mockScrollEl.scrollTop = 0;
+ mockScrollEl.clientHeight = 500;
+ MockIntersectionObserver.lastCallback = null;
+ vi.stubGlobal("IntersectionObserver", MockIntersectionObserver);
+ });
+
+ afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+ });
+
+ it("shows loading spinner when isLoading is true", () => {
+ render(<ChatMessagesContainer {...baseProps} isLoading />);
+ expect(screen.getByTestId("loading-spinner")).toBeDefined();
+ });
+
+ it("does not show spinner when not loading", () => {
+ render(<ChatMessagesContainer {...baseProps} isLoading={false} />);
+ expect(screen.queryByTestId("loading-spinner")).toBeNull();
+ });
+});
+
+// ── pagination sentinel ───────────────────────────────────────────────────
+
describe("ChatMessagesContainer", () => {
beforeEach(() => {
mockScrollEl.scrollHeight = 100;
@@ -125,21 +244,21 @@ describe("ChatMessagesContainer", () => {
});
it("renders top sentinel for backward pagination", () => {
- render(<ChatMessagesContainer {...BASE_PROPS} />);
+ render(<ChatMessagesContainer {...baseProps} />);
expect(
screen.getByRole("button", { name: /load older messages/i }),
).toBeDefined();
});
it("hides sentinel when hasMoreMessages is false", () => {
- render(<ChatMessagesContainer {...BASE_PROPS} hasMoreMessages={false} />);
+ render(<ChatMessagesContainer {...baseProps} hasMoreMessages={false} />);
expect(
screen.queryByRole("button", { name: /load older messages/i }),
).toBeNull();
});
it("hides sentinel when onLoadMore is not provided", () => {
- render(<ChatMessagesContainer {...BASE_PROPS} onLoadMore={undefined} />);
+ render(<ChatMessagesContainer {...baseProps} onLoadMore={undefined} />);
expect(
screen.queryByRole("button", { name: /load older messages/i }),
).toBeNull();
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/makePromotedBubble.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/makePromotedBubble.test.ts
new file mode 100644
index 0000000000..cca9b2a25c
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/makePromotedBubble.test.ts
@@ -0,0 +1,33 @@
+import { describe, expect, it } from "vitest";
+
+import { makePromotedUserBubble } from "../makePromotedBubble";
+
+describe("makePromotedUserBubble", () => {
+ it("joins texts with double newlines", () => {
+ const b = makePromotedUserBubble(["first", "second"], "midturn", "abc");
+ expect(b.role).toBe("user");
+ expect(b.parts).toHaveLength(1);
+ expect(b.parts[0]).toMatchObject({
+ type: "text",
+ text: "first\n\nsecond",
+ state: "done",
+ });
+ });
+
+ it("encodes the prefix + suffix in the id for dedup", () => {
+ const a = makePromotedUserBubble(["x"], "auto-continue", "assistant-123");
+ const b = makePromotedUserBubble(["x"], "midturn", "uuid-456");
+ expect(a.id).toBe("promoted-auto-continue-assistant-123");
+ expect(b.id).toBe("promoted-midturn-uuid-456");
+ });
+
+ it("handles a single text without joining", () => {
+ const b = makePromotedUserBubble(["only"], "midturn", "s");
+ expect(b.parts[0]).toMatchObject({ text: "only" });
+ });
+
+ it("yields empty-text bubble if given an empty list", () => {
+ const b = makePromotedUserBubble([], "midturn", "s");
+ expect(b.parts[0]).toMatchObject({ text: "" });
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/queueFollowUpMessage.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/queueFollowUpMessage.test.ts
new file mode 100644
index 0000000000..1dc7aa0c83
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/queueFollowUpMessage.test.ts
@@ -0,0 +1,107 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { queueFollowUpMessage } from "../queueFollowUpMessage";
+
+vi.mock("@/services/environment", () => ({
+ environment: {
+ getAGPTServerBaseUrl: () => "https://api.example.test",
+ },
+}));
+
+vi.mock("../../helpers", () => ({
+ getCopilotAuthHeaders: async () => ({ Authorization: "Bearer test-token" }),
+}));
+
+describe("queueFollowUpMessage", () => {
+ const originalFetch = global.fetch;
+
+ beforeEach(() => {
+ global.fetch = vi.fn();
+ });
+
+ afterEach(() => {
+ global.fetch = originalFetch;
+ vi.restoreAllMocks();
+ });
+
+ it("POSTs to /stream and returns kind=queued on 202", async () => {
+ const fetchMock = vi.mocked(global.fetch);
+ fetchMock.mockResolvedValueOnce(
+ new Response(
+ JSON.stringify({
+ buffer_length: 2,
+ max_buffer_length: 10,
+ turn_in_flight: true,
+ }),
+ { status: 202, headers: { "Content-Type": "application/json" } },
+ ),
+ );
+
+ const result = await queueFollowUpMessage("sess-1", "hello");
+
+ expect(result).toEqual({
+ kind: "queued",
+ buffer_length: 2,
+ max_buffer_length: 10,
+ turn_in_flight: true,
+ });
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ const [url, init] = fetchMock.mock.calls[0];
+ expect(url).toBe(
+ "https://api.example.test/api/chat/sessions/sess-1/stream",
+ );
+ expect(init?.method).toBe("POST");
+ const headers = init?.headers as Record<string, string>;
+ expect(headers["Content-Type"]).toBe("application/json");
+ expect(headers.Authorization).toBe("Bearer test-token");
+ expect(JSON.parse(init?.body as string)).toEqual({
+ message: "hello",
+ is_user_message: true,
+ context: null,
+ file_ids: null,
+ });
+ });
+
+ it("returns kind=raced_started_turn on 200 (race: server started a new turn)", async () => {
+ const fetchMock = vi.mocked(global.fetch);
+ // SSE-shaped 200 response — the body must be drainable
+ const sseBody = new ReadableStream({
+ start(controller) {
+ controller.enqueue(new TextEncoder().encode("data: hello\n\n"));
+ controller.close();
+ },
+ });
+ fetchMock.mockResolvedValueOnce(
+ new Response(sseBody, {
+ status: 200,
+ headers: { "Content-Type": "text/event-stream" },
+ }),
+ );
+
+ const result = await queueFollowUpMessage("sess-1", "hi");
+
+ expect(result).toEqual({ kind: "raced_started_turn", status: 200 });
+ });
+
+ it("throws when response is neither 200 nor 202", async () => {
+ const fetchMock = vi.mocked(global.fetch);
+ fetchMock.mockResolvedValueOnce(
+ new Response(JSON.stringify({ detail: "boom" }), { status: 500 }),
+ );
+
+ await expect(queueFollowUpMessage("sess-1", "hi")).rejects.toThrow(
+ /Expected 202 \(queued\) or 200 \(raced new turn\) from \/stream; got 500/,
+ );
+ });
+
+ it("throws on 429 rate-limit response", async () => {
+ const fetchMock = vi.mocked(global.fetch);
+ fetchMock.mockResolvedValueOnce(
+ new Response(JSON.stringify({ detail: "too many" }), { status: 429 }),
+ );
+
+ await expect(queueFollowUpMessage("sess-1", "hi")).rejects.toThrow(
+ /got 429/,
+ );
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/stripReplayPrefix.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/stripReplayPrefix.test.ts
new file mode 100644
index 0000000000..0da83c2339
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/stripReplayPrefix.test.ts
@@ -0,0 +1,198 @@
+import type { UIMessage } from "ai";
+import { describe, expect, it } from "vitest";
+
+import { stripReplayPrefix } from "../stripReplayPrefix";
+
+function user(id: string, text: string): UIMessage {
+ return {
+ id,
+ role: "user",
+ parts: [{ type: "text" as const, text, state: "done" as const }],
+ };
+}
+
+function assistant(id: string, ...segments: string[]): UIMessage {
+ return {
+ id,
+ role: "assistant",
+ parts: segments.map((text) => ({
+ type: "text" as const,
+ text,
+ state: "done" as const,
+ })),
+ };
+}
+
+/** Helper — extract the joined text content of an assistant's parts. */
+function textOf(m: UIMessage): string {
+ return (m.parts ?? [])
+ .map((p) => ("text" in p && typeof p.text === "string" ? p.text : ""))
+ .join("");
+}
+
+describe("stripReplayPrefix", () => {
+ it("passes through when there is no replay", () => {
+ const msgs = [
+ user("u1", "hi"),
+ assistant("a1", "hello"),
+ user("u2", "how are you"),
+ assistant("a2", "good thanks"),
+ ];
+ const result = stripReplayPrefix(msgs);
+ expect(result.map((m) => m.id)).toEqual(["u1", "a1", "u2", "a2"]);
+ expect(textOf(result[3])).toBe("good thanks");
+ });
+
+ it("drops an assistant that is a pure replay of an earlier one", () => {
+ // a2 has the same text as a1 — CLI --resume replayed it as the new turn's
+ // assistant before Claude emitted any new content yet.
+ const msgs = [
+ user("u1", "hi"),
+ assistant("a1", "hello world"),
+ user("u2", "tell me more"),
+ assistant("a2", "hello world"),
+ ];
+ const result = stripReplayPrefix(msgs);
+ const ids = result.map((m) => m.id);
+ expect(ids).not.toContain("a2");
+ expect(ids).toEqual(["u1", "a1", "u2"]);
+ });
+
+ it("strips the replayed prefix leaving only the new content", () => {
+ // a2 starts with a1's text then continues with new content.
+ const msgs = [
+ user("u1", "hi"),
+ assistant("a1", "hello world"),
+ user("u2", "tell me more"),
+ assistant("a2", "hello world\n\nAnd here is more info"),
+ ];
+ const result = stripReplayPrefix(msgs);
+ const a2 = result.find((m) => m.id === "a2")!;
+ expect(a2).toBeDefined();
+ expect(textOf(a2)).toBe("\n\nAnd here is more info");
+ });
+
+ it("drops an assistant whose text is still a streaming-catch-up prefix of an earlier one", () => {
+ // a2 is shorter than a1 and is a prefix of a1 — the CLI replay is
+ // still streaming. Once a2 grows past a1 we'll hit the strip path;
+ // for now, drop a2 to avoid the duplicate flash.
+ const msgs = [
+ user("u1", "hi"),
+ assistant("a1", "hello world how are you today"),
+ user("u2", "tell me more"),
+ assistant("a2", "hello worl"),
+ ];
+ const result = stripReplayPrefix(msgs);
+ const ids = result.map((m) => m.id);
+ expect(ids).not.toContain("a2");
+ });
+
+ it("preserves non-text parts (step-start / tool-* ) during stripping", () => {
+ // a2 is "prefix + new-text" with non-text parts interspersed. Only the
+ // leading replay text should be stripped; structural parts stay.
+ const a2: UIMessage = {
+ id: "a2",
+ role: "assistant",
+ parts: [
+ { type: "text" as const, text: "hello", state: "done" as const },
+ {
+ type: "tool-run_block" as const,
+ toolCallId: "tc1",
+ input: {},
+ state: "output-available" as const,
+ output: "ok",
+ },
+ {
+ type: "text" as const,
+ text: " world + extra",
+ state: "done" as const,
+ },
+ ],
+ };
+ const msgs: UIMessage[] = [
+ user("u1", "hi"),
+ assistant("a1", "hello world"),
+ user("u2", "tell me more"),
+ a2,
+ ];
+ const result = stripReplayPrefix(msgs);
+ const trimmed = result.find((m) => m.id === "a2");
+ expect(trimmed).toBeDefined();
+ // Leading "hello" text part consumed entirely, " world" partially stripped
+ // (11 chars stripped, but " world" is 6 chars so the first 5 of " world"
+ // which is part of replay stripped — tool-run_block and remaining text kept)
+ const parts = trimmed!.parts ?? [];
+ expect(parts[0]).toMatchObject({ type: "tool-run_block" });
+ });
+
+ it("leaves user messages untouched regardless of prefix matches", () => {
+ const msgs = [
+ user("u1", "hello"),
+ user("u2", "hello world"), // user u2 starts with u1's content
+ ];
+ const result = stripReplayPrefix(msgs);
+ expect(result.map((m) => m.id)).toEqual(["u1", "u2"]);
+ expect(textOf(result[1])).toBe("hello world");
+ });
+
+ it("passes empty-text assistants through (nothing to compare)", () => {
+ const msgs = [
+ user("u1", "hi"),
+ assistant("a1"), // no parts
+ user("u2", "x"),
+ assistant("a2", "response"),
+ ];
+ const result = stripReplayPrefix(msgs);
+ const ids = result.map((m) => m.id);
+ expect(ids).toContain("a1");
+ expect(ids).toContain("a2");
+ });
+
+ it("preserves non-text parts that follow the stripped text prefix", () => {
+ // a2 shares the leading "hello" text with a1, but has a non-text
+ // structural part after the text (e.g. a tool-call) — that part must
+ // survive the strip. Exercises the `remaining === 0` branch.
+ const msgs: UIMessage[] = [
+ user("u1", "hi"),
+ assistant("a1", "hello"),
+ user("u2", "more"),
+ {
+ id: "a2",
+ role: "assistant",
+ parts: [
+ { type: "text" as const, text: "hello", state: "done" as const },
+ // Non-text part (opaque to stripLeadingTextChars) must be kept.
+ { type: "step-start" as const },
+ { type: "text" as const, text: "!", state: "done" as const },
+ ],
+ } as UIMessage,
+ ];
+
+ const result = stripReplayPrefix(msgs);
+ const a2 = result.find((m) => m.id === "a2")!;
+ const kinds = (a2.parts ?? []).map((p) => p.type);
+ // "hello" text is dropped; step-start + trailing "!" text remain.
+ expect(kinds).toEqual(["step-start", "text"]);
+ expect(textOf(a2)).toBe("!");
+ });
+
+ it("returns an empty result array for an empty input", () => {
+ expect(stripReplayPrefix([])).toEqual([]);
+ });
+
+ it("handles the longest matching earlier assistant as the strip anchor", () => {
+ // a3 matches a2's full text (which is longer than a1's). Should strip a2's
+ // length, not a1's.
+ const msgs = [
+ user("u1", "hi"),
+ assistant("a1", "hello"),
+ user("u2", "again"),
+ assistant("a2", "hello and more"),
+ user("u3", "third turn"),
+ assistant("a3", "hello and more\n\n[new content]"),
+ ];
+ const result = stripReplayPrefix(msgs);
+ const a3 = result.find((m) => m.id === "a3")!;
+ expect(textOf(a3)).toBe("\n\n[new content]");
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/makePromotedBubble.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/makePromotedBubble.ts
new file mode 100644
index 0000000000..734abcb3e0
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/makePromotedBubble.ts
@@ -0,0 +1,27 @@
+import type { UIMessage } from "ai";
+
+/**
+ * Build a user-bubble `UIMessage` that represents pending chips which the
+ * backend has just drained. The prefix identifies the promotion path —
+ * `auto-continue` bubbles sit right before the auto-continue assistant,
+ * while `midturn` bubbles sit at the tail of the visible chat while the
+ * stream is still going. Force-hydrate replaces both flavours with the
+ * real DB row once the stream ends.
+ */
+export function makePromotedUserBubble(
+ texts: string[],
+ prefix: "auto-continue" | "midturn",
+ suffix: string,
+): UIMessage {
+ return {
+ id: `promoted-${prefix}-${suffix}`,
+ role: "user" as const,
+ parts: [
+ {
+ type: "text" as const,
+ text: texts.join("\n\n"),
+ state: "done" as const,
+ },
+ ],
+ };
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/queueFollowUpMessage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/queueFollowUpMessage.ts
new file mode 100644
index 0000000000..4688701a73
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/queueFollowUpMessage.ts
@@ -0,0 +1,78 @@
+import { environment } from "@/services/environment";
+
+import { getCopilotAuthHeaders } from "../helpers";
+
+export type QueueFollowUpResult =
+ | {
+ kind: "queued";
+ buffer_length: number;
+ max_buffer_length: number;
+ turn_in_flight: boolean;
+ }
+ | {
+ kind: "raced_started_turn";
+ status: number;
+ };
+
+/**
+ * POST a follow-up message to the unified ``/stream`` endpoint when the
+ * client believes a turn is already in flight.
+ *
+ * The server decides:
+ * - turn in flight → push to pending buffer, return ``202 JSON`` → kind="queued"
+ * - session idle → start a new turn, return ``200 SSE`` → kind="raced_started_turn"
+ *
+ * The "raced" branch covers a real race condition: the client's
+ * ``isInflightRef`` reads true for one render, but by the time the request
+ * lands the previous turn already finished. Throwing in that case would
+ * surface a misleading error toast — instead we drain the SSE response so
+ * the connection is closed cleanly and signal "raced" to the caller, who
+ * can rely on ``useHydrateOnStreamEnd`` to surface the new turn's output.
+ *
+ * Throws only on transport errors or unexpected non-200/202 status codes.
+ */
+export async function queueFollowUpMessage(
+ sessionId: string,
+ message: string,
+): Promise<QueueFollowUpResult> {
+ const url = `${environment.getAGPTServerBaseUrl()}/api/chat/sessions/${sessionId}/stream`;
+ const res = await fetch(url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ ...(await getCopilotAuthHeaders()),
+ },
+ body: JSON.stringify({
+ message,
+ is_user_message: true,
+ context: null,
+ file_ids: null,
+ }),
+ });
+
+ if (res.status === 202) {
+ const body = (await res.json()) as {
+ buffer_length: number;
+ max_buffer_length: number;
+ turn_in_flight: boolean;
+ };
+ return { kind: "queued", ...body };
+ }
+
+ if (res.status === 200) {
+ // Race: server treated this as a fresh turn (previous turn finished
+ // between our isInflight read and the request landing). Drain the body
+ // so the underlying connection isn't held open, then signal the caller
+ // — useHydrateOnStreamEnd will pick the assistant rows up next poll.
+ try {
+ await res.body?.cancel();
+ } catch {
+ // ignore — connection cleanup is best-effort
+ }
+ return { kind: "raced_started_turn", status: 200 };
+ }
+
+ throw new Error(
+ `Expected 202 (queued) or 200 (raced new turn) from /stream; got ${res.status}`,
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/stripReplayPrefix.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/stripReplayPrefix.ts
new file mode 100644
index 0000000000..260b293183
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/stripReplayPrefix.ts
@@ -0,0 +1,122 @@
+import type { UIMessage } from "ai";
+
+import { deduplicateMessages } from "../helpers";
+
+/**
+ * Strip replayed leading text from assistant messages.
+ *
+ * **Why this exists**: the Claude Agent SDK with `--resume` replays the
+ * previous turn's assistant text at the start of the next turn's assistant
+ * message. Left unchecked, the UI shows the same greeting/response twice
+ * (once under Turn N−1's user bubble, again at the top of Turn N). We
+ * detect and strip that replayed prefix so each turn stays anchored to its
+ * own user message.
+ *
+ * **How** (three cases, evaluated against the longest matching earlier
+ * assistant text):
+ *
+ * 1. earlier **equals** current → pure replay; drop the current message.
+ * 2. earlier is a **strict prefix** of current → strip those leading chars
+ * from the current message's text parts (preserving non-text parts).
+ * 3. current is a **strict prefix** of earlier (live-streaming catch-up) →
+ * drop the current message. Once its text grows past the earlier one,
+ * case 2 takes over.
+ *
+ * Comparison is by concatenated text content rather than by part index
+ * because `--resume` interleaves different step-boundary part layouts, so
+ * a structural prefix match is unreliable. Claude only replays TEXT at the
+ * top of the next turn in practice, so text-only comparison covers the
+ * cases we see.
+ */
+export function stripReplayPrefix(raw: UIMessage[]): UIMessage[] {
+ const deduped = deduplicateMessages(raw);
+ const texts = deduped.map(concatText);
+ const out: UIMessage[] = [];
+
+ for (let i = 0; i < deduped.length; i++) {
+ const msg = deduped[i];
+ if (msg.role !== "assistant" || !texts[i]) {
+ out.push(msg);
+ continue;
+ }
+
+ const match = findLongestReplay(texts, i);
+ if (match.drop) continue;
+ if (match.stripLen === 0) {
+ out.push(msg);
+ continue;
+ }
+ const trimmed = stripLeadingTextChars(msg, match.stripLen);
+ if (trimmed !== null) out.push(trimmed);
+ }
+ return out;
+}
+
+function concatText(msg: UIMessage): string {
+ return (msg.parts ?? [])
+ .map((p) => ("text" in p && typeof p.text === "string" ? p.text : ""))
+ .join("");
+}
+
+/**
+ * Return either `{ drop: true }` if the current message should be removed,
+ * or `{ stripLen }` with the number of leading text chars to strip (0 =
+ * keep as-is).
+ */
+function findLongestReplay(
+ texts: string[],
+ i: number,
+): { drop: boolean; stripLen: number } {
+ const myText = texts[i];
+ let stripLen = 0;
+
+ for (let j = 0; j < i; j++) {
+ const earlier = texts[j];
+ if (!earlier) continue;
+
+ if (myText.startsWith(earlier)) {
+ // Case 1 + 2: earlier is a leading prefix of current.
+ if (earlier.length < stripLen) continue;
+ if (earlier.length === myText.length) return { drop: true, stripLen };
+ stripLen = earlier.length;
+ } else if (earlier.startsWith(myText)) {
+ // Case 3: live-streaming replay is still catching up.
+ return { drop: true, stripLen };
+ }
+ }
+ return { drop: false, stripLen };
+}
+
+/**
+ * Drop the leading `n` characters of text from *msg*'s parts, preserving
+ * non-text parts (step-start/finish, tool-*) unchanged. Returns `null` if
+ * the result would be empty (all text consumed and no structural parts).
+ */
+function stripLeadingTextChars(msg: UIMessage, n: number): UIMessage | null {
+ const trimmed: UIMessage["parts"] = [];
+ let remaining = n;
+
+ for (const part of msg.parts ?? []) {
+ if (remaining === 0) {
+ trimmed.push(part);
+ continue;
+ }
+ if ("text" in part && typeof part.text === "string") {
+ if (part.text.length <= remaining) {
+ remaining -= part.text.length;
+ continue; // drop entire part
+ }
+ trimmed.push({ ...part, text: part.text.slice(remaining) });
+ remaining = 0;
+ } else {
+ trimmed.push(part);
+ }
+ }
+
+ const empty =
+ trimmed.length === 0 ||
+ trimmed.every(
+ (p) => "text" in p && typeof p.text === "string" && p.text.length === 0,
+ );
+ return empty ? null : { ...msg, parts: trimmed };
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
index 2d56b27303..0551fb0387 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
@@ -11,8 +11,11 @@ import { useSupabase } from "@/lib/supabase/hooks/useSupabase";
import { useQueryClient } from "@tanstack/react-query";
import type { FileUIPart } from "ai";
import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
-import { useEffect, useRef, useState } from "react";
+import { useEffect, useMemo, useRef, useState } from "react";
import { concatWithAssistantMerge } from "./helpers/convertChatSessionToUiMessages";
+import { queueFollowUpMessage } from "./helpers/queueFollowUpMessage";
+import { stripReplayPrefix } from "./helpers/stripReplayPrefix";
+import { useCopilotPendingChips } from "./useCopilotPendingChips";
import { useCopilotUIStore } from "./store";
import { useChatSession } from "./useChatSession";
import { useCopilotNotifications } from "./useCopilotNotifications";
@@ -66,6 +69,7 @@ export function useCopilotPage() {
const {
messages: currentMessages,
+ setMessages,
sendMessage,
stop,
status,
@@ -92,11 +96,35 @@ export function useCopilotPage() {
initialPageRawMessages: rawSessionMessages,
});
+ // Ref that mirrors whether a stream turn is currently in-flight.
+ // Updated synchronously on every render so it always reflects the latest
+ // status — unlike reading `status` inside onSend (which captures the
+ // closure's render-cycle value and can be stale for a frame).
+ // Setting it to true *before* calling sendMessage prevents rapid
+ // double-presses from both routing to /stream before React can re-render
+ // with status="submitted".
+ const isInflightRef = useRef(false);
+ isInflightRef.current = status === "streaming" || status === "submitted";
+
// Combine paginated messages with current page messages, merging consecutive
// assistant UIMessages at the page boundary so reasoning + response parts
// stay in a single bubble. Paged messages are older history prepended before
// the current page.
- const messages = concatWithAssistantMerge(pagedMessages, currentMessages);
+ const rawMessages = concatWithAssistantMerge(pagedMessages, currentMessages);
+
+ // Drop / trim assistant messages whose leading text is a replay of an
+ // earlier assistant (Claude Agent SDK's `--resume` behaviour). See
+ // helpers/stripReplayPrefix.ts for the three cases.
+ const messages = useMemo(() => stripReplayPrefix(rawMessages), [rawMessages]);
+
+ // Chip state machine (peek sync + auto-continue promotion + mid-turn poll)
+ // lives in a dedicated hook so this component is just glue.
+ const { queuedMessages, appendChip } = useCopilotPendingChips({
+ sessionId,
+ status,
+ messages,
+ setMessages,
+ });
useCopilotNotifications(sessionId);
@@ -252,12 +280,57 @@ export function useCopilotPage() {
isUserStoppingRef.current = false;
if (sessionId) {
+ const isInFlight = isInflightRef.current;
+
+ if (isInFlight) {
+ // File attachments cannot be included in a queued pending message —
+ // the queue API does not support file_ids. Inform the user and bail.
+ if (files && files.length > 0) {
+ toast({
+ title: "Please wait to attach files",
+ description:
+ "File attachments can't be queued until the current response finishes.",
+ variant: "destructive",
+ });
+ return;
+ }
+
+ // Queue the follow-up into the pending buffer. Both queue and new-
+ // turn go through the same ``/stream`` endpoint; the server returns
+ // 202 for the queue path (pending buffer) and an SSE stream for the
+ // new-turn path. Using a plain ``fetch`` here keeps the 202 JSON
+ // from confusing the AI SDK's stream parser.
+ try {
+ const result = await queueFollowUpMessage(sessionId, trimmed);
+ if (result.kind === "queued") {
+ appendChip(trimmed);
+ }
+ // kind === "raced_started_turn": the client thought a turn was in
+ // flight but the previous turn finished before the request landed.
+ // The server already kicked off a new turn; useHydrateOnStreamEnd
+ // will surface its output when it completes. No chip + no toast.
+ } catch (err) {
+ toast({
+ title: "Could not queue message",
+ description: "Please wait for the current response to finish.",
+ variant: "destructive",
+ });
+ throw err;
+ }
+ return;
+ }
+
+ // Mark in-flight synchronously before sendMessage so any rapid
+ // second press sees isInflightRef.current=true and routes to /pending
+ // instead of triggering a duplicate /stream POST.
+ isInflightRef.current = true;
if (files && files.length > 0) {
setIsUploadingFiles(true);
try {
const uploaded = await uploadFiles(files, sessionId);
if (uploaded.length === 0) {
// All uploads failed — abort send so chips revert to editable
+ isInflightRef.current = false;
throw new Error("All file uploads failed");
}
const fileParts = buildFileParts(uploaded);
@@ -394,6 +467,10 @@ export function useCopilotPage() {
isLoggedIn,
createSession,
onSend,
+ // onEnqueue delegates to onSend, which internally routes to the pending
+ // endpoint when isInflightRef.current is true.
+ onEnqueue: onSend,
+ queuedMessages,
// Pagination
hasMoreMessages: hasMore,
isLoadingMore,
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.ts
new file mode 100644
index 0000000000..7778623693
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.ts
@@ -0,0 +1,285 @@
+import { getV2GetPendingMessages } from "@/app/api/__generated__/endpoints/chat/chat";
+import type { UIMessage } from "ai";
+import { useCallback, useEffect, useRef, useState } from "react";
+
+import { makePromotedUserBubble } from "./helpers/makePromotedBubble";
+
+const MID_TURN_POLL_MS = 2_000;
+
+type ChatStatus = "submitted" | "streaming" | "ready" | "error";
+
+interface Args {
+ sessionId: string | null;
+ status: ChatStatus;
+ messages: UIMessage[];
+ setMessages: (
+ updater: UIMessage[] | ((prev: UIMessage[]) => UIMessage[]),
+ ) => void;
+}
+
+/**
+ * Owns the chip lifecycle: keep the local chip list in sync with Redis,
+ * promote chips to user-bubbles when the backend drains (auto-continue or
+ * mid-turn via the MCP wrapper), and surface the list + queue op to the
+ * chat input.
+ *
+ * State machine:
+ *
+ * ┌────────┐ user queues ┌────────────┐ backend turn-start drain
+ * │ empty │ ───────────▶ │ showing │ ─────────────────────────┐
+ * └────────┘ │ chips │ │
+ * ▲ └────────────┘ │
+ * │ │
+ * │ ┌──────────────────────────────────────────────┘
+ * │ │ 1. auto-continue chain: promote combined bubble
+ * │ │ 2. mid-turn poll sees count drop: promote partial
+ * └────────────┘ 3. stream ends, hydration takes over
+ */
+export function useCopilotPendingChips({
+ sessionId,
+ status,
+ messages,
+ setMessages,
+}: Args) {
+ const [queuedMessages, setQueuedMessages] = useState<string[]>([]);
+
+ usePeekOnBoundary({ sessionId, status, setQueuedMessages });
+
+ useAutoContinuePromotion({
+ sessionId,
+ status,
+ messages,
+ queuedMessages,
+ setMessages,
+ setQueuedMessages,
+ });
+
+ useMidTurnDrainPromotion({
+ sessionId,
+ status,
+ queuedMessages,
+ setMessages,
+ setQueuedMessages,
+ });
+
+ const appendChip = useCallback((text: string) => {
+ setQueuedMessages((prev) => [...prev, text]);
+ }, []);
+
+ return { queuedMessages, appendChip };
+}
+
+// ── 1. Peek sync ───────────────────────────────────────────────────────
+// Restore chips from Redis on session load + any time a turn ends (the
+// backend may have drained; we reconcile with server truth). Also
+// re-peeks on `submitted → streaming` so turn-start drains reconcile
+// without a separate effect — one edge-triggered peek covers both cases.
+
+function usePeekOnBoundary({
+ sessionId,
+ status,
+ setQueuedMessages,
+}: {
+ sessionId: string | null;
+ status: ChatStatus;
+ setQueuedMessages: (v: string[]) => void;
+}) {
+ const prevSessionIdRef = useRef<string | null>(sessionId);
+ const prevStatusRef = useRef<ChatStatus>(status);
+
+ useEffect(() => {
+ const prevStatus = prevStatusRef.current;
+ const sessionChanged = prevSessionIdRef.current !== sessionId;
+ prevSessionIdRef.current = sessionId;
+ prevStatusRef.current = status;
+
+ // Clear any stale chips from the previous session before the peek
+ // resolves — otherwise the new session briefly shows the old session's
+ // chips against its own messages.
+ if (sessionChanged) setQueuedMessages([]);
+
+ if (!sessionId) return;
+
+ const isIdle = status === "ready" || status === "error";
+ const turnStarting = prevStatus === "submitted" && status === "streaming";
+
+ // Peek on: session-change, idle (covers both first-mount-in-idle and
+ // becameIdle transitions), and turn-start drain. One effect, three
+ // edges — replaces the previous split between usePeekSync and the
+ // auto-continue effect's duplicate turn-start peek.
+ if (!sessionChanged && !isIdle && !turnStarting) return;
+
+ void getV2GetPendingMessages(sessionId).then((res) => {
+ if (res.status !== 200) return;
+ // Turn-start drain path: only clear if the backend really emptied
+ // the buffer. A non-zero count means our chips survived the drain
+ // (e.g. the turn is still consuming them mid-round) — keep them.
+ if (turnStarting && !sessionChanged) {
+ if (res.data.count === 0) setQueuedMessages([]);
+ return;
+ }
+ // Session-load or idle-after-turn: replace with server truth.
+ setQueuedMessages(res.data.count > 0 ? res.data.messages : []);
+ });
+ }, [sessionId, status, setQueuedMessages]);
+}
+
+// ── 2. Auto-continue promotion ─────────────────────────────────────────
+// When the backend auto-continues (a SECOND new assistant ID appears in
+// the same stream chain), combine chips into one user bubble and insert
+// it just before that assistant — matching the DB's chronological order.
+//
+// Tracking model: remember the FIRST assistant id seen after
+// `submitted → streaming` (that's Turn 1's opener). Any later new
+// assistant id in the same chain is the auto-continue. Reset on every
+// turn boundary.
+
+function useAutoContinuePromotion({
+ sessionId,
+ status,
+ messages,
+ queuedMessages,
+ setMessages,
+ setQueuedMessages,
+}: {
+ sessionId: string | null;
+ status: ChatStatus;
+ messages: UIMessage[];
+ queuedMessages: string[];
+ setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void;
+ setQueuedMessages: (v: string[]) => void;
+}) {
+ const prevStatusRef = useRef(status);
+ // The opener is the first assistant id observed after a turn starts.
+ // Any LATER assistant id in the same chain is the auto-continue.
+ // Reset to null on every turn boundary (turn-start or becameIdle) so
+ // the next chain starts fresh.
+ const openerAssistantIdRef = useRef<string | null>(null);
+
+ useEffect(() => {
+ const prevStatus = prevStatusRef.current;
+ prevStatusRef.current = status;
+
+ const turnStarting = prevStatus === "submitted" && status === "streaming";
+ const becameIdle =
+ (prevStatus === "streaming" || prevStatus === "submitted") &&
+ (status === "ready" || status === "error");
+ if (turnStarting || becameIdle) {
+ openerAssistantIdRef.current = null;
+ }
+
+ if (!sessionId) return;
+ const isActive = status === "streaming" || status === "submitted";
+ if (!isActive) return;
+
+ const assistantIds = messages
+ .filter((m) => m.role === "assistant")
+ .map((m) => m.id);
+ if (assistantIds.length === 0) return;
+
+ const latest = assistantIds[assistantIds.length - 1];
+ // First assistant id of this chain — it's Turn 1's opener.
+ if (openerAssistantIdRef.current === null) {
+ openerAssistantIdRef.current = latest;
+ return;
+ }
+ // Same id as opener — no new assistant yet, wait.
+ if (latest === openerAssistantIdRef.current) return;
+ // A different id means the backend auto-continued.
+ if (queuedMessages.length === 0) return;
+
+ promoteBeforeAssistant(setMessages, latest, queuedMessages);
+ setQueuedMessages([]);
+ }, [
+ messages,
+ status,
+ sessionId,
+ queuedMessages,
+ setMessages,
+ setQueuedMessages,
+ ]);
+}
+
+function promoteBeforeAssistant(
+ setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void,
+ assistantId: string,
+ texts: string[],
+): void {
+ setMessages((prev) => {
+ const bubbleId = `promoted-auto-continue-${assistantId}`;
+ if (prev.some((m) => m.id === bubbleId)) return prev;
+ const bubble = makePromotedUserBubble(texts, "auto-continue", assistantId);
+ const idx = prev.findIndex((m) => m.id === assistantId);
+ const insertAt = idx === -1 ? prev.length : idx;
+ return [...prev.slice(0, insertAt), bubble, ...prev.slice(insertAt)];
+ });
+}
+
+// ── 3. Mid-turn drain promotion ────────────────────────────────────────
+// The MCP tool wrapper can drain the buffer at a tool boundary without
+// emitting an SSE event, so the client doesn't know until we poll. On
+// every poll, if the backend count dropped below our local chip count,
+// promote the difference and keep the remainder as chips.
+//
+// TODO(followup): replace the 2s poll with an SSE event pushed from the
+// backend at drain time — the MCP wrapper already knows when it drains,
+// so a single "pending:drained" event would let us drop this effect
+// entirely. Tracked separately from this PR.
+
+function useMidTurnDrainPromotion({
+ sessionId,
+ status,
+ queuedMessages,
+ setMessages,
+ setQueuedMessages,
+}: {
+ sessionId: string | null;
+ status: ChatStatus;
+ queuedMessages: string[];
+ setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void;
+ setQueuedMessages: (v: string[]) => void;
+}) {
+ useEffect(() => {
+ if (!sessionId) return;
+ const isActive = status === "streaming" || status === "submitted";
+ if (!isActive || queuedMessages.length === 0) return;
+
+ const interval = setInterval(() => {
+ void pollBackendAndPromote(
+ sessionId,
+ queuedMessages,
+ setMessages,
+ setQueuedMessages,
+ );
+ }, MID_TURN_POLL_MS);
+ return () => clearInterval(interval);
+ }, [sessionId, status, queuedMessages, setMessages, setQueuedMessages]);
+}
+
+async function pollBackendAndPromote(
+ sessionId: string,
+ localChips: string[],
+ setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void,
+ setQueuedMessages: (v: string[]) => void,
+): Promise<void> {
+ let backendCount: number;
+ try {
+ const res = await getV2GetPendingMessages(sessionId);
+ if (res.status !== 200) return;
+ backendCount = res.data.count;
+ } catch {
+ return; // harmless; next tick or hydration will reconcile
+ }
+ if (localChips.length === 0) return;
+ if (backendCount >= localChips.length) return;
+
+ const drainedCount = localChips.length - backendCount;
+ const drained = localChips.slice(0, drainedCount);
+ const remaining = localChips.slice(drainedCount);
+
+ setMessages((prev) => [
+ ...prev,
+ makePromotedUserBubble(drained, "midturn", crypto.randomUUID()),
+ ]);
+ setQueuedMessages(remaining);
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
index 666b87bfba..2412ff5988 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
@@ -20,6 +20,7 @@ import {
disconnectSessionStream,
} from "./helpers";
import type { CopilotLlmModel, CopilotMode } from "./store";
+import { useHydrateOnStreamEnd } from "./useHydrateOnStreamEnd";
const RECONNECT_BASE_DELAY_MS = 1_000;
const RECONNECT_MAX_ATTEMPTS = 3;
@@ -419,16 +420,15 @@ export function useCopilotStream({
};
}, [refetchSession, setMessages]);
- // Hydrate messages from REST API when not actively streaming
- useEffect(() => {
- if (!hydratedMessages || hydratedMessages.length === 0) return;
- if (status === "streaming" || status === "submitted") return;
- if (isReconnectScheduled) return;
- setMessages((prev) => {
- if (prev.length >= hydratedMessages.length) return prev;
- return deduplicateMessages(hydratedMessages);
- });
- }, [hydratedMessages, setMessages, status, isReconnectScheduled]);
+ // After-stream hydration — force-replace AI-SDK state with the DB's view
+ // once React Query has actually refetched, then keep length-gated top-ups
+ // working for pagination. See useHydrateOnStreamEnd for the timing dance.
+ useHydrateOnStreamEnd({
+ status,
+ hydratedMessages,
+ isReconnectScheduled,
+ setMessages,
+ });
// Track resume state per session
const hasResumedRef = useRef<Map<string, boolean>>(new Map());
@@ -567,6 +567,7 @@ export function useCopilotStream({
return {
messages,
+ setMessages,
sendMessage,
stop,
status,
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts
new file mode 100644
index 0000000000..815d7fb3d3
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts
@@ -0,0 +1,80 @@
+import type { UIMessage } from "ai";
+import { useEffect, useRef } from "react";
+
+import { deduplicateMessages } from "./helpers";
+
+type ChatStatus = "submitted" | "streaming" | "ready" | "error";
+
+interface Args {
+ status: ChatStatus;
+ hydratedMessages: UIMessage[] | undefined;
+ isReconnectScheduled: boolean;
+ setMessages: (
+ updater: UIMessage[] | ((prev: UIMessage[]) => UIMessage[]),
+ ) => void;
+}
+
+/**
+ * After a stream ends, replace the in-memory AI SDK messages with the
+ * definitive DB state, then keep length-gated top-ups working for later
+ * pagination / sync events.
+ *
+ * **The tricky bit** (hence this being its own hook): `status` flips to
+ * "ready" BEFORE the post-turn refetch completes — there's a ~500 ms delay
+ * plus the DB round trip. If we force-hydrate immediately, `hydratedMessages`
+ * is the STALE pre-turn snapshot and we'd drop any newly-persisted row
+ * (mid-turn follow-up user rows, for example).
+ *
+ * Solution: when the stream ends, snapshot the `hydratedMessages` reference
+ * that was current at that moment, and refuse to force-hydrate until React
+ * Query swaps in a new reference. Once we see a fresh reference, replace
+ * and clear the flag.
+ */
+export function useHydrateOnStreamEnd({
+ status,
+ hydratedMessages,
+ isReconnectScheduled,
+ setMessages,
+}: Args) {
+ const prevStatusRef = useRef(status);
+ const needsForceHydrateRef = useRef(false);
+ const staleRefAtStreamEnd = useRef<typeof hydratedMessages | null>(null);
+
+ // Arm the force-hydrate flag the moment the stream transitions to idle.
+ useEffect(() => {
+ const wasActive =
+ prevStatusRef.current === "streaming" ||
+ prevStatusRef.current === "submitted";
+ const isNowIdle = status === "ready" || status === "error";
+ prevStatusRef.current = status;
+ if (wasActive && isNowIdle) {
+ needsForceHydrateRef.current = true;
+ staleRefAtStreamEnd.current = hydratedMessages ?? null;
+ }
+ }, [status, hydratedMessages]);
+
+ // Apply hydration when the right data shows up.
+ useEffect(() => {
+ if (!hydratedMessages || hydratedMessages.length === 0) return;
+ if (status === "streaming" || status === "submitted") return;
+ if (isReconnectScheduled) return;
+
+ if (needsForceHydrateRef.current) {
+ if (hydratedMessages === staleRefAtStreamEnd.current) {
+ // Still the pre-turn snapshot — wait for the refetch.
+ return;
+ }
+ setMessages(deduplicateMessages(hydratedMessages));
+ needsForceHydrateRef.current = false;
+ staleRefAtStreamEnd.current = null;
+ return;
+ }
+
+ // Regular length-gated top-up (e.g. pagination brought in older messages).
+ setMessages((prev) =>
+ prev.length >= hydratedMessages.length
+ ? prev
+ : deduplicateMessages(hydratedMessages),
+ );
+ }, [hydratedMessages, setMessages, status, isReconnectScheduled]);
+}
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index 24ec485dbc..920348db25 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -1640,6 +1640,47 @@
}
}
},
+ "/api/chat/sessions/{session_id}/messages/pending": {
+ "get": {
+ "tags": ["v2", "chat", "chat"],
+ "summary": "Get Pending Messages",
+ "description": "Peek at the pending-message buffer without consuming it.\n\nReturns the current contents of the session's pending message buffer\nso the frontend can restore the queued-message indicator after a page\nrefresh and clear it correctly once a turn drains the buffer.",
+ "operationId": "getV2GetPendingMessages",
+ "security": [{ "HTTPBearerJWT": [] }],
+ "parameters": [
+ {
+ "name": "session_id",
+ "in": "path",
+ "required": true,
+ "schema": { "type": "string", "title": "Session Id" }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "Successful Response",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/PeekPendingMessagesResponse"
+ }
+ }
+ }
+ },
+ "401": {
+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"
+ },
+ "404": { "description": "Session not found or access denied" },
+ "422": {
+ "description": "Validation Error",
+ "content": {
+ "application/json": {
+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }
+ }
+ }
+ }
+ }
+ }
+ },
"/api/chat/sessions/{session_id}/stream": {
"delete": {
"tags": ["v2", "chat", "chat"],
@@ -1705,7 +1746,7 @@
"post": {
"tags": ["v2", "chat", "chat"],
"summary": "Stream Chat Post",
- "description": "Stream chat responses for a session (POST with context support).\n\nStreams the AI/completion responses in real time over Server-Sent Events (SSE), including:\n - Text fragments as they are generated\n - Tool call UI elements (if invoked)\n - Tool execution results\n\nThe AI generation runs in a background task that continues even if the client disconnects.\nAll chunks are written to a per-turn Redis stream for reconnection support. If the client\ndisconnects, they can reconnect using GET /sessions/{session_id}/stream to resume.\n\nArgs:\n session_id: The chat session identifier to associate with the streamed messages.\n request: Request body containing message, is_user_message, and optional context.\n user_id: Authenticated user ID.\nReturns:\n StreamingResponse: SSE-formatted response chunks.",
+ "description": "Start a new turn OR queue a follow-up — decided server-side.\n\n- **Session idle**: starts a turn. Returns an SSE stream (``text/event-stream``)\n with Vercel AI SDK chunks (text fragments, tool-call UI, tool results).\n The generation runs in a background task that survives client disconnects;\n reconnect via ``GET /sessions/{session_id}/stream`` to resume.\n\n- **Session has a turn in flight**: pushes the message into the per-session\n pending buffer and returns ``202 application/json`` with\n ``QueuePendingMessageResponse``. The executor running the current turn\n drains the buffer between tool-call rounds (baseline) or at the start of\n the next turn (SDK). Clients should detect the 202 and surface the\n message as a queued-chip in the UI.\n\nArgs:\n session_id: The chat session identifier.\n request: Request body with message, is_user_message, and optional context.\n user_id: Authenticated user ID.",
"operationId": "postV2StreamChatPost",
"security": [{ "HTTPBearerJWT": [] }],
"parameters": [
@@ -1729,9 +1770,20 @@
"description": "Successful Response",
"content": { "application/json": { "schema": {} } }
},
+ "202": {
+ "description": "Session has a turn in flight — message queued into the pending buffer and will be picked up between tool-call rounds by the executor currently processing the turn.",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/QueuePendingMessageResponse"
+ }
+ }
+ }
+ },
"401": {
"$ref": "#/components/responses/HTTP401NotAuthenticatedError"
},
+ "404": { "description": "Session not found or access denied" },
"422": {
"description": "Validation Error",
"content": {
@@ -1739,6 +1791,9 @@
"schema": { "$ref": "#/components/schemas/HTTPValidationError" }
}
}
+ },
+ "429": {
+ "description": "Token rate-limit or call-frequency cap exceeded"
}
}
}
@@ -12191,6 +12246,20 @@
"required": ["total_items", "total_pages", "current_page", "page_size"],
"title": "Pagination"
},
+ "PeekPendingMessagesResponse": {
+ "properties": {
+ "messages": {
+ "items": { "type": "string" },
+ "type": "array",
+ "title": "Messages"
+ },
+ "count": { "type": "integer", "title": "Count" }
+ },
+ "type": "object",
+ "required": ["messages", "count"],
+ "title": "PeekPendingMessagesResponse",
+ "description": "Response for the pending-message peek (GET) endpoint.\n\nReturns a read-only view of the pending buffer — messages are NOT\nconsumed. The frontend uses this to restore the queued-message\nindicator after a page refresh and to decide when to clear it once\na turn has ended."
+ },
"PendingHumanReviewModel": {
"properties": {
"node_exec_id": {
@@ -12900,6 +12969,20 @@
"required": ["providers", "pagination"],
"title": "ProviderResponse"
},
+ "QueuePendingMessageResponse": {
+ "properties": {
+ "buffer_length": { "type": "integer", "title": "Buffer Length" },
+ "max_buffer_length": {
+ "type": "integer",
+ "title": "Max Buffer Length"
+ },
+ "turn_in_flight": { "type": "boolean", "title": "Turn In Flight" }
+ },
+ "type": "object",
+ "required": ["buffer_length", "max_buffer_length", "turn_in_flight"],
+ "title": "QueuePendingMessageResponse",
+ "description": "Response returned by ``POST /stream`` with status 202 when a message\nis queued because the session already has a turn in flight.\n\n- ``buffer_length``: how many messages are now in the session's\n pending buffer (after this push)\n- ``max_buffer_length``: the per-session cap (server-side constant)\n- ``turn_in_flight``: ``True`` if a copilot turn was running when\n we checked — purely informational for UX feedback. Always ``True``\n for responses from ``POST /stream`` with status 202."
+ },
"RateLimitResetResponse": {
"properties": {
"success": { "type": "boolean", "title": "Success" },