Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

perf(review): avoid rebuilding the review revision on text-only updates

lykccded25e50c6
diff --git a/apps/desktop/src/components/workpanel/ReviewHistoryPanel.tsx b/apps/desktop/src/components/workpanel/ReviewHistoryPanel.tsx
index a0f0a78db..291023d4a 100644
--- a/apps/desktop/src/components/workpanel/ReviewHistoryPanel.tsx
+++ b/apps/desktop/src/components/workpanel/ReviewHistoryPanel.tsx
@@ -2,7 +2,7 @@ import { createElement, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import type { ReviewChangesPage, ReviewContextLines, ReviewTurnsPage } from "@pi-desktop/shared";
import { api } from "../../lib/api";
-import { groupReviewFiles, readReviewPages, reviewHistoryRevision } from "../../lib/review-history";
+import { createReviewHistoryRevision, groupReviewFiles, readReviewPages } from "../../lib/review-history";
import { scrollReviewToAnchor, useReviewFocusRequest } from "../../lib/review-focus-request";
import { readReviewContextLines, rememberReviewContextLines } from "../../lib/review-preferences";
import { useAppStore } from "../../stores/app-store";
@@ -27,7 +27,9 @@ export function ReviewHistoryPanel({ sessionId }: { sessionId: string }) {
const [loading, setLoading] = useState(true), [error, setError] = useState(false);
const [reload, setReload] = useState(0), [paging, setPaging] = useState(false);
const generation = useRef(0), handledFocus = useRef(0), scroller = useRef<HTMLDivElement>(null);
- const revision = useMemo(() => reviewHistoryRevision(messages), [messages]);
+ // ReviewTab keys this panel by session, so each session gets a fresh reader.
+ const readRevision = useMemo(createReviewHistoryRevision, []);
+ const revision = useMemo(() => readRevision(messages), [messages, readRevision]);
useEffect(() => {
if (!focus || focus.seq === handledFocus.current || (focus.sessionId && focus.sessionId !== sessionId)) return;
handledFocus.current = focus.seq;
diff --git a/apps/desktop/src/lib/review-history.ts b/apps/desktop/src/lib/review-history.ts
index e39bef803..a0e2e89ac 100644
--- a/apps/desktop/src/lib/review-history.ts
+++ b/apps/desktop/src/lib/review-history.ts
@@ -16,12 +16,39 @@ function serializeReviewPayload(review: unknown): string {
return serialized;
}
-/** Review facts refresh independently of streamed text, including late delegates. */
-export function reviewHistoryRevision(messages: readonly UiMessage[]): string {
- return messages.filter(message => message.role === "tool").map(message => {
- const review = (message.toolResult as { details?: { review?: unknown } } | undefined)?.details?.review ?? null;
- return `${message.id}:${message.toolStatus}:${serializeReviewPayload(review)}`;
- }).join("|");
+type ReviewRevisionFact = {
+ id: UiMessage["id"];
+ status: UiMessage["toolStatus"];
+ payload: string;
+};
+
+/** One reader per Review panel. Rebuild its revision only when tool facts change. */
+export function createReviewHistoryRevision() {
+ let previous: ReviewRevisionFact[] = [];
+ let revision = "";
+
+ return (messages: readonly UiMessage[]): string => {
+ const next: ReviewRevisionFact[] = [];
+ let changed = false;
+ for (const message of messages) {
+ if (message.role !== "tool") continue;
+ const review = (message.toolResult as { details?: { review?: unknown } } | undefined)?.details?.review ?? null;
+ const payload = serializeReviewPayload(review);
+ const old = previous[next.length];
+ if (old && old.id === message.id && old.status === message.toolStatus && old.payload === payload) {
+ next.push(old);
+ } else {
+ next.push({ id: message.id, status: message.toolStatus, payload });
+ changed = true;
+ }
+ }
+
+ if (changed || next.length !== previous.length) {
+ previous = next;
+ revision = next.map(({ id, status, payload }) => `${id}:${status}:${payload}`).join("|");
+ }
+ return revision;
+ };
}
export type NumberedReviewLine = DiffLine & { oldLine: number | null; newLine: number | null };
diff --git a/apps/desktop/test/review-history.test.mjs b/apps/desktop/test/review-history.test.mjs
index e7429b109..fca38cd2f 100644
--- a/apps/desktop/test/review-history.test.mjs
+++ b/apps/desktop/test/review-history.test.mjs
@@ -1,13 +1,58 @@
import assert from "node:assert/strict";
import test from "node:test";
-import { reviewLineNumber, numberedReviewHunks, groupReviewFiles, readReviewPages, reviewFilePathLabel, reviewHistoryRevision } from "../src/lib/review-history.ts";
+import { createReviewHistoryRevision, reviewLineNumber, numberedReviewHunks, groupReviewFiles, readReviewPages, reviewFilePathLabel } from "../src/lib/review-history.ts";
import { withReviewChangeState } from "../src/lib/workspace-review.ts";
function entry(id, path = "a.ts", root) {
return { root, message: { id, role: "tool", content: "", createdAt: "2026-10-30", toolName: "Edit", toolStatus: "success", toolResult: { details: { root: "workspace", review: { version: 1, snapshotId: id, messageId: id, path, operation: "edit", status: "modified", state: "active", additions: 1, deletions: 1, hunks: [], reversible: true } } } } };
}
+test("streamed assistant text does not rebuild the revision of unchanged tool facts", () => {
+ const reviewHistoryRevision = createReviewHistoryRevision();
+ const { message } = entry("edited");
+ const assistant = { id: "reply", role: "assistant", content: "Working" };
+ const join = Array.prototype.join;
+ let builds = 0;
+ let before, after, reloaded;
+ try {
+ Array.prototype.join = function (separator) {
+ if (separator === "|" && typeof this[0] === "string" && this[0].startsWith("edited:success:")) builds++;
+ return join.call(this, separator);
+ };
+ before = reviewHistoryRevision([message, assistant]);
+ after = reviewHistoryRevision([{ ...message }, { ...assistant, content: "Working on it" }]);
+ reloaded = reviewHistoryRevision([structuredClone(message)]);
+ } finally {
+ Array.prototype.join = join;
+ }
+ assert.equal(after, before);
+ assert.equal(reloaded, before);
+ assert.equal(builds, 1);
+});
+
+test("late delegated tool evidence changes the revision even when the parent is unchanged", () => {
+ const reviewHistoryRevision = createReviewHistoryRevision();
+ const parent = entry("parent-edit").message;
+ const delegate = { ...entry("delegated-edit", "late.txt").message, parentToolCallId: "task" };
+ const before = reviewHistoryRevision([parent]);
+ const after = reviewHistoryRevision([parent, delegate]);
+ assert.notEqual(after, before);
+ assert.equal(reviewHistoryRevision([parent, structuredClone(delegate)]), after);
+ assert.equal(reviewHistoryRevision([parent]), before);
+});
+
+test("a new session's revision reader does not carry over old tool facts", () => {
+ const previousSession = createReviewHistoryRevision();
+ const nextSession = createReviewHistoryRevision();
+ const old = previousSession([entry("edit", "old.txt").message]);
+ assert.equal(nextSession([]), "");
+ const next = nextSession([entry("edit", "new.txt").message]);
+ assert.notEqual(next, old);
+ assert.equal(previousSession([entry("edit", "old.txt").message]), old);
+});
+
test("streamed assistant text does not cause the same review payload to be serialized again", () => {
+ const reviewHistoryRevision = createReviewHistoryRevision();
const { message } = entry("edited");
let hunkReads = 0;
Object.defineProperty(message.toolResult.details.review, "hunks", {
@@ -25,6 +70,7 @@ test("streamed assistant text does not cause the same review payload to be seria
});
test("review revision compares content, including new changes and tool status", () => {
+ const reviewHistoryRevision = createReviewHistoryRevision();
const { message } = entry("edited");
const before = reviewHistoryRevision([message]);
const reloaded = structuredClone(message);
@@ -41,6 +87,7 @@ test("review revision compares content, including new changes and tool status",
});
test("a rollback invalidates the cached revision for the same message and snapshot", () => {
+ const reviewHistoryRevision = createReviewHistoryRevision();
const { message } = entry("edited");
const before = reviewHistoryRevision([message]);
const rolledBack = withReviewChangeState(message, "rolledBack");
diff --git a/scripts/e2e/review-history.tsx b/scripts/e2e/review-history.tsx
index 9f60796c3..308530f81 100644
--- a/scripts/e2e/review-history.tsx
+++ b/scripts/e2e/review-history.tsx
@@ -321,12 +321,15 @@ window.reviewProbe = async () => {
await until(() => !document.querySelector('.review-history.is-nowrap'), "Word wrap restores");
headers()[0].click(); await frame(); assert(!document.querySelector(".review-history-file-body"), "File can collapse after browsing");
+ await mode(true); await choose("对话轮次", seed.round); await ready();
+ assert(!headers().some(node => node.textContent?.includes("late.txt")), "The old round starts without the late edit");
+ const late = await action("late") as UiMessage;
+ useAppStore.setState(state => ({ messages: [...state.messages, late] }));
+ await until(() => headers().some(node => node.textContent?.includes("late.txt")), "Late delegate refreshes the displayed old round without a selection change");
await mode(false); await ready();
- await action("late");
- useAppStore.setState({ messages: [seed.late] }); await ready();
assert(headers().some(node => node.textContent?.includes("latest.txt")), "Late old delegate does not take over Latest");
await action("restart");
- await mount(seed.other); assert(headers().length === 0, "Other session has no leaked files");
+ await mount(seed.other); assert(headers().length === 0, "Other session has no leaked files after the late delegate refresh");
await contextLines(1, 5);
flushSync(() => root.render(null));
await mount(seed.other); await contextLines(1, 1);