Skip to content

Commit d61ae5a

Browse files
perf: send at most 200 checkpoint files to clients, with totals
A checkpoint over a vendored or generated tree can list 15k files (1.9 MB of JSON), and every snapshot, live event and turn item shipped the whole list. The wire now carries the first 200 files plus filesTotal (count, additions, deletions); storage keeps the full list. The changed-files card shows the totals and a "N more files in the full diff" row, and proactive diff opening and the mobile review list use the totals. Older clients ignore filesTotal and count the 200-file prefix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 08a463e commit d61ae5a

10 files changed

Lines changed: 167 additions & 38 deletions

File tree

‎apps/mobile/src/features/review/reviewModel.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
22
import type { ChangeTypes, FileDiffMetadata } from "@pierre/diffs/types";
3-
import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thread-checkpoints";
3+
import {
4+
checkpointFileTotals,
5+
type ThreadCheckpointSummary,
6+
} from "@t3tools/client-runtime/state/thread-checkpoints";
47
import type { ReviewDiffPreviewSource } from "@t3tools/contracts";
58
import { quoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
69
import * as Arr from "effect/Array";
@@ -107,7 +110,7 @@ function checkpointTitle(checkpoint: ThreadCheckpointSummary): string {
107110
}
108111

109112
function checkpointSubtitle(checkpoint: ThreadCheckpointSummary): string {
110-
const fileCount = checkpoint.files.length;
113+
const fileCount = checkpointFileTotals(checkpoint).count;
111114
if (checkpoint.status !== "ready") {
112115
return `Diff ${checkpoint.status}`;
113116
}

‎apps/server/src/orchestration-v2/ThreadStream.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
buildBoundedThreadProjection,
1111
type BoundedProjectionResult,
1212
} from "./threadHistoryPaging.ts";
13-
import { projectThreadProjectionForWire } from "./WireProjection.ts";
13+
import {
14+
capProjectionCheckpointFilesForWire,
15+
projectThreadProjectionForWire,
16+
} from "./WireProjection.ts";
1417

1518
export const THREAD_RESUME_MAX_REPLAY_EVENTS = 128;
1619

@@ -44,10 +47,9 @@ export function boundedSnapshotResponseFields(input: {
4447
readonly compactIds: boolean;
4548
}) {
4649
const { bounded } = input;
50+
const capped = capProjectionCheckpointFilesForWire(bounded.projection);
4751
// Dedupe keeps shared items one object, which the compaction's identity check needs.
48-
const projection = input.dedupeBodies
49-
? dedupeSnapshotBodies(bounded.projection)
50-
: bounded.projection;
52+
const projection = input.dedupeBodies ? dedupeSnapshotBodies(capped) : capped;
5153
const compactProjection = input.compactTurnItems ? omitLocalVisibleTurnItems(projection) : null;
5254
return {
5355
projection: compactProjection ?? projection,

‎apps/server/src/orchestration-v2/WireProjection.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import {
2+
CheckpointId,
3+
CheckpointRef,
4+
CheckpointScopeId,
25
MessageId,
36
ContextHandoffId,
47
ProviderThreadId,
@@ -381,6 +384,46 @@ describe("live body deduper", () => {
381384
]);
382385
});
383386

387+
it("caps a large checkpoint's files for the wire and still dedupes its item", () => {
388+
const files = Array.from({ length: 250 }, (_, index) => ({
389+
path: `vendor/file-${index}.ts`,
390+
kind: "modified",
391+
additions: 2,
392+
deletions: 1,
393+
}));
394+
const checkpointId = CheckpointId.make("checkpoint-large");
395+
const scopeId = CheckpointScopeId.make("scope-large");
396+
const captured = event(1, {
397+
type: "checkpoint.captured",
398+
payload: {
399+
id: checkpointId,
400+
threadId: base.threadId,
401+
scopeId,
402+
runId: null,
403+
nodeId: NodeId.make("node-large"),
404+
parentCheckpointId: null,
405+
ordinalWithinScope: 1,
406+
appRunOrdinal: null,
407+
ref: CheckpointRef.make("refs/t3/checkpoints/large"),
408+
status: "ready",
409+
files,
410+
capturedAt: at,
411+
},
412+
});
413+
const item = event(2, {
414+
type: "turn-item.updated",
415+
payload: { ...base, type: "checkpoint", checkpointId, scopeId, files },
416+
});
417+
const dedupe = makeLiveBodyDeduper();
418+
const [record, card] = [captured, item].map((streamed) =>
419+
dedupe({ ...streamed, event: projectDomainEventForWire(streamed.event) }),
420+
);
421+
422+
const filesTotal = { count: 250, additions: 500, deletions: 250 };
423+
expect(payloadOf(record!)).toMatchObject({ files: files.slice(0, 200), filesTotal });
424+
expect(payloadOf(card!)).toMatchObject({ files: [], filesOmitted: true, filesTotal });
425+
});
426+
384427
it("ships a reply whole when its message arrives after it", () => {
385428
const first = reply(1, "a");
386429
expect([first, message(2, "a")].map(makeLiveBodyDeduper())[0]).toBe(first);

‎apps/server/src/orchestration-v2/WireProjection.ts‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
toolOutputIndicatesFailure,
2222
} from "@t3tools/shared/toolOutput";
2323

24+
// A checkpoint over vendored or generated trees can list 15k+ files; the card needs a prefix.
25+
const MAX_WIRE_CHECKPOINT_FILES = 200;
2426
const MAX_DETAIL_STRING_BYTES = 32_768;
2527
const MAX_DYNAMIC_VALUE_BYTES = 16_384;
2628
const MAX_ON_DEMAND_BYTES = 256 * 1024;
@@ -96,8 +98,59 @@ function summarizeDynamicValue(value: unknown): unknown {
9698
};
9799
}
98100

101+
type CheckpointFiles = Pick<OrchestrationV2Checkpoint, "files" | "filesTotal">;
102+
103+
// Same input, same output object: snapshot compaction relies on shared item identity.
104+
const cappedCheckpointFiles = new WeakMap<object, object>();
105+
106+
function capCheckpointFiles<T extends CheckpointFiles>(value: T): T {
107+
if (value.files.length <= MAX_WIRE_CHECKPOINT_FILES || value.filesTotal !== undefined) {
108+
return value;
109+
}
110+
const cached = cappedCheckpointFiles.get(value);
111+
if (cached !== undefined) return cached as T;
112+
let additions = 0;
113+
let deletions = 0;
114+
for (const file of value.files) {
115+
additions += file.additions;
116+
deletions += file.deletions;
117+
}
118+
const capped = {
119+
...value,
120+
files: value.files.slice(0, MAX_WIRE_CHECKPOINT_FILES),
121+
filesTotal: { count: value.files.length, additions, deletions },
122+
};
123+
cappedCheckpointFiles.set(value, capped);
124+
return capped;
125+
}
126+
127+
/** Idempotent; bounded snapshots that skip `projectThreadProjectionForWire` still need it. */
128+
export function capProjectionCheckpointFilesForWire(
129+
projection: OrchestrationV2ThreadProjection,
130+
): OrchestrationV2ThreadProjection {
131+
const checkpoints = projection.checkpoints.map(capCheckpointFiles);
132+
const turnItems = projection.turnItems.map(capCheckpointItemFiles);
133+
const visibleTurnItems = projection.visibleTurnItems.map((row) => {
134+
const item = capCheckpointItemFiles(row.item);
135+
return item === row.item ? row : { ...row, item };
136+
});
137+
const unchanged = (next: ReadonlyArray<unknown>, previous: ReadonlyArray<unknown>) =>
138+
next.every((value, index) => value === previous[index]);
139+
return unchanged(checkpoints, projection.checkpoints) &&
140+
unchanged(turnItems, projection.turnItems) &&
141+
unchanged(visibleTurnItems, projection.visibleTurnItems)
142+
? projection
143+
: { ...projection, checkpoints, turnItems, visibleTurnItems };
144+
}
145+
146+
function capCheckpointItemFiles(item: OrchestrationV2TurnItem): OrchestrationV2TurnItem {
147+
return item.type === "checkpoint" ? capCheckpointFiles(item) : item;
148+
}
149+
99150
export function projectTurnItemForWire(item: OrchestrationV2TurnItem): OrchestrationV2TurnItem {
100151
switch (item.type) {
152+
case "checkpoint":
153+
return capCheckpointFiles(item);
101154
case "handoff": {
102155
const { summary: _summary, ...projected } = item;
103156
return projected;
@@ -241,6 +294,7 @@ export function projectThreadProjectionForWire(
241294
};
242295
return {
243296
...projection,
297+
checkpoints: projection.checkpoints.map(capCheckpointFiles),
244298
contextHandoffs: projection.contextHandoffs.map(projectContextHandoffForWire),
245299
turnItems: projection.turnItems.map(project),
246300
visibleTurnItems: projection.visibleTurnItems.map((row) => ({
@@ -257,7 +311,9 @@ export function projectDomainEventForWire(
257311
? { ...event, payload: projectTurnItemForWire(event.payload) }
258312
: event.type === "context-handoff.updated"
259313
? { ...event, payload: projectContextHandoffForWire(event.payload) }
260-
: event;
314+
: event.type === "checkpoint.captured"
315+
? { ...event, payload: capCheckpointFiles(event.payload) }
316+
: event;
261317
}
262318

263319
const LIVE_DEDUPE_MEMORY = 32;

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
deriveLatestThreadRun,
44
deriveThreadRuntime,
55
} from "@t3tools/client-runtime/state/thread-execution";
6+
import { checkpointFileTotals } from "@t3tools/client-runtime/state/thread-checkpoints";
67
import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
78
import {
89
latestUnheldRun,
@@ -218,7 +219,7 @@ export function shouldOpenProactiveTurnDiff(input: {
218219
}
219220

220221
export function resolveProactiveTurnDiffAction(input: {
221-
checkpoint: Pick<TurnDiffSummary, "status" | "files"> | undefined;
222+
checkpoint: Pick<TurnDiffSummary, "status" | "files" | "filesTotal"> | undefined;
222223
isGitRepo: boolean | undefined;
223224
}): "defer" | "ignore" | "open" {
224225
if (input.checkpoint === undefined || input.checkpoint.status === "missing") return "defer";
@@ -230,11 +231,8 @@ export function resolveProactiveTurnDiffAction(input: {
230231
) {
231232
return "ignore";
232233
}
233-
const changedLines = input.checkpoint.files.reduce(
234-
(total, file) => total + file.additions + file.deletions,
235-
0,
236-
);
237-
return input.checkpoint.files.length >= 3 || changedLines >= 50 ? "open" : "ignore";
234+
const totals = checkpointFileTotals(input.checkpoint);
235+
return totals.count >= 3 || totals.additions + totals.deletions >= 50 ? "open" : "ignore";
238236
}
239237

240238
/** Mark reactivated Diff tabs to preserve review scope. Already-active tabs must not leave a marker for

‎apps/web/src/components/chat/ChangedFilesTree.tsx‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import { type RunId } from "@t3tools/contracts";
1+
import { type OrchestrationV2CheckpointFileTotals, type RunId } from "@t3tools/contracts";
22
import { type MouseEvent, type ReactNode, memo, useLayoutEffect, useRef, useState } from "react";
33
import { type TurnDiffFileChange } from "../../types";
4-
import {
5-
buildTurnDiffTree,
6-
summarizeTurnDiffStats,
7-
type TurnDiffTreeNode,
8-
} from "../../lib/turnDiffTree";
4+
import { buildTurnDiffTree, type TurnDiffTreeNode } from "../../lib/turnDiffTree";
95
import { ChevronRightIcon, FileDiffIcon } from "lucide-react";
106
import { ChevronsDownUp, ChevronsUpDown, Folder, FolderClosed } from "lucide";
117
import { cn } from "~/lib/utils";
@@ -30,7 +26,9 @@ type ChangedFileContextMenuHandler = (filePath: string, event: MouseEvent) => vo
3026

3127
export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
3228
runId: RunId;
29+
/** May be a prefix of the changed files; `totals` covers all of them. */
3330
files: ReadonlyArray<TurnDiffFileChange>;
31+
totals: OrchestrationV2CheckpointFileTotals;
3432
allDirectoriesExpanded: boolean;
3533
resolvedTheme: "light" | "dark";
3634
onToggleAllDirectories: () => void;
@@ -40,13 +38,14 @@ export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
4038
const {
4139
runId,
4240
files,
41+
totals,
4342
allDirectoriesExpanded,
4443
resolvedTheme,
4544
onToggleAllDirectories,
4645
onOpenTurnDiff,
4746
onFileContextMenu,
4847
} = props;
49-
const summaryStat = summarizeTurnDiffStats(files);
48+
const hiddenFileCount = totals.count - files.length;
5049
const hasDirectories = files.some((file) => /[/\\]/.test(file.path));
5150

5251
return (
@@ -60,12 +59,12 @@ export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
6059
>
6160
<div className="flex min-w-0 flex-wrap items-center gap-x-3 gap-y-1 text-xs font-medium text-foreground">
6261
<span>
63-
{files.length} changed file{files.length === 1 ? "" : "s"}
62+
{totals.count.toLocaleString()} changed file{totals.count === 1 ? "" : "s"}
6463
</span>
65-
{hasNonZeroStat(summaryStat) && (
64+
{hasNonZeroStat(totals) && (
6665
<DiffStatLabel
67-
additions={summaryStat.additions}
68-
deletions={summaryStat.deletions}
66+
additions={totals.additions}
67+
deletions={totals.deletions}
6968
layout="inline"
7069
className="text-xs leading-4"
7170
/>
@@ -126,6 +125,19 @@ export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
126125
onOpenTurnDiff={onOpenTurnDiff}
127126
onFileContextMenu={onFileContextMenu}
128127
/>
128+
{hiddenFileCount > 0 && (
129+
<div className="px-3 pb-2">
130+
<Button
131+
type="button"
132+
size="xs"
133+
variant="ghost-muted"
134+
onClick={() => onOpenTurnDiff(runId, files[0]?.path)}
135+
>
136+
{hiddenFileCount.toLocaleString()} more file{hiddenFileCount === 1 ? "" : "s"} in the
137+
full diff
138+
</Button>
139+
</div>
140+
)}
129141
</div>
130142
);
131143
});

‎apps/web/src/components/chat/TimelineAssistantRows.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Button } from "../ui/button";
99
import { ProposedPlanCard } from "./ProposedPlanCard";
1010
import { HtmlRenderFrame } from "./HtmlRenderFrame";
1111
import { McpAppFrame } from "./McpAppFrame";
12+
import { checkpointFileTotals } from "@t3tools/client-runtime/state/thread-checkpoints";
1213
import { ChangedFilesCard } from "./ChangedFilesTree";
1314
import { useFileContextMenuHandler } from "../../fileContextMenu";
1415
import { useProject, useThreadShell } from "../../state/entities";
@@ -347,6 +348,7 @@ function AssistantChangedFilesSectionInner({
347348
<ChangedFilesCard
348349
runId={turnSummary.runId}
349350
files={checkpointFiles}
351+
totals={checkpointFileTotals(turnSummary)}
350352
allDirectoriesExpanded={allDirectoriesExpanded}
351353
resolvedTheme={resolvedTheme}
352354
onToggleAllDirectories={() =>

‎apps/web/src/lib/turnDiffTree.ts‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,6 @@ function toTreeNodes(directory: MutableDirectoryNode): TurnDiffTreeNode[] {
9898
return [...subdirectories, ...files];
9999
}
100100

101-
export function summarizeTurnDiffStats(files: ReadonlyArray<TurnDiffFileChange>): TurnDiffStat {
102-
return files.reduce(
103-
(acc, file) => {
104-
const stat = readStat(file);
105-
if (!stat) return acc;
106-
return {
107-
additions: acc.additions + stat.additions,
108-
deletions: acc.deletions + stat.deletions,
109-
};
110-
},
111-
{ additions: 0, deletions: 0 },
112-
);
113-
}
114-
115101
export function buildTurnDiffTree(files: ReadonlyArray<TurnDiffFileChange>): TurnDiffTreeNode[] {
116102
const root: MutableDirectoryNode = {
117103
name: "",

‎packages/client-runtime/src/state/threadCheckpoints.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
CheckpointRef,
44
CheckpointScopeId,
55
MessageId,
6+
OrchestrationV2CheckpointFileTotals,
67
OrchestrationV2ThreadProjection,
78
RunId,
89
} from "@t3tools/contracts";
@@ -15,12 +16,14 @@ export interface ThreadCheckpointSummary {
1516
readonly checkpointTurnCount: number;
1617
readonly checkpointRef: CheckpointRef;
1718
readonly status: "ready" | "missing" | "error" | "stale";
19+
/** A prefix of the changed files when `filesTotal` is set; see `checkpointFileTotals`. */
1820
readonly files: ReadonlyArray<{
1921
readonly path: string;
2022
readonly kind: string;
2123
readonly additions: number;
2224
readonly deletions: number;
2325
}>;
26+
readonly filesTotal?: OrchestrationV2CheckpointFileTotals;
2427
readonly assistantMessageId: MessageId | null;
2528
readonly completedAt: string;
2629
}
@@ -43,9 +46,23 @@ export function deriveThreadCheckpointSummaries(
4346
checkpointRef: checkpoint.ref,
4447
status: checkpoint.status,
4548
files: checkpoint.files,
49+
...(checkpoint.filesTotal === undefined ? {} : { filesTotal: checkpoint.filesTotal }),
4650
assistantMessageId,
4751
completedAt: DateTime.formatIso(checkpoint.capturedAt),
4852
},
4953
];
5054
});
5155
}
56+
57+
export function checkpointFileTotals(
58+
checkpoint: Pick<ThreadCheckpointSummary, "files" | "filesTotal">,
59+
): OrchestrationV2CheckpointFileTotals {
60+
if (checkpoint.filesTotal !== undefined) return checkpoint.filesTotal;
61+
let additions = 0;
62+
let deletions = 0;
63+
for (const file of checkpoint.files) {
64+
additions += file.additions;
65+
deletions += file.deletions;
66+
}
67+
return { count: checkpoint.files.length, additions, deletions };
68+
}

0 commit comments

Comments
 (0)