xonecas.io
64875
Mc

mini-coder / commit 4b39bb1

fix: reduce verbose conversation ui lag

xonecas2026-04-08 4b39bb131bf700f161d6de6b853f938efae8d463 parent a86e507

10 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

TODO.md+1−1
@@ -7,7 +7,7 @@ The spec.md and code are the sources of truth, not this file, don't assume anyth
77
88 ## Open items
99
10-- [ ] **Refresh git state after each completed turn** (`ui/agent.ts`) — git state is refreshed before a turn starts but not after agent actions mutate the repo, so the status bar and next prompt footer can go stale.
10+- None right now.
1111
1212 ## Low priority (deffered until further notice)
1313
src/session.test.ts+32−0
@@ -6,6 +6,7 @@ import type {
66 UserMessage,
77 } from "@mariozechner/pi-ai";
88 import {
9+ addMessageToStats,
910 appendMessage,
1011 appendPromptHistory,
1112 computeStats,
@@ -492,6 +493,37 @@ describe("prompt history", () => {
492493 });
493494
494495 describe("cumulative stats", () => {
496+ test("addMessageToStats adds assistant usage and ignores non-assistant messages", () => {
497+ let stats = {
498+ totalInput: 0,
499+ totalOutput: 0,
500+ totalCost: 0,
501+ };
502+
503+ stats = addMessageToStats(
504+ stats,
505+ makeAssistant("done", {
506+ input: 200,
507+ output: 80,
508+ totalTokens: 280,
509+ cost: {
510+ input: 0.002,
511+ output: 0.003,
512+ cacheRead: 0,
513+ cacheWrite: 0,
514+ total: 0.005,
515+ },
516+ }),
517+ );
518+ stats = addMessageToStats(stats, makeUser("hello"));
519+
520+ expect(stats).toEqual({
521+ totalInput: 200,
522+ totalOutput: 80,
523+ totalCost: 0.005,
524+ });
525+ });
526+
495527 test("computeStats sums usage from assistant messages", () => {
496528 const messages: Message[] = [
497529 makeUser("hello"),
src/session.ts+35−13
@@ -732,22 +732,44 @@ export function forkSession(db: Database, sourceId: string): Session {
732732 * @param messages - The full persisted message history for a session.
733733 * @returns Aggregated {@link SessionStats}.
734734 */
735+/**
736+ * Add one persisted message's assistant usage to cumulative session stats.
737+ *
738+ * Non-assistant messages and assistant messages without valid `usage` are
739+ * ignored and return the original totals unchanged.
740+ *
741+ * @param stats - Running cumulative session totals.
742+ * @param message - Persisted message to fold into the totals.
743+ * @returns Updated cumulative session stats.
744+ */
745+export function addMessageToStats(
746+ stats: SessionStats,
747+ message: PersistedMessage,
748+): SessionStats {
749+ const usage = getAssistantUsage(message);
750+ if (!usage) {
751+ return stats;
752+ }
753+
754+ return {
755+ totalInput: stats.totalInput + usage.input,
756+ totalOutput: stats.totalOutput + usage.output,
757+ totalCost: stats.totalCost + usage.cost.total,
758+ };
759+}
760+
735761 export function computeStats(
736762 messages: readonly PersistedMessage[],
737763 ): SessionStats {
738- let totalInput = 0;
739- let totalOutput = 0;
740- let totalCost = 0;
741-
742- for (const msg of messages) {
743- const usage = getAssistantUsage(msg);
744- if (!usage) {
745- continue;
746- }
747- totalInput += usage.input;
748- totalOutput += usage.output;
749- totalCost += usage.cost.total;
764+ let stats: SessionStats = {
765+ totalInput: 0,
766+ totalOutput: 0,
767+ totalCost: 0,
768+ };
769+
770+ for (const message of messages) {
771+ stats = addMessageToStats(stats, message);
750772 }
751773
752- return { totalInput, totalOutput, totalCost };
774+ return stats;
753775 }
src/tools.test.ts+21−0
@@ -357,6 +357,27 @@ describe("shell", () => {
357357 expect(updates.some((text) => text.includes("first"))).toBe(true);
358358 expect(updates.at(-1)).toContain("second");
359359 });
360+
361+ test("throttles progressive output updates for chatty commands", async () => {
362+ const updates: string[] = [];
363+
364+ const result = await executeShell(
365+ {
366+ command:
367+ "i=1; while [ $i -le 20 ]; do printf '%s\\n' \"$i\"; sleep 0.01; i=$((i + 1)); done",
368+ },
369+ tmp,
370+ {
371+ onUpdate: (partial) => {
372+ updates.push(resultText(partial));
373+ },
374+ },
375+ );
376+
377+ expect(result.isError).toBe(false);
378+ expect(updates.at(-1)).toContain("20");
379+ expect(updates.length).toBeLessThanOrEqual(6);
380+ });
360381 });
361382
362383 // ---------------------------------------------------------------------------
src/tools.ts+58−6
@@ -158,6 +158,7 @@ interface ShellOpts {
158158
159159 const DEFAULT_MAX_LINES = 1000;
160160 const DEFAULT_MAX_BYTES = 50_000;
161+const SHELL_UPDATE_INTERVAL_MS = 75;
161162
162163 /** Format combined stdout/stderr for display in tool results. */
163164 function formatShellOutput(stdout: string, stderr: string): string {
@@ -215,6 +216,7 @@ export async function executeShell(
215216 const shell = process.env.SHELL || "/bin/sh";
216217 const maxLines = opts?.maxLines ?? DEFAULT_MAX_LINES;
217218 const maxBytes = opts?.maxBytes ?? DEFAULT_MAX_BYTES;
219+ let updateTimer: ReturnType<typeof setTimeout> | null = null;
218220
219221 try {
220222 const spawnOpts: Parameters<typeof Bun.spawn>[1] = {
@@ -227,40 +229,90 @@ export async function executeShell(
227229
228230 let stdoutBuf = "";
229231 let stderrBuf = "";
230- const reportUpdate = (): void => {
231- const output = truncateOutput(
232+ let lastReportedOutput = "";
233+ let lastReportAt = 0;
234+
235+ const clearPendingUpdate = (): void => {
236+ if (updateTimer) {
237+ clearTimeout(updateTimer);
238+ updateTimer = null;
239+ }
240+ };
241+
242+ const buildProgressOutput = (): string => {
243+ return truncateOutput(
232244 formatShellOutput(stdoutBuf, stderrBuf),
233245 maxLines,
234246 maxBytes,
235247 );
236- if (!output) {
248+ };
249+
250+ const emitUpdate = (): void => {
251+ clearPendingUpdate();
252+ if (!opts?.onUpdate) {
253+ return;
254+ }
255+
256+ const output = buildProgressOutput();
257+ if (!output || output === lastReportedOutput) {
258+ return;
259+ }
260+
261+ lastReportedOutput = output;
262+ lastReportAt = Date.now();
263+ opts.onUpdate(textResult(output, false));
264+ };
265+
266+ const scheduleUpdate = (): void => {
267+ if (!opts?.onUpdate) {
268+ return;
269+ }
270+
271+ const elapsed = Date.now() - lastReportAt;
272+ if (elapsed >= SHELL_UPDATE_INTERVAL_MS) {
273+ emitUpdate();
237274 return;
238275 }
239- opts?.onUpdate?.(textResult(output, false));
276+ if (updateTimer) {
277+ return;
278+ }
279+
280+ updateTimer = setTimeout(() => {
281+ emitUpdate();
282+ }, SHELL_UPDATE_INTERVAL_MS - elapsed);
240283 };
241284
242285 const [stdout, stderr, exitCode] = await Promise.all([
243286 consumeShellStream(proc.stdout as ReadableStream<Uint8Array>, (chunk) => {
244287 stdoutBuf += chunk;
245- reportUpdate();
288+ scheduleUpdate();
246289 }),
247290 consumeShellStream(proc.stderr as ReadableStream<Uint8Array>, (chunk) => {
248291 stderrBuf += chunk;
249- reportUpdate();
292+ scheduleUpdate();
250293 }),
251294 proc.exited,
252295 ]);
253296
297+ clearPendingUpdate();
254298 const output = truncateOutput(
255299 formatShellOutput(stdout.trimEnd(), stderr.trimEnd()),
256300 maxLines,
257301 maxBytes,
258302 );
303+ if (opts?.onUpdate && output && output !== lastReportedOutput) {
304+ lastReportedOutput = output;
305+ opts.onUpdate(textResult(output, false));
306+ }
259307
260308 const isError = exitCode !== 0;
261309 const body = output || "(no output)";
262310 return textResult(`Exit code: ${exitCode}\n${body}`, isError);
263311 } catch (err) {
312+ if (updateTimer) {
313+ clearTimeout(updateTimer);
314+ updateTimer = null;
315+ }
264316 const message = err instanceof Error ? err.message : String(err);
265317 return textResult(`Shell error: ${message}`, true);
266318 }
src/ui.test.ts+178−0
@@ -14,10 +14,13 @@ import {
1414 registerFauxProvider,
1515 unregisterApiProviders,
1616 } from "@mariozechner/pi-ai";
17+import { getGitState } from "./git.ts";
1718 import type { AppState } from "./index.ts";
1819 import {
20+ appendMessage,
1921 appendPromptHistory,
2022 createSession,
23+ createUiMessage,
2124 loadMessages,
2225 openDatabase,
2326 } from "./session.ts";
@@ -424,6 +427,122 @@ describe("ui rendering", () => {
424427 }
425428 });
426429
430+ test("completed turns keep in-memory messages and stats authoritative", async () => {
431+ const api = "ui-authoritative-state-test";
432+ const sourceId = "ui-authoritative-state-test-source";
433+ const model: Model<string> = {
434+ id: "ui-authoritative-state-model",
435+ name: "UI Authoritative State Model",
436+ api,
437+ provider: "test",
438+ baseUrl: "http://localhost:0",
439+ reasoning: false,
440+ input: ["text"],
441+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
442+ contextWindow: 128000,
443+ maxTokens: 4096,
444+ };
445+
446+ const releaseStream = createDeferred<void>();
447+ const finalMessage: AssistantMessage = {
448+ role: "assistant",
449+ content: [fauxText("Done.")],
450+ api,
451+ provider: model.provider,
452+ model: model.id,
453+ usage: {
454+ input: 123,
455+ output: 45,
456+ cacheRead: 0,
457+ cacheWrite: 0,
458+ totalTokens: 168,
459+ cost: {
460+ input: 0.001,
461+ output: 0.002,
462+ cacheRead: 0,
463+ cacheWrite: 0,
464+ total: 0.003,
465+ },
466+ },
467+ stopReason: "stop",
468+ timestamp: Date.now(),
469+ };
470+
471+ const buildProviderStream = () => {
472+ const stream = createAssistantMessageEventStream();
473+
474+ queueMicrotask(() => {
475+ void releaseStream.promise.then(() => {
476+ stream.push({
477+ type: "done",
478+ reason: "stop",
479+ message: finalMessage,
480+ });
481+ stream.end(finalMessage);
482+ });
483+ });
484+
485+ return stream;
486+ };
487+
488+ registerApiProvider(
489+ {
490+ api,
491+ stream: () => buildProviderStream(),
492+ streamSimple: () => buildProviderStream(),
493+ },
494+ sourceId,
495+ );
496+
497+ const state = createTestState();
498+ state.cwd = process.cwd();
499+ state.canonicalCwd = process.cwd();
500+ state.model = model;
501+
502+ try {
503+ handleInput("hello", state);
504+
505+ await waitFor(() => state.running && state.session !== null);
506+ const sessionId = state.session?.id;
507+ if (!sessionId) {
508+ throw new Error("Expected a session to be created");
509+ }
510+
511+ appendMessage(state.db, sessionId, createUiMessage("Injected from DB"));
512+ releaseStream.resolve();
513+
514+ await waitFor(() => !state.running);
515+ await waitFor(() =>
516+ state.messages.some(
517+ (message) =>
518+ message.role === "assistant" &&
519+ typeof message.content !== "string" &&
520+ message.content[0]?.type === "text" &&
521+ message.content[0].text === "Done.",
522+ ),
523+ );
524+
525+ expect(state.messages.map((message) => message.role)).toEqual([
526+ "user",
527+ "assistant",
528+ ]);
529+ expect(state.stats).toEqual({
530+ totalInput: 123,
531+ totalOutput: 45,
532+ totalCost: 0.003,
533+ });
534+
535+ const persistedRoles = loadMessages(state.db, sessionId).map(
536+ (message) => message.role,
537+ );
538+ expect(persistedRoles).toEqual(["user", "ui", "assistant"]);
539+ } finally {
540+ await stopRunningTurn(state);
541+ unregisterApiProviders(sourceId);
542+ state.db.close();
543+ }
544+ });
545+
427546 test("submit/runtime errors are appended to the conversation log and persisted", async () => {
428547 const api = "ui-submit-error-test";
429548 const sourceId = "ui-submit-error-test-source";
@@ -881,6 +1000,65 @@ describe("ui rendering", () => {
8811000 }
8821001 });
8831002
1003+ test("git status refreshes after a completed turn mutates the repo", async () => {
1004+ const faux = registerFauxProvider();
1005+ const state = createTestState();
1006+ const repo = createTempDir();
1007+ const originalPath = process.env.PATH;
1008+ process.env.PATH = `${process.env.PATH ?? ""}:/usr/bin:/bin`;
1009+
1010+ const runGit = (...args: string[]) => {
1011+ const result = Bun.spawnSync(["git", ...args], {
1012+ cwd: repo,
1013+ stdout: "pipe",
1014+ stderr: "pipe",
1015+ });
1016+ if (result.exitCode !== 0) {
1017+ throw new Error(
1018+ `git ${args.join(" ")} failed: ${result.stderr.toString()}`,
1019+ );
1020+ }
1021+ };
1022+
1023+ runGit("init");
1024+ runGit("config", "user.name", "Mini Coder");
1025+ runGit("config", "user.email", "mini-coder@example.com");
1026+ writeFileSync(join(repo, "tracked.txt"), "initial\n", "utf-8");
1027+ runGit("add", "tracked.txt");
1028+ runGit("commit", "-m", "init");
1029+
1030+ state.cwd = repo;
1031+ state.canonicalCwd = repo;
1032+ state.model = faux.getModel();
1033+ state.git = await getGitState(repo);
1034+ faux.setResponses([
1035+ fauxAssistantMessage(
1036+ [
1037+ fauxToolCall("shell", {
1038+ command: "printf 'updated\\n' >> tracked.txt",
1039+ }),
1040+ ],
1041+ { stopReason: "toolUse" },
1042+ ),
1043+ fauxAssistantMessage("Done."),
1044+ ]);
1045+
1046+ try {
1047+ expect(state.git?.modified).toBe(0);
1048+
1049+ handleInput("update the repo", state);
1050+
1051+ await waitFor(() => state.running);
1052+ await waitFor(() => !state.running);
1053+ expect(state.git?.modified).toBe(1);
1054+ } finally {
1055+ await stopRunningTurn(state);
1056+ process.env.PATH = originalPath;
1057+ faux.unregister();
1058+ state.db.close();
1059+ }
1060+ });
1061+
8841062 test("streamed tool call arguments stay visible before execution begins", async () => {
8851063 const api = "ui-streaming-toolcall-test";
8861064 const sourceId = "ui-streaming-toolcall-test-source";
src/ui.ts+5−1
@@ -31,7 +31,10 @@ import {
3131 resetUiAgentState,
3232 } from "./ui/agent.ts";
3333 import { createCommandController } from "./ui/commands.ts";
34-import { buildConversationLogNodes } from "./ui/conversation.ts";
34+import {
35+ buildConversationLogNodes,
36+ resetConversationRenderCache,
37+} from "./ui/conversation.ts";
3538 import type { InputController } from "./ui/input.ts";
3639 import {
3740 autocompleteInputPath,
@@ -97,6 +100,7 @@ export function resetUiState(): void {
97100 dividerTick = 0;
98101 stopDividerAnimation();
99102 resetUiAgentState();
103+ resetConversationRenderCache();
100104 activeOverlay = null;
101105 stdinWasRaw = false;
102106 }
src/ui/agent.ts+5−8
@@ -22,11 +22,10 @@ import type { AppState } from "../index.ts";
2222 import { buildPrompt, buildToolList, MAX_PROMPT_HISTORY } from "../index.ts";
2323 import { parseInput } from "../input.ts";
2424 import {
25+ addMessageToStats,
2526 appendMessage,
2627 appendPromptHistory,
27- computeStats,
2828 filterModelMessages,
29- loadMessages,
3029 truncatePromptHistory,
3130 } from "../session.ts";
3231 import { executeReadImage } from "../tools.ts";
@@ -239,8 +238,7 @@ export function createUiAgentController(
239238 signal: state.abortController.signal,
240239 onEvent: (event) => handleAgentEvent(event, state),
241240 });
242- state.messages = loadMessages(state.db, session.id);
243- state.stats = computeStats(state.messages);
241+ state.git = await getGitState(state.cwd);
244242 } finally {
245243 state.running = false;
246244 state.abortController = null;
@@ -286,7 +284,7 @@ export function createUiAgentController(
286284
287285 case "assistant_message":
288286 state.messages.push(event.message);
289- state.stats = computeStats(state.messages);
287+ state.stats = addMessageToStats(state.stats, event.message);
290288 streamingContent = [];
291289 runtime.scrollConversationToBottom();
292290 runtime.render();
@@ -300,16 +298,15 @@ export function createUiAgentController(
300298 const pending = pendingToolResults.find(
301299 (toolResult) => toolResult.toolCallId === event.toolCallId,
302300 );
303- const content = structuredClone(event.result.content);
304301 if (pending) {
305302 pending.toolName = event.name;
306- pending.content = content;
303+ pending.content = event.result.content;
307304 pending.isError = event.result.isError;
308305 } else {
309306 pendingToolResults.push({
310307 toolCallId: event.toolCallId,
311308 toolName: event.name,
312- content,
309+ content: event.result.content,
313310 isError: event.result.isError,
314311 });
315312 }
src/ui/conversation.test.ts+107−1
@@ -1,4 +1,4 @@
1-import { describe, expect, test } from "bun:test";
1+import { afterEach, describe, expect, test } from "bun:test";
22 import type { Node } from "@cel-tui/types";
33 import {
44 fauxAssistantMessage,
@@ -13,6 +13,7 @@ import {
1313 previewToolRenderLines,
1414 renderAssistantMessage,
1515 renderToolResult,
16+ resetConversationRenderCache,
1617 type ToolRenderLine,
1718 } from "./conversation.ts";
1819
@@ -35,6 +36,10 @@ function collectText(node: Node | null): string[] {
3536 return node.children.flatMap((child) => collectText(child));
3637 }
3738
39+afterEach(() => {
40+ resetConversationRenderCache();
41+});
42+
3843 describe("ui/conversation", () => {
3944 test("renderAssistantMessage preserves visible paragraph order in streamed markdown", () => {
4045 const message = fauxAssistantMessage("First paragraph\n\nSecond paragraph");
@@ -122,6 +127,107 @@ describe("ui/conversation", () => {
122127 expect(text).not.toContain("Exit code: 0");
123128 });
124129
130+ test("buildConversationLogNodes reuses cached committed nodes when the log is unchanged", () => {
131+ const state = {
132+ messages: [fauxAssistantMessage("Committed response")],
133+ showReasoning: false,
134+ verbose: false,
135+ theme: DEFAULT_THEME,
136+ };
137+ const streaming = {
138+ isStreaming: false,
139+ content: [],
140+ pendingToolResults: [],
141+ };
142+
143+ const first = buildConversationLogNodes(state, streaming);
144+ const second = buildConversationLogNodes(state, streaming);
145+
146+ expect(second).toBe(first);
147+ });
148+
149+ test("buildConversationLogNodes reuses the committed prefix when only the streaming tail changes", () => {
150+ const state = {
151+ messages: [fauxAssistantMessage("Committed response")],
152+ showReasoning: false,
153+ verbose: false,
154+ theme: DEFAULT_THEME,
155+ };
156+
157+ const committed = buildConversationLogNodes(state, {
158+ isStreaming: false,
159+ content: [],
160+ pendingToolResults: [],
161+ });
162+ const withStreamingTail = buildConversationLogNodes(state, {
163+ isStreaming: true,
164+ content: [fauxText("Streaming tail")],
165+ pendingToolResults: [],
166+ });
167+ const text = collectText({
168+ type: "vstack",
169+ props: {},
170+ children: withStreamingTail,
171+ });
172+
173+ expect(withStreamingTail[0]).toBe(committed[0]);
174+ expect(text).toContain("Committed response");
175+ expect(text).toContain("Streaming tail");
176+ });
177+
178+ test("buildConversationLogNodes rebuilds cached tool nodes when verbose mode changes", () => {
179+ const output = Array.from({ length: 25 }, (_, i) => `line ${i + 1}`).join(
180+ "\n",
181+ );
182+ const state = {
183+ messages: [
184+ fauxAssistantMessage([
185+ fauxToolCall("shell", { command: "seq 1 25" }, { id: "tool-1" }),
186+ ]),
187+ {
188+ role: "toolResult" as const,
189+ toolCallId: "tool-1",
190+ toolName: "shell",
191+ content: [{ type: "text" as const, text: output }],
192+ isError: false,
193+ timestamp: Date.now(),
194+ },
195+ ],
196+ showReasoning: false,
197+ verbose: false,
198+ theme: DEFAULT_THEME,
199+ };
200+
201+ const previewNodes = buildConversationLogNodes(state, {
202+ isStreaming: false,
203+ content: [],
204+ pendingToolResults: [],
205+ });
206+ const previewText = collectText({
207+ type: "vstack",
208+ props: {},
209+ children: previewNodes,
210+ });
211+
212+ const verboseNodes = buildConversationLogNodes(
213+ { ...state, verbose: true },
214+ {
215+ isStreaming: false,
216+ content: [],
217+ pendingToolResults: [],
218+ },
219+ );
220+ const verboseText = collectText({
221+ type: "vstack",
222+ props: {},
223+ children: verboseNodes,
224+ });
225+
226+ expect(previewNodes).not.toBe(verboseNodes);
227+ expect(previewText).toContain("And 5 lines more");
228+ expect(verboseText).toContain("line 25");
229+ });
230+
125231 test("renderAssistantMessage shows in-progress thinking when reasoning is enabled", () => {
126232 const node = renderAssistantMessage(
127233 {
src/ui/conversation.ts+260−24
@@ -65,6 +65,55 @@ interface AssistantRenderState {
6565 errorMessage?: string;
6666 }
6767
68+type ConversationMessage = AppState["messages"][number];
69+
70+type ToolCallRenderInfo = {
71+ name: string;
72+ args: Record<string, unknown>;
73+};
74+
75+interface ConversationRenderCache {
76+ messages: readonly ConversationMessage[] | null;
77+ count: number;
78+ showReasoning: boolean;
79+ verbose: boolean;
80+ theme: Theme | null;
81+ nodes: Node[];
82+ toolCallArgs: Map<string, ToolCallRenderInfo>;
83+}
84+
85+const EMPTY_STREAMING_CONTENT: AssistantMessage["content"] = [];
86+const EMPTY_PENDING_TOOL_RESULTS: readonly PendingToolResult[] = [];
87+const EMPTY_RENDER_NODES: Node[] = [];
88+const EMPTY_TOOL_RESULT_ARGS: Record<string, unknown> = Object.freeze({});
89+
90+const conversationRenderCache: ConversationRenderCache = {
91+ messages: null,
92+ count: 0,
93+ showReasoning: false,
94+ verbose: false,
95+ theme: null,
96+ nodes: EMPTY_RENDER_NODES,
97+ toolCallArgs: new Map(),
98+};
99+
100+let editToolDiffCache = new WeakMap<
101+ Record<string, unknown>,
102+ ToolRenderLine[]
103+>();
104+
105+/** Reset cached committed conversation renders and derived edit diffs. */
106+export function resetConversationRenderCache(): void {
107+ conversationRenderCache.messages = null;
108+ conversationRenderCache.count = 0;
109+ conversationRenderCache.showReasoning = false;
110+ conversationRenderCache.verbose = false;
111+ conversationRenderCache.theme = null;
112+ conversationRenderCache.nodes = EMPTY_RENDER_NODES;
113+ conversationRenderCache.toolCallArgs = new Map();
114+ editToolDiffCache = new WeakMap();
115+}
116+
68117 /** Semantic line kinds used when rendering tool output in the log. */
69118 type ToolRenderLineKind =
70119 | "command"
@@ -261,10 +310,137 @@ function buildShellToolLines(
261310 );
262311 }
263312
313+function splitDiffSourceLines(text: string): string[] {
314+ if (text === "") {
315+ return [];
316+ }
317+
318+ const lines = text.split("\n");
319+ if (lines.at(-1) === "") {
320+ lines.pop();
321+ }
322+ return lines;
323+}
324+
325+function findSharedPrefixLineCount(
326+ oldLines: readonly string[],
327+ newLines: readonly string[],
328+): number {
329+ let prefixCount = 0;
330+ while (
331+ prefixCount < oldLines.length &&
332+ prefixCount < newLines.length &&
333+ oldLines[prefixCount] === newLines[prefixCount]
334+ ) {
335+ prefixCount += 1;
336+ }
337+ return prefixCount;
338+}
339+
340+function findSharedSuffixBounds(
341+ oldLines: readonly string[],
342+ newLines: readonly string[],
343+ prefixCount: number,
344+): { oldSuffixIndex: number; newSuffixIndex: number } {
345+ let oldSuffixIndex = oldLines.length - 1;
346+ let newSuffixIndex = newLines.length - 1;
347+
348+ while (
349+ oldSuffixIndex >= prefixCount &&
350+ newSuffixIndex >= prefixCount &&
351+ oldLines[oldSuffixIndex] === newLines[newSuffixIndex]
352+ ) {
353+ oldSuffixIndex -= 1;
354+ newSuffixIndex -= 1;
355+ }
356+
357+ return { oldSuffixIndex, newSuffixIndex };
358+}
359+
360+function hasSharedMiddleLine(
361+ removedLines: readonly string[],
362+ addedLines: readonly string[],
363+): boolean {
364+ const largerMiddle =
365+ removedLines.length >= addedLines.length ? removedLines : addedLines;
366+ const smallerMiddle =
367+ removedLines.length >= addedLines.length ? addedLines : removedLines;
368+ const largerMiddleSet = new Set(largerMiddle);
369+ return smallerMiddle.some((line) => largerMiddleSet.has(line));
370+}
371+
372+function pushPrefixedDiffLines(
373+ lines: ToolRenderLine[],
374+ content: readonly string[],
375+ kind: "text" | "diffAdded" | "diffRemoved",
376+ prefix: string,
377+): void {
378+ for (const line of content) {
379+ lines.push({ kind, text: `${prefix}${line}` });
380+ }
381+}
382+
383+function buildSimpleStructuredDiffLines(
384+ oldText: string,
385+ newText: string,
386+): ToolRenderLine[] | null {
387+ const context = 2;
388+ const oldLines = splitDiffSourceLines(oldText);
389+ const newLines = splitDiffSourceLines(newText);
390+ const prefixCount = findSharedPrefixLineCount(oldLines, newLines);
391+ const { oldSuffixIndex, newSuffixIndex } = findSharedSuffixBounds(
392+ oldLines,
393+ newLines,
394+ prefixCount,
395+ );
396+ const removedLines = oldLines.slice(prefixCount, oldSuffixIndex + 1);
397+ const addedLines = newLines.slice(prefixCount, newSuffixIndex + 1);
398+
399+ if (removedLines.length === 0 && addedLines.length === 0) {
400+ return [];
401+ }
402+ if (hasSharedMiddleLine(removedLines, addedLines)) {
403+ return null;
404+ }
405+
406+ const contextStart = Math.max(0, prefixCount - context);
407+ const unchangedPrefix = oldLines.slice(contextStart, prefixCount);
408+ const unchangedSuffix = oldLines.slice(
409+ oldSuffixIndex + 1,
410+ Math.min(oldLines.length, oldSuffixIndex + 1 + context),
411+ );
412+ const oldStart =
413+ contextStart === 0 ? (removedLines.length > 0 ? 1 : 0) : contextStart + 1;
414+ const newStart =
415+ contextStart === 0 ? (addedLines.length > 0 ? 1 : 0) : contextStart + 1;
416+ const oldLineCount =
417+ unchangedPrefix.length + removedLines.length + unchangedSuffix.length;
418+ const newLineCount =
419+ unchangedPrefix.length + addedLines.length + unchangedSuffix.length;
420+ const diffLines: ToolRenderLine[] = [
421+ {
422+ kind: "text",
423+ text: `@@ -${oldStart},${oldLineCount} +${newStart},${newLineCount} @@`,
424+ },
425+ ];
426+
427+ pushPrefixedDiffLines(diffLines, unchangedPrefix, "text", " ");
428+ pushPrefixedDiffLines(diffLines, removedLines, "diffRemoved", "-");
429+ pushPrefixedDiffLines(diffLines, addedLines, "diffAdded", "+");
430+ pushPrefixedDiffLines(diffLines, unchangedSuffix, "text", " ");
431+
432+ return diffLines;
433+}
434+
264435 function buildStructuredDiffLines(
265436 oldText: string,
266437 newText: string,
267438 ): ToolRenderLine[] {
439+ const fastDiffLines = buildSimpleStructuredDiffLines(oldText, newText);
440+ if (fastDiffLines !== null) {
441+ return fastDiffLines;
442+ }
443+
268444 const patch = structuredPatch("", "", oldText, newText, "", "", {
269445 context: 2,
270446 });
@@ -290,6 +466,22 @@ function buildStructuredDiffLines(
290466 return diffLines;
291467 }
292468
469+function getCachedEditToolDiffLines(
470+ args: Record<string, unknown>,
471+): ToolRenderLine[] {
472+ const cached = editToolDiffCache.get(args);
473+ if (cached) {
474+ return cached;
475+ }
476+
477+ const diffLines = buildStructuredDiffLines(
478+ getToolArgString(args, "oldText"),
479+ getToolArgString(args, "newText"),
480+ );
481+ editToolDiffCache.set(args, diffLines);
482+ return diffLines;
483+}
484+
293485 function splitPrefixedToolTextLines(
294486 text: string,
295487 kind: "diffAdded" | "diffRemoved",
@@ -312,9 +504,7 @@ function splitPrefixedToolTextLines(
312504
313505 /** Build the logical render lines for an edit tool result. */
314506 function buildEditToolLines(
315- _filePath: string,
316- oldText: string,
317- newText: string,
507+ args: Record<string, unknown>,
318508 isError: boolean,
319509 resultText: string,
320510 verbose: boolean,
@@ -326,7 +516,7 @@ function buildEditToolLines(
326516 );
327517 }
328518
329- const diffLines = buildStructuredDiffLines(oldText, newText);
519+ const diffLines = getCachedEditToolDiffLines(args);
330520
331521 return previewToolRenderLines(
332522 diffLines.length > 0
@@ -516,9 +706,7 @@ function renderToolResultContent(
516706 if (toolName === "edit") {
517707 return renderToolBlock(
518708 buildEditToolLines(
519- getToolArgString(args, "path"),
520- getToolArgString(args, "oldText"),
521- getToolArgString(args, "newText"),
709+ args,
522710 isError,
523711 getToolContentText(content),
524712 opts.verbose,
@@ -574,8 +762,6 @@ function renderUiMessage(msg: UiMessage, theme: Theme): Node {
574762 ]);
575763 }
576764
577-type ConversationMessage = AppState["messages"][number];
578-
579765 function pushConversationNode(nodes: Node[], node: Node | null): void {
580766 if (!node) {
581767 return;
@@ -588,7 +774,7 @@ function pushConversationNode(nodes: Node[], node: Node | null): void {
588774
589775 function rememberToolCallArgs(
590776 message: AssistantMessage,
591- toolCallArgs: Map<string, { name: string; args: Record<string, unknown> }>,
777+ toolCallArgs: Map<string, ToolCallRenderInfo>,
592778 ): void {
593779 for (const block of message.content) {
594780 if (block.type !== "toolCall") {
@@ -614,7 +800,7 @@ function renderToolResultMessage(
614800 function renderConversationMessage(
615801 message: ConversationMessage,
616802 renderOpts: ConversationRenderOpts,
617- toolCallArgs: Map<string, { name: string; args: Record<string, unknown> }>,
803+ toolCallArgs: Map<string, ToolCallRenderInfo>,
618804 theme: Theme,
619805 ): Node | null {
620806 if (message.role === "ui") {
@@ -631,13 +817,66 @@ function renderConversationMessage(
631817 const info = toolCallArgs.get(message.toolCallId);
632818 return renderToolResultMessage(
633819 info?.name ?? message.toolName,
634- info?.args ?? {},
820+ info?.args ?? EMPTY_TOOL_RESULT_ARGS,
635821 message.content,
636822 message.isError,
637823 renderOpts,
638824 );
639825 }
640826
827+function canReuseCommittedConversationCache(
828+ state: Pick<AppState, "messages" | "showReasoning" | "verbose" | "theme">,
829+): boolean {
830+ return (
831+ conversationRenderCache.messages === state.messages &&
832+ conversationRenderCache.showReasoning === state.showReasoning &&
833+ conversationRenderCache.verbose === state.verbose &&
834+ conversationRenderCache.theme === state.theme &&
835+ conversationRenderCache.count <= state.messages.length
836+ );
837+}
838+
839+function cacheCommittedConversation(
840+ state: Pick<AppState, "messages" | "showReasoning" | "verbose" | "theme">,
841+ renderOpts: ConversationRenderOpts,
842+): void {
843+ if (!canReuseCommittedConversationCache(state)) {
844+ conversationRenderCache.messages = state.messages;
845+ conversationRenderCache.count = 0;
846+ conversationRenderCache.showReasoning = state.showReasoning;
847+ conversationRenderCache.verbose = state.verbose;
848+ conversationRenderCache.theme = state.theme;
849+ conversationRenderCache.nodes = [];
850+ conversationRenderCache.toolCallArgs = new Map();
851+ }
852+
853+ for (
854+ let index = conversationRenderCache.count;
855+ index < state.messages.length;
856+ index++
857+ ) {
858+ pushConversationNode(
859+ conversationRenderCache.nodes,
860+ renderConversationMessage(
861+ state.messages[index]!,
862+ renderOpts,
863+ conversationRenderCache.toolCallArgs,
864+ state.theme,
865+ ),
866+ );
867+ }
868+
869+ conversationRenderCache.count = state.messages.length;
870+}
871+
872+function hasStreamingTail(streaming: StreamingConversationState): boolean {
873+ return (
874+ (streaming.content !== EMPTY_STREAMING_CONTENT ||
875+ streaming.pendingToolResults !== EMPTY_PENDING_TOOL_RESULTS) &&
876+ (streaming.content.length > 0 || streaming.pendingToolResults.length > 0)
877+ );
878+}
Cut short: this commit's diff is over 1000 lines.