Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 101 additions & 15 deletions src/functions/smart-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<T extends ProjectResolvable>(
kv: StateKV,
results: T[],
projectFilter: string,
): Promise<T[]> {
const sessionCache = new Map<string, Promise<Session | null>>();
const loadSession = (sessionId: string): Promise<Session | null> => {
let cached = sessionCache.get(sessionId);
if (!cached) {
cached = kv.get<Session>(KV.sessions, sessionId).then((s) => s ?? null);
sessionCache.set(sessionId, cached);
}
return cached;
};

const memoryProjectCache = new Map<string, Promise<string | null>>();
const loadMemoryProject = (obsId: string): Promise<string | null> => {
let cached = memoryProjectCache.get(obsId);
if (!cached) {
cached = kv
.get<Memory>(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,
Expand Down
31 changes: 23 additions & 8 deletions src/mcp/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ function validate(toolName: string, args: Record<string, unknown>): 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": {
Expand Down Expand Up @@ -190,16 +194,17 @@ async function handleProxy(
): Promise<{ content: Array<{ type: string; text: string }> }> {
switch (v.tool) {
case "memory_save": {
const body: Record<string, unknown> = {
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);
}
Expand All @@ -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),
Expand All @@ -220,6 +226,7 @@ async function handleProxy(
const body: Record<string, unknown> = { 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),
Expand Down Expand Up @@ -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,
Expand All @@ -289,6 +297,13 @@ async function handleLocal(
const all =
await kvInstance.list<Record<string, unknown>>("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"] : "",
Expand Down
12 changes: 12 additions & 0 deletions src/mcp/tools-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
Expand Down Expand Up @@ -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).",
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
},
required: ["query"],
},
Expand Down
124 changes: 124 additions & 0 deletions test/mcp-standalone-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> | 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<string, unknown> | 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<string, unknown> | 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<string, unknown> | 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<string, unknown> | 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) => {
Expand Down Expand Up @@ -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");
Expand Down
Loading