Merge PR #3480: fix(memory): preserve namespace and key identity in entry cache

This commit is contained in:
ruv committed 2026-09-27 13:15:50 -04:00
commit 1be3f4ca37
2 files changed
+38 -9

No files matched your search

@@ -0,0 +1,29 @@
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import Database from 'better-sqlite3';
vi.mock('../src/memory/intelligence.js', () => ({ recordSignalProcessed() {} }));
import * as bridge from '../src/memory/memory-bridge.js';
let db: Database.Database;
beforeEach(() => {
db=new Database(':memory:'); const cache=new Map();
bridge.__setMemoryBridgeRegistryForTests({getAgentDB:()=>({database:db}),get:(name:string)=>name==='tieredCache'?cache:null});
});
afterEach(()=>{bridge._resetRegistryCacheForTest();db.close();});
const put=(namespace:string,key:string,value:string)=>bridge.bridgeStoreEntry({namespace,key,value,generateEmbeddingFlag:false,upsert:true});
const get=(namespace:string,key:string)=>bridge.bridgeGetEntry({namespace,key});
it.each([
['a:b','a_b','key','key'],
['default','default','x:y','x_y'],
['a:b','a','c','b:c'],
])('keeps cache identities distinct for %s / %s',async(ns1,ns2,key1,key2)=>{
expect(await put(ns1,key1,'first')).toMatchObject({success:true});
expect(await put(ns2,key2,'second')).toMatchObject({success:true});
expect(await get(ns1,key1)).toMatchObject({entry:{content:'first'}});
expect(await get(ns2,key2)).toMatchObject({entry:{content:'second'}});
expect(await get(ns1,key1)).toMatchObject({entry:{content:'first'}});
await put(ns2,key2,'updated');
expect(await get(ns1,key1)).toMatchObject({entry:{content:'first'}});
expect(await get(ns2,key2)).toMatchObject({entry:{content:'updated'}});
await bridge.bridgeDeleteEntry({namespace:ns1,key:key1});
expect(await get(ns1,key1)).toMatchObject({found:false});
expect(await get(ns2,key2)).toMatchObject({entry:{content:'updated'}});
});
@@ -600,6 +600,12 @@ function computeTermDocFreqs(
// ===== Phase 2: TieredCache helpers =====
/** Preserve the exact namespace/key tuple; punctuation is valid memory data. */
function entryCacheKey(namespace: string, key: string): string {
return `entry:${JSON.stringify([namespace, key])}`;
}
/**
* Try to read from TieredCache before hitting DB.
* Returns cached value or null if cache miss.
@@ -1249,9 +1255,7 @@ export async function bridgeStoreEntry(options: {
// next `bridgeGetEntry` to load the authoritative row (which correctly
// parses tags/metadata from the JSON columns) and repopulate the cache
// from that full shape.
const safeNs = String(namespace).replace(/:/g, '_');
const safeKey = String(key).replace(/:/g, '_');
const cacheKey = `entry:${safeNs}:${safeKey}`;
const cacheKey = entryCacheKey(namespace, key);
await cacheInvalidate(registry, cacheKey);
// Phase 4: AttestationLog write audit
@@ -1622,9 +1626,7 @@ export async function bridgeGetEntry(options: {
const { key, namespace = 'default' } = options;
// Phase 2: Check TieredCache first
const safeNs = String(namespace).replace(/:/g, '_');
const safeKey = String(key).replace(/:/g, '_');
const cacheKey = `entry:${safeNs}:${safeKey}`;
const cacheKey = entryCacheKey(namespace, key);
dropCacheIfDbChangedElsewhere(registry, ctx);
const cached = await cacheGet(registry, cacheKey);
if (cached && cached.content) {
@@ -1763,9 +1765,7 @@ export async function bridgeDeleteEntry(options: {
} catch { /* non-WAL, busy, or unsupported — non-fatal */ }
// Phase 2: Invalidate cache
const safeNs = String(namespace).replace(/:/g, '_');
const safeKey = String(key).replace(/:/g, '_');
await cacheInvalidate(registry, `entry:${safeNs}:${safeKey}`);
await cacheInvalidate(registry, entryCacheKey(namespace, key));
// Phase 4: AttestationLog delete audit
if (changes > 0) {