From 27094e5c3b322d63739b64b611efe72caed00a9f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 07:17:23 +0000 Subject: [PATCH 1/2] test(web): restoring a deep diff scroll position in a large cached diff loops Fails on main: each commit-phase flush loads only four more files, and with every patch cached each batch settles in the next commit, so 227 files go past React's nested update limit (#42669). Co-Authored-By: Claude Opus 5.5 --- .../useReviewFilePatches.browser.test.tsx | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/apps/web/src/components/diffs/useReviewFilePatches.browser.test.tsx b/apps/web/src/components/diffs/useReviewFilePatches.browser.test.tsx index 8311e9dffea8..fa864beec7fa 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.browser.test.tsx +++ b/apps/web/src/components/diffs/useReviewFilePatches.browser.test.tsx @@ -5,6 +5,7 @@ import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; import * as Option from "effect/Option"; import { expect, it, vi } from "vite-plus/test"; import { userEvent } from "vite-plus/test/browser"; +import { useLayoutEffect, useRef, useState } from "react"; import { act, render } from "../../../test/render"; type PatchAtom = Atom.Writable>; @@ -27,6 +28,7 @@ vi.mock("~/state/review", () => ({ }, })); +import { useCodeViewScrollRestore } from "./useCodeViewScrollRestore"; import { useReviewFilePatches } from "./useReviewFilePatches"; function source(paths: string[], hash: string): ReviewDiffPreviewSource { @@ -151,3 +153,99 @@ it("preserves deletion and addition patches for a file replaced by a symlink", a await expect.element(screen.getByText("new", { exact: true })).toBeVisible(); await expect.element(screen.getByText("Pending files: 0")).toBeVisible(); }); + +const FILE_HEIGHT = 100; + +function RestoringViewer({ + source, + onViewer, +}: { + source: ReviewDiffPreviewSource; + onViewer: (view: { onScroll: (top: number) => void }) => void; +}) { + const files = useReviewFilePatches({ + environmentId: EnvironmentId.make("environment"), + cwd: "/project", + source, + baseRef: "HEAD", + ignoreWhitespace: false, + theme: "dark", + previewInput: { cwd: "/project", metadataOnly: true }, + preview: null, + }); + const loaded = files.renderableFiles.length - files.remainingFileCount; + const loadedRef = useRef(0); + const [viewer] = useState(() => ({ + getHeight: () => 500, + getScrollHeight: () => loadedRef.current * FILE_HEIGHT, + })); + const restore = useCodeViewScrollRestore( + "big-diff", + !files.isPending && files.remainingFileCount === 0, + files.remainingFileCount > 0 ? files.loadNextFiles : null, + ); + // Stands in for the code view's ref callback. + useLayoutEffect(() => { + restore.attach({ getInstance: () => viewer, scrollTo: () => {} }); + onViewer({ onScroll: (top) => restore.onScroll(top, viewer) }); + }, [restore, viewer, onViewer]); + // Stands in for onPostRender, which the code view calls in the commit that renders a batch. + useLayoutEffect(() => { + loadedRef.current = loaded; + restore.flush(); + }); + return Loaded files: {loaded}; +} + +it("restores a deep scroll position in a large cached diff without an update loop", async () => { + state.patches.clear(); + const registry = AtomRegistry.make(); + using _registry = { [Symbol.dispose]: () => registry.dispose() }; + const paths = Array.from( + { length: 227 }, + (_, index) => `file-${String(index).padStart(3, "0")}.txt`, + ); + const metadata = source(paths, "large"); + const cacheKey = JSON.stringify([ + "environment", + "/project", + "working-tree", + "HEAD", + null, + "HEAD", + false, + ]); + for (const path of paths) { + state.patches.set( + JSON.stringify([cacheKey, path]), + Atom.make>( + AsyncResult.success({ + ...metadata, + diff: `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+new\n`, + files: metadata.files!.filter((file) => file.path === path), + }), + ).pipe(Atom.keepAlive), + ); + } + let view: { onScroll: (top: number) => void } | undefined; + const onViewer = (next: { onScroll: (top: number) => void }) => { + view = next; + }; + const errors: unknown[] = []; + const onError = (event: ErrorEvent) => errors.push(event.error); + window.addEventListener("error", onError); + using _listener = { [Symbol.dispose]: () => window.removeEventListener("error", onError) }; + const tree = (mount: number) => ( + + + + ); + await using screen = await render(tree(1)); + await expect.element(screen.getByText("Loaded files: 4")).toBeVisible(); + // Scrolled near the bottom of the diff, then the thread was left and shown again. + view!.onScroll(220 * FILE_HEIGHT); + await screen.rerender(tree(2)); + + await expect.element(screen.getByText(/Loaded files: (22[2-7])/)).toBeVisible(); + expect(errors).toEqual([]); +}); From d8c1128da8cb95b76f4797115c0556d71ea56376 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 25 Nov 2026 07:21:10 +0000 Subject: [PATCH 2/2] fix(web): restoring a deep Diff panel position no longer crashes on large cached diffs The scroll restore loaded the next batch of files from the code view's post-render callback, inside React's commit phase. With every patch cached, each four-file batch settled in the next commit, so a deep position in a diff of 200+ files chained past React's nested update limit (#42669). Queue the load in a microtask so each batch starts outside the commit that asked for it. Co-Authored-By: Claude Opus 5.5 --- .../components/diffs/useCodeViewScrollRestore.test.ts | 7 +++++-- .../src/components/diffs/useCodeViewScrollRestore.ts | 11 ++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/diffs/useCodeViewScrollRestore.test.ts b/apps/web/src/components/diffs/useCodeViewScrollRestore.test.ts index ec503dff2c83..bb4d280f056d 100644 --- a/apps/web/src/components/diffs/useCodeViewScrollRestore.test.ts +++ b/apps/web/src/components/diffs/useCodeViewScrollRestore.test.ts @@ -41,7 +41,7 @@ describe("createCodeViewScrollRestore", () => { expect(again.scrolls).toEqual([{ type: "position", position: 4032 }]); }); - it("loads more files until the position is reachable, then gives up once none remain", () => { + it("loads more files until the position is reachable, then gives up once none remain", async () => { const first = viewer(10_000); mount("thread-c", first).onScroll(4000, first.instance); @@ -53,7 +53,10 @@ describe("createCodeViewScrollRestore", () => { restore.onScroll(0, loading.instance); restore.flush(); expect(loading.scrolls).toEqual([]); - expect(batches).toBe(2); + // Batches load after the commit that asked for them, once per commit. + expect(batches).toBe(0); + await Promise.resolve(); + expect(batches).toBe(1); loading.instance.scrollHeight = 10_000; restore.flush(); restore.flush(); diff --git a/apps/web/src/components/diffs/useCodeViewScrollRestore.ts b/apps/web/src/components/diffs/useCodeViewScrollRestore.ts index 464df7a49836..4f5fe4cac333 100644 --- a/apps/web/src/components/diffs/useCodeViewScrollRestore.ts +++ b/apps/web/src/components/diffs/useCodeViewScrollRestore.ts @@ -26,11 +26,20 @@ export function createCodeViewScrollRestore() { let viewer: Viewer | undefined; let handle: ScrollRestoreHandle | null = null; let pending: number | null = null; + let loadQueued = false; const flush = () => { if (pending === null || !viewer || !handle || viewer.getHeight() === 0) return; const maxScrollTop = viewer.getScrollHeight() - viewer.getHeight(); if (maxScrollTop < pending && !settled) { - loadMore?.(); + // flush runs in the commit phase; loading there nests one update per cached batch, and + // React throws past 50 of them. + if (!loadQueued) { + loadQueued = true; + queueMicrotask(() => { + loadQueued = false; + loadMore?.(); + }); + } return; } // Pierre lands an unclamped position target one sticky header (32px) above it.