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.
perf(backend/copilot): cap eager-loaded chat messages at 1000 most-recent (#13047)
Zamil Majdyb2719fd9e431
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex f3930c5ec4..cb3aa92f78 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -94,6 +94,8 @@ from backend.copilot.transcript import (detect_gap,download_transcript,extract_context_messages,+ fill_hole_between_transcript_and_gap,+ next_uncovered_sequence,strip_for_upload,upload_transcript,validate_transcript,@@ -1251,11 +1253,23 @@ async def _load_prior_transcript()gap = detect_gap(restore, session_messages)- if gap:- _append_gap_to_builder(gap, transcript_builder)+ # Hole-fill: when the cap engaged AND the windowed view starts above the+ # transcript watermark, the sequences in between live in DB but neither+ # the transcript nor the loaded gap. Persisting them to the builder here+ # (so the next-turn upload includes them) closes the loop — without this,+ # detect_gap on every subsequent turn would re-discover the same hole.+ hole: list[ChatMessage] = []+ if restore.message_count > 0:+ hole = await fill_hole_between_transcript_and_gap(+ session_id, restore.message_count, gap+ )+ if hole or gap:+ _append_gap_to_builder(hole + gap, transcript_builder)logger.info(- "[Baseline] Filled gap: loaded %d transcript msgs + %d gap msgs from DB",+ "[Baseline] Filled gap: loaded %d transcript msgs + %d hole msgs "+ "+ %d gap msgs from DB",restore.message_count,+ len(hole),len(gap),)@@ -1523,7 +1537,9 @@ async def stream_chat_completion_baseline(# gap (DB messages after watermark) + current user turn.# This avoids re-reading the full session history from DB on every turn.# See extract_context_messages() in transcript.py for the shared primitive.- prior_context = extract_context_messages(transcript_download, session.messages)+ prior_context = await extract_context_messages(+ transcript_download, session.messages, session_id=session.session_id+ )messages_for_context = await _compress_session_messages(prior_context + ([session.messages[-1]] if session.messages else []),model=active_model,@@ -2179,11 +2195,15 @@ async def stream_chat_completion_baseline()if user_id and should_upload_transcript(user_id, transcript_upload_safe):+ # Watermark = the next uncovered DB sequence (= max non-reasoning+ # sequence + 1). Stays accurate on cap-engaged sessions because+ # the loaded window always includes the most-recent rows; the+ # max(sequence) is the last assistant just appended this turn.await _upload_final_transcript(user_id=user_id,session_id=session_id,transcript_builder=transcript_builder,- session_msg_count=len(session.messages),+ session_msg_count=next_uncovered_sequence(session.messages),)# Clean up the ephemeral working directory used for file attachments.diff --git a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.pyindex 8a9e435743..1db3045551 100644--- a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py@@ -56,9 +56,14 @@ def _make_transcript_content(*roles: str) -> str:def _make_session_messages(*roles: str) -> list[ChatMessage]:- """Build a list of ChatMessage objects matching the given roles."""+ """Build a list of ChatMessage objects matching the given roles.++ Sequences are assigned 0..N-1 to mirror the production schema, where+ ``ChatMessage.sequence`` is NOT NULL on every persisted row.+ """return [- ChatMessage(role=r, content=f"{r} message {i}") for i, r in enumerate(roles)+ ChatMessage(role=r, content=f"{r} message {i}", sequence=i)+ for i, r in enumerate(roles)]diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.pyindex d4aa1a5007..582c7e1431 100644--- a/autogpt_platform/backend/backend/copilot/db.py+++ b/autogpt_platform/backend/backend/copilot/db.py@@ -21,13 +21,7 @@ from pydantic import BaseModelfrom backend.data import dbfrom backend.util.json import SafeJson, sanitize_string-from .model import (- ChatMessage,- ChatSession,- ChatSessionInfo,- ChatSessionMetadata,- cache_chat_session,-)+from .model import ChatMessage, ChatSessionInfo, ChatSessionMetadata, cache_chat_sessionfrom .model import get_chat_session as get_chat_session_cachedlogger = logging.getLogger(__name__)@@ -44,13 +38,23 @@ class PaginatedMessages(BaseModel):session: ChatSessionInfo-async def get_chat_session(session_id: str) -> ChatSession | None:- """Get a chat session by ID from the database."""- session = await PrismaChatSession.prisma().find_unique(- where={"id": session_id},- include={"Messages": {"order_by": {"sequence": "asc"}}},- )- return ChatSession.from_db(session) if session else None+# Cap on messages eagerly loaded with a session. Set above the realistic p99+# of any actual copilot conversation (typical: 10-100 msgs; power user: a few+# hundred; cap engages only on pathological multi-thousand-message sessions).+# Older history beyond the cap relies on the GCS transcript checkpoint via+# ``extract_context_messages`` — if a session somehow exceeds this cap WITHOUT+# a transcript, the cap-hit warning below makes the case observable in logs.+MAX_LOADED_CHAT_MESSAGES = 1000+++# NOTE: there is intentionally no eager-load ``get_chat_session`` here. Callers+# that need the full session (LLM context, sub-session tools) call+# :func:`get_chat_messages_paginated` directly with+# ``limit=MAX_LOADED_CHAT_MESSAGES`` and assemble :class:`ChatSession` from the+# returned ``session`` (full :class:`ChatSessionInfo` — credentials,+# successful_agent_runs, usage, metadata) plus ``messages``. Going through the+# paginated path means tool-pair boundary expansion and the visibility+# guarantee already apply, and the cap-hit signal lives in ``has_more``.async def get_chat_session_metadata(session_id: str) -> ChatSessionInfo | None:@@ -66,73 +70,81 @@ async def get_chat_messages_paginated(limit: int = 50,before_sequence: int | None = None,user_id: str | None = None,+ *,+ after_sequence: int | None = None,) -> PaginatedMessages | None:- """Get paginated messages for a session, newest first.+ """Get a window of messages for a session in ascending sequence order.Verifies session existence (and ownership when ``user_id`` is provided)in parallel with the message query. Returns ``None`` when the sessionis not found or does not belong to the user.- After fetching, a visibility guarantee ensures the page contains at least- one user or assistant message. If the entire page is tool messages (which- are hidden in the UI), it expands backward until a visible message is found- so the chat never appears blank.+ Three access patterns share this function:++ - **Cursor pagination (UI scroll-back)**: pass ``before_sequence`` to fetch+ ``limit`` messages older than the cursor. Tool-pair boundary expansion+ and visibility expansion fire so the rendered chat is never malformed.+ - **Tail load (LLM-context eager-load)**: pass neither bound to fetch the+ most-recent ``limit`` messages. Same boundary/visibility fixes apply.+ - **Range fetch (hole-fill)**: pass both ``after_sequence`` and+ ``before_sequence`` to fetch the inclusive-exclusive range+ ``[after_sequence, before_sequence)``. Boundary/visibility expansion is+ skipped — the caller asked for an exact range, not a "best-effort window".++ ``has_more`` is ``True`` iff the underlying query found more rows than+ ``limit`` (i.e. the cap engaged); irrelevant for range fetches but+ harmlessly populated."""- # Build session-existence / ownership checksession_where: ChatSessionWhereInput = {"id": session_id}if user_id is not None:session_where["userId"] = user_id- # Build message include — fetch paginated messages in the same query+ msg_filter: dict = {}+ if before_sequence is not None:+ msg_filter["lt"] = before_sequence+ if after_sequence is not None:+ msg_filter["gte"] = after_sequence+msg_include: FindManyChatMessageArgsFromChatSession = {"order_by": {"sequence": "desc"},"take": limit + 1,}- if before_sequence is not None:- msg_include["where"] = {"sequence": {"lt": before_sequence}}+ if msg_filter:+ msg_include["where"] = {"sequence": msg_filter}- # Single query: session existence/ownership + paginated messagessession = await PrismaChatSession.prisma().find_first(where=session_where,include={"Messages": msg_include},)-if session is None:return Nonesession_info = ChatSessionInfo.from_db(session)results = list(session.Messages) if session.Messages else []-has_more = len(results) > limitresults = results[:limit]-- # Reverse to ascending order+ # Reverse DESC → ASC so callers see oldest-first.results.reverse()- # Tool-call boundary fix: if the oldest message is a tool message,- # expand backward to include the preceding assistant message that- # owns the tool_calls, so convertChatSessionMessagesToUiMessages- # can pair them correctly.- if results and results[0].role == "tool":- results, has_more = await _expand_tool_boundary(- session_id, results, has_more, user_id- )-- # Visibility guarantee: if the entire page has no user/assistant messages- # (all tool messages), the chat would appear blank. Expand backward- # until we find at least one visible message.- if results and not any(m.role in ("user", "assistant") for m in results):- results, has_more = await _expand_for_visibility(- session_id, results, has_more, user_id- )+ # Range fetches (after_sequence set) ask for an exact window — never expand+ # outside it. Cursor / tail loads get the boundary + visibility fixes so+ # the UI render and LLM-context build are well-formed.+ is_range_fetch = after_sequence is not None+ if not is_range_fetch:+ if results and results[0].role == "tool":+ results, has_more = await _expand_tool_boundary(+ session_id, results, has_more, user_id+ )+ if results and not any(m.role in ("user", "assistant") for m in results):+ results, has_more = await _expand_for_visibility(+ session_id, results, has_more, user_id+ )messages = [ChatMessage.from_db(m) for m in results]- oldest_sequence = messages[0].sequence if messages else None-return PaginatedMessages(messages=messages,has_more=has_more,- oldest_sequence=oldest_sequence,+ oldest_sequence=messages[0].sequence if messages else None,session=session_info,)diff --git a/autogpt_platform/backend/backend/copilot/db_test.py b/autogpt_platform/backend/backend/copilot/db_test.pyindex 93368093a1..d86fb43cba 100644--- a/autogpt_platform/backend/backend/copilot/db_test.py+++ b/autogpt_platform/backend/backend/copilot/db_test.py@@ -608,3 +608,10 @@ async def test_update_message_content_by_sequence_sanitizes_content():where={"sessionId": "sess-1", "sequence": 0},data={"content": "sanitized"},)+++# NOTE: previously this file had a separate suite for ``db.get_chat_session``+# (windowed eager-load). That function was removed in favour of going through+# ``get_chat_messages_paginated`` directly — see ``model._get_session_from_db``.+# Cap-hit + tool-pair boundary behaviour is now covered by the paginated tests+# above and the integration coverage in ``model_test.py``.diff --git a/autogpt_platform/backend/backend/copilot/model.py b/autogpt_platform/backend/backend/copilot/model.pyindex 6b5e54586e..9cba86b03c 100644--- a/autogpt_platform/backend/backend/copilot/model.py+++ b/autogpt_platform/backend/backend/copilot/model.py@@ -602,11 +602,36 @@ async def _get_session_from_cache(session_id: str) -> ChatSession | None:async def _get_session_from_db(session_id: str) -> ChatSession | None:- """Get a chat session from the database."""- session = await chat_db().get_chat_session(session_id)- if not session:+ """Get a chat session from the database, capped at MAX_LOADED_CHAT_MESSAGES.++ Goes through the paginated loader (with ``limit=MAX_LOADED_CHAT_MESSAGES``,+ no cursor) so the LLM-context path inherits the same tool-pair boundary+ expansion and visibility guarantees the UI scroll-back path uses. The+ cap-hit log surfaces here (the LLM-context use case) rather than inside the+ generic paginated function.+ """+ # Local import to avoid the model→db→model cycle.+ from .db import MAX_LOADED_CHAT_MESSAGES++ page = await chat_db().get_chat_messages_paginated(+ session_id, limit=MAX_LOADED_CHAT_MESSAGES+ )+ if page is None:return None+ if page.has_more:+ logger.warning(+ "Session %s loaded with capped messages (%d) — older history "+ "must come from transcript checkpoint or context will be lost",+ session_id,+ MAX_LOADED_CHAT_MESSAGES,+ )++ session = ChatSession(+ **page.session.model_dump(),+ messages=page.messages,+ )+logger.info(f"Loaded session {session_id} from DB: "f"has_messages={bool(session.messages)}, "@@ -698,8 +723,9 @@ async def _save_session_to_db(db = chat_db()if not skip_existence_check:- # Check if session exists in DB- existing = await db.get_chat_session(session.session_id)+ # Existence check only — use the metadata-only fetch, no need to+ # eager-load any messages here.+ existing = await db.get_chat_session_metadata(session.session_id)if not existing:# Create new session@@ -724,8 +750,22 @@ async def _save_session_to_db(total_completion_tokens=total_completion,)- # Add new messages (only those after existing count)- new_messages = session.messages[existing_message_count:]+ # Identify unsaved messages. Two cases:+ #+ # - Brand-new session (existing_message_count == 0): persist every message,+ # regardless of whether it carries a sequence (callers may construct+ # ``ChatSession`` from a list of ChatMessage objects that already have+ # sequences set, e.g. when re-using a fixture across tests — those still+ # need to be saved as new rows).+ # - Existing session: filter by ``sequence is None``. Slicing by+ # ``session.messages[existing_message_count:]`` is incorrect for windowed+ # loads (cap of MAX_LOADED_CHAT_MESSAGES) — a session with 1500 saved+ # messages comes back with 1000 entries, and that slice would silently+ # drop every newly-appended message.+ if existing_message_count == 0:+ new_messages = list(session.messages)+ else:+ new_messages = [m for m in session.messages if m.sequence is None]if new_messages:messages_data = []for msg in new_messages:diff --git a/autogpt_platform/backend/backend/copilot/model_test.py b/autogpt_platform/backend/backend/copilot/model_test.pyindex 6f907f4072..4c48b56391 100644--- a/autogpt_platform/backend/backend/copilot/model_test.py+++ b/autogpt_platform/backend/backend/copilot/model_test.py@@ -943,6 +943,134 @@ async def test_append_and_save_message_lock_release_failure_is_ignored(assert result is not None[email protected](loop_scope="session")+async def test_save_session_to_db_persists_new_messages_when_windowed(+ mocker: MockerFixture,+) -> None:+ """Regression: when session.messages is the windowed tail (cap-limited),+ new messages must still be persisted. The earlier index-based slice+ ``session.messages[existing_message_count:]`` silently dropped them."""+ from .model import _save_session_to_db++ # Simulate a session loaded with the cap: DB has 1500 messages, only the+ # last 3 are in memory (sequences 1497..1499), plus one freshly appended+ # message that has no sequence yet.+ loaded = [+ ChatMessage(role="user", content=f"old-{seq}", sequence=seq)+ for seq in (1497, 1498, 1499)+ ]+ new_msg = ChatMessage(role="assistant", content="brand-new")+ session = _make_session_with_messages(*loaded, new_msg)++ mock_db = mocker.MagicMock()+ mock_db.update_chat_session = mocker.AsyncMock()+ mock_db.add_chat_messages_batch = mocker.AsyncMock()+ mocker.patch("backend.copilot.model.chat_db", return_value=mock_db)++ await _save_session_to_db(+ session, existing_message_count=1500, skip_existence_check=True+ )++ mock_db.add_chat_messages_batch.assert_awaited_once()+ kwargs = mock_db.add_chat_messages_batch.call_args.kwargs+ assert kwargs["start_sequence"] == 1500+ assert len(kwargs["messages"]) == 1+ assert kwargs["messages"][0]["content"] == "brand-new"+ # And the in-memory new message receives its sequence back-fill.+ assert new_msg.sequence == 1500+++# ─── _get_session_from_db cap-hit warning ──────────────────────────────+++def _make_paginated_response(*, has_more: bool, message_count: int):+ """Build a PaginatedMessages stub for _get_session_from_db tests."""+ from datetime import UTC, datetime++ from .db import PaginatedMessages+ from .model import ChatSessionInfo++ info = ChatSessionInfo(+ session_id="sess-cap",+ user_id="u1",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ )+ messages = [+ ChatMessage(role="user", content=f"m-{i}", sequence=i)+ for i in range(message_count)+ ]+ return PaginatedMessages(+ messages=messages,+ has_more=has_more,+ oldest_sequence=messages[0].sequence if messages else None,+ session=info,+ )++[email protected](loop_scope="session")+async def test_get_session_from_db_warns_when_cap_engages(+ mocker: MockerFixture, caplog: pytest.LogCaptureFixture+) -> None:+ """When ``get_chat_messages_paginated`` reports ``has_more=True`` (the+ cap engaged on the LLM-context path), ``_get_session_from_db`` must+ forward the cap-hit warning so observers can see context loss."""+ from .model import _get_session_from_db++ page = _make_paginated_response(has_more=True, message_count=3)+ mock_db = mocker.MagicMock()+ mock_db.get_chat_messages_paginated = mocker.AsyncMock(return_value=page)+ mocker.patch("backend.copilot.model.chat_db", return_value=mock_db)++ with caplog.at_level("WARNING", logger="backend.copilot.model"):+ session = await _get_session_from_db("sess-cap")++ assert session is not None and len(session.messages) == 3+ cap_warnings = [+ r for r in caplog.records if "loaded with capped messages" in r.getMessage()+ ]+ assert len(cap_warnings) == 1++[email protected](loop_scope="session")+async def test_get_session_from_db_silent_when_below_cap(+ mocker: MockerFixture, caplog: pytest.LogCaptureFixture+) -> None:+ """``has_more=False`` is the common path — the cap-hit warning must not+ fire there, otherwise every normal session load would log a false+ positive."""+ from .model import _get_session_from_db++ page = _make_paginated_response(has_more=False, message_count=3)+ mock_db = mocker.MagicMock()+ mock_db.get_chat_messages_paginated = mocker.AsyncMock(return_value=page)+ mocker.patch("backend.copilot.model.chat_db", return_value=mock_db)++ with caplog.at_level("WARNING", logger="backend.copilot.model"):+ await _get_session_from_db("sess-cap")++ cap_warnings = [+ r for r in caplog.records if "loaded with capped messages" in r.getMessage()+ ]+ assert cap_warnings == []++[email protected](loop_scope="session")+async def test_get_session_from_db_returns_none_when_missing(+ mocker: MockerFixture,+) -> None:+ """Missing session → ``get_chat_messages_paginated`` returns ``None`` →+ forwarder returns ``None`` (no crash on the .has_more access)."""+ from .model import _get_session_from_db++ mock_db = mocker.MagicMock()+ mock_db.get_chat_messages_paginated = mocker.AsyncMock(return_value=None)+ mocker.patch("backend.copilot.model.chat_db", return_value=mock_db)++ assert await _get_session_from_db("missing") is None++# ─── get_or_create_builder_session ─────────────────────────────────────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.pyindex 212fca189b..9e0008e787 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@@ -73,7 +73,10 @@ def _make_session(messages: list[ChatMessage]) -> ChatSession:def _msgs(*pairs: tuple[str, str]) -> list[ChatMessage]:- return [ChatMessage(role=r, content=c) for r, c in pairs]+ """Build messages with sequences 0..N-1 mirroring the production schema."""+ return [+ ChatMessage(role=r, content=c, sequence=i) for i, (r, c) in enumerate(pairs)+ ]# ---------------------------------------------------------------------------@@ -281,9 +284,9 @@ class TestSdkToFastModeSwitch:user_id="user-1",session_id="session-1",session_messages=[- ChatMessage(role="user", content="sdk-question"),- ChatMessage(role="assistant", content="sdk-answer"),- ChatMessage(role="user", content="baseline-question"),+ ChatMessage(role="user", content="sdk-question", sequence=0),+ ChatMessage(role="assistant", content="sdk-answer", sequence=1),+ ChatMessage(role="user", content="baseline-question", sequence=2),],transcript_builder=baseline_builder,)@@ -324,7 +327,11 @@ class TestSdkToFastModeSwitch:# Build a session with 10 alternating user/assistant messages + current usersession_messages = [- ChatMessage(role="user" if i % 2 == 0 else "assistant", content=f"msg-{i}")+ ChatMessage(+ role="user" if i % 2 == 0 else "assistant",+ content=f"msg-{i}",+ sequence=i,+ )for i in range(10)]diff --git a/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py b/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.pyindex da9477121d..14dcd62d80 100644--- a/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py@@ -469,3 +469,187 @@ async def test_build_query_no_resume_above_token_floor_compresses(monkeypatch):# Above the floor → history is injected (not the bare message)assert "<conversation_history>" in resultassert "Now, the user says:\nnew" in result+++# ---------------------------------------------------------------------------+# Cap-engaged: window starts above absolute sequence 0+# ---------------------------------------------------------------------------+++async def test_build_query_resume_cap_engaged_uses_sequence_gap(monkeypatch):+ """When the eager-load cap engages, the windowed ``prior`` doesn't start+ at absolute sequence 0; index-based ``prior[transcript_msg_count:]`` is+ wrong. ``transcript_msg_count`` IS the next uncovered DB sequence, so+ filtering by ``sequence >= watermark`` reads it directly."""+ # Window contains the 3 most-recent rows of a much longer conversation.+ # Watermark = 1500 (next uncovered DB sequence).+ session = _make_session(+ [+ ChatMessage(role="user", content="cap-window-user", sequence=1500),+ ChatMessage(role="assistant", content="cap-window-asst", sequence=1501),+ ChatMessage(role="user", content="current turn", sequence=1502),+ ]+ )++ async def _mock_compress(msgs, target_tokens=None):+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages",+ _mock_compress,+ )++ # No DB lookup expected — the watermark is already a sequence.+ fake_db = type("FakeDB", (), {})()+ monkeypatch.setattr(+ "backend.copilot.sdk.service.chat_db",+ lambda: fake_db,+ )++ result, was_compacted = await _build_query_message(+ "current turn",+ session,+ use_resume=True,+ transcript_msg_count=1500,+ session_id="test-session",+ )+ # Both windowed messages have sequence >= 1500 — they form the gap.+ assert "<conversation_history>" in result+ 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+++async def test_build_query_resume_cap_engaged_hole_fills_above_watermark(monkeypatch):+ """When the window starts above the watermark, the missing sequences are+ fetched from DB and prepended to ``window_gap`` so the LLM sees the full+ post-watermark conversation."""+ from datetime import UTC, datetime++ from backend.copilot.db import PaginatedMessages+ from backend.copilot.model import ChatSessionInfo++ # Window covers sequences 1502..1503; watermark sits at 1500 leaving+ # sequences 1500..1501 in the hole.+ session = _make_session(+ [+ ChatMessage(role="user", content="window-1502", sequence=1502),+ ChatMessage(role="assistant", content="window-1503", sequence=1503),+ ChatMessage(role="user", content="current turn", sequence=1504),+ ]+ )++ async def _mock_compress(msgs, target_tokens=None):+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages",+ _mock_compress,+ )++ hole_page = PaginatedMessages(+ messages=[+ ChatMessage(role="user", content="hole-1500", sequence=1500),+ ChatMessage(role="assistant", content="hole-1501", sequence=1501),+ ],+ has_more=False,+ oldest_sequence=1500,+ session=ChatSessionInfo(+ session_id="test-session",+ user_id="u1",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ ),+ )++ fake_db = type(+ "FakeDB",+ (),+ {+ "get_chat_messages_paginated": staticmethod(+ lambda *args, **kwargs: _async_return(hole_page)+ ),+ },+ )()+ monkeypatch.setattr(+ "backend.copilot.sdk.service.chat_db",+ lambda: fake_db,+ )++ result, _ = await _build_query_message(+ "current turn",+ session,+ use_resume=True,+ transcript_msg_count=1500,+ session_id="test-session",+ )+ # All 4 messages (2 hole + 2 window) are in the gap.+ assert "<conversation_history>" in result+ assert "hole-1500" in result+ assert "hole-1501" in result+ assert "window-1502" in result+ assert "window-1503" in result+ assert "Now, the user says:\ncurrent turn" in result+++async def test_build_query_resume_cap_engaged_hole_fill_failure_uses_window_only(+ monkeypatch,+):+ """If the hole-fill fetch raises, fall back to the windowed gap rather+ than dropping the entire LLM context."""+ session = _make_session(+ [+ ChatMessage(role="user", content="window-1502", sequence=1502),+ ChatMessage(role="assistant", content="window-1503", sequence=1503),+ ChatMessage(role="user", content="current", sequence=1504),+ ]+ )++ async def _mock_compress(msgs, target_tokens=None):+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages",+ _mock_compress,+ )++ fake_db = type(+ "FakeDB",+ (),+ {+ "get_chat_messages_paginated": staticmethod(+ lambda *args, **kwargs: _async_raise(RuntimeError("db down"))+ ),+ },+ )()+ monkeypatch.setattr(+ "backend.copilot.sdk.service.chat_db",+ lambda: fake_db,+ )++ result, _ = await _build_query_message(+ "current",+ session,+ use_resume=True,+ transcript_msg_count=1500,+ session_id="test-session",+ )+ # Hole fetch failed but window_gap survives — context is partial but+ # not empty, and the turn keeps going.+ assert "<conversation_history>" in result+ assert "window-1502" in result+ assert "window-1503" in result+ assert "Now, the user says:\ncurrent" in result+++async def _async_return(value):+ return value+++async def _async_raise(exc):+ raise excdiff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 51e86c1fd5..80eb576d07 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -41,6 +41,7 @@ from langsmith.integrations.claude_agent_sdk import configure_claude_agent_sdkfrom opentelemetry import trace as otel_tracefrom pydantic import BaseModel+from backend.data.db_accessors import chat_dbfrom backend.data.redis_client import get_redis_asyncfrom backend.executor.cluster_lock import AsyncClusterLockfrom backend.util.exceptions import NotFoundError@@ -134,6 +135,7 @@ from ..transcript import (compact_transcript,download_transcript,extract_context_messages,+ next_uncovered_sequence,projects_base,read_compacted_entries,strip_for_upload,@@ -796,8 +798,7 @@ async def _consume_sdk_until_done(try:await asyncio.shield(upsert_chat_session(ctx.session))logger.debug(- "%s Intermediate flush: %d messages "- "(msgs_since=%d, elapsed=%.1fs)",+ "%s Intermediate flush: %d messages (msgs_since=%d, elapsed=%.1fs)",ctx.log_prefix,len(ctx.session.messages),loop_state.msgs_since_flush,@@ -2602,6 +2603,76 @@ async def _build_query_message()if use_resume and transcript_msg_count > 0:+ # Cap-engaged: the windowed ``prior`` doesn't start at absolute sequence+ # 0, so any index-based slice ``prior[transcript_msg_count:]`` is wrong+ # regardless of whether the watermark fits inside ``len(prior)``. Two+ # signals indicate cap engagement:+ # 1. transcript_msg_count >= len(prior) — watermark beyond the window+ # 2. prior[0].sequence > 0 — window starts above absolute sequence 0+ # Either condition means the index-based path silently mis-slices+ # (or returns []), so route through the sequence-based gap detection.+ # ``transcript_msg_count`` IS the next uncovered DB sequence, so+ # filtering ``sequence >= transcript_msg_count`` reads it directly.+ 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+ ]+ window_start_seq = (+ min(m.sequence for m in window_gap if m.sequence is not None)+ if window_gap+ else None+ )+ hole: list[ChatMessage] = []+ if window_start_seq is not None and window_start_seq > transcript_msg_count:+ try:+ hole_page = await chat_db().get_chat_messages_paginated(+ session_id,+ limit=window_start_seq - transcript_msg_count,+ after_sequence=transcript_msg_count,+ before_sequence=window_start_seq,+ )+ if hole_page:+ hole = [m for m in hole_page.messages if m.role != "reasoning"]+ except Exception as e:+ logger.warning(+ "[SDK] [%s] hole-fill DB fetch failed (range=[%d,%d)):"+ " %s — sending gap without hole",+ session_id[:8],+ transcript_msg_count,+ window_start_seq,+ e,+ )+ gap = hole + window_gap+ compressed, was_compressed = await _compress_messages(gap, target_tokens)+ gap_context = _format_conversation_context(compressed)+ if gap_context:+ logger.info(+ "[SDK] [%s] Cap-engaged stale watermark (%d) — sequence-"+ "based gap=%d msgs from windowed view (compressed=%s, "+ "context_bytes=%d)",+ session_id[:8],+ transcript_msg_count,+ len(gap),+ was_compressed,+ len(gap_context),+ )+ return (+ f"{gap_context}\n\nNow, the user says:\n{current_message}",+ was_compressed,+ )+ logger.warning(+ "[SDK] [%s] Cap-engaged + empty sequence-based gap: window may"+ " not contain post-watermark rows (transcript=%d, db=%d)",+ session_id[:8],+ transcript_msg_count,+ msg_count,+ )+ return current_message, Falseif 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@@ -3567,7 +3638,11 @@ async def _restore_cli_session_for_turn(# Build context from transcript content + gap, falling back to full DB.# extract_context_messages handles both: non-None baseline_download uses# the compacted transcript + gap; None falls back to all prior DB messages.- context_msgs = extract_context_messages(result.baseline_download, session.messages)+ context_msgs = await extract_context_messages(+ result.baseline_download,+ session.messages,+ session_id=session.session_id,+ )result.context_messages = context_msgsresult.transcript_msg_count = (result.baseline_download.message_count@@ -5060,27 +5135,20 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# (using the GCS watermark as the anchor for gap detection),# which makes len(session.messages) safe to use here.#- # Mid-turn follow-up user rows (persisted via the- # StreamToolOutputAvailable handler) are NOT part of the CLI- # JSONL — the CLI only knows them as embedded text inside a- # tool_result, and even that embedding can be stripped by- # the CLI's internal tool_result size cap. Deduct them- # from the watermark so detect_gap on the next turn- # treats them as gap-fill entries and the model sees them- # as real user messages instead of missing text.+ # Watermark = next uncovered DB sequence. Mid-turn+ # follow-up user rows (StreamToolOutputAvailable) are+ # persisted to DB but not to the CLI's JSONL, so we step+ # the watermark back by their count — detect_gap on the+ # next turn re-injects them as the model would otherwise+ # only see them embedded in the tool_result (and the CLI's+ # internal tool_result size cap can strip that embedding)._midturn_offset = (state.midturn_user_rows if state is not None else 0)- # ``role="reasoning"`` rows are persisted to session.messages- # for frontend replay but never appear in the CLI JSONL- # (extended_thinking lives embedded in assistant entries, not- # as standalone rows). Exclude them from the watermark so- # ``detect_gap`` on the next turn doesn't skip real- # user/assistant rows. See sentry comment 3106186683.- _non_reasoning_count = sum(- 1 for m in session.messages if m.role != "reasoning"+ _jsonl_covered = max(+ 0,+ next_uncovered_sequence(session.messages) - _midturn_offset,)- _jsonl_covered = _non_reasoning_count - _midturn_offsetawait asyncio.shield(upload_transcript(user_id=user_id,diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex 48b0ca3bea..174d237331 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -873,9 +873,11 @@ class TestRestoreCliSessionModeCheck:session_id="test-session",user_id="user-1",messages=[- ChatMessage(role="user", content="hello-unique-marker"),- ChatMessage(role="assistant", content="world-unique-marker"),- ChatMessage(role="user", content="follow up"),+ ChatMessage(role="user", content="hello-unique-marker", sequence=0),+ ChatMessage(+ role="assistant", content="world-unique-marker", sequence=1+ ),+ ChatMessage(role="user", content="follow up", sequence=2),],title="test",usage=[],@@ -963,9 +965,9 @@ class TestRestoreCliSessionModeCheck:session_id="test-session",user_id="user-1",messages=[- ChatMessage(role="user", content="hi"),- ChatMessage(role="assistant", content="hello"),- ChatMessage(role="user", content="follow up"),+ ChatMessage(role="user", content="hi", sequence=0),+ ChatMessage(role="assistant", content="hello", sequence=1),+ ChatMessage(role="user", content="follow up", sequence=2),],title="test",usage=[],@@ -1048,9 +1050,9 @@ class TestRestoreCliSessionModeCheck:session_id="test-session",user_id="user-1",messages=[- ChatMessage(role="user", content="DB_USER"),- ChatMessage(role="assistant", content="DB_ASSISTANT"),- ChatMessage(role="user", content="current turn"),+ ChatMessage(role="user", content="DB_USER", sequence=0),+ ChatMessage(role="assistant", content="DB_ASSISTANT", sequence=1),+ ChatMessage(role="user", content="current turn", sequence=2),],title="test",usage=[],@@ -1136,11 +1138,11 @@ class TestRestoreCliSessionModeCheck:session_id="test-session",user_id="user-1",messages=[- ChatMessage(role="user", content="DB_USER_0"),- ChatMessage(role="assistant", content="DB_ASSISTANT_1"),- ChatMessage(role="user", content="GAP_USER_2"),- ChatMessage(role="assistant", content="GAP_ASSISTANT_3"),- ChatMessage(role="user", content="current turn"),+ ChatMessage(role="user", content="DB_USER_0", sequence=0),+ ChatMessage(role="assistant", content="DB_ASSISTANT_1", sequence=1),+ ChatMessage(role="user", content="GAP_USER_2", sequence=2),+ ChatMessage(role="assistant", content="GAP_ASSISTANT_3", sequence=3),+ ChatMessage(role="user", content="current turn", sequence=4),],title="test",usage=[],diff --git a/autogpt_platform/backend/backend/copilot/transcript.py b/autogpt_platform/backend/backend/copilot/transcript.pyindex d89064de54..b2333ea858 100644--- a/autogpt_platform/backend/backend/copilot/transcript.py+++ b/autogpt_platform/backend/backend/copilot/transcript.py@@ -20,16 +20,16 @@ import shutilimport timefrom dataclasses import dataclassfrom pathlib import Path-from typing import TYPE_CHECKING, Literal+from typing import Literalfrom uuid import uuid4+from backend.data.db_accessors import chat_dbfrom backend.util import jsonfrom backend.util.clients import get_openai_clientfrom backend.util.prompt import CompressResult, compress_contextfrom backend.util.workspace_storage import GCSWorkspaceStorage, get_workspace_storage-if TYPE_CHECKING:- from .model import ChatMessage+from .model import ChatMessagelogger = logging.getLogger(__name__)@@ -817,109 +817,237 @@ async def download_transcript(return TranscriptDownload(content=content, message_count=message_count, mode=mode)+def next_uncovered_sequence(session_messages: list[ChatMessage]) -> int:+ """Return the watermark to upload alongside this transcript.++ Equals ``max(sequence) + 1`` over non-reasoning rows in the loaded+ window, or ``0`` when there are no eligible rows. The window always+ contains the most-recent rows (paginated tail-load), so this is correct+ even when the eager-load cap engaged on a long session — the max in the+ window is still the absolute max in DB.++ Reasoning rows are excluded because they live only in the DB for+ frontend replay; they never appear in the JSONL transcript.+ """+ seqs = [+ m.sequence+ for m in session_messages+ if m.role != "reasoning" and m.sequence is not None+ ]+ return max(seqs) + 1 if seqs else 0+++def _gap_by_index(+ session_messages: list[ChatMessage], watermark: int+) -> list[ChatMessage]:+ """Index-based gap fallback: ``session_messages`` is treated as the full+ history (list-index == absolute sequence). Used when no message in the+ candidate set carries a sequence — only test fixtures and the post-DB-+ failure cache state hit this in practice.+ """+ total = len(session_messages)+ if watermark >= total - 1:+ return []+ if session_messages[watermark - 1].role != "assistant":+ return []+ return list(session_messages[watermark : total - 1])+++def _gap_by_sequence(+ candidates: list[tuple[int, ChatMessage]],+ watermark: int,+) -> list[ChatMessage]:+ """Sequence-based gap with role-shape sanity check.++ ``watermark`` is the "next uncovered DB sequence" — the value persisted in+ the transcript's ``message_count`` field. Gap = candidates whose sequence+ is at or above the watermark.++ Returns [] when:+ - No post-watermark candidates (transcript is current).+ - The pre-watermark message immediately before the gap isn't an assistant+ turn (data shift / deletion).+ - The window starts above the watermark and the first gap message is a+ tool row that boundary expansion failed to pair with its owner+ (malformed window — would emit an orphan tool_result).+ """+ gap = [m for seq, m in candidates if seq >= watermark]+ pre_gap = [(seq, m) for seq, m in candidates if seq < watermark]+ if not gap:+ return []++ if pre_gap:+ _, prev = max(pre_gap, key=lambda item: item[0])+ if prev.role != "assistant":+ return []+ return gap++ # Window starts above the watermark — can't see the pre-gap row to verify+ # its role. ``get_chat_messages_paginated`` already does tool-pair boundary+ # expansion so ``gap[0]`` is normally user (fresh turn) or assistant (turn+ # that began before the cap). Reject only when it's still a tool row+ # (boundary expansion couldn't find the owner) — leaving that in would+ # emit an unmatched tool_result. The DB-side hole-fill prepends the+ # missing user turn that bridges the transcript and ``gap[0]``.+ if gap[0].role == "tool":+ return []+ window_start = min(seq for seq, _ in candidates if seq >= watermark)+ if window_start > watermark:+ logger.warning(+ "detect_gap: window starts at seq=%d but watermark is %d — "+ "DB-side hole-fill required",+ window_start,+ watermark,+ )+ return gap++def detect_gap(download: TranscriptDownload,session_messages: list[ChatMessage],) -> list[ChatMessage]:- """Return chat-db messages after the transcript watermark (excluding current user turn).-- Returns [] if transcript is current, watermark is zero, or the watermark- position doesn't end on an assistant turn (misaligned watermark).+ """Return chat-db messages at or after the transcript watermark+ (excluding the current user turn).++ ``download.message_count`` is the *next uncovered DB sequence* — the+ sequence the next message will receive. The gap is candidates with+ ``sequence >= watermark`` after dropping reasoning rows (those are+ persisted for frontend replay only and never sent to the model).++ Two paths:+ - **Sequence-based** (production): every prior message has a ``sequence``+ (NOT NULL in schema). Filter by ``sequence >= watermark`` with a+ role-shape sanity check at the boundary.+ - **Index fallback**: triggered when no prior message has a sequence —+ only test fixtures and the post-DB-failure cache state. Treats the list+ as the full history and slices by index.++ Hole-fill (sequences in DB but neither in transcript nor window) is+ performed by ``extract_context_messages``, not here — this function stays+ pure / sync."""if download.message_count == 0:return []- wm = download.message_count- total = len(session_messages)- if wm >= total - 1:+ session_messages = [m for m in session_messages if m.role != "reasoning"]+ if len(session_messages) < 2:+ return []++ candidates: list[tuple[int, ChatMessage]] = [+ (m.sequence, m) for m in session_messages[:-1] if m.sequence is not None+ ]+ if not candidates:+ return _gap_by_index(session_messages, download.message_count)+ return _gap_by_sequence(candidates, download.message_count)+++def _decode_transcript_content(content: bytes | str) -> str | None:+ """Return decoded transcript content, or ``None`` on UTF-8 errors."""+ if isinstance(content, str):+ return content+ try:+ return content.decode("utf-8")+ except UnicodeDecodeError:+ return None+++def _parse_transcript_to_messages(+ download: TranscriptDownload,+) -> list[ChatMessage] | None:+ """Decode + parse a transcript download into ``ChatMessage`` rows, or+ ``None`` if the download is empty / malformed."""+ if not download.content:+ return None+ content_str = _decode_transcript_content(download.content)+ if content_str is None:+ return None+ raw = _transcript_to_messages(content_str)+ if not raw:+ return None+ return [ChatMessage(role=m["role"], content=m.get("content") or "") for m in raw]+++async def fill_hole_between_transcript_and_gap(+ session_id: str,+ watermark: int,+ gap: list[ChatMessage],+) -> list[ChatMessage]:+ """Fetch DB rows in the range ``[watermark, gap[0].sequence)``.++ When the cap engaged and the transcript is stale, the windowed view+ starts above the watermark — sequences in between live in DB but+ neither in transcript nor in the loaded gap. Returns [] when there is+ no hole or the fetch fails (best-effort — partial context beats crash).+ Reasoning rows in the range are filtered.+ """+ if not gap or gap[0].sequence is None:return []- # Sanity: position wm-1 should be an assistant turn; misaligned watermark- # means the DB messages shifted (e.g. deletion) — skip gap to avoid wrong context.- # In normal operation ``message_count`` is always written after a complete- # user→assistant exchange (never mid-turn), so the last covered position is- # always assistant. This guard fires only on data corruption or message deletion.- if session_messages[wm - 1].role != "assistant":+ hole_end = gap[0].sequence+ if watermark >= hole_end:+ return []+ try:+ page = await chat_db().get_chat_messages_paginated(+ session_id,+ limit=hole_end - watermark,+ after_sequence=watermark,+ before_sequence=hole_end,+ )+ if page is None:+ return []+ return [m for m in page.messages if m.role != "reasoning"]+ except Exception as e:+ logger.error(+ "fill_hole_between_transcript_and_gap: fetch failed for "+ "session=%s range=[%d,%d): %s",+ session_id,+ watermark,+ hole_end,+ e,+ )return []- return list(session_messages[wm : total - 1])-def extract_context_messages(+async def extract_context_messages(download: TranscriptDownload | None,session_messages: "list[ChatMessage]",+ *,+ session_id: str | None = None,) -> "list[ChatMessage]":- """Return context messages for the current turn: transcript content + gap.-- This is the shared context primitive used by both the SDK path- (``use_resume=False`` → ``<conversation_history>`` injection) and the- baseline path (OpenAI messages array).-- How it works:-- - When a transcript exists, ``TranscriptBuilder.load_previous`` preserves- ``isCompactSummary=True`` compaction entries, so the returned messages- mirror the compacted context the CLI would see via ``--resume``.- - The gap (DB messages after the transcript watermark) is always small in- normal operation; it only grows during mode switches or when an upload- was missed.- - Falls back to full DB messages when no transcript exists (first turn,- upload failure, or GCS unavailable).- - Returns *prior* messages only (excluding the current user turn at- ``session_messages[-1]``). Callers that need the current turn append- ``session_messages[-1]`` themselves.- - **Tool calls from transcript entries are flattened to text**: assistant- messages derived from the JSONL use ``_flatten_assistant_content``, which- serialises ``tool_use`` blocks as human-readable text rather than- structured ``tool_calls``. Gap messages (from DB) preserve their- original ``tool_calls`` field. This is the same trade-off as the old- ``_compress_session_messages(session.messages)`` approach — no regression.-- Args:- download: The ``TranscriptDownload`` from GCS, or ``None`` when no- transcript is available. ``content`` may be either ``bytes`` or- ``str`` (the baseline path decodes + strips before returning).- session_messages: All messages in the session, with the current user- turn as the last element.-- Returns:- A list of ``ChatMessage`` objects covering the prior conversation- context, suitable for injection as conversation history.+ """Return context messages for the current turn: transcript + (hole-fill) + gap.++ Shared primitive used by both SDK (``use_resume=False`` →+ ``<conversation_history>`` injection) and baseline (OpenAI messages array)+ paths.++ Layered structure:+ - **Transcript**: GCS-cached compaction (preserves ``isCompactSummary=True``).+ Tool-use blocks are flattened to text via ``_flatten_assistant_content``.+ - **Hole-fill** (only when cap-engaged + stale-transcript): sequences+ ``[watermark, window_start)`` fetched directly from DB so the LLM never+ sees a mid-conversation hole. Requires ``session_id``.+ - **Gap**: DB messages from the loaded window with ``sequence >= watermark``,+ preserving structured ``tool_calls``.++ Falls back to full prior messages (``session_messages[:-1]``) when no+ transcript is available. Excludes ``role="reasoning"`` rows. The+ current user turn at ``session_messages[-1]`` is excluded — callers append+ it themselves."""- from .model import ChatMessage as _ChatMessage # runtime import-- # ``role="reasoning"`` rows are persisted for frontend replay of- # extended_thinking content but are NOT conversation context — the- # transcript-based --resume path already carries thinking separately,- # and sending them back to the model as user/assistant turns would be- # both redundant and malformed. Drop them before any gap detection- # or transcript comparison so ordering invariants still hold.session_messages = [m for m in session_messages if m.role != "reasoning"]-prior = session_messages[:-1]if download is None:return prior-- raw_content = download.content- if not raw_content:+ transcript_msgs = _parse_transcript_to_messages(download)+ if transcript_msgs is None:return prior- # Handle both bytes (raw GCS download) and str (pre-decoded baseline path).- if isinstance(raw_content, bytes):- try:- content_str: str = raw_content.decode("utf-8")- except UnicodeDecodeError:- return prior- else:- content_str = raw_content-- raw = _transcript_to_messages(content_str)- if not raw:- return prior-- transcript_msgs = [- _ChatMessage(role=m["role"], content=m.get("content") or "") for m in raw- ]gap = detect_gap(download, session_messages)- return transcript_msgs + gap+ hole: list[ChatMessage] = []+ if session_id and download.message_count > 0:+ hole = await fill_hole_between_transcript_and_gap(+ session_id, download.message_count, gap+ )+ return transcript_msgs + hole + gapasync def delete_transcript(user_id: str, session_id: str) -> None:diff --git a/autogpt_platform/backend/backend/copilot/transcript_test.py b/autogpt_platform/backend/backend/copilot/transcript_test.pyindex 96c7b3fc70..b46c4b9c41 100644--- a/autogpt_platform/backend/backend/copilot/transcript_test.py+++ b/autogpt_platform/backend/backend/copilot/transcript_test.py@@ -7,6 +7,8 @@ from __future__ import annotationsfrom unittest.mock import MagicMock+import pytest+from backend.util import jsonfrom .transcript import (@@ -1463,10 +1465,16 @@ class TestRestoreCliSession:def _msgs(*roles: str):- """Build a list of ChatMessage objects with the given roles."""+ """Build a list of ChatMessage objects with the given roles.++ Sequences are assigned 0..N-1 to mirror the production schema, where+ ``ChatMessage.sequence`` is NOT NULL on every persisted row.+ """from .model import ChatMessage- return [ChatMessage(role=r, content=f"{r}-{i}") for i, r in enumerate(roles)]+ return [+ ChatMessage(role=r, content=f"{r}-{i}", sequence=i) for i, r in enumerate(roles)+ ]class TestDetectGap:@@ -1536,6 +1544,87 @@ class TestDetectGap:assert len(gap) == 1assert gap[0].role == "assistant"+ def test_windowed_messages_use_sequence_filter(self):+ """When the loaded list is a window (not full history), the gap is+ derived from each message's own ``sequence`` field — not list-index.++ Simulates a session with 1000 total messages but only the most-recent+ 200 loaded; transcript watermark is 950 (transcript covers 0..949).+ Gap = sequences 950..998 (excluding the current user turn at 999).+ """+ from .model import ChatMessage++ # 200 messages with sequences 800..999. Last is current user turn.+ windowed: list[ChatMessage] = []+ for seq in range(800, 1000):+ role = "user" if seq % 2 == 0 else "assistant"+ windowed.append(+ ChatMessage(role=role, content=f"{role}-{seq}", sequence=seq)+ )+ dl = self._dl(950)+ gap = detect_gap(dl, windowed)+ assert len(gap) == 49 # 950..998+ assert gap[0].sequence == 950+ assert gap[-1].sequence == 998+ # Current turn (seq 999) not included.+ assert all(m.sequence != 999 for m in gap)++ def test_window_above_watermark_returns_gap_when_starts_with_user(self):+ """When the window starts above the watermark and the first gap+ message is ``user`` (well-formed boundary), the gap is returned."""+ from .model import ChatMessage++ # Window covers seq 1000..1199. Watermark at 800 (well below window).+ # gap[0] is at seq 1000, role 'user' (the next turn after the+ # assistant at wm-1=799).+ windowed: list[ChatMessage] = []+ for seq in range(1000, 1200):+ role = "user" if seq % 2 == 0 else "assistant" # 1000 → 'user'+ windowed.append(+ ChatMessage(role=role, content=f"{role}-{seq}", sequence=seq)+ )+ dl = self._dl(800)+ gap = detect_gap(dl, windowed)+ # Returns 199 messages (1000..1198, excluding current turn at 1199).+ assert len(gap) == 199+ assert gap[0].sequence == 1000+ assert gap[0].role == "user"++ def test_window_above_watermark_returned_when_starts_with_assistant(self):+ """When the window starts above the watermark and the first gap+ message is ``assistant``, the gap is still returned — boundary+ expansion in ``get_chat_messages_paginated`` may legitimately leave an+ assistant at position 0 (window started mid-turn), and the DB-side+ hole-fill prepends the user turn that bridges the transcript."""+ from .model import ChatMessage++ # Window starts at seq 1001 with 'assistant' first.+ windowed: list[ChatMessage] = []+ for seq in range(1001, 1200):+ role = "assistant" if seq % 2 == 1 else "user" # 1001 → 'assistant'+ windowed.append(+ ChatMessage(role=role, content=f"{role}-{seq}", sequence=seq)+ )+ dl = self._dl(800)+ gap = detect_gap(dl, windowed)+ # Returns 198 messages (1001..1198, excluding current turn at 1199).+ assert len(gap) == 198+ assert gap[0].sequence == 1001+ assert gap[0].role == "assistant"++ def test_window_above_watermark_skipped_when_starts_with_tool(self):+ """An unmatched tool row at gap[0] is malformed (boundary expansion+ couldn't find the owner) — reject the gap to avoid an orphan+ tool_result reaching the LLM."""+ from .model import ChatMessage++ windowed: list[ChatMessage] = [+ ChatMessage(role="tool", content="tool-1001", sequence=1001),+ ChatMessage(role="user", content="user-1002", sequence=1002),+ ]+ dl = self._dl(800)+ assert detect_gap(dl, windowed) == []+# ---------------------------------------------------------------------------# extract_context_messages@@ -1577,61 +1666,57 @@ def _make_valid_transcript(*roles: str) -> str:class TestExtractContextMessages:"""``extract_context_messages`` returns the shared context primitive."""- def test_none_download_returns_prior(self):+ @pytest.mark.asyncio+ async def test_none_download_returns_prior(self):"""No download → falls back to all session messages except current turn."""messages = _msgs("user", "assistant", "user")- result = extract_context_messages(None, messages)+ result = await extract_context_messages(None, messages)assert result == messages[:-1]assert len(result) == 2- def test_empty_content_download_returns_prior(self):+ @pytest.mark.asyncio+ async def test_empty_content_download_returns_prior(self):"""Empty bytes content → falls back to all prior messages."""dl = TranscriptDownload(content=b"", message_count=2, mode="sdk")messages = _msgs("user", "assistant", "user")- result = extract_context_messages(dl, messages)+ result = await extract_context_messages(dl, messages)assert result == messages[:-1]- def test_valid_transcript_no_gap_returns_transcript_messages(self):+ @pytest.mark.asyncio+ async def test_valid_transcript_no_gap_returns_transcript_messages(self):"""Transcript covers all prior turns → only transcript messages returned."""- # Transcript: [user, assistant] — 2 messages- # Session: [user, assistant, user(current)] — watermark=2 covers prefixtranscript_content = _make_valid_transcript("user", "assistant")dl = TranscriptDownload(content=transcript_content.encode("utf-8"), message_count=2, mode="sdk")messages = _msgs("user", "assistant", "user")- result = extract_context_messages(dl, messages)- # Transcript has 2 messages (user + assistant) and no gap+ result = await extract_context_messages(dl, messages)assert len(result) == 2assert result[0].role == "user"assert result[1].role == "assistant"- def test_valid_transcript_with_gap_returns_transcript_plus_gap(self):+ @pytest.mark.asyncio+ async def test_valid_transcript_with_gap_returns_transcript_plus_gap(self):"""Transcript is stale → gap messages appended after transcript content."""- # Transcript: [user, assistant] — watermark=2- # Session: [user, assistant, user, assistant, user(current)]- # Gap: [user(2), assistant(3)] — positions 2 and 3transcript_content = _make_valid_transcript("user", "assistant")dl = TranscriptDownload(content=transcript_content.encode("utf-8"), message_count=2, mode="sdk")messages = _msgs("user", "assistant", "user", "assistant", "user")- result = extract_context_messages(dl, messages)- # 2 transcript messages + 2 gap messages = 4+ result = await extract_context_messages(dl, messages)assert len(result) == 4- assert result[0].role == "user" # transcript user- assert result[1].role == "assistant" # transcript assistant- assert result[2].role == "user" # gap user- assert result[3].role == "assistant" # gap assistant+ assert result[0].role == "user"+ assert result[1].role == "assistant"+ assert result[2].role == "user"+ assert result[3].role == "assistant"- def test_compact_summary_entries_preserved(self):+ @pytest.mark.asyncio+ async def test_compact_summary_entries_preserved(self):"""``isCompactSummary=True`` entries survive ``_transcript_to_messages``."""import json as stdlib_jsonfrom .transcript import STOP_REASON_END_TURN- # Build a transcript where one entry is a compaction summary.- # isCompactSummary=True entries have type in STRIPPABLE_TYPES but are kept.compact_entry = stdlib_json.dumps({"type": "summary",@@ -1664,12 +1749,121 @@ class TestExtractContextMessages:content=content.encode("utf-8"), message_count=2, mode="sdk")messages = _msgs("user", "assistant", "user")- result = extract_context_messages(dl, messages)- # Both the compact summary and the assistant response are present+ result = await extract_context_messages(dl, messages)assert len(result) == 2roles = [m.role for m in result]- assert "user" in roles # compact summary has role=user+ assert "user" in rolesassert "assistant" in roles- # The compact summary content is preservedcompact_msgs = [m for m in result if m.role == "user"]assert any("COMPACT_SUMMARY_CONTENT" in (m.content or "") for m in compact_msgs)++ @pytest.mark.asyncio+ async def test_hole_filled_via_db_when_window_starts_above_watermark(self, mocker):+ """When the cap engages and the window starts above the watermark, the+ missing sequences must be fetched from DB and inserted between the+ transcript and the gap — never sent to the LLM as a hole."""+ from datetime import UTC, datetime++ from .db import PaginatedMessages+ from .model import ChatMessage, ChatSessionInfo++ # Transcript covers seq 0..1 (watermark=2 = next uncovered sequence).+ # Window holds the tail seq 5..7 + current turn. Missing seq 2..4+ # must be fetched.+ transcript_content = _make_valid_transcript("user", "assistant")+ dl = TranscriptDownload(+ content=transcript_content.encode("utf-8"), message_count=2, mode="sdk"+ )+ windowed = [+ ChatMessage(role="user", content="user-5", sequence=5),+ ChatMessage(role="assistant", content="assistant-6", sequence=6),+ ChatMessage(role="user", content="user-7-current", sequence=7),+ ]++ hole_page = PaginatedMessages(+ messages=[+ ChatMessage(role="user", content="hole-2", sequence=2),+ ChatMessage(role="assistant", content="hole-3", sequence=3),+ ChatMessage(role="user", content="hole-4", sequence=4),+ ],+ has_more=False,+ oldest_sequence=2,+ session=ChatSessionInfo(+ session_id="sess-hole",+ user_id="u1",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ ),+ )+ mock_db = mocker.MagicMock()+ mock_db.get_chat_messages_paginated = mocker.AsyncMock(return_value=hole_page)+ mocker.patch("backend.copilot.transcript.chat_db", return_value=mock_db)++ result = await extract_context_messages(dl, windowed, session_id="sess-hole")++ assert [m.role for m in result] == [+ "user", # transcript+ "assistant", # transcript+ "user", # hole-2+ "assistant", # hole-3+ "user", # hole-4+ "user", # gap user-5+ "assistant", # gap assistant-6+ ]+ # Range fetch uses the watermark directly — no count→sequence translation.+ mock_db.get_chat_messages_paginated.assert_awaited_once_with(+ "sess-hole", limit=3, after_sequence=2, before_sequence=5+ )++ @pytest.mark.asyncio+ async def test_hole_fill_filters_reasoning_rows_from_results(self, mocker):+ """The range fetch may still return reasoning rows that exist inside+ the hole — they must be stripped before the LLM context is assembled."""+ from datetime import UTC, datetime++ from .db import PaginatedMessages+ from .model import ChatMessage, ChatSessionInfo++ transcript_content = _make_valid_transcript("user", "assistant")+ dl = TranscriptDownload(+ content=transcript_content.encode("utf-8"), message_count=2, mode="sdk"+ )+ windowed = [+ ChatMessage(role="user", content="user-6", sequence=6),+ ChatMessage(role="assistant", content="assistant-7", sequence=7),+ ChatMessage(role="user", content="user-8-current", sequence=8),+ ]+ hole_page = PaginatedMessages(+ messages=[+ ChatMessage(role="user", content="hole-2", sequence=2),+ ChatMessage(role="reasoning", content="hole-thinking", sequence=3),+ ChatMessage(role="assistant", content="hole-4", sequence=4),+ ChatMessage(role="user", content="hole-5", sequence=5),+ ],+ has_more=False,+ oldest_sequence=2,+ session=ChatSessionInfo(+ session_id="sess-hole",+ user_id="u1",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ ),+ )+ mock_db = mocker.MagicMock()+ mock_db.get_chat_messages_paginated = mocker.AsyncMock(return_value=hole_page)+ mocker.patch("backend.copilot.transcript.chat_db", return_value=mock_db)++ result = await extract_context_messages(dl, windowed, session_id="sess-hole")++ # Reasoning row at seq 3 is dropped; the rest flows through.+ assert [m.role for m in result] == [+ "user", # transcript+ "assistant", # transcript+ "user", # hole-2+ "assistant", # hole-4 (hole-3 reasoning filtered)+ "user", # hole-5+ "user", # gap user-6+ "assistant", # gap assistant-7+ ]diff --git a/autogpt_platform/backend/backend/data/db_manager.py b/autogpt_platform/backend/backend/data/db_manager.pyindex 9f5da2aa9a..af2919a298 100644--- a/autogpt_platform/backend/backend/data/db_manager.py+++ b/autogpt_platform/backend/backend/data/db_manager.py@@ -379,8 +379,12 @@ class DatabaseManager(AppService):delete_user_link = _(platform_linking_db.delete_user_link)# ============ CoPilot Chat Sessions ============ #- get_chat_session = _(chat_db.get_chat_session)+ # NOTE: no eager-load `get_chat_session` here — callers go through+ # `get_chat_messages_paginated` (with `limit=MAX_LOADED_CHAT_MESSAGES`) so+ # tool-pair boundary expansion + visibility guarantees apply uniformly+ # across the LLM-context and UI scroll paths.get_chat_session_metadata = _(chat_db.get_chat_session_metadata)+ get_chat_messages_paginated = _(chat_db.get_chat_messages_paginated)create_chat_session = _(chat_db.create_chat_session)update_chat_session = _(chat_db.update_chat_session)add_chat_message = _(chat_db.add_chat_message)@@ -605,8 +609,8 @@ class DatabaseManagerAsyncClient(AppServiceClient):delete_user_link = d.delete_user_link# ============ CoPilot Chat Sessions ============ #- get_chat_session = d.get_chat_sessionget_chat_session_metadata = d.get_chat_session_metadata+ get_chat_messages_paginated = d.get_chat_messages_paginatedcreate_chat_session = d.create_chat_sessionupdate_chat_session = d.update_chat_sessionadd_chat_message = d.add_chat_messagediff --git a/autogpt_platform/backend/scripts/download_transcripts.py b/autogpt_platform/backend/scripts/download_transcripts.pyindex a9b32e8494..57e6e2ceba 100644--- a/autogpt_platform/backend/scripts/download_transcripts.py+++ b/autogpt_platform/backend/scripts/download_transcripts.py@@ -198,9 +198,12 @@ async def cmd_load(session_ids: list[str]) -> None:# Create chat session in DBtry:- from backend.copilot.db import create_chat_session, get_chat_session+ from backend.copilot.db import (+ create_chat_session,+ get_chat_session_metadata,+ )- existing = await get_chat_session(sid)+ existing = await get_chat_session_metadata(sid)if existing:print(f"[{sid[:12]}] Session already exists in DB, skipping creation")else:diff --git a/autogpt_platform/backend/test/copilot/test_transcript_watermark.py b/autogpt_platform/backend/test/copilot/test_transcript_watermark.pyindex bd88726339..d440a00ac6 100644--- a/autogpt_platform/backend/test/copilot/test_transcript_watermark.py+++ b/autogpt_platform/backend/test/copilot/test_transcript_watermark.py@@ -23,20 +23,28 @@ from backend.copilot.sdk.service import _build_query_messagedef _make_messages(n_pairs: int, *, current_user: str = "current") -> list[MagicMock]:"""Build a flat list of n_pairs*2 alternating user/asst messages, plus- one trailing user message for the *current* turn."""+ one trailing user message for the *current* turn.++ ``sequence`` is left ``None`` so the cap-engaged sequence-based path+ in ``_build_query_message`` (which short-circuits on ``sequence > 0``)+ doesn't fire — these tests target the legacy index-based gap branch.+ """msgs: list[MagicMock] = []for i in range(n_pairs):u = MagicMock()u.role = "user"u.content = f"user message {i}"+ u.sequence = Nonea = MagicMock()a.role = "assistant"a.content = f"assistant response {i}"+ a.sequence = Nonemsgs.extend([u, a])# Current turn's user messagecur = MagicMock()cur.role = "user"cur.content = current_user+ cur.sequence = Nonemsgs.append(cur)return msgs