From da7a0fa786a2b22c57d0d3086fdf55cccf072f31 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Sun, 4 Oct 2026 10:35:53 -0400 Subject: [PATCH 01/13] fix(web): keep the review scope across agent turns An agent turn rebuilds the thread shell, and the completed-turn path always selected Changes. A review left on Uncommitted therefore snapped back on every turn. Keep the current scope while the diff stays open on the same thread, and select Changes only when the diff newly opens. --- .../web/src/components/ChatView.logic.test.ts | 87 +++++++++++++++++++ apps/web/src/components/ChatView.logic.ts | 36 ++++++++ apps/web/src/components/ChatView.tsx | 26 +++++- apps/web/src/hooks/useProactivePanels.ts | 6 +- 4 files changed, 151 insertions(+), 4 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 3304899216fcd..c224a8e3e3f9a 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -1,3 +1,4 @@ +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { boundAttachmentIds, mergeDirectPreviewAnnotation, @@ -45,6 +46,8 @@ import { resetHeldThreadTimeline, resolveThreadSwitchTimeline, timelineHasEphemeralPreviewUrls, + shouldApplyProactiveChangesScope, + shouldResetDiffSelectionToChanges, createLocalDispatchSnapshot, deriveCommittedServerUserMessageIds, deriveComposerSendState, @@ -352,6 +355,90 @@ describe("deriveCommittedServerUserMessageIds", () => { }); }); +describe("diff selection across agent turns", () => { + const threadRef = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-1")); + const otherThreadRef = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-2")); + const openThread = { threadKey: "env-1:thread-1", diffOpen: true }; + + it("keeps Uncommitted when the same thread refreshes", () => { + expect( + shouldResetDiffSelectionToChanges({ + diffOpen: true, + activeThreadRef: threadRef, + explicitThreadRef: null, + previous: openThread, + threadKey: "env-1:thread-1", + }), + ).toBe(false); + }); + + it("shows Changes when the diff opens or the thread changes", () => { + expect( + shouldResetDiffSelectionToChanges({ + diffOpen: true, + activeThreadRef: threadRef, + explicitThreadRef: null, + previous: { threadKey: "env-1:thread-1", diffOpen: false }, + threadKey: "env-1:thread-1", + }), + ).toBe(true); + expect( + shouldResetDiffSelectionToChanges({ + diffOpen: true, + activeThreadRef: otherThreadRef, + explicitThreadRef: null, + previous: openThread, + threadKey: "env-1:thread-2", + }), + ).toBe(true); + expect( + shouldResetDiffSelectionToChanges({ + diffOpen: true, + activeThreadRef: threadRef, + explicitThreadRef: null, + previous: null, + threadKey: "env-1:thread-1", + }), + ).toBe(true); + }); + + it("leaves a timeline turn selection in place", () => { + expect( + shouldResetDiffSelectionToChanges({ + diffOpen: true, + activeThreadRef: threadRef, + explicitThreadRef: threadRef, + previous: { threadKey: "env-1:thread-1", diffOpen: false }, + threadKey: "env-1:thread-1", + }), + ).toBe(false); + }); + + it("does nothing while the diff is closed", () => { + expect( + shouldResetDiffSelectionToChanges({ + diffOpen: false, + activeThreadRef: threadRef, + explicitThreadRef: null, + previous: openThread, + threadKey: "env-1:thread-1", + }), + ).toBe(false); + }); + + it("shows Changes only when a completed turn newly opens the diff", () => { + expect( + shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff: false }), + ).toBe(true); + expect(shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff: true })).toBe( + false, + ); + expect( + shouldApplyProactiveChangesScope({ openedDiff: false, alreadyReviewingDiff: false }), + ).toBe(false); + }); +}); + describe("resolveThreadSwitchTimeline", () => { afterEach(() => { resetHeldThreadTimeline(); diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index c7c0f77bc97fb..30112f177fa0c 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -202,6 +202,42 @@ export function shouldRetargetThreadPullRequestPanel( ); } +export interface DiffPanelOpenContext { + readonly threadKey: string | null; + readonly diffOpen: boolean; +} + +/** + * Generic openings show Changes: the first time the diff is on screen, a tab + * fallback, or a switch to another thread. A timeline click passes the same + * thread ref it just selected and keeps that turn. An agent turn replaces the + * thread shell without closing the diff, and that refresh must leave Uncommitted + * or the selected turn alone. + */ +export function shouldResetDiffSelectionToChanges(input: { + readonly diffOpen: boolean; + readonly activeThreadRef: ScopedThreadRef | null; + readonly explicitThreadRef: ScopedThreadRef | null; + readonly previous: DiffPanelOpenContext | null; + readonly threadKey: string | null; +}): boolean { + if (!input.diffOpen || input.activeThreadRef === null) return false; + if (input.explicitThreadRef === input.activeThreadRef) return false; + return !( + input.previous !== null && + input.previous.diffOpen && + input.previous.threadKey === input.threadKey + ); +} + +/** Opening the diff for a completed turn shows Changes. A review already on screen stays put. */ +export function shouldApplyProactiveChangesScope(input: { + readonly openedDiff: boolean; + readonly alreadyReviewingDiff: boolean; +}): boolean { + return input.openedDiff && !input.alreadyReviewingDiff; +} + export function shouldOpenProactiveTurnDiff(input: { previousRunningTurnId: RunId | null | undefined; runningTurnId: RunId | null; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 253cc1ea2e84b..931bcab87a3c4 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -31,7 +31,11 @@ import { ThreadFind, ThreadFindCanvas } from "./chat/ThreadFindProvider"; import { closeThreadFind, openThreadFind, useThreadFindOpen } from "./chat/threadFindStore"; import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar"; import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner"; -import { resolveDraftHeroState } from "./ChatView.logic"; +import { + type DiffPanelOpenContext, + resolveDraftHeroState, + shouldResetDiffSelectionToChanges, +} from "./ChatView.logic"; import { useQueuedRunEdit } from "./chat/useQueuedRunEdit"; import { recallCheckoutIsRepo, rememberCheckoutIsRepo } from "./ChatView.logic"; import { useLogicalProjectEnvironments } from "../hooks/useLogicalProjectEnvironments"; @@ -976,14 +980,30 @@ export default function ChatView(props: ChatViewProps) { ); const diffOpen = activeRightPanelKind === "diff"; const explicitDiffOpenRef = useRef(null); + const diffPanelOpenContextRef = useRef(null); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- resets the diff panel store to branch scope when the diff opens without an explicit request useLayoutEffect(() => { const explicitThreadRef = explicitDiffOpenRef.current; explicitDiffOpenRef.current = null; - if (diffOpen && activeThreadRef && explicitThreadRef !== activeThreadRef) { + const previous = diffPanelOpenContextRef.current; + diffPanelOpenContextRef.current = { threadKey: activeThreadKey, diffOpen }; + // Generic openings show Changes: the first time the diff is on screen, a tab + // fallback, or a switch to another thread. A timeline click keeps its turn. + // An agent turn replaces the thread shell without closing the diff, and that + // refresh must leave Uncommitted or the selected turn alone. + if ( + activeThreadRef && + shouldResetDiffSelectionToChanges({ + diffOpen, + activeThreadRef, + explicitThreadRef, + previous, + threadKey: activeThreadKey, + }) + ) { useDiffPanelStore.getState().selectGitScope(activeThreadRef, "branch"); } - }, [activeThreadRef, diffOpen]); + }, [activeThreadKey, activeThreadRef, diffOpen]); const rightPanelState = useRightPanelStore((state) => selectThreadRightPanelState(state.byThreadKey, activeThreadRef), ); diff --git a/apps/web/src/hooks/useProactivePanels.ts b/apps/web/src/hooks/useProactivePanels.ts index 60c78a850c25f..1fe137872ba08 100644 --- a/apps/web/src/hooks/useProactivePanels.ts +++ b/apps/web/src/hooks/useProactivePanels.ts @@ -1,3 +1,4 @@ +import { shouldApplyProactiveChangesScope } from "../components/ChatView.logic"; import type { RunId, ScopedThreadRef, ThreadLinkedPullRequest } from "@t3tools/contracts"; import { scopedThreadKey } from "@t3tools/client-runtime/environment"; import { useEffect, useRef } from "react"; @@ -176,6 +177,7 @@ export function useProactivePanels(input: { diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId, }; if (diffAction !== "open" || newlyCompletedTurnId === null) return; + const alreadyReviewingDiff = openSurface?.kind === "diff"; if ( !panels.openProactive( threadRef, @@ -185,7 +187,9 @@ export function useProactivePanels(input: { ) { return; } - useDiffPanelStore.getState().selectGitScope(threadRef, "branch"); + if (shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff })) { + useDiffPanelStore.getState().selectGitScope(threadRef, "branch"); + } onDiffPanelOpen?.(); }, [ activeLatestRun?.runId, From 130680b853c2347b0a91895fbd4b7ab40cc34d9c Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Sun, 4 Oct 2026 11:10:07 -0400 Subject: [PATCH 02/13] fix(web): keep diff scroll across review refreshes A new diff hash remounted the viewer and dropped the pages already on screen, so an agent turn sent the reader back to the top of Uncommitted, Changes, and pull request review. Co-authored-by: Cursor --- apps/web/src/components/DiffPanel.tsx | 7 +- .../diffs/reviewFileDiffRetention.test.ts | 94 ++++++++ .../diffs/reviewFileDiffRetention.ts | 88 +++++++ .../components/diffs/useReviewFilePatches.ts | 224 +++++++++++------- .../pullRequest/PullRequestCodeTab.tsx | 9 +- .../pullRequest/pullRequestDiff.logic.test.ts | 25 ++ .../pullRequest/pullRequestDiff.logic.ts | 21 ++ 7 files changed, 377 insertions(+), 91 deletions(-) create mode 100644 apps/web/src/components/diffs/reviewFileDiffRetention.test.ts create mode 100644 apps/web/src/components/diffs/reviewFileDiffRetention.ts diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index c257d0ea52d34..cc3fd451631c8 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -96,6 +96,7 @@ import { vcsEnvironment } from "../state/vcs"; import { buildBaseRefChoices, filterBaseRefChoices } from "../lib/baseRefChoices"; import { createGitDiffFileContentsLoader } from "../lib/diffFileContents"; +import { reviewDiffViewerKey } from "./diffs/reviewFileDiffRetention"; import { useReviewFilePatches } from "./diffs/useReviewFilePatches"; import { DiffFileLoadingBoundary } from "./diffs/DiffFileLoadingBoundary"; import { DiffFileStatus } from "./diffs/DiffFileStatus"; @@ -496,6 +497,7 @@ export default function DiffPanel({ ]; const { scope: filePatchScope, + family: filePatchFamily, isPending: areFilePatchesPending, fileStates, retry, @@ -1154,7 +1156,10 @@ export default function DiffPanel({ { + it("keeps the same review across git snapshots", () => { + const family = reviewSnapshotFamily(identity); + expect(reviewSnapshotFamily(identity)).toBe(family); + expect(reviewSnapshotScope(family, "hash-a")).not.toBe(reviewSnapshotScope(family, "hash-b")); + expect(reviewDiffViewerKey("section", family)).toBe(`section:${family}`); + expect(reviewDiffViewerKey("section", null)).toBe("section:preview"); + }); + + it("keeps loaded file indexes when the snapshot family is unchanged", () => { + const family = reviewSnapshotFamily(identity); + expect(reviewPatchIndicesForFamily({ family, indices: [0, 1, 2, 3, 8] }, family)).toEqual([ + 0, 1, 2, 3, 8, + ]); + expect( + reviewPatchIndicesForFamily({ family, indices: [0, 1, 2, 3, 8] }, `${family}:other`), + ).toEqual([0, 1, 2, 3]); + }); +}); + +describe("retained review files", () => { + const loaded = file("src/app.ts", "hash:src/app.ts"); + const placeholder = file("src/app.ts", "next:src/app.ts:pending"); + + it("keeps the open file until the next patch arrives", () => { + expect( + retainedReviewFile({ + loaded: null, + previous: loaded, + placeholder, + sameFamily: true, + }), + ).toBe(loaded); + }); + + it("uses the newly loaded patch", () => { + const next = file("src/app.ts", "hash-b:src/app.ts"); + expect( + retainedReviewFile({ + loaded: next, + previous: loaded, + placeholder, + sameFamily: true, + }), + ).toBe(next); + }); + + it("does not carry a file into a different review", () => { + expect( + retainedReviewFile({ + loaded: null, + previous: loaded, + placeholder, + sameFamily: false, + }), + ).toBe(placeholder); + expect(isPendingReviewFile(placeholder)).toBe(true); + }); + + it("stores loaded files and ignores placeholders", () => { + const previous = new Map([["src/app.ts", loaded]]); + expect(retainLoadedReviewFiles(previous, [loaded, placeholder], (entry) => entry.name)).toBe( + previous, + ); + const next = file("src/app.ts", "hash-b:src/app.ts"); + const retained = retainLoadedReviewFiles(previous, [next], (entry) => entry.name); + expect(retained.get("src/app.ts")).toBe(next); + }); +}); diff --git a/apps/web/src/components/diffs/reviewFileDiffRetention.ts b/apps/web/src/components/diffs/reviewFileDiffRetention.ts new file mode 100644 index 0000000000000..81eb4be4e331f --- /dev/null +++ b/apps/web/src/components/diffs/reviewFileDiffRetention.ts @@ -0,0 +1,88 @@ +import type { FileDiffMetadata } from "@pierre/diffs"; + +const INITIAL_PATCH_INDICES = [0, 1, 2, 3] as const; +const EMPTY_RETAINED_FILES: ReadonlyMap = new Map(); + +export interface ReviewSnapshotIdentity { + readonly environmentId: string | undefined; + readonly cwd: string | undefined; + readonly kind: string | undefined; + readonly baseRef: string | null; + readonly ignoreWhitespace: boolean; +} + +/** Identity of a review that survives a new git snapshot. The diff hash is not part of it. */ +export function reviewSnapshotFamily(input: ReviewSnapshotIdentity): string { + return JSON.stringify([ + input.environmentId, + input.cwd, + input.kind, + input.baseRef, + input.ignoreWhitespace, + ]); +} + +export function reviewSnapshotScope(family: string, diffHash: string | undefined): string { + return JSON.stringify([family, diffHash ?? null]); +} + +/** + * Keep requesting the files already on screen when the snapshot changes. A different + * review starts over at the first page. + */ +export function reviewPatchIndicesForFamily( + requested: { readonly family: string; readonly indices: readonly number[] }, + family: string, +): readonly number[] { + return requested.family === family ? requested.indices : INITIAL_PATCH_INDICES; +} + +export function isPendingReviewFile(file: { readonly cacheKey?: string | undefined }): boolean { + return file.cacheKey?.endsWith(":pending") === true; +} + +/** Show the file already on screen until its replacement patch arrives. */ +export function retainedReviewFile(input: { + readonly loaded: T | null; + readonly previous: T | undefined; + readonly placeholder: T; + readonly sameFamily: boolean; +}): T { + if (input.loaded) return input.loaded; + if (input.sameFamily && input.previous && !isPendingReviewFile(input.previous)) { + return input.previous; + } + return input.placeholder; +} + +export function retainLoadedReviewFiles( + previous: ReadonlyMap, + files: readonly FileDiffMetadata[], + pathOf: (file: FileDiffMetadata) => string, +): ReadonlyMap { + const next = new Map(); + for (const file of files) { + if (!isPendingReviewFile(file)) next.set(pathOf(file), file); + } + if (next.size === previous.size) { + let unchanged = true; + for (const [path, file] of next) { + if (previous.get(path) !== file) { + unchanged = false; + break; + } + } + if (unchanged) return previous; + } + return next.size === 0 ? EMPTY_RETAINED_FILES : next; +} + +/** The viewer remounts for a new review section, not for a new git snapshot of the same one. */ +export function reviewDiffViewerKey( + sectionMountKey: string, + snapshotFamily: string | null, +): string { + return snapshotFamily === null + ? `${sectionMountKey}:preview` + : `${sectionMountKey}:${snapshotFamily}`; +} diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts index b683e13a8f6ee..3e7f365c72b3f 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.ts +++ b/apps/web/src/components/diffs/useReviewFilePatches.ts @@ -7,10 +7,20 @@ import type { } from "@t3tools/contracts"; import * as AsyncResult from "effect/reactivity/AsyncResult"; import * as Atom from "effect/reactivity/Atom"; -import { useContext, useState } from "react"; +import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "react"; import { useStableCallback } from "~/hooks/useStableCallback"; import { getRenderablePatch, resolveFileDiffPath, type RenderablePatch } from "~/lib/diffRendering"; import { reviewEnvironment } from "~/state/review"; +import { + isPendingReviewFile, + retainLoadedReviewFiles, + retainedReviewFile, + reviewPatchIndicesForFamily, + reviewSnapshotFamily, + reviewSnapshotScope, +} from "./reviewFileDiffRetention"; + +const EMPTY_RETAINED_FILES: ReadonlyMap = new Map(); const filePathCollator = new Intl.Collator(undefined, { numeric: true, @@ -39,39 +49,96 @@ export function useReviewFilePatches({ preview: RenderablePatch | null; }) { const registry = useContext(RegistryContext); - const scope = JSON.stringify([ - environmentId, - cwd, - source?.kind, - source?.diffHash, - baseRef, - ignoreWhitespace, - ]); - const [requested, setRequested] = useState(() => ({ scope, indices: DEFAULT_INDICES })); - const queries = reviewFileQueries({ + const snapshotFamily = reviewSnapshotFamily({ environmentId, cwd, - source, + kind: source?.kind, baseRef, ignoreWhitespace, - previewInput, - scope, - requested, }); + const scope = reviewSnapshotScope(snapshotFamily, source?.diffHash); + const [requested, setRequested] = useState({ family: snapshotFamily, indices: [0, 1, 2, 3] }); + const [retainedFiles, setRetainedFiles] = useState<{ + readonly family: string; + readonly byPath: ReadonlyMap; + }>({ family: snapshotFamily, byPath: EMPTY_RETAINED_FILES }); + const previousByPath = retainedFiles.family === snapshotFamily ? retainedFiles.byPath : EMPTY_RETAINED_FILES; + const files = sortedReviewFiles(source); + const queries = reviewFileQueries({ environmentId, cwd, source, baseRef, ignoreWhitespace, previewInput, scope, + requested: { scope, indices: [...reviewPatchIndicesForFamily(requested, snapshotFamily)] } }); const queriedPatchesAtom = reviewPatchesAtom( JSON.stringify([theme, queries.map(({ index, target }) => [index, target])]), ); const patches = useAtomValue(queriedPatchesAtom); const retry = useStableCallback((path: string) => { - const files = sortedReviewFiles(source); - const query = queries.find(({ index }) => files[index]?.path === path)?.query; - if (query) registry.refresh(query); - }); - const files = sortedReviewFiles(source); - const pendingIndex = files.findIndex((_, index) => { - const patch = patches.get(index); - return !patch || patch._tag === "Initial"; - }); + const requestFiles = useCallback( + (nextIndices: number[]) => + setRequested((current) => { + const previous = reviewPatchIndicesForFamily(current, snapshotFamily); + const added = nextIndices.filter((index) => !previous.includes(index)); + if (added.length === 0 && current.family === snapshotFamily) return current; + return { family: snapshotFamily, indices: [...previous, ...added] }; + }), + [snapshotFamily], + ); + const requestFile = useCallback((index: number) => requestFiles([index]), [requestFiles]); + const renderableFiles = useMemo( + () => + source + ? files.map((file, index): FileDiffMetadata => { + const result = patches.get(index); + const loaded = + result?._tag === "Success" && result.value.patch?.kind === "files" + ? (result.value.patch.files.find( + (candidate) => resolveFileDiffPath(candidate) === file.path, + ) ?? null) + : null; + return retainedReviewFile({ + loaded, + previous: previousByPath.get(file.path), + sameFamily: true, + placeholder: { + name: file.path, + ...(file.previousPath ? { prevName: file.previousPath } : {}), + type: file.previousPath ? "rename-changed" : "change", + hunks: [], + additionLines: [], + deletionLines: [], + additionCharacterCount: 0, + deletionCharacterCount: 0, + splitLineCount: 0, + unifiedLineCount: 0, + isPartial: true, + cacheKey: `${scope}:${file.path}:pending`, + }, + }); + }) + : (preview?.kind === "files" ? preview.files : []).toSorted((a, b) => + filePathCollator.compare(resolveFileDiffPath(a), resolveFileDiffPath(b)), + ), + [source, files, patches, scope, preview, previousByPath], + ); + const nextRetainedFiles = source + ? retainLoadedReviewFiles(previousByPath, renderableFiles, resolveFileDiffPath) + : EMPTY_RETAINED_FILES; + useEffect(() => { + if (!source) return; + if (retainedFiles.family === snapshotFamily && retainedFiles.byPath === nextRetainedFiles) + return; + setRetainedFiles({ family: snapshotFamily, byPath: nextRetainedFiles }); + }, [nextRetainedFiles, retainedFiles.byPath, retainedFiles.family, snapshotFamily, source]); + const pendingIndex = source + ? files.findIndex((file, index) => { + const patch = patches.get(index); + if (patch && patch._tag !== "Initial") return false; + const rendered = renderableFiles[index]; + return ( + rendered === undefined || + isPendingReviewFile(rendered) || + resolveFileDiffPath(rendered) !== file.path + ); + }) + : -1; const settledFileCount = source ? pendingIndex < 0 ? files.length @@ -79,73 +146,56 @@ export function useReviewFilePatches({ : preview?.kind === "files" ? preview.files.length : 0; - const requestFiles = (indices: number[]) => - setRequested((current) => { - const previous = current.scope === scope ? current.indices : [0, 1, 2, 3]; - const added = indices.filter((index) => !previous.includes(index)); - return added.length === 0 ? current : { scope, indices: [...previous, ...added] }; - }); - // The first unsettled file already requested means a batch is in flight; wait for it. - const loadNextFiles = () => { - if (queries.some(({ index }) => index === settledFileCount)) return; - requestFiles(Array.from({ length: 4 }, (_, index) => settledFileCount + index)); - }; - const requestFile = (index: number) => requestFiles([index]); - const renderableFiles = source - ? files.map((file, index): FileDiffMetadata => { - const result = patches.get(index); - if (result?._tag === "Success" && result.value.patch?.kind === "files") { - const loaded = result.value.patch.files.find( - (candidate) => resolveFileDiffPath(candidate) === file.path, - ); - if (loaded) return loaded; - } - return { - name: file.path, - ...(file.previousPath ? { prevName: file.previousPath } : {}), - type: file.previousPath ? "rename-changed" : "change", - hunks: [], - additionLines: [], - deletionLines: [], - additionCharacterCount: 0, - deletionCharacterCount: 0, - splitLineCount: 0, - unifiedLineCount: 0, - isPartial: true, - cacheKey: `${scope}:${file.path}:pending`, - }; - }) - : (preview?.kind === "files" ? preview.files : []).toSorted((a, b) => - filePathCollator.compare(resolveFileDiffPath(a), resolveFileDiffPath(b)), - ); - const fileStates = new Map( - files.map((file, index) => { - const patch = patches.get(index); - return [ - file.path, - { - error: - patch?._tag === "Failure" || - (patch?._tag === "Success" && - (patch.value.patch?.kind !== "files" || - !patch.value.patch.files.some( - (candidate) => resolveFileDiffPath(candidate) === file.path, - ))), - truncated: patch?._tag === "Success" && patch.value.source.truncated, - }, - ] as const; - }), + const loadNextFiles = useCallback( + () => { + if (queries.some(({ index }) => index === settledFileCount)) return; + requestFiles(Array.from({ length: 4 }, (_, index) => settledFileCount + index)); + }, + [queries, requestFiles, settledFileCount], ); - const readyFilePaths = new Set( - files - .filter((_, index) => { - const patch = patches.get(index); - return patch && patch._tag !== "Initial"; - }) - .map((file) => file.path), + const fileStates = useMemo( + () => + new Map( + files.map((file, index) => { + const patch = patches.get(index); + return [ + file.path, + { + error: + patch?._tag === "Failure" || + (patch?._tag === "Success" && + (patch.value.patch?.kind !== "files" || + !patch.value.patch.files.some( + (candidate) => resolveFileDiffPath(candidate) === file.path, + ))), + truncated: patch?._tag === "Success" && patch.value.source.truncated, + }, + ] as const; + }), + ), + [files, patches], + ); + const readyFilePaths = useMemo( + () => + new Set( + files + .filter((file, index) => { + const patch = patches.get(index); + if (patch && patch._tag !== "Initial") return true; + const rendered = renderableFiles[index]; + return ( + rendered !== undefined && + !isPendingReviewFile(rendered) && + resolveFileDiffPath(rendered) === file.path + ); + }) + .map((file) => file.path), + ), + [files, patches, renderableFiles], ); return { scope, + family: snapshotFamily, fileStates, isPending: [...patches.values()].some((patch) => patch._tag === "Initial" || patch.waiting), retry, diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index d1b165c62147b..095edf82a06d6 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -109,6 +109,7 @@ import { getFileViewedRange, isFileDiffCollapsed, isLineInFileDiff, + retainReviewDiffSlices, toggleFileDiffFoldForViewed, type DiffFoldOverride, } from "./pullRequestDiff.logic"; @@ -443,14 +444,16 @@ function PullRequestCodeTab({ isViewed: isFileViewed, isStale: isFileViewedStale, } = filesViewed; + // Reread the first page and the viewed ticks. The pages already on screen stay until that + // page arrives: blanking them remounts the viewer and sends the reader back to the top, + // including on every agent turn. const appliedRefreshToken = useRef(refreshToken); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a new refresh token clears loaded slices and refetches the first diff page and viewed files useEffect(() => { if (appliedRefreshToken.current === refreshToken) return; appliedRefreshToken.current = refreshToken; - setSliceState({ key: scopeKey, cursor: null, slices: NO_SLICES }); - // An equal refetch keeps the answer's identity, so the emptied slices refill only if the - // applied page is forgotten too. + setSliceState((previous) => retainReviewDiffSlices(previous, scopeKey)); + // An equal refetch keeps the answer's identity, so forget the applied page to reapply it. setAppliedPage(null); refreshFirstDiffPage(); refreshFilesViewed(); diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts index 552fa748f07f6..c92b907f36504 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts @@ -5,6 +5,7 @@ import { getFileViewedRange, isFileDiffCollapsed, isLineInFileDiff, + retainReviewDiffSlices, toggleFileDiffFoldForViewed, } from "./pullRequestDiff.logic"; @@ -19,6 +20,30 @@ function fileWithHunks( return { name: "src/app.ts", hunks } as unknown as FileDiffMetadata; } +describe("retainReviewDiffSlices", () => { + const slices = [{ cursor: null, patch: "page-1" }]; + + it("keeps the open pages when the same review refreshes", () => { + const previous = { key: "pr-1", cursor: "page-2", slices }; + expect(retainReviewDiffSlices(previous, "pr-1")).toEqual({ + key: "pr-1", + cursor: null, + slices, + }); + expect(retainReviewDiffSlices({ key: "pr-1", cursor: null, slices }, "pr-1").slices).toBe( + slices, + ); + }); + + it("starts over when the review itself changes", () => { + expect(retainReviewDiffSlices({ key: "pr-1", cursor: null, slices }, "pr-2")).toEqual({ + key: "pr-2", + cursor: null, + slices: [], + }); + }); +}); + describe("isLineInFileDiff", () => { const file = fileWithHunks([ { deletionStart: 10, deletionCount: 3, additionStart: 10, additionCount: 5 }, diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts index f175c0151431a..8dddc84592224 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts @@ -1,6 +1,27 @@ import type { FileDiffMetadata } from "@pierre/diffs"; import type { PullRequestDiffSide } from "@t3tools/contracts"; +export interface ReviewDiffSliceState { + readonly key: string; + readonly cursor: string | null; + readonly slices: ReadonlyArray; +} + +/** + * A refresh rereads the first page, and the pages already on screen stay until that page + * arrives. Clearing them remounts the viewer and sends the reader back to the top. + */ +export function retainReviewDiffSlices( + previous: ReviewDiffSliceState, + scopeKey: string, +): ReviewDiffSliceState { + if (previous.key !== scopeKey) { + return { key: scopeKey, cursor: null, slices: [] }; + } + if (previous.cursor === null) return previous; + return { key: previous.key, cursor: null, slices: previous.slices }; +} + /** Only rendered hunks can anchor comments; list others off-diff. */ export function isLineInFileDiff( file: FileDiffMetadata, From 1b9881501622efc8da21811ea7241a481aa5e399 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Sun, 4 Oct 2026 11:57:41 -0400 Subject: [PATCH 03/13] fix(web): reread later diff pages and drop failed patches A refresh that left the first page unchanged never fetched the pages after it, and a failed file patch stayed on screen as the current diff. Co-authored-by: Cursor --- .../web/src/components/ChatView.logic.test.ts | 9 +++++++ apps/web/src/components/ChatView.logic.ts | 13 +++++++--- .../diffs/reviewFileDiffRetention.test.ts | 15 +++++++++++ .../diffs/reviewFileDiffRetention.ts | 8 ++++-- .../components/diffs/useReviewFilePatches.ts | 1 + .../pullRequest/PullRequestCodeTab.tsx | 25 ++++++++++++++++--- .../pullRequest/pullRequestDiff.logic.test.ts | 15 ++++++----- .../pullRequest/pullRequestDiff.logic.ts | 13 +++++++--- 8 files changed, 79 insertions(+), 20 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index c224a8e3e3f9a..8808f23c0d034 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -412,6 +412,15 @@ describe("diff selection across agent turns", () => { threadKey: "env-1:thread-1", }), ).toBe(false); + expect( + shouldResetDiffSelectionToChanges({ + diffOpen: true, + activeThreadRef: scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-1")), + explicitThreadRef: scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-1")), + previous: { threadKey: "env-1:thread-1", diffOpen: false }, + threadKey: "env-1:thread-1", + }), + ).toBe(false); }); it("does nothing while the diff is closed", () => { diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 30112f177fa0c..0dd5bfda42da8 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -209,8 +209,9 @@ export interface DiffPanelOpenContext { /** * Generic openings show Changes: the first time the diff is on screen, a tab - * fallback, or a switch to another thread. A timeline click passes the same - * thread ref it just selected and keeps that turn. An agent turn replaces the + * fallback, or a switch to another thread. A timeline click names the thread it + * just selected and keeps that turn, matched by id because the shell hands out + * a new ref object whenever the thread updates. An agent turn replaces the * thread shell without closing the diff, and that refresh must leave Uncommitted * or the selected turn alone. */ @@ -222,7 +223,13 @@ export function shouldResetDiffSelectionToChanges(input: { readonly threadKey: string | null; }): boolean { if (!input.diffOpen || input.activeThreadRef === null) return false; - if (input.explicitThreadRef === input.activeThreadRef) return false; + if ( + input.explicitThreadRef !== null && + input.explicitThreadRef.environmentId === input.activeThreadRef.environmentId && + input.explicitThreadRef.threadId === input.activeThreadRef.threadId + ) { + return false; + } return !( input.previous !== null && input.previous.diffOpen && diff --git a/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts b/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts index 8f44c5e7eb74a..059705dc26f77 100644 --- a/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts +++ b/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts @@ -54,10 +54,23 @@ describe("retained review files", () => { previous: loaded, placeholder, sameFamily: true, + pending: true, }), ).toBe(loaded); }); + it("drops the open file once its replacement fails or no longer includes it", () => { + expect( + retainedReviewFile({ + loaded: null, + previous: loaded, + placeholder, + sameFamily: true, + pending: false, + }), + ).toBe(placeholder); + }); + it("uses the newly loaded patch", () => { const next = file("src/app.ts", "hash-b:src/app.ts"); expect( @@ -66,6 +79,7 @@ describe("retained review files", () => { previous: loaded, placeholder, sameFamily: true, + pending: false, }), ).toBe(next); }); @@ -77,6 +91,7 @@ describe("retained review files", () => { previous: loaded, placeholder, sameFamily: false, + pending: true, }), ).toBe(placeholder); expect(isPendingReviewFile(placeholder)).toBe(true); diff --git a/apps/web/src/components/diffs/reviewFileDiffRetention.ts b/apps/web/src/components/diffs/reviewFileDiffRetention.ts index 81eb4be4e331f..28c9c1dd10de2 100644 --- a/apps/web/src/components/diffs/reviewFileDiffRetention.ts +++ b/apps/web/src/components/diffs/reviewFileDiffRetention.ts @@ -41,15 +41,19 @@ export function isPendingReviewFile(file: { readonly cacheKey?: string | undefin return file.cacheKey?.endsWith(":pending") === true; } -/** Show the file already on screen until its replacement patch arrives. */ +/** + * Show the file already on screen while its replacement is still loading. + * A failure, or a snapshot that no longer contains the file, must not keep the old diff. + */ export function retainedReviewFile(input: { readonly loaded: T | null; readonly previous: T | undefined; readonly placeholder: T; readonly sameFamily: boolean; + readonly pending: boolean; }): T { if (input.loaded) return input.loaded; - if (input.sameFamily && input.previous && !isPendingReviewFile(input.previous)) { + if (input.pending && input.sameFamily && input.previous && !isPendingReviewFile(input.previous)) { return input.previous; } return input.placeholder; diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts index 3e7f365c72b3f..286881e8ec421 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.ts +++ b/apps/web/src/components/diffs/useReviewFilePatches.ts @@ -97,6 +97,7 @@ export function useReviewFilePatches({ loaded, previous: previousByPath.get(file.path), sameFamily: true, + pending: result === undefined || result._tag === "Initial" || result.waiting, placeholder: { name: file.path, ...(file.previousPath ? { prevName: file.previousPath } : {}), diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 095edf82a06d6..cc2e8c9f9a80f 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -444,14 +444,18 @@ function PullRequestCodeTab({ isViewed: isFileViewed, isStale: isFileViewedStale, } = filesViewed; - // Reread the first page and the viewed ticks. The pages already on screen stay until that - // page arrives: blanking them remounts the viewer and sends the reader back to the top, - // including on every agent turn. + // Reread from the first page. It stays on screen so the reader does not jump; later pages + // belong to the snapshot being replaced, so they are fetched again. Each of those pages is + // its own cached query, so the walk asks for it again instead of replaying the old snapshot. + const refreshGeneration = useRef(0); + const refreshedCursor = useRef(undefined); const appliedRefreshToken = useRef(refreshToken); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a new refresh token clears loaded slices and refetches the first diff page and viewed files useEffect(() => { if (appliedRefreshToken.current === refreshToken) return; appliedRefreshToken.current = refreshToken; + refreshGeneration.current += 1; + refreshedCursor.current = undefined; setSliceState((previous) => retainReviewDiffSlices(previous, scopeKey)); // An equal refetch keeps the answer's identity, so forget the applied page to reapply it. setAppliedPage(null); @@ -463,11 +467,21 @@ function PullRequestCodeTab({ useEffect(() => { if (appliedBackgroundRefreshToken.current === backgroundRefreshToken) return; appliedBackgroundRefreshToken.current = backgroundRefreshToken; + refreshGeneration.current += 1; + refreshedCursor.current = undefined; // Keeping slices mounted preserves scroll; the page merge invalidates changed pages. setSliceState((previous) => ({ ...previous, cursor: null })); refreshFirstDiffPage(); refreshFilesViewed(); }, [backgroundRefreshToken, refreshFirstDiffPage, refreshFilesViewed]); + useEffect(() => { + if (refreshGeneration.current === 0 || cursor === null) return; + if (refreshedCursor.current === cursor) return; + refreshedCursor.current = cursor; + diffQuery.refresh(); + }, [cursor, diffQuery.refresh]); + const nextCursor = loadedSlices.at(-1)?.nextCursor ?? null; + const codeViewReveal = useCodeViewFileReveal(); const answeredRevealNonce = useRef(null); @@ -499,6 +513,11 @@ function PullRequestCodeTab({ ignoreWhitespace, ), ); + // What a slice withheld: the host declining to inline part of it, or a patch the viewer could + // not structure and so dropped. Neither says anything about there being more to fetch. + const withheldContent = + loadedSlices.some((slice) => slice.truncated) || + parsedSlices.some((parsed) => parsed?.kind === "raw"); const files = parsedSlices.flatMap((parsed) => parsed?.kind === "files" ? orderDiffFiles(parsed.files) : [], ); diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts index c92b907f36504..1a1d76fa3e00c 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts @@ -21,18 +21,17 @@ function fileWithHunks( } describe("retainReviewDiffSlices", () => { - const slices = [{ cursor: null, patch: "page-1" }]; + const first = { cursor: null, patch: "page-1" }; + const slices = [first, { cursor: "page-2", patch: "page-2" }]; - it("keeps the open pages when the same review refreshes", () => { - const previous = { key: "pr-1", cursor: "page-2", slices }; - expect(retainReviewDiffSlices(previous, "pr-1")).toEqual({ + it("keeps the first page and drops later ones when the same review refreshes", () => { + expect(retainReviewDiffSlices({ key: "pr-1", cursor: "page-2", slices }, "pr-1")).toEqual({ key: "pr-1", cursor: null, - slices, + slices: [first], }); - expect(retainReviewDiffSlices({ key: "pr-1", cursor: null, slices }, "pr-1").slices).toBe( - slices, - ); + const open = { key: "pr-1", cursor: null, slices: [first] }; + expect(retainReviewDiffSlices(open, "pr-1")).toBe(open); }); it("starts over when the review itself changes", () => { diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts index 8dddc84592224..671ddeb080602 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts @@ -8,8 +8,9 @@ export interface ReviewDiffSliceState { } /** - * A refresh rereads the first page, and the pages already on screen stay until that page - * arrives. Clearing them remounts the viewer and sends the reader back to the top. + * A refresh rereads from the first page. That page stays on screen so the reader does not + * jump. Later pages are positions in the snapshot being replaced: keeping them when the + * first page comes back unchanged would leave those files stale for good. */ export function retainReviewDiffSlices( previous: ReviewDiffSliceState, @@ -18,8 +19,12 @@ export function retainReviewDiffSlices( if (previous.key !== scopeKey) { return { key: scopeKey, cursor: null, slices: [] }; } - if (previous.cursor === null) return previous; - return { key: previous.key, cursor: null, slices: previous.slices }; + const first = previous.slices[0]; + if (first === undefined) { + return previous.cursor === null ? previous : { key: scopeKey, cursor: null, slices: [] }; + } + if (previous.slices.length === 1 && previous.cursor === null) return previous; + return { key: scopeKey, cursor: null, slices: [first] }; } /** Only rendered hunks can anchor comments; list others off-diff. */ From 667f1f7d948c7f24fe525ce1e8f70e5c49095f8d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 20:59:55 +0000 Subject: [PATCH 04/13] fix(web): preserve review context through refreshed diffs --- .../web/src/components/ChatView.logic.test.ts | 41 +-- apps/web/src/components/ChatView.logic.ts | 9 - apps/web/src/components/ChatView.tsx | 4 - apps/web/src/components/DiffPanel.tsx | 19 +- .../diffs/reviewFileDiffRetention.test.ts | 109 ------- .../diffs/reviewFileDiffRetention.ts | 92 ------ .../components/diffs/useReviewFilePatches.ts | 305 +++++++++--------- .../pullRequest/PullRequestCodeTab.tsx | 172 ++++++---- .../pullRequest/pullRequestDiff.logic.test.ts | 4 +- .../pullRequest/pullRequestDiff.logic.ts | 16 +- apps/web/src/hooks/useProactivePanels.ts | 2 +- apps/web/src/lib/diffRendering.test.ts | 73 +++++ apps/web/src/lib/diffRendering.ts | 34 ++ 13 files changed, 401 insertions(+), 479 deletions(-) delete mode 100644 apps/web/src/components/diffs/reviewFileDiffRetention.test.ts delete mode 100644 apps/web/src/components/diffs/reviewFileDiffRetention.ts diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 8808f23c0d034..901a446461c4a 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -360,7 +360,7 @@ describe("diff selection across agent turns", () => { const otherThreadRef = scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-2")); const openThread = { threadKey: "env-1:thread-1", diffOpen: true }; - it("keeps Uncommitted when the same thread refreshes", () => { + it("keeps the active review when an agent updates or completes a turn", () => { expect( shouldResetDiffSelectionToChanges({ diffOpen: true, @@ -370,6 +370,9 @@ describe("diff selection across agent turns", () => { threadKey: "env-1:thread-1", }), ).toBe(false); + expect(shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff: true })).toBe( + false, + ); }); it("shows Changes when the diff opens or the thread changes", () => { @@ -400,18 +403,12 @@ describe("diff selection across agent turns", () => { threadKey: "env-1:thread-1", }), ).toBe(true); + expect( + shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff: false }), + ).toBe(true); }); it("leaves a timeline turn selection in place", () => { - expect( - shouldResetDiffSelectionToChanges({ - diffOpen: true, - activeThreadRef: threadRef, - explicitThreadRef: threadRef, - previous: { threadKey: "env-1:thread-1", diffOpen: false }, - threadKey: "env-1:thread-1", - }), - ).toBe(false); expect( shouldResetDiffSelectionToChanges({ diffOpen: true, @@ -422,30 +419,6 @@ describe("diff selection across agent turns", () => { }), ).toBe(false); }); - - it("does nothing while the diff is closed", () => { - expect( - shouldResetDiffSelectionToChanges({ - diffOpen: false, - activeThreadRef: threadRef, - explicitThreadRef: null, - previous: openThread, - threadKey: "env-1:thread-1", - }), - ).toBe(false); - }); - - it("shows Changes only when a completed turn newly opens the diff", () => { - expect( - shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff: false }), - ).toBe(true); - expect(shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff: true })).toBe( - false, - ); - expect( - shouldApplyProactiveChangesScope({ openedDiff: false, alreadyReviewingDiff: false }), - ).toBe(false); - }); }); describe("resolveThreadSwitchTimeline", () => { diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 0dd5bfda42da8..be2be9e93020b 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -207,14 +207,6 @@ export interface DiffPanelOpenContext { readonly diffOpen: boolean; } -/** - * Generic openings show Changes: the first time the diff is on screen, a tab - * fallback, or a switch to another thread. A timeline click names the thread it - * just selected and keeps that turn, matched by id because the shell hands out - * a new ref object whenever the thread updates. An agent turn replaces the - * thread shell without closing the diff, and that refresh must leave Uncommitted - * or the selected turn alone. - */ export function shouldResetDiffSelectionToChanges(input: { readonly diffOpen: boolean; readonly activeThreadRef: ScopedThreadRef | null; @@ -237,7 +229,6 @@ export function shouldResetDiffSelectionToChanges(input: { ); } -/** Opening the diff for a completed turn shows Changes. A review already on screen stays put. */ export function shouldApplyProactiveChangesScope(input: { readonly openedDiff: boolean; readonly alreadyReviewingDiff: boolean; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 931bcab87a3c4..df027d555132a 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -987,10 +987,6 @@ export default function ChatView(props: ChatViewProps) { explicitDiffOpenRef.current = null; const previous = diffPanelOpenContextRef.current; diffPanelOpenContextRef.current = { threadKey: activeThreadKey, diffOpen }; - // Generic openings show Changes: the first time the diff is on screen, a tab - // fallback, or a switch to another thread. A timeline click keeps its turn. - // An agent turn replaces the thread shell without closing the diff, and that - // refresh must leave Uncommitted or the selected turn alone. if ( activeThreadRef && shouldResetDiffSelectionToChanges({ diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index cc3fd451631c8..737c5eed78d25 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -96,7 +96,6 @@ import { vcsEnvironment } from "../state/vcs"; import { buildBaseRefChoices, filterBaseRefChoices } from "../lib/baseRefChoices"; import { createGitDiffFileContentsLoader } from "../lib/diffFileContents"; -import { reviewDiffViewerKey } from "./diffs/reviewFileDiffRetention"; import { useReviewFilePatches } from "./diffs/useReviewFilePatches"; import { DiffFileLoadingBoundary } from "./diffs/DiffFileLoadingBoundary"; import { DiffFileStatus } from "./diffs/DiffFileStatus"; @@ -373,13 +372,13 @@ export default function DiffPanel({ ? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}` : null; - const selectedGitSource = branchDiffPreview.data?.sources.find( + const preview = branchDiffPreview.isSuccess ? branchDiffPreview.data : null; + const selectedGitSource = preview?.sources.find( (source) => source.kind === (selectedGitScope === "unstaged" ? "working-tree" : "branch-range"), ); const refreshPreviewQuery = branchDiffPreview.refresh; const refreshDiffFromUserAction = refreshPreviewQuery; - const preview = branchDiffPreview.data; const gitDiff = selectedGitSource?.diff; const toolCallPreview = @@ -387,7 +386,9 @@ export default function DiffPanel({ const selectedPatch = toolCallSelection ? toolCallPreview?.patch : selectedTurn - ? activeCheckpointDiff.data?.diff + ? activeCheckpointDiff.error === null + ? activeCheckpointDiff.data?.diff + : undefined : gitDiff; const isSelectedPatchTruncated = toolCallPreview?.truncated === true || @@ -507,10 +508,11 @@ export default function DiffPanel({ settledFileCount, loadNextFiles, } = useReviewFilePatches({ + reviewKey: collapseScopeKey, environmentId: activeThread?.environmentId, cwd: branchDiffPreview.data?.cwd, - source: lazySource, - baseRef: lazySource?.baseRef ?? selectedBaseRef, + source: selectedGitSource ?? null, + baseRef: selectedGitSource?.baseRef ?? selectedBaseRef, ignoreWhitespace: diffIgnoreWhitespace, theme: resolvedTheme, previewInput: branchDiffPreviewInput, @@ -1156,10 +1158,7 @@ export default function DiffPanel({ { - it("keeps the same review across git snapshots", () => { - const family = reviewSnapshotFamily(identity); - expect(reviewSnapshotFamily(identity)).toBe(family); - expect(reviewSnapshotScope(family, "hash-a")).not.toBe(reviewSnapshotScope(family, "hash-b")); - expect(reviewDiffViewerKey("section", family)).toBe(`section:${family}`); - expect(reviewDiffViewerKey("section", null)).toBe("section:preview"); - }); - - it("keeps loaded file indexes when the snapshot family is unchanged", () => { - const family = reviewSnapshotFamily(identity); - expect(reviewPatchIndicesForFamily({ family, indices: [0, 1, 2, 3, 8] }, family)).toEqual([ - 0, 1, 2, 3, 8, - ]); - expect( - reviewPatchIndicesForFamily({ family, indices: [0, 1, 2, 3, 8] }, `${family}:other`), - ).toEqual([0, 1, 2, 3]); - }); -}); - -describe("retained review files", () => { - const loaded = file("src/app.ts", "hash:src/app.ts"); - const placeholder = file("src/app.ts", "next:src/app.ts:pending"); - - it("keeps the open file until the next patch arrives", () => { - expect( - retainedReviewFile({ - loaded: null, - previous: loaded, - placeholder, - sameFamily: true, - pending: true, - }), - ).toBe(loaded); - }); - - it("drops the open file once its replacement fails or no longer includes it", () => { - expect( - retainedReviewFile({ - loaded: null, - previous: loaded, - placeholder, - sameFamily: true, - pending: false, - }), - ).toBe(placeholder); - }); - - it("uses the newly loaded patch", () => { - const next = file("src/app.ts", "hash-b:src/app.ts"); - expect( - retainedReviewFile({ - loaded: next, - previous: loaded, - placeholder, - sameFamily: true, - pending: false, - }), - ).toBe(next); - }); - - it("does not carry a file into a different review", () => { - expect( - retainedReviewFile({ - loaded: null, - previous: loaded, - placeholder, - sameFamily: false, - pending: true, - }), - ).toBe(placeholder); - expect(isPendingReviewFile(placeholder)).toBe(true); - }); - - it("stores loaded files and ignores placeholders", () => { - const previous = new Map([["src/app.ts", loaded]]); - expect(retainLoadedReviewFiles(previous, [loaded, placeholder], (entry) => entry.name)).toBe( - previous, - ); - const next = file("src/app.ts", "hash-b:src/app.ts"); - const retained = retainLoadedReviewFiles(previous, [next], (entry) => entry.name); - expect(retained.get("src/app.ts")).toBe(next); - }); -}); diff --git a/apps/web/src/components/diffs/reviewFileDiffRetention.ts b/apps/web/src/components/diffs/reviewFileDiffRetention.ts deleted file mode 100644 index 28c9c1dd10de2..0000000000000 --- a/apps/web/src/components/diffs/reviewFileDiffRetention.ts +++ /dev/null @@ -1,92 +0,0 @@ -import type { FileDiffMetadata } from "@pierre/diffs"; - -const INITIAL_PATCH_INDICES = [0, 1, 2, 3] as const; -const EMPTY_RETAINED_FILES: ReadonlyMap = new Map(); - -export interface ReviewSnapshotIdentity { - readonly environmentId: string | undefined; - readonly cwd: string | undefined; - readonly kind: string | undefined; - readonly baseRef: string | null; - readonly ignoreWhitespace: boolean; -} - -/** Identity of a review that survives a new git snapshot. The diff hash is not part of it. */ -export function reviewSnapshotFamily(input: ReviewSnapshotIdentity): string { - return JSON.stringify([ - input.environmentId, - input.cwd, - input.kind, - input.baseRef, - input.ignoreWhitespace, - ]); -} - -export function reviewSnapshotScope(family: string, diffHash: string | undefined): string { - return JSON.stringify([family, diffHash ?? null]); -} - -/** - * Keep requesting the files already on screen when the snapshot changes. A different - * review starts over at the first page. - */ -export function reviewPatchIndicesForFamily( - requested: { readonly family: string; readonly indices: readonly number[] }, - family: string, -): readonly number[] { - return requested.family === family ? requested.indices : INITIAL_PATCH_INDICES; -} - -export function isPendingReviewFile(file: { readonly cacheKey?: string | undefined }): boolean { - return file.cacheKey?.endsWith(":pending") === true; -} - -/** - * Show the file already on screen while its replacement is still loading. - * A failure, or a snapshot that no longer contains the file, must not keep the old diff. - */ -export function retainedReviewFile(input: { - readonly loaded: T | null; - readonly previous: T | undefined; - readonly placeholder: T; - readonly sameFamily: boolean; - readonly pending: boolean; -}): T { - if (input.loaded) return input.loaded; - if (input.pending && input.sameFamily && input.previous && !isPendingReviewFile(input.previous)) { - return input.previous; - } - return input.placeholder; -} - -export function retainLoadedReviewFiles( - previous: ReadonlyMap, - files: readonly FileDiffMetadata[], - pathOf: (file: FileDiffMetadata) => string, -): ReadonlyMap { - const next = new Map(); - for (const file of files) { - if (!isPendingReviewFile(file)) next.set(pathOf(file), file); - } - if (next.size === previous.size) { - let unchanged = true; - for (const [path, file] of next) { - if (previous.get(path) !== file) { - unchanged = false; - break; - } - } - if (unchanged) return previous; - } - return next.size === 0 ? EMPTY_RETAINED_FILES : next; -} - -/** The viewer remounts for a new review section, not for a new git snapshot of the same one. */ -export function reviewDiffViewerKey( - sectionMountKey: string, - snapshotFamily: string | null, -): string { - return snapshotFamily === null - ? `${sectionMountKey}:preview` - : `${sectionMountKey}:${snapshotFamily}`; -} diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts index 286881e8ec421..196e14526bd73 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.ts +++ b/apps/web/src/components/diffs/useReviewFilePatches.ts @@ -7,18 +7,16 @@ import type { } from "@t3tools/contracts"; import * as AsyncResult from "effect/reactivity/AsyncResult"; import * as Atom from "effect/reactivity/Atom"; -import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "react"; +import { useContext, useEffect, useRef, useState } from "react"; import { useStableCallback } from "~/hooks/useStableCallback"; -import { getRenderablePatch, resolveFileDiffPath, type RenderablePatch } from "~/lib/diffRendering"; -import { reviewEnvironment } from "~/state/review"; import { - isPendingReviewFile, - retainLoadedReviewFiles, + getRenderablePatch, + resolveFileDiffPath, retainedReviewFile, - reviewPatchIndicesForFamily, - reviewSnapshotFamily, - reviewSnapshotScope, -} from "./reviewFileDiffRetention"; + retainLoadedReviewFiles, + type RenderablePatch, +} from "~/lib/diffRendering"; +import { reviewEnvironment } from "~/state/review"; const EMPTY_RETAINED_FILES: ReadonlyMap = new Map(); @@ -26,9 +24,10 @@ const filePathCollator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base", }); -const DEFAULT_INDICES = [0, 1, 2, 3]; +const PAGE_SIZE = 4; export function useReviewFilePatches({ + reviewKey, environmentId, cwd, source, @@ -38,6 +37,7 @@ export function useReviewFilePatches({ previewInput, preview, }: { + reviewKey: string | null; environmentId: EnvironmentId | undefined; cwd: string | undefined; source: ReviewDiffPreviewSource | null; @@ -49,165 +49,170 @@ export function useReviewFilePatches({ preview: RenderablePatch | null; }) { const registry = useContext(RegistryContext); - const snapshotFamily = reviewSnapshotFamily({ + const family = JSON.stringify([ + reviewKey, + environmentId, + cwd, + source?.id, + source?.kind, + source?.baseRef, + baseRef, + source?.headRef, + ignoreWhitespace, + ]); + const scope = JSON.stringify([family, source?.diffHash]); + const lazy = source?.truncated === true && source.files !== undefined; + const files = lazy ? sortedReviewFiles(source) : []; + const [requested, setRequested] = useState<{ family: string; paths: readonly string[] }>({ + family, + paths: [], + }); + const [retained, setRetained] = useState(() => ({ + family, + source, + byPath: + !lazy && preview?.kind === "files" + ? retainLoadedReviewFiles(EMPTY_RETAINED_FILES, preview.files) + : EMPTY_RETAINED_FILES, + })); + const previousByPath = retained.family === family ? retained.byPath : EMPTY_RETAINED_FILES; + const requestedPaths = new Set([ + ...files.slice(0, PAGE_SIZE).map((file) => file.path), + ...(requested.family === family ? requested.paths : []), + ...previousByPath.keys(), + ]); + const queries = reviewFileQueries({ environmentId, cwd, - kind: source?.kind, + source, baseRef, ignoreWhitespace, + previewInput, + scope, + files, + requestedPaths, }); - const scope = reviewSnapshotScope(snapshotFamily, source?.diffHash); - const [requested, setRequested] = useState({ family: snapshotFamily, indices: [0, 1, 2, 3] }); - const [retainedFiles, setRetainedFiles] = useState<{ - readonly family: string; - readonly byPath: ReadonlyMap; - }>({ family: snapshotFamily, byPath: EMPTY_RETAINED_FILES }); - const previousByPath = retainedFiles.family === snapshotFamily ? retainedFiles.byPath : EMPTY_RETAINED_FILES; - const files = sortedReviewFiles(source); - const queries = reviewFileQueries({ environmentId, cwd, source, baseRef, ignoreWhitespace, previewInput, scope, - requested: { scope, indices: [...reviewPatchIndicesForFamily(requested, snapshotFamily)] } }); const queriedPatchesAtom = reviewPatchesAtom( JSON.stringify([theme, queries.map(({ index, target }) => [index, target])]), ); const patches = useAtomValue(queriedPatchesAtom); const retry = useStableCallback((path: string) => { - const requestFiles = useCallback( - (nextIndices: number[]) => - setRequested((current) => { - const previous = reviewPatchIndicesForFamily(current, snapshotFamily); - const added = nextIndices.filter((index) => !previous.includes(index)); - if (added.length === 0 && current.family === snapshotFamily) return current; - return { family: snapshotFamily, indices: [...previous, ...added] }; - }), - [snapshotFamily], - ); - const requestFile = useCallback((index: number) => requestFiles([index]), [requestFiles]); - const renderableFiles = useMemo( - () => - source - ? files.map((file, index): FileDiffMetadata => { - const result = patches.get(index); - const loaded = - result?._tag === "Success" && result.value.patch?.kind === "files" - ? (result.value.patch.files.find( - (candidate) => resolveFileDiffPath(candidate) === file.path, - ) ?? null) - : null; - return retainedReviewFile({ - loaded, - previous: previousByPath.get(file.path), - sameFamily: true, - pending: result === undefined || result._tag === "Initial" || result.waiting, - placeholder: { - name: file.path, - ...(file.previousPath ? { prevName: file.previousPath } : {}), - type: file.previousPath ? "rename-changed" : "change", - hunks: [], - additionLines: [], - deletionLines: [], - additionCharacterCount: 0, - deletionCharacterCount: 0, - splitLineCount: 0, - unifiedLineCount: 0, - isPartial: true, - cacheKey: `${scope}:${file.path}:pending`, - }, - }); - }) - : (preview?.kind === "files" ? preview.files : []).toSorted((a, b) => - filePathCollator.compare(resolveFileDiffPath(a), resolveFileDiffPath(b)), - ), - [source, files, patches, scope, preview, previousByPath], - ); - const nextRetainedFiles = source - ? retainLoadedReviewFiles(previousByPath, renderableFiles, resolveFileDiffPath) - : EMPTY_RETAINED_FILES; - useEffect(() => { - if (!source) return; - if (retainedFiles.family === snapshotFamily && retainedFiles.byPath === nextRetainedFiles) - return; - setRetainedFiles({ family: snapshotFamily, byPath: nextRetainedFiles }); - }, [nextRetainedFiles, retainedFiles.byPath, retainedFiles.family, snapshotFamily, source]); - const pendingIndex = source - ? files.findIndex((file, index) => { - const patch = patches.get(index); - if (patch && patch._tag !== "Initial") return false; - const rendered = renderableFiles[index]; + const query = queries.find(({ index }) => files[index]?.path === path)?.query; + if (query) registry.refresh(query); + }); + const requestFiles = (indices: number[]) => + setRequested((current) => { + const paths = new Set(current.family === family ? current.paths : []); + for (const index of indices) { + const file = files[index]; + if (file) paths.add(file.path); + } + return current.family === family && paths.size === current.paths.length + ? current + : { family, paths: [...paths] }; + }); + const renderableFiles = lazy + ? files.map((file, index): FileDiffMetadata => { + const result = patches.get(index); + const loaded = + result?._tag === "Success" && result.value.patch?.kind === "files" + ? (result.value.patch.files.find( + (candidate) => resolveFileDiffPath(candidate) === file.path, + ) ?? null) + : null; + return retainedReviewFile({ + loaded, + previous: previousByPath.get(file.path), + sameFamily: true, + pending: + result === undefined || + result._tag === "Initial" || + (result._tag === "Success" && result.waiting), + placeholder: { + name: file.path, + ...(file.previousPath ? { prevName: file.previousPath } : {}), + type: file.previousPath ? "rename-changed" : "change", + hunks: [], + additionLines: [], + deletionLines: [], + additionCharacterCount: 0, + deletionCharacterCount: 0, + splitLineCount: 0, + unifiedLineCount: 0, + isPartial: true, + cacheKey: `${scope}:${file.path}:pending`, + }, + }); + }) + : (preview?.kind === "files" ? preview.files : []).toSorted((a, b) => + filePathCollator.compare(resolveFileDiffPath(a), resolveFileDiffPath(b)), + ); + const nextRetained = !source + ? EMPTY_RETAINED_FILES + : !lazy && retained.family === family && retained.source === source + ? previousByPath + : retainLoadedReviewFiles(previousByPath, renderableFiles); + if ( + retained.family !== family || + retained.source !== source || + retained.byPath !== nextRetained + ) { + setRetained({ family, source, byPath: nextRetained }); + } + const readyFilePaths = new Set( + files + .filter((_, index) => { + const result = patches.get(index); return ( - rendered === undefined || - isPendingReviewFile(rendered) || - resolveFileDiffPath(rendered) !== file.path + (result !== undefined && result._tag !== "Initial") || + !renderableFiles[index]?.cacheKey?.endsWith(":pending") ); }) - : -1; - const settledFileCount = source + .map((file) => file.path), + ); + const pendingIndex = files.findIndex((file) => !readyFilePaths.has(file.path)); + const settledFileCount = lazy ? pendingIndex < 0 ? files.length : pendingIndex - : preview?.kind === "files" - ? preview.files.length - : 0; - const loadNextFiles = useCallback( - () => { - if (queries.some(({ index }) => index === settledFileCount)) return; - requestFiles(Array.from({ length: 4 }, (_, index) => settledFileCount + index)); - }, - [queries, requestFiles, settledFileCount], - ); - const fileStates = useMemo( - () => - new Map( - files.map((file, index) => { - const patch = patches.get(index); - return [ - file.path, - { - error: - patch?._tag === "Failure" || - (patch?._tag === "Success" && - (patch.value.patch?.kind !== "files" || - !patch.value.patch.files.some( - (candidate) => resolveFileDiffPath(candidate) === file.path, - ))), - truncated: patch?._tag === "Success" && patch.value.source.truncated, - }, - ] as const; - }), - ), - [files, patches], - ); - const readyFilePaths = useMemo( - () => - new Set( - files - .filter((file, index) => { - const patch = patches.get(index); - if (patch && patch._tag !== "Initial") return true; - const rendered = renderableFiles[index]; - return ( - rendered !== undefined && - !isPendingReviewFile(rendered) && - resolveFileDiffPath(rendered) === file.path - ); - }) - .map((file) => file.path), - ), - [files, patches, renderableFiles], + : renderableFiles.length; + const fileStates = new Map( + files.map((file, index) => { + const patch = patches.get(index); + return [ + file.path, + { + error: + patch?._tag === "Failure" || + (patch?._tag === "Success" && + !patch.waiting && + (patch.value.patch?.kind !== "files" || + !patch.value.patch.files.some( + (candidate) => resolveFileDiffPath(candidate) === file.path, + ))), + truncated: patch?._tag === "Success" && patch.value.source.truncated, + }, + ] as const; + }), ); return { scope, - family: snapshotFamily, + family, fileStates, isPending: [...patches.values()].some((patch) => patch._tag === "Initial" || patch.waiting), retry, - requestFile, + requestFile: (index: number) => requestFiles([index]), readyFilePaths, renderableFiles, settledFileCount, - loadNextFiles, + loadNextFiles: () => { + if (queries.some(({ index }) => index === settledFileCount)) return; + requestFiles(Array.from({ length: PAGE_SIZE }, (_, index) => settledFileCount + index)); + }, }; } +// The refresh effect keys on the identity of these queries. function reviewFileQueries({ environmentId, cwd, @@ -216,7 +221,8 @@ function reviewFileQueries({ ignoreWhitespace, previewInput, scope, - requested, + files, + requestedPaths, }: { environmentId: EnvironmentId | undefined; cwd: string | undefined; @@ -225,17 +231,16 @@ function reviewFileQueries({ ignoreWhitespace: boolean; previewInput: ReviewDiffPreviewInput | undefined; scope: string; - requested: { scope: string; indices: number[] }; + files: ReturnType; + requestedPaths: ReadonlySet; }) { - const indices = requested.scope === scope ? requested.indices : DEFAULT_INDICES; - const files = sortedReviewFiles(source); const queries = !environmentId || !cwd || !source ? [] - : indices - .filter((index) => index < files.length) - .map((index) => { - const file = files[index]!; + : files + .map((file, index) => ({ file, index })) + .filter(({ file }) => requestedPaths.has(file.path)) + .map(({ file, index }) => { const target: ReviewPatchTarget = { environmentId, input: { @@ -264,6 +269,7 @@ function sortedReviewFiles(source: ReviewDiffPreviewSource | null) { type ReviewPatchTarget = Parameters[0]; +// Derived atoms parse each query result once, even when another file finishes loading. const parsedPatchAtom = Atom.family((key: string) => { const [theme, target] = JSON.parse(key) as ["light" | "dark", ReviewPatchTarget]; return Atom.map(reviewEnvironment.diffFilePatch(target), (result) => @@ -283,6 +289,7 @@ const parsedPatchAtom = Atom.family((key: string) => { ); }); +// Keyed by theme and query targets so every render with the same files reads the same atom. const reviewPatchesAtom = Atom.family((key: string) => { const [theme, entries] = JSON.parse(key) as [ "light" | "dark", diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index cc2e8c9f9a80f..c311183451c04 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -2,8 +2,8 @@ import { capitalizeTerm, sourceControlClients, } from "@t3tools/client-runtime/source-control-clients"; -import type { CodeViewItem, DiffLineAnnotation, SelectedLineRange } from "@pierre/diffs"; -import type { CodeViewAnnotationArgs, CodeViewDiffItem } from "@pierre/diffs/react"; +import type { CodeViewItem, DiffLineAnnotation, FileDiffMetadata, SelectedLineRange } from "@pierre/diffs"; +import type { CodeViewAnnotationArgs, CodeViewDiffItem, CodeViewHandle } from "@pierre/diffs/react"; import type { EnvironmentId, PullRequestDetailView, @@ -29,7 +29,7 @@ import { TriangleAlertIcon, } from "lucide-react"; import { ChevronDown, ChevronRight, ChevronsDownUp, ChevronsUpDown } from "lucide"; -import { useAtomRefresh } from "@effect/atom-react"; +import { RegistryContext } from "@effect/atom-react"; import * as Schema from "effect/Schema"; import { createContext, @@ -53,7 +53,8 @@ import { } from "@t3tools/client-runtime/pull-request-editing"; import { orderDiffFiles } from "./pullRequestFileOrder.logic"; import { - buildFileDiffRenderKey, + buildFileDiffIdentityKey, + buildFileDiffContentVersion, fnv1a32, getDiffLineStat, getRenderablePatch, @@ -157,6 +158,7 @@ const NO_SLICES: ReadonlyArray = []; // Query results outlive the panel's slices, so their parses survive closing and reopening // without retaining anything beyond the query cache's lifetime. const sliceParses = new WeakMap>(); +const fileContentVersions = new WeakMap(); function parseSlice(slice: DiffSlice, cacheKey: string, ignoreWhitespace: boolean) { let parses = sliceParses.get(slice.source); @@ -320,7 +322,11 @@ function PullRequestCodeTab({ readonly key: string; readonly cursor: string | null; readonly slices: ReadonlyArray; - }>({ key: "", cursor: null, slices: NO_SLICES }); + readonly refreshing: ReadonlyArray | null; + }>({ key: "", cursor: null, slices: NO_SLICES, refreshing: null }); + const [viewer, setViewer] = useState | null>( + null, + ); const referenceKey = pullRequestReviewKey(reference); // A commit opened from the timeline or left selected across a force-push may no longer be @@ -342,11 +348,12 @@ function PullRequestCodeTab({ setFoldOverride(null); setVisibleCommitCount(COMMIT_PAGE_SIZE); setOrphansOpen(false); - setSliceState({ key: scopeKey, cursor: null, slices: NO_SLICES }); + setSliceState({ key: scopeKey, cursor: null, slices: NO_SLICES, refreshing: null }); } const loadedSlices = sliceState.key === scopeKey ? sliceState.slices : NO_SLICES; const cursor = sliceState.key === scopeKey ? sliceState.cursor : null; + const registry = use(RegistryContext); const diffQuery = useEnvironmentQuery( pullRequestEnvironment.diff({ environmentId, @@ -362,7 +369,7 @@ function PullRequestCodeTab({ readonly cursor: string | null; readonly scopeKey: string; } | null>(null); - const data = diffQuery.data; + const data = diffQuery.isSuccess && !diffQuery.isPending ? diffQuery.data : null; if ( data !== null && (appliedPage?.data !== data || @@ -380,9 +387,19 @@ function PullRequestCodeTab({ nextCursor: data.nextCursor, omittedFileStats: data.omittedFileStats ?? [], }; + if (previous.key === scopeKey && previous.refreshing !== null) { + const index = previous.refreshing.findIndex((slice) => slice.cursor === cursor); + const refreshing = [ + ...(index < 0 ? previous.refreshing : previous.refreshing.slice(0, index)), + next, + ]; + return refreshing.length >= previous.slices.length || next.nextCursor === null + ? { key: scopeKey, cursor, slices: refreshing, refreshing: null } + : { ...previous, refreshing }; + } const index = slices.findIndex((slice) => slice.cursor === cursor); if (index === -1) { - return { key: scopeKey, cursor, slices: [...slices, next] }; + return { key: scopeKey, cursor, slices: [...slices, next], refreshing: null }; } const existing = slices[index]; if ( @@ -404,15 +421,22 @@ function PullRequestCodeTab({ return previous; } // Drop later slices when replacing a changed page; their cursors target the old diff. - return { key: scopeKey, cursor, slices: [...slices.slice(0, index), next] }; + return { key: scopeKey, cursor, slices: [...slices.slice(0, index), next], refreshing: null }; }); } - const refreshFirstDiffPage = useAtomRefresh( - pullRequestEnvironment.diff({ - environmentId, - input: { ...reference, ...(commit === null ? {} : { commit }) }, - }), - ); + const failedSliceIndex = loadedSlices.findIndex((slice) => slice.cursor === cursor); + if ( + diffQuery.error !== null && + !diffQuery.isPending && + (sliceState.refreshing !== null || failedSliceIndex !== -1) + ) { + setAppliedPage(null); + setSliceState((previous) => ({ + ...previous, + slices: previous.refreshing ?? previous.slices.slice(0, failedSliceIndex), + refreshing: null, + })); + } const reviewKey = referenceKey; const pendingComments = usePendingReviewComments(reference); const addComment = usePullRequestReviewStore((store) => store.addComment); @@ -444,44 +468,67 @@ function PullRequestCodeTab({ isViewed: isFileViewed, isStale: isFileViewedStale, } = filesViewed; - // Reread from the first page. It stays on screen so the reader does not jump; later pages - // belong to the snapshot being replaced, so they are fetched again. Each of those pages is - // its own cached query, so the walk asks for it again instead of replaying the old snapshot. - const refreshGeneration = useRef(0); - const refreshedCursor = useRef(undefined); - const appliedRefreshToken = useRef(refreshToken); - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a new refresh token clears loaded slices and refetches the first diff page and viewed files + const appliedRefresh = useRef({ refreshToken, backgroundRefreshToken }); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- refreshes mounted review pages from the environment query cache useEffect(() => { - if (appliedRefreshToken.current === refreshToken) return; - appliedRefreshToken.current = refreshToken; - refreshGeneration.current += 1; - refreshedCursor.current = undefined; - setSliceState((previous) => retainReviewDiffSlices(previous, scopeKey)); - // An equal refetch keeps the answer's identity, so forget the applied page to reapply it. + const previous = appliedRefresh.current; + appliedRefresh.current = { refreshToken, backgroundRefreshToken }; + if ( + previous.refreshToken === refreshToken && + previous.backgroundRefreshToken === backgroundRefreshToken + ) + return; + registry.refresh( + pullRequestEnvironment.diff({ + environmentId, + input: { ...reference, ...(commit === null ? {} : { commit }) }, + }), + ); setAppliedPage(null); - refreshFirstDiffPage(); - refreshFilesViewed(); - }, [refreshToken, scopeKey, refreshFirstDiffPage, refreshFilesViewed]); - const appliedBackgroundRefreshToken = useRef(backgroundRefreshToken); - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a background refresh token refetches the first diff page and viewed files while keeping slices - useEffect(() => { - if (appliedBackgroundRefreshToken.current === backgroundRefreshToken) return; - appliedBackgroundRefreshToken.current = backgroundRefreshToken; - refreshGeneration.current += 1; - refreshedCursor.current = undefined; - // Keeping slices mounted preserves scroll; the page merge invalidates changed pages. - setSliceState((previous) => ({ ...previous, cursor: null })); - refreshFirstDiffPage(); + // Stage a fresh walk while all displayed pages keep the reader's scroll geometry. + setSliceState((current) => ({ + ...retainReviewDiffSlices(current, scopeKey), + refreshing: [], + })); refreshFilesViewed(); - }, [backgroundRefreshToken, refreshFirstDiffPage, refreshFilesViewed]); + }, [ + refreshToken, + backgroundRefreshToken, + environmentId, + reference, + commit, + scopeKey, + registry, + refreshFilesViewed, + ]); useEffect(() => { - if (refreshGeneration.current === 0 || cursor === null) return; - if (refreshedCursor.current === cursor) return; - refreshedCursor.current = cursor; - diffQuery.refresh(); - }, [cursor, diffQuery.refresh]); - const nextCursor = loadedSlices.at(-1)?.nextCursor ?? null; - + const last = sliceState.refreshing?.at(-1); + if ( + !last || + last.cursor !== cursor || + last.nextCursor === null || + diffQuery.isPending || + diffQuery.error !== null + ) + return; + registry.refresh( + pullRequestEnvironment.diff({ + environmentId, + input: { ...reference, cursor: last.nextCursor, ...(commit === null ? {} : { commit }) }, + }), + ); + // oxlint-disable-next-line react/set-state-in-effect -- Advance only after invalidating the cached next page. + setSliceState((current) => ({ ...current, cursor: last.nextCursor })); + }, [ + sliceState.refreshing, + cursor, + diffQuery.isPending, + diffQuery.error, + registry, + environmentId, + reference, + commit, + ]); const codeViewReveal = useCodeViewFileReveal(); const answeredRevealNonce = useRef(null); @@ -533,7 +580,7 @@ function PullRequestCodeTab({ ? parsed.files.map( (file, index) => [ - buildFileDiffRenderKey(file), + buildFileDiffIdentityKey(file), getDiffLineStat([parsed.sourceFiles[index] ?? file]), ] as const, ) @@ -562,7 +609,7 @@ function PullRequestCodeTab({ })(); const annotatedFiles = files.map((fileDiff) => { - const fileKey = buildFileDiffRenderKey(fileDiff); + const fileKey = buildFileDiffIdentityKey(fileDiff); const path = resolveFileDiffPath(fileDiff); const groups = new Map(); const groupAt = (side: PullRequestDiffSide, line: number) => { @@ -671,6 +718,11 @@ function PullRequestCodeTab({ const items: CodeViewDiffItem[] = annotatedFiles.map( ({ fileKey, path, fileDiff, annotations, annotationsVersion }) => { + let contentVersion = fileContentVersions.get(fileDiff); + if (contentVersion === undefined) { + contentVersion = buildFileDiffContentVersion(fileDiff); + fileContentVersions.set(fileDiff, contentVersion); + } const collapsed = isFileDiffCollapsed(fileKey, effectiveFoldOverride, toggledFiles); const viewedMark = filesViewedEnabled ? `e${isFileViewed(path) ? "v" : ""}${isFileViewedStale(path) ? "s" : ""}` @@ -681,7 +733,9 @@ function PullRequestCodeTab({ fileDiff, annotations, collapsed, - version: fnv1a32(`${collapsed ? "1" : "0"}:${viewedMark}:${annotationsVersion}`), + version: fnv1a32( + `${contentVersion}:${collapsed ? "1" : "0"}:${viewedMark}:${annotationsVersion}`, + ), }; }, ); @@ -693,12 +747,19 @@ function PullRequestCodeTab({ // Do not re-arm the visible sentinel after failure; it would retry indefinitely. const canLoadNextSlice = + sliceState.refreshing === null && nextCursor !== null && nextCursor !== cursor && !diffQuery.isPending && diffQuery.error === null; const loadNextSlice = () => { - if (nextCursor === null) return; + if (nextCursor === null || sliceState.refreshing !== null) return; + registry.refresh( + pullRequestEnvironment.diff({ + environmentId, + input: { ...reference, cursor: nextCursor, ...(commit === null ? {} : { commit }) }, + }), + ); setSliceState((previous) => ({ ...previous, cursor: nextCursor })); }; const sentinelRef = (node: HTMLDivElement | null) => { @@ -772,7 +833,7 @@ function PullRequestCodeTab({ if (!range || !canSelectLines) return; const item = context.item; if (item.type !== "diff") return; - const file = files.find((candidate) => buildFileDiffRenderKey(candidate) === item.id); + const file = files.find((candidate) => buildFileDiffIdentityKey(candidate) === item.id); if (!file) return; // On a host without ranged comments a range collapses to its last line: one that silently // lost its first line there would be worse than one line. @@ -799,7 +860,7 @@ function PullRequestCodeTab({ text: string, onFinish: (comment: ReviewCommentContext) => void, ) => { - const file = files.find((candidate) => buildFileDiffRenderKey(candidate) === anchor.fileKey); + const file = files.find((candidate) => buildFileDiffIdentityKey(candidate) === anchor.fileKey); const comment = file === undefined ? null @@ -1473,6 +1534,7 @@ function PullRequestCodeTab({ {/* The viewer must own its scroll element to position virtual rows within it. */} + key={scopeKey} className="@container h-full overflow-auto [scrollbar-gutter:stable]" containerRef={scrollerRef} viewerRef={codeViewReveal.attach} diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts index 1a1d76fa3e00c..014bd8ae0d940 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts @@ -24,11 +24,11 @@ describe("retainReviewDiffSlices", () => { const first = { cursor: null, patch: "page-1" }; const slices = [first, { cursor: "page-2", patch: "page-2" }]; - it("keeps the first page and drops later ones when the same review refreshes", () => { + it("keeps every displayed page while the same review refreshes", () => { expect(retainReviewDiffSlices({ key: "pr-1", cursor: "page-2", slices }, "pr-1")).toEqual({ key: "pr-1", cursor: null, - slices: [first], + slices, }); const open = { key: "pr-1", cursor: null, slices: [first] }; expect(retainReviewDiffSlices(open, "pr-1")).toBe(open); diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts index 671ddeb080602..a87f51974dde0 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts @@ -7,24 +7,12 @@ export interface ReviewDiffSliceState { readonly slices: ReadonlyArray; } -/** - * A refresh rereads from the first page. That page stays on screen so the reader does not - * jump. Later pages are positions in the snapshot being replaced: keeping them when the - * first page comes back unchanged would leave those files stale for good. - */ export function retainReviewDiffSlices( previous: ReviewDiffSliceState, scopeKey: string, ): ReviewDiffSliceState { - if (previous.key !== scopeKey) { - return { key: scopeKey, cursor: null, slices: [] }; - } - const first = previous.slices[0]; - if (first === undefined) { - return previous.cursor === null ? previous : { key: scopeKey, cursor: null, slices: [] }; - } - if (previous.slices.length === 1 && previous.cursor === null) return previous; - return { key: scopeKey, cursor: null, slices: [first] }; + if (previous.key !== scopeKey) return { key: scopeKey, cursor: null, slices: [] }; + return previous.cursor === null ? previous : { ...previous, cursor: null }; } /** Only rendered hunks can anchor comments; list others off-diff. */ diff --git a/apps/web/src/hooks/useProactivePanels.ts b/apps/web/src/hooks/useProactivePanels.ts index 1fe137872ba08..d2b68f101f5f3 100644 --- a/apps/web/src/hooks/useProactivePanels.ts +++ b/apps/web/src/hooks/useProactivePanels.ts @@ -1,4 +1,3 @@ -import { shouldApplyProactiveChangesScope } from "../components/ChatView.logic"; import type { RunId, ScopedThreadRef, ThreadLinkedPullRequest } from "@t3tools/contracts"; import { scopedThreadKey } from "@t3tools/client-runtime/environment"; import { useEffect, useRef } from "react"; @@ -11,6 +10,7 @@ import { import type { TurnDiffSummary } from "../types"; import { observeProactivePanelUserChoice, + shouldApplyProactiveChangesScope, resolveProactiveTurnDiffAction, shouldOpenProactivePullRequest, shouldOpenProactiveTurnDiff, diff --git a/apps/web/src/lib/diffRendering.test.ts b/apps/web/src/lib/diffRendering.test.ts index 7634177ec7462..d28c619b956f4 100644 --- a/apps/web/src/lib/diffRendering.test.ts +++ b/apps/web/src/lib/diffRendering.test.ts @@ -7,6 +7,8 @@ import { buildFileDiffRenderKey, getDiffLineStat, getRenderablePatch, + retainLoadedReviewFiles, + retainedReviewFile, resolveFileDiffPath, resolveFileDiffPreviousPath, } from "./diffRendering"; @@ -222,6 +224,77 @@ describe("getRenderablePatch", () => { }); describe("diff file reconciliation", () => { + const reviewFile = (line: string) => { + const patch = getRenderablePatch( + [ + "diff --git a/app.ts b/app.ts", + "--- a/app.ts", + "+++ b/app.ts", + "@@ -1 +1 @@", + "-before", + `+${line}`, + ].join("\n"), + "review", + ); + if (patch?.kind !== "files" || !patch.files[0]) throw new Error("Expected a file patch"); + return patch.files[0]; + }; + const loaded = reviewFile("after"); + const placeholder = { + ...loaded, + cacheKey: "refresh:app.ts:pending", + hunks: [], + additionLines: [], + deletionLines: [], + splitLineCount: 0, + unifiedLineCount: 0, + }; + + it("keeps loaded hunks during a same-review refresh until fresh content arrives", () => { + const previous = retainLoadedReviewFiles(new Map(), [loaded]); + const pending = retainedReviewFile({ + loaded: null, + previous: previous.get("app.ts"), + placeholder, + sameFamily: true, + pending: true, + }); + expect(pending).toBe(loaded); + expect(getDiffLineStat([pending])).toEqual({ additions: 1, deletions: 1 }); + + const refreshed = retainedReviewFile({ + loaded: reviewFile("newer"), + previous: pending, + placeholder, + sameFamily: true, + pending: false, + }); + expect(refreshed.additionLines.join("")).toBe("newer"); + expect(buildFileDiffContentVersion(refreshed)).not.toBe(buildFileDiffContentVersion(pending)); + }); + + it("drops old hunks after a failed refresh or a switch to another review", () => { + const failed = retainedReviewFile({ + loaded: null, + previous: loaded, + placeholder, + sameFamily: true, + pending: false, + }); + expect(failed.hunks).toEqual([]); + const retained = retainLoadedReviewFiles(new Map([["app.ts", loaded]]), [failed]); + expect(retained.has("app.ts")).toBe(false); + + const switched = retainedReviewFile({ + loaded: null, + previous: loaded, + placeholder, + sameFamily: false, + pending: true, + }); + expect(switched.hunks).toEqual([]); + }); + it("keeps Pierre's render key stable when a partial diff hydrates", () => { const patch = [ "diff --git a/example.ts b/example.ts", diff --git a/apps/web/src/lib/diffRendering.ts b/apps/web/src/lib/diffRendering.ts index aac9b1f3e6366..89349a96819c8 100644 --- a/apps/web/src/lib/diffRendering.ts +++ b/apps/web/src/lib/diffRendering.ts @@ -217,6 +217,40 @@ export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string { return cacheKey.endsWith(":hydrated") ? cacheKey.slice(0, -":hydrated".length) : cacheKey; } +export function retainedReviewFile(input: { + readonly loaded: FileDiffMetadata | null; + readonly previous: FileDiffMetadata | undefined; + readonly placeholder: FileDiffMetadata; + readonly sameFamily: boolean; + readonly pending: boolean; +}): FileDiffMetadata { + if (input.loaded) return input.loaded; + if ( + input.pending && + input.sameFamily && + input.previous && + !input.previous.cacheKey?.endsWith(":pending") + ) { + return input.previous; + } + return input.placeholder; +} + +export function retainLoadedReviewFiles( + previous: ReadonlyMap, + files: readonly FileDiffMetadata[], +): ReadonlyMap { + const next = new Map( + files + .filter((file) => !file.cacheKey?.endsWith(":pending")) + .map((file) => [resolveFileDiffPath(file), file]), + ); + return next.size === previous.size && + [...next].every(([path, file]) => previous.get(path) === file) + ? previous + : next; +} + function hashFileDiffPart(hash: number, value: string | number | boolean | undefined): number { const serialized = value === undefined ? "undefined" : String(value); const withLength = fnv1a32(`${typeof value}:${serialized.length}:`, hash); From 5fb7fc73ae610aba9a7a0344cfbfddc99ee8089d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 21:01:16 +0000 Subject: [PATCH 05/13] fix(web): retain requested review paths after a failed refresh --- apps/web/src/components/diffs/useReviewFilePatches.ts | 3 +++ apps/web/src/components/pullRequest/PullRequestCodeTab.tsx | 5 ----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts index 196e14526bd73..e926e8bfacd22 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.ts +++ b/apps/web/src/components/diffs/useReviewFilePatches.ts @@ -81,6 +81,9 @@ export function useReviewFilePatches({ ...(requested.family === family ? requested.paths : []), ...previousByPath.keys(), ]); + if (requested.family !== family || requested.paths.length !== requestedPaths.size) { + setRequested({ family, paths: [...requestedPaths] }); + } const queries = reviewFileQueries({ environmentId, cwd, diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index c311183451c04..d70a342b0acb8 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -560,11 +560,6 @@ function PullRequestCodeTab({ ignoreWhitespace, ), ); - // What a slice withheld: the host declining to inline part of it, or a patch the viewer could - // not structure and so dropped. Neither says anything about there being more to fetch. - const withheldContent = - loadedSlices.some((slice) => slice.truncated) || - parsedSlices.some((parsed) => parsed?.kind === "raw"); const files = parsedSlices.flatMap((parsed) => parsed?.kind === "files" ? orderDiffFiles(parsed.files) : [], ); From 897b729684d5d6277cdcbd99561c6bb7306d51f7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 21:24:25 +0000 Subject: [PATCH 06/13] fix(web): discard review drafts when their file changes --- .../pullRequest/PullRequestCodeTab.tsx | 65 +++++++++++++++++-- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index d70a342b0acb8..dc36fb69a105e 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -43,6 +43,7 @@ import { import { observeIntersection } from "~/lib/observeIntersection"; import { useLocalStorage } from "~/hooks/useLocalStorage"; +import { useStableCallback } from "~/hooks/useStableCallback"; import { useClientSettings, updateClientSettings } from "~/hooks/useSettings"; import { useRenderedAppearance } from "~/hooks/useTheme"; import { areAllDiffFilesCollapsed } from "~/lib/diffCollapse"; @@ -160,6 +161,15 @@ const NO_SLICES: ReadonlyArray = []; const sliceParses = new WeakMap>(); const fileContentVersions = new WeakMap(); +function fileContentVersion(file: FileDiffMetadata) { + let version = fileContentVersions.get(file); + if (version === undefined) { + version = buildFileDiffContentVersion(file); + fileContentVersions.set(file, version); + } + return version; +} + function parseSlice(slice: DiffSlice, cacheKey: string, ignoreWhitespace: boolean) { let parses = sliceParses.get(slice.source); if (parses === undefined) { @@ -244,6 +254,8 @@ interface MutableAnnotationGroup { interface DraftAnchor { readonly fileKey: string; + readonly contentVersion: number; + readonly scopeKey: string; readonly path: string; readonly oldPath: string | null; readonly position: PullRequestReviewPosition; @@ -313,6 +325,8 @@ function PullRequestCodeTab({ const [selectedLines, setSelectedLines] = useState<{ id: string; range: SelectedLineRange; + contentVersion: number; + scopeKey: string; } | null>(null); const [draft, setDraft] = useState(null); const [threadPending, setThreadPending] = useState(false); @@ -563,6 +577,23 @@ function PullRequestCodeTab({ const files = parsedSlices.flatMap((parsed) => parsed?.kind === "files" ? orderDiffFiles(parsed.files) : [], ); + const contentVersions = new Map( + files.map((file) => [buildFileDiffIdentityKey(file), fileContentVersion(file)]), + ); + const isCurrentFile = (fileKey: string, version: number, anchorScopeKey: string) => + anchorScopeKey === scopeKey && + contentVersions.get(fileKey) === version && + !isFileDiffCollapsed(fileKey, effectiveFoldOverride, toggledFiles); + // Stable viewer identities preserve scroll, but line coordinates belong to one file version. + if (draft !== null && !isCurrentFile(draft.fileKey, draft.contentVersion, draft.scopeKey)) { + setDraft(null); + } + if ( + selectedLines !== null && + !isCurrentFile(selectedLines.id, selectedLines.contentVersion, selectedLines.scopeKey) + ) { + setSelectedLines(null); + } const filePaths = files.map((file) => resolveFileDiffPath(file)); const omittedFileStats = new Map( loadedSlices.flatMap((slice) => @@ -713,11 +744,7 @@ function PullRequestCodeTab({ const items: CodeViewDiffItem[] = annotatedFiles.map( ({ fileKey, path, fileDiff, annotations, annotationsVersion }) => { - let contentVersion = fileContentVersions.get(fileDiff); - if (contentVersion === undefined) { - contentVersion = buildFileDiffContentVersion(fileDiff); - fileContentVersions.set(fileDiff, contentVersion); - } + const contentVersion = fileContentVersion(fileDiff); const collapsed = isFileDiffCollapsed(fileKey, effectiveFoldOverride, toggledFiles); const viewedMark = filesViewedEnabled ? `e${isFileViewed(path) ? "v" : ""}${isFileViewedStale(path) ? "s" : ""}` @@ -829,7 +856,12 @@ function PullRequestCodeTab({ const item = context.item; if (item.type !== "diff") return; const file = files.find((candidate) => buildFileDiffIdentityKey(candidate) === item.id); - if (!file) return; + if ( + !file || + item.collapsed === true || + !isCurrentFile(item.id, fileContentVersion(item.fileDiff), scopeKey) + ) + return; // On a host without ranged comments a range collapses to its last line: one that silently // lost its first line there would be worse than one line. const path = resolveFileDiffPath(file); @@ -843,6 +875,8 @@ function PullRequestCodeTab({ if (anchor === null) return; setDraft({ fileKey: item.id, + contentVersion: fileContentVersion(file), + scopeKey, path, oldPath: previousPath === path ? null : previousPath, ...anchor, @@ -850,11 +884,19 @@ function PullRequestCodeTab({ }); }; + // Portals can retain a submit handler from before a refresh invalidated its draft. + const isCurrentDraft = useStableCallback( + (anchor: DraftAnchor) => + draft === anchor && + canCommentOnLines && + isCurrentFile(anchor.fileKey, anchor.contentVersion, anchor.scopeKey), + ); const finishSelection = ( anchor: DraftAnchor, text: string, onFinish: (comment: ReviewCommentContext) => void, ) => { + if (!isCurrentDraft(anchor)) return; const file = files.find((candidate) => buildFileDiffIdentityKey(candidate) === anchor.fileKey); const comment = file === undefined @@ -1063,6 +1105,7 @@ function PullRequestCodeTab({ setSelectedLines(null); }} onComment={(body) => { + if (!isCurrentDraft(draft)) return; if (!canCommentOnLines) { if (onAddToAgentSelection) { finishSelection(draft, body, (comment) => @@ -1535,7 +1578,15 @@ function PullRequestCodeTab({ viewerRef={codeViewReveal.attach} items={items} selectedLines={selectedLines} - onSelectedLinesChange={setSelectedLines} + onSelectedLinesChange={(selection) => { + const contentVersion = + selection === null ? undefined : contentVersions.get(selection.id); + setSelectedLines( + selection !== null && contentVersion !== undefined + ? { ...selection, contentVersion, scopeKey } + : null, + ); + }} options={diffViewOptions} renderCodeViewFooter={renderCodeViewFooter} renderHeaderPrefix={renderHeaderPrefix} From 18d839ea29724f11bf2b4112ff1d8389a397ec74 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Sat, 24 Oct 2026 14:17:21 -0400 Subject: [PATCH 07/13] fix(web): drop the unused file patch scope binding The diff viewer now keys its mount on the patch family, so the scope destructured from useReviewFilePatches was left unused after the rebase. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/components/DiffPanel.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index 737c5eed78d25..b72c8f31da0e0 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -497,7 +497,6 @@ export default function DiffPanel({ ...matchingBaseRefChoices.map(valueForBaseRefChoice), ]; const { - scope: filePatchScope, family: filePatchFamily, isPending: areFilePatchesPending, fileStates, From 6a31f36b10ffa6945a68279d27e0a23d94f7e3b0 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Mon, 26 Oct 2026 09:22:42 -0400 Subject: [PATCH 08/13] fix(review): allow agent selection without review posting access --- .../src/components/pullRequest/PullRequestCodeTab.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index dc36fb69a105e..22b50a73b6546 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -2,7 +2,12 @@ import { capitalizeTerm, sourceControlClients, } from "@t3tools/client-runtime/source-control-clients"; -import type { CodeViewItem, DiffLineAnnotation, FileDiffMetadata, SelectedLineRange } from "@pierre/diffs"; +import type { + CodeViewItem, + DiffLineAnnotation, + FileDiffMetadata, + SelectedLineRange, +} from "@pierre/diffs"; import type { CodeViewAnnotationArgs, CodeViewDiffItem, CodeViewHandle } from "@pierre/diffs/react"; import type { EnvironmentId, @@ -888,7 +893,7 @@ function PullRequestCodeTab({ const isCurrentDraft = useStableCallback( (anchor: DraftAnchor) => draft === anchor && - canCommentOnLines && + canSelectLines && isCurrentFile(anchor.fileKey, anchor.contentVersion, anchor.scopeKey), ); const finishSelection = ( From 444bd159a15b7f1369c71bcca9fa63db4fc46c59 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Tue, 27 Oct 2026 15:11:24 -0400 Subject: [PATCH 09/13] fix: preserve feature behavior after upstream rebase --- apps/web/src/components/diffs/useReviewFilePatches.ts | 2 +- apps/web/src/components/pullRequest/PullRequestCodeTab.tsx | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts index e926e8bfacd22..28bcb4e951e1f 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.ts +++ b/apps/web/src/components/diffs/useReviewFilePatches.ts @@ -7,7 +7,7 @@ import type { } from "@t3tools/contracts"; import * as AsyncResult from "effect/reactivity/AsyncResult"; import * as Atom from "effect/reactivity/Atom"; -import { useContext, useEffect, useRef, useState } from "react"; +import { useContext, useState } from "react"; import { useStableCallback } from "~/hooks/useStableCallback"; import { getRenderablePatch, diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 22b50a73b6546..bf40b2b5d0180 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -8,7 +8,7 @@ import type { FileDiffMetadata, SelectedLineRange, } from "@pierre/diffs"; -import type { CodeViewAnnotationArgs, CodeViewDiffItem, CodeViewHandle } from "@pierre/diffs/react"; +import type { CodeViewAnnotationArgs, CodeViewDiffItem } from "@pierre/diffs/react"; import type { EnvironmentId, PullRequestDetailView, @@ -343,9 +343,6 @@ function PullRequestCodeTab({ readonly slices: ReadonlyArray; readonly refreshing: ReadonlyArray | null; }>({ key: "", cursor: null, slices: NO_SLICES, refreshing: null }); - const [viewer, setViewer] = useState | null>( - null, - ); const referenceKey = pullRequestReviewKey(reference); // A commit opened from the timeline or left selected across a force-push may no longer be From 5dc109da4b96345c99fb29ef27109c3d51b4534b Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Wed, 28 Oct 2026 09:00:19 -0400 Subject: [PATCH 10/13] fix: preserve feature behavior on current upstream --- apps/web/src/components/DiffPanel.tsx | 4 ++-- apps/web/src/components/pullRequest/PullRequestCodeTab.tsx | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index b72c8f31da0e0..f620cf9228851 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -387,8 +387,8 @@ export default function DiffPanel({ ? toolCallPreview?.patch : selectedTurn ? activeCheckpointDiff.error === null - ? activeCheckpointDiff.data?.diff - : undefined + ? activeCheckpointDiff.data?.diff + : undefined : gitDiff; const isSelectedPatchTruncated = toolCallPreview?.truncated === true || diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index bf40b2b5d0180..c8a09ba11d0af 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -517,6 +517,7 @@ function PullRequestCodeTab({ registry, refreshFilesViewed, ]); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- invalidates the next cached environment diff page before advancing its refresh walk useEffect(() => { const last = sliceState.refreshing?.at(-1); if ( From 77edac97fe732b161db7ad0c07fb4ed67a2fb97f Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Wed, 28 Oct 2026 10:23:45 -0400 Subject: [PATCH 11/13] fix(web): invalidate local diff drafts after content changes --- .../AnnotatableCodeView.browser.test.tsx | 15 +-- .../diffs/AnnotatableCodeView.logic.test.ts | 63 +++++++++ .../diffs/AnnotatableCodeView.logic.ts | 30 +++++ .../components/diffs/AnnotatableCodeView.tsx | 124 +++++++++++------- 4 files changed, 172 insertions(+), 60 deletions(-) create mode 100644 apps/web/src/components/diffs/AnnotatableCodeView.logic.test.ts create mode 100644 apps/web/src/components/diffs/AnnotatableCodeView.logic.ts diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx index 8725a6a0074a8..a32bcacfbd5bd 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx @@ -6,7 +6,7 @@ import { describe, expect, it, vi } from "vite-plus/test"; import { act, render } from "../../../test/render"; import { DraftId, useComposerDraftStore } from "~/composerDraftStore"; -import { buildFileDiffIdentityKey } from "~/lib/diffRendering"; +import { buildFileDiffContentVersion, buildFileDiffIdentityKey } from "~/lib/diffRendering"; type ViewerProps = Omit>, "options"> & { options: { @@ -54,12 +54,7 @@ const target = DraftId.make("diff-draft-test"); const range: SelectedLineRange = { start: 1, end: 1, side: "additions" }; let container: HTMLDivElement; -function file( - name: string, - fileVersion = 1, - type: FileDiffMetadata["type"] = "change", - prevName?: string, -) { +function file(name: string, type: FileDiffMetadata["type"] = "change", prevName?: string) { const fileDiff = parsePatchFiles( `diff --git a/${name} b/${name}\n--- a/${name}\n+++ b/${name}\n@@ -1 +1 @@\n-old\n+new\n`, name, @@ -70,7 +65,7 @@ function file( fileDiff, filePath: name, fileKey: buildFileDiffIdentityKey(fileDiff), - fileVersion, + fileVersion: buildFileDiffContentVersion(fileDiff), collapsed: false, }; } @@ -132,11 +127,11 @@ function setup(): Disposable { describe("diff comment drafts", () => { it("keeps the text and selected line through refresh and a provisional lazy patch", async () => { using _drafts = setup(); - const original = file("shared.ts", 1, "rename-pure", "old.ts"); + const original = file("shared.ts", "rename-pure", "old.ts"); await using screen = await render(view([original]), { container }); await click("Comment on shared.ts"); await typeComment("Keep this draft"); - const refreshed = file("shared.ts", 2, "rename-pure", "old.ts"); + const refreshed = file("shared.ts", "rename-pure", "old.ts"); await screen.rerender(view([refreshed], "refreshed")); expect(editor().value).toBe("Keep this draft"); expect(container.querySelector("mark")?.textContent).toBe("Line 1"); diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.logic.test.ts b/apps/web/src/components/diffs/AnnotatableCodeView.logic.test.ts new file mode 100644 index 0000000000000..1a2739d85a3f6 --- /dev/null +++ b/apps/web/src/components/diffs/AnnotatableCodeView.logic.test.ts @@ -0,0 +1,63 @@ +import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles"; +import { describe, expect, it } from "vite-plus/test"; + +import { buildFileDiffContentVersion, buildFileDiffIdentityKey } from "~/lib/diffRendering"; + +import { isDiffCommentAnchorCurrent } from "./AnnotatableCodeView.logic"; + +function file(contents: string) { + return parsePatchFiles( + `diff --git a/shared.ts b/shared.ts\n--- a/shared.ts\n+++ b/shared.ts\n@@ -1 +1 @@\n-old\n+${contents}\n`, + )[0]!.files[0]!; +} + +describe("diff comment anchors", () => { + it("invalidates old line coordinates when the same file changes", () => { + const original = file("new"); + const refreshed = file("newer"); + const anchor = { + fileKey: buildFileDiffIdentityKey(original), + filePath: "shared.ts", + previousFilePath: "shared.ts", + fileVersion: buildFileDiffContentVersion(original), + }; + expect(buildFileDiffIdentityKey(refreshed)).toBe(anchor.fileKey); + const changedFiles = new Map([ + [ + anchor.fileKey, + { fileVersion: buildFileDiffContentVersion(refreshed), fileDiff: refreshed }, + ], + ]); + expect(isDiffCommentAnchorCurrent(anchor, changedFiles, [])).toBe(false); + expect( + isDiffCommentAnchorCurrent( + anchor, + new Map([[anchor.fileKey, { ...anchor, fileDiff: original }]]), + [], + ), + ).toBe(true); + const pending = { ...original, type: "rename-changed" as const, hunks: [] }; + expect(isDiffCommentAnchorCurrent(anchor, new Map(), [pending])).toBe(true); + expect(isDiffCommentAnchorCurrent(anchor, changedFiles, [pending])).toBe(false); + const sameIdentityPending = { ...original, hunks: [], cacheKey: "shared:pending" }; + expect( + isDiffCommentAnchorCurrent( + anchor, + new Map([ + [ + anchor.fileKey, + { + fileDiff: sameIdentityPending, + fileVersion: buildFileDiffContentVersion(sameIdentityPending), + }, + ], + ]), + [sameIdentityPending], + ), + ).toBe(true); + expect( + isDiffCommentAnchorCurrent(anchor, new Map(), [{ ...pending, prevName: "other.ts" }]), + ).toBe(false); + expect(isDiffCommentAnchorCurrent(anchor, new Map(), [])).toBe(false); + }); +}); diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.logic.ts b/apps/web/src/components/diffs/AnnotatableCodeView.logic.ts new file mode 100644 index 0000000000000..21a144abe27dc --- /dev/null +++ b/apps/web/src/components/diffs/AnnotatableCodeView.logic.ts @@ -0,0 +1,30 @@ +import type { FileDiffMetadata } from "@pierre/diffs"; + +import { resolveFileDiffPath, resolveFileDiffPreviousPath } from "~/lib/diffRendering"; + +export interface DiffCommentAnchor { + readonly fileKey: string; + readonly filePath: string; + readonly previousFilePath: string; + readonly fileVersion: number; +} + +export function isDiffCommentAnchorCurrent( + anchor: DiffCommentAnchor, + filesByKey: ReadonlyMap< + string, + { readonly fileVersion: number; readonly fileDiff: FileDiffMetadata } + >, + pendingFiles: ReadonlyArray, +): boolean { + const file = filesByKey.get(anchor.fileKey); + if (file !== undefined && !file.fileDiff.cacheKey?.endsWith(":pending")) { + return file.fileVersion === anchor.fileVersion; + } + // Lazy placeholders have provisional change types, so match both paths instead. + return pendingFiles.some( + (file) => + resolveFileDiffPath(file) === anchor.filePath && + resolveFileDiffPreviousPath(file) === anchor.previousFilePath, + ); +} diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.tsx index 81bcba75aa797..2828b6a9ad710 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.tsx @@ -11,7 +11,13 @@ import type { ScopedThreadRef } from "@t3tools/contracts"; import { useState, type ReactNode, type Ref } from "react"; import { type DraftId, useComposerDraftStore } from "~/composerDraftStore"; -import { fnv1a32, resolveFileDiffPath, resolveFileDiffPreviousPath } from "~/lib/diffRendering"; +import { + buildFileDiffContentVersion, + fnv1a32, + resolveFileDiffPath, + resolveFileDiffPreviousPath, +} from "~/lib/diffRendering"; +import { useStableCallback } from "~/hooks/useStableCallback"; import { buildDiffReviewComment, restoreDiffReviewCommentRange, @@ -20,6 +26,7 @@ import { import { nextFileCommentId } from "../files/fileCommentAnnotations"; import { DiffCommentAnnotation } from "./DiffCommentAnnotation"; +import { isDiffCommentAnchorCurrent, type DiffCommentAnchor } from "./AnnotatableCodeView.logic"; import { StyledDiffCodeView, type StyledDiffCodeViewOptions } from "./StyledDiffCodeView"; interface DiffCommentAnnotationEntry { @@ -128,28 +135,25 @@ export function AnnotatableCodeView({ const [selectedLines, setSelectedLines] = useState<{ id: string; range: SelectedLineRange; + anchor: DiffCommentAnchor; } | null>(null); - const [draft, setDraft] = useState<{ - fileKey: string; - filePath: string; - previousFilePath: string; - annotation: DiffCommentLineAnnotation; - } | null>(null); + const [draft, setDraft] = useState< + | (DiffCommentAnchor & { + annotation: DiffCommentLineAnnotation; + }) + | null + >(null); const [draftText, setDraftText] = useState(""); const filesByKey = new Map(files.map((file) => [file.fileKey, file])); - if ( - draft !== null && - !filesByKey.has(draft.fileKey) && - // Lazy placeholders have provisional change types, so match both paths instead. - !pendingFiles.some( - (file) => - resolveFileDiffPath(file) === draft.filePath && - resolveFileDiffPreviousPath(file) === draft.previousFilePath, - ) - ) { + if (draft !== null && !isDiffCommentAnchorCurrent(draft, filesByKey, pendingFiles)) { setDraft(null); setDraftText(""); + } + if ( + selectedLines !== null && + !isDiffCommentAnchorCurrent(selectedLines.anchor, filesByKey, pendingFiles) + ) { setSelectedLines(null); } const items: CodeViewDiffItem[] = files.map( @@ -202,10 +206,10 @@ export function AnnotatableCodeView({ } }; - const submitEntry = (entryId: string, text: string) => { + const submitEntry = useStableCallback((entryId: string, text: string) => { const entry = draft?.annotation.metadata.entries.find((candidate) => candidate.id === entryId); const file = draft ? filesByKey.get(draft.fileKey) : undefined; - if (!entry || !file) return; + if (!entry || !file || draft?.fileVersion !== file.fileVersion) return; const comment = buildDiffReviewComment({ id: entry.id, sectionId, @@ -219,38 +223,43 @@ export function AnnotatableCodeView({ setSelectedLines(null); setDraft(null); setDraftText(""); - }; + }); - const beginComment = (range: SelectedLineRange | null, context: DiffSelectionContext) => { - if (!range) return; - const item = context.item; - if (item.type !== "diff") return; - const id = nextFileCommentId(); - const filePath = resolveFileDiffPath(item.fileDiff); - const comment = buildDiffReviewComment({ - id, - sectionId, - sectionTitle, - filePath, - fileDiff: item.fileDiff, - range, - text: "", - }); - if (!comment) return; - setDraftText(""); - setDraft({ - fileKey: item.id, - filePath, - previousFilePath: resolveFileDiffPreviousPath(item.fileDiff), - annotation: { - side: annotationSide(range), - lineNumber: range.end, - metadata: { - entries: [{ id, kind: "draft", range, rangeLabel: comment.rangeLabel, text: "" }], + const beginComment = useStableCallback( + (range: SelectedLineRange | null, context: DiffSelectionContext) => { + if (!range) return; + const item = context.item; + if (item.type !== "diff") return; + const file = filesByKey.get(item.id); + if (!file || file.fileVersion !== buildFileDiffContentVersion(item.fileDiff)) return; + const id = nextFileCommentId(); + const filePath = resolveFileDiffPath(item.fileDiff); + const comment = buildDiffReviewComment({ + id, + sectionId, + sectionTitle, + filePath, + fileDiff: item.fileDiff, + range, + text: "", + }); + if (!comment) return; + setDraftText(""); + setDraft({ + fileKey: item.id, + fileVersion: file.fileVersion, + filePath, + previousFilePath: resolveFileDiffPreviousPath(item.fileDiff), + annotation: { + side: annotationSide(range), + lineNumber: range.end, + metadata: { + entries: [{ id, kind: "draft", range, rangeLabel: comment.rangeLabel, text: "" }], + }, }, - }, - }); - }; + }); + }, + ); return ( @@ -267,7 +276,22 @@ export function AnnotatableCodeView({ {...(renderCodeViewFooter ? { renderCodeViewFooter } : {})} items={items} selectedLines={selectedLines} - onSelectedLinesChange={setSelectedLines} + onSelectedLinesChange={(selection) => { + const file = selection === null ? undefined : filesByKey.get(selection.id); + setSelectedLines( + selection !== null && file !== undefined + ? { + ...selection, + anchor: { + fileKey: file.fileKey, + fileVersion: file.fileVersion, + filePath: file.filePath, + previousFilePath: resolveFileDiffPreviousPath(file.fileDiff), + }, + } + : null, + ); + }} onRevealSearchMatch={(item) => onRevealSearchMatch(item.id)} options={{ ...options, From a7e9fca88d608038a6cc9775b5377a42ad2bb7d3 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Wed, 28 Oct 2026 10:58:46 -0400 Subject: [PATCH 12/13] fix(web): guard diff comments until refreshed content renders --- apps/web/src/components/DiffPanel.tsx | 1 + .../AnnotatableCodeView.browser.test.tsx | 275 ++++++++++++------ .../components/diffs/AnnotatableCodeView.tsx | 49 +++- 3 files changed, 219 insertions(+), 106 deletions(-) diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index f620cf9228851..0d6f66e6891df 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -1161,6 +1161,7 @@ export default function DiffPanel({ className="h-full min-h-0 overflow-auto" files={codeViewFiles} pendingFiles={pendingFiles} + isRefreshingDiff={isRefreshingDiff} renderCodeViewFooter={renderLoadingBoundary} sectionId={reviewSectionId} sectionTitle={reviewSectionTitle} diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx index a32bcacfbd5bd..72c2b19e6e324 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx @@ -1,6 +1,7 @@ -import type { CodeViewDiffItem, FileDiffMetadata, SelectedLineRange } from "@pierre/diffs"; +import type { FileDiffMetadata, SelectedLineRange } from "@pierre/diffs"; import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles"; -import { StrictMode, type ComponentProps } from "react"; +import { WorkerPoolManager } from "@pierre/diffs/worker"; +import { createRef, StrictMode } from "react"; import { describe, expect, it, vi } from "vite-plus/test"; import { act, render } from "../../../test/render"; @@ -8,56 +9,20 @@ import { act, render } from "../../../test/render"; import { DraftId, useComposerDraftStore } from "~/composerDraftStore"; import { buildFileDiffContentVersion, buildFileDiffIdentityKey } from "~/lib/diffRendering"; -type ViewerProps = Omit>, "options"> & { - options: { - enableLineSelection: boolean; - enableGutterUtility: boolean; - onGutterUtilityClick: ( - range: SelectedLineRange, - context: { item: CodeViewDiffItem }, - ) => void; - }; -}; - -// Replace only the virtualized viewer; comment editing and submission use the real components. -vi.mock("./StyledDiffCodeView", () => ({ - StyledDiffCodeView: (props: ViewerProps) => - props.items - ?.filter((item) => item.type === "diff") - .map((item) => ( -
- - {props.selectedLines?.id === item.id ? ( - Line {props.selectedLines.range.start} - ) : null} - {item.annotations?.map((annotation) => ( -
- {props.renderAnnotation?.(annotation, item.fileDiff, item)} -
- ))} -
- )), -})); - -import { AnnotatableCodeView } from "./AnnotatableCodeView"; -import type { StyledDiffCodeView } from "./StyledDiffCodeView"; +import { AnnotatableCodeView, type AnnotatableCodeViewHandle } from "./AnnotatableCodeView"; const target = DraftId.make("diff-draft-test"); const range: SelectedLineRange = { start: 1, end: 1, side: "additions" }; -let container: HTMLDivElement; -function file(name: string, type: FileDiffMetadata["type"] = "change", prevName?: string) { +function file( + name: string, + type: FileDiffMetadata["type"] = "change", + prevName?: string, + contents = "new", +) { const fileDiff = parsePatchFiles( - `diff --git a/${name} b/${name}\n--- a/${name}\n+++ b/${name}\n@@ -1 +1 @@\n-old\n+new\n`, - name, + `diff --git a/${name} b/${name}\n--- a/${name}\n+++ b/${name}\n@@ -1 +1,${contents.split("\n").length} @@\n-old\n+${contents}\n`, + `${name}:${contents}`, )[0]!.files[0]!; fileDiff.type = type; if (prevName) fileDiff.prevName = prevName; @@ -70,17 +35,25 @@ function file(name: string, type: FileDiffMetadata["type"] = "change", prevName? }; } -function view( - files: ReturnType[], - codeViewKey = "initial", - pendingFiles: FileDiffMetadata[] = [], -) { - return ( +function setup() { + const reset = () => useComposerDraftStore.setState(useComposerDraftStore.getInitialState(), true); + reset(); + const container = document.createElement("div"); + Object.assign(container.style, { width: "800px", height: "400px", display: "grid" }); + const viewerRef = createRef(); + const view = ( + files: ReturnType[], + codeViewKey = "initial", + pendingFiles: FileDiffMetadata[] = [], + isRefreshingDiff = false, + ) => ( ); -} - -function click(label: string) { - const button = Array.from(container.querySelectorAll("button")).find( - (node) => node.textContent === label, - ); - if (!button) throw new Error(`Missing button: ${label}`); - return act(() => button.click()); -} - -function editor() { - const textarea = container.querySelector("textarea"); - if (!textarea) throw new Error("Comment editor is not available"); - return textarea; -} - -function typeComment(text: string) { - return act(() => { - Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!.call( - editor(), - text, + const renderedFile = () => { + const item = viewerRef.current?.getInstance()?.getRenderedItems()[0]; + if (item?.type !== "diff") throw new Error("Diff is not rendered"); + return item; + }; + const select = () => + act(() => { + // Invoke Pierre's own wrappers, which resolve their context from the latest item. + const { instance } = renderedFile(); + instance.options.onLineSelectionChange?.(range); + instance.options.onGutterUtilityClick?.(range); + }); + const editor = () => { + const textarea = container.querySelector("textarea"); + if (!textarea) throw new Error("Comment editor is not available"); + return textarea; + }; + const click = (label: string) => { + const button = Array.from(container.querySelectorAll("button")).find( + (node) => node.textContent === label, ); - editor().dispatchEvent(new Event("input", { bubbles: true })); - }); -} - -function setup(): Disposable { - const reset = () => useComposerDraftStore.setState(useComposerDraftStore.getInitialState(), true); - reset(); - container = document.createElement("div"); - return { [Symbol.dispose]: reset }; + if (!button) throw new Error(`Missing button: ${label}`); + return act(() => button.click()); + }; + const typeComment = (text: string) => + act(() => { + Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!.call( + editor(), + text, + ); + editor().dispatchEvent(new Event("input", { bubbles: true })); + }); + return { + container, + viewerRef, + view, + renderedFile, + select, + editor, + click, + typeComment, + [Symbol.dispose]: reset, + }; } describe("diff comment drafts", () => { it("keeps the text and selected line through refresh and a provisional lazy patch", async () => { - using _drafts = setup(); + using test = setup(); const original = file("shared.ts", "rename-pure", "old.ts"); - await using screen = await render(view([original]), { container }); - await click("Comment on shared.ts"); - await typeComment("Keep this draft"); + await using screen = await render(test.view([original]), { container: test.container }); + await vi.waitFor(() => expect(test.renderedFile().instance.getLineIndex(1)).toBeDefined()); + await test.select(); + await vi.waitFor(() => expect(test.editor().value).toBe("")); + await test.typeComment("Keep this draft"); const refreshed = file("shared.ts", "rename-pure", "old.ts"); - await screen.rerender(view([refreshed], "refreshed")); - expect(editor().value).toBe("Keep this draft"); - expect(container.querySelector("mark")?.textContent).toBe("Line 1"); + await screen.rerender(test.view([refreshed], "refreshed", [], true)); + await vi.waitFor(() => expect(test.editor().value).toBe("Keep this draft")); + expect(test.viewerRef.current?.getSelectedLines()).toEqual( + expect.objectContaining({ id: original.fileKey, range }), + ); + await test.click("Comment"); + expect(useComposerDraftStore.getState().getComposerDraft(target)?.reviewComments ?? []).toEqual( + [], + ); + await act(() => { + test + .editor() + .dispatchEvent( + new KeyboardEvent("keydown", { key: "Enter", metaKey: true, bubbles: true }), + ); + }); + expect(useComposerDraftStore.getState().getComposerDraft(target)?.reviewComments ?? []).toEqual( + [], + ); + expect(test.editor().value).toBe("Keep this draft"); await screen.rerender( - view([], "loading", [ + test.view([], "loading", [ { ...refreshed.fileDiff, type: "rename-changed", hunks: [], cacheKey: "shared:pending" }, ]), ); - expect(container.querySelector("textarea")).toBeNull(); - await screen.rerender(view([refreshed], "loaded")); - expect(editor().value).toBe("Keep this draft"); - expect(container.querySelector("mark")?.textContent).toBe("Line 1"); - await click("Comment"); + expect(test.container.querySelector("textarea")).toBeNull(); + await screen.rerender(test.view([refreshed], "loaded")); + await vi.waitFor(() => expect(test.editor().value).toBe("Keep this draft")); + expect(test.viewerRef.current?.getSelectedLines()).toEqual( + expect.objectContaining({ id: original.fileKey, range }), + ); + await test.click("Comment"); expect(useComposerDraftStore.getState().getComposerDraft(target)?.reviewComments).toEqual([ expect.objectContaining({ filePath: "shared.ts", @@ -153,6 +159,81 @@ describe("diff comment drafts", () => { selection: { ...range, endSide: "additions" }, }), ]); - expect(container.querySelector("p")?.textContent).toBe("Keep this draft"); + }); + + it("rejects old line ranges and retained submission until Pierre adopts refreshed content", async () => { + using test = setup(); + const original = file("adoption.ts"); + const refreshed = file("adoption.ts", "change", undefined, "inserted\n+new"); + const highlight = WorkerPoolManager.prototype.highlightDiffAST; + let resumeHighlight: (() => void) | undefined; + let pool: WorkerPoolManager | undefined; + using _highlight = vi + .spyOn(WorkerPoolManager.prototype, "highlightDiffAST") + .mockImplementation(function (this: WorkerPoolManager, instance, diff) { + pool = this; + if (diff === refreshed.fileDiff) { + resumeHighlight = () => highlight.call(this, instance, diff); + return; + } + return highlight.call(this, instance, diff); + }); + await using screen = await render(test.view([original]), { container: test.container }); + await vi.waitFor(() => { + expect(pool?.getDiffResultCache(original.fileDiff)).toBeDefined(); + expect(test.renderedFile().instance.getLineIndex(1)).toBeDefined(); + }); + await act(() => test.viewerRef.current?.getInstance()?.render(true)); + await test.select(); + await vi.waitFor(() => expect(test.editor().value).toBe("")); + await test.typeComment("Old coordinates"); + const retainedSubmit = Array.from(test.container.querySelectorAll("button")).find( + (node) => node.textContent === "Comment", + )!; + const previousInstance = test.renderedFile().instance; + + await screen.rerender(test.view([refreshed])); + await vi.waitFor(() => expect(resumeHighlight).toBeDefined()); + const pending = test.renderedFile(); + expect(pending.item.fileDiff).toBe(refreshed.fileDiff); + expect(pending.instance).toBe(previousInstance); + expect(pending.element.shadowRoot?.textContent).not.toContain("inserted"); + expect(retainedSubmit.isConnected).toBe(true); + expect( + pending.instance.__getNextRenderData({ + fileDiff: refreshed.fileDiff, + lineAnnotations: undefined, + }).fileDiff, + ).toBe(original.fileDiff); + await test.typeComment("Still visible old draft"); + expect(retainedSubmit.disabled).toBe(false); + await act(() => retainedSubmit.click()); + expect(useComposerDraftStore.getState().getComposerDraft(target)?.reviewComments ?? []).toEqual( + [], + ); + await test.select(); + expect(test.viewerRef.current?.getSelectedLines()).toBeNull(); + + await act(() => resumeHighlight?.()); + await vi.waitFor(() => { + expect( + test.renderedFile().instance.__getNextRenderData({ + fileDiff: refreshed.fileDiff, + lineAnnotations: undefined, + }).fileDiff, + ).toBe(refreshed.fileDiff); + expect(test.renderedFile().element.shadowRoot?.textContent).toContain("inserted"); + expect(test.container.querySelector("textarea")).toBeNull(); + }); + await test.select(); + await vi.waitFor(() => expect(test.editor().value).toBe("")); + await test.typeComment("Current coordinates"); + await test.click("Comment"); + expect(useComposerDraftStore.getState().getComposerDraft(target)?.reviewComments).toEqual([ + expect.objectContaining({ + text: "Current coordinates", + diff: expect.stringContaining("+inserted"), + }), + ]); }); }); diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.tsx index 2828b6a9ad710..04c2d108e51b9 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.tsx @@ -8,13 +8,12 @@ import type { } from "@pierre/diffs"; import type { CodeViewHandle } from "@pierre/diffs/react"; import type { ScopedThreadRef } from "@t3tools/contracts"; -import { useState, type ReactNode, type Ref } from "react"; +import { useRef, useState, type ReactNode, type Ref } from "react"; import { type DraftId, useComposerDraftStore } from "~/composerDraftStore"; import { buildFileDiffContentVersion, fnv1a32, - resolveFileDiffPath, resolveFileDiffPreviousPath, } from "~/lib/diffRendering"; import { useStableCallback } from "~/hooks/useStableCallback"; @@ -88,6 +87,7 @@ interface AnnotatableCodeViewProps { collapsed: boolean; }>; pendingFiles: ReadonlyArray; + isRefreshingDiff: boolean; sectionId: string; sectionTitle: string; composerDraftTarget: ScopedThreadRef | DraftId; @@ -114,6 +114,7 @@ export function AnnotatableCodeView({ codeViewKey, files, pendingFiles, + isRefreshingDiff, sectionId, sectionTitle, composerDraftTarget, @@ -144,8 +145,19 @@ export function AnnotatableCodeView({ | null >(null); const [draftText, setDraftText] = useState(""); + const renderedFiles = useRef(new Map()); const filesByKey = new Map(files.map((file) => [file.fileKey, file])); + const isCurrentFileRendered = (fileKey: string) => { + const file = filesByKey.get(fileKey); + const rendered = renderedFiles.current.get(fileKey); + return ( + !isRefreshingDiff && + file !== undefined && + rendered !== undefined && + (file.fileDiff === rendered || file.fileVersion === buildFileDiffContentVersion(rendered)) + ); + }; if (draft !== null && !isDiffCommentAnchorCurrent(draft, filesByKey, pendingFiles)) { setDraft(null); setDraftText(""); @@ -209,7 +221,7 @@ export function AnnotatableCodeView({ const submitEntry = useStableCallback((entryId: string, text: string) => { const entry = draft?.annotation.metadata.entries.find((candidate) => candidate.id === entryId); const file = draft ? filesByKey.get(draft.fileKey) : undefined; - if (!entry || !file || draft?.fileVersion !== file.fileVersion) return; + if (isRefreshingDiff || !entry || !file || draft?.fileVersion !== file.fileVersion) return; const comment = buildDiffReviewComment({ id: entry.id, sectionId, @@ -231,15 +243,15 @@ export function AnnotatableCodeView({ const item = context.item; if (item.type !== "diff") return; const file = filesByKey.get(item.id); - if (!file || file.fileVersion !== buildFileDiffContentVersion(item.fileDiff)) return; + if (!file || !isCurrentFileRendered(item.id)) return; const id = nextFileCommentId(); - const filePath = resolveFileDiffPath(item.fileDiff); + const filePath = file.filePath; const comment = buildDiffReviewComment({ id, sectionId, sectionTitle, filePath, - fileDiff: item.fileDiff, + fileDiff: file.fileDiff, range, text: "", }); @@ -249,7 +261,7 @@ export function AnnotatableCodeView({ fileKey: item.id, fileVersion: file.fileVersion, filePath, - previousFilePath: resolveFileDiffPreviousPath(item.fileDiff), + previousFilePath: resolveFileDiffPreviousPath(file.fileDiff), annotation: { side: annotationSide(range), lineNumber: range.end, @@ -277,6 +289,7 @@ export function AnnotatableCodeView({ items={items} selectedLines={selectedLines} onSelectedLinesChange={(selection) => { + if (selection !== null && !isCurrentFileRendered(selection.id)) return; const file = selection === null ? undefined : filesByKey.get(selection.id); setSelectedLines( selection !== null && file !== undefined @@ -295,9 +308,26 @@ export function AnnotatableCodeView({ onRevealSearchMatch={(item) => onRevealSearchMatch(item.id)} options={{ ...options, - enableGutterUtility: draft === null, - enableLineSelection: draft === null, + enableGutterUtility: draft === null && !isRefreshingDiff, + enableLineSelection: draft === null && !isRefreshingDiff, onGutterUtilityClick: beginComment, + onPostRender: (node, _instance, phase, context) => { + if (context.type === "diff") { + if (phase === "unmount") { + renderedFiles.current.delete(context.item.id); + } else { + // At post-render this is the data Pierre just adopted, including retained rows. + const { fileDiff } = context.instance.__getNextRenderData({ + fileDiff: context.item.fileDiff, + lineAnnotations: undefined, + }); + renderedFiles.current.set(context.item.id, fileDiff); + } + options.onPostRender?.(node, context.instance, phase, context); + } else { + options.onPostRender?.(node, context.instance, phase, context); + } + }, }} renderHeaderFilenameSuffix={(item) => item.type === "diff" ? renderHeaderFilenameSuffix(item.fileDiff) : null @@ -322,6 +352,7 @@ export function AnnotatableCodeView({ onTextChange={setDraftText} onCancel={() => removeEntry(entry.id)} onComment={(text) => submitEntry(entry.id, text)} + pending={isRefreshingDiff} onDelete={() => removeEntry(entry.id)} /> ))} From bb12eab2f19261d8c4e131e48c372d6fe7480419 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Wed, 28 Oct 2026 11:17:28 -0400 Subject: [PATCH 13/13] test(web): await diff viewer readiness in comment regressions --- .../diffs/AnnotatableCodeView.browser.test.tsx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx index 72c2b19e6e324..f68de96687ecf 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx @@ -50,6 +50,7 @@ function setup() { { using test = setup(); const original = file("shared.ts", "rename-pure", "old.ts"); await using screen = await render(test.view([original]), { container: test.container }); - await vi.waitFor(() => expect(test.renderedFile().instance.getLineIndex(1)).toBeDefined()); + await expect.element(screen.getByText("new", { exact: true })).toBeVisible(); + expect(test.renderedFile().instance.getLineIndex(1)).toBeDefined(); await test.select(); await vi.waitFor(() => expect(test.editor().value).toBe("")); await test.typeComment("Keep this draft"); @@ -179,10 +181,12 @@ describe("diff comment drafts", () => { return highlight.call(this, instance, diff); }); await using screen = await render(test.view([original]), { container: test.container }); - await vi.waitFor(() => { - expect(pool?.getDiffResultCache(original.fileDiff)).toBeDefined(); - expect(test.renderedFile().instance.getLineIndex(1)).toBeDefined(); - }); + await expect.element(screen.getByText("new", { exact: true })).toBeVisible(); + const initialPool = pool; + if (!initialPool) throw new Error("Diff worker pool is unavailable"); + await act(() => initialPool.primeDiffHighlightCache(original.fileDiff)); + expect(initialPool.getDiffResultCache(original.fileDiff)).toBeDefined(); + expect(test.renderedFile().instance.getLineIndex(1)).toBeDefined(); await act(() => test.viewerRef.current?.getInstance()?.render(true)); await test.select(); await vi.waitFor(() => expect(test.editor().value).toBe(""));