Skip to content

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

perf(transcript): reuse completed text facts during structural appends

xjxc2b8a9ce66c2
diff --git a/apps/desktop/src/lib/assistant-turns.ts b/apps/desktop/src/lib/assistant-turns.ts
index e47a06489..9fe477026 100644
--- a/apps/desktop/src/lib/assistant-turns.ts
+++ b/apps/desktop/src/lib/assistant-turns.ts
@@ -6,6 +6,7 @@ import type {
} from "@pi-desktop/shared";
import { addUsage, hostedSearchRounds } from "@pi-desktop/shared";
import { isDelegationStartTool } from "./tool-display";
+import { messageContentFacts } from "./transcript-summary";
export type AssistantActivityItem =
| { kind: "thinking"; message: UiMessage }
@@ -71,7 +72,7 @@ function isVisibleMessage(message: UiMessage): boolean {
if (message.role === "system" && message.modelSystem) return false;
return !(
message.role === "assistant" &&
- !(message.content || "").trim() &&
+ !messageContentFacts(message).hasContent &&
!messageThinking(message) &&
!message.hostedSearch &&
!message.error
@@ -108,7 +109,7 @@ function collectSubagentRuns(
// showing: the text is the only place its narration and report exist.
const thinking = messageThinking(message);
if (thinking) run.items.push({ kind: "thinking", message });
- if ((message.content || "").trim() || message.error) {
+ if (messageContentFacts(message).hasContent || message.error) {
run.items.push({ kind: "answer", message });
}
}
@@ -270,9 +271,10 @@ export function buildTranscriptEntries(
for (const round of hostedSearchRounds(message.hostedSearch)) {
pushActivity({ kind: "hostedSearch", message, round });
}
- if ((message.content || "").trim() || !thinking || message.error) {
+ const hasContent = messageContentFacts(message).hasContent;
+ if (hasContent || !thinking || message.error) {
current.parts.push({ kind: "message", message });
- if (!current.anchorId && (message.content || "").trim()) {
+ if (!current.anchorId && hasContent) {
current.anchorId = message.id;
}
}
diff --git a/apps/desktop/test/transcript-projection.test.mjs b/apps/desktop/test/transcript-projection.test.mjs
index 15b30035f..75748a9b4 100644
--- a/apps/desktop/test/transcript-projection.test.mjs
+++ b/apps/desktop/test/transcript-projection.test.mjs
@@ -36,6 +36,38 @@ function assertProjection(messages, compactions) {
return actual;
}
+test("appending tool calls does not reread completed parent or delegate text", () => {
+ let reads = 0;
+ let messages = [message("user", "user", "Continue"), message("task", "tool", "running", {
+ toolName: "Task", toolCallId: "call", toolArgs: {},
+ })];
+ for (let index = 0; index < 1_000; index++) {
+ const row = message(`old-${index}`, "assistant", "", index % 2
+ ? { parentToolCallId: "call", agentName: "worker" } : {});
+ Object.defineProperty(row, "content", {
+ enumerable: true,
+ get() { reads++; return " Completed response. ".repeat(36); },
+ });
+ messages.push(row);
+ }
+ const before = getTranscriptProjection(messages);
+ reads = 0;
+ for (let index = 0; index < 3; index++) {
+ messages = upsertLiveSessionMessage(messages, message(`new-tool-${index}`, "tool", "", {
+ toolName: "Read", toolCallId: `new-tool-${index}`, toolStatus: "running",
+ }));
+ getTranscriptProjection(messages);
+ }
+ assert.equal(reads, 0, "structural appends must reuse immutable content facts");
+ assert.equal(before.entries[1].parts.at(-1).kind, "message", "old snapshots stay unchanged");
+ assertProjection(messages);
+ // A same-id replacement is a new immutable snapshot and must invalidate text.
+ messages = upsertLiveSessionMessage(messages, message("old-0", "assistant", " ", { thinking: "Still thinking" }));
+ const updated = assertProjection(messages);
+ assert.equal(updated.entries[1].anchorId, "old-2");
+ assert.equal(updated.entries[1].parts[1].items[0].message.thinking, "Still thinking");
+});
+
for (const count of [100, 1_000, 10_784]) {
test(`${count} loaded messages: one tail delta does not read historical content`, () => {
let reads = 0;
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index 2dc2bd8d6..1624622dc 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -1668,6 +1668,8 @@ storage but compose into one assistant turn until the next user message.
normal 100-row page and deliberately loaded full histories, and to foreground
and background session caches. Shallow array copies
remain permitted; cold loads and structural changes may rebuild the projection.
+ Structural appends reuse cached content facts for unchanged immutable messages,
+ including delegate answers, instead of rereading and trimming completed text.
- Completed rows and unchanged parts within a large active turn/activity group
retain their render boundaries. Deferred presentation consumes one immutable
projection snapshot. Older-row and child updates, terminal re-keying, Copy,
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 96128c6d1..dc1b2ab1e 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -6125,13 +6125,16 @@ must keep splitting are covered by `markdown-blocks.test.mjs`.
Separately stream a 256-fragment single turn and a 256-tool activity group;
update a nested child through the real read-only subagent dock, stream the
giant group's final thinking item, and deliver actual `tool_update` partials
- to a running tool in the middle of that group.
+ to a running tool in the middle of that group. Append three tool starts to the
+ loaded 10,784-row session and switch away and back.
- **Expected**: Ordinary warmed deltas read zero unchanged bodies; unchanged
groups do not rerender. Mounted history stays bounded despite full canonical
input. Background updates never replace the foreground, and revalidation never
discards its cached live tail. Content, minimap previews, disclosure choice,
reader scroll ownership, completed Copy text and terminal identity remain
correct. Changed tools/children/thinking still reach their real components.
+ Structural tool appends read zero unchanged completed bodies and survive session
+ reselection; immutable text facts are also reused for delegate answers.
- **Specs linked**: `04-ux/08-component-spec.md` §7.6.
- **Acceptance**: C (chat stream), Quality. **Milestone**: M5 maintenance.
- **Status**: Automated fixture in `scripts/e2e/transcript-long-history.tsx`,
diff --git a/scripts/e2e/transcript-long-history.tsx b/scripts/e2e/transcript-long-history.tsx
index 9e9ba76f9..84ff5d516 100644
--- a/scripts/e2e/transcript-long-history.tsx
+++ b/scripts/e2e/transcript-long-history.tsx
@@ -364,6 +364,20 @@ export async function transcriptLongHistoryProbe() {
assert(useAppStore.getState().messages[1] === parent, "child update replaced its stable parent Task");
}
}
+ // Structural growth must reuse completed text too, not just same-row deltas.
+ await select(largest);
+ largest.reads.count = 0;
+ for (let index = 0; index < 3; index++) {
+ const id = `appended-long-history-tool-${index}`;
+ send(largest.summary.id, { type: "tool_start", toolCallId: id, toolName: "Read",
+ args: { path: `append-probe-${index}.txt` } });
+ await until(() => host.querySelector(`[data-message-id="${id}"]`) !== null, "appended tool row");
+ await settled();
+ }
+ assert(largest.reads.count === 0, `tool appends reread ${largest.reads.count} completed bodies`);
+ largest.messages = useAppStore.getState().messages;
+ await select(sessions[0]); await select(largest);
+ assert(host.querySelector('[data-message-id="appended-long-history-tool-2"]'), "switch lost appended tool");
assert(reads.every((read) => read.limit === 100), "ordinary selection requested unbounded Host history");
assert(errors.length === 0, `render errors: ${errors.map(String)}`);
return { ok: true, workCounts, hostPageSize: 100, simultaneousRunningSessions: 3,