Skip to content

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

perf(review): reuse serialization for unchanged diff payloads

lyk820e8eefe894
diff --git a/apps/desktop/src/components/workpanel/ReviewHistoryPanel.tsx b/apps/desktop/src/components/workpanel/ReviewHistoryPanel.tsx
index 0ded93d63..a0f0a78db 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 } from "../../lib/review-history";
+import { groupReviewFiles, readReviewPages, reviewHistoryRevision } 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,9 +27,7 @@ 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);
- // Review facts refresh independently of streamed text, including late delegates.
- const revision = useMemo(() => messages.filter(message => message.role === "tool")
- .map(message => `${message.id}:${message.toolStatus}:${JSON.stringify((message.toolResult as { details?: { review?: unknown } } | undefined)?.details?.review ?? null)}`).join("|"), [messages]);
+ const revision = useMemo(() => reviewHistoryRevision(messages), [messages]);
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 8f159d788..e39bef803 100644
--- a/apps/desktop/src/lib/review-history.ts
+++ b/apps/desktop/src/lib/review-history.ts
@@ -1,6 +1,29 @@
-import type { DiffHunk, DiffLine, ReviewChangesPage, ReviewHistoryChange } from "@pi-desktop/shared";
+import type { DiffHunk, DiffLine, ReviewChangesPage, ReviewHistoryChange, UiMessage } from "@pi-desktop/shared";
import { reviewChangeFromMessage, type ReviewChangeEntry } from "./workspace-review.ts";
+const serializedReviewByPayload = new WeakMap<object, string>();
+
+function serializeReviewPayload(review: unknown): string {
+ if (review === null || typeof review !== "object") return String(JSON.stringify(review));
+
+ const cached = serializedReviewByPayload.get(review);
+ if (cached !== undefined) return cached;
+
+ // Renderer updates replace review payloads, including rollback. Cache by
+ // payload identity, not message ID, without keeping old diffs alive.
+ const serialized = String(JSON.stringify(review));
+ serializedReviewByPayload.set(review, serialized);
+ 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("|");
+}
+
export type NumberedReviewLine = DiffLine & { oldLine: number | null; newLine: number | null };
export function numberedReviewHunks(hunks: readonly DiffHunk[]): { header: string; lines: NumberedReviewLine[] }[] {
return hunks.map(hunk => {
diff --git a/apps/desktop/test/review-history.test.mjs b/apps/desktop/test/review-history.test.mjs
index ed2b616c3..e7429b109 100644
--- a/apps/desktop/test/review-history.test.mjs
+++ b/apps/desktop/test/review-history.test.mjs
@@ -1,10 +1,53 @@
import assert from "node:assert/strict";
import test from "node:test";
-import { reviewLineNumber, numberedReviewHunks, groupReviewFiles, readReviewPages, reviewFilePathLabel } from "../src/lib/review-history.ts";
+import { reviewLineNumber, numberedReviewHunks, groupReviewFiles, readReviewPages, reviewFilePathLabel, reviewHistoryRevision } 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 cause the same review payload to be serialized again", () => {
+ const { message } = entry("edited");
+ let hunkReads = 0;
+ Object.defineProperty(message.toolResult.details.review, "hunks", {
+ enumerable: true,
+ get() {
+ hunkReads++;
+ return [{ header: "@@ -1 +1 @@", lines: [{ type: "add", text: "updated" }] }];
+ },
+ });
+ const assistant = { id: "reply", role: "assistant", content: "Working" };
+ const before = reviewHistoryRevision([message, assistant]);
+ const after = reviewHistoryRevision([{ ...message }, { ...assistant, content: "Working on it" }]);
+ assert.equal(after, before);
+ assert.equal(hunkReads, 1);
+});
+
+test("review revision compares content, including new changes and tool status", () => {
+ const { message } = entry("edited");
+ const before = reviewHistoryRevision([message]);
+ const reloaded = structuredClone(message);
+ assert.equal(reviewHistoryRevision([reloaded]), before);
+
+ const updated = structuredClone(message);
+ updated.toolResult.details.review.hunks = [{
+ header: "@@ -1 +1 @@",
+ lines: [{ type: "add", text: "replacement" }],
+ }];
+ assert.notEqual(reviewHistoryRevision([updated]), before);
+ assert.notEqual(reviewHistoryRevision([message, entry("another-edit").message]), before);
+ assert.notEqual(reviewHistoryRevision([{ ...message, toolStatus: "running" }]), before);
+});
+
+test("a rollback invalidates the cached revision for the same message and snapshot", () => {
+ const { message } = entry("edited");
+ const before = reviewHistoryRevision([message]);
+ const rolledBack = withReviewChangeState(message, "rolledBack");
+ assert.notEqual(reviewHistoryRevision([rolledBack]), before);
+ assert.equal(reviewHistoryRevision([message]), before);
+});
+
test("groups only the same execution root and path, deduplicating snapshots", () => {
const a = entry("one", "src/a.ts", "D:/main"), b = entry("two", "src/a.ts", "D:/main");
b.message.parentToolCallId = "task";