The best way to control Claude Code, Codex and more from any computer or phone
fix(mobile): reduce diff entry and source highlighting delays (#24770)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 25, 2026 at 00:45 UTC
15702a9f28620588c0b71811d9d725e98789fdd4
13 files changed
+684
-75
apps/mobile/src/features/diffs/nativeReviewDiffSource.test.ts
+18
-11
@@ -58,17 +58,24 @@ const failureKeys = [
58
["function", () => () => SOURCE],
59
] as const;
60
61
-it.each(failureKeys)("bounds transient retries for a %s key", async (_name, createKey) => {
62
- const key = createKey();
63
- const fetch = vi.fn(async () => {
64
- throw new Error("Offline");
65
- });
66
- const load = createNativeReviewDiffSourceLoader(fetch);
67
- await expect(load(key)).rejects.toThrow("Offline");
68
- expect(await load(key)).toBeNull();
69
- expect(await load(key)).toBeNull();
70
- expect(fetch).toHaveBeenCalledTimes(2);
71
-});
61
+it.each(failureKeys)(
62
+ "recovers after two transient failures on a later load for a %s key",
63
+ async (_name, createKey) => {
64
+ const key = createKey();
65
+ const fetch = vi
66
+ .fn()
67
+ .mockRejectedValueOnce(new Error("Offline"))
68
+ .mockRejectedValueOnce(new Error("Still offline"))
69
+ .mockResolvedValue(SOURCE);
70
+ const load = createNativeReviewDiffSourceLoader(fetch);
71
+ await expect(load(key)).rejects.toThrow("Offline");
72
+ await expect(load(key)).rejects.toThrow("Still offline");
73
+ expect(fetch).toHaveBeenCalledTimes(2);
74
+ expect(await load(key)).toBe(SOURCE);
75
+ expect(await load(key)).toBe(SOURCE);
76
+ expect(fetch).toHaveBeenCalledTimes(3);
77
+ },
78
+);
79
80
it.each(failureKeys)(
81
"remembers permanent unavailability for a %s key",
apps/mobile/src/features/diffs/nativeReviewDiffSource.ts
+2
-3
@@ -5,7 +5,6 @@ const MAX_SOURCE_FILES = 4;
5
// Bound retained UTF-16 text across files without discarding an already-fetched
6
// authoritative source just because it is large. Provider response limits still apply.
7
const MAX_CACHED_SOURCE_CHARACTERS = 8_000_000;
8
-const MAX_SOURCE_ATTEMPTS = 2;
8
9
function isWeakKey(value: unknown): value is object {
10
return (typeof value === "object" && value !== null) || typeof value === "function";
@@ -158,12 +157,12 @@ export function createNativeReviewDiffSourceLoader<FileId>(
157
permanent,
158
reason: reason ?? "transient",
159
});
161
- if (permanent || attempt >= MAX_SOURCE_ATTEMPTS) {
160
+ if (permanent) {
161
failure.unavailable = true;
162
traceDiffHighlight("source-unavailable", {
163
loader,
164
file,
166
- reason: permanent ? "permanent-error" : "attempts",
165
+ reason: "permanent-error",
166
});
167
return null;
168
}
apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx
+27
-6
@@ -43,6 +43,7 @@ import {
43
import { usePullRequestDiffHighlightFiles } from "./usePullRequestDiffHighlightFiles";
44
import { isPullRequestTestFile } from "./pullRequestFileFilters";
45
import { useFocusEffect, useNavigation, type StaticScreenProps } from "@react-navigation/native";
46
+import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
47
import { RegistryContext, useAtomRefresh, useAtomValue } from "@effect/atom-react";
48
import { pullRequestReviewKey } from "./pullRequestReviewDrafts";
49
import { type PullRequestDetail, type PullRequestListEntry } from "@t3tools/contracts";
@@ -88,7 +89,7 @@ import {
89
type NativeReviewDiffViewProps,
90
} from "../diffs/nativeReviewDiffSurface";
91
import {
91
- buildNativeReviewDiffData,
92
+ getCachedNativeReviewDiffData,
93
NATIVE_REVIEW_DIFF_CONTENT_WIDTH,
94
} from "../review/nativeReviewDiffAdapter";
95
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
@@ -99,6 +100,7 @@ import { PullRequestActionToolbar, PullRequestCardGroup } from "./PullRequestCon
100
import {
101
pullRequestTarget as requestTarget,
102
usePullRequestDiff,
103
+ usePullRequestDiffEntry,
104
type PullRequestDiffParams,
105
type PullRequestParams,
106
} from "./usePullRequestDiff";
@@ -847,16 +849,35 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestP
849
);
850
}
851
850
-export function PullRequestFileRouteScreen({
851
- route,
852
-}: StaticScreenProps<
852
+type PullRequestFileRouteProps = StaticScreenProps<
853
PullRequestDiffParams & {
854
readonly path: string;
855
readonly fileSelectionId?: string;
856
readonly replacingDraftId?: string;
857
readonly focus?: GuideDiffFocus;
858
}
859
->) {
859
+>;
860
+
861
+export function PullRequestFileRouteScreen(props: PullRequestFileRouteProps) {
862
+ const navigation =
863
+ useNavigation<NativeStackNavigationProp<ReactNavigation.RootParamList, "PullRequestFile">>();
864
+ const ready = usePullRequestDiffEntry(navigation);
865
+ if (ready) return <PullRequestFileRouteContent {...props} />;
866
+ const title = props.route.params.commit ? "Commit changes" : "Changes";
867
+ return (
868
+ <View className="flex-1 bg-screen">
869
+ <NativeStackScreenOptions options={{ title, headerTitle: title }} />
870
+ {Platform.OS === "android" ? (
871
+ <AndroidScreenHeader title={title} onBack={() => navigation.goBack()} />
872
+ ) : null}
873
+ <View className="flex-1 items-center justify-center">
874
+ <AppActivityIndicator accessibilityLabel="Loading changes" />
875
+ </View>
876
+ </View>
877
+ );
878
+}
879
+
880
+function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
881
const { layout } = useAdaptiveWorkspaceLayout();
882
const navigation = useNavigation();
883
const insets = useSafeAreaInsets();
@@ -894,7 +915,7 @@ export function PullRequestFileRouteScreen({
915
!result.isPending &&
916
!result.error;
917
const activity = useEnvironmentQuery(composerPullRequests.activity(requestTarget(route.params)));
897
- const baseData = useMemo(() => buildNativeReviewDiffData(parsed), [parsed]);
918
+ const baseData = useMemo(() => getCachedNativeReviewDiffData({ parsedDiff: parsed }), [parsed]);
919
const highlightFiles = usePullRequestDiffHighlightFiles(route.params, parsed, baseData.files);
920
const data = useMemo(
921
() =>
apps/mobile/src/features/pull-requests/pullRequestDiffHighlightFiles.test.ts
+8
-4
@@ -265,8 +265,12 @@ it.each(["permanent", "transient"])(
265
fetch.mockRejectedValueOnce(error);
266
if (failure === "transient") fetch.mockRejectedValueOnce(error);
267
const first = createPullRequestDiffHighlightFiles({ target }, parsed, files, fetch, cache);
268
- if (failure === "transient") await expect(first[1]!.loadContents!()).rejects.toThrow("Offline");
269
- expect(await first[1]!.loadContents!()).toBeNull();
268
+ if (failure === "transient") {
269
+ await expect(first[1]!.loadContents!()).rejects.toThrow("Offline");
270
+ await expect(first[1]!.loadContents!()).rejects.toThrow("Offline");
271
+ } else {
272
+ expect(await first[1]!.loadContents!()).toBeNull();
273
+ }
274
const second = createPullRequestDiffHighlightFiles(
275
{ target },
276
{ ...parsed, files: [parsed.files[0]!, { ...parsed.files[1]! }] },
@@ -275,8 +279,8 @@ it.each(["permanent", "transient"])(
279
cache,
280
);
281
expect(await second[1]!.loadContents!()).toBe(contents);
278
- expect(await first[1]!.loadContents!()).toBeNull();
279
- expect(fetch).toHaveBeenCalledTimes(failure === "permanent" ? 2 : 3);
282
+ expect(await first[1]!.loadContents!()).toBe(failure === "permanent" ? null : contents);
283
+ expect(fetch).toHaveBeenCalledTimes(failure === "permanent" ? 2 : 4);
284
},
285
);
286
apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts
+66
-2
@@ -1,8 +1,13 @@
1
-import { describe, expect, it } from "vite-plus/test";
1
+import { describe, expect, it, vi } from "vite-plus/test";
2
import type { PullRequestDiffResult } from "@t3tools/contracts";
3
import type { ReviewParsedDiff } from "../review/reviewModel";
4
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
5
-import { combinePullRequestDiffPages, retainPullRequestDiffPage } from "./pullRequestDiffPages";
5
+import * as reviewModel from "../review/reviewModel";
6
+import {
7
+ combinePullRequestDiffPages,
8
+ getCachedPullRequestDiffPages,
9
+ retainPullRequestDiffPage,
10
+} from "./pullRequestDiffPages";
11
12
const GITHUB = sourceControlClients.get("github").changeRequest;
13
@@ -24,6 +29,65 @@ function withheldDiff(): PullRequestDiffResult {
29
}
30
31
describe("paged mobile pull request diffs", () => {
32
+ it("isolates scopes, cursors, and provider terminology for the same result", () => {
33
+ const result = withheldDiff();
34
+ const pages = [{ cursor: null, result }];
35
+ const first = getCachedPullRequestDiffPages(pages, "first", GITHUB);
36
+ const otherScope = getCachedPullRequestDiffPages(pages, "second", GITHUB);
37
+ const otherCursor = getCachedPullRequestDiffPages(
38
+ [{ cursor: "next", result }],
39
+ "first",
40
+ GITHUB,
41
+ );
42
+ const otherProvider = getCachedPullRequestDiffPages(
43
+ pages,
44
+ "first",
45
+ sourceControlClients.get("gitlab").changeRequest,
46
+ );
47
+ expect(otherScope).not.toBe(first);
48
+ expect(otherCursor).not.toBe(first);
49
+ expect(otherProvider).not.toBe(first);
50
+ if (first.kind !== "files" || otherScope.kind !== "files" || otherProvider.kind !== "files")
51
+ throw new Error("Expected structured files");
52
+ expect(otherScope.files[0]?.id).not.toBe(first.files[0]?.id);
53
+ expect(first.notice).toContain("pull request");
54
+ expect(otherProvider.notice).toContain("merge request");
55
+ expect(getCachedPullRequestDiffPages(pages, "first", GITHUB)).toBe(first);
56
+ });
57
+
58
+ it("reuses earlier pages on append and invalidates changed continuation results", () => {
59
+ using parse = vi.spyOn(reviewModel, "buildReviewParsedDiff");
60
+ const firstPage = { cursor: null, result: diff("first.ts") };
61
+ const first = getCachedPullRequestDiffPages([firstPage], "append", GITHUB);
62
+ const nextPage = { cursor: "next", result: withheldDiff() };
63
+ const combined = getCachedPullRequestDiffPages([firstPage, nextPage], "append", GITHUB);
64
+ expect(parse).toHaveBeenCalledTimes(2);
65
+ if (first.kind !== "files" || combined.kind !== "files") throw new Error("Expected files");
66
+ expect(combined.files[0]).toBe(first.files[0]);
67
+ expect(combined.files.map((file) => file.path)).toEqual(["first.ts", "large.ts"]);
68
+ expect(getCachedPullRequestDiffPages([firstPage, nextPage], "append", GITHUB)).toBe(combined);
69
+ const refreshed = getCachedPullRequestDiffPages(
70
+ [
71
+ firstPage,
72
+ {
73
+ ...nextPage,
74
+ result: {
75
+ ...nextPage.result,
76
+ omittedFileStats: [{ path: "large.ts", additions: 4000, deletions: 2000 }],
77
+ },
78
+ },
79
+ ],
80
+ "append",
81
+ GITHUB,
82
+ );
83
+ expect(parse).toHaveBeenCalledTimes(3);
84
+ expect(refreshed).not.toBe(combined);
85
+ if (refreshed.kind !== "files") throw new Error("Expected refreshed files");
86
+ expect(refreshed.files[0]).toBe(first.files[0]);
87
+ expect(refreshed.files[1]).toMatchObject({ additions: 4000, deletions: 2000 });
88
+ expect(combined.files[1]?.additions).toBe(3000);
89
+ });
90
+
91
it("withheld PR hunks must retain provider line counts", () => {
92
const result = withheldDiff();
93
const cache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>();
apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts
+40
@@ -7,6 +7,46 @@ export interface PullRequestDiffPage {
7
readonly result: PullRequestDiffResult;
8
}
9
10
+interface CachedPullRequestDiffPages {
11
+ readonly pages: readonly PullRequestDiffPage[];
12
+ readonly cache: WeakMap<PullRequestDiffResult, ReviewParsedDiff>;
13
+ readonly parsed: ReviewParsedDiff;
14
+}
15
+
16
+// Keep only the latest combination per scoped first page; query results own its lifetime.
17
+const parsedSnapshots = new WeakMap<
18
+ PullRequestDiffResult,
19
+ Map<string, CachedPullRequestDiffPages>
20
+>();
21
+
22
+export function getCachedPullRequestDiffPages(
23
+ pages: readonly PullRequestDiffPage[],
24
+ scope: string,
25
+ terminology: ChangeRequestTerminology,
26
+): ReviewParsedDiff {
27
+ const first = pages[0];
28
+ if (!first) return { kind: "empty" };
29
+ const key = JSON.stringify([scope, terminology.singular, first.cursor]);
30
+ let snapshots = parsedSnapshots.get(first.result);
31
+ if (!snapshots) {
32
+ snapshots = new Map();
33
+ parsedSnapshots.set(first.result, snapshots);
34
+ }
35
+ const previous = snapshots.get(key);
36
+ if (
37
+ previous?.pages.length === pages.length &&
38
+ pages.every((page, index) => {
39
+ const cached = previous.pages[index];
40
+ return cached?.cursor === page.cursor && cached.result === page.result;
41
+ })
42
+ )
43
+ return previous.parsed;
44
+ const cache = previous?.cache ?? new WeakMap<PullRequestDiffResult, ReviewParsedDiff>();
45
+ const parsed = combinePullRequestDiffPages(pages, cache, scope, terminology);
46
+ snapshots.set(key, { pages, cache, parsed });
47
+ return parsed;
48
+}
49
+
50
/** A changed page invalidates later cursors, which belong to the previous patch. */
51
export function retainPullRequestDiffPage(
52
pages: readonly PullRequestDiffPage[],
apps/mobile/src/features/pull-requests/usePullRequestDiff.test.tsx
+130
-2
@@ -3,14 +3,16 @@ import { sourceControlClients } from "@t3tools/client-runtime/source-control-cli
3
import type { PullRequestDiffResult } from "@t3tools/contracts";
4
import * as Effect from "effect/Effect";
5
import { Atom, AtomRegistry } from "effect/reactivity";
6
-import { act, useLayoutEffect } from "react";
6
+import { act, useLayoutEffect, useState } from "react";
7
import { createRoot } from "react-dom/client";
8
import { expect, it, vi } from "vite-plus/test";
9
10
const mocks = vi.hoisted(() => ({ diff: vi.fn() }));
11
vi.mock("../../state/pull-requests", () => ({ composerPullRequests: { diff: mocks.diff } }));
12
13
-import { usePullRequestDiff } from "./usePullRequestDiff";
13
+import { usePullRequestDiff, usePullRequestDiffEntry } from "./usePullRequestDiff";
14
+import * as reviewModel from "../review/reviewModel";
15
+import { getCachedNativeReviewDiffData } from "../review/nativeReviewDiffAdapter";
16
17
function patch(path: string, nextCursor: string | null = null): PullRequestDiffResult {
18
return {
@@ -67,6 +69,15 @@ function setup() {
69
);
70
return {
71
requests,
72
+ remount() {
73
+ act(() =>
74
+ root.render(
75
+ <RegistryContext.Provider value={registry}>
76
+ <Probe key="new-route" />
77
+ </RegistryContext.Provider>,
78
+ ),
79
+ );
80
+ },
81
get current() {
82
return current;
83
},
@@ -87,6 +98,123 @@ function setup() {
98
};
99
}
100
101
+it("reuses the parsed snapshot when a new route reads the same query result", async () => {
102
+ using parse = vi.spyOn(reviewModel, "buildReviewParsedDiff");
103
+ using screen = setup();
104
+ await screen.succeed(0, patch("first.ts"));
105
+ const parsed = screen.current.parsed;
106
+ const parses = parse.mock.calls.length;
107
+ screen.remount();
108
+ expect(screen.current.parsed).toBe(parsed);
109
+ expect(parse).toHaveBeenCalledTimes(parses);
110
+ expect(screen.requests).toHaveLength(1);
111
+});
112
+
113
+function setupEntry() {
114
+ vi.useFakeTimers();
115
+ (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
116
+ const frames = new Map<number, FrameRequestCallback>();
117
+ let frameId = 0;
118
+ const requestFrame = vi
119
+ .spyOn(globalThis, "requestAnimationFrame")
120
+ .mockImplementation((callback) => {
121
+ frames.set(++frameId, callback);
122
+ return frameId;
123
+ });
124
+ const cancelFrame = vi.spyOn(globalThis, "cancelAnimationFrame").mockImplementation((id) => {
125
+ if (id != null) frames.delete(id);
126
+ });
127
+ const listeners = new Map<string, (event: { data: { closing: boolean } }) => void>();
128
+ const navigation = {
129
+ addListener(
130
+ type: "transitionStart" | "transitionEnd",
131
+ listener: (event: { data: { closing: boolean } }) => void,
132
+ ) {
133
+ listeners.set(type, listener);
134
+ return () => {
135
+ listeners.delete(type);
136
+ };
137
+ },
138
+ };
139
+ const events: string[] = [];
140
+ const prepare = vi.fn(() => {
141
+ events.push("prepare");
142
+ return getCachedNativeReviewDiffData({
143
+ parsedDiff: reviewModel.buildReviewParsedDiff(
144
+ patch("first.ts").patch + patch("neighbor.ts").patch,
145
+ "entry",
146
+ ),
147
+ });
148
+ });
149
+ function Body() {
150
+ const [data] = useState(prepare);
151
+ return <div>{data.files.map((file) => file.path).join(",")}</div>;
152
+ }
153
+ function Entry() {
154
+ const ready = usePullRequestDiffEntry(navigation);
155
+ useLayoutEffect(() => {
156
+ events.push(ready ? "body-commit" : "shell-commit");
157
+ }, [ready]);
158
+ return ready ? <Body /> : <div>Opening changes</div>;
159
+ }
160
+ const container = document.createElement("div");
161
+ const root = createRoot(container);
162
+ act(() => root.render(<Entry />));
163
+ return {
164
+ prepare,
165
+ events,
166
+ container,
167
+ frame() {
168
+ const pending = [...frames.values()];
169
+ frames.clear();
170
+ act(() => {
171
+ for (const callback of pending) callback(0);
172
+ });
173
+ },
174
+ event(type: "transitionStart" | "transitionEnd", closing = false) {
175
+ act(() => listeners.get(type)?.({ data: { closing } }));
176
+ },
177
+ fallback() {
178
+ act(() => vi.runAllTimers());
179
+ },
180
+ [Symbol.dispose]() {
181
+ act(() => root.unmount());
182
+ requestFrame.mockRestore();
183
+ cancelFrame.mockRestore();
184
+ vi.useRealTimers();
185
+ },
186
+ };
187
+}
188
+
189
+it("commits the loading shell and waits for the opening transition before building the diff", () => {
190
+ using screen = setupEntry();
191
+ expect(screen.events).toEqual(["shell-commit"]);
192
+ screen.event("transitionStart");
193
+ screen.frame();
194
+ screen.frame();
195
+ screen.event("transitionEnd", true);
196
+ expect(screen.prepare).not.toHaveBeenCalled();
197
+ screen.event("transitionEnd");
198
+ expect(screen.events).toEqual(["shell-commit", "prepare", "body-commit"]);
199
+ expect(screen.container.textContent).toBe("first.ts,neighbor.ts");
200
+});
201
+
202
+it.each(["unanimated", "missing-completion"] as const)(
203
+ "opens the diff after a %s presentation without stranding the shell",
204
+ (presentation) => {
205
+ using screen = setupEntry();
206
+ if (presentation === "missing-completion") screen.event("transitionStart");
207
+ screen.frame();
208
+ expect(screen.prepare).not.toHaveBeenCalled();
209
+ screen.frame();
210
+ if (presentation === "missing-completion") {
211
+ expect(screen.prepare).not.toHaveBeenCalled();
212
+ screen.fallback();
213
+ }
214
+ expect(screen.prepare).toHaveBeenCalledOnce();
215
+ },
216
+);
217
+
218
it("retries a failed continuation without discarding loaded files or restarting page one", async () => {
219
using screen = setup();
220
await screen.succeed(0, patch("first.ts", "second"));
apps/mobile/src/features/pull-requests/usePullRequestDiff.ts
+45
-12
@@ -1,19 +1,13 @@
1
import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
2
-import {
3
- EnvironmentId,
4
- ProjectId,
5
- type PullRequestDiffResult,
6
- type PullRequestListEntry,
7
-} from "@t3tools/contracts";
2
+import { EnvironmentId, ProjectId, type PullRequestListEntry } from "@t3tools/contracts";
3
import { useAtomRefresh } from "@effect/atom-react";
9
-import { useCallback, useMemo, useState } from "react";
4
+import { useCallback, useLayoutEffect, useMemo, useState } from "react";
5
6
import { composerPullRequests } from "../../state/pull-requests";
7
import { useEnvironmentQuery } from "../../state/query";
8
import { usePullToRefresh } from "../../state/use-pull-to-refresh";
14
-import type { ReviewParsedDiff } from "../review/reviewModel";
9
import {
16
- combinePullRequestDiffPages,
10
+ getCachedPullRequestDiffPages,
11
retainPullRequestDiffPage,
12
type PullRequestDiffPage,
13
} from "./pullRequestDiffPages";
@@ -43,6 +37,46 @@ export function pullRequestTarget(params: PullRequestParams) {
37
38
const NO_PAGES: readonly PullRequestDiffPage[] = [];
39
40
+export function usePullRequestDiffEntry(navigation: {
41
+ readonly addListener: (
42
+ type: "transitionStart" | "transitionEnd",
43
+ listener: (event: { readonly data: { readonly closing: boolean } }) => void,
44
+ ) => () => void;
45
+}) {
46
+ const [ready, setReady] = useState(false);
47
+ useLayoutEffect(() => {
48
+ if (ready) return;
49
+ let opening = false;
50
+ let closing = false;
51
+ const finish = () => {
52
+ if (!closing) setReady(true);
53
+ };
54
+ const removeStart = navigation.addListener("transitionStart", ({ data }) => {
55
+ closing = data.closing;
56
+ if (!closing) opening = true;
57
+ });
58
+ const removeEnd = navigation.addListener("transitionEnd", ({ data }) => {
59
+ closing = data.closing;
60
+ if (!closing) finish();
61
+ });
62
+ // Initial and unanimated presentations may have no transition events; let the shell paint first.
63
+ let frame = requestAnimationFrame(() => {
64
+ frame = requestAnimationFrame(() => {
65
+ if (!opening) finish();
66
+ });
67
+ });
68
+ // A missing native completion must not strand the route on its loading state.
69
+ const fallback = setTimeout(finish, 1_000);
70
+ return () => {
71
+ removeStart();
72
+ removeEnd();
73
+ cancelAnimationFrame(frame);
74
+ clearTimeout(fallback);
75
+ };
76
+ }, [navigation, ready]);
77
+ return ready;
78
+}
79
+
80
export function usePullRequestDiff(
81
params: PullRequestDiffParams,
82
terminology: ChangeRequestTerminology,
@@ -74,10 +108,9 @@ export function usePullRequestDiff(
108
}, [cursor, key, query.data, state.key, state.pages]);
109
if (state.key !== key || state.pages !== pages) setState({ key, cursor, pages });
110
77
- const [parseCache] = useState(() => new WeakMap<PullRequestDiffResult, ReviewParsedDiff>());
111
const parsed = useMemo(
79
- () => combinePullRequestDiffPages(pages, parseCache, key, terminology),
80
- [key, pages, parseCache, terminology],
112
+ () => getCachedPullRequestDiffPages(pages, key, terminology),
113
+ [key, pages, terminology],
114
);
115
const patch = useMemo(() => pages.map((page) => page.result.patch).join("\n"), [pages]);
116
const nextCursor = pages.at(-1)?.result.nextCursor ?? null;
apps/server/src/pullRequest/PullRequestService.test.ts
+126
@@ -3981,6 +3981,132 @@ it.effect("pauses uncached activity on a host after one activity read is rate li
3981
}),
3982
);
3983
3984
+it.effect("authorizes cold source reads with paginated paths without fetching patch hunks", () =>
3985
+ Effect.gen(function* () {
3986
+ const pages: Array<string | undefined> = [];
3987
+ const contents: Array<string> = [];
3988
+ const service = yield* makeService({
3989
+ projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
3990
+ providers: [
3991
+ fakePullRequestProvider(SourceControlProviderKind.make("github"), {
3992
+ getDiff: () => Effect.die("Membership must not fetch patch hunks"),
3993
+ getDiffFilePaths: (input) =>
3994
+ Effect.sync(() => {
3995
+ pages.push(input.cursor);
3996
+ return input.cursor === undefined
3997
+ ? { files: [{ oldPath: "before.ts", newPath: "after.ts" }], nextCursor: "next" }
3998
+ : { files: [{ oldPath: "gone.ts", newPath: "gone.ts" }], nextCursor: null };
3999
+ }),
4000
+ getDiffFileContents: (input) =>
4001
+ Effect.sync(() => {
4002
+ contents.push(`${input.oldPath}->${input.newPath}`);
4003
+ return { oldContents: "before", newContents: "after" };
4004
+ }),
4005
+ }),
4006
+ ],
4007
+ });
4008
+ const ref = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
4009
+ yield* service.diffFileContents({
4010
+ ...ref,
4011
+ changeType: "rename-changed",
4012
+ oldPath: "before.ts",
4013
+ newPath: "after.ts",
4014
+ });
4015
+ yield* service.diffFileContents({
4016
+ ...ref,
4017
+ changeType: "deleted",
4018
+ oldPath: "gone.ts",
4019
+ newPath: "gone.ts",
4020
+ });
4021
+ for (const [oldPath, newPath] of [
4022
+ [".env", ".env"],
4023
+ ["../after.ts", "../after.ts"],
4024
+ ["secret.ts", "after.ts"],
4025
+ ] as const) {
4026
+ const error = yield* service
4027
+ .diffFileContents({ ...ref, changeType: "change", oldPath, newPath })
4028
+ .pipe(Effect.flip);
4029
+ assert.strictEqual(error.reason, "not-found");
4030
+ }
4031
+ assert.deepStrictEqual(pages, [undefined, "next"]);
4032
+ assert.deepStrictEqual(contents, ["before.ts->after.ts", "gone.ts->gone.ts"]);
4033
+ }),
4034
+);
4035
+
4036
+it.effect.each(["commit", "credential", "expiry", "refresh"] as const)(
4037
+ "keeps cold metadata membership in its original scope through an in-flight %s",
4038
+ (boundary) =>
4039
+ Effect.gen(function* () {
4040
+ const started = yield* Deferred.make<void>();
4041
+ const release = yield* Deferred.make<void>();
4042
+ let credential = "broad";
4043
+ let pathReads = 0;
4044
+ let contentReads = 0;
4045
+ const service = yield* makeService({
4046
+ projects: [
4047
+ project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" }),
4048
+ ],
4049
+ providers: [
4050
+ fakePullRequestProvider(SourceControlProviderKind.make("github"), {
4051
+ withVerifiedCredential: (_, use) =>
4052
+ Effect.suspend(() =>
4053
+ use({ accountId: "101", viewer: "octocat", credentialFingerprint: credential }),
4054
+ ),
4055
+ getDiff: () => Effect.die("Membership must not fetch patch hunks"),
4056
+ getDiffFilePaths: (input) =>
4057
+ Effect.gen(function* () {
4058
+ assert.strictEqual(input.cwd, "/a");
4059
+ assert.strictEqual(input.host, "github.com");
4060
+ assert.strictEqual(input.repository, "acme/web");
4061
+ if (++pathReads !== 1) return { files: [], nextCursor: null };
4062
+ assert.strictEqual(
4063
+ input.commit,
4064
+ boundary === "commit" ? "a".repeat(40) : undefined,
4065
+ );
4066
+ yield* Deferred.succeed(started, undefined);
4067
+ yield* Deferred.await(release);
4068
+ return {
4069
+ files: [{ oldPath: "private.ts", newPath: "private.ts" }],
4070
+ nextCursor: null,
4071
+ };
4072
+ }),
4073
+ getDiffFileContents: () =>
4074
+ Effect.sync(() => {
4075
+ contentReads += 1;
4076
+ return { oldContents: "old", newContents: "new" };
4077
+ }),
4078
+ }),
4079
+ ],
4080
+ });
4081
+ const input = {
4082
+ projectId: "p1" as ProjectId,
4083
+ repository: "acme/web",
4084
+ number: 1,
4085
+ host: "github.com",
4086
+ expectedAccountId: "101",
4087
+ changeType: "change" as const,
4088
+ oldPath: "private.ts",
4089
+ newPath: "private.ts",
4090
+ };
4091
+ const initial = boundary === "commit" ? { ...input, commit: "a".repeat(40) } : input;
4092
+ const loading = yield* Effect.forkChild(
4093
+ service.withRoutingCredential(initial, service.diffFileContents(initial)),
4094
+ );
4095
+ yield* Deferred.await(started);
4096
+ if (boundary === "expiry") yield* TestClock.adjust("61 seconds");
4097
+ if (boundary === "refresh") yield* service.invalidate({ reference: input });
4098
+ yield* Deferred.succeed(release, undefined);
4099
+ yield* Fiber.join(loading);
4100
+ if (boundary === "credential") credential = "restricted";
4101
+ const error = yield* service
4102
+ .withRoutingCredential(input, service.diffFileContents(input))
4103
+ .pipe(Effect.flip);
4104
+ assert.strictEqual(error.reason, "not-found");
4105
+ assert.strictEqual(pathReads, 2);
4106
+ assert.strictEqual(contentReads, 1);
4107
+ }),
4108
+);
4109
+
4110
it.effect(
4111
"reuses loaded oversized diff slices to expand only files the change request changes",
4112
() =>
apps/server/src/pullRequest/PullRequestService.ts
+32
-6
@@ -86,6 +86,7 @@ import type { SourceControlProviderContext } from "@t3tools/source-control-core/
86
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
87
import {
88
type ProviderChangeRequest,
89
+ type ProviderDiffFilePaths,
90
type ProviderFilterOptionPage,
91
type ProviderListCursor,
92
type ProviderChangeRequestWatchFingerprint,
@@ -543,6 +544,9 @@ function withRateLimitBackoff(
544
}),
545
getViewerPermissions: interactive("getViewerPermissions", api.getViewerPermissions),
546
getDiff: wrap("getDiff", api.getDiff),
547
+ ...(api.getDiffFilePaths === undefined
548
+ ? {}
549
+ : { getDiffFilePaths: wrap("getDiffFilePaths", api.getDiffFilePaths) }),
550
...(api.getDiffFileContents === undefined
551
? {}
552
: { getDiffFileContents: wrap("getDiffFileContents", api.getDiffFileContents) }),
@@ -3475,14 +3479,10 @@ export const make = Effect.gen(function* () {
3479
const fileKey = (oldPath: string, newPath: string) => JSON.stringify([oldPath, newPath]);
3480
const sliceKey = (input: PullRequestDiffInput) =>
3481
JSON.stringify([refCacheKey(input), input.cursor ?? null, input.commit ?? null]);
3478
- const record = (key: string, slice: PullRequestDiffResult, at: number) => {
3482
+ const recordPaths = (key: string, slice: ProviderDiffFilePaths, at: number) => {
3483
const entry = {
3484
at,
3481
- files: new Set(
3482
- parseDiffFilePaths(slice.patch).map((file) =>
3483
- fileKey(file.oldPath ?? file.newPath ?? "", file.newPath ?? file.oldPath ?? ""),
3484
- ),
3485
- ),
3485
+ files: new Set(slice.files.map((file) => fileKey(file.oldPath, file.newPath))),
3486
nextCursor: slice.nextCursor,
3487
};
3488
held.delete(key);
@@ -3493,11 +3493,37 @@ export const make = Effect.gen(function* () {
3493
held.set(key, entry);
3494
return entry;
3495
};
3496
+ const record = (key: string, slice: PullRequestDiffResult, at: number) =>
3497
+ recordPaths(
3498
+ key,
3499
+ {
3500
+ files: parseDiffFilePaths(slice.patch).map((file) => ({
3501
+ oldPath: file.oldPath ?? file.newPath ?? "",
3502
+ newPath: file.newPath ?? file.oldPath ?? "",
3503
+ })),
3504
+ nextCursor: slice.nextCursor,
3505
+ },
3506
+ at,
3507
+ );
3508
const read = Effect.fn("PullRequestService.read")(function* (input: PullRequestDiffInput) {
3509
const key = sliceKey(input);
3510
const now = yield* Clock.currentTimeMillis;
3511
const cached = held.get(key);
3512
if (cached !== undefined && now - cached.at <= ttlMs) return cached;
3513
+ const project = yield* requireProject(input);
3514
+ if (project.api.capabilities.diff && project.api.getDiffFilePaths) {
3515
+ const slice = yield* project.api
3516
+ .getDiffFilePaths({
3517
+ cwd: project.project.workspaceRoot,
3518
+ repository: project.repository,
3519
+ host: project.host,
3520
+ number: input.number,
3521
+ ...(input.cursor === undefined ? {} : { cursor: input.cursor }),
3522
+ ...(input.commit === undefined ? {} : { commit: input.commit }),
3523
+ })
3524
+ .pipe(Effect.mapError(toPullRequestError("diffFileContents")));
3525
+ return recordPaths(key, slice, now);
3526
+ }
3527
const slice = yield* diff(input);
3528
return record(key, slice, now);
3529
});
packages/source-control-core/src/server/PullRequestProvider.ts
+14
@@ -223,6 +223,11 @@ export interface ProviderDiffFileContents {
223
readonly newContents: string;
224
}
225
226
+export interface ProviderDiffFilePaths {
227
+ readonly files: ReadonlyArray<{ readonly oldPath: string; readonly newPath: string }>;
228
+ readonly nextCursor: string | null;
229
+}
230
+
231
export interface ProviderFilesViewed {
232
readonly files: ReadonlyArray<PullRequestFileViewed>;
233
/** More files exist; missing entries are unknown, not "unviewed". */
@@ -394,6 +399,15 @@ export interface PullRequestProviderApi {
399
},
400
) => Effect.Effect<ProviderDiffSlice, PullRequestProviderError>;
401
402
+ /** Exact changed path pairs, including renames, without fetching patch contents. */
403
+ readonly getDiffFilePaths?: (
404
+ input: ProviderRepositoryRef & {
405
+ readonly number: number;
406
+ readonly cursor?: string | undefined;
407
+ readonly commit?: string | undefined;
408
+ },
409
+ ) => Effect.Effect<ProviderDiffFilePaths, PullRequestProviderError>;
410
+
411
readonly getDiffFileContents?: (
412
input: ProviderRepositoryRef & {
413
readonly number: number;
packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts
+118
@@ -164,6 +164,124 @@ const assertRequestId = (body: unknown) =>
164
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u,
165
);
166
167
+describe("GitCafePullRequestProvider changed paths", () => {
168
+ const snapshot = { version: pull.version, headOid, comparisonBaseOid: baseOid };
169
+
170
+ it.effect("pages changed paths without fetching diff-file hunks", () => {
171
+ const server = fakeGitCafe({
172
+ "GET /repos/owner/repo/pulls/7": pull,
173
+ "GET /repos/owner/repo/pulls/7/changes": (request: SentRequest) => ({
174
+ ...snapshot,
175
+ items: request.query.has("after")
176
+ ? [{ path: "gone.ts", status: "deleted" }]
177
+ : [{ path: "new name.ts", oldPath: "old name.ts", status: "renamed" }],
178
+ next: request.query.has("after") ? null : "next-page",
179
+ }),
180
+ });
181
+ return Effect.gen(function* () {
182
+ const provider = yield* GitCafePullRequestProvider.make;
183
+ const first = yield* provider.getDiffFilePaths!(target);
184
+ const second = yield* provider.getDiffFilePaths!({ ...target, cursor: first.nextCursor! });
185
+ assert.deepStrictEqual(first.files, [{ oldPath: "old name.ts", newPath: "new name.ts" }]);
186
+ assert.deepStrictEqual(second, {
187
+ files: [{ oldPath: "gone.ts", newPath: "gone.ts" }],
188
+ nextCursor: null,
189
+ });
190
+ assert.isFalse(server.sent.some((request) => request.path.includes("/diff-file")));
191
+ }).pipe(Effect.provide(server.layer));
192
+ });
193
+
194
+ it.effect.each(["getDiff", "getDiffFilePaths"] as const)(
195
+ "%s rejects a repeated continuation cursor",
196
+ (method) => {
197
+ const server = fakeGitCafe({
198
+ "GET /repos/owner/repo/pulls/7": pull,
199
+ "GET /repos/owner/repo/pulls/7/changes": { ...snapshot, items: [], next: "same-page" },
200
+ });
201
+ return Effect.gen(function* () {
202
+ const provider = yield* GitCafePullRequestProvider.make;
203
+ const first = yield* provider[method]!(target);
204
+ const input = { ...target, cursor: first.nextCursor! };
205
+ const result =
206
+ method === "getDiff"
207
+ ? yield* provider.getDiff(input).pipe(Effect.result)
208
+ : yield* provider.getDiffFilePaths!(input).pipe(Effect.result);
209
+ assert.strictEqual(result._tag, "Failure");
210
+ }).pipe(Effect.provide(server.layer));
211
+ },
212
+ );
213
+
214
+ // getDiff cases guard parity with the existing patch path; getDiffFilePaths must refuse the same pages.
215
+ it.effect.each(
216
+ (["getDiff", "getDiffFilePaths"] as const).flatMap((method) =>
217
+ [{ version: 5 }, { headOid: baseOid }, { comparisonBaseOid: headOid }].map((changed) => ({
218
+ method,
219
+ changed,
220
+ })),
221
+ ),
222
+ )(
223
+ "$method refuses a continuation from a different revision ($changed)",
224
+ ({ method, changed }) => {
225
+ const server = fakeGitCafe({
226
+ "GET /repos/owner/repo/pulls/7": pull,
227
+ "GET /repos/owner/repo/pulls/7/changes": (request: SentRequest) => ({
228
+ ...snapshot,
229
+ ...(request.query.has("after") ? changed : {}),
230
+ items: [],
231
+ next: request.query.has("after") ? null : "next-page",
232
+ }),
233
+ });
234
+ return Effect.gen(function* () {
235
+ const provider = yield* GitCafePullRequestProvider.make;
236
+ const read = (cursor?: string) =>
237
+ method === "getDiff"
238
+ ? provider.getDiff({ ...target, ...(cursor === undefined ? {} : { cursor }) })
239
+ : provider.getDiffFilePaths!({
240
+ ...target,
241
+ ...(cursor === undefined ? {} : { cursor }),
242
+ });
243
+ const first = yield* read();
244
+ const input = { ...target, cursor: first.nextCursor! };
245
+ const result =
246
+ method === "getDiff"
247
+ ? yield* provider.getDiff(input).pipe(Effect.result)
248
+ : yield* provider.getDiffFilePaths!(input).pipe(Effect.result);
249
+ assert.strictEqual(result._tag, "Failure");
250
+ }).pipe(Effect.provide(server.layer));
251
+ },
252
+ );
253
+
254
+ it.effect.each(["getDiff", "getDiffFilePaths"] as const)(
255
+ "%s reads a pull that reports no head yet",
256
+ (method) => {
257
+ const server = fakeGitCafe({
258
+ "GET /repos/owner/repo/pulls/7": { ...pull, headOid: null },
259
+ "GET /repos/owner/repo/pulls/7/changes": { ...snapshot, items: [], next: null },
260
+ });
261
+ return Effect.gen(function* () {
262
+ const provider = yield* GitCafePullRequestProvider.make;
263
+ const result = yield* provider[method]!(target);
264
+ assert.isNull(result.nextCursor);
265
+ }).pipe(Effect.provide(server.layer));
266
+ },
267
+ );
268
+
269
+ it.effect.each([{ version: 5 }, { headOid: baseOid }])(
270
+ "refuses initial changed paths from a different pull revision (%j)",
271
+ (changed) => {
272
+ const server = fakeGitCafe({
273
+ "GET /repos/owner/repo/pulls/7": pull,
274
+ "GET /repos/owner/repo/pulls/7/changes": { ...snapshot, ...changed, items: [], next: null },
275
+ });
276
+ return Effect.gen(function* () {
277
+ const provider = yield* GitCafePullRequestProvider.make;
278
+ const result = yield* provider.getDiffFilePaths!(target).pipe(Effect.result);
279
+ assert.strictEqual(result._tag, "Failure");
280
+ }).pipe(Effect.provide(server.layer));
281
+ },
282
+ );
283
+});
284
+
285
describe("GitCafePullRequestProvider source contents", () => {
286
const comparisonBase = "a".repeat(40);
287
const snapshot = { version: pull.version, headOid, comparisonBaseOid: comparisonBase };
packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts
+58
-29
@@ -56,6 +56,8 @@ const AUTHOR_DISCOVERY_PAGES = 5;
56
const KNOWN_AUTHORS_LIMIT = 10_000;
57
/** GitCafe's cap on paths per `/diff-files` read, and so one diff page. */
58
const DIFF_FILES_BATCH = 64;
59
+/** Changed-path pages carry no hunks, so they can be read in larger pages. */
60
+const CHANGES_PAGE_LIMIT = 500;
61
/** Line totals require hunk reads; omit them for large PRs. */
62
const LINE_STATS_MAX_FILES = DIFF_FILES_BATCH * 4;
63
const MAX_CHANGE_PAGES = 20;
@@ -522,6 +524,45 @@ export const make = Effect.gen(function* () {
524
return { ...stack, members };
525
});
526
527
+ const readDiffPage = Effect.fn("GitCafePullRequestProvider.readDiffPage")(function* (
528
+ operation: string,
529
+ input: Parameters<PullRequestProviderApi["getDiff"]>[0],
530
+ limit: number,
531
+ ) {
532
+ if (input.commit !== undefined)
533
+ return yield* failure(operation, "GitCafe commit diffs are not supported yet.");
534
+ const cursor =
535
+ input.cursor === undefined
536
+ ? null
537
+ : yield* decodeDiffCursor(input.cursor).pipe(
538
+ Effect.mapError((cause) => failure(operation, "Invalid GitCafe diff cursor.", cause)),
539
+ );
540
+ // A pull without a head yet has nothing to pin; its version still is.
541
+ const expected = cursor ?? (yield* readPull(operation, input));
542
+ const version = expected.version;
543
+ const path = yield* pullPath(operation, input);
544
+ const query = new URLSearchParams({ expectedVersion: String(version), limit: String(limit) });
545
+ if (cursor !== null) query.set("after", cursor.after);
546
+ const page = yield* read(operation, input, { path: `${path}/changes?${query}` }, Changes);
547
+ if (
548
+ page.version !== version ||
549
+ (expected.headOid !== null && page.headOid !== expected.headOid) ||
550
+ (cursor !== null && cursor.baseOid !== page.comparisonBaseOid)
551
+ )
552
+ return yield* failure(
553
+ operation,
554
+ "GitCafe answered from a different revision of the pull request. Refresh the diff.",
555
+ );
556
+ if (cursor !== null && page.next !== null && page.next === cursor.after)
557
+ return yield* failure(operation, "GitCafe returned a repeated diff page cursor.");
558
+ const revision = { version, headOid: page.headOid, baseOid: page.comparisonBaseOid };
559
+ return {
560
+ items: page.items,
561
+ revision,
562
+ nextCursor: page.next === null ? null : encodeDiffCursor({ ...revision, after: page.next }),
563
+ };
564
+ });
565
+
566
const readDiffFiles = Effect.fn("GitCafePullRequestProvider.readDiffFiles")(function* (
567
input: PullRef,
568
revision: { readonly version: number; readonly headOid: string; readonly baseOid: string },
@@ -605,7 +646,10 @@ export const make = Effect.gen(function* () {
646
let revision: { version: number; headOid: string; baseOid: string } | undefined;
647
let after: string | null = null;
648
for (let page = 0; page < MAX_CHANGE_PAGES; page++) {
608
- const query = new URLSearchParams({ expectedVersion: String(version), limit: "500" });
649
+ const query = new URLSearchParams({
650
+ expectedVersion: String(version),
651
+ limit: String(CHANGES_PAGE_LIMIT),
652
+ });
653
if (after !== null) query.set("after", after);
654
const batch: typeof Changes.Type = yield* read(
655
"getChangeRequest",
@@ -966,36 +1010,21 @@ export const make = Effect.gen(function* () {
1010
getViewerPermissions: (input) =>
1011
readPull("getViewerPermissions", input).pipe(Effect.map(Json.toViewerPermissions)),
1012
getDiff: Effect.fn("GitCafePullRequestProvider.getDiff")(function* (input) {
969
- if (input.commit !== undefined)
970
- return yield* failure("getDiff", "GitCafe commit diffs are not supported yet.");
971
- const cursor =
972
- input.cursor === undefined
973
- ? null
974
- : yield* decodeDiffCursor(input.cursor).pipe(
975
- Effect.mapError((cause) => failure("getDiff", "Invalid GitCafe diff cursor.", cause)),
976
- );
977
- const version = cursor?.version ?? (yield* readPull("getDiff", input)).version;
978
- const path = yield* pullPath("getDiff", input);
979
- const query = new URLSearchParams({
980
- expectedVersion: String(version),
981
- limit: String(DIFF_FILES_BATCH),
982
- });
983
- if (cursor !== null) query.set("after", cursor.after);
984
- const page = yield* read("getDiff", input, { path: `${path}/changes?${query}` }, Changes);
985
- if (
986
- page.version !== version ||
987
- (cursor !== null &&
988
- (cursor.headOid !== page.headOid || cursor.baseOid !== page.comparisonBaseOid))
989
- )
990
- return yield* failure(
991
- "getDiff",
992
- "GitCafe answered from a different revision of the pull request. Refresh the diff.",
993
- );
994
- const revision = { version, headOid: page.headOid, baseOid: page.comparisonBaseOid };
995
- const details = yield* readDiffFiles(input, revision, detailablePaths(page.items));
1013
+ const page = yield* readDiffPage("getDiff", input, DIFF_FILES_BATCH);
1014
+ const details = yield* readDiffFiles(input, page.revision, detailablePaths(page.items));
1015
return {
1016
...Json.toDiff({ items: withDetails(page.items, details) }),
998
- nextCursor: page.next === null ? null : encodeDiffCursor({ ...revision, after: page.next }),
1017
+ nextCursor: page.nextCursor,
1018
+ };
1019
+ }),
1020
+ getDiffFilePaths: Effect.fn("GitCafePullRequestProvider.getDiffFilePaths")(function* (input) {
1021
+ const page = yield* readDiffPage("getDiffFilePaths", input, CHANGES_PAGE_LIMIT);
1022
+ return {
1023
+ files: page.items.map((file) => ({
1024
+ oldPath: file.oldPath ?? file.path,
1025
+ newPath: file.path,
1026
+ })),
1027
+ nextCursor: page.nextCursor,
1028
};
1029
}),
1030
getDiffFileContents: Effect.fn("GitCafePullRequestProvider.getDiffFileContents")(