@devries-cameron20 / t3code-1 / commits / e5b5da3a7c
Fork 0

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

fix(web): reopening a guide with many cached diff pages no longer crashes (#27550)

Julius Marminge committed Nov 11, 2026 at 15:37 UTC e5b5da3a7cd629c931ae71306595ae121dc2360b
2 files changed +99 -24
apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx
+77 -2
@@ -6,7 +6,7 @@ import { expect, it, vi } from "vite-plus/test";
6 import { act, render } from "../../../../test/render";
7
8 const state = vi.hoisted(() => ({
9 - pages: new Map<string | null, Atom.Writable<AsyncResult.AsyncResult<PullRequestDiffResult>>>(),
9 + pages: new Map<string | null, Atom.Atom<AsyncResult.AsyncResult<PullRequestDiffResult>>>(),
10 }));
11 vi.mock("~/state/pullRequests", () => ({
12 pullRequestEnvironment: {
@@ -33,6 +33,10 @@ function Probe({ wantedPaths }: { wantedPaths: string[] }) {
33 );
34 }
35
36 +function setup() {
37 + state.pages.clear();
38 +}
39 +
40 function page(path: string, value: string, nextCursor: string | null): PullRequestDiffResult {
41 return {
42 patch: `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+${value}\n`,
@@ -46,7 +50,7 @@ it.each([
50 { name: "ends before a newly added file", path: "other.ts", cursor: null },
51 { name: "has an obsolete next cursor", path: "other.ts", cursor: "old-next" },
52 ])("waits for a cached page that $name to settle", async ({ path, cursor }) => {
49 - state.pages.clear();
53 + setup();
54 await using stack = new AsyncDisposableStack();
55 const registry = stack.adopt(AtomRegistry.make(), (value) => value.dispose());
56 const first = Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(
@@ -68,3 +72,74 @@ it.each([
72 await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null))));
73 expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH\n"]] });
74 });
75 +
76 +it("walks more cached pages than React's re-render limit and stops at the requested file", async () => {
77 + setup();
78 + await using stack = new AsyncDisposableStack();
79 + const registry = stack.adopt(AtomRegistry.make(), (value) => value.dispose());
80 + const pageCount = 30;
81 + for (let index = 0; index < pageCount; index++) {
82 + const last = index === pageCount - 1;
83 + state.pages.set(
84 + index === 0 ? null : `cursor-${index}`,
85 + Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(
86 + AsyncResult.success(
87 + page(last ? "wanted.ts" : `file-${index}.ts`, "FOUND", `cursor-${index + 1}`),
88 + ),
89 + ),
90 + );
91 + }
92 + let pastPageReads = 0;
93 + state.pages.set(
94 + `cursor-${pageCount}`,
95 + Atom.make((): AsyncResult.AsyncResult<PullRequestDiffResult> => {
96 + pastPageReads++;
97 + return AsyncResult.initial(true);
98 + }),
99 + );
100 + await using screen = await render(
101 + <RegistryContext.Provider value={registry}>
102 + <Probe wantedPaths={["wanted.ts"]} />
103 + </RegistryContext.Provider>,
104 + );
105 + const result = JSON.parse(screen.container.querySelector("output")!.textContent!);
106 + expect(result.loading).toBe(false);
107 + expect(result.files).toContainEqual(["wanted.ts", "FOUND\n"]);
108 + expect(pastPageReads).toBe(0);
109 +});
110 +
111 +it("waits for a later cached page that is still refreshing", async () => {
112 + setup();
113 + await using stack = new AsyncDisposableStack();
114 + const registry = stack.adopt(AtomRegistry.make(), (value) => value.dispose());
115 + state.pages.set(
116 + null,
117 + Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(
118 + AsyncResult.success(page("first.ts", "FIRST", "cursor-1")),
119 + ),
120 + );
121 + const second = Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(
122 + AsyncResult.success(page("other.ts", "STALE", "old-next"), { waiting: true }),
123 + );
124 + state.pages.set("cursor-1", second);
125 + state.pages.set(
126 + "old-next",
127 + Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(AsyncResult.initial(true)),
128 + );
129 + await using screen = await render(
130 + <RegistryContext.Provider value={registry}>
131 + <Probe wantedPaths={["wanted.ts"]} />
132 + </RegistryContext.Provider>,
133 + );
134 + const result = () => JSON.parse(screen.container.querySelector("output")!.textContent!);
135 + expect(result()).toEqual({ loading: true, files: [["first.ts", "FIRST\n"]] });
136 +
137 + await act(() => registry.set(second, AsyncResult.success(page("wanted.ts", "FRESH", null))));
138 + expect(result()).toEqual({
139 + loading: false,
140 + files: [
141 + ["first.ts", "FIRST\n"],
142 + ["wanted.ts", "FRESH\n"],
143 + ],
144 + });
145 +});
apps/web/src/components/pullRequest/guide/useGuideDiffFiles.ts
+22 -22
@@ -1,6 +1,8 @@
1 import type { FileDiffMetadata } from "@pierre/diffs";
2 import type { EnvironmentId, PullRequestRef } from "@t3tools/contracts";
3 -import { useState } from "react";
3 +import { RegistryContext } from "@effect/atom-react";
4 +import { AsyncResult } from "effect/reactivity";
5 +import { useContext, useState } from "react";
6
7 import { getRenderablePatch, resolveFileDiffPath } from "~/lib/diffRendering";
8 import { pullRequestEnvironment } from "~/state/pullRequests";
@@ -39,35 +41,33 @@ export function useGuideDiffFiles({
41 });
42 const current: LoadedDiff =
43 loaded.key === key ? loaded : { key, cursor: null, done: false, files: NO_FILES, folded: null };
42 - const missing = wantedPaths.some((path) => !current.files.has(path));
43 - const loading = missing && !current.done;
44 - const query = useEnvironmentQuery(
45 - loading
46 - ? pullRequestEnvironment.diff({
47 - environmentId,
48 - input: { ...reference, ...(current.cursor === null ? {} : { cursor: current.cursor }) },
49 - })
50 - : null,
51 - );
44 + const registry = useContext(RegistryContext);
45 + const isMissing = (diff: LoadedDiff) => wantedPaths.some((path) => !diff.files.has(path));
46 + const pageAtom = (cursor: string | null) =>
47 + pullRequestEnvironment.diff({
48 + environmentId,
49 + input: { ...reference, ...(cursor === null ? {} : { cursor }) },
50 + });
51 + const loading = isMissing(current) && !current.done;
52 + const query = useEnvironmentQuery(loading ? pageAtom(current.cursor) : null);
53
53 - const data = query.data;
54 - if (data !== null && !query.isPending && !current.done && current.folded !== data) {
54 + // One update for every settled page: an update per page trips React's re-render limit.
55 + let next = current;
56 + let data = query.isPending ? null : query.data;
57 + while (data !== null && !next.done && next.folded !== data && isMissing(next)) {
58 const parsed = getRenderablePatch(
59 data.patch,
57 - `pull-request-guide:${key}:${current.cursor ?? "first"}`,
60 + `pull-request-guide:${key}:${next.cursor ?? "first"}`,
61 );
59 - const files = new Map(current.files);
62 + const files = new Map(next.files);
63 if (parsed?.kind === "files") {
64 for (const file of parsed.files) files.set(resolveFileDiffPath(file), file);
65 }
63 - setLoaded({
64 - key,
65 - cursor: data.nextCursor,
66 - done: data.nextCursor === null,
67 - files,
68 - folded: data,
69 - });
66 + next = { key, cursor: data.nextCursor, done: data.nextCursor === null, files, folded: data };
67 + const result = next.done || !isMissing(next) ? null : registry.get(pageAtom(next.cursor));
68 + data = result === null || result.waiting ? null : AsyncResult.getOrElse(result, () => null);
69 }
70 + if (next !== current) setLoaded(next);
71
72 return {
73 files: current.files,