TypeScript
fix(web): cap list layout passes to prevent React error 185 (#23315)
Yash Singh05b697f6a201
diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsxindex bc04aeeb4d5..227a0cda730 100644--- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx@@ -1,6 +1,6 @@import { EnvironmentId, MessageId, RunId, ScheduledTaskId } from "@t3tools/contracts";import type { LegendListRef } from "@legendapp/list/react";-import { createRef, type ComponentProps, type ReactElement } from "react";+import { createRef, Profiler, type ComponentProps, type ReactElement } from "react";import { describe, expect, it, vi } from "vite-plus/test";import "~/index.css";@@ -129,6 +129,51 @@ const TASK_ID = ScheduledTaskId.make("task-1");const AUTOMATION_PROMPT = "Triage the inbox\n\nLabel every new issue and close duplicates.";describe("MessagesTimeline messages", () => {+ it("settles when equally long messages have very different rendered heights", async () => {+ const props = baseProps();+ const tall = message("a\n\n".repeat(6000), { role: "assistant" }, "tall");+ const earlier = Array.from({ length: 80 }, (_, index) =>+ message(+ `<details><summary>Reply ${index}</summary>${"compact ".repeat(2250)}</details>`,+ { role: "assistant" },+ `earlier-${index}`,+ ),+ );+ let nestedCommits = 0;+ let maxNestedCommits = 0;+ const onRender = (_id: string, phase: string) => {+ nestedCommits = phase === "nested-update" ? nestedCommits + 1 : 0;+ maxNestedCommits = Math.max(maxNestedCommits, nestedCommits);+ };+ const timeline = (entries: Entry[]) => (+ <Profiler id="timeline" onRender={onRender}>+ <MessagesTimeline {...props} timelineEntries={entries} />+ </Profiler>+ );+ await using screen = await renderElement(timeline([tall]));+ await vi.waitFor(() =>+ expect(props.listRef.current?.getState().sizeAtIndex(0)).toBeGreaterThan(100_000),+ );+ const viewport = props.listRef.current!.getScrollableNode()!;+ await act(() => {+ viewport.scrollTop = 0;+ viewport.dispatchEvent(new Event("scroll"));+ });+ await screen.rerender(timeline([...earlier, tall]));+ await act(() => {+ void props.listRef.current!.scrollToIndex({ index: 0, animated: false });+ });+ await vi.waitFor(async () => {+ await act(() => {+ viewport.scrollTop = 0;+ viewport.dispatchEvent(new Event("scroll"));+ });+ expect(screen.text().includes("Reply 0")).toBe(true);+ expect(props.listRef.current!.getState().sizeAtIndex(0)).toBeGreaterThan(0);+ });+ expect(maxNestedCommits).toBeLessThan(20);+ });+it("preserves XML-like tags and comparisons in user messages as text", async () => {const source = ['Without reading a file, do you have <global-agent-instructions scope="workspace">',diff --git a/patches/@[email protected] b/patches/@[email protected]index a9ec41463fd..58f642e725e 100644--- a/patches/@[email protected]+++ b/patches/@[email protected]@@ -269,7 +269,7 @@ index ce1fe00001c9e5aee6c6ea8bb2d4757d4586d002..ca6e596aa53327a097a388ffd545a4c6* Number of columns to render items in.* @default 1diff --git a/react-native.js b/react-native.js-index b3c5a306b293f797a8b338adfca3060c0f6db22b..3c67a536d850d8c508336cf0f10c0c898ea67f12 100644+index b3c5a306b293f797a8b338adfca3060c0f6db22b..d7f59b63cd4982dfc9c347732caa77e0974864df 100644--- a/react-native.js+++ b/react-native.js@@ -717,6 +717,15 @@ function hasActiveInitialScroll(state) {@@ -909,7 +909,7 @@ index b3c5a306b293f797a8b338adfca3060c0f6db22b..3c67a536d850d8c508336cf0f10c0c89recycleItems,refreshControl: refreshControlElement ? stylePaddingTopState > 0 ? React2__namespace.cloneElement(refreshControlElement, {diff --git a/react-native.mjs b/react-native.mjs-index 40e87cda8c9bc79a889e5542f29af429a24b24d4..c1b68df0f53d566c98a4820b39079396a09f1208 100644+index 40e87cda8c9bc79a889e5542f29af429a24b24d4..c2ca39bab6ea1ebaaed25190a01e8cf2e395ece9 100644--- a/react-native.mjs+++ b/react-native.mjs@@ -696,6 +696,15 @@ function hasActiveInitialScroll(state) {@@ -1619,7 +1619,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..d7cbe1cb43449cc29822b2646deabbdd}}diff --git a/react.js b/react.js-index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68257ae9f1 100644+index 914d2dafaafa001c9ab6791a0a0581659295e333..199aae77b3620b7c4bb2ecd96b82978c0430059a 100644--- a/react.js+++ b/react.js@@ -4702,7 +4702,8 @@ function maybeUpdateAnchoredEndSpace(ctx) {@@ -1655,7 +1655,39 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68setContainerLayoutBaseline(element, rect);measurements.push({containerId,-@@ -5651,6 +5659,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) {+@@ -4978,12 +4986,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo+ }) {+ const ctx = useStateContext();+ const [containerLayoutEpoch] = useArr$(["containerLayoutEpoch"]);++ const layoutPasses = React3__namespace.useRef(0);+ React3__namespace.useLayoutEffect(() => {+- {+- const targetContainerIds = getContainerLayoutEffectScope(ctx);+- if (targetContainerIds !== void 0) {+- measureContainersInLayoutEffect(ctx, targetContainerIds);+- }++ const targetContainerIds = getContainerLayoutEffectScope(ctx);++ if (targetContainerIds === void 0) return;++ if (layoutPasses.current === 0) {++ queueMicrotask(() => { layoutPasses.current = 0; });++ }++ // Correcting large estimates can mount one row per commit. Bound the++ // synchronous chain below React's nested-update limit, then keep measuring++ // the latest pending rows on the next frame. Idle lists schedule no work.++ if (layoutPasses.current++ < 8) {++ measureContainersInLayoutEffect(ctx, targetContainerIds);++ } else {++ ctx.pendingContainerIds = targetContainerIds;++ ctx.state.scheduledWork.frame(() => {++ const pendingIds = getContainerLayoutEffectScope(ctx);++ if (pendingIds !== void 0) {++ measureContainersInLayoutEffect(ctx, pendingIds);++ }++ }, "containerLayout");+ }+ }, [ctx, containerLayoutEpoch]);+ return children;+@@ -5651,6 +5673,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) {});// src/utils/reordering.ts@@ -1674,7 +1706,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68function sortDOMElements(container, indexByElement) {const elements = Array.from(container.children);if (elements.length <= 1) return elements;-@@ -5690,9 +5710,9 @@ function sortDOMElements(container, indexByElement) {+@@ -5690,9 +5724,9 @@ function sortDOMElements(container, indexByElement) {}}if (nextStableElement) {@@ -1686,7 +1718,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68}}}-@@ -6446,8 +6466,8 @@ function ScrollAdjust() {+@@ -6446,8 +6480,8 @@ function ScrollAdjust() {window.getComputedStyle(contentNode)[axis.paddingEndProp]);const temporaryPaddingEnd = `${(currentPaddingEnd || 0) + pad}px`;@@ -1697,7 +1729,7 @@ index 914d2dafaafa001c9ab6791a0a0581659295e333..86dbd9a5c7345577e4a8a435041ecb68scrollBy();if (resetPaddingRafRef.current !== void 0) {diff --git a/react.mjs b/react.mjs-index 95465f2ab89ce41a10553f58af83618f7310e83c..cff49dc5c769cab264b8527a268851295c44aa33 100644+index 95465f2ab89ce41a10553f58af83618f7310e83c..a123bd3e9df510100d44dfae1f59b87e2e456cfc 100644--- a/react.mjs+++ b/react.mjs@@ -4681,7 +4681,8 @@ function maybeUpdateAnchoredEndSpace(ctx) {@@ -1733,7 +1765,39 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..cff49dc5c769cab264b8527a26885129setContainerLayoutBaseline(element, rect);measurements.push({containerId,-@@ -5630,6 +5638,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) {+@@ -4957,12 +4965,26 @@ var ContainerLayoutCoordinator = typedMemo(function ContainerLayoutCoordinatorCo+ }) {+ const ctx = useStateContext();+ const [containerLayoutEpoch] = useArr$(["containerLayoutEpoch"]);++ const layoutPasses = React3.useRef(0);+ React3.useLayoutEffect(() => {+- {+- const targetContainerIds = getContainerLayoutEffectScope(ctx);+- if (targetContainerIds !== void 0) {+- measureContainersInLayoutEffect(ctx, targetContainerIds);+- }++ const targetContainerIds = getContainerLayoutEffectScope(ctx);++ if (targetContainerIds === void 0) return;++ if (layoutPasses.current === 0) {++ queueMicrotask(() => { layoutPasses.current = 0; });++ }++ // Correcting large estimates can mount one row per commit. Bound the++ // synchronous chain below React's nested-update limit, then keep measuring++ // the latest pending rows on the next frame. Idle lists schedule no work.++ if (layoutPasses.current++ < 8) {++ measureContainersInLayoutEffect(ctx, targetContainerIds);++ } else {++ ctx.pendingContainerIds = targetContainerIds;++ ctx.state.scheduledWork.frame(() => {++ const pendingIds = getContainerLayoutEffectScope(ctx);++ if (pendingIds !== void 0) {++ measureContainersInLayoutEffect(ctx, pendingIds);++ }++ }, "containerLayout");+ }+ }, [ctx, containerLayoutEpoch]);+ return children;+@@ -5630,6 +5652,18 @@ var ContainerSlot = typedMemo(function ContainerSlot2(props) {});// src/utils/reordering.ts@@ -1752,7 +1816,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..cff49dc5c769cab264b8527a26885129function sortDOMElements(container, indexByElement) {const elements = Array.from(container.children);if (elements.length <= 1) return elements;-@@ -5669,9 +5689,9 @@ function sortDOMElements(container, indexByElement) {+@@ -5669,9 +5703,9 @@ function sortDOMElements(container, indexByElement) {}}if (nextStableElement) {@@ -1764,7 +1828,7 @@ index 95465f2ab89ce41a10553f58af83618f7310e83c..cff49dc5c769cab264b8527a26885129}}}-@@ -6425,8 +6445,8 @@ function ScrollAdjust() {+@@ -6425,8 +6459,8 @@ function ScrollAdjust() {window.getComputedStyle(contentNode)[axis.paddingEndProp]);const temporaryPaddingEnd = `${(currentPaddingEnd || 0) + pad}px`;diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 232a7da308c..90fdc80f171 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -333,7 +333,7 @@ patchedDependencies:'@expo/[email protected]': 90fa980e8b6480631fb2fbb04559834e3c612db40c550cd828f11be297d8d760'@expo/[email protected]': 5a26480373850c1a7943919aa21f904151b335a0cb01042109e0746def37923e'@ff-labs/[email protected]': cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c- '@legendapp/[email protected]': 7b62e3a8814279aef02146d986ba3b50763ffd04e38ff36ce132e2065600ade3+ '@legendapp/[email protected]': 011a862a7116ec5b5f4bba17d37bc58c9b5a8f64a6a9f527345f316ce9c96e63'@pierre/[email protected]': 2ec81ba1e8ce4681095a3473c2bf78e630417740c622720ef624febb467c25fc'@react-native-ai/[email protected]': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d'@react-native-menu/[email protected]': a4866ecfcd44f318043de839d20110c17e14909004900f002ffe12050376f2ba@@ -515,7 +515,7 @@ importers:version: 58.0.20(patch_hash=5a26480373850c1a7943919aa21f904151b335a0cb01042109e0746def37923e)(@babel/[email protected]([email protected]))([email protected])([email protected]([email protected]))([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected]))([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])'@legendapp/list':specifier: 'catalog:'- version: 3.3.5(patch_hash=7b62e3a8814279aef02146d986ba3b50763ffd04e38ff36ce132e2065600ade3)([email protected]([email protected]))([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])+ version: 3.3.5(patch_hash=011a862a7116ec5b5f4bba17d37bc58c9b5a8f64a6a9f527345f316ce9c96e63)([email protected]([email protected]))([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])'@material/material-color-utilities':specifier: 0.3.0version: 0.3.0@@ -1012,7 +1012,7 @@ importers:version: 0.9.0'@legendapp/list':specifier: 'catalog:'- version: 3.3.5(patch_hash=7b62e3a8814279aef02146d986ba3b50763ffd04e38ff36ce132e2065600ade3)([email protected]([email protected]))([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])+ version: 3.3.5(patch_hash=011a862a7116ec5b5f4bba17d37bc58c9b5a8f64a6a9f527345f316ce9c96e63)([email protected]([email protected]))([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])'@noble/hashes':specifier: 'catalog:'version: 1.8.0@@ -14926,7 +14926,7 @@ snapshots:dependencies:jsbi: 4.3.2- '@legendapp/[email protected](patch_hash=7b62e3a8814279aef02146d986ba3b50763ffd04e38ff36ce132e2065600ade3)([email protected]([email protected]))([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])':+ '@legendapp/[email protected](patch_hash=011a862a7116ec5b5f4bba17d37bc58c9b5a8f64a6a9f527345f316ce9c96e63)([email protected]([email protected]))([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])':dependencies:react: 19.3.0use-sync-external-store: 1.6.0([email protected])