xonecas.io
64875
Mc

mini-coder / commit 0396a86

refactor(turn): remove useInstructions dead code, experimental_repairToolCall, and textDelta legacy fallback

xonecas2026-03-14 0396a86d75be4304c5a6844b991a6814f59929a2 parent f629fc8

17 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.

performance-plan.md+194−0
@@ -0,0 +1,194 @@
1+# Performance Improvement Plan
2+
3+Based on the audit in the previous session. Three high-impact fixes, three medium fixes, four low/deferred items.
4+
5+---
6+
7+## High Impact
8+
9+### P1 — Wrap `saveMessages` in a transaction
10+
11+**File:** `src/session/db/message-repo.ts`
12+
13+Each `stmt.run()` without an explicit transaction triggers its own implicit WAL flush.
14+`saveMessages` is called twice per turn (user message + model messages), each time potentially
15+writing multiple rows. `saveSnapshot` already does this correctly with `db.transaction`.
16+
17+**Change:** Wrap the insert loop in `db.transaction(() => { ... })`.
18+
19+---
20+
21+### P2 — Cache `buildSystemPrompt` on `SessionRunner`
22+
23+**Files:** `src/agent/session-runner.ts`, `src/agent/system-prompt.ts`
24+
25+`buildSystemPrompt` is called on every `processUserInput`. It does:
26+- Up to 5 `existsSync` + 2 `readFileSync` calls for context files
27+- `loadSkillsIndex` → directory walk (`readdirSync` + `statSync` + `openSync`/`readSync`/`closeSync`) across 4 locations
28+
29+None of these inputs change between turns. The system prompt is fixed for the lifetime of
30+a `SessionRunner` instance (only `extraSystemPrompt` changes, and that has a setter).
31+
32+**Change:**
33+- Compute the prompt once in the `SessionRunner` constructor and store it.
34+- Re-compute only when `extraSystemPrompt` is mutated (already a public setter property).
35+- No change to the `buildSystemPrompt` function signature.
36+
37+---
38+
39+### P3 — Remove intermediate diff from `applyFileEdit`
40+
41+**Files:** `src/tools/shared.ts`, `src/tools/write-result.ts`, `src/agent/tools.ts`
42+
43+Current flow for any write tool when hooks are active (the default config):
44+1. `applyFileEdit` writes the file and calls `generateDiff(original, written)` → **diff #1**
45+2. Hook runs (reformatter rewrites the file)
46+3. `finalizeWriteResult` re-reads and calls `generateDiff(original, reformatted)` → **diff #2**
47+
48+Diff #1 is always discarded when a hook fires. `generateDiff` runs an LCS O(m×n) algorithm;
49+computing it twice wastes CPU on every write in a repo with hooks.
50+
51+When no hook fires, `stripWriteResultMeta` is used, which keeps diff #1 — so that path stays correct.
52+
53+**Change:**
54+- Remove `generateDiff` call from `applyFileEdit`; store only `_before` + `_filePath`.
55+- Move diff computation into `finalizeWriteResult` (already reads the post-hook file).
56+- For the no-hook path (`stripWriteResultMeta`), add the diff computation there too so callers still receive a populated `diff` field.
57+- Update `WriteResultMeta` and `applyFileEdit` return type accordingly.
58+
59+---
60+
61+## Medium Impact
62+
63+### P4 — Gate `getMessageDiagnostics` behind API log being enabled
64+
65+**File:** `src/llm-api/turn.ts`
66+
67+`getMessageDiagnostics` is called 2–3× per turn. It calls `JSON.stringify` + `Buffer.byteLength`
68+on every message in history. For long sessions (100+ messages) this serialises megabytes of data
69+just to produce numbers for the API log.
70+
71+**Change:**
72+- Check whether the API log is enabled before computing diagnostics.
73+- If disabled, skip all three `getMessageDiagnostics` calls entirely.
74+- `api-log.ts` already has the flag; expose a `isApiLogEnabled()` helper or check the env directly.
75+
76+---
77+
78+### P5 — Hoist `db.prepare` to a module-level lazy singleton in `saveMessages`
79+
80+**File:** `src/session/db/message-repo.ts`
81+
82+`db.prepare(...)` compiles the SQL statement on every `saveMessages` call (twice per turn).
83+SQLite statement preparation is cheap but non-trivial; it is unnecessary to repeat it.
84+
85+**Change:**
86+- Store the prepared statement in a module-level `let` variable, initialised lazily on first use
87+ (same pattern as `getDb()`).
88+- Apply the same treatment to `addPromptHistory` and `getPromptHistory` if they are on hot paths.
89+
90+---
91+
92+### P6 — Avoid object spread in `withCwdDefault` per tool invocation
93+
94+**File:** `src/agent/tools.ts`
95+
96+`withCwdDefault` wraps every tool call in a new object spread:
97+```ts
98+const withDefault = { cwd, ...snapshotCallback ? {snapshotCallback} : {}, ...input };
99+```
100+This allocates a new plain object on every invocation and spreads all tool arguments.
101+
102+**Change:**
103+- Mutate `cwd` as a default on the input object only when absent:
104+ ```ts
105+ const patched = input as Record<string, unknown>;
106+ if (patched.cwd === undefined) patched.cwd = cwd;
107+ if (snapshotCallback && patched.snapshotCallback === undefined)
108+ patched.snapshotCallback = snapshotCallback;
109+ return originalExecute(patched);
110+ ```
111+- This avoids the spread allocation entirely.
112+
113+---
114+
115+## Low / Deferred
116+
117+### P7 — Replace string concatenation in `consumeTail` with chunk accumulation
118+
119+**File:** `src/agent/subagent-runner.ts`
120+
121+`tail += decoder.decode(...)` in a loop is O(n²) in theory, bounded to ~16 KB by the slicing
122+guard. Still wasteful: each iteration copies the whole accumulated string.
123+
124+**Change:**
125+- Accumulate `Uint8Array` chunks into an array.
126+- After the loop, call `new TextDecoder().decode(Buffer.concat(chunks))` once.
127+- Then slice to `maxBytes` if needed.
128+- Remove the mid-loop `tail.slice(-maxBytes)` guard; it is no longer necessary.
129+
130+---
131+
132+### P8 — Replace LCS diff with Myers diff to reduce memory
133+
134+**File:** `src/tools/diff.ts`
135+
136+`lcsTable` allocates a flat `number[]` of `(m+1)*(n+1)` entries. Two 1000-line files produce ~1M
137+entries (~8 MB) per diff call. Every `replace`, `insert`, and `create` invocation pays this cost.
138+
139+**Change:**
140+- Implement Myers diff (Eugene Myers, 1986) in place of `lcsTable` / `editScript`.
141+- Myers requires only O(n·d) space where d is the number of edits — typically a small fraction of
142+ file size.
143+- The public API (`generateDiff`) and unified-diff output format are unchanged; only the internal
144+ `lcsTable` + `editScript` functions are replaced.
145+
146+---
147+
148+### P9 — Combine sequential message-history transforms in `runTurn`
149+
150+**File:** `src/llm-api/turn.ts`
151+
152+Five separate O(n) passes over the full message history per turn:
153+`sanitizeGemini` → `stripGPTCommentary` → `stripOpenAIItemIds` → `applyContextPruning` →
154+`compactToolResultPayloads`. Each allocates a new array even when returning the same messages
155+unchanged.
156+
157+**Change:**
158+- Merge `stripGPTCommentary` and `stripOpenAIItemIds` into a single pass: both are
159+ OpenAI-only, both iterate messages looking for assistant content parts to filter. They can share
160+ one `messages.map(...)` call with both predicates applied per message.
161+- `sanitizeGemini`, `applyContextPruning`, and `compactToolResultPayloads` operate on different
162+ structural concerns and keep their fast-path early exits; leave them as separate named functions
163+ but note the combined pass reduces allocations on OpenAI sessions by one full array copy.
164+
165+---
166+
167+### P10 — Move `VACUUM` off the synchronous startup path
168+
169+**File:** `src/session/db/connection.ts`
170+
171+`pruneOldData` calls `db.exec("VACUUM;")` synchronously before the CLI prompt is shown. With a
172+large or fragmented DB this can add measurable latency to startup.
173+
174+**Change:**
175+- Replace the synchronous `VACUUM` call with `setImmediate(() => db.exec("VACUUM;"))` so it runs
176+ after the event loop yields control back to the CLI render path.
177+- The WAL checkpoint (`wal_checkpoint(TRUNCATE)`) is fast and can remain synchronous.
178+
179+---
180+
181+## Execution Order
182+
183+```
184+P1 saveMessages transaction — 5 min, isolated, zero risk
185+P5 hoist db.prepare — 10 min, isolated, zero risk
186+P10 defer VACUUM off startup path — 10 min, isolated, zero risk
187+P7 consumeTail chunk accumulation — 15 min, isolated
188+P3 remove intermediate diff — 30 min, touches shared.ts + write-result.ts + tools.ts
189+P8 Myers diff — 60 min, self-contained in diff.ts, existing tests cover it
190+P2 cache system prompt — 45 min, touches session-runner.ts
191+P6 withCwdDefault spread — 20 min, touches tools.ts
192+P4 gate diagnostics on log flag — 20 min, touches turn.ts
193+P9 merge OpenAI message transforms — 30 min, touches turn.ts
194+```
src/agent/session-runner.ts+26−7
@@ -82,7 +82,20 @@ export class SessionRunner {
8282 public totalIn = 0;
8383 public totalOut = 0;
8484 public lastContextTokens = 0;
85- public extraSystemPrompt: string | undefined;
85+ // P2: Cache the system prompt so the file-system reads in buildSystemPrompt
86+ // (context files, skills index) are not repeated on every processUserInput call.
87+ // Re-built only when extraSystemPrompt changes.
88+ private _extraSystemPrompt: string | undefined;
89+ private _systemPrompt: string | undefined;
90+
91+ public get extraSystemPrompt(): string | undefined {
92+ return this._extraSystemPrompt;
93+ }
94+ public set extraSystemPrompt(value: string | undefined) {
95+ this._extraSystemPrompt = value;
96+ this._systemPrompt = undefined; // invalidate cache
97+ }
98+
8699 private isSubagent: boolean | undefined;
87100 private killSubprocesses: (() => void) | undefined;
88101
@@ -140,6 +153,7 @@ export class SessionRunner {
140153 this.totalOut = 0;
141154 this.lastContextTokens = 0;
142155 this.sessionTimeAnchor = new Date(this.session.createdAt).toISOString();
156+ this._systemPrompt = undefined; // invalidate cached prompt (new session anchor)
143157 this.snapshotStack.length = 0;
144158 }
145159
@@ -185,12 +199,17 @@ export class SessionRunner {
185199 this.coreHistory.push(userMsg);
186200
187201 const llm = resolveModel(this.currentModel);
188- const systemPrompt = buildSystemPrompt(
189- this.sessionTimeAnchor,
190- this.cwd,
191- this.extraSystemPrompt,
192- this.isSubagent,
193- );
202+ // P2: Use cached system prompt; rebuild only on first use or after
203+ // extraSystemPrompt / sessionTimeAnchor change.
204+ if (!this._systemPrompt) {
205+ this._systemPrompt = buildSystemPrompt(
206+ this.sessionTimeAnchor,
207+ this.cwd,
208+ this._extraSystemPrompt,
209+ this.isSubagent,
210+ );
211+ }
212+ const systemPrompt = this._systemPrompt;
194213
195214 let lastAssistantText = "";
196215
src/agent/subagent-runner.ts+6−8
@@ -35,22 +35,20 @@ async function consumeTail(
3535 ): Promise<string> {
3636 if (!stream) return "";
3737 const reader = stream.getReader();
38- const decoder = new TextDecoder();
39- let tail = "";
38+ // P7: Accumulate Uint8Array chunks instead of string-concatenating (O(n²));
39+ // decode and slice once at the end.
40+ const chunks: Uint8Array[] = [];
4041 try {
4142 while (true) {
4243 const { done, value } = await reader.read();
4344 if (done) break;
44- tail += decoder.decode(value, { stream: true });
45- if (tail.length > maxBytes * 2) {
46- tail = tail.slice(-maxBytes);
47- }
45+ if (value) chunks.push(value as Uint8Array);
4846 }
49- tail += decoder.decode();
5047 } finally {
5148 reader.releaseLock();
5249 }
53- return tail.length > maxBytes ? tail.slice(-maxBytes) : tail;
50+ const text = new TextDecoder().decode(Buffer.concat(chunks));
51+ return text.length > maxBytes ? text.slice(-maxBytes) : text;
5452 }
5553
5654 function formatSubagentDiagnostics(stdout: string, stderr: string): string {
src/agent/tools.ts+11−7
@@ -24,6 +24,7 @@ import { createSubagentTool, type SubagentOutput } from "../tools/subagent.ts";
2424 import {
2525 finalizeWriteResult,
2626 stripWriteResultMeta,
27+ type WriteResultMeta,
2728 } from "../tools/write-result.ts";
2829
2930 // ─── Tool wrappers ────────────────────────────────────────────────────────────
@@ -40,12 +41,15 @@ function withCwdDefault(
4041 return {
4142 ...tool,
4243 execute: async (input: unknown) => {
43- const withDefault = {
44- cwd,
45- ...(snapshotCallback ? { snapshotCallback } : {}),
46- ...(typeof input === "object" && input ? input : {}),
47- };
48- return originalExecute(withDefault);
44+ // P6: Mutate defaults onto the existing input object instead of
45+ // allocating a new spread object on every tool invocation.
46+ const patched = (
47+ typeof input === "object" && input !== null ? input : {}
48+ ) as Record<string, unknown>;
49+ if (patched.cwd === undefined) patched.cwd = cwd;
50+ if (snapshotCallback && patched.snapshotCallback === undefined)
51+ patched.snapshotCallback = snapshotCallback;
52+ return originalExecute(patched);
4953 },
5054 };
5155 }
@@ -92,7 +96,7 @@ function withHooks<
9296 }
9397 if (finalizeResult) {
9498 return stripWriteResultMeta(
95- result as TOutput & { _filePath: string; _before: string },
99+ result as TOutput & WriteResultMeta & { path: string },
96100 ) as TFinalOutput;
97101 }
98102 return result as TFinalOutput;
src/llm-api/api-log.ts+5−0
@@ -56,6 +56,11 @@ function sanitizeForLog(data: unknown): unknown {
5656 return result;
5757 }
5858
59+/** Returns true when the API log file writer is active. */
60+export function isApiLogEnabled(): boolean {
61+ return writer !== null;
62+}
63+
5964 export function logApiEvent(event: string, data?: unknown): void {
6065 if (!writer) return;
6166
src/llm-api/turn.ts+116−51
@@ -6,7 +6,7 @@ import {
66 stepCountIs,
77 streamText,
88 } from "ai";
9-import { logApiEvent } from "./api-log.ts";
9+import { isApiLogEnabled, logApiEvent } from "./api-log.ts";
1010 import {
1111 getCacheFamily,
1212 getCachingProviderOptions,
@@ -61,6 +61,20 @@ function safeStringify(value: unknown): string {
6161 }
6262 }
6363
64+/**
65+ * P4: Lightweight alternative to getMessageDiagnostics that computes only the
66+ * fields needed for the context-pruned yield event (no roleBreakdown / tool
67+ * contributor maps). Used when the API log is disabled.
68+ */
69+function getMessageStats(messages: CoreMessage[]): {
70+ messageCount: number;
71+ totalBytes: number;
72+} {
73+ let totalBytes = 0;
74+ for (const m of messages) totalBytes += getByteLength(safeStringify(m));
75+ return { messageCount: messages.length, totalBytes };
76+}
77+
6478 export function getMessageDiagnostics(
6579 messages: CoreMessage[],
6680 ): MessageDiagnostics {
@@ -615,6 +629,83 @@ export function stripGPTCommentaryFromHistory(
615629
616630 // ─── Main turn function ───────────────────────────────────────────────────────
617631
632+/**
633+ * P9: Merged single-pass replacement for the sequential
634+ * `stripGPTCommentaryFromHistory` + `stripOpenAIItemIdsFromHistory` calls in
635+ * `runTurn`. Both transforms are OpenAI-only; combining them into one loop
636+ * halves the number of full-array allocations on OpenAI sessions.
637+ *
638+ * The individual exported functions are kept for direct use / tests.
639+ */
640+function stripOpenAIHistoryTransforms(
641+ messages: CoreMessage[],
642+ modelString: string,
643+): CoreMessage[] {
644+ if (!isOpenAIGPT(modelString)) return messages;
645+
646+ let mutated = false;
647+ const result: CoreMessage[] = [];
648+ let skipToolResults = false;
649+
650+ for (const message of messages) {
651+ // Commentary: skip orphaned tool-result messages following a fully-dropped
652+ // assistant message.
653+ if (skipToolResults) {
654+ if (message.role === "tool") {
655+ mutated = true;
656+ continue;
657+ }
658+ skipToolResults = false;
659+ }
660+
661+ // Item-ID strip: filter providerOptions.openai.itemId from all content parts.
662+ let contentMutated = false;
663+ const strippedContent = Array.isArray(message.content)
664+ ? message.content.map((part) => {
665+ const cleaned = stripOpenAIItemIdFromPart(part);
666+ if (cleaned.changed) contentMutated = true;
667+ return cleaned.part;
668+ })
669+ : message.content;
670+
671+ const msgAfterIdStrip: CoreMessage = contentMutated
672+ ? ({
673+ ...message,
674+ content: strippedContent as CoreMessage["content"],
675+ } as CoreMessage)
676+ : message;
677+
678+ if (contentMutated) mutated = true;
679+
680+ // Commentary strip: filter phase:"commentary" text parts from assistant messages.
681+ if (
682+ message.role === "assistant" &&
683+ Array.isArray(msgAfterIdStrip.content)
684+ ) {
685+ const filtered = msgAfterIdStrip.content.filter(
686+ (part) => !isCommentaryTextPart(part),
687+ );
688+ if (filtered.length === msgAfterIdStrip.content.length) {
689+ result.push(msgAfterIdStrip);
690+ } else if (filtered.length === 0) {
691+ // Entire message was commentary: drop it and any following tool results.
692+ mutated = true;
693+ skipToolResults = true;
694+ } else {
695+ mutated = true;
696+ result.push({
697+ ...msgAfterIdStrip,
698+ content: filtered,
699+ } as CoreMessage);
700+ }
701+ } else {
702+ result.push(msgAfterIdStrip);
703+ }
704+ }
705+
706+ return mutated ? result : messages;
707+}
708+
618709 /**
619710 * Returns true when the model string refers to an OpenAI GPT model routed
620711 * through the OpenAI SDK stack (direct OpenAI or Zen OpenAI).
@@ -751,7 +842,6 @@ export async function* runTurn(options: {
751842 // OpenAI GPT models use the Responses API (@ai-sdk/openai v3 / ai v6 default),
752843 // but we keep prompts only in `system` messages for consistency across
753844 // providers and to avoid prompting shape drift on GPT reasoning models.
754- const useInstructions = false; // TODO: This is dead code, we should remove it and its usage. Keep the comment above.
755845
756846 const toolCount = Object.keys(toolSet).length;
757847 const thinkingOpts = thinkingEffort
@@ -785,32 +875,34 @@ export async function* runTurn(options: {
785875 });
786876 }
787877
788- const gptSanitizedMessages = stripGPTCommentaryFromHistory(
878+ // P9: Single-pass merged transform (commentary + item-ID strip) instead of
879+ // two sequential full-array passes; individual functions kept for tests.
880+ const openAIStrippedMessages = stripOpenAIHistoryTransforms(
789881 geminiSanitizedMessages,
790882 modelString,
791883 );
792- if (gptSanitizedMessages !== geminiSanitizedMessages) {
793- logApiEvent("gpt commentary stripped from history", { modelString });
794- }
795-
796- const openAIItemIdsStrippedMessages = stripOpenAIItemIdsFromHistory(
797- gptSanitizedMessages,
798- modelString,
799- );
800- if (openAIItemIdsStrippedMessages !== gptSanitizedMessages) {
801- logApiEvent("openai item ids stripped from history", { modelString });
884+ if (
885+ openAIStrippedMessages !== geminiSanitizedMessages &&
886+ isApiLogEnabled()
887+ ) {
888+ logApiEvent("openai history transforms applied", { modelString });
802889 }
803890
804- const prePruneDiagnostics = getMessageDiagnostics(
805- openAIItemIdsStrippedMessages,
806- );
807- logApiEvent("turn context pre-prune", prePruneDiagnostics);
891+ // P4: Only compute full diagnostics when the API log is active; otherwise
892+ // use a lightweight count+bytes helper that skips roleBreakdown/topContributors.
893+ const apiLogOn = isApiLogEnabled();
894+ const prePruneDiagnostics = apiLogOn
895+ ? getMessageDiagnostics(openAIStrippedMessages)
896+ : getMessageStats(openAIStrippedMessages);
897+ if (apiLogOn) logApiEvent("turn context pre-prune", prePruneDiagnostics);
808898 const prunedMessages = applyContextPruning(
809- openAIItemIdsStrippedMessages,
899+ openAIStrippedMessages,
810900 pruningMode,
811901 );
812- const postPruneDiagnostics = getMessageDiagnostics(prunedMessages);
813- logApiEvent("turn context post-prune", postPruneDiagnostics);
902+ const postPruneDiagnostics = apiLogOn
903+ ? getMessageDiagnostics(prunedMessages)
904+ : getMessageStats(prunedMessages);
905+ if (apiLogOn) logApiEvent("turn context post-prune", postPruneDiagnostics);
814906 if (
815907 (pruningMode === "balanced" || pruningMode === "aggressive") &&
816908 (postPruneDiagnostics.messageCount < prePruneDiagnostics.messageCount ||
@@ -834,7 +926,7 @@ export async function* runTurn(options: {
834926 prunedMessages,
835927 toolResultPayloadCapBytes,
836928 );
837- if (turnMessages !== prunedMessages) {
929+ if (turnMessages !== prunedMessages && apiLogOn) {
838930 logApiEvent("turn context post-compaction", {
839931 capBytes: toolResultPayloadCapBytes,
840932 diagnostics: getMessageDiagnostics(turnMessages),
@@ -866,17 +958,6 @@ export async function* runTurn(options: {
866958 },
867959 }
868960 : {}),
869- ...(useInstructions && systemPrompt
870- ? {
871- openai: {
872- ...(isRecord(thinkingOpts?.openai)
873- ? (thinkingOpts.openai as object)
874- : {}),
875- store: false,
876- instructions: systemPrompt,
877- },
878- }
879- : {}),
880961 };
881962
882963 if (cacheOpts) {
@@ -932,21 +1013,11 @@ export async function* runTurn(options: {
9321013 return undefined;
9331014 },
9341015
935- ...(finalSystemPrompt && !useInstructions
936- ? { system: finalSystemPrompt }
937- : {}),
1016+ ...(finalSystemPrompt ? { system: finalSystemPrompt } : {}),
9381017 ...(Object.keys(mergedProviderOptions).length > 0
9391018 ? { providerOptions: mergedProviderOptions }
9401019 : {}),
9411020 ...(signal ? { abortSignal: signal } : {}),
942- // TODO: I don't think this is ever used. we should probably remove it
943- experimental_repairToolCall: async ({ toolCall }) => {
944- // To avoid 400 Bad Request from the API when it receives malformed JSON
945- // in the assistant's tool_calls history, we repair it to a minimal valid JSON.
946- // This allows the framework to parse it, fail schema validation gracefully,
947- // and send a proper validation error back to the model without crashing the step loop.
948- return { ...toolCall, args: "{}" };
949- },
9501021 };
9511022
9521023 const result = streamText(streamOpts) as StreamTextResultFull;
@@ -978,14 +1049,8 @@ export async function* runTurn(options: {
9781049
9791050 switch (c.type) {
9801051 case "text-delta": {
981- // AI SDK v6: property is `text`, not `textDelta`
982- // TODO: Why do we still check for textDelta then?
983- const delta =
984- typeof c.text === "string"
985- ? c.text
986- : typeof c.textDelta === "string"
987- ? c.textDelta
988- : "";
1052+ // AI SDK v6: property is `text`.
1053+ const delta = typeof c.text === "string" ? c.text : "";
9891054 yield {
9901055 type: "text-delta",
9911056 delta,
src/session/db/connection.ts+5−1
@@ -170,7 +170,11 @@ export function pruneOldData(): void {
170170
171171 // Reclaim pages freed by deletions.
172172 if (deletedSessions > 0 || deletedHistory > 0) {
173- db.exec("VACUUM;");
173+ // P10: Defer VACUUM off the synchronous startup path so the CLI prompt
174+ // renders before the (potentially slow) page-reclaim scan runs.
175+ setImmediate(() => {
176+ db.exec("VACUUM;");
177+ });
174178 }
175179
176180 // Checkpoint WAL (including any pages written by VACUUM) so the WAL file
src/session/db/message-repo.ts+21−7
@@ -2,25 +2,39 @@ import { renderError } from "../../cli/output.ts";
22 import type { CoreMessage } from "../../llm-api/turn.ts";
33 import { getDb } from "./connection.ts";
44
5+// P5: Hoist prepared statement to module-level lazy singleton to avoid re-compiling SQL each call.
6+type Stmt = ReturnType<ReturnType<typeof getDb>["prepare"]>;
7+let _insertMsgStmt: Stmt | null = null;
8+function getInsertMsgStmt(): Stmt {
9+ if (!_insertMsgStmt) {
10+ _insertMsgStmt = getDb().prepare(
11+ `INSERT INTO messages (session_id, payload, turn_index, created_at)
12+ VALUES (?, ?, ?, ?)`,
13+ );
14+ }
15+ return _insertMsgStmt;
16+}
17+
518 export function saveMessages(
619 sessionId: string,
720 msgs: CoreMessage[],
821 turnIndex = 0,
922 ): void {
1023 const db = getDb();
11- const stmt = db.prepare(
12- `INSERT INTO messages (session_id, payload, turn_index, created_at)
13- VALUES (?, ?, ?, ?)`,
14- );
24+ const stmt = getInsertMsgStmt();
1525 const now = Date.now();
1626 // Persistence invariant: write CoreMessage as lossless JSON (no reshaping) so
1727 // model-authored payloads round-trip exactly, including providerOptions /
1828 // providerMetadata thought signatures and part ordering required for Gemini
1929 // tool-call replay correctness.
2030
21- for (const msg of msgs) {
22- stmt.run(sessionId, JSON.stringify(msg), turnIndex, now);
23- }
31+ // P1: Single transaction so all rows in a saveMessages call are flushed together
32+ // instead of triggering one implicit WAL flush per row.
33+ db.transaction(() => {
34+ for (const msg of msgs) {
35+ stmt.run(sessionId, JSON.stringify(msg), turnIndex, now);
36+ }
37+ })();
2438 }
2539
2640 /**
src/tools/create.ts+2−4
@@ -2,7 +2,6 @@ import { existsSync, mkdirSync } from "node:fs";
22 import { dirname } from "node:path";
33 import { z } from "zod";
44 import type { ToolDef } from "../llm-api/types.ts";
5-import { generateDiff } from "./diff.ts";
65 import { resolvePath } from "./shared.ts";
76 import type { WriteResultMeta } from "./write-result.ts";
87
@@ -18,7 +17,6 @@ type CreateInput = z.infer<typeof CreateSchema> & {
1817
1918 interface CreateOutput {
2019 path: string;
21- diff: string;
2220 created: boolean;
2321 }
2422
@@ -43,13 +41,13 @@ export const createTool: ToolDef<CreateInput, CreateToolOutput> = {
4341 await input.snapshotCallback?.(filePath);
4442 await Bun.write(filePath, input.content);
4543
46- const diff = generateDiff(relPath, before, input.content);
44+ // P3: diff deferred to finalizeWriteResult / stripWriteResultMeta.
4745 return {
4846 path: relPath,
49- diff,
5047 created,
5148 _filePath: filePath,
5249 _before: before,
50+ _updated: input.content,
5351 };
5452 },
5553 };
src/tools/diff.ts+73−49
@@ -14,69 +14,93 @@ export function generateDiff(
1414 }
1515
1616 // ---------------------------------------------------------------------------
17-// LCS-based unified diff
17+// Myers diff algorithm (Eugene Myers, 1986)
18+// O(n * d) time and space where n = max(m, n_after), d = edit distance.
19+// For typical small-edit cases this is far more memory-efficient than the
20+// previous LCS approach which always allocated O(m * n) space.
1821 // ---------------------------------------------------------------------------
1922
20-/**
21- * Compute the longest common subsequence lengths table for two string arrays.
22- * Returns a flat (m+1)*(n+1) array where index i*(n+1)+j holds the LCS length
23- * of before[0..i-1] and after[0..j-1].
24- */
25-function lcsTable(before: string[], after: string[]): number[] {
26- const m = before.length;
27- const n = after.length;
28- const w = n + 1;
29- const dp: number[] = new Array<number>((m + 1) * w).fill(0);
30- for (let i = 1; i <= m; i++) {
31- for (let j = 1; j <= n; j++) {
32- if (before[i - 1] === after[j - 1]) {
33- dp[i * w + j] = (dp[(i - 1) * w + (j - 1)] ?? 0) + 1;
34- } else {
35- dp[i * w + j] = Math.max(
36- dp[(i - 1) * w + j] ?? 0,
37- dp[i * w + (j - 1)] ?? 0,
38- );
39- }
40- }
41- }
42- return dp;
43-}
44-
4523 type EditKind = "eq" | "del" | "ins";
4624 type EditOp = { kind: EditKind; line: string };
4725
4826 /**
49- * Backtrack through the LCS table to produce a sequence of edit operations:
50- * - "eq" : line present in both (context)
51- * - "del" : line only in before (removed)
52- * - "ins" : line only in after (added)
27+ * Produce a minimal edit script (sequence of eq/del/ins operations) using the
28+ * Myers diff algorithm with trace-based backtracking.
5329 */
5430 function editScript(before: string[], after: string[]): EditOp[] {
31+ const m = before.length;
5532 const n = after.length;
56- const w = n + 1;
57- const dp = lcsTable(before, after);
5833
34+ if (m === 0) return after.map((line) => ({ kind: "ins" as const, line }));
35+ if (n === 0) return before.map((line) => ({ kind: "del" as const, line }));
36+
37+ const max = m + n;
38+ const offset = max; // shift so negative diagonal indices are valid array indices
39+ // V[k + offset] = furthest x reached on diagonal k
40+ const V = new Int32Array(2 * max + 2);
41+ // Snapshot of V at the start of each edit-distance iteration d, used for
42+ // backtracking. trace[d] is the state when d edits have already been applied.
43+ const trace: Int32Array[] = [];
44+
45+ outer: for (let d = 0; d <= max; d++) {
46+ trace.push(V.slice());
47+ for (let k = -d; k <= d; k += 2) {
48+ // Decide whether to move down (insert from after) or right (delete from before).
49+ const fromInsert =
50+ k === -d ||
51+ (k !== d &&
52+ (V[k - 1 + offset] as number) < (V[k + 1 + offset] as number));
53+ let x = fromInsert
54+ ? (V[k + 1 + offset] as number)
55+ : (V[k - 1 + offset] as number) + 1;
56+ let y = x - k;
57+ // Follow the snake: advance while lines are equal.
58+ while (x < m && y < n && before[x] === after[y]) {
59+ x++;
60+ y++;
61+ }
62+ V[k + offset] = x;
63+ if (x >= m && y >= n) break outer;
64+ }
65+ }
66+
67+ // Backtrack through the trace snapshots to reconstruct the edit script in
68+ // reverse, then flip at the end.
5969 const ops: EditOp[] = [];
60- let i = before.length;
61- let j = after.length;
62- while (i > 0 || j > 0) {
63- const bLine = before[i - 1] ?? "";
64- const aLine = after[j - 1] ?? "";
65- if (i > 0 && j > 0 && bLine === aLine) {
66- ops.push({ kind: "eq", line: bLine });
67- i--;
68- j--;
69- } else if (
70- j > 0 &&
71- (i === 0 || (dp[i * w + (j - 1)] ?? 0) >= (dp[(i - 1) * w + j] ?? 0))
72- ) {
73- ops.push({ kind: "ins", line: aLine });
74- j--;
70+ let x = m;
71+ let y = n;
72+
73+ for (let d = trace.length - 1; d >= 1; d--) {
74+ const Vd = trace[d] as Int32Array;
75+ const k = x - y;
76+ const fromInsert =
77+ k === -d ||
78+ (k !== d &&
79+ (Vd[k - 1 + offset] as number) < (Vd[k + 1 + offset] as number));
80+ const prevX = fromInsert
81+ ? (Vd[k + 1 + offset] as number)
82+ : (Vd[k - 1 + offset] as number);
83+ const prevY = prevX - (fromInsert ? k + 1 : k - 1);
84+
85+ // Snake: equal lines between the end of this edit and the current position.
86+ // Recorded in reverse because we are walking backwards.
87+ for (let i = x - 1; i >= prevX + (fromInsert ? 0 : 1); i--) {
88+ ops.push({ kind: "eq", line: before[i] as string });
89+ }
90+ if (fromInsert) {
91+ ops.push({ kind: "ins", line: after[prevY] as string });
7592 } else {
76- ops.push({ kind: "del", line: bLine });
77- i--;
93+ ops.push({ kind: "del", line: before[prevX] as string });
7894 }
95+ x = prevX;
96+ y = prevY;
97+ }
98+
99+ // Remaining equal prefix (before the first edit).
100+ for (let i = x - 1; i >= 0; i--) {
101+ ops.push({ kind: "eq", line: before[i] as string });
79102 }
103+
80104 ops.reverse();
81105 return ops;
82106 }
src/tools/hooks.ts+12−6
@@ -1,6 +1,8 @@
11 import { accessSync, constants } from "node:fs";
22 import { homedir } from "node:os";
33 import { join } from "node:path";
4+import { generateDiff } from "./diff.ts";
5+import type { WriteResultMeta } from "./write-result.ts";
46
57 // ─── Hook discovery ───────────────────────────────────────────────────────────
68
@@ -69,40 +71,44 @@ export async function runHook(
6971
7072 // ─── Env builders (one per tool) ─────────────────────────────────────────────
7173
74+// P3: Accept WriteResultMeta so diff is computed here (only when a hook fires)
75+// instead of being pre-computed and then discarded in applyFileEdit.
76+type WriteResult = WriteResultMeta & { path: string };
77+
7278 export function hookEnvForCreate(
73- result: { path: string; diff: string; created: boolean },
79+ result: WriteResult & { created: boolean },
7480 cwd: string,
7581 ): Record<string, string> {
7682 return {
7783 TOOL: "create",
7884 FILEPATH: result.path,
79- DIFF: result.diff,
85+ DIFF: generateDiff(result.path, result._before, result._updated),
8086 CREATED: String(result.created),
8187 CWD: cwd,
8288 };
8389 }
8490
8591 export function hookEnvForReplace(
86- result: { path: string; diff: string; deleted: boolean },
92+ result: WriteResult & { deleted: boolean },
8793 cwd: string,
8894 ): Record<string, string> {
8995 return {
9096 TOOL: "replace",
9197 FILEPATH: result.path,
92- DIFF: result.diff,
98+ DIFF: generateDiff(result.path, result._before, result._updated),
9399 DELETED: String(result.deleted),
94100 CWD: cwd,
95101 };
96102 }
97103
98104 export function hookEnvForInsert(
99- result: { path: string; diff: string },
105+ result: WriteResult,
100106 cwd: string,
101107 ): Record<string, string> {
102108 return {
103109 TOOL: "insert",
104110 FILEPATH: result.path,
105- DIFF: result.diff,
111+ DIFF: generateDiff(result.path, result._before, result._updated),
106112 CWD: cwd,
107113 };
108114 }
src/tools/insert.test.ts+15−12
@@ -1,9 +1,12 @@
11 import { afterEach, beforeEach, describe, expect, test } from "bun:test";
22 import { insertTool } from "./insert.ts";
3-
43 import { createTestHelpers } from "./test-helpers.ts";
4+import { stripWriteResultMeta } from "./write-result.ts";
55
66 const { setup, teardown, write, read, anchor, getDir } = createTestHelpers();
7+// P3: diff is deferred; strip meta before asserting on public fields.
8+const execute = async (...args: Parameters<typeof insertTool.execute>) =>
9+ stripWriteResultMeta(await insertTool.execute(...args));
710
811 // ---------------------------------------------------------------------------
912 // Setup / teardown
@@ -24,7 +27,7 @@ afterEach(async () => {
2427 describe("insertTool", () => {
2528 test("inserts a single line after the anchor", async () => {
2629 const name = await write("f.txt", "a\nb\nc\n");
27- const result = await insertTool.execute({
30+ const result = await execute({
2831 path: name,
2932 cwd: getDir(),
3033 anchor: anchor(1, "a"),
@@ -40,7 +43,7 @@ describe("insertTool", () => {
4043
4144 test("inserts a single line before the anchor", async () => {
4245 const name = await write("f.txt", "a\nb\nc\n");
43- const result = await insertTool.execute({
46+ const result = await execute({
4447 path: name,
4548 cwd: getDir(),
4649 anchor: anchor(2, "b"),
@@ -55,7 +58,7 @@ describe("insertTool", () => {
5558
5659 test("inserts multiple lines", async () => {
5760 const name = await write("f.txt", "a\nb\n");
58- await insertTool.execute({
61+ await execute({
5962 path: name,
6063 cwd: getDir(),
6164 anchor: anchor(1, "a"),
@@ -68,7 +71,7 @@ describe("insertTool", () => {
6871
6972 test("inserts before the first line", async () => {
7073 const name = await write("f.txt", "a\nb\n");
71- await insertTool.execute({
74+ await execute({
7275 path: name,
7376 cwd: getDir(),
7477 anchor: anchor(1, "a"),
@@ -81,7 +84,7 @@ describe("insertTool", () => {
8184
8285 test("inserts after the last line", async () => {
8386 const name = await write("f.txt", "a\nb\n");
84- await insertTool.execute({
87+ await execute({
8588 path: name,
8689 cwd: getDir(),
8790 anchor: anchor(2, "b"),
@@ -94,7 +97,7 @@ describe("insertTool", () => {
9497
9598 test("returns path relative to cwd", async () => {
9699 const name = await write("f.txt", "a\nb\n");
97- const result = await insertTool.execute({
100+ const result = await execute({
98101 path: name,
99102 cwd: getDir(),
100103 anchor: anchor(1, "a"),
@@ -107,7 +110,7 @@ describe("insertTool", () => {
107110
108111 test("diff contains only added lines, no removals", async () => {
109112 const name = await write("f.txt", "a\nb\nc\n");
110- const result = await insertTool.execute({
113+ const result = await execute({
111114 path: name,
112115 cwd: getDir(),
113116 anchor: anchor(2, "b"),
@@ -121,7 +124,7 @@ describe("insertTool", () => {
121124
122125 test("throws when file does not exist", async () => {
123126 await expect(
124- insertTool.execute({
127+ execute({
125128 path: "missing.txt",
126129 cwd: getDir(),
127130 anchor: "1:00",
@@ -134,7 +137,7 @@ describe("insertTool", () => {
134137 test("throws when anchor hash does not match", async () => {
135138 const name = await write("f.txt", "a\nb\nc\n");
136139 await expect(
137- insertTool.execute({
140+ execute({
138141 path: name,
139142 cwd: getDir(),
140143 anchor: "2:ff", // wrong hash for "b"
@@ -147,7 +150,7 @@ describe("insertTool", () => {
147150 test("throws on malformed anchor format", async () => {
148151 const name = await write("f.txt", "a\n");
149152 await expect(
150- insertTool.execute({
153+ execute({
151154 path: name,
152155 cwd: getDir(),
153156 anchor: "not-an-anchor",
@@ -158,7 +161,7 @@ describe("insertTool", () => {
158161 });
159162 test("accepts anchor with trailing pipe separator", async () => {
160163 const name = await write("f.txt", "a\nb\nc\n");
161- await insertTool.execute({
164+ await execute({
162165 path: name,
163166 cwd: getDir(),
164167 anchor: `${anchor(2, "b")}|`,
src/tools/insert.ts+0−1
@@ -20,7 +20,6 @@ type InsertInput = z.infer<typeof InsertSchema> & {
2020
2121 interface InsertOutput {
2222 path: string;
23- diff: string;
2423 }
2524
2625 export interface InsertToolOutput extends InsertOutput, WriteResultMeta {}
src/tools/replace.test.ts+15−12
@@ -1,9 +1,12 @@
11 import { afterEach, beforeEach, describe, expect, test } from "bun:test";
22 import { replaceTool } from "./replace.ts";
3-
43 import { createTestHelpers } from "./test-helpers.ts";
4+import { stripWriteResultMeta } from "./write-result.ts";
55
66 const { setup, teardown, write, read, anchor, getDir } = createTestHelpers();
7+// P3: diff is deferred; strip meta before asserting on public fields.
8+const execute = async (...args: Parameters<typeof replaceTool.execute>) =>
9+ stripWriteResultMeta(await replaceTool.execute(...args));
710
811 // ---------------------------------------------------------------------------
Cut short: this commit's diff is over 1000 lines.
src/tools/replace.ts+0−1
Cut short: this commit's diff is over 1000 lines.
src/tools/shared.ts+3−2
Cut short: this commit's diff is over 1000 lines.
src/tools/write-result.ts+23−10
Cut short: this commit's diff is over 1000 lines.