Порівняти коміти

...
1 Коміти
Автор SHA1 Повідомлення Дата
l0ng-ai 43163e3fd6 perf(agents): stop a running task's Changes tab from reloading every diff on each update
While a task runs, it is published again with every token count. The Changes tab rebuilt its
diff items from the whole task object, so each update re-read the base text of every changed
file over IPC, hashed all of them, and handed the view a new array that rendered every mounted
diff block and every row of the file tree again. The diffs now depend only on the task fields
they use (id, base, isolation, branch, cwd), an unchanged list keeps its array, and the file
tree is memoized. The all-in-one diff also works out each diff's key once per list instead of
hashing every file's text on every scroll frame (autosave off).
2026-11-27 09:49:24 +08:00
2 змінених файлів з 124 додано та 105 видалено

No files matched your search

+119 -102
Переглянути файл
@@ -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<CombinedDiffHandle>(null);
const busy = task.status === "starting" || task.status === "running";
const fileTree =
changes === null ? (
<Spinner />
) : (
<Tree<ChangeRow>
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 ? (
<Spinner />
) : (
<Tree<ChangeRow>
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 (
<span className={styles.changeGroup}>
<span className={styles.changeGroupName}>
{n.label === "Uncommitted" ? <T>Uncommitted</T> : <T>Committed</T>}
</span>
<span className={styles.muted}>
{n.label === "Committed"
? tx(n.count === 1 ? "1 commit" : "{count} commits").replace("{count}", String(n.count))
: `${n.count} ${tx(n.count === 1 ? "file" : "files")}`}
</span>
<span className={styles.changeStat}>
{n.added > 0 && <span className={styles.added}>+{n.added}</span>}{" "}
{n.removed > 0 && <span className={styles.removed}>−{n.removed}</span>}
</span>
</span>
);
}
if (n.type === "repo") {
const files = n.children.flatMap((c) => (c.type === "file" ? [c.change] : []));
const stats = statsOf(files);
return (
<span className={styles.changeGroup} title={n.root}>
<Icon name="folder" size={14} />
<span className={styles.changeGroupName}>{basename(n.root)}</span>
<span className={styles.muted}>{`${files.length} ${tx(files.length === 1 ? "file" : "files")}`}</span>
<span className={styles.changeStat}>
{stats.added > 0 && <span className={styles.added}>+{stats.added}</span>}{" "}
{stats.removed > 0 && <span className={styles.removed}>−{stats.removed}</span>}
</span>
</span>
);
}
if (n.type === "commit") {
const stats = statsOf(n.commit.files);
return (
<span className={styles.changeCommit} title={n.commit.message}>
<Icon name="commit" size={14} />
<span className={styles.changeCommitHash}>{n.commit.shortId}</span>
<span className={styles.changeCommitSummary}>{n.commit.summary}</span>
{n.commit.timeMs != null && (
<span className={styles.changeCommitTime}>{formatSentAt(n.commit.timeMs)}</span>
)}
<span className={styles.changeStat}>
{stats.added > 0 && <span className={styles.added}>+{stats.added}</span>}{" "}
{stats.removed > 0 && <span className={styles.removed}>−{stats.removed}</span>}
</span>
</span>
);
}
return (
<span className={styles.changeGroup}>
<span className={styles.changeGroupName}>
{n.label === "Uncommitted" ? <T>Uncommitted</T> : <T>Committed</T>}
</span>
<span className={styles.muted}>
{n.label === "Committed"
? tx(n.count === 1 ? "1 commit" : "{count} commits").replace("{count}", String(n.count))
: `${n.count} ${tx(n.count === 1 ? "file" : "files")}`}
<span className={styles.change} title={n.change.relPath}>
<Icon name={fileIconName(basename(n.change.path), n.change.path)} size={16} />
<span className={styles.changeName} style={{ color: KIND_COLOR[n.change.kind] }}>
{basename(n.change.path)}
</span>
<span className={styles.changeDir}>{changeDir(diffTask, n.change)}</span>
<span className={styles.changeStat}>
{n.added > 0 && <span className={styles.added}>+{n.added}</span>}{" "}
{n.removed > 0 && <span className={styles.removed}>−{n.removed}</span>}
{n.change.added > 0 && <span className={styles.added}>+{n.change.added}</span>}{" "}
{n.change.removed > 0 && <span className={styles.removed}>−{n.change.removed}</span>}
</span>
</span>
);
}
if (n.type === "repo") {
const files = n.children.flatMap((c) => (c.type === "file" ? [c.change] : []));
const stats = statsOf(files);
return (
<span className={styles.changeGroup} title={n.root}>
<Icon name="folder" size={14} />
<span className={styles.changeGroupName}>{basename(n.root)}</span>
<span className={styles.muted}>{`${files.length} ${tx(files.length === 1 ? "file" : "files")}`}</span>
<span className={styles.changeStat}>
{stats.added > 0 && <span className={styles.added}>+{stats.added}</span>}{" "}
{stats.removed > 0 && <span className={styles.removed}>−{stats.removed}</span>}
</span>
</span>
);
}
if (n.type === "commit") {
const stats = statsOf(n.commit.files);
return (
<span className={styles.changeCommit} title={n.commit.message}>
<Icon name="commit" size={14} />
<span className={styles.changeCommitHash}>{n.commit.shortId}</span>
<span className={styles.changeCommitSummary}>{n.commit.summary}</span>
{n.commit.timeMs != null && (
<span className={styles.changeCommitTime}>{formatSentAt(n.commit.timeMs)}</span>
)}
<span className={styles.changeStat}>
{stats.added > 0 && <span className={styles.added}>+{stats.added}</span>}{" "}
{stats.removed > 0 && <span className={styles.removed}>−{stats.removed}</span>}
</span>
</span>
);
}
return (
<span className={styles.change} title={n.change.relPath}>
<Icon name={fileIconName(basename(n.change.path), n.change.path)} size={16} />
<span className={styles.changeName} style={{ color: KIND_COLOR[n.change.kind] }}>
{basename(n.change.path)}
</span>
<span className={styles.changeDir}>{changeDir(task, n.change)}</span>
<span className={styles.changeStat}>
{n.change.added > 0 && <span className={styles.added}>+{n.change.added}</span>}{" "}
{n.change.removed > 0 && <span className={styles.removed}>−{n.change.removed}</span>}
</span>
</span>
);
}}
/>
);
}}
/>
),
[loading, view, rows, grouped, selected, diffTask],
);
return (
<section className={styles.changes} aria-label={tx("Changes")}>
<CombinedDiffView
@@ -542,6 +553,9 @@ function TaskChanges({ task, tabKey }: { task: AgentTask; tabKey: string }) {
);
}
/** What a task's diffs are built from. */
type DiffTask = Pick<AgentTask, "id" | "base" | "isolation" | "branch" | "cwd">;
/** 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<ChangeRow, { type: "file"
* whose diff is unchanged keeps its object, so its block doesn't load again as the task works.
*/
function useDiffItems(
task: AgentTask,
task: DiffTask,
rows: readonly ChangeRow[],
grouped: boolean,
scope: ChangesScope,
@@ -598,8 +612,11 @@ function useDiffItems(
? before
: item;
});
// Unchanged, the list keeps its array too: a new one renders every block again.
setItems((before) =>
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<AgentTask, "cwd">, 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,
+5 -3
Переглянути файл
@@ -243,6 +243,8 @@ export function CombinedDiffView({
const [viewStates] = useState(() => new Map<string, DiffViewState>());
const [entriesCache] = useState<DiffEntriesCache>(() => 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<HTMLElement>("[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,