xonecas.io
64875
Mc

mini-coder / commit 92ad1df

fix: reduce sqlite busy errors during message appends

xonecas2026-04-12 92ad1df41c971d1eb6ce510d600e6f2caa731ed3 parent eb6bd79

3 files changed

TODO.md+3−4
@@ -7,16 +7,15 @@ The spec.md and code are the sources of truth, not this file, don't assume anyth
77
88 ## Open items
99
10-- [ ] `shell` results are flattened into one text blob (`src/tools.ts`, `src/ui/conversation.ts`)
1110 - [ ] Invalid `settings.json` is fatal instead of falling back to no saved settings (`src/settings.ts`)
12-- [ ] Escape-over-overlay behavior is not encoded in app code (`src/ui/commands.ts`, `src/ui.ts`)
13-- [ ] Custom provider discovery ignores configured `apiKey` (`src/index.ts`)
1411
1512 ## Low priority (deferred until further notice)
1613
14+- [ ] Escape-over-overlay behavior is not encoded in app code (`src/ui/commands.ts`, `src/ui.ts`)
15+- [ ] Custom provider discovery ignores configured `apiKey` (`src/index.ts`)
1716 - [ ] Slash-command autocomplete does not preserve the draft (`src/ui/commands.ts`)
18-- [ ] Many `mc` instances on the same host can cause SQL Busy errors
1917 - [ ] `/session` can switch sessions mid-run (`ui/commands.ts`, `ui/agent.ts`)
18+- [ ] `shell` results are flattened into one text blob (`src/tools.ts`, `src/ui/conversation.ts`)
2019 - [ ] Custom XML parsing and frontmatter parsing are still in use (`skills.ts`, `prompt.ts`)
2120
2221 ## Plugin related (documented only, no plugin-related work is planned right now)
src/session.test.ts+106−0
@@ -1,4 +1,7 @@
11 import { describe, expect, test } from "bun:test";
2+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { join } from "node:path";
25 import type {
36 AssistantMessage,
47 Message,
@@ -86,6 +89,31 @@ function loadSessionOrThrow(db: ReturnType<typeof openDatabase>, id: string) {
8689 return session;
8790 }
8891
92+async function waitForChildStdout(
93+ stdout: ReadableStream<Uint8Array> | number | null,
94+ needle: string,
95+): Promise<void> {
96+ if (!(stdout instanceof ReadableStream)) {
97+ throw new Error("Expected child stdout to be piped");
98+ }
99+
100+ const reader = stdout.getReader();
101+ const decoder = new TextDecoder();
102+ let output = "";
103+
104+ while (!output.includes(needle)) {
105+ const { done, value } = await reader.read();
106+ if (done) {
107+ break;
108+ }
109+ output += decoder.decode(value, { stream: true });
110+ }
111+
112+ if (!output.includes(needle)) {
113+ throw new Error(`Child exited before emitting ${JSON.stringify(needle)}`);
114+ }
115+}
116+
89117 // ---------------------------------------------------------------------------
90118 // Tests
91119 // ---------------------------------------------------------------------------
@@ -205,6 +233,17 @@ describe("session persistence", () => {
205233 });
206234 });
207235
236+describe("database initialization", () => {
237+ test("openDatabase enables a busy timeout for transient writer contention", () => {
238+ const db = openDatabase(":memory:");
239+
240+ const row = db.query<{ timeout: number }, []>("PRAGMA busy_timeout").get();
241+
242+ expect(row?.timeout).toBeGreaterThan(0);
243+ db.close();
244+ });
245+});
246+
208247 describe("message persistence and turn numbering", () => {
209248 test("first user message gets turn 1", () => {
210249 const db = openDatabase(":memory:");
@@ -328,6 +367,73 @@ describe("message persistence and turn numbering", () => {
328367 expect(updated.updatedAt).toBeGreaterThanOrEqual(before);
329368 db.close();
330369 });
370+
371+ test("appendMessage when another writer commits first waits and assigns the next turn", async () => {
372+ const tempDir = mkdtempSync(join(tmpdir(), "mini-coder-session-test-"));
373+ const dbPath = join(tempDir, "session.db");
374+ const lockScriptPath = join(tempDir, "hold-lock.ts");
375+ const db = openDatabase(dbPath);
376+ const session = createSession(db, { cwd: "/tmp/test" });
377+
378+ writeFileSync(
379+ lockScriptPath,
380+ [
381+ 'import { Database } from "bun:sqlite";',
382+ "const dbPath = process.argv[2];",
383+ "const sessionId = process.argv[3];",
384+ 'if (!dbPath || !sessionId) throw new Error("missing args");',
385+ "const db = new Database(dbPath);",
386+ 'db.run("PRAGMA journal_mode = WAL");',
387+ 'db.run("BEGIN IMMEDIATE");',
388+ "const now = Date.now();",
389+ 'db.run("INSERT INTO messages (session_id, turn, data, created_at) VALUES (?, ?, ?, ?)", [',
390+ " sessionId,",
391+ " 1,",
392+ ' JSON.stringify({ role: "user", content: "locked", timestamp: now }),',
393+ " now,",
394+ "]);",
395+ 'db.run("UPDATE sessions SET updated_at = ? WHERE id = ?", [now, sessionId]);',
396+ 'console.log("locked");',
397+ "await Bun.sleep(250);",
398+ 'db.run("COMMIT");',
399+ "db.close();",
400+ "",
401+ ].join("\n"),
402+ "utf-8",
403+ );
404+
405+ const child = Bun.spawn(
406+ [process.execPath, lockScriptPath, dbPath, session.id],
407+ {
408+ stdout: "pipe",
409+ stderr: "pipe",
410+ },
411+ );
412+
413+ try {
414+ await waitForChildStdout(child.stdout, "locked");
415+
416+ const turn = appendMessage(db, session.id, makeUser("after lock"));
417+ expect(turn).toBe(2);
418+
419+ const exitCode = await child.exited;
420+ const stderr = child.stderr
421+ ? await new Response(child.stderr).text()
422+ : "";
423+ expect({ exitCode, stderr }).toEqual({ exitCode: 0, stderr: "" });
424+
425+ const turns = db
426+ .query<{ turn: number | null }, [string]>(
427+ "SELECT turn FROM messages WHERE session_id = ? ORDER BY id",
428+ )
429+ .all(session.id)
430+ .map((row) => row.turn);
431+ expect(turns).toEqual([1, 2]);
432+ } finally {
433+ db.close();
434+ rmSync(tempDir, { recursive: true, force: true });
435+ }
436+ });
331437 });
332438
333439 describe("undo", () => {
src/session.ts+47−17
@@ -160,6 +160,8 @@ const EMPTY_ASSISTANT_USAGE: AssistantMessage["usage"] = {
160160 },
161161 };
162162
163+const SQLITE_BUSY_TIMEOUT_MS = 1_000;
164+
163165 /** Row shape returned by `SELECT * FROM prompt_history`. */
164166 type PromptHistoryRow = {
165167 id: number;
@@ -253,6 +255,7 @@ CREATE INDEX IF NOT EXISTS idx_prompt_history_created_at ON prompt_history(creat
253255 export function openDatabase(path: string): Database {
254256 const db = new Database(path);
255257 db.run("PRAGMA journal_mode = WAL");
258+ db.run(`PRAGMA busy_timeout = ${SQLITE_BUSY_TIMEOUT_MS}`);
256259 db.run("PRAGMA foreign_keys = ON");
257260 db.exec(SCHEMA);
258261 return db;
@@ -709,6 +712,31 @@ export function getAssistantUsage(
709712 };
710713 }
711714
715+function runInImmediateTransaction<T>(db: Database, callback: () => T): T {
716+ if (db.inTransaction) {
717+ return callback();
718+ }
719+
720+ db.run("BEGIN IMMEDIATE");
721+ try {
722+ const result = callback();
723+ db.run("COMMIT");
724+ return result;
725+ } catch (error) {
726+ if (db.inTransaction) {
727+ try {
728+ db.run("ROLLBACK");
729+ } catch (rollbackError) {
730+ throw new AggregateError(
731+ [error, rollbackError],
732+ "Failed to roll back SQLite transaction",
733+ );
734+ }
735+ }
736+ throw error;
737+ }
738+}
739+
712740 /**
713741 * Append a UI-only message to a session's history.
714742 *
@@ -790,25 +818,27 @@ export function appendMessage(
790818 message: PersistedMessage,
791819 turn?: number,
792820 ): number | null {
793- const now = Date.now();
794-
795- let effectiveTurn: number | null;
796- if (isUiMessage(message)) {
797- effectiveTurn = null;
798- } else if (turn !== undefined) {
799- effectiveTurn = turn;
800- } else {
801- const row = db.query<MaxTurnRow, [string]>(SQL.maxTurn).get(sessionId);
802- effectiveTurn = (row?.max_turn ?? 0) + 1;
803- }
821+ return runInImmediateTransaction(db, () => {
822+ const now = Date.now();
823+
824+ let effectiveTurn: number | null;
825+ if (isUiMessage(message)) {
826+ effectiveTurn = null;
827+ } else if (turn !== undefined) {
828+ effectiveTurn = turn;
829+ } else {
830+ const row = db.query<MaxTurnRow, [string]>(SQL.maxTurn).get(sessionId);
831+ effectiveTurn = (row?.max_turn ?? 0) + 1;
832+ }
804833
805- db.run(
806- "INSERT INTO messages (session_id, turn, data, created_at) VALUES (?, ?, ?, ?)",
807- [sessionId, effectiveTurn, JSON.stringify(message), now],
808- );
809- db.run("UPDATE sessions SET updated_at = ? WHERE id = ?", [now, sessionId]);
834+ db.run(
835+ "INSERT INTO messages (session_id, turn, data, created_at) VALUES (?, ?, ?, ?)",
836+ [sessionId, effectiveTurn, JSON.stringify(message), now],
837+ );
838+ db.run("UPDATE sessions SET updated_at = ? WHERE id = ?", [now, sessionId]);
810839
811- return effectiveTurn;
840+ return effectiveTurn;
841+ });
812842 }
813843
814844 /**