From 114c97a32f0cc1ca51951ea3b36f3d78afbb4b01 Mon Sep 17 00:00:00 2001 From: martinvlad Date: Tue, 13 Oct 2026 07:57:21 -0700 Subject: [PATCH 1/2] fix(memory): create the AgentDB directory before the bridge opens it (#4132) In a project without `.swarm/` (any fresh git worktree), getRegistry passed `/.swarm/agentdb-memory.db` to registry.initialize() without creating the directory. better-sqlite3 refuses a missing parent, so AgentDB was marked unavailable, the degraded registry was cached for the server's lifetime, and hierarchical memory silently ran on the volatile fallback while every store reported success. Create the parent directory first (skipped for :memory:). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RMjr52YGVk9fkqtNvb5jg2 --- .../memory-bridge-creates-db-dir-4132.test.ts | 48 +++++++++++++++++++ .../cli/src/memory/memory-bridge.ts | 8 +++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 v3/@claude-flow/cli/__tests__/memory-bridge-creates-db-dir-4132.test.ts diff --git a/v3/@claude-flow/cli/__tests__/memory-bridge-creates-db-dir-4132.test.ts b/v3/@claude-flow/cli/__tests__/memory-bridge-creates-db-dir-4132.test.ts new file mode 100644 index 0000000000..01c6f262e4 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/memory-bridge-creates-db-dir-4132.test.ts @@ -0,0 +1,48 @@ +/** + * #4132: in a project without `.swarm/` (any fresh git worktree) the bridge + * opened AgentDB at `/.swarm/agentdb-memory.db` without creating the + * directory. better-sqlite3 refuses a missing parent, AgentDB was marked + * unavailable, the degraded registry was cached, and hierarchical memory ran + * on the volatile fallback while every store reported success. + */ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { existsSync, mkdtempSync, realpathSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { dirname, join } from 'node:path'; + +class DirectoryCheckingRegistry { + // Mirrors better-sqlite3: opening a file in a missing directory fails. + async initialize(options: { dbPath: string }) { + if (!existsSync(dirname(options.dbPath))) throw new Error('Cannot open database because the directory does not exist'); + } + get() { return null; } + getAgentDB() { return null; } + async shutdown() {} +} + +vi.mock('../src/memory/intelligence.js', () => ({ + initializeIntelligence: vi.fn(async () => ({ reasoningBankEnabled: false, sonaEnabled: false })), +})); + +const bridge = await import('../src/memory/memory-bridge.js'); + +let root: string; +beforeEach(() => { + root = realpathSync(mkdtempSync(join(tmpdir(), 'ruflo-bridge-4132-'))); + bridge._resetRegistryCacheForTest(); + bridge.__setMemoryBridgeRegistryFactoryForTests(() => new DirectoryCheckingRegistry()); +}); +afterEach(async () => { + await bridge.shutdownBridge(); + rmSync(root, { recursive: true, force: true }); +}); + +describe('#4132 the bridge creates the AgentDB directory before opening it', () => { + it('opens a database whose .swarm directory does not exist yet', async () => { + const db = join(root, 'fresh-worktree', '.swarm', 'agentdb-memory.db'); + expect(existsSync(dirname(db))).toBe(false); + expect(await bridge.getControllerRegistry(db)).not.toBeNull(); + expect(bridge.getBridgeFailureReason(db)).toBeNull(); + expect(existsSync(dirname(db))).toBe(true); + }); +}); diff --git a/v3/@claude-flow/cli/src/memory/memory-bridge.ts b/v3/@claude-flow/cli/src/memory/memory-bridge.ts index 3921ad2476..b3e80334e9 100644 --- a/v3/@claude-flow/cli/src/memory/memory-bridge.ts +++ b/v3/@claude-flow/cli/src/memory/memory-bridge.ts @@ -23,7 +23,7 @@ import { resolveAgentdbBetterSqlite3 } from './shared-sqlite.js'; import { encodeEmbeddingQ8, MAX_LIST_EMBEDDINGS } from './embedding-q8.js'; import * as path from 'path'; import * as crypto from 'crypto'; -import { realpathSync } from 'node:fs'; +import { mkdirSync, realpathSync } from 'node:fs'; import { createRequire } from 'node:module'; import { AsyncLocalStorage } from 'node:async_hooks'; import { validateFeedbackPatterns } from './feedback-patterns.js'; @@ -270,6 +270,12 @@ async function getRegistry(dbPath?: string): Promise { origLog.apply(console, args); }; + // #4132: better-sqlite3 cannot create a missing parent directory. In a + // fresh checkout or worktree `.swarm/` does not exist yet, so AgentDB + // failed to open, the degraded registry was cached for the server's + // lifetime and hierarchical memory silently ran volatile. + if (resolvedPath !== ':memory:') mkdirSync(path.dirname(resolvedPath), { recursive: true }); + try { await (registry as any).initialize({ // #2786: use agentdb-memory.db (plaintext) so native better-sqlite3 doesn't hit the encrypted memory.db. From dc9b25121bf3ed277128c86cc8b993a06b5ab887 Mon Sep 17 00:00:00 2001 From: Dragan Spiridonov Date: Tue, 13 Oct 2026 19:06:39 +0200 Subject: [PATCH 2/2] fix(memory): restore logging when database directory creation fails --- .../memory-bridge-creates-db-dir-4132.test.ts | 13 ++++++++++++- v3/@claude-flow/cli/src/memory/memory-bridge.ts | 3 +-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/v3/@claude-flow/cli/__tests__/memory-bridge-creates-db-dir-4132.test.ts b/v3/@claude-flow/cli/__tests__/memory-bridge-creates-db-dir-4132.test.ts index 01c6f262e4..f3c5ff790e 100644 --- a/v3/@claude-flow/cli/__tests__/memory-bridge-creates-db-dir-4132.test.ts +++ b/v3/@claude-flow/cli/__tests__/memory-bridge-creates-db-dir-4132.test.ts @@ -6,7 +6,7 @@ * on the volatile fallback while every store reported success. */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import { existsSync, mkdtempSync, realpathSync, rmSync } from 'node:fs'; +import { existsSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; @@ -38,6 +38,17 @@ afterEach(async () => { }); describe('#4132 the bridge creates the AgentDB directory before opening it', () => { + it('restores console.log when directory creation fails', async () => { + const parent = join(root, 'not-a-directory'); + writeFileSync(parent, 'fixture'); + const originalLog = console.log; + try { + expect(await bridge.getControllerRegistry(join(parent, 'agentdb-memory.db'))).toBeNull(); + expect(bridge.getBridgeFailureReason(join(parent, 'agentdb-memory.db'))).toBeTruthy(); + expect(console.log).toBe(originalLog); + } finally { console.log = originalLog; } + }); + it('opens a database whose .swarm directory does not exist yet', async () => { const db = join(root, 'fresh-worktree', '.swarm', 'agentdb-memory.db'); expect(existsSync(dirname(db))).toBe(false); diff --git a/v3/@claude-flow/cli/src/memory/memory-bridge.ts b/v3/@claude-flow/cli/src/memory/memory-bridge.ts index b3e80334e9..82ceb8ee8b 100644 --- a/v3/@claude-flow/cli/src/memory/memory-bridge.ts +++ b/v3/@claude-flow/cli/src/memory/memory-bridge.ts @@ -274,9 +274,8 @@ async function getRegistry(dbPath?: string): Promise { // fresh checkout or worktree `.swarm/` does not exist yet, so AgentDB // failed to open, the degraded registry was cached for the server's // lifetime and hierarchical memory silently ran volatile. - if (resolvedPath !== ':memory:') mkdirSync(path.dirname(resolvedPath), { recursive: true }); - try { + if (resolvedPath !== ':memory:') mkdirSync(path.dirname(resolvedPath), { recursive: true }); await (registry as any).initialize({ // #2786: use agentdb-memory.db (plaintext) so native better-sqlite3 doesn't hit the encrypted memory.db. dbPath: resolvedPath,