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(copilot): bundle of chat stream stability fixes (PK dedup, race, compaction, errors, chips) (#12948)
Zamil Majdy529c203e7fee
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.pyindex 7d1bf9c00b..768ebf2121 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py@@ -159,6 +159,21 @@ class StreamChatRequest(BaseModel):description="Model tier: 'standard' for the default model, 'advanced' for the highest-capability model. ""If None, the server applies per-user LD targeting then falls back to config.",)+ message_id: str | None = Field(+ default=None,+ max_length=64,+ description=(+ "Optional per-click UUID generated by the frontend. Becomes "+ "the persisted ``ChatMessage.id`` (PK). Frontend / network / "+ "RMQ-redelivery retransmits of the same logical send reuse "+ "the id, so the Postgres unique-constraint on the PK is the "+ "atomic dedup primitive: a duplicate INSERT returns a "+ "subscribe-only response without creating a parallel turn. "+ "Distinct user clicks (even with identical text) MUST send "+ "different ids — the frontend's per-click ``crypto.randomUUID()`` "+ "guarantees that."+ ),+ )class QueuePendingMessageRequest(BaseModel):@@ -987,10 +1002,21 @@ async def stream_chat_post(# Atomically append user message to session BEFORE creating task to avoid# race condition where GET_SESSION sees task as "running" but message isn't# saved yet. append_and_save_message returns None when a duplicate is- # detected — in that case skip enqueue to avoid processing the message twice.+ # detected — both the trailing-same-role check and the+ # ``ChatMessage.id`` PK collision (frontend-supplied per-click UUID+ # → Postgres unique constraint) feed into that signal. In either+ # case we skip enqueue and let the SSE generator subscribe to any+ # existing in-flight turn for this session.+ #+ # Note: the in-flight branch is handled at the top of this handler+ # via ``queue_pending_for_http`` (see ``is_turn_in_flight`` check+ # near the start) — that path returns early. Any request that+ # reaches this point is starting a fresh turn, so we always mint a+ # ``turn_id`` unless ``append_and_save_message`` reports a duplicate.is_duplicate_message = Falseif request.message:message = ChatMessage(+ id=request.message_id,role="user" if request.is_user_message else "assistant",content=request.message,)@@ -1010,9 +1036,8 @@ async def stream_chat_post(# For duplicate messages, skip create_session entirely so the infra-retry# client subscribes to the *existing* turn's Redis stream and receives the# in-progress executor output rather than an empty stream.- turn_id = ""- if not is_duplicate_message:- turn_id = str(uuid4())+ turn_id = "" if is_duplicate_message else str(uuid4())+ if turn_id:log_meta["turn_id"] = turn_idsession_create_start = time.perf_counter()await stream_registry.create_session(diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex a8866026ce..fb59fb5e4c 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -1396,35 +1396,10 @@ async def stream_chat_completion_baseline(len(drained_at_start_pending),session_id,)- # 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- )+ # NOTE: combine + per-row persist both happen *after*+ # ``inject_user_context`` below — see the comment near that+ # call for the ordering rationale. At this point ``message``+ # is still the original turn-starting send.# Select model based on the per-request tier toggle (standard / advanced).# The path (fast vs extended_thinking) is already decided — we're in the@@ -1599,6 +1574,43 @@ async def stream_chat_completion_baseline(else:logger.warning("[Baseline] No user message found for context injection")+ # Now that ``inject_user_context`` has wrapped + persisted the+ # original turn-starting send into its row, fold pending into the+ # model's current-turn input AND persist each pending message as+ # its own raw-text user row. Three things to keep in sync:+ #+ # 1. ``openai_messages`` (what the model sees this round) — append+ # each pending as a separate user entry, matching the mid-turn+ # drain pattern below.+ # 2. ``message`` (used for transcript/title fallbacks) — combine.+ # 3. ``session.messages`` + DB — one raw row per pending via+ # ``persist_pending_as_user_rows(transcript_builder=None)``.+ #+ # Order matters: persist must run after inject so inject targets+ # the routes.py-saved row, not a pending row.+ if drained_at_start_pending:+ # Persist FIRST. Only fold pending into the model's prompt+ # (``message`` for transcript fallback + ``openai_messages`` for+ # the live LLM call) when persistence succeeded — if the helper+ # rolled back and re-queued the pending into Redis, leaving+ # those untouched ensures the NEXT turn's drain doesn't+ # double-combine (re-queued pending + combined-from-this-turn)+ # into the model's context.+ persisted_ok = await persist_pending_as_user_rows(+ session,+ None,+ drained_at_start_pending,+ log_prefix="[Baseline]",+ )+ if persisted_ok:+ message = combine_pending_with_current(+ drained_at_start_pending,+ message,+ request_arrival_at=request_arrival_at,+ )+ for pm in drained_at_start_pending:+ openai_messages.append(format_pending_as_user_message(pm))+# 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>.diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.pyindex 263334d114..56e99622f7 100644--- a/autogpt_platform/backend/backend/copilot/db.py+++ b/autogpt_platform/backend/backend/copilot/db.py@@ -362,6 +362,21 @@ async def add_chat_message(return ChatMessage.from_db(message)+_CHAT_MESSAGE_PKEY_CONSTRAINT = "ChatMessage_pkey"+++def is_duplicate_chat_message_id_error(exc: BaseException) -> bool:+ """True iff *exc* is a ``UniqueViolationError`` on the ``ChatMessage``+ primary key — i.e. the caller supplied a duplicate ``id``.++ Used by ``append_and_save_message`` to distinguish the dedup signal+ from the unrelated ``(sessionId, sequence)`` collision that the+ streaming-callback race retries silently."""+ return isinstance(+ exc, UniqueViolationError+ ) and _CHAT_MESSAGE_PKEY_CONSTRAINT in str(exc)++async def add_chat_messages_batch(session_id: str,messages: list[dict[str, Any]],@@ -404,6 +419,12 @@ async def add_chat_messages_batch("createdAt": now,}+ # Optional caller-supplied PK (frontend per-click UUID+ # for user messages). When omitted Prisma generates+ # via ``@default(uuid())``.+ if msg.get("id") is not None:+ data["id"] = msg["id"]+# Add optional string fields — sanitize to strip# PostgreSQL-incompatible control characters.if msg.get("content") is not None:@@ -439,9 +460,17 @@ async def add_chat_messages_batch(# Return next sequence number for counter syncreturn start_sequence + len(messages)- except UniqueViolationError:+ except UniqueViolationError as e:+ # Two distinct unique constraints on ``ChatMessage`` can fire here:+ # - ``ChatMessage_pkey`` (the row ``id``): caller supplied a+ # duplicate PK — this is the idempotency signal, propagate+ # so the route layer can short-circuit.+ # - ``ChatMessage_sessionId_sequence_key``: the streaming loop+ # and a long-running callback raced on ``sequence`` — retry+ # with a freshly queried offset (existing behaviour).+ if is_duplicate_chat_message_id_error(e):+ raiseif attempt < max_retries - 1:- # Collision detected - query MAX(sequence)+1 and retry with correct offsetlogger.info(f"Collision detected for session {session_id} at sequence "f"{start_sequence}, querying DB for latest sequence"diff --git a/autogpt_platform/backend/backend/copilot/executor/manager.py b/autogpt_platform/backend/backend/copilot/executor/manager.pyindex 08baf73c05..8f1f0281cc 100644--- a/autogpt_platform/backend/backend/copilot/executor/manager.py+++ b/autogpt_platform/backend/backend/copilot/executor/manager.py@@ -85,7 +85,7 @@ class CoPilotExecutor(AppProcess):self._run_client = Noneself._task_locks: dict[str, ClusterLock] = {}- self._active_tasks_lock = threading.Lock()+ self._active_tasks_lock_obj: threading.Lock | None = None# ============ Main Entry Points (AppProcess interface) ============ #@@ -94,6 +94,13 @@ class CoPilotExecutor(AppProcess):logger.info(f"Pod assigned executor_id: {self.executor_id}")logger.info(f"Spawn max-{self.pool_size} workers...")+ # Materialise the active-tasks lock NOW, before any worker threads+ # exist, so subsequent multi-threaded reads of the lazy property+ # never race on its check-then-init pattern. ``__init__`` can't+ # do this because ``_thread.lock`` is not picklable and the+ # forkserver start method serialises the process object.+ self._active_tasks_lock # noqa: B018 — touch to materialise+pool_size_gauge.set(self.pool_size)self._update_metrics()start_http_server(settings.config.copilot_executor_port)@@ -502,6 +509,12 @@ class CoPilotExecutor(AppProcess):# ============ Lazy-initialized Properties ============ #+ @property+ def _active_tasks_lock(self) -> threading.Lock:+ if self._active_tasks_lock_obj is None:+ self._active_tasks_lock_obj = threading.Lock()+ return self._active_tasks_lock_obj+@propertydef cancel_thread(self) -> threading.Thread:if self._cancel_thread is None:diff --git a/autogpt_platform/backend/backend/copilot/model.py b/autogpt_platform/backend/backend/copilot/model.pyindex 75d00aceb3..06b5df9cc4 100644--- a/autogpt_platform/backend/backend/copilot/model.py+++ b/autogpt_platform/backend/backend/copilot/model.py@@ -18,6 +18,7 @@ from openai.types.chat.chat_completion_message_tool_call_param import (ChatCompletionMessageToolCallParam,Function,)+from prisma.errors import UniqueViolationErrorfrom prisma.models import ChatMessage as PrismaChatMessagefrom prisma.models import ChatSession as PrismaChatSessionfrom pydantic import BaseModel, PrivateAttr@@ -63,6 +64,14 @@ class ChatSessionMetadata(BaseModel):class ChatMessage(BaseModel):+ id: str | None = None+ """Stable per-send id. When supplied by the caller (e.g. AI SDK's+ ``messageId`` plumbed from the frontend's ``crypto.randomUUID()`` per+ user click), it becomes the Prisma row's ``id`` — the PK uniqueness+ constraint then makes the INSERT itself the atomic dedup primitive+ for retransmits / RMQ redeliveries / browser auto-retries. ``None``+ leaves Prisma to generate via ``@default(uuid())``."""+role: strcontent: str | None = Nonename: str | None = None@@ -78,6 +87,7 @@ class ChatMessage(BaseModel):def from_db(prisma_message: PrismaChatMessage) -> "ChatMessage":"""Convert a Prisma ChatMessage to a Pydantic ChatMessage."""return ChatMessage(+ id=prisma_message.id,role=prisma_message.role,content=prisma_message.content,name=prisma_message.name,@@ -697,6 +707,13 @@ async def _save_session_to_db(for msg in new_messages:messages_data.append({+ # ``id`` is optional — when the caller supplies one+ # (frontend-provided per-click UUID) it becomes the+ # Prisma row's PK and a duplicate POST raises+ # ``UniqueViolationError`` we catch as the dedup+ # signal. When None, Prisma generates via+ # ``@default(uuid())`` (existing behaviour).+ "id": msg.id,"role": msg.role,"content": msg.content,"name": msg.name,@@ -747,22 +764,35 @@ async def append_and_save_message(if session is None:raise ValueError(f"Session {session_id} not found")- # Idempotency: skip if the trailing block of same-role messages already- # contains this content. Uses is_message_duplicate which checks all+ # Idempotency layer 1: caller-supplied id collision. The frontend+ # sends a per-click UUID as ``message.id``; AI SDK / network / RMQ+ # retransmits of the *same* logical send carry the same id. Catch+ # the dup before the DB round-trip when we already see it cached+ # in-memory. The DB INSERT below is the authoritative atomic+ # check (PK uniqueness on ``ChatMessage.id``) — this is just the+ # fast path.+ if message.id is not None and any(+ existing.id == message.id for existing in session.messages+ ):+ return None # duplicate — caller should skip enqueue++ # Idempotency layer 2: trailing same-role-and-content collapse.+ # Catches retransmits from older clients that don't supply+ # ``message.id``, plus mid-turn re-sends of identical text from+ # the same role. Uses is_message_duplicate which checks all# consecutive trailing messages of the same role, not just [-1].#- # This collapses infra/nginx retries whether they land on the same pod- # (serialised by the Redis lock) or a different pod.- #- # Legit same-text messages are distinguished by the assistant turn- # between them: if the user said "yes", got a response, and says- # "yes" again, session.messages[-1] is the assistant reply, so the- # role check fails and the second message goes through normally.+ # Legit same-text messages across turns are distinguished by the+ # intervening assistant reply: if the user said "yes", got a+ # response, and says "yes" again, ``messages[-1]`` is the+ # assistant reply, so the role check fails and the second+ # message goes through normally.#- # Edge case: if a turn dies without writing any assistant message,- # the user's next send of the same text is blocked here permanently.- # The fix is to ensure failed turns always write an error/timeout- # assistant message so the session always ends on an assistant turn.+ # Edge case: if a turn dies without writing any assistant+ # message, the user's next send of the same text is blocked here+ # permanently. The fix is to ensure failed turns always write+ # an error/timeout assistant message so the session always ends+ # on an assistant turn.if message.content is not None and is_message_duplicate(session.messages, message.role, message.content):@@ -774,6 +804,19 @@ async def append_and_save_message(try:await _save_session_to_db(session, existing_message_count)except Exception as e:+ # Any save failure rolls back the optimistic append so the+ # in-memory ``session.messages`` stays consistent with what's+ # persisted (the function-scoped session is fresh per call,+ # but the cache write below would otherwise persist the+ # phantom row).+ session.messages.pop()+ # PK collision is the dedup signal — caller short-circuits to+ # subscribe-only. Other ``UniqueViolationError`` (e.g. a+ # ``(sessionId, sequence)`` race that exhausted the retry+ # loop in ``add_chat_messages_batch``) and unrelated+ # exceptions surface as ``DatabaseError``.+ if isinstance(e, UniqueViolationError) and "ChatMessage_pkey" in str(e):+ return Noneraise DatabaseError(f"Failed to persist message to session {session_id}") from ediff --git a/autogpt_platform/backend/backend/copilot/model_test.py b/autogpt_platform/backend/backend/copilot/model_test.pyindex b1dd1a6596..6f907f4072 100644--- a/autogpt_platform/backend/backend/copilot/model_test.py+++ b/autogpt_platform/backend/backend/copilot/model_test.py@@ -519,6 +519,26 @@ def test_all_same_role_messages():assert is_message_duplicate(msgs, "user", "new") is False+def test_chatmessage_id_field_round_trips():+ """``ChatMessage.id`` round-trips through ``model_dump``/``model_validate``+ so the route layer can serialise it to the DB INSERT and the cache can+ rebuild the in-memory list with stable PKs."""+ msg = ChatMessage(+ id="00000000-0000-4000-8000-000000000001", role="user", content="hi"+ )+ dumped = msg.model_dump()+ assert dumped["id"] == "00000000-0000-4000-8000-000000000001"+ rebuilt = ChatMessage.model_validate(dumped)+ assert rebuilt.id == msg.id+++def test_chatmessage_id_defaults_to_none():+ """Old code paths that don't set id stay null so Prisma's+ ``@default(uuid())`` generator fires server-side as before."""+ msg = ChatMessage(role="assistant", content="hi")+ assert msg.id is None++# --------------------------------------------------------------------------- ## maybe_append_user_message ## --------------------------------------------------------------------------- #diff --git a/autogpt_platform/backend/backend/copilot/pending_message_helpers.py b/autogpt_platform/backend/backend/copilot/pending_message_helpers.pyindex e1302267bf..59547408ca 100644--- a/autogpt_platform/backend/backend/copilot/pending_message_helpers.py+++ b/autogpt_platform/backend/backend/copilot/pending_message_helpers.py@@ -326,32 +326,42 @@ async def persist_session_safe(async def persist_pending_as_user_rows(session: "ChatSession",- transcript_builder: "TranscriptBuilder",+ transcript_builder: "TranscriptBuilder | None",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``.+ """Append ``pending`` as user rows to *session* (and optionally+ *transcript_builder*), persist, and roll back + re-queue if the+ persist silently failed.++ Used by both **mid-turn** drain (MCP tool boundaries; pass a real+ ``transcript_builder`` so the entries become additional user turns+ in the conversation flow) and **turn-start** drain (pass ``None``;+ the transcript already gets one entry for the whole turn at+ turn-end via ``append_user(combined_current_message)`` — appending+ each pending here as well would triple-count them in the next+ turn's ``--resume`` context).++ Differs across baseline/SDK paths only in (a) how the displayed+ string is derived 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.+ 1. Snapshot transcript (if any) + record the session.messages length.+ 2. Append one user row per pending message; mirror to the+ transcript only when *transcript_builder* is provided.3. ``persist_session_safe`` — swallowed errors mean no sequences getback-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.+ delete the appended rows, restore the transcript snapshot (if+ any), 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@@ -361,12 +371,15 @@ async def persist_pending_as_user_rows(return Truesession_anchor = len(session.messages)- transcript_snapshot = transcript_builder.snapshot()+ transcript_snapshot = (+ transcript_builder.snapshot() if transcript_builder is not None else None+ )for pm in pending:content = content_of(pm)session.messages.append(ChatMessage(role="user", content=content))- transcript_builder.append_user(content=content)+ if transcript_builder is not None:+ 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).@@ -395,7 +408,8 @@ async def persist_pending_as_user_rows(len(pending),)del session.messages[session_anchor:]- transcript_builder.restore(transcript_snapshot)+ if transcript_builder is not None and transcript_snapshot is not None:+ transcript_builder.restore(transcript_snapshot)if on_rollback is not None:on_rollback(session_anchor)# ``push_pending_message`` uses the bounded ``capped_rpush`` (LTRIMdiff --git a/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py b/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.pyindex 3b2d9b66f6..254a128c25 100644--- a/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/pending_message_helpers_test.py@@ -444,6 +444,147 @@ async def test_persist_pending_happy_path_appends_and_returns_true(push_mock.assert_not_awaited()+async def test_persist_pending_no_transcript_path_skips_transcript(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Turn-start drain: passing ``transcript_builder=None`` MUST NOT touch+ the transcript — it would triple-count pending entries in the next+ turn's ``--resume`` context (the combined ``current_message`` is what+ gets written to transcript at turn-end)."""+ 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 = []++ async def _fake_upsert(sess: Any) -> Any:+ for i, m in enumerate(sess.messages):+ m.sequence = i+ return sess++ monkeypatch.setattr(helpers_module, "upsert_chat_session", _fake_upsert)++ pending = [PM(content="chip-1"), PM(content="chip-2")]+ ok = await persist_pending_as_user_rows(session, None, pending, log_prefix="[T]")+ assert ok is True+ assert [m.content for m in session.messages] == ["chip-1", "chip-2"]+ assert [m.sequence for m in session.messages] == [0, 1]+++async def test_turn_start_drain_invariants_one_bubble_per_send(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Locks the chip-queue invariant that's broken five different ways+ in this PR's history.++ After a turn-start drain in the SDK / baseline service, the on-disk+ ``session.messages`` MUST satisfy all of:++ 1. **N+1 user rows total** (1 routes.py-saved row + N pending).+ 2. **Original row carries the wrapped envelopes** but NOT any+ pending text — the bubble in the UI shows just the original+ send (markdown strips the trusted ``<memory_context>`` /+ ``<user_context>`` blocks for display).+ 3. **Each pending row carries its raw chip text alone** — no+ envelopes, no neighbours' texts joined with ``\\n\\n``.+ 4. The COMBINED ``current_message`` is what the model sees this+ turn (and lands in the transcript at turn-end via+ ``append_user``) — separate from the per-row DB state.++ Past regressions this test would have caught:++ a. Persisting pending BEFORE ``inject_user_context`` (pending row+ received the full wrapped+combined string from inject's+ reverse-walk → bubble showed everything twice).+ b. Wrapping the COMBINED text in ``inject_user_context`` (original+ row carried both texts → chip text duplicated across two+ bubbles).+ c. Pre-PR behaviour: combining everything into the existing row+ (one bubble with ``"send\\n\\nchip"`` joined, chip's+ cardinality lost).+ """+ from backend.copilot.pending_message_helpers import (+ combine_pending_with_current,+ 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"+ # Routes.py pre-saved the original send at seq=0. inject_user_context+ # then wrapped its content with envelopes (simulated here).+ session.messages = [+ MagicMock(+ role="user",+ content=(+ "<memory_context>\nfacts\n</memory_context>\n\n"+ "<user_context>\nbio\n</user_context>\n\n"+ "can you sleep for 2 seconds then 3 seconds"+ ),+ sequence=0,+ )+ ]++ async def _fake_upsert(sess: Any) -> Any:+ # Simulate DB sequence back-fill on the rows that don't have one.+ next_seq = (+ max(+ (m.sequence for m in sess.messages if m.sequence is not None),+ default=-1,+ )+ + 1+ )+ for m in sess.messages:+ if m.sequence is None:+ m.sequence = next_seq+ next_seq += 1+ return sess++ monkeypatch.setattr(helpers_module, "upsert_chat_session", _fake_upsert)++ pending = [PM(content="oh sleep 4 secs in between", enqueued_at=0.0)]+ original = "can you sleep for 2 seconds then 3 seconds"++ # Step 1: combine for the model's prompt. Result is what the SDK CLI+ # sees as the current-turn user input.+ combined = combine_pending_with_current(pending, original, request_arrival_at=0.0)+ assert "can you sleep for 2 seconds then 3 seconds" in combined+ assert "oh sleep 4 secs in between" in combined++ # Step 2: persist pending as separate user rows; transcript untouched.+ ok = await persist_pending_as_user_rows(session, None, pending, log_prefix="[T]")+ assert ok is True++ # Invariant 1: N+1 user rows.+ assert len(session.messages) == 2++ # Invariant 2: original row keeps wrapped envelopes; no pending text.+ original_row = session.messages[0]+ assert "<memory_context>" in original_row.content+ assert "<user_context>" in original_row.content+ assert "can you sleep" in original_row.content+ assert "oh sleep 4 secs" not in original_row.content, (+ "regression: original row absorbed pending text — "+ "inject ran on the combined string instead of the original send"+ )++ # Invariant 3: pending row is raw chip text only.+ chip_row = session.messages[1]+ assert (+ chip_row.content == "oh sleep 4 secs in between"+ ), f"regression: chip row content drifted from raw text — got {chip_row.content!r}"+ assert "<memory_context>" not in chip_row.content+ assert "can you sleep" not in chip_row.content, (+ "regression: chip row absorbed original text — "+ "persist ran before inject, or the chip was joined with `\\n\\n`"+ )++@pytest.mark.asyncioasync def test_persist_pending_rollback_when_sequence_missing(monkeypatch: pytest.MonkeyPatch,diff --git a/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.py b/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.pyindex 5b99296314..a923aae0ae 100644--- a/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.py@@ -474,8 +474,18 @@ class TestCompressMessages:assert compacted is True@pytest.mark.asyncio- async def test_scenario_o_run_compression_exception_returns_originals(self):- """Scenario O: _run_compression raises → return original messages, False."""+ async def test_scenario_o_run_compression_exception_drops_history(self):+ """Scenario O: when both LLM AND truncation paths inside+ ``_run_compression`` fail, ``_compress_messages`` MUST drop history+ entirely instead of returning the originals.++ Returning the originals would feed the same too-long payload back+ into the retry loop, guaranteeing another ``Prompt is too long`` and+ burning the retry budget without progress (regression guard for+ SENTRY-1207's persistent recurrence: a session whose stored history+ exceeds the model's context window cannot be rescued by re-trying+ with the same history).+ """msgs = [ChatMessage(role="user", content="q"),ChatMessage(role="assistant", content="a"),@@ -487,8 +497,10 @@ class TestCompressMessages:):result, compacted = await _compress_messages(msgs)- assert result == msgs- assert compacted is False+ # History dropped, marked as compacted so callers know reduction+ # occurred (current message goes alone, smallest possible payload).+ assert result == []+ assert compacted is True@pytest.mark.asyncioasync def test_compaction_messages_filtered_before_compression(self):@@ -517,10 +529,19 @@ class TestRetryTargetTokens:assert _RETRY_TARGET_TOKENS[0] == 50_000- def test_second_retry_uses_second_slot(self):- from backend.copilot.sdk.service import _RETRY_TARGET_TOKENS+ def test_second_retry_lowered_to_bare_message_floor(self):+ """The second retry's target must be at or below the bare-message+ floor so ``_build_query_message`` triggers its escape hatch and+ sends the current message alone. Without this, sessions whose+ stored history exceeds the model's context window can exhaust all+ retries without ever falling through to the floor (regression guard+ for SENTRY-1207's persistent ``Prompt is too long``)."""+ from backend.copilot.sdk.service import (+ _BARE_MESSAGE_TOKEN_FLOOR,+ _RETRY_TARGET_TOKENS,+ )- assert _RETRY_TARGET_TOKENS[1] == 15_000+ assert _RETRY_TARGET_TOKENS[1] <= _BARE_MESSAGE_TOKEN_FLOORdef test_second_slot_smaller_than_first(self):from backend.copilot.sdk.service import _RETRY_TARGET_TOKENSdiff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 202783541d..6cc9878bb6 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -4,6 +4,7 @@import asyncioimport base64+import functoolsimport jsonimport loggingimport os@@ -139,6 +140,7 @@ from .openrouter_cost import record_turn_cost_from_openrouterfrom .response_adapter import SDKResponseAdapterfrom .security_hooks import create_security_hooksfrom .tool_adapter import (+ MCP_TOOL_PREFIX,create_copilot_mcp_server,get_copilot_tool_names,get_sdk_disallowed_tools,@@ -442,7 +444,7 @@ class _StreamContext:# compress_context applies progressively more aggressive reduction:# LLM summarize → content truncate → middle-out delete → first/last trim.# Index 0 = first retry, 1 = second retry; last value applies beyond that.-_RETRY_TARGET_TOKENS: tuple[int, ...] = (50_000, 15_000)+_RETRY_TARGET_TOKENS: tuple[int, ...] = (50_000, 5_000)# Below this token budget the model context is so tight that injecting any# conversation history would likely exceed the limit regardless of content.@@ -1304,6 +1306,58 @@ def _write_cli_session_to_disk(return False+def delete_stale_cli_session_file(+ sdk_cwd: str,+ session_id: str,+ log_prefix: str,+) -> bool:+ """Delete the local CLI session file at the predictable path.++ Used so a subsequent CLI invocation with ``--session-id`` (no ``--resume``)+ doesn't trip ``"Session ID already in use"``. Path-traversal guard:+ rejects paths outside the CLI projects base.++ Returns True if a file was deleted, False otherwise (missing, traversal,+ or unlink failure).+ """+ real_path = os.path.realpath(cli_session_path(sdk_cwd, session_id))+ if not real_path.startswith(projects_base() + os.sep):+ # Mirror ``_write_cli_session_to_disk``'s defense-in-depth: log+ # rather than fail silently when the resolved path escapes the+ # projects base. In normal operation this is unreachable+ # (session_id is a server-generated UUID and ``cli_session_path``+ # is deterministic), so a hit indicates a config or tampering+ # issue that's worth surfacing.+ logger.warning(+ "%s CLI session delete path outside projects base: %s",+ log_prefix,+ os.path.basename(real_path),+ )+ return False+ # Direct unlink — no exists() check (avoids TOCTOU with the file being+ # deleted by another process between check and unlink).+ try:+ Path(real_path).unlink()+ logger.info(+ "%s Removed stale local CLI session file at %s",+ log_prefix,+ os.path.basename(real_path),+ )+ return True+ except FileNotFoundError:+ return False+ except OSError as unlink_err:+ # Sanitise log: basename + strerror only (no full path / no raw+ # exception which can echo absolute paths back in some libc errors).+ logger.warning(+ "%s Failed to remove stale local CLI session file %s: %s",+ log_prefix,+ os.path.basename(real_path),+ unlink_err.strerror or type(unlink_err).__name__,+ )+ return False++def read_cli_session_from_disk(sdk_cwd: str,session_id: str,@@ -1571,11 +1625,22 @@ async def _compress_messages(target_tokens=target_tokens,)except Exception as exc:- # Guard against timeouts or unexpected errors in compression —- # return the original messages so the caller can proceed without- # compaction rather than propagating the error to the retry loop.- logger.warning("[SDK] _compress_messages failed, returning originals: %s", exc)- return messages, False+ # Both the LLM summarize path AND the truncation fallback inside+ # ``_run_compression`` failed (timeouts, tokenization error, etc.).+ # Returning the originals here would silently feed the same+ # too-long payload back into the retry loop, guaranteeing another+ # ``Prompt is too long`` and burning the retry budget for no+ # progress. Drop history entirely instead — the caller will fall+ # back to the bare current message, which is the tightest+ # compression we can offer without an LLM, and is the only thing+ # that can definitively recover a session whose stored history+ # exceeds the model's context window.+ logger.warning(+ "[SDK] _compress_messages failed — dropping history to bare"+ " message to guarantee retry progress: %s",+ exc,+ )+ return [], Trueif result.was_compacted:logger.info(@@ -2246,6 +2311,29 @@ class _EmptyToolBreakResult:error_code: str | None # Error code (if tripped)+def _no_arg_tool_names() -> frozenset[str]:+ """Tool names whose schema declares zero arguments (required + properties).++ A ``ToolUseBlock`` with ``input == {}`` for one of these is a *legitimate*+ invocation of a no-arg tool, NOT the model-saturation failure mode the+ empty-tool-call breaker targets (sessions where the model emits ``{}`` for+ EVERY arg-needing tool because context-saturation broke argument+ serialization). Cached because the registry is module-level and immutable+ after import. Includes both bare and MCP-prefixed names because+ ``ToolUseBlock.name`` carries the MCP prefix when the tool is registered+ through the copilot MCP server.+ """+ from backend.copilot.tools import TOOL_REGISTRY++ bare = {+ name+ for name, tool in TOOL_REGISTRY.items()+ if not (tool.parameters.get("required") or tool.parameters.get("properties"))+ }+ return frozenset(bare | {f"{MCP_TOOL_PREFIX}{name}" for name in bare})++def _check_empty_tool_breaker(sdk_msg: object,consecutive: int,@@ -2260,12 +2348,17 @@ def _check_empty_tool_breaker(if not isinstance(sdk_msg, AssistantMessage):return _EmptyToolBreakResult(consecutive, False, None, None, None)+ no_arg = _no_arg_tool_names()empty_tools = [- b.name for b in sdk_msg.content if isinstance(b, ToolUseBlock) and not b.input+ b.name+ for b in sdk_msg.content+ if isinstance(b, ToolUseBlock) and not b.input and b.name not in no_arg]if not empty_tools:# Reset on any non-empty-tool AssistantMessage (including text-only- # messages — any() over empty content is False).+ # messages — any() over empty content is False). Legitimate no-arg+ # tools (e.g. ``get_agent_building_guide``) also reset the counter+ # since they're a normal model action, not the saturation failure.return _EmptyToolBreakResult(0, False, None, None, None)consecutive += 1@@ -3155,22 +3248,7 @@ async def _restore_cli_session_for_turn(# session_id with "Session ID already in use". T1 may have# left a valid file at this path; we clear it so the fallback# path (session_id= without --resume) can create a new session.- _stale_path = os.path.realpath(cli_session_path(sdk_cwd, session_id))- if Path(_stale_path).exists() and _stale_path.startswith(- projects_base() + os.sep- ):- try:- Path(_stale_path).unlink()- logger.debug(- "%s Removed stale local CLI session file for clean fallback",- log_prefix,- )- except OSError as _unlink_err:- logger.debug(- "%s Failed to remove stale local session file: %s",- log_prefix,- _unlink_err,- )+ delete_stale_cli_session_file(sdk_cwd, session_id, log_prefix)if cli_restore is not None:result.transcript_content = stripped@@ -3792,37 +3870,10 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssueslog_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.- 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- )+ # NOTE: combining and per-row persistence both happen *after*+ # ``inject_user_context`` below — see the comment near that+ # call. At this point ``current_message`` is still the+ # original turn-starting send (no pending text yet).if not current_message.strip():yield StreamError(@@ -3869,6 +3920,46 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesif prefixed_message is not None:current_message = prefixed_message+ # Now that ``inject_user_context`` has wrapped + persisted the+ # ORIGINAL turn-starting send into its row, fold any pending+ # chips into the prompt the model sees AND persist each pending+ # message as its own raw-text user row in the DB. Order matters+ # because:+ #+ # - inject targets the last user row (= routes.py-saved row at+ # this point) and writes envelopes + the value passed in.+ # If we'd combined first, the wrapped row would carry the+ # chip text too, and persisting the chip as a separate row+ # below would make the chip appear twice in the UI.+ # - inject targets the last user row. If we'd persisted+ # pending rows first, inject would land on a pending row+ # instead of the original send.+ #+ # ``transcript_builder=None``: the model still sees the combined+ # text below as a single user turn that gets written to the+ # transcript at turn-end via ``append_user(current_message)``.+ # Adding each pending to the transcript here would triple-count+ # them in the next turn's ``--resume`` context.+ if pending_messages:+ # Persist FIRST. Only fold pending into the model's prompt+ # when persistence succeeded — if the helper rolled back and+ # re-queued the pending into Redis, leaving ``current_message``+ # untouched ensures the NEXT turn's drain doesn't double-+ # combine (re-queued pending + combined-from-this-turn) into+ # the model's context.+ persisted_ok = await persist_pending_as_user_rows(+ session,+ None,+ pending_messages,+ log_prefix=log_prefix,+ )+ if persisted_ok:+ current_message = combine_pending_with_current(+ pending_messages,+ current_message,+ request_arrival_at=request_arrival_at,+ )+query_message, was_compacted = await _build_query_message(current_message,session,@@ -4016,21 +4107,21 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesif ctx.use_resume and ctx.resume_file:sdk_options_kwargs_retry["resume"] = ctx.resume_filesdk_options_kwargs_retry.pop("session_id", None)- elif "session_id" in sdk_options_kwargs:- # Initial invocation used session_id (T1 or mode-switch- # T1): keep it so the CLI writes the session file to the- # predictable path for upload_transcript(). Storage is- # ephemeral per invocation, so no "Session ID already in- # use" conflict occurs — no prior file was restored.- sdk_options_kwargs_retry.pop("resume", None)- sdk_options_kwargs_retry["session_id"] = session_idelse:- # T2+ retry without --resume: initial invocation used- # --resume, which restored the T1 session file to local- # storage. Re-using session_id without --resume would- # fail with "Session ID already in use".+ # No --resume on this retry. Whether we entered with+ # ``session_id`` (T1, mode-switch) or with ``--resume`` (T2+),+ # we want the recovery turn's CLI write to land on the+ # predictable ``cli_session_path(.., session_id)`` so the+ # post-turn ``upload_transcript`` actually picks up the+ # rescued (compacted) content. Without this, a T2+ retry+ # would drop session_id to dodge "Session ID already in use",+ # write to a random path, and the upload would silently grab+ # the stale pre-failure file — leaving GCS bloated and+ # guaranteeing the next turn re-trips prompt-too-long.+ if sdk_cwd:+ delete_stale_cli_session_file(sdk_cwd, session_id, log_prefix)sdk_options_kwargs_retry.pop("resume", None)- sdk_options_kwargs_retry.pop("session_id", None)+ sdk_options_kwargs_retry["session_id"] = session_id# Recompute system_prompt for retry — the preset is safe on# every turn (requires CLI ≥ 2.1.98, installed in the Docker# image and configured via CHAT_CLAUDE_AGENT_CLI_PATH).diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex 8683ca7504..48b0ca3bea 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -736,17 +736,20 @@ def _build_retry_sdk_options(ctx_resume_file: str | None,session_id: str,) -> dict:- """Mirror the retry branch in stream_chat_completion_sdk."""+ """Mirror the retry branch in stream_chat_completion_sdk.++ Production-side companion: ``delete_stale_cli_session_file`` is invoked+ on every non-resume retry path so the CLI doesn't trip "Session ID+ already in use" when we re-attach ``session_id``. This helper only+ mirrors the kwarg shape (file-system side effect is tested separately).+ """retry: dict = dict(initial_kwargs)if ctx_use_resume and ctx_resume_file:retry["resume"] = ctx_resume_fileretry.pop("session_id", None)- elif "session_id" in initial_kwargs:- retry.pop("resume", None)- retry["session_id"] = session_idelse:retry.pop("resume", None)- retry.pop("session_id", None)+ retry["session_id"] = session_idreturn retry@@ -818,12 +821,21 @@ class TestSdkSessionIdSelection:assert retry.get("session_id") == self.SESSION_IDassert "resume" not in retry- def test_retry_removes_session_id_for_t2_plus(self):- """Retry for T2+ (initial used --resume) removes session_id to avoid conflict."""+ def test_retry_keeps_session_id_for_t2_plus(self):+ """Retry for T2+ now keeps session_id so the recovery turn writes to+ the predictable ``cli_session_path`` and gets uploaded. Production+ clears the stale local file via ``delete_stale_cli_session_file``+ before this branch runs to dodge "Session ID already in use".++ Regression guard for SENTRY-1207: previously this branch dropped+ session_id, the CLI wrote to a random path, and the post-turn+ upload silently grabbed the stale pre-failure file — so GCS stayed+ bloated and every subsequent turn re-tripped prompt-too-long.+ """initial = _build_sdk_options(True, self.SESSION_ID, self.SESSION_ID)# T2+ retry where context reduction dropped --resumeretry = _build_retry_sdk_options(initial, False, None, self.SESSION_ID)- assert "session_id" not in retry+ assert retry.get("session_id") == self.SESSION_IDassert "resume" not in retrydef test_retry_t2_with_resume_sets_resume(self):@@ -1297,3 +1309,134 @@ class TestCompactionTargetTokens:# Target derived from the RUNTIME model, not the compactor model.assert captured["target_tokens"] == 12345+++# ---------------------------------------------------------------------------+# delete_stale_cli_session_file — clears a leftover local session file so a+# subsequent --session-id (no --resume) invocation doesn't trip "Session ID+# already in use". Critical for the prompt-too-long retry path.+# ---------------------------------------------------------------------------+++class TestDeleteStaleCliSessionFile:+ def test_deletes_file_when_present(self, tmp_path) -> None:+ from backend.copilot.sdk.service import delete_stale_cli_session_file++ sdk_cwd = str(tmp_path / "cwd")+ session_id = "sess-deadbeef"++ with (+ patch(+ "backend.copilot.sdk.service.cli_session_path",+ return_value=str(tmp_path / "session.jsonl"),+ ),+ patch(+ "backend.copilot.sdk.service.projects_base",+ return_value=str(tmp_path),+ ),+ ):+ target = tmp_path / "session.jsonl"+ target.write_text("{}\n")++ removed = delete_stale_cli_session_file(sdk_cwd, session_id, "[t]")++ assert removed is True+ assert not target.exists()++ def test_returns_false_when_file_missing(self, tmp_path) -> None:+ from backend.copilot.sdk.service import delete_stale_cli_session_file++ with (+ patch(+ "backend.copilot.sdk.service.cli_session_path",+ return_value=str(tmp_path / "missing.jsonl"),+ ),+ patch(+ "backend.copilot.sdk.service.projects_base",+ return_value=str(tmp_path),+ ),+ ):+ removed = delete_stale_cli_session_file("/cwd", "sess", "[t]")++ assert removed is False++ def test_path_traversal_guard_rejects_outside_projects_base(self, tmp_path) -> None:+ """Refuse to delete files outside the projects base, even if they exist."""+ from backend.copilot.sdk.service import delete_stale_cli_session_file++ outside = tmp_path / "outside.jsonl"+ outside.write_text("data")+ projects = tmp_path / "projects"+ projects.mkdir()++ with (+ patch(+ "backend.copilot.sdk.service.cli_session_path",+ return_value=str(outside),+ ),+ patch(+ "backend.copilot.sdk.service.projects_base",+ return_value=str(projects),+ ),+ ):+ removed = delete_stale_cli_session_file("/cwd", "sess", "[t]")++ # File was outside projects base — guard rejected, file untouched.+ assert removed is False+ assert outside.exists()+++# ---------------------------------------------------------------------------+# Empty-tool-call circuit breaker — must NOT false-positive on no-arg tools+# ---------------------------------------------------------------------------+++class TestEmptyToolCallNoArgException:+ """No-arg copilot tools (e.g. ``get_agent_building_guide``) legitimately+ invoke with ``input == {}``. The breaker must NOT count them as the+ saturation-failure mode it targets — that would be a false positive+ every time the model calls such a tool, polluting Sentry and (after 5+ consecutive) aborting the stream wrongly.+ """++ def test_no_arg_tool_names_includes_mcp_prefixed_form(self) -> None:+ from backend.copilot.sdk.service import _no_arg_tool_names++ names = _no_arg_tool_names()+ # Both bare and MCP-prefixed forms — the SDK's ToolUseBlock.name+ # carries the MCP prefix when registered through the copilot+ # MCP server.+ assert "get_agent_building_guide" in names+ assert "mcp__copilot__get_agent_building_guide" in names++ def test_no_arg_tool_with_empty_input_does_not_trip_counter(self) -> None:+ """Regression guard: a single empty-input call to a registered+ no-arg tool must reset the consecutive counter (returning 0),+ same as a non-empty AssistantMessage. This proves+ ``get_agent_building_guide`` mid-session won't trip the+ circuit-breaker after 5 invocations across a long session."""+ from claude_agent_sdk import AssistantMessage, ToolUseBlock++ from backend.copilot.sdk.service import _check_empty_tool_breaker++ msg = AssistantMessage(+ content=[+ ToolUseBlock(+ id="tu_1",+ name="mcp__copilot__get_agent_building_guide",+ input={},+ )+ ],+ model="anthropic/claude-sonnet-4-6",+ )+ # ctx + state aren't read on the no-arg fast-path — pass MagicMocks.+ ctx = MagicMock()+ ctx.log_prefix = "[t]"+ state = MagicMock()++ result = _check_empty_tool_breaker(msg, consecutive=4, ctx=ctx, state=state)++ # Consecutive counter MUST reset (no-arg tool is a normal action,+ # not the saturation failure).+ assert result.count == 0+ assert result.tripped is Falsediff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.tsindex f716ae5d56..cbccc16734 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts@@ -98,7 +98,7 @@ beforeEach(() => {mockUseCopilotStream.mockReturnValue(defaultStream);mockUseCopilotPendingChips.mockReturnValue({queuedMessages: [],- appendChip: vi.fn(),+ queueMessage: vi.fn(),});mockUseGetV2GetSession.mockReturnValue({data: undefined,@@ -546,7 +546,7 @@ describe("useBuilderChatPanel", () => {});it("queues a follow-up via the helper when onSend is called while streaming", async () => {- const appendChip = vi.fn();+ const queueMessage = vi.fn();const sendMessage = vi.fn();mockUseCopilotStream.mockReturnValue({...defaultStream,@@ -555,7 +555,7 @@ describe("useBuilderChatPanel", () => {});mockUseCopilotPendingChips.mockReturnValue({queuedMessages: [],- appendChip,+ queueMessage,});createBuilderSession.mockResolvedValue({status: 200,@@ -576,7 +576,7 @@ describe("useBuilderChatPanel", () => {});await result.current.onSend("queued msg");// The message must be appended as a chip AND not sent directly.- expect(appendChip).toHaveBeenCalledWith("queued msg");+ expect(queueMessage).toHaveBeenCalledWith("queued msg");expect(sendMessage).not.toHaveBeenCalled();});diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.tsindex 0eea11ded7..1776eec035 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts@@ -138,7 +138,7 @@ export function useBuilderChatPanel({copilotModel: undefined,});- const { queuedMessages, appendChip } = useCopilotPendingChips({+ const { queuedMessages, queueMessage } = useCopilotPendingChips({sessionId,status,messages,@@ -469,7 +469,7 @@ export function useBuilderChatPanel({if (!sessionId) return;const isInFlight = status === "streaming" || status === "submitted";if (isInFlight) {- appendChip(trimmed);+ queueMessage(trimmed);try {const { queueFollowUpMessage } = await import("@/app/(platform)/copilot/helpers/queueFollowUpMessage"diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamTransport.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamTransport.test.tsnew file mode 100644index 0000000000..0052928179--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamTransport.test.ts@@ -0,0 +1,103 @@+import { describe, expect, it, vi } from "vitest";++vi.mock("@/services/environment", () => ({+ environment: {+ getAGPTServerBaseUrl: () => "http://test.local",+ },+}));++vi.mock("../helpers", () => ({+ getCopilotAuthHeaders: async () => ({ "x-test": "auth" }),+}));++import { createCopilotTransport } from "../copilotStreamTransport";++const UUID_RE =+ /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;++function makeRefs() {+ return {+ copilotModeRef: { current: undefined },+ copilotModelRef: { current: undefined },+ };+}++function lastMessage(text: string) {+ return [+ {+ id: "ai-sdk-generated-id",+ role: "user" as const,+ parts: [{ type: "text" as const, text }],+ },+ ];+}++describe("copilotStreamTransport.prepareSendMessagesRequest", () => {+ it("attaches a freshly generated UUIDv4 as message_id on every body", async () => {+ const transport = createCopilotTransport({+ sessionId: "sess-1",+ ...makeRefs(),+ });+ // The transport stores the prepared closure on the underlying object;+ // exercise it directly through the public ChatTransport surface.+ const body1 = await (+ transport as unknown as {+ prepareSendMessagesRequest: (args: {+ messages: ReturnType<typeof lastMessage>;+ }) => Promise<{ body: { message_id?: string } }>;+ }+ ).prepareSendMessagesRequest({ messages: lastMessage("hi") });+ expect(body1.body.message_id).toMatch(UUID_RE);+ });++ it(+ "emits a different message_id per call so distinct user clicks dedupe " ++ "as distinct sends server-side",+ async () => {+ const transport = createCopilotTransport({+ sessionId: "sess-1",+ ...makeRefs(),+ });+ const prep = (+ transport as unknown as {+ prepareSendMessagesRequest: (args: {+ messages: ReturnType<typeof lastMessage>;+ }) => Promise<{ body: { message_id?: string } }>;+ }+ ).prepareSendMessagesRequest;++ const a = await prep({ messages: lastMessage("hi") });+ const b = await prep({ messages: lastMessage("hi") });+ expect(a.body.message_id).not.toBe(b.body.message_id);+ },+ );++ it(+ "does NOT pass message_id via AI SDK messageId on sendMessage — " ++ "messageId is replace-mode and would break optimistic render",+ async () => {+ // This is a contract check: the transport reads ``last.id`` (AI SDK's+ // auto-generated id) but must NOT use it as the dedup key, because+ // AI SDK's optimistic-render path treats ``messageId`` on+ // ``sendMessage`` as "edit the existing message with that id". Since+ // useSendMessage no longer threads a custom messageId, ``last.id`` is+ // an SDK-internal nanoid that's unrelated to our dedup UUID.+ const transport = createCopilotTransport({+ sessionId: "sess-1",+ ...makeRefs(),+ });+ const prep = (+ transport as unknown as {+ prepareSendMessagesRequest: (args: {+ messages: ReturnType<typeof lastMessage>;+ }) => Promise<{ body: { message_id?: string } }>;+ }+ ).prepareSendMessagesRequest;++ const out = await prep({ messages: lastMessage("hi") });+ expect(out.body.message_id).toMatch(UUID_RE);+ // ``last.id`` is "ai-sdk-generated-id" — must NOT be used as message_id.+ expect(out.body.message_id).not.toBe("ai-sdk-generated-id");+ },+ );+});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.tsindex 43413770f4..35ee934bf8 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@@ -370,7 +370,7 @@ describe("useCopilotPage — onSend queue-in-flight path", () => {"sess-1","follow-up",);- // appendChip should have been called, bringing the chip into queuedMessages.+ // queueMessage should have been called, bringing the entry into queuedMessages.await waitFor(() => {expect(result.current.queuedMessages).toContain("follow-up");});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.tsindex 7c62121124..0a0841f71c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPendingChips.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPendingChips.test.ts@@ -106,7 +106,7 @@ describe("useCopilotPendingChips", () => {});});- it("appendChip adds a chip locally without hitting backend", () => {+ it("queueMessage adds a chip locally without hitting backend", () => {const setMessages = vi.fn();const { result } = renderHook(() =>useCopilotPendingChips({@@ -118,17 +118,14 @@ describe("useCopilotPendingChips", () => {);act(() => {- result.current.appendChip("hello");+ result.current.queueMessage("hello");});expect(result.current.queuedMessages).toEqual(["hello"]);});- it("promotes chips to a bubble when a second new assistant appears (auto-continue)", () => {+ it("promotes one bubble per chip 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({@@ -145,12 +142,12 @@ describe("useCopilotPendingChips", () => {messages: [user("u1"), assistant("a1", "turn1")],});- // User queues a chip while turn 1 is still streaming.+ // User queues two distinct chips while turn 1 is still streaming.act(() => {- result.current.appendChip("followup");+ result.current.queueMessage("followup-a");+ result.current.queueMessage("followup-b");});- // Clear any prior setMessages calls so we only inspect the promotion.setMessages.mockClear();// A SECOND new assistant id appears → auto-continue detected.@@ -162,7 +159,6 @@ describe("useCopilotPendingChips", () => {],});- // setMessages was called with an updater that inserts the promoted bubble.const promoteCall = setMessages.mock.calls.find(([fn]) => typeof fn === "function",);@@ -171,25 +167,28 @@ describe("useCopilotPendingChips", () => {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) =>+ // One bubble per chip — cardinality preserved.+ const promoted = after.filter((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",+ expect(promoted).toHaveLength(2);+ expect(+ promoted.map((m) => (m.parts![0] as { text?: string }).text),+ ).toEqual(["followup-a", "followup-b"]);++ // Both bubbles inserted before a2.+ const a2Idx = after.findIndex((m) => m.id === "a2");+ const promotedIndices = promoted.map((m) =>+ after.findIndex((x) => x.id === m.id),);+ expect(promotedIndices.every((i) => i < a2Idx)).toBe(true);- // And chips have been cleared.+ // Chips 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"] },@@ -217,7 +216,6 @@ describe("useCopilotPendingChips", () => {});rerender({ status: "submitted" });- // Backend really drained — count is 0.peekMock.mockResolvedValue({status: 200,data: { count: 0, messages: [] },@@ -231,8 +229,6 @@ describe("useCopilotPendingChips", () => {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"] },@@ -260,8 +256,6 @@ describe("useCopilotPendingChips", () => {});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" });@@ -286,10 +280,9 @@ describe("useCopilotPendingChips", () => {);act(() => {- result.current.appendChip("survives");+ result.current.queueMessage("survives");});- // Simulate a transient network failure on the peek.peekMock.mockRejectedValue(new Error("network blip"));await act(async () => {@@ -298,7 +291,6 @@ describe("useCopilotPendingChips", () => {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;@@ -308,7 +300,7 @@ describe("useCopilotPendingChips", () => {expect(promoteCall).toBeUndefined();});- it("mid-turn poll promotes drained chips when backend count drops", async () => {+ it("mid-turn poll promotes one bubble per drained chip", async () => {vi.useFakeTimers();const setMessages = vi.fn();const { result } = renderHook(() =>@@ -321,11 +313,10 @@ describe("useCopilotPendingChips", () => {);act(() => {- result.current.appendChip("chipA");- result.current.appendChip("chipB");+ result.current.queueMessage("chipA");+ result.current.queueMessage("chipB");});- // Backend now reports count=0 (drained by MCP wrapper).peekMock.mockResolvedValue({status: 200,data: { count: 0, messages: [] },@@ -333,12 +324,10 @@ describe("useCopilotPendingChips", () => {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[])([]);@@ -346,25 +335,130 @@ describe("useCopilotPendingChips", () => {});expect(promotedCall).toBeDefined();- // And — crucially — the promoted bubble is inserted BEFORE the- // trailing streaming assistant, not after. The AI SDK's ``useChat``- // streams SSE deltas into ``messages[-1]``; if the last message is- // a user bubble instead of the still-streaming assistant, every- // subsequent chunk lands in the wrong slot and the UI freezes until- // a page refresh.const streamingUpdater = promotedCall![0] as (prev: UIMessage[],) => UIMessage[];const priorMessages = [user("u1"), assistant("a1", "streaming...")];const afterPromotion = streamingUpdater(priorMessages);- expect(afterPromotion).toHaveLength(3);++ // Two chips → two bubbles, both inserted before the streaming assistant.+ expect(afterPromotion).toHaveLength(4);const lastIdx = afterPromotion.length - 1;expect(afterPromotion[lastIdx].role).toBe("assistant");expect(afterPromotion[lastIdx].id).toBe("a1");- expect(afterPromotion[lastIdx - 1].id.startsWith("promoted-midturn-")).toBe(- true,+ const promoted = afterPromotion.filter((m) =>+ m.id.startsWith("promoted-midturn-"),);- // And remaining chips cleared.+ expect(promoted).toHaveLength(2);+ expect(+ promoted.map((m) => (m.parts![0] as { text?: string }).text),+ ).toEqual(["chipA", "chipB"]);+expect(result.current.queuedMessages).toEqual([]);});++ it("mid-turn poll: a peek that resolves after the user switches sessions does not promote into the new session", async () => {+ vi.useFakeTimers();+ const setMessagesA = vi.fn();+ const setMessagesB = vi.fn();+ type Props = { sessionId: string; setMessages: typeof setMessagesA };+ const { result, rerender } = renderHook<+ ReturnType<typeof useCopilotPendingChips>,+ Props+ >(+ ({ sessionId, setMessages }) =>+ useCopilotPendingChips({+ sessionId,+ status: "streaming",+ messages: [user("u1"), assistant("a1")],+ setMessages,+ }),+ {+ initialProps: { sessionId: "sess-A", setMessages: setMessagesA },+ },+ );++ act(() => {+ result.current.queueMessage("chipA");+ });++ // Poll fires for sess-A but resolves AFTER we've switched to sess-B.+ let resolvePeek!: (value: unknown) => void;+ peekMock.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ resolvePeek = resolve;+ }),+ );++ await act(async () => {+ vi.advanceTimersByTime(2_000);+ });++ // User switches sessions.+ rerender({ sessionId: "sess-B", setMessages: setMessagesB });++ // Old poll resolves now — backend says count=0 — but we've already+ // moved on to sess-B. The session-id guard must short-circuit so+ // setMessagesA is never called for the promotion path.+ setMessagesA.mockClear();+ await act(async () => {+ resolvePeek({ status: 200, data: { count: 0, messages: [] } });+ await Promise.resolve();+ await Promise.resolve();+ });++ const promotedCallA = setMessagesA.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(promotedCallA).toBeUndefined();+ });++ it("mid-turn poll: chip appended during in-flight poll survives the drain", async () => {+ vi.useFakeTimers();+ const setMessages = vi.fn();+ const { result } = renderHook(() =>+ useCopilotPendingChips({+ sessionId: "s",+ status: "streaming",+ messages: [user("u1"), assistant("a1")],+ setMessages,+ }),+ );++ act(() => {+ result.current.queueMessage("chipA");+ });++ // Backend has drained chipA (count=0). The poll's GET resolves, but+ // the user appends chipB before the setState commits.+ let resolvePeek!: (value: unknown) => void;+ peekMock.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ resolvePeek = resolve;+ }),+ );++ await act(async () => {+ vi.advanceTimersByTime(2_000);+ });++ // While the GET is still pending, append a second chip.+ act(() => {+ result.current.queueMessage("chipB");+ });++ // Now resolve the in-flight peek.+ await act(async () => {+ resolvePeek({ status: 200, data: { count: 0, messages: [] } });+ await Promise.resolve();+ await Promise.resolve();+ });++ // chipA was drained → promoted; chipB survived as a chip.+ expect(result.current.queuedMessages).toEqual(["chipB"]);+ });});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.tsxindex eea82b08a4..12da2bb3e9 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@@ -295,6 +295,27 @@ export function ChatMessagesContainer({const lastMessage = messages[messages.length - 1];const graphExecId = useMemo(() => extractGraphExecId(messages), [messages]);+ // The backend appends a persisted error marker to ``session.messages`` AND+ // yields a ``StreamError`` SSE event on final-failure paths. Both surface+ // the same error string — the marker becomes an in-line ErrorCard bubble,+ // the SSE event sets ``error`` on ``useChat``. Without dedup, the user sees+ // the same error twice. Suppress the trailing banner whenever the last+ // assistant message already carries the marker.+ const lastAssistantHasErrorMarker = useMemo(() => {+ for (let i = messages.length - 1; i >= 0; i--) {+ const msg = messages[i];+ if (msg.role !== "assistant") continue;+ for (let j = msg.parts.length - 1; j >= 0; j--) {+ const part = msg.parts[j];+ if (part.type !== "text") continue;+ const { markerType } = parseSpecialMarkers(part.text);+ return markerType === "error" || markerType === "retryable_error";+ }+ return false;+ }+ return false;+ }, [messages]);+const hasInflight = (() => {if (lastMessage?.role !== "assistant") return false;// Ignore bookkeeping parts. data-cursor is legacy resume metadata and@@ -618,7 +639,7 @@ export function ChatMessagesContainer({</MessageContent></Message>))}- {error && (+ {error && !lastAssistantHasErrorMarker && (<details className="rounded-lg bg-red-50 p-4 text-sm text-red-700"><summary className="cursor-pointer font-medium">The assistant encountered an error. Please try sending yourdiff --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.tsxindex 1961c9be63..386fde66f2 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@@ -85,13 +85,25 @@ vi.mock("../../JobStatsBar/useElapsedTimer", () => ({vi.mock("../../CopilotPendingReviews/CopilotPendingReviews", () => ({CopilotPendingReviews: () => null,}));+// Tests below override this default by re-mocking ../helpers as needed.vi.mock("../helpers", () => ({buildRenderSegments: () => [],getTurnMessages: () => [],- parseSpecialMarkers: () => ({ markerType: null }),+ parseSpecialMarkers: (text: string) => {+ if (typeof text === "string" && text.startsWith("[__COPILOT_ERROR_")) {+ return { markerType: "error" };+ }+ if (+ typeof text === "string" &&+ text.startsWith("[__COPILOT_RETRYABLE_ERROR_")+ ) {+ return { markerType: "retryable_error" };+ }+ return { markerType: null };+ },splitReasoningAndResponse: (parts: unknown[]) => ({- reasoningParts: [],- responseParts: parts,+ reasoning: [],+ response: parts,}),}));@@ -304,6 +316,100 @@ describe("ChatMessagesContainer — loading", () => {});});+// ── error banner dedup ────────────────────────────────────────────────────++describe("ChatMessagesContainer — error banner dedup", () => {+ beforeEach(() => {+ mockScrollEl.scrollHeight = 100;+ mockScrollEl.scrollTop = 0;+ mockScrollEl.clientHeight = 500;+ MockIntersectionObserver.lastCallback = null;+ vi.stubGlobal("IntersectionObserver", MockIntersectionObserver);+ });++ afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+ });++ it("renders the trailing banner when no persisted error marker is in messages", () => {+ render(+ <ChatMessagesContainer+ {...baseProps}+ error={new Error("SDK stream error: Prompt is too long")}+ status="error"+ messages={[+ {+ id: "u-1",+ role: "user",+ parts: [{ type: "text", text: "go" }],+ },+ ]}+ />,+ );+ expect(+ screen.getByText("SDK stream error: Prompt is too long"),+ ).toBeDefined();+ expect(screen.getByText(/encountered an error/i)).toBeDefined();+ });++ it("suppresses the trailing banner when the last assistant message carries an error marker", () => {+ render(+ <ChatMessagesContainer+ {...baseProps}+ error={new Error("SDK stream error: Prompt is too long")}+ status="error"+ messages={[+ {+ id: "u-1",+ role: "user",+ parts: [{ type: "text", text: "go" }],+ },+ {+ id: "a-1",+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "[__COPILOT_ERROR_f7a1__] SDK stream error: Prompt is too long",+ },+ ],+ },+ ]}+ />,+ );+ expect(screen.queryByText(/encountered an error/i)).toBeNull();+ });++ it("suppresses the trailing banner when the marker is retryable", () => {+ render(+ <ChatMessagesContainer+ {...baseProps}+ error={new Error("Transient error")}+ status="error"+ messages={[+ {+ id: "u-1",+ role: "user",+ parts: [{ type: "text", text: "go" }],+ },+ {+ id: "a-1",+ role: "assistant",+ parts: [+ {+ type: "text",+ text: "[__COPILOT_RETRYABLE_ERROR_a9c2__] Transient error",+ },+ ],+ },+ ]}+ />,+ );+ expect(screen.queryByText(/encountered an error/i)).toBeNull();+ });+});+// ── pagination sentinel ───────────────────────────────────────────────────describe("ChatMessagesContainer", () => {diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.tsindex 71d5a22a48..12d2f2aa7b 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts@@ -1,5 +1,6 @@import type { FileUIPart, UIMessage } from "ai";import { create } from "zustand";+import { createJSONStorage, persist } from "zustand/middleware";/*** Per-session state. Zustand (not React refs) so it can survive events we@@ -74,72 +75,100 @@ interface CopilotStreamStore {resetAll: () => void;}-export const useCopilotStreamStore = create<CopilotStreamStore>((set, get) => ({- sessions: {},- messageSnapshots: {},- pendingFirstSend: null,- pendingFileParts: [],+export const useCopilotStreamStore = create<CopilotStreamStore>()(+ persist(+ (set, get) => ({+ sessions: {},+ messageSnapshots: {},+ pendingFirstSend: null,+ pendingFileParts: [],- getCoord(sessionId) {- return get().sessions[sessionId] ?? defaultCoord;- },- updateCoord(sessionId, patch) {- set((state) => ({- sessions: {- ...state.sessions,- [sessionId]: {- ...(state.sessions[sessionId] ?? defaultCoord),- ...patch,- },+ getCoord(sessionId) {+ return get().sessions[sessionId] ?? defaultCoord;},- }));- },- clearSession(sessionId) {- set((state) => {- const sessions = { ...state.sessions };- delete sessions[sessionId];+ updateCoord(sessionId, patch) {+ set((state) => ({+ sessions: {+ ...state.sessions,+ [sessionId]: {+ ...(state.sessions[sessionId] ?? defaultCoord),+ ...patch,+ },+ },+ }));+ },+ clearSession(sessionId) {+ set((state) => {+ const sessions = { ...state.sessions };+ delete sessions[sessionId];- const messageSnapshots = { ...state.messageSnapshots };- delete messageSnapshots[sessionId];+ const messageSnapshots = { ...state.messageSnapshots };+ delete messageSnapshots[sessionId];- return {- sessions,- messageSnapshots,- };- });- },- getMessageSnapshot(sessionId) {- return get().messageSnapshots[sessionId] ?? [];- },- setMessageSnapshot(sessionId, messages) {- set((state) => ({- messageSnapshots: {- ...state.messageSnapshots,- [sessionId]: messages,+ return {+ sessions,+ messageSnapshots,+ };+ });+ },+ getMessageSnapshot(sessionId) {+ return get().messageSnapshots[sessionId] ?? [];+ },+ setMessageSnapshot(sessionId, messages) {+ set((state) => ({+ messageSnapshots: {+ ...state.messageSnapshots,+ [sessionId]: messages,+ },+ }));},- }));- },- setPendingFirstSend(send) {- set({ pendingFirstSend: send });- },- setPendingFileParts(parts) {- set({ pendingFileParts: parts });- },- takePendingFirstSend() {- const { pendingFirstSend, pendingFileParts } = get();- set({ pendingFirstSend: null, pendingFileParts: [] });- return { send: pendingFirstSend, parts: pendingFileParts };- },+ setPendingFirstSend(send) {+ set({ pendingFirstSend: send });+ },+ setPendingFileParts(parts) {+ set({ pendingFileParts: parts });+ },+ takePendingFirstSend() {+ const { pendingFirstSend, pendingFileParts } = get();+ set({ pendingFirstSend: null, pendingFileParts: [] });+ return { send: pendingFirstSend, parts: pendingFileParts };+ },- resetAll() {- set({- sessions: {},- messageSnapshots: {},- pendingFirstSend: null,- pendingFileParts: [],- });- },-}));+ resetAll() {+ set({+ sessions: {},+ messageSnapshots: {},+ pendingFirstSend: null,+ pendingFileParts: [],+ });+ },+ }),+ {+ // Persist ONLY the per-session ``sessions`` map (which carries+ // ``lastSubmittedMessageText``). Refresh / new tab in the same browser+ // session keeps the dedup memory so the wrapped sendMessage's+ // ``getSendSuppressionReason`` can still block duplicate POSTs after+ // the in-memory state would otherwise be lost.+ // ``messageSnapshots`` is intentionally excluded — it's a per-render+ // cache of UIMessages (often hundreds) that the next mount should+ // re-derive from the server, not restore from storage.+ // ``pendingFirstSend`` / ``pendingFileParts`` are tab-local artefacts+ // of the session-creation remount and would be confusing if persisted.+ name: "copilot-stream-store",+ version: 1,+ // SSR-safe storage adapter: ``window.sessionStorage`` in the browser,+ // a no-op stub during Next.js SSR / vitest where ``window`` is+ // undefined. Returning ``undefined`` from the factory would make+ // zustand throw on its first ``getItem`` call.+ storage: createJSONStorage(() =>+ typeof window !== "undefined" && window.sessionStorage+ ? window.sessionStorage+ : { getItem: () => null, setItem: () => {}, removeItem: () => {} },+ ),+ partialize: (state) => ({ sessions: state.sessions }),+ },+ ),+);export const DEFAULT_SESSION_COORD = defaultCoord;diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.tsindex fdde7cbd08..24776fb8a7 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts@@ -54,6 +54,20 @@ export function createCopilotTransport({return match?.[1];}).filter(Boolean) as string[] | undefined;+ // ``message_id`` becomes the persisted ``ChatMessage.id`` (PK) —+ // Postgres' uniqueness constraint then makes the INSERT itself+ // the atomic dedup primitive: a retransmit (SDK-internal retry,+ // browser auto-retry, RMQ redelivery) lands on a duplicate PK+ // and the backend short-circuits to subscribe-only.+ //+ // Generated here (rather than in ``useSendMessage``) for two+ // reasons: (1) AI SDK's ``messageId`` arg on ``sendMessage`` is+ // "replace-existing-message" semantics — passing a fresh UUID+ // puts the SDK into edit-mode with no target and breaks+ // optimistic render. (2) ``prepareSendMessagesRequest`` is+ // called once per logical ``sendMessages`` call and the+ // prepared body is reused across SDK-internal retries, so a+ // single per-call UUID is exactly the stability we need.return {body: {message: (@@ -64,6 +78,7 @@ export function createCopilotTransport({file_ids: fileIds && fileIds.length > 0 ? fileIds : null,mode: copilotModeRef.current ?? null,model: copilotModelRef.current ?? null,+ message_id: crypto.randomUUID(),},headers: await getCopilotAuthHeaders(),};diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.tsindex b308ef69df..e260b43b79 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts@@ -1,5 +1,9 @@+import type { UIDataTypes, UIMessage, UITools } from "ai";import { describe, expect, it } from "vitest";-import { convertChatSessionMessagesToUiMessages } from "../convertChatSessionToUiMessages";+import {+ concatWithAssistantMerge,+ convertChatSessionMessagesToUiMessages,+} from "../convertChatSessionToUiMessages";const SESSION_ID = "sess-test";@@ -275,3 +279,108 @@ describe("convertChatSessionMessagesToUiMessages", () => {expect(result.stats.get(mergedId)?.createdAt).toBe(later);});});++// --------------------------------------------------------------------------- //+// concatWithAssistantMerge — page-boundary stitching //+// --------------------------------------------------------------------------- //++function uiAssistant(+ sessionId: string,+ seq: number,+ text: string,+): UIMessage<unknown, UIDataTypes, UITools> {+ return {+ id: `${sessionId}-seq-${seq}`,+ role: "assistant",+ parts: [{ type: "text", text, state: "done" }],+ };+}++function uiUser(+ sessionId: string,+ seq: number,+ text: string,+): UIMessage<unknown, UIDataTypes, UITools> {+ return {+ id: `${sessionId}-seq-${seq}`,+ role: "user",+ parts: [{ type: "text", text, state: "done" }],+ };+}++describe("concatWithAssistantMerge", () => {+ it("returns b unchanged when a is empty", () => {+ const b = [uiAssistant(SESSION_ID, 0, "hi")];+ expect(concatWithAssistantMerge([], b)).toEqual(b);+ });++ it("returns a unchanged when b is empty", () => {+ const a = [uiAssistant(SESSION_ID, 0, "hi")];+ expect(concatWithAssistantMerge(a, [])).toEqual(a);+ });++ it("merges two ASSISTANT bubbles whose DB sequences are strictly adjacent", () => {+ const a = [uiAssistant(SESSION_ID, 2, "hello")];+ const b = [uiAssistant(SESSION_ID, 3, " world")];+ const result = concatWithAssistantMerge(a, b);+ expect(result).toHaveLength(1);+ expect(result[0].role).toBe("assistant");+ expect(result[0].parts).toHaveLength(2);+ });++ it("does NOT merge when DB sequences are not adjacent — a hidden user/reasoning row would be silently swallowed", () => {+ // Real-world repro: assistant_seq3 + (user_seq4 not yet hydrated) ++ // assistant_seq6. Pre-fix this stitched seq3 and seq6 into one bubble+ // and the chip's user follow-up at seq4 vanished.+ const a = [uiAssistant(SESSION_ID, 3, "before")];+ const b = [uiAssistant(SESSION_ID, 6, "after")];+ const result = concatWithAssistantMerge(a, b);+ expect(result).toHaveLength(2);+ expect(result[0].id).toBe(`${SESSION_ID}-seq-3`);+ expect(result[1].id).toBe(`${SESSION_ID}-seq-6`);+ });++ it("does NOT merge when last-of-a is user and first-of-b is assistant", () => {+ const a = [uiUser(SESSION_ID, 4, "follow up")];+ const b = [uiAssistant(SESSION_ID, 5, "got it")];+ const result = concatWithAssistantMerge(a, b);+ expect(result).toHaveLength(2);+ });++ it("merges a single-row page B onto a multi-row merged bubble in page A using the LAST seq of the bubble", () => {+ // Sentry: pre-fix, a merged bubble holding seq=5+6 was keyed+ // ``-seq-5``, and a cross-page assistant at seq=7 failed the+ // ``firstSeq === lastSeq + 1`` check (7 !== 5+1) and split into+ // two bubbles. The in-page merge now advances the id to the last+ // seq, so the adjacency check sees ``7 === 6+1`` and merges.+ const pageA = convertChatSessionMessagesToUiMessages(+ SESSION_ID,+ [+ { role: "reasoning", content: "thinking", sequence: 5 },+ { role: "assistant", content: "first part", sequence: 6 },+ ],+ { isComplete: true },+ ).messages;+ expect(pageA).toHaveLength(1);+ expect(pageA[0].id).toBe(`${SESSION_ID}-seq-6`);+ const pageB = [uiAssistant(SESSION_ID, 7, "continued")];+ const result = concatWithAssistantMerge(pageA, pageB);+ expect(result).toHaveLength(1);+ expect(result[0].parts.length).toBeGreaterThan(1);+ });++ it("does NOT merge across hydrated → streaming boundary (streaming ids fail seq-extraction)", () => {+ const a = [uiAssistant(SESSION_ID, 5, "from db")];+ const b: UIMessage<unknown, UIDataTypes, UITools>[] = [+ {+ id: "ai-sdk-streaming-uuid",+ role: "assistant",+ parts: [{ type: "text", text: " streaming", state: "streaming" }],+ },+ ];+ const result = concatWithAssistantMerge(a, b);+ // Refusing to merge is the safer default — streaming consumer handles+ // its own assistant continuity inside the active turn.+ expect(result).toHaveLength(2);+ });+});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.tsindex cca9b2a25c..45536e2449 100644--- 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@@ -3,31 +3,21 @@ 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");+ it("renders one bubble per chip with the chip text verbatim", () => {+ const b = makePromotedUserBubble("first", "midturn", "abc");expect(b.role).toBe("user");expect(b.parts).toHaveLength(1);expect(b.parts[0]).toMatchObject({type: "text",- text: "first\n\nsecond",+ text: "first",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");+ 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/convertChatSessionToUiMessages.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.tsindex ea574ac77d..dbbe7d1b9c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts@@ -119,13 +119,33 @@ function toToolInput(rawArguments: unknown): unknown {return {};}+// Capture trailing sequence number from a hydrated UIMessage id of the+// shape ``<sessionId>-seq-<N>``. Streaming-path ids (AI SDK uuids) and+// idx-based fallback ids (``-idx-<N>``) don't match — return null so the+// caller refuses the merge in those cases (the safer default).+const HYDRATED_ID_SEQ_RE = /-seq-(\d+)$/;++function extractDbSequence(uiMessage: UIMessage): number | null {+ if (typeof uiMessage.id !== "string") return null;+ const match = HYDRATED_ID_SEQ_RE.exec(uiMessage.id);+ return match ? Number(match[1]) : null;+}+/*** Concatenate two UIMessage arrays, merging consecutive assistant messages* at the join point so that reasoning + response parts stay in a single bubble.** Within each page, `convertChatSessionMessagesToUiMessages` already merges- * consecutive assistant DB rows. This handles the boundary between pages+ * consecutive assistant DB rows. This handles the boundary between pages* (or between older-pages and the current/streaming page).+ *+ * The merge is gated on **DB-sequence adjacency**: extract the trailing+ * ``-seq-N`` from each id and only merge when ``firstSeq === lastSeq + 1``.+ * Without that check, a hydration-race window (where a user/reasoning row+ * exists in the DB but has not yet been hydrated into either page) would+ * silently swallow the missing row by stitching the two surrounding+ * assistant bubbles into one — matching the chip-disappearing /+ * "merged as previous chat" report.*/export function concatWithAssistantMerge(a: UIMessage<unknown, UIDataTypes, UITools>[],@@ -135,14 +155,24 @@ export function concatWithAssistantMerge(if (b.length === 0) return a;const last = a[a.length - 1];const first = b[0];- if (last.role === "assistant" && first.role === "assistant") {- return [- ...a.slice(0, -1),- { ...last, parts: [...last.parts, ...first.parts] },- ...b.slice(1),- ];+ if (last.role !== "assistant" || first.role !== "assistant") {+ return [...a, ...b];}- return [...a, ...b];+ // Both sides assistant — only merge when the underlying DB sequences are+ // strictly adjacent (no skipped user / reasoning row between them that a+ // hydration race could be hiding). Streaming-path ids fail extraction and+ // refuse the merge, which is fine: the streaming consumer handles its own+ // assistant continuity inside the active turn.+ const lastSeq = extractDbSequence(last);+ const firstSeq = extractDbSequence(first);+ if (lastSeq === null || firstSeq === null || firstSeq !== lastSeq + 1) {+ return [...a, ...b];+ }+ return [+ ...a.slice(0, -1),+ { ...last, parts: [...last.parts, ...first.parts] },+ ...b.slice(1),+ ];}/**@@ -301,9 +331,29 @@ export function convertChatSessionMessagesToUiMessages(// Merge consecutive assistant messages (including reasoning rows) into a// single UIMessage to avoid split bubbles on page reload.+ //+ // The merged bubble's ``id`` advances to the LAST DB sequence in the+ // group (i.e. the row we are currently appending) so+ // ``concatWithAssistantMerge`` can read the correct adjacency from the+ // id alone — without that, a merged bubble holding seq=5+6 would still+ // be keyed ``-seq-5``, and a cross-page assistant at seq=7 would fail+ // the ``firstSeq === lastSeq + 1`` check (7 !== 5+1) and split into two+ // bubbles instead of joining the ongoing turn.const prevUI = uiMessages[uiMessages.length - 1];if (uiRole === "assistant" && prevUI && prevUI.role === "assistant") {prevUI.parts.push(...parts);+ const oldId = prevUI.id;+ const newId =+ msg.sequence != null+ ? `${sessionId}-seq-${msg.sequence}`+ : `${sessionId}-idx-${idx}`;+ if (newId !== oldId) {+ // Migrate stats over to the new id and let the old key go.+ const existingStats = stats.get(oldId);+ stats.delete(oldId);+ if (existingStats) stats.set(newId, existingStats);+ prevUI.id = newId;+ }// Capture duration on merged message (last assistant msg wins)if (msg.duration_ms != null) {patchStats(prevUI.id, { durationMs: msg.duration_ms });diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/makePromotedBubble.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/makePromotedBubble.tsindex 734abcb3e0..dca92d0f3b 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/makePromotedBubble.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/makePromotedBubble.ts@@ -1,15 +1,16 @@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.+ * Build a user-bubble `UIMessage` representing a single pending chip the+ * backend has just drained. One chip → one bubble (preserves cardinality+ * across the chip→drain→promote lifecycle). 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[],+ text: string,prefix: "auto-continue" | "midturn",suffix: string,): UIMessage {@@ -19,7 +20,7 @@ export function makePromotedUserBubble(parts: [{type: "text" as const,- text: texts.join("\n\n"),+ text,state: "done" as const,},],diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.tsindex 90058df00f..fcb1f85376 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts@@ -147,7 +147,7 @@ export function useCopilotPage() {// 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({+ const { queuedMessages, queueMessage } = useCopilotPendingChips({sessionId,status,messages,@@ -187,7 +187,7 @@ export function useCopilotPage() {try {await queueFollowUpMessage(sessionId, trimmed);- appendChip(trimmed);+ queueMessage(trimmed);} catch (err) {if (err instanceof Error &&diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.tsindex 3a7641010b..7e2d748293 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.ts@@ -1,6 +1,6 @@import { getV2GetPendingMessages } from "@/app/api/__generated__/endpoints/chat/chat";import type { UIMessage } from "ai";-import { useCallback, useEffect, useRef, useState } from "react";+import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { makePromotedUserBubble } from "./helpers/makePromotedBubble";@@ -8,6 +8,13 @@ const MID_TURN_POLL_MS = 2_000;type ChatStatus = "submitted" | "streaming" | "ready" | "error";+interface QueuedMessage {+ id: string;+ text: string;+}++type QueueUpdater = (prev: QueuedMessage[]) => QueuedMessage[];+interface Args {sessionId: string | null;status: ChatStatus;@@ -23,6 +30,12 @@ interface Args {* mid-turn via the MCP wrapper), and surface the list + queue op to the* chat input.*+ * Each chip carries a frontend-only ``id`` so concurrent draining and+ * appending stays race-safe — the poll captures a stale snapshot of the+ * chip array, but the eventual state mutation drops chips by id rather+ * than by array slice, so a chip the user enqueues during the in-flight+ * poll cannot be silently overwritten.+ ** State machine:** ┌────────┐ user queues ┌────────────┐ backend turn-start drain@@ -31,8 +44,8 @@ interface Args {* ▲ └────────────┘ │* │ │* │ ┌──────────────────────────────────────────────┘- * │ │ 1. auto-continue chain: promote combined bubble- * │ │ 2. mid-turn poll sees count drop: promote partial+ * │ │ 1. auto-continue chain: promote one bubble per chip+ * │ │ 2. mid-turn poll sees count drop: promote drained chips* └────────────┘ 3. stream ends, hydration takes over*/export function useCopilotPendingChips({@@ -41,32 +54,38 @@ export function useCopilotPendingChips({messages,setMessages,}: Args) {- const [queuedMessages, setQueuedMessages] = useState<string[]>([]);+ const [queue, setQueue] = useState<QueuedMessage[]>([]);+ // Stable string view for consumers that only need the texts. Memoised so+ // downstream components don't re-render on identity churn alone.+ const queuedMessages = useMemo(+ () => queue.map((entry) => entry.text),+ [queue],+ );- usePeekOnBoundary({ sessionId, status, setQueuedMessages });+ usePeekOnBoundary({ sessionId, status, setMessages, setQueue });useAutoContinuePromotion({sessionId,status,messages,- queuedMessages,+ queue,setMessages,- setQueuedMessages,+ setQueue,});useMidTurnDrainPromotion({sessionId,status,- queuedMessages,+ queue,setMessages,- setQueuedMessages,+ setQueue,});- const appendChip = useCallback((text: string) => {- setQueuedMessages((prev) => [...prev, text]);+ const queueMessage = useCallback((text: string) => {+ setQueue((prev) => [...prev, { id: crypto.randomUUID(), text }]);}, []);- return { queuedMessages, appendChip };+ return { queuedMessages, queueMessage };}// ── 1. Peek sync ───────────────────────────────────────────────────────@@ -78,14 +97,23 @@ export function useCopilotPendingChips({function usePeekOnBoundary({sessionId,status,- setQueuedMessages,+ setMessages,+ setQueue,}: {sessionId: string | null;status: ChatStatus;- setQueuedMessages: (v: string[]) => void;+ setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void;+ setQueue: (updater: QueueUpdater) => void;}) {const prevSessionIdRef = useRef<string | null>(sessionId);const prevStatusRef = useRef<ChatStatus>(status);+ // Snapshot of chip ids known to be in-flight to the server at the+ // moment a peek GET is issued. Anything NOT in this set when the GET+ // resolves was appended after the request — preserve it so a+ // concurrently-queued chip isn't wiped by the server's now-stale+ // truth. Set inside the effect (closes over the current chips+ // value), read inside the ``.then`` handler.+ const inFlightSnapshotIdsRef = useRef<Set<string>>(new Set());useEffect(() => {const prevStatus = prevStatusRef.current;@@ -96,7 +124,7 @@ function usePeekOnBoundary({// 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 (sessionChanged) setQueue(() => []);if (!sessionId) return;@@ -109,25 +137,75 @@ function usePeekOnBoundary({// auto-continue effect's duplicate turn-start peek.if (!sessionChanged && !isIdle && !turnStarting) return;+ // Capture sessionId at request time and compare to the live ref on+ // resolve. Without this, a peek that resolves after the user+ // switched sessions could bleed old-session chips into the new+ // session. We deliberately don't use a per-effect cancelled flag+ // here because this effect re-runs on every status change too, and+ // we don't want chip-appends in another effect to invalidate this+ // peek's result.+ const requestSessionId = sessionId;+ // Capture the id-set of queue entries currently in local state so+ // the resolve handler can preserve any entry the user queues during+ // the GET window.+ setQueue((current) => {+ inFlightSnapshotIdsRef.current = new Set(+ current.map((entry) => entry.id),+ );+ return current;+ });void getV2GetPendingMessages(sessionId).then((res) => {+ if (prevSessionIdRef.current !== requestSessionId) return;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.+ const inFlightIds = inFlightSnapshotIdsRef.current;+ // Turn-start drain path: when the backend has drained everything+ // it had at GET time, promote those drained entries to user+ // bubbles BEFORE removing them from local state. Without the+ // promote step, the chips disappear from the chip-strip but the+ // bubble for each drained entry only shows up later via+ // hydration once the turn fully ends — leaving the user staring+ // at a streaming assistant that's responding to text they can no+ // longer see typed in the chat. Entries appended after the GET+ // fired (during the React render between turn-start and the GET+ // resolving) survive untouched.if (turnStarting && !sessionChanged) {- if (res.data.count === 0) setQueuedMessages([]);+ if (res.data.count === 0) {+ setQueue((current) => {+ const drained = current.filter((entry) =>+ inFlightIds.has(entry.id),+ );+ if (drained.length > 0) {+ promoteChipsToTrailingBubbles(setMessages, drained);+ }+ return current.filter((entry) => !inFlightIds.has(entry.id));+ });+ }return;}- // Session-load or idle-after-turn: replace with server truth.- setQueuedMessages(res.data.count > 0 ? res.data.messages : []);+ // Session-load or idle-after-turn: rebase to server truth, then+ // re-attach any entries the user queued during the GET window+ // (they're not in inFlightIds because the snapshot was taken at+ // GET fire time). Without this re-attach, an entry queued after+ // an "idle" transition but before the peek resolves silently+ // disappears.+ setQueue((current) => {+ const fromServer = res.data.messages.map((text) => ({+ id: crypto.randomUUID(),+ text,+ }));+ const queuedDuringWindow = current.filter(+ (entry) => !inFlightIds.has(entry.id),+ );+ return [...fromServer, ...queuedDuringWindow];+ });});- }, [sessionId, status, setQueuedMessages]);+ }, [sessionId, status, setQueue]);}// ── 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.+// the same stream chain), promote drained chips into user bubbles 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@@ -138,16 +216,16 @@ function useAutoContinuePromotion({sessionId,status,messages,- queuedMessages,+ queue,setMessages,- setQueuedMessages,+ setQueue,}: {sessionId: string | null;status: ChatStatus;messages: UIMessage[];- queuedMessages: string[];+ queue: QueuedMessage[];setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void;- setQueuedMessages: (v: string[]) => void;+ setQueue: (updater: QueueUpdater) => void;}) {const prevStatusRef = useRef(status);// The opener is the first assistant id observed after a turn starts.@@ -186,32 +264,73 @@ function useAutoContinuePromotion({// 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;+ if (queue.length === 0) return;++ const promotedIds = new Set(queue.map((entry) => entry.id));+ promoteBeforeAssistant(setMessages, latest, queue);+ // Drop only the entries we promoted; entries appended after the+ // snapshot (during the React commit) survive.+ setQueue((current) =>+ current.filter((entry) => !promotedIds.has(entry.id)),+ );+ }, [messages, status, sessionId, queue, setMessages, setQueue]);+}- promoteBeforeAssistant(setMessages, latest, queuedMessages);- setQueuedMessages([]);- }, [- messages,- status,- sessionId,- queuedMessages,- setMessages,- setQueuedMessages,- ]);+/**+ * Splice promoted user bubbles for *drained* in just before the trailing+ * streaming assistant message — same insertion shape as the mid-turn poll+ * promotion so AI SDK's streaming continues into the right slot.+ *+ * Used by the turn-start drain path in ``usePeekOnBoundary``: when the+ * backend has already drained chips before the frontend's first peek+ * resolves, we'd otherwise just remove them from local state and the+ * bubble would only appear via hydration after the turn ends. This+ * helper makes the bubble visible immediately so the user can see what+ * the model is responding to.+ */+function promoteChipsToTrailingBubbles(+ setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void,+ drained: QueuedMessage[],+): void {+ setMessages((prev) => {+ const newBubbles = drained+ .map((entry) =>+ makePromotedUserBubble(entry.text, "midturn", bubbleIdFor(entry)),+ )+ .filter((bubble) => !prev.some((m) => m.id === bubble.id));+ if (newBubbles.length === 0) return prev;+ const lastIdx = prev.length - 1;+ if (lastIdx >= 0 && prev[lastIdx].role === "assistant") {+ return [...prev.slice(0, lastIdx), ...newBubbles, prev[lastIdx]];+ }+ return [...prev, ...newBubbles];+ });+}++// One stable bubble id per queued message, shared between auto-continue+// and mid-turn promotion paths. Without this, a poll resolving after+// auto-continue already promoted the same entry would render the bubble+// twice (different ids → dedup misses).+function bubbleIdFor(entry: QueuedMessage): string {+ return `pending-chip-${entry.id}`;}function promoteBeforeAssistant(setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void,assistantId: string,- texts: string[],+ queue: QueuedMessage[],): 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)];+ const newBubbles = queue+ .map((entry) =>+ makePromotedUserBubble(entry.text, "auto-continue", bubbleIdFor(entry)),+ )+ // Skip bubbles that are already in the array (effect re-run safety).+ .filter((bubble) => !prev.some((m) => m.id === bubble.id));+ if (newBubbles.length === 0) return prev;+ return [...prev.slice(0, insertAt), ...newBubbles, ...prev.slice(insertAt)];});}@@ -229,38 +348,53 @@ function promoteBeforeAssistant(function useMidTurnDrainPromotion({sessionId,status,- queuedMessages,+ queue,setMessages,- setQueuedMessages,+ setQueue,}: {sessionId: string | null;status: ChatStatus;- queuedMessages: string[];+ queue: QueuedMessage[];setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void;- setQueuedMessages: (v: string[]) => void;+ setQueue: (updater: QueueUpdater) => void;}) {+ // Live ref tracks the latest sessionId so a poll captured at request+ // time can detect a session switch on resolve. A cancellation flag+ // would also fire on enqueue (this effect re-runs on every queue+ // change), wrongly aborting an in-flight poll for the same session —+ // the sessionId comparison only invalidates on actual session changes.+ const latestSessionIdRef = useRef<string | null>(sessionId);+ useEffect(() => {+ latestSessionIdRef.current = sessionId;+ }, [sessionId]);+useEffect(() => {if (!sessionId) return;const isActive = status === "streaming" || status === "submitted";- if (!isActive || queuedMessages.length === 0) return;+ if (!isActive || queue.length === 0) return;+ const requestSessionId = sessionId;+ const isCurrentSession = () =>+ latestSessionIdRef.current === requestSessionId;const interval = setInterval(() => {void pollBackendAndPromote(sessionId,- queuedMessages,+ queue,setMessages,- setQueuedMessages,+ setQueue,+ isCurrentSession,);}, MID_TURN_POLL_MS);return () => clearInterval(interval);- }, [sessionId, status, queuedMessages, setMessages, setQueuedMessages]);+ }, [sessionId, status, queue, setMessages, setQueue]);}async function pollBackendAndPromote(sessionId: string,- localChips: string[],+ snapshotQueue: QueuedMessage[],setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void,- setQueuedMessages: (v: string[]) => void,+ setQueue: (updater: QueueUpdater) => void,+ isCurrentSession: () => boolean,): Promise<void> {let backendCount: number;try {@@ -270,12 +404,16 @@ async function pollBackendAndPromote(} catch {return; // harmless; next tick or hydration will reconcile}- if (localChips.length === 0) return;- if (backendCount >= localChips.length) return;+ // Bail if the user switched sessions while the GET was in flight —+ // promoting these entries to messages for a different session would+ // leak old-session bubbles into the new session.+ if (!isCurrentSession()) return;+ if (snapshotQueue.length === 0) return;+ if (backendCount >= snapshotQueue.length) return;- const drainedCount = localChips.length - backendCount;- const drained = localChips.slice(0, drainedCount);- const remaining = localChips.slice(drainedCount);+ const drainedCount = snapshotQueue.length - backendCount;+ const drained = snapshotQueue.slice(0, drainedCount);+ const drainedIds = new Set(drained.map((entry) => entry.id));// Splice the promoted bubble at ``len-1`` so the trailing streaming// assistant stays at ``messages[-1]``. AI SDK's ``useChat`` streams@@ -284,25 +422,29 @@ async function pollBackendAndPromote(// subsequent chunk lands in the wrong slot (silently) until a page// refresh. Inserting before the assistant keeps the stream flowing.//- // The one tradeoff: during streaming the promoted bubble clusters+ // The one tradeoff: during streaming the promoted bubbles cluster// just above the current streaming assistant — which is earlier in// the chronological order than the DB-canonical spot (between the- // tool result it rode in on and the continuing assistant). AI SDK's+ // tool result they rode in on and the continuing assistant). AI SDK's// single-message-per-turn model can't represent that mid-turn split// client-side. ``useHydrateOnStreamEnd`` replaces the in-memory// messages with the DB-canonical order once the stream ends, so the- // bubble snaps to the correct position.+ // bubbles snap to the correct position.setMessages((prev) => {- const bubble = makePromotedUserBubble(- drained,- "midturn",- crypto.randomUUID(),- );+ const newBubbles = drained+ .map((entry) =>+ makePromotedUserBubble(entry.text, "midturn", bubbleIdFor(entry)),+ )+ // Skip bubbles that are already there (effect re-run safety).+ .filter((bubble) => !prev.some((m) => m.id === bubble.id));+ if (newBubbles.length === 0) return prev;const lastIdx = prev.length - 1;if (lastIdx >= 0 && prev[lastIdx].role === "assistant") {- return [...prev.slice(0, lastIdx), bubble, prev[lastIdx]];+ return [...prev.slice(0, lastIdx), ...newBubbles, prev[lastIdx]];}- return [...prev, bubble];+ return [...prev, ...newBubbles];});- setQueuedMessages(remaining);+ // Drop only the drained entries by id; entries appended after the+ // snapshot survive the in-flight poll race.+ setQueue((current) => current.filter((entry) => !drainedIds.has(entry.id)));}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useSendMessage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useSendMessage.tsindex 2156a81552..a57474dc03 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useSendMessage.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useSendMessage.ts@@ -97,6 +97,14 @@ export function useSendMessage({files: File[],prebuiltParts: FileUIPart[],) {+ // The per-click UUID that becomes the backend's ``ChatMessage.id``+ // is generated inside the transport's ``prepareSendMessagesRequest``+ // (one call per ``sendMessages``, stable across SDK-internal+ // retries) — NOT here. AI SDK's ``messageId`` arg means+ // "replace-existing-message" (edit mode), not "id of a new+ // message", so passing it would put the SDK into edit-mode with no+ // target and break the optimistic-render path that pushes the user+ // bubble into ``messages`` synchronously.if (prebuiltParts.length > 0) {sendMessage({ text, files: prebuiltParts });return;diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex bf0d22f56f..25871bb0b7 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -16308,6 +16308,14 @@],"title": "Model","description": "Model tier: 'standard' for the default model, 'advanced' for the highest-capability model. If None, the server applies per-user LD targeting then falls back to config."+ },+ "message_id": {+ "anyOf": [+ { "type": "string", "maxLength": 64 },+ { "type": "null" }+ ],+ "title": "Message Id",+ "description": "Optional per-click UUID generated by the frontend. Becomes the persisted ``ChatMessage.id`` (PK). Frontend / network / RMQ-redelivery retransmits of the same logical send reuse the id, so the Postgres unique-constraint on the PK is the atomic dedup primitive: a duplicate INSERT returns a subscribe-only response without creating a parallel turn. Distinct user clicks (even with identical text) MUST send different ids — the frontend's per-click ``crypto.randomUUID()`` guarantees that."}},"type": "object",