From 4e78ca2d43462f8ae6c1e65a5642690a77ac2d06 Mon Sep 17 00:00:00 2001 From: EyJunge1 <149941075+EyJunge1@users.noreply.github.com> Date: Fri, 2 Oct 2026 22:59:53 +0200 Subject: [PATCH 1/3] fix(turso): enable multiprocess_wal and harden re-embed recovery Allow concurrent OpenCode sessions on macOS/Linux via Turso multiprocess_wal, handle .db-tshm sidecars, clarify lock errors, and make interrupted re-embed swap recovery Windows-safe (#350, #357). --- README.md | 2 + src/index.ts | 9 +- src/services/turso/connection-manager.ts | 39 +++++- src/services/turso/encryption-migrator.ts | 22 ++-- src/services/turso/engine-migrator.ts | 16 +-- src/services/turso/legacy-migrator.ts | 20 ++- src/services/turso/sqlite-handle-release.ts | 11 +- tests/turso-connection-manager.test.ts | 2 +- tests/turso-multiprocess.test.ts | 137 ++++++++++++++++++++ 9 files changed, 222 insertions(+), 36 deletions(-) create mode 100644 tests/turso-multiprocess.test.ts diff --git a/README.md b/README.md index 34702fe3..7e836499 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ This plugin uses embedded Turso (`@tursodatabase/database`) with `F32_BLOB` vect Startup recovers interrupted re-embed swaps, converts libSQL DiskANN indexes to the current Turso engine, and then verifies or upgrades the legacy shard schema. Engine conversion runs even when a store already has a completed legacy migration marker, and preserves stored vectors without re-embedding. Each converted database is backed up as `.pre-tursodb-.bak`. +On macOS and Linux, multiple OpenCode sessions can share the same `storagePath` via Turso’s experimental `multiprocess_wal` (every process must use the same mode — restart all sessions after upgrading). On Windows the engine remains single-process: only one OpenCode session can own the memory databases at a time. + On first startup after upgrading, opencode-mem automatically migrates existing memory shard databases to native Turso/libSQL vector format: - Each shard is backed up as `.db.legacy.bak` before rewrite diff --git a/src/index.ts b/src/index.ts index 8fdd737e..5aded79e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -324,11 +324,18 @@ export const OpenCodeMemPlugin: Plugin = async (ctx: PluginInput) => { } catch (error) { log("Turso ready gate failed before web server start", { error: String(error) }); if (ctx.client?.tui) { + const { isTursoMultiProcessLockError } = + await import("./services/turso/connection-manager.js"); + const lockHeld = isTursoMultiProcessLockError(error); ctx.client.tui .showToast({ body: { title: "Memory Explorer", - message: "Database migration failed; web UI not started", + message: lockHeld + ? process.platform === "win32" + ? "Memory DB locked by another OpenCode session (Windows is single-owner)" + : "Memory DB locked by another session — close it or restart all OpenCode windows" + : "Database migration failed; web UI not started", variant: "error", duration: 8000, }, diff --git a/src/services/turso/connection-manager.ts b/src/services/turso/connection-manager.ts index fcffffb0..1cac05d4 100644 --- a/src/services/turso/connection-manager.ts +++ b/src/services/turso/connection-manager.ts @@ -10,6 +10,9 @@ import { resolveOrCreateDatabaseEncryptionKey } from "./encryption-key.js"; export type ConnectFactory = (path: string, opts?: DatabaseOpts) => Promise; +/** Experimental flags required on every Turso open (Mixing Modes is rejected). */ +export const TURSO_EXPERIMENTAL_FEATURES = ["encryption", "multiprocess_wal"] as const; + function assertPathInsideStorage(dbPath: string): void { const storageRoot = resolve(CONFIG.storagePath); const resolvedPath = resolve(dbPath); @@ -20,9 +23,14 @@ function assertPathInsideStorage(dbPath: string): void { } } -function buildConnectOptions(encryption?: EncryptionOpts | null): DatabaseOpts { +/** + * Shared connect options for `@tursodatabase/database`. + * Always enables encryption + multiprocess_wal so every opener uses the same mode. + * On Windows, multiprocess_wal is accepted but ignored by Turso (single-process). + */ +export function buildConnectOptions(encryption?: EncryptionOpts | null): DatabaseOpts { const opts: DatabaseOpts = { - experimental: ["encryption"], + experimental: [...TURSO_EXPERIMENTAL_FEATURES], }; if (encryption) { opts.encryption = encryption; @@ -39,6 +47,28 @@ export function resolveDatabaseEncryption(): EncryptionOpts | null { }; } +const LOCK_ERROR_RE = + /File is locked by another process|already open (with|without) experimental multiprocess WAL|Locking error/i; + +export function isTursoMultiProcessLockError(error: unknown): boolean { + const message = error instanceof Error ? error.message : String(error); + return LOCK_ERROR_RE.test(message); +} + +export function wrapTursoOpenError(dbPath: string, error: unknown): Error { + const message = error instanceof Error ? error.message : String(error); + if (isTursoMultiProcessLockError(error)) { + const platformHint = + process.platform === "win32" + ? "On Windows the Turso engine still allows only one process as database owner — close other OpenCode sessions." + : "Another OpenCode session (or an older opencode-mem without multiprocess_wal) still holds the database — close it and retry."; + return new Error(`Failed to open database ${dbPath}: ${message}. ${platformHint}`, { + cause: error, + }); + } + return error instanceof Error ? error : new Error(message, { cause: error }); +} + export class TursoConnectionManager { private readonly connections = new Map(); private readonly pending = new Map>(); @@ -92,13 +122,16 @@ export class TursoConnectionManager { } if (encryption) { const message = error instanceof Error ? error.message : String(error); + if (isTursoMultiProcessLockError(error)) { + throw wrapTursoOpenError(dbPath, error); + } throw new Error( `Failed to open encrypted database ${dbPath}: ${message}. ` + `Check databaseEncryptionKey / cipher, or remove encryption config for plaintext shards.`, { cause: error } ); } - throw error; + throw wrapTursoOpenError(dbPath, error); } })(); diff --git a/src/services/turso/encryption-migrator.ts b/src/services/turso/encryption-migrator.ts index 1861fe39..ab7ed6dd 100644 --- a/src/services/turso/encryption-migrator.ts +++ b/src/services/turso/encryption-migrator.ts @@ -3,7 +3,11 @@ import { existsSync, readdirSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { CONFIG } from "../../config.js"; import { log } from "../logger.js"; -import { resolveDatabaseEncryption, tursoConnectionManager } from "./connection-manager.js"; +import { + resolveDatabaseEncryption, + tursoConnectionManager, + buildConnectOptions, +} from "./connection-manager.js"; import { acquireTursoOperationLock } from "./operation-lock.js"; import { withSqliteFileLockRetry, @@ -51,10 +55,7 @@ async function isAlreadyEncrypted(dbPath: string): Promise { const encryption = resolveDatabaseEncryption(); if (!encryption) return false; try { - const db = await connect(dbPath, { - encryption, - experimental: ["encryption"], - }); + const db = await connect(dbPath, buildConnectOptions(encryption)); await db.close(); return true; } catch { @@ -64,7 +65,7 @@ async function isAlreadyEncrypted(dbPath: string): Promise { async function canOpenPlain(dbPath: string): Promise { try { - const db = await connect(dbPath, { experimental: ["encryption"] }); + const db = await connect(dbPath, buildConnectOptions()); await db.close(); return true; } catch { @@ -84,12 +85,9 @@ async function dumpAndReloadEncrypted(dbPath: string): Promise { const backupPath = `${dbPath}.pre-encrypt-${Date.now()}.bak`; // Re-open plain to copy rows into encrypted staged DB. - const source = await connect(dbPath, { experimental: ["encryption"] }); + const source = await connect(dbPath, buildConnectOptions()); const sourceDb = new TursoDb(source); - const staged = await connect(stagedPath, { - encryption, - experimental: ["encryption"], - }); + const staged = await connect(stagedPath, buildConnectOptions(encryption)); const stagedDb = new TursoDb(staged); try { @@ -214,7 +212,7 @@ async function dumpAndReloadEncrypted(dbPath: string): Promise { }); // Verify encrypted open - const verify = await connect(dbPath, { encryption, experimental: ["encryption"] }); + const verify = await connect(dbPath, buildConnectOptions(encryption)); await verify.close(); log("Encrypted database at rest", { dbPath, backupPath }); diff --git a/src/services/turso/engine-migrator.ts b/src/services/turso/engine-migrator.ts index 00bbf3b4..b9fca43f 100644 --- a/src/services/turso/engine-migrator.ts +++ b/src/services/turso/engine-migrator.ts @@ -12,7 +12,11 @@ import { removeSqliteDatabase, collectReleasedSqliteHandles, } from "./sqlite-handle-release.js"; -import { tursoConnectionManager, resolveDatabaseEncryption } from "./connection-manager.js"; +import { + tursoConnectionManager, + resolveDatabaseEncryption, + buildConnectOptions, +} from "./connection-manager.js"; import { TursoDb } from "./turso-db.js"; import { tursoShardManager } from "./shard-manager.js"; import { parseExtractedVector } from "./vector-utils.js"; @@ -52,10 +56,7 @@ function collectCandidateDbs(): string[] { async function needsEngineRewrite(dbPath: string): Promise { try { const encryption = resolveDatabaseEncryption(); - const opts = encryption - ? { encryption, experimental: ["encryption" as const] } - : { experimental: ["encryption" as const] }; - const db = await connect(dbPath, opts); + const db = await connect(dbPath, buildConnectOptions(encryption)); try { await db.all("SELECT 1"); return false; @@ -222,10 +223,7 @@ async function rewriteGenericDb(dbPath: string, tables: string[]): Promise await tursoConnectionManager.closeConnection(dbPath); const encryption = resolveDatabaseEncryption(); - const opts = encryption - ? { encryption, experimental: ["encryption" as const] } - : { experimental: ["encryption" as const] }; - const stagedNative = await connect(stagedPath, opts); + const stagedNative = await connect(stagedPath, buildConnectOptions(encryption)); const staged = new TursoDb(stagedNative); try { // Recreate schema by copying CREATE SQL from source via libsql. diff --git a/src/services/turso/legacy-migrator.ts b/src/services/turso/legacy-migrator.ts index 0e52b3a7..ed29d9f4 100644 --- a/src/services/turso/legacy-migrator.ts +++ b/src/services/turso/legacy-migrator.ts @@ -513,7 +513,7 @@ async function allShardsComplete(): Promise { return true; } -function recoverInterruptedReembedSwaps(): void { +async function recoverInterruptedReembedSwaps(): Promise { for (const dirName of ["users", "projects"] as const) { const dir = join(CONFIG.storagePath, dirName); if (!existsSync(dir)) continue; @@ -543,11 +543,21 @@ function recoverInterruptedReembedSwaps(): void { } if (existsSync(expectedDbPath)) { - if (existsSync(stagedPath)) removeSqliteDatabase(stagedPath); + if (existsSync(stagedPath)) { + await withSqliteFileLockRetry(() => removeSqliteDatabase(stagedPath)); + } } else if (existsSync(stagedPath)) { - renameSqliteDatabase(stagedPath, expectedDbPath); + // Target is absent after a crash mid-swap; retry covers Windows EPERM + // while native handles release (same pattern as engine/encryption swaps). + await withSqliteFileLockRetry(() => { + if (existsSync(expectedDbPath)) removeSqliteDatabase(expectedDbPath); + renameSqliteDatabase(stagedPath, expectedDbPath); + }); } else if (existsSync(backupPath)) { - renameSqliteDatabase(backupPath, expectedDbPath); + await withSqliteFileLockRetry(() => { + if (existsSync(expectedDbPath)) removeSqliteDatabase(expectedDbPath); + renameSqliteDatabase(backupPath, expectedDbPath); + }); } else { throw new Error("neither staged replacement nor source backup exists"); } @@ -612,7 +622,7 @@ export async function runLegacyTursoMigration(): Promise { mkdirSync(CONFIG.storagePath, { recursive: true }); } - recoverInterruptedReembedSwaps(); + await recoverInterruptedReembedSwaps(); // Recovery may put a libSQL shard back at its active path. Convert DiskANN // indexes before any legacy verification opens it with the Turso engine, diff --git a/src/services/turso/sqlite-handle-release.ts b/src/services/turso/sqlite-handle-release.ts index e002c745..87b59c38 100644 --- a/src/services/turso/sqlite-handle-release.ts +++ b/src/services/turso/sqlite-handle-release.ts @@ -11,11 +11,12 @@ const GC_PASSES = 3; const FILE_LOCK_RETRY_DELAYS_MS = [25, 50, 100, 200, 400, 800, 1600, 3200]; export const RETRYABLE_FILE_LOCK_CODES = new Set(["EBUSY", "EPERM", "EACCES"]); -const SQLITE_SIDE_SUFFIXES = ["-wal", "-shm"] as const; +const SQLITE_SIDE_SUFFIXES = ["-wal", "-shm", "-tshm"] as const; /** - * Rename a SQLite/Turso database file together with its WAL/SHM sidecars. + * Rename a SQLite/Turso database file together with its WAL/SHM/TSHM sidecars. * Leaving a `.tmp-wal` behind after renaming only the main file drops commits. + * `-tshm` is Turso multiprocess WAL shared memory (multiprocess_wal). */ export function renameSqliteDatabase(fromPath: string, toPath: string): void { renameSync(fromPath, toPath); @@ -32,7 +33,7 @@ export function renameSqliteDatabase(fromPath: string, toPath: string): void { } /** - * Copy a SQLite/Turso database file together with its WAL/SHM sidecars. + * Copy a SQLite/Turso database file together with its WAL/SHM/TSHM sidecars. */ export function copySqliteDatabase(fromPath: string, toPath: string): void { copyFileSync(fromPath, toPath); @@ -49,10 +50,10 @@ export function copySqliteDatabase(fromPath: string, toPath: string): void { } /** - * Remove a SQLite/Turso database file together with WAL/SHM sidecars. + * Remove a SQLite/Turso database file together with WAL/SHM/TSHM sidecars. */ export function removeSqliteDatabase(dbPath: string): void { - for (const path of [dbPath, `${dbPath}-wal`, `${dbPath}-shm`]) { + for (const path of [dbPath, `${dbPath}-wal`, `${dbPath}-shm`, `${dbPath}-tshm`]) { if (existsSync(path)) unlinkSync(path); } } diff --git a/tests/turso-connection-manager.test.ts b/tests/turso-connection-manager.test.ts index a652ddcb..336fb337 100644 --- a/tests/turso-connection-manager.test.ts +++ b/tests/turso-connection-manager.test.ts @@ -169,7 +169,7 @@ describe("turso connection manager", () => { expect(opens).toHaveLength(1); expect(opens[0]?.path).toBe(dbPath); expect(opens[0]?.opts).toMatchObject({ - experimental: ["encryption"], + experimental: ["encryption", "multiprocess_wal"], }); expect(opens[0]?.opts).not.toHaveProperty("encryption"); } finally { diff --git a/tests/turso-multiprocess.test.ts b/tests/turso-multiprocess.test.ts new file mode 100644 index 00000000..65b00d79 --- /dev/null +++ b/tests/turso-multiprocess.test.ts @@ -0,0 +1,137 @@ +import { afterEach, describe, expect, it } from "bun:test"; +import { connect } from "@tursodatabase/database"; +import { mkdtempSync, writeFileSync, existsSync } from "node:fs"; +import { join } from "node:path"; +import { tmpdir } from "node:os"; +import { cleanupTursoTestDirectory } from "./turso-test-utils.js"; + +describe("turso multiprocess_wal", () => { + let baseDir: string; + + afterEach(async () => { + await cleanupTursoTestDirectory(baseDir); + }); + + it("buildConnectOptions always enables encryption and multiprocess_wal", async () => { + const { buildConnectOptions, TURSO_EXPERIMENTAL_FEATURES } = + await import("../src/services/turso/connection-manager.js"); + expect([...TURSO_EXPERIMENTAL_FEATURES]).toEqual(["encryption", "multiprocess_wal"]); + expect(buildConnectOptions()).toEqual({ + experimental: ["encryption", "multiprocess_wal"], + }); + expect(buildConnectOptions({ cipher: "aes256gcm", hexkey: "aa".repeat(32) })).toMatchObject({ + experimental: ["encryption", "multiprocess_wal"], + encryption: { cipher: "aes256gcm" }, + }); + }); + + it("wrapTursoOpenError explains multi-process lock failures", async () => { + const { wrapTursoOpenError, isTursoMultiProcessLockError } = + await import("../src/services/turso/connection-manager.js"); + const locked = new Error( + "failed to open database /tmp/x.db: Locking error: Failed locking file. File is locked by another process" + ); + expect(isTursoMultiProcessLockError(locked)).toBe(true); + const wrapped = wrapTursoOpenError("/tmp/x.db", locked); + expect(wrapped.message).toMatch(/Failed to open database \/tmp\/x\.db/); + expect(wrapped.message).toMatch(/OpenCode session|single-owner|multiprocess_wal/i); + }); + + it.skipIf(process.platform === "win32")( + "allows two concurrent opens with multiprocess_wal", + async () => { + baseDir = mkdtempSync(join(tmpdir(), "turso-mp-share-")); + const { CONFIG } = await import("../src/config.js"); + CONFIG.storagePath = baseDir; + CONFIG.databaseEncryptionEnabled = false; + const { buildConnectOptions } = await import("../src/services/turso/connection-manager.js"); + + const dbPath = join(baseDir, "shared.db"); + const opts = buildConnectOptions(); + const first = await connect(dbPath, opts); + try { + await first.exec("CREATE TABLE t(x INTEGER); INSERT INTO t VALUES (1);"); + const second = await connect(dbPath, opts); + try { + const rows = await second.all("SELECT x FROM t"); + expect(rows).toEqual([{ x: 1 }]); + expect(existsSync(`${dbPath}-tshm`) || existsSync(`${dbPath}-wal`)).toBe(true); + } finally { + await second.close(); + } + } finally { + await first.close(); + } + } + ); + + it.skipIf(process.platform === "win32")( + "rejects a second process exclusive open without multiprocess_wal", + async () => { + baseDir = mkdtempSync(join(tmpdir(), "turso-mp-excl-")); + const dbPath = join(baseDir, "exclusive.db"); + const exclusiveOpts = { experimental: ["encryption" as const] }; + const first = await connect(dbPath, exclusiveOpts); + try { + await first.exec("CREATE TABLE t(x INTEGER);"); + + const { spawnSync } = await import("node:child_process"); + const script = ` + import { connect } from "@tursodatabase/database"; + const db = await connect(${JSON.stringify(dbPath)}, { experimental: ["encryption"] }); + await db.close(); + `; + const child = spawnSync(process.execPath, ["--eval", script], { + encoding: "utf-8", + timeout: 15_000, + }); + expect(child.status).not.toBe(0); + expect(`${child.stderr}\n${child.stdout}`).toMatch( + /locked by another process|Locking error/i + ); + } finally { + await first.close(); + } + } + ); +}); + +describe("sqlite sidecars include -tshm", () => { + let baseDir: string; + + afterEach(async () => { + await cleanupTursoTestDirectory(baseDir); + }); + + it("rename/copy/remove move -wal -shm and -tshm together", async () => { + baseDir = mkdtempSync(join(tmpdir(), "turso-tshm-")); + const { copySqliteDatabase, renameSqliteDatabase, removeSqliteDatabase } = + await import("../src/services/turso/sqlite-handle-release.js"); + + const src = join(baseDir, "src.db"); + const dst = join(baseDir, "dst.db"); + const renamed = join(baseDir, "renamed.db"); + writeFileSync(src, "main"); + writeFileSync(`${src}-wal`, "wal"); + writeFileSync(`${src}-shm`, "shm"); + writeFileSync(`${src}-tshm`, "tshm"); + + copySqliteDatabase(src, dst); + expect(existsSync(dst)).toBe(true); + expect(existsSync(`${dst}-wal`)).toBe(true); + expect(existsSync(`${dst}-shm`)).toBe(true); + expect(existsSync(`${dst}-tshm`)).toBe(true); + + renameSqliteDatabase(dst, renamed); + expect(existsSync(renamed)).toBe(true); + expect(existsSync(`${renamed}-tshm`)).toBe(true); + expect(existsSync(dst)).toBe(false); + expect(existsSync(`${dst}-tshm`)).toBe(false); + + removeSqliteDatabase(renamed); + expect(existsSync(renamed)).toBe(false); + expect(existsSync(`${renamed}-wal`)).toBe(false); + expect(existsSync(`${renamed}-shm`)).toBe(false); + expect(existsSync(`${renamed}-tshm`)).toBe(false); + }); +}); From 7108b72c1af5c36265c8366dbc46851d6d456018 Mon Sep 17 00:00:00 2001 From: EyJunge1 <149941075+EyJunge1@users.noreply.github.com> Date: Fri, 2 Oct 2026 23:02:10 +0200 Subject: [PATCH 2/3] fix(i18n): fill missing Dutch keys for UI catalog parity Package smoke failed because nl was missing 14 keys added with the explorer overhaul (filter/nav/confirm/profile-meta). --- web/src/lib/i18n/translations.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/web/src/lib/i18n/translations.ts b/web/src/lib/i18n/translations.ts index 6ffc4cc6..61528b91 100644 --- a/web/src/lib/i18n/translations.ts +++ b/web/src/lib/i18n/translations.ts @@ -701,6 +701,8 @@ export const translations = { "tab-profile": "GEBRUIKERSPROFIEL", "nav-menu": "Menu", "nav-close": "Menu sluiten", + "nav-collapse": "Zijbalk inklappen", + "nav-expand": "Zijbalk uitklappen", "nav-language": "Taal", "nav-theme": "Thema", "theme-dark": "Donker", @@ -711,6 +713,8 @@ export const translations = { "label-content": "Inhoud:", "btn-cleanup": "Opschonen", "btn-deduplicate": "Dedupliceren", + "btn-filter": "Filteren", + "btn-filter-clear": "Selectie wissen", "btn-delete-selected": "Selectie verwijderen", "btn-select-all": "Pagina selecteren", "btn-deselect-all": "Selectie opheffen", @@ -746,8 +750,11 @@ export const translations = { "btn-reembed": "Opnieuw vectoriseren (data behouden)", "migration-note": "Sluit de browser niet af. Hiermee worden je memories opnieuw gevectoriseerd met technische tags om de zoeknauwkeurigheid te verbeteren.", + "placeholder-filter": "Filteren…", "placeholder-search": "Memories zoeken...", "placeholder-tags": "react, hooks, auth (komma-gescheiden)", + "placeholder-new-tag": "Nieuwe tag…", + "btn-add-tag": "Tag toevoegen", "placeholder-content": "Voer de inhoud van de memory in...", "toast-add-success": "Memory succesvol toegevoegd", "toast-add-error": "Inhoud en tag zijn verplicht", @@ -773,10 +780,15 @@ export const translations = { "confirm-delete-pair": "Deze memory EN de gekoppelde prompt verwijderen?", "confirm-delete-prompt": "Deze prompt EN de gekoppelde memory verwijderen?", "confirm-bulk-delete": "{count} geselecteerde memories verwijderen?", + "confirm-bulk-delete-title": "Selectie verwijderen", "confirm-cleanup": "Hiermee worden alle memories verwijderd die niet meer relevant zijn. Doorgaan?", + "confirm-cleanup-title": "Memories opschonen", "confirm-dedup": "Hiermee worden dubbele of sterk overeenkomende memories samengevoegd. Doorgaan?", + "confirm-dedup-title": "Memories dedupliceren", + "confirm-migration-title": "Migratie bevestigen", + "confirm-migration": "{strategy} uitvoeren?\n\nDeze bewerking is onomkeerbaar.", "text-selected": "{count} geselecteerd", "text-page": "Pagina {current} van {total}", "text-total": "Totaal: {count}", @@ -789,6 +801,7 @@ export const translations = { "profile-version": "VERSIE", "profile-prompts": "PROMPTS", "profile-updated": "LAATST GEWIJZIGD", + "profile-meta": "{count} prompts · {date}", "profile-preferences": "VOORKEUREN", "profile-type-pref": "Voorkeur", "profile-type-pat": "Patroon", @@ -799,6 +812,7 @@ export const translations = { "badge-prompt": "GEBRUIKERSPROMPT", "badge-memory": "MEMORY", "badge-pinned": "VASTGEZET", + "section-all": "Alles", "badge-linked": "GEKOPPELD", "date-created": "Aangemaakt:", "date-updated": "Bijgewerkt:", From 3a219e1bfeb68bb074e9141fbc6bda8ef4031ff1 Mon Sep 17 00:00:00 2001 From: EyJunge1 <149941075+EyJunge1@users.noreply.github.com> Date: Fri, 2 Oct 2026 23:05:29 +0200 Subject: [PATCH 3/3] fix(turso): enable multiprocess_wal only on Unix Windows rejects the flag with "not supported by the active IO backend"; keep encryption-only opens there so package smoke stays green. --- README.md | 2 +- src/services/turso/connection-manager.ts | 30 ++++++++++++++++++++---- tests/turso-connection-manager.test.ts | 3 ++- tests/turso-multiprocess.test.ts | 13 ++++++---- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 7e836499..2c713832 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ This plugin uses embedded Turso (`@tursodatabase/database`) with `F32_BLOB` vect Startup recovers interrupted re-embed swaps, converts libSQL DiskANN indexes to the current Turso engine, and then verifies or upgrades the legacy shard schema. Engine conversion runs even when a store already has a completed legacy migration marker, and preserves stored vectors without re-embedding. Each converted database is backed up as `.pre-tursodb-.bak`. -On macOS and Linux, multiple OpenCode sessions can share the same `storagePath` via Turso’s experimental `multiprocess_wal` (every process must use the same mode — restart all sessions after upgrading). On Windows the engine remains single-process: only one OpenCode session can own the memory databases at a time. +On macOS and Linux, multiple OpenCode sessions can share the same `storagePath` via Turso’s experimental `multiprocess_wal` (every process must use the same mode — restart all sessions after upgrading). On Windows the engine rejects that flag, so only one OpenCode session can own the memory databases at a time. On first startup after upgrading, opencode-mem automatically migrates existing memory shard databases to native Turso/libSQL vector format: diff --git a/src/services/turso/connection-manager.ts b/src/services/turso/connection-manager.ts index 1cac05d4..17fe7cce 100644 --- a/src/services/turso/connection-manager.ts +++ b/src/services/turso/connection-manager.ts @@ -10,8 +10,28 @@ import { resolveOrCreateDatabaseEncryptionKey } from "./encryption-key.js"; export type ConnectFactory = (path: string, opts?: DatabaseOpts) => Promise; -/** Experimental flags required on every Turso open (Mixing Modes is rejected). */ -export const TURSO_EXPERIMENTAL_FEATURES = ["encryption", "multiprocess_wal"] as const; +/** Always-on experimental flags for every Turso open. */ +export const TURSO_BASE_EXPERIMENTAL_FEATURES = ["encryption"] as const; + +/** + * Multiprocess WAL is Unix-only. On Windows the default IO backend rejects the + * flag (`experimental multiprocess WAL is not supported by the active IO backend`). + */ +export function supportsTursoMultiprocessWal( + platform: NodeJS.Platform = process.platform +): boolean { + return platform !== "win32"; +} + +/** Experimental flags required on every Turso open for the current platform. */ +export function tursoExperimentalFeatures( + platform: NodeJS.Platform = process.platform +): Array<(typeof TURSO_BASE_EXPERIMENTAL_FEATURES)[number] | "multiprocess_wal"> { + if (supportsTursoMultiprocessWal(platform)) { + return [...TURSO_BASE_EXPERIMENTAL_FEATURES, "multiprocess_wal"]; + } + return [...TURSO_BASE_EXPERIMENTAL_FEATURES]; +} function assertPathInsideStorage(dbPath: string): void { const storageRoot = resolve(CONFIG.storagePath); @@ -25,12 +45,12 @@ function assertPathInsideStorage(dbPath: string): void { /** * Shared connect options for `@tursodatabase/database`. - * Always enables encryption + multiprocess_wal so every opener uses the same mode. - * On Windows, multiprocess_wal is accepted but ignored by Turso (single-process). + * Always enables encryption; adds multiprocess_wal on Unix so concurrent + * OpenCode sessions can share the store. Windows stays single-process. */ export function buildConnectOptions(encryption?: EncryptionOpts | null): DatabaseOpts { const opts: DatabaseOpts = { - experimental: [...TURSO_EXPERIMENTAL_FEATURES], + experimental: tursoExperimentalFeatures(), }; if (encryption) { opts.encryption = encryption; diff --git a/tests/turso-connection-manager.test.ts b/tests/turso-connection-manager.test.ts index 336fb337..7ebabe65 100644 --- a/tests/turso-connection-manager.test.ts +++ b/tests/turso-connection-manager.test.ts @@ -169,7 +169,8 @@ describe("turso connection manager", () => { expect(opens).toHaveLength(1); expect(opens[0]?.path).toBe(dbPath); expect(opens[0]?.opts).toMatchObject({ - experimental: ["encryption", "multiprocess_wal"], + experimental: + process.platform === "win32" ? ["encryption"] : ["encryption", "multiprocess_wal"], }); expect(opens[0]?.opts).not.toHaveProperty("encryption"); } finally { diff --git a/tests/turso-multiprocess.test.ts b/tests/turso-multiprocess.test.ts index 65b00d79..b75c472c 100644 --- a/tests/turso-multiprocess.test.ts +++ b/tests/turso-multiprocess.test.ts @@ -12,15 +12,18 @@ describe("turso multiprocess_wal", () => { await cleanupTursoTestDirectory(baseDir); }); - it("buildConnectOptions always enables encryption and multiprocess_wal", async () => { - const { buildConnectOptions, TURSO_EXPERIMENTAL_FEATURES } = + it("buildConnectOptions enables encryption and multiprocess_wal on Unix only", async () => { + const { buildConnectOptions, tursoExperimentalFeatures, supportsTursoMultiprocessWal } = await import("../src/services/turso/connection-manager.js"); - expect([...TURSO_EXPERIMENTAL_FEATURES]).toEqual(["encryption", "multiprocess_wal"]); + expect(tursoExperimentalFeatures("linux")).toEqual(["encryption", "multiprocess_wal"]); + expect(tursoExperimentalFeatures("darwin")).toEqual(["encryption", "multiprocess_wal"]); + expect(tursoExperimentalFeatures("win32")).toEqual(["encryption"]); + expect(supportsTursoMultiprocessWal("win32")).toBe(false); expect(buildConnectOptions()).toEqual({ - experimental: ["encryption", "multiprocess_wal"], + experimental: tursoExperimentalFeatures(), }); expect(buildConnectOptions({ cipher: "aes256gcm", hexkey: "aa".repeat(32) })).toMatchObject({ - experimental: ["encryption", "multiprocess_wal"], + experimental: tursoExperimentalFeatures(), encryption: { cipher: "aes256gcm" }, }); });