From 16b1800a96619eda691a578f5245577f67123906 Mon Sep 17 00:00:00 2001 From: gsxdsm Date: Sun, 3 May 2026 02:07:36 -0700 Subject: [PATCH] fix(core): evict cached AgentStore Database on close Companion to the per-rootDir Database cache: when an AgentStore is closed, drop it from the cache so the next instantiation opens a fresh connection instead of handing out the closed handle. Also tolerate "database is not open" on close, since multiple AgentStore instances now share one underlying Database. Co-Authored-By: Claude Opus 4.7 (1M context) --- packages/core/src/agent-store.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/core/src/agent-store.ts b/packages/core/src/agent-store.ts index c4bdac15c..8f656ef74 100644 --- a/packages/core/src/agent-store.ts +++ b/packages/core/src/agent-store.ts @@ -2383,8 +2383,22 @@ export class AgentStore extends EventEmitter { * Close the underlying SQLite connection and release resources. */ close(): void { - if (this._db) { + if (!this._db) { + return; + } + + if (!this.inMemoryDb && agentStoreDbCache.get(this.rootDir) === this._db) { + agentStoreDbCache.delete(this.rootDir); + } + + try { this._db.close(); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + if (!message.includes("database is not open")) { + throw error; + } + } finally { this._db = null; } }