AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): fix initial load missing messages + forward pagination for completed sessions (#12796)
Zamil Majdy87e4d4275081
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.pyindex 7496c214ac..b5b1d0d6fe 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py@@ -191,6 +191,8 @@ class SessionDetailResponse(BaseModel):active_stream: ActiveStreamInfo | None = None # Present if stream is still activehas_more_messages: bool = Falseoldest_sequence: int | None = None+ newest_sequence: int | None = None+ forward_paginated: bool = Falsetotal_prompt_tokens: int = 0total_completion_tokens: int = 0metadata: ChatSessionMetadata = ChatSessionMetadata()@@ -455,52 +457,113 @@ async def update_session_title_route(async def get_session(session_id: str,user_id: Annotated[str, Security(auth.get_user_id)],- limit: int = Query(default=50, ge=1, le=200),- before_sequence: int | None = Query(default=None, ge=0),+ limit: int = Query(+ default=50,+ ge=1,+ le=200,+ description="Maximum number of messages to return.",+ ),+ before_sequence: int | None = Query(+ default=None,+ ge=0,+ description=(+ "Backward pagination cursor. Return messages with sequence number "+ "strictly less than this value. Used by active-session load-more. "+ "Mutually exclusive with after_sequence."+ ),+ ),+ after_sequence: int | None = Query(+ default=None,+ ge=0,+ description=(+ "Forward pagination cursor. Return messages with sequence number "+ "strictly greater than this value. Used by completed-session load-more. "+ "Mutually exclusive with before_sequence."+ ),+ ),) -> SessionDetailResponse:"""Retrieve the details of a specific chat session.- Supports cursor-based pagination via ``limit`` and ``before_sequence``.- When no pagination params are provided, returns the most recent messages.+ Supports cursor-based pagination via ``limit``, ``before_sequence``, and+ ``after_sequence``. The two cursor parameters are mutually exclusive.- Args:- session_id: The unique identifier for the desired chat session.- user_id: The authenticated user's ID.- limit: Maximum number of messages to return (1-200, default 50).- before_sequence: Return messages with sequence < this value (cursor).-- Returns:- SessionDetailResponse: Details for the requested session, including- active_stream info and pagination metadata.+ On the initial load (no cursor provided) of a completed session, messages+ are returned in forward order starting from sequence 0 so the user always+ sees their initial prompt. Active sessions use the legacy newest-first+ order so streaming context is preserved."""+ if before_sequence is not None and after_sequence is not None:+ raise HTTPException(+ status_code=400,+ detail="before_sequence and after_sequence are mutually exclusive",+ )++ is_initial_load = before_sequence is None and after_sequence is None++ # Check active stream before the DB query on initial loads so we can+ # choose the correct pagination direction (forward for completed sessions,+ # newest-first for active ones).+ active_session = None+ last_message_id = None+ if is_initial_load:+ active_session, last_message_id = await stream_registry.get_active_session(+ session_id, user_id+ )++ # Completed sessions on initial load start from sequence 0 so the user's+ # initial prompt is always visible. Active sessions keep the legacy+ # newest-first behavior to preserve streaming context.+ from_start = is_initial_load and active_session is None+ forward_paginated = from_start or after_sequence is not None+page = await get_chat_messages_paginated(- session_id, limit, before_sequence, user_id=user_id+ session_id,+ limit,+ before_sequence=before_sequence,+ after_sequence=after_sequence,+ from_start=from_start,+ user_id=user_id,)if page is None:raise NotFoundError(f"Session {session_id} not found.")++ # Close the TOCTOU window: if the session was active at pre-check, re-verify+ # after the DB fetch. The session may have completed between the two awaits,+ # which would have caused messages to be fetched newest-first even though the+ # session is now complete. Re-fetch from seq 0 so the initial prompt is+ # always visible.+ if is_initial_load and active_session is not None:+ post_active, _ = await stream_registry.get_active_session(session_id, user_id)+ if post_active is None:+ active_session = None+ last_message_id = None+ from_start = True+ forward_paginated = True+ page = await get_chat_messages_paginated(+ session_id,+ limit,+ before_sequence=None,+ after_sequence=None,+ from_start=True,+ user_id=user_id,+ )+ if page is None:+ raise NotFoundError(f"Session {session_id} not found.")+messages = [_strip_injected_context(message.model_dump()) for message in page.messages]- # Only check active stream on initial load (not on "load more" requests)active_stream_info = None- if before_sequence is None:- active_session, last_message_id = await stream_registry.get_active_session(- session_id, user_id+ if active_session and last_message_id is not None:+ active_stream_info = ActiveStreamInfo(+ turn_id=active_session.turn_id,+ last_message_id=last_message_id,)- logger.info(- f"[GET_SESSION] session={session_id}, active_session={active_session is not None}, "- f"msg_count={len(messages)}, last_role={messages[-1].get('role') if messages else 'none'}"- )- if active_session:- active_stream_info = ActiveStreamInfo(- turn_id=active_session.turn_id,- last_message_id=last_message_id,- )# Skip session metadata on "load more" — frontend only needs messages- if before_sequence is not None:+ if not is_initial_load:return SessionDetailResponse(id=page.session.session_id,created_at=page.session.started_at.isoformat(),@@ -510,6 +573,8 @@ async def get_session(active_stream=None,has_more_messages=page.has_more,oldest_sequence=page.oldest_sequence,+ newest_sequence=page.newest_sequence,+ forward_paginated=forward_paginated,total_prompt_tokens=0,total_completion_tokens=0,)@@ -526,6 +591,8 @@ async def get_session(active_stream=active_stream_info,has_more_messages=page.has_more,oldest_sequence=page.oldest_sequence,+ newest_sequence=page.newest_sequence,+ forward_paginated=forward_paginated,total_prompt_tokens=total_prompt,total_completion_tokens=total_completion,metadata=page.session.metadata,diff --git a/autogpt_platform/backend/backend/api/features/chat/routes_test.py b/autogpt_platform/backend/backend/api/features/chat/routes_test.pyindex 597aad01ad..8d34832c82 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes_test.py@@ -980,3 +980,146 @@ def test_disconnect_stream_returns_404_when_session_missing(assert response.status_code == 404mock_disconnect.assert_not_awaited()+++# ─── GET /sessions/{session_id} — forward/backward pagination ──────────────────+++def _make_paginated_messages(+ mocker: pytest_mock.MockerFixture, *, has_more: bool = False+):+ """Return a mock PaginatedMessages and configure the DB patch."""+ from datetime import UTC, datetime++ from backend.copilot.db import PaginatedMessages+ from backend.copilot.model import ChatMessage, ChatSessionInfo, ChatSessionMetadata++ now = datetime.now(UTC)+ session_info = ChatSessionInfo(+ session_id="sess-1",+ user_id=TEST_USER_ID,+ usage=[],+ started_at=now,+ updated_at=now,+ metadata=ChatSessionMetadata(),+ )+ page = PaginatedMessages(+ messages=[ChatMessage(role="user", content="hello", sequence=0)],+ has_more=has_more,+ oldest_sequence=0,+ newest_sequence=0,+ session=session_info,+ )+ mock_paginate = mocker.patch(+ "backend.api.features.chat.routes.get_chat_messages_paginated",+ new_callable=AsyncMock,+ return_value=page,+ )+ return page, mock_paginate+++def test_get_session_completed_returns_forward_paginated(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ """Completed sessions (no active stream) return forward_paginated=True."""+ _make_paginated_messages(mocker)+ mocker.patch(+ "backend.api.features.chat.routes.stream_registry.get_active_session",+ new_callable=AsyncMock,+ return_value=(None, None),+ )++ response = client.get("/sessions/sess-1")++ assert response.status_code == 200+ data = response.json()+ assert data["forward_paginated"] is True+ assert data["newest_sequence"] == 0+++def test_get_session_active_returns_backward_paginated(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ """Active sessions (with running stream) return forward_paginated=False."""+ from backend.copilot.stream_registry import ActiveSession++ _make_paginated_messages(mocker)+ active = MagicMock(spec=ActiveSession)+ active.turn_id = "turn-1"+ mocker.patch(+ "backend.api.features.chat.routes.stream_registry.get_active_session",+ new_callable=AsyncMock,+ return_value=(active, "msg-1"),+ )++ response = client.get("/sessions/sess-1")++ assert response.status_code == 200+ data = response.json()+ assert data["forward_paginated"] is False+ assert data["active_stream"] is not None+ assert data["active_stream"]["turn_id"] == "turn-1"+++def test_get_session_after_sequence_returns_forward_paginated(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ """after_sequence param returns forward_paginated=True; no stream check needed."""+ _, mock_paginate = _make_paginated_messages(mocker)++ response = client.get("/sessions/sess-1?after_sequence=10")++ assert response.status_code == 200+ data = response.json()+ assert data["forward_paginated"] is True+ call_kwargs = mock_paginate.call_args+ assert call_kwargs.kwargs.get("after_sequence") == 10+ assert call_kwargs.kwargs.get("before_sequence") is None+++def test_get_session_both_cursors_returns_400(+ test_user_id: str,+) -> None:+ """Sending both before_sequence and after_sequence returns 400."""+ response = client.get("/sessions/sess-1?before_sequence=5&after_sequence=10")++ assert response.status_code == 400+++def test_get_session_toctou_refetch_when_session_completes_mid_request(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ """Race condition: session was active at pre-check but completes before DB fetch.++ The route should detect the race via a post-fetch re-check, then re-fetch+ from seq 0 so the initial prompt is always visible.+ """+ from backend.copilot.stream_registry import ActiveSession++ page, mock_paginate = _make_paginated_messages(mocker)+ active = MagicMock(spec=ActiveSession)+ active.turn_id = "turn-1"++ # First call: session appears active. Second call: session has completed.+ mock_get_active = mocker.patch(+ "backend.api.features.chat.routes.stream_registry.get_active_session",+ new_callable=AsyncMock,+ side_effect=[(active, "msg-1"), (None, None)],+ )++ response = client.get("/sessions/sess-1")++ assert response.status_code == 200+ data = response.json()+ # Post-race: session is now completed → forward_paginated=True, no stream+ assert data["forward_paginated"] is True+ assert data["active_stream"] is None+ # The DB was queried twice: once newest-first, once from_start=True+ assert mock_paginate.call_count == 2+ assert mock_get_active.call_count == 2+ second_call = mock_paginate.call_args_list[1]+ assert second_call.kwargs.get("from_start") is Truediff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.pyindex b85e08606c..bc4964ec35 100644--- a/autogpt_platform/backend/backend/copilot/db.py+++ b/autogpt_platform/backend/backend/copilot/db.py@@ -10,9 +10,11 @@ from prisma.models import ChatMessage as PrismaChatMessagefrom prisma.models import ChatSession as PrismaChatSessionfrom prisma.types import (ChatMessageCreateInput,+ ChatMessageWhereInput,ChatSessionCreateInput,ChatSessionUpdateInput,ChatSessionWhereInput,+ FindManyChatMessageArgsFromChatSession,)from pydantic import BaseModel@@ -30,6 +32,8 @@ from .model import get_chat_session as get_chat_session_cachedlogger = logging.getLogger(__name__)+_BOUNDARY_SCAN_LIMIT = 10+class PaginatedMessages(BaseModel):"""Result of a paginated message query."""@@ -37,6 +41,7 @@ class PaginatedMessages(BaseModel):messages: list[ChatMessage]has_more: boololdest_sequence: int | None+ newest_sequence: int | Nonesession: ChatSessionInfo@@ -61,32 +66,48 @@ async def get_chat_messages_paginated(session_id: str,limit: int = 50,before_sequence: int | None = None,+ after_sequence: int | None = None,+ from_start: bool = False,user_id: str | None = None,) -> PaginatedMessages | None:- """Get paginated messages for a session, newest first.-- Verifies session existence (and ownership when ``user_id`` is provided)- in parallel with the message query. Returns ``None`` when the session- is not found or does not belong to the user.-- Args:- session_id: The chat session ID.- limit: Max messages to return.- before_sequence: Cursor — return messages with sequence < this value.- user_id: If provided, filters via ``Session.userId`` so only the- session owner's messages are returned (acts as an ownership guard).+ """Get paginated messages for a session.++ Three modes:++ - ``before_sequence`` set: backward pagination (DESC), returns messages+ with sequence < ``before_sequence``. Used for active sessions or manual+ backward navigation.+ - ``from_start=True`` or ``after_sequence`` set: forward pagination (ASC).+ Returns messages from sequence 0 (``from_start``) or after+ ``after_sequence``. Used on initial load of completed sessions and for+ loading subsequent forward pages.+ - Both cursors ``None`` and ``from_start=False``: newest-first (DESC+ without filter). Used for active sessions on initial load.++ Verifies session existence (and ownership when ``user_id`` is provided).+ Returns ``None`` when the session is not found or does not belong to the+ user."""# 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_include: dict[str, Any] = {- "order_by": {"sequence": "desc"},+ forward = from_start or after_sequence is not None++ # Build message include — fetch paginated messages in the same query.+ # Note: when both from_start=True and after_sequence is not None, the+ # after_sequence filter takes precedence (the elif branch below is skipped).+ # This combination is not reachable via the HTTP route (mutual exclusion is+ # enforced there), so we rely on the documented priority here without an+ # additional assertion.+ msg_include: FindManyChatMessageArgsFromChatSession = {+ "order_by": {"sequence": "asc" if forward else "desc"},"take": limit + 1,}- if before_sequence is not None:+ if after_sequence is not None:+ msg_include["where"] = {"sequence": {"gt": after_sequence}}+ elif before_sequence is not None:msg_include["where"] = {"sequence": {"lt": before_sequence}}# Single query: session existence/ownership + paginated messages@@ -104,57 +125,96 @@ async def get_chat_messages_paginated(has_more = len(results) > limitresults = results[:limit]- # Reverse to ascending order- 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.- _BOUNDARY_SCAN_LIMIT = 10- if results and results[0].role == "tool":- boundary_where: dict[str, Any] = {- "sessionId": session_id,- "sequence": {"lt": results[0].sequence},- }- if user_id is not None:- boundary_where["Session"] = {"is": {"userId": user_id}}- extra = await PrismaChatMessage.prisma().find_many(- where=boundary_where,- order={"sequence": "desc"},- take=_BOUNDARY_SCAN_LIMIT,- )- # Find the first non-tool message (should be the assistant)- boundary_msgs = []- found_owner = False- for msg in extra:- boundary_msgs.append(msg)- if msg.role != "tool":- found_owner = True- break- boundary_msgs.reverse()- if not found_owner:- logger.warning(- "Boundary expansion did not find owning assistant message "- "for session=%s before sequence=%s (%d msgs scanned)",- session_id,- results[0].sequence,- len(extra),+ if not forward:+ # Backward mode: DB returned DESC; reverse to ascending order.+ 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":+ boundary_where: ChatMessageWhereInput = {+ "sessionId": session_id,+ "sequence": {"lt": results[0].sequence},+ }+ if user_id is not None:+ boundary_where["Session"] = {"is": {"userId": user_id}}+ extra = await PrismaChatMessage.prisma().find_many(+ where=boundary_where,+ order={"sequence": "desc"},+ take=_BOUNDARY_SCAN_LIMIT,)- if boundary_msgs:- results = boundary_msgs + results- # Only mark has_more if the expanded boundary isn't the- # very start of the conversation (sequence 0).- if boundary_msgs[0].sequence > 0:+ # Find the first non-tool message (should be the assistant)+ boundary_msgs = []+ found_owner = False+ for msg in extra:+ boundary_msgs.append(msg)+ if msg.role != "tool":+ found_owner = True+ break+ boundary_msgs.reverse()+ if not found_owner:+ logger.warning(+ "Boundary expansion did not find owning assistant message "+ "for session=%s before sequence=%s (%d msgs scanned)",+ session_id,+ results[0].sequence,+ len(extra),+ )+ if boundary_msgs:+ results = boundary_msgs + results+ # Only mark has_more if the expanded boundary isn't the+ # very start of the conversation (sequence 0).+ if boundary_msgs[0].sequence > 0:+ has_more = True+ else:+ # Forward mode: DB returned ASC.+ # Tool-call tail boundary fix: if the last message in this page is a+ # tool message, the NEXT forward page would start after it and begin+ # mid-tool-group — the owning assistant message is on this page but+ # the following tool results are on the next page.+ # Trim the current page so it ends on the owning assistant message,+ # which keeps tool groups intact across page boundaries.+ if results and results[-1].role == "tool":+ # Walk backward through results to find the last non-tool message.+ trim_idx = len(results) - 1+ while trim_idx >= 0 and results[trim_idx].role == "tool":+ trim_idx -= 1++ if trim_idx >= 0:+ # Trim results so the page ends at the owning assistant.+ # Mark has_more=True so the client knows to fetch the rest.+ results = results[: trim_idx + 1]has_more = True+ else:+ # Entire page is tool messages with no visible owner — log and+ # keep as-is so the caller is not stuck with an empty page.+ logger.warning(+ "Forward tail boundary: entire page is tool messages "+ "for session=%s, no owning assistant found (%d msgs)",+ session_id,+ len(results),+ )messages = [ChatMessage.from_db(m) for m in results]- oldest_sequence = messages[0].sequence if messages else None+ # oldest_sequence is only meaningful in backward mode (used as backward+ # pagination cursor). In forward mode the page always starts near seq 0+ # and clients should use newest_sequence as the forward cursor instead.+ # Return None in forward mode so clients don't accidentally treat it as a+ # backward cursor on a forward-paginated session.+ oldest_sequence = messages[0].sequence if (messages and not forward) else None+ # newest_sequence is only meaningful in forward mode; in backward mode it+ # points to the last message of the page (not the session's newest message)+ # which is not a valid forward cursor. Return None in backward mode so+ # clients don't accidentally use it as one.+ newest_sequence = messages[-1].sequence if (messages and forward) else Nonereturn PaginatedMessages(messages=messages,has_more=has_more,oldest_sequence=oldest_sequence,+ newest_sequence=newest_sequence,session=session_info,)diff --git a/autogpt_platform/backend/backend/copilot/db_test.py b/autogpt_platform/backend/backend/copilot/db_test.pyindex a2eb050bc4..f9e7ad515f 100644--- a/autogpt_platform/backend/backend/copilot/db_test.py+++ b/autogpt_platform/backend/backend/copilot/db_test.py@@ -175,6 +175,187 @@ async def test_no_where_on_messages_without_before_sequence(assert "where" not in include["Messages"]+# ---------- Forward pagination (from_start / after_sequence) ----------+++async def test_from_start_uses_asc_order_no_where(+ mock_db: tuple[AsyncMock, AsyncMock],+):+ """from_start=True queries messages in ASC order with no where filter."""+ find_first, _ = mock_db+ find_first.return_value = _make_session(+ messages=[_make_msg(0), _make_msg(1), _make_msg(2)],+ )++ await get_chat_messages_paginated(SESSION_ID, limit=50, from_start=True)++ call_kwargs = find_first.call_args+ include = call_kwargs.kwargs.get("include") or call_kwargs[1].get("include")+ assert include["Messages"]["order_by"] == {"sequence": "asc"}+ assert "where" not in include["Messages"]+++async def test_from_start_returns_messages_ascending(+ mock_db: tuple[AsyncMock, AsyncMock],+):+ """from_start=True returns messages in ascending sequence order."""+ find_first, _ = mock_db+ find_first.return_value = _make_session(+ messages=[_make_msg(0), _make_msg(1), _make_msg(2)],+ )++ page = await get_chat_messages_paginated(SESSION_ID, limit=50, from_start=True)++ assert page is not None+ assert [m.sequence for m in page.messages] == [0, 1, 2]+ assert (+ page.oldest_sequence is None+ ) # None in forward mode — not a valid backward cursor+ assert page.newest_sequence == 2+ assert page.has_more is False+++async def test_from_start_has_more_when_results_exceed_limit(+ mock_db: tuple[AsyncMock, AsyncMock],+):+ """from_start=True sets has_more when DB returns more than limit items."""+ find_first, _ = mock_db+ find_first.return_value = _make_session(+ messages=[_make_msg(0), _make_msg(1), _make_msg(2)],+ )++ page = await get_chat_messages_paginated(SESSION_ID, limit=2, from_start=True)++ assert page is not None+ assert page.has_more is True+ assert [m.sequence for m in page.messages] == [0, 1]+ assert page.newest_sequence == 1+++async def test_after_sequence_uses_gt_filter_asc_order(+ mock_db: tuple[AsyncMock, AsyncMock],+):+ """after_sequence adds a sequence > N where clause and uses ASC order."""+ find_first, _ = mock_db+ find_first.return_value = _make_session(+ messages=[_make_msg(11), _make_msg(12)],+ )++ await get_chat_messages_paginated(SESSION_ID, limit=50, after_sequence=10)++ call_kwargs = find_first.call_args+ include = call_kwargs.kwargs.get("include") or call_kwargs[1].get("include")+ assert include["Messages"]["order_by"] == {"sequence": "asc"}+ assert include["Messages"]["where"] == {"sequence": {"gt": 10}}+++async def test_after_sequence_returns_messages_in_order(+ mock_db: tuple[AsyncMock, AsyncMock],+):+ """after_sequence returns only messages with sequence > cursor, ascending."""+ find_first, _ = mock_db+ find_first.return_value = _make_session(+ messages=[_make_msg(11), _make_msg(12), _make_msg(13)],+ )++ page = await get_chat_messages_paginated(SESSION_ID, limit=50, after_sequence=10)++ assert page is not None+ assert [m.sequence for m in page.messages] == [11, 12, 13]+ assert (+ page.oldest_sequence is None+ ) # None in forward mode — not a valid backward cursor+ assert page.newest_sequence == 13+ assert page.has_more is False+++async def test_newest_sequence_none_for_backward_mode(+ mock_db: tuple[AsyncMock, AsyncMock],+):+ """newest_sequence is None in backward mode — it is not a valid forward cursor."""+ find_first, _ = mock_db+ find_first.return_value = _make_session(+ messages=[_make_msg(5), _make_msg(4), _make_msg(3)],+ )++ page = await get_chat_messages_paginated(SESSION_ID, limit=50)++ assert page is not None+ assert page.newest_sequence is None+ assert page.oldest_sequence == 3+++async def test_forward_mode_no_boundary_expansion(+ mock_db: tuple[AsyncMock, AsyncMock],+):+ """Forward pagination never triggers backward boundary expansion."""+ find_first, find_many = mock_db+ find_first.return_value = _make_session(+ messages=[_make_msg(0, role="tool"), _make_msg(1, role="tool")],+ )++ await get_chat_messages_paginated(SESSION_ID, limit=50, from_start=True)++ assert find_many.call_count == 0+++async def test_forward_tail_boundary_trims_trailing_tool_messages(+ mock_db: tuple[AsyncMock, AsyncMock],+):+ """Forward pages that end with tool messages are trimmed to the owning+ assistant so the next after_sequence page doesn't start mid-tool-group."""+ find_first, _ = mock_db+ # DB returns 4 messages ASC: assistant at 0, tool at 1, tool at 2, tool at 3+ find_first.return_value = _make_session(+ messages=[+ _make_msg(0, role="assistant"),+ _make_msg(1, role="tool"),+ _make_msg(2, role="tool"),+ _make_msg(3, role="tool"),+ ],+ )++ page = await get_chat_messages_paginated(SESSION_ID, limit=10, from_start=True)++ assert page is not None+ # Page should be trimmed to end at the assistant message+ assert [m.sequence for m in page.messages] == [0]+ assert page.newest_sequence == 0+ # has_more must be True so the client fetches the tool messages on next page+ assert page.has_more is True+++async def test_forward_tail_boundary_no_trim_when_last_not_tool(+ mock_db: tuple[AsyncMock, AsyncMock],+):+ """Forward pages that end with a non-tool message are not trimmed."""+ find_first, _ = mock_db+ find_first.return_value = _make_session(+ messages=[+ _make_msg(0, role="user"),+ _make_msg(1, role="assistant"),+ _make_msg(2, role="tool"),+ _make_msg(3, role="assistant"),+ ],+ )++ page = await get_chat_messages_paginated(SESSION_ID, limit=10, from_start=True)++ assert page is not None+ assert [m.sequence for m in page.messages] == [0, 1, 2, 3]+ assert page.newest_sequence == 3+ assert page.has_more is False++@pytest.mark.asyncioasync def test_user_id_filter_applied_to_session_where(mock_db: tuple[AsyncMock, AsyncMock],diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsxindex 88f70c75d8..62255037eb 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx@@ -93,6 +93,7 @@ export function CopilotPage() {hasMoreMessages,isLoadingMore,loadMore,+ forwardPaginated,// Mobile drawerisMobile,isDrawerOpen,@@ -217,6 +218,7 @@ export function CopilotPage() {hasMoreMessages={hasMoreMessages}isLoadingMore={isLoadingMore}onLoadMore={loadMore}+ forwardPaginated={forwardPaginated}droppedFiles={droppedFiles}onDroppedFilesConsumed={handleDroppedFilesConsumed}historicalDurations={historicalDurations}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useChatSession.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useChatSession.test.tsnew file mode 100644index 0000000000..a6d8c5e896--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useChatSession.test.ts@@ -0,0 +1,122 @@+import { renderHook } from "@testing-library/react";+import { beforeEach, describe, expect, it, vi } from "vitest";+import { useChatSession } from "../useChatSession";++const mockUseGetV2GetSession = vi.fn();++vi.mock("@/app/api/__generated__/endpoints/chat/chat", () => ({+ useGetV2GetSession: (...args: unknown[]) => mockUseGetV2GetSession(...args),+ usePostV2CreateSession: () => ({ mutateAsync: vi.fn(), isPending: false }),+ getGetV2GetSessionQueryKey: (id: string) => ["session", id],+ getGetV2ListSessionsQueryKey: () => ["sessions"],+}));++vi.mock("@tanstack/react-query", () => ({+ useQueryClient: () => ({+ invalidateQueries: vi.fn(),+ setQueryData: vi.fn(),+ }),+}));++vi.mock("nuqs", () => ({+ parseAsString: { withDefault: (v: unknown) => v },+ useQueryState: () => ["sess-1", vi.fn()],+}));++vi.mock("../helpers/convertChatSessionToUiMessages", () => ({+ convertChatSessionMessagesToUiMessages: vi.fn(() => ({+ messages: [],+ historicalDurations: new Map(),+ })),+}));++vi.mock("../helpers", () => ({+ resolveSessionDryRun: vi.fn(() => false),+}));++vi.mock("@sentry/nextjs", () => ({+ captureException: vi.fn(),+}));++function makeQueryResult(data: object | null) {+ return {+ data: data ? { status: 200, data } : undefined,+ isLoading: false,+ isError: false,+ isFetching: false,+ refetch: vi.fn(),+ };+}++describe("useChatSession — newestSequence and forwardPaginated", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it("returns null / false when no session data", () => {+ mockUseGetV2GetSession.mockReturnValue(makeQueryResult(null));+ const { result } = renderHook(() => useChatSession());+ expect(result.current.newestSequence).toBeNull();+ expect(result.current.forwardPaginated).toBe(false);+ });++ it("returns newestSequence from session data", () => {+ mockUseGetV2GetSession.mockReturnValue(+ makeQueryResult({+ messages: [],+ has_more_messages: true,+ oldest_sequence: 0,+ newest_sequence: 99,+ forward_paginated: false,+ active_stream: null,+ }),+ );+ const { result } = renderHook(() => useChatSession());+ expect(result.current.newestSequence).toBe(99);+ });++ it("returns null for newestSequence when field is missing", () => {+ mockUseGetV2GetSession.mockReturnValue(+ makeQueryResult({+ messages: [],+ has_more_messages: false,+ oldest_sequence: 0,+ newest_sequence: null,+ forward_paginated: false,+ active_stream: null,+ }),+ );+ const { result } = renderHook(() => useChatSession());+ expect(result.current.newestSequence).toBeNull();+ });++ it("returns forwardPaginated=true when session is forward-paginated", () => {+ mockUseGetV2GetSession.mockReturnValue(+ makeQueryResult({+ messages: [],+ has_more_messages: true,+ oldest_sequence: 0,+ newest_sequence: 49,+ forward_paginated: true,+ active_stream: null,+ }),+ );+ const { result } = renderHook(() => useChatSession());+ expect(result.current.forwardPaginated).toBe(true);+ });++ it("returns forwardPaginated=false when session is backward-paginated", () => {+ mockUseGetV2GetSession.mockReturnValue(+ makeQueryResult({+ messages: [],+ has_more_messages: true,+ oldest_sequence: 50,+ newest_sequence: 99,+ forward_paginated: false,+ active_stream: null,+ }),+ );+ const { result } = renderHook(() => useChatSession());+ expect(result.current.forwardPaginated).toBe(false);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPage.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPage.test.tsnew file mode 100644index 0000000000..cd23a51195--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotPage.test.ts@@ -0,0 +1,202 @@+import { act, renderHook, waitFor } from "@testing-library/react";+import { beforeEach, describe, expect, it, vi } from "vitest";+import { useCopilotPage } from "../useCopilotPage";++const mockUseChatSession = vi.fn();+const mockUseCopilotStream = vi.fn();+const mockUseLoadMoreMessages = vi.fn();++vi.mock("../useChatSession", () => ({+ useChatSession: (...args: unknown[]) => mockUseChatSession(...args),+}));+vi.mock("../useCopilotStream", () => ({+ useCopilotStream: (...args: unknown[]) => mockUseCopilotStream(...args),+}));+vi.mock("../useLoadMoreMessages", () => ({+ useLoadMoreMessages: (...args: unknown[]) => mockUseLoadMoreMessages(...args),+}));+vi.mock("../useCopilotNotifications", () => ({+ useCopilotNotifications: () => undefined,+}));+vi.mock("../useWorkflowImportAutoSubmit", () => ({+ useWorkflowImportAutoSubmit: () => undefined,+}));+vi.mock("../store", () => ({+ useCopilotUIStore: () => ({+ sessionToDelete: null,+ setSessionToDelete: vi.fn(),+ isDrawerOpen: false,+ setDrawerOpen: vi.fn(),+ copilotChatMode: "chat",+ copilotLlmModel: null,+ isDryRun: false,+ }),+}));+vi.mock("../helpers/convertChatSessionToUiMessages", () => ({+ concatWithAssistantMerge: (a: unknown[], b: unknown[]) => [...a, ...b],+}));+vi.mock("@/app/api/__generated__/endpoints/chat/chat", () => ({+ useDeleteV2DeleteSession: () => ({ mutate: vi.fn(), isPending: false }),+ useGetV2ListSessions: () => ({ data: undefined, isLoading: false }),+ getGetV2ListSessionsQueryKey: () => ["sessions"],+}));+vi.mock("@/components/molecules/Toast/use-toast", () => ({+ toast: vi.fn(),+}));+vi.mock("@/lib/direct-upload", () => ({+ uploadFileDirect: vi.fn(),+}));+vi.mock("@/lib/hooks/useBreakpoint", () => ({+ useBreakpoint: () => "lg",+}));+vi.mock("@/lib/supabase/hooks/useSupabase", () => ({+ useSupabase: () => ({ isUserLoading: false, isLoggedIn: true }),+}));+vi.mock("@tanstack/react-query", () => ({+ useQueryClient: () => ({ invalidateQueries: vi.fn() }),+}));+vi.mock("@/services/feature-flags/use-get-flag", () => ({+ Flag: { CHAT_MODE_OPTION: "CHAT_MODE_OPTION" },+ useGetFlag: () => false,+}));++function makeBaseChatSession(overrides: Record<string, unknown> = {}) {+ return {+ sessionId: "sess-1",+ setSessionId: vi.fn(),+ hydratedMessages: [],+ rawSessionMessages: [],+ historicalDurations: new Map(),+ hasActiveStream: false,+ hasMoreMessages: false,+ oldestSequence: null,+ newestSequence: null,+ forwardPaginated: false,+ isLoadingSession: false,+ isSessionError: false,+ createSession: vi.fn(),+ isCreatingSession: false,+ refetchSession: vi.fn(),+ sessionDryRun: false,+ ...overrides,+ };+}++function makeBaseCopilotStream(overrides: Record<string, unknown> = {}) {+ return {+ messages: [],+ sendMessage: vi.fn(),+ stop: vi.fn(),+ status: "ready",+ error: undefined,+ isReconnecting: false,+ isSyncing: false,+ isUserStoppingRef: { current: false },+ rateLimitMessage: null,+ dismissRateLimit: vi.fn(),+ ...overrides,+ };+}++function makeBaseLoadMore(overrides: Record<string, unknown> = {}) {+ return {+ pagedMessages: [],+ hasMore: false,+ isLoadingMore: false,+ loadMore: vi.fn(),+ resetPaged: vi.fn(),+ ...overrides,+ };+}++describe("useCopilotPage — forwardPaginated message ordering", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it("prepends pagedMessages before currentMessages when forwardPaginated=false", () => {+ const pagedMsg = { id: "paged", role: "user" };+ const currentMsg = { id: "current", role: "assistant" };+ mockUseChatSession.mockReturnValue(+ makeBaseChatSession({ forwardPaginated: false }),+ );+ mockUseCopilotStream.mockReturnValue(+ makeBaseCopilotStream({ messages: [currentMsg] }),+ );+ mockUseLoadMoreMessages.mockReturnValue(+ makeBaseLoadMore({ pagedMessages: [pagedMsg] }),+ );++ const { result } = renderHook(() => useCopilotPage());++ // Backward: pagedMessages (older) come first+ expect(result.current.messages[0]).toEqual(pagedMsg);+ expect(result.current.messages[1]).toEqual(currentMsg);+ });++ it("appends pagedMessages after currentMessages when forwardPaginated=true", () => {+ const pagedMsg = { id: "paged", role: "assistant" };+ const currentMsg = { id: "current", role: "user" };+ mockUseChatSession.mockReturnValue(+ makeBaseChatSession({ forwardPaginated: true }),+ );+ mockUseCopilotStream.mockReturnValue(+ makeBaseCopilotStream({ messages: [currentMsg] }),+ );+ mockUseLoadMoreMessages.mockReturnValue(+ makeBaseLoadMore({ pagedMessages: [pagedMsg] }),+ );++ const { result } = renderHook(() => useCopilotPage());++ // Forward: currentMessages (beginning of session) come first+ expect(result.current.messages[0]).toEqual(currentMsg);+ expect(result.current.messages[1]).toEqual(pagedMsg);+ });++ it("calls resetPaged when forwardPaginated transitions false→true with paged messages", async () => {+ const mockResetPaged = vi.fn();+ const pagedMsg = { id: "paged", role: "user" };++ mockUseChatSession.mockReturnValue(+ makeBaseChatSession({ forwardPaginated: false }),+ );+ mockUseCopilotStream.mockReturnValue(makeBaseCopilotStream());+ mockUseLoadMoreMessages.mockReturnValue(+ makeBaseLoadMore({+ pagedMessages: [pagedMsg],+ resetPaged: mockResetPaged,+ }),+ );++ const { rerender } = renderHook(() => useCopilotPage());++ // Simulate session completing — forwardPaginated flips to true+ mockUseChatSession.mockReturnValue(+ makeBaseChatSession({ forwardPaginated: true }),+ );++ act(() => {+ rerender();+ });++ await waitFor(() => {+ expect(mockResetPaged).toHaveBeenCalled();+ });+ });++ it("does not call resetPaged when forwardPaginated is already true on mount", () => {+ const mockResetPaged = vi.fn();+ mockUseChatSession.mockReturnValue(+ makeBaseChatSession({ forwardPaginated: true }),+ );+ mockUseCopilotStream.mockReturnValue(makeBaseCopilotStream());+ mockUseLoadMoreMessages.mockReturnValue(+ makeBaseLoadMore({ pagedMessages: [], resetPaged: mockResetPaged }),+ );++ renderHook(() => useCopilotPage());++ expect(mockResetPaged).not.toHaveBeenCalled();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useLoadMoreMessages.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useLoadMoreMessages.test.tsnew file mode 100644index 0000000000..8f781e5f46--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useLoadMoreMessages.test.ts@@ -0,0 +1,568 @@+import { act, renderHook, waitFor } from "@testing-library/react";+import { beforeEach, describe, expect, it, vi } from "vitest";+import { useLoadMoreMessages } from "../useLoadMoreMessages";++const mockGetV2GetSession = vi.fn();++vi.mock("@/app/api/__generated__/endpoints/chat/chat", () => ({+ getV2GetSession: (...args: unknown[]) => mockGetV2GetSession(...args),+}));++vi.mock("../helpers/convertChatSessionToUiMessages", () => ({+ convertChatSessionMessagesToUiMessages: vi.fn(() => ({ messages: [] })),+ extractToolOutputsFromRaw: vi.fn(() => []),+}));++const BASE_ARGS = {+ sessionId: "sess-1",+ initialOldestSequence: 0,+ initialNewestSequence: 49,+ initialHasMore: true,+ forwardPaginated: true,+ initialPageRawMessages: [],+};++function makeSuccessResponse(overrides: {+ messages?: unknown[];+ has_more_messages?: boolean;+ oldest_sequence?: number;+ newest_sequence?: number;+}) {+ return {+ status: 200,+ data: {+ messages: overrides.messages ?? [],+ has_more_messages: overrides.has_more_messages ?? false,+ oldest_sequence: overrides.oldest_sequence ?? 0,+ newest_sequence: overrides.newest_sequence ?? 49,+ },+ };+}++describe("useLoadMoreMessages", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it("initialises with empty pagedMessages and correct cursors", () => {+ const { result } = renderHook(() => useLoadMoreMessages(BASE_ARGS));+ expect(result.current.pagedMessages).toHaveLength(0);+ expect(result.current.hasMore).toBe(true);+ expect(result.current.isLoadingMore).toBe(false);+ });++ it("resetPaged clears paged state and sets hasMore=false during transition", () => {+ const { result } = renderHook(() => useLoadMoreMessages(BASE_ARGS));++ act(() => {+ result.current.resetPaged();+ });++ expect(result.current.pagedMessages).toHaveLength(0);+ // hasMore must be false during transition to prevent forward loadMore+ // from firing on the now-active session before forwardPaginated updates.+ expect(result.current.hasMore).toBe(false);+ expect(result.current.isLoadingMore).toBe(false);+ });++ it("resetPaged exposes a fresh loadMore via incremented epoch", () => {+ const { result } = renderHook(() => useLoadMoreMessages(BASE_ARGS));+ // Just verify resetPaged is callable and doesn't throw.+ expect(() => {+ act(() => {+ result.current.resetPaged();+ });+ }).not.toThrow();+ });++ it("resets all state on sessionId change", () => {+ const { result, rerender } = renderHook(+ (props) => useLoadMoreMessages(props),+ { initialProps: BASE_ARGS },+ );++ rerender({+ ...BASE_ARGS,+ sessionId: "sess-2",+ initialOldestSequence: 10,+ initialNewestSequence: 59,+ initialHasMore: false,+ });++ expect(result.current.pagedMessages).toHaveLength(0);+ expect(result.current.hasMore).toBe(false);+ expect(result.current.isLoadingMore).toBe(false);+ });++ describe("loadMore — forward pagination", () => {+ it("calls getV2GetSession with after_sequence and updates newestSequence", async () => {+ const rawMsg = { role: "user", content: "hi", sequence: 50 };+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({+ messages: [rawMsg],+ has_more_messages: true,+ newest_sequence: 99,+ }),+ );++ const { result } = renderHook(() =>+ useLoadMoreMessages({ ...BASE_ARGS, forwardPaginated: true }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(mockGetV2GetSession).toHaveBeenCalledWith(+ "sess-1",+ expect.objectContaining({ after_sequence: 49 }),+ );+ expect(result.current.hasMore).toBe(true);+ expect(result.current.isLoadingMore).toBe(false);+ });++ it("sets hasMore=false when response has no more messages", async () => {+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({ has_more_messages: false }),+ );++ const { result } = renderHook(() =>+ useLoadMoreMessages({ ...BASE_ARGS, forwardPaginated: true }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(result.current.hasMore).toBe(false);+ });++ it("is a no-op when hasMore is false", async () => {+ const { result } = renderHook(() =>+ useLoadMoreMessages({+ ...BASE_ARGS,+ initialHasMore: false,+ forwardPaginated: true,+ }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(mockGetV2GetSession).not.toHaveBeenCalled();+ });+ });++ describe("loadMore — backward pagination", () => {+ it("calls getV2GetSession with before_sequence", async () => {+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({+ messages: [{ role: "user", content: "old", sequence: 0 }],+ has_more_messages: false,+ oldest_sequence: 0,+ }),+ );++ const { result } = renderHook(() =>+ useLoadMoreMessages({+ ...BASE_ARGS,+ forwardPaginated: false,+ initialOldestSequence: 50,+ }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(mockGetV2GetSession).toHaveBeenCalledWith(+ "sess-1",+ expect.objectContaining({ before_sequence: 50 }),+ );+ expect(result.current.hasMore).toBe(false);+ });+ });++ describe("loadMore — error handling", () => {+ it("does not set hasMore=false on first error", async () => {+ mockGetV2GetSession.mockRejectedValueOnce(new Error("network error"));++ const { result } = renderHook(() => useLoadMoreMessages(BASE_ARGS));++ await act(async () => {+ await result.current.loadMore();+ });++ // First error — hasMore still true+ expect(result.current.hasMore).toBe(true);+ expect(result.current.isLoadingMore).toBe(false);+ });++ it("sets hasMore=false after MAX_CONSECUTIVE_ERRORS (3) errors", async () => {+ mockGetV2GetSession.mockRejectedValue(new Error("network error"));++ const { result } = renderHook(() => useLoadMoreMessages(BASE_ARGS));++ for (let i = 0; i < 3; i++) {+ await act(async () => {+ await result.current.loadMore();+ });+ // Reset the in-flight guard between calls+ await waitFor(() => expect(result.current.isLoadingMore).toBe(false));+ }++ expect(result.current.hasMore).toBe(false);+ });++ it("ignores non-200 response and increments error count", async () => {+ mockGetV2GetSession.mockResolvedValueOnce({ status: 500, data: {} });++ const { result } = renderHook(() => useLoadMoreMessages(BASE_ARGS));++ await act(async () => {+ await result.current.loadMore();+ });++ // One error, not yet at threshold — hasMore still true+ expect(result.current.hasMore).toBe(true);+ expect(result.current.isLoadingMore).toBe(false);+ });++ it("sets hasMore=false after MAX_CONSECUTIVE_ERRORS (3) non-200 responses", async () => {+ mockGetV2GetSession.mockResolvedValue({ status: 503, data: {} });++ const { result } = renderHook(() => useLoadMoreMessages(BASE_ARGS));++ for (let i = 0; i < 3; i++) {+ await act(async () => {+ await result.current.loadMore();+ });+ await waitFor(() => expect(result.current.isLoadingMore).toBe(false));+ }++ expect(result.current.hasMore).toBe(false);+ });++ it("discards in-flight error when epoch changes mid-flight (resetPaged called)", async () => {+ let rejectRequest!: (e: Error) => void;+ mockGetV2GetSession.mockReturnValueOnce(+ new Promise((_, rej) => {+ rejectRequest = rej;+ }),+ );++ const { result } = renderHook(() => useLoadMoreMessages(BASE_ARGS));++ act(() => {+ result.current.loadMore();+ });++ // Reset epoch mid-flight+ act(() => {+ result.current.resetPaged();+ });++ // Reject the in-flight request — stale error should be discarded+ await act(async () => {+ rejectRequest(new Error("network error"));+ });++ // State unchanged: no hasMore=false, no errorCount, isLoadingMore cleared+ expect(result.current.hasMore).toBe(false); // false from resetPaged+ expect(result.current.isLoadingMore).toBe(false);+ });+ });++ describe("loadMore — forward pagination cursor advancement", () => {+ it("advances newestSequence after a successful forward load", async () => {+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({+ messages: [{ role: "user", content: "hi", sequence: 50 }],+ has_more_messages: true,+ newest_sequence: 99,+ }),+ );++ const { result } = renderHook(() =>+ useLoadMoreMessages({ ...BASE_ARGS, forwardPaginated: true }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ // A second loadMore should use after_sequence: 99 (advanced cursor)+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({ has_more_messages: false, newest_sequence: 149 }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(mockGetV2GetSession).toHaveBeenLastCalledWith(+ "sess-1",+ expect.objectContaining({ after_sequence: 99 }),+ );+ });++ it("does not regress newestSequence when parent refetches after pages loaded", async () => {+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({+ messages: [{ role: "user", content: "msg", sequence: 50 }],+ has_more_messages: true,+ newest_sequence: 99,+ }),+ );++ const { result, rerender } = renderHook(+ (props) => useLoadMoreMessages(props),+ { initialProps: { ...BASE_ARGS, forwardPaginated: true } },+ );++ // Load one page — newestSequence advances to 99+ await act(async () => {+ await result.current.loadMore();+ });++ // Parent refetches with a lower newest_sequence (49) — should NOT regress cursor+ rerender({+ ...BASE_ARGS,+ forwardPaginated: true,+ initialNewestSequence: 49,+ });++ // Next loadMore should still use the advanced cursor (99)+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({ has_more_messages: false, newest_sequence: 149 }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(mockGetV2GetSession).toHaveBeenLastCalledWith(+ "sess-1",+ expect.objectContaining({ after_sequence: 99 }),+ );+ });+ });++ describe("loadMore — MAX_OLDER_MESSAGES truncation", () => {+ it("truncates accumulated messages at MAX_OLDER_MESSAGES (2000)", async () => {+ // Single load of 2001 messages exceeds the limit in one shot.+ // This avoids relying on cross-render closure staleness: estimatedTotal =+ // pagedRawMessages.length (0, fresh) + 2001 = 2001 >= 2000 → hasMore=false.+ const args = { ...BASE_ARGS, forwardPaginated: false };++ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({+ messages: Array.from({ length: 2001 }, (_, i) => ({+ role: "user",+ content: `msg ${i}`,+ sequence: i,+ })),+ has_more_messages: true,+ oldest_sequence: 0,+ }),+ );++ const { result } = renderHook(() => useLoadMoreMessages(args));++ await act(async () => {+ await result.current.loadMore();+ });++ expect(result.current.hasMore).toBe(false);+ });++ it("forward truncation keeps first MAX_OLDER_MESSAGES items (not last)", async () => {+ // 1990 messages already paged; load 20 more forward — total 2010 > 2000.+ // Forward truncation must keep slice(0, 2000), not slice(-2000),+ // to preserve the beginning of the conversation.+ const forwardNearLimitArgs = {+ ...BASE_ARGS,+ forwardPaginated: true,+ initialNewestSequence: 49,+ initialOldestSequence: 0,+ initialHasMore: true,+ };++ const { result } = renderHook((props) => useLoadMoreMessages(props), {+ initialProps: forwardNearLimitArgs,+ });++ // First load: 1990 messages — advances newestSequence to 2039+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({+ messages: Array.from({ length: 1990 }, (_, i) => ({+ role: "assistant",+ content: `msg ${i + 50}`,+ sequence: i + 50,+ })),+ has_more_messages: true,+ newest_sequence: 2039,+ }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ // Second load: 20 more messages pushes total to 2010 > 2000.+ // Truncation keeps seq 50..2049 (2000 items); discards seq 2050..2059 (10 items).+ // Even though the server says has_more_messages=false, hasMore stays true+ // because there are discarded items that need to be re-fetched.+ // The cursor (newestSequence) advances to 2049 — the last kept item's sequence.+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({+ messages: Array.from({ length: 20 }, (_, i) => ({+ role: "assistant",+ content: `msg ${i + 2040}`,+ sequence: i + 2040,+ })),+ has_more_messages: false,+ newest_sequence: 2059,+ }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ // Truncation occurred (2010 > 2000): hasMore=true so discarded items can be fetched.+ // Cursor advances to last kept item (seq 2049), not the server's newest (2059).+ await waitFor(() => expect(result.current.hasMore).toBe(true));+ });+ });++ describe("loadMore — null cursor guard", () => {+ it("is a no-op when newestSequence is null (forwardPaginated=true)", async () => {+ const { result } = renderHook(() =>+ useLoadMoreMessages({+ ...BASE_ARGS,+ forwardPaginated: true,+ initialNewestSequence: null,+ }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(mockGetV2GetSession).not.toHaveBeenCalled();+ });++ it("is a no-op when oldestSequence is null (forwardPaginated=false)", async () => {+ const { result } = renderHook(() =>+ useLoadMoreMessages({+ ...BASE_ARGS,+ forwardPaginated: false,+ initialOldestSequence: null,+ }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(mockGetV2GetSession).not.toHaveBeenCalled();+ });+ });++ describe("pagedMessages — initialPageRawMessages extraToolOutputs", () => {+ it("calls extractToolOutputsFromRaw for backward pagination with non-empty initialPageRawMessages", async () => {+ const { extractToolOutputsFromRaw } = await import(+ "../helpers/convertChatSessionToUiMessages"+ );++ const rawMsg = { role: "user", content: "old", sequence: 0 };+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({+ messages: [rawMsg],+ has_more_messages: false,+ oldest_sequence: 0,+ }),+ );++ const { result } = renderHook(() =>+ useLoadMoreMessages({+ ...BASE_ARGS,+ forwardPaginated: false,+ initialOldestSequence: 50,+ initialPageRawMessages: [{ role: "assistant", content: "response" }],+ }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(extractToolOutputsFromRaw).toHaveBeenCalled();+ });++ it("does NOT call extractToolOutputsFromRaw for forward pagination", async () => {+ const { extractToolOutputsFromRaw } = await import(+ "../helpers/convertChatSessionToUiMessages"+ );++ const rawMsg = { role: "assistant", content: "hi", sequence: 50 };+ mockGetV2GetSession.mockResolvedValueOnce(+ makeSuccessResponse({+ messages: [rawMsg],+ has_more_messages: false,+ newest_sequence: 99,+ }),+ );++ const { result } = renderHook(() =>+ useLoadMoreMessages({+ ...BASE_ARGS,+ forwardPaginated: true,+ initialPageRawMessages: [{ role: "user", content: "hello" }],+ }),+ );++ await act(async () => {+ await result.current.loadMore();+ });++ expect(extractToolOutputsFromRaw).not.toHaveBeenCalled();+ });+ });++ describe("loadMore — epoch / stale-response guard", () => {+ it("discards response when epoch changes during flight (resetPaged called)", async () => {+ let resolveRequest!: (v: unknown) => void;+ mockGetV2GetSession.mockReturnValueOnce(+ new Promise((res) => {+ resolveRequest = res;+ }),+ );++ const { result } = renderHook(() => useLoadMoreMessages(BASE_ARGS));++ // Start the request without awaiting+ act(() => {+ result.current.loadMore();+ });++ // Reset epoch mid-flight+ act(() => {+ result.current.resetPaged();+ });++ // Now resolve the in-flight request+ await act(async () => {+ resolveRequest(+ makeSuccessResponse({ messages: [{ role: "user", content: "hi" }] }),+ );+ });++ // Response discarded — pagedMessages stays empty, isLoadingMore stays false+ expect(result.current.pagedMessages).toHaveLength(0);+ expect(result.current.isLoadingMore).toBe(false);+ });+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsxindex 7f3c1d0328..6731057658 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx@@ -30,6 +30,7 @@ export interface ChatContainerProps {hasMoreMessages?: boolean;isLoadingMore?: boolean;onLoadMore?: () => void;+ forwardPaginated?: boolean;/** Files dropped onto the chat window. */droppedFiles?: File[];/** Called after droppedFiles have been consumed by ChatInput. */@@ -54,6 +55,7 @@ export const ChatContainer = ({hasMoreMessages,isLoadingMore,onLoadMore,+ forwardPaginated,droppedFiles,onDroppedFilesConsumed,historicalDurations,@@ -108,6 +110,7 @@ export const ChatContainer = ({hasMoreMessages={hasMoreMessages}isLoadingMore={isLoadingMore}onLoadMore={onLoadMore}+ forwardPaginated={forwardPaginated}onRetry={handleRetry}historicalDurations={historicalDurations}/>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsxindex d12f97106b..d7f3123f9c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx@@ -43,6 +43,10 @@ interface Props {hasMoreMessages?: boolean;isLoadingMore?: boolean;onLoadMore?: () => void;+ /** When true the load-more sentinel is placed at the bottom (forward+ * pagination for completed sessions). When false it is at the top+ * (backward pagination for active sessions). */+ forwardPaginated?: boolean;onRetry?: () => void;historicalDurations?: Map<string, number>;}@@ -136,11 +140,25 @@ export function LoadMoreSentinel({isLoading,messageCount,onLoadMore,+ rootMargin = "200px 0px 0px 0px",+ adjustScroll = true,+ forwardPaginated = false,}: {hasMore: boolean;isLoading: boolean;messageCount: number;onLoadMore: () => void;+ /** IntersectionObserver rootMargin. Top sentinel uses "200px 0px 0px 0px"+ * (pre-trigger when approaching from above); bottom sentinel should use+ * "0px 0px 200px 0px" (pre-trigger when approaching from below). */+ rootMargin?: string;+ /** Whether to adjust scrollTop after load to preserve visual position.+ * True for backward pagination (prepend above); false for forward+ * pagination (append below) where no adjustment is needed. */+ adjustScroll?: boolean;+ /** When true the button reads "Load newer messages" (forward pagination).+ * When false (default) it reads "Load older messages". */+ forwardPaginated?: boolean;}) {const sentinelRef = useRef<HTMLDivElement>(null);const onLoadMoreRef = useRef(onLoadMore);@@ -185,11 +203,11 @@ export function LoadMoreSentinel({if (autoFillRoundsRef.current >= MAX_AUTO_FILL_ROUNDS) return;captureAndLoad(true);},- { rootMargin: "200px 0px 0px 0px" },+ { rootMargin },);observer.observe(sentinelRef.current);return () => observer.disconnect();- }, [hasMore, isLoading, scrollRef]);+ }, [hasMore, isLoading, rootMargin, scrollRef]);// After React commits new DOM nodes (prepended messages), adjust// scrollTop so the user stays at the same visual position.@@ -202,7 +220,9 @@ export function LoadMoreSentinel({scrollSnapshotRef.current;if (!el || prevHeight === 0) return;const delta = el.scrollHeight - prevHeight;- if (delta > 0) {+ // Only restore scroll position for backward pagination (content prepended+ // above). Forward pagination appends below — no adjustment needed.+ if (adjustScroll && delta > 0) {el.scrollTop = prevTop + delta;}// Reset the auto-fill backoff whenever the container becomes@@ -216,7 +236,7 @@ export function LoadMoreSentinel({}scrollSnapshotRef.current = { scrollHeight: 0, scrollTop: 0 };autoTriggeredRef.current = false;- }, [messageCount, scrollRef]);+ }, [adjustScroll, messageCount, scrollRef]);return (<div@@ -235,7 +255,7 @@ export function LoadMoreSentinel({size="small"onClick={() => captureAndLoad(false)}>- Load older messages+ {forwardPaginated ? "Load newer messages" : "Load older messages"}</Button>))}@@ -252,6 +272,7 @@ export function ChatMessagesContainer({hasMoreMessages,isLoadingMore,onLoadMore,+ forwardPaginated,onRetry,historicalDurations,}: Props) {@@ -330,7 +351,7 @@ export function ChatMessagesContainer({}><ConversationContent className="flex min-h-full flex-1 flex-col gap-6 px-3 py-6">- {hasMoreMessages && onLoadMore && (+ {hasMoreMessages && onLoadMore && !forwardPaginated && (<LoadMoreSentinelhasMore={hasMoreMessages}isLoading={!!isLoadingMore}@@ -489,6 +510,17 @@ export function ChatMessagesContainer({</pre></details>)}+ {hasMoreMessages && onLoadMore && forwardPaginated && (+ <LoadMoreSentinel+ hasMore={hasMoreMessages}+ isLoading={!!isLoadingMore}+ messageCount={messages.length}+ onLoadMore={onLoadMore}+ rootMargin="0px 0px 200px 0px"+ adjustScroll={false}+ forwardPaginated+ />+ )}</ConversationContent><ConversationScrollButton /></Conversation>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsxnew file mode 100644index 0000000000..ca7ee0d181--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx@@ -0,0 +1,173 @@+import { render, screen, cleanup } from "@/tests/integrations/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { ChatMessagesContainer } from "../ChatMessagesContainer";++const mockScrollEl = {+ scrollHeight: 100,+ scrollTop: 0,+ clientHeight: 500,+};++vi.mock("use-stick-to-bottom", () => ({+ useStickToBottomContext: () => ({ scrollRef: { current: mockScrollEl } }),+ Conversation: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+ ConversationContent: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+ ConversationScrollButton: () => null,+}));++vi.mock("@/components/ai-elements/conversation", () => ({+ Conversation: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+ ConversationContent: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+ ConversationScrollButton: () => null,+}));++vi.mock("@/components/ai-elements/message", () => ({+ Message: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+ MessageContent: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+ MessageActions: ({ children }: { children: React.ReactNode }) => (+ <div>{children}</div>+ ),+}));++vi.mock("../components/AssistantMessageActions", () => ({+ AssistantMessageActions: () => null,+}));+vi.mock("../components/CopyButton", () => ({ CopyButton: () => null }));+vi.mock("../components/CollapsedToolGroup", () => ({+ CollapsedToolGroup: () => null,+}));+vi.mock("../components/MessageAttachments", () => ({+ MessageAttachments: () => null,+}));+vi.mock("../components/MessagePartRenderer", () => ({+ MessagePartRenderer: () => null,+}));+vi.mock("../components/ReasoningCollapse", () => ({+ ReasoningCollapse: () => null,+}));+vi.mock("../components/ThinkingIndicator", () => ({+ ThinkingIndicator: () => null,+}));+vi.mock("../../JobStatsBar/TurnStatsBar", () => ({+ TurnStatsBar: () => null,+}));+vi.mock("../../JobStatsBar/useElapsedTimer", () => ({+ useElapsedTimer: () => ({ elapsedSeconds: 0 }),+}));+vi.mock("../../CopilotPendingReviews/CopilotPendingReviews", () => ({+ CopilotPendingReviews: () => null,+}));+vi.mock("../helpers", () => ({+ buildRenderSegments: () => [],+ getTurnMessages: () => [],+ parseSpecialMarkers: () => ({ markerType: null }),+ splitReasoningAndResponse: (parts: unknown[]) => ({+ reasoningParts: [],+ responseParts: parts,+ }),+}));++type ObserverCallback = (entries: { isIntersecting: boolean }[]) => void;+class MockIntersectionObserver {+ static lastCallback: ObserverCallback | null = null;+ private callback: ObserverCallback;+ constructor(cb: ObserverCallback) {+ this.callback = cb;+ MockIntersectionObserver.lastCallback = cb;+ }+ observe() {}+ disconnect() {}+ unobserve() {}+ takeRecords() {+ return [];+ }+ root = null;+ rootMargin = "";+ thresholds = [];+}++const BASE_PROPS = {+ messages: [],+ status: "ready" as const,+ error: undefined,+ isLoading: false,+ sessionID: "sess-1",+ hasMoreMessages: true,+ isLoadingMore: false,+ onLoadMore: vi.fn(),+ onRetry: vi.fn(),+};++describe("ChatMessagesContainer", () => {+ beforeEach(() => {+ mockScrollEl.scrollHeight = 100;+ mockScrollEl.scrollTop = 0;+ mockScrollEl.clientHeight = 500;+ MockIntersectionObserver.lastCallback = null;+ vi.stubGlobal("IntersectionObserver", MockIntersectionObserver);+ });++ afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+ });++ it("renders top sentinel when forwardPaginated is false (backward pagination)", () => {+ render(<ChatMessagesContainer {...BASE_PROPS} forwardPaginated={false} />);+ expect(+ screen.getByRole("button", { name: /load older messages/i }),+ ).toBeDefined();+ });++ it("renders top sentinel when forwardPaginated is undefined (default, backward)", () => {+ render(<ChatMessagesContainer {...BASE_PROPS} />);+ expect(+ screen.getByRole("button", { name: /load older messages/i }),+ ).toBeDefined();+ });++ it("renders bottom sentinel when forwardPaginated is true (forward pagination)", () => {+ render(<ChatMessagesContainer {...BASE_PROPS} forwardPaginated={true} />);+ expect(+ screen.getByRole("button", { name: /load newer messages/i }),+ ).toBeDefined();+ });++ it("hides sentinel when hasMoreMessages is false", () => {+ render(+ <ChatMessagesContainer+ {...BASE_PROPS}+ hasMoreMessages={false}+ forwardPaginated={true}+ />,+ );+ expect(+ screen.queryByRole("button", { name: /load older messages/i }),+ ).toBeNull();+ });++ it("hides sentinel when onLoadMore is not provided", () => {+ render(+ <ChatMessagesContainer+ {...BASE_PROPS}+ onLoadMore={undefined}+ forwardPaginated={true}+ />,+ );+ expect(+ screen.queryByRole("button", { name: /load older messages/i }),+ ).toBeNull();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/LoadMoreSentinel.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/LoadMoreSentinel.test.tsxindex 3cbf4cbe48..d3f4f08c9e 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/LoadMoreSentinel.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/LoadMoreSentinel.test.tsx@@ -172,6 +172,36 @@ describe("LoadMoreSentinel", () => {expect(mockScrollEl.scrollTop).toBe(200);});+ it("does NOT adjust scroll when adjustScroll=false (forward pagination)", () => {+ mockScrollEl.scrollHeight = 100;+ mockScrollEl.scrollTop = 50;+ const onLoadMore = vi.fn();+ const { rerender } = render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={5}+ onLoadMore={onLoadMore}+ adjustScroll={false}+ />,+ );+ // Fire observer to capture snapshot.+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ // Simulate DOM growing from appended newer messages (forward load-more).+ mockScrollEl.scrollHeight = 300;+ rerender(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={10}+ onLoadMore={onLoadMore}+ adjustScroll={false}+ />,+ );+ // scrollTop should remain unchanged — no jump for forward pagination.+ expect(mockScrollEl.scrollTop).toBe(50);+ });+it("ignores same-frame duplicate triggers until isLoading transitions", () => {const onLoadMore = vi.fn();const { rerender } = render(diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.tsnew file mode 100644index 0000000000..33b2879cc9--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts@@ -0,0 +1,59 @@+import { describe, expect, it } from "vitest";+import { convertChatSessionMessagesToUiMessages } from "../convertChatSessionToUiMessages";++const SESSION_ID = "sess-test";++describe("convertChatSessionMessagesToUiMessages", () => {+ it("does not drop user messages with null content", () => {+ const result = convertChatSessionMessagesToUiMessages(+ SESSION_ID,+ [{ role: "user", content: null, sequence: 0 }],+ { isComplete: true },+ );++ expect(result.messages).toHaveLength(1);+ expect(result.messages[0].role).toBe("user");+ });++ it("does not drop user messages with empty string content", () => {+ const result = convertChatSessionMessagesToUiMessages(+ SESSION_ID,+ [{ role: "user", content: "", sequence: 0 }],+ { isComplete: true },+ );++ expect(result.messages).toHaveLength(1);+ expect(result.messages[0].role).toBe("user");+ });++ it("still drops non-user messages with null content", () => {+ const result = convertChatSessionMessagesToUiMessages(+ SESSION_ID,+ [{ role: "assistant", content: null, sequence: 0 }],+ { isComplete: true },+ );++ expect(result.messages).toHaveLength(0);+ });++ it("still drops non-user messages with empty string content", () => {+ const result = convertChatSessionMessagesToUiMessages(+ SESSION_ID,+ [{ role: "assistant", content: "", sequence: 0 }],+ { isComplete: true },+ );++ expect(result.messages).toHaveLength(0);+ });++ it("includes user message with normal content", () => {+ const result = convertChatSessionMessagesToUiMessages(+ SESSION_ID,+ [{ role: "user", content: "hello", sequence: 0 }],+ { isComplete: true },+ );++ expect(result.messages).toHaveLength(1);+ expect(result.messages[0].role).toBe("user");+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.tsindex 5021d661f0..10b0ad52c1 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts@@ -253,6 +253,11 @@ export function convertChatSessionMessagesToUiMessages(}}+ // User messages must always be rendered, even with empty content, so the+ // initial prompt is visible when reloading a session.+ if (parts.length === 0 && msg.role === "user") {+ parts.push({ type: "text", text: "", state: "done" });+ }if (parts.length === 0) return;// Merge consecutive assistant messages into a single UIMessagediff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.tsindex b5a02620c2..8357ee8af9 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts@@ -86,6 +86,16 @@ export function useChatSession({ dryRun = false }: UseChatSessionOptions = {}) {return sessionQuery.data.data.oldest_sequence ?? null;}, [sessionQuery.data]);+ const newestSequence = useMemo(() => {+ if (sessionQuery.data?.status !== 200) return null;+ return sessionQuery.data.data.newest_sequence ?? null;+ }, [sessionQuery.data]);++ const forwardPaginated = useMemo(() => {+ if (sessionQuery.data?.status !== 200) return false;+ return !!sessionQuery.data.data.forward_paginated;+ }, [sessionQuery.data]);+// Memoize so the effect in useCopilotPage doesn't infinite-loop on a new// array reference every render. Re-derives only when query data changes.// When the session is complete (no active stream), mark dangling tool@@ -185,6 +195,8 @@ export function useChatSession({ dryRun = false }: UseChatSessionOptions = {}) {hasActiveStream,hasMoreMessages,oldestSequence,+ newestSequence,+ forwardPaginated,isLoadingSession: sessionQuery.isLoading,isSessionError: sessionQuery.isError,createSession,diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.tsindex 9e118c2bbc..3e9be079db 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts@@ -56,6 +56,8 @@ export function useCopilotPage() {hasActiveStream,hasMoreMessages,oldestSequence,+ newestSequence,+ forwardPaginated,isLoadingSession,isSessionError,createSession,@@ -84,18 +86,26 @@ export function useCopilotPage() {copilotModel: isModeToggleEnabled ? copilotLlmModel : undefined,});- const { olderMessages, hasMore, isLoadingMore, loadMore } =+ const { pagedMessages, hasMore, isLoadingMore, loadMore, resetPaged } =useLoadMoreMessages({sessionId,initialOldestSequence: oldestSequence,+ initialNewestSequence: newestSequence,initialHasMore: hasMoreMessages,+ forwardPaginated,initialPageRawMessages: rawSessionMessages,});- // Combine older (paginated) messages with current page messages,- // merging consecutive assistant UIMessages at the page boundary so- // reasoning + response parts stay in a single bubble.- const messages = concatWithAssistantMerge(olderMessages, currentMessages);+ // Combine paginated messages with current page messages, merging consecutive+ // assistant UIMessages at the page boundary so reasoning + response parts+ // stay in a single bubble.+ // Forward pagination (completed sessions): current page is the beginning,+ // paged messages are newer pages appended after.+ // Backward pagination (active sessions): paged messages are older history+ // prepended before the current page.+ const messages = forwardPaginated+ ? concatWithAssistantMerge(currentMessages, pagedMessages)+ : concatWithAssistantMerge(pagedMessages, currentMessages);useCopilotNotifications(sessionId);@@ -170,6 +180,23 @@ export function useCopilotPage() {}}, [sessionId, pendingMessage, sendMessage]);+ // --- Clear backward-paginated messages when session completes ---+ // When a session transitions from active (forwardPaginated=false) to complete+ // (forwardPaginated=true), any backward-paginated older messages would be+ // appended after currentMessages instead of before, causing chronological+ // disorder. Reset paged state so the completed session renders cleanly.+ const prevForwardPaginatedRef = useRef(forwardPaginated);+ useEffect(() => {+ if (+ !prevForwardPaginatedRef.current &&+ forwardPaginated &&+ pagedMessages.length > 0+ ) {+ resetPaged();+ }+ prevForwardPaginatedRef.current = forwardPaginated;+ }, [forwardPaginated, pagedMessages.length, resetPaged]);+// --- Extract prompt from URL hash on mount (e.g. /copilot#prompt=Hello) ---useWorkflowImportAutoSubmit({createSession,@@ -251,6 +278,15 @@ export function useCopilotPage() {isUserStoppingRef.current = false;if (sessionId) {+ // When continuing a completed session that had forward-paginated history+ // loaded, the paged messages would appear in wrong position relative to+ // the new streaming turn (pagedMessages are newer pages, so they'd end+ // up after the streaming turn). Reset paged state so ordering is correct+ // during streaming; the user can reload history afterward if needed.+ if (forwardPaginated && pagedMessages.length > 0) {+ resetPaged();+ }+if (files && files.length > 0) {setIsUploadingFiles(true);try {@@ -397,6 +433,7 @@ export function useCopilotPage() {hasMoreMessages: hasMore,isLoadingMore,loadMore,+ forwardPaginated,// Mobile drawerisMobile,isDrawerOpen,diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.tsindex 313b2d5fb8..7c3f1b7c24 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts@@ -9,7 +9,11 @@ import {interface UseLoadMoreMessagesArgs {sessionId: string | null;initialOldestSequence: number | null;+ initialNewestSequence: number | null;initialHasMore: boolean;+ /** True when the initial page was loaded from sequence 0 forward (completed+ * sessions). False when loaded newest-first (active sessions). */+ forwardPaginated: boolean;/** Raw messages from the initial page, used for cross-page tool output matching. */initialPageRawMessages: unknown[];}@@ -20,16 +24,21 @@ const MAX_OLDER_MESSAGES = 2000;export function useLoadMoreMessages({sessionId,initialOldestSequence,+ initialNewestSequence,initialHasMore,+ forwardPaginated,initialPageRawMessages,}: UseLoadMoreMessagesArgs) {- // Store accumulated raw messages from all older pages (in ascending order).+ // Accumulated raw messages from all extra pages (ascending order).// Re-converting them all together ensures tool outputs are matched across// inter-page boundaries.- const [olderRawMessages, setOlderRawMessages] = useState<unknown[]>([]);+ const [pagedRawMessages, setPagedRawMessages] = useState<unknown[]>([]);const [oldestSequence, setOldestSequence] = useState<number | null>(initialOldestSequence,);+ const [newestSequence, setNewestSequence] = useState<number | null>(+ initialNewestSequence,+ );const [hasMore, setHasMore] = useState(initialHasMore);const [isLoadingMore, setIsLoadingMore] = useState(false);const isLoadingMoreRef = useRef(false);@@ -46,7 +55,7 @@ export function useLoadMoreMessages({// The parent's `initialOldestSequence` drifts forward every time the// session query refetches (e.g. after a stream completes — see// `useCopilotStream` invalidation on `streaming → ready`). If we- // wiped `olderRawMessages` every time that happened, users who had+ // wiped `pagedRawMessages` every time that happened, users who had// scrolled back would lose their loaded history on each new turn and// subsequent `loadMore` calls would fetch messages that overlap with// the AI SDK's retained state in `currentMessages`, producing visible@@ -63,8 +72,9 @@ export function useLoadMoreMessages({// Session changed — full resetprevSessionIdRef.current = sessionId;prevInitialOldestRef.current = initialOldestSequence;- setOlderRawMessages([]);+ setPagedRawMessages([]);setOldestSequence(initialOldestSequence);+ setNewestSequence(initialNewestSequence);setHasMore(initialHasMore);setIsLoadingMore(false);isLoadingMoreRef.current = false;@@ -75,49 +85,64 @@ export function useLoadMoreMessages({prevInitialOldestRef.current = initialOldestSequence;- // If we haven't paged back yet, mirror the parent so the first+ // If we haven't paged yet, mirror the parent so the first// `loadMore` starts from the correct cursor.- if (olderRawMessages.length === 0) {+ //+ // When paged messages exist (pagedRawMessages.length > 0) we intentionally+ // do NOT update `hasMore` or `newestSequence` from the parent. A parent+ // refetch (e.g. after a new turn completes) may carry a fresh+ // `initialHasMore=true` or a larger `initialNewestSequence`, but those+ // reflect the *initial* page window, not the forward-paged window we have+ // already advanced into. Overwriting the local cursor here would cause the+ // next `loadMore` to re-fetch pages we already have. The local cursor is+ // advanced correctly inside `loadMore` itself via `setNewestSequence`.+ if (pagedRawMessages.length === 0) {setOldestSequence(initialOldestSequence);+ // Only regress the forward cursor if we haven't paged ahead yet —+ // otherwise a parent refetch would reset a cursor we already advanced.+ setNewestSequence((prev) =>+ prev !== null && prev > (initialNewestSequence ?? -1)+ ? prev+ : initialNewestSequence,+ );setHasMore(initialHasMore);}- }, [sessionId, initialOldestSequence, initialHasMore]);+ }, [sessionId, initialOldestSequence, initialNewestSequence, initialHasMore]);// Convert all accumulated raw messages in one pass so tool outputs- // are matched across inter-page boundaries. Initial page tool outputs- // are included via extraToolOutputs to handle the boundary between- // the last older page and the initial/streaming page.- const olderMessages: UIMessage<unknown, UIDataTypes, UITools>[] =+ // are matched across inter-page boundaries.+ // For backward pagination only: include initial page tool outputs so older+ // paged pages can match tool calls whose outputs landed in the initial page.+ // For forward pagination this is unnecessary — tool calls in newer paged+ // pages cannot have their outputs in the older initial page.+ const pagedMessages: UIMessage<unknown, UIDataTypes, UITools>[] =useMemo(() => {- if (!sessionId || olderRawMessages.length === 0) return [];+ if (!sessionId || pagedRawMessages.length === 0) return [];const extraToolOutputs =- initialPageRawMessages.length > 0+ !forwardPaginated && initialPageRawMessages.length > 0? extractToolOutputsFromRaw(initialPageRawMessages): undefined;return convertChatSessionMessagesToUiMessages(sessionId,- olderRawMessages,+ pagedRawMessages,{ isComplete: true, extraToolOutputs },).messages;- }, [sessionId, olderRawMessages, initialPageRawMessages]);+ }, [sessionId, pagedRawMessages, initialPageRawMessages, forwardPaginated]);async function loadMore() {- if (- !sessionId ||- !hasMore ||- isLoadingMoreRef.current ||- oldestSequence === null- )- return;+ if (!sessionId || !hasMore || isLoadingMoreRef.current) return;++ const cursor = forwardPaginated ? newestSequence : oldestSequence;+ if (cursor === null) return;const requestEpoch = epochRef.current;isLoadingMoreRef.current = true;setIsLoadingMore(true);try {- const response = await getV2GetSession(sessionId, {- limit: 50,- before_sequence: oldestSequence,- });+ const params = forwardPaginated+ ? { limit: 50, after_sequence: cursor }+ : { limit: 50, before_sequence: cursor };+ const response = await getV2GetSession(sessionId, params);// Discard response if session/pagination was reset while awaitingif (epochRef.current !== requestEpoch) return;@@ -136,18 +161,66 @@ export function useLoadMoreMessages({consecutiveErrorsRef.current = 0;const newRaw = (response.data.messages ?? []) as unknown[];- setOlderRawMessages((prev) => {- const merged = [...newRaw, ...prev];+ // Estimate total after merge using the closure-captured pagedRawMessages.length.+ // This is a safe approximation: worst case it's one page stale (one extra load+ // allowed), but it avoids the React-18-batching pitfall where a functional+ // updater's mutations are not visible until the next render.+ const estimatedTotal = pagedRawMessages.length + newRaw.length;+ setPagedRawMessages((prev) => {+ // Forward: append to end. Backward: prepend to start.+ const merged = forwardPaginated+ ? [...prev, ...newRaw]+ : [...newRaw, ...prev];if (merged.length > MAX_OLDER_MESSAGES) {- return merged.slice(merged.length - MAX_OLDER_MESSAGES);+ // Backward: discard the oldest (front) items — user has scrolled far+ // back and we shed the furthest history.+ // Forward: discard the newest (tail) items — we only ever fetch+ // forward, so the tail is the most recently appended page; shedding+ // it means the sentinel stalls, which is safer than discarding the+ // beginning of the conversation the user is here to read.+ return forwardPaginated+ ? merged.slice(0, MAX_OLDER_MESSAGES)+ : merged.slice(merged.length - MAX_OLDER_MESSAGES);}return merged;});- setOldestSequence(response.data.oldest_sequence ?? null);- if (newRaw.length + olderRawMessages.length >= MAX_OLDER_MESSAGES) {- setHasMore(false);++ if (forwardPaginated) {+ const willTruncateForward = estimatedTotal > MAX_OLDER_MESSAGES;+ if (willTruncateForward) {+ // Truncation shed the newest tail. Advance the cursor to the last KEPT+ // item's sequence so the sentinel re-fetches the discarded items next+ // time rather than jumping past them.+ // lastKeptIdx: index within newRaw of the last item that survives.+ // prev contributes pagedRawMessages.length items; total kept = MAX.+ const lastKeptIdx = MAX_OLDER_MESSAGES - 1 - pagedRawMessages.length;+ if (lastKeptIdx >= 0 && lastKeptIdx < newRaw.length) {+ const lastKeptMsg = newRaw[lastKeptIdx] as { sequence?: number };+ if (typeof lastKeptMsg?.sequence === "number") {+ setNewestSequence(lastKeptMsg.sequence);+ setHasMore(true); // Discarded items still exist — keep sentinel active+ } else {+ // Sequence unavailable — fall back; truncated items will be lost+ setNewestSequence(response.data.newest_sequence ?? null);+ setHasMore(!!response.data.has_more_messages);+ }+ } else {+ // All of newRaw was dropped (already at MAX_OLDER_MESSAGES cap).+ // Stop to avoid an infinite re-fetch loop at the display cap.+ setHasMore(false);+ }+ } else {+ setNewestSequence(response.data.newest_sequence ?? null);+ setHasMore(!!response.data.has_more_messages);+ }} else {- setHasMore(!!response.data.has_more_messages);+ setOldestSequence(response.data.oldest_sequence ?? null);+ if (estimatedTotal >= MAX_OLDER_MESSAGES) {+ // Backward: accumulated MAX_OLDER_MESSAGES — stop to avoid unbounded memory.+ setHasMore(false);+ } else {+ setHasMore(!!response.data.has_more_messages);+ }}} catch (error) {if (epochRef.current !== requestEpoch) return;@@ -164,5 +237,22 @@ export function useLoadMoreMessages({}}- return { olderMessages, hasMore, isLoadingMore, loadMore };+ function resetPaged() {+ setPagedRawMessages([]);+ setOldestSequence(initialOldestSequence);+ setNewestSequence(initialNewestSequence);+ // Set hasMore=false during the session-transition window so no loadMore+ // fires with forward pagination (after_sequence) on the now-active session.+ // The useEffect will restore hasMore from the parent after the refetch+ // completes and forwardPaginated switches to false.+ setHasMore(false);+ // Clear the loading state so the spinner doesn't stay stuck if a loadMore+ // was in flight when resetPaged was called.+ setIsLoadingMore(false);+ isLoadingMoreRef.current = false;+ consecutiveErrorsRef.current = 0;+ epochRef.current += 1;+ }++ return { pagedMessages, hasMore, isLoadingMore, loadMore, resetPaged };}diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex c68070f811..ff426c267e 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -1498,7 +1498,7 @@"get": {"tags": ["v2", "chat", "chat"],"summary": "Get Session",- "description": "Retrieve the details of a specific chat session.\n\nSupports cursor-based pagination via ``limit`` and ``before_sequence``.\nWhen no pagination params are provided, returns the most recent messages.\n\nArgs:\n session_id: The unique identifier for the desired chat session.\n user_id: The authenticated user's ID.\n limit: Maximum number of messages to return (1-200, default 50).\n before_sequence: Return messages with sequence < this value (cursor).\n\nReturns:\n SessionDetailResponse: Details for the requested session, including\n active_stream info and pagination metadata.",+ "description": "Retrieve the details of a specific chat session.\n\nSupports cursor-based pagination via ``limit``, ``before_sequence``, and\n``after_sequence``. The two cursor parameters are mutually exclusive.\n\nOn the initial load (no cursor provided) of a completed session, messages\nare returned in forward order starting from sequence 0 so the user always\nsees their initial prompt. Active sessions use the legacy newest-first\norder so streaming context is preserved.","operationId": "getV2GetSession","security": [{ "HTTPBearerJWT": [] }],"parameters": [@@ -1516,9 +1516,11 @@"type": "integer","maximum": 200,"minimum": 1,+ "description": "Maximum number of messages to return.","default": 50,"title": "Limit"- }+ },+ "description": "Maximum number of messages to return."},{"name": "before_sequence",@@ -1529,8 +1531,24 @@{ "type": "integer", "minimum": 0 },{ "type": "null" }],+ "description": "Backward pagination cursor. Return messages with sequence number strictly less than this value. Used by active-session load-more. Mutually exclusive with after_sequence.","title": "Before Sequence"- }+ },+ "description": "Backward pagination cursor. Return messages with sequence number strictly less than this value. Used by active-session load-more. Mutually exclusive with after_sequence."+ },+ {+ "name": "after_sequence",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ { "type": "integer", "minimum": 0 },+ { "type": "null" }+ ],+ "description": "Forward pagination cursor. Return messages with sequence number strictly greater than this value. Used by completed-session load-more. Mutually exclusive with before_sequence.",+ "title": "After Sequence"+ },+ "description": "Forward pagination cursor. Return messages with sequence number strictly greater than this value. Used by completed-session load-more. Mutually exclusive with before_sequence."}],"responses": {@@ -13273,6 +13291,15 @@"anyOf": [{ "type": "integer" }, { "type": "null" }],"title": "Oldest Sequence"},+ "newest_sequence": {+ "anyOf": [{ "type": "integer" }, { "type": "null" }],+ "title": "Newest Sequence"+ },+ "forward_paginated": {+ "type": "boolean",+ "title": "Forward Paginated",+ "default": false+ },"total_prompt_tokens": {"type": "integer","title": "Total Prompt Tokens",