diff --git a/v3/@claude-flow/cli/__tests__/memory-cache-cross-process-3360.test.ts b/v3/@claude-flow/cli/__tests__/memory-cache-cross-process-3360.test.ts new file mode 100644 index 0000000000..628c55acea --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/memory-cache-cross-process-3360.test.ts @@ -0,0 +1,401 @@ +/** + * Regression guard — `memory_retrieve` served stale values after ANOTHER + * process wrote to the same store. + * + * Several MCP servers routinely share one database file: Claude Code, Codex + * and Grok each start their own `mcp start` against the same `.swarm/` + * store. `bridgeGetEntry` consults a per-process TieredCache first, and each + * process only invalidated that cache on its own writes. Once server B had + * read a key, an update or delete made by server A never reached B: B kept + * returning the old value (`found: true`) until its cache entry expired + * (5-minute TieredCache TTL by default). + * + * A second better-sqlite3 connection to the same file stands in for the + * other server process — that is exactly the distinction SQLite's + * `PRAGMA data_version` draws (commits by a different connection), which the + * fix relies on. + */ + +import { describe, it, expect, afterAll, beforeEach } from 'vitest'; +import Database from 'better-sqlite3'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; + +const root = mkdtempSync(join(tmpdir(), 'ruflo-cache-cross-process-')); +const dbPath = join(root, 'memory.db'); + +function makeDb(): Database.Database { + const d = new Database(dbPath); + d.pragma('journal_mode = WAL'); + d.exec(` + CREATE TABLE IF NOT EXISTS memory_entries ( + id TEXT PRIMARY KEY, + key TEXT NOT NULL, + namespace TEXT DEFAULT 'default', + content TEXT NOT NULL, + type TEXT DEFAULT 'semantic', + embedding TEXT, + embedding_dimensions INTEGER, + embedding_model TEXT, + tags TEXT, + metadata TEXT, + provenance_type TEXT DEFAULT 'unknown', + created_at INTEGER, + updated_at INTEGER, + last_accessed_at INTEGER, + access_count INTEGER DEFAULT 0, + expires_at INTEGER, + status TEXT DEFAULT 'active', + UNIQUE(namespace, key) + ); + CREATE TABLE IF NOT EXISTS vector_indexes ( + id TEXT PRIMARY KEY, + name TEXT, + dimensions INTEGER + ); + `); + return d; +} + +/** Map-backed stand-in for TieredCacheManager (get/set/delete/clear). */ +function makeTieredCache() { + const store = new Map(); + return { + get: (k: string) => store.get(k), + set: (k: string, v: unknown) => { store.set(k, v); }, + delete: (k: string) => { store.delete(k); }, + clear: () => { store.clear(); }, + }; +} + +// `db` is this process's connection (what agentdb hands the bridge); +// `otherProcess` is a second connection to the same file. +const db = makeDb(); +const otherProcess = new Database(dbPath); + +afterAll(() => { + otherProcess.close(); + db.close(); + rmSync(root, { recursive: true, force: true }); +}); + +async function bridge() { + const mod = await import('../src/memory/memory-bridge.js'); + mod.__setMemoryBridgeRegistryForTests({ + getAgentDB: () => ({ database: db, embedder: null }), + get: (kind: string) => (kind === 'tieredCache' ? cache : null), + }); + return mod; +} + +let cache = makeTieredCache(); + +beforeEach(() => { + cache = makeTieredCache(); +}); + +describe('bridgeGetEntry — writes by another process sharing the store', () => { + it('returns the value another process updated, not the cached one', async () => { + const { bridgeStoreEntry, bridgeGetEntry } = await bridge(); + const ns = 'cross-process-update'; + await bridgeStoreEntry({ key: 'k', value: 'v1', namespace: ns, generateEmbeddingFlag: false, dbPath }); + + const first = await bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); + expect(first!.entry!.content).toBe('v1'); + + // The cache still does its job when nobody else has written. + const second = await bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); + expect(second!.cacheHit).toBe(true); + expect(second!.entry!.content).toBe('v1'); + + otherProcess + .prepare('UPDATE memory_entries SET content = ?, updated_at = ? WHERE namespace = ? AND key = ?') + .run('v2', Date.now(), ns, 'k'); + + const third = await bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); + expect(third!.entry!.content).toBe('v2'); + expect(third!.cacheHit).toBe(false); + }); + + it('reports not-found for a key another process deleted', async () => { + const { bridgeStoreEntry, bridgeGetEntry } = await bridge(); + const ns = 'cross-process-delete'; + await bridgeStoreEntry({ key: 'k', value: 'doomed', namespace: ns, generateEmbeddingFlag: false, dbPath }); + expect((await bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.entry!.content).toBe('doomed'); + + // Same soft delete bridgeDeleteEntry performs. + otherProcess + .prepare(`UPDATE memory_entries SET status = 'deleted', updated_at = ? WHERE namespace = ? AND key = ?`) + .run(Date.now(), ns, 'k'); + + const after = await bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); + expect(after!.success).toBe(true); + expect(after!.found).toBe(false); + }); + + it("never queries data_version on agentdb's sql.js fallback (isWasm)", async () => { + const mod = await import('../src/memory/memory-bridge.js'); + const probes: string[] = []; + // agentdb's SqlJsDatabase has pragma()/prepare() but rejects data_version + // (with a console error); the bridge must not ask it. + const wasmDb = new Proxy(db, { + get(target, prop) { + if (prop === 'pragma') return (sql: string, ...a: unknown[]) => { probes.push(`pragma ${sql}`); return (target as any).pragma(sql, ...a); }; + if (prop === 'prepare') return (sql: string) => { if (/data_version/i.test(sql)) probes.push(sql); return target.prepare(sql); }; + const v = Reflect.get(target, prop, target); + return typeof v === 'function' ? v.bind(target) : v; + }, + }); + mod.__setMemoryBridgeRegistryForTests({ + getAgentDB: () => ({ database: wasmDb, embedder: null, isWasm: true }), + get: (kind: string) => (kind === 'tieredCache' ? cache : null), + }); + const ns = 'wasm-fallback'; + await mod.bridgeStoreEntry({ key: 'k', value: 'w', namespace: ns, generateEmbeddingFlag: false, dbPath }); + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.entry!.content).toBe('w'); + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.cacheHit).toBe(true); + expect(probes.filter((p) => /data_version/i.test(p))).toEqual([]); + }); + + it('stops probing a handle whose data_version check throws', async () => { + const mod = await import('../src/memory/memory-bridge.js'); + let attempts = 0; + const brokenDb = new Proxy(db, { + get(target, prop) { + if (prop === 'prepare') { + return (sql: string) => { + if (/data_version/i.test(sql)) { attempts++; throw new Error('unsupported pragma'); } + return target.prepare(sql); + }; + } + if (prop === 'pragma') { + return (sql: string, ...a: unknown[]) => { + if (/data_version/i.test(sql)) { attempts++; throw new Error('unsupported pragma'); } + return (target as any).pragma(sql, ...a); + }; + } + const v = Reflect.get(target, prop, target); + return typeof v === 'function' ? v.bind(target) : v; + }, + }); + mod.__setMemoryBridgeRegistryForTests({ + getAgentDB: () => ({ database: brokenDb, embedder: null }), + get: (kind: string) => (kind === 'tieredCache' ? cache : null), + }); + const ns = 'broken-probe'; + await mod.bridgeStoreEntry({ key: 'k', value: 'b', namespace: ns, generateEmbeddingFlag: false, dbPath }); + for (let i = 0; i < 3; i++) { + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.entry!.content).toBe('b'); + } + expect(attempts).toBe(1); + }); + + it('never probes a non-native handle even when isWasm is not reported', async () => { + const mod = await import('../src/memory/memory-bridge.js'); + const probes: string[] = []; + // Shape of agentdb's SqlJsDatabase: has pragma()/prepare(), but no + // better-sqlite3 `inTransaction`. (A forwarding object, not a Proxy of the + // real handle: a Proxy can't hide the native class's own properties.) + const sqlJsLike = new Proxy({} as Record, { + has: (_t, p) => p !== 'inTransaction' && Reflect.has(db, p), + get(_t, prop) { + if (prop === 'pragma') return (sql: string, ...a: unknown[]) => { probes.push(`pragma ${sql}`); return (db as any).pragma(sql, ...a); }; + if (prop === 'prepare') return (sql: string) => { if (/data_version/i.test(sql)) probes.push(sql); return db.prepare(sql); }; + if (prop === 'inTransaction') return undefined; + const v = Reflect.get(db, prop, db); + return typeof v === 'function' ? v.bind(db) : v; + }, + }); + mod.__setMemoryBridgeRegistryForTests({ + getAgentDB: () => ({ database: sqlJsLike, embedder: null }), + get: (kind: string) => (kind === 'tieredCache' ? cache : null), + }); + const ns = 'no-intransaction'; + await mod.bridgeStoreEntry({ key: 'k', value: 's', namespace: ns, generateEmbeddingFlag: false, dbPath }); + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.entry!.content).toBe('s'); + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.cacheHit).toBe(true); + expect(probes.filter((p) => /data_version/i.test(p))).toEqual([]); + }); + + it('keeps checking after a transient failure of the check', async () => { + const mod = await import('../src/memory/memory-bridge.js'); + let failNext = true; + // A handle whose data_version read fails once (e.g. SQLITE_BUSY), then works. + const flakyDb = new Proxy(db, { + get(target, prop) { + if (prop === 'prepare') { + return (sql: string) => { + const stmt = target.prepare(sql); + if (!/data_version/i.test(sql)) return stmt; + return { get: () => { if (failNext) { failNext = false; throw new Error('SQLITE_BUSY'); } return stmt.get(); } }; + }; + } + const v = Reflect.get(target, prop, target); + return typeof v === 'function' ? v.bind(target) : v; + }, + }); + mod.__setMemoryBridgeRegistryForTests({ + getAgentDB: () => ({ database: flakyDb, embedder: null }), + get: (kind: string) => (kind === 'tieredCache' ? cache : null), + }); + const ns = 'transient-failure'; + await mod.bridgeStoreEntry({ key: 'k', value: 'v1', namespace: ns, generateEmbeddingFlag: false, dbPath }); + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.entry!.content).toBe('v1'); // check throws here + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.entry!.content).toBe('v1'); + + otherProcess + .prepare('UPDATE memory_entries SET content = ?, updated_at = ? WHERE namespace = ? AND key = ?') + .run('v2', Date.now(), ns, 'k'); + + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.entry!.content).toBe('v2'); + }); + + it('drops an already-warm cache when the check itself fails', async () => { + const mod = await import('../src/memory/memory-bridge.js'); + let failNext = false; + // Same flaky handle, but the failure is armed AFTER the cache is warm and + // after another process has written: the read must not trust the cache it + // could not validate. + const flakyDb = new Proxy(db, { + get(target, prop) { + if (prop === 'prepare') { + return (sql: string) => { + const stmt = target.prepare(sql); + if (!/data_version/i.test(sql)) return stmt; + return { get: () => { if (failNext) { failNext = false; throw new Error('SQLITE_BUSY'); } return stmt.get(); } }; + }; + } + const v = Reflect.get(target, prop, target); + return typeof v === 'function' ? v.bind(target) : v; + }, + }); + mod.__setMemoryBridgeRegistryForTests({ + getAgentDB: () => ({ database: flakyDb, embedder: null }), + get: (kind: string) => (kind === 'tieredCache' ? cache : null), + }); + const ns = 'transient-failure-warm-cache'; + await mod.bridgeStoreEntry({ key: 'k', value: 'v1', namespace: ns, generateEmbeddingFlag: false, dbPath }); + await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); // warms the cache + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.cacheHit).toBe(true); + + otherProcess + .prepare('UPDATE memory_entries SET content = ?, updated_at = ? WHERE namespace = ? AND key = ?') + .run('v2', Date.now(), ns, 'k'); + + failNext = true; // the check can't see the foreign commit — it must fail safe + const after = await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); + expect(after!.cacheHit).toBe(false); + expect(after!.entry!.content).toBe('v2'); + }); + + it('drops an already-warm cache when the check returns an unusable answer', async () => { + const mod = await import('../src/memory/memory-bridge.js'); + let breakNext = false; + // The check neither throws nor answers: `PRAGMA data_version` yields a row + // without a usable number. That is no more evidence the store is unchanged + // than a throw is, so it must take the same path as the throwing case. + const oddDb = new Proxy(db, { + get(target, prop) { + if (prop === 'prepare') { + return (sql: string) => { + const stmt = target.prepare(sql); + if (!/data_version/i.test(sql)) return stmt; + return { get: () => (breakNext ? { data_version: undefined } : stmt.get()) }; + }; + } + const v = Reflect.get(target, prop, target); + return typeof v === 'function' ? v.bind(target) : v; + }, + }); + mod.__setMemoryBridgeRegistryForTests({ + getAgentDB: () => ({ database: oddDb, embedder: null }), + get: (kind: string) => (kind === 'tieredCache' ? cache : null), + }); + const ns = 'unusable-answer-warm-cache'; + await mod.bridgeStoreEntry({ key: 'k', value: 'v1', namespace: ns, generateEmbeddingFlag: false, dbPath }); + await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); // warms the cache + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.cacheHit).toBe(true); + + otherProcess + .prepare('UPDATE memory_entries SET content = ?, updated_at = ? WHERE namespace = ? AND key = ?') + .run('v2', Date.now(), ns, 'k'); + + breakNext = true; // the check cannot validate — it must not trust the cache + const after = await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); + expect(after!.cacheHit).toBe(false); + expect(after!.entry!.content).toBe('v2'); + + // And the baseline was not advanced on an unusable answer: once the check + // works again it still sees the foreign commit rather than skipping it. + breakNext = false; + expect((await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.entry!.content).toBe('v2'); + }); + + it('clears the cache it inherits when the handle is replaced', async () => { + const { bridgeStoreEntry, bridgeGetEntry } = await bridge(); + const ns = 'replaced-handle'; + await bridgeStoreEntry({ key: 'k', value: 'v', namespace: ns, generateEmbeddingFlag: false, dbPath }); + await bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); // warms the cache + expect((await bridgeGetEntry({ key: 'k', namespace: ns, dbPath }))!.cacheHit).toBe(true); + + // A new handle (e.g. the registry reopened the database) has no baseline + // of its own, so its first check must not trust the cache it inherits. + const mod = await import('../src/memory/memory-bridge.js'); + const reopened = new Proxy(db, { + get(target, prop) { + const v = Reflect.get(target, prop, target); + return typeof v === 'function' ? v.bind(target) : v; + }, + }); + mod.__setMemoryBridgeRegistryForTests({ + getAgentDB: () => ({ database: reopened, embedder: null }), + get: (kind: string) => (kind === 'tieredCache' ? cache : null), + }); + const first = await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); + expect(first!.cacheHit).toBe(false); + expect(first!.entry!.content).toBe('v'); + }); + + it('still answers the read when the handle itself throws on the `in` check', async () => { + const { bridgeStoreEntry, bridgeGetEntry } = await bridge(); + const ns = 'hostile-handle'; + await bridgeStoreEntry({ key: 'k', value: 'hostile', namespace: ns, generateEmbeddingFlag: false, dbPath }); + + const mod = await import('../src/memory/memory-bridge.js'); + // An exotic handle whose `has` trap explodes, so `'inTransaction' in db` + // throws. The check must swallow it and leave the read intact. + const hostileDb = new Proxy(db, { + has() { throw new Error('exotic handle: `in` explodes'); }, + get(target, prop) { + const v = Reflect.get(target, prop, target); + return typeof v === 'function' ? v.bind(target) : v; + }, + }); + mod.__setMemoryBridgeRegistryForTests({ + getAgentDB: () => ({ database: hostileDb, embedder: null }), + get: (kind: string) => (kind === 'tieredCache' ? cache : null), + }); + const r = await mod.bridgeGetEntry({ key: 'k', namespace: ns, dbPath }); + expect(r).not.toBeNull(); + expect(r!.found).toBe(true); + expect(r!.entry!.content).toBe('hostile'); + }); + + it("this process's own writes do not flush the cache", async () => { + const { bridgeStoreEntry, bridgeGetEntry } = await bridge(); + const ns = 'own-writes'; + await bridgeStoreEntry({ key: 'a', value: 'A', namespace: ns, generateEmbeddingFlag: false, dbPath }); + await bridgeStoreEntry({ key: 'b', value: 'B', namespace: ns, generateEmbeddingFlag: false, dbPath }); + await bridgeGetEntry({ key: 'a', namespace: ns, dbPath }); // populates 'a' + + // An unrelated write on this connection (plus the access_count bumps the + // reads themselves perform) must not look like a foreign commit. + await bridgeStoreEntry({ key: 'c', value: 'C', namespace: ns, generateEmbeddingFlag: false, dbPath }); + await bridgeGetEntry({ key: 'b', namespace: ns, dbPath }); + + const again = await bridgeGetEntry({ key: 'a', namespace: ns, dbPath }); + expect(again!.cacheHit).toBe(true); + expect(again!.entry!.content).toBe('A'); + }); +}); diff --git a/v3/@claude-flow/cli/src/memory/memory-bridge.ts b/v3/@claude-flow/cli/src/memory/memory-bridge.ts index 948ef83b11..9935b51e35 100644 --- a/v3/@claude-flow/cli/src/memory/memory-bridge.ts +++ b/v3/@claude-flow/cli/src/memory/memory-bridge.ts @@ -573,6 +573,79 @@ async function cacheGet(registry: any, cacheKey: string): Promise { } } +/** + * Cross-process cache coherence. + * + * The TieredCache lives in this process, but the database file is shared: + * Claude Code, Codex and Grok each run their own `mcp start` against the + * same `.swarm/` store, and each process only invalidates entries on its + * OWN writes. A key another process updated or deleted kept coming back + * from this process's cache — old value, `found: true` — until the cached + * entry expired (TieredCache TTL, 5 minutes by default; reads don't extend + * it). + * + * SQLite's `PRAGMA data_version` changes whenever a DIFFERENT connection has + * committed to the database file since this connection last asked, and never + * for this connection's own writes (https://sqlite.org/pragma.html#pragma_data_version). + * Checking it before a cached read is an exact "has anyone else written?" + * test that costs one pragma call. When it moves, drop the whole cache: + * every cached shape was derived from the old database state. The first + * check on a handle records its baseline and clears: a no-op for a fresh + * handle, and the right thing if a handle is ever replaced under a live + * cache. + * + * Native better-sqlite3 handles only. agentdb's sql.js fallback works on a + * private in-memory image that never sees another process's commits, so + * there is nothing to detect; it is skipped rather than probed. A handle + * that cannot prepare the check is remembered and never probed again; a + * check that fails on a given read (e.g. SQLITE_BUSY) clears the cache for + * safety and is simply retried on the next read. + */ +const _cacheDataVersionStmts = new WeakMap(); +const _cacheDataVersions = new WeakMap(); +const _cacheDataVersionUnsupported = new WeakSet(); + +function dropCacheIfDbChangedElsewhere(registry: any, ctx: { db: any; agentdb?: any }): void { + // Never let this check break the read it guards: any throw falls back to + // the pre-fix behaviour (a possibly stale hit). + try { + const db = ctx.db; + if (!db || ctx.agentdb?.isWasm === true || !('inTransaction' in db)) return; + if (_cacheDataVersionUnsupported.has(db)) return; + let stmt = _cacheDataVersionStmts.get(db); + if (!stmt) { + try { + stmt = db.prepare('PRAGMA data_version'); + _cacheDataVersionStmts.set(db, stmt!); + } catch { + _cacheDataVersionUnsupported.add(db); // structural: this handle can't answer + return; + } + } + let cache: any = null; + try { cache = registry.get('tieredCache'); } catch { /* no cache, nothing to drop */ } + try { + const version = stmt!.get()?.data_version; + if (typeof version !== 'number') { + // #3360: an unusable answer is not evidence the store is unchanged. + // Same posture as the catch below — a check we cannot validate drops + // the cache rather than trusting it, and leaves the baseline alone so + // the next read re-checks. + if (cache && typeof cache.clear === 'function') cache.clear(); + return; + } + if (_cacheDataVersions.get(db) === version) return; + if (cache && typeof cache.clear === 'function') cache.clear(); + _cacheDataVersions.set(db, version); + } catch { + // Transient (e.g. SQLITE_BUSY): don't trust the cache for this read. + if (cache && typeof cache.clear === 'function') cache.clear(); + } + } catch { + // Non-fatal. + } +} + /** * Write to TieredCache after DB write. */ @@ -1494,6 +1567,7 @@ export async function bridgeGetEntry(options: { const safeNs = String(namespace).replace(/:/g, '_'); const safeKey = String(key).replace(/:/g, '_'); const cacheKey = `entry:${safeNs}:${safeKey}`; + dropCacheIfDbChangedElsewhere(registry, ctx); const cached = await cacheGet(registry, cacheKey); if (cached && cached.content) { return {