Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

fix(search): close failed SQLite initialization handles

CloudTianTian126218f15284
diff --git a/.github/workflows/native-dependencies.yml b/.github/workflows/native-dependencies.yml
index 4dd220e5..5425127b 100644
--- a/.github/workflows/native-dependencies.yml
+++ b/.github/workflows/native-dependencies.yml
@@ -62,6 +62,7 @@ jobs:
test/main/util/ensure-deps.test.ts
test/main/util/ensure-dev-dependencies.test.ts
test/main/util/linux-source-dependencies.test.ts
+ test/main/features/search/chat_store.test.ts
- name: Verify workbook formulas and interrupted directory recovery
run: >-
npm run test:js -- --maxWorkers=1
diff --git a/src/main/features/search/chat_store.ts b/src/main/features/search/chat_store.ts
index 3ff5d71d..1eb0f295 100644
--- a/src/main/features/search/chat_store.ts
+++ b/src/main/features/search/chat_store.ts
@@ -191,9 +191,16 @@ function openDatabase(dbPath: string): Database.Database {
// WAL keeps a reader (a search) from blocking behind an append; DELETE mode
// would serialize them. `synchronous = NORMAL` is the same durability trade
// vec_store makes for a derived index that reconciles from source.
- db.pragma('journal_mode = WAL');
- db.pragma('synchronous = NORMAL');
- return db;
+ try {
+ db.pragma('journal_mode = WAL');
+ db.pragma('synchronous = NORMAL');
+ return db;
+ } catch (err) {
+ // The constructor may succeed before a pragma fails. This handle has
+ // not reached the cache or handleFor's cleanup scope yet.
+ try { db.close(); } catch { /* preserve the initialization error */ }
+ throw err;
+ }
}
function handleFor(uid: string): Handle {
diff --git a/test/main/features/search/chat_store.test.ts b/test/main/features/search/chat_store.test.ts
index 38da6b8c..f014c9bd 100644
--- a/test/main/features/search/chat_store.test.ts
+++ b/test/main/features/search/chat_store.test.ts
@@ -1,4 +1,4 @@
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
@@ -35,6 +35,36 @@ function doc(cid: string, msgIndex: number, text: string, role = 'user') {
}
describe('search/chat_store', () => {
+ it.each([0, 1])('closes a failed SQLite initialization at pragma %s and retries with a fresh handle', async failingPragma => {
+ const store = await loadStore();
+ const failure = new Error('fixture SQLite initialization failure');
+ const original = Database.prototype.pragma;
+ let count = 0;
+ const pragma = vi.spyOn(Database.prototype, 'pragma').mockImplementation(function (...args: any[]) {
+ if (count++ === failingPragma) throw failure;
+ return original.apply(this, args as any);
+ });
+ const close = vi.spyOn(Database.prototype, 'close');
+ try {
+ expect(() => store.docCount(UID)).toThrow(failure);
+ const failedHandle = pragma.mock.contexts[0] as Database.Database;
+ expect(close.mock.contexts).toContain(failedHandle);
+ expect(failedHandle.open).toBe(false);
+ // Failure must not poison the cache or leave a file handle needed for
+ // recovery; the same source-owned index can initialize on the next call.
+ expect(store.docCount(UID)).toBe(0);
+ store.upsertDoc(UID, doc('c1', 0, 'recoveryword'));
+ expect(store.postingsFor(UID, 'recoveryword')).toHaveLength(1);
+ } finally {
+ // Original code leaks this handle. Clean it in the negative fixture so
+ // failure evidence does not itself strand a native handle on Windows.
+ const failedHandle = pragma.mock.contexts[0] as Database.Database;
+ if (failedHandle?.open) failedHandle.close();
+ close.mockRestore();
+ pragma.mockRestore();
+ }
+ });
+
it('keeps small free space reusable and reclaims substantial space after a rebuild', async () => {
const store = await loadStore();
store.upsertDoc(UID, doc('c1', 0, 'still searchable'));