@devries-cameron20 / t3code-1 / commits / 1114c3815b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): scrolling a huge thread's history no longer loops the message list (#21269)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 18, 2026 at 13:04 UTC 1114c3815ba16c585f89e99147a952a92df5c24c
2 files changed +53 -2
apps/web/src/components/chat/MessagesTimeline.browser.test.tsx
+43 -1
@@ -8,7 +8,7 @@ import {
8 ThreadId,
9 } from "@t3tools/contracts";
10 import type { LegendListRef } from "@legendapp/list/react";
11 -import { act, createRef, useState, type ComponentProps, type ReactElement } from "react";
11 +import { act, createRef, Profiler, useState, type ComponentProps, type ReactElement } from "react";
12 import { createRoot, type Root } from "react-dom/client";
13 import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
14 import { userEvent } from "vite-plus/test/browser";
@@ -1165,6 +1165,48 @@ describe("MessagesTimeline scroll position", () => {
1165 return props.listRef.current!.getScrollableNode()!;
1166 }
1167
1168 + it("settles when earlier short replies load above long ones", async () => {
1169 + const props = baseProps();
1170 + const short = Array.from({ length: 80 }, (_, index) =>
1171 + message(`Short reply ${index}`, { role: "assistant" }, `short-${index}`),
1172 + );
1173 + const long = Array.from({ length: 4 }, (_, index) =>
1174 + message(
1175 + `${"Long handbook paragraph. ".repeat(40)}\n\n`.repeat(60),
1176 + { role: "assistant" },
1177 + `long-${index}`,
1178 + ),
1179 + );
1180 + let nestedCommits = 0;
1181 + let maxNestedCommits = 0;
1182 + const onRender = (_id: string, phase: string) => {
1183 + nestedCommits = phase === "nested-update" ? nestedCommits + 1 : 0;
1184 + maxNestedCommits = Math.max(maxNestedCommits, nestedCommits);
1185 + };
1186 + const timeline = (entries: Entry[]) => (
1187 + <Profiler id="timeline" onRender={onRender}>
1188 + <MessagesTimeline {...props} timelineEntries={entries} />
1189 + </Profiler>
1190 + );
1191 + await renderElement(timeline(long));
1192 + const viewport = await waitForList(props);
1193 + await act(async () => {
1194 + viewport.scrollTop = 0;
1195 + viewport.dispatchEvent(new Event("scroll"));
1196 + });
1197 +
1198 + maxNestedCommits = 0;
1199 + await act(async () => roots.at(-1)!.render(timeline([...short, ...long])));
1200 + await vi.waitFor(async () => {
1201 + await act(async () => {
1202 + viewport.scrollTop = 0;
1203 + viewport.dispatchEvent(new Event("scroll"));
1204 + });
1205 + expect(text()).toContain("Short reply 0");
1206 + });
1207 + expect(maxNestedCommits).toBeLessThan(8);
1208 + });
1209 +
1210 it("remembers a manual reading position when the chat closes", async () => {
1211 const props = baseProps();
1212 function Chat() {
apps/web/src/components/chat/MessagesTimeline.tsx
+10 -1
@@ -1540,8 +1540,17 @@ function keyExtractor(item: MessagesTimelineRow) {
1540 return item.id;
1541 }
1542
1543 +// LegendList estimates unmeasured rows from the running average size of their
1544 +// type. One type for every assistant reply lets a few very long replies, or
1545 +// one that is streaming, lift the estimate for short replies to thousands of
1546 +// pixels. Rows then mount one at a time, each measurement shrinks the list
1547 +// and mounts the next row in the same commit, and scrolling into unmeasured
1548 +// history exceeds React's nested update limit. Doubling-length buckets keep
1549 +// each average within about 2x of the rows it estimates.
1550 function getItemType(item: MessagesTimelineRow) {
1544 - return item.kind === "message" ? `message:${item.message.role}` : item.kind;
1551 + if (item.kind !== "message") return item.kind;
1552 + const lengthBucket = Math.max(0, Math.floor(Math.log2(item.message.text.length / 1000)));
1553 + return `message:${item.message.role}:${lengthBucket}`;
1554 }
1555
1556 interface TimelinePositionState {