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) => {