From b8a47a817406415fd496f66b8a6041980c442135 Mon Sep 17 00:00:00 2001 From: Joseph Faye Date: Tue, 9 Jun 2026 12:28:27 +0200 Subject: [PATCH 1/3] fix(smart-search): apply the project filter to results, not just lessons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mem::smart-search accepts a `project` argument but only forwards it to mem::lesson-recall — the primary observation results are filtered by agentId alone. A project-scoped search therefore returns observations from every other project, so callers cannot use `project` to isolate memory per concern. Apply the project filter wherever the agentId filter already runs: the expand-ids branch, the hybrid-results branch, and the over-fetch trigger (the BM25/vector index carries neither agentId nor project, so both need post-filter headroom). When `project` is omitted behaviour is unchanged. Adds two tests: a project-scoped search returns only the matching project's observations, and an unscoped search still returns all. Signed-off-by: Joseph Faye --- src/functions/smart-search.ts | 31 +++++++++--------- test/smart-search.test.ts | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index 5d0147d9e0..f2d9ab1fc7 100644 --- a/src/functions/smart-search.ts +++ b/src/functions/smart-search.ts @@ -115,6 +115,10 @@ export function registerSmartSearchFunction( const filterAgentId = wildcardAgent ? undefined : explicitAgentId ?? envAgentId; + const filterProject = + typeof data.project === "string" && data.project.trim().length > 0 + ? data.project.trim() + : undefined; if ( isolated && !wildcardAgent && @@ -156,9 +160,9 @@ export function registerSmartSearchFunction( if (r) expanded.push(r); } - const scoped = filterAgentId - ? expanded.filter((e) => e.observation.agentId === filterAgentId) - : expanded; + const scoped = expanded + .filter((e) => !filterAgentId || e.observation.agentId === filterAgentId) + .filter((e) => !filterProject || e.observation.project === filterProject); void recordAccessBatch( kv, @@ -187,12 +191,12 @@ 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 + // agentId or project (BM25/vector indexes carry neither), 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 || filterProject ? Math.min(limit * 3, 300) : limit; @@ -203,11 +207,10 @@ export function registerSmartSearchFunction( : Promise.resolve([]), ]); - const filteredHybrid = filterAgentId - ? hybridResults - .filter((r) => r.observation.agentId === filterAgentId) - .slice(0, limit) - : hybridResults.slice(0, limit); + const filteredHybrid = hybridResults + .filter((r) => !filterAgentId || r.observation.agentId === filterAgentId) + .filter((r) => !filterProject || r.observation.project === filterProject) + .slice(0, limit); const compact: CompactSearchResult[] = filteredHybrid.map((r) => ({ obsId: r.observation.id, diff --git a/test/smart-search.test.ts b/test/smart-search.test.ts index 9d0c94e0c0..cb49547eae 100644 --- a/test/smart-search.test.ts +++ b/test/smart-search.test.ts @@ -291,4 +291,63 @@ describe("Smart Search Function", () => { expect(result.lessons).toEqual([]); }); }); + + describe("project scoping", () => { + function seedTwoProjects() { + const builderObs = makeObs({ + id: "obs_builder", + sessionId: "ses_1", + title: "Planner model choice", + project: "builder", + }); + const reviewObs = makeObs({ + id: "obs_review", + sessionId: "ses_1", + title: "Reviewer escalation", + project: "review", + }); + searchResults = [ + { + observation: builderObs, + bm25Score: 0.8, + vectorScore: 0, + combinedScore: 0.8, + sessionId: "ses_1", + }, + { + observation: reviewObs, + bm25Score: 0.7, + vectorScore: 0, + combinedScore: 0.7, + sessionId: "ses_1", + }, + ]; + } + + it("compact mode returns only observations from the requested project", async () => { + seedTwoProjects(); + + const result = (await sdk.trigger("mem::smart-search", { + query: "model", + project: "builder", + includeLessons: false, + })) as { results: CompactSearchResult[] }; + + expect(result.results.map((r) => r.obsId)).toEqual(["obs_builder"]); + }); + + it("omitting project returns observations from every project", async () => { + seedTwoProjects(); + + const result = (await sdk.trigger("mem::smart-search", { + query: "model", + includeLessons: false, + })) as { results: CompactSearchResult[] }; + + expect(result.results.map((r) => r.obsId).sort()).toEqual([ + "obs_builder", + "obs_review", + ]); + }); + }); }); From 417fb7397b50c5eb5ff28a0af793f28fb45bb5d0 Mon Sep 17 00:00:00 2001 From: Joseph Faye Date: Tue, 9 Jun 2026 15:25:31 +0200 Subject: [PATCH 2/3] fix(smart-search): resolve project per hit; normalize lesson-recall scope MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first pass filtered on r.observation.project, but the observation returned by the hybrid index does not carry the resolved project. Live testing showed a project-scoped search dropping ALL results — including those of the requested project. Resolve project the same way mem::search does: the session's project when the session exists, otherwise the memory's own KV.memories record (entries indexed via mem::remember use a synthetic sessionId with no session row). A null result means the project is unknown and the hit passes through (matches search.ts, backward compatible). Both lookups are cached. Tests now seed KV.memories and use the synthetic 'memory' sessionId so they exercise the real resolver path instead of a synthetic observation that already carries project. Addresses the CodeRabbit review: pass the normalized filterProject (not raw data.project) to recallLessons so observation filtering and lesson scoping agree on whitespace input. Signed-off-by: Joseph Faye --- src/functions/smart-search.ts | 64 ++++++++++++++++++++++++++++++----- test/smart-search.test.ts | 27 ++++++++++----- 2 files changed, 75 insertions(+), 16 deletions(-) diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index f2d9ab1fc7..60ff7d29c8 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"; @@ -72,6 +74,32 @@ export function resetFollowupStatsForTests(): void { // full content is fetched via memory_lesson_recall when the caller needs it. const LESSON_CONTENT_PREVIEW_CHARS = 240; +// Resolve the project a hit belongs to, the same way mem::search does: +// the session's project when the session exists, otherwise the memory's +// own project. Entries indexed via mem::remember use a synthetic +// sessionId ('memory') with no KV.sessions row, so their project lives +// only on the KV.memories record. Returns null when the project is +// unknown (no session, no memory record); callers treat null as +// "unscoped — let it through" to stay backward-compatible, matching +// search.ts. Both lookups are cached, so a filtered page costs at most +// one read per distinct session and per distinct unbound observation. +function makeProjectResolver(kv: StateKV) { + const sessionCache = new Map(); + const memoryProjectCache = new Map(); + return async (sessionId: string, obsId: string): Promise => { + if (!sessionCache.has(sessionId)) { + sessionCache.set(sessionId, (await kv.get(KV.sessions, sessionId)) ?? null); + } + const session = sessionCache.get(sessionId)!; + if (session) return session.project; + if (!memoryProjectCache.has(obsId)) { + const memory = await kv.get(KV.memories, obsId).catch(() => null); + memoryProjectCache.set(obsId, memory?.project ?? null); + } + return memoryProjectCache.get(obsId)!; + }; +} + export function registerSmartSearchFunction( sdk: ISdk, kv: StateKV, @@ -160,9 +188,19 @@ export function registerSmartSearchFunction( if (r) expanded.push(r); } - const scoped = expanded - .filter((e) => !filterAgentId || e.observation.agentId === filterAgentId) - .filter((e) => !filterProject || e.observation.project === filterProject); + const agentScoped = filterAgentId + ? expanded.filter((e) => e.observation.agentId === filterAgentId) + : expanded; + let scoped = agentScoped; + if (filterProject) { + const resolveProject = makeProjectResolver(kv); + const kept: typeof expanded = []; + for (const e of agentScoped) { + const project = await resolveProject(e.sessionId, e.observation.id); + if (project === null || project === filterProject) kept.push(e); + } + scoped = kept; + } void recordAccessBatch( kv, @@ -203,14 +241,24 @@ 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, filterProject) : Promise.resolve([]), ]); - const filteredHybrid = hybridResults - .filter((r) => !filterAgentId || r.observation.agentId === filterAgentId) - .filter((r) => !filterProject || r.observation.project === filterProject) - .slice(0, limit); + const agentFiltered = filterAgentId + ? hybridResults.filter((r) => r.observation.agentId === filterAgentId) + : hybridResults; + let scopedHybrid = agentFiltered; + if (filterProject) { + const resolveProject = makeProjectResolver(kv); + const kept: HybridSearchResult[] = []; + for (const r of agentFiltered) { + const project = await resolveProject(r.sessionId, r.observation.id); + if (project === null || project === filterProject) kept.push(r); + } + scopedHybrid = kept; + } + const filteredHybrid = scopedHybrid.slice(0, limit); const compact: CompactSearchResult[] = filteredHybrid.map((r) => ({ obsId: r.observation.id, diff --git a/test/smart-search.test.ts b/test/smart-search.test.ts index cb49547eae..68889db9dc 100644 --- a/test/smart-search.test.ts +++ b/test/smart-search.test.ts @@ -293,17 +293,28 @@ describe("Smart Search Function", () => { }); describe("project scoping", () => { - function seedTwoProjects() { + // Mirror what mem::remember produces: observations indexed under a + // synthetic sessionId ("memory") with no KV.sessions row, and the + // project carried on the KV.memories record. This exercises the real + // resolver path (session miss -> KV.memories lookup) rather than a + // synthetic observation that already carries `project`. + async function seedTwoProjects() { const builderObs = makeObs({ id: "obs_builder", - sessionId: "ses_1", + sessionId: "memory", title: "Planner model choice", - project: "builder", }); const reviewObs = makeObs({ id: "obs_review", - sessionId: "ses_1", + sessionId: "memory", title: "Reviewer escalation", + }); + await kv.set("mem:memories", "obs_builder", { + id: "obs_builder", + project: "builder", + }); + await kv.set("mem:memories", "obs_review", { + id: "obs_review", project: "review", }); searchResults = [ @@ -312,20 +323,20 @@ describe("Smart Search Function", () => { bm25Score: 0.8, vectorScore: 0, combinedScore: 0.8, - sessionId: "ses_1", + sessionId: "memory", }, { observation: reviewObs, bm25Score: 0.7, vectorScore: 0, combinedScore: 0.7, - sessionId: "ses_1", + sessionId: "memory", }, ]; } it("compact mode returns only observations from the requested project", async () => { - seedTwoProjects(); + await seedTwoProjects(); const result = (await sdk.trigger("mem::smart-search", { query: "model", @@ -337,7 +348,7 @@ describe("Smart Search Function", () => { }); it("omitting project returns observations from every project", async () => { - seedTwoProjects(); + await seedTwoProjects(); const result = (await sdk.trigger("mem::smart-search", { query: "model", From 7a9a4462375e0cf5c4cfaa2de58a315b9543767c Mon Sep 17 00:00:00 2001 From: Joseph Faye Date: Tue, 9 Jun 2026 15:45:38 +0200 Subject: [PATCH 3/3] fix(smart-search): guard session read, parallelize project resolution Address the CodeRabbit review on the project-resolution helper: - the session KV read now degrades to null on rejection (matching the memory lookup), so a transient state::get failure no longer fails the whole project-scoped search. - resolve each hit's project concurrently via Promise.all instead of an awaited serial loop, in both the expand-ids and hybrid branches; the lookups are independent and the resolver caches both reads. Signed-off-by: Joseph Faye --- src/functions/smart-search.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.ts index 60ff7d29c8..82f8d0037b 100644 --- a/src/functions/smart-search.ts +++ b/src/functions/smart-search.ts @@ -88,7 +88,8 @@ function makeProjectResolver(kv: StateKV) { const memoryProjectCache = new Map(); return async (sessionId: string, obsId: string): Promise => { if (!sessionCache.has(sessionId)) { - sessionCache.set(sessionId, (await kv.get(KV.sessions, sessionId)) ?? null); + const session = await kv.get(KV.sessions, sessionId).catch(() => null); + sessionCache.set(sessionId, session ?? null); } const session = sessionCache.get(sessionId)!; if (session) return session.project; @@ -194,12 +195,12 @@ export function registerSmartSearchFunction( let scoped = agentScoped; if (filterProject) { const resolveProject = makeProjectResolver(kv); - const kept: typeof expanded = []; - for (const e of agentScoped) { - const project = await resolveProject(e.sessionId, e.observation.id); - if (project === null || project === filterProject) kept.push(e); - } - scoped = kept; + const projects = await Promise.all( + agentScoped.map((e) => resolveProject(e.sessionId, e.observation.id)), + ); + scoped = agentScoped.filter( + (_e, i) => projects[i] === null || projects[i] === filterProject, + ); } void recordAccessBatch( @@ -251,12 +252,12 @@ export function registerSmartSearchFunction( let scopedHybrid = agentFiltered; if (filterProject) { const resolveProject = makeProjectResolver(kv); - const kept: HybridSearchResult[] = []; - for (const r of agentFiltered) { - const project = await resolveProject(r.sessionId, r.observation.id); - if (project === null || project === filterProject) kept.push(r); - } - scopedHybrid = kept; + const projects = await Promise.all( + agentFiltered.map((r) => resolveProject(r.sessionId, r.observation.id)), + ); + scopedHybrid = agentFiltered.filter( + (_r, i) => projects[i] === null || projects[i] === filterProject, + ); } const filteredHybrid = scopedHybrid.slice(0, limit);