Skip to content

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

feat(copilot): render context compaction as tool-call UI events (#12250)

Ottoa897f9e12420
diff --git a/autogpt_platform/backend/backend/copilot/constants.py b/autogpt_platform/backend/backend/copilot/constants.py
new file mode 100644
index 0000000000..d1d2003116
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/constants.py
@@ -0,0 +1,11 @@
+"""Shared constants for the CoPilot module."""
+
+# Special message prefixes for text-based markers (parsed by frontend).
+# The hex suffix makes accidental LLM generation of these strings virtually
+# impossible, avoiding false-positive marker detection in normal conversation.
+COPILOT_ERROR_PREFIX = "[__COPILOT_ERROR_f7a1__]" # Renders as ErrorCard
+COPILOT_SYSTEM_PREFIX = "[__COPILOT_SYSTEM_e3b0__]" # Renders as system info message
+
+# Compaction notice messages shown to users.
+COMPACTION_DONE_MSG = "Earlier messages were summarized to fit within context limits."
+COMPACTION_TOOL_NAME = "context_compaction"
diff --git a/autogpt_platform/backend/backend/copilot/sdk/compaction.py b/autogpt_platform/backend/backend/copilot/sdk/compaction.py
new file mode 100644
index 0000000000..bd12bdc54f
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/compaction.py
@@ -0,0 +1,239 @@
+"""Compaction tracking for SDK-based chat sessions.
+
+Encapsulates the state machine and event emission for context compaction,
+both pre-query (history compressed before SDK query) and SDK-internal
+(PreCompact hook fires mid-stream).
+
+All compaction-related helpers live here: event builders, message filtering,
+persistence, and the ``CompactionTracker`` state machine.
+"""
+
+import asyncio
+import logging
+import uuid
+from collections.abc import Callable
+
+from ..constants import COMPACTION_DONE_MSG, COMPACTION_TOOL_NAME
+from ..model import ChatMessage, ChatSession
+from ..response_model import (
+ StreamBaseResponse,
+ StreamFinishStep,
+ StreamStartStep,
+ StreamToolInputAvailable,
+ StreamToolInputStart,
+ StreamToolOutputAvailable,
+)
+
+logger = logging.getLogger(__name__)
+
+
+# ---------------------------------------------------------------------------
+# Event builders (private — use CompactionTracker or compaction_events)
+# ---------------------------------------------------------------------------
+
+
+def _start_events(tool_call_id: str) -> list[StreamBaseResponse]:
+ """Build the opening events for a compaction tool call."""
+ return [
+ StreamStartStep(),
+ StreamToolInputStart(toolCallId=tool_call_id, toolName=COMPACTION_TOOL_NAME),
+ StreamToolInputAvailable(
+ toolCallId=tool_call_id, toolName=COMPACTION_TOOL_NAME, input={}
+ ),
+ ]
+
+
+def _end_events(tool_call_id: str, message: str) -> list[StreamBaseResponse]:
+ """Build the closing events for a compaction tool call."""
+ return [
+ StreamToolOutputAvailable(
+ toolCallId=tool_call_id,
+ toolName=COMPACTION_TOOL_NAME,
+ output=message,
+ ),
+ StreamFinishStep(),
+ ]
+
+
+def _new_tool_call_id() -> str:
+ return f"compaction-{uuid.uuid4().hex[:12]}"
+
+
+# ---------------------------------------------------------------------------
+# Public event builder
+# ---------------------------------------------------------------------------
+
+
+def emit_compaction(session: ChatSession) -> list[StreamBaseResponse]:
+ """Create, persist, and return a self-contained compaction tool call.
+
+ Convenience for callers that don't use ``CompactionTracker`` (e.g. the
+ 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)
+ return evts
+
+
+def compaction_events(
+ message: str, tool_call_id: str | None = None
+) -> list[StreamBaseResponse]:
+ """Emit a self-contained compaction tool call (already completed).
+
+ When *tool_call_id* is provided it is reused (e.g. for persistence that
+ must match an already-streamed start event). Otherwise a new ID is
+ generated.
+ """
+ tc_id = tool_call_id or _new_tool_call_id()
+ return _start_events(tc_id) + _end_events(tc_id, message)
+
+
+# ---------------------------------------------------------------------------
+# Message filtering
+# ---------------------------------------------------------------------------
+
+
+def filter_compaction_messages(
+ messages: list[ChatMessage],
+) -> list[ChatMessage]:
+ """Remove synthetic compaction tool-call messages (UI-only artifacts).
+
+ Strips assistant messages whose only tool calls are compaction calls,
+ and their corresponding tool-result messages.
+ """
+ compaction_ids: set[str] = set()
+ filtered: list[ChatMessage] = []
+ for msg in messages:
+ if msg.role == "assistant" and msg.tool_calls:
+ for tc in msg.tool_calls:
+ if tc.get("function", {}).get("name") == COMPACTION_TOOL_NAME:
+ compaction_ids.add(tc.get("id", ""))
+ real_calls = [
+ tc
+ for tc in msg.tool_calls
+ if tc.get("function", {}).get("name") != COMPACTION_TOOL_NAME
+ ]
+ if not real_calls and not msg.content:
+ continue
+ if msg.role == "tool" and msg.tool_call_id in compaction_ids:
+ continue
+ filtered.append(msg)
+ return filtered
+
+
+# ---------------------------------------------------------------------------
+# Persistence
+# ---------------------------------------------------------------------------
+
+
+def _persist(session: ChatSession, tool_call_id: str, message: str) -> None:
+ """Append compaction tool-call + result to session messages.
+
+ Compaction events are synthetic so they bypass the normal adapter
+ accumulation. This explicitly records them so they survive a page refresh.
+ """
+ session.messages.append(
+ ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[
+ {
+ "id": tool_call_id,
+ "type": "function",
+ "function": {
+ "name": COMPACTION_TOOL_NAME,
+ "arguments": "{}",
+ },
+ }
+ ],
+ )
+ )
+ session.messages.append(
+ ChatMessage(role="tool", content=message, tool_call_id=tool_call_id)
+ )
+
+
+# ---------------------------------------------------------------------------
+# CompactionTracker — state machine for streaming sessions
+# ---------------------------------------------------------------------------
+
+
+class CompactionTracker:
+ """Tracks compaction state and yields UI events.
+
+ 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.
+
+ 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.
+ """
+
+ def __init__(self) -> None:
+ self._compact_start = asyncio.Event()
+ self._start_emitted = False
+ self._done = False
+ self._tool_call_id = ""
+
+ @property
+ def on_compact(self) -> Callable[[], None]:
+ """Callback for the PreCompact hook."""
+ return self._compact_start.set
+
+ # ------------------------------------------------------------------
+ # Pre-query compaction
+ # ------------------------------------------------------------------
+
+ def emit_pre_query(self, session: ChatSession) -> list[StreamBaseResponse]:
+ """Emit + persist a self-contained compaction tool call."""
+ self._done = True
+ return emit_compaction(session)
+
+ # ------------------------------------------------------------------
+ # SDK-internal compaction
+ # ------------------------------------------------------------------
+
+ def reset_for_query(self) -> None:
+ """Reset per-query state before a new SDK query."""
+ self._done = False
+ self._start_emitted = False
+ self._tool_call_id = ""
+
+ def emit_start_if_ready(self) -> list[StreamBaseResponse]:
+ """If the PreCompact hook fired, emit start events (spinning tool)."""
+ if self._compact_start.is_set() and not self._start_emitted and not self._done:
+ self._compact_start.clear()
+ self._start_emitted = True
+ self._tool_call_id = _new_tool_call_id()
+ return _start_events(self._tool_call_id)
+ return []
+
+ async def emit_end_if_ready(self, session: ChatSession) -> list[StreamBaseResponse]:
+ """If compaction is in progress, emit end events and persist."""
+ # Yield so pending hook tasks can set compact_start
+ await asyncio.sleep(0)
+
+ if self._done:
+ return []
+ if not self._start_emitted and not self._compact_start.is_set():
+ return []
+
+ if self._start_emitted:
+ # Close the open spinner
+ done_events = _end_events(self._tool_call_id, COMPACTION_DONE_MSG)
+ persist_id = self._tool_call_id
+ 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
+ )
+
+ self._compact_start.clear()
+ self._start_emitted = False
+ self._done = True
+ _persist(session, persist_id, COMPACTION_DONE_MSG)
+ return done_events
diff --git a/autogpt_platform/backend/backend/copilot/sdk/compaction_test.py b/autogpt_platform/backend/backend/copilot/sdk/compaction_test.py
new file mode 100644
index 0000000000..024381aeb6
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/compaction_test.py
@@ -0,0 +1,291 @@
+"""Tests for sdk/compaction.py — event builders, filtering, persistence, and
+CompactionTracker state machine."""
+
+import pytest
+
+from backend.copilot.constants import COMPACTION_DONE_MSG, COMPACTION_TOOL_NAME
+from backend.copilot.model import ChatMessage, ChatSession
+from backend.copilot.response_model import (
+ StreamFinishStep,
+ StreamStartStep,
+ StreamToolInputAvailable,
+ StreamToolInputStart,
+ StreamToolOutputAvailable,
+)
+from backend.copilot.sdk.compaction import (
+ CompactionTracker,
+ compaction_events,
+ emit_compaction,
+ filter_compaction_messages,
+)
+
+# ---------------------------------------------------------------------------
+# Helpers
+# ---------------------------------------------------------------------------
+
+
+def _make_session() -> ChatSession:
+ return ChatSession.new(user_id="test-user")
+
+
+# ---------------------------------------------------------------------------
+# compaction_events
+# ---------------------------------------------------------------------------
+
+
+class TestCompactionEvents:
+ def test_returns_start_and_end_events(self):
+ evts = compaction_events("done")
+ assert len(evts) == 5
+ assert isinstance(evts[0], StreamStartStep)
+ assert isinstance(evts[1], StreamToolInputStart)
+ assert isinstance(evts[2], StreamToolInputAvailable)
+ assert isinstance(evts[3], StreamToolOutputAvailable)
+ assert isinstance(evts[4], StreamFinishStep)
+
+ def test_uses_provided_tool_call_id(self):
+ evts = compaction_events("msg", tool_call_id="my-id")
+ tool_start = evts[1]
+ assert isinstance(tool_start, StreamToolInputStart)
+ assert tool_start.toolCallId == "my-id"
+
+ def test_generates_id_when_not_provided(self):
+ evts = compaction_events("msg")
+ tool_start = evts[1]
+ assert isinstance(tool_start, StreamToolInputStart)
+ assert tool_start.toolCallId.startswith("compaction-")
+
+ def test_tool_name_is_context_compaction(self):
+ evts = compaction_events("msg")
+ tool_start = evts[1]
+ assert isinstance(tool_start, StreamToolInputStart)
+ assert tool_start.toolName == COMPACTION_TOOL_NAME
+
+
+# ---------------------------------------------------------------------------
+# emit_compaction
+# ---------------------------------------------------------------------------
+
+
+class TestEmitCompaction:
+ def test_persists_to_session(self):
+ session = _make_session()
+ assert len(session.messages) == 0
+ evts = emit_compaction(session)
+ assert len(evts) == 5
+ # Should have appended 2 messages (assistant tool call + tool result)
+ assert len(session.messages) == 2
+ assert session.messages[0].role == "assistant"
+ assert session.messages[0].tool_calls is not None
+ assert (
+ session.messages[0].tool_calls[0]["function"]["name"]
+ == COMPACTION_TOOL_NAME
+ )
+ assert session.messages[1].role == "tool"
+ assert session.messages[1].content == COMPACTION_DONE_MSG
+
+
+# ---------------------------------------------------------------------------
+# filter_compaction_messages
+# ---------------------------------------------------------------------------
+
+
+class TestFilterCompactionMessages:
+ def test_removes_compaction_tool_calls(self):
+ msgs = [
+ ChatMessage(role="user", content="hello"),
+ ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[
+ {
+ "id": "comp-1",
+ "type": "function",
+ "function": {"name": COMPACTION_TOOL_NAME, "arguments": "{}"},
+ }
+ ],
+ ),
+ ChatMessage(
+ role="tool", content=COMPACTION_DONE_MSG, tool_call_id="comp-1"
+ ),
+ ChatMessage(role="assistant", content="world"),
+ ]
+ filtered = filter_compaction_messages(msgs)
+ assert len(filtered) == 2
+ assert filtered[0].content == "hello"
+ assert filtered[1].content == "world"
+
+ def test_keeps_non_compaction_tool_calls(self):
+ msgs = [
+ ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[
+ {
+ "id": "real-1",
+ "type": "function",
+ "function": {"name": "search", "arguments": "{}"},
+ }
+ ],
+ ),
+ ChatMessage(role="tool", content="result", tool_call_id="real-1"),
+ ]
+ filtered = filter_compaction_messages(msgs)
+ assert len(filtered) == 2
+
+ def test_keeps_assistant_with_content_and_compaction_call(self):
+ """If assistant message has both content and a compaction tool call,
+ the message is kept (has real content)."""
+ msgs = [
+ ChatMessage(
+ role="assistant",
+ content="I have content",
+ tool_calls=[
+ {
+ "id": "comp-1",
+ "type": "function",
+ "function": {"name": COMPACTION_TOOL_NAME, "arguments": "{}"},
+ }
+ ],
+ ),
+ ]
+ filtered = filter_compaction_messages(msgs)
+ assert len(filtered) == 1
+
+ def test_empty_list(self):
+ assert filter_compaction_messages([]) == []
+
+
+# ---------------------------------------------------------------------------
+# CompactionTracker
+# ---------------------------------------------------------------------------
+
+
+class TestCompactionTracker:
+ def test_on_compact_sets_event(self):
+ tracker = CompactionTracker()
+ tracker.on_compact()
+ assert tracker._compact_start.is_set()
+
+ def test_emit_start_if_ready_no_event(self):
+ tracker = CompactionTracker()
+ assert tracker.emit_start_if_ready() == []
+
+ def test_emit_start_if_ready_with_event(self):
+ tracker = CompactionTracker()
+ tracker.on_compact()
+ evts = tracker.emit_start_if_ready()
+ assert len(evts) == 3
+ assert isinstance(evts[0], StreamStartStep)
+ assert isinstance(evts[1], StreamToolInputStart)
+ assert isinstance(evts[2], StreamToolInputAvailable)
+
+ def test_emit_start_only_once(self):
+ tracker = CompactionTracker()
+ tracker.on_compact()
+ evts1 = tracker.emit_start_if_ready()
+ assert len(evts1) == 3
+ # Second call should return empty
+ evts2 = tracker.emit_start_if_ready()
+ assert evts2 == []
+
+ @pytest.mark.asyncio
+ async def test_emit_end_after_start(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.on_compact()
+ tracker.emit_start_if_ready()
+ evts = await tracker.emit_end_if_ready(session)
+ assert len(evts) == 2
+ assert isinstance(evts[0], StreamToolOutputAvailable)
+ assert isinstance(evts[1], StreamFinishStep)
+ # Should persist
+ assert len(session.messages) == 2
+
+ @pytest.mark.asyncio
+ async def test_emit_end_without_start_self_contained(self):
+ """If PreCompact fired but start was never emitted, emit_end
+ produces a self-contained compaction event."""
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.on_compact()
+ # Don't call emit_start_if_ready
+ evts = await tracker.emit_end_if_ready(session)
+ assert len(evts) == 5 # Full self-contained event
+ assert isinstance(evts[0], StreamStartStep)
+ assert len(session.messages) == 2
+
+ @pytest.mark.asyncio
+ async def test_emit_end_no_op_when_done(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.on_compact()
+ tracker.emit_start_if_ready()
+ await tracker.emit_end_if_ready(session)
+ # Second call should be no-op
+ evts = await tracker.emit_end_if_ready(session)
+ assert evts == []
+
+ @pytest.mark.asyncio
+ async def test_emit_end_no_op_when_nothing_happened(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ evts = await tracker.emit_end_if_ready(session)
+ assert evts == []
+
+ def test_emit_pre_query(self):
+ tracker = CompactionTracker()
+ session = _make_session()
+ evts = tracker.emit_pre_query(session)
+ assert len(evts) == 5
+ assert len(session.messages) == 2
+ assert tracker._done is True
+
+ def test_reset_for_query(self):
+ tracker = CompactionTracker()
+ tracker._done = True
+ tracker._start_emitted = True
+ tracker._tool_call_id = "old"
+ tracker.reset_for_query()
+ assert tracker._done is False
+ assert tracker._start_emitted is False
+ assert tracker._tool_call_id == ""
+
+ @pytest.mark.asyncio
+ async def test_pre_query_blocks_sdk_compaction(self):
+ """After pre-query compaction, SDK compaction events are suppressed."""
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query(session)
+ tracker.on_compact()
+ evts = tracker.emit_start_if_ready()
+ assert evts == [] # _done blocks it
+
+ @pytest.mark.asyncio
+ async def test_reset_allows_new_compaction(self):
+ """After reset_for_query, compaction can fire again."""
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.emit_pre_query(session)
+ tracker.reset_for_query()
+ tracker.on_compact()
+ evts = tracker.emit_start_if_ready()
+ assert len(evts) == 3 # Start events emitted
+
+ @pytest.mark.asyncio
+ async def test_tool_call_id_consistency(self):
+ """Start and end events use the same tool_call_id."""
+ tracker = CompactionTracker()
+ session = _make_session()
+ tracker.on_compact()
+ start_evts = tracker.emit_start_if_ready()
+ end_evts = await tracker.emit_end_if_ready(session)
+ start_evt = start_evts[1]
+ end_evt = end_evts[0]
+ assert isinstance(start_evt, StreamToolInputStart)
+ assert isinstance(end_evt, StreamToolOutputAvailable)
+ assert start_evt.toolCallId == end_evt.toolCallId
+ # Persisted ID should also match
+ tool_calls = session.messages[0].tool_calls
+ assert tool_calls is not None
+ assert tool_calls[0]["id"] == start_evt.toolCallId
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 d66b8f873a..307d375238 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py
@@ -118,7 +118,7 @@ async def test_build_query_resume_up_to_date():
ChatMessage(role="user", content="what's new?"),
]
)
- result = await _build_query_message(
+ result, was_compacted = await _build_query_message(
"what's new?",
session,
use_resume=True,
@@ -127,6 +127,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
@pytest.mark.asyncio
@@ -141,7 +142,7 @@ async def test_build_query_resume_stale_transcript():
ChatMessage(role="user", content="turn 3"),
]
)
- result = await _build_query_message(
+ result, was_compacted = await _build_query_message(
"turn 3",
session,
use_resume=True,
@@ -152,6 +153,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
@pytest.mark.asyncio
@@ -164,7 +166,7 @@ async def test_build_query_resume_zero_msg_count():
ChatMessage(role="user", content="new msg"),
]
)
- result = await _build_query_message(
+ result, was_compacted = await _build_query_message(
"new msg",
session,
use_resume=True,
@@ -172,13 +174,14 @@ async def test_build_query_resume_zero_msg_count():
session_id="test-session",
)
assert result == "new msg"
+ assert was_compacted is False
@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 = await _build_query_message(
+ result, was_compacted = await _build_query_message(
"first",
session,
use_resume=False,
@@ -186,6 +189,7 @@ async def test_build_query_no_resume_single_message():
session_id="test-session",
)
assert result == "first"
+ assert was_compacted is False
@pytest.mark.asyncio
@@ -199,16 +203,16 @@ async def test_build_query_no_resume_multi_message(monkeypatch):
]
)
- # Mock _compress_conversation_history to return the messages as-is
- async def _mock_compress(sess):
- return sess.messages[:-1]
+ # Mock _compress_messages to return the messages as-is
+ async def _mock_compress(msgs):
+ return msgs, False
monkeypatch.setattr(
- "backend.copilot.sdk.service._compress_conversation_history",
+ "backend.copilot.sdk.service._compress_messages",
_mock_compress,
)
- result = await _build_query_message(
+ result, was_compacted = await _build_query_message(
"new question",
session,
use_resume=False,
@@ -219,3 +223,33 @@ 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
+
+
+async def test_build_query_no_resume_multi_message_compacted(monkeypatch):
+ """When compression actually compacts, was_compacted should be True."""
+ session = _make_session(
+ [
+ ChatMessage(role="user", content="old"),
+ ChatMessage(role="assistant", content="reply"),
+ ChatMessage(role="user", content="new"),
+ ]
+ )
+
+ async def _mock_compress(msgs):
+ return msgs, True # Simulate actual compaction
+
+ monkeypatch.setattr(
+ "backend.copilot.sdk.service._compress_messages",
+ _mock_compress,
+ )
+
+ result, was_compacted = await _build_query_message(
+ "new",
+ session,
+ use_resume=False,
+ transcript_msg_count=0,
+ session_id="test-session",
+ )
+ assert was_compacted is True
diff --git a/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py b/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
index 0d6e4817ae..edac1f8907 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
@@ -126,6 +126,7 @@ def create_security_hooks(
user_id: str | None,
sdk_cwd: str | None = None,
max_subtasks: int = 3,
+ on_compact: Callable[[], None] | None = None,
on_stop: Callable[[str, str], None] | None = None,
) -> dict[str, Any]:
"""Create the security hooks configuration for Claude Agent SDK.
@@ -306,6 +307,8 @@ def create_security_hooks(
logger.info(
f"[SDK] Context compaction triggered: {trigger}, user={user_id}"
)
+ if on_compact is not None:
+ on_compact()
return cast(SyncHookJSONOutput, {})
# --- Stop hook: capture transcript path for stateless resume ---
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 1ea1ab1a85..f367ac8c81 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -30,6 +30,7 @@ from backend.util.prompt import compress_context
from backend.util.settings import Settings
from ..config import ChatConfig
+from ..constants import COPILOT_ERROR_PREFIX, COPILOT_SYSTEM_PREFIX
from ..model import (
ChatMessage,
ChatSession,
@@ -55,6 +56,7 @@ from ..service import (
from ..tools.e2b_sandbox import get_or_create_sandbox
from ..tools.sandbox import WORKSPACE_PREFIX, make_session_path
from ..tracking import track_user_message
+from .compaction import CompactionTracker, filter_compaction_messages
from .response_adapter import SDKResponseAdapter
from .security_hooks import create_security_hooks
from .tool_adapter import (
@@ -145,15 +147,9 @@ class CapturedTranscript:
_SDK_CWD_PREFIX = WORKSPACE_PREFIX
-# Special message prefixes for text-based markers (parsed by frontend).
-# The hex suffix makes accidental LLM generation of these strings virtually
-# impossible, avoiding false-positive marker detection in normal conversation.
-COPILOT_ERROR_PREFIX = "[__COPILOT_ERROR_f7a1__]" # Renders as ErrorCard
-COPILOT_SYSTEM_PREFIX = "[__COPILOT_SYSTEM_e3b0__]" # Renders as system info message
-
# Heartbeat interval — keep SSE alive through proxies/LBs during tool execution.
# IMPORTANT: Must be less than frontend timeout (12s in useCopilotPage.ts)
-_HEARTBEAT_INTERVAL = 3.0 # seconds
+_HEARTBEAT_INTERVAL = 10.0 # seconds
# Appended to the system prompt to inform the agent about available tools.
@@ -370,21 +366,20 @@ def _cleanup_sdk_tool_results(cwd: str) -> None:
pass
-async def _compress_conversation_history(
- session: ChatSession,
-) -> list[ChatMessage]:
- """Compress prior conversation messages if they exceed the token threshold.
+async def _compress_messages(
+ messages: list[ChatMessage],
+) -> tuple[list[ChatMessage], bool]:
+ """Compress a list of messages if they exceed the token threshold.
Uses the shared compress_context() from prompt.py which supports:
- LLM summarization of old messages (keeps recent ones intact)
- Progressive content truncation as fallback
- Middle-out deletion as last resort
-
- Returns the compressed prior messages (everything except the current message).
"""
- messages = session.messages[:-1]
+ messages = filter_compaction_messages(messages)
+
if len(messages) < 2:
- return messages
+ return messages, False
# Convert ChatMessages to dicts for compress_context
messages_dict = []
@@ -432,9 +427,9 @@ async def _compress_conversation_history(
tool_call_id=m.get("tool_call_id"),
)
for m in result.messages
- ]
+ ], True
- return messages
+ return messages, False
def _format_conversation_context(messages: list[ChatMessage]) -> str | None:
@@ -449,6 +444,9 @@ def _format_conversation_context(messages: list[ChatMessage]) -> str | None:
if not messages:
return None
+ # Filter out compaction messages first, then format
+ messages = filter_compaction_messages(messages)
+
lines: list[str] = []
for msg in messages:
if msg.role == "user":
@@ -504,36 +502,46 @@ async def _build_query_message(
use_resume: bool,
transcript_msg_count: int,
session_id: str,
-) -> str:
+) -> tuple[str, bool]:
"""Build the query message with appropriate context.
- With --resume the CLI already has full context, so only the new message
- is needed. Without resume, compress history into a context prefix.
- Hybrid mode: if the transcript is stale, compress only the gap.
+ Returns:
+ Tuple of (query_message, was_compacted).
"""
msg_count = len(session.messages)
if use_resume and transcript_msg_count > 0:
if transcript_msg_count < msg_count - 1:
gap = session.messages[transcript_msg_count:-1]
- gap_context = _format_conversation_context(gap)
+ compressed, was_compressed = await _compress_messages(gap)
+ gap_context = _format_conversation_context(compressed)
if gap_context:
logger.info(
- f"[SDK] Transcript stale: covers {transcript_msg_count} "
- f"of {msg_count} messages, compressing {len(gap)} missed"
+ "[SDK] Transcript stale: covers %d of %d messages, "
+ "gap=%d (compressed=%s)",
+ transcript_msg_count,
+ msg_count,
+ len(gap),
+ was_compressed,
+ )
+ return (
+ f"{gap_context}\n\nNow, the user says:\n{current_message}",
+ was_compressed,
)
- return f"{gap_context}\n\nNow, the user says:\n{current_message}"
elif not use_resume and msg_count > 1:
logger.warning(
f"[SDK] Using compression fallback for session "
f"{session_id} ({msg_count} messages) — no transcript for --resume"
)
- compressed = await _compress_conversation_history(session)
+ compressed, was_compressed = await _compress_messages(session.messages[:-1])
history_context = _format_conversation_context(compressed)
if history_context:
- return f"{history_context}\n\nNow, the user says:\n{current_message}"
+ return (
+ f"{history_context}\n\nNow, the user says:\n{current_message}",
+ was_compressed,
+ )
- return current_message
+ return current_message, False
async def stream_chat_completion_sdk(
@@ -729,11 +737,15 @@ async def stream_chat_completion_sdk(
f"{transcript_path}"
)
+ # Track SDK-internal compaction (PreCompact hook → start, next msg → end)
+ compaction = CompactionTracker()
+
security_hooks = create_security_hooks(
user_id,
sdk_cwd=sdk_cwd,
max_subtasks=config.claude_agent_max_subtasks,
on_stop=_on_stop if config.claude_agent_use_resume else None,
+ on_compact=compaction.on_compact,
)
# --- Resume strategy: download transcript from bucket ---
@@ -815,7 +827,7 @@ async def stream_chat_completion_sdk(
)
return
- query_message = await _build_query_message(
+ query_message, was_compacted = await _build_query_message(
current_message,
session,
use_resume,
@@ -829,6 +841,11 @@ async def stream_chat_completion_sdk(
len(session.messages),
len(query_message),
)
+
+ compaction.reset_for_query()
+ if was_compacted:
+ for ev in compaction.emit_pre_query(session):
+ yield ev
await client.query(query_message, session_id=session_id)
assistant_response = ChatMessage(role="assistant", content="")
@@ -860,9 +877,9 @@ async def stream_chat_completion_sdk(
)
if not done:
- # Timeout — emit heartbeat but keep the task alive
- # Also refresh lock TTL to keep it alive
await lock.refresh()
+ for ev in compaction.emit_start_if_ready():
+ yield ev
yield StreamHeartbeat()
continue
@@ -958,6 +975,10 @@ async def stream_chat_completion_sdk(
sdk_msg.result or "(no error message provided)",
)
+ # Emit compaction end if SDK finished compacting
+ for ev in await compaction.emit_end_if_ready(session):
+ yield ev
+
for response in adapter.convert_message(sdk_msg):
if isinstance(response, StreamStart):
continue
diff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript.py b/autogpt_platform/backend/backend/copilot/sdk/transcript.py
index 39a9b1352c..b014d6f28e 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/transcript.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript.py
@@ -357,29 +357,29 @@ async def upload_transcript(
# Check existing transcript size to avoid overwriting newer with older
path = _build_storage_path(user_id, session_id, storage)
+ content_skipped = False
try:
existing = await storage.retrieve(path)
if len(existing) >= new_size:
logger.info(
- f"[Transcript] Skipping upload — existing ({len(existing)}B) "
+ f"[Transcript] Skipping content upload — existing ({len(existing)}B) "
f">= new ({new_size}B) for session {session_id}"
)
- return
+ content_skipped = True
except (FileNotFoundError, Exception):
pass # No existing transcript or retrieval error — proceed with upload
- await storage.store(
- workspace_id=wid,
- file_id=fid,
- filename=fname,
- content=encoded,
- )
+ if not content_skipped:
+ await storage.store(
+ workspace_id=wid,
+ file_id=fid,
+ filename=fname,
+ content=encoded,
+ )
- # Store metadata alongside the transcript so the next turn can detect
- # staleness and only compress the gap instead of the full history.
- # Wrapped in try/except so a metadata write failure doesn't orphan
- # the already-uploaded transcript — the next turn will just fall back
- # to full gap fill (msg_count=0).
+ # Always update metadata (even when content is skipped) so message_count
+ # stays current. The gap-fill logic in _build_query_message relies on
+ # message_count to avoid re-compressing the same messages every turn.
try:
meta = {"message_count": message_count, "uploaded_at": time.time()}
mwid, mfid, mfname = _meta_storage_path_parts(user_id, session_id)
@@ -394,7 +394,8 @@ async def upload_transcript(
logger.info(
f"[Transcript] Uploaded {new_size}B "
- f"(stripped from {len(content)}B, msg_count={message_count}) "
+ f"(stripped from {len(content)}B, msg_count={message_count}, "
+ f"content_skipped={content_skipped}) "
f"for session {session_id}"
)
diff --git a/autogpt_platform/backend/backend/copilot/service.py b/autogpt_platform/backend/backend/copilot/service.py
index 181d119587..a5dfe612de 100644
--- a/autogpt_platform/backend/backend/copilot/service.py
+++ b/autogpt_platform/backend/backend/copilot/service.py
@@ -33,6 +33,7 @@ from backend.util.exceptions import NotFoundError
from backend.util.settings import AppEnvironment, Settings
from .config import ChatConfig
+from .constants import COMPACTION_TOOL_NAME
from .model import (
ChatMessage,
ChatSession,
@@ -545,6 +546,31 @@ async def stream_chat_completion(
system_prompt=system_prompt,
text_block_id=text_block_id,
):
+ # Pass through out-of-band events (e.g. compaction notices)
+ # without contaminating the primary text stream state.
+ if isinstance(chunk, (StreamStartStep, StreamFinishStep)):
+ yield chunk
+ continue
+ if (
+ isinstance(
+ chunk,
+ (
+ StreamToolInputStart,
+ StreamToolInputAvailable,
+ StreamToolOutputAvailable,
+ ),
+ )
+ and getattr(chunk, "toolName", None) == COMPACTION_TOOL_NAME
+ ):
+ yield chunk
+ continue
+ if (
+ isinstance(chunk, (StreamTextStart, StreamTextDelta, StreamTextEnd))
+ and getattr(chunk, "id", None) != text_block_id
+ ):
+ yield chunk
+ continue
+
if isinstance(chunk, StreamTextStart):
# Emit text-start before first text delta
if not has_received_text:
@@ -994,6 +1020,10 @@ async def _stream_chat_chunks(
logger.info(
f"Context compacted for streaming: {context_result.token_count} tokens"
)
+ from .sdk.compaction import emit_compaction
+
+ for ev in emit_compaction(session):
+ yield ev
# Loop to handle tool calls and continue conversation
while True:
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx
index ad6753e6ac..c5f28ebd66 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx
@@ -3,6 +3,7 @@
import React from "react";
import { ToolUIPart } from "ai";
import {
+ ArrowsClockwiseIcon,
CheckCircleIcon,
CircleDashedIcon,
CircleIcon,
@@ -57,6 +58,7 @@ type ToolCategory =
| "search"
| "edit"
| "todo"
+ | "compaction"
| "other";
function getToolCategory(toolName: string): ToolCategory {
@@ -93,6 +95,8 @@ function getToolCategory(toolName: string): ToolCategory {
return "edit";
case "TodoWrite":
return "todo";
+ case "context_compaction":
+ return "compaction";
default:
return "other";
}
@@ -148,6 +152,10 @@ function ToolIcon({
return (
<ListChecksIcon size={14} weight="regular" className={iconClass} />
);
+ case "compaction":
+ return (
+ <ArrowsClockwiseIcon size={14} weight="regular" className={iconClass} />
+ );
default:
return <GearIcon size={14} weight="regular" className={iconClass} />;
}
@@ -178,6 +186,8 @@ function AccordionIcon({ category }: { category: ToolCategory }) {
return <PencilSimpleIcon size={32} weight="light" />;
case "todo":
return <ListChecksIcon size={32} weight="light" />;
+ case "compaction":
+ return <ArrowsClockwiseIcon size={32} weight="light" />;
default:
return <GearIcon size={32} weight="light" />;
}
@@ -300,6 +310,8 @@ function getAnimationText(part: ToolUIPart, category: ToolCategory): string {
return shortSummary ? `Editing ${shortSummary}` : "Editing file…";
case "todo":
return shortSummary ? `${shortSummary}` : "Updating task list…";
+ case "compaction":
+ return "Summarizing earlier messages…";
default:
return `Running ${formatToolName(toolName)}…`;
}
@@ -343,6 +355,8 @@ function getAnimationText(part: ToolUIPart, category: ToolCategory): string {
return shortSummary ? `Edited ${shortSummary}` : "Edit completed";
case "todo":
return "Updated task list";
+ case "compaction":
+ return "Earlier messages were summarized";
default:
return `${formatToolName(toolName)} completed`;
}
@@ -846,7 +860,9 @@ export function GenericTool({ part }: Props) {
typeof part.input === "object" &&
Array.isArray((part.input as Record<string, unknown>).todos);
- const showAccordion = hasOutput || hasError || hasTodoInput;
+ // Compaction shows only a status line — no expandable accordion.
+ const showAccordion =
+ category !== "compaction" && (hasOutput || hasError || hasTodoInput);
const accordionData = showAccordion
? getAccordionData(category, toolName, part.input, output ?? {})
: null;