Skip to content

Commit ace518f

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-29609
2 parents ede2715 + 30084f0 commit ace518f

2 files changed

Lines changed: 81 additions & 38 deletions

File tree

‎apps/server/src/persistence/OrchestrationEventStore.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ const makeEventStore = Effect.gen(function* () {
645645
FROM orchestration_events
646646
${
647647
threadId === undefined
648-
? sql``
648+
? // The planner can otherwise pick the covering stream index and scan every agent event.
649+
sql`INDEXED BY idx_orchestration_events_application_sequence`
649650
: sql`INDEXED BY idx_orchestration_events_agent_stream_sequence`
650651
}
651652
WHERE application_event_version = 2

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,76 @@ function parseSlice(slice: DiffSlice, cacheKey: string, ignoreWhitespace: boolea
176176
return parsed;
177177
}
178178

179+
type DiffModel = {
180+
readonly parsedSlices: ReadonlyArray<RenderablePatch | null>;
181+
readonly loadedFiles: ReadonlyArray<Extract<RenderablePatch, { kind: "files" }>["files"][number]>;
182+
readonly sourceFileStats: ReadonlyMap<string, ReturnType<typeof getDiffLineStat>>;
183+
};
184+
const diffModels = new WeakMap<ReadonlyArray<DiffSlice>, Map<string, DiffModel>>();
185+
const patchHashes = new WeakMap<DiffSlice, number>();
186+
187+
// Hashing, ordering and counting every file of a huge pull request costs a second, and the tab
188+
// renders far more often than its slices change, so the model is kept per slice list.
189+
function diffModel(
190+
slices: ReadonlyArray<DiffSlice>,
191+
keyPrefix: string,
192+
ignoreWhitespace: boolean,
193+
): DiffModel {
194+
let models = diffModels.get(slices);
195+
if (models === undefined) diffModels.set(slices, (models = new Map()));
196+
const cached = models.get(keyPrefix);
197+
if (cached !== undefined) return cached;
198+
const parsedSlices = slices.map((slice) => {
199+
let hash = patchHashes.get(slice);
200+
if (hash === undefined) patchHashes.set(slice, (hash = fnv1a32(slice.patch)));
201+
// The cache key carries the theme, which the tokenizer caches against, and the patch's
202+
// hash: a refreshed page reuses its cursor, so position alone would return a stale parse.
203+
return parseSlice(slice, `${keyPrefix}:${slice.cursor ?? "first"}:${hash}`, ignoreWhitespace);
204+
});
205+
const model: DiffModel = {
206+
parsedSlices,
207+
loadedFiles: orderDiffFilesLikeTree(
208+
parsedSlices.flatMap((parsed) =>
209+
parsed?.kind === "files" ? orderDiffFiles(parsed.files) : [],
210+
),
211+
),
212+
sourceFileStats: new Map(
213+
parsedSlices.flatMap((parsed) =>
214+
parsed?.kind === "files"
215+
? parsed.files.map(
216+
(file, index) =>
217+
[
218+
buildFileDiffRenderKey(file),
219+
getDiffLineStat([parsed.sourceFiles[index] ?? file]),
220+
] as const,
221+
)
222+
: [],
223+
),
224+
),
225+
};
226+
models.set(keyPrefix, model);
227+
return model;
228+
}
229+
230+
const threadsByPathCache = new WeakMap<
231+
ReadonlyArray<PullRequestReviewThread>,
232+
ReadonlyMap<string, ReadonlyArray<PullRequestReviewThread>>
233+
>();
234+
235+
function reviewThreadsByPath(threads: ReadonlyArray<PullRequestReviewThread>) {
236+
let byPath = threadsByPathCache.get(threads);
237+
if (byPath === undefined) {
238+
const grouped = new Map<string, PullRequestReviewThread[]>();
239+
for (const thread of threads) {
240+
const list = grouped.get(thread.path);
241+
if (list) list.push(thread);
242+
else grouped.set(thread.path, [thread]);
243+
}
244+
threadsByPathCache.set(threads, (byPath = grouped));
245+
}
246+
return byPath;
247+
}
248+
179249
interface FileViewedToggleState {
180250
readonly enabled: boolean;
181251
readonly isViewed: (path: string) => boolean;
@@ -491,20 +561,10 @@ function PullRequestCodeTab({
491561
// Agent context is local, so it does not require host comment permission.
492562
const canSelectLines =
493563
commit === null && (review.inlineComment || onAddToAgentSelection !== undefined);
494-
// The cache key carries the theme, which is what the tokenizer caches against, so a theme
495-
// change is still a fresh parse. The patch's own hash is part of it too: a refreshed page
496-
// reuses its cursor, and a key of position alone would hand back the parse it replaced.
497-
const parsedSlices = loadedSlices.map((slice) =>
498-
parseSlice(
499-
slice,
500-
`pull-request:${scopeKey}:${resolvedTheme}:${ignoreWhitespace}:${slice.cursor ?? "first"}:${fnv1a32(slice.patch)}`,
501-
ignoreWhitespace,
502-
),
503-
);
504-
const loadedFiles = orderDiffFilesLikeTree(
505-
parsedSlices.flatMap((parsed) =>
506-
parsed?.kind === "files" ? orderDiffFiles(parsed.files) : [],
507-
),
564+
const { parsedSlices, loadedFiles, sourceFileStats } = diffModel(
565+
loadedSlices,
566+
`pull-request:${scopeKey}:${resolvedTheme}:${ignoreWhitespace}`,
567+
ignoreWhitespace,
508568
);
509569
const testFileCount = loadedFiles.filter((file) =>
510570
isTestChangedFile(resolveFileDiffPath(file)),
@@ -523,33 +583,16 @@ function PullRequestCodeTab({
523583
slice.omittedFileStats.map((file) => [file.path, file] as const),
524584
),
525585
);
526-
const sourceFileStats = new Map(
527-
parsedSlices.flatMap((parsed) =>
528-
parsed?.kind === "files"
529-
? parsed.files.map(
530-
(file, index) =>
531-
[
532-
buildFileDiffRenderKey(file),
533-
getDiffLineStat([parsed.sourceFiles[index] ?? file]),
534-
] as const,
535-
)
536-
: [],
537-
),
538-
);
539586
const fileTreeEntries = diffFileTreeEntries(files);
587+
const threadsByPath = reviewThreadsByPath(detail.reviewThreads);
540588
// File membership does not guarantee a rendered hunk for the thread.
541589
const placedThreadIds = (() => {
542590
const placed = new Set<string>();
543591
// Commit diffs use different line coordinates; list conversations separately.
544592
if (commit !== null) return placed;
545593
for (const file of files) {
546-
const path = resolveFileDiffPath(file);
547-
for (const thread of detail.reviewThreads) {
548-
if (
549-
thread.path === path &&
550-
thread.line !== null &&
551-
isLineInFileDiff(file, thread.side, thread.line)
552-
) {
594+
for (const thread of threadsByPath.get(resolveFileDiffPath(file)) ?? []) {
595+
if (thread.line !== null && isLineInFileDiff(file, thread.side, thread.line)) {
553596
placed.add(thread.id);
554597
}
555598
}
@@ -576,9 +619,8 @@ function PullRequestCodeTab({
576619
return created;
577620
};
578621

579-
for (const thread of detail.reviewThreads) {
580-
if (thread.path !== path || thread.line === null) continue;
581-
if (!placedThreadIds.has(thread.id)) continue;
622+
for (const thread of threadsByPath.get(path) ?? []) {
623+
if (thread.line === null || !placedThreadIds.has(thread.id)) continue;
582624
groupAt(thread.side, thread.line).threads.push(thread);
583625
}
584626
if (commit === null) {

0 commit comments

Comments
 (0)