xonecas.io
64875
Mc

mini-coder / commit cabcbc7

fix(session): make startup sqlite maintenance opportunistic

xonecas2026-03-14 cabcbc7f8a68f5273489d6285ffe37fa24d75855 parent 1571d49

2 files changed

src/session/db/connection.test.ts+18−0
@@ -0,0 +1,18 @@
1+import { describe, expect, test } from "bun:test";
2+import { isSqliteBusyError } from "./connection.ts";
3+
4+describe("isSqliteBusyError", () => {
5+ test("matches sqlite busy messages", () => {
6+ expect(
7+ isSqliteBusyError(new Error("SQLiteError: database is locked")),
8+ ).toBe(true);
9+ expect(
10+ isSqliteBusyError(new Error("SQLITE_BUSY: database is locked")),
11+ ).toBe(true);
12+ });
13+
14+ test("ignores unrelated errors", () => {
15+ expect(isSqliteBusyError(new Error("network timeout"))).toBe(false);
16+ expect(isSqliteBusyError("database is locked")).toBe(false);
17+ });
18+});
src/session/db/connection.ts+44−16
@@ -110,12 +110,33 @@ const SCHEMA = `
110110
111111 let _db: Database | null = null;
112112
113+export function isSqliteBusyError(err: unknown): boolean {
114+ if (!(err instanceof Error)) return false;
115+ const message = err.message.toLowerCase();
116+ return (
117+ message.includes("database is locked") || message.includes("sqlite_busy")
118+ );
119+}
120+
121+function runBestEffortMaintenance(task: () => void): void {
122+ try {
123+ task();
124+ } catch (err) {
125+ if (!isSqliteBusyError(err)) throw err;
126+ }
127+}
128+
129+function configureDb(db: Database): void {
130+ db.exec("PRAGMA journal_mode=WAL;");
131+ db.exec("PRAGMA foreign_keys=ON;");
132+ db.exec("PRAGMA busy_timeout=1000;");
133+}
134+
113135 export function getDb(): Database {
114136 if (!_db) {
115137 const dbPath = getDbPath();
116138 let db = new Database(dbPath, { create: true });
117- db.exec("PRAGMA journal_mode=WAL;");
118- db.exec("PRAGMA foreign_keys=ON;");
139+ configureDb(db);
119140
120141 const version =
121142 db.query<{ user_version: number }, []>("PRAGMA user_version").get()
@@ -130,8 +151,7 @@ export function getDb(): Database {
130151 if (existsSync(path)) unlinkSync(path);
131152 }
132153 db = new Database(dbPath, { create: true });
133- db.exec("PRAGMA journal_mode=WAL;");
134- db.exec("PRAGMA foreign_keys=ON;");
154+ configureDb(db);
135155 db.exec(SCHEMA);
136156 db.exec(`PRAGMA user_version = ${DB_VERSION};`);
137157 } else {
@@ -153,31 +173,39 @@ const MAX_PROMPT_HISTORY = 500;
153173 */
154174 export function pruneOldData(): void {
155175 const db = getDb();
176+ let deletedSessions = 0;
177+ let deletedHistory = 0;
156178
157- const deletedSessions = db.run(
158- `DELETE FROM sessions WHERE id NOT IN (
179+ runBestEffortMaintenance(() => {
180+ deletedSessions = db.run(
181+ `DELETE FROM sessions WHERE id NOT IN (
159182 SELECT id FROM sessions ORDER BY updated_at DESC LIMIT ?
160183 )`,
161- [MAX_SESSIONS],
162- ).changes;
184+ [MAX_SESSIONS],
185+ ).changes;
163186
164- const deletedHistory = db.run(
165- `DELETE FROM prompt_history WHERE id NOT IN (
187+ deletedHistory = db.run(
188+ `DELETE FROM prompt_history WHERE id NOT IN (
166189 SELECT id FROM prompt_history ORDER BY id DESC LIMIT ?
167190 )`,
168- [MAX_PROMPT_HISTORY],
169- ).changes;
191+ [MAX_PROMPT_HISTORY],
192+ ).changes;
193+ });
170194
171195 // Reclaim pages freed by deletions.
172196 if (deletedSessions > 0 || deletedHistory > 0) {
173197 // Defer VACUUM off the synchronous startup path so the CLI prompt
174198 // renders before the (potentially slow) page-reclaim scan runs.
175199 setImmediate(() => {
176- db.exec("VACUUM;");
200+ runBestEffortMaintenance(() => {
201+ db.exec("VACUUM;");
202+ });
177203 });
178204 }
179205
180- // Checkpoint WAL (including any pages written by VACUUM) so the WAL file
181- // shrinks back to near-zero.
182- db.exec("PRAGMA wal_checkpoint(TRUNCATE);");
206+ // Passive checkpoint keeps startup opportunistic: if another process is using
207+ // the database, we skip shrinking the WAL instead of failing the whole CLI.
208+ runBestEffortMaintenance(() => {
209+ db.exec("PRAGMA wal_checkpoint(PASSIVE);");
210+ });
183211 }