diff --git a/CLAUDE.md b/CLAUDE.md index da792d348c..c8299e66df 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -13,7 +13,9 @@ possible to be able to do that. Every feature here is judged against that senten - **Right scope** — results are bounded by project and Session (`scope: "project" | "global"`, `src/types.ts:309`). Branch is not a bound: every branch and worktree of a repo shares one project (`test/branch-recall.test.ts`). One repo's work - never surfaces in another's. + never surfaces in another's. Project-scoped Recall excludes a non-Global Memory with no + resolvable project, checking its own project before its Session and Summary; unscoped Recall + still returns it. An Observation with no resolvable project still passes the project filter. - **Least record that restates** — store the smallest durable claim that reconstructs a decision later: the conclusion and why, not the transcript that produced it. Observations are raw and cheap; a Memory earns its place by being worth re-reading. diff --git a/src/functions/search.ts b/src/functions/search.ts index d7f53dcc46..0620f5360b 100644 --- a/src/functions/search.ts +++ b/src/functions/search.ts @@ -559,18 +559,8 @@ export function createSessionLoader( } } -// A Global Memory matches every project, ahead of its session's project. -// Otherwise a result's project is its session's. Results with no session entry fall -// back to KV.memories, then to the Session Summary, and pass through when -// neither has a project. Two cases arrive without a session: -// 1. Synthetic sessionId: memories indexed via mem::remember use -// sessionIds[0] ?? 'memory'. The string 'memory' has no session entry; -// neither does a real sessionId from a different lifecycle. -// 2. Evicted session: eviction deletes the session record and keeps its -// observations indexed, but its Session Summary still names the -// project. Only an observation with neither passes through unscoped: -// we lose the ability to filter but never block a result we can no -// longer verify. +// An unscoped Observation can outlive its Session and Summary through eviction; +// retain it even when its project can no longer be verified. export function createProjectMatcher( kv: StateKV, project: string, @@ -585,17 +575,24 @@ export function createProjectMatcher( return memories.get(obsId)! } return async (sessionId, obsId) => { - if (obsId.startsWith('mem_') && (await loadMemory(obsId))?.global === true) return true + const isMemory = obsId.startsWith('mem_') + if (isMemory) { + const memory = await loadMemory(obsId) + if (memory?.global === true) return true + if (memory?.project) return memory.project === project + } const session = await loadSession(sessionId) if (session) return session.project === project - const memProject = (await loadMemory(obsId))?.project ?? null - if (memProject !== null) return memProject === project + if (!isMemory) { + const memProject = (await loadMemory(obsId))?.project ?? null + if (memProject !== null) return memProject === project + } if (!summaryProjects.has(sessionId)) { const summary = await kv.get(KV.summaries, sessionId).catch(() => null) summaryProjects.set(sessionId, summary?.project || null) } const summaryProject = summaryProjects.get(sessionId)! - return summaryProject === null || summaryProject === project + return summaryProject === project || (summaryProject === null && !isMemory) } } diff --git a/test/project-matcher-unscoped-memory.test.ts b/test/project-matcher-unscoped-memory.test.ts new file mode 100644 index 0000000000..b135f1db9f --- /dev/null +++ b/test/project-matcher-unscoped-memory.test.ts @@ -0,0 +1,81 @@ +import { describe, it, expect, vi } from "vitest"; + +vi.mock("../src/logger.js", () => ({ + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, +})); + +import { createProjectMatcher } from "../src/functions/search.js"; +import { KV } from "../src/state/schema.js"; +import type { StateKV } from "../src/state/kv.js"; + +function makeKV(seed: Record>, failScopes: string[] = []) { + const reads: string[] = []; + const kv = { + get: async (scope: string, key: string) => { + reads.push(scope); + if (failScopes.includes(scope)) throw new Error("read failed"); + return seed[scope]?.[key] ?? null; + }, + } as unknown as StateKV; + return { kv, reads }; +} + +describe("createProjectMatcher with a Memory", () => { + it("excludes a non-Global Memory with no project, Session or Summary", async () => { + const { kv } = makeKV({ [KV.memories]: { mem_a: { id: "mem_a" } } }); + expect(await createProjectMatcher(kv, "alpha")("memory", "mem_a")).toBe(false); + }); + + it("still passes an Observation with no project, Session or Summary", async () => { + const { kv } = makeKV({}); + expect(await createProjectMatcher(kv, "alpha")("gone", "obs_a")).toBe(true); + }); + + it("passes a Global Memory", async () => { + const { kv } = makeKV({ [KV.memories]: { mem_g: { id: "mem_g", global: true } } }); + expect(await createProjectMatcher(kv, "alpha")("memory", "mem_g")).toBe(true); + }); + + it("matches an explicit project without reading a Session", async () => { + const { kv, reads } = makeKV({ + [KV.memories]: { mem_p: { id: "mem_p", project: "alpha" } }, + [KV.sessions]: { memory: { id: "memory", project: "beta" } }, + }); + expect(await createProjectMatcher(kv, "alpha")("memory", "mem_p")).toBe(true); + expect(reads).not.toContain(KV.sessions); + }); + + it("does not fail when the Session read fails and the Memory names its project", async () => { + const { kv } = makeKV( + { [KV.memories]: { mem_p: { id: "mem_p", project: "alpha" } } }, + [KV.sessions], + ); + expect(await createProjectMatcher(kv, "alpha")("s1", "mem_p")).toBe(true); + }); + + it("retains Session read failures for Observations", async () => { + const { kv } = makeKV({}, [KV.sessions]); + await expect(createProjectMatcher(kv, "alpha")("s1", "obs_a")).rejects.toThrow("read failed"); + }); + + it("uses a Memory's Session or Summary when its project is absent", async () => { + const { kv } = makeKV({ + [KV.memories]: { mem_a: { id: "mem_a" } }, + [KV.sessions]: { s1: { project: "beta" } }, + [KV.summaries]: { s2: { project: "alpha" }, s3: { project: "beta" } }, + }); + const matches = createProjectMatcher(kv, "alpha"); + expect(await matches("s1", "mem_a")).toBe(false); + expect(await matches("s2", "mem_a")).toBe(true); + expect(await matches("s3", "mem_a")).toBe(false); + }); + + it("rejects an explicit other project even when the Session matches", async () => { + const { kv, reads } = makeKV({ + [KV.memories]: { mem_b: { project: "beta" } }, + [KV.sessions]: { s1: { project: "alpha" } }, + }); + expect(await createProjectMatcher(kv, "alpha")("s1", "mem_b")).toBe(false); + expect(reads).not.toContain(KV.sessions); + }); +});