From 5633bddd09f6712048b53a4712b323901f066e3c Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Tue, 10 Nov 2026 15:46:41 +0800 Subject: [PATCH] fix: skip unnecessary session reads for scoped recall --- src/functions/search.ts | 4 +++- test/search-project-isolation.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/functions/search.ts b/src/functions/search.ts index f17ae33f9f..9f9459a4d7 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -929,7 +929,9 @@ export function registerSearchFunction(sdk: IIIClient, kv: StateKV): void { ? await loadMemoryProject(r.obsId) : null if (projectFilter && memoryProject !== null && memoryProject !== projectFilter) return null - const session = projectFilter || cwdFilter ? await loadSession(r.sessionId) : null + const session = cwdFilter || (projectFilter && memoryProject === null) + ? await loadSession(r.sessionId) + : null if (projectFilter && memoryProject === null && !session) return null if (cwdFilter && layer === "observation" && !session) return null if (session) { diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index 83a4d9b413..65a8e7eea4 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -285,6 +285,27 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( expect(sessionReads).not.toContain("session_10"); }); + it("recalls an explicitly matching memory when session reads fail", async () => { + const memory = addMemory({ project: "project-a", sessionIds: ["session_unavailable"] }); + failSessionReads = true; + expect(await recall()).toEqual([memory.id]); + expect(sessionReads).toEqual([]); + }); + + it("still reads sessions for directory-filtered explicitly scoped memories", async () => { + const session = addSession("project-a"); + addMemory({ project: "project-a", sessionIds: [session.id] }); + expect(await recall({ cwd: "/different/project" })).toEqual([]); + expect(sessionReads).toEqual([session.id]); + }); + + it("still requires session ownership for memories without an explicit project", async () => { + addMemory({ sessionIds: ["session_unavailable"] }); + failSessionReads = true; + await expect(recall()).rejects.toThrow("session store unavailable"); + expect(sessionReads).toEqual(["session_unavailable"]); + }); + it("does not read sessions for agent-only filtering", async () => { const memory = addMemory({ project: "project-a", agentId: "agent-a" }); failSessionReads = true; @@ -305,6 +326,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( }); const keyword = vi.spyOn(getSearchIndex(), "search").mockReturnValue([]); expect(await recall({ limit: 2 })).toEqual([matching.id]); + expect(calls).toBe(2); expect(keyword).not.toHaveBeenCalled(); }); }