From 2859ef5d8c85e8c1dff25a6a6506ef5769c41a09 Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Tue, 10 Nov 2026 15:52:28 +0800 Subject: [PATCH 1/2] fix: skip unnecessary session reads for scoped recall Signed-off-by: reevesc88 --- 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(); }); } From 2cc4739ecce29489c18aecd302d870741d1861a1 Mon Sep 17 00:00:00 2001 From: reevesc88 Date: Tue, 10 Nov 2026 15:57:11 +0800 Subject: [PATCH 2/2] test: name directory isolation regression by behavior Signed-off-by: reevesc88 --- test/search-project-isolation.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/search-project-isolation.test.ts b/test/search-project-isolation.test.ts index 65a8e7eea4..7a333113ef 100644 --- a/test/search-project-isolation.test.ts +++ b/test/search-project-isolation.test.ts @@ -292,7 +292,7 @@ describe.each(["keyword", "hybrid", "hybrid-fallback"] as const)( expect(sessionReads).toEqual([]); }); - it("still reads sessions for directory-filtered explicitly scoped memories", async () => { + it("excludes explicitly scoped memories outside the requested directory", async () => { const session = addSession("project-a"); addMemory({ project: "project-a", sessionIds: [session.id] }); expect(await recall({ cwd: "/different/project" })).toEqual([]);