diff --git a/v3/@claude-flow/cli/__tests__/memory-bridge-agentdb-init-error-4622.test.ts b/v3/@claude-flow/cli/__tests__/memory-bridge-agentdb-init-error-4622.test.ts index 2a95a23eae..d3877adacb 100644 --- a/v3/@claude-flow/cli/__tests__/memory-bridge-agentdb-init-error-4622.test.ts +++ b/v3/@claude-flow/cli/__tests__/memory-bridge-agentdb-init-error-4622.test.ts @@ -12,6 +12,7 @@ import * as path from 'node:path'; import { __setMemoryBridgeRegistryFactoryForTests, _resetRegistryCacheForTest, + bridgeHealthCheck, getBridgeFailureReason, isBridgeAvailable, } from '../src/memory/memory-bridge.js'; @@ -24,6 +25,7 @@ function fakeRegistry(initError: string | null, hasRecord = true) { getAgentDB: () => (initError ? null : {}), get: (name: string) => controllers.get(name), set: (name: string, value: unknown) => { controllers.set(name, value); }, + listControllers: () => [], }; if (hasRecord) reg.getAgentDBInitError = () => initError; return reg; @@ -66,4 +68,27 @@ describe('memory bridge: AgentDB init failure (#4622)', () => { expect(await isBridgeAvailable(db)).toBe(true); expect(getBridgeFailureReason(db)).toBeNull(); }); + + it('surfaces the AgentDB init cause and a lock hint in health', async () => { + const lock = 'Database already open. Cannot acquire lock'; + __setMemoryBridgeRegistryFactoryForTests(() => fakeRegistry(lock)); + const db = dbFile(); + + const health = await bridgeHealthCheck(db); + expect(health).not.toBeNull(); + expect(health?.available).toBe(false); + expect(health?.agentdbInitError).toContain(lock); + expect(health?.bridgeFailureReason).toContain(lock); + expect(health?.hint).toMatch(/another process/i); + }); + + it('keeps health available when AgentDB opened', async () => { + __setMemoryBridgeRegistryFactoryForTests(() => fakeRegistry(null)); + const db = dbFile(); + + const health = await bridgeHealthCheck(db); + expect(health?.available).toBe(true); + expect(health?.agentdbInitError).toBeNull(); + expect(health?.hint).toBeUndefined(); + }); }); diff --git a/v3/@claude-flow/cli/src/memory/memory-bridge.ts b/v3/@claude-flow/cli/src/memory/memory-bridge.ts index e6739b6847..882354e5fb 100644 --- a/v3/@claude-flow/cli/src/memory/memory-bridge.ts +++ b/v3/@claude-flow/cli/src/memory/memory-bridge.ts @@ -3643,6 +3643,12 @@ export async function bridgeHealthCheck( controllers: Array<{ name: string; enabled: boolean; level: number }>; attestationCount?: number; cacheStats?: { size: number; hits: number; misses: number }; + /** Why AgentDB failed to open, or null when it opened (#4654). */ + agentdbInitError?: string | null; + /** Last bridge-level failure, including the AgentDB lock cause (#4622/#4654). */ + bridgeFailureReason?: string | null; + /** Actionable next step when a second process owns the native store. */ + hint?: string; hierarchicalMemory?: { controller: string; durable: boolean; @@ -3658,6 +3664,12 @@ export async function bridgeHealthCheck( try { const controllers = registry.listControllers(); + const resolvedPath = canonicalDbPath(dbPath); + const agentdbInitError = agentdbInitFailure(registry, resolvedPath); + const bridgeFailureReason = getBridgeFailureReason(dbPath); + const lockHint = agentdbInitError && /already open|cannot acquire lock|locked/i.test(agentdbInitError) + ? 'Another process (often the ruflo daemon or a second MCP server) owns the native vector store. Stop one owner and reconnect; reads currently use the volatile fallback.' + : undefined; // Phase 4: AttestationLog stats let attestationCount = 0; @@ -3686,7 +3698,18 @@ export async function bridgeHealthCheck( }; } - return { available: true, controllers, attestationCount, cacheStats, hierarchicalMemory }; + return { + // A registry with a failed AgentDB still serves its other controllers, + // but it is not a working persistent bridge (#4622/#4654). + available: agentdbInitError === null, + controllers, + attestationCount, + cacheStats, + agentdbInitError, + bridgeFailureReason, + ...(lockHint ? { hint: lockHint } : {}), + hierarchicalMemory, + }; } catch { return null; }