Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: recover local store on start

Samrat Talukdar1917da972e4a
diff --git a/apps/server/src/runtime-db-journal.test.ts b/apps/server/src/runtime-db-journal.test.ts
new file mode 100644
index 00000000..7232e54f
--- /dev/null
+++ b/apps/server/src/runtime-db-journal.test.ts
@@ -0,0 +1,137 @@
+import assert from "node:assert/strict";
+import { spawn } from "node:child_process";
+import { chmodSync, existsSync, statSync } from "node:fs";
+import { mkdtemp, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import test from "node:test";
+import {
+ RuntimeStoreReadonlyError,
+ isSqliteReadonlyError,
+ readRuntimeSqliteWithRecovery,
+} from "./runtime-db.js";
+import { listRuntimeOpencodeConfigRows } from "./runtime-opencode-config-store.js";
+import type { ServerConfig } from "./types.js";
+
+/**
+ * A child writes runtime_opencode_configs rows, opens a write transaction
+ * that spills pages into the file, and is killed mid-transaction: the
+ * `-journal` it leaves is hot, as after an app killed mid-commit.
+ */
+const WRITER = `
+const path = process.argv[process.argv.length - 1];
+const open = async () => {
+ if (typeof process.versions.bun === "string") {
+ const { Database } = await import("bun:sqlite");
+ const db = new Database(path, { create: true });
+ return { exec: (sql) => db.exec(sql), run: (sql, ...args) => db.query(sql).run(...args) };
+ }
+ const { DatabaseSync } = await import("node:sqlite");
+ const db = new DatabaseSync(path);
+ return { exec: (sql) => db.exec(sql), run: (sql, ...args) => db.prepare(sql).run(...args) };
+};
+const db = await open();
+db.exec("CREATE TABLE IF NOT EXISTS runtime_opencode_configs (workspace_id TEXT PRIMARY KEY NOT NULL, config_json TEXT NOT NULL, updated_at INTEGER NOT NULL)");
+for (let i = 0; i < 200; i += 1) db.run("INSERT OR REPLACE INTO runtime_opencode_configs VALUES (?, ?, ?)", "w" + i, JSON.stringify({ note: "x".repeat(4000) }), i);
+db.exec("PRAGMA cache_size = 2");
+db.exec("BEGIN IMMEDIATE");
+for (let i = 0; i < 200; i += 1) db.run("UPDATE runtime_opencode_configs SET config_json = ? WHERE workspace_id = ?", JSON.stringify({ note: "y".repeat(4000) }), "w" + i);
+process.stdout.write("ready\\n");
+setInterval(() => {}, 1000);
+`;
+
+async function leaveHotJournal(root: string, dbPath: string): Promise<void> {
+ const script = join(root, "writer.mjs");
+ await writeFile(script, WRITER);
+ const child = spawn(process.execPath, [script, dbPath], { stdio: ["ignore", "pipe", "inherit"] });
+ await new Promise<void>((resolve, reject) => {
+ child.stdout.on("data", (chunk: Buffer) => {
+ if (chunk.toString().includes("ready")) resolve();
+ });
+ child.on("exit", (code) => reject(new Error(`writer exited early (${code})`)));
+ });
+ const exited = new Promise((resolve) => child.once("exit", resolve));
+ child.kill("SIGKILL");
+ await exited;
+ assert.ok(statSync(`${dbPath}-journal`).size > 0, "the killed writer left a journal");
+}
+
+async function readOnlyCount(dbPath: string): Promise<number> {
+ if (typeof process.versions.bun === "string") {
+ const { Database } = await import("bun:sqlite");
+ const db = new Database(dbPath, { readonly: true, create: false });
+ try {
+ return Number((db.query("SELECT count(*) AS c FROM runtime_opencode_configs").get() as { c: number }).c);
+ } finally {
+ db.close();
+ }
+ }
+ const { DatabaseSync } = await import("node:sqlite");
+ const db = new DatabaseSync(dbPath, { readOnly: true });
+ try {
+ return Number((db.prepare("SELECT count(*) AS c FROM runtime_opencode_configs").get() as { c: number }).c);
+ } finally {
+ db.close();
+ }
+}
+
+test("a hot journal fails read-only reads until it is rolled back", async () => {
+ const root = await mkdtemp(join(tmpdir(), "omnirush-runtime-journal-"));
+ try {
+ const dbPath = join(root, "runtime.sqlite");
+ await leaveHotJournal(root, dbPath);
+ // What every start of 3.5.4 hit: SQLITE_READONLY_ROLLBACK on the first read.
+ await assert.rejects(() => readOnlyCount(dbPath), (error: unknown) => isSqliteReadonlyError(error));
+ assert.equal(await readRuntimeSqliteWithRecovery(dbPath, () => readOnlyCount(dbPath)), 200);
+ assert.equal(existsSync(`${dbPath}-journal`), false, "the journal was rolled back");
+ assert.equal(await readOnlyCount(dbPath), 200);
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+});
+
+test("the start's first read of the runtime store recovers from a hot journal", async () => {
+ const root = await mkdtemp(join(tmpdir(), "omnirush-runtime-journal-"));
+ const previous = process.env.OMNIRUSH_RUNTIME_DB;
+ try {
+ const dbPath = join(root, "runtime.sqlite");
+ await leaveHotJournal(root, dbPath);
+ process.env.OMNIRUSH_RUNTIME_DB = dbPath;
+ const rows = await listRuntimeOpencodeConfigRows({} as ServerConfig);
+ assert.equal(rows.length, 200);
+ // The rolled-back transaction's updates are gone: the rows are as last committed.
+ assert.equal(JSON.stringify(rows[0]?.value).includes("yyyy"), false);
+ } finally {
+ if (previous === undefined) delete process.env.OMNIRUSH_RUNTIME_DB;
+ else process.env.OMNIRUSH_RUNTIME_DB = previous;
+ await rm(root, { recursive: true, force: true });
+ }
+});
+
+test("a runtime store that cannot be written is reported as read-only", async (t) => {
+ if (process.platform !== "win32" && typeof process.getuid === "function" && process.getuid() === 0) {
+ t.skip("root writes read-only files");
+ return;
+ }
+ const root = await mkdtemp(join(tmpdir(), "omnirush-runtime-journal-"));
+ const dbPath = join(root, "runtime.sqlite");
+ try {
+ await leaveHotJournal(root, dbPath);
+ // The read-only attribute (Windows) / no write bit: the read-write open falls back to read-only.
+ chmodSync(dbPath, 0o444);
+ await assert.rejects(
+ () => readRuntimeSqliteWithRecovery(dbPath, () => readOnlyCount(dbPath)),
+ (error: unknown) => error instanceof RuntimeStoreReadonlyError
+ && (error as { code?: string }).code === "SQLITE_READONLY"
+ && /readonly database \(runtime store\)/.test((error as Error).message),
+ );
+ } finally {
+ chmodSync(dbPath, 0o644);
+ await rm(root, { recursive: true, force: true });
+ }
+});
+
+test("other read errors pass through untouched", async () => {
+ const boom = new Error("no such table: runtime_opencode_configs");
+ await assert.rejects(() => readRuntimeSqliteWithRecovery("/nonexistent/runtime.sqlite", () => { throw boom; }), (error) => error === boom);
+});
diff --git a/apps/server/src/runtime-db.ts b/apps/server/src/runtime-db.ts
index 2ae0785f..db9be4c3 100644
--- a/apps/server/src/runtime-db.ts
+++ b/apps/server/src/runtime-db.ts
@@ -1,3 +1,4 @@
+import { existsSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { omnirushConfigDir } from "@omnirush/paths";
import type { Database as BunDatabase } from "bun:sqlite";
@@ -67,3 +68,87 @@ export async function openRuntimeSqliteDatabase(path: string): Promise<RuntimeSq
close: () => sqlite.close(),
};
}
+
+/** SQLite's primary result code (8 = SQLITE_READONLY) of a node:sqlite or bun:sqlite error, else null. */
+export function sqlitePrimaryCode(error: unknown): number | null {
+ if (!error || typeof error !== "object") return null;
+ const record = error as { errcode?: unknown; errno?: unknown; code?: unknown };
+ if (typeof record.errcode === "number" && Number.isInteger(record.errcode)) return record.errcode & 0xff;
+ if (typeof record.code === "string" && record.code.startsWith("SQLITE_") && typeof record.errno === "number") return record.errno & 0xff;
+ return null;
+}
+
+/** "attempt to write a readonly database": a read-only file, or a read-only connection that met a hot journal. */
+export function isSqliteReadonlyError(error: unknown): boolean {
+ if (sqlitePrimaryCode(error) === 8) return true;
+ const code = error && typeof error === "object" ? (error as { code?: unknown }).code : undefined;
+ if (typeof code === "string" && code.startsWith("SQLITE_READONLY")) return true;
+ return error instanceof Error && /attempt to write a readonly database/i.test(error.message);
+}
+
+/**
+ * A write cut short (the app killed or the PC shut down mid-commit, or on
+ * Windows a journal an antivirus held open so it could not be deleted) leaves
+ * a hot `<db>-journal`. Only a read-write connection rolls it back; every
+ * read-only connection fails with SQLITE_READONLY_ROLLBACK ("attempt to write
+ * a readonly database") until one does. Opening the file read-write and
+ * reading once rolls it back. Throws what that open threw (for instance when
+ * the file itself cannot be written).
+ */
+export async function rollBackRuntimeSqliteJournal(path: string): Promise<void> {
+ if (!existsSync(path)) return;
+ if (typeof process.versions.bun === "string") {
+ const { Database } = await import("bun:sqlite");
+ const sqlite = new Database(path, { create: false, readwrite: true });
+ try {
+ sqlite.query("SELECT count(*) FROM sqlite_master").get();
+ } finally {
+ sqlite.close();
+ }
+ return;
+ }
+ const { DatabaseSync } = await importNodeSqlite();
+ const sqlite = new DatabaseSync(path);
+ try {
+ sqlite.prepare("SELECT count(*) FROM sqlite_master").get();
+ // The rollback ran if the connection could write; a file that is itself read-only opens read-only.
+ sqlite.exec("BEGIN IMMEDIATE; ROLLBACK;");
+ } finally {
+ sqlite.close();
+ }
+}
+
+/** The runtime store cannot be written: kept recognizable for the desktop's start report. */
+export class RuntimeStoreReadonlyError extends Error {
+ readonly code = "SQLITE_READONLY";
+ readonly store = "runtime";
+ constructor(cause: unknown) {
+ super("attempt to write a readonly database (runtime store)", { cause });
+ this.name = "Error";
+ }
+}
+
+/**
+ * Run a read-only read of the runtime store; when it fails as read-only,
+ * roll back a hot journal through a read-write open and read once more.
+ * A store that still cannot be written throws RuntimeStoreReadonlyError.
+ */
+export async function readRuntimeSqliteWithRecovery<T>(path: string, read: () => Promise<T> | T): Promise<T> {
+ try {
+ return await read();
+ } catch (error) {
+ if (!isSqliteReadonlyError(error)) throw error;
+ try {
+ await rollBackRuntimeSqliteJournal(path);
+ } catch (recoveryError) {
+ if (isSqliteReadonlyError(recoveryError)) throw new RuntimeStoreReadonlyError(recoveryError);
+ throw error;
+ }
+ try {
+ return await read();
+ } catch (retryError) {
+ if (isSqliteReadonlyError(retryError)) throw new RuntimeStoreReadonlyError(retryError);
+ throw retryError;
+ }
+ }
+}
diff --git a/apps/server/src/runtime-opencode-config-store.ts b/apps/server/src/runtime-opencode-config-store.ts
index 6dbc1a3a..7ee42b2c 100644
--- a/apps/server/src/runtime-opencode-config-store.ts
+++ b/apps/server/src/runtime-opencode-config-store.ts
@@ -7,7 +7,7 @@ import {
OMNIRUSH_UI_MCP_REGISTRY_COMMAND_MESSAGE,
withoutOmniRushUiMcpRegistryEntries,
} from "./omnirush-ui-mcp-command.js";
-import { importNodeSqlite, runtimeDbPath } from "./runtime-db.js";
+import { importNodeSqlite, readRuntimeSqliteWithRecovery, runtimeDbPath } from "./runtime-db.js";
import type { ServerConfig } from "./types.js";
import { createWorkspaceKvStore, isRecord } from "./workspace-kv-store.js";
@@ -198,24 +198,26 @@ export async function listRuntimeOpencodeConfigRows(config: ServerConfig): Promi
FROM runtime_opencode_configs
`;
try {
- let rows: unknown[];
- if (typeof process.versions.bun === "string") {
- const { Database } = await import("bun:sqlite");
- const sqlite = new Database(path, { readonly: true, create: false });
- try {
- rows = sqlite.query(sql).all();
- } finally {
- sqlite.close();
+ // The first read at every start: a hot journal left by a cut-short write
+ // would fail it (and so the start) every time unless rolled back here.
+ const rows = await readRuntimeSqliteWithRecovery(path, async (): Promise<unknown[]> => {
+ if (typeof process.versions.bun === "string") {
+ const { Database } = await import("bun:sqlite");
+ const sqlite = new Database(path, { readonly: true, create: false });
+ try {
+ return sqlite.query(sql).all();
+ } finally {
+ sqlite.close();
+ }
}
- } else {
const { DatabaseSync } = await importNodeSqlite();
const sqlite = new DatabaseSync(path, { readOnly: true });
try {
- rows = sqlite.prepare(sql).all();
+ return sqlite.prepare(sql).all();
} finally {
sqlite.close();
}
- }
+ });
return rows.flatMap((row) => {
const parsed = runtimeOpencodeConfigRow(row);
return parsed ? [parsed] : [];
diff --git a/apps/server/src/workspace-kv-store.ts b/apps/server/src/workspace-kv-store.ts
index d5551037..be754c3c 100644
--- a/apps/server/src/workspace-kv-store.ts
+++ b/apps/server/src/workspace-kv-store.ts
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { existsSync } from "node:fs";
-import { openRuntimeSqliteDatabase, runtimeDbPath, type RuntimeSqliteDatabase } from "./runtime-db.js";
+import { isSqliteReadonlyError, openRuntimeSqliteDatabase, runtimeDbPath, type RuntimeSqliteDatabase } from "./runtime-db.js";
import type { ServerConfig } from "./types.js";
type WorkspaceKvSchemaVersionColumn = {
@@ -112,11 +112,27 @@ function rowFromUnknown(row: unknown): WorkspaceKvStoredRow | undefined {
};
}
+async function forgetRuntimeDb(path: string): Promise<void> {
+ const opened = dbByPath.get(path);
+ dbByPath.delete(path);
+ tableDbByPath.delete(path);
+ const runtime = await opened?.catch(() => null);
+ try {
+ runtime?.close();
+ } catch {
+ // Already closed.
+ }
+}
+
async function runtimeDb(path: string): Promise<RuntimeSqliteDatabase> {
const existing = dbByPath.get(path);
if (existing) return existing;
const db = openRuntimeSqliteDatabase(path);
dbByPath.set(path, db);
+ // A failed open is not kept: the next start (Try again) opens again.
+ db.catch(() => {
+ if (dbByPath.get(path) === db) dbByPath.delete(path);
+ });
return db;
}
@@ -197,6 +213,10 @@ async function workspaceKvDb(path: string, config: WorkspaceKvTableConfig): Prom
if (existing) return existing;
const db = openTableDb(path, config);
tableDbs.set(config.tableName, db);
+ const tables = tableDbs;
+ db.catch(() => {
+ if (tables.get(config.tableName) === db) tables.delete(config.tableName);
+ });
return db;
}
@@ -233,8 +253,16 @@ export function createWorkspaceKvStore<T>(options: WorkspaceKvStoreOptions<T>) {
valueJson: string,
updatedAt: number,
): Promise<void> {
- const db = await workspaceKvDb(runtimeDbPath(serverConfig), config);
- db.upsert({ workspaceId, valueJson, updatedAt });
+ const path = runtimeDbPath(serverConfig);
+ const db = await workspaceKvDb(path, config);
+ try {
+ db.upsert({ workspaceId, valueJson, updatedAt });
+ } catch (error) {
+ // A connection opened while the file was read-only stays read-only:
+ // drop it so a write after the file is fixed opens it again.
+ if (isSqliteReadonlyError(error)) await forgetRuntimeDb(path);
+ throw error;
+ }
}
return {