From 6909c21437dff21e7971bdecf8eed81db3263b45 Mon Sep 17 00:00:00 2001 From: sk91 Date: Thu, 13 Aug 2026 20:52:46 +0300 Subject: [PATCH 1/3] fix(mcp): thread project param through standalone MCP bridge MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit memory_save's inputSchema advertised a project field, but validate() and handleProxy() in src/mcp/standalone.ts never read it, so it was silently dropped before reaching /agentmemory/remember (#926). memory_recall and memory_smart_search had no project field at all in their inputSchema, so a memory saved with project set could not be read back scoped to that project through the MCP tools (#787). Fix: - validate(): extract project for memory_save, memory_recall, and memory_smart_search. - handleProxy(): forward project into the POST bodies for /agentmemory/remember, /agentmemory/search, and /agentmemory/smart-search. - tools-registry.ts: add project to the memory_recall and memory_smart_search inputSchema (memory_save already had it). Adds regression tests in mcp-standalone-proxy.test.ts that exercise handleToolCall (validate + handleProxy) directly, asserting project appears in each outgoing request body — the previous test file only covered mem::remember against a mocked KV store and never exercised this MCP shim, which is why the bug shipped unnoticed. Verified via manual smoke test against a local server: memory_save persists project (confirmed via GET /agentmemory/memories), and memory_recall correctly scopes results to the given project (backed by mem::search's existing project filter). Note: memory_smart_search forwards project to the server, and it is honored for lesson recall, but mem::smart-search's main hybrid-search results are not filtered by project server-side (src/functions/smart- search.ts only threads project into recallLessons, not into the hybridResults filter). That's a backend gap in mem::smart-search itself, not a transport bug in this MCP bridge — filed separately, not fixed in this PR. --- src/mcp/standalone.ts | 25 ++++++++++++---- src/mcp/tools-registry.ts | 12 ++++++++ test/mcp-standalone-proxy.test.ts | 48 +++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/mcp/standalone.ts b/src/mcp/standalone.ts index 1ace150b1d..9df4dee883 100644 --- a/src/mcp/standalone.ts +++ b/src/mcp/standalone.ts @@ -105,6 +105,7 @@ interface Validated { tokenBudget?: number; memoryIds?: string[]; reason?: string; + project?: string; } function validate(toolName: string, args: Record): Validated { @@ -122,6 +123,10 @@ function validate(toolName: string, args: Record): Validated { v.type = (args["type"] as string) || "fact"; v.concepts = normalizeList(args["concepts"]); v.files = normalizeList(args["files"]); + const project = args["project"]; + if (typeof project === "string" && project.trim()) { + v.project = project.trim(); + } return v; } case "memory_recall": @@ -143,6 +148,10 @@ function validate(toolName: string, args: Record): Validated { const n = Number(budget); if (Number.isFinite(n) && n > 0) v.tokenBudget = Math.floor(n); } + const project = args["project"]; + if (typeof project === "string" && project.trim()) { + v.project = project.trim(); + } return v; } case "memory_sessions": { @@ -173,14 +182,16 @@ async function handleProxy( ): Promise<{ content: Array<{ type: string; text: string }> }> { switch (v.tool) { case "memory_save": { + const body: Record = { + content: v.content, + type: v.type, + concepts: v.concepts, + files: v.files, + }; + if (v.project != null) body["project"] = v.project; const result = await handle.call("/agentmemory/remember", { method: "POST", - body: JSON.stringify({ - content: v.content, - type: v.type, - concepts: v.concepts, - files: v.files, - }), + body: JSON.stringify(body), }); return textResponse(result); } @@ -191,6 +202,7 @@ async function handleProxy( format: v.format ?? "full", }; if (v.tokenBudget != null) body["token_budget"] = v.tokenBudget; + if (v.project != null) body["project"] = v.project; const result = await handle.call("/agentmemory/search", { method: "POST", body: JSON.stringify(body), @@ -201,6 +213,7 @@ async function handleProxy( const body: Record = { query: v.query, limit: v.limit }; if (v.format != null) body["format"] = v.format; if (v.tokenBudget != null) body["token_budget"] = v.tokenBudget; + if (v.project != null) body["project"] = v.project; const result = await handle.call("/agentmemory/smart-search", { method: "POST", body: JSON.stringify(body), diff --git a/src/mcp/tools-registry.ts b/src/mcp/tools-registry.ts index 464cb3b0c4..abb82ebfd5 100644 --- a/src/mcp/tools-registry.ts +++ b/src/mcp/tools-registry.ts @@ -32,6 +32,12 @@ export const CORE_TOOLS: McpToolDef[] = [ type: "number", description: "Optional token budget to trim returned results", }, + project: { + type: "string", + description: + "Filter results to memories saved under this stable canonical project " + + "identifier (same value used with memory_save's project field).", + }, }, required: ["query"], }, @@ -130,6 +136,12 @@ export const CORE_TOOLS: McpToolDef[] = [ description: "Comma-separated observation IDs to expand", }, limit: { type: "number", description: "Max results (default 10)" }, + project: { + type: "string", + description: + "Filter results to memories saved under this stable canonical project " + + "identifier (same value used with memory_save's project field).", + }, }, required: ["query"], }, diff --git a/test/mcp-standalone-proxy.test.ts b/test/mcp-standalone-proxy.test.ts index dc08a024ee..4ce446e069 100644 --- a/test/mcp-standalone-proxy.test.ts +++ b/test/mcp-standalone-proxy.test.ts @@ -130,6 +130,54 @@ describe("@agentmemory/mcp standalone — server proxy (issue #159)", () => { expect(recallBody).not.toHaveProperty("token_budget"); }); + it("forwards project on memory_save to POST /agentmemory/remember (#926)", async () => { + let rememberBody: Record | undefined; + installFetch((url, init) => { + if (url.endsWith("/agentmemory/livez")) return new Response("ok", { status: 200 }); + if (url.endsWith("/agentmemory/remember")) { + rememberBody = init?.body ? JSON.parse(init.body as string) : undefined; + return new Response(JSON.stringify({ id: "m-1", action: "created" }), { + status: 200, + headers: { "content-type": "application/json" }, + }); + } + return new Response("not found", { status: 404 }); + }); + await handleToolCall("memory_save", { + content: "project-scoped entry", + project: "acme-widgets", + }); + expect(rememberBody?.["project"]).toBe("acme-widgets"); + }); + + it("forwards project on memory_recall to POST /agentmemory/search (#787)", async () => { + let searchBody: Record | undefined; + installFetch((url, init) => { + if (url.endsWith("/agentmemory/livez")) return new Response("ok", { status: 200 }); + if (url.endsWith("/agentmemory/search")) { + searchBody = init?.body ? JSON.parse(init.body as string) : undefined; + return new Response(JSON.stringify({ mode: "full", facts: [] }), { status: 200 }); + } + return new Response("not found", { status: 404 }); + }); + await handleToolCall("memory_recall", { query: "auth bug", project: "acme-widgets" }); + expect(searchBody?.["project"]).toBe("acme-widgets"); + }); + + it("forwards project on memory_smart_search to POST /agentmemory/smart-search (#787)", async () => { + let smartSearchBody: Record | undefined; + installFetch((url, init) => { + if (url.endsWith("/agentmemory/livez")) return new Response("ok", { status: 200 }); + if (url.endsWith("/agentmemory/smart-search")) { + smartSearchBody = init?.body ? JSON.parse(init.body as string) : undefined; + return new Response(JSON.stringify({ mode: "compact", results: [] }), { status: 200 }); + } + return new Response("not found", { status: 404 }); + }); + await handleToolCall("memory_smart_search", { query: "auth bug", project: "acme-widgets" }); + expect(smartSearchBody?.["project"]).toBe("acme-widgets"); + }); + it("proxies memory_governance_delete to the DELETE REST endpoint", async () => { const calls: Array<{ url: string; method: string; body?: unknown }> = []; installFetch((url, init) => { From 78833f7a9138cba1ad2722911d00468c67fe1624 Mon Sep 17 00:00:00 2001 From: sk91 Date: Thu, 13 Aug 2026 21:08:08 +0300 Subject: [PATCH 2/3] fix(mcp): address CodeRabbit review on #1195 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - mem::smart-search now filters hybrid results by project, not just lessons. Previously data.project only reached recallLessons(), so a project-scoped memory_smart_search still returned every project's observations. Resolves each hit's project via its session (falling back to a KV.memories probe for synthetic 'memory' sessions), same pattern as mem::search's existing project filter. Unresolvable projects pass through unscoped rather than being incorrectly excluded. - standalone.ts's handleLocal fallback now persists project on memory_save and scopes memory_recall/memory_smart_search to it. Previously a scoped request during a server outage would silently drop project on save and return unscoped results on read — the same bug class as #926/#787, just in the local-KV fallback path instead of the proxy path. - Added a whitespace-trimming regression test for memory_save/ memory_recall so a future change that forwards an untrimmed project value would be caught. All addressing CodeRabbit's review comments on PR #1195. --- src/functions/smart-search.ts | 88 ++++++++++++++++++++++++++----- src/mcp/standalone.ts | 8 +++ test/mcp-standalone-proxy.test.ts | 76 ++++++++++++++++++++++++++ test/smart-search.test.ts | 85 +++++++++++++++++++++++++++++ 4 files changed, 244 insertions(+), 13 deletions(-) diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index 5d0147d9e0..fdf5c209e3 100644 --- a/src/functions/smart-search.ts +++ b/src/functions/smart-search.ts @@ -5,6 +5,8 @@ import type { CompressedObservation, HybridSearchResult, Lesson, + Memory, + Session, } from "../types.js"; import { KV } from "../state/schema.js"; import { StateKV } from "../state/kv.js"; @@ -186,15 +188,21 @@ export function registerSmartSearchFunction( const lessonLimit = Math.min(limit, 10); const includeLessons = data.includeLessons !== false; + const projectFilter = + typeof data.project === "string" && data.project.trim().length > 0 + ? data.project.trim() + : undefined; + // Over-fetch when filtering. Hybrid search can't filter on - // agentId (BM25/vector indexes don't carry it), so we ask the - // searcher for more hits than we need and trim post-filter. 3× - // is a defensible middle ground: enough headroom for a small - // workload, capped at 300 so a 100-limit request never asks for - // thousands of hits. - const overFetchLimit = filterAgentId - ? Math.min(limit * 3, 300) - : limit; + // agentId or project (BM25/vector indexes don't carry them), so + // we ask the searcher for more hits than we need and trim + // post-filter. 3× is a defensible middle ground: enough headroom + // for a small workload, capped at 300 so a 100-limit request + // never asks for thousands of hits. + const overFetchLimit = + filterAgentId || projectFilter + ? Math.min(limit * 3, 300) + : limit; const [hybridResults, lessons] = await Promise.all([ searchFn(data.query, overFetchLimit), @@ -203,11 +211,23 @@ export function registerSmartSearchFunction( : Promise.resolve([]), ]); - const filteredHybrid = filterAgentId - ? hybridResults - .filter((r) => r.observation.agentId === filterAgentId) - .slice(0, limit) - : hybridResults.slice(0, limit); + const agentFiltered = filterAgentId + ? hybridResults.filter((r) => r.observation.agentId === filterAgentId) + : hybridResults; + + // #787 follow-up: project was previously only threaded into + // recallLessons above — the main hybrid results leaked results + // from every project regardless of the requested scope. Mirrors + // mem::search's session -> project resolution (see search.ts): + // resolve each hit's session, fall back to a KV.memories probe + // for synthetic 'memory' sessions (mem::remember entries), and + // let unresolvable/unknown projects pass through unscoped rather + // than incorrectly excluding them. + const filteredHybrid = ( + projectFilter + ? await filterByProject(kv, agentFiltered, projectFilter) + : agentFiltered + ).slice(0, limit); const compact: CompactSearchResult[] = filteredHybrid.map((r) => ({ obsId: r.observation.id, @@ -287,6 +307,48 @@ export function registerSmartSearchFunction( ); } +// Resolves each hit's project the same way mem::search does (see +// search.ts's loadSession/loadMemoryProject): via its session's +// `project` field, falling back to a KV.memories probe for synthetic +// sessionIds (mem::remember entries indexed under 'memory' or a +// sessionId with no KV.sessions row). A result whose project can't be +// resolved is treated as unscoped and passes through — we never want +// an indexing gap to silently exclude an otherwise-matching result. +async function filterByProject( + kv: StateKV, + results: HybridSearchResult[], + projectFilter: string, +): Promise { + const sessionCache = new Map(); + const loadSession = async (sessionId: string): Promise => { + if (sessionCache.has(sessionId)) return sessionCache.get(sessionId)!; + const s = await kv.get(KV.sessions, sessionId); + sessionCache.set(sessionId, s ?? null); + return s ?? null; + }; + + const memoryProjectCache = new Map(); + const loadMemoryProject = async (obsId: string): Promise => { + if (memoryProjectCache.has(obsId)) return memoryProjectCache.get(obsId)!; + const mem = await kv.get(KV.memories, obsId).catch(() => null); + const proj = mem?.project ?? null; + memoryProjectCache.set(obsId, proj); + return proj; + }; + + const kept: HybridSearchResult[] = []; + for (const r of results) { + const session = await loadSession(r.sessionId); + if (session) { + if (session.project === projectFilter) kept.push(r); + continue; + } + const memProject = await loadMemoryProject(r.observation.id); + if (memProject === null || memProject === projectFilter) kept.push(r); + } + return kept; +} + async function recallLessons( sdk: ISdk, query: string, diff --git a/src/mcp/standalone.ts b/src/mcp/standalone.ts index 9df4dee883..6e0e77ee8d 100644 --- a/src/mcp/standalone.ts +++ b/src/mcp/standalone.ts @@ -265,6 +265,7 @@ async function handleLocal( content: v.content, concepts: v.concepts, files: v.files, + project: v.project, createdAt: isoNow, updatedAt: isoNow, strength: 7, @@ -283,6 +284,13 @@ async function handleLocal( const all = await kvInstance.list>("mem:memories"); const results = all + // #926/#787 follow-up: the local KV fallback used to ignore + // project entirely, so a scoped search during a server outage + // could return memories from every project. Filter strictly — + // unlike the server's session-resolution fallback, this KV has + // no session store to fall back on, so an unscoped record is + // excluded rather than passed through when a filter is active. + .filter((m) => v.project == null || m["project"] === v.project) .filter((m) => { const text = [ typeof m["title"] === "string" ? m["title"] : "", diff --git a/test/mcp-standalone-proxy.test.ts b/test/mcp-standalone-proxy.test.ts index 4ce446e069..54425de84a 100644 --- a/test/mcp-standalone-proxy.test.ts +++ b/test/mcp-standalone-proxy.test.ts @@ -150,6 +150,26 @@ describe("@agentmemory/mcp standalone — server proxy (issue #159)", () => { expect(rememberBody?.["project"]).toBe("acme-widgets"); }); + it("trims whitespace off project before forwarding (memory_save)", async () => { + let rememberBody: Record | undefined; + installFetch((url, init) => { + if (url.endsWith("/agentmemory/livez")) return new Response("ok", { status: 200 }); + if (url.endsWith("/agentmemory/remember")) { + rememberBody = init?.body ? JSON.parse(init.body as string) : undefined; + return new Response(JSON.stringify({ id: "m-1", action: "created" }), { + status: 200, + headers: { "content-type": "application/json" }, + }); + } + return new Response("not found", { status: 404 }); + }); + await handleToolCall("memory_save", { + content: "project-scoped entry", + project: " acme-widgets ", + }); + expect(rememberBody?.["project"]).toBe("acme-widgets"); + }); + it("forwards project on memory_recall to POST /agentmemory/search (#787)", async () => { let searchBody: Record | undefined; installFetch((url, init) => { @@ -178,6 +198,20 @@ describe("@agentmemory/mcp standalone — server proxy (issue #159)", () => { expect(smartSearchBody?.["project"]).toBe("acme-widgets"); }); + it("trims whitespace off project before forwarding (memory_recall)", async () => { + let searchBody: Record | undefined; + installFetch((url, init) => { + if (url.endsWith("/agentmemory/livez")) return new Response("ok", { status: 200 }); + if (url.endsWith("/agentmemory/search")) { + searchBody = init?.body ? JSON.parse(init.body as string) : undefined; + return new Response(JSON.stringify({ mode: "full", facts: [] }), { status: 200 }); + } + return new Response("not found", { status: 404 }); + }); + await handleToolCall("memory_recall", { query: "auth bug", project: " acme-widgets " }); + expect(searchBody?.["project"]).toBe("acme-widgets"); + }); + it("proxies memory_governance_delete to the DELETE REST endpoint", async () => { const calls: Array<{ url: string; method: string; body?: unknown }> = []; installFetch((url, init) => { @@ -215,6 +249,48 @@ describe("@agentmemory/mcp standalone — server proxy (issue #159)", () => { ]); }); + it("local fallback preserves project on memory_save and scopes memory_recall/memory_smart_search to it (#926/#787)", async () => { + installFetch(() => { + throw new Error("ECONNREFUSED"); + }); + const localKv = new InMemoryKV(undefined); + await handleToolCall( + "memory_save", + { content: "alpha widget note", project: "proj-alpha" }, + localKv, + ); + await handleToolCall( + "memory_save", + { content: "beta widget note", project: "proj-beta" }, + localKv, + ); + + const scopedRecall = await handleToolCall( + "memory_recall", + { query: "widget", project: "proj-alpha" }, + localKv, + ); + const scopedResults = JSON.parse(scopedRecall.content[0].text).results; + expect(scopedResults).toHaveLength(1); + expect(scopedResults[0].content).toBe("alpha widget note"); + + const scopedSmartSearch = await handleToolCall( + "memory_smart_search", + { query: "widget", project: "proj-beta" }, + localKv, + ); + const scopedSmartResults = JSON.parse(scopedSmartSearch.content[0].text).results; + expect(scopedSmartResults).toHaveLength(1); + expect(scopedSmartResults[0].content).toBe("beta widget note"); + + const unscopedRecall = await handleToolCall( + "memory_recall", + { query: "widget" }, + localKv, + ); + expect(JSON.parse(unscopedRecall.content[0].text).results).toHaveLength(2); + }); + it("local fallback returns the same shape as proxy for memory_smart_search", async () => { installFetch(() => { throw new Error("ECONNREFUSED"); diff --git a/test/smart-search.test.ts b/test/smart-search.test.ts index 9d0c94e0c0..66b60310a5 100644 --- a/test/smart-search.test.ts +++ b/test/smart-search.test.ts @@ -291,4 +291,89 @@ describe("Smart Search Function", () => { expect(result.lessons).toEqual([]); }); }); + + describe("project filter on hybrid results (#787 follow-up)", () => { + it("filters observations by session project, keeping only the matching project", async () => { + const result = (await sdk.trigger("mem::smart-search", { + query: "auth", + project: "my-project", + })) as { results: CompactSearchResult[] }; + + // Both obs_1 and obs_2 belong to ses_1, whose project is "my-project". + expect(result.results.length).toBe(2); + }); + + it("excludes observations whose session belongs to a different project", async () => { + const result = (await sdk.trigger("mem::smart-search", { + query: "auth", + project: "someone-elses-project", + })) as { results: CompactSearchResult[] }; + + expect(result.results).toEqual([]); + }); + + it("resolves project via KV.memories for synthetic memory sessions (mem::remember entries)", async () => { + const memObs = makeObs({ + id: "mem_1", + sessionId: "memory", + title: "Saved insight", + }); + const memResult: HybridSearchResult = { + observation: memObs, + bm25Score: 0.5, + vectorScore: 0, + combinedScore: 0.5, + sessionId: "memory", + }; + await kv.set("mem:memories", "mem_1", { + id: "mem_1", + project: "proj-alpha", + }); + + const localSdk = mockSdk(); + const localSearchFn = async () => [memResult]; + registerSmartSearchFunction(localSdk as never, kv as never, localSearchFn); + + const matching = (await localSdk.trigger("mem::smart-search", { + query: "insight", + project: "proj-alpha", + })) as { results: CompactSearchResult[] }; + expect(matching.results.length).toBe(1); + + const mismatched = (await localSdk.trigger("mem::smart-search", { + query: "insight", + project: "proj-beta", + })) as { results: CompactSearchResult[] }; + expect(mismatched.results).toEqual([]); + }); + + it("passes through results whose project can't be resolved (no session, no memory record)", async () => { + const orphanObs = makeObs({ id: "obs_orphan", sessionId: "ses_gone" }); + const orphanResult: HybridSearchResult = { + observation: orphanObs, + bm25Score: 0.4, + vectorScore: 0, + combinedScore: 0.4, + sessionId: "ses_gone", + }; + + const localSdk = mockSdk(); + const localSearchFn = async () => [orphanResult]; + registerSmartSearchFunction(localSdk as never, kv as never, localSearchFn); + + const result = (await localSdk.trigger("mem::smart-search", { + query: "auth", + project: "any-project", + })) as { results: CompactSearchResult[] }; + expect(result.results.length).toBe(1); + }); + + it("does not filter results when no project is given", async () => { + const result = (await sdk.trigger("mem::smart-search", { + query: "auth", + })) as { results: CompactSearchResult[] }; + + expect(result.results.length).toBe(2); + }); + }); }); From 4a3d54212ac451d276459914ff26125990bd990d Mon Sep 17 00:00:00 2001 From: sk91 Date: Thu, 13 Aug 2026 21:15:41 +0300 Subject: [PATCH 3/3] fix(mcp): project-scope expandIds, parallelize KV lookups, fix test types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - projectFilter is now computed before the expandIds early-return branch and applied there too. Previously expandIds + project could return observations from other projects (the same class of leak #554 fixed for agentId, just for project). recallLessons() now gets the same normalized/trimmed projectFilter instead of raw data.project, so whitespace no longer causes hybrid-result and lesson filtering to disagree. - filterByProject is now generic (ProjectResolvable) so the same session/memory resolution logic covers both the hybrid-results path and the expandIds path, and resolves lookups via Promise.all with promise-cached session/memory lookups instead of a sequential await loop — a project-filtered search can over-fetch up to 300 hits, so serial resolution was an unnecessary latency hit. - Added graphScore to the HybridSearchResult test fixtures (required field, missed because test/ is excluded from tsconfig.json so tsc didn't catch it). Addresses CodeRabbit's second review pass on PR #1195. --- src/functions/smart-search.ts | 92 ++++++++++++++++++++++------------- test/smart-search.test.ts | 16 ++++++ 2 files changed, 74 insertions(+), 34 deletions(-) diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index fdf5c209e3..8327668585 100644 --- a/src/functions/smart-search.ts +++ b/src/functions/smart-search.ts @@ -131,6 +131,16 @@ export function registerSmartSearchFunction( ); } + // #787 follow-up: computed once, up front — same reasoning as + // filterAgentId above. The expandIds branch used to skip this + // entirely, letting a caller pass expandIds + project and get + // back observations from every project (#554-style leak, but + // for project instead of agent). + const projectFilter = + typeof data.project === "string" && data.project.trim().length > 0 + ? data.project.trim() + : undefined; + if (data.expandIds && data.expandIds.length > 0) { const raw = data.expandIds.slice(0, 20); const items = raw.map((entry) => { @@ -158,9 +168,12 @@ export function registerSmartSearchFunction( if (r) expanded.push(r); } - const scoped = filterAgentId + const agentScoped = filterAgentId ? expanded.filter((e) => e.observation.agentId === filterAgentId) : expanded; + const scoped = projectFilter + ? await filterByProject(kv, agentScoped, projectFilter) + : agentScoped; void recordAccessBatch( kv, @@ -188,11 +201,6 @@ export function registerSmartSearchFunction( const lessonLimit = Math.min(limit, 10); const includeLessons = data.includeLessons !== false; - const projectFilter = - typeof data.project === "string" && data.project.trim().length > 0 - ? data.project.trim() - : undefined; - // Over-fetch when filtering. Hybrid search can't filter on // agentId or project (BM25/vector indexes don't carry them), so // we ask the searcher for more hits than we need and trim @@ -207,7 +215,7 @@ export function registerSmartSearchFunction( const [hybridResults, lessons] = await Promise.all([ searchFn(data.query, overFetchLimit), includeLessons - ? recallLessons(sdk, data.query, lessonLimit, data.project) + ? recallLessons(sdk, data.query, lessonLimit, projectFilter) : Promise.resolve([]), ]); @@ -307,6 +315,13 @@ export function registerSmartSearchFunction( ); } +// Shape both HybridSearchResult and the expandIds branch's expanded +// entries satisfy — enough to resolve a project for either. +interface ProjectResolvable { + sessionId: string; + observation: { id: string }; +} + // Resolves each hit's project the same way mem::search does (see // search.ts's loadSession/loadMemoryProject): via its session's // `project` field, falling back to a KV.memories probe for synthetic @@ -314,39 +329,48 @@ export function registerSmartSearchFunction( // sessionId with no KV.sessions row). A result whose project can't be // resolved is treated as unscoped and passes through — we never want // an indexing gap to silently exclude an otherwise-matching result. -async function filterByProject( +// +// Lookups are cached by promise (not by resolved value) and resolved +// via Promise.all so concurrent hits sharing a sessionId/obsId only +// hit the KV once, and independent lookups run in parallel instead of +// serially — a project-filtered search can over-fetch up to 300 hits. +async function filterByProject( kv: StateKV, - results: HybridSearchResult[], + results: T[], projectFilter: string, -): Promise { - const sessionCache = new Map(); - const loadSession = async (sessionId: string): Promise => { - if (sessionCache.has(sessionId)) return sessionCache.get(sessionId)!; - const s = await kv.get(KV.sessions, sessionId); - sessionCache.set(sessionId, s ?? null); - return s ?? null; +): Promise { + const sessionCache = new Map>(); + const loadSession = (sessionId: string): Promise => { + let cached = sessionCache.get(sessionId); + if (!cached) { + cached = kv.get(KV.sessions, sessionId).then((s) => s ?? null); + sessionCache.set(sessionId, cached); + } + return cached; }; - const memoryProjectCache = new Map(); - const loadMemoryProject = async (obsId: string): Promise => { - if (memoryProjectCache.has(obsId)) return memoryProjectCache.get(obsId)!; - const mem = await kv.get(KV.memories, obsId).catch(() => null); - const proj = mem?.project ?? null; - memoryProjectCache.set(obsId, proj); - return proj; + const memoryProjectCache = new Map>(); + const loadMemoryProject = (obsId: string): Promise => { + let cached = memoryProjectCache.get(obsId); + if (!cached) { + cached = kv + .get(KV.memories, obsId) + .then((mem) => mem?.project ?? null) + .catch(() => null); + memoryProjectCache.set(obsId, cached); + } + return cached; }; - const kept: HybridSearchResult[] = []; - for (const r of results) { - const session = await loadSession(r.sessionId); - if (session) { - if (session.project === projectFilter) kept.push(r); - continue; - } - const memProject = await loadMemoryProject(r.observation.id); - if (memProject === null || memProject === projectFilter) kept.push(r); - } - return kept; + const matches = await Promise.all( + results.map(async (r) => { + const session = await loadSession(r.sessionId); + if (session) return session.project === projectFilter; + const memProject = await loadMemoryProject(r.observation.id); + return memProject === null || memProject === projectFilter; + }), + ); + return results.filter((_, i) => matches[i]); } async function recallLessons( diff --git a/test/smart-search.test.ts b/test/smart-search.test.ts index 66b60310a5..21d89b4a28 100644 --- a/test/smart-search.test.ts +++ b/test/smart-search.test.ts @@ -322,6 +322,7 @@ describe("Smart Search Function", () => { observation: memObs, bm25Score: 0.5, vectorScore: 0, + graphScore: 0, combinedScore: 0.5, sessionId: "memory", }; @@ -353,6 +354,7 @@ describe("Smart Search Function", () => { observation: orphanObs, bm25Score: 0.4, vectorScore: 0, + graphScore: 0, combinedScore: 0.4, sessionId: "ses_gone", }; @@ -375,5 +377,19 @@ describe("Smart Search Function", () => { expect(result.results.length).toBe(2); }); + + it("also scopes the expandIds branch — a caller can't bypass project scoping by expanding directly", async () => { + const matching = (await sdk.trigger("mem::smart-search", { + expandIds: ["obs_1"], + project: "my-project", + })) as { mode: string; results: unknown[] }; + expect(matching.results.length).toBe(1); + + const mismatched = (await sdk.trigger("mem::smart-search", { + expandIds: ["obs_1"], + project: "someone-elses-project", + })) as { mode: string; results: unknown[] }; + expect(mismatched.results).toEqual([]); + }); }); });