Skip to content

Commit b73093b

Browse files
committed
fix(web): cap list layout passes to prevent React error 185
- Defer excess row measurements to the next frame - Add a regression test for timelines with large height differences
1 parent defb7e6 commit b73093b

3 files changed

Lines changed: 123 additions & 15 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.browser.test.tsx‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EnvironmentId, MessageId, RunId, ScheduledTaskId } from "@t3tools/contracts";
22
import type { LegendListRef } from "@legendapp/list/react";
3-
import { createRef, type ComponentProps, type ReactElement } from "react";
3+
import { createRef, Profiler, type ComponentProps, type ReactElement } from "react";
44
import { describe, expect, it, vi } from "vite-plus/test";
55

66
import "~/index.css";
@@ -129,6 +129,50 @@ const TASK_ID = ScheduledTaskId.make("task-1");
129129
const AUTOMATION_PROMPT = "Triage the inbox\n\nLabel every new issue and close duplicates.";
130130

131131
describe("MessagesTimeline messages", () => {
132+
it("settles when equally long messages have very different rendered heights", async () => {
133+
const props = baseProps();
134+
const tall = message("a\n\n".repeat(6000), { role: "assistant" }, "tall");
135+
const earlier = Array.from({ length: 80 }, (_, index) =>
136+
message(
137+
`<details><summary>Reply ${index}</summary>${"compact ".repeat(2250)}</details>`,
138+
{ role: "assistant" },
139+
`earlier-${index}`,
140+
),
141+
);
142+
let nestedCommits = 0;
143+
let maxNestedCommits = 0;
144+
const onRender = (_id: string, phase: string) => {
145+
nestedCommits = phase === "nested-update" ? nestedCommits + 1 : 0;
146+
maxNestedCommits = Math.max(maxNestedCommits, nestedCommits);
147+
};
148+
const timeline = (entries: Entry[]) => (
149+
<Profiler id="timeline" onRender={onRender}>
150+
<MessagesTimeline {...props} timelineEntries={entries} />
151+
</Profiler>
152+
);
153+
await using screen = await renderElement(timeline([tall]));
154+
await vi.waitFor(() =>
155+
expect(props.listRef.current?.getState().sizeAtIndex(0)).toBeGreaterThan(100_000),
156+
);
157+
const viewport = props.listRef.current!.getScrollableNode()!;
158+
await act(() => {
159+
viewport.scrollTop = 0;
160+
viewport.dispatchEvent(new Event("scroll"));
161+
});
162+
await screen.rerender(timeline([...earlier, tall]));
163+
await act(() => {
164+
void props.listRef.current!.scrollToIndex({ index: 0, animated: false });
165+
});
166+
await vi.waitFor(async () => {
167+
await act(() => {
168+
viewport.scrollTop = 0;
169+
viewport.dispatchEvent(new Event("scroll"));
170+
});
171+
expect(screen.text().includes("Reply 0")).toBe(true);
172+
});
173+
expect(maxNestedCommits).toBeLessThan(20);
174+
});
175+
132176
it("preserves XML-like tags and comparisons in user messages as text", async () => {
133177
const source = [
134178
'Without reading a file, do you have <global-agent-instructions scope="workspace">',

‎patches/@[email protected]‎

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ index ce1fe00001c9e5aee6c6ea8bb2d4757d4586d002..ca6e596aa53327a097a388ffd545a4c6
269269
* Number of columns to render items in.
270270
* @default 1
271271
diff --git a/react-native.js b/react-native.js
272-
index b3c5a306b293f797a8b338adfca3060c0f6db22b..3c67a536d850d8c508336cf0f10c0c898ea67f12 100644
272+
index b3c5a306b293f797a8b338adfca3060c0f6db22b..d7f59b63cd4982dfc9c347732caa77e0974864df 100644
273273
--- a/react-native.js
274274
+++ b/react-native.js
275275
@@ -717,6 +717,15 @@ function hasActiveInitialScroll(state) {
@@ -909,7 +909,7 @@ index b3c5a306b293f797a8b338adfca3060c0f6db22b..3c67a536d850d8c508336cf0f10c0c89
909909
recycleItems,
910910
refreshControl: refreshControlElement ? stylePaddingTopState > 0 ? React2__namespace.cloneElement(refreshControlElement, {
911911
diff --git a/react-native.mjs b/react-native.mjs
912-
index 40e87cda8c9bc79a889e5542f29af429a24b24d4..c1b68df0f53d566c98a4820b39079396a09f1208 100644
912+
index 40e87cda8c9bc79a889e5542f29af429a24b24d4..c2ca39bab6ea1ebaaed25190a01e8cf2e395ece9 100644
913913
--- a/react-native.mjs
914914
+++ b/react-native.mjs
915915
@@ -696,6 +696,15 @@ function hasActiveInitialScroll(state) {
@@ -1619,7 +1619,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..d7cbe1cb43449cc29822b2646deabbdd
16191619
}
16201620
}
16211621
diff --git a/react.js b/react.js
1622-
index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68257ae9f1 100644
1622+
index 914d2dafaafa001c9ab6791a0a0581659295e333..199aae77b3620b7c4bb2ecd96b82978c0430059a 100644
16231623
--- a/react.js
16241624
+++ b/react.js
16251625
@@ -4702,7 +4702,8 @@ function maybeUpdateAnchoredEndSpace(ctx) {
@@ -1655,7 +1655,39 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68
16551655
setContainerLayoutBaseline(element, rect);
16561656
measurements.push({
16571657
containerId,
1658-
@@ -5651,6 +5659,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) {
1658+
@@ -4978,12 +4986,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo
1659+
}) {
1660+
const ctx = useStateContext();
1661+
const [containerLayoutEpoch] = useArr$(["containerLayoutEpoch"]);
1662+
+ const layoutPasses = React3__namespace.useRef(0);
1663+
React3__namespace.useLayoutEffect(() => {
1664+
- {
1665+
- const targetContainerIds = getContainerLayoutEffectScope(ctx);
1666+
- if (targetContainerIds !== void 0) {
1667+
- measureContainersInLayoutEffect(ctx, targetContainerIds);
1668+
- }
1669+
+ const targetContainerIds = getContainerLayoutEffectScope(ctx);
1670+
+ if (targetContainerIds === void 0) return;
1671+
+ if (layoutPasses.current === 0) {
1672+
+ queueMicrotask(() => { layoutPasses.current = 0; });
1673+
+ }
1674+
+ // Correcting large estimates can mount one row per commit. Bound the
1675+
+ // synchronous chain below React's nested-update limit, then keep measuring
1676+
+ // the latest pending rows on the next frame. Idle lists schedule no work.
1677+
+ if (layoutPasses.current++ < 8) {
1678+
+ measureContainersInLayoutEffect(ctx, targetContainerIds);
1679+
+ } else {
1680+
+ ctx.pendingContainerIds = targetContainerIds;
1681+
+ ctx.state.scheduledWork.frame(() => {
1682+
+ const pendingIds = getContainerLayoutEffectScope(ctx);
1683+
+ if (pendingIds !== void 0) {
1684+
+ measureContainersInLayoutEffect(ctx, pendingIds);
1685+
+ }
1686+
+ }, "containerLayout");
1687+
}
1688+
}, [ctx, containerLayoutEpoch]);
1689+
return children;
1690+
@@ -5651,6 +5673,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) {
16591691
});
16601692

16611693
// src/utils/reordering.ts
@@ -1674,7 +1706,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68
16741706
function sortDOMElements(container, indexByElement) {
16751707
const elements = Array.from(container.children);
16761708
if (elements.length <= 1) return elements;
1677-
@@ -5690,9 +5710,9 @@ function sortDOMElements(container, indexByElement) {
1709+
@@ -5690,9 +5724,9 @@ function sortDOMElements(container, indexByElement) {
16781710
}
16791711
}
16801712
if (nextStableElement) {
@@ -1686,7 +1718,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68
16861718
}
16871719
}
16881720
}
1689-
@@ -6446,8 +6466,8 @@ function ScrollAdjust() {
1721+
@@ -6446,8 +6480,8 @@ function ScrollAdjust() {
16901722
window.getComputedStyle(contentNode)[axis.paddingEndProp]
16911723
);
16921724
const temporaryPaddingEnd = `${(currentPaddingEnd || 0) + pad}px`;
@@ -1697,7 +1729,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68
16971729
scrollBy();
16981730
if (resetPaddingRafRef.current !== void 0) {
16991731
diff --git a/react.mjs b/react.mjs
1700-
index 95465f2ab89ce41a10553f58af83618f7310e83c..cff49dc5c769cab264b8527a268851295c44aa33 100644
1732+
index 95465f2ab89ce41a10553f58af83618f7310e83c..a123bd3e9df510100d44dfae1f59b87e2e456cfc 100644
17011733
--- a/react.mjs
17021734
+++ b/react.mjs
17031735
@@ -4681,7 +4681,8 @@ function maybeUpdateAnchoredEndSpace(ctx) {
@@ -1733,7 +1765,39 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..cff49dc5c769cab264b8527a26885129
17331765
setContainerLayoutBaseline(element, rect);
17341766
measurements.push({
17351767
containerId,
1736-
@@ -5630,6 +5638,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) {
1768+
@@ -4957,12 +4965,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo
1769+
}) {
1770+
const ctx = useStateContext();
1771+
const [containerLayoutEpoch] = useArr$(["containerLayoutEpoch"]);
1772+
+ const layoutPasses = React3.useRef(0);
1773+
React3.useLayoutEffect(() => {
1774+
- {
1775+
- const targetContainerIds = getContainerLayoutEffectScope(ctx);
1776+
- if (targetContainerIds !== void 0) {
1777+
- measureContainersInLayoutEffect(ctx, targetContainerIds);
1778+
- }
1779+
+ const targetContainerIds = getContainerLayoutEffectScope(ctx);
1780+
+ if (targetContainerIds === void 0) return;
1781+
+ if (layoutPasses.current === 0) {
1782+
+ queueMicrotask(() => { layoutPasses.current = 0; });
1783+
+ }
1784+
+ // Correcting large estimates can mount one row per commit. Bound the
1785+
+ // synchronous chain below React's nested-update limit, then keep measuring
1786+
+ // the latest pending rows on the next frame. Idle lists schedule no work.
1787+
+ if (layoutPasses.current++ < 8) {
1788+
+ measureContainersInLayoutEffect(ctx, targetContainerIds);
1789+
+ } else {
1790+
+ ctx.pendingContainerIds = targetContainerIds;
1791+
+ ctx.state.scheduledWork.frame(() => {
1792+
+ const pendingIds = getContainerLayoutEffectScope(ctx);
1793+
+ if (pendingIds !== void 0) {
1794+
+ measureContainersInLayoutEffect(ctx, pendingIds);
1795+
+ }
1796+
+ }, "containerLayout");
1797+
}
1798+
}, [ctx, containerLayoutEpoch]);
1799+
return children;
1800+
@@ -5630,6 +5652,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) {
17371801
});
17381802

17391803
// src/utils/reordering.ts
@@ -1752,7 +1816,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..cff49dc5c769cab264b8527a26885129
17521816
function sortDOMElements(container, indexByElement) {
17531817
const elements = Array.from(container.children);
17541818
if (elements.length <= 1) return elements;
1755-
@@ -5669,9 +5689,9 @@ function sortDOMElements(container, indexByElement) {
1819+
@@ -5669,9 +5703,9 @@ function sortDOMElements(container, indexByElement) {
17561820
}
17571821
}
17581822
if (nextStableElement) {
@@ -1764,7 +1828,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..cff49dc5c769cab264b8527a26885129
17641828
}
17651829
}
17661830
}
1767-
@@ -6425,8 +6445,8 @@ function ScrollAdjust() {
1831+
@@ -6425,8 +6459,8 @@ function ScrollAdjust() {
17681832
window.getComputedStyle(contentNode)[axis.paddingEndProp]
17691833
);
17701834
const temporaryPaddingEnd = `${(currentPaddingEnd || 0) + pad}px`;

‎pnpm-lock.yaml‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)