Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(app): open a thread at its newest message

M3gA-Mind040001f69e43
diff --git a/app/src/components/assistant-ui/thread.openScroll.test.tsx b/app/src/components/assistant-ui/thread.openScroll.test.tsx
new file mode 100644
index 0000000000..58d501be59
--- /dev/null
+++ b/app/src/components/assistant-ui/thread.openScroll.test.tsx
@@ -0,0 +1,207 @@
+/**
+ * Opening a thread must land on the newest message.
+ *
+ * This is driven through the REAL Redux cache rather than a hand-fed message
+ * array, because the cache is the whole reason the defect is intermittent.
+ * `useOpenHumanExternalStore` reads
+ * `state.thread.messagesByThreadId[threadId]`, so a thread visited earlier in
+ * the session hands its messages over on the very render the id changes — it
+ * never passes through the empty state that re-arms assistant-ui's
+ * `scrollToBottomOnInitialize` latch. A thread NOT yet cached does briefly
+ * empty, so it scrolls correctly even unfixed. A fix checked only against a
+ * fresh thread therefore looks right and fixes nothing, which is why the
+ * cached switch below is the load-bearing case.
+ *
+ * jsdom performs no layout and stubs `scrollTo` to a no-op, so the observable
+ * here is the imperative call and its argument, not a resulting `scrollTop`.
+ */
+import { AssistantUiRuntimeProvider } from '@/providers/AssistantUiRuntimeProvider';
+import chatRuntimeReducer from '@/store/chatRuntimeSlice';
+import threadReducer, { loadThreadMessages } from '@/store/threadSlice';
+import type { ThreadMessage } from '@/types/thread';
+import { configureStore } from '@reduxjs/toolkit';
+import { render } from '@testing-library/react';
+import { act } from 'react';
+import { Provider } from 'react-redux';
+import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from 'vitest';
+
+import { Thread } from './thread';
+
+vi.mock('@/services/api/threadApi', () => ({
+ threadApi: { getDerivedTranscript: vi.fn().mockResolvedValue({ items: [], nextCursor: null }) },
+}));
+
+function msg(id: string, sender: 'user' | 'agent', content: string): ThreadMessage {
+ return {
+ id,
+ content,
+ type: 'text',
+ extraMetadata: {},
+ sender,
+ createdAt: `2026-09-23T00:00:${id.padStart(2, '0')}Z`,
+ };
+}
+
+/** Seed a thread's messages exactly as a real fetch would, without the fetch. */
+function cacheThread(
+ store: ReturnType<typeof makeStore>,
+ threadId: string,
+ messages: ThreadMessage[]
+) {
+ act(() => {
+ store.dispatch({ type: loadThreadMessages.fulfilled.type, payload: { threadId, messages } });
+ });
+}
+
+function makeStore() {
+ return configureStore({ reducer: { thread: threadReducer, chatRuntime: chatRuntimeReducer } });
+}
+
+/**
+ * The viewport is created by `<Thread />` itself, so the spy has to be on the
+ * prototype. Height metrics are zero in jsdom; tests that care about the
+ * reader's position set them per-element.
+ */
+let scrollToSpy: MockInstance;
+let scrollIntoViewSpy: MockInstance;
+
+beforeEach(() => {
+ scrollToSpy = vi.spyOn(HTMLElement.prototype, 'scrollTo').mockImplementation(() => {});
+ scrollIntoViewSpy = vi.spyOn(Element.prototype, 'scrollIntoView').mockImplementation(() => {});
+});
+
+afterEach(() => {
+ scrollToSpy.mockRestore();
+ scrollIntoViewSpy.mockRestore();
+});
+
+function viewportOf(container: HTMLElement): HTMLElement {
+ const el = container.querySelector<HTMLElement>('[data-slot="aui_thread-viewport"]');
+ if (!el) throw new Error('viewport not rendered');
+ return el;
+}
+
+/** Did anything ask this viewport to jump to its own bottom? */
+function scrolledToBottom(viewport: HTMLElement): boolean {
+ // `mock.contexts`, not `mock.instances`: the latter only records calls made
+ // with `new`, so it is empty for a method spy and would make this vacuously
+ // false for every viewport.
+ return scrollToSpy.mock.calls.some((call, i) => {
+ if (scrollToSpy.mock.contexts[i] !== viewport) return false;
+ const options = call[0] as ScrollToOptions | undefined;
+ return typeof options === 'object' && options !== null && options.top === viewport.scrollHeight;
+ });
+}
+
+describe('opening a thread scrolls to the newest message', () => {
+ it('scrolls to the bottom when the last message is from the assistant', () => {
+ const store = makeStore();
+ cacheThread(store, 't-assistant', [
+ msg('1', 'user', 'question'),
+ msg('2', 'agent', 'a long answer'),
+ ]);
+
+ const { container } = render(
+ <Provider store={store}>
+ <AssistantUiRuntimeProvider threadId="t-assistant">
+ <Thread />
+ </AssistantUiRuntimeProvider>
+ </Provider>
+ );
+
+ expect(scrolledToBottom(viewportOf(container))).toBe(true);
+ });
+
+ it('scrolls when switching to an ALREADY-CACHED thread, which never empties', () => {
+ const store = makeStore();
+ cacheThread(store, 't-a', [msg('1', 'user', 'first thread')]);
+ cacheThread(store, 't-b', [msg('2', 'user', 'hi'), msg('3', 'agent', 'second thread')]);
+
+ const { container, rerender } = render(
+ <Provider store={store}>
+ <AssistantUiRuntimeProvider threadId="t-a">
+ <Thread />
+ </AssistantUiRuntimeProvider>
+ </Provider>
+ );
+
+ const viewport = viewportOf(container);
+ // The opening scroll for t-a is not what this test is about.
+ scrollToSpy.mockClear();
+
+ rerender(
+ <Provider store={store}>
+ <AssistantUiRuntimeProvider threadId="t-b">
+ <Thread />
+ </AssistantUiRuntimeProvider>
+ </Provider>
+ );
+
+ // Same viewport element across the switch — no remount laundered the state.
+ expect(viewportOf(container)).toBe(viewport);
+ // Both threads are cached, so t-b's messages were present on the render the
+ // id flipped: assistant-ui's latch never re-armed and only our own
+ // thread-keyed scroll can have fired.
+ expect(scrolledToBottom(viewport)).toBe(true);
+ });
+
+ it('does not yank a reader who has scrolled up when a new turn arrives', () => {
+ const store = makeStore();
+ cacheThread(store, 't-reader', [msg('1', 'user', 'q'), msg('2', 'agent', 'a')]);
+
+ const { container } = render(
+ <Provider store={store}>
+ <AssistantUiRuntimeProvider threadId="t-reader">
+ <Thread />
+ </AssistantUiRuntimeProvider>
+ </Provider>
+ );
+
+ // Put the reader far up the transcript: 1000px of content, 200px tall
+ // viewport, parked at the top — 800px from the bottom, well past the 80px
+ // follow threshold.
+ const viewport = viewportOf(container);
+ Object.defineProperty(viewport, 'scrollHeight', { value: 1000, configurable: true });
+ Object.defineProperty(viewport, 'clientHeight', { value: 200, configurable: true });
+ viewport.scrollTop = 0;
+ scrollToSpy.mockClear();
+ scrollIntoViewSpy.mockClear();
+
+ // A new user turn lands in the SAME thread — no thread switch.
+ cacheThread(store, 't-reader', [
+ msg('1', 'user', 'q'),
+ msg('2', 'agent', 'a'),
+ msg('4', 'user', 'follow-up'),
+ ]);
+
+ expect(scrollIntoViewSpy).not.toHaveBeenCalled();
+ expect(scrolledToBottom(viewport)).toBe(false);
+ });
+
+ it('still aligns a new turn for a reader who is already at the bottom', () => {
+ const store = makeStore();
+ cacheThread(store, 't-bottom', [msg('1', 'user', 'q'), msg('2', 'agent', 'a')]);
+
+ const { container } = render(
+ <Provider store={store}>
+ <AssistantUiRuntimeProvider threadId="t-bottom">
+ <Thread />
+ </AssistantUiRuntimeProvider>
+ </Provider>
+ );
+
+ const viewport = viewportOf(container);
+ Object.defineProperty(viewport, 'scrollHeight', { value: 1000, configurable: true });
+ Object.defineProperty(viewport, 'clientHeight', { value: 200, configurable: true });
+ viewport.scrollTop = 800; // pinned to the bottom
+ scrollIntoViewSpy.mockClear();
+
+ cacheThread(store, 't-bottom', [
+ msg('1', 'user', 'q'),
+ msg('2', 'agent', 'a'),
+ msg('4', 'user', 'follow-up'),
+ ]);
+
+ expect(scrollIntoViewSpy).toHaveBeenCalled();
+ });
+});
diff --git a/app/src/components/assistant-ui/thread.tsx b/app/src/components/assistant-ui/thread.tsx
index 5f005899dc..d1523b67d5 100644
--- a/app/src/components/assistant-ui/thread.tsx
+++ b/app/src/components/assistant-ui/thread.tsx
@@ -33,6 +33,7 @@ import {
useAuiEditCapabilities,
useAuiReloadCapability,
} from '@/features/conversations/components/aui/auiThreadState';
+import { useAuiThreadId } from '@/providers/AssistantUiRuntimeProvider';
import {
ActionBarMorePrimitive,
ActionBarPrimitive,
@@ -321,6 +322,8 @@ const ThreadRoot: FC<{
const viewportRef = useRef<HTMLDivElement>(null);
const messageGroupRef = useRef<HTMLDivElement>(null);
+ useOpenThreadAtBottom(viewportRef);
+
return (
<ThreadPrimitive.Root
className="aui-root aui-thread-root bg-background @container flex h-full flex-col"
@@ -387,6 +390,70 @@ const ThreadRoot: FC<{
);
};
+/**
+ * Opening a thread lands on its newest message.
+ *
+ * assistant-ui has two stock knobs for this and BOTH are inert here:
+ *
+ * - `scrollToBottomOnThreadSwitch` listens for `threads.selectionChanged`,
+ * which fires only when its `mainThreadId` changes. That id is
+ * `adapter.threadId ?? DEFAULT_THREAD_ID`, and `useOpenHumanExternalStore`
+ * returns no `threadId` — the real thread travels out-of-band through
+ * `AuiThreadIdContext` — so `mainThreadId` never leaves the default and the
+ * event never fires.
+ * - `scrollToBottomOnInitialize` latches on the first non-empty render and
+ * re-arms only while the thread has zero messages. `<AssistantUiChat>` is
+ * mounted without a `key`, so this viewport survives thread switches with
+ * that latch still set.
+ *
+ * The second one is why the defect is intermittent rather than total, and it
+ * is the case to keep in mind. `useOpenHumanExternalStore` reads
+ * `state.thread.messagesByThreadId[threadId]`, a cache cleared only on delete
+ * or sign-out, so a thread visited earlier this session hands its messages
+ * over on the very render the id changes: it never passes through the empty
+ * state that re-arms the latch, and the viewport keeps the PREVIOUS thread's
+ * `scrollTop`. A thread not yet cached does briefly read empty and therefore
+ * scrolls correctly even unfixed — so a fix checked only against a fresh
+ * thread looks right and fixes nothing.
+ *
+ * Hence: latch on the thread id rather than on emptiness. Nothing here is
+ * conditional on the reader's scroll position, unlike `ThreadBottomFollower`
+ * below — "don't yank the reader who scrolled up" is about a new turn arriving
+ * in the thread being read, and a scroll offset left over from a different
+ * thread is not a reading position worth restoring.
+ *
+ * This lives in `ThreadRoot`, which owns `viewportRef`, rather than in
+ * `ThreadBottomFollower`, which is handed it: a descendant's layout effect
+ * runs before its ancestor's ref is attached, so the follower sees
+ * `viewportRef.current === null` on the mount that matters and would burn the
+ * latch without scrolling.
+ */
+function useOpenThreadAtBottom(viewportRef: RefObject<HTMLDivElement | null>) {
+ const hasMessages = useAuiState(s => s.thread.messages.length > 0);
+ const threadId = useAuiThreadId();
+ // Which thread this viewport has already been dropped to the bottom for.
+ // `undefined` (nothing opened yet) is deliberately distinct from the
+ // `string | null` a thread id can be, so the initial value cannot collide
+ // with a genuine "no thread selected".
+ const openedThreadRef = useRef<string | null | undefined>(undefined);
+
+ useLayoutEffect(() => {
+ // Wait for the transcript: on the uncached path the messages arrive a tick
+ // after the id changes, and a scroll issued against an empty viewport goes
+ // nowhere. Leaving the latch alone here is what lets that second pass run.
+ if (!hasMessages) return;
+ if (openedThreadRef.current === threadId) return;
+ const viewport = viewportRef.current;
+ if (!viewport) return;
+
+ openedThreadRef.current = threadId;
+ // `behavior: 'instant'` overrides the viewport's `scroll-smooth` class:
+ // opening a thread should start at the bottom, not animate down through the
+ // entire history to get there.
+ viewport.scrollTo({ top: viewport.scrollHeight, behavior: 'instant' });
+ }, [hasMessages, threadId, viewportRef]);
+}
+
const FOLLOW_BOTTOM_THRESHOLD_PX = 80;
/**