diff --git a/apps/server/src/services/plugins/plugin-api.ts b/apps/server/src/services/plugins/plugin-api.ts index 47e0a2473..62e4de48e 100644 --- a/apps/server/src/services/plugins/plugin-api.ts +++ b/apps/server/src/services/plugins/plugin-api.ts @@ -594,15 +594,21 @@ export function createPluginApi(options: { }, }; + // One reused handle per plugin generation (the SDK contract and the fake + // host both promise reuse). A plugin that closes the handle itself gets a + // fresh one on the next call; the host closes whatever is open on dispose. + let databaseHandle: Database.Database | undefined; const storage: PluginStorage = { kv, database() { assertLive(); + if (databaseHandle?.open) return databaseHandle; const dir = join(dataDir, "plugins", pluginId); mkdirSync(dir, { recursive: true }); const database = new Database(join(dir, "data.db")); database.pragma("journal_mode = WAL"); database.pragma("busy_timeout = 5000"); + databaseHandle = database; databaseHandles.push(database); return database; },