Skip to content

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

fix(backend): carry the drained text on the pending-drained hint (#14574)

Abhimanyu Yadav225cf074f095
diff --git a/autogpt_platform/backend/backend/copilot/pending_messages.py b/autogpt_platform/backend/backend/copilot/pending_messages.py
index c2c58bf9f9..296dac9896 100644
--- a/autogpt_platform/backend/backend/copilot/pending_messages.py
+++ b/autogpt_platform/backend/backend/copilot/pending_messages.py
@@ -24,11 +24,15 @@ buffer is trimmed to the latest ``MAX_PENDING_MESSAGES`` on every push.
import json
import logging
import time
+import uuid
from typing import Any, cast
from pydantic import BaseModel, Field, ValidationError
-from backend.copilot.response_model import StreamPendingDrained
+from backend.copilot.response_model import (
+ StreamPendingDrained,
+ StreamPendingDrainedMessage,
+)
from backend.copilot.stream_registry import get_session, publish_chunk
from backend.data.redis_client import get_redis_async
from backend.data.redis_helpers import capped_rpush, capped_rpush_if_hash_field
@@ -67,6 +71,10 @@ class PendingMessageContext(BaseModel):
class PendingMessage(BaseModel):
"""A user message queued for injection into an in-flight turn."""
+ # Stable identity so the mid-turn drain hint can key the UI bubble it
+ # renders for this message. Defaulted rather than required: entries
+ # written by older workers are still sitting in Redis and must validate.
+ id: str = Field(default_factory=lambda: str(uuid.uuid4()))
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
@@ -214,11 +222,13 @@ async def drain_pending_messages(session_id: str) -> list[PendingMessage]:
len(messages),
session_id,
)
- await _notify_pending_drained(session_id, len(messages))
+ await _notify_pending_drained(session_id, messages)
return messages
-async def _notify_pending_drained(session_id: str, drained_count: int) -> None:
+async def _notify_pending_drained(
+ session_id: str, drained: list[PendingMessage]
+) -> None:
"""Emit a ``data-pending-drained`` hint onto the session's live SSE
stream so the frontend promotes its queued chips to bubbles right away
instead of waiting for its backstop poll.
@@ -228,6 +238,10 @@ async def _notify_pending_drained(session_id: str, drained_count: int) -> None:
or dropped emit only delays the chip→bubble swap — it never loses data.
The active turn is looked up from the session so the chunk lands on the
correct per-turn Redis stream.
+
+ The hint also carries the drained text so the client can render the
+ follow-up bubble exactly where the backend injected it, between the
+ tool chain that ran before the drain and the work that follows it.
"""
try:
active = await get_session(session_id)
@@ -235,7 +249,13 @@ async def _notify_pending_drained(session_id: str, drained_count: int) -> None:
return
await publish_chunk(
active.turn_id,
- StreamPendingDrained(drainedCount=drained_count),
+ StreamPendingDrained(
+ drainedCount=len(drained),
+ messages=[
+ StreamPendingDrainedMessage(id=m.id, content=m.content)
+ for m in drained
+ ],
+ ),
session_id=session_id,
)
except Exception:
diff --git a/autogpt_platform/backend/backend/copilot/pending_messages_test.py b/autogpt_platform/backend/backend/copilot/pending_messages_test.py
index 23576eedda..b97f7a4bc7 100644
--- a/autogpt_platform/backend/backend/copilot/pending_messages_test.py
+++ b/autogpt_platform/backend/backend/copilot/pending_messages_test.py
@@ -203,6 +203,21 @@ async def test_drain_empty_returns_empty_list(fake_redis: _FakeRedis) -> None:
assert await drain_pending_messages("nope") == []
+async def test_drain_defaults_id_for_legacy_entries(fake_redis: _FakeRedis) -> None:
+ """Entries written before ``PendingMessage.id`` existed are still sitting
+ in Redis when the new worker rolls out; they must validate (with a fresh
+ id) instead of being dropped as malformed."""
+ fake_redis.lists["copilot:pending:{legacy}"] = [
+ json.dumps({"content": "queued before the deploy"})
+ ]
+
+ drained = await drain_pending_messages("legacy")
+
+ assert [m.content for m in drained] == ["queued before the deploy"]
+ assert drained[0].id
+
+
# ── Mid-turn drain SSE hint ─────────────────────────────────────────
@@ -216,7 +231,8 @@ async def test_drain_emits_pending_drained_hint(
from backend.copilot.response_model import StreamPendingDrained
from backend.copilot.stream_registry import ActiveSession
- await push_pending_message("sessHint", PendingMessage(content="hi"))
+ queued = PendingMessage(content="hi")
+ await push_pending_message("sessHint", queued)
active = ActiveSession(
session_id="sessHint",
@@ -242,6 +258,9 @@ async def test_drain_emits_pending_drained_hint(
assert args[0] == "turn-xyz"
assert isinstance(args[1], StreamPendingDrained)
assert args[1].drainedCount == 1
+ # The hint carries the drained text + a stable id so the client can render
+ # the follow-up bubble at the drain point instead of guessing its content.
+ assert [(m.id, m.content) for m in args[1].messages] == [(queued.id, "hi")]
assert kwargs["session_id"] == "sessHint"
diff --git a/autogpt_platform/backend/backend/copilot/response_model.py b/autogpt_platform/backend/backend/copilot/response_model.py
index c4f5967f76..3bad4d3bf4 100644
--- a/autogpt_platform/backend/backend/copilot/response_model.py
+++ b/autogpt_platform/backend/backend/copilot/response_model.py
@@ -473,6 +473,13 @@ class StreamCompactionProgress(StreamBaseResponse):
return f"data: {json.dumps({'type': self.type.value, 'data': data})}\n\n"
+class StreamPendingDrainedMessage(BaseModel):
+ """One user follow-up carried by a ``data-pending-drained`` hint."""
+
+ id: str = Field(description="Stable id of the drained pending message")
+ content: str = Field(description="Raw text the user typed mid-turn")
+
+
class StreamPendingDrained(StreamBaseResponse):
"""Hint that the pending-message buffer was drained mid-turn.
@@ -483,12 +490,23 @@ class StreamPendingDrained(StreamBaseResponse):
for its slower backstop poll. ``drainedCount`` is informational only —
correctness comes from the client's re-read, so a dropped hint just
delays the chip→bubble swap until the next poll.
+
+ ``messages`` carries the drained text (with a stable id per message) so
+ the client can render the follow-up bubble at the exact point in the
+ stream where the backend injected it — between the tool chain that ran
+ before the drain and the work that follows it. Older clients ignore the
+ field; older backends omit it and the client falls back to its buffer
+ re-read.
"""
type: ResponseType = ResponseType.PENDING_DRAINED
drainedCount: int = Field(
default=0, description="How many messages were drained in this batch"
)
+ messages: list[StreamPendingDrainedMessage] = Field(
+ default_factory=list,
+ description="The drained messages, in enqueue order (oldest first)",
+ )
def to_sse(self) -> str:
"""Emit as an AI SDK v5 data part (``type='data-pending-drained'``)
@@ -496,6 +514,9 @@ class StreamPendingDrained(StreamBaseResponse):
it as an unknown chunk type."""
data = {
"type": self.type.value,
- "data": {"drainedCount": self.drainedCount},
+ "data": {
+ "drainedCount": self.drainedCount,
+ "messages": [m.model_dump() for m in self.messages],
+ },
}
return f"data: {json.dumps(data)}\n\n"
diff --git a/autogpt_platform/backend/backend/copilot/response_model_test.py b/autogpt_platform/backend/backend/copilot/response_model_test.py
index c993fe83d8..aa2a781005 100644
--- a/autogpt_platform/backend/backend/copilot/response_model_test.py
+++ b/autogpt_platform/backend/backend/copilot/response_model_test.py
@@ -9,6 +9,8 @@ from backend.copilot.response_model import (
ResponseType,
StreamCompactionProgress,
StreamModeChanged,
+ StreamPendingDrained,
+ StreamPendingDrainedMessage,
StreamToolDisplayAvailable,
ToolDisplayData,
)
@@ -82,3 +84,25 @@ class TestStreamCompactionProgress:
with pytest.raises(ValidationError):
StreamCompactionProgress(phase="done")
+
+
+class TestStreamPendingDrained:
+ def test_to_sse_carries_the_drained_messages(self):
+ """The client renders the mid-turn follow-up bubble straight from the
+ hint, so the text has to ride along inside the ``data`` envelope."""
+ evt = StreamPendingDrained(
+ drainedCount=1,
+ messages=[StreamPendingDrainedMessage(id="pm-1", content="also add tests")],
+ )
+ payload = json.loads(evt.to_sse().removeprefix("data: "))
+ assert payload == {
+ "type": "data-pending-drained",
+ "data": {
+ "drainedCount": 1,
+ "messages": [{"id": "pm-1", "content": "also add tests"}],
+ },
+ }
+
+ def test_messages_default_to_empty(self):
+ payload = json.loads(StreamPendingDrained(drainedCount=2).to_sse()[6:])
+ assert payload["data"] == {"drainedCount": 2, "messages": []}
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry_test.py b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
index cd983d3127..c374b97471 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry_test.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
@@ -579,14 +579,29 @@ def test_reconstruct_chunk_round_trips_pending_drained():
silently drops the hint and falls back to the slow backstop poll."""
import orjson
- from backend.copilot.response_model import StreamPendingDrained
+ from backend.copilot.response_model import (
+ StreamPendingDrained,
+ StreamPendingDrainedMessage,
+ )
- stored = orjson.loads(StreamPendingDrained(drainedCount=3).model_dump_json())
+ stored = orjson.loads(
+ StreamPendingDrained(
+ drainedCount=2,
+ messages=[
+ StreamPendingDrainedMessage(id="pm-1", content="also add tests"),
+ StreamPendingDrainedMessage(id="pm-2", content="and docs"),
+ ],
+ ).model_dump_json()
+ )
chunk = stream_registry._reconstruct_chunk(stored)
assert isinstance(chunk, StreamPendingDrained)
- assert chunk.drainedCount == 3
+ assert chunk.drainedCount == 2
+ assert [(m.id, m.content) for m in chunk.messages] == [
+ ("pm-1", "also add tests"),
+ ("pm-2", "and docs"),
+ ]
def test_reconstruct_mode_changed_chunk():