From 7d745f02adf0b4b87f060f6c79e4629b8a222f74 Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Wed, 11 Nov 2026 23:36:16 +0800 Subject: [PATCH] fix(search): skip stored-memory reads when index has memoryType Compact smart-search was issuing a KV read per memory hit even when the hybrid index already carried memoryType. Prefer the indexed value and only read the store for legacy rows; add coverage for the skip and failed-read paths. Signed-off-by: reevesc88 --- src/functions/smart-search.ts | 3 ++- test/memory-type-metadata.test.ts | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index 4030ba6e4b..b46bfb8965 100644 --- a/src/functions/smart-search.ts +++ b/src/functions/smart-search.ts @@ -251,7 +251,8 @@ export function registerSmartSearchFunction( const compact: CompactSearchResult[] = await Promise.all(filteredHybrid.map(async (r) => { const layer = r.layer ?? getSearchIndex().layerOf(r.observation.id) ?? getSearchResultLayer(r.observation.id, r.sessionId); const memoryType = layer === "memory" - ? (await kv.get(KV.memories, r.observation.id).catch(() => null))?.type ?? r.observation.memoryType + ? r.observation.memoryType + ?? (await kv.get(KV.memories, r.observation.id).catch(() => null))?.type : undefined; return { obsId: r.observation.id, diff --git a/test/memory-type-metadata.test.ts b/test/memory-type-metadata.test.ts index da93b3646a..c9a93ae1de 100644 --- a/test/memory-type-metadata.test.ts +++ b/test/memory-type-metadata.test.ts @@ -46,14 +46,23 @@ function fixture(saved: Memory, legacy = false, failMemoryReads = false) { bm25Score: 1, vectorScore: 0, graphScore: 0, }]); registerSmartSearchFunction(sdk as unknown as IIIClient, kv as unknown as StateKV, search); - return { call: (args: object) => handler(args), saved }; + return { call: (args: object) => handler(args), saved, kv }; } describe("original memory type metadata", () => { - it("keeps compact search working when the stored memory read fails", async () => { + it("skips stored-memory reads when the search index already carries memoryType", async () => { const f = fixture(memory("bug"), false, true); + const get = vi.spyOn(f.kv, "get"); const compact = await f.call({ query: "Synthetic", agentId: "*", targetLayer: "memory", includeLessons: false }); expect(compact.results[0]).toMatchObject({ obsId: f.saved.id, type: "decision", memoryType: "bug" }); + expect(get).not.toHaveBeenCalledWith("mem:memories", f.saved.id); + }); + + it("keeps compact search working when a legacy index forces a stored-memory read that fails", async () => { + const f = fixture(memory("bug"), true, true); + const compact = await f.call({ query: "Synthetic", agentId: "*", targetLayer: "memory", includeLessons: false }); + expect(compact.results[0]).toMatchObject({ obsId: f.saved.id, type: "decision" }); + expect(compact.results[0]).not.toHaveProperty("memoryType"); }); it.each(types)("preserves %s without changing the observation type or input", (type) => {