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(frontend/copilot): load older chat messages reliably and preserve scrollback across turns (#12792)
Krzysztof Czerwinskic955b3901c26
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 5161103f4b..d12f97106b 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@@ -9,6 +9,7 @@ import {MessageActions,MessageContent,} from "@/components/ai-elements/message";+import { Button } from "@/components/atoms/Button/Button";import { LoadingSpinner } from "@/components/atoms/LoadingSpinner/LoadingSpinner";import { FileUIPart, UIDataTypes, UIMessage, UITools } from "ai";import { useEffect, useLayoutEffect, useRef } from "react";@@ -111,18 +112,26 @@ function extractGraphExecId(return null;}+// Max consecutive auto-triggered loads where the container remains+// non-scrollable afterwards. Prevents chewing through history on+// sessions whose every page collapses below viewport height. The+// manual "Load older messages" button always remains clickable.+const MAX_AUTO_FILL_ROUNDS = 3;+/**- * Triggers `onLoadMore` when scrolled near the top, and preserves the- * user's scroll position after older messages are prepended to the DOM.+ * Triggers `onLoadMore` when scrolled near the top, preserves the+ * user's scroll position after older messages are prepended, and+ * exposes a manual "Load older messages" button as a fallback when+ * auto-fill backs off or the container isn't scrollable.** Scroll preservation works by:- * 1. Capturing `scrollHeight` / `scrollTop` in the observer callback+ * 1. Capturing `scrollHeight` / `scrollTop` just before `onLoadMore`* (synchronous, before React re-renders).* 2. Restoring `scrollTop` in a `useLayoutEffect` keyed on* `messageCount` so it only fires when messages actually change* (not on intermediate renders like the loading-spinner toggle).*/-function LoadMoreSentinel({+export function LoadMoreSentinel({hasMore,isLoading,messageCount,@@ -138,33 +147,43 @@ function LoadMoreSentinel({onLoadMoreRef.current = onLoadMore;// Pre-mutation scroll snapshot, written synchronously before onLoadMoreconst scrollSnapshotRef = useRef({ scrollHeight: 0, scrollTop: 0 });+ // Consecutive auto-triggered loads that left the container non-scrollable+ const autoFillRoundsRef = useRef(0);+ // True if the pending load was triggered by the observer (not the button)+ const autoTriggeredRef = useRef(false);+ // Same-frame re-entry guard — the parent's `isLoading` flag lags by a+ // render, so the observer or button could otherwise fire a duplicate+ // load and overwrite the captured scroll snapshot before the first+ // load settles.+ const loadPendingRef = useRef(false);const { scrollRef } = useStickToBottomContext();- // IntersectionObserver to trigger load when sentinel is near viewport.- // Only fires when the container is actually scrollable to prevent- // exhausting all pages when content fits without scrolling.+ useEffect(() => {+ if (!isLoading) loadPendingRef.current = false;+ }, [isLoading]);++ function captureAndLoad(fromObserver: boolean) {+ if (loadPendingRef.current) return;+ loadPendingRef.current = true;+ const el = scrollRef.current;+ if (el) {+ scrollSnapshotRef.current = {+ scrollHeight: el.scrollHeight,+ scrollTop: el.scrollTop,+ };+ }+ autoTriggeredRef.current = fromObserver;+ onLoadMoreRef.current();+ }+useEffect(() => {if (!sentinelRef.current || !hasMore || isLoading) return;+ if (autoFillRoundsRef.current >= MAX_AUTO_FILL_ROUNDS) return;const observer = new IntersectionObserver(([entry]) => {if (!entry.isIntersecting) return;- const scrollParent =- sentinelRef.current?.closest('[role="log"]') ??- sentinelRef.current?.parentElement;- if (- scrollParent &&- scrollParent.scrollHeight <= scrollParent.clientHeight- )- return;- // Capture scroll metrics *before* the state update- const el = scrollRef.current;- if (el) {- scrollSnapshotRef.current = {- scrollHeight: el.scrollHeight,- scrollTop: el.scrollTop,- };- }- onLoadMoreRef.current();+ if (autoFillRoundsRef.current >= MAX_AUTO_FILL_ROUNDS) return;+ captureAndLoad(true);},{ rootMargin: "200px 0px 0px 0px" },);@@ -186,12 +205,40 @@ function LoadMoreSentinel({if (delta > 0) {el.scrollTop = prevTop + delta;}+ // Reset the auto-fill backoff whenever the container becomes+ // scrollable (from any load), so a manual button click can unstick+ // auto-fill after it has hit the cap. Only count non-scrollable+ // outcomes against the cap when the load itself was auto-triggered.+ if (el.scrollHeight > el.clientHeight) {+ autoFillRoundsRef.current = 0;+ } else if (autoTriggeredRef.current) {+ autoFillRoundsRef.current += 1;+ }scrollSnapshotRef.current = { scrollHeight: 0, scrollTop: 0 };+ autoTriggeredRef.current = false;}, [messageCount, scrollRef]);return (- <div ref={sentinelRef} className="flex justify-center py-1">- {isLoading && <LoadingSpinner className="h-5 w-5 text-neutral-400" />}+ <div+ ref={sentinelRef}+ className="flex flex-col items-center justify-center gap-2 py-1"+ >+ {isLoading ? (+ <LoadingSpinner+ data-testid="load-more-spinner"+ className="h-5 w-5 text-neutral-400"+ />+ ) : (+ hasMore && (+ <Button+ variant="ghost"+ size="small"+ onClick={() => captureAndLoad(false)}+ >+ Load older messages+ </Button>+ )+ )}</div>);}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.tsxnew file mode 100644index 0000000000..3cbf4cbe48--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/LoadMoreSentinel.test.tsx@@ -0,0 +1,310 @@+import {+ render,+ screen,+ fireEvent,+ cleanup,+} from "@/tests/integrations/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { LoadMoreSentinel } from "../ChatMessagesContainer";++const mockScrollEl = {+ scrollHeight: 100,+ scrollTop: 0,+ clientHeight: 500,+};++vi.mock("use-stick-to-bottom", () => ({+ useStickToBottomContext: () => ({ scrollRef: { current: mockScrollEl } }),+}));++type ObserverCallback = (entries: { isIntersecting: boolean }[]) => void;++class MockIntersectionObserver {+ static lastCallback: ObserverCallback | null = null;+ static lastOptions: IntersectionObserverInit | undefined = undefined;+ private callback: ObserverCallback;+ constructor(cb: ObserverCallback, options?: IntersectionObserverInit) {+ this.callback = cb;+ MockIntersectionObserver.lastCallback = cb;+ MockIntersectionObserver.lastOptions = options;+ }+ observe() {}+ disconnect() {}+ unobserve() {}+ takeRecords() {+ return [];+ }+ root = null;+ rootMargin = "";+ thresholds = [];+ fire(entries: { isIntersecting: boolean }[]) {+ this.callback(entries);+ }+}++describe("LoadMoreSentinel", () => {+ beforeEach(() => {+ mockScrollEl.scrollHeight = 100;+ mockScrollEl.scrollTop = 0;+ mockScrollEl.clientHeight = 500;+ MockIntersectionObserver.lastCallback = null;+ vi.stubGlobal("IntersectionObserver", MockIntersectionObserver);+ });++ afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+ });++ it("renders 'Load older messages' button when hasMore is true and not loading", () => {+ render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={5}+ onLoadMore={vi.fn()}+ />,+ );+ expect(+ screen.getByRole("button", { name: /load older messages/i }),+ ).toBeDefined();+ });++ it("calls onLoadMore when the button is clicked", () => {+ const onLoadMore = vi.fn();+ render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={5}+ onLoadMore={onLoadMore}+ />,+ );+ fireEvent.click(+ screen.getByRole("button", { name: /load older messages/i }),+ );+ expect(onLoadMore).toHaveBeenCalledTimes(1);+ });++ it("hides the button and shows a spinner while loading", () => {+ render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={true}+ messageCount={5}+ onLoadMore={vi.fn()}+ />,+ );+ expect(+ screen.queryByRole("button", { name: /load older messages/i }),+ ).toBeNull();+ expect(screen.getByTestId("load-more-spinner")).toBeDefined();+ });++ it("hides the button when hasMore is false", () => {+ render(+ <LoadMoreSentinel+ hasMore={false}+ isLoading={false}+ messageCount={5}+ onLoadMore={vi.fn()}+ />,+ );+ expect(+ screen.queryByRole("button", { name: /load older messages/i }),+ ).toBeNull();+ });++ it("triggers onLoadMore when the IntersectionObserver fires", () => {+ const onLoadMore = vi.fn();+ render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={5}+ onLoadMore={onLoadMore}+ />,+ );+ expect(MockIntersectionObserver.lastCallback).toBeDefined();+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ expect(onLoadMore).toHaveBeenCalledTimes(1);+ });++ it("ignores observer entries that are not intersecting", () => {+ const onLoadMore = vi.fn();+ render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={5}+ onLoadMore={onLoadMore}+ />,+ );+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: false }]);+ expect(onLoadMore).not.toHaveBeenCalled();+ });++ it("restores scroll position after older messages are prepended", () => {+ mockScrollEl.scrollHeight = 100;+ mockScrollEl.scrollTop = 0;+ const onLoadMore = vi.fn();+ const { rerender } = render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={5}+ onLoadMore={onLoadMore}+ />,+ );+ // Auto-fire via observer — this captures the snapshot (prev 100/0).+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ // Simulate DOM growing from prepended older messages.+ mockScrollEl.scrollHeight = 300;+ rerender(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={10}+ onLoadMore={onLoadMore}+ />,+ );+ // scrollTop should be restored to prev + delta = 0 + (300 - 100) = 200.+ expect(mockScrollEl.scrollTop).toBe(200);+ });++ it("ignores same-frame duplicate triggers until isLoading transitions", () => {+ const onLoadMore = vi.fn();+ const { rerender } = render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={5}+ onLoadMore={onLoadMore}+ />,+ );+ // Two observer fires back-to-back — the second must be a no-op while+ // the first load is still pending (isLoading hasn't propagated yet).+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ expect(onLoadMore).toHaveBeenCalledTimes(1);+ // A manual click in the same window is also blocked.+ fireEvent.click(+ screen.getByRole("button", { name: /load older messages/i }),+ );+ expect(onLoadMore).toHaveBeenCalledTimes(1);+ // Simulate parent flipping isLoading on then off — load cycle settled.+ rerender(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={true}+ messageCount={5}+ onLoadMore={onLoadMore}+ />,+ );+ rerender(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={6}+ onLoadMore={onLoadMore}+ />,+ );+ // Now a fresh trigger should fire again.+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ expect(onLoadMore).toHaveBeenCalledTimes(2);+ });++ function simulateLoadCycle(+ rerender: (ui: React.ReactElement) => void,+ props: {+ hasMore: boolean;+ messageCount: number;+ onLoadMore: () => void;+ },+ ) {+ // Parent pattern: isLoading goes true while fetching, then false with+ // a higher messageCount once new messages land.+ rerender(+ <LoadMoreSentinel+ hasMore={props.hasMore}+ isLoading={true}+ messageCount={props.messageCount - 1}+ onLoadMore={props.onLoadMore}+ />,+ );+ rerender(+ <LoadMoreSentinel+ hasMore={props.hasMore}+ isLoading={false}+ messageCount={props.messageCount}+ onLoadMore={props.onLoadMore}+ />,+ );+ }++ it("resets the auto-fill backoff once the container becomes scrollable via a manual click", () => {+ mockScrollEl.clientHeight = 1000;+ mockScrollEl.scrollHeight = 100;+ const onLoadMore = vi.fn();+ const { rerender } = render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={5}+ onLoadMore={onLoadMore}+ />,+ );+ for (let round = 1; round <= 3; round++) {+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ mockScrollEl.scrollHeight += 50;+ simulateLoadCycle(rerender, {+ hasMore: true,+ messageCount: 5 + round,+ onLoadMore,+ });+ }+ fireEvent.click(+ screen.getByRole("button", { name: /load older messages/i }),+ );+ mockScrollEl.scrollHeight = 2000;+ simulateLoadCycle(rerender, {+ hasMore: true,+ messageCount: 9,+ onLoadMore,+ });+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ expect(onLoadMore).toHaveBeenCalledTimes(5);+ });++ it("stops auto-triggering after 3 non-scrollable rounds but keeps the manual button working", () => {+ mockScrollEl.clientHeight = 1000;+ mockScrollEl.scrollHeight = 100;+ const onLoadMore = vi.fn();+ const { rerender } = render(+ <LoadMoreSentinel+ hasMore={true}+ isLoading={false}+ messageCount={5}+ onLoadMore={onLoadMore}+ />,+ );+ for (let round = 1; round <= 3; round++) {+ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ mockScrollEl.scrollHeight += 50;+ simulateLoadCycle(rerender, {+ hasMore: true,+ messageCount: 5 + round,+ onLoadMore,+ });+ }+ expect(onLoadMore).toHaveBeenCalledTimes(3);++ MockIntersectionObserver.lastCallback?.([{ isIntersecting: true }]);+ expect(onLoadMore).toHaveBeenCalledTimes(3);++ fireEvent.click(+ screen.getByRole("button", { name: /load older messages/i }),+ );+ expect(onLoadMore).toHaveBeenCalledTimes(4);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.tsindex 13efd957f9..313b2d5fb8 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts@@ -41,7 +41,23 @@ export function useLoadMoreMessages({const prevSessionIdRef = useRef(sessionId);const prevInitialOldestRef = useRef(initialOldestSequence);- // Sync initial values from parent when they change+ // Sync initial values from parent when they change.+ //+ // 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+ // 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+ // duplicates.+ //+ // Instead: once any older page is loaded, preserve local state across+ // refetches. The local cursor (`oldestSequence`) still points to the+ // oldest message we've explicitly loaded, so the next `loadMore`+ // fetches cleanly before it. Any messages between the refetched+ // initial window and the older pages are covered by AI SDK's+ // retained state in `currentMessages`.useEffect(() => {if (prevSessionIdRef.current !== sessionId) {// Session changed — full reset@@ -54,23 +70,14 @@ export function useLoadMoreMessages({isLoadingMoreRef.current = false;consecutiveErrorsRef.current = 0;epochRef.current += 1;- } else if (- prevInitialOldestRef.current !== initialOldestSequence &&- olderRawMessages.length > 0- ) {- // Same session but initial window shifted (e.g. new messages arrived) —- // clear paged state to avoid gaps/duplicates- prevInitialOldestRef.current = initialOldestSequence;- setOlderRawMessages([]);- setOldestSequence(initialOldestSequence);- setHasMore(initialHasMore);- setIsLoadingMore(false);- isLoadingMoreRef.current = false;- consecutiveErrorsRef.current = 0;- epochRef.current += 1;- } else {- // Update from parent when initial data changes (e.g. refetch)- prevInitialOldestRef.current = initialOldestSequence;+ return;+ }++ prevInitialOldestRef.current = initialOldestSequence;++ // If we haven't paged back yet, mirror the parent so the first+ // `loadMore` starts from the correct cursor.+ if (olderRawMessages.length === 0) {setOldestSequence(initialOldestSequence);setHasMore(initialHasMore);}