fix(memory): expose AgentDB init failures in health (#4667)

agentdb_health could return available:true with controllers while AgentDB itself had failed to open (for example because the daemon or another MCP server owned the RuVector store). The only trace was the separate isBridgeAvailable() check; the health payload had no init error or lock hint (#4654).\n\nbridgeHealthCheck now reads the registry's getAgentDBInitError(), reports available:false, includes agentdbInitError and the recorded bridgeFailureReason, and adds an actionable hint when the cause is an already-open/locked native store. Older registries without the record are unchanged.\n\nRegression extends the #4622 fake registry with listControllers and covers failed and healthy health payloads. Baseline returns available:true and no error for the failure case; candidate 5/5 with the Windows bridge opt-in, existing 3 tests unchanged.
This commit is contained in:
ordinary being authored and GitHub committed 2026-11-04 01:01:20 -06:00
1 parent 55260ca55a
commit 762fbf74c2
2 files changed
+49 -1

No files matched your search

@@ -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();
});
});
@@ -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;
}