Skip to content

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

feat(platform): show live compaction progress in the Copilot chat (#14143)

Ubbef20f0c0c3934
diff --git a/autogpt_platform/backend/backend/copilot/constants.py b/autogpt_platform/backend/backend/copilot/constants.py
index 643df99758..fb58120998 100644
--- a/autogpt_platform/backend/backend/copilot/constants.py
+++ b/autogpt_platform/backend/backend/copilot/constants.py
@@ -42,6 +42,7 @@ COPILOT_NODE_EXEC_ID_SEPARATOR = ":"
# Compaction notice messages shown to users.
COMPACTION_DONE_MSG = "Earlier messages were summarized to fit within context limits."
+COMPACTION_DROPPED_MSG = "Earlier messages were dropped to fit within context limits."
COMPACTION_TOOL_NAME = "context_compaction"
diff --git a/autogpt_platform/backend/backend/copilot/response_model.py b/autogpt_platform/backend/backend/copilot/response_model.py
index b2a835b2ee..a5256a7f46 100644
--- a/autogpt_platform/backend/backend/copilot/response_model.py
+++ b/autogpt_platform/backend/backend/copilot/response_model.py
@@ -62,6 +62,10 @@ class ResponseType(str, Enum):
# bubbles immediately instead of waiting for its backstop poll.
PENDING_DRAINED = "data-pending-drained"
MODE_CHANGED = "data-mode-changed"
+ # Live context-compaction progress — phase transitions plus token/message
+ # deltas. Transient (never persisted); the durable record is the
+ # ``context_compaction`` tool row's JSON output.
+ COMPACTION = "data-compaction"
class StreamBaseResponse(BaseModel):
@@ -384,6 +388,54 @@ class StreamStatus(StreamBaseResponse):
return f"data: {json.dumps(data)}\n\n"
+CompactionPhase = Literal["summarizing", "rebuilding"]
+
+_COMPACTION_STAT_FIELDS = {
+ "phase",
+ "tokensBefore",
+ "tokensAfter",
+ "messagesBefore",
+ "messagesAfter",
+}
+
+
+class StreamCompactionProgress(StreamBaseResponse):
+ """Live progress for a context-compaction cycle.
+
+ Emitted twice per cycle: ``summarizing`` before the compression work
+ starts, and ``rebuilding`` once it finishes — the latter covers the
+ transcript upload, CLI restart and uncached prefill, which is the long
+ silence the progress bar exists to narrate. Stats ride along whenever
+ they are known; ``summarizing`` carries only an estimate of
+ ``tokensBefore`` (used to pace the curve), while ``rebuilding`` carries
+ the measured before/after counts.
+
+ Transient — never persisted. The durable record is the
+ ``context_compaction`` tool row's JSON output. Note that transient
+ does not mean local: these events cross the executor → Redis →
+ rest_server relay, so the type must stay registered in
+ ``stream_registry._reconstruct_chunk`` or every phase is dropped.
+ """
+
+ type: ResponseType = ResponseType.COMPACTION
+ phase: CompactionPhase = Field(
+ ..., description="Compaction stage this event reports"
+ )
+ tokensBefore: int | None = None
+ tokensAfter: int | None = None
+ messagesBefore: int | None = None
+ messagesAfter: int | None = None
+
+ def to_sse(self) -> str:
+ """Emit as an AI SDK v5 data part so the client surfaces it as
+ `type="data-compaction"` on `message.parts`."""
+ data = self.model_dump(
+ include=_COMPACTION_STAT_FIELDS,
+ exclude_none=True,
+ )
+ return f"data: {json.dumps({'type': self.type.value, 'data': data})}\n\n"
+
+
class StreamPendingDrained(StreamBaseResponse):
"""Hint that the pending-message buffer was drained mid-turn.
diff --git a/autogpt_platform/backend/backend/copilot/response_model_test.py b/autogpt_platform/backend/backend/copilot/response_model_test.py
index 07837d5717..b605e6797a 100644
--- a/autogpt_platform/backend/backend/copilot/response_model_test.py
+++ b/autogpt_platform/backend/backend/copilot/response_model_test.py
@@ -2,7 +2,14 @@
import json
-from backend.copilot.response_model import StreamModeChanged
+import pytest
+from pydantic import ValidationError
+
+from backend.copilot.response_model import (
+ ResponseType,
+ StreamCompactionProgress,
+ StreamModeChanged,
+)
def test_mode_changed_serializes_as_ai_sdk_data_part():
@@ -15,3 +22,49 @@ def test_mode_changed_serializes_as_ai_sdk_data_part():
"type": "data-mode-changed",
"data": {"mode": "extended_thinking"},
}
+
+
+class TestStreamCompactionProgress:
+ def test_type_is_data_compaction(self):
+ evt = StreamCompactionProgress(phase="summarizing")
+ assert evt.type == ResponseType.COMPACTION
+ assert ResponseType.COMPACTION.value == "data-compaction"
+
+ def test_to_sse_wraps_fields_in_data_envelope(self):
+ evt = StreamCompactionProgress(
+ phase="rebuilding",
+ tokensBefore=128_000,
+ tokensAfter=31_000,
+ messagesBefore=412,
+ messagesAfter=38,
+ )
+ line = evt.to_sse()
+ assert line.startswith("data: ")
+ assert line.endswith("\n\n")
+ payload = json.loads(line[len("data: ") : -2])
+ assert payload["type"] == "data-compaction"
+ assert payload["data"] == {
+ "phase": "rebuilding",
+ "tokensBefore": 128_000,
+ "tokensAfter": 31_000,
+ "messagesBefore": 412,
+ "messagesAfter": 38,
+ }
+
+ def test_to_sse_omits_unknown_stats(self):
+ evt = StreamCompactionProgress(phase="summarizing")
+ payload = json.loads(evt.to_sse()[len("data: ") : -2])
+ assert payload["data"] == {"phase": "summarizing"}
+
+ def test_phase_is_constrained_to_the_emitted_stages(self):
+ """The wire contract is typed, not prose.
+
+ ``done`` was documented for a long time but never emitted; the
+ frontend indexes a curve table by this value, so an unmodelled
+ phase reaching it is a client-side crash rather than a no-op.
+ """
+ for phase in ("summarizing", "rebuilding"):
+ assert StreamCompactionProgress(phase=phase).phase == phase
+
+ with pytest.raises(ValidationError):
+ StreamCompactionProgress(phase="done")
diff --git a/autogpt_platform/backend/backend/copilot/sdk/compaction.py b/autogpt_platform/backend/backend/copilot/sdk/compaction.py
index 6cff192d66..6ab3b915d6 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/compaction.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/compaction.py
@@ -9,15 +9,28 @@ persistence, and the ``CompactionTracker`` state machine.
"""
import asyncio
+import json
+import logging
import uuid
from collections import Counter, deque
+from collections.abc import Iterable
from dataclasses import dataclass, field
from typing import Any
-from ..constants import COMPACTION_DONE_MSG, COMPACTION_TOOL_NAME
+from pydantic import BaseModel, ConfigDict, Field
+
+from backend.util.prompt import estimate_token_count_str
+
+from ..constants import (
+ COMPACTION_DONE_MSG,
+ COMPACTION_DROPPED_MSG,
+ COMPACTION_TOOL_NAME,
+)
from ..model import ChatMessage, ChatSession
from ..response_model import (
+ CompactionPhase,
StreamBaseResponse,
+ StreamCompactionProgress,
StreamFinishStep,
StreamStartStep,
StreamToolInputAvailable,
@@ -39,6 +52,161 @@ class CompactionResult:
transcript_path: str = ""
+class CompactionStats(BaseModel):
+ """Token and message counts for one compaction cycle.
+
+ Every field is optional: the SDK-internal path learns the counts from
+ the CLI transcript (sometimes not at all), while the pre-query path
+ reads them straight off ``CompressResult``.
+
+ The camelCase serialization aliases are the wire names, declared once here
+ so the tool row's JSON output and the ``data-compaction`` progress event
+ cannot drift apart from each other or from the fields.
+ """
+
+ model_config = ConfigDict(frozen=True)
+
+ tokens_before: int | None = Field(default=None, serialization_alias="tokensBefore")
+ tokens_after: int | None = Field(default=None, serialization_alias="tokensAfter")
+ messages_before: int | None = Field(
+ default=None, serialization_alias="messagesBefore"
+ )
+ messages_after: int | None = Field(
+ default=None, serialization_alias="messagesAfter"
+ )
+ # Compression failed outright and the history was dropped, not condensed.
+ # A payload fact rather than a wire stat: the settled row reports the
+ # reset, while the progress events (``to_wire``) never carry it.
+ dropped: bool = Field(default=False, exclude=True)
+
+ def to_wire(self) -> dict[str, Any]:
+ """Known counts under their client-facing names; unknowns omitted."""
+ return self.model_dump(by_alias=True, exclude_none=True)
+
+
+def build_compaction_output(stats: "CompactionStats | None") -> str:
+ """Encode the tool row's output as JSON, always carrying ``summary``.
+
+ ``summary`` repeats ``COMPACTION_DONE_MSG`` verbatim so a client that
+ cannot parse the JSON — or a session persisted before this change —
+ still has a human-readable sentence to fall back on. A dropped
+ history gets ``COMPACTION_DROPPED_MSG`` and ``dropped: true`` instead,
+ so neither a parsing client nor a legacy one can read a reset as a
+ summary.
+ """
+ if stats is not None and stats.dropped:
+ payload: dict[str, Any] = {"summary": COMPACTION_DROPPED_MSG, "dropped": True}
+ else:
+ payload = {"summary": COMPACTION_DONE_MSG}
+ if stats is not None:
+ payload.update(stats.to_wire())
+ return json.dumps(payload)
+
+
+# ---------------------------------------------------------------------------
+# Sizing the SDK-internal path
+# ---------------------------------------------------------------------------
+
+logger = logging.getLogger(__name__)
+
+
+def _is_message_entry(entry: dict) -> bool:
+ return entry.get("type") in ("user", "assistant") or bool(
+ entry.get("isCompactSummary")
+ )
+
+
+def _entry_text(entry: dict) -> str:
+ """The text the model reads from one CLI transcript entry."""
+ content = (entry.get("message") or {}).get("content")
+ if isinstance(content, str):
+ return content
+ if not isinstance(content, list):
+ return ""
+ parts: list[str] = []
+ for block in content:
+ if not isinstance(block, dict):
+ continue
+ kind = block.get("type")
+ if kind == "text":
+ parts.append(str(block.get("text") or ""))
+ elif kind == "thinking":
+ parts.append(str(block.get("thinking") or ""))
+ elif kind == "tool_use":
+ parts.append(json.dumps(block.get("input") or {}, default=str))
+ elif kind == "tool_result":
+ result = block.get("content")
+ parts.append(
+ result if isinstance(result, str) else json.dumps(result, default=str)
+ )
+ return "\n".join(parts)
+
+
+def _measure_transcript(
+ entries: Iterable[dict], *, model: str
+) -> tuple[int | None, int | None]:
+ """``(tokens, turns)`` for CLI transcript entries; ``(None, None)`` if empty.
+
+ Turns are user and assistant entries (tool results are user entries in
+ the CLI format) plus the compaction summary. Tokens are estimated over
+ their text with the tokenizer ``compress_context`` measures the
+ pre-query path with, so the two paths report comparable numbers. The
+ estimate is cosmetic: a tokenizer failure yields no count rather than
+ an error.
+ """
+ rows = [e for e in entries if _is_message_entry(e)]
+ if not rows:
+ return None, None
+ try:
+ tokens = estimate_token_count_str(
+ "\n".join(_entry_text(e) for e in rows), model=model
+ )
+ except Exception:
+ logger.warning(
+ "[SDK] Could not size %d transcript entries for the compaction row",
+ len(rows),
+ exc_info=True,
+ )
+ tokens = 0
+ return (tokens or None), len(rows)
+
+
+def transcript_stats(entries: Iterable[dict], *, model: str) -> CompactionStats:
+ """Size the context the CLI is about to condense: the before-counts."""
+ tokens, turns = _measure_transcript(entries, model=model)
+ return CompactionStats(tokens_before=tokens, messages_before=turns)
+
+
+def sdk_compaction_stats(
+ before: Iterable[dict],
+ compacted: list[dict] | None,
+ *,
+ model: str,
+ start: "CompactionStats | None" = None,
+) -> CompactionStats:
+ """Before/after counts for one CLI-side compaction cycle.
+
+ *before* is the transcript builder's mirror of the CLI context prior to
+ compaction; *compacted* is what ``read_compacted_entries`` found in the
+ session file afterwards — ``None`` when it could not be read, in which
+ case only the before-counts are reported and the card falls back to
+ its generic copy. *start* reuses the counts measured when the row
+ opened so a cycle is not tokenized twice.
+ """
+ stats = start if start is not None else transcript_stats(before, model=model)
+ if compacted is None:
+ return stats
+ tokens, turns = _measure_transcript(compacted, model=model)
+ return stats.model_copy(update={"tokens_after": tokens, "messages_after": turns})
+
+
+def _progress(
+ phase: CompactionPhase, stats: "CompactionStats | None" = None
+) -> StreamCompactionProgress:
+ stats = stats or CompactionStats()
+ return StreamCompactionProgress(phase=phase, **stats.to_wire())
+
+
# ---------------------------------------------------------------------------
# Event builders (private — use CompactionTracker or compaction_events)
# ---------------------------------------------------------------------------
@@ -91,8 +259,9 @@ def emit_compaction(session: ChatSession) -> list[StreamBaseResponse]:
legacy non-SDK streaming path in ``service.py``).
"""
tc_id = _new_tool_call_id()
- evts = compaction_events(COMPACTION_DONE_MSG, tool_call_id=tc_id)
- _persist(session, tc_id, COMPACTION_DONE_MSG)
+ output = build_compaction_output(None)
+ evts = compaction_events(output, tool_call_id=tc_id)
+ _persist(session, tc_id, output)
return evts
@@ -183,20 +352,30 @@ class CompactionTracker:
Two compaction paths:
1. **Pre-query** — history compressed before the SDK query starts.
- Call :meth:`emit_pre_query` to yield a self-contained tool call.
+ Call :meth:`emit_pre_query_start` to open the row before the work
+ runs, then :meth:`emit_pre_query_end` (or :meth:`abort_pre_query`
+ if the prediction was wrong) to close it.
2. **SDK-internal** — ``PreCompact`` hook fires mid-stream.
Call :meth:`emit_start_if_ready` on heartbeat ticks and
- :meth:`emit_end_if_ready` when a message arrives.
+ :meth:`emit_end_if_ready` when a message arrives. The hook also
+ sets :attr:`hook_fired`; the SDK message loop waits on it so the
+ row opens as soon as the hook lands instead of at the next
+ heartbeat — otherwise a compaction that finishes inside one
+ heartbeat interval collapses into a self-contained row and the
+ live bar never shows.
"""
def __init__(self) -> None:
+ self.hook_fired = asyncio.Event()
self._start_emitted = False
+ self._start_stats: CompactionStats | None = None
self._tool_call_id = ""
self._active_transcript_path: str = ""
self._pending_transcript_paths: deque[str] = deque()
self._attempted_sources: list[str] = []
self._completed_sources: list[str] = []
+ self._pre_query_tool_call_id: str = ""
@property
def attempt_count(self) -> int:
@@ -239,43 +418,155 @@ class CompactionTracker:
"""Callback for the PreCompact hook. Queues an SDK compaction attempt."""
self._attempted_sources.append("sdk_internal")
self._pending_transcript_paths.append(transcript_path)
+ self.hook_fired.set()
# ------------------------------------------------------------------
# Pre-query compaction
# ------------------------------------------------------------------
- def emit_pre_query(self, session: ChatSession) -> list[StreamBaseResponse]:
- """Emit + persist a self-contained compaction tool call."""
+ def emit_pre_query_start(
+ self, tokens_before: int | None = None
+ ) -> list[StreamBaseResponse]:
+ """Open a compaction row BEFORE the compression work runs.
+
+ The row stays open — no output event — until
+ :meth:`emit_pre_query_end` or :meth:`abort_pre_query` closes it,
+ so the progress bar spans the real work instead of appearing
+ after it.
+
+ *tokens_before* is the pre-check's token estimate for the slice about
+ to be compressed. It rides the ``summarizing`` phase so the client can
+ pace its progress curve against the real size of the work — without it
+ every compaction, 20K or 500K, animates on the same floor. Optional
+ because the SDK-internal path has no estimate to offer.
+
+ Deliberately does NOT record an attempt: the caller opens this row
+ on a *prediction* (``_will_compact``), and a prediction that turns
+ out wrong is retired by :meth:`abort_pre_query`. Counting it here
+ would inflate ``compaction_attempt_count`` with compactions that
+ never ran. :meth:`emit_pre_query_end` records the attempt.
+ """
+ self._pre_query_tool_call_id = _new_tool_call_id()
+ return [
+ *_start_events(self._pre_query_tool_call_id),
+ _progress("summarizing", CompactionStats(tokens_before=tokens_before)),
+ ]
+
+ def emit_pre_query_end(
+ self, session: ChatSession, stats: "CompactionStats | None"
+ ) -> list[StreamBaseResponse]:
+ """Close the pre-query row and hand off to the rebuild phase."""
+ output = build_compaction_output(stats)
+ tc_id = self._pre_query_tool_call_id
+ if tc_id:
+ events: list[StreamBaseResponse] = list(_end_events(tc_id, output))
+ else:
+ # No open row (the pre-check missed) — emit a self-contained one.
+ tc_id = _new_tool_call_id()
+ events = list(_start_events(tc_id) + _end_events(tc_id, output))
+ self._pre_query_tool_call_id = ""
self._attempted_sources.append("pre_query")
- self._completed_sources.append("pre_query")
- return emit_compaction(session)
+ # A drop closes the row honestly but is not a compaction that
+ # happened — it must not inflate ``compaction_count``.
+ if stats is None or not stats.dropped:
+ self._completed_sources.append("pre_query")
+ _persist(session, tc_id, output)
+ events.append(_progress("rebuilding", stats))
+ return events
+
+ def abort_pre_query(self) -> list[StreamBaseResponse]:
+ """Close an optimistically-opened row when no compaction happened.
+
+ The pre-check in ``stream_chat_completion_sdk`` predicts compaction
+ from a token estimate; when the estimate is wrong the row must be
+ retired without persisting anything, so a refresh doesn't replay a
+ compaction that never occurred.
+
+ The empty ``output`` is the sentinel that tells the client this row
+ is retired rather than completed, and it is the only signal needed:
+ the client treats phases left behind by a retired row as stale and
+ stops animating. Emitting a trailing phase here would instead claim
+ a stage that never ran.
+ """
+ tc_id = self._pre_query_tool_call_id
+ if not tc_id:
+ return []
+ self._pre_query_tool_call_id = ""
+ return list(_end_events(tc_id, ""))
# ------------------------------------------------------------------
# SDK-internal compaction
# ------------------------------------------------------------------
def reset_for_query(self) -> None:
- """Reset per-query state before a new SDK query."""
+ """Reset per-query state before a new SDK query.
+
+ The pre-query row cycle always closes before the query starts, so a
+ pre-query id still set here is stale by definition. Dropping it keeps
+ a broken call order from attaching this query's compaction to a row
+ the client no longer has open.
+ """
self._start_emitted = False
+ self._start_stats = None
self._tool_call_id = ""
self._active_transcript_path = ""
self._pending_transcript_paths.clear()
+ self.hook_fired.clear()
+ self._pre_query_tool_call_id = ""
+
+ @property
+ def has_pending_start(self) -> bool:
+ """A PreCompact hook fired and its row has not been opened yet."""
+ return bool(self._pending_transcript_paths) and not self._start_emitted
+
+ @property
+ def pending_transcript_path(self) -> str | None:
+ """Session file of the compaction the next SDK message will close.
+
+ ``None`` when no cycle is in flight. The path itself may be empty
+ when the hook carried none — the caller still closes the row, it
+ just cannot read the compacted entries.
+ """
+ if self._start_emitted:
+ return self._active_transcript_path
+ if self._pending_transcript_paths:
+ return self._pending_transcript_paths[0]
+ return None
- def emit_start_if_ready(self) -> list[StreamBaseResponse]:
- """If the PreCompact hook fired, emit start events (spinning tool)."""
+ @property
+ def start_stats(self) -> "CompactionStats | None":
+ """Counts measured when the open row was emitted, if any."""
+ return self._start_stats
+
+ def emit_start_if_ready(
+ self, stats: "CompactionStats | None" = None
+ ) -> list[StreamBaseResponse]:
+ """If the PreCompact hook fired, emit start events (spinning tool).
+
+ *stats* carries the before-counts measured off the transcript
+ builder; ``tokens_before`` paces the client's curve and the
+ counts are kept for the settled row (:attr:`start_stats`).
+ """
if self._pending_transcript_paths and not self._start_emitted:
self._start_emitted = True
+ self._start_stats = stats
self._tool_call_id = _new_tool_call_id()
self._active_transcript_path = self._pending_transcript_paths.popleft()
- return _start_events(self._tool_call_id)
+ return [*_start_events(self._tool_call_id), _progress("summarizing", stats)]
return []
- async def emit_end_if_ready(self, session: ChatSession) -> CompactionResult:
+ async def emit_end_if_ready(
+ self, session: ChatSession, stats: "CompactionStats | None" = None
+ ) -> CompactionResult:
"""If compaction is in progress, emit end events and persist.
Returns a ``CompactionResult`` with ``just_ended=True`` and the
captured ``transcript_path`` when a compaction cycle completes.
This avoids a separate flag check (TOCTOU-safe).
+
+ *stats* is the measured before/after (see
+ :func:`sdk_compaction_stats`); it lands in the persisted output and
+ rides the ``rebuilding`` phase.
"""
# Yield so pending hook tasks can set compact_start
await asyncio.sleep(0)
@@ -283,17 +574,17 @@ class CompactionTracker:
if not self._start_emitted and not self._pending_transcript_paths:
return CompactionResult()
+ output = build_compaction_output(stats)
+
if self._start_emitted:
# Close the open spinner
- done_events = _end_events(self._tool_call_id, COMPACTION_DONE_MSG)
+ done_events = _end_events(self._tool_call_id, output)
persist_id = self._tool_call_id
transcript_path = self._active_transcript_path
else:
# PreCompact fired but start never emitted — self-contained
persist_id = _new_tool_call_id()
- done_events = compaction_events(
- COMPACTION_DONE_MSG, tool_call_id=persist_id
- )
+ done_events = compaction_events(output, tool_call_id=persist_id)
transcript_path = (
self._pending_transcript_paths.popleft()
if self._pending_transcript_paths
@@ -301,10 +592,12 @@ class CompactionTracker:
)
self._start_emitted = False
+ self._start_stats = None
self._tool_call_id = ""
self._active_transcript_path = ""
self._completed_sources.append("sdk_internal")
- _persist(session, persist_id, COMPACTION_DONE_MSG)
+ _persist(session, persist_id, output)
+ done_events.append(_progress("rebuilding", stats))
return CompactionResult(
events=done_events, just_ended=True, transcript_path=transcript_path
)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/compaction_test.py b/autogpt_platform/backend/backend/copilot/sdk/compaction_test.py
index d48b977701..29c4f1e78b 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/compaction_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/compaction_test.py
@@ -1,11 +1,18 @@
"""Tests for sdk/compaction.py — event builders, filtering, persistence, and
CompactionTracker state machine."""
+import json as stdlib_json
+
import pytest
-from backend.copilot.constants import COMPACTION_DONE_MSG, COMPACTION_TOOL_NAME
+from backend.copilot.constants import (
+ COMPACTION_DONE_MSG,
+ COMPACTION_DROPPED_MSG,
+ COMPACTION_TOOL_NAME,
+)
from backend.copilot.model import ChatMessage, ChatSession
from backend.copilot.response_model import (
+ StreamCompactionProgress,
StreamFinishStep,
StreamStartStep,
StreamToolInputAvailable,
@@ -13,10 +20,14 @@ from backend.copilot.response_model import (
StreamToolOutputAvailable,
)
from backend.copilot.sdk.compaction import (
+ CompactionStats,
CompactionTracker,
+ build_compaction_output,
compaction_events,
emit_compaction,
filter_compaction_messages,
+ sdk_compaction_stats,
+ transcript_stats,
)
# ---------------------------------------------------------------------------
@@ -82,7 +93,10 @@ class TestEmitCompaction:
== COMPACTION_TOOL_NAME
)
assert session.messages[1].role == "tool"
- assert session.messages[1].content == COMPACTION_DONE_MSG
+ assert (
+ stdlib_json.loads(session.messages[1].content or "")["summary"]
+ == COMPACTION_DONE_MSG
+ )
# ---------------------------------------------------------------------------
@@ -168,6 +182,55 @@ class TestCompactionTracker:
assert tracker.attempt_count == 1
assert list(tracker._pending_transcript_paths) == [""]
+ def test_on_compact_sets_hook_fired(self):
+ tracker = CompactionTracker()
+ assert not tracker.hook_fired.is_set()
+ tracker.on_compact()
+ assert tracker.hook_fired.is_set()
+
+ def test_pending_transcript_path_follows_the_cycle(self):
+ tracker = CompactionTracker()
+ assert tracker.pending_transcript_path is None
+ assert tracker.has_pending_start is False
+ tracker.on_compact("/tmp/session.jsonl")
+ assert tracker.has_pending_start is True
+ assert tracker.pending_transcript_path == "/tmp/session.jsonl"
+ tracker.emit_start_if_ready()
+ assert tracker.has_pending_start is False
+ assert tracker.pending_transcript_path == "/tmp/session.jsonl"
+
+ @pytest.mark.asyncio
+ async def test_start_stats_pace_the_bar_and_end_stats_settle_the_row(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.on_compact("/tmp/session.jsonl")
+ start = tracker.emit_start_if_ready(
+ CompactionStats(tokens_before=128_000, messages_before=412)
+ )
+ assert isinstance(start[3], StreamCompactionProgress)
+ assert start[3].tokensBefore == 128_000
+ assert tracker.start_stats is not None
+ assert tracker.start_stats.messages_before == 412
+
+ result = await tracker.emit_end_if_ready(
+ session,
+ CompactionStats(
+ tokens_before=128_000,
+ tokens_after=31_000,
+ messages_before=412,
+ messages_after=38,
+ ),
+ )
+ assert tracker.pending_transcript_path is None
+ assert tracker.start_stats is None
+ parsed = stdlib_json.loads(session.messages[1].content or "")
+ assert parsed["tokensAfter"] == 31_000
+ assert parsed["messagesBefore"] == 412
+ rebuilding = result.events[-1]
+ assert isinstance(rebuilding, StreamCompactionProgress)
+ assert rebuilding.phase == "rebuilding"
+ assert rebuilding.tokensAfter == 31_000
+
def test_emit_start_if_ready_no_event(self):
tracker = CompactionTracker()
assert tracker.emit_start_if_ready() == []
@@ -176,16 +239,18 @@ class TestCompactionTracker:
tracker = CompactionTracker()
tracker.on_compact()
evts = tracker.emit_start_if_ready()
- assert len(evts) == 3
+ assert len(evts) == 4
assert isinstance(evts[0], StreamStartStep)
assert isinstance(evts[1], StreamToolInputStart)
assert isinstance(evts[2], StreamToolInputAvailable)
+ assert isinstance(evts[3], StreamCompactionProgress)
+ assert evts[3].phase == "summarizing"
def test_emit_start_only_once(self):
tracker = CompactionTracker()
tracker.on_compact()
evts1 = tracker.emit_start_if_ready()
- assert len(evts1) == 3
+ assert len(evts1) == 4
# Second call should return empty
evts2 = tracker.emit_start_if_ready()
assert evts2 == []
@@ -198,9 +263,11 @@ class TestCompactionTracker:
tracker.emit_start_if_ready()
result = await tracker.emit_end_if_ready(session)
assert result.just_ended is True
- assert len(result.events) == 2
+ assert len(result.events) == 3
assert isinstance(result.events[0], StreamToolOutputAvailable)
assert isinstance(result.events[1], StreamFinishStep)
+ assert isinstance(result.events[2], StreamCompactionProgress)
+ assert result.events[2].phase == "rebuilding"
# Should persist
assert len(session.messages) == 2
@@ -214,8 +281,9 @@ class TestCompactionTracker:
# Don't call emit_start_if_ready
result = await tracker.emit_end_if_ready(session)
assert result.just_ended is True
- assert len(result.events) == 5 # Full self-contained event
+ assert len(result.events) == 6 # Full self-contained event + rebuilding phase
assert isinstance(result.events[0], StreamStartStep)
+ assert isinstance(result.events[-1], StreamCompactionProgress)
assert len(session.messages) == 2
@pytest.mark.asyncio
@@ -239,11 +307,13 @@ class TestCompactionTracker:
assert result.just_ended is False
assert result.events == []
- def test_emit_pre_query(self):
+ def test_emit_pre_query_start_then_end(self):
tracker = CompactionTracker()
session = _make_session()
- evts = tracker.emit_pre_query(session)
- assert len(evts) == 5
+ start_evts = tracker.emit_pre_query_start()
+ end_evts = tracker.emit_pre_query_end(session, None)
+ assert len(start_evts) == 4
+ assert len(end_evts) == 3
assert len(session.messages) == 2
assert tracker.attempt_count == 1
assert tracker.completed_count == 1
@@ -266,15 +336,27 @@ class TestCompactionTracker:
assert tracker._active_transcript_path == ""
assert list(tracker._pending_transcript_paths) == []
+ def test_reset_for_query_forgets_pre_query_row(self):
+ """A pre-query row id never crosses the query boundary."""
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query_start()
+ tracker.reset_for_query()
+ assert tracker._pre_query_tool_call_id == ""
+ assert tracker.abort_pre_query() == []
+ end_evts = tracker.emit_pre_query_end(session, None)
+ assert any(isinstance(e, StreamToolInputStart) for e in end_evts)
+
@pytest.mark.asyncio
async def test_pre_query_does_not_block_sdk_compaction_within_query(self):
"""SDK auto-compaction can still fire after a pre-query compaction."""
tracker = CompactionTracker()
session = _make_session()
- tracker.emit_pre_query(session)
+ tracker.emit_pre_query_start()
+ tracker.emit_pre_query_end(session, None)
tracker.on_compact()
evts = tracker.emit_start_if_ready()
- assert len(evts) == 3
+ assert len(evts) == 4
result = await tracker.emit_end_if_ready(session)
assert result.just_ended is True
assert tracker.completed_count == 2
@@ -284,11 +366,12 @@ class TestCompactionTracker:
"""After reset_for_query, compaction can fire again."""
tracker = CompactionTracker()
session = _make_session()
- tracker.emit_pre_query(session)
+ tracker.emit_pre_query_start()
+ tracker.emit_pre_query_end(session, None)
tracker.reset_for_query()
tracker.on_compact()
evts = tracker.emit_start_if_ready()
- assert len(evts) == 3 # Start events emitted
+ assert len(evts) == 4 # Start events emitted
@pytest.mark.asyncio
async def test_tool_call_id_consistency(self):
@@ -319,13 +402,13 @@ class TestCompactionTracker:
tracker.emit_start_if_ready()
result1 = await tracker.emit_end_if_ready(session)
assert result1.just_ended is True
- assert len(result1.events) == 2
+ assert len(result1.events) == 3
assert result1.transcript_path == "/path/1"
# Second compaction cycle in the same query
tracker.on_compact("/path/2")
start_evts = tracker.emit_start_if_ready()
- assert len(start_evts) == 3
+ assert len(start_evts) == 4
result2 = await tracker.emit_end_if_ready(session)
assert result2.just_ended is True
assert result2.transcript_path == "/path/2"
@@ -350,7 +433,7 @@ class TestCompactionTracker:
# Second compaction in new query
tracker.on_compact("/path/2")
start_evts = tracker.emit_start_if_ready()
- assert len(start_evts) == 3
+ assert len(start_evts) == 4
result2 = await tracker.emit_end_if_ready(session)
assert result2.just_ended is True
assert result2.transcript_path == "/path/2"
@@ -413,7 +496,8 @@ class TestCompactionTracker:
tracker = CompactionTracker()
session = _make_session()
- tracker.emit_pre_query(session)
+ tracker.emit_pre_query_start()
+ tracker.emit_pre_query_end(session, None)
tracker.on_compact("/path/1")
tracker.on_compact("/path/2")
@@ -428,7 +512,8 @@ class TestCompactionTracker:
tracker = CompactionTracker()
session = _make_session()
- tracker.emit_pre_query(session)
+ tracker.emit_pre_query_start()
+ tracker.emit_pre_query_end(session, None)
tracker.on_compact("/path/1")
tracker.on_compact("/path/2")
@@ -438,3 +523,307 @@ class TestCompactionTracker:
"completed_count": 1,
"completed_sources": "pre_query",
}
+
+
+# ---------------------------------------------------------------------------
+# build_compaction_output
+# ---------------------------------------------------------------------------
+
+
+class TestBuildCompactionOutput:
+ def test_encodes_stats_as_json_with_summary(self):
+ out = build_compaction_output(
+ CompactionStats(
+ tokens_before=128_000,
+ tokens_after=31_000,
+ messages_before=412,
+ messages_after=38,
+ )
+ )
+ parsed = stdlib_json.loads(out)
+ assert parsed["summary"] == COMPACTION_DONE_MSG
+ assert parsed["tokensBefore"] == 128_000
+ assert parsed["tokensAfter"] == 31_000
+ assert parsed["messagesBefore"] == 412
+ assert parsed["messagesAfter"] == 38
+
+ def test_omits_unknown_fields(self):
+ parsed = stdlib_json.loads(build_compaction_output(CompactionStats()))
+ assert parsed == {"summary": COMPACTION_DONE_MSG}
+
+ def test_none_stats_still_carries_summary(self):
+ parsed = stdlib_json.loads(build_compaction_output(None))
+ assert parsed == {"summary": COMPACTION_DONE_MSG}
+
+ def test_dropped_history_is_reported_as_dropped(self):
+ parsed = stdlib_json.loads(
+ build_compaction_output(CompactionStats(dropped=True, messages_before=9))
+ )
+ assert parsed == {
+ "summary": COMPACTION_DROPPED_MSG,
+ "dropped": True,
+ "messagesBefore": 9,
+ }
+ # A payload fact, not a wire stat: progress events never carry it.
+ assert "dropped" not in CompactionStats(dropped=True).to_wire()
+
+
+# ---------------------------------------------------------------------------
+# Pre-query split emitters
+# ---------------------------------------------------------------------------
+
+
+class TestPreQueryEmitters:
+ def test_start_emits_open_row_and_summarizing_phase(self):
+ tracker = CompactionTracker()
+ evts = tracker.emit_pre_query_start()
+ assert isinstance(evts[0], StreamStartStep)
+ assert isinstance(evts[1], StreamToolInputStart)
+ assert isinstance(evts[2], StreamToolInputAvailable)
+ assert isinstance(evts[3], StreamCompactionProgress)
+ assert evts[3].phase == "summarizing"
+ assert not any(isinstance(e, StreamToolOutputAvailable) for e in evts)
+
+ def test_end_closes_the_same_tool_call_id(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ start = tracker.emit_pre_query_start()
+ tool_start = start[1]
+ assert isinstance(tool_start, StreamToolInputStart)
+
+ end = tracker.emit_pre_query_end(session, CompactionStats(tokens_before=9))
+ output_evt = end[0]
+ assert isinstance(output_evt, StreamToolOutputAvailable)
+ assert output_evt.toolCallId == tool_start.toolCallId
+ assert isinstance(end[1], StreamFinishStep)
+ assert isinstance(end[2], StreamCompactionProgress)
+ assert end[2].phase == "rebuilding"
+
+ def test_end_persists_json_output_to_session(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query_start()
+ tracker.emit_pre_query_end(
+ session, CompactionStats(tokens_before=100, tokens_after=10)
+ )
+ assert len(session.messages) == 2
+ assert session.messages[1].role == "tool"
+ parsed = stdlib_json.loads(session.messages[1].content or "")
+ assert parsed["tokensBefore"] == 100
+ assert parsed["tokensAfter"] == 10
+
+ def test_dropped_end_closes_honestly_without_counting_a_compaction(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query_start()
+ evts = tracker.emit_pre_query_end(
+ session, CompactionStats(dropped=True, messages_before=9)
+ )
+ assert isinstance(evts[0], StreamToolOutputAvailable)
+ parsed = stdlib_json.loads(session.messages[1].content or "")
+ assert parsed["dropped"] is True
+ assert parsed["summary"] == COMPACTION_DROPPED_MSG
+ assert tracker.attempt_sources == ("pre_query",)
+ assert tracker.completed_sources == ()
+
+ def test_end_without_start_is_self_contained(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ evts = tracker.emit_pre_query_end(session, None)
+ assert isinstance(evts[0], StreamStartStep)
+ assert isinstance(evts[3], StreamToolOutputAvailable)
+ assert isinstance(evts[4], StreamFinishStep)
+ assert isinstance(evts[5], StreamCompactionProgress)
+ assert len(session.messages) == 2
+
+ def test_abort_closes_row_without_persisting(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query_start()
+ evts = tracker.abort_pre_query()
+ assert isinstance(evts[0], StreamToolOutputAvailable)
+ # The empty output IS the retirement sentinel — a client that sees a
+ # summary here renders a compaction that never ran.
+ assert evts[0].output == ""
+ assert isinstance(evts[1], StreamFinishStep)
+ assert session.messages == []
+
+ def test_abort_claims_no_phase_of_its_own(self):
+ """A retired prediction reports the retirement, not a stage.
+
+ The empty-output sentinel is the whole signal: the client treats any
+ phase left behind by a retired row as stale and stops animating. A
+ trailing phase event here would name a stage that never ran — and
+ there is no honest value to send, since the row is being withdrawn
+ rather than advanced.
+ """
+ tracker = CompactionTracker()
+ tracker.emit_pre_query_start()
+ evts = tracker.abort_pre_query()
+ assert not [e for e in evts if isinstance(e, StreamCompactionProgress)]
+
+ def test_abort_is_a_noop_when_no_row_is_open(self):
+ tracker = CompactionTracker()
+ assert tracker.abort_pre_query() == []
+
+ def test_pre_query_counts_as_an_attempt_and_a_completion(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query_start()
+ # The counters record real work, not predictions — a row that is
+ # opened optimistically and then aborted must leave no trace.
+ assert tracker.attempt_sources == ()
+ tracker.emit_pre_query_end(session, None)
+ assert tracker.attempt_sources == ("pre_query",)
+ assert tracker.completed_sources == ("pre_query",)
+
+ def test_abort_records_neither_an_attempt_nor_a_completion(self):
+ tracker = CompactionTracker()
+ tracker.emit_pre_query_start()
+ tracker.abort_pre_query()
+ assert tracker.attempt_sources == ()
+ assert tracker.completed_sources == ()
+
+
+# ---------------------------------------------------------------------------
+# SDK-internal rebuilding phase
+# ---------------------------------------------------------------------------
+
+
+class TestSdkInternalRebuildingPhase:
+ @pytest.mark.asyncio
+ async def test_end_if_ready_appends_rebuilding_phase(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.on_compact("/tmp/session.jsonl")
+ tracker.emit_start_if_ready()
+ result = await tracker.emit_end_if_ready(session)
+ assert result.just_ended is True
+ assert isinstance(result.events[-1], StreamCompactionProgress)
+ assert result.events[-1].phase == "rebuilding"
+
+
+# ---------------------------------------------------------------------------
+# Pre-query ordering (row opens before the work, not after)
+# ---------------------------------------------------------------------------
+
+
+class TestPreQueryOrdering:
+ def test_start_carries_no_output_event(self):
+ """The opening events must leave the row OPEN.
+
+ ``emit_pre_query_start`` runs before the compression work, so any
+ output or finish event it carries would close the row while the work
+ is still running — the user sees a completed row and then sits
+ through the expensive part in silence. Closing is
+ ``emit_pre_query_end``'s job, or ``abort_pre_query``'s.
+ """
+ tracker = CompactionTracker()
+ evts = tracker.emit_pre_query_start()
+ assert not any(isinstance(e, StreamToolOutputAvailable) for e in evts)
+ assert not any(isinstance(e, StreamFinishStep) for e in evts)
+
+ def test_start_then_end_produces_exactly_one_persisted_row(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query_start()
+ tracker.emit_pre_query_end(session, CompactionStats(tokens_before=10))
+ assert len(session.messages) == 2
+
+ def test_aborted_prediction_persists_nothing(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query_start()
+ tracker.abort_pre_query()
+ assert session.messages == []
+
+ def test_error_between_start_and_end_can_still_be_closed_by_abort(self):
+ """Regression: an exception raised between start and end (e.g. the
+ compression work itself failing) must not leave a permanently-open
+ row on the client.
+
+ Scope: this covers the tracker primitive only — that ``abort_pre_query``
+ closes an open row and that a second call is a no-op. That
+ ``stream_chat_completion_sdk``'s except branch actually reaches for it
+ is a separate claim, covered end to end by
+ ``retry_scenarios_test.TestPreQueryCompactionRowTiming``.
+ """
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query_start()
+ try:
+ raise RuntimeError("compression blew up")
+ except RuntimeError:
+ close_evts = tracker.abort_pre_query()
+ assert len(close_evts) > 0
+ assert session.messages == []
+ # A second close (e.g. a defensive call on a later error path) is a
+ # no-op rather than emitting a duplicate close.
+ assert tracker.abort_pre_query() == []
+
+
+# ---------------------------------------------------------------------------
+# Sizing the SDK-internal path
+# ---------------------------------------------------------------------------
+
+
+class TestSdkCompactionStats:
+ BEFORE = [
+ {"type": "user", "message": {"role": "user", "content": "hello there"}},
+ {
+ "type": "assistant",
+ "message": {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "hi"},
+ {"type": "tool_use", "name": "find_block", "input": {"q": "email"}},
+ ],
+ },
+ },
+ {
+ "type": "user",
+ "message": {
+ "role": "user",
+ "content": [
+ {"type": "tool_result", "tool_use_id": "t1", "content": "3 blocks"}
+ ],
+ },
+ },
+ {"type": "progress", "message": {}},
+ ]
+ COMPACTED = [
+ {
+ "type": "summary",
+ "isCompactSummary": True,
+ "message": {"role": "user", "content": "Summary of the chat"},
+ }
+ ]
+
+ def test_counts_turns_and_tokens_before(self):
+ stats = transcript_stats(self.BEFORE, model="gpt-4o")
+ assert stats.messages_before == 3
+ assert stats.tokens_before is not None and stats.tokens_before > 0
+ assert stats.tokens_after is None
+ assert stats.messages_after is None
+
+ def test_after_counts_come_from_the_compacted_file(self):
+ stats = sdk_compaction_stats(self.BEFORE, self.COMPACTED, model="gpt-4o")
+ assert stats.messages_before == 3
+ assert stats.messages_after == 1
+ assert stats.tokens_after is not None
+ assert stats.tokens_after < (stats.tokens_before or 0)
+
+ def test_unreadable_compacted_file_keeps_only_the_before_counts(self):
+ stats = sdk_compaction_stats(self.BEFORE, None, model="gpt-4o")
+ assert stats.messages_before == 3
+ assert stats.messages_after is None
+
+ def test_reuses_the_counts_measured_at_start(self):
+ start = CompactionStats(tokens_before=999, messages_before=7)
+ stats = sdk_compaction_stats([], self.COMPACTED, model="gpt-4o", start=start)
+ assert stats.tokens_before == 999
+ assert stats.messages_before == 7
+ assert stats.messages_after == 1
+
+ def test_empty_transcript_has_no_counts(self):
+ assert transcript_stats([], model="gpt-4o") == CompactionStats()
diff --git a/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.py b/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.py
index a923aae0ae..eecb4bdc15 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.py
@@ -27,7 +27,7 @@ Compression unit tests
| L | [1 msg] | None | ([msg], False) — single-msg guard |
| M | [2+ msgs] | None | target_tokens=None forwarded to _run_compression |
| N | [2+ msgs] | 30_000 | target_tokens=30_000 forwarded |
-| O | [2+ msgs], run fails | None | returns originals, False |
+| O | [2+ msgs], run fails | None | drops history, no stats |
"""
from __future__ import annotations
@@ -38,6 +38,7 @@ from unittest.mock import AsyncMock, patch
import pytest
from backend.copilot.model import ChatMessage, ChatSession
+from backend.copilot.sdk.compaction import CompactionStats
from backend.copilot.sdk.service import _build_query_message, _compress_messages
from backend.util.prompt import CompressResult
@@ -69,7 +70,7 @@ def _passthrough_compress(target_tokens=None):
async def _mock(msgs, tok=None):
calls.append((msgs, tok))
- return msgs, False
+ return msgs, False, None
_mock.calls = calls # type: ignore[attr-defined]
return _mock
@@ -89,11 +90,11 @@ class TestBuildQueryMessageResume:
session = _make_session(
_msgs(("user", "q1"), ("assistant", "a1"), ("user", "q2"))
)
- result, compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"q2", session, use_resume=True, transcript_msg_count=2, session_id="s"
)
assert result == "q2"
- assert compacted is False
+ assert stats is None
@pytest.mark.asyncio
async def test_scenario_b_stale_transcript_injects_gap(self, monkeypatch):
@@ -109,13 +110,13 @@ class TestBuildQueryMessageResume:
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
)
- result, compacted = await _build_query_message(
+ result, _stats = await _build_query_message(
"q3", session, use_resume=True, transcript_msg_count=2, session_id="s"
)
assert "<conversation_history>" in result
@@ -141,7 +142,7 @@ class TestBuildQueryMessageResume:
async def _mock_compress(msgs, target_tokens=None):
captured.append(target_tokens)
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
@@ -169,13 +170,13 @@ class TestBuildQueryMessageNoResumeNoTranscript:
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
)
- result, compacted = await _build_query_message(
+ result, _stats = await _build_query_message(
"q2", session, use_resume=False, transcript_msg_count=0, session_id="s"
)
assert "<conversation_history>" in result
@@ -193,7 +194,7 @@ class TestBuildQueryMessageNoResumeNoTranscript:
async def _mock_compress(msgs, target_tokens=None):
captured.append(target_tokens)
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
@@ -235,7 +236,7 @@ class TestBuildQueryMessageNoResumeWithTranscript:
async def _mock_compress(msgs, target_tokens=None):
compressed_msgs.append(list(msgs))
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
@@ -275,7 +276,7 @@ class TestBuildQueryMessageNoResumeWithTranscript:
async def _mock_compress(msgs, target_tokens=None):
captured.append(target_tokens)
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
@@ -315,7 +316,7 @@ class TestBuildQueryMessageNoResumeWithTranscript:
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
@@ -346,7 +347,7 @@ class TestBuildQueryMessageNoResumeWithTranscript:
async def _mock_compress(msgs, target_tokens=None):
captured.append(target_tokens)
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
@@ -384,7 +385,7 @@ class TestBuildQueryMessageNoResumeWithTranscript:
async def _mock_compress(msgs, target_tokens=None):
nonlocal call_count
call_count += 1
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
@@ -409,17 +410,19 @@ class TestCompressMessages:
@pytest.mark.asyncio
async def test_scenario_k_empty_list_returns_empty(self):
"""Scenario K: empty input → short-circuit, no compression."""
- result, compacted = await _compress_messages([])
+ result, compacted, stats = await _compress_messages([])
assert result == []
assert compacted is False
+ assert stats is None
@pytest.mark.asyncio
async def test_scenario_l_single_message_returns_as_is(self):
"""Scenario L: single message → short-circuit (< 2 guard)."""
msg = ChatMessage(role="user", content="hello")
- result, compacted = await _compress_messages([msg])
+ result, compacted, stats = await _compress_messages([msg])
assert result == [msg]
assert compacted is False
+ assert stats is None
@pytest.mark.asyncio
async def test_scenario_m_target_tokens_none_forwarded(self):
@@ -466,12 +469,24 @@ class TestCompressMessages:
new_callable=AsyncMock,
return_value=fake_result,
) as mock_run:
- result, compacted = await _compress_messages(msgs, target_tokens=30_000)
+ result, compacted, stats = await _compress_messages(
+ msgs, target_tokens=30_000
+ )
mock_run.assert_awaited_once()
_, kwargs = mock_run.call_args
assert kwargs.get("target_tokens") == 30_000
assert compacted is True
+ # Pin the whole payoff frame, not just its existence: the before/after
+ # pairs are what the UI renders, and a swap between them would ship a
+ # backwards "31K → 128K tokens" with the suite still green.
+ assert stats == CompactionStats(
+ tokens_before=50,
+ tokens_after=5,
+ messages_before=2,
+ messages_after=1,
+ )
+ assert [(m.role, m.content) for m in result] == [("user", "summary")]
@pytest.mark.asyncio
async def test_scenario_o_run_compression_exception_drops_history(self):
@@ -485,6 +500,11 @@ class TestCompressMessages:
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).
+
+ It must ALSO say what happened. Nothing was summarized — the history
+ was thrown away — so the stats carry ``dropped`` and no token counts:
+ enough for the caller to close the row as a reset the user can read,
+ without claiming a summary or counting a completed compaction.
"""
msgs = [
ChatMessage(role="user", content="q"),
@@ -495,12 +515,43 @@ class TestCompressMessages:
new_callable=AsyncMock,
side_effect=RuntimeError("compression timeout"),
):
- result, compacted = await _compress_messages(msgs)
+ result, compacted, stats = await _compress_messages(msgs)
# History dropped, marked as compacted so callers know reduction
# occurred (current message goes alone, smallest possible payload).
assert result == []
assert compacted is True
+ assert stats == CompactionStats(dropped=True, messages_before=2)
+
+ @pytest.mark.asyncio
+ async def test_drop_path_reports_dropped_not_condensed(self):
+ """A dropped history must not surface as a completed compaction.
+
+ ``_build_query_message`` falls through to the bare message when the
+ drop leaves nothing to inject. The stats it hands back say
+ ``dropped`` with no token counts, so the caller closes the row as a
+ reset rather than persisting a claim that the history was summarized.
+ """
+ session = _make_session(
+ _msgs(("user", "q1"), ("assistant", "a1"), ("user", "q2"))
+ )
+ with patch(
+ "backend.copilot.sdk.service._run_compression",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("compression timeout"),
+ ):
+ result, stats = await _build_query_message(
+ "q2",
+ session,
+ use_resume=False,
+ transcript_msg_count=0,
+ session_id="s",
+ )
+
+ assert result == "q2"
+ assert stats == CompactionStats(dropped=True, messages_before=2)
+ assert stats.tokens_before is None
+ assert stats.tokens_after is None
@pytest.mark.asyncio
async def test_compaction_messages_filtered_before_compression(self):
@@ -559,18 +610,18 @@ class TestSingleMessageSessions:
async def test_no_resume_single_message_returns_bare(self):
"""First turn (1 message): no prior history to inject."""
session = _make_session([ChatMessage(role="user", content="hello")])
- result, compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"hello", session, use_resume=False, transcript_msg_count=0, session_id="s"
)
assert result == "hello"
- assert compacted is False
+ assert stats is None
@pytest.mark.asyncio
async def test_resume_single_message_returns_bare(self):
"""First turn with resume flag: transcript is empty so no gap."""
session = _make_session([ChatMessage(role="user", content="hello")])
- result, compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"hello", session, use_resume=True, transcript_msg_count=0, session_id="s"
)
assert result == "hello"
- assert compacted is False
+ assert stats is None
diff --git a/autogpt_platform/backend/backend/copilot/sdk/e2e_compaction_test.py b/autogpt_platform/backend/backend/copilot/sdk/e2e_compaction_test.py
index 727e6a6d31..e2b1161235 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/e2e_compaction_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/e2e_compaction_test.py
@@ -19,6 +19,7 @@ import asyncio
from backend.copilot.model import ChatSession
from backend.copilot.response_model import (
+ StreamCompactionProgress,
StreamFinishStep,
StreamStartStep,
StreamToolInputAvailable,
@@ -280,10 +281,12 @@ class TestCompactionE2E:
# --- Step 8: Next SDK message arrives → emit_start ---
start_events = tracker.emit_start_if_ready()
- assert len(start_events) == 3
+ assert len(start_events) == 4
assert isinstance(start_events[0], StreamStartStep)
assert isinstance(start_events[1], StreamToolInputStart)
assert isinstance(start_events[2], StreamToolInputAvailable)
+ assert isinstance(start_events[3], StreamCompactionProgress)
+ assert start_events[3].phase == "summarizing"
# Verify tool_call_id is set
tool_call_id = start_events[1].toolCallId
@@ -293,9 +296,11 @@ class TestCompactionE2E:
result = _run(tracker.emit_end_if_ready(session))
assert result.just_ended is True
assert result.transcript_path == str(session_file)
- assert len(result.events) == 2
+ assert len(result.events) == 3
assert isinstance(result.events[0], StreamToolOutputAvailable)
assert isinstance(result.events[1], StreamFinishStep)
+ assert isinstance(result.events[2], StreamCompactionProgress)
+ assert result.events[2].phase == "rebuilding"
# Verify same tool_call_id
assert result.events[0].toolCallId == tool_call_id
diff --git a/autogpt_platform/backend/backend/copilot/sdk/mode_switch_context_test.py b/autogpt_platform/backend/backend/copilot/sdk/mode_switch_context_test.py
index 9e0008e787..8b623b5e03 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/mode_switch_context_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/mode_switch_context_test.py
@@ -109,7 +109,7 @@ class TestFastToSdkModeSwitch:
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
@@ -117,7 +117,7 @@ class TestFastToSdkModeSwitch:
# transcript_msg_count=4: baseline uploaded a transcript covering all
# 4 prior messages, but use_resume=False (no CLI session from baseline).
- result, compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"sdk-q1",
session,
use_resume=False,
@@ -132,7 +132,7 @@ class TestFastToSdkModeSwitch:
assert "baseline-q2" in result
assert "baseline-a2" in result
assert "Now, the user says:\nsdk-q1" in result
- assert compacted is False
+ assert stats is None
@pytest.mark.asyncio
async def test_scenario_r_single_baseline_turn_injected(self, monkeypatch):
@@ -146,7 +146,7 @@ class TestFastToSdkModeSwitch:
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
@@ -187,7 +187,7 @@ class TestFastToSdkModeSwitch:
)
# transcript_msg_count=6 covers all prior messages → no gap.
- result, compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"sdk-q2",
session,
use_resume=True, # T2: --resume works after T1 set session_id
@@ -197,7 +197,7 @@ class TestFastToSdkModeSwitch:
# --resume has full context — bare message only.
assert result == "sdk-q2"
- assert compacted is False
+ assert stats is None
@pytest.mark.asyncio
async def test_mode_switch_t1_compresses_all_baseline_turns(self, monkeypatch):
@@ -219,7 +219,7 @@ class TestFastToSdkModeSwitch:
async def _mock_compress(msgs, target_tokens=None):
compressed_batches.append(list(msgs))
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages", _mock_compress
diff --git a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py
index 8a91cc08cf..9a139ac1fc 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py
@@ -7,6 +7,10 @@ from pydantic import ValidationError
from backend.copilot.config import ChatConfig
from backend.copilot.constants import is_transient_api_error
+from backend.copilot.model import ChatSession
+from backend.copilot.response_model import StreamCompactionProgress
+from backend.copilot.sdk.compaction import CompactionTracker
+from backend.copilot.sdk.service import _EPHEMERAL_EVENT_TYPES
def _make_config(**overrides) -> ChatConfig:
@@ -851,6 +855,39 @@ class TestEphemeralEventTypesConstant:
assert StreamHeartbeat in _EPHEMERAL_EVENT_TYPES
+ def test_compaction_progress_is_in_ephemeral_types(self):
+ """SDK-internal compaction emits progress phases inside the attempt
+ loop, alongside the tool-row events that are already exempt. Counting
+ them would make a transient failure after a compaction unretryable.
+ """
+ assert StreamCompactionProgress in _EPHEMERAL_EVENT_TYPES, (
+ "StreamCompactionProgress must be in _EPHEMERAL_EVENT_TYPES — a "
+ "cosmetic progress phase must not suppress the transient retry"
+ )
+
+ def test_every_compaction_row_event_is_ephemeral(self):
+ """The whole compaction row — open, close and progress — is cosmetic.
+
+ Guards the class of bug where a new compaction event type is added to
+ the stream but not exempted here.
+ """
+ tracker = CompactionTracker()
+ session = ChatSession.new(user_id="test-user", dry_run=False)
+ events = [
+ *tracker.emit_pre_query_start(),
+ *tracker.emit_pre_query_end(session, None),
+ ]
+ assert events
+ non_ephemeral = [
+ type(e).__name__
+ for e in events
+ if not isinstance(e, _EPHEMERAL_EVENT_TYPES)
+ ]
+ assert not non_ephemeral, (
+ f"compaction emits non-ephemeral event(s) {non_ephemeral} — they "
+ "would be counted toward events_yielded and block transient retries"
+ )
+
def test_is_a_tuple(self):
"""isinstance() requires a tuple (not a list) as second argument."""
from backend.copilot.sdk.service import _EPHEMERAL_EVENT_TYPES
diff --git a/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py b/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py
index 14dcd62d80..a90cba19c5 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py
@@ -5,6 +5,7 @@ from datetime import UTC, datetime
import pytest
from backend.copilot.model import ChatMessage, ChatSession
+from backend.copilot.sdk.compaction import CompactionStats
from backend.copilot.sdk.service import (
_BARE_MESSAGE_TOKEN_FLOOR,
_build_query_message,
@@ -119,7 +120,7 @@ async def test_build_query_resume_up_to_date():
ChatMessage(role="user", content="what's new?"),
]
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"what's new?",
session,
use_resume=True,
@@ -128,7 +129,7 @@ async def test_build_query_resume_up_to_date():
)
# transcript_msg_count == msg_count - 1, so no gap
assert result == "what's new?"
- assert was_compacted is False
+ assert stats is None
@pytest.mark.asyncio
@@ -147,7 +148,7 @@ async def test_build_query_resume_misaligned_watermark():
ChatMessage(role="user", content="turn 3"),
]
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"turn 3",
session,
use_resume=True,
@@ -156,7 +157,7 @@ async def test_build_query_resume_misaligned_watermark():
)
# Misaligned watermark → skip gap, return bare message
assert result == "turn 3"
- assert was_compacted is False
+ assert stats is None
@pytest.mark.asyncio
@@ -171,7 +172,7 @@ async def test_build_query_resume_stale_transcript():
ChatMessage(role="user", content="turn 3"),
]
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"turn 3",
session,
use_resume=True,
@@ -182,7 +183,7 @@ async def test_build_query_resume_stale_transcript():
assert "turn 2" in result
assert "reply 2" in result
assert "Now, the user says:\nturn 3" in result
- assert was_compacted is False # gap context does not compact
+ assert stats is None # gap context does not compact
@pytest.mark.asyncio
@@ -195,7 +196,7 @@ async def test_build_query_resume_zero_msg_count():
ChatMessage(role="user", content="new msg"),
]
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"new msg",
session,
use_resume=True,
@@ -203,14 +204,14 @@ async def test_build_query_resume_zero_msg_count():
session_id="test-session",
)
assert result == "new msg"
- assert was_compacted is False
+ assert stats is None
@pytest.mark.asyncio
async def test_build_query_no_resume_single_message():
"""Without --resume and only 1 message, return raw message."""
session = _make_session([ChatMessage(role="user", content="first")])
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"first",
session,
use_resume=False,
@@ -218,7 +219,7 @@ async def test_build_query_no_resume_single_message():
session_id="test-session",
)
assert result == "first"
- assert was_compacted is False
+ assert stats is None
@pytest.mark.asyncio
@@ -234,14 +235,14 @@ async def test_build_query_no_resume_multi_message(monkeypatch):
# Mock _compress_messages to return the messages as-is
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages",
_mock_compress,
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"new question",
session,
use_resume=False,
@@ -252,7 +253,7 @@ async def test_build_query_no_resume_multi_message(monkeypatch):
assert "older question" in result
assert "older answer" in result
assert "Now, the user says:\nnew question" in result
- assert was_compacted is False # mock returns False
+ assert stats is None # mock returns no stats
@pytest.mark.asyncio
@@ -276,7 +277,7 @@ async def test_build_query_session_msg_ceiling_prevents_pending_duplication():
]
)
# transcript covers hist1+hist2 (2 messages); pre-drain count was 3 (includes current_msg)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"current msg with pending1 pending2",
session,
use_resume=True,
@@ -286,7 +287,7 @@ async def test_build_query_session_msg_ceiling_prevents_pending_duplication():
)
# Gap should be empty (transcript_msg_count == ceiling - 1), so no history prepended
assert result == "current msg with pending1 pending2"
- assert was_compacted is False
+ assert stats is None
# Pending messages must NOT appear in gap context
assert "pending1" not in result.split("current msg")[0]
@@ -309,7 +310,7 @@ async def test_build_query_session_msg_ceiling_preserves_real_gap():
ChatMessage(role="user", content="pending2"),
]
)
- result, was_compacted = await _build_query_message(
+ result, _stats = await _build_query_message(
"current",
session,
use_resume=True,
@@ -345,7 +346,7 @@ async def test_build_query_session_msg_ceiling_suppresses_spurious_no_resume_fal
ChatMessage(role="user", content="What is 7 plus 7?"), # pending
]
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"What is 2 plus 2?\n\nWhat is 7 plus 7?",
session,
use_resume=False,
@@ -355,14 +356,14 @@ async def test_build_query_session_msg_ceiling_suppresses_spurious_no_resume_fal
)
# Should return current_message directly without wrapping in history context
assert result == "What is 2 plus 2?\n\nWhat is 7 plus 7?"
- assert was_compacted is False
+ assert stats is None
# Pending question must NOT appear in a spurious history section
assert "<conversation_history>" not in result
@pytest.mark.asyncio
async def test_build_query_no_resume_multi_message_compacted(monkeypatch):
- """When compression actually compacts, was_compacted should be True."""
+ """When compression actually compacts, stats should be non-None."""
session = _make_session(
[
ChatMessage(role="user", content="old"),
@@ -372,21 +373,22 @@ async def test_build_query_no_resume_multi_message_compacted(monkeypatch):
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, True # Simulate actual compaction
+ # Simulate actual compaction
+ return msgs, True, CompactionStats(messages_before=3, messages_after=3)
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages",
_mock_compress,
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"new",
session,
use_resume=False,
transcript_msg_count=0,
session_id="test-session",
)
- assert was_compacted is True
+ assert stats is not None
@pytest.mark.asyncio
@@ -404,7 +406,7 @@ async def test_build_query_no_resume_at_token_floor():
ChatMessage(role="user", content="new question"),
]
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"new question",
session,
use_resume=False,
@@ -414,7 +416,7 @@ async def test_build_query_no_resume_at_token_floor():
)
# At the floor threshold, no history is injected
assert result == "new question"
- assert was_compacted is False
+ assert stats is None
@pytest.mark.asyncio
@@ -427,7 +429,7 @@ async def test_build_query_no_resume_below_token_floor():
ChatMessage(role="user", content="new"),
]
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"new",
session,
use_resume=False,
@@ -436,7 +438,7 @@ async def test_build_query_no_resume_below_token_floor():
target_tokens=_BARE_MESSAGE_TOKEN_FLOOR - 1,
)
assert result == "new"
- assert was_compacted is False
+ assert stats is None
@pytest.mark.asyncio
@@ -451,14 +453,14 @@ async def test_build_query_no_resume_above_token_floor_compresses(monkeypatch):
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages",
_mock_compress,
)
- result, was_compacted = await _build_query_message(
+ result, _stats = await _build_query_message(
"new",
session,
use_resume=False,
@@ -493,7 +495,7 @@ async def test_build_query_resume_cap_engaged_uses_sequence_gap(monkeypatch):
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages",
@@ -507,7 +509,7 @@ async def test_build_query_resume_cap_engaged_uses_sequence_gap(monkeypatch):
lambda: fake_db,
)
- result, was_compacted = await _build_query_message(
+ result, stats = await _build_query_message(
"current turn",
session,
use_resume=True,
@@ -519,7 +521,7 @@ async def test_build_query_resume_cap_engaged_uses_sequence_gap(monkeypatch):
assert "cap-window-user" in result
assert "cap-window-asst" in result
assert "Now, the user says:\ncurrent turn" in result
- assert was_compacted is False
+ assert stats is None
@pytest.mark.asyncio
@@ -543,7 +545,7 @@ async def test_build_query_resume_cap_engaged_hole_fills_above_watermark(monkeyp
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages",
@@ -611,7 +613,7 @@ async def test_build_query_resume_cap_engaged_hole_fill_failure_uses_window_only
)
async def _mock_compress(msgs, target_tokens=None):
- return msgs, False
+ return msgs, False, None
monkeypatch.setattr(
"backend.copilot.sdk.service._compress_messages",
diff --git a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
index 4ab9195114..4385388ce0 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
@@ -22,10 +22,26 @@ Scenario matrix:
from __future__ import annotations
+import asyncio
+import contextlib
+import time
+from datetime import UTC, datetime
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
-
+from claude_agent_sdk import ResultMessage
+
+from backend.copilot.constants import COMPACTION_TOOL_NAME
+from backend.copilot.model import ChatMessage, ChatSession
+from backend.copilot.response_model import (
+ StreamCompactionProgress,
+ StreamError,
+ StreamToolInputAvailable,
+ StreamToolInputStart,
+ StreamToolOutputAvailable,
+)
+from backend.copilot.sdk.compaction import CompactionStats
+from backend.copilot.sdk.service import _RestoreResult, stream_chat_completion_sdk
from backend.copilot.transcript import (
TranscriptDownload,
_flatten_assistant_content,
@@ -1049,6 +1065,7 @@ def _make_sdk_patches(
agent_max_turns=1000,
claude_agent_max_budget_usd=100.0,
claude_agent_max_thinking_tokens=0,
+ claude_agent_autocompact_pct_override=0,
claude_agent_thinking_effort=None,
claude_agent_fallback_model=None,
# Real strings: the stamp path canonicalizes the model for
@@ -1919,6 +1936,102 @@ class TestStreamChatCompletionRetryIntegration:
), f"Expected 'retrying' or 'interrupted' in StreamStatus, got: {[e.message for e in status_events]}"
assert any(isinstance(e, StreamStart) for e in events)
+ @pytest.mark.asyncio
+ async def test_transient_retry_inside_reduced_attempt_reduces_once(self):
+ """A transient error inside a reduced-context attempt must not reduce again.
+
+ Attempt 1 dies with prompt-too-long, so attempt 2 opens a compaction
+ row, summarizes the transcript and persists the row. The SDK call on
+ that attempt then dies with ECONNRESET. The transient retry re-enters
+ the loop head without advancing ``attempt`` — it must reuse the
+ reduced context rather than summarize a second time and persist a
+ second "Condensed" row for the same compaction.
+ """
+ # Prior history is what makes the turn restore a transcript at all —
+ # a one-message session never calls ``download_transcript``.
+ session = ChatSession(
+ session_id="test-session-id",
+ user_id="test-user",
+ usage=[],
+ started_at=datetime.now(UTC),
+ updated_at=datetime.now(UTC),
+ messages=[
+ ChatMessage(role="user", content="prior question"),
+ ChatMessage(role="assistant", content="prior answer"),
+ ChatMessage(role="user", content="hello"),
+ ],
+ )
+ result_msg = self._make_result_message()
+ call_count = [0]
+
+ def _client_factory(*args, **kwargs):
+ call_count[0] += 1
+ if call_count[0] == 1:
+ return self._make_client_mock(raises_on_enter=True)
+ if call_count[0] == 2:
+ return self._make_client_mock_mid_stream_error(
+ error=Exception("ECONNRESET: connection reset by peer"),
+ )
+ return self._make_client_mock(result_message=result_msg)
+
+ original_transcript = _build_transcript(
+ [("user", "prior question"), ("assistant", "prior answer")]
+ )
+ compacted_transcript = _build_transcript(
+ [("user", "[summary]"), ("assistant", "summary reply")]
+ )
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=original_transcript,
+ compacted_transcript=compacted_transcript,
+ client_side_effect=_client_factory,
+ )
+
+ # The shared patches mock ``os.makedirs`` but not the session-file
+ # write, so ``process_cli_restore`` fails and the turn falls back to
+ # the DB path with no transcript to summarize. Hand it a valid
+ # restore so attempt 2 runs the real summarize branch.
+ patches.append(
+ (
+ f"{_SVC}.process_cli_restore",
+ dict(return_value=(original_transcript, True)),
+ )
+ )
+
+ events = []
+ mocks: dict[str, MagicMock] = {}
+ with contextlib.ExitStack() as stack:
+ stack.enter_context(patch(f"{_SVC}.asyncio.sleep", new_callable=AsyncMock))
+ for target, kwargs in patches:
+ mocks[target] = stack.enter_context(patch(target, **kwargs))
+ async for event in stream_chat_completion_sdk(
+ session_id="test-session-id",
+ message="hello",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ events.append(event)
+
+ assert call_count[0] == 3, f"Expected 3 SDK calls, got {call_count[0]}"
+ errors = [e for e in events if isinstance(e, StreamError)]
+ assert not errors, f"Unexpected StreamError: {errors}"
+ assert mocks[f"{_SVC}.compact_transcript"].await_count == 1
+ opened = [
+ e
+ for e in events
+ if isinstance(e, StreamToolInputStart)
+ and e.toolName == COMPACTION_TOOL_NAME
+ ]
+ assert len(opened) == 1, f"Expected one compaction row, got {len(opened)}"
+ persisted = [
+ tc
+ for m in session.messages
+ for tc in (m.tool_calls or [])
+ if tc.get("function", {}).get("name") == COMPACTION_TOOL_NAME
+ ]
+ assert len(persisted) == 1, f"Expected one persisted row, got {persisted}"
+
@pytest.mark.asyncio
async def test_resume_skipped_when_cli_session_missing(self):
"""When restore_cli_session returns False, --resume is NOT passed to ClaudeSDKClient.
@@ -1982,3 +2095,423 @@ class TestStreamChatCompletionRetryIntegration:
f"{captured_options}"
)
assert any(isinstance(e, StreamStart) for e in events)
+
+
+# ---------------------------------------------------------------------------
+# Pre-query compaction row — timing and lifecycle at generator level
+# ---------------------------------------------------------------------------
+
+
+class TestPreQueryCompactionRowTiming:
+ """The compaction row exists to narrate the wait, so it must SPAN it.
+
+ Opening and closing the row around work that already finished is the
+ regression this class pins; it has shipped twice, both times because the
+ predictor and the compressor were fed different inputs and the row was
+ only emitted after the fact. Asserting event ORDER is not enough — the
+ broken build emitted the same order, just with every event inside the
+ same 10ms. These tests assert the elapsed GAP.
+ """
+
+ _WORK_SECONDS = 0.25
+
+ def _make_result_message(self):
+ return ResultMessage(
+ subtype="success",
+ result="done",
+ duration_ms=100,
+ duration_api_ms=50,
+ is_error=False,
+ num_turns=1,
+ session_id="test-session-id",
+ )
+
+ def _make_client_mock(self, raises_on_enter=False, result_message=None):
+ async def _receive():
+ if result_message is not None:
+ yield result_message
+
+ client = MagicMock()
+ client.receive_response = _receive
+ client.query = AsyncMock()
+ client._transport = MagicMock()
+ client._transport.write = AsyncMock()
+ if raises_on_enter:
+ client.query.side_effect = Exception(
+ "prompt is too long (context_length_exceeded)"
+ )
+
+ cm = AsyncMock()
+ cm.__aenter__.return_value = client
+ cm.__aexit__.return_value = None
+ return cm
+
+ def _big_prior(self):
+ return [
+ ChatMessage(
+ role="user" if i % 2 == 0 else "assistant",
+ content="word " * 30_000,
+ sequence=i,
+ )
+ for i in range(8)
+ ]
+
+ def _session_with_history(self, prior):
+ return ChatSession(
+ session_id="test-session-id",
+ user_id="test-user",
+ usage=[],
+ started_at=datetime.now(UTC),
+ updated_at=datetime.now(UTC),
+ messages=[*prior, ChatMessage(role="user", content="hello", sequence=8)],
+ )
+
+ def _no_resume_restore(self, prior):
+ """Force the no-resume path with a transcript+gap context slice.
+
+ This is the shape production hits on essentially every no-resume turn
+ that has a baseline transcript — and the shape the first fix missed,
+ because the predictor read the full session while the compressor read
+ this list.
+ """
+ return _RestoreResult(
+ transcript_content="seeded",
+ transcript_covers_prefix=True,
+ use_resume=False,
+ resume_file=None,
+ transcript_msg_count=len(prior),
+ context_messages=list(prior),
+ )
+
+ def _slow_compress(self, calls):
+ async def _compress(msgs, target_tokens=None):
+ calls.append(len(msgs))
+ await asyncio.sleep(self._WORK_SECONDS)
+ return (
+ list(msgs)[:1],
+ True,
+ CompactionStats(
+ tokens_before=200_000,
+ tokens_after=20_000,
+ messages_before=len(msgs),
+ messages_after=1,
+ ),
+ )
+
+ return _compress
+
+ async def _run(self, session, extra_patches, client_factory):
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=_build_transcript([("user", "q"), ("assistant", "a")]),
+ compacted_transcript=_build_transcript([("user", "[summary]")]),
+ client_side_effect=client_factory,
+ )
+ patches = [p for p in patches if p[0] != f"{_SVC}.download_transcript"]
+ patches.extend(extra_patches)
+
+ timeline = []
+ with contextlib.ExitStack() as stack:
+ for target, kwargs in patches:
+ stack.enter_context(patch(target, **kwargs))
+ # The generator re-raises after yielding StreamError so the caller
+ # can roll back; the events already on the wire are what we assert.
+ with contextlib.suppress(Exception):
+ async for event in stream_chat_completion_sdk(
+ session_id="test-session-id",
+ message="hello",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ timeline.append((time.monotonic(), event))
+ return timeline
+
+ @staticmethod
+ def _compaction_marks(timeline):
+ opened = [
+ t
+ for t, e in timeline
+ if isinstance(e, StreamToolInputAvailable)
+ and e.toolName == COMPACTION_TOOL_NAME
+ ]
+ closed = [
+ t
+ for t, e in timeline
+ if isinstance(e, StreamToolOutputAvailable)
+ and e.toolName == COMPACTION_TOOL_NAME
+ ]
+ return opened, closed
+
+ @pytest.mark.asyncio
+ async def test_row_stays_open_across_the_compression_work(self):
+ prior = self._big_prior()
+ session = self._session_with_history(prior)
+ result_msg = self._make_result_message()
+ calls: list[int] = []
+
+ timeline = await self._run(
+ session,
+ [
+ (
+ f"{_SVC}._restore_cli_session_for_turn",
+ dict(
+ new_callable=AsyncMock,
+ return_value=self._no_resume_restore(prior),
+ ),
+ ),
+ (f"{_SVC}._compress_messages", dict(new=self._slow_compress(calls))),
+ ],
+ lambda *a, **k: self._make_client_mock(result_message=result_msg),
+ )
+
+ assert calls, "the no-resume branch never reached _compress_messages"
+ opened, closed = self._compaction_marks(timeline)
+ assert opened, "no compaction row was opened before the work"
+ assert closed, "the compaction row was never closed"
+ gap = closed[0] - opened[0]
+ assert gap >= self._WORK_SECONDS, (
+ f"the row opened and closed within {gap * 1000:.1f}ms — it was "
+ "emitted AFTER the compression work, so the user watched the wait "
+ "with nothing on screen"
+ )
+
+ @pytest.mark.asyncio
+ async def test_row_opens_before_the_compressor_is_called(self):
+ """Order, not just duration: the input event precedes the work."""
+ prior = self._big_prior()
+ session = self._session_with_history(prior)
+ result_msg = self._make_result_message()
+ marks: list[str] = []
+
+ async def _compress(msgs, target_tokens=None):
+ marks.append("compress")
+ return (
+ list(msgs)[:1],
+ True,
+ CompactionStats(messages_before=len(msgs), messages_after=1),
+ )
+
+ timeline = await self._run(
+ session,
+ [
+ (
+ f"{_SVC}._restore_cli_session_for_turn",
+ dict(
+ new_callable=AsyncMock,
+ return_value=self._no_resume_restore(prior),
+ ),
+ ),
+ (f"{_SVC}._compress_messages", dict(new=_compress)),
+ ],
+ lambda *a, **k: self._make_client_mock(result_message=result_msg),
+ )
+
+ assert marks == ["compress"]
+ opened, closed = self._compaction_marks(timeline)
+ assert opened and closed
+ # The row is opened by the predictor, so it must be on the wire before
+ # the compressor even runs — and the close after it.
+ assert opened[0] < closed[0]
+
+ @pytest.mark.asyncio
+ async def test_summarizing_phase_carries_the_token_estimate(self):
+ """Without ``tokensBefore`` the client's pacing collapses to its floor,
+ so a 500K compaction animates exactly like a 20K one."""
+ prior = self._big_prior()
+ session = self._session_with_history(prior)
+ result_msg = self._make_result_message()
+ calls: list[int] = []
+
+ timeline = await self._run(
+ session,
+ [
+ (
+ f"{_SVC}._restore_cli_session_for_turn",
+ dict(
+ new_callable=AsyncMock,
+ return_value=self._no_resume_restore(prior),
+ ),
+ ),
+ (f"{_SVC}._compress_messages", dict(new=self._slow_compress(calls))),
+ ],
+ lambda *a, **k: self._make_client_mock(result_message=result_msg),
+ )
+
+ summarizing = [
+ e
+ for _, e in timeline
+ if isinstance(e, StreamCompactionProgress) and e.phase == "summarizing"
+ ]
+ assert summarizing, "no summarizing phase was emitted"
+ assert summarizing[0].tokensBefore is not None
+ assert summarizing[0].tokensBefore > 100_000
+
+ @pytest.mark.asyncio
+ async def test_retry_opens_and_closes_its_own_row(self):
+ """The retry re-reduces context, so it gets its own row — opened
+ before ``_reduce_context`` runs and closed after the rebuild."""
+ prior = self._big_prior()
+ session = self._session_with_history(prior)
+ result_msg = self._make_result_message()
+ attempts = [0]
+
+ def _client_factory(*args, **kwargs):
+ attempts[0] += 1
+ if attempts[0] == 1:
+ return self._make_client_mock(raises_on_enter=True)
+ return self._make_client_mock(result_message=result_msg)
+
+ calls: list[int] = []
+ timeline = await self._run(
+ session,
+ [
+ (
+ f"{_SVC}._restore_cli_session_for_turn",
+ dict(
+ new_callable=AsyncMock,
+ return_value=self._no_resume_restore(prior),
+ ),
+ ),
+ (f"{_SVC}._compress_messages", dict(new=self._slow_compress(calls))),
+ ],
+ _client_factory,
+ )
+
+ assert attempts[0] == 2
+ opened, closed = self._compaction_marks(timeline)
+ assert len(opened) == 2, f"expected a row per attempt, got {len(opened)}"
+ assert len(closed) == 2
+ for i in (0, 1):
+ gap = closed[i] - opened[i]
+ assert (
+ gap >= self._WORK_SECONDS
+ ), f"attempt {i + 1}'s row spanned only {gap * 1000:.1f}ms"
+
+ @pytest.mark.asyncio
+ async def test_retry_that_reduces_nothing_retires_its_row(self):
+ """The opposite branch: the retry opened a row and then found nothing
+ to condense, so the row must be retired, not completed.
+
+ A dropped transcript is not a summarized one, and ``compact_transcript``
+ failing lands in the drop branch — so a close here would persist a
+ durable "Condensed" row for a conversation that was truncated instead.
+ """
+ prior = [
+ ChatMessage(role="user", content="hi", sequence=0),
+ ChatMessage(role="assistant", content="hello", sequence=1),
+ ]
+ session = self._session_with_history(prior)
+ result_msg = self._make_result_message()
+ attempts = [0]
+
+ def _client_factory(*args, **kwargs):
+ attempts[0] += 1
+ if attempts[0] == 1:
+ return self._make_client_mock(raises_on_enter=True)
+ return self._make_client_mock(result_message=result_msg)
+
+ async def _no_compression(msgs, target_tokens=None):
+ return list(msgs), False, None
+
+ timeline = await self._run(
+ session,
+ [
+ (
+ f"{_SVC}._restore_cli_session_for_turn",
+ dict(
+ new_callable=AsyncMock,
+ return_value=self._no_resume_restore(prior),
+ ),
+ ),
+ # Compaction fails, so ``_reduce_context`` drops the transcript.
+ (
+ f"{_SVC}.compact_transcript",
+ dict(new_callable=AsyncMock, return_value=None),
+ ),
+ (f"{_SVC}._compress_messages", dict(new=_no_compression)),
+ ],
+ _client_factory,
+ )
+
+ assert attempts[0] == 2
+ opened, closed = self._compaction_marks(timeline)
+ assert len(opened) == 1, "only the retry should have opened a row"
+ assert len(closed) == 1, "the retry's row was left open"
+
+ outputs = [
+ e.output
+ for _, e in timeline
+ if isinstance(e, StreamToolOutputAvailable)
+ and e.toolName == COMPACTION_TOOL_NAME
+ ]
+ assert outputs == [""], "the row was completed instead of retired"
+
+ persisted = [
+ m
+ for m in session.messages
+ if m.tool_calls
+ and any(
+ call.get("function", {}).get("name") == COMPACTION_TOOL_NAME
+ for call in m.tool_calls
+ )
+ ]
+ assert persisted == [], "a retired row must not survive a refresh"
+
+ @pytest.mark.asyncio
+ async def test_error_during_compression_closes_the_open_row(self):
+ """The generator's except branch must retire a row left open by a
+ failure between start and end — otherwise the client is stuck with a
+ compaction spinner that never resolves."""
+ prior = self._big_prior()
+ session = self._session_with_history(prior)
+
+ async def _explode(msgs, target_tokens=None):
+ raise RuntimeError("compression blew up")
+
+ timeline = await self._run(
+ session,
+ [
+ (
+ f"{_SVC}._restore_cli_session_for_turn",
+ dict(
+ new_callable=AsyncMock,
+ return_value=self._no_resume_restore(prior),
+ ),
+ ),
+ (f"{_SVC}._compress_messages", dict(new=_explode)),
+ ],
+ lambda *a, **k: self._make_client_mock(),
+ )
+
+ opened, closed = self._compaction_marks(timeline)
+ assert opened, "no row was opened"
+ assert len(closed) == len(opened), "an opened row was left unclosed"
+ assert any(isinstance(e, StreamError) for _, e in timeline)
+
+ @pytest.mark.asyncio
+ async def test_no_row_when_nothing_will_be_compressed(self):
+ """A short session must not flash a compaction bar."""
+ prior = [
+ ChatMessage(role="user", content="hi", sequence=0),
+ ChatMessage(role="assistant", content="hello", sequence=1),
+ ]
+ session = self._session_with_history(prior)
+ result_msg = self._make_result_message()
+
+ timeline = await self._run(
+ session,
+ [
+ (
+ f"{_SVC}._restore_cli_session_for_turn",
+ dict(
+ new_callable=AsyncMock,
+ return_value=self._no_resume_restore(prior),
+ ),
+ ),
+ ],
+ lambda *a, **k: self._make_client_mock(result_message=result_msg),
+ )
+
+ opened, _closed = self._compaction_marks(timeline)
+ assert opened == []
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 8f73c461e3..8b80d1b565 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -58,6 +58,11 @@ from backend.integrations.codex.transport import PooledCodexRuntimeLease
from backend.integrations.credential_lease import CredentialLease
from backend.util.exceptions import NotFoundError
from backend.util.feature_flag import Flag, is_feature_enabled
+from backend.util.prompt import (
+ DEFAULT_COMPRESSION_RESERVE,
+ estimate_token_count,
+ get_compression_target,
+)
from backend.util.settings import Settings
from ..config import ChatConfig, CopilotLLMModel, CopilotMode
@@ -116,6 +121,7 @@ from ..rate_limit import (
)
from ..response_model import (
StreamBaseResponse,
+ StreamCompactionProgress,
StreamError,
StreamFinish,
StreamFinishStep,
@@ -171,7 +177,13 @@ from ..transcript import (
validate_transcript,
)
from ..transcript_builder import TranscriptBuilder, TranscriptSnapshot
-from .compaction import CompactionTracker, filter_compaction_messages
+from .compaction import (
+ CompactionStats,
+ CompactionTracker,
+ filter_compaction_messages,
+ sdk_compaction_stats,
+ transcript_stats,
+)
from .env import build_sdk_env # noqa: F401 — re-export for backward compat
from .openrouter_cost import record_turn_cost_from_openrouter
from .response_adapter import SDKResponseAdapter
@@ -297,6 +309,54 @@ class _SDKLoopState:
stream_error_code: str | None = None
+async def _open_sdk_compaction_row(
+ ctx: "_StreamContext", state: "_RetryState"
+) -> list[StreamBaseResponse]:
+ """Open the row for a CLI-side compaction the PreCompact hook announced.
+
+ Sized off our own mirror of the CLI context: the transcript builder
+ holds the entries the CLI is about to condense, so its token and turn
+ counts are the ``tokensBefore`` that paces the bar and the
+ ``messagesBefore`` the settled row reports. Measured off the loop — a
+ full context is hundreds of thousands of characters.
+ """
+ if not ctx.compaction.has_pending_start:
+ return []
+ stats = await asyncio.to_thread(
+ transcript_stats,
+ state.transcript_builder.entries_as_dicts(),
+ model=_compression_model(),
+ )
+ return ctx.compaction.emit_start_if_ready(stats)
+
+
+async def _measure_sdk_compaction(
+ ctx: "_StreamContext", state: "_RetryState"
+) -> tuple[bool, list[dict] | None, CompactionStats | None]:
+ """Read what the CLI kept after compacting and size the row's payoff.
+
+ Runs before the row closes so the settled output carries the numbers.
+ Returns ``(measured, compacted, stats)``: ``measured`` is False when no
+ cycle was pending, and the compacted entries are handed back so the
+ caller can sync the transcript builder without a second read.
+ """
+ # Let a PreCompact hook that raced this message land before we look —
+ # ``emit_end_if_ready`` yields for the same reason.
+ await asyncio.sleep(0)
+ path = ctx.compaction.pending_transcript_path
+ if path is None:
+ return False, None, None
+ compacted = await asyncio.to_thread(read_compacted_entries, path)
+ stats = await asyncio.to_thread(
+ sdk_compaction_stats,
+ state.transcript_builder.entries_as_dicts(),
+ compacted,
+ model=_compression_model(),
+ start=ctx.compaction.start_stats,
+ )
+ return True, compacted, stats
+
+
async def _consume_sdk_until_done(
client: ClaudeSDKClient,
ctx: "_StreamContext",
@@ -313,11 +373,11 @@ async def _consume_sdk_until_done(
fires a synthetic re-prompt and invokes this again for the second
pass — bounded to one re-prompt per turn.
"""
- async for sdk_msg in _iter_sdk_messages(client):
+ async for sdk_msg in _iter_sdk_messages(client, wake=ctx.compaction.hook_fired):
# Heartbeat sentinel — refresh lock and keep SSE alive
if sdk_msg is None:
await ctx.lock.refresh()
- for ev in ctx.compaction.emit_start_if_ready():
+ for ev in await _open_sdk_compaction_row(ctx, state):
yield ev
yield StreamHeartbeat()
@@ -607,7 +667,8 @@ async def _consume_sdk_until_done(
# Emit compaction end if SDK finished compacting.
# Sync TranscriptBuilder with the CLI's active context.
- compact_result = await ctx.compaction.emit_end_if_ready(ctx.session)
+ measured, compacted, end_stats = await _measure_sdk_compaction(ctx, state)
+ compact_result = await ctx.compaction.emit_end_if_ready(ctx.session, end_stats)
if compact_result.events:
# Compaction events end with StreamFinishStep, which maps to
# Vercel AI SDK's "finish-step" — that clears activeTextParts.
@@ -627,10 +688,13 @@ async def _consume_sdk_until_done(
yield ev
entries_replaced = False
if compact_result.just_ended:
- compacted = await asyncio.to_thread(
- read_compacted_entries,
- compact_result.transcript_path,
- )
+ if not measured and compact_result.transcript_path:
+ # The hook landed in the one yield between the measurement
+ # and the close: the row went out without numbers, but the
+ # builder must still mirror what the CLI kept.
+ compacted = await asyncio.to_thread(
+ read_compacted_entries, compact_result.transcript_path
+ )
if compacted is not None:
state.transcript_builder.replace_entries(
compacted, log_prefix=ctx.log_prefix
@@ -1074,6 +1138,9 @@ _EPHEMERAL_EVENT_TYPES = (
StreamToolInputStart,
StreamToolInputAvailable,
StreamToolOutputAvailable,
+ # The compaction row's live progress phases ride along with those UI
+ # events on the SDK-internal path, inside the attempt loop.
+ StreamCompactionProgress,
# Transient StreamError and StreamStatus are ephemeral notifications,
# not content. Counting them would prevent the backoff retry from
# firing because _next_transient_backoff() returns None when
@@ -1219,7 +1286,7 @@ class _RetryState:
options: ClaudeAgentOptions
query_message: str
- was_compacted: bool
+ compaction_stats: "CompactionStats | None"
use_resume: bool
resume_file: str | None
transcript_msg_count: int
@@ -1895,11 +1962,17 @@ async def _safe_close_sdk_client(
async def _iter_sdk_messages(
client: ClaudeSDKClient,
+ wake: asyncio.Event | None = None,
) -> AsyncGenerator[Any, None]:
"""Yield SDK messages with heartbeat-based timeouts.
Uses an explicit async iterator with non-cancelling heartbeats.
+ ``wake`` cuts a heartbeat short: when it is set, the generator yields
+ the heartbeat sentinel at once and clears the event instead of waiting
+ out ``_HEARTBEAT_INTERVAL``. The PreCompact hook sets it so the
+ compaction row opens before the CLI's next message can close it.
+
CRITICAL: we must NOT cancel `__anext__()` mid-flight — doing so
(via `asyncio.timeout` or `wait_for`) corrupts the SDK's internal
anyio memory stream, causing `StopAsyncIteration` on the next call
@@ -1915,6 +1988,7 @@ async def _iter_sdk_messages(
"""
msg_iter = client.receive_response().__aiter__()
pending_task: asyncio.Task[Any] | None = None
+ wake_task: asyncio.Task[Any] | None = None
async def _next_msg() -> Any:
"""Await the next SDK message, wrapped for use with `asyncio.Task`."""
@@ -1924,25 +1998,44 @@ async def _iter_sdk_messages(
while True:
if pending_task is None:
pending_task = asyncio.create_task(_next_msg())
+ waiters: set[asyncio.Task[Any]] = {pending_task}
+ if wake is not None:
+ if wake_task is None:
+ wake_task = asyncio.create_task(wake.wait())
+ waiters.add(wake_task)
+
+ done, _ = await asyncio.wait(
+ waiters,
+ timeout=_HEARTBEAT_INTERVAL,
+ return_when=asyncio.FIRST_COMPLETED,
+ )
- done, _ = await asyncio.wait({pending_task}, timeout=_HEARTBEAT_INTERVAL)
+ if wake is not None and wake_task in done:
+ # Woken: hand the sentinel over BEFORE any message that
+ # landed in the same tick, or the row the hook announced
+ # would be closed by the very message meant to follow it.
+ wake_task = None
+ wake.clear()
+ yield None
+ continue
- if not done:
- yield None # heartbeat sentinel
+ if pending_task not in done:
+ yield None # heartbeat sentinel: the interval elapsed
continue
- pending_task = None
+ msg_task, pending_task = pending_task, None
try:
- yield done.pop().result()
+ yield msg_task.result()
except StopAsyncIteration:
return
finally:
- if pending_task is not None and not pending_task.done():
- pending_task.cancel()
- try:
- await pending_task
- except (asyncio.CancelledError, StopAsyncIteration):
- pass
+ for task in (pending_task, wake_task):
+ if task is not None and not task.done():
+ task.cancel()
+ try:
+ await task
+ except (asyncio.CancelledError, StopAsyncIteration):
+ pass
def _normalize_model_name(raw_model: str) -> str:
@@ -2601,10 +2694,250 @@ def _format_sdk_content_blocks(blocks: list) -> list[dict[str, Any]]:
return result
+def _compression_model() -> str:
+ """The model whose context window governs pre-query compression.
+
+ ``_compress_messages`` hands this to ``_run_compression``, and the
+ pre-query predictor sizes its estimate against the same name. Both read
+ it here rather than naming a model each: the runtime model (Codex 400K,
+ Opus 120K) and the compressor's model have different windows, so any
+ call site that picks its own opens compaction rows for work the
+ compressor won't do — or does the work with no row open at all.
+ """
+ return config.thinking_standard_model
+
+
+def _to_compress_dict(msg: ChatMessage) -> dict[str, Any]:
+ """One ``ChatMessage`` in the dict shape the compressor reads.
+
+ Shared by ``_compress_messages`` (what actually gets compressed) and
+ ``_will_compact`` (what gets estimated). ``_msg_tokens`` counts the
+ serialized ``function.arguments`` of every tool call, and copilot
+ assistant rows carry whole graph JSON in ``create_agent``/``edit_agent``
+ payloads — an estimate built from ``role``/``content`` alone undercounts
+ the tool-heaviest sessions by the widest margin.
+ """
+ payload: dict[str, Any] = {"role": msg.role}
+ if msg.content:
+ payload["content"] = msg.content
+ if msg.tool_calls:
+ payload["tool_calls"] = msg.tool_calls
+ if msg.tool_call_id:
+ payload["tool_call_id"] = msg.tool_call_id
+ return payload
+
+
+# Characters per token, measured with ``o200k_base`` over the payload shapes
+# this history actually carries: base64-ish blobs 1.5, CJK 1.8, numeric IDs
+# 2.6, graph JSON 2.7, source code 4.6, English prose 4.9-6.5.
+# ``estimate_token_count`` then scales raw counts by up to 1.5x for the
+# Claude-5 tokenizer generation, which drags the dense end down to ~1.0
+# characters per *estimated* token.
+#
+# The two bounds bracket the estimate rather than replacing it: at or below
+# ``limit * _MIN_CHARS_PER_TOKEN`` no history can reach the threshold, above
+# ``limit * _MAX_CHARS_PER_TOKEN`` none can miss it, so both ends are decided
+# by arithmetic alone. Only the band between them pays for a tokenizer pass,
+# and that band is bounded by construction — at the production limit (~138K
+# tokens) the widest input tiktoken can see is ~830K characters, ~80ms. The
+# megabyte histories that made tokenizing expensive in the first place are
+# exactly the ones the upper cut answers for free.
+_MIN_CHARS_PER_TOKEN = 1.0
+_MAX_CHARS_PER_TOKEN = 6.0
+
+# Pacing hint only, never the decision: when the bounds settle the question
+# without tokenizing there is no estimate to report, and ``summarizing`` with
+# no ``tokens_before`` animates a 500K compaction exactly like a 20K one. The
+# midpoint of the measured range is wrong by ~2x either way — good enough to
+# pick a progress curve, which is all the client does with it.
+_NOMINAL_CHARS_PER_TOKEN = 3.0
+
+
+@dataclass(frozen=True)
+class _CompactionForecast:
+ """Whether the upcoming turn compresses, and how big the input is.
+
+ ``tokens_before`` is the size the prediction was made from; it rides
+ the ``summarizing`` phase so the client can pace its progress curve.
+ ``None`` when no size was established at all.
+ """
+
+ expected: bool
+ tokens_before: int | None = None
+
+
+_NO_COMPACTION = _CompactionForecast(expected=False)
+
+
+def _payload_chars(rows: list[ChatMessage]) -> int:
+ """Characters the estimator would tokenize for *rows*.
+
+ Content plus tool-call names and serialized arguments — the fields
+ ``_to_compress_dict`` hands the estimator. The per-message wrapper
+ tokens ``_msg_tokens`` adds are left out; they are absorbed by the
+ width of the bounds above.
+ """
+ total = 0
+ for msg in rows:
+ total += len(msg.content or "")
+ for call in msg.tool_calls or []:
+ function = call.get("function", {})
+ total += len(str(function.get("name") or ""))
+ total += len(str(function.get("arguments") or ""))
+ return total
+
+
+def _will_compact(messages: list[ChatMessage], model: str) -> _CompactionForecast:
+ """Cheap pre-check: would ``_compress_messages`` do real work?
+
+ Mirrors ``compress_context``'s early-return condition (``prompt.py``:
+ ``original_count + reserve <= target_tokens``) so the UI can open a
+ compaction row *before* the expensive LLM summarization starts instead of
+ announcing it afterwards.
+
+ A character count decides the clear-cut cases outright; only histories
+ that land in the ambiguous band near the threshold are tokenized, and
+ that band is bounded (see ``_MIN_CHARS_PER_TOKEN``), so the tokenizer
+ can never run on the huge histories that would make it expensive.
+
+ Being wrong is survivable in both directions — a false negative falls
+ back to a self-contained row, a false positive is retired by
+ ``abort_pre_query`` — but a false positive flashes a bar on screen, so
+ the tokenizer is spent exactly where the bounds cannot separate the two.
+ Any failure means no prediction: this is cosmetic, and a tiktoken
+ download failure or an unknown model must not take the stream with it.
+ """
+ rows = [m for m in filter_compaction_messages(messages) if m.role != "reasoning"]
+ if len(rows) < 2:
+ return _NO_COMPACTION
+ chars = _payload_chars(rows)
+ try:
+ # ``estimated > limit`` is ``estimated + reserve > target``, the
+ # negation of the compressor's early return.
+ limit = get_compression_target(model) - DEFAULT_COMPRESSION_RESERVE
+ if chars <= limit * _MIN_CHARS_PER_TOKEN:
+ return _NO_COMPACTION
+ if chars > limit * _MAX_CHARS_PER_TOKEN:
+ return _CompactionForecast(
+ expected=True,
+ tokens_before=int(chars / _NOMINAL_CHARS_PER_TOKEN),
+ )
+ estimated = estimate_token_count(
+ [_to_compress_dict(m) for m in rows], model=model
+ )
+ except Exception:
+ logger.warning(
+ "[SDK] Compaction pre-check failed on %d rows (%d chars) —"
+ " skipping the prediction (the row still opens after the fact)",
+ len(rows),
+ chars,
+ exc_info=True,
+ )
+ return _NO_COMPACTION
+ return _CompactionForecast(expected=estimated > limit, tokens_before=estimated)
+
+
+def _expect_pre_query_compaction(
+ messages: list[ChatMessage],
+ model: str,
+ *,
+ use_resume: bool,
+ transcript_msg_count: int,
+ session_msg_ceiling: int,
+ prior_messages: "list[ChatMessage] | None" = None,
+ target_tokens: int | None = None,
+) -> _CompactionForecast:
+ """Predict whether ``_build_query_message`` will compress on this turn.
+
+ Mirrors ``_build_query_message``'s branch selection so the compaction
+ row only opens when the branch actually taken feeds ``_compress_messages``
+ a slice that would compress. On the steady-state resume path the
+ transcript covers the full history and nothing is compressed — running
+ ``_will_compact`` over the whole session there returns a false positive
+ on EVERY turn once cumulative history exceeds the compression target,
+ opening a row that is immediately aborted.
+
+ ``prior_messages`` must be the same value handed to
+ ``_build_query_message``: on the no-resume path it is the compacted
+ transcript + hole + gap, which is normally far smaller than the raw
+ session and is non-``None`` on essentially every turn with a baseline
+ transcript. Predicting over the full session there is the mirror-image
+ error — the branch compresses a slice this function never looked at.
+
+ ``target_tokens`` must be the same value handed to
+ ``_build_query_message`` — the no-resume branch there abandons history
+ injection entirely once the budget falls to ``_BARE_MESSAGE_TOKEN_FLOOR``,
+ and a mirror that skips that guard predicts compression for a branch that
+ returns the bare message.
+
+ False negatives are safe: ``emit_pre_query_end`` emits a self-contained
+ row when compression happens without a prediction. That covers slices
+ this estimator cannot see, like cap-engaged hole rows fetched from the DB.
+ """
+ prior = [
+ m for m in messages[: max(0, session_msg_ceiling - 1)] if m.role != "reasoning"
+ ]
+ if not use_resume:
+ if session_msg_ceiling <= 1:
+ return _NO_COMPACTION
+ if target_tokens is not None and target_tokens <= _BARE_MESSAGE_TOKEN_FLOOR:
+ return _NO_COMPACTION
+ source = prior_messages if prior_messages is not None else prior
+ return _will_compact(source, model)
+ if transcript_msg_count <= 0:
+ # ``use_resume`` with no covered rows compresses nothing.
+ return _NO_COMPACTION
+ cap_engaged = transcript_msg_count >= len(prior) or (
+ bool(prior) and prior[0].sequence is not None and prior[0].sequence > 0
+ )
+ if cap_engaged and prior and prior[0].sequence is not None:
+ window_gap = [
+ m
+ for m in prior
+ if m.sequence is not None and m.sequence >= transcript_msg_count
+ ]
+ return _will_compact(window_gap, model)
+ if transcript_msg_count < session_msg_ceiling - 1:
+ if transcript_msg_count > len(prior):
+ return _NO_COMPACTION
+ if prior[transcript_msg_count - 1].role != "assistant":
+ # Misaligned watermark — _build_query_message skips the gap.
+ return _NO_COMPACTION
+ return _will_compact(prior[transcript_msg_count:], model)
+ # Scenario A: --resume covers the full context; nothing is compressed.
+ return _NO_COMPACTION
+
+
+def _retry_reduced_context(
+ *,
+ reduced: ReducedContext,
+ compaction_stats: "CompactionStats | None",
+) -> bool:
+ """Did a context-reduction retry actually summarize anything?
+
+ The retry opens its compaction row optimistically, before
+ ``_reduce_context`` runs, so the close is a claim about work that may
+ not have happened. The row says the conversation was condensed, and
+ only two things earn that: the transcript came back summarized
+ (``transcript_lost`` false — the drop branch is the only other exit),
+ or ``_build_query_message`` compressed DB history, which surfaces as
+ ``compaction_stats``.
+
+ Dropping the transcript is deliberately NOT enough. It reduces
+ context, but by discarding history rather than summarizing it, and
+ ``compact_transcript`` failing lands there just as surely as a second
+ retry does — so a guess made before ``_reduce_context`` ran would
+ persist a durable "Condensed" row for a conversation that was
+ truncated. The non-retry path already refuses to call a drop a
+ summarize; this keeps the retry path honest about the same thing.
+ """
+ return not reduced.transcript_lost or compaction_stats is not None
+
+
async def _compress_messages(
messages: list[ChatMessage],
target_tokens: int | None = None,
-) -> tuple[list[ChatMessage], bool]:
+) -> tuple[list[ChatMessage], bool, "CompactionStats | None"]:
"""Compress a list of messages if they exceed the token threshold.
Delegates to `_run_compression` (`transcript.py`) which centralizes
@@ -2631,24 +2964,15 @@ async def _compress_messages(
]
if len(messages) < 2:
- return messages, False
+ return messages, False, None
# Convert ChatMessages to dicts for compress_context
- messages_dict = []
- for msg in messages:
- msg_dict: dict[str, Any] = {"role": msg.role}
- if msg.content:
- msg_dict["content"] = msg.content
- if msg.tool_calls:
- msg_dict["tool_calls"] = msg.tool_calls
- if msg.tool_call_id:
- msg_dict["tool_call_id"] = msg.tool_call_id
- messages_dict.append(msg_dict)
+ messages_dict = [_to_compress_dict(msg) for msg in messages]
try:
result = await _run_compression(
messages_dict,
- config.thinking_standard_model,
+ _compression_model(),
"[SDK]",
target_tokens=target_tokens,
)
@@ -2664,11 +2988,16 @@ async def _compress_messages(
# 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"
+ "[SDK] _compress_messages failed — dropping %d messages to bare"
" message to guarantee retry progress: %s",
+ len(messages),
exc,
)
- return [], True
+ # A drop is not a summarize: the row must not claim "condensed" and
+ # must not count as a completed compaction. It still has to close
+ # honestly — the user just lost their history, and the settled row
+ # is where they learn it — so the stats say only what happened.
+ return [], True, CompactionStats(dropped=True, messages_before=len(messages))
if result.was_compacted:
logger.info(
@@ -2679,7 +3008,7 @@ async def _compress_messages(
result.messages_dropped,
)
# Convert compressed dicts back to ChatMessages
- return [
+ compacted = [
ChatMessage(
role=m["role"],
content=m.get("content"),
@@ -2687,9 +3016,16 @@ async def _compress_messages(
tool_call_id=m.get("tool_call_id"),
)
for m in result.messages
- ], True
+ ]
+ stats = CompactionStats(
+ tokens_before=result.original_token_count,
+ tokens_after=result.token_count,
+ messages_before=len(messages),
+ messages_after=len(compacted),
+ )
+ return compacted, True, stats
- return messages, False
+ return messages, False, None
def _session_messages_to_transcript(messages: list[ChatMessage]) -> str:
@@ -2802,7 +3138,8 @@ async def _build_query_message(
session_msg_ceiling: int | None = None,
target_tokens: int | None = None,
prior_messages: "list[ChatMessage] | None" = None,
-) -> tuple[str, bool]:
+ expect_compaction: bool = False,
+) -> tuple[str, "CompactionStats | None"]:
"""Build the query message with appropriate context.
When ``use_resume=True``, the CLI has the full session via ``--resume``;
@@ -2823,9 +3160,12 @@ async def _build_query_message(
messages so that mid-turn drains do not skew the gap calculation
and cause pending messages to be duplicated in both the gap context
and ``current_message``.
+ expect_compaction: What the caller's pre-check predicted. Logged only,
+ beside the branch actually taken, so a row that opens without
+ compression (or compression with no row) is one grep away.
Returns:
- Tuple of (query_message, was_compacted).
+ Tuple of (query_message, compaction_stats or None).
"""
msg_count = len(session.messages)
# Use the ceiling if supplied (prevents pending-message duplication when
@@ -2849,14 +3189,21 @@ async def _build_query_message(
# mid-turn user rows from the next LLM query.
prior = [m for m in prior if m.role != "reasoning"]
+ # Every branch below makes at most one ``_compress_messages`` call and then
+ # returns, so this is written exactly once per invocation.
+ compaction_stats: CompactionStats | None = None
+
logger.info(
"[SDK] [%s] Context path: use_resume=%s, transcript_msg_count=%d,"
- " db_msg_count=%d, target_tokens=%s",
+ " db_msg_count=%d, target_tokens=%s, expect_compaction=%s,"
+ " prior_messages=%s",
session_id[:8],
use_resume,
transcript_msg_count,
msg_count,
target_tokens,
+ expect_compaction,
+ len(prior_messages) if prior_messages is not None else None,
)
if use_resume and transcript_msg_count > 0:
@@ -2905,7 +3252,10 @@ async def _build_query_message(
e,
)
gap = hole + window_gap
- compressed, was_compressed = await _compress_messages(gap, target_tokens)
+ compressed, _was_compressed, stats = await _compress_messages(
+ gap, target_tokens
+ )
+ compaction_stats = stats
gap_context = _format_conversation_context(compressed)
if gap_context:
logger.info(
@@ -2915,12 +3265,12 @@ async def _build_query_message(
session_id[:8],
transcript_msg_count,
len(gap),
- was_compressed,
+ stats is not None,
len(gap_context),
)
return (
f"{gap_context}\n\nNow, the user says:\n{current_message}",
- was_compressed,
+ compaction_stats,
)
logger.warning(
"[SDK] [%s] Cap-engaged + empty sequence-based gap: window may"
@@ -2929,7 +3279,7 @@ async def _build_query_message(
transcript_msg_count,
msg_count,
)
- return current_message, False
+ return current_message, compaction_stats
if transcript_msg_count < effective_count - 1:
# Sanity-check the watermark: the last covered position should be
# an assistant turn. A user-role message here means the count is
@@ -2947,9 +3297,12 @@ async def _build_query_message(
transcript_msg_count,
msg_count,
)
- return current_message, False
+ return current_message, compaction_stats
gap = prior[transcript_msg_count:]
- compressed, was_compressed = await _compress_messages(gap, target_tokens)
+ compressed, _was_compressed, stats = await _compress_messages(
+ gap, target_tokens
+ )
+ compaction_stats = stats
gap_context = _format_conversation_context(compressed)
if gap_context:
logger.info(
@@ -2958,12 +3311,12 @@ async def _build_query_message(
transcript_msg_count,
msg_count,
len(gap),
- was_compressed,
+ stats is not None,
len(gap_context),
)
return (
f"{gap_context}\n\nNow, the user says:\n{current_message}",
- was_compressed,
+ compaction_stats,
)
logger.warning(
"[SDK] [%s] Transcript stale: gap produced empty context"
@@ -2979,7 +3332,7 @@ async def _build_query_message(
session_id[:8],
transcript_msg_count,
)
- return current_message, False
+ return current_message, compaction_stats
elif not use_resume and effective_count > 1:
# No --resume: the CLI starts a fresh session with no prior context.
@@ -3002,7 +3355,7 @@ async def _build_query_message(
_BARE_MESSAGE_TOKEN_FLOOR,
msg_count,
)
- return current_message, False
+ return current_message, compaction_stats
source = prior_messages if prior_messages is not None else prior
logger.warning(
@@ -3013,18 +3366,21 @@ async def _build_query_message(
"transcript+gap" if prior_messages is not None else "full-db",
target_tokens,
)
- compressed, was_compressed = await _compress_messages(source, target_tokens)
+ compressed, _was_compressed, stats = await _compress_messages(
+ source, target_tokens
+ )
+ compaction_stats = stats
history_context = _format_conversation_context(compressed)
if history_context:
logger.info(
"[SDK] [%s] Fallback context built: compressed=%s, context_bytes=%d",
session_id[:8],
- was_compressed,
+ stats is not None,
len(history_context),
)
return (
f"{history_context}\n\nNow, the user says:\n{current_message}",
- was_compressed,
+ compaction_stats,
)
logger.warning(
"[SDK] [%s] Fallback context empty after compression"
@@ -3033,7 +3389,7 @@ async def _build_query_message(
len(source),
)
- return current_message, False
+ return current_message, compaction_stats
# Claude API vision-supported image types.
@@ -3609,9 +3965,6 @@ async def _run_stream_attempt(
)
ctx.compaction.reset_for_query()
- if state.was_compacted:
- for ev in ctx.compaction.emit_pre_query(ctx.session):
- yield ev
# Narrate the silent gap between dispatching the query and the
# SDK's first real chunk — usually <1s but can stretch to several
@@ -3800,6 +4153,7 @@ async def _seed_transcript(
transcript_covers_prefix: bool,
transcript_msg_count: int,
log_prefix: str,
+ msg_ceiling: int,
) -> tuple[str, bool, int]:
"""Seed the transcript builder from compressed DB messages.
@@ -3810,14 +4164,21 @@ async def _seed_transcript(
on the next pod gets a usable compact base even for sessions that started
on old pods.
+ ``msg_ceiling`` is ``len(session.messages)`` as of *before* the compaction
+ row was persisted. Slicing off just the last entry is not enough: closing
+ a pre-query compaction row appends a synthetic assistant+tool pair, so the
+ naive ``[:-1]`` leaves the current user message inside the seed — and the
+ turn appends it again via ``append_user``, duplicating the live message in
+ the transcript and inflating the watermark by two.
+
Returns ``(transcript_content, transcript_covers_prefix, transcript_msg_count)``
updated values — unchanged if seeding is not possible.
"""
- if len(session.messages) <= 1:
+ if msg_ceiling <= 1:
return "", transcript_covers_prefix, transcript_msg_count
- _prior = session.messages[:-1]
- _comp, _ = await _compress_messages(_prior, _SEED_TARGET_TOKENS)
+ _prior = session.messages[: msg_ceiling - 1]
+ _comp, _, _ = await _compress_messages(_prior, _SEED_TARGET_TOKENS)
if not _comp:
return "", transcript_covers_prefix, transcript_msg_count
@@ -4221,6 +4582,11 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
# Defaults ensure the finally block can always reference these safely even when
# an early return (e.g. sdk_cwd error) skips their normal assignment below.
sdk_model: str | None = None
+ # ``compaction`` is constructed after several fallible setup steps below
+ # (sandbox/system-prompt/CLI-restore). The except branch needs to close
+ # a pre-query row on ANY failure in this function, including ones raised
+ # before that point, so it must tolerate ``compaction`` still being None.
+ compaction: CompactionTracker | None = None
codex_effort: "CodexReasoningEffort | None" = None
codex_gateway: CodexAnthropicGateway | None = None
deferred_codex_cleanup_error: BaseException | None = None
@@ -4800,7 +5166,19 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
request_arrival_at=request_arrival_at,
)
- query_message, was_compacted = await _build_query_message(
+ forecast = _expect_pre_query_compaction(
+ session.messages,
+ _compression_model(),
+ use_resume=use_resume,
+ transcript_msg_count=transcript_msg_count,
+ session_msg_ceiling=_pre_drain_msg_count,
+ prior_messages=restore_context_messages,
+ )
+ if forecast.expected:
+ for ev in compaction.emit_pre_query_start(forecast.tokens_before):
+ yield ev
+
+ query_message, compaction_stats = await _build_query_message(
current_message,
session,
use_resume,
@@ -4808,7 +5186,20 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
session_id,
session_msg_ceiling=_pre_drain_msg_count,
prior_messages=restore_context_messages,
+ expect_compaction=forecast.expected,
)
+
+ # Snapshot before the close persists its synthetic assistant+tool pair:
+ # ``_seed_transcript`` slices ``session.messages`` and would otherwise
+ # read the compaction row as history and the current user message as
+ # prior context, seeding a transcript that repeats the live turn.
+ pre_compaction_msg_count = len(session.messages)
+ if compaction_stats is not None:
+ for ev in compaction.emit_pre_query_end(session, compaction_stats):
+ yield ev
+ elif forecast.expected:
+ for ev in compaction.abort_pre_query():
+ yield ev
# If files are attached, prepare them: images become vision
# content blocks in the user message, other files go to sdk_cwd.
attachments = await _prepare_file_attachments(
@@ -4846,6 +5237,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
transcript_covers_prefix,
transcript_msg_count,
log_prefix,
+ pre_compaction_msg_count,
)
tried_compaction = False
@@ -4884,7 +5276,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
state = _RetryState(
options=sdk_options,
query_message=query_message,
- was_compacted=was_compacted,
+ compaction_stats=compaction_stats,
use_resume=use_resume,
resume_file=resume_file,
transcript_msg_count=transcript_msg_count,
@@ -4895,6 +5287,13 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
attempt = 0
_last_reset_attempt = -1
+ # Attempt 0 runs on the context built above; every later attempt
+ # reduces context exactly once. Transient retries and building-mode
+ # restarts `continue` back to the loop head without advancing
+ # `attempt`, and re-running the reduction there would summarize the
+ # history a second time and persist a second compaction row for the
+ # same logical compaction (mirrors the `_last_reset_attempt` guard).
+ _last_reduced_attempt = 0
while attempt < _MAX_STREAM_ATTEMPTS:
# Reset transient retry counter per context-level attempt so
# each attempt (original, compacted, no-transcript) gets the
@@ -4918,7 +5317,8 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
# Reset tool-level circuit breaker so failures from a previous
# (rolled-back) attempt don't carry over to the fresh attempt.
reset_tool_failure_counters()
- if attempt > 0:
+ if attempt != _last_reduced_attempt:
+ _last_reduced_attempt = attempt
logger.info(
"%s Retrying with reduced context (%d/%d)",
log_prefix,
@@ -4926,6 +5326,8 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
_MAX_STREAM_ATTEMPTS,
)
yield StreamStatus(message="Optimizing conversation context\u2026")
+ for ev in compaction.emit_pre_query_start():
+ yield ev
ctx = await _reduce_context(
transcript_content,
@@ -4977,15 +5379,29 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
# falls back to full session.messages[:-1] from DB — the authoritative
# source. transcript+gap is an optimisation for the first attempt only;
# on retry the extra overhead of full-DB context is acceptable.
- state.query_message, state.was_compacted = await _build_query_message(
- current_message,
- session,
- state.use_resume,
- state.transcript_msg_count,
- session_id,
- session_msg_ceiling=_pre_drain_msg_count,
- target_tokens=state.target_tokens,
+ state.query_message, state.compaction_stats = (
+ await _build_query_message(
+ current_message,
+ session,
+ state.use_resume,
+ state.transcript_msg_count,
+ session_id,
+ session_msg_ceiling=_pre_drain_msg_count,
+ target_tokens=state.target_tokens,
+ expect_compaction=True,
+ )
)
+ if _retry_reduced_context(
+ reduced=ctx,
+ compaction_stats=state.compaction_stats,
+ ):
+ for ev in compaction.emit_pre_query_end(
+ session, state.compaction_stats
+ ):
+ yield ev
+ else:
+ for ev in compaction.abort_pre_query():
+ yield ev
if attachments.hint:
state.query_message = f"{state.query_message}\n\n{attachments.hint}"
# warm_ctx is already baked into current_message via
@@ -5347,6 +5763,15 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
e, asyncio.CancelledError
) or _is_sdk_disconnect_error(e)
if not is_cancellation:
+ # An open pre-query row (emit_pre_query_start already yielded,
+ # but the matching emit_pre_query_end/abort_pre_query never ran
+ # because the error hit between the two) must be closed here —
+ # otherwise the client is left with a permanently-open
+ # compaction row. abort_pre_query is a no-op when nothing is
+ # open, so this is safe to call unconditionally.
+ if compaction is not None:
+ for ev in compaction.abort_pre_query():
+ yield ev
yield StreamError(errorText=display_msg, code=code)
raise
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py
index d87d32315c..96cf507f6b 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py
@@ -7,7 +7,9 @@ Covers ``_is_prompt_too_long``, ``_reduce_context``, ``_iter_sdk_messages``,
from __future__ import annotations
import asyncio
+import json
from collections.abc import AsyncGenerator
+from datetime import UTC, datetime
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -15,13 +17,27 @@ from claude_agent_sdk import AssistantMessage, TextBlock, ToolUseBlock
from backend.copilot import config as cfg_mod
from backend.copilot.config import ChatConfig
+from backend.util.prompt import (
+ DEFAULT_COMPRESSION_RESERVE,
+ compress_context,
+ estimate_token_count,
+ get_compression_target,
+)
+from ..model import ChatMessage, ChatSession
from ..model_router import ResolvedModel
+from ..transcript_builder import TranscriptBuilder
+from .compaction import CompactionStats
from .conftest import build_test_transcript as _build_transcript
from .service import (
+ _BARE_MESSAGE_TOKEN_FLOOR,
_RETRY_TARGET_TOKENS,
ReducedContext,
+ _build_query_message,
_compaction_target_tokens,
+ _compress_messages,
+ _compression_model,
+ _expect_pre_query_compaction,
_is_prompt_too_long,
_is_tool_only_message,
_iter_sdk_messages,
@@ -29,7 +45,9 @@ from .service import (
_reduce_context,
_resolve_sdk_model_for_request,
_restore_cli_session_for_turn,
+ _retry_reduced_context,
_TokenUsage,
+ _will_compact,
)
# ---------------------------------------------------------------------------
@@ -277,6 +295,49 @@ class TestIterSdkMessages:
assert all(m is None for m in received)
+ @pytest.mark.asyncio
+ async def test_wake_event_cuts_heartbeat_short(self) -> None:
+ """A set ``wake`` yields the sentinel without waiting out the interval."""
+ client = AsyncMock()
+ wake = asyncio.Event()
+
+ async def _slow_receive() -> AsyncGenerator[str]:
+ await asyncio.sleep(100) # never completes
+ yield "never" # pragma: no cover — unreachable, yield makes this an async generator
+
+ client.receive_response = _slow_receive
+
+ with patch("backend.copilot.sdk.service._HEARTBEAT_INTERVAL", 100):
+ gen = _iter_sdk_messages(client, wake=wake)
+ asyncio.get_running_loop().call_later(0.01, wake.set)
+ first = await asyncio.wait_for(gen.__anext__(), timeout=1)
+ await gen.aclose()
+
+ assert first is None
+ assert not wake.is_set()
+
+ @pytest.mark.asyncio
+ async def test_wake_sentinel_precedes_concurrent_message(self) -> None:
+ """A message landing with the wake is delivered after the sentinel."""
+ client = AsyncMock()
+ wake = asyncio.Event()
+
+ async def _receive() -> AsyncGenerator[str]:
+ await wake.wait()
+ yield "msg"
+
+ client.receive_response = _receive
+
+ with patch("backend.copilot.sdk.service._HEARTBEAT_INTERVAL", 100):
+ gen = _iter_sdk_messages(client, wake=wake)
+ asyncio.get_running_loop().call_later(0.01, wake.set)
+ received = [
+ await asyncio.wait_for(gen.__anext__(), timeout=1) for _ in range(2)
+ ]
+ await gen.aclose()
+
+ assert received == [None, "msg"]
+
@pytest.mark.asyncio
async def test_exception_propagates(self) -> None:
client = AsyncMock()
@@ -1682,3 +1743,758 @@ class TestSameModelCanonicalization:
routing_source="ld",
)
assert msgs[0].routing_source == "ld" # same model, not "fallback"
+
+
+def _msg(role: str, content: str) -> ChatMessage:
+ return ChatMessage(role=role, content=content)
+
+
+def _limit(model: str = "gpt-4o") -> int:
+ """The threshold ``_will_compact`` mirrors: target minus the reserve."""
+ return get_compression_target(model) - DEFAULT_COMPRESSION_RESERVE
+
+
+_FILLER = "the quick brown fox jumps over the lazy dog "
+
+
+def _band_history(
+ fraction: float, model: str = "gpt-4o", rows: int = 8
+) -> list[ChatMessage]:
+ """A history whose token estimate is *fraction* of the threshold.
+
+ Sized with the real tokenizer so the payload lands inside the ambiguous
+ band, which is where ``_will_compact`` actually tokenizes — and the only
+ region where its threshold can disagree with the compressor's. Keep
+ *fraction* within roughly 0.5-1.2: outside that the character prefilter
+ answers on its own and the tokenizer never runs.
+ """
+ probe = 100
+ unit = (
+ estimate_token_count(
+ [{"role": "user", "content": _FILLER * probe}], model=model
+ )
+ / probe
+ )
+ per_row = max(1, int(_limit(model) * fraction) // rows)
+ body = _FILLER * max(1, round(per_row / unit))
+ return [
+ ChatMessage(role="user" if i % 2 == 0 else "assistant", content=body)
+ for i in range(rows)
+ ]
+
+
+class TestWillCompact:
+ def test_false_for_short_history(self):
+ assert _will_compact([_msg("user", "hi")], "gpt-4o").expected is False
+
+ def test_false_for_empty_history(self):
+ assert _will_compact([], "gpt-4o").expected is False
+
+ def test_true_when_history_exceeds_the_compression_target(self):
+ # get_compression_target for gpt-4o is well under 1M tokens; a
+ # megabyte of prose comfortably clears it.
+ big = [_msg("user", "word " * 200_000), _msg("assistant", "ok")]
+ forecast = _will_compact(big, "gpt-4o")
+ assert forecast.expected is True
+ # The size rides along so the client can pace its progress curve
+ # against the real size of the work instead of a constant floor.
+ assert forecast.tokens_before is not None
+ assert forecast.tokens_before > 100_000
+
+ def test_ignores_reasoning_and_compaction_rows(self):
+ rows = [
+ _msg("reasoning", "word " * 200_000),
+ _msg("user", "hi"),
+ ]
+ assert _will_compact(rows, "gpt-4o").expected is False
+
+ def test_counts_tool_call_arguments(self):
+ """Tool-call payloads must be counted, not silently dropped.
+
+ Copilot assistant rows carry whole graph JSON in ``create_agent`` /
+ ``edit_agent`` arguments. An estimate built from role+content alone
+ reads these rows as nearly empty, so the sessions that need the
+ compaction row most are exactly the ones that never open one.
+ """
+ graph_json = json.dumps({"nodes": ["n" * 40] * 40_000})
+ rows = [
+ ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[
+ {
+ "id": "call-1",
+ "type": "function",
+ "function": {
+ "name": "create_agent",
+ "arguments": graph_json,
+ },
+ }
+ ],
+ ),
+ _msg("user", "ship it"),
+ ]
+ assert _will_compact(rows, "gpt-4o").expected is True
+
+ def test_tokenizer_failure_degrades_to_a_false_negative(self):
+ """The prediction is cosmetic; nothing it does may kill the stream.
+
+ tiktoken downloads its BPE table on first use and models come and go,
+ so this call can fail for reasons that have nothing to do with the
+ conversation. Giving up returns False — a false negative, which
+ ``emit_pre_query_end`` covers with a self-contained row.
+ """
+ over_threshold = _band_history(1.1)
+ assert _will_compact(over_threshold, "gpt-4o").expected is True
+ with patch(
+ "backend.copilot.sdk.service.estimate_token_count",
+ side_effect=RuntimeError("tiktoken download failed"),
+ ):
+ assert _will_compact(over_threshold, "gpt-4o").expected is False
+
+ def test_huge_history_skips_the_tokenizer_entirely(self):
+ """The expensive path must not run where it costs the most.
+
+ Above ``limit * _MAX_CHARS_PER_TOKEN`` no history can be under the
+ threshold whatever it contains, so the answer is arithmetic — which
+ is what keeps the tokenizer's input bounded.
+ """
+ huge = [_msg("user", "word " * 400_000), _msg("assistant", "ok")]
+ with patch(
+ "backend.copilot.sdk.service.estimate_token_count",
+ side_effect=AssertionError("tokenizer must not run on a huge history"),
+ ):
+ forecast = _will_compact(huge, "gpt-4o")
+ assert forecast.expected is True
+ # No estimate was taken, but the client still needs something to pace
+ # against, so the character count stands in for one.
+ assert forecast.tokens_before is not None
+
+ def test_tiny_history_skips_the_tokenizer_entirely(self):
+ small = [_msg("user", "hi there"), _msg("assistant", "hello")]
+ with patch(
+ "backend.copilot.sdk.service.estimate_token_count",
+ side_effect=AssertionError("tokenizer must not run on a tiny history"),
+ ):
+ assert _will_compact(small, "gpt-4o").expected is False
+
+
+def _seq_msg(role: str, content: str, sequence: int) -> ChatMessage:
+ return ChatMessage(role=role, content=content, sequence=sequence)
+
+
+def _big_history(n: int = 10) -> list[ChatMessage]:
+ """Alternating user/assistant rows whose total comfortably exceeds the
+ gpt-4o compression target (see TestWillCompact)."""
+ return [
+ _seq_msg("user" if i % 2 == 0 else "assistant", "word " * 30_000, i)
+ for i in range(n)
+ ]
+
+
+class TestExpectPreQueryCompaction:
+ def test_steady_state_resume_never_predicts(self):
+ # The transcript covers the full history — _build_query_message's
+ # Scenario A compresses nothing, so the pre-check must stay False
+ # even though the cumulative history exceeds the target.
+ messages = _big_history()
+ assert _will_compact(messages, "gpt-4o").expected is True
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=True,
+ transcript_msg_count=len(messages) - 1,
+ session_msg_ceiling=len(messages),
+ )
+ ).expected is False
+
+ def test_no_resume_predicts_over_full_history(self):
+ messages = _big_history()
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=False,
+ transcript_msg_count=0,
+ session_msg_ceiling=len(messages),
+ )
+ ).expected is True
+
+ def test_no_resume_predicts_over_prior_messages_when_given(self):
+ """The no-resume branch compresses ``prior_messages`` when it is set.
+
+ ``prior_messages`` is the compacted transcript + hole + gap — normally
+ far smaller than the raw session and non-None on essentially every
+ no-resume turn with a baseline transcript. Predicting over the full
+ session there opens a row for compression that never runs.
+ """
+ messages = _big_history()
+ small_prior = [_msg("user", "hi"), _msg("assistant", "hello")]
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=False,
+ transcript_msg_count=0,
+ session_msg_ceiling=len(messages),
+ prior_messages=small_prior,
+ )
+ ).expected is False
+
+ def test_no_resume_single_message_never_predicts(self):
+ messages = [_msg("user", "word " * 200_000)]
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=False,
+ transcript_msg_count=0,
+ session_msg_ceiling=1,
+ )
+ ).expected is False
+
+ def test_no_resume_below_the_bare_message_floor_never_predicts(self):
+ """``_build_query_message`` abandons history injection entirely once
+ the retry budget reaches ``_BARE_MESSAGE_TOKEN_FLOOR``, so the mirror
+ must too — otherwise the row opens for a branch that returns the bare
+ message and compresses nothing."""
+ messages = _big_history()
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=False,
+ transcript_msg_count=0,
+ session_msg_ceiling=len(messages),
+ target_tokens=_BARE_MESSAGE_TOKEN_FLOOR,
+ )
+ ).expected is False
+ # A budget above the floor still compresses, so the row still opens.
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=False,
+ transcript_msg_count=0,
+ session_msg_ceiling=len(messages),
+ target_tokens=_BARE_MESSAGE_TOKEN_FLOOR + 1,
+ )
+ ).expected is True
+
+ def test_resume_with_zero_watermark_never_predicts(self):
+ messages = _big_history()
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=True,
+ transcript_msg_count=0,
+ session_msg_ceiling=len(messages),
+ )
+ ).expected is False
+
+ def test_resume_with_a_big_stale_gap_predicts(self):
+ # Watermark covers rows 0-1 (last covered row is the assistant at
+ # index 1); the remaining gap is large enough to compress.
+ messages = _big_history()
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=True,
+ transcript_msg_count=2,
+ session_msg_ceiling=len(messages),
+ )
+ ).expected is True
+
+ def test_resume_with_a_small_gap_never_predicts(self):
+ messages = [
+ _seq_msg("user", "word " * 30_000, 0),
+ _seq_msg("assistant", "word " * 30_000, 1),
+ _seq_msg("user", "hi", 2),
+ _seq_msg("assistant", "ok", 3),
+ _seq_msg("user", "thanks", 4),
+ ]
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=True,
+ transcript_msg_count=2,
+ session_msg_ceiling=len(messages),
+ )
+ ).expected is False
+
+ def test_resume_misaligned_watermark_never_predicts(self):
+ # prior[transcript_msg_count - 1] is a user row — _build_query_message
+ # skips the gap entirely, so no compaction row should open.
+ messages = _big_history()
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=True,
+ transcript_msg_count=1,
+ session_msg_ceiling=len(messages),
+ )
+ ).expected is False
+
+ def test_cap_engaged_window_predicts_from_post_watermark_rows(self):
+ # The windowed view starts above absolute sequence 0, so the
+ # sequence-based gap path runs; rows at/after the watermark form
+ # the compressible slice.
+ messages = [
+ _seq_msg("user" if i % 2 == 0 else "assistant", "word " * 30_000, 100 + i)
+ for i in range(10)
+ ]
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=True,
+ transcript_msg_count=102,
+ session_msg_ceiling=len(messages),
+ )
+ ).expected is True
+
+ def test_cap_engaged_with_no_post_watermark_rows_never_predicts(self):
+ messages = [
+ _seq_msg("user" if i % 2 == 0 else "assistant", "word " * 30_000, 100 + i)
+ for i in range(10)
+ ]
+ assert (
+ _expect_pre_query_compaction(
+ messages,
+ "gpt-4o",
+ use_resume=True,
+ transcript_msg_count=200,
+ session_msg_ceiling=len(messages),
+ )
+ ).expected is False
+
+
+# ---------------------------------------------------------------------------
+# Predictor vs. compressor — the two must never disagree
+# ---------------------------------------------------------------------------
+
+
+def _make_session(messages: list[ChatMessage]) -> ChatSession:
+ now = datetime.now(UTC)
+ return ChatSession(
+ session_id="differential-session",
+ user_id="user-1",
+ messages=messages,
+ title="t",
+ usage=[],
+ started_at=now,
+ updated_at=now,
+ )
+
+
+_DIFFERENTIAL_CASES: list[dict] = [
+ # name, use_resume, transcript_msg_count, messages, prior_messages
+ {
+ "name": "no-resume, full db history",
+ "use_resume": False,
+ "transcript_msg_count": 0,
+ "messages": _big_history(),
+ "prior_messages": None,
+ },
+ {
+ "name": "no-resume, small transcript+gap prior",
+ "use_resume": False,
+ "transcript_msg_count": 4,
+ "messages": _big_history(),
+ "prior_messages": [_msg("user", "hi"), _msg("assistant", "hello")],
+ },
+ {
+ "name": "no-resume, large transcript+gap prior",
+ "use_resume": False,
+ "transcript_msg_count": 4,
+ "messages": _big_history(),
+ "prior_messages": _big_history(6),
+ },
+ {
+ "name": "no-resume, single message",
+ "use_resume": False,
+ "transcript_msg_count": 0,
+ "messages": [_msg("user", "word " * 200_000)],
+ "prior_messages": None,
+ },
+ {
+ "name": "resume, watermark aligned with a big gap",
+ "use_resume": True,
+ "transcript_msg_count": 2,
+ "messages": _big_history(),
+ "prior_messages": None,
+ },
+ {
+ "name": "resume, watermark misaligned on a user row",
+ "use_resume": True,
+ "transcript_msg_count": 1,
+ "messages": _big_history(),
+ "prior_messages": None,
+ },
+ {
+ "name": "resume, zero watermark",
+ "use_resume": True,
+ "transcript_msg_count": 0,
+ "messages": _big_history(),
+ "prior_messages": None,
+ },
+ {
+ "name": "resume, full coverage",
+ "use_resume": True,
+ "transcript_msg_count": 9,
+ "messages": _big_history(),
+ "prior_messages": None,
+ },
+ {
+ "name": "resume, cap engaged with post-watermark rows",
+ "use_resume": True,
+ "transcript_msg_count": 102,
+ "messages": [
+ _seq_msg("user" if i % 2 == 0 else "assistant", "word " * 30_000, 100 + i)
+ for i in range(10)
+ ],
+ "prior_messages": None,
+ },
+ {
+ "name": "resume, cap engaged with no post-watermark rows",
+ "use_resume": True,
+ "transcript_msg_count": 200,
+ "messages": [
+ _seq_msg("user" if i % 2 == 0 else "assistant", "word " * 30_000, 100 + i)
+ for i in range(10)
+ ],
+ "prior_messages": None,
+ },
+]
+
+
+class TestPredictorMatchesCompressor:
+ """``_expect_pre_query_compaction`` is a hand-written mirror of
+ ``_build_query_message``'s branch tree. Nothing but a differential test
+ keeps the two honest: every divergence so far has shipped as either a bar
+ that flashes and aborts, or expensive work with no row open at all.
+
+ This pins branch SELECTION — that the predictor and the compressor look at
+ the same slice. The threshold applied to that slice is pinned separately
+ by ``TestWillCompactThresholdMatchesCompressContext``, which drives the
+ real ``compress_context``; measuring it here would mean re-calling
+ ``_will_compact`` to grade ``_will_compact``.
+ """
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "case", _DIFFERENTIAL_CASES, ids=[c["name"] for c in _DIFFERENTIAL_CASES]
+ )
+ async def test_predictor_agrees_with_the_slice_that_gets_compressed(
+ self, case, monkeypatch
+ ):
+ messages = case["messages"]
+ session = _make_session(messages)
+ ceiling = len(messages)
+
+ forecast = _expect_pre_query_compaction(
+ messages,
+ _compression_model(),
+ use_resume=case["use_resume"],
+ transcript_msg_count=case["transcript_msg_count"],
+ session_msg_ceiling=ceiling,
+ prior_messages=case["prior_messages"],
+ )
+
+ # Spy on the compressor: run the real pre-check against the slice
+ # _build_query_message actually hands it, so the two see identical
+ # inputs by construction rather than by a hand-copied estimate.
+ compressed_slices: list[list[ChatMessage]] = []
+
+ async def _spy(msgs, target_tokens=None):
+ compressed_slices.append(list(msgs))
+ return msgs, False, None
+
+ monkeypatch.setattr("backend.copilot.sdk.service._compress_messages", _spy)
+ await _build_query_message(
+ "now what?",
+ session,
+ case["use_resume"],
+ case["transcript_msg_count"],
+ "differential-session",
+ session_msg_ceiling=ceiling,
+ prior_messages=case["prior_messages"],
+ )
+
+ would_compact = any(
+ _will_compact(slice_, _compression_model()).expected
+ for slice_ in compressed_slices
+ )
+
+ # Both directions are enforced. A false positive flashes a bar that
+ # aborts; a false negative is survivable (``emit_pre_query_end`` still
+ # emits a self-contained row) but leaves the user staring at a frozen
+ # screen for the whole summarization, which is the bug this PR exists
+ # to fix — so neither is allowed to creep back in through the branch
+ # tree. Cases whose slice the estimator genuinely cannot see (the
+ # cap-engaged hole rows fetched from the DB) are not in the matrix.
+ assert forecast.expected == would_compact, (
+ f"{case['name']}: predicted {forecast.expected} but "
+ f"_build_query_message compressed {len(compressed_slices)} slice(s) "
+ f"of which would_compact={would_compact}"
+ )
+
+
+class TestWillCompactThresholdMatchesCompressContext:
+ """``_will_compact``'s threshold is a copy of ``compress_context``'s early
+ return (``original_count + reserve <= target_tokens``). Grading it against
+ itself proves nothing, so these cases drive the real compressor with real
+ payloads and read ``was_compacted`` back.
+
+ ``client=None`` keeps the LLM out of it: the summarization step is skipped
+ and the remaining strategies still only run when the early return doesn't
+ fire, which is exactly the boundary under test.
+ """
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ ("fraction", "expected"),
+ [(0.5, False), (0.9, False), (1.1, True), (1.6, True)],
+ ids=["half", "just-under", "just-over", "well-over"],
+ )
+ async def test_prediction_matches_the_compressor(self, fraction, expected):
+ rows = _band_history(fraction)
+ payload = [{"role": m.role, "content": m.content} for m in rows]
+
+ result = await compress_context(payload, model="gpt-4o", client=None)
+
+ assert result.was_compacted is expected
+ assert _will_compact(rows, "gpt-4o").expected is result.was_compacted
+
+ @pytest.mark.asyncio
+ async def test_threshold_reads_the_compressors_own_constants(self):
+ """A payload sized off ``get_compression_target`` and
+ ``DEFAULT_COMPRESSION_RESERVE`` must land on the compressor's own
+ boundary — if either constant is read differently on the two sides,
+ one of these two cases flips.
+ """
+ under = [{"role": "user", "content": _FILLER}] * 2
+ assert (
+ await compress_context(under, model="gpt-4o", client=None)
+ ).was_compacted is False
+
+ at_limit = _band_history(1.0)
+ payload = [{"role": m.role, "content": m.content} for m in at_limit]
+ estimated = estimate_token_count(payload, model="gpt-4o")
+ result = await compress_context(payload, model="gpt-4o", client=None)
+ # ``compress_context`` sums per-message, the estimator scales the sum,
+ # so the two differ by at most a rounding step per message.
+ assert abs(result.original_token_count - estimated) <= len(payload)
+ assert result.was_compacted is (
+ result.original_token_count + DEFAULT_COMPRESSION_RESERVE
+ > get_compression_target("gpt-4o")
+ )
+
+
+# ---------------------------------------------------------------------------
+# _retry_reduced_context
+# ---------------------------------------------------------------------------
+
+
+def _summarized() -> ReducedContext:
+ """``_reduce_context``'s success exit: the transcript came back compacted."""
+ return ReducedContext(TranscriptBuilder(), False, None, False, True)
+
+
+def _dropped() -> ReducedContext:
+ """``_reduce_context``'s fallback exit: the transcript was discarded.
+
+ Reached both when ``compact_transcript`` fails on the first retry and on
+ every later retry.
+ """
+ return ReducedContext(TranscriptBuilder(), False, None, True, True, 50_000)
+
+
+class TestRetryReducedContext:
+ """The retry row must only persist when the conversation was summarized.
+
+ ``_reduce_context`` is called after the row is already open, so the
+ close is a claim about work that may not have happened.
+ """
+
+ def test_summarized_transcript_counts_even_without_compressed_history(self):
+ # First retry, compaction succeeded — a real summarization the row
+ # can honestly report.
+ assert (
+ _retry_reduced_context(reduced=_summarized(), compaction_stats=None) is True
+ )
+
+ def test_compressed_history_counts_without_a_transcript(self):
+ assert (
+ _retry_reduced_context(
+ reduced=_dropped(),
+ compaction_stats=CompactionStats(tokens_before=100, tokens_after=10),
+ )
+ is True
+ )
+
+ def test_nothing_reduced_retires_the_row(self):
+ # A later retry whose transcript was already dropped and whose
+ # history already fits reduces nothing.
+ assert (
+ _retry_reduced_context(reduced=_dropped(), compaction_stats=None) is False
+ )
+
+ @pytest.mark.asyncio
+ async def test_failed_compaction_is_not_a_summarization(self):
+ """A live transcript that FAILS to compact takes the drop branch.
+
+ The row is durable, so honouring it here tells the user — permanently,
+ on every refresh — that a conversation which was truncated had been
+ summarized. Driven through the real ``_reduce_context`` because the
+ distinction only exists in its return value: the inputs (a live
+ transcript, first retry) are identical to the success case.
+ """
+ transcript = _build_transcript([("user", "hi"), ("assistant", "hello")])
+
+ with patch(
+ "backend.copilot.sdk.service.compact_transcript",
+ new_callable=AsyncMock,
+ return_value=None,
+ ):
+ ctx = await _reduce_context(
+ transcript, False, "sess-123", "/tmp/cwd", "[test]"
+ )
+
+ assert ctx.transcript_lost is True
+ assert _retry_reduced_context(reduced=ctx, compaction_stats=None) is False
+
+ def test_both_signals_still_reports_a_reduction(self):
+ assert (
+ _retry_reduced_context(
+ reduced=_summarized(),
+ compaction_stats=CompactionStats(messages_before=9, messages_after=2),
+ )
+ is True
+ )
+
+
+# ---------------------------------------------------------------------------
+# The compressor's model is the predictor's model
+# ---------------------------------------------------------------------------
+
+
+class TestCompressionModelIsShared:
+ """The predictor sizes an estimate against a context window; the
+ compressor compresses against one. When those were two different models
+ — the runtime model (Codex, 400K) versus the compressor's default
+ (Sonnet, 200K) — every turn in the band between them either compacted
+ with no row open or opened a row that instantly aborted.
+ """
+
+ @pytest.mark.asyncio
+ async def test_compress_messages_uses_the_shared_model(self):
+ from backend.util.prompt import CompressResult
+
+ captured: dict[str, str] = {}
+
+ async def _fake_run(messages, model, log_prefix, target_tokens=None):
+ captured["model"] = model
+ return CompressResult(
+ messages=list(messages),
+ token_count=1,
+ was_compacted=False,
+ original_token_count=1,
+ )
+
+ with patch("backend.copilot.sdk.service._run_compression", new=_fake_run):
+ await _compress_messages([_msg("user", "q"), _msg("assistant", "a")])
+
+ assert captured["model"] == _compression_model()
+
+
+# ---------------------------------------------------------------------------
+# _seed_transcript
+# ---------------------------------------------------------------------------
+
+
+class TestSeedTranscript:
+ @pytest.mark.asyncio
+ async def test_seed_excludes_the_current_turn_and_the_compaction_row(
+ self, monkeypatch
+ ):
+ """The pre-query row is persisted BEFORE seeding runs.
+
+ Slicing off only the last entry then leaves the current user message
+ inside the seed; the turn appends it again via ``append_user``, so the
+ next turn's ``--resume`` context shows the live message twice and the
+ watermark is two rows ahead of reality.
+ """
+ from .compaction import _persist
+ from .service import _seed_transcript
+ from .transcript_builder import TranscriptBuilder
+
+ session = _make_session(
+ [
+ _msg("user", "q1"),
+ _msg("assistant", "a1"),
+ _msg("user", "current turn"),
+ ]
+ )
+ ceiling = len(session.messages)
+ _persist(session, "compaction-abc", '{"summary": "x"}')
+ assert len(session.messages) == ceiling + 2
+
+ seen: list[list[ChatMessage]] = []
+
+ async def _passthrough(msgs, target_tokens=None):
+ seen.append(list(msgs))
+ return list(msgs), False, None
+
+ monkeypatch.setattr(
+ "backend.copilot.sdk.service._compress_messages", _passthrough
+ )
+
+ content, covers, count = await _seed_transcript(
+ session,
+ TranscriptBuilder(),
+ False,
+ 0,
+ "[test]",
+ ceiling,
+ )
+
+ assert [(m.role, m.content) for m in seen[0]] == [
+ ("user", "q1"),
+ ("assistant", "a1"),
+ ]
+ assert "current turn" not in content
+ assert covers is True
+ assert count == 2
+
+
+# ---------------------------------------------------------------------------
+# A failed compression drops history — and says so
+# ---------------------------------------------------------------------------
+
+
+class TestCompressionFailureIsReportedAsADrop:
+ @pytest.mark.asyncio
+ async def test_failure_returns_dropped_stats(self):
+ """Both compression tiers failing drops the history to the bare
+ message. That is a real, user-visible loss, so the stats say
+ ``dropped`` — enough for the row to close honestly — while claiming
+ none of the counts a summarize would have produced."""
+ rows = [_msg("user", "q"), _msg("assistant", "a"), _msg("user", "q2")]
+ with patch(
+ "backend.copilot.sdk.service._run_compression",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("summarizer down"),
+ ):
+ compressed, reduced, stats = await _compress_messages(rows)
+ assert compressed == []
+ assert reduced is True
+ assert stats is not None
+ assert stats.dropped is True
+ assert stats.messages_before == 3
+ assert stats.tokens_after is None
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_test.py
index 665903b707..7fe0c75025 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service_test.py
@@ -1103,7 +1103,7 @@ class TestRetryStateObservedModel:
return _RetryState(
options=options,
query_message="",
- was_compacted=False,
+ compaction_stats=None,
use_resume=False,
resume_file=None,
transcript_msg_count=0,
@@ -1545,7 +1545,7 @@ class TestConsumeSdkUntilDone:
return _RetryState(
options=MagicMock(),
query_message="hello",
- was_compacted=False,
+ compaction_stats=None,
use_resume=False,
resume_file=None,
transcript_msg_count=0,
@@ -1585,7 +1585,7 @@ class TestConsumeSdkUntilDone:
acc = self._acc()
loop_state = self._loop_state()
- async def fake_iter(client):
+ async def fake_iter(client, wake=None):
yield AssistantMessage(content=[TextBlock(text="hi")], model="test")
yield ResultMessage(
subtype="success",
@@ -1630,7 +1630,7 @@ class TestConsumeSdkUntilDone:
acc = self._acc()
loop_state = self._loop_state()
- async def fake_iter(client):
+ async def fake_iter(client, wake=None):
yield None # heartbeat
yield ResultMessage(
subtype="success",
@@ -1679,7 +1679,7 @@ class TestConsumeSdkUntilDone:
acc = self._acc()
loop_state = self._loop_state()
- async def fake_iter(client):
+ async def fake_iter(client, wake=None):
yield AssistantMessage(
content=[
ToolUseBlock(id="t1", name=f"{MCP_TOOL_PREFIX}find_block", input={})
@@ -1748,7 +1748,7 @@ class TestConsumeSdkUntilDone:
acc = self._acc()
loop_state = self._loop_state()
- async def fake_iter(client):
+ async def fake_iter(client, wake=None):
yield SystemMessage(subtype="init", data={})
yield AssistantMessage(
content=[
@@ -1810,7 +1810,7 @@ class TestConsumeSdkUntilDone:
acc = self._acc()
loop_state = self._loop_state()
- async def fake_iter(client):
+ async def fake_iter(client, wake=None):
yield ResultMessage(
subtype="error",
duration_ms=1,
@@ -1849,7 +1849,7 @@ class TestConsumeSdkUntilDone:
acc = self._acc()
loop_state = self._loop_state()
- async def fake_iter(client):
+ async def fake_iter(client, wake=None):
yield SystemMessage(subtype="task_progress", data={"step": 1})
yield ResultMessage(
subtype="success",
@@ -1888,7 +1888,7 @@ class TestConsumeSdkUntilDone:
acc = self._acc()
loop_state = self._loop_state()
- async def fake_iter(client):
+ async def fake_iter(client, wake=None):
# Two consecutive AssistantMessages with empty tool args —
# the breaker counter should advance but not yet trip.
for i in range(2):
@@ -2099,7 +2099,7 @@ class TestStreamEndedWithoutResultMessage:
return _RetryState(
options=MagicMock(),
query_message="hello",
- was_compacted=False,
+ compaction_stats=None,
use_resume=False,
resume_file=None,
transcript_msg_count=0,
@@ -2129,7 +2129,7 @@ class TestStreamEndedWithoutResultMessage:
ctx = self._ctx()
state = self._state()
- async def empty_iter(_client):
+ async def empty_iter(_client, wake=None):
# Drain immediately — no ResultMessage ever arrives. Mirrors
# the CLI exiting on per-query ``max_budget_usd`` exhaustion
# mid-tool-call.
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry.py b/autogpt_platform/backend/backend/copilot/stream_registry.py
index 54e5a1e224..7874ca2dcc 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry.py
@@ -43,6 +43,7 @@ from .executor.utils import COPILOT_CONSUMER_TIMEOUT_SECONDS, get_session_lock_k
from .response_model import (
ResponseType,
StreamBaseResponse,
+ StreamCompactionProgress,
StreamError,
StreamFinish,
StreamFinishStep,
@@ -1171,6 +1172,7 @@ def _reconstruct_chunk(chunk_data: dict) -> StreamBaseResponse | None:
ResponseType.DREAM_OPERATIONS.value: StreamDreamOperations,
ResponseType.PENDING_DRAINED.value: StreamPendingDrained,
ResponseType.MODE_CHANGED.value: StreamModeChanged,
+ ResponseType.COMPACTION.value: StreamCompactionProgress,
}
chunk_type = chunk_data.get("type")
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry_test.py b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
index 7f19eae38a..236c60ae4f 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry_test.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
@@ -548,3 +548,63 @@ def test_reconstruct_mode_changed_chunk():
assert isinstance(chunk, StreamModeChanged)
assert chunk.mode == "extended_thinking"
assert '"data"' in chunk.to_sse()
+
+
+def test_reconstruct_chunk_round_trips_compaction_progress():
+ """Regression: ``data-compaction`` was added to the wire protocol without
+ being registered here, so every live compaction phase was dropped on the
+ executor → Redis → rest_server relay ("Unknown chunk type: data-compaction")
+ and the progress bar never moved in a running deployment.
+ """
+ import orjson
+
+ from backend.copilot.response_model import StreamCompactionProgress
+
+ stored = orjson.loads(
+ StreamCompactionProgress(
+ phase="rebuilding",
+ tokensBefore=128_000,
+ tokensAfter=31_000,
+ messagesBefore=412,
+ messagesAfter=38,
+ ).model_dump_json()
+ )
+
+ chunk = stream_registry._reconstruct_chunk(stored)
+
+ assert isinstance(chunk, StreamCompactionProgress)
+ assert chunk.phase == "rebuilding"
+ assert chunk.tokensBefore == 128_000
+ assert chunk.tokensAfter == 31_000
+ assert chunk.messagesBefore == 412
+ assert chunk.messagesAfter == 38
+ assert '"data"' in chunk.to_sse()
+
+
+# ``data-cursor`` is deprecated and no longer emitted by new subscriptions
+# (see ``StreamCursor``), so it is the one type that may stay unmapped.
+_UNRELAYED_RESPONSE_TYPES = {"CURSOR"}
+
+
+def test_every_response_type_survives_the_relay():
+ """Every event the backend can publish must be reconstructable.
+
+ An unmapped type is discarded with only a log warning, so the feature
+ that emits it silently does nothing in a deployment while every test
+ stays green — this has now happened twice (MODE_CHANGED, COMPACTION).
+ Adding a ``ResponseType`` should force a decision here.
+ """
+ import inspect
+ import re
+
+ from backend.copilot.response_model import ResponseType
+
+ source = inspect.getsource(stream_registry._reconstruct_chunk)
+ mapped = set(re.findall(r"ResponseType\.(\w+)\.value", source))
+ expected = {m.name for m in ResponseType} - _UNRELAYED_RESPONSE_TYPES
+
+ missing = sorted(expected - mapped)
+ assert not missing, (
+ f"ResponseType(s) {missing} are not registered in _reconstruct_chunk — "
+ "they would be dropped on the executor → Redis → rest_server relay"
+ )
diff --git a/autogpt_platform/backend/backend/copilot/transcript_builder.py b/autogpt_platform/backend/backend/copilot/transcript_builder.py
index ab25a92aeb..6a7dfac293 100644
--- a/autogpt_platform/backend/backend/copilot/transcript_builder.py
+++ b/autogpt_platform/backend/backend/copilot/transcript_builder.py
@@ -251,6 +251,16 @@ class TranscriptBuilder:
"""Total number of entries in the complete context."""
return len(self._entries)
+ def entries_as_dicts(self) -> list[dict[str, Any]]:
+ """The active context in the CLI's own entry shape (``type`` + ``message``).
+
+ Shallow: ``message`` is shared with the builder, so callers must
+ only read it. Lets the compaction sizing helpers measure our
+ mirror of the CLI context with the same code that measures the
+ CLI's session file.
+ """
+ return [{"type": e.type, "message": e.message} for e in self._entries]
+
@property
def is_empty(self) -> bool:
"""Whether this builder has any entries."""
diff --git a/autogpt_platform/backend/backend/util/prompt.py b/autogpt_platform/backend/backend/util/prompt.py
index 95c0a07860..46ad5f2f0d 100644
--- a/autogpt_platform/backend/backend/util/prompt.py
+++ b/autogpt_platform/backend/backend/util/prompt.py
@@ -256,6 +256,12 @@ def estimate_token_count_str(
DEFAULT_TOKEN_THRESHOLD = 120_000
DEFAULT_KEEP_RECENT = 15
+# Response headroom subtracted from the compression target. Named (rather than
+# inlined in ``compress_context``'s signature) because the copilot's pre-query
+# predictor mirrors the early-return condition below and must read the same
+# value — a silent desync there opens compaction rows for work that never runs.
+DEFAULT_COMPRESSION_RESERVE = 2_048
+
# Reserve tokens for system prompt, tool definitions, and per-turn overhead.
# The actual model context limit minus this reserve = compression target.
_CONTEXT_OVERHEAD_RESERVE = 60_000
@@ -740,7 +746,7 @@ async def compress_context(
model: str = "gpt-4o",
client: AsyncOpenAI | None = None,
keep_recent: int = DEFAULT_KEEP_RECENT,
- reserve: int = 2_048,
+ reserve: int = DEFAULT_COMPRESSION_RESERVE,
start_cap: int = 8_192,
floor_cap: int = 128,
) -> CompressResult:
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/compaction-progress.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/compaction-progress.test.tsx
new file mode 100644
index 0000000000..9ce22fe149
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/compaction-progress.test.tsx
@@ -0,0 +1,697 @@
+import type { SessionDetailResponseMessagesItem } from "@/app/api/__generated__/models/sessionDetailResponseMessagesItem";
+import { server } from "@/mocks/mock-server";
+import { copilotStreamHandler } from "@/tests/integrations/copilot-sse";
+import { screen, waitFor } from "@testing-library/react";
+import type { UIMessageChunk } from "ai";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { resetCopilotChatRegistry } from "../copilotChatRegistry";
+import {
+ renderHost,
+ sessionHandler,
+ TEST_BACKEND_BASE_URL,
+ TEST_SESSION_ID,
+ typeAndSend,
+} from "./sse-helpers";
+
+// Pin the backend host so the CoPilot transport's absolute URL is
+// deterministic — the transport bypasses the Next proxy on purpose
+// (Vercel function-timeout dodge), so MSW has to match an absolute URL.
+vi.mock("@/services/environment", async (importActual) => {
+ const actual = await importActual<typeof import("@/services/environment")>();
+ return {
+ ...actual,
+ environment: {
+ ...actual.environment,
+ getAGPTServerBaseUrl: () => TEST_BACKEND_BASE_URL,
+ },
+ };
+});
+
+// Replace the auth token fetch with a static header so we don't need
+// real auth in tests.
+vi.mock("../helpers", async (importActual) => {
+ const actual = await importActual<typeof import("../helpers")>();
+ return {
+ ...actual,
+ getCopilotAuthHeaders: async () => ({ "x-test-auth": "yes" }),
+ };
+});
+
+// useChatSession depends on useAuth via useCopilotPage's auth gate.
+vi.mock("@/lib/auth/hooks/useAuth", () => ({
+ useAuth: () => ({ isUserLoading: false, isLoggedIn: true }),
+}));
+
+// Keep mode/model toggles and artifacts off so the chat input renders a
+// single, predictable Submit button.
+vi.mock("@/services/feature-flags/use-get-flag", () => ({
+ Flag: {
+ ARTIFACTS: "ARTIFACTS",
+ CHAT_MODE_OPTION: "CHAT_MODE_OPTION",
+ ENABLE_PLATFORM_PAYMENT: "ENABLE_PLATFORM_PAYMENT",
+ },
+ useGetFlag: () => false,
+}));
+
+// Shared chunk builders — every scenario below is "open row → phase(s) →
+// close (or not) → whatever content follows", so only the tails differ.
+function openCompactionRow(id = "compaction-1"): UIMessageChunk[] {
+ return [
+ {
+ type: "tool-input-start",
+ toolCallId: id,
+ toolName: "context_compaction",
+ },
+ {
+ type: "tool-input-available",
+ toolCallId: id,
+ toolName: "context_compaction",
+ input: {},
+ },
+ ];
+}
+
+function closeCompactionRow(
+ stats: Record<string, number>,
+ id = "compaction-1",
+): UIMessageChunk {
+ return {
+ type: "tool-output-available",
+ toolCallId: id,
+ output: JSON.stringify({
+ summary: "Earlier messages were summarized to fit within context limits.",
+ ...stats,
+ }),
+ };
+}
+
+function summarizingPhase(tokensBefore = 128000): UIMessageChunk {
+ return {
+ type: "data-compaction",
+ data: { phase: "summarizing", tokensBefore },
+ };
+}
+
+const REBUILDING_PHASE: UIMessageChunk = {
+ type: "data-compaction",
+ data: { phase: "rebuilding" },
+};
+
+function textChunks(text: string, id = "t1"): UIMessageChunk[] {
+ return [
+ { type: "text-start", id },
+ { type: "text-delta", id, delta: text },
+ { type: "text-end", id },
+ ];
+}
+
+const FULL_STATS = {
+ tokensBefore: 128000,
+ tokensAfter: 31000,
+ messagesBefore: 412,
+ messagesAfter: 38,
+};
+
+// Mirrors the backend's emission order: the row is OPEN while the work
+// runs, then closes with JSON stats.
+const COMPACTION_TURN: UIMessageChunk[] = [
+ { type: "start" },
+ { type: "start-step" },
+ ...openCompactionRow(),
+ summarizingPhase(),
+ closeCompactionRow(FULL_STATS),
+ { type: "finish-step" },
+ REBUILDING_PHASE,
+ { type: "start-step" },
+ ...textChunks("All caught up."),
+ { type: "finish-step" },
+ { type: "finish" },
+];
+
+// A settled compaction row followed immediately by another completed
+// generic tool call, both ahead of the final text — reproduces the
+// collapsed-group bug where two adjacent "generic completed tool" parts
+// fold into a single `CollapsedToolGroup`, burying the payoff copy.
+const COMPACTION_THEN_TOOL_TURN: UIMessageChunk[] = [
+ { type: "start" },
+ { type: "start-step" },
+ ...openCompactionRow(),
+ summarizingPhase(),
+ closeCompactionRow(FULL_STATS),
+ { type: "finish-step" },
+ REBUILDING_PHASE,
+ { type: "start-step" },
+ {
+ type: "tool-input-start",
+ toolCallId: "search-1",
+ toolName: "web_search",
+ },
+ {
+ type: "tool-input-available",
+ toolCallId: "search-1",
+ toolName: "web_search",
+ input: { query: "latest news" },
+ },
+ {
+ type: "tool-output-available",
+ toolCallId: "search-1",
+ output: JSON.stringify({ result: "ok" }),
+ },
+ { type: "finish-step" },
+ { type: "start-step" },
+ ...textChunks("Done."),
+ { type: "finish-step" },
+ { type: "finish" },
+];
+
+// A pre-check false positive: the row opens, the phase streams, then the
+// prediction is retired — the row closes with the abort sentinel (output "")
+// and the turn continues with normal text. No compaction happened, so no
+// compaction copy may survive the settle.
+const ABORTED_COMPACTION_TURN: UIMessageChunk[] = [
+ { type: "start" },
+ { type: "start-step" },
+ ...openCompactionRow(),
+ summarizingPhase(),
+ {
+ type: "tool-output-available",
+ toolCallId: "compaction-1",
+ output: "",
+ },
+ { type: "finish-step" },
+ { type: "start-step" },
+ ...textChunks("No condensing needed."),
+ { type: "finish-step" },
+ { type: "finish" },
+];
+
+// Two compaction cycles in one assistant message: the first settles with
+// JSON stats, then a second cycle opens and streams its `summarizing`
+// phase. The live phase belongs to the second row only — the first must
+// keep showing its settled payoff copy.
+const TWO_CYCLE_TURN: UIMessageChunk[] = [
+ { type: "start" },
+ { type: "start-step" },
+ ...openCompactionRow("compaction-1"),
+ summarizingPhase(),
+ closeCompactionRow({ tokensBefore: 128000, tokensAfter: 31000 }),
+ { type: "finish-step" },
+ REBUILDING_PHASE,
+ { type: "start-step" },
+ ...textChunks("Continuing."),
+ { type: "finish-step" },
+ { type: "start-step" },
+ ...openCompactionRow("compaction-2"),
+ summarizingPhase(64000),
+ // The hold sits before this chunk so the second cycle stays live long
+ // enough to assert against (see SECOND_CYCLE_HOLD_INDEX).
+ closeCompactionRow(
+ { tokensBefore: 64000, tokensAfter: 20000 },
+ "compaction-2",
+ ),
+ { type: "finish-step" },
+ REBUILDING_PHASE,
+ { type: "start-step" },
+ ...textChunks("Done again.", "t2"),
+ { type: "finish-step" },
+ { type: "finish" },
+];
+
+const SECOND_CYCLE_HOLD_INDEX = TWO_CYCLE_TURN.findIndex(
+ (c) => c.type === "tool-output-available" && c.toolCallId === "compaction-2",
+);
+
+// A turn whose stream dies right after a `data-compaction` part — the Stop
+// button or a terminal error ends the stream with no trailing text, so no
+// content part ever lands to null out the phase. The streaming gate must
+// retire the bar when the stream closes.
+const DEAD_STREAM_TURN: UIMessageChunk[] = [
+ { type: "start" },
+ { type: "start-step" },
+ ...openCompactionRow(),
+ summarizingPhase(),
+ closeCompactionRow({ tokensBefore: 128000, tokensAfter: 31000 }),
+ { type: "finish-step" },
+ REBUILDING_PHASE,
+ // The hold sits before this chunk so the live "Reloading context…" bar
+ // is observable before the stream closes (see DEAD_STREAM_HOLD_INDEX).
+ { type: "finish" },
+];
+
+const DEAD_STREAM_HOLD_INDEX = DEAD_STREAM_TURN.findIndex(
+ (c) => c.type === "finish",
+);
+
+// A turn interrupted while the compaction row is still OPEN — no
+// `tool-output-available` ever arrives, so the row stays `input-available`.
+// Nothing may keep animating once the stream closes, and the card must not
+// claim a compaction that never reported completion.
+const INTERRUPTED_OPEN_ROW_TURN: UIMessageChunk[] = [
+ { type: "start" },
+ { type: "start-step" },
+ ...openCompactionRow(),
+ summarizingPhase(),
+ { type: "finish" },
+];
+
+const INTERRUPTED_HOLD_INDEX = INTERRUPTED_OPEN_ROW_TURN.findIndex(
+ (c) => c.type === "finish",
+);
+
+// The compaction fails outright: the row closes with `tool-output-error`
+// and the turn recovers with normal text. Nothing was condensed, so no
+// compaction copy — live or settled — may render for it.
+const FAILED_COMPACTION_TURN: UIMessageChunk[] = [
+ { type: "start" },
+ { type: "start-step" },
+ ...openCompactionRow(),
+ summarizingPhase(),
+ {
+ type: "tool-output-error",
+ toolCallId: "compaction-1",
+ errorText: "compaction failed",
+ },
+ { type: "finish-step" },
+ { type: "start-step" },
+ ...textChunks("Carrying on regardless."),
+ { type: "finish-step" },
+ { type: "finish" },
+];
+
+// Compression failed outright and the backend dropped the history: the row
+// closes with `dropped: true` so the user is told the context was reset
+// rather than watching the bar vanish.
+const DROPPED_COMPACTION_TURN: UIMessageChunk[] = [
+ { type: "start" },
+ { type: "start-step" },
+ ...openCompactionRow(),
+ summarizingPhase(),
+ {
+ type: "tool-output-available",
+ toolCallId: "compaction-1",
+ output: JSON.stringify({
+ summary: "Earlier messages were dropped to fit within context limits.",
+ dropped: true,
+ messagesBefore: 412,
+ }),
+ },
+ { type: "finish-step" },
+ REBUILDING_PHASE,
+ { type: "start-step" },
+ ...textChunks("Starting over from here."),
+ { type: "finish-step" },
+ { type: "finish" },
+];
+
+// Holds long enough to outlast `waitFor`'s default 1000ms window, so a
+// loaded CI runner can't watch the held state expire mid-poll.
+const HOLD_MS = 1_500;
+// Post-hold assertions wait for the stream to play out past the hold.
+const POST_HOLD_TIMEOUT = { timeout: 5_000 };
+
+// The rebuild phase is emitted AFTER the tool row closes — it covers the
+// transcript upload, CLI restart and uncached prefill, which is the silence
+// this feature exists to narrate. Holding on the chunk that follows it keeps
+// that window observable: settling the row on `output-available` would erase
+// the phase entirely.
+const REBUILDING_HOLD_INDEX =
+ COMPACTION_TURN.findIndex(
+ (c) =>
+ c.type === "data-compaction" &&
+ (c.data as { phase?: string } | undefined)?.phase === "rebuilding",
+ ) + 1;
+
+// `COMPACTION_TURN`'s chunk index of `tool-output-available` — the delay
+// applied *before* this chunk is what keeps the turn parked in the
+// "summarizing" phase long enough for `waitFor` to observe it (see below).
+const SUMMARIZING_HOLD_INDEX = COMPACTION_TURN.findIndex(
+ (c) => c.type === "tool-output-available",
+);
+
+describe("context compaction progress", () => {
+ beforeEach(() => {
+ resetCopilotChatRegistry();
+ server.use(
+ sessionHandler(),
+ // A uniform 15ms gap keeps the stream from resolving inside a single
+ // microtask tick, but the "summarizing" phase itself only spans the
+ // single gap between the `data-compaction` chunk and
+ // `tool-output-available` — at 15ms that's narrower than
+ // `waitFor`'s ~50ms poll interval, so the assertion below could miss
+ // it entirely depending on scheduling. Hold specifically at that
+ // gap for long enough to make the phase reliably observable.
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: COMPACTION_TURN,
+ perChunkDelaysMs: COMPACTION_TURN.map((_, i) =>
+ i === SUMMARIZING_HOLD_INDEX ? HOLD_MS : 15,
+ ),
+ }),
+ );
+ });
+
+ afterEach(() => {
+ resetCopilotChatRegistry();
+ });
+
+ it("shows a live bar while compacting, not a completed message", async () => {
+ renderHost();
+ await typeAndSend("summarise this");
+
+ await waitFor(() => {
+ expect(screen.getByRole("progressbar")).toBeDefined();
+ expect(screen.getByText("Condensing our conversation…")).toBeDefined();
+ // CompactionCard owns its own spinner/label — the generic
+ // ThinkingIndicator must not double up alongside it. The tool row
+ // (or, before it opens, the step-start marker) keeps `hasInflight`
+ // true throughout the compaction turn, so this holds from the very
+ // first chunk onward — not just once the bar is visible.
+ expect(screen.queryByText("Thinking...")).toBeNull();
+ });
+ });
+
+ it("lands on the payoff copy with real numbers", async () => {
+ renderHost();
+ await typeAndSend("summarise this");
+
+ await waitFor(() => {
+ expect(
+ screen.getByText(/Condensed 412 messages · 128K → 31K tokens/),
+ ).toBeDefined();
+ }, POST_HOLD_TIMEOUT);
+ });
+
+ it("never shows the old apologetic copy", async () => {
+ renderHost();
+ await typeAndSend("summarise this");
+
+ await waitFor(() => {
+ expect(screen.getByText("All caught up.")).toBeDefined();
+ }, POST_HOLD_TIMEOUT);
+ expect(screen.queryByText(/Earlier messages were summarized/)).toBeNull();
+ });
+
+ it("keeps a settled compaction row out of a collapsed tool group when another tool call follows it", async () => {
+ server.use(
+ sessionHandler(),
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: COMPACTION_THEN_TOOL_TURN,
+ delayMsBetweenChunks: 15,
+ }),
+ );
+
+ renderHost();
+ await typeAndSend("search and summarise");
+
+ await waitFor(() => {
+ expect(
+ screen.getByText(/Condensed 412 messages · 128K → 31K tokens/),
+ ).toBeDefined();
+ });
+ // If the compaction row had folded into a CollapsedToolGroup with the
+ // adjacent web_search call, the payoff copy above would be hidden
+ // behind a "N tool calls completed" toggle instead of standing alone.
+ expect(screen.queryByText(/tool calls/)).toBeNull();
+ });
+
+ it("renders nothing for a compaction that failed", async () => {
+ server.use(
+ sessionHandler(),
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: FAILED_COMPACTION_TURN,
+ delayMsBetweenChunks: 15,
+ }),
+ );
+
+ renderHost();
+ await typeAndSend("summarise this");
+
+ await waitFor(() => {
+ expect(screen.getByText("Carrying on regardless.")).toBeDefined();
+ });
+ // A failed compaction condensed nothing — neither the live copy nor
+ // the settled "Condensed…" claim may render for it.
+ expect(screen.queryByText(/Condensing our conversation/)).toBeNull();
+ expect(screen.queryByText(/Condensed/)).toBeNull();
+ expect(screen.queryByRole("progressbar")).toBeNull();
+ });
+
+ it("tells the user the context was reset when history was dropped", async () => {
+ server.use(
+ sessionHandler(),
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: DROPPED_COMPACTION_TURN,
+ delayMsBetweenChunks: 15,
+ }),
+ );
+
+ renderHost();
+ await typeAndSend("summarise this");
+
+ await waitFor(() => {
+ expect(screen.getByText("Starting over from here.")).toBeDefined();
+ expect(
+ screen.getByText(
+ "Started a fresh context — earlier messages were dropped",
+ ),
+ ).toBeDefined();
+ });
+ // A drop condensed nothing: no payoff claim, no lingering bar.
+ expect(screen.queryByText(/Condensed/)).toBeNull();
+ expect(screen.queryByRole("progressbar")).toBeNull();
+ });
+
+ it("renders nothing for a row retired by an aborted prediction", async () => {
+ server.use(
+ sessionHandler(),
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: ABORTED_COMPACTION_TURN,
+ delayMsBetweenChunks: 15,
+ }),
+ );
+
+ renderHost();
+ await typeAndSend("quick question");
+
+ await waitFor(() => {
+ expect(screen.getByText("No condensing needed.")).toBeDefined();
+ });
+ // The abort sentinel (output "") must not read as a real compaction —
+ // neither the live copy nor the settled payoff copy may survive.
+ expect(screen.queryByText(/Condensing our conversation/)).toBeNull();
+ expect(screen.queryByText(/Condensed the conversation/)).toBeNull();
+ expect(screen.queryByRole("progressbar")).toBeNull();
+ });
+
+ it("keeps the first cycle settled while a second cycle streams", async () => {
+ server.use(
+ sessionHandler(),
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: TWO_CYCLE_TURN,
+ perChunkDelaysMs: TWO_CYCLE_TURN.map((_, i) =>
+ i === SECOND_CYCLE_HOLD_INDEX ? HOLD_MS : 15,
+ ),
+ }),
+ );
+
+ renderHost();
+ await typeAndSend("summarise twice");
+
+ await waitFor(() => {
+ // Second cycle live in `summarizing`…
+ expect(screen.getByText("Condensing our conversation…")).toBeDefined();
+ // …while the first row keeps its settled payoff copy…
+ expect(
+ screen.getByText(/Condensed the conversation · 128K → 31K tokens/),
+ ).toBeDefined();
+ // …and only the live row carries a progress bar. Without the
+ // per-row phase gate, the second cycle's phase re-animates the
+ // first (closed) row and two bars render.
+ expect(screen.getAllByRole("progressbar")).toHaveLength(1);
+ });
+ });
+
+ it("retires the live bar when the stream dies after a compaction phase", async () => {
+ server.use(
+ sessionHandler(),
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: DEAD_STREAM_TURN,
+ perChunkDelaysMs: DEAD_STREAM_TURN.map((_, i) =>
+ i === DEAD_STREAM_HOLD_INDEX ? HOLD_MS : 15,
+ ),
+ }),
+ );
+
+ renderHost();
+ await typeAndSend("summarise this");
+
+ // Live during the stream: the trailing `rebuilding` phase keeps the
+ // bar up while the connection is open.
+ await waitFor(() => {
+ expect(screen.getByRole("progressbar")).toBeDefined();
+ });
+ // Once the stream closes with no trailing text, the streaming gate
+ // must null the phase — the row settles instead of spinning forever.
+ await waitFor(() => {
+ expect(screen.queryByRole("progressbar")).toBeNull();
+ expect(
+ screen.getByText(/Condensed the conversation · 128K → 31K tokens/),
+ ).toBeDefined();
+ }, POST_HOLD_TIMEOUT);
+ });
+
+ it("keeps the bar live through the rebuild that follows the closed row", async () => {
+ server.use(
+ sessionHandler(),
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: COMPACTION_TURN,
+ perChunkDelaysMs: COMPACTION_TURN.map((_, i) =>
+ i === REBUILDING_HOLD_INDEX ? HOLD_MS : 15,
+ ),
+ }),
+ );
+
+ renderHost();
+ await typeAndSend("summarise this");
+
+ // The tool row has already closed with its JSON output by this point;
+ // the row must still be live and narrating the rebuild.
+ await waitFor(() => {
+ expect(screen.getByText("Reloading context…")).toBeDefined();
+ // With the row closed, `hasInflight` is false — without the live-
+ // compaction gate the ThinkingIndicator would stack its own spinner
+ // and timer directly under the CompactionCard's.
+ expect(screen.queryByText("Thinking...")).toBeNull();
+ });
+ expect(screen.getByRole("progressbar")).toBeDefined();
+ });
+
+ it("renders nothing for a row left open when the stream is interrupted", async () => {
+ server.use(
+ sessionHandler(),
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: INTERRUPTED_OPEN_ROW_TURN,
+ perChunkDelaysMs: INTERRUPTED_OPEN_ROW_TURN.map((_, i) =>
+ i === INTERRUPTED_HOLD_INDEX ? HOLD_MS : 15,
+ ),
+ }),
+ );
+
+ renderHost();
+ await typeAndSend("summarise this");
+
+ // Live while the connection is open.
+ await waitFor(() => {
+ expect(screen.getByRole("progressbar")).toBeDefined();
+ });
+ // The row never closed, so once the stream is gone it is neither a live
+ // bar nor a "Condensed…" claim — it disappears.
+ await waitFor(() => {
+ expect(screen.queryByRole("progressbar")).toBeNull();
+ }, POST_HOLD_TIMEOUT);
+ expect(screen.queryByText(/Condensed/)).toBeNull();
+ expect(screen.queryByText(/Condensing/)).toBeNull();
+ });
+});
+
+// A row restored from the DB never carries a `data-compaction` part — that
+// part only ever exists on a live stream. `getLatestCompactionPhase` reads
+// null in that case, and the tool part's persisted output makes it
+// `output-available`, so the card renders settled — this proves the
+// back-compat path for both the new JSON payload and the old plain-sentence
+// rows some sessions still have on disk.
+describe("compaction rows restored from the database", () => {
+ beforeEach(() => {
+ resetCopilotChatRegistry();
+ });
+
+ afterEach(() => {
+ resetCopilotChatRegistry();
+ });
+
+ it("renders new JSON rows with their numbers and no bar", async () => {
+ const messages: SessionDetailResponseMessagesItem[] = [
+ { role: "user", content: "hi", sequence: 0 },
+ {
+ role: "assistant",
+ content: "",
+ sequence: 1,
+ tool_calls: [
+ {
+ id: "compaction-1",
+ type: "function",
+ function: { name: "context_compaction", arguments: "{}" },
+ },
+ ],
+ },
+ {
+ role: "tool",
+ tool_call_id: "compaction-1",
+ sequence: 2,
+ content: JSON.stringify({
+ summary:
+ "Earlier messages were summarized to fit within context limits.",
+ tokensBefore: 128000,
+ tokensAfter: 31000,
+ }),
+ },
+ ];
+ renderHost({ sessionOverride: { messages } });
+
+ await waitFor(() => {
+ expect(
+ screen.getByText("Condensed the conversation · 128K → 31K tokens"),
+ ).toBeDefined();
+ });
+ expect(screen.queryByRole("progressbar")).toBeNull();
+ });
+
+ it("renders legacy plain-sentence rows without crashing", async () => {
+ const messages: SessionDetailResponseMessagesItem[] = [
+ { role: "user", content: "hi", sequence: 0 },
+ {
+ role: "assistant",
+ content: "",
+ sequence: 1,
+ tool_calls: [
+ {
+ id: "compaction-1",
+ type: "function",
+ function: { name: "context_compaction", arguments: "{}" },
+ },
+ ],
+ },
+ {
+ role: "tool",
+ tool_call_id: "compaction-1",
+ sequence: 2,
+ content:
+ "Earlier messages were summarized to fit within context limits.",
+ },
+ ];
+ renderHost({ sessionOverride: { messages } });
+
+ await waitFor(() => {
+ expect(
+ screen.getByText("Condensed the conversation to keep going"),
+ ).toBeDefined();
+ });
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expert-threads.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expert-threads.test.tsx
index 47d0668d27..cc9ef56476 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expert-threads.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expert-threads.test.tsx
@@ -144,6 +144,7 @@ vi.mock(
);
vi.mock("../components/ChatMessagesContainer/helpers", () => ({
buildRenderSegments: () => [],
+ getLatestCompactionPhase: () => null,
getTurnMessages: () => [],
isChainableToolPart: () => false,
parseSpecialMarkers: () => ({ markerType: null }),
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/messageParts.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/messageParts.test.ts
new file mode 100644
index 0000000000..0c9de4d5af
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/messageParts.test.ts
@@ -0,0 +1,83 @@
+import type { UIMessage } from "ai";
+import { describe, expect, it } from "vitest";
+import {
+ getLatestAssistantStatusMessage,
+ isBookkeepingPart,
+} from "../messageParts";
+
+type Part = UIMessage["parts"][number];
+
+function part(type: string, data?: unknown): Part {
+ return { type, data } as unknown as Part;
+}
+
+function assistant(parts: Part[]): UIMessage {
+ return { id: "m1", role: "assistant", parts } as UIMessage;
+}
+
+const STATUS = part("data-status", { message: "Looking up your agents" });
+
+describe("isBookkeepingPart", () => {
+ it("covers every transient part the stream drives chrome with", () => {
+ expect(isBookkeepingPart({ type: "data-cursor" })).toBe(true);
+ expect(isBookkeepingPart({ type: "data-status" })).toBe(true);
+ expect(isBookkeepingPart({ type: "data-dream-operations" })).toBe(true);
+ expect(isBookkeepingPart({ type: "data-compaction" })).toBe(true);
+ });
+
+ it("leaves content alone", () => {
+ expect(isBookkeepingPart({ type: "text" })).toBe(false);
+ expect(isBookkeepingPart({ type: "reasoning" })).toBe(false);
+ expect(isBookkeepingPart({ type: "step-start" })).toBe(false);
+ expect(isBookkeepingPart({ type: "tool-run_agent" })).toBe(false);
+ expect(isBookkeepingPart({ type: "tool-context_compaction" })).toBe(false);
+ });
+});
+
+describe("getLatestAssistantStatusMessage", () => {
+ it("reads the latest status", () => {
+ expect(getLatestAssistantStatusMessage([assistant([STATUS])])).toBe(
+ "Looking up your agents",
+ );
+ });
+
+ it("keeps the status while a compaction runs behind it", () => {
+ // A compaction starting is not the model answering — the status is
+ // still the most recent thing the user was told.
+ const messages = [
+ assistant([
+ STATUS,
+ part("data-compaction", {
+ phase: "summarizing",
+ tokensBefore: 128_000,
+ }),
+ part("data-compaction", { phase: "rebuilding" }),
+ ]),
+ ];
+ expect(getLatestAssistantStatusMessage(messages)).toBe(
+ "Looking up your agents",
+ );
+ });
+
+ it("drops the status once real content lands past it", () => {
+ const messages = [
+ assistant([
+ STATUS,
+ part("data-compaction", { phase: "summarizing" }),
+ { type: "text", text: "Here you go." } as unknown as Part,
+ ]),
+ ];
+ expect(getLatestAssistantStatusMessage(messages)).toBeNull();
+ });
+
+ it("ignores non-assistant tails and malformed payloads", () => {
+ expect(
+ getLatestAssistantStatusMessage([
+ { id: "u1", role: "user", parts: [STATUS] } as UIMessage,
+ ]),
+ ).toBeNull();
+ expect(
+ getLatestAssistantStatusMessage([assistant([part("data-status", {})])]),
+ ).toBeNull();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
index ea9e2a8f80..6e9b126fb7 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
@@ -24,6 +24,9 @@ import type { TurnStatsMap } from "../../helpers/convertChatSessionToUiMessages"
import { revealKickoffMessages } from "../../expertKickoff";
import {
buildRenderSegments,
+ getLastCompactionCallId,
+ getLatestCompactionPhase,
+ getLatestCompactionStats,
getTurnMessages,
type MessagePart,
type RenderSegment,
@@ -31,10 +34,18 @@ import {
shouldShowTaskListNotice,
splitReasoningAndResponse,
} from "./helpers";
+import {
+ getLatestAssistantStatusMessage,
+ isBookkeepingPart,
+} from "../../messageParts";
import { RESTORE_STALL_TIMEOUT_MS } from "../../restoreConstants";
import type { ExpertIdentity } from "../../useExpertMap";
import { useCopilotUIStore } from "../../store";
import { ChatMinimap } from "../ChatMinimap/ChatMinimap";
+import type {
+ CompactionPhase,
+ CompactionStats,
+} from "../CompactionCard/helpers";
import { WorkCard } from "../WorkCard/WorkCard";
import { getWorkRunMetadata, toPreview } from "../WorkCard/helpers";
import { AssistantMessageActions } from "./components/AssistantMessageActions";
@@ -110,6 +121,10 @@ interface RenderSegmentOptions {
fileUrlBuilder?: (fileId: string) => string;
forceArtifacts?: boolean;
readOnly?: boolean;
+ compactionPhase?: CompactionPhase | null;
+ liveCompactionCallId?: string | null;
+ liveCompactionStats?: CompactionStats;
+ isCurrentlyStreaming?: boolean;
}
function renderSegments(
@@ -117,7 +132,16 @@ function renderSegments(
messageID: string,
options: RenderSegmentOptions = {},
): React.ReactNode[] {
- const { onRetry, fileUrlBuilder, forceArtifacts, readOnly } = options;
+ const {
+ onRetry,
+ fileUrlBuilder,
+ forceArtifacts,
+ readOnly,
+ compactionPhase,
+ liveCompactionCallId,
+ liveCompactionStats,
+ isCurrentlyStreaming,
+ } = options;
return segments.map((seg, segIdx) => {
if (seg.kind === "collapsed-group") {
return <CollapsedToolGroup key={`group-${segIdx}`} parts={seg.parts} />;
@@ -140,6 +164,10 @@ function renderSegments(
fileUrlBuilder={fileUrlBuilder}
forceArtifacts={forceArtifacts}
readOnly={readOnly}
+ compactionPhase={compactionPhase}
+ liveCompactionCallId={liveCompactionCallId}
+ liveCompactionStats={liveCompactionStats}
+ isCurrentlyStreaming={isCurrentlyStreaming}
/>
);
});
@@ -418,15 +446,9 @@ export function ChatMessagesContainer({
const hasInflight = (() => {
if (lastMessage?.role !== "assistant") return false;
- // Ignore bookkeeping parts. data-cursor is legacy resume metadata and
- // data-status is transient copy for the Thinking indicator; neither
- // counts as "real" content that hides the indicator.
- const parts = lastMessage.parts.filter(
- (p) =>
- p.type !== "data-cursor" &&
- p.type !== "data-status" &&
- p.type !== "data-dream-operations",
- );
+ // Ignore bookkeeping parts — none of them counts as "real" content that
+ // hides the Thinking indicator. See `isBookkeepingPart` for the list.
+ const parts = lastMessage.parts.filter((p) => !isBookkeepingPart(p));
if (parts.length === 0) return false;
const lastPart = parts[parts.length - 1];
@@ -461,21 +483,16 @@ export function ChatMessagesContainer({
// the Thinking indicator is up — but only if it wasn't invalidated by a
// more recent content part (in which case the model has moved on and the
// status is stale).
- const latestStatusMessage = (() => {
- if (lastMessage?.role !== "assistant") return null;
- for (let i = lastMessage.parts.length - 1; i >= 0; i--) {
- const part = lastMessage.parts[i];
- if (part.type === "data-cursor") continue;
- if (part.type === "data-dream-operations") continue;
- if (part.type === "data-status") {
- const data = (part as { data?: { message?: unknown } }).data;
- return typeof data?.message === "string" ? data.message : null;
- }
- // Any other part = the model has produced output past the status.
- return null;
- }
- return null;
- })();
+ const latestStatusMessage = getLatestAssistantStatusMessage(messages);
+
+ // A live compaction narrates itself via CompactionCard — the generic
+ // Thinking indicator must not stack a second spinner and timer under it.
+ // The `rebuilding` phase arrives after the tool row has already closed,
+ // so `hasInflight` alone cannot cover that window.
+ const liveCompactionPhase =
+ isChatStreaming && lastMessage?.role === "assistant"
+ ? getLatestCompactionPhase(lastMessage.parts)
+ : null;
// Suppressed during active-session restore so the ThinkingIndicator and
// the "Retrieving latest messages" spinner can't both render — the
@@ -483,6 +500,7 @@ export function ChatMessagesContainer({
// ``hasConnectedThisMountRef`` latch in useCopilotStream for why).
const showThinking =
!isRestoringActiveSession &&
+ liveCompactionPhase === null &&
(status === "submitted" || (status === "streaming" && !hasInflight));
const isActivelyStreaming = status === "streaming" || status === "submitted";
const { elapsedSeconds } = useElapsedTimer(
@@ -673,12 +691,33 @@ export function ChatMessagesContainer({
isAssistant &&
messageIndex <= messages.length - 1 &&
(!nextMessage || nextMessage.role === "user");
- // data-cursor / data-status parts are internal bookkeeping —
- // strip them before any render/split logic so they never reach
- // the user UI. data-status surfaces via ThinkingIndicator.
+ // Bookkeeping parts are stripped before any render/split logic so
+ // they never reach the user UI, and so one landing between two
+ // tool calls can't split a chain. data-status surfaces via
+ // ThinkingIndicator; data-compaction via CompactionCard.
const renderableParts = message.parts.filter(
- (p) => p.type !== "data-cursor" && p.type !== "data-status",
+ (p) => !isBookkeepingPart(p),
);
+ // Only a message that is actively streaming can have a live
+ // compaction phase — a stopped or failed turn must not leave an
+ // eternal progress bar. Replayed/settled messages never carry
+ // `data-compaction` parts, so they derive null either way.
+ const compactionPhase = isCurrentlyStreaming
+ ? getLatestCompactionPhase(message.parts)
+ : null;
+ // The phase belongs to the LAST compaction row only; earlier
+ // (settled) rows in the same message stay settled.
+ const liveCompactionCallId =
+ compactionPhase !== null
+ ? getLastCompactionCallId(message.parts)
+ : null;
+ // Stats streamed on the `data-compaction` parts pace the live
+ // progress curve while the tool row is still open (its own
+ // output stats only exist once it closes).
+ const liveCompactionStats =
+ compactionPhase !== null
+ ? getLatestCompactionStats(message.parts)
+ : undefined;
const textParts = renderableParts.filter(
(p): p is Extract<typeof p, { type: "text" }> =>
p.type === "text",
@@ -764,6 +803,9 @@ export function ChatMessagesContainer({
fileUrlBuilder={fileUrlBuilder}
forceArtifacts={readOnly}
readOnly={readOnly}
+ compactionPhase={compactionPhase}
+ liveCompactionCallId={liveCompactionCallId}
+ liveCompactionStats={liveCompactionStats}
/>
) : responseSegments ? (
renderSegments(responseSegments, message.id, {
@@ -771,6 +813,10 @@ export function ChatMessagesContainer({
fileUrlBuilder,
forceArtifacts: readOnly,
readOnly,
+ compactionPhase,
+ liveCompactionCallId,
+ liveCompactionStats,
+ isCurrentlyStreaming,
})
) : (
(() => {
@@ -784,6 +830,10 @@ export function ChatMessagesContainer({
fileUrlBuilder={fileUrlBuilder}
forceArtifacts={readOnly}
readOnly={readOnly}
+ compactionPhase={compactionPhase}
+ liveCompactionCallId={liveCompactionCallId}
+ liveCompactionStats={liveCompactionStats}
+ isCurrentlyStreaming={isCurrentlyStreaming}
/>
));
return isNewToolUI ? (
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
index 2fe818106c..781fe2ac54 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
@@ -117,6 +117,7 @@ vi.mock("../../CopilotPendingReviews/CopilotPendingReviews", () => ({
// Tests below override this default by re-mocking ../helpers as needed.
vi.mock("../helpers", () => ({
buildRenderSegments: () => [],
+ getLatestCompactionPhase: () => null,
getTurnMessages: () => [],
isChainableToolPart: () => false,
parseSpecialMarkers: (text: string) => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/helpers.test.ts
index 0be4efbf7c..e1e0f08380 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/helpers.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/helpers.test.ts
@@ -4,6 +4,8 @@ import {
buildRenderSegments,
extractWorkspaceArtifacts,
filePartToArtifactRef,
+ getLatestCompactionPhase,
+ getLatestCompactionStats,
getMessageArtifacts,
getMostRecentArtifact,
isCompletedToolPart,
@@ -966,6 +968,135 @@ describe("getMostRecentArtifact", () => {
});
});
+function dataPart(type: string, data?: unknown): MessagePart {
+ return { type, data } as unknown as MessagePart;
+}
+
+function compactionRowPart(
+ state: string,
+ output?: unknown,
+ id = "compaction-1",
+): MessagePart {
+ return {
+ type: "tool-context_compaction",
+ state,
+ toolCallId: id,
+ toolName: "context_compaction",
+ input: {},
+ output,
+ } as unknown as MessagePart;
+}
+
+describe("getLatestCompactionPhase", () => {
+ const openRow = compactionRowPart("input-available");
+ const summarizing = dataPart("data-compaction", {
+ phase: "summarizing",
+ tokensBefore: 128_000,
+ });
+
+ it("reads the latest phase behind the open row", () => {
+ expect(
+ getLatestCompactionPhase([stepStartPart(), openRow, summarizing]),
+ ).toBe("summarizing");
+ });
+
+ it("survives ANY transient data part landing mid-compaction", () => {
+ // data-pending-drained and data-mode-changed are real parts the backend
+ // emits mid-turn; an enumerated deny-list dropped the phase (and the
+ // bar) the moment one arrived.
+ const parts = [
+ openRow,
+ summarizing,
+ dataPart("data-pending-drained", { count: 1 }),
+ dataPart("data-mode-changed", { mode: "chat" }),
+ dataPart("data-status", { message: "working" }),
+ dataPart("data-some-future-part"),
+ ];
+ expect(getLatestCompactionPhase(parts)).toBe("summarizing");
+ });
+
+ it("nulls the phase once real content lands past it", () => {
+ expect(
+ getLatestCompactionPhase([openRow, summarizing, textPart("Back to it.")]),
+ ).toBeNull();
+ });
+
+ it("skips whitespace-only streaming text", () => {
+ expect(
+ getLatestCompactionPhase([openRow, summarizing, textPart(" ")]),
+ ).toBe("summarizing");
+ });
+
+ it("drops the phase when the row was retired by the abort sentinel", () => {
+ const abortedRow = compactionRowPart("output-available", "");
+ expect(getLatestCompactionPhase([abortedRow, summarizing])).toBeNull();
+ });
+
+ it("drops the phase when the row closed with an error", () => {
+ const failedRow = compactionRowPart("output-error");
+ expect(getLatestCompactionPhase([failedRow, summarizing])).toBeNull();
+ });
+
+ it("ignores an earlier retired row when a later cycle is live", () => {
+ const abortedRow = compactionRowPart(
+ "output-available",
+ "",
+ "compaction-1",
+ );
+ const secondRow = compactionRowPart(
+ "input-available",
+ undefined,
+ "compaction-2",
+ );
+ expect(
+ getLatestCompactionPhase([
+ abortedRow,
+ textPart("hi"),
+ secondRow,
+ summarizing,
+ ]),
+ ).toBe("summarizing");
+ });
+
+ it("returns null with no compaction parts at all", () => {
+ expect(getLatestCompactionPhase([textPart("hello")])).toBeNull();
+ });
+});
+
+describe("getLatestCompactionStats", () => {
+ it("merges stats across data-compaction parts, later phases winning", () => {
+ const parts = [
+ dataPart("data-compaction", {
+ phase: "summarizing",
+ tokensBefore: 128_000,
+ }),
+ dataPart("data-compaction", {
+ phase: "rebuilding",
+ tokensBefore: 128_000,
+ tokensAfter: 31_000,
+ messagesBefore: 412,
+ messagesAfter: 38,
+ }),
+ ];
+ expect(getLatestCompactionStats(parts)).toEqual({
+ tokensBefore: 128_000,
+ tokensAfter: 31_000,
+ messagesBefore: 412,
+ messagesAfter: 38,
+ });
+ });
+
+ it("survives phase-only and junk payloads", () => {
+ const parts = [
+ dataPart("data-compaction", { phase: "summarizing" }),
+ dataPart("data-compaction", null),
+ dataPart("data-compaction"),
+ textPart("hi"),
+ ];
+ expect(getLatestCompactionStats(parts)).toEqual({});
+ });
+});
+
describe("shouldShowTaskListNotice", () => {
const activeTodos: TodoItem[] = [
{ content: "Step 1", status: "in_progress" },
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/ChainMessageParts.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/ChainMessageParts.tsx
index 08237a122f..ec7dc66d0e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/ChainMessageParts.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/ChainMessageParts.tsx
@@ -1,6 +1,10 @@
import { isChainableToolPart, type MessagePart } from "../helpers";
import { buildChainSegments } from "../../ToolChain/helpers";
import { ToolChain } from "../../ToolChain/ToolChain";
+import type {
+ CompactionPhase,
+ CompactionStats,
+} from "../../CompactionCard/helpers";
import { MessagePartRenderer } from "./MessagePartRenderer";
interface Props {
@@ -11,6 +15,9 @@ interface Props {
fileUrlBuilder?: (fileId: string) => string;
forceArtifacts?: boolean;
readOnly?: boolean;
+ compactionPhase?: CompactionPhase | null;
+ liveCompactionCallId?: string | null;
+ liveCompactionStats?: CompactionStats;
}
export function ChainMessageParts({
@@ -21,6 +28,9 @@ export function ChainMessageParts({
fileUrlBuilder,
forceArtifacts,
readOnly,
+ compactionPhase,
+ liveCompactionCallId,
+ liveCompactionStats,
}: Props) {
const segments = buildChainSegments(
parts,
@@ -53,6 +63,10 @@ export function ChainMessageParts({
fileUrlBuilder={fileUrlBuilder}
forceArtifacts={forceArtifacts}
readOnly={readOnly}
+ compactionPhase={compactionPhase}
+ liveCompactionCallId={liveCompactionCallId}
+ liveCompactionStats={liveCompactionStats}
+ isCurrentlyStreaming={isCurrentlyStreaming}
/>
);
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessagePartRenderer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessagePartRenderer.tsx
index fc3efe6a2f..dae799432e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessagePartRenderer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessagePartRenderer.tsx
@@ -23,8 +23,16 @@ import { RunMCPToolComponent } from "../../../tools/RunMCPTool/RunMCPTool";
import { SearchDocsTool } from "../../../tools/SearchDocs/SearchDocs";
import { SetupTriggerTool } from "../../../tools/SetupTrigger/SetupTrigger";
import { ViewAgentOutputTool } from "../../../tools/ViewAgentOutput/ViewAgentOutput";
+import { CompactionCard } from "../../CompactionCard/CompactionCard";
+import {
+ parseCompactionOutput,
+ type CompactionPhase,
+ type CompactionStats,
+} from "../../CompactionCard/helpers";
+import { COMPACTION_PART_TYPE } from "../../ToolChain/helpers";
import {
extractWorkspaceArtifacts,
+ isRetiredCompactionRow,
parseSpecialMarkers,
resolveWorkspaceUrls,
} from "../helpers";
@@ -144,6 +152,22 @@ interface Props {
/** Read-only mode — forwarded so embedded ``ArtifactCard``s
* download on click instead of opening a panel. */
readOnly?: boolean;
+ /** Live `data-compaction` phase for the enclosing message, derived by
+ * the caller from the message's parts. Drives the compaction row's
+ * progress bar; null once the row has settled into history. */
+ compactionPhase?: CompactionPhase | null;
+ /** Tool-call ID of the message's last compaction row — the only row the
+ * live phase applies to. Earlier (settled) rows render as history even
+ * while a later cycle streams its phases. */
+ liveCompactionCallId?: string | null;
+ /** Stats streamed on the message's `data-compaction` parts. They pace the
+ * live progress curve before the tool row closes; once it does, the
+ * row's own parsed output wins. */
+ liveCompactionStats?: CompactionStats;
+ /** Whether the enclosing message is still streaming. A compaction row
+ * only animates while it is; once the stream ends the row is history,
+ * however it was left. */
+ isCurrentlyStreaming?: boolean;
}
export function MessagePartRenderer({
@@ -154,6 +178,10 @@ export function MessagePartRenderer({
fileUrlBuilder,
forceArtifacts,
readOnly,
+ compactionPhase,
+ liveCompactionCallId,
+ liveCompactionStats,
+ isCurrentlyStreaming,
}: Props) {
const key = `${messageID}-${partIndex}`;
@@ -267,6 +295,44 @@ export function MessagePartRenderer({
// list while any task is active. See `TaskListNotice` rendering in
// `ChatMessagesContainer`.
return null;
+ case COMPACTION_PART_TYPE: {
+ const toolPart = part as ToolUIPart;
+ // A failed compaction, or one closed by the abort sentinel (output ""),
+ // condensed nothing — settled "Condensed…" copy would report work that
+ // never happened. Render nothing; failure messaging belongs to the
+ // turn-level error surfaces, not a maintenance row. Same predicate the
+ // phase derivation uses, so the row and the bar can never disagree.
+ if (isRetiredCompactionRow(part)) return null;
+ const settled = toolPart.state === "output-available";
+ // A row still open when the stream is over never completed — the
+ // user stopped the turn or the connection dropped mid-compaction.
+ // Claiming "Condensed the conversation" would report work that
+ // never finished, so render nothing, like the abort sentinel.
+ if (!isCurrentlyStreaming && !settled) return null;
+ const isLiveRow =
+ liveCompactionCallId != null &&
+ toolPart.toolCallId === liveCompactionCallId;
+ const phase = isLiveRow ? (compactionPhase ?? null) : null;
+ const outputStats = parseCompactionOutput(
+ settled ? toolPart.output : undefined,
+ );
+ // The streamed `data-compaction` stats pace the live curve while the
+ // row is still open; the row's own output wins once it lands.
+ const stats = isLiveRow
+ ? { ...liveCompactionStats, ...outputStats }
+ : outputStats;
+ return (
+ <CompactionCard
+ key={key}
+ phase={phase}
+ stats={stats}
+ // While streaming, an open row with no phase yet is still live —
+ // settling on `phase === null` alone would flash the settled copy
+ // in the gap before the first progress part lands.
+ isSettled={!isCurrentlyStreaming || (settled && phase === null)}
+ />
+ );
+ }
default:
// Render a generic tool indicator for SDK built-in
// tools (Read, Glob, Grep, etc.) or any unrecognized tool
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/helpers.ts
index c69aa73c0c..793135197d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/helpers.ts
@@ -4,6 +4,13 @@ import { parseWorkspaceURI } from "@/lib/workspace-uri";
import { FileUIPart, ToolUIPart, UIDataTypes, UIMessage, UITools } from "ai";
import { isCorruptedCardToolPart } from "../../helpers/toolOutput";
import type { ArtifactRef } from "../../store";
+import {
+ COMPACTION_DATA_PART_TYPE,
+ readCompactionStats,
+ type CompactionPhase,
+ type CompactionStats,
+} from "../CompactionCard/helpers";
+import { COMPACTION_PART_TYPE } from "../ToolChain/helpers";
import type { TodoItem } from "../TaskProgressBar/helpers";
export function shouldShowTaskListNotice({
@@ -53,6 +60,11 @@ const LEGACY_CUSTOM_TOOL_TYPES = new Set([
"tool-search_feature_requests",
"tool-create_feature_request",
"tool-decompose_goal",
+ // Renders via CompactionCard, not the generic completed-tool grouping —
+ // excluding it here keeps a settled compaction row from folding into a
+ // collapsed "N tool calls" group next to an adjacent completed tool,
+ // which would bury its payoff copy.
+ COMPACTION_PART_TYPE,
]);
const REASONING_TOOL_TYPES = new Set([
@@ -73,9 +85,101 @@ export function isReasoningToolPart(part: MessagePart): boolean {
// compact result view for known backend tools and a structured fallback for
// SDK or future tools, so no tool can fall back to the legacy top-level UI.
export function isChainableToolPart(part: MessagePart): boolean {
+ if (part.type === COMPACTION_PART_TYPE) return false;
return part.type === "reasoning" || part.type.startsWith("tool-");
}
+const COMPACTION_PHASES = new Set(["summarizing", "rebuilding"]);
+
+// All `data-*` parts are transient bookkeeping (status, cursor,
+// pending-drained, mode-changed, …) — none of them is content that settles
+// a compaction row, and neither may any future one. Enumerating them here
+// would silently kill the bar the day a new data part ships mid-compaction.
+function isCompactionTransparentPart(part: MessagePart): boolean {
+ return (
+ part.type.startsWith("data-") ||
+ part.type === COMPACTION_PART_TYPE ||
+ part.type === "step-start"
+ );
+}
+
+/**
+ * A row closed by the abort sentinel (output "") or an error never compacted
+ * anything — any phase parts it left behind are stale, and the row itself
+ * renders nothing. This is a cross-language contract with the backend's
+ * close paths, so it lives here once: `MessagePartRenderer` decides what to
+ * draw with the same predicate `getLatestCompactionPhase` decides with.
+ */
+export function isRetiredCompactionRow(part: MessagePart): boolean {
+ if (part.type !== COMPACTION_PART_TYPE || !("state" in part)) return false;
+ const tool = part as ToolUIPart;
+ if (tool.state === "output-error") return true;
+ return (
+ tool.state === "output-available" &&
+ typeof tool.output === "string" &&
+ tool.output.trim() === ""
+ );
+}
+
+/**
+ * Latest `data-compaction` phase on a message, or null once real content has
+ * landed past it (at which point the compaction row is settled history).
+ */
+export function getLatestCompactionPhase(
+ parts: MessagePart[],
+): CompactionPhase | null {
+ const lastRow = parts.findLast((p) => p.type === COMPACTION_PART_TYPE);
+ if (lastRow && isRetiredCompactionRow(lastRow)) return null;
+ for (let i = parts.length - 1; i >= 0; i--) {
+ const part = parts[i];
+ if (part.type === COMPACTION_DATA_PART_TYPE) {
+ const data = (part as { data?: { phase?: unknown } }).data;
+ const phase = data?.phase;
+ if (typeof phase === "string" && COMPACTION_PHASES.has(phase)) {
+ return phase as CompactionPhase;
+ }
+ return null;
+ }
+ if (isCompactionTransparentPart(part)) continue;
+ if (part.type === "text" && "text" in part && !part.text.trim()) continue;
+ return null;
+ }
+ return null;
+}
+
+/**
+ * Stats carried by the message's `data-compaction` parts, merged in stream
+ * order (later phases override earlier ones). They pace the live progress
+ * curve before the tool row closes with its own — authoritative — stats.
+ */
+export function getLatestCompactionStats(
+ parts: MessagePart[],
+): CompactionStats {
+ const stats: CompactionStats = {};
+ for (const part of parts) {
+ if (part.type !== COMPACTION_DATA_PART_TYPE) continue;
+ const data = (part as { data?: unknown }).data;
+ if (typeof data !== "object" || data === null) continue;
+ Object.assign(stats, readCompactionStats(data as Record<string, unknown>));
+ }
+ return stats;
+}
+
+/**
+ * Tool-call ID of the message's last `tool-context_compaction` part. The live
+ * phase applies only to this row — without the ID gate, a second compaction
+ * cycle's `summarizing` part would flip earlier (settled) rows back to live.
+ */
+export function getLastCompactionCallId(parts: MessagePart[]): string | null {
+ for (let i = parts.length - 1; i >= 0; i--) {
+ const part = parts[i];
+ if (part.type === COMPACTION_PART_TYPE && "toolCallId" in part) {
+ return (part as ToolUIPart).toolCallId ?? null;
+ }
+ }
+ return null;
+}
+
// Default workspace-file URL shape: ``/api/proxy/api/workspace/files/<uuid>/download``.
// Other surfaces (e.g. public share viewer) pass their own pattern into
// ``filePartToArtifactRef`` rather than loosen this one — keeping the
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/CompactionCard.stories.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/CompactionCard.stories.tsx
new file mode 100644
index 0000000000..2aba48c911
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/CompactionCard.stories.tsx
@@ -0,0 +1,59 @@
+import type { Meta, StoryObj } from "@storybook/nextjs";
+import { CompactionCard } from "./CompactionCard";
+
+const meta: Meta<typeof CompactionCard> = {
+ title: "Copilot/CompactionCard",
+ component: CompactionCard,
+ parameters: {
+ docs: {
+ description: {
+ component:
+ "Context compaction as one continuous moment. The bar approaches each phase's ceiling exponentially so it can never finish before the work does; entering the rebuild phase raises the ceiling, and the row settles into a one-line receipt when the stream moves on.",
+ },
+ },
+ },
+};
+export default meta;
+
+type Story = StoryObj<typeof CompactionCard>;
+
+export const Summarizing: Story = {
+ args: {
+ phase: "summarizing",
+ stats: { tokensBefore: 128_000 },
+ isSettled: false,
+ },
+};
+
+export const Rebuilding: Story = {
+ args: {
+ phase: "rebuilding",
+ stats: { tokensBefore: 128_000, tokensAfter: 31_000, messagesBefore: 412 },
+ isSettled: false,
+ },
+};
+
+export const Settled: Story = {
+ args: {
+ phase: null,
+ stats: {
+ tokensBefore: 128_000,
+ tokensAfter: 31_000,
+ messagesBefore: 412,
+ messagesAfter: 38,
+ },
+ isSettled: true,
+ },
+};
+
+export const SettledLegacyRow: Story = {
+ args: { phase: null, stats: {}, isSettled: true },
+};
+
+export const SettledDropped: Story = {
+ args: {
+ phase: null,
+ stats: { dropped: true, messagesBefore: 412 },
+ isSettled: true,
+ },
+};
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/CompactionCard.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/CompactionCard.tsx
new file mode 100644
index 0000000000..b517169a15
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/CompactionCard.tsx
@@ -0,0 +1,95 @@
+"use client";
+
+import { Icon } from "@/components/atoms/Icon/Icon";
+import { ReloadIcon } from "@hugeicons/core-free-icons";
+import { formatElapsed } from "../JobStatsBar/formatElapsed";
+import {
+ barPercent,
+ compactionLabel,
+ type CompactionPhase,
+ type CompactionStats,
+} from "./helpers";
+import { useCompactionProgress } from "./useCompactionProgress";
+import { usePrefersReducedMotion } from "./usePrefersReducedMotion";
+
+export const SHOW_TIME_AFTER_SECONDS = 20;
+
+interface Props {
+ phase: CompactionPhase | null;
+ stats: CompactionStats;
+ isSettled: boolean;
+}
+
+export function CompactionCard({ phase, stats, isSettled }: Props) {
+ if (isSettled) {
+ return (
+ <div className="flex items-center gap-2 py-2 text-xs text-muted-foreground">
+ <Icon icon={ReloadIcon} size={14} />
+ <span className="min-w-0 truncate">
+ {compactionLabel(phase, stats)}
+ </span>
+ </div>
+ );
+ }
+
+ // A live row that hasn't received its first `data-compaction` part yet is
+ // in the opening state — animate it as `summarizing` rather than letting
+ // null read as finished copy over a frozen bar.
+ return <LiveCompaction phase={phase ?? "summarizing"} stats={stats} />;
+}
+
+interface LiveProps {
+ phase: CompactionPhase;
+ stats: CompactionStats;
+}
+
+function LiveCompaction({ phase, stats }: LiveProps) {
+ const { progress, elapsedSeconds } = useCompactionProgress(
+ phase,
+ stats.tokensBefore,
+ );
+ const prefersReducedMotion = usePrefersReducedMotion();
+ const label = compactionLabel(phase, stats);
+ const percent = Math.round(progress * 100);
+ // Reduced motion gets the progress, not the crawl: the fill jumps a step at
+ // a time instead of easing for minutes. `aria-valuenow` keeps the exact
+ // percent either way — assistive tech reads the truth, not the rendering.
+ const width = barPercent(progress, prefersReducedMotion);
+ const showTime = elapsedSeconds >= SHOW_TIME_AFTER_SECONDS;
+
+ return (
+ <div className="flex flex-col gap-1.5 py-2">
+ <div className="flex items-center gap-2 text-xs text-muted-foreground">
+ <Icon
+ icon={ReloadIcon}
+ size={14}
+ className="[animation-duration:2.4s] motion-safe:animate-spin"
+ />
+ {/* Polite live region on the label ONLY — the progressbar's
+ aria-valuenow moves every committed percent and would flood the
+ announcement queue. */}
+ <span aria-live="polite" className="min-w-0 flex-1 truncate">
+ {label}
+ </span>
+ {showTime && (
+ <span className="font-mono tabular-nums text-muted-foreground">
+ {formatElapsed(elapsedSeconds)}
+ </span>
+ )}
+ </div>
+ <div
+ role="progressbar"
+ aria-label={label}
+ aria-valuemin={0}
+ aria-valuemax={100}
+ aria-valuenow={percent}
+ className="h-1 w-full overflow-hidden rounded-full bg-muted"
+ >
+ <div
+ className="h-full rounded-full bg-foreground"
+ style={{ width: `${width}%` }}
+ />
+ </div>
+ </div>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/__tests__/CompactionCard.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/__tests__/CompactionCard.test.tsx
new file mode 100644
index 0000000000..9043e3358c
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/__tests__/CompactionCard.test.tsx
@@ -0,0 +1,157 @@
+import { render, screen } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { CompactionCard } from "../CompactionCard";
+
+function preferReducedMotion() {
+ vi.stubGlobal("matchMedia", (query: string) => ({
+ matches: query.includes("prefers-reduced-motion"),
+ media: query,
+ addEventListener: () => {},
+ removeEventListener: () => {},
+ }));
+}
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+describe("CompactionCard", () => {
+ it("narrates the summarizing phase with a live progress bar", () => {
+ render(<CompactionCard phase="summarizing" stats={{}} isSettled={false} />);
+ expect(screen.getByText("Condensing our conversation…")).toBeDefined();
+ expect(screen.getByRole("progressbar")).toBeDefined();
+ });
+
+ it("narrates the rebuilding phase", () => {
+ render(
+ <CompactionCard
+ phase="rebuilding"
+ stats={{ tokensBefore: 128_000 }}
+ isSettled={false}
+ />,
+ );
+ expect(screen.getByText("Reloading context…")).toBeDefined();
+ });
+
+ it("shows the payoff numbers once settled", () => {
+ render(
+ <CompactionCard
+ phase={null}
+ stats={{
+ tokensBefore: 128_000,
+ tokensAfter: 31_000,
+ messagesBefore: 412,
+ messagesAfter: 38,
+ }}
+ isSettled
+ />,
+ );
+ expect(
+ screen.getByText("Condensed 412 messages · 128K → 31K tokens"),
+ ).toBeDefined();
+ });
+
+ it("treats a live row with no phase yet as the opening state", () => {
+ // Between `tool-input-start` and the first `data-compaction` chunk the
+ // row is live but phaseless — it must read and animate as summarizing,
+ // never as finished copy over a frozen bar.
+ render(<CompactionCard phase={null} stats={{}} isSettled={false} />);
+ expect(screen.getByText("Condensing our conversation…")).toBeDefined();
+ expect(screen.getByRole("progressbar")).toBeDefined();
+ expect(
+ screen.queryByText("Condensed the conversation to keep going"),
+ ).toBeNull();
+ });
+
+ it("drops the bar once settled", () => {
+ render(
+ <CompactionCard
+ phase={null}
+ stats={{ tokensBefore: 128_000, tokensAfter: 31_000 }}
+ isSettled
+ />,
+ );
+ expect(
+ screen.getByText("Condensed the conversation · 128K → 31K tokens"),
+ ).toBeDefined();
+ expect(screen.queryByRole("progressbar")).toBeNull();
+ });
+
+ it("reports a reset, not a condensation, when history was dropped", () => {
+ render(
+ <CompactionCard
+ phase={null}
+ stats={{ dropped: true, messagesBefore: 412 }}
+ isSettled
+ />,
+ );
+ expect(
+ screen.getByText(
+ "Started a fresh context — earlier messages were dropped",
+ ),
+ ).toBeDefined();
+ expect(screen.queryByText(/Condensed/)).toBeNull();
+ });
+
+ it("degrades to plain copy for legacy rows without stats", () => {
+ render(<CompactionCard phase={null} stats={{}} isSettled />);
+ expect(
+ screen.getByText("Condensed the conversation to keep going"),
+ ).toBeDefined();
+ });
+
+ it("exposes the progress value to assistive tech", () => {
+ render(<CompactionCard phase="summarizing" stats={{}} isSettled={false} />);
+ const bar = screen.getByRole("progressbar");
+ expect(bar.getAttribute("aria-valuemin")).toBe("0");
+ expect(bar.getAttribute("aria-valuemax")).toBe("100");
+ expect(bar.getAttribute("aria-valuenow")).toBe("2");
+ expect(bar.getAttribute("aria-label")).toBe("Condensing our conversation…");
+ });
+
+ it("reports the token drop when compression summarized rows in place", () => {
+ // What QA actually sees on a live run: the same message count in and
+ // out, because compression rewrites rows rather than removing them. The
+ // token half is the whole payoff, and it has to render.
+ render(
+ <CompactionCard
+ phase={null}
+ stats={{
+ tokensBefore: 233_000,
+ tokensAfter: 97_000,
+ messagesBefore: 36,
+ messagesAfter: 36,
+ }}
+ isSettled
+ />,
+ );
+ expect(
+ screen.getByText("Condensed the conversation · 233K → 97K tokens"),
+ ).toBeDefined();
+ });
+
+ it("steps the bar under reduced motion but keeps aria-valuenow exact", () => {
+ preferReducedMotion();
+ render(<CompactionCard phase="summarizing" stats={{}} isSettled={false} />);
+ const bar = screen.getByRole("progressbar");
+ // The opening 2% is below the first step, so the fill is still empty
+ // while the value assistive tech reads stays truthful.
+ expect(bar.getAttribute("aria-valuenow")).toBe("2");
+ expect((bar.firstElementChild as HTMLElement).style.width).toBe("0%");
+ });
+
+ it("paints the exact percent when motion is allowed", () => {
+ render(<CompactionCard phase="summarizing" stats={{}} isSettled={false} />);
+ const bar = screen.getByRole("progressbar");
+ expect((bar.firstElementChild as HTMLElement).style.width).toBe("2%");
+ });
+
+ it("announces label changes politely without turning the bar into a live region", () => {
+ render(<CompactionCard phase="summarizing" stats={{}} isSettled={false} />);
+ const label = screen.getByText("Condensing our conversation…");
+ expect(label.getAttribute("aria-live")).toBe("polite");
+ expect(
+ screen.getByRole("progressbar").getAttribute("aria-live"),
+ ).toBeNull();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/__tests__/helpers.test.ts
new file mode 100644
index 0000000000..878f44cf9b
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/__tests__/helpers.test.ts
@@ -0,0 +1,207 @@
+import { describe, expect, it } from "vitest";
+import {
+ barPercent,
+ compactionLabel,
+ formatTokens,
+ parseCompactionOutput,
+ phaseProgress,
+ REDUCED_MOTION_STEP_PERCENT,
+ tauForTokens,
+} from "../helpers";
+
+describe("phaseProgress", () => {
+ it("starts at the base value", () => {
+ expect(phaseProgress(0.02, 0.55, 0, 15_000)).toBeCloseTo(0.02, 5);
+ });
+
+ it("never reaches the cap", () => {
+ expect(phaseProgress(0.02, 0.55, 10 * 60 * 1000, 15_000)).toBeLessThan(
+ 0.55,
+ );
+ });
+
+ it("is monotonically increasing", () => {
+ let prev = -1;
+ for (let t = 0; t <= 120_000; t += 1_000) {
+ const p = phaseProgress(0.02, 0.55, t, 15_000);
+ expect(p).toBeGreaterThan(prev);
+ prev = p;
+ }
+ });
+
+ it("reaches ~63% of the span at one time constant", () => {
+ expect(phaseProgress(0, 1, 15_000, 15_000)).toBeCloseTo(0.632, 2);
+ });
+});
+
+describe("tauForTokens", () => {
+ it("floors at 12s for unknown or small contexts", () => {
+ expect(tauForTokens(undefined)).toBe(12_000);
+ expect(tauForTokens(1_000)).toBe(12_000);
+ });
+
+ it("grows with context size", () => {
+ expect(tauForTokens(200_000)).toBeGreaterThan(tauForTokens(50_000));
+ });
+
+ it("caps at 45s", () => {
+ expect(tauForTokens(5_000_000)).toBe(45_000);
+ });
+});
+
+describe("parseCompactionOutput", () => {
+ it("reads the JSON payload", () => {
+ const stats = parseCompactionOutput(
+ JSON.stringify({
+ summary:
+ "Earlier messages were summarized to fit within context limits.",
+ tokensBefore: 128_000,
+ tokensAfter: 31_000,
+ messagesBefore: 412,
+ messagesAfter: 38,
+ }),
+ );
+ expect(stats.tokensBefore).toBe(128_000);
+ expect(stats.messagesAfter).toBe(38);
+ });
+
+ it("accepts an already-parsed object", () => {
+ const stats = parseCompactionOutput({ summary: "x", tokensBefore: 5 });
+ expect(stats.tokensBefore).toBe(5);
+ });
+
+ it("reads the dropped flag", () => {
+ const stats = parseCompactionOutput(
+ JSON.stringify({ summary: "dropped", dropped: true, messagesBefore: 9 }),
+ );
+ expect(stats).toEqual({ dropped: true, messagesBefore: 9 });
+ });
+
+ it("yields no stats for the legacy plain sentence", () => {
+ const legacy =
+ "Earlier messages were summarized to fit within context limits.";
+ expect(parseCompactionOutput(legacy)).toEqual({});
+ });
+
+ it("survives junk", () => {
+ expect(parseCompactionOutput(undefined)).toEqual({});
+ expect(parseCompactionOutput(42)).toEqual({});
+ });
+
+ it("drops implausible counts — zeros and fractions never reach the copy", () => {
+ const stats = parseCompactionOutput({
+ summary: "x",
+ tokensBefore: 0,
+ tokensAfter: 0,
+ messagesBefore: 412.5,
+ messagesAfter: 38,
+ });
+ expect(stats).toEqual({ messagesAfter: 38 });
+ });
+});
+
+describe("formatTokens", () => {
+ it("abbreviates thousands", () => {
+ expect(formatTokens(31_000)).toBe("31K");
+ expect(formatTokens(128_000)).toBe("128K");
+ });
+
+ it("keeps small numbers exact", () => {
+ expect(formatTokens(840)).toBe("840");
+ });
+
+ it("rounds to one decimal below 10K", () => {
+ expect(formatTokens(4_200)).toBe("4.2K");
+ expect(formatTokens(4_000)).toBe("4K");
+ });
+
+ it("is continuous at the 10K boundary", () => {
+ expect(formatTokens(9_999)).toBe("10K");
+ expect(formatTokens(10_000)).toBe("10K");
+ });
+
+ it("switches to millions instead of rendering 1000K", () => {
+ expect(formatTokens(999_499)).toBe("999K");
+ expect(formatTokens(999_500)).toBe("1M");
+ expect(formatTokens(1_000_000)).toBe("1M");
+ expect(formatTokens(1_500_000)).toBe("1.5M");
+ expect(formatTokens(128_000_000)).toBe("128M");
+ });
+});
+
+describe("barPercent", () => {
+ it("paints the exact percent when motion is allowed", () => {
+ expect(barPercent(0.02, false)).toBe(2);
+ expect(barPercent(0.473, false)).toBe(47);
+ });
+
+ it("steps the fill under reduced motion instead of creeping", () => {
+ expect(barPercent(0.473, true)).toBe(40);
+ expect(barPercent(0.5, true)).toBe(50);
+ expect(barPercent(0.59, true)).toBe(50);
+ });
+
+ it("never claims progress the curve has not made", () => {
+ for (let p = 0; p <= 1.0001; p += 0.01) {
+ const stepped = barPercent(p, true);
+ expect(stepped).toBeLessThanOrEqual(barPercent(p, false));
+ expect(stepped % REDUCED_MOTION_STEP_PERCENT).toBe(0);
+ }
+ });
+
+ it("still reaches a full bar", () => {
+ expect(barPercent(1, true)).toBe(100);
+ });
+});
+
+describe("compactionLabel", () => {
+ it("narrates each live phase", () => {
+ expect(compactionLabel("summarizing", {})).toBe(
+ "Condensing our conversation…",
+ );
+ expect(compactionLabel("rebuilding", {})).toBe("Reloading context…");
+ });
+
+ it("celebrates settled rows with real numbers when it has them", () => {
+ expect(
+ compactionLabel(null, {
+ tokensBefore: 128_000,
+ tokensAfter: 31_000,
+ messagesBefore: 412,
+ messagesAfter: 38,
+ }),
+ ).toBe("Condensed 412 messages · 128K → 31K tokens");
+ });
+
+ it("settles with tokens only when no messages were removed", () => {
+ expect(
+ compactionLabel(null, { tokensBefore: 128_000, tokensAfter: 31_000 }),
+ ).toBe("Condensed the conversation · 128K → 31K tokens");
+ });
+
+ it("skips the message count when content was summarized in place", () => {
+ // Observed in production: 60 messages in, 60 messages out — the rows
+ // survived, only their content shrank. "Condensed 60 messages" would
+ // read as removal that never happened.
+ expect(
+ compactionLabel(null, {
+ tokensBefore: 128_000,
+ tokensAfter: 31_000,
+ messagesBefore: 60,
+ messagesAfter: 60,
+ }),
+ ).toBe("Condensed the conversation · 128K → 31K tokens");
+ });
+
+ it("refuses to advertise an inverted token measurement", () => {
+ expect(
+ compactionLabel(null, { tokensBefore: 31_000, tokensAfter: 128_000 }),
+ ).toBe("Condensed the conversation to keep going");
+ });
+
+ it("degrades gracefully for legacy rows with no stats", () => {
+ expect(compactionLabel(null, {})).toBe(
+ "Condensed the conversation to keep going",
+ );
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/__tests__/useCompactionProgress.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/__tests__/useCompactionProgress.test.tsx
new file mode 100644
index 0000000000..3997e6fbc3
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/__tests__/useCompactionProgress.test.tsx
@@ -0,0 +1,194 @@
+import { act, render, renderHook, screen } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { formatElapsed } from "../../JobStatsBar/formatElapsed";
+import { CompactionCard, SHOW_TIME_AFTER_SECONDS } from "../CompactionCard";
+import { PARKED_POLL_MS, PHASE_CURVE, type CompactionPhase } from "../helpers";
+import { useCompactionProgress } from "../useCompactionProgress";
+
+let frameCallbacks: Map<number, FrameRequestCallback>;
+let nextFrameId: number;
+let cancelledFrames: number[];
+let now: number;
+
+beforeEach(() => {
+ frameCallbacks = new Map();
+ nextFrameId = 1;
+ cancelledFrames = [];
+ now = 0;
+ vi.stubGlobal("requestAnimationFrame", (cb: FrameRequestCallback) => {
+ const id = nextFrameId++;
+ frameCallbacks.set(id, cb);
+ return id;
+ });
+ vi.stubGlobal("cancelAnimationFrame", (id: number) => {
+ cancelledFrames.push(id);
+ frameCallbacks.delete(id);
+ });
+ vi.spyOn(performance, "now").mockImplementation(() => now);
+ // Only the timer functions — faking requestAnimationFrame here would
+ // clobber the stub above and swallow every frame.
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+});
+
+// A parked curve leaves no frame pending — it waits on the slow timer. This
+// releases that timer so the frame loop picks back up.
+function runParkedPoll() {
+ act(() => {
+ vi.advanceTimersByTime(PARKED_POLL_MS);
+ });
+}
+
+function runFrame(advanceMs: number) {
+ now += advanceMs;
+ const pending = Array.from(frameCallbacks.values());
+ frameCallbacks.clear();
+ act(() => {
+ for (const cb of pending) cb(now);
+ });
+}
+
+function renderProgress(phase: CompactionPhase = "summarizing") {
+ return renderHook(
+ (props: { phase: CompactionPhase }) =>
+ useCompactionProgress(props.phase, 128_000),
+ { initialProps: { phase } },
+ );
+}
+
+describe("useCompactionProgress", () => {
+ it("never decreases", () => {
+ const { result } = renderProgress();
+ let prev = result.current.progress;
+ for (let i = 0; i < 50; i++) {
+ runFrame(500);
+ expect(result.current.progress).toBeGreaterThanOrEqual(prev);
+ prev = result.current.progress;
+ }
+ });
+
+ it("carries progress over into a new phase instead of resetting", () => {
+ const { result, rerender } = renderProgress();
+ for (let i = 0; i < 20; i++) runFrame(1_000);
+ const beforeSwitch = result.current.progress;
+ expect(beforeSwitch).toBeGreaterThan(0.1);
+
+ rerender({ phase: "rebuilding" });
+ runFrame(16);
+ expect(result.current.progress).toBeGreaterThanOrEqual(beforeSwitch);
+ runFrame(5_000);
+ expect(result.current.progress).toBeGreaterThan(beforeSwitch);
+ });
+
+ it("only commits state when the visible percent changes", () => {
+ let renders = 0;
+ renderHook(() => {
+ renders++;
+ return useCompactionProgress("summarizing", 128_000);
+ });
+ const before = renders;
+ // 30 one-millisecond frames move the curve far less than one percent
+ // and stay inside the same second — no commit, no re-render.
+ for (let i = 0; i < 30; i++) runFrame(1);
+ expect(renders).toBe(before);
+ });
+
+ it("parks the loop once pinned at the phase ceiling and never crosses it", () => {
+ const { result } = renderProgress();
+ for (let i = 0; i < 60 && frameCallbacks.size > 0; i++) {
+ runFrame(60_000);
+ }
+ expect(frameCallbacks.size).toBe(0);
+ expect(result.current.progress).toBeLessThanOrEqual(
+ PHASE_CURVE.summarizing.cap,
+ );
+ // Parked means parked: time passing schedules nothing new.
+ runFrame(60_000);
+ expect(frameCallbacks.size).toBe(0);
+ });
+
+ it("wakes a parked loop when the phase raises the ceiling", () => {
+ const { result, rerender } = renderProgress();
+ for (let i = 0; i < 60 && frameCallbacks.size > 0; i++) {
+ runFrame(60_000);
+ }
+ expect(frameCallbacks.size).toBe(0);
+ const parkedAt = result.current.progress;
+
+ rerender({ phase: "rebuilding" });
+ runParkedPoll();
+ expect(frameCallbacks.size).toBe(1);
+ runFrame(16);
+ runFrame(10_000);
+ expect(result.current.progress).toBeGreaterThan(parkedAt);
+ expect(result.current.progress).toBeLessThanOrEqual(
+ PHASE_CURVE.rebuilding.cap,
+ );
+ });
+
+ it("keeps counting while the curve is parked at its ceiling", () => {
+ // The park deliberately idles on a slow timer rather than dropping the
+ // loop entirely — a rebuild that stalls for two minutes must still show
+ // two minutes, not the age it had when the bar stopped moving.
+ const { result } = renderProgress();
+ for (let i = 0; i < 60 && frameCallbacks.size > 0; i++) runFrame(60_000);
+ expect(frameCallbacks.size).toBe(0);
+ const parkedProgress = result.current.progress;
+ const parkedSeconds = result.current.elapsedSeconds;
+
+ runParkedPoll();
+ runFrame(30_000);
+
+ expect(result.current.elapsedSeconds).toBe(parkedSeconds + 30);
+ expect(result.current.progress).toBe(parkedProgress);
+ });
+
+ it("cancels the pending frame on unmount", () => {
+ const { unmount } = renderProgress();
+ runFrame(500);
+ expect(frameCallbacks.size).toBe(1);
+ unmount();
+ expect(frameCallbacks.size).toBe(0);
+ expect(cancelledFrames.length).toBeGreaterThan(0);
+ });
+});
+
+// The card's elapsed readout rides the same frame clock, so it is exercised
+// through the same harness rather than a second set of stubs.
+describe("CompactionCard elapsed readout", () => {
+ function renderCard() {
+ return render(
+ <CompactionCard
+ phase="summarizing"
+ stats={{ tokensBefore: 128_000 }}
+ isSettled={false}
+ />,
+ );
+ }
+
+ it("stays quiet until the wait is worth mentioning", () => {
+ renderCard();
+ runFrame((SHOW_TIME_AFTER_SECONDS - 1) * 1_000);
+ expect(
+ screen.queryByText(formatElapsed(SHOW_TIME_AFTER_SECONDS - 1)),
+ ).toBeNull();
+ });
+
+ it("shows the timer once past the threshold and keeps it ticking", () => {
+ renderCard();
+ runFrame(SHOW_TIME_AFTER_SECONDS * 1_000);
+ expect(
+ screen.getByText(formatElapsed(SHOW_TIME_AFTER_SECONDS)),
+ ).toBeDefined();
+
+ runFrame(45_000);
+ expect(
+ screen.getByText(formatElapsed(SHOW_TIME_AFTER_SECONDS + 45)),
+ ).toBeDefined();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/helpers.ts
new file mode 100644
index 0000000000..a0a0a8389f
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/helpers.ts
@@ -0,0 +1,169 @@
+// The wire contract: the backend only ever emits these two phases. There is
+// no "done" — a finished compaction settles the row instead.
+export type CompactionPhase = "summarizing" | "rebuilding";
+
+/**
+ * AI SDK v5 wire type of the transient progress part the backend streams
+ * while a compaction runs (`ResponseType.COMPACTION`). Every filter that has
+ * to see past it references this constant — spelling the string by hand in
+ * one more place is how a bookkeeping part starts rendering as content.
+ */
+export const COMPACTION_DATA_PART_TYPE = "data-compaction";
+
+export interface CompactionStats {
+ tokensBefore?: number;
+ tokensAfter?: number;
+ messagesBefore?: number;
+ messagesAfter?: number;
+ // The backend could not condense the history and dropped it instead. A
+ // settled row carrying this reports the reset — never "Condensed…".
+ dropped?: true;
+}
+
+export const DROPPED_LABEL =
+ "Started a fresh context — earlier messages were dropped";
+
+// Each phase approaches its own ceiling exponentially, so the bar can never
+// land before the work does. Entering a new phase raises the ceiling, which
+// reads as real progress rather than a stalled bar.
+export const PHASE_CURVE: Record<
+ CompactionPhase,
+ { cap: number; tauMs: number }
+> = {
+ summarizing: { cap: 0.55, tauMs: 15_000 },
+ rebuilding: { cap: 0.92, tauMs: 20_000 },
+};
+
+export const INITIAL_PROGRESS = 0.02;
+// Within half a percent of the ceiling the rounded bar can no longer move,
+// so the rAF loop parks itself instead of spinning at 60fps forever.
+export const SETTLE_EPSILON = 0.005;
+
+// How often a curve parked at its ceiling re-checks for a new phase. Slow
+// enough that a stalled rebuild costs nothing, fast enough that the handover
+// to the next phase reads as continuous.
+export const PARKED_POLL_MS = 1_000;
+
+// Under `prefers-reduced-motion` the bar advances in whole steps instead of
+// creeping a pixel at a time. The progress is the feature — a compaction can
+// run for minutes and the user deserves to know it is moving — but the crawl
+// is the motion, so we keep the information and drop the animation.
+export const REDUCED_MOTION_STEP_PERCENT = 10;
+
+/**
+ * Width the bar actually paints. Always floors to the step under reduced
+ * motion: rounding up would claim progress the curve has not made.
+ */
+export function barPercent(progress: number, prefersReducedMotion: boolean) {
+ const percent = Math.round(progress * 100);
+ if (!prefersReducedMotion) return percent;
+ return (
+ Math.floor(percent / REDUCED_MOTION_STEP_PERCENT) *
+ REDUCED_MOTION_STEP_PERCENT
+ );
+}
+
+const TAU_FLOOR_MS = 12_000;
+const TAU_CEILING_MS = 45_000;
+const TAU_MS_PER_TOKEN = 1_000 / 10_000;
+
+export function tauForTokens(tokensBefore: number | undefined): number {
+ const scaled = (tokensBefore ?? 0) * TAU_MS_PER_TOKEN;
+ return Math.min(TAU_CEILING_MS, Math.max(TAU_FLOOR_MS, scaled));
+}
+
+export function phaseProgress(
+ base: number,
+ cap: number,
+ elapsedMs: number,
+ tauMs: number,
+): number {
+ // Floor the decay term to keep the asymptote strictly below cap in float64.
+ const decay = Math.max(Math.exp(-elapsedMs / tauMs), 1e-9);
+ return base + (cap - base) * (1 - decay);
+}
+
+export function readCompactionStats(
+ source: Record<string, unknown>,
+): CompactionStats {
+ const stats: CompactionStats = {};
+ const keys = [
+ "tokensBefore",
+ "tokensAfter",
+ "messagesBefore",
+ "messagesAfter",
+ ] as const;
+ for (const key of keys) {
+ const value = source[key];
+ // Counts are positive integers by construction — anything else (0, a
+ // fraction, NaN) is a measurement bug and must not reach the copy.
+ if (typeof value === "number" && Number.isInteger(value) && value > 0) {
+ stats[key] = value;
+ }
+ }
+ if (source.dropped === true) stats.dropped = true;
+ return stats;
+}
+
+/**
+ * Stats from a settled row's output. The payload's `summary` prose is
+ * transcript-level detail the card deliberately does not surface — the
+ * settled copy is `compactionLabel`'s verified claim, so legacy
+ * plain-sentence outputs simply yield no stats.
+ */
+export function parseCompactionOutput(output: unknown): CompactionStats {
+ let value: unknown = output;
+ if (typeof value === "string") {
+ try {
+ value = JSON.parse(value);
+ } catch {
+ return {};
+ }
+ }
+ if (typeof value !== "object" || value === null) return {};
+ return readCompactionStats(value as Record<string, unknown>);
+}
+
+function formatWithUnit(value: number, unit: string): string {
+ const rounded = value < 10 ? Math.round(value * 10) / 10 : Math.round(value);
+ const text = Number.isInteger(rounded) ? String(rounded) : rounded.toFixed(1);
+ return `${text}${unit}`;
+}
+
+export function formatTokens(n: number): string {
+ if (n < 1_000) return String(n);
+ // 999_500 rounds up to 1M — switching units there keeps the scale
+ // continuous ("999K" → "1M", never "1000K").
+ if (n < 999_500) return formatWithUnit(n / 1_000, "K");
+ return formatWithUnit(n / 1_000_000, "M");
+}
+
+export function compactionLabel(
+ phase: CompactionPhase | null,
+ stats: CompactionStats,
+): string {
+ if (phase === "summarizing") return "Condensing our conversation…";
+ if (phase === "rebuilding") return "Reloading context…";
+ if (stats.dropped) return DROPPED_LABEL;
+
+ // A number only earns its place when both ends of the measurement exist
+ // and the "after" actually shrank — an equal or inverted pair (e.g. 60
+ // messages summarized in place, none removed) reads as a broken claim.
+ const condensedMessages =
+ stats.messagesBefore !== undefined &&
+ stats.messagesAfter !== undefined &&
+ stats.messagesAfter < stats.messagesBefore;
+ const headline = condensedMessages
+ ? `Condensed ${stats.messagesBefore} messages`
+ : "Condensed the conversation";
+ const tokens =
+ stats.tokensBefore !== undefined &&
+ stats.tokensAfter !== undefined &&
+ stats.tokensAfter < stats.tokensBefore
+ ? `${formatTokens(stats.tokensBefore)} → ${formatTokens(stats.tokensAfter)} tokens`
+ : null;
+ if (tokens) return `${headline} · ${tokens}`;
+ return condensedMessages
+ ? headline
+ : "Condensed the conversation to keep going";
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/useCompactionProgress.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/useCompactionProgress.ts
new file mode 100644
index 0000000000..510f6ca893
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/useCompactionProgress.ts
@@ -0,0 +1,96 @@
+import { useMountEffect } from "@/hooks/useMountEffect";
+import { useRef, useState } from "react";
+import {
+ INITIAL_PROGRESS,
+ PARKED_POLL_MS,
+ PHASE_CURVE,
+ SETTLE_EPSILON,
+ phaseProgress,
+ tauForTokens,
+ type CompactionPhase,
+} from "./helpers";
+
+export function useCompactionProgress(
+ phase: CompactionPhase,
+ tokensBefore: number | undefined,
+) {
+ const [progress, setProgress] = useState(INITIAL_PROGRESS);
+ const [elapsedSeconds, setElapsedSeconds] = useState(0);
+
+ // Read during render so the rAF loop always sees the current phase without
+ // a dependency-keyed effect restarting the animation mid-flight.
+ const phaseRef = useRef(phase);
+ const tokensRef = useRef(tokensBefore);
+ const baseRef = useRef(INITIAL_PROGRESS);
+ const phaseStartRef = useRef<number | null>(null);
+ const lastPhaseRef = useRef<CompactionPhase>(phase);
+ const progressRef = useRef(INITIAL_PROGRESS);
+ const percentRef = useRef(Math.round(INITIAL_PROGRESS * 100));
+ const secondsRef = useRef(0);
+ phaseRef.current = phase;
+ tokensRef.current = tokensBefore;
+
+ useMountEffect(() => {
+ let frame = 0;
+ let parkedTimer: ReturnType<typeof setTimeout> | undefined;
+ const mountedAt = performance.now();
+
+ function tick(now: number) {
+ const current = phaseRef.current;
+
+ if (current !== lastPhaseRef.current) {
+ lastPhaseRef.current = current;
+ baseRef.current = progressRef.current;
+ phaseStartRef.current = now;
+ }
+ if (phaseStartRef.current === null) phaseStartRef.current = now;
+
+ const sincePhase = now - phaseStartRef.current;
+ const curve = PHASE_CURVE[current];
+ const tau =
+ current === "summarizing"
+ ? tauForTokens(tokensRef.current)
+ : curve.tauMs;
+ let next = phaseProgress(baseRef.current, curve.cap, sincePhase, tau);
+
+ // The bar is a promise to the user: it never goes backwards.
+ next = Math.max(progressRef.current, next);
+ progressRef.current = next;
+
+ // Quantize commits: only a change the DOM can show (a whole percent, a
+ // whole second) triggers a render, not 60 identical frames a second.
+ const percent = Math.round(next * 100);
+ if (percent !== percentRef.current) {
+ percentRef.current = percent;
+ setProgress(next);
+ }
+ const seconds = Math.floor((now - mountedAt) / 1000);
+ if (seconds !== secondsRef.current) {
+ secondsRef.current = seconds;
+ setElapsedSeconds(seconds);
+ }
+
+ // Once the curve is visually pinned at its ceiling there is nothing
+ // left to animate, so drop off the frame clock and idle on a slow
+ // timer instead — a stalled rebuild must not hold the tab at 60Hz for
+ // minutes. The timer keeps the elapsed seconds honest and picks the
+ // frame loop back up when a later phase raises the ceiling, so the bar
+ // still never finishes before the work does.
+ if (curve.cap - next <= SETTLE_EPSILON) {
+ parkedTimer = setTimeout(() => {
+ frame = requestAnimationFrame(tick);
+ }, PARKED_POLL_MS);
+ return;
+ }
+ frame = requestAnimationFrame(tick);
+ }
+
+ frame = requestAnimationFrame(tick);
+ return () => {
+ cancelAnimationFrame(frame);
+ if (parkedTimer !== undefined) clearTimeout(parkedTimer);
+ };
+ });
+
+ return { progress, elapsedSeconds };
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/usePrefersReducedMotion.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/usePrefersReducedMotion.ts
new file mode 100644
index 0000000000..1048576046
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CompactionCard/usePrefersReducedMotion.ts
@@ -0,0 +1,31 @@
+import { useSyncExternalStore } from "react";
+
+const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)";
+
+function mediaQuery(): MediaQueryList | null {
+ if (typeof window === "undefined" || !window.matchMedia) return null;
+ return window.matchMedia(REDUCED_MOTION_QUERY);
+}
+
+function subscribe(onStoreChange: () => void) {
+ const query = mediaQuery();
+ if (!query) return () => {};
+ query.addEventListener("change", onStoreChange);
+ return () => query.removeEventListener("change", onStoreChange);
+}
+
+function getSnapshot(): boolean {
+ return mediaQuery()?.matches ?? false;
+}
+
+function getServerSnapshot(): boolean {
+ return false;
+}
+
+/**
+ * Live `prefers-reduced-motion` state. Subscribed rather than read once, so
+ * flipping the OS setting takes effect without a reload.
+ */
+export function usePrefersReducedMotion() {
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/helpers.ts
index 2edc28c5d3..ab83a46b83 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/helpers.ts
@@ -138,7 +138,12 @@ function getProviderIconSrc(tool: ToolUIPart): string | undefined {
return undefined;
}
+// The compaction row owns its own progress bar and payoff frame — folding
+// it into a chain would bury both behind a collapsed "summarized context".
+export const COMPACTION_PART_TYPE = "tool-context_compaction";
+
export function isChainPart(part: MessagePart): boolean {
+ if (part.type === COMPACTION_PART_TYPE) return false;
return part.type === "reasoning" || part.type.startsWith("tool-");
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts
index 0ff6837532..ef9af7ff58 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts
@@ -123,29 +123,6 @@ export function hasVisibleAssistantContent(messages: UIMessage[]): boolean {
});
}
-/**
- * Surface the latest backend-emitted status message for the trailing assistant
- * message, if that status has not already been invalidated by newer visible
- * parts. Used to show progress during restore/replay before answer text lands.
- */
-export function getLatestAssistantStatusMessage(
- messages: UIMessage[],
-): string | null {
- const last = messages[messages.length - 1];
- if (last?.role !== "assistant") return null;
- for (let i = last.parts.length - 1; i >= 0; i--) {
- const part = last.parts[i];
- if (part.type === "data-cursor") continue;
- if (part.type === "data-dream-operations") continue;
- if (part.type === "data-status") {
- const data = (part as { data?: { message?: unknown } }).data;
- return typeof data?.message === "string" ? data.message : null;
- }
- return null;
- }
- return null;
-}
-
/** Mark any in-progress tool parts as completed/errored so spinners stop. */
export function resolveInProgressTools(
messages: UIMessage[],
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/messageParts.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/messageParts.ts
new file mode 100644
index 0000000000..4237740500
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/messageParts.ts
@@ -0,0 +1,56 @@
+import type { UIMessage } from "ai";
+
+import { COMPACTION_DATA_PART_TYPE } from "./components/CompactionCard/helpers";
+import { DREAM_OPERATIONS_PART_TYPE } from "./dreamOperations";
+
+/** Transient copy for the Thinking indicator — never rendered inline. */
+export const STATUS_PART_TYPE = "data-status";
+/** Legacy resume metadata. */
+const CURSOR_PART_TYPE = "data-cursor";
+
+/**
+ * Stream bookkeeping: parts the backend sends to drive chrome (indicators,
+ * resume cursors, progress rows) rather than message content.
+ *
+ * This set is the single list. Every new transient `data-*` part must be
+ * added here and nowhere else — the filters used to enumerate them one call
+ * site at a time, and missing one is a silent UI bug (a stray part counts as
+ * "real content", hides the Thinking indicator, splits a tool chain, or
+ * kills a live progress bar) rather than a type error.
+ */
+const BOOKKEEPING_PART_TYPES: ReadonlySet<string> = new Set([
+ CURSOR_PART_TYPE,
+ STATUS_PART_TYPE,
+ DREAM_OPERATIONS_PART_TYPE,
+ COMPACTION_DATA_PART_TYPE,
+]);
+
+export function isBookkeepingPart(part: { type: string }): boolean {
+ return BOOKKEEPING_PART_TYPES.has(part.type);
+}
+
+/**
+ * Surface the latest backend-emitted status message for the trailing assistant
+ * message, if that status has not already been invalidated by newer visible
+ * parts. Used to show progress during restore/replay before answer text lands.
+ */
+export function getLatestAssistantStatusMessage(
+ messages: UIMessage[],
+): string | null {
+ const last = messages[messages.length - 1];
+ if (last?.role !== "assistant") return null;
+ for (let i = last.parts.length - 1; i >= 0; i--) {
+ const part = last.parts[i];
+ if (part.type === STATUS_PART_TYPE) {
+ const data = (part as { data?: { message?: unknown } }).data;
+ return typeof data?.message === "string" ? data.message : null;
+ }
+ // Other bookkeeping lands after the status all the time (a compaction
+ // starts, a cursor checkpoints) without the model having said anything
+ // new — reading through it keeps a still-relevant status on screen.
+ if (isBookkeepingPart(part)) continue;
+ // Anything else = the model has produced output past the status.
+ return null;
+ }
+ return null;
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
index 96bf117528..83e2ad1665 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
@@ -6,7 +6,7 @@ import type { UIMessage } from "ai";
import { parseAsString, useQueryState } from "nuqs";
import { useEffect, useMemo, useRef } from "react";
import { concatWithAssistantMerge } from "./helpers/convertChatSessionToUiMessages";
-import { getLatestAssistantStatusMessage } from "./helpers";
+import { getLatestAssistantStatusMessage } from "./messageParts";
import type { WorkspaceAttachment } from "./helpers/workspaceAttachments";
import { queueFollowUpMessage } from "./helpers/queueFollowUpMessage";
import { stripReplayPrefix } from "./helpers/stripReplayPrefix";
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
index ebe1880922..52e569f8b4 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
@@ -27,7 +27,6 @@ import {
import {
deduplicateMessages,
extractSendMessageText,
- getLatestAssistantStatusMessage,
getSendSuppressionReason,
hasActiveBackendStream,
hasInProgressAssistantParts,
@@ -35,6 +34,7 @@ import {
resolveModeChangedMode,
} from "./helpers";
import { extractDbSequence } from "./helpers/convertChatSessionToUiMessages";
+import { getLatestAssistantStatusMessage } from "./messageParts";
import { useCopilotUIStore } from "./store";
import type { CopilotLlmModel, CopilotMode } from "./store";
import { useCopilotReconnect } from "./useCopilotReconnect";