Skip to content

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

fix(frontend/copilot): drop the virtualizer's scroll-end update once the transcript unmounts (#15860)

Reinier van der Leerb7432f5ca6cb
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/__tests__/VirtualTranscript.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/__tests__/VirtualTranscript.test.tsx
index 20684f0011..bc74c3618e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/__tests__/VirtualTranscript.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/__tests__/VirtualTranscript.test.tsx
@@ -402,4 +402,20 @@ describe("VirtualTranscript", () => {
renderTranscript(messages);
expect(mountedIDs()).toHaveLength(60);
});
+
+ it("leaves no update behind when it unmounts mid-scroll", () => {
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
+ try {
+ renderTranscript(Array.from({ length: 500 }, (_, i) => message(i)));
+ scrollTo(bottomOffset(500));
+ cleanup();
+ // Vitest deletes `window` when it tears the environment down, and an
+ // update landing after that fails the run.
+ vi.stubGlobal("window", undefined);
+ vi.advanceTimersByTime(1_000);
+ } finally {
+ vi.unstubAllGlobals();
+ vi.useRealTimers();
+ }
+ });
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useVirtualTranscript.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useVirtualTranscript.ts
index 43656953f7..e4d083b8e6 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useVirtualTranscript.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useVirtualTranscript.ts
@@ -1,6 +1,10 @@
"use client";
-import { useVirtualizer } from "@tanstack/react-virtual";
+import {
+ observeElementOffset,
+ useVirtualizer,
+ type Virtualizer,
+} from "@tanstack/react-virtual";
import {
useCallback,
useLayoutEffect,
@@ -78,6 +82,7 @@ export function useVirtualTranscript({
getScrollElement: () => scrollRef.current,
estimateSize: (index) => estimateRowHeight(rowsRef.current[index].message),
getItemKey,
+ observeElementOffset: observeOffsetUntilUnsubscribed,
overscan: OVERSCAN_ROWS,
gap,
scrollMargin,
@@ -214,3 +219,19 @@ function useJumpToMessage({
};
}, [jumpRef, rowsRef, virtualizer]);
}
+
+// virtual-core 3.13 leaves its 150 ms scroll-end timer running on unsubscribe,
+// so it updates an unmounted list; 3.17.8 (react-virtual 3.14.10) cancels it.
+function observeOffsetUntilUnsubscribed(
+ instance: Virtualizer<HTMLElement, Element>,
+ cb: (offset: number, isScrolling: boolean) => void,
+) {
+ let isSubscribed = true;
+ const unsubscribe = observeElementOffset(instance, (offset, isScrolling) => {
+ if (isSubscribed) cb(offset, isScrolling);
+ });
+ return () => {
+ isSubscribed = false;
+ unsubscribe?.();
+ };
+}