#1 Persistent memory for AI coding agents based on real-world benchmarks
TypeScript agentmemoryagentsaiclaudeclaudecodecodexcopilotcursorgenaiharnesshermesmemoryompopenclawopencodepi
Bound smart-search project validation and surface lookup failures (#1517) (#1518)
Rohit Ghumare9caace69c562
diff --git a/src/functions/smart-search.ts b/src/functions/smart-search.tsindex 65d9b3e2..a442363b 100644--- a/src/functions/smart-search.ts+++ b/src/functions/smart-search.ts@@ -122,6 +122,8 @@ export function registerSmartSearchFunction();}+ const matchesProject = project ? makeProjectMatcher(kv, project) : undefined;+if (data.expandIds && data.expandIds.length > 0) {const raw = data.expandIds.slice(0, 20);const items = raw.map((entry) => {@@ -149,21 +151,9 @@ export function registerSmartSearchFunction(if (r) expanded.push(r);}- const projectMatches = project- ? await Promise.all(- expanded.map((entry) =>- observationMatchesProject(- kv,- entry.obsId,- entry.sessionId,- project,- ),- ),- )- : expanded.map(() => true);- const projectScoped = expanded.filter(- (_, index) => projectMatches[index],- );+ const projectScoped = matchesProject+ ? await filterProjectResults(expanded, (entry) => matchesProject(entry.obsId, entry.sessionId))+ : expanded;const scoped = filterAgentId? projectScoped.filter((e) => e.observation.agentId === filterAgentId): projectScoped;@@ -193,12 +183,6 @@ export function registerSmartSearchFunction(// observations so 10 covers most recall flows.const lessonLimit = Math.min(limit, 10);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 || project? Math.max(Math.min(limit * 10, 300), 100): limit;@@ -206,24 +190,13 @@ 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, project): Promise.resolve([]),]);- let projectResults = hybridResults;- if (project) {- const matches = await Promise.all(- hybridResults.map((result) =>- observationMatchesProject(- kv,- result.observation.id,- result.sessionId,- project,- ),- ),- );- projectResults = hybridResults.filter((_, index) => matches[index]);- }+ const projectResults = matchesProject+ ? await filterProjectResults(hybridResults, (result) => matchesProject(result.observation.id, result.sessionId))+ : hybridResults;const filteredHybrid = filterAgentId? projectResults@@ -417,16 +390,32 @@ async function findObservation(return null;}-async function observationMatchesProject(- kv: StateKV,- obsId: string,- sessionId: string,- project: string,-): Promise<boolean> {- const memory = await kv.get<Memory>(KV.memories, obsId).catch(() => null);- if (memory?.project !== undefined) return memory.project === project;- const session = await kv- .get<{ project?: string }>(KV.sessions, sessionId)- .catch(() => null);- return session?.project === project;+function makeProjectMatcher(kv: StateKV, project: string): (obsId: string, sessionId: string) => Promise<boolean> {+ const memories = new Map<string, Promise<Memory | null>>();+ const sessions = new Map<string, Promise<{ project?: string } | null>>();+ return async (obsId, sessionId) => {+ let memoryRead = memories.get(obsId);+ if (!memoryRead) {+ memoryRead = kv.get<Memory>(KV.memories, obsId);+ memories.set(obsId, memoryRead);+ }+ const memory = await memoryRead;+ if (memory?.project !== undefined) return memory.project === project;+ let sessionRead = sessions.get(sessionId);+ if (!sessionRead) {+ sessionRead = kv.get<{ project?: string }>(KV.sessions, sessionId);+ sessions.set(sessionId, sessionRead);+ }+ return (await sessionRead)?.project === project;+ };+}++async function filterProjectResults<T>(items: T[], matches: (item: T) => Promise<boolean>): Promise<T[]> {+ const scoped: T[] = [];+ for (let offset = 0; offset < items.length; offset += 10) {+ const batch = items.slice(offset, offset + 10);+ const selected = await Promise.all(batch.map(matches));+ scoped.push(...batch.filter((_, index) => selected[index]));+ }+ return scoped;}diff --git a/test/smart-search-project-validation.test.ts b/test/smart-search-project-validation.test.tsnew file mode 100644index 00000000..f1ee4dbf--- /dev/null+++ b/test/smart-search-project-validation.test.ts@@ -0,0 +1,75 @@+import { describe, it, expect, vi } from "vitest";+import { registerSmartSearchFunction } from "../src/functions/smart-search.js";+import type { CompressedObservation, HybridSearchResult } from "../src/types.js";++vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } }));++function fixture(count: number, sharedSession: boolean) {+ const records = new Map<string, unknown>();+ const get = vi.fn(async (scope: string, key: string) => records.get(scope + "/" + key) ?? null);+ const kv = { get, set: vi.fn(async () => {}), list: vi.fn(async () => []) };+ const hits: HybridSearchResult[] = Array.from({ length: count }, (_, i) => {+ const sessionId = sharedSession ? "shared" : "session-" + i;+ const observation: CompressedObservation = { id: "obs-" + i, sessionId, timestamp: "2026-01-01T00:00:00Z",+ type: "discovery", title: "policy", narrative: "policy", facts: [], concepts: [], files: [], importance: 5 };+ records.set("mem:sessions/" + sessionId, { project: "orbit" });+ return { observation, sessionId, bm25Score: 1, vectorScore: 0, combinedScore: 1 };+ });+ let handle!: (data: Record<string, unknown>) => Promise<{ results: Array<{ obsId: string }> }>;+ const trigger = vi.fn(async () => ({ success: true, lessons: [] }));+ const sdk = { registerFunction: (_name: string, fn: typeof handle) => { handle = fn; }, trigger };+ registerSmartSearchFunction(sdk as never, kv as never, async () => hits);+ return { get, records, trigger, query: (data: Record<string, unknown>) => handle(data) };+}++describe("smart search project validation", () => {+ it("bounds concurrent state lookups for a large scoped result page", async () => {+ const f = fixture(60, false);+ const original = f.get.getMockImplementation()!;+ let active = 0;+ let maximum = 0;+ f.get.mockImplementation(async (scope, key) => {+ if (scope !== "mem:sessions" && scope !== "mem:memories") return original(scope, key);+ active++;+ maximum = Math.max(maximum, active);+ await new Promise<void>((resolve) => setImmediate(resolve));+ const result = await original(scope, key);+ active--;+ return result;+ });+ const result = await f.query({ query: "policy", project: "orbit", limit: 100, includeLessons: false });+ expect(result.results).toHaveLength(60);+ expect(maximum).toBeLessThanOrEqual(10);+ });++ it("shares in-flight project reads for results in the same session", async () => {+ const f = fixture(30, true);+ const result = await f.query({ query: "policy", project: "orbit", limit: 100, includeLessons: false });+ expect(result.results).toHaveLength(30);+ expect(f.get.mock.calls.filter(([scope, key]) => scope === "mem:sessions" && key === "shared")).toHaveLength(1);+ });++ it.each(["mem:sessions", "mem:memories"])("reports %s lookup failure instead of passing or dropping rows silently", async (failedScope) => {+ const f = fixture(1, true);+ const original = f.get.getMockImplementation()!;+ f.get.mockImplementation(async (scope, key) => {+ if (scope === failedScope) throw new Error("state unavailable");+ return original(scope, key);+ });+ await expect(f.query({ query: "policy", project: "orbit", includeLessons: false })).rejects.toThrow("state unavailable");+ });++ it("uses the same normalized project for memories and lessons", async () => {+ const f = fixture(1, true);+ const result = await f.query({ query: "policy", project: " orbit " });+ expect(result.results).toHaveLength(1);+ expect(f.trigger).toHaveBeenCalledWith({ function_id: "mem::lesson-recall", payload: { query: "policy", project: "orbit", limit: 10 } });+ });++ it("excludes unscoped evidence from an explicit project but preserves unfiltered recall", async () => {+ const f = fixture(1, true);+ f.records.clear();+ expect((await f.query({ query: "policy", project: "orbit", includeLessons: false })).results).toEqual([]);+ expect((await f.query({ query: "policy", includeLessons: false })).results).toHaveLength(1);+ });+});