diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index 5d0147d9e0..8327668585 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"; @@ -129,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) => { @@ -156,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, @@ -187,27 +202,40 @@ export function registerSmartSearchFunction( const includeLessons = data.includeLessons !== false; // 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), includeLessons - ? recallLessons(sdk, data.query, lessonLimit, data.project) + ? recallLessons(sdk, data.query, lessonLimit, projectFilter) : 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 +315,64 @@ 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 +// 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. +// +// 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: T[], + projectFilter: string, +): 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 = (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 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( sdk: ISdk, query: string, diff --git a/src/mcp/standalone.ts b/src/mcp/standalone.ts index f425b159c4..e6371d487a 100644 --- a/src/mcp/standalone.ts +++ b/src/mcp/standalone.ts @@ -160,6 +160,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": { @@ -190,16 +194,17 @@ 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; + if (v.agentId != null) body["agentId"] = v.agentId; const result = await handle.call("/agentmemory/remember", { method: "POST", - body: JSON.stringify({ - content: v.content, - type: v.type, - concepts: v.concepts, - files: v.files, - ...(v.project !== undefined && { project: v.project }), - ...(v.agentId !== undefined && { agentId: v.agentId }), - }), + body: JSON.stringify(body), }); return textResponse(result); } @@ -210,6 +215,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), @@ -220,6 +226,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), @@ -271,6 +278,7 @@ async function handleLocal( content: v.content, concepts: v.concepts, files: v.files, + project: v.project, createdAt: isoNow, updatedAt: isoNow, strength: 7, @@ -289,6 +297,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/src/mcp/tools-registry.ts b/src/mcp/tools-registry.ts index 1225b4ce7d..52fca02cd8 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"], }, @@ -136,6 +142,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..54425de84a 100644 --- a/test/mcp-standalone-proxy.test.ts +++ b/test/mcp-standalone-proxy.test.ts @@ -130,6 +130,88 @@ 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("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) => { + 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("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) => { @@ -167,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..21d89b4a28 100644 --- a/test/smart-search.test.ts +++ b/test/smart-search.test.ts @@ -291,4 +291,105 @@ 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, + graphScore: 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, + graphScore: 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); + }); + + 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([]); + }); + }); });