diff --git a/src/features/agents/session/ChangesTab.tsx b/src/features/agents/session/ChangesTab.tsx index 48b234a344..14863faaa9 100644 --- a/src/features/agents/session/ChangesTab.tsx +++ b/src/features/agents/session/ChangesTab.tsx @@ -339,109 +339,120 @@ function TaskChanges({ task, tabKey }: { task: AgentTask; tabKey: string }) { }); } }; - const items = useDiffItems(task, rows, grouped, scope); + // Running, the task is published again with every token count: its diffs only need these fields. + const { id: taskId, base, isolation, branch, cwd } = task; + const diffTask = useMemo( + () => ({ id: taskId, base, isolation, branch, cwd }), + [taskId, base, isolation, branch, cwd], + ); + const items = useDiffItems(diffTask, rows, grouped, scope); const diff = useRef(null); const busy = task.status === "starting" || task.status === "running"; - const fileTree = - changes === null ? ( - - ) : ( - - key={view} - aria-label="Changed files" - roots={rows} - getKey={(n) => n.key} - getLabel={(n) => - n.type === "file" - ? basename(n.change.path) - : n.type === "commit" - ? n.commit.summary - : n.type === "repo" - ? basename(n.root) - : n.label - } - getChildren={(n) => (n.type === "file" ? undefined : n.children)} - isExpandable={(n) => n.type !== "file" && n.children.length > 0} - defaultExpanded={[...(grouped ? ["group:uncommitted", "group:committed"] : []), ...repositoryKeys(rows)]} - activateExpandable={(n) => n.type === "commit"} - selected={selected} - onSelectionChange={(sel, lead) => { - if (lead?.type === "file" && !lead.commit) setSelected(sel); - // The diff follows the tree: a row shows its first file. - const first = lead && firstFile(lead); - if (first) diff.current?.reveal(first); - }} - onActivate={(n) => { - const first = firstFile(n); - if (first) diff.current?.reveal(first); - }} - renderNode={(n) => { - if (n.type === "group") { + // Memoized like the diffs: a task update (every token count while it runs) mustn't render every row again. + const loading = changes === null; + const fileTree = useMemo( + () => + loading ? ( + + ) : ( + + key={view} + aria-label="Changed files" + roots={rows} + getKey={(n) => n.key} + getLabel={(n) => + n.type === "file" + ? basename(n.change.path) + : n.type === "commit" + ? n.commit.summary + : n.type === "repo" + ? basename(n.root) + : n.label + } + getChildren={(n) => (n.type === "file" ? undefined : n.children)} + isExpandable={(n) => n.type !== "file" && n.children.length > 0} + defaultExpanded={[...(grouped ? ["group:uncommitted", "group:committed"] : []), ...repositoryKeys(rows)]} + activateExpandable={(n) => n.type === "commit"} + selected={selected} + onSelectionChange={(sel, lead) => { + if (lead?.type === "file" && !lead.commit) setSelection({ view, keys: sel }); + // The diff follows the tree: a row shows its first file. + const first = lead && firstFile(lead); + if (first) diff.current?.reveal(first); + }} + onActivate={(n) => { + const first = firstFile(n); + if (first) diff.current?.reveal(first); + }} + renderNode={(n) => { + if (n.type === "group") { + return ( + + + {n.label === "Uncommitted" ? Uncommitted : Committed} + + + {n.label === "Committed" + ? tx(n.count === 1 ? "1 commit" : "{count} commits").replace("{count}", String(n.count)) + : `${n.count} ${tx(n.count === 1 ? "file" : "files")}`} + + + {n.added > 0 && +{n.added}}{" "} + {n.removed > 0 && −{n.removed}} + + + ); + } + if (n.type === "repo") { + const files = n.children.flatMap((c) => (c.type === "file" ? [c.change] : [])); + const stats = statsOf(files); + return ( + + + {basename(n.root)} + {`${files.length} ${tx(files.length === 1 ? "file" : "files")}`} + + {stats.added > 0 && +{stats.added}}{" "} + {stats.removed > 0 && −{stats.removed}} + + + ); + } + if (n.type === "commit") { + const stats = statsOf(n.commit.files); + return ( + + + {n.commit.shortId} + {n.commit.summary} + {n.commit.timeMs != null && ( + {formatSentAt(n.commit.timeMs)} + )} + + {stats.added > 0 && +{stats.added}}{" "} + {stats.removed > 0 && −{stats.removed}} + + + ); + } return ( - - - {n.label === "Uncommitted" ? Uncommitted : Committed} - - - {n.label === "Committed" - ? tx(n.count === 1 ? "1 commit" : "{count} commits").replace("{count}", String(n.count)) - : `${n.count} ${tx(n.count === 1 ? "file" : "files")}`} + + + + {basename(n.change.path)} + {changeDir(diffTask, n.change)} - {n.added > 0 && +{n.added}}{" "} - {n.removed > 0 && −{n.removed}} + {n.change.added > 0 && +{n.change.added}}{" "} + {n.change.removed > 0 && −{n.change.removed}} ); - } - if (n.type === "repo") { - const files = n.children.flatMap((c) => (c.type === "file" ? [c.change] : [])); - const stats = statsOf(files); - return ( - - - {basename(n.root)} - {`${files.length} ${tx(files.length === 1 ? "file" : "files")}`} - - {stats.added > 0 && +{stats.added}}{" "} - {stats.removed > 0 && −{stats.removed}} - - - ); - } - if (n.type === "commit") { - const stats = statsOf(n.commit.files); - return ( - - - {n.commit.shortId} - {n.commit.summary} - {n.commit.timeMs != null && ( - {formatSentAt(n.commit.timeMs)} - )} - - {stats.added > 0 && +{stats.added}}{" "} - {stats.removed > 0 && −{stats.removed}} - - - ); - } - return ( - - - - {basename(n.change.path)} - - {changeDir(task, n.change)} - - {n.change.added > 0 && +{n.change.added}}{" "} - {n.change.removed > 0 && −{n.change.removed}} - - - ); - }} - /> - ); + }} + /> + ), + [loading, view, rows, grouped, selected, diffTask], + ); return (
; + /** Key of the first file row at or under `row`: what the diff scrolls to for it. */ function firstFile(row: ChangeRow): string | null { if (row.type === "file") return row.key; @@ -563,7 +577,7 @@ function fileRows(rows: readonly ChangeRow[]): Extract + before && before.length === kept.length && kept.every((item, i) => item === before[i]) ? before : kept, + ); previous.current = new Map(kept.map((item) => [item.key, item])); - setItems(kept); }); return () => { live = false; @@ -609,14 +626,14 @@ function useDiffItems( } /** The folder of a changed file, as the tree names it. */ -function changeDir(task: AgentTask, change: AgentChange): string { +function changeDir(task: Pick, change: AgentChange): string { if (!change.repo) return relativeDir(task.cwd, change.path); return change.relPath.includes("/") ? dirname(change.relPath) : ""; } /** Diff of `path` between the task's start (or its last turn's) and now. */ async function changeDiffData( - task: AgentTask, + task: DiffTask, path: string, scope: ChangesScope, kind?: AgentChange["kind"], @@ -653,7 +670,7 @@ export async function openChangeDiff(task: AgentTask, path: string, scope: Chang } /** Diff of `path` in the grouped view: `HEAD` ↔ the working file. */ -function uncommittedDiffData(task: AgentTask, change: AgentChange): DiffData { +function uncommittedDiffData(task: DiffTask, change: AgentChange): DiffData { const left: DiffData["left"] = change.kind === "added" || change.kind === "untracked" ? { type: "text", text: "", label: "(does not exist)", absent: true } @@ -674,14 +691,14 @@ function uncommittedDiffData(task: AgentTask, change: AgentChange): DiffData { } /** Registration needed before a diff reads revisions inside the task's own worktree. */ -async function ensureTaskRepo(task: AgentTask) { +async function ensureTaskRepo(task: DiffTask) { // A worktree is its own repository, outside the project's: `vcs_file_at` reads it only once // it is registered as an external log for this window. if (task.isolation === "worktree") await call(commands.vcsOpenExternalLog(task.cwd)).catch(() => undefined); } /** Diff of a file of one of the task's commits: `parent ↔ commit`. */ -function commitFileDiff(task: AgentTask, commit: AgentCommit, file: AgentChange): DiffData { +function commitFileDiff(task: DiffTask, commit: AgentCommit, file: AgentChange): DiffData { return commitFileDiffData( { id: commit.id, diff --git a/src/features/vcs/CombinedDiff.tsx b/src/features/vcs/CombinedDiff.tsx index 0839619535..861872b7f8 100644 --- a/src/features/vcs/CombinedDiff.tsx +++ b/src/features/vcs/CombinedDiff.tsx @@ -243,6 +243,8 @@ export function CombinedDiffView({ const [viewStates] = useState(() => new Map()); const [entriesCache] = useState(() => new Map()); const keys = useMemo(() => entries.map((e) => e.key), [entries]); + // Each diff's own key, hashing its texts: worked out once, not on every scroll frame. + const diffKeys = useMemo(() => items.map((item) => diffInput(item).key), [items]); // Blocks that stay mounted when scrolled away: the one holding the keyboard focus, so typing still // reaches it, and with autosave off those with unsaved staged edits, which unmounting would save. @@ -253,12 +255,12 @@ export function CombinedDiffView({ const slot = scrollRef.current?.contains(active) ? active?.closest("[data-index]") : null; if (slot) shown.add(Number(slot.dataset.index)); if (useProject.getState().settings?.files?.autoSave === false) - items.forEach((item, index) => { - if (ownedStagedDocument(combinedDiffDocumentOwner(tabKey, diffInput(item).key))?.dirty) shown.add(index); + diffKeys.forEach((key, index) => { + if (ownedStagedDocument(combinedDiffDocumentOwner(tabKey, key))?.dirty) shown.add(index); }); return [...shown].sort((a, b) => a - b); }, - [items, tabKey], + [diffKeys, tabKey], ); const virtualizer = useVirtualizer({ count: items.length,