From eeaa5d9255acd6b341bb87c95081d112eba9820b Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Sun, 4 Oct 2026 10:35:53 -0400 Subject: [PATCH 01/19] 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 | 134 +++++++++--------- apps/web/src/components/ChatView.logic.ts | 36 +++++ apps/web/src/hooks/useProactivePanels.ts | 6 +- 3 files changed, 111 insertions(+), 65 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 8a777ce0d7b95..0b913a4f3f20c 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -1,5 +1,5 @@ -import { rewindToMessage } from "./chat/chatViewSend"; -import * as session from "../state/session"; +import { shouldShowOpenInPicker } from "./chat/OpenInPicker.logic"; +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { environmentContextRecord, environmentContextReference, @@ -73,6 +73,8 @@ import { resetHeldThreadTimeline, resolveThreadSwitchTimeline, timelineHasEphemeralPreviewUrls, + shouldApplyProactiveChangesScope, + shouldResetDiffSelectionToChanges, createLocalDispatchSnapshot, deriveCommittedServerUserMessageIds, deriveComposerSendState, @@ -451,84 +453,88 @@ describe("deriveCommittedServerUserMessageIds", () => { }); }); -describe("hasCompactableConversation", () => { - const at = DateTime.makeUnsafe("2026-10-26T08:00:00.000Z"); - const runId = RunId.make("run:thread:thread-1:ordinal:1"); - const itemBase = (id: string, ordinal: number) => ({ - id: TurnItemId.make(id), - threadId, - runId, - nodeId: null, - providerThreadId: null, - providerTurnId: null, - nativeItemRef: null, - parentItemId: null, - ordinal, - status: "completed" as const, - title: null, - startedAt: at, - completedAt: at, - updatedAt: at, - }); - const userMessage = (text: string): OrchestrationV2ProjectedTurnItem => ({ - position: 0, - visibility: "local", - sourceThreadId: threadId, - sourceItemId: TurnItemId.make("turn-item:user"), - item: { - ...itemBase("turn-item:user", 1), - createdBy: "user", - creationSource: "web", - type: "user_message", - messageId: MessageId.make("message-user"), - inputIntent: "turn_start", - text, - attachments: [], - }, +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); }); - const assistantMessage: OrchestrationV2ProjectedTurnItem = { - position: 1, - visibility: "local", - sourceThreadId: threadId, - sourceItemId: TurnItemId.make("turn-item:assistant"), - item: { - ...itemBase("turn-item:assistant", 2), - type: "assistant_message", - messageId: MessageId.make("message-assistant"), - text: "done", - streaming: false, - }, - }; - it("counts a user message beyond the loaded page", () => { + 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( - hasCompactableConversation({ - visibleTurnItems: [assistantMessage], - hasMoreHistory: true, - latestUserMessageAt: "2026-10-26T08:00:00.000Z", + shouldResetDiffSelectionToChanges({ + diffOpen: true, + activeThreadRef: threadRef, + explicitThreadRef: null, + previous: null, + threadKey: "env-1:thread-1", }), ).toBe(true); }); - it("stays unavailable when the thread never had a user message", () => { + it("leaves a timeline turn selection in place", () => { expect( - hasCompactableConversation({ - visibleTurnItems: [assistantMessage], - hasMoreHistory: true, - latestUserMessageAt: null, + shouldResetDiffSelectionToChanges({ + diffOpen: true, + activeThreadRef: threadRef, + explicitThreadRef: threadRef, + previous: { threadKey: "env-1:thread-1", diffOpen: false }, + threadKey: "env-1:thread-1", }), ).toBe(false); }); - it("does not count a lone /compact on a fully loaded thread", () => { + it("does nothing while the diff is closed", () => { expect( - hasCompactableConversation({ - visibleTurnItems: [userMessage("/compact"), assistantMessage], - hasMoreHistory: false, - latestUserMessageAt: "2026-10-26T08:00:00.000Z", + 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 65d93da19b08e..eed7866acfb1d 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -222,6 +222,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/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 830ed00a33b6e5428d2eb41b4032f1acdd3d9352 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Sun, 4 Oct 2026 11:10:07 -0400 Subject: [PATCH 02/19] 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 | 5 +- .../diffs/reviewFileDiffRetention.test.ts | 94 +++++++ .../diffs/reviewFileDiffRetention.ts | 88 ++++++ .../components/diffs/useReviewFilePatches.ts | 253 ++++++++++-------- .../pullRequest/PullRequestCodeTab.tsx | 15 +- .../pullRequest/pullRequestDiff.logic.test.ts | 32 ++- .../pullRequest/pullRequestDiff.logic.ts | 21 ++ 7 files changed, 397 insertions(+), 111 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 fa9db80f30556..995138cb4e7c1 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -121,6 +121,7 @@ import { buildBaseRefChoices, filterBaseRefChoices } from "../lib/baseRefChoices import { createGitDiffFileContentsLoader } from "../lib/diffFileContents"; import { observeIntersection } from "~/lib/observeIntersection"; +import { reviewDiffViewerKey } from "./diffs/reviewFileDiffRetention"; import { useReviewFilePatches } from "./diffs/useReviewFilePatches"; import { DiffFileLoadingBoundary } from "./diffs/DiffFileLoadingBoundary"; import { DiffFileStatus } from "./diffs/DiffFileStatus"; @@ -717,6 +718,8 @@ export default function DiffPanel({ ...matchingBaseRefChoices.map(valueForBaseRefChoice), ]; const { + scope: filePatchScope, + family: filePatchFamily, isPending: areFilePatchesPending, fileStates, retry, @@ -1771,7 +1774,7 @@ export default function DiffPanel({ projectId={activeThread?.projectId} viewerRef={attachCodeView} onScroll={codeViewScroll.onScroll} - codeViewKey={codeViewMountKey} + codeViewKey={reviewDiffViewerKey(codeViewMountKey, lazySource ? filePatchFamily : null)} className="h-full min-h-0 overflow-auto" files={codeViewFiles} pendingFiles={pendingFiles} diff --git a/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts b/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts new file mode 100644 index 0000000000000..8f44c5e7eb74a --- /dev/null +++ b/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts @@ -0,0 +1,94 @@ +import type { FileDiffMetadata } from "@pierre/diffs"; +import { describe, expect, it } from "vite-plus/test"; + +import { + isPendingReviewFile, + retainLoadedReviewFiles, + retainedReviewFile, + reviewDiffViewerKey, + reviewPatchIndicesForFamily, + reviewSnapshotFamily, + reviewSnapshotScope, +} from "./reviewFileDiffRetention"; + +const identity = { + environmentId: "env-1", + cwd: "/repo", + kind: "working-tree", + baseRef: "HEAD", + ignoreWhitespace: false, +}; + +function file(name: string, cacheKey?: string): FileDiffMetadata { + return { name, ...(cacheKey ? { cacheKey } : {}) } as FileDiffMetadata; +} + +describe("review snapshot identity", () => { + 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 0c889d501264d..917fa90d3139f 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.ts +++ b/apps/web/src/components/diffs/useReviewFilePatches.ts @@ -5,19 +5,28 @@ import type { ReviewDiffPreviewInput, ReviewDiffPreviewSource, } from "@t3tools/contracts"; -import * as Option from "effect/Option"; 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, sensitivity: "base", }); -const INITIAL_FILE_COUNT = 4; +const DEFAULT_INDICES = [0, 1, 2, 3]; export function useReviewFilePatches({ environmentId, @@ -40,46 +49,96 @@ export function useReviewFilePatches({ preview: RenderablePatch | null; }) { const registry = useContext(RegistryContext); - const scope = JSON.stringify([ - environmentId, - cwd, - source?.kind, - source?.baseRef, - source?.headRef, - baseRef, - ignoreWhitespace, - ]); - const files = sortedReviewFiles(source); - const automaticPaths = files.slice(0, INITIAL_FILE_COUNT).map((file) => file.path); - const [requested, setRequested] = useState(() => ({ scope, paths: automaticPaths })); - const previousPaths = requested.scope === scope ? requested.paths : []; - const paths = [...new Set([...previousPaths, ...automaticPaths])]; - if (requested.scope !== scope || paths.length !== previousPaths.length) { - setRequested({ scope, paths }); - } - 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 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 @@ -87,77 +146,63 @@ export function useReviewFilePatches({ : preview?.kind === "files" ? preview.files.length : 0; - const requestFiles = (indices: number[]) => - setRequested((current) => { - const previous = current.scope === scope ? current.paths : []; - const added = indices.flatMap((index) => { - const path = files[index]?.path; - return path !== undefined && !previous.includes(path) ? [path] : []; - }); - return added.length === 0 ? current : { scope, paths: [...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 = (path: string) => - requestFiles([files.findIndex((file) => file.path === path)]); - const renderableFiles = source - ? files.flatMap((file, index): FileDiffMetadata[] => { - const result = patches.get(index); - const value = result && Option.getOrUndefined(AsyncResult.value(result)); - if (value?.patch?.kind === "files") { - const loaded = value.patch.files.filter( - (candidate) => resolveFileDiffPath(candidate) === file.path, - ); - if (loaded.length > 0) return loaded; - } - return [ - { - name: file.path, - ...(file.previousPath ? { prevName: file.previousPath } : {}), - type: file.changeType ?? (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 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, requestFile, + readyFilePaths, renderableFiles, - remainingFileCount: source ? files.length - settledFileCount : 0, + settledFileCount, loadNextFiles, }; } @@ -179,19 +224,17 @@ function reviewFileQueries({ ignoreWhitespace: boolean; previewInput: ReviewDiffPreviewInput | undefined; scope: string; - requested: { scope: string; paths: string[] }; + requested: { scope: string; indices: number[] }; }) { + const indices = requested.scope === scope ? requested.indices : DEFAULT_INDICES; const files = sortedReviewFiles(source); - const paths = new Set([ - ...files.slice(0, INITIAL_FILE_COUNT).map((file) => file.path), - ...(requested.scope === scope ? requested.paths : []), - ]); const queries = !environmentId || !cwd || !source ? [] - : files - .flatMap((file, index) => (paths.has(file.path) ? [{ file, index }] : [])) - .map(({ file, index }) => { + : indices + .filter((index) => index < files.length) + .map((index) => { + const file = files[index]!; const target: ReviewPatchTarget = { environmentId, input: { diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 724047857a25e..25a5034be2b1c 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -144,7 +144,15 @@ import { toViewerSide, } from "./PullRequestReviewAnnotation"; import { pullRequestCompletionScope } from "./PullRequestMarkdownTextarea"; -import { getFileViewedRange, isLineInFileDiff } from "./pullRequestDiff.logic"; +import { + getFileViewedRange, + isFileDiffCollapsed, + isLineInFileDiff, + withFileDiffFold, + retainReviewDiffSlices, + type DiffFoldOverride, + type FileDiffFoldChoices, +} from "./pullRequestDiff.logic"; import { PullRequestDiffStat, PullRequestMetaLine } from "./pullRequestPresentation"; import { PullRequestDiffUnavailableState } from "./PullRequestDiffUnavailableState"; import { usePullRequestFilesViewed } from "./usePullRequestFilesViewed"; @@ -668,9 +676,8 @@ function PullRequestCodeTab({ 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(); refreshWithheldFiles(); diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts index ce73f75d2db0c..90431a3d70dd0 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts @@ -1,7 +1,13 @@ import type { FileDiffMetadata } from "@pierre/diffs"; import { describe, expect, it } from "vite-plus/test"; -import { getFileViewedRange, isLineInFileDiff } from "./pullRequestDiff.logic"; +import { + getFileViewedRange, + isFileDiffCollapsed, + isLineInFileDiff, + withFileDiffFold, + retainReviewDiffSlices, +} from "./pullRequestDiff.logic"; function fileWithHunks( hunks: ReadonlyArray<{ @@ -14,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 f0c01a0611c3b..8005b436ba3ea 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 1969d6256a1d0307ef5fa3a2dd321fdc499b87b2 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Sun, 4 Oct 2026 11:57:41 -0400 Subject: [PATCH 03/19] 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 --- apps/web/src/components/ChatView.logic.test.ts | 9 +++++++++ apps/web/src/components/ChatView.logic.ts | 13 ++++++++++--- .../diffs/reviewFileDiffRetention.test.ts | 15 +++++++++++++++ .../components/diffs/reviewFileDiffRetention.ts | 8 ++++++-- .../components/diffs/useReviewFilePatches.ts | 1 + .../pullRequest/PullRequestCodeTab.tsx | 17 +++++++++++++++++ .../pullRequest/pullRequestDiff.logic.test.ts | 15 +++++++-------- .../pullRequest/pullRequestDiff.logic.ts | 13 +++++++++---- 8 files changed, 74 insertions(+), 17 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 0b913a4f3f20c..005656815dd67 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -510,6 +510,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 eed7866acfb1d..2fc8012b4aa30 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -229,8 +229,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. */ @@ -242,7 +243,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 917fa90d3139f..74f4e4a88e03c 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 25a5034be2b1c..8c8162e195e33 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -671,11 +671,18 @@ function PullRequestCodeTab({ const refreshWithheldFiles = useEffectEvent(() => { for (const path of withheldPaths) atomRegistry.refresh(withheldFileDiff(path)); }); + // 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); @@ -688,12 +695,22 @@ 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(); refreshWithheldFiles(); 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); diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts index 90431a3d70dd0..b3d9a41a50a44 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 8005b436ba3ea..55ef27f90a930 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 f66fb142f33457c58d4a3d10723b6202dbcfe74d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 20:59:55 +0000 Subject: [PATCH 04/19] 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/DiffPanel.tsx | 12 +- .../diffs/reviewFileDiffRetention.test.ts | 109 ------- .../diffs/reviewFileDiffRetention.ts | 92 ------ .../components/diffs/useReviewFilePatches.ts | 305 +++++++++--------- .../pullRequest/PullRequestCodeTab.tsx | 168 ++++++---- .../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 | 113 ++++--- apps/web/src/lib/diffRendering.ts | 34 ++ 12 files changed, 394 insertions(+), 511 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 005656815dd67..5d4032996c951 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -458,7 +458,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, @@ -468,6 +468,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", () => { @@ -498,18 +501,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, @@ -520,30 +517,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 2fc8012b4aa30..f802bcc659ab8 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -227,14 +227,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; @@ -257,7 +249,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/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index 995138cb4e7c1..04bf41dcb34d3 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -121,7 +121,6 @@ import { buildBaseRefChoices, filterBaseRefChoices } from "../lib/baseRefChoices import { createGitDiffFileContentsLoader } from "../lib/diffFileContents"; import { observeIntersection } from "~/lib/observeIntersection"; -import { reviewDiffViewerKey } from "./diffs/reviewFileDiffRetention"; import { useReviewFilePatches } from "./diffs/useReviewFilePatches"; import { DiffFileLoadingBoundary } from "./diffs/DiffFileLoadingBoundary"; import { DiffFileStatus } from "./diffs/DiffFileStatus"; @@ -503,7 +502,8 @@ export default function DiffPanel({ ? `${panelThreadRef.environmentId}:${panelThreadRef.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" @@ -563,7 +563,6 @@ export default function DiffPanel({ const refreshDiffFromUserAction = refreshPreviewQuery; const threadFolderName = activeProject?.title ?? gt("Project folder"); - const preview = branchDiffPreview.data; const gitDiff = selectedGitSource?.diff; const toolCallPreview = @@ -588,7 +587,9 @@ export default function DiffPanel({ : selectedTurn ? shownSections.length > 0 ? shownSections.map((section) => section.diff).join("\n") - : activeCheckpointDiff.data?.diff + : activeCheckpointDiff.error === null + ? activeCheckpointDiff.data?.diff + : undefined : gitDiff; const isSelectedPatchTruncated = toolCallPreview?.truncated === true || @@ -728,6 +729,7 @@ export default function DiffPanel({ remainingFileCount, loadNextFiles, } = useReviewFilePatches({ + reviewKey: collapseScopeKey, environmentId: activeThread?.environmentId, cwd: branchDiffPreview.data?.cwd, source: lazySource, @@ -1774,7 +1776,7 @@ export default function DiffPanel({ projectId={activeThread?.projectId} viewerRef={attachCodeView} onScroll={codeViewScroll.onScroll} - codeViewKey={reviewDiffViewerKey(codeViewMountKey, lazySource ? filePatchFamily : null)} + codeViewKey={`${codeViewMountKey}:${filePatchFamily}`} className="h-full min-h-0 overflow-auto" files={codeViewFiles} pendingFiles={pendingFiles} diff --git a/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts b/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts deleted file mode 100644 index 059705dc26f77..0000000000000 --- a/apps/web/src/components/diffs/reviewFileDiffRetention.test.ts +++ /dev/null @@ -1,109 +0,0 @@ -import type { FileDiffMetadata } from "@pierre/diffs"; -import { describe, expect, it } from "vite-plus/test"; - -import { - isPendingReviewFile, - retainLoadedReviewFiles, - retainedReviewFile, - reviewDiffViewerKey, - reviewPatchIndicesForFamily, - reviewSnapshotFamily, - reviewSnapshotScope, -} from "./reviewFileDiffRetention"; - -const identity = { - environmentId: "env-1", - cwd: "/repo", - kind: "working-tree", - baseRef: "HEAD", - ignoreWhitespace: false, -}; - -function file(name: string, cacheKey?: string): FileDiffMetadata { - return { name, ...(cacheKey ? { cacheKey } : {}) } as FileDiffMetadata; -} - -describe("review snapshot identity", () => { - 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 74f4e4a88e03c..8c3b3c5b05d2a 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: { @@ -267,6 +272,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) => @@ -286,6 +292,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 8c8162e195e33..761cb10cbf7b2 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -19,7 +19,7 @@ import type { FileDiffMetadata, SelectedLineRange, } from "@pierre/diffs"; -import type { CodeViewAnnotationArgs, CodeViewDiffItem } from "@pierre/diffs/react"; +import type { CodeViewAnnotationArgs, CodeViewDiffItem, CodeViewHandle } from "@pierre/diffs/react"; import type { EnvironmentId, PullRequestDetailView, @@ -49,8 +49,7 @@ import { ChevronsDownUpIcon, ChevronsUpDownIcon, } from "lucide-react"; -import { RegistryContext, useAtomRefresh, useAtomValue } from "@effect/atom-react"; -import { AsyncResult, Atom } from "effect/reactivity"; +import { RegistryContext } from "@effect/atom-react"; import * as Schema from "effect/Schema"; import { createContext, @@ -226,6 +225,7 @@ const NO_SLICES: ReadonlyArray = []; const sliceParses = new WeakMap>(); // Cached parses keep a raw patch's reason untranslated; the view resolves it in the UI language. const keepMessage = ((message: string) => message) as MessageResolver; +const fileContentVersions = new WeakMap(); function parseSlice(slice: DiffSlice, cacheKey: string, ignoreWhitespace: boolean) { let parses = sliceParses.get(slice.source); @@ -534,7 +534,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 @@ -553,11 +557,12 @@ function PullRequestCodeTab({ setFolds(EMPTY_PULL_REQUEST_DIFF_FOLDS); 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, @@ -574,7 +579,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 || @@ -592,9 +597,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 ( @@ -617,15 +632,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); @@ -671,46 +693,69 @@ function PullRequestCodeTab({ const refreshWithheldFiles = useEffectEvent(() => { for (const path of withheldPaths) atomRegistry.refresh(withheldFileDiff(path)); }); - // 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. - setAppliedPage(null); - refreshFirstDiffPage(); - refreshWithheldFiles(); - 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(); + 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 }) }, + }), + ); refreshWithheldFiles(); + setAppliedPage(null); + // 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, + refreshWithheldFiles, + ]); 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); @@ -890,7 +935,7 @@ function PullRequestCodeTab({ : null; const annotatedFiles = files.map((fileDiff) => { - const fileKey = buildFileDiffRenderKey(fileDiff); + const fileKey = buildFileDiffIdentityKey(fileDiff); const path = resolveFileDiffPath(fileDiff); const groups = new Map(); // Stacked view draws a context row once, so its old side shares the new side's annotation. @@ -1033,6 +1078,11 @@ function PullRequestCodeTab({ const items: CodeViewDiffItem[] = annotatedFiles.map( ({ fileKey, foldKey, path, fileDiff, annotations, annotationsVersion }) => { const group = foldGroup(path); + let contentVersion = fileContentVersions.get(fileDiff); + if (contentVersion === undefined) { + contentVersion = buildFileDiffContentVersion(fileDiff); + fileContentVersions.set(fileDiff, contentVersion); + } const collapsed = isFileDiffCollapsed( foldKey, group.override, @@ -1048,7 +1098,9 @@ function PullRequestCodeTab({ fileDiff, annotations, collapsed, - version: fnv1a32(`${collapsed ? "1" : "0"}:${viewedMark}:${annotationsVersion}`), + version: fnv1a32( + `${contentVersion}:${collapsed ? "1" : "0"}:${viewedMark}:${annotationsVersion}`, + ), }; }, ); @@ -1060,12 +1112,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) => { @@ -1149,7 +1208,7 @@ function PullRequestCodeTab({ if (!range || !canSelectLines || withheldRefetching) 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. @@ -2095,6 +2154,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={(node) => { // Pierre sets tabIndex -1 on this root and forwards no data attributes. diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts index b3d9a41a50a44..07ea743527d1b 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 55ef27f90a930..6b259b499ba15 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 a47c0068fe675..58e1692579969 100644 --- a/apps/web/src/lib/diffRendering.test.ts +++ b/apps/web/src/lib/diffRendering.test.ts @@ -9,6 +9,8 @@ import { getDiffLineStat, getRenderablePatch, getRenderablePatchSections, + retainLoadedReviewFiles, + retainedReviewFile, resolveFileDiffPath, resolveFileDiffPreviousPath, } from "./diffRendering"; @@ -268,48 +270,75 @@ describe("getRenderablePatch", () => { }); describe("diff file reconciliation", () => { - it("retains repeated blocks and their keys when content changes or other files appear", () => { - const parse = (blocks: [string, string][]) => { - const parsed = getRenderablePatch( - blocks - .map(([path, content]) => - [ - `diff --git a/${path} b/${path}`, - `--- a/${path}`, - `+++ b/${path}`, - "@@ -1 +1 @@", - "-before", - `+${content}`, - "", - ].join("\n"), - ) - .join(""), - ); - if (parsed?.kind !== "files") throw new Error("Expected file diffs"); - return parsed.files; - }; - const files = parse([ - ["same.ts", "first"], - ["same.ts", "second"], - ]); - const before = buildFileDiffEntries([...files, files[1]!]); - expect(before.map((entry) => entry.fileDiff)).toEqual([...files, files[1]]); - expect(new Set(before.map((entry) => entry.fileKey)).size).toBe(3); - expect(before[0]?.fileKey).toBe(buildFileDiffIdentityKey(files[0]!)); - expect(buildFileDiffEntries([...files, files[1]!])).toEqual(before); - - const after = buildFileDiffEntries( - parse([ - ["unrelated.ts", "new"], - ["same.ts", "first"], - ["same.ts", "updated second"], - ["same.ts", "second"], - ]), - ).slice(1); - expect(after.map((entry) => entry.fileKey)).toEqual(before.map((entry) => entry.fileKey)); - expect(after[0]?.fileVersion).toBe(before[0]?.fileVersion); - expect(after[1]?.fileVersion).not.toBe(before[1]?.fileVersion); - expect(after[2]?.fileVersion).toBe(before[2]?.fileVersion); + 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", () => { diff --git a/apps/web/src/lib/diffRendering.ts b/apps/web/src/lib/diffRendering.ts index 518022758ab80..221f1a9d24b34 100644 --- a/apps/web/src/lib/diffRendering.ts +++ b/apps/web/src/lib/diffRendering.ts @@ -249,6 +249,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 ac573c2ff2389ede07d485b3af2882aefe6d66a4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 21:01:16 +0000 Subject: [PATCH 05/19] fix(web): retain requested review paths after a failed refresh --- apps/web/src/components/diffs/useReviewFilePatches.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts index 8c3b3c5b05d2a..b58f4d129eb15 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, From ccf64f80d7604fd3d851e0a072b13f29c87b67b9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 21:24:25 +0000 Subject: [PATCH 06/19] fix(web): discard review drafts when their file changes --- .../pullRequest/PullRequestCodeTab.tsx | 103 +++++++++++++----- 1 file changed, 73 insertions(+), 30 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 761cb10cbf7b2..4438435ce8ae1 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -68,6 +68,7 @@ import { m, type MessageResolver } from "~/i18n/messages"; 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"; @@ -227,6 +228,15 @@ const sliceParses = new WeakMap message) as MessageResolver; 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) { @@ -391,9 +401,9 @@ interface MutableAnnotationGroup { } interface DraftAnchor { - // A refresh re-keys every file's render key, so the draft finds its file by identity and content. - readonly identityKey: string; - readonly fileVersion: number; + readonly fileKey: string; + readonly contentVersion: number; + readonly scopeKey: string; readonly path: string; readonly oldPath: string | null; readonly position: PullRequestReviewPosition; @@ -525,6 +535,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); @@ -882,20 +894,31 @@ function PullRequestCodeTab({ : loadedFiles; const hiddenTestFileCount = loadedFiles.length - files.length; const nextCursor = loadedSlices.at(-1)?.nextCursor ?? null; - const draftFile = - draft === null - ? undefined - : loadedFiles.find((file) => buildFileDiffIdentityKey(file) === draft.identityKey); - const draftFileKey = - draftFile !== undefined && buildFileDiffContentVersion(draftFile) === draft?.fileVersion - ? buildFileDiffRenderKey(draftFile) - : null; - if (draft !== null && draftFileKey === null) { - // Never attach the draft to lines of a changed file; one missing mid-refresh may come back. - if (draftFile !== undefined || (loadedSlices.length > 0 && nextCursor === null)) { - setDraft(null); - setSelectedLines(null); - } + const contentVersions = new Map( + loadedFiles.map((file) => [buildFileDiffIdentityKey(file), fileContentVersion(file)]), + ); + const isCurrentFile = (fileKey: string, version: number, anchorScopeKey: string) => { + const file = loadedFiles.find((candidate) => buildFileDiffIdentityKey(candidate) === fileKey); + if (file === undefined || anchorScopeKey !== scopeKey || contentVersions.get(fileKey) !== version) + return false; + const path = resolveFileDiffPath(file); + const group = foldGroup(path); + return !isFileDiffCollapsed( + buildFileDiffFoldKey(file), + group.override, + group.choices, + filesViewedEnabled && isFileViewed(path) && !isFileViewedStale(path), + ); + }; + // 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( @@ -976,7 +999,7 @@ function PullRequestCodeTab({ groupAt(anchor.side, anchor.line).pending.push(comment); } } - if (draft !== null && draftFileKey === fileKey) { + if (draft !== null && draft.fileKey === fileKey) { const anchor = getReviewPositionAnchor(draft.position); groupAt(anchor.side, anchor.line).draft = true; } @@ -1078,11 +1101,7 @@ function PullRequestCodeTab({ const items: CodeViewDiffItem[] = annotatedFiles.map( ({ fileKey, foldKey, path, fileDiff, annotations, annotationsVersion }) => { const group = foldGroup(path); - let contentVersion = fileContentVersions.get(fileDiff); - if (contentVersion === undefined) { - contentVersion = buildFileDiffContentVersion(fileDiff); - fileContentVersions.set(fileDiff, contentVersion); - } + const contentVersion = fileContentVersion(fileDiff); const collapsed = isFileDiffCollapsed( foldKey, group.override, @@ -1209,7 +1228,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); @@ -1222,8 +1246,9 @@ function PullRequestCodeTab({ resolveDiffReviewAnchor(file, range, rangeComments); if (anchor === null) return; setDraft({ - identityKey: buildFileDiffIdentityKey(file), - fileVersion: buildFileDiffContentVersion(file), + fileKey: item.id, + contentVersion: fileContentVersion(file), + scopeKey, path, oldPath: previousPath === path ? null : previousPath, ...anchor, @@ -1231,21 +1256,30 @@ 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 = - draftFile === undefined || draftFileKey === null + file === undefined ? null : buildDiffReviewComment({ - id: `pull-request-selection:${diffReviewSelectionId(draftFileKey, anchor.range)}`, + id: `pull-request-selection:${diffReviewSelectionId(anchor.fileKey, anchor.range)}`, sectionId: `pull-request:${detail.number}`, // oxlint-disable-next-line t3code/no-untranslated-copy -- context title sent to the agent sectionTitle: `PR #${detail.number} review`, filePath: anchor.path, - fileDiff: draftFile, + fileDiff: file, range: anchor.range, text, }); @@ -1507,6 +1541,7 @@ function PullRequestCodeTab({ setSelectedLines(null); }} onComment={(body) => { + if (!isCurrentDraft(draft)) return; if (!canCommentOnLines) { if (onAddToAgentSelection) { finishSelection(draft, body, (comment) => @@ -2164,7 +2199,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 4d49fb9426da531428a1fe78a308ef8a3e44e4a2 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Sat, 24 Oct 2026 14:17:21 -0400 Subject: [PATCH 07/19] 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 04bf41dcb34d3..628531c2a320a 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -719,7 +719,6 @@ export default function DiffPanel({ ...matchingBaseRefChoices.map(valueForBaseRefChoice), ]; const { - scope: filePatchScope, family: filePatchFamily, isPending: areFilePatchesPending, fileStates, From 694c4d37ab836445b77b23d1c53b255321753d52 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Mon, 26 Oct 2026 09:22:42 -0400 Subject: [PATCH 08/19] fix(review): allow agent selection without review posting access --- apps/web/src/components/pullRequest/PullRequestCodeTab.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 4438435ce8ae1..7e0e1c66fad60 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -1260,7 +1260,8 @@ function PullRequestCodeTab({ const isCurrentDraft = useStableCallback( (anchor: DraftAnchor) => draft === anchor && - canCommentOnLines && + canSelectLines && + !withheldRefetching && isCurrentFile(anchor.fileKey, anchor.contentVersion, anchor.scopeKey), ); const finishSelection = ( From 2ed7d765240429a97e566ac26337149b07334290 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Tue, 27 Oct 2026 15:11:24 -0400 Subject: [PATCH 09/19] 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 b58f4d129eb15..7cebe989ef31d 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 7e0e1c66fad60..10fb13216a36d 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -19,7 +19,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, @@ -548,9 +548,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 a139867a5503b496472e4197975e6ed2ecf1b1e6 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Wed, 28 Oct 2026 09:00:19 -0400 Subject: [PATCH 10/19] fix: preserve feature behavior on current upstream --- apps/web/src/components/pullRequest/PullRequestCodeTab.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 10fb13216a36d..e0f62bb0a7591 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -737,6 +737,7 @@ function PullRequestCodeTab({ refreshFilesViewed, refreshWithheldFiles, ]); + // 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 8ed1e4d7b5f68f18b97dc382d36f5f52465b3218 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Wed, 28 Oct 2026 10:23:45 -0400 Subject: [PATCH 11/19] fix(web): invalidate local diff drafts after content changes --- .../AnnotatableCodeView.browser.test.tsx | 8 +- .../diffs/AnnotatableCodeView.logic.test.ts | 63 +++++++++ .../diffs/AnnotatableCodeView.logic.ts | 30 ++++ .../components/diffs/AnnotatableCodeView.tsx | 128 ++++++++++-------- 4 files changed, 172 insertions(+), 57 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 b6a27bc213886..2ade72793dc7b 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: { @@ -75,7 +75,7 @@ function file( fileDiff, filePath: name, fileKey: buildFileDiffIdentityKey(fileDiff), - fileVersion, + fileVersion: buildFileDiffContentVersion(fileDiff), collapsed: false, }; } @@ -141,11 +141,11 @@ function setup(nextRange: SelectedLineRange = defaultRange): 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 38dda110232b1..79106efa354b8 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.tsx @@ -11,7 +11,13 @@ import type { EnvironmentId, ProjectId, ScopedThreadRef } from "@t3tools/contrac 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 { @@ -140,28 +147,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( @@ -221,10 +225,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, @@ -238,41 +242,44 @@ 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; - const selection = comment.selection ?? range; - setDraftText(""); - setDraft({ - fileKey: item.id, - filePath, - previousFilePath: resolveFileDiffPreviousPath(item.fileDiff), - annotation: { - side: annotationSide(selection), - lineNumber: selection.end, - metadata: { - entries: [ - { id, kind: "draft", range: selection, 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; + const selection = comment.selection ?? range; + setDraftText(""); + setDraft({ + fileKey: item.id, + fileVersion: file.fileVersion, + filePath, + previousFilePath: resolveFileDiffPreviousPath(item.fileDiff), + annotation: { + side: annotationSide(selection), + lineNumber: selection.end, + metadata: { + entries: [{ id, kind: "draft", range: selection, rangeLabel: comment.rangeLabel, text: "" }], + }, }, - }, - }); - }; + }); + }, + ); return ( @@ -290,7 +297,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, + ); + }} options={{ ...options, enableGutterUtility: draft === null, From 535336af16acff13599efc6183a64ab1f8e8a9c6 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Wed, 28 Oct 2026 10:58:46 -0400 Subject: [PATCH 12/19] fix(web): guard diff comments until refreshed content renders --- apps/web/src/components/DiffPanel.tsx | 1 + .../AnnotatableCodeView.browser.test.tsx | 304 +++++++++++------- .../components/diffs/AnnotatableCodeView.tsx | 52 ++- 3 files changed, 222 insertions(+), 135 deletions(-) diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index 628531c2a320a..c7294b3eb777c 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -1779,6 +1779,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 2ade72793dc7b..4e618de918a67 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,66 +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 defaultRange: SelectedLineRange = { start: 1, end: 1, side: "additions" }; -let range: SelectedLineRange = defaultRange; -let container: HTMLDivElement; +const range: SelectedLineRange = { start: 1, end: 1, side: "additions" }; function file( name: string, - fileVersion = 1, type: FileDiffMetadata["type"] = "change", prevName?: string, - patchBody = "@@ -1 +1 @@\n-old\n+new\n", + contents = "new", ) { const fileDiff = parsePatchFiles( - `diff --git a/${name} b/${name}\n--- a/${name}\n+++ b/${name}\n${patchBody}`, - { cacheKeyPrefix: 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`, + { cacheKeyPrefix: `${name}:${contents}` }, )[0]!.files[0]!; fileDiff.type = type; if (prevName) fileDiff.prevName = prevName; @@ -80,17 +35,25 @@ function file( }; } -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 = (selection: SelectedLineRange = range) => + act(() => { + // Invoke Pierre's own wrappers, which resolve their context from the latest item. + const { instance } = renderedFile(); + instance.options.onLineSelectionChange?.(selection); + instance.options.onGutterUtilityClick?.(selection); + }); + 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(nextRange: SelectedLineRange = defaultRange): Disposable { - const reset = () => { - range = defaultRange; - useComposerDraftStore.setState(useComposerDraftStore.getInitialState(), true); + 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, }; - reset(); - range = nextRange; - container = document.createElement("div"); - return { [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", @@ -167,28 +159,92 @@ describe("diff comment drafts", () => { selection: { ...range, endSide: "additions" }, }), ]); - expect(container.querySelector("p")?.textContent).toBe("Keep this draft"); }); - it("anchors upward-dragged drafts and submitted comments below the bottom line", async () => { - using _drafts = setup({ start: 3, end: 1, side: "additions" }); - const entry = file( - "upward.ts", - 1, - "change", - undefined, - "@@ -1,3 +1,3 @@\n one\n-two\n+TWO\n three\n", - ); - await using _screen = await render(view([entry]), { container }); - await click("Comment on upward.ts"); - expect(container.querySelector("[data-annotation]")?.getAttribute("data-annotation")).toBe( - "additions:3", - ); - await typeComment("Check lines 1-3"); - await click("Comment"); - expect(container.querySelector("[data-annotation]")?.getAttribute("data-annotation")).toBe( - "additions:3", + 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"), + }), + ]); + }); + + it("anchors upward-dragged drafts and submitted comments below the bottom line", async () => { + using test = setup(); + const entry = file("upward.ts", "change", undefined, "one\n+two\n+three"); + await using screen = await render(test.view([entry]), { container: test.container }); + await vi.waitFor(() => expect(test.renderedFile().instance.getLineIndex(3)).toBeDefined()); + await test.select({ start: 3, end: 1, side: "additions" }); + await test.typeComment("Check lines 1-3"); + await test.click("Comment"); expect(useComposerDraftStore.getState().getComposerDraft(target)?.reviewComments).toEqual([ expect.objectContaining({ filePath: "upward.ts", diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.tsx index 79106efa354b8..195fc9d69b636 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, CodeViewProps } from "@pierre/diffs/react"; import type { EnvironmentId, ProjectId, 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"; @@ -91,6 +90,7 @@ interface AnnotatableCodeViewProps { collapsed: boolean; }>; pendingFiles: ReadonlyArray; + isRefreshingDiff: boolean; sectionId: string; sectionTitle: string; composerDraftTarget: ScopedThreadRef | DraftId; @@ -123,6 +123,7 @@ export function AnnotatableCodeView({ codeViewKey, files, pendingFiles, + isRefreshingDiff, sectionId, sectionTitle, composerDraftTarget, @@ -156,8 +157,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(""); @@ -228,7 +240,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, @@ -250,15 +262,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: "", }); @@ -269,7 +281,7 @@ export function AnnotatableCodeView({ fileKey: item.id, fileVersion: file.fileVersion, filePath, - previousFilePath: resolveFileDiffPreviousPath(item.fileDiff), + previousFilePath: resolveFileDiffPreviousPath(file.fileDiff), annotation: { side: annotationSide(selection), lineNumber: selection.end, @@ -298,6 +310,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 @@ -315,11 +328,27 @@ export function AnnotatableCodeView({ }} options={{ ...options, - enableGutterUtility: draft === null, - enableLineSelection: draft === null, + enableGutterUtility: draft === null && !isRefreshingDiff, + enableLineSelection: draft === null && !isRefreshingDiff, onGutterUtilityClick: beginComment, - onItemCollapsedChange: (_item, nextItem) => - onFileCollapsedChange(nextItem.id, nextItem.collapsed === true), + onRevealSearchMatch: (item) => onRevealSearchMatch(item.id), + 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 @@ -355,6 +384,7 @@ export function AnnotatableCodeView({ onTextChange={setDraftText} onCancel={() => removeEntry(entry.id)} onComment={(text) => submitEntry(entry.id, text)} + pending={isRefreshingDiff} onDelete={() => removeEntry(entry.id)} /> ))} From 2f090289b1987cfda0539883678f4acb0d4a2274 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Wed, 28 Oct 2026 11:17:28 -0400 Subject: [PATCH 13/19] 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 4e618de918a67..3e369b61a25da 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("")); From 94189e13634ded6eeff2c11947fe2251cf903d8e Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Thu, 12 Nov 2026 09:31:43 -0500 Subject: [PATCH 14/19] fix(web): preserve current diff navigation and multi-block refreshes --- .../web/src/components/ChatView.logic.test.ts | 73 ++------------ apps/web/src/components/ChatView.logic.ts | 27 ------ .../useReviewFilePatches.browser.test.tsx | 17 +++- .../components/diffs/useReviewFilePatches.ts | 94 +++++++++++-------- .../pullRequest/PullRequestCodeTab.tsx | 56 +++++++++-- apps/web/src/lib/diffRendering.test.ts | 11 ++- apps/web/src/lib/diffRendering.ts | 2 +- 7 files changed, 130 insertions(+), 150 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 5d4032996c951..1b4e58c7bd499 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -1,5 +1,4 @@ import { shouldShowOpenInPicker } from "./chat/OpenInPicker.logic"; -import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { environmentContextRecord, environmentContextReference, @@ -74,7 +73,6 @@ import { resolveThreadSwitchTimeline, timelineHasEphemeralPreviewUrls, shouldApplyProactiveChangesScope, - shouldResetDiffSelectionToChanges, createLocalDispatchSnapshot, deriveCommittedServerUserMessageIds, deriveComposerSendState, @@ -454,69 +452,14 @@ 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 the active review when an agent updates or completes a turn", () => { - expect( - shouldResetDiffSelectionToChanges({ - diffOpen: true, - activeThreadRef: threadRef, - explicitThreadRef: null, - previous: openThread, - 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", () => { - 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); - expect( - shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff: false }), - ).toBe(true); - }); - - it("leaves a timeline turn selection in place", () => { - 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.each([false, true])( + "only selects Changes when opening a new review (already open: %s)", + (alreadyReviewingDiff) => { + expect(shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff })).toBe( + !alreadyReviewingDiff, + ); + }, + ); }); describe("resolveThreadSwitchTimeline", () => { diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index f802bcc659ab8..2be37ca97275f 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -222,33 +222,6 @@ export function shouldRetargetThreadPullRequestPanel( ); } -export interface DiffPanelOpenContext { - readonly threadKey: string | null; - readonly diffOpen: boolean; -} - -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 !== null && - input.explicitThreadRef.environmentId === input.activeThreadRef.environmentId && - input.explicitThreadRef.threadId === input.activeThreadRef.threadId - ) { - return false; - } - return !( - input.previous !== null && - input.previous.diffOpen && - input.previous.threadKey === input.threadKey - ); -} - export function shouldApplyProactiveChangesScope(input: { readonly openedDiff: boolean; readonly alreadyReviewingDiff: boolean; diff --git a/apps/web/src/components/diffs/useReviewFilePatches.browser.test.tsx b/apps/web/src/components/diffs/useReviewFilePatches.browser.test.tsx index 8311e9dffea88..64ccd1cf80b66 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.browser.test.tsx +++ b/apps/web/src/components/diffs/useReviewFilePatches.browser.test.tsx @@ -46,6 +46,7 @@ function source(paths: string[], hash: string): ReviewDiffPreviewSource { function Probe({ source }: { source: ReviewDiffPreviewSource }) { const files = useReviewFilePatches({ + reviewKey: "review", environmentId: EnvironmentId.make("environment"), cwd: "/project", source, @@ -116,14 +117,17 @@ it("retains automatic and explicitly opened files through metadata insertion, re await expect.element(screen.getByText("new", { exact: true })).toBeVisible(); await expect.element(screen.getByText("e.txt:loaded")).toBeVisible(); await expect.element(screen.getByText("f.txt:loaded")).toBeVisible(); + const refreshedAtom = [...state.patches] + .filter(([key]) => JSON.parse(key)[1] === "f.txt") + .at(-1)![1]; await act(() => registry.set( - atom, + refreshedAtom, AsyncResult.fail(new Error("refresh failed"), { previousSuccess: Option.some(loaded) }), ), ); - await expect.element(screen.getByText("f.txt:loaded")).toBeVisible(); - await act(() => registry.set(atom, loaded)); + await expect.element(screen.getByText("f.txt:pending")).toBeVisible(); + await act(() => registry.set(refreshedAtom, loaded)); await expect.element(screen.getByText("f.txt:loaded")).toBeVisible(); }); @@ -150,4 +154,11 @@ it("preserves deletion and addition patches for a file replaced by a symlink", a await expect.element(screen.getByText("deleted", { exact: true })).toBeVisible(); await expect.element(screen.getByText("new", { exact: true })).toBeVisible(); await expect.element(screen.getByText("Pending files: 0")).toBeVisible(); + await screen.rerender( + + + , + ); + await expect.element(screen.getByText("deleted", { exact: true })).toBeVisible(); + await expect.element(screen.getByText("new", { exact: true })).toBeVisible(); }); diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts index 7cebe989ef31d..c28bbb1c4fd66 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.ts +++ b/apps/web/src/components/diffs/useReviewFilePatches.ts @@ -61,7 +61,8 @@ export function useReviewFilePatches({ ignoreWhitespace, ]); const scope = JSON.stringify([family, source?.diffHash]); - const lazy = source?.truncated === true && source.files !== undefined; + const lazy = + (source?.truncated === true || source?.patchesDeferred === true) && source.files !== undefined; const files = lazy ? sortedReviewFiles(source) : []; const [requested, setRequested] = useState<{ family: string; paths: readonly string[] }>({ family, @@ -70,12 +71,19 @@ export function useReviewFilePatches({ const [retained, setRetained] = useState(() => ({ family, source, - byPath: + byKey: !lazy && preview?.kind === "files" ? retainLoadedReviewFiles(EMPTY_RETAINED_FILES, preview.files) : EMPTY_RETAINED_FILES, })); - const previousByPath = retained.family === family ? retained.byPath : EMPTY_RETAINED_FILES; + const previousByKey = retained.family === family ? retained.byKey : EMPTY_RETAINED_FILES; + const previousByPath = new Map(); + for (const file of previousByKey.values()) { + const path = resolveFileDiffPath(file); + const entries = previousByPath.get(path) ?? []; + entries.push(file); + previousByPath.set(path, entries); + } const requestedPaths = new Set([ ...files.slice(0, PAGE_SIZE).map((file) => file.path), ...(requested.family === family ? requested.paths : []), @@ -115,37 +123,42 @@ export function useReviewFilePatches({ : { family, paths: [...paths] }; }); const renderableFiles = lazy - ? files.map((file, index): FileDiffMetadata => { + ? files.flatMap((file, index): FileDiffMetadata[] => { const result = patches.get(index); const loaded = result?._tag === "Success" && result.value.patch?.kind === "files" - ? (result.value.patch.files.find( + ? result.value.patch.files.filter( (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`, - }, - }); + ) + : []; + if (loaded.length > 0) return loaded; + const pending = + result === undefined || + result._tag === "Initial" || + (result._tag === "Success" && result.waiting); + const previous = pending ? previousByPath.get(file.path) : undefined; + return (previous ?? [undefined]).map((previous) => + retainedReviewFile({ + loaded: null, + previous, + sameFamily: true, + pending, + 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)), @@ -153,22 +166,21 @@ export function useReviewFilePatches({ 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 }); + ? previousByKey + : retainLoadedReviewFiles(previousByKey, renderableFiles); + if (retained.family !== family || retained.source !== source || retained.byKey !== nextRetained) { + setRetained({ family, source, byKey: nextRetained }); } + const retainedPaths = new Set( + renderableFiles.filter((file) => !file.cacheKey?.endsWith(":pending")).map(resolveFileDiffPath), + ); const readyFilePaths = new Set( files .filter((_, index) => { const result = patches.get(index); return ( (result !== undefined && result._tag !== "Initial") || - !renderableFiles[index]?.cacheKey?.endsWith(":pending") + retainedPaths.has(files[index]!.path) ); }) .map((file) => file.path), @@ -204,10 +216,10 @@ export function useReviewFilePatches({ fileStates, isPending: [...patches.values()].some((patch) => patch._tag === "Initial" || patch.waiting), retry, - requestFile: (index: number) => requestFiles([index]), + requestFile: (path: string) => requestFiles([files.findIndex((file) => file.path === path)]), readyFilePaths, renderableFiles, - settledFileCount, + remainingFileCount: lazy ? files.length - settledFileCount : 0, loadNextFiles: () => { if (queries.some(({ index }) => index === settledFileCount)) return; requestFiles(Array.from({ length: PAGE_SIZE }, (_, index) => settledFileCount + index)); diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index e0f62bb0a7591..404d48d42b994 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -120,7 +120,7 @@ import { FileExplorerPane } from "../files/FileExplorerPane"; import { useCodeViewFileReveal } from "../diffs/useCodeViewFileReveal"; import { useHideTestFiles } from "../diffs/useHideTestFiles"; import { diffFileTreeEntries } from "../diffs/diffFileTree.logic"; -import { StyledDiffCodeView } from "../diffs/StyledDiffCodeView"; +import { StyledDiffCodeView, type StyledDiffCodeViewOptions } from "../diffs/StyledDiffCodeView"; import { Button, InlineButton } from "../ui/button"; import { Checkbox } from "../ui/checkbox"; import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; @@ -292,7 +292,7 @@ function diffModel( ? parsed.files.map( (file, index) => [ - buildFileDiffRenderKey(file), + buildFileDiffIdentityKey(file), getDiffLineStat([parsed.sourceFiles[index] ?? file]), ] as const, ) @@ -540,6 +540,7 @@ function PullRequestCodeTab({ } | null>(null); const [draft, setDraft] = useState(null); const [threadPending, setThreadPending] = useState(false); + const renderedFiles = useRef(new Map()); const [orphansOpen, setOrphansOpen] = useState(false); // Scope slices before reset to prevent using a previous pull request's data or cursor. const [sliceState, setSliceState] = useState<{ @@ -891,10 +892,21 @@ function PullRequestCodeTab({ }) : loadedFiles; const hiddenTestFileCount = loadedFiles.length - files.length; + const foldsAsViewed = (path: string) => + filesViewedEnabled && isFileViewed(path) && !isFileViewedStale(path); const nextCursor = loadedSlices.at(-1)?.nextCursor ?? null; const contentVersions = new Map( loadedFiles.map((file) => [buildFileDiffIdentityKey(file), fileContentVersion(file)]), ); + const isCurrentFileRendered = (fileKey: string) => { + const rendered = renderedFiles.current.get(fileKey); + return ( + !diffQuery.isPending && + sliceState.refreshing === null && + rendered !== undefined && + contentVersions.get(fileKey) === fileContentVersion(rendered) + ); + }; const isCurrentFile = (fileKey: string, version: number, anchorScopeKey: string) => { const file = loadedFiles.find((candidate) => buildFileDiffIdentityKey(candidate) === fileKey); if (file === undefined || anchorScopeKey !== scopeKey || contentVersions.get(fileKey) !== version) @@ -1094,8 +1106,6 @@ function PullRequestCodeTab({ ); - const foldsAsViewed = (path: string) => - filesViewedEnabled && isFileViewed(path) && !isFileViewedStale(path); const items: CodeViewDiffItem[] = annotatedFiles.map( ({ fileKey, foldKey, path, fileDiff, annotations, annotationsVersion }) => { const group = foldGroup(path); @@ -1196,7 +1206,13 @@ function PullRequestCodeTab({ setFoldChoices(path, (current) => withFileDiffFold(current, buildFileDiffFoldKey(item.fileDiff), false), ); - if (range) setSelectedLines({ id: item.id, range }); + if (range) + setSelectedLines({ + id: item.id, + range, + contentVersion: fileContentVersion(item.fileDiff), + scopeKey, + }); codeViewReveal.request(item.id, range); }; @@ -1228,6 +1244,7 @@ function PullRequestCodeTab({ const file = files.find((candidate) => buildFileDiffIdentityKey(candidate) === item.id); if ( !file || + !isCurrentFileRendered(item.id) || item.collapsed === true || !isCurrentFile(item.id, fileContentVersion(item.fileDiff), scopeKey) ) @@ -1260,6 +1277,7 @@ function PullRequestCodeTab({ draft === anchor && canSelectLines && !withheldRefetching && + isCurrentFileRendered(anchor.fileKey) && isCurrentFile(anchor.fileKey, anchor.contentVersion, anchor.scopeKey), ); const finishSelection = ( @@ -1370,13 +1388,24 @@ function PullRequestCodeTab({ new Error(m(msg("Unchanged lines are unavailable in a file with cut lines."))), ) : loadDiffFiles(fileDiff), - enableGutterUtility: canSelectLines && draft === null && !withheldRefetching, - enableLineSelection: canSelectLines && draft === null && !withheldRefetching, + enableGutterUtility: + canSelectLines && draft === null && !withheldRefetching && !diffQuery.isPending && sliceState.refreshing === null, + enableLineSelection: + canSelectLines && draft === null && !withheldRefetching && !diffQuery.isPending && sliceState.refreshing === null, onGutterUtilityClick: beginComment, onLineSelectionEnd: beginComment, - onPostRender: (fileContainer: HTMLElement, instance: object) => { + onPostRender: (fileContainer, instance, phase, context) => { + if (context.type === "diff") { + if (phase === "unmount") renderedFiles.current.delete(context.item.id); + else { + const { fileDiff } = context.instance.__getNextRenderData({ + fileDiff: context.item.fileDiff, + lineAnnotations: undefined, + }); + renderedFiles.current.set(context.item.id, fileDiff); + } + } codeViewReveal.flush(); - // Containers are reused across files, so the flag is set or cleared on every render. fileContainer.toggleAttribute( "data-cut-lines", "fileDiff" in instance && cutFiles.has(instance.fileDiff), @@ -1396,7 +1425,13 @@ function PullRequestCodeTab({ ), ); }, - }; + onRevealSearchMatch: (item: CodeViewItem) => { + if (item.type !== "diff") return; + setFoldChoices(resolveFileDiffPath(item.fileDiff), (choices) => + withFileDiffFold(choices, buildFileDiffFoldKey(item.fileDiff), false), + ); + }, + } satisfies StyledDiffCodeViewOptions; const runThreadCommand = async ( label: string, @@ -2199,6 +2234,7 @@ function PullRequestCodeTab({ items={items} selectedLines={selectedLines} onSelectedLinesChange={(selection) => { + if (selection !== null && !isCurrentFileRendered(selection.id)) return; const contentVersion = selection === null ? undefined : contentVersions.get(selection.id); setSelectedLines( diff --git a/apps/web/src/lib/diffRendering.test.ts b/apps/web/src/lib/diffRendering.test.ts index 58e1692579969..ad97844310a89 100644 --- a/apps/web/src/lib/diffRendering.test.ts +++ b/apps/web/src/lib/diffRendering.test.ts @@ -300,7 +300,7 @@ describe("diff file reconciliation", () => { const previous = retainLoadedReviewFiles(new Map(), [loaded]); const pending = retainedReviewFile({ loaded: null, - previous: previous.get("app.ts"), + previous: previous.get(buildFileDiffIdentityKey(loaded)), placeholder, sameFamily: true, pending: true, @@ -328,8 +328,11 @@ describe("diff file reconciliation", () => { pending: false, }); expect(failed.hunks).toEqual([]); - const retained = retainLoadedReviewFiles(new Map([["app.ts", loaded]]), [failed]); - expect(retained.has("app.ts")).toBe(false); + const retained = retainLoadedReviewFiles( + new Map([[buildFileDiffIdentityKey(loaded), loaded]]), + [failed], + ); + expect(retained.has(buildFileDiffIdentityKey(loaded))).toBe(false); const switched = retainedReviewFile({ loaded: null, @@ -388,6 +391,8 @@ describe("diff file reconciliation", () => { expect(buildFileDiffIdentityKey(deleted)).not.toBe(buildFileDiffIdentityKey(added)); expect(new Set(parsed.files.map(buildFileDiffIdentityKey)).size).toBe(parsed.files.length); + const retained = retainLoadedReviewFiles(new Map(), parsed.files); + expect([...retained.values()]).toEqual([deleted, added]); }); it("keeps identities stable and versions local to the changed file", () => { diff --git a/apps/web/src/lib/diffRendering.ts b/apps/web/src/lib/diffRendering.ts index 221f1a9d24b34..d0c47b073489d 100644 --- a/apps/web/src/lib/diffRendering.ts +++ b/apps/web/src/lib/diffRendering.ts @@ -275,7 +275,7 @@ export function retainLoadedReviewFiles( const next = new Map( files .filter((file) => !file.cacheKey?.endsWith(":pending")) - .map((file) => [resolveFileDiffPath(file), file]), + .map((file) => [buildFileDiffIdentityKey(file), file]), ); return next.size === previous.size && [...next].every(([path, file]) => previous.get(path) === file) From a4d649f9c1dca90dfee2c0b0768706f7d076d2ac Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Thu, 12 Nov 2026 09:36:18 -0500 Subject: [PATCH 15/19] refactor(web): keep proactive review selection guard local --- apps/web/src/components/ChatView.logic.test.ts | 12 ------------ apps/web/src/components/ChatView.logic.ts | 7 ------- apps/web/src/hooks/useProactivePanels.ts | 3 +-- 3 files changed, 1 insertion(+), 21 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 1b4e58c7bd499..9f9399ed58231 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -72,7 +72,6 @@ import { resetHeldThreadTimeline, resolveThreadSwitchTimeline, timelineHasEphemeralPreviewUrls, - shouldApplyProactiveChangesScope, createLocalDispatchSnapshot, deriveCommittedServerUserMessageIds, deriveComposerSendState, @@ -451,17 +450,6 @@ describe("deriveCommittedServerUserMessageIds", () => { }); }); -describe("diff selection across agent turns", () => { - it.each([false, true])( - "only selects Changes when opening a new review (already open: %s)", - (alreadyReviewingDiff) => { - expect(shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff })).toBe( - !alreadyReviewingDiff, - ); - }, - ); -}); - describe("resolveThreadSwitchTimeline", () => { afterEach(() => { resetHeldThreadTimeline(); diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 2be37ca97275f..65d93da19b08e 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -222,13 +222,6 @@ export function shouldRetargetThreadPullRequestPanel( ); } -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/hooks/useProactivePanels.ts b/apps/web/src/hooks/useProactivePanels.ts index d2b68f101f5f3..5f1a2840abf60 100644 --- a/apps/web/src/hooks/useProactivePanels.ts +++ b/apps/web/src/hooks/useProactivePanels.ts @@ -10,7 +10,6 @@ import { import type { TurnDiffSummary } from "../types"; import { observeProactivePanelUserChoice, - shouldApplyProactiveChangesScope, resolveProactiveTurnDiffAction, shouldOpenProactivePullRequest, shouldOpenProactiveTurnDiff, @@ -187,7 +186,7 @@ export function useProactivePanels(input: { ) { return; } - if (shouldApplyProactiveChangesScope({ openedDiff: true, alreadyReviewingDiff })) { + if (!alreadyReviewingDiff) { useDiffPanelStore.getState().selectGitScope(threadRef, "branch"); } onDiffPanelOpen?.(); From a46218dd6337399c347370f9f6a29ed77fed5b39 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Thu, 12 Nov 2026 10:48:01 -0500 Subject: [PATCH 16/19] fix(web): ignore stale pull request refresh cursors --- .../PullRequestCodeTab.browser.test.tsx | 70 +++++++++++-------- .../pullRequest/PullRequestCodeTab.tsx | 4 +- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx index 68c88f824861f..bcd48e6a22666 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx @@ -1,5 +1,7 @@ import { sha256 } from "@noble/hashes/sha2"; import { bytesToHex } from "@noble/hashes/utils"; +import { RegistryContext } from "@effect/atom-react"; +import { AsyncResult, AtomRegistry } from "effect/reactivity"; import { EnvironmentId, ProjectId, @@ -39,7 +41,7 @@ const diffAtoms = await vi.hoisted(async () => { truncated: false, nextCursor: null, }; - const pagedDiffAtom = Atom.family((shortPages: boolean) => { + const pagesFor = (shortPages: boolean): Record => { const pages: Record = { first: { patch: Array.from({ length: shortPages ? 2 : 40 }, (_, index) => @@ -54,8 +56,17 @@ const diffAtoms = await vi.hoisted(async () => { nextCursor: null, }, }; - return Atom.family((cursor: string) => Atom.make(AsyncResult.success(pages[cursor]!))); + return pages; + }; + const pageResultsFor = Atom.family((shortPages: boolean) => { + const pages = pagesFor(shortPages); + return Atom.family((cursor: string) => + Atom.make>(AsyncResult.success(pages[cursor]!)), + ); }); + const pagedDiffAtom = Atom.family((shortPages: boolean) => + Atom.family((cursor: string) => Atom.make((get) => get(pageResultsFor(shortPages)(cursor)))), + ); return { // An environment query whose refetch returns equal content hands back the previous value // itself, so a refresh publishes a new result around the very same page. @@ -78,9 +89,11 @@ const diffAtoms = await vi.hoisted(async () => { }), ), pagedDiffAtom, + pageResults: pageResultsFor(false), + pages: pagesFor(false), }; }); -const { diffAtom, whitespaceOnlyDiffAtom, onlyTestFilesDiffAtom, testFileDiffAtom, pagedDiffAtom } = +const { diffAtom, whitespaceOnlyDiffAtom, onlyTestFilesDiffAtom, testFileDiffAtom, pagedDiffAtom, pageResults, pages } = diffAtoms; const mocks = vi.hoisted(() => ({ @@ -91,6 +104,7 @@ const mocks = vi.hoisted(() => ({ whitespaceOnly: false, refreshed: false, diffCommits: [] as Array, + diffCursors: [] as Array, viewedPaths: null as ReadonlySet | null, viewedListeners: new Set<() => void>(), })); @@ -139,6 +153,7 @@ vi.mock(import("~/state/pullRequests"), async (original) => { ...actual.pullRequestEnvironment, diff: (({ input }: { input: { cursor?: string; commit?: string } }) => { mocks.diffCommits.push(input.commit); + mocks.diffCursors.push(input.cursor); if (mocks.paged) return pagedDiffAtom(mocks.shortPages)(input.cursor ?? "first"); if (mocks.onlyTestFiles) return onlyTestFilesDiffAtom; if (mocks.whitespaceOnly) return whitespaceOnlyDiffAtom; @@ -215,6 +230,7 @@ function setup({ paged }: { paged: boolean }) { mocks.whitespaceOnly = false; mocks.refreshed = false; mocks.diffCommits = []; + mocks.diffCursors = []; mocks.viewedPaths = null; mocks.viewedListeners.clear(); } @@ -288,34 +304,32 @@ it("restores saved Viewed folds, follows cleared marks, and allows explicit expa await expect .element(screen.getByRole("button", { name: "Expand diff", exact: true })) .toHaveAttribute("aria-expanded", "false"); +}); - await screen.getByRole("button", { name: "Expand diff", exact: true }).click(); - await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); - screen.getByRole("checkbox", { name: "Viewed" }).element().focus(); - await userEvent.keyboard(" "); - await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).not.toBeChecked(); - await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); - screen.getByRole("checkbox", { name: "Viewed" }).element().focus(); - await userEvent.keyboard(" "); - await expect - .element(screen.getByRole("button", { name: "Expand diff", exact: true })) - .toHaveAttribute("aria-expanded", "false"); +it("restarts a refresh without advancing the cursor of the replaced page walk", async () => { + setup({ paged: true }); + const registry = AtomRegistry.make(); + using _registry = { [Symbol.dispose]: () => registry.dispose() }; + using _firstPage = { [Symbol.dispose]: registry.mount(pageResults("first")) }; + const content = (token: number) => ( + + {tab(token, { path: "late.test.ts", nonce: 1 })} + + ); + await using screen = await render(content(0), { container: pagedContainer() }); + await expect.element(screen.getByText("41 files", { exact: true })).toBeVisible(); - deliverViewedPaths(new Set()); - await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).not.toBeChecked(); - await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); - deliverViewedPaths(new Set(["a.ts"])); - await expect - .element(screen.getByRole("button", { name: "Expand diff", exact: true })) - .toHaveAttribute("aria-expanded", "false"); - await screen.getByRole("button", { name: "Expand all files", exact: true }).click(); - await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); + await act(() => registry.set(pageResults("first"), AsyncResult.initial())); + await screen.rerender(content(1)); + await act(async () => { + registry.set(pageResults("first"), AsyncResult.success(pages.first!)); + await screen.rerender(content(2)); + }); - await screen.rerender(
); - await screen.rerender(tab(1)); - await expect - .element(screen.getByRole("button", { name: "Expand diff", exact: true })) - .toHaveAttribute("aria-expanded", "false"); + await vi.waitFor(() => expect(mocks.diffCursors.at(-1)).toBe("page-2")); + await expect.element(screen.getByText("41 files", { exact: true })).toBeVisible(); + await screen.rerender(content(3)); + await expect.element(screen.getByText("41 files", { exact: true })).toBeVisible(); }); // regression: #13279 a Viewed answer that lands after a link reveals a file must not fold it diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 404d48d42b994..60e4e7332fe94 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -756,7 +756,9 @@ function PullRequestCodeTab({ }), ); // oxlint-disable-next-line react/set-state-in-effect -- Advance only after invalidating the cached next page. - setSliceState((current) => ({ ...current, cursor: last.nextCursor })); + setSliceState((current) => + current.refreshing?.at(-1) === last ? { ...current, cursor: last.nextCursor } : current, + ); }, [ sliceState.refreshing, cursor, From 2a26af71160af293b084b174863bc07cf79eca24 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Thu, 12 Nov 2026 11:33:38 -0500 Subject: [PATCH 17/19] fix(web): defer diff links until refresh completes --- .../pullRequest/PullRequestCodeTab.browser.test.tsx | 11 ++++++++--- .../src/components/pullRequest/PullRequestCodeTab.tsx | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx index bcd48e6a22666..18476357ba775 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx @@ -311,9 +311,11 @@ it("restarts a refresh without advancing the cursor of the replaced page walk", const registry = AtomRegistry.make(); using _registry = { [Symbol.dispose]: () => registry.dispose() }; using _firstPage = { [Symbol.dispose]: registry.mount(pageResults("first")) }; - const content = (token: number) => ( + const url = `${detail.url}/files#diff-${bytesToHex(sha256(new TextEncoder().encode("late.test.ts")))}R4-R5`; + const settled = vi.fn(); + const content = (token: number, revealUrl?: string) => ( - {tab(token, { path: "late.test.ts", nonce: 1 })} + {tab(token, { path: "late.test.ts", nonce: 1 }, revealUrl, settled)} ); await using screen = await render(content(0), { container: pagedContainer() }); @@ -321,13 +323,16 @@ it("restarts a refresh without advancing the cursor of the replaced page walk", await act(() => registry.set(pageResults("first"), AsyncResult.initial())); await screen.rerender(content(1)); + await screen.rerender(content(1, url)); + expect(settled).not.toHaveBeenCalled(); await act(async () => { registry.set(pageResults("first"), AsyncResult.success(pages.first!)); - await screen.rerender(content(2)); + await screen.rerender(content(2, url)); }); await vi.waitFor(() => expect(mocks.diffCursors.at(-1)).toBe("page-2")); await expect.element(screen.getByText("41 files", { exact: true })).toBeVisible(); + await vi.waitFor(() => expect(settled).toHaveBeenCalledWith(url)); await screen.rerender(content(3)); await expect.element(screen.getByText("41 files", { exact: true })).toBeVisible(); }); diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 60e4e7332fe94..37ffe0527009c 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -2010,6 +2010,7 @@ function PullRequestCodeTab({ const revealLink = useEffectEvent(() => { if (revealUrl === undefined) answeredLink.current = null; if (revealUrl === undefined || linkAnchor?.kind !== "diff") return; + if (sliceState.refreshing !== null) return; // The panel selects the link's commit after this tab's effects run, so the files on screen // can still be another commit's. const linkCommit = pullRequestLinkCommit(revealUrl); From 1b379510f1a290a8529bbaa064d656726a9560da Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Tue, 17 Nov 2026 10:33:27 -0500 Subject: [PATCH 18/19] fix(review): preserve current diff behavior while retaining refresh geometry --- .../web/src/components/ChatView.logic.test.ts | 83 ++++++++++++++++++- .../components/diffs/AnnotatableCodeView.tsx | 3 +- .../components/diffs/useReviewFilePatches.ts | 2 +- .../PullRequestCodeTab.browser.test.tsx | 28 +++++++ .../pullRequest/PullRequestCodeTab.tsx | 20 ++--- .../pullRequest/pullRequestDiff.logic.test.ts | 2 - apps/web/src/lib/diffRendering.test.ts | 44 ++++++++++ 7 files changed, 167 insertions(+), 15 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 9f9399ed58231..8a777ce0d7b95 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -1,4 +1,5 @@ -import { shouldShowOpenInPicker } from "./chat/OpenInPicker.logic"; +import { rewindToMessage } from "./chat/chatViewSend"; +import * as session from "../state/session"; import { environmentContextRecord, environmentContextReference, @@ -450,6 +451,86 @@ describe("deriveCommittedServerUserMessageIds", () => { }); }); +describe("hasCompactableConversation", () => { + const at = DateTime.makeUnsafe("2026-10-26T08:00:00.000Z"); + const runId = RunId.make("run:thread:thread-1:ordinal:1"); + const itemBase = (id: string, ordinal: number) => ({ + id: TurnItemId.make(id), + threadId, + runId, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal, + status: "completed" as const, + title: null, + startedAt: at, + completedAt: at, + updatedAt: at, + }); + const userMessage = (text: string): OrchestrationV2ProjectedTurnItem => ({ + position: 0, + visibility: "local", + sourceThreadId: threadId, + sourceItemId: TurnItemId.make("turn-item:user"), + item: { + ...itemBase("turn-item:user", 1), + createdBy: "user", + creationSource: "web", + type: "user_message", + messageId: MessageId.make("message-user"), + inputIntent: "turn_start", + text, + attachments: [], + }, + }); + const assistantMessage: OrchestrationV2ProjectedTurnItem = { + position: 1, + visibility: "local", + sourceThreadId: threadId, + sourceItemId: TurnItemId.make("turn-item:assistant"), + item: { + ...itemBase("turn-item:assistant", 2), + type: "assistant_message", + messageId: MessageId.make("message-assistant"), + text: "done", + streaming: false, + }, + }; + + it("counts a user message beyond the loaded page", () => { + expect( + hasCompactableConversation({ + visibleTurnItems: [assistantMessage], + hasMoreHistory: true, + latestUserMessageAt: "2026-10-26T08:00:00.000Z", + }), + ).toBe(true); + }); + + it("stays unavailable when the thread never had a user message", () => { + expect( + hasCompactableConversation({ + visibleTurnItems: [assistantMessage], + hasMoreHistory: true, + latestUserMessageAt: null, + }), + ).toBe(false); + }); + + it("does not count a lone /compact on a fully loaded thread", () => { + expect( + hasCompactableConversation({ + visibleTurnItems: [userMessage("/compact"), assistantMessage], + hasMoreHistory: false, + latestUserMessageAt: "2026-10-26T08:00:00.000Z", + }), + ).toBe(false); + }); +}); + describe("resolveThreadSwitchTimeline", () => { afterEach(() => { resetHeldThreadTimeline(); diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.tsx index 195fc9d69b636..b501117dd1759 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.tsx @@ -331,7 +331,8 @@ export function AnnotatableCodeView({ enableGutterUtility: draft === null && !isRefreshingDiff, enableLineSelection: draft === null && !isRefreshingDiff, onGutterUtilityClick: beginComment, - onRevealSearchMatch: (item) => onRevealSearchMatch(item.id), + onItemCollapsedChange: (_item, nextItem) => + onFileCollapsedChange(nextItem.id, nextItem.collapsed === true), onPostRender: (node, _instance, phase, context) => { if (context.type === "diff") { if (phase === "unmount") { diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts index c28bbb1c4fd66..bde0884005433 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.ts +++ b/apps/web/src/components/diffs/useReviewFilePatches.ts @@ -146,7 +146,7 @@ export function useReviewFilePatches({ placeholder: { name: file.path, ...(file.previousPath ? { prevName: file.previousPath } : {}), - type: file.previousPath ? "rename-changed" : "change", + type: file.changeType ?? (file.previousPath ? "rename-changed" : "change"), hunks: [], additionLines: [], deletionLines: [], diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx index 18476357ba775..0d18464530d15 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx @@ -304,6 +304,34 @@ it("restores saved Viewed folds, follows cleared marks, and allows explicit expa await expect .element(screen.getByRole("button", { name: "Expand diff", exact: true })) .toHaveAttribute("aria-expanded", "false"); + + await screen.getByRole("button", { name: "Expand diff", exact: true }).click(); + await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); + screen.getByRole("checkbox", { name: "Viewed" }).element().focus(); + await userEvent.keyboard(" "); + await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).not.toBeChecked(); + await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); + screen.getByRole("checkbox", { name: "Viewed" }).element().focus(); + await userEvent.keyboard(" "); + await expect + .element(screen.getByRole("button", { name: "Expand diff", exact: true })) + .toHaveAttribute("aria-expanded", "false"); + + deliverViewedPaths(new Set()); + await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).not.toBeChecked(); + await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); + deliverViewedPaths(new Set(["a.ts"])); + await expect + .element(screen.getByRole("button", { name: "Expand diff", exact: true })) + .toHaveAttribute("aria-expanded", "false"); + await screen.getByRole("button", { name: "Expand all files", exact: true }).click(); + await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); + + await screen.rerender(
); + await screen.rerender(tab(1)); + await expect + .element(screen.getByRole("button", { name: "Expand diff", exact: true })) + .toHaveAttribute("aria-expanded", "false"); }); it("restarts a refresh without advancing the cursor of the replaced page walk", async () => { diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index 37ffe0527009c..de560dde1023e 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -49,7 +49,8 @@ import { ChevronsDownUpIcon, ChevronsUpDownIcon, } from "lucide-react"; -import { RegistryContext } from "@effect/atom-react"; +import { RegistryContext, useAtomValue } from "@effect/atom-react"; +import { AsyncResult, Atom } from "effect/reactivity"; import * as Schema from "effect/Schema"; import { createContext, @@ -146,12 +147,8 @@ import { import { pullRequestCompletionScope } from "./PullRequestMarkdownTextarea"; import { getFileViewedRange, - isFileDiffCollapsed, isLineInFileDiff, - withFileDiffFold, retainReviewDiffSlices, - type DiffFoldOverride, - type FileDiffFoldChoices, } from "./pullRequestDiff.logic"; import { PullRequestDiffStat, PullRequestMetaLine } from "./pullRequestPresentation"; import { PullRequestDiffUnavailableState } from "./PullRequestDiffUnavailableState"; @@ -726,6 +723,7 @@ function PullRequestCodeTab({ ...retainReviewDiffSlices(current, scopeKey), refreshing: [], })); + refreshWithheldFiles(); refreshFilesViewed(); }, [ refreshToken, @@ -1408,11 +1406,13 @@ function PullRequestCodeTab({ } } codeViewReveal.flush(); - fileContainer.toggleAttribute( - "data-cut-lines", - "fileDiff" in instance && cutFiles.has(instance.fileDiff), - ); - if (withheldFiles.size > 0) markCutLineNotes(fileContainer); + if (phase !== "unmount") { + fileContainer.toggleAttribute( + "data-cut-lines", + "fileDiff" in instance && cutFiles.has(instance.fileDiff), + ); + if (withheldFiles.size > 0) markCutLineNotes(fileContainer); + } }, onItemCollapsedChange: ( _item: CodeViewItem, diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts index 07ea743527d1b..654a81b5ef3b5 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts @@ -3,9 +3,7 @@ import { describe, expect, it } from "vite-plus/test"; import { getFileViewedRange, - isFileDiffCollapsed, isLineInFileDiff, - withFileDiffFold, retainReviewDiffSlices, } from "./pullRequestDiff.logic"; diff --git a/apps/web/src/lib/diffRendering.test.ts b/apps/web/src/lib/diffRendering.test.ts index ad97844310a89..54aea52381d4c 100644 --- a/apps/web/src/lib/diffRendering.test.ts +++ b/apps/web/src/lib/diffRendering.test.ts @@ -270,6 +270,50 @@ describe("getRenderablePatch", () => { }); describe("diff file reconciliation", () => { + it("retains repeated blocks and their keys when content changes or other files appear", () => { + const parse = (blocks: [string, string][]) => { + const parsed = getRenderablePatch( + blocks + .map(([path, content]) => + [ + `diff --git a/${path} b/${path}`, + `--- a/${path}`, + `+++ b/${path}`, + "@@ -1 +1 @@", + "-before", + `+${content}`, + "", + ].join("\n"), + ) + .join(""), + ); + if (parsed?.kind !== "files") throw new Error("Expected file diffs"); + return parsed.files; + }; + const files = parse([ + ["same.ts", "first"], + ["same.ts", "second"], + ]); + const before = buildFileDiffEntries([...files, files[1]!]); + expect(before.map((entry) => entry.fileDiff)).toEqual([...files, files[1]]); + expect(new Set(before.map((entry) => entry.fileKey)).size).toBe(3); + expect(before[0]?.fileKey).toBe(buildFileDiffIdentityKey(files[0]!)); + expect(buildFileDiffEntries([...files, files[1]!])).toEqual(before); + + const after = buildFileDiffEntries( + parse([ + ["unrelated.ts", "new"], + ["same.ts", "first"], + ["same.ts", "updated second"], + ["same.ts", "second"], + ]), + ).slice(1); + expect(after.map((entry) => entry.fileKey)).toEqual(before.map((entry) => entry.fileKey)); + expect(after[0]?.fileVersion).toBe(before[0]?.fileVersion); + expect(after[1]?.fileVersion).not.toBe(before[1]?.fileVersion); + expect(after[2]?.fileVersion).toBe(before[2]?.fileVersion); + }); + const reviewFile = (line: string) => { const patch = getRenderablePatch( [ From 3219446998abef1e7342e28998183bbe89c5a452 Mon Sep 17 00:00:00 2001 From: Jake Leventhal Date: Sun, 29 Nov 2026 14:05:57 -0500 Subject: [PATCH 19/19] fix(review): retain withheld guards and viewed folds after refresh --- .../AnnotatableCodeView.browser.test.tsx | 2 +- .../components/diffs/AnnotatableCodeView.tsx | 4 ++- .../PullRequestCodeTab.browser.test.tsx | 20 +++++++++---- .../pullRequest/PullRequestCodeTab.tsx | 28 +++++++++++-------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx index 3e369b61a25da..eb48761f58fda 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx @@ -244,7 +244,7 @@ describe("diff comment drafts", () => { it("anchors upward-dragged drafts and submitted comments below the bottom line", async () => { using test = setup(); const entry = file("upward.ts", "change", undefined, "one\n+two\n+three"); - await using screen = await render(test.view([entry]), { container: test.container }); + await using _screen = await render(test.view([entry]), { container: test.container }); await vi.waitFor(() => expect(test.renderedFile().instance.getLineIndex(3)).toBeDefined()); await test.select({ start: 3, end: 1, side: "additions" }); await test.typeComment("Check lines 1-3"); diff --git a/apps/web/src/components/diffs/AnnotatableCodeView.tsx b/apps/web/src/components/diffs/AnnotatableCodeView.tsx index b501117dd1759..fad8590448d56 100644 --- a/apps/web/src/components/diffs/AnnotatableCodeView.tsx +++ b/apps/web/src/components/diffs/AnnotatableCodeView.tsx @@ -286,7 +286,9 @@ export function AnnotatableCodeView({ side: annotationSide(selection), lineNumber: selection.end, metadata: { - entries: [{ id, kind: "draft", range: selection, rangeLabel: comment.rangeLabel, text: "" }], + entries: [ + { id, kind: "draft", range: selection, rangeLabel: comment.rangeLabel, text: "" }, + ], }, }, }); diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx index 0d18464530d15..81b0852af251e 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx @@ -61,7 +61,9 @@ const diffAtoms = await vi.hoisted(async () => { const pageResultsFor = Atom.family((shortPages: boolean) => { const pages = pagesFor(shortPages); return Atom.family((cursor: string) => - Atom.make>(AsyncResult.success(pages[cursor]!)), + Atom.make>( + AsyncResult.success(pages[cursor]!), + ), ); }); const pagedDiffAtom = Atom.family((shortPages: boolean) => @@ -93,8 +95,15 @@ const diffAtoms = await vi.hoisted(async () => { pages: pagesFor(false), }; }); -const { diffAtom, whitespaceOnlyDiffAtom, onlyTestFilesDiffAtom, testFileDiffAtom, pagedDiffAtom, pageResults, pages } = - diffAtoms; +const { + diffAtom, + whitespaceOnlyDiffAtom, + onlyTestFilesDiffAtom, + testFileDiffAtom, + pagedDiffAtom, + pageResults, + pages, +} = diffAtoms; const mocks = vi.hoisted(() => ({ paged: false, @@ -622,12 +631,11 @@ it("keeps a later test file folded by hand through a refresh and bulk toggles wh await filter.click(); await expect.element(screen.getByText("1 / 2", { exact: true })).toBeVisible(); - // A refresh drops the loaded later page, so the bulk toggles below run without it. + // Keep the later page loaded while the test files are hidden. await screen.rerender(tab(1)); - await expect.element(screen.getByText("1 test file hidden", { exact: true })).toBeVisible(); + await expect.element(screen.getByText("2 test files hidden", { exact: true })).toBeVisible(); await screen.getByRole("button", { name: "Collapse all files", exact: true }).click(); await screen.getByRole("button", { name: "Expand all files", exact: true }).click(); - await loadNextPage(); await filter.click(); await expect.element(screen.getByText("4 files", { exact: true })).toBeVisible(); diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index de560dde1023e..fe65b06807041 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -89,7 +89,6 @@ import { isWorkspaceImagePreviewPath } from "@t3tools/shared/mediaPreview"; import { buildFileDiffContentVersion, buildFileDiffIdentityKey, - buildFileDiffRenderKey, fnv1a32, getDiffLineStat, getRenderablePatch, @@ -716,7 +715,6 @@ function PullRequestCodeTab({ input: { ...reference, ...(commit === null ? {} : { commit }) }, }), ); - refreshWithheldFiles(); setAppliedPage(null); // Stage a fresh walk while all displayed pages keep the reader's scroll geometry. setSliceState((current) => ({ @@ -726,6 +724,7 @@ function PullRequestCodeTab({ refreshWithheldFiles(); refreshFilesViewed(); }, [ + appliedRefresh, refreshToken, backgroundRefreshToken, environmentId, @@ -734,7 +733,6 @@ function PullRequestCodeTab({ scopeKey, registry, refreshFilesViewed, - refreshWithheldFiles, ]); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- invalidates the next cached environment diff page before advancing its refresh walk useEffect(() => { @@ -909,7 +907,11 @@ function PullRequestCodeTab({ }; const isCurrentFile = (fileKey: string, version: number, anchorScopeKey: string) => { const file = loadedFiles.find((candidate) => buildFileDiffIdentityKey(candidate) === fileKey); - if (file === undefined || anchorScopeKey !== scopeKey || contentVersions.get(fileKey) !== version) + if ( + file === undefined || + anchorScopeKey !== scopeKey || + contentVersions.get(fileKey) !== version + ) return false; const path = resolveFileDiffPath(file); const group = foldGroup(path); @@ -1389,9 +1391,17 @@ function PullRequestCodeTab({ ) : loadDiffFiles(fileDiff), enableGutterUtility: - canSelectLines && draft === null && !withheldRefetching && !diffQuery.isPending && sliceState.refreshing === null, + canSelectLines && + draft === null && + !withheldRefetching && + !diffQuery.isPending && + sliceState.refreshing === null, enableLineSelection: - canSelectLines && draft === null && !withheldRefetching && !diffQuery.isPending && sliceState.refreshing === null, + canSelectLines && + draft === null && + !withheldRefetching && + !diffQuery.isPending && + sliceState.refreshing === null, onGutterUtilityClick: beginComment, onLineSelectionEnd: beginComment, onPostRender: (fileContainer, instance, phase, context) => { @@ -1427,12 +1437,6 @@ function PullRequestCodeTab({ ), ); }, - onRevealSearchMatch: (item: CodeViewItem) => { - if (item.type !== "diff") return; - setFoldChoices(resolveFileDiffPath(item.fileDiff), (choices) => - withFileDiffFold(choices, buildFileDiffFoldKey(item.fileDiff), false), - ); - }, } satisfies StyledDiffCodeViewOptions; const runThreadCommand = async (