From 0ee14fa8a7187d3a7210540d0be52c584017c89c Mon Sep 17 00:00:00 2001 From: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> Date: Fri, 13 Nov 2026 06:28:41 +0400 Subject: [PATCH 01/10] perf(api): count consolidation summaries without loading records Signed-off-by: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> --- src/triggers/api.ts | 13 +++- test/consolidation-summary-counts.test.ts | 91 +++++++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 test/consolidation-summary-counts.test.ts diff --git a/src/triggers/api.ts b/src/triggers/api.ts index 46e318b89d..701d9d49f8 100644 --- a/src/triggers/api.ts +++ b/src/triggers/api.ts @@ -480,17 +480,26 @@ export function createStatusReporter(sdk: IIIClient, kv: StateKV, deps: StatusRe }; } +async function countConsolidationSummaries(kv: StateKV): Promise { + if (typeof kv.listKeys === "function") { + try { + return (await kv.listKeys(KV.summaries)).length; + } catch {} + } + return (await kv.list(KV.summaries).catch(() => [])).length; +} + export function createConsolidationStatusReader(kv: StateKV) { const counts = singleFlight(async () => { const [summaries, memories, semantic, procedural, relations] = await Promise.all([ - kv.list(KV.summaries).catch(() => []), + countConsolidationSummaries(kv), collectAll(kv, KV.memories).catch(() => []), collectAll(kv, KV.semantic).catch(() => []), collectAll(kv, KV.procedural).catch(() => []), collectAll(kv, KV.relations).catch(() => []), ]); return { - summaries: summaries.length, + summaries, recurringPatterns: memories.filter( (m) => m.isLatest && diff --git a/test/consolidation-summary-counts.test.ts b/test/consolidation-summary-counts.test.ts new file mode 100644 index 0000000000..e745acefcd --- /dev/null +++ b/test/consolidation-summary-counts.test.ts @@ -0,0 +1,91 @@ +import { describe, expect, it, vi } from "vitest"; +import { createConsolidationStatusReader } from "../src/triggers/api.js"; +import { StateKV, type StateBackend } from "../src/state/kv.js"; +import { KV } from "../src/state/schema.js"; + +type Call = { function_id: string; payload: { scope: string; key?: string } }; +type Row = Record | null; + +function fixture(backend: StateBackend, options: { rejectSummaryKeys?: boolean; rejectSummaryValues?: boolean } = {}) { + const data = new Map>([ + [KV.summaries, new Map([["summary-a", { sessionId: "a", content: "a".repeat(32768) }], ["summary-b", { sessionId: "b", content: "b".repeat(32768) }], ["summary-null", null]])], + [KV.semantic, new Map([["semantic-a", { id: "a" }], ["semantic-b", { id: "b" }], ["semantic-null", null]])], + [KV.procedural, new Map([["procedure-a", { id: "a" }], ["procedure-null", null]])], + [KV.relations, new Map([["relation-a", { id: "a" }], ["relation-b", { id: "b" }], ["relation-c", { id: "c" }], ["relation-null", null]])], + [KV.memories, new Map([ + ["recurring-a", { id: "recurring-a", isLatest: true, type: "pattern", recurrence: 2 }], + ["recurring-b", { id: "recurring-b", isLatest: true, type: "pattern", sessionIds: ["s1", "s2"] }], + ["repeated-session", { id: "repeated-session", isLatest: true, type: "pattern", sessionIds: ["s1", "s1"] }], + ["stale", { id: "stale", isLatest: false, type: "pattern", recurrence: 5 }], + ["not-pattern", { id: "not-pattern", isLatest: true, type: "fact", recurrence: 5 }], + ["not-latest", { id: "not-latest", type: "pattern", recurrence: 5 }], + ["null", null], + ])], + ]); + const calls: Call[] = []; + const sdk = { + trigger: vi.fn(async (call: Call) => { + calls.push(structuredClone(call)); + const rows = data.get(call.payload.scope) ?? new Map(); + if (call.function_id === "state::list_keys") { + if (call.payload.scope === KV.summaries && options.rejectSummaryKeys) throw new Error("state::list_keys unavailable"); + const keys = [...rows.keys()]; + return backend === "redis" ? { keys } : keys; + } + if (call.function_id === "state::list") { + if (call.payload.scope === KV.summaries && options.rejectSummaryValues) throw new Error("summary values unavailable"); + return structuredClone([...rows.values()]); + } + if (call.function_id === "state::get") return structuredClone(rows.get(call.payload.key ?? "") ?? null); + throw new Error(`Unexpected fixture invocation: ${call.function_id}`); + }), + }; + return { kv: new StateKV(sdk as never, { backend }), data, calls }; +} + +function expectCounts(status: Awaited>>, summaries: number) { + expect(status.summaries).toBe(summaries); + expect(status.recurringPatterns).toBe(2); + expect(status.tiers.find((tier) => tier.id === "semantic")?.count).toBe(2); + expect(status.tiers.find((tier) => tier.id === "procedural")?.count).toBe(1); + expect(status.tiers.find((tier) => tier.id === "relations")?.count).toBe(3); +} + +describe("consolidation summary counts", () => { + it.each(["file", "redis"] as const)("counts summaries without transferring their record values on %s", async (backend) => { + const { kv, calls } = fixture(backend); + const status = await createConsolidationStatusReader(kv)({ lastRun: null }); + expectCounts(status, 3); + expect(calls.filter((call) => call.payload.scope === KV.summaries && ["state::list", "state::get"].includes(call.function_id))).toEqual([]); + }); + + it.each(["file", "redis"] as const)("preserves nullable rows, real pattern filtering and count reuse on %s", async (backend) => { + const { kv, data, calls } = fixture(backend); + const read = createConsolidationStatusReader(kv); + expectCounts(await read({ lastRun: null }), 3); + const before = calls.length; + data.get(KV.summaries)!.set("later", { sessionId: "later" }); + expectCounts(await read({ lastRun: null }), 3); + expect(calls).toHaveLength(before); + expect(calls.some((call) => call.payload.scope === KV.memories && call.function_id === "state::get")).toBe(true); + }); + + it.each(["file", "redis"] as const)("keeps the working value-list fallback when summary key listing rejects on %s", async (backend) => { + const { kv, calls } = fixture(backend, { rejectSummaryKeys: true }); + expectCounts(await createConsolidationStatusReader(kv)({ lastRun: null }), 3); + expect(calls.filter((call) => call.payload.scope === KV.summaries && call.function_id === "state::list")).toHaveLength(1); + }); + + it("supports list-only adapters without a key-list method", async () => { + const { data } = fixture("file"); + const kv = { + list: async (scope: string) => [...(data.get(scope)?.values() ?? [])].filter((row) => row !== null), + }; + expectCounts(await createConsolidationStatusReader(kv as never)({ lastRun: null }), 2); + }); + + it("retains the empty best-effort summary count when both read paths fail", async () => { + const { kv } = fixture("file", { rejectSummaryKeys: true, rejectSummaryValues: true }); + expectCounts(await createConsolidationStatusReader(kv)({ lastRun: null }), 0); + }); +}); From 8e3e3eb9716a26bad85cf0d1a8dbb553d8681495 Mon Sep 17 00:00:00 2001 From: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> Date: Fri, 13 Nov 2026 07:45:40 +0400 Subject: [PATCH 02/10] perf(capture): avoid session scans for durable replays Signed-off-by: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> --- src/functions/observe.ts | 8 +- test/observe-durable-lookup.test.ts | 195 ++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 test/observe-durable-lookup.test.ts diff --git a/src/functions/observe.ts b/src/functions/observe.ts index 45dd08cd2d..8c92e2df37 100644 --- a/src/functions/observe.ts +++ b/src/functions/observe.ts @@ -267,8 +267,12 @@ export function registerObserveFunction( const promptRef = durable || typeof payload.eventId !== "string" ? obsId : payload.eventId; return withKeyedLock(`obs:${payload.sessionId}`, async () => { - const existing = await kv.list(KV.observations(payload.sessionId)); - const stored = durable ? existing.find((o) => o?.id === obsId) : undefined; + const direct = durable + ? await kv.get(KV.observations(payload.sessionId), obsId).catch(() => null) + : null; + const keyed = direct?.id === obsId ? direct : undefined; + const existing = keyed ? [] : await kv.list(KV.observations(payload.sessionId)); + const stored = keyed ?? (durable ? existing.find((o) => o?.id === obsId) : undefined); if (stored) { if (livePrompt) await recordLivePrompt(kv, payload.sessionId, livePrompt, promptRef); await restoreIndexEntries(stored); diff --git a/test/observe-durable-lookup.test.ts b/test/observe-durable-lookup.test.ts new file mode 100644 index 0000000000..3a8e423c39 --- /dev/null +++ b/test/observe-durable-lookup.test.ts @@ -0,0 +1,195 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { registerObserveFunction } from "../src/functions/observe.js"; +import { applyUpdateOps, StateKV, type StateBackend } from "../src/state/kv.js"; +import { KV } from "../src/state/schema.js"; +import type { CompressedObservation, HookPayload } from "../src/types.js"; +import { SESSION_SOURCE_MAX_BYTES } from "../src/functions/observation-source-budget.js"; + +const indexed = vi.hoisted(() => new Map()); +vi.mock("../src/functions/search.js", () => ({ + getSearchIndex: () => ({ + has: (id: string) => indexed.has(id), + add: (row: CompressedObservation) => indexed.set(row.id, structuredClone(row)), + remove: (id: string) => indexed.delete(id), + }), + getVectorIndex: () => null, + scheduleIndexSave: () => {}, + vectorIndexAddGuarded: async () => false, + vectorIndexRemove: () => {}, +})); +vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } })); + +const SESSION = "ses_durable_lookup"; +const SCOPE = KV.observations(SESSION); +const ID = "obs_replay0001"; +const TIMESTAMP = "2026-11-12T00:00:00.000Z"; +type Call = { function_id: string; payload: { scope?: string; key?: string; value?: unknown; ops?: Parameters[1] } }; + +function row(id = ID): CompressedObservation { + return { + id, sessionId: SESSION, timestamp: TIMESTAMP, type: "command_run", + title: "Persisted command", facts: [], narrative: "Stored result", concepts: [], files: [], + }; +} + +function payload(overrides: Partial = {}): HookPayload { + return { + sessionId: SESSION, hookType: "post_tool_use", timestamp: TIMESTAMP, + observationId: ID, data: { tool_name: "Bash", tool_input: { command: "echo fixture" }, tool_output: "fixture" }, + ...overrides, + }; +} + +function fixture(options: { backend?: StateBackend; rejectList?: boolean; rejectPointRead?: boolean; cap?: number } = {}) { + const data = new Map>([ + [SCOPE, new Map([[ID, row()], ["obs_unrelated1", { ...row("obs_unrelated1"), narrative: "x".repeat(32768) }]])], + [KV.sessions, new Map([[SESSION, { id: SESSION, project: "fixture", status: "active", observationCount: 2 }]])], + ]); + const calls: Call[] = []; + const handlers = new Map Promise>(); + const sdk = { + registerFunction: (id: string, handler: (data: HookPayload) => Promise) => handlers.set(id, handler), + trigger: async (call: Call): Promise => { + calls.push(structuredClone(call)); + if (!call.function_id.startsWith("state::")) return null; + const scope = call.payload.scope!; + if (!data.has(scope)) data.set(scope, new Map()); + const entries = data.get(scope)!; + const key = call.payload.key!; + if (call.function_id === "state::get") { + if (scope === SCOPE && options.rejectPointRead) throw new Error("point read unavailable"); + return structuredClone(entries.get(key) ?? null); + } + if (call.function_id === "state::list") { + if (scope === SCOPE && options.rejectList) throw new Error("whole observation list unavailable"); + return structuredClone([...entries.values()]); + } + if (call.function_id === "state::list_keys") { + const keys = [...entries.keys()]; + return options.backend === "redis" ? { keys } : keys; + } + if (call.function_id === "state::set") { + entries.set(key, structuredClone(call.payload.value)); + return call.payload.value; + } + if (call.function_id === "state::delete") return entries.delete(key); + if (call.function_id === "state::update") { + const old_value = structuredClone(entries.get(key) ?? null); + const new_value = applyUpdateOps(old_value, call.payload.ops!); + entries.set(key, new_value); + return { old_value, new_value, errors: [] }; + } + throw new Error(`Unexpected state invocation: ${call.function_id}`); + }, + }; + const kv = new StateKV(sdk as never, { backend: options.backend }); + registerObserveFunction(sdk as never, kv, undefined, options.cap); + const observe = (body = payload()) => handlers.get("mem::observe")!(body) as Promise>; + const observationLists = () => calls.filter((call) => call.payload.scope === SCOPE && call.function_id === "state::list"); + return { data, calls, observe, observationLists }; +} + +beforeEach(() => { + indexed.clear(); + vi.stubEnv("AGENTMEMORY_AUTO_COMPRESS", "false"); +}); +afterEach(() => vi.unstubAllEnvs()); + +describe("durable observation lookup", () => { + it.each(["file", "redis"] as const)("replays the stored record without loading its session on %s", async (backend) => { + const f = fixture({ backend }); + const before = structuredClone(f.data); + expect(await f.observe()).toEqual({ observationId: ID, deduplicated: true, existing: true }); + expect(indexed.get(ID)).toEqual(row()); + expect(f.data).toEqual(before); + expect(f.observationLists()).toEqual([]); + }); + + it("can replay a point-readable record when the whole session cannot be read", async () => { + const f = fixture({ rejectList: true }); + expect(await f.observe()).toEqual({ observationId: ID, deduplicated: true, existing: true }); + expect(indexed.get(ID)).toEqual(row()); + expect(f.observationLists()).toEqual([]); + }); + + it("records a replayed live prompt and restores its index without reading other observations", async () => { + const f = fixture(); + expect(await f.observe(payload({ hookType: "prompt_submit", data: { prompt: "repair the fixture" } }))).toMatchObject({ existing: true, observationId: ID }); + const ledgers = [...f.data.get(KV.capturePrompts)!.values()] as Array<{ prompts: Record }>; + expect(ledgers).toHaveLength(1); + expect(Object.values(ledgers[0]!.prompts)).toEqual([{ live: [ID], claimed: [] }]); + expect(indexed.get(ID)).toEqual(row()); + expect(f.observationLists()).toEqual([]); + }); + + it("keeps the working list fallback if the point read fails", async () => { + const f = fixture({ rejectPointRead: true }); + expect(await f.observe()).toMatchObject({ existing: true, observationId: ID }); + expect(indexed.get(ID)).toEqual(row()); + expect(f.observationLists()).toHaveLength(1); + }); + + it("retains legacy ID matching when a row is stored under a different key", async () => { + const f = fixture(); + f.data.get(SCOPE)!.delete(ID); + f.data.get(SCOPE)!.set("legacy-key", row()); + expect(await f.observe()).toMatchObject({ existing: true, observationId: ID }); + expect(indexed.get(ID)).toEqual(row()); + expect([...f.data.get(SCOPE)!.keys()]).toEqual(["obs_unrelated1", "legacy-key"]); + expect(f.observationLists()).toHaveLength(1); + }); + + it("does not mistake a mismatched record at the requested key for the matching observation", async () => { + const f = fixture(); + f.data.get(SCOPE)!.set(ID, row("obs_wrong_id1")); + f.data.get(SCOPE)!.set("legacy-key", row()); + expect(await f.observe()).toMatchObject({ existing: true, observationId: ID }); + expect(indexed.get(ID)).toEqual(row()); + expect(indexed.has("obs_wrong_id1")).toBe(false); + expect(f.observationLists()).toHaveLength(1); + }); + + it("still reads the session and respects its source budget for a new durable observation", async () => { + const f = fixture(); + f.data.get(SCOPE)!.delete(ID); + const largeSource = { hookType: "post_tool_use", originalBytes: SESSION_SOURCE_MAX_BYTES, truncated: false, toolOutput: "s".repeat(SESSION_SOURCE_MAX_BYTES) }; + f.data.get(SCOPE)!.set("obs_unrelated1", { ...row("obs_unrelated1"), source: largeSource }); + expect(await f.observe()).toMatchObject({ observationId: ID }); + expect(f.data.get(SCOPE)!.get(ID)).toMatchObject({ id: ID, sessionId: SESSION }); + expect((f.data.get(SCOPE)!.get(ID) as CompressedObservation).source).toBeUndefined(); + expect((f.data.get(SCOPE)!.get("obs_unrelated1") as CompressedObservation).source).toEqual(largeSource); + expect(f.observationLists()).toHaveLength(1); + }); + + it("keeps cap eviction and normal writes for a non-durable observation", async () => { + const f = fixture({ cap: 2 }); + const result = await f.observe(payload({ observationId: undefined })); + expect(result.observationId).toMatch(/^obs_/); + expect(result.deduplicated).toBeUndefined(); + expect(f.data.get(SCOPE)!.size).toBe(2); + expect(f.data.get(SCOPE)!.has(result.observationId as string)).toBe(true); + expect(f.observationLists()).toHaveLength(1); + expect(f.calls.some(call => call.function_id === "state::get" && call.payload.scope === SCOPE)).toBe(false); + }); + + it("keeps an invalid observation ID on the existing non-durable path", async () => { + const f = fixture(); + const result = await f.observe(payload({ observationId: "invalid" })); + expect(result.observationId).toMatch(/^obs_/); + expect(result.observationId).not.toBe("invalid"); + expect(result.deduplicated).toBeUndefined(); + expect(f.observationLists()).toHaveLength(1); + expect(f.calls.some(call => call.function_id === "state::get" && call.payload.scope === SCOPE)).toBe(false); + }); + + it("keeps concurrent durable replays idempotent without full-session reads", async () => { + const f = fixture(); + const before = structuredClone(f.data); + expect(await Promise.all(Array.from({ length: 8 }, () => f.observe()))).toEqual( + Array.from({ length: 8 }, () => ({ observationId: ID, deduplicated: true, existing: true })), + ); + expect(f.data).toEqual(before); + expect(indexed.get(ID)).toEqual(row()); + expect(f.observationLists()).toEqual([]); + }); +}); From 3da8b57415acff20907351ea1fe0e4d0cd21d2f5 Mon Sep 17 00:00:00 2001 From: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> Date: Fri, 13 Nov 2026 08:49:33 +0400 Subject: [PATCH 03/10] test(capture): complete durable lookup fixtures Signed-off-by: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> --- test/observe-durable-lookup.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/observe-durable-lookup.test.ts b/test/observe-durable-lookup.test.ts index 3a8e423c39..d0d586b085 100644 --- a/test/observe-durable-lookup.test.ts +++ b/test/observe-durable-lookup.test.ts @@ -28,13 +28,13 @@ type Call = { function_id: string; payload: { scope?: string; key?: string; valu function row(id = ID): CompressedObservation { return { id, sessionId: SESSION, timestamp: TIMESTAMP, type: "command_run", - title: "Persisted command", facts: [], narrative: "Stored result", concepts: [], files: [], + title: "Persisted command", facts: [], narrative: "Stored result", concepts: [], files: [], importance: 5, }; } function payload(overrides: Partial = {}): HookPayload { return { - sessionId: SESSION, hookType: "post_tool_use", timestamp: TIMESTAMP, + sessionId: SESSION, project: "fixture", cwd: "/tmp/fixture", hookType: "post_tool_use", timestamp: TIMESTAMP, observationId: ID, data: { tool_name: "Bash", tool_input: { command: "echo fixture" }, tool_output: "fixture" }, ...overrides, }; From 05fcfddd45a36e8cfd7780f43b93f0831e9a5241 Mon Sep 17 00:00:00 2001 From: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:21:16 +0400 Subject: [PATCH 04/10] fix(insights): retain only the requested top results while paging Signed-off-by: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> --- src/functions/reflect.ts | 45 +- src/state/kv.ts | 21 +- test/fixtures/insight-list-memory.ts | 94 +++ test/fixtures/insight-list.json | 1061 ++++++++++++++++++++++++++ test/insight-list.test.ts | 106 +++ 5 files changed, 1309 insertions(+), 18 deletions(-) create mode 100644 test/fixtures/insight-list-memory.ts create mode 100644 test/fixtures/insight-list.json create mode 100644 test/insight-list.test.ts diff --git a/src/functions/reflect.ts b/src/functions/reflect.ts index 7997c17137..7dbd2ff6fd 100644 --- a/src/functions/reflect.ts +++ b/src/functions/reflect.ts @@ -2,7 +2,7 @@ import { createGraphIndexReader } from "../state/graph-retrieval-index.js"; import { lookupObservationSession } from "../state/obs-index.js"; import type { IIIClient } from "iii-sdk"; import type { StateKV } from "../state/kv.js"; -import { collectAll } from "../state/kv.js"; +import { collectAll, compareInsertionOrder, insertionOrderKey } from "../state/kv.js"; import { KV, fingerprintId } from "../state/schema.js"; import type { Insight, @@ -570,19 +570,40 @@ export function registerReflectFunctions( if (limit === null) return invalidOption("limit must be a positive safe integer"); if (!validConfidence(data?.minConfidence)) return invalidOption("minConfidence must be a finite number between 0 and 1"); const minConfidence = data?.minConfidence ?? 0; - let items = await collectAll(kv, KV.insights); - - items = items.filter( - (i) => !i.deleted && i.confidence >= minConfidence, - ); - - if (data?.project) { - items = items.filter((i) => i.project === data.project); + const paged = typeof kv.iterate === "function"; + const redisOrder = paged && kv.backend === "redis"; + type Entry = { insight: Insight; order: ReturnType }; + const items: Entry[] = []; + const compare = (a: Entry, b: Entry) => + b.insight.confidence - a.insight.confidence || + (redisOrder ? compareInsertionOrder(a.order, b.order) : 0); + const consider = (insight: Insight) => { + if (insight.deleted || !(insight.confidence >= minConfidence)) return; + if (data?.project && insight.project !== data.project) return; + const entry: Entry = { + insight, + order: redisOrder ? insertionOrderKey(insight) : { at: 0, id: "" }, + }; + if (items.length === limit && compare(entry, items[items.length - 1]!) >= 0) return; + let low = 0; + let high = items.length; + while (low < high) { + const middle = (low + high) >>> 1; + if (compare(entry, items[middle]!) < 0) high = middle; + else low = middle + 1; + } + items.splice(low, 0, entry); + if (items.length > limit) items.pop(); + }; + if (paged) { + for await (const page of kv.iterate(KV.insights)) { + for (const insight of page) consider(insight); + } + } else { + for (const insight of await collectAll(kv, KV.insights)) consider(insight); } - items.sort((a, b) => b.confidence - a.confidence); - - const result = { success: true, insights: items.slice(0, limit) }; + const result = { success: true, insights: items.map((entry) => entry.insight) }; return checkPayloadFrameSize(result, "reduce limit or filter by project") ?? result; }, ); diff --git a/src/state/kv.ts b/src/state/kv.ts index 8d08c58af3..5aa5bd8a5f 100644 --- a/src/state/kv.ts +++ b/src/state/kv.ts @@ -94,14 +94,23 @@ function idKey(value: unknown): string { return typeof id === 'string' ? id : typeof id === 'number' ? String(id) : '' } +export function insertionOrderKey(value: unknown): { at: number; id: string } { + return { at: orderKey(value), id: idKey(value) } +} + +export function compareInsertionOrder( + a: ReturnType, + b: ReturnType, +): number { + if (a.at !== b.at) return a.at < b.at ? -1 : 1 + if (a.id === b.id) return 0 + return a.id < b.id ? -1 : 1 +} + export function orderLikeInsertion(values: T[]): T[] { return values - .map((value) => ({ value, at: orderKey(value), id: idKey(value) })) - .sort((a, b) => { - if (a.at !== b.at) return a.at < b.at ? -1 : 1 - if (a.id === b.id) return 0 - return a.id < b.id ? -1 : 1 - }) + .map((value) => ({ value, ...insertionOrderKey(value) })) + .sort(compareInsertionOrder) .map((entry) => entry.value) } diff --git a/test/fixtures/insight-list-memory.ts b/test/fixtures/insight-list-memory.ts new file mode 100644 index 0000000000..20ca4221e2 --- /dev/null +++ b/test/fixtures/insight-list-memory.ts @@ -0,0 +1,94 @@ +import { strict as assert } from "node:assert"; +import { createHash } from "node:crypto"; +import { registerReflectFunctions } from "../../src/functions/reflect.js"; +import { StateKV } from "../../src/state/kv.js"; +import { KV } from "../../src/state/schema.js"; + +const backend = process.argv[2]; +assert(backend === "file" || backend === "redis"); +assert.equal(typeof global.gc, "function", "Run this probe with --expose-gc"); + +const count = 4096; +const functions = new Map unknown>(); +let reads = 0; +let peakHeapBytes = 0; +let modelRequests = 0; + +function sampleHeap() { + global.gc!(); + peakHeapBytes = Math.max(peakHeapBytes, process.memoryUsage().heapUsed); +} + +function record(index: number) { + const bytes = Buffer.alloc(12 * 1024, index % 251); + bytes.writeUInt32LE(index); + const concept = bytes.toString("base64"); + return JSON.parse(JSON.stringify({ + id: `historical-${index.toString().padStart(5, "0")}`, + title: `Historical insight ${index}`, + content: `Complete insight ${index}; Unicode Ж and é remain intact.`, + confidence: (index + 1) / count, + reinforcements: 3, + sourceConceptCluster: [concept], + sourceMemoryIds: [`memory-${index}`], + sourceLessonIds: [], + sourceCrystalIds: [], + tags: [concept], + createdAt: "2026-01-01T00:00:00.000Z", + updatedAt: "2026-01-01T00:00:00.000Z", + decayRate: 0.05, + project: "large-store", + legacy_extra: { index, values: [0, false, null] }, + })); +} + +const sdk = { + registerFunction: (id: string | { id: string }, handler: (data: unknown) => unknown) => { + functions.set(typeof id === "string" ? id : id.id, handler); + }, + trigger: async ({ function_id, payload }: { + function_id: string; + payload: { scope: string; key?: string }; + }) => { + assert.equal(payload.scope, KV.insights); + if (function_id === "state::list_keys") { + return Array.from({ length: count }, (_, index) => String(index)); + } + assert.equal(function_id, "state::get"); + const index = Number(payload.key); + assert(Number.isInteger(index) && index >= 0 && index < count); + // Sample while the handler still owns earlier pages, before the next read. + if (reads % 200 === 0) sampleHeap(); + reads++; + return record(index); + }, +}; + +const unexpectedModel = async () => { + modelRequests++; + throw new Error("Listing insights must not request a model"); +}; +const kv = new StateKV(sdk as never, { backend }); +registerReflectFunctions(sdk as never, kv, { + name: "no-model", + compress: unexpectedModel, + summarize: unexpectedModel, +} as never); + +sampleHeap(); +const baselineHeapBytes = peakHeapBytes; +const result = await functions.get("mem::insight-list")!({ limit: 1, project: "large-store" }); +sampleHeap(); +assert.deepEqual(result, { success: true, insights: [record(count - 1)] }); +assert.equal(reads, count); +assert.equal(modelRequests, 0); +console.log(JSON.stringify({ + backend, + records: count, + reads, + modelRequests, + baselineHeapBytes, + peakHeapBytes, + heapGrowthBytes: peakHeapBytes - baselineHeapBytes, + completeResultSha256: createHash("sha256").update(JSON.stringify(result)).digest("hex"), +})); diff --git a/test/fixtures/insight-list.json b/test/fixtures/insight-list.json new file mode 100644 index 0000000000..ae737c2fbd --- /dev/null +++ b/test/fixtures/insight-list.json @@ -0,0 +1,1061 @@ +{ + "records": { + "late": { + "id": "ins_date_late", + "title": "Fixture late", + "content": "Complete historical insight late; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.8, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-04T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "late", + "values": [ + 0, + false, + null + ] + } + }, + "id_late": { + "id": "ins_mjxjdhc0_a", + "title": "Fixture id_late", + "content": "Complete historical insight id_late; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.8, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-01T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "id_late", + "values": [ + 0, + false, + null + ] + } + }, + "high_b": { + "id": "high-b", + "title": "Fixture high_b", + "content": "Complete historical insight high_b; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.95, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-02T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "B", + "legacy_extra": { + "keep": "high_b", + "values": [ + 0, + false, + null + ] + } + }, + "field_early": { + "id": "ins_field_early", + "title": "Fixture field_early", + "content": "Complete historical insight field_early; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.8, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-01T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "field_early", + "values": [ + 0, + false, + null + ] + } + }, + "tie_b": { + "id": "plain-b", + "title": "Fixture tie_b", + "content": "Complete historical insight tie_b; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.8, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-02T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "tie_b", + "values": [ + 0, + false, + null + ] + } + }, + "deleted_a": { + "id": "deleted-a", + "title": "Fixture deleted_a", + "content": "Complete historical insight deleted_a; commas, accents é and Unicode Ж remain intact.", + "confidence": 1, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-02T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "deleted_a", + "values": [ + 0, + false, + null + ] + }, + "deleted": true + }, + "no_time": { + "id": "plain-z", + "title": "Fixture no_time", + "content": "Complete historical insight no_time; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.8, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "not-a-date", + "updatedAt": "not-a-date", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "no_time", + "values": [ + 0, + false, + null + ] + } + }, + "tie_a": { + "id": "plain-a", + "title": "Fixture tie_a", + "content": "Complete historical insight tie_a; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.8, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-02T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "tie_a", + "values": [ + 0, + false, + null + ] + } + }, + "unscoped": { + "id": "unscoped", + "title": "Fixture unscoped", + "content": "Complete historical insight unscoped; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.9, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-02T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "legacy_extra": { + "keep": "unscoped", + "values": [ + 0, + false, + null + ] + } + }, + "id_early": { + "id": "ins_mjt91xc0_z", + "title": "Fixture id_early", + "content": "Complete historical insight id_early; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.8, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-05T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "id_early", + "values": [ + 0, + false, + null + ] + } + }, + "low_a": { + "id": "low-a", + "title": "Fixture low_a", + "content": "Complete historical insight low_a; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.7, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-02T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "low_a", + "values": [ + 0, + false, + null + ] + } + }, + "fallback": { + "id": "fallback", + "title": "Fixture fallback", + "content": "Complete historical insight fallback; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.8, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "not-a-date", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "fallback", + "values": [ + 0, + false, + null + ] + }, + "timestamp": "2026-01-01T00:00:00.000Z", + "startedAt": "2026-01-05T00:00:00.000Z" + }, + "top_a": { + "id": "top-a", + "title": "Fixture top_a", + "content": "Complete historical insight top_a; commas, accents é and Unicode Ж remain intact.", + "confidence": 1, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-02T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "top_a", + "values": [ + 0, + false, + null + ] + } + }, + "zero_a": { + "id": "zero-a", + "title": "Fixture zero_a", + "content": "Complete historical insight zero_a; commas, accents é and Unicode Ж remain intact.", + "confidence": 0, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-02T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "A", + "legacy_extra": { + "keep": "zero_a", + "values": [ + 0, + false, + null + ] + } + }, + "low_b": { + "id": "low-b", + "title": "Fixture low_b", + "content": "Complete historical insight low_b; commas, accents é and Unicode Ж remain intact.", + "confidence": 0.6, + "reinforcements": 3, + "sourceConceptCluster": [ + "alpha", + "comma,inside", + "Ж" + ], + "sourceMemoryIds": [ + "mem-one", + "mem-two" + ], + "sourceLessonIds": [ + "lesson-one" + ], + "sourceCrystalIds": [ + "crystal-one" + ], + "evidenceVersion": 1, + "tags": [ + "alpha", + "same content preserved" + ], + "createdAt": "2026-01-02T00:00:00.000Z", + "updatedAt": "2026-01-09T00:00:00.000Z", + "lastReinforcedAt": "2026-01-08T00:00:00.000Z", + "lastDecayedAt": "2026-01-07T00:00:00.000Z", + "decayRate": 0.05, + "project": "B", + "legacy_extra": { + "keep": "low_b", + "values": [ + 0, + false, + null + ] + } + } + }, + "pages": [ + [ + "late", + "id_late", + "high_b" + ], + [ + "field_early", + "tie_b", + "deleted_a", + "no_time" + ], + [ + "tie_a", + "unscoped", + "id_early", + "low_a" + ], + [ + "fallback", + "top_a", + "zero_a", + "low_b" + ] + ], + "cases": [ + { + "name": "small3_projectA_filters_before_selection", + "request": { + "project": "A", + "limit": 3 + }, + "expected": { + "file": [ + "top_a", + "late", + "id_late" + ], + "redis": [ + "top_a", + "id_early", + "fallback" + ] + } + }, + { + "name": "small5_projectA_confidence_ties", + "request": { + "project": "A", + "limit": 5 + }, + "expected": { + "file": [ + "top_a", + "late", + "id_late", + "field_early", + "tie_b" + ], + "redis": [ + "top_a", + "id_early", + "fallback", + "field_early", + "tie_a" + ] + } + }, + { + "name": "small5_global", + "request": { + "limit": 5 + }, + "expected": { + "file": [ + "top_a", + "high_b", + "unscoped", + "late", + "id_late" + ], + "redis": [ + "top_a", + "high_b", + "unscoped", + "id_early", + "fallback" + ] + } + }, + { + "name": "default_global_all14", + "request": {}, + "expected": { + "file": [ + "top_a", + "high_b", + "unscoped", + "late", + "id_late", + "field_early", + "tie_b", + "no_time", + "tie_a", + "id_early", + "fallback", + "low_a", + "low_b", + "zero_a" + ], + "redis": [ + "top_a", + "high_b", + "unscoped", + "id_early", + "fallback", + "field_early", + "tie_a", + "tie_b", + "id_late", + "late", + "no_time", + "low_a", + "low_b", + "zero_a" + ] + } + }, + { + "name": "empty_project_is_unfiltered", + "request": { + "project": "" + }, + "expected": { + "file": [ + "top_a", + "high_b", + "unscoped", + "late", + "id_late", + "field_early", + "tie_b", + "no_time", + "tie_a", + "id_early", + "fallback", + "low_a", + "low_b", + "zero_a" + ], + "redis": [ + "top_a", + "high_b", + "unscoped", + "id_early", + "fallback", + "field_early", + "tie_a", + "tie_b", + "id_late", + "late", + "no_time", + "low_a", + "low_b", + "zero_a" + ] + } + }, + { + "name": "project_filter_exact_no_trim", + "request": { + "project": " A " + }, + "expected": { + "file": [], + "redis": [] + } + }, + { + "name": "projectB", + "request": { + "project": "B" + }, + "expected": { + "file": [ + "high_b", + "low_b" + ], + "redis": [ + "high_b", + "low_b" + ] + } + }, + { + "name": "projectA_minConfidence_inclusive", + "request": { + "project": "A", + "minConfidence": 0.8 + }, + "expected": { + "file": [ + "top_a", + "late", + "id_late", + "field_early", + "tie_b", + "no_time", + "tie_a", + "id_early", + "fallback" + ], + "redis": [ + "top_a", + "id_early", + "fallback", + "field_early", + "tie_a", + "tie_b", + "id_late", + "late", + "no_time" + ] + } + }, + { + "name": "projectA_confidence1", + "request": { + "project": "A", + "minConfidence": 1 + }, + "expected": { + "file": [ + "top_a" + ], + "redis": [ + "top_a" + ] + } + }, + { + "name": "projectA_limit501_caps500", + "request": { + "project": "A", + "limit": 501 + }, + "expected": { + "file": [ + "top_a", + "late", + "id_late", + "field_early", + "tie_b", + "no_time", + "tie_a", + "id_early", + "fallback", + "low_a", + "zero_a" + ], + "redis": [ + "top_a", + "id_early", + "fallback", + "field_early", + "tie_a", + "tie_b", + "id_late", + "late", + "no_time", + "low_a", + "zero_a" + ] + } + }, + { + "name": "limit_MAX_SAFE_INTEGER_caps500", + "request": { + "limit": 9007199254740991 + }, + "expected": { + "file": [ + "top_a", + "high_b", + "unscoped", + "late", + "id_late", + "field_early", + "tie_b", + "no_time", + "tie_a", + "id_early", + "fallback", + "low_a", + "low_b", + "zero_a" + ], + "redis": [ + "top_a", + "high_b", + "unscoped", + "id_early", + "fallback", + "field_early", + "tie_a", + "tie_b", + "id_late", + "late", + "no_time", + "low_a", + "low_b", + "zero_a" + ] + } + }, + { + "name": "unknown_project_empty", + "request": { + "project": "missing", + "limit": 1 + }, + "expected": { + "file": [], + "redis": [] + } + } + ], + "invalid": [ + { + "request": { + "limit": 0 + }, + "expected": { + "success": false, + "invalid": true, + "error": "limit must be a positive safe integer" + } + }, + { + "request": { + "limit": -1 + }, + "expected": { + "success": false, + "invalid": true, + "error": "limit must be a positive safe integer" + } + }, + { + "request": { + "limit": 1.5 + }, + "expected": { + "success": false, + "invalid": true, + "error": "limit must be a positive safe integer" + } + }, + { + "request": { + "limit": "3" + }, + "expected": { + "success": false, + "invalid": true, + "error": "limit must be a positive safe integer" + } + }, + { + "request": { + "limit": null + }, + "expected": { + "success": false, + "invalid": true, + "error": "limit must be a positive safe integer" + } + }, + { + "request": { + "limit": 9007199254740992 + }, + "expected": { + "success": false, + "invalid": true, + "error": "limit must be a positive safe integer" + } + }, + { + "request": { + "minConfidence": -0.1 + }, + "expected": { + "success": false, + "invalid": true, + "error": "minConfidence must be a finite number between 0 and 1" + } + }, + { + "request": { + "minConfidence": 1.1 + }, + "expected": { + "success": false, + "invalid": true, + "error": "minConfidence must be a finite number between 0 and 1" + } + }, + { + "request": { + "minConfidence": "0.8" + }, + "expected": { + "success": false, + "invalid": true, + "error": "minConfidence must be a finite number between 0 and 1" + } + }, + { + "request": { + "minConfidence": null + }, + "expected": { + "success": false, + "invalid": true, + "error": "minConfidence must be a finite number between 0 and 1" + } + }, + { + "request": { + "limit": 0, + "minConfidence": "bad" + }, + "expected": { + "success": false, + "invalid": true, + "error": "limit must be a positive safe integer" + } + } + ] +} diff --git a/test/insight-list.test.ts b/test/insight-list.test.ts new file mode 100644 index 0000000000..30a890ac6f --- /dev/null +++ b/test/insight-list.test.ts @@ -0,0 +1,106 @@ +import { readFileSync } from "node:fs"; +import { spawnSync } from "node:child_process"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; +import { registerReflectFunctions } from "../src/functions/reflect.js"; +import { StateKV } from "../src/state/kv.js"; +import { KV } from "../src/state/schema.js"; + +type Backend = "file" | "redis"; +type Row = Record; +const fixture = JSON.parse(readFileSync(new URL("./fixtures/insight-list.json", import.meta.url), "utf8")) as { + records: Record; + pages: string[][]; + cases: { name: string; request: Row; expected: Record }[]; + invalid: { request: Row; expected: Row }[]; +}; +const rows = fixture.pages.flat().map((alias) => fixture.records[alias]!); + +function register(kv: unknown) { + const functions = new Map Promise>(); + const sdk = { + registerFunction: (id: string | { id: string }, handler: (data: unknown) => Promise) => { + functions.set(typeof id === "string" ? id : id.id, handler); + }, + }; + registerReflectFunctions(sdk as never, kv as never, {} as never); + return (data: unknown) => functions.get("mem::insight-list")!(data); +} + +function paged(backend: Backend) { + let pagesRead = 0; + const kv = { + backend, + async *iterate(scope: string) { + expect(scope).toBe(KV.insights); + for (const page of fixture.pages) { + pagesRead++; + yield page.map((alias) => structuredClone(fixture.records[alias]!)); + } + }, + }; + return { call: register(kv), pagesRead: () => pagesRead }; +} + +describe.each(["file", "redis"] as const)("complete historical insight list (%s)", (backend) => { + it.each(fixture.cases)("preserves exact records and order: $name", async ({ request, expected }) => { + const f = paged(backend); + expect(await f.call(request)).toEqual({ + success: true, + insights: expected[backend].map((alias) => fixture.records[alias]), + }); + expect(f.pagesRead()).toBe(fixture.pages.length); + }); + + it.each(fixture.invalid)("validates before reading: $request", async ({ request, expected }) => { + const f = paged(backend); + expect(await f.call(request)).toEqual(expected); + expect(f.pagesRead()).toBe(0); + }); + + it("preserves complete order through real StateKV page reads", async () => { + const sdk = { + trigger: async ({ function_id, payload }: { function_id: string; payload: { scope: string; key?: string } }) => { + expect(payload.scope).toBe(KV.insights); + if (function_id === "state::list_keys") return rows.map((_, index) => String(index)); + expect(function_id).toBe("state::get"); + return structuredClone(rows[Number(payload.key)]); + }, + }; + const kv = new StateKV(sdk as never, { backend }); + const iterate = kv.iterate.bind(kv); + kv.iterate = (scope) => iterate(scope, { pageSize: 4 }); + const expected = fixture.cases.find((item) => item.name === "small5_projectA_confidence_ties")!; + expect(await register(kv)(expected.request)).toEqual({ + success: true, + insights: expected.expected[backend].map((alias) => fixture.records[alias]), + }); + }); + + it("retains the list-only fallback's original tie order", async () => { + const kv = { backend, list: async () => structuredClone(rows) }; + const expected = fixture.cases.find((item) => item.name === "small3_projectA_filters_before_selection")!; + expect(await register(kv)(expected.request)).toEqual({ + success: true, + insights: expected.expected.file.map((alias) => fixture.records[alias]), + }); + }); + + it("propagates a later page error instead of returning a partial top result", async () => { + const error = new Error("later insight page unavailable"); + const kv = { backend, async *iterate() { yield rows.slice(0, 3); throw error; } }; + await expect(register(kv)({ limit: 1 })).rejects.toBe(error); + }); + + it("keeps a small query below a retained-heap budget for large historical records", () => { + const result = spawnSync(process.execPath, [ + "--max-old-space-size=384", "--expose-gc", "--import", "tsx", + fileURLToPath(new URL("./fixtures/insight-list-memory.ts", import.meta.url)), backend, + ], { encoding: "utf8", timeout: 60_000 }); + expect(result.error).toBeUndefined(); + expect(result.status, result.stderr).toBe(0); + const metrics = JSON.parse(result.stdout.trim().split("\n").at(-1)!); + expect(metrics).toMatchObject({ backend, records: 4096, reads: 4096, modelRequests: 0 }); + expect(metrics.heapGrowthBytes).toBeLessThan(24 * 1024 * 1024); + }, 65_000); +}); From 2fdfa5b1e4950abc5935a84883a912b28ac9fc30 Mon Sep 17 00:00:00 2001 From: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:49:27 +0400 Subject: [PATCH 05/10] test(hooks): validate source entrypoints in CI Signed-off-by: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> --- .github/workflows/ci.yml | 1 + package.json | 1 + test/fixtures/hook-source-fetch.mjs | 32 +++++ test/helpers/hook-source.ts | 180 ++++++++++++++++++++++++++++ test/post-tool-use-source.test.ts | 30 +++++ test/pre-tool-use-source.test.ts | 30 +++++ test/prompt-submit-source.test.ts | 21 ++++ test/session-start-source.test.ts | 28 +++++ test/stop-source.test.ts | 31 +++++ tsconfig.hooks.json | 9 ++ 10 files changed, 363 insertions(+) create mode 100644 test/fixtures/hook-source-fetch.mjs create mode 100644 test/helpers/hook-source.ts create mode 100644 test/post-tool-use-source.test.ts create mode 100644 test/pre-tool-use-source.test.ts create mode 100644 test/prompt-submit-source.test.ts create mode 100644 test/session-start-source.test.ts create mode 100644 test/stop-source.test.ts create mode 100644 tsconfig.hooks.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2806b98182..7dcd395239 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,6 +71,7 @@ jobs: # in one pass. - run: npm install --legacy-peer-deps --no-audit --no-fund - run: npm run build + - run: npm run typecheck:hooks - run: npm run skills:check - run: npx tsc --noEmit --project tsconfig.eval.json - run: npm test diff --git a/package.json b/package.json index 5540b52f8f..bab547b6b4 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "scripts": { "build": "tsdown && node scripts/copy-assets.mjs", + "typecheck:hooks": "tsc --noEmit --project tsconfig.hooks.json", "dev": "tsx src/index.ts", "start": "node dist/cli.mjs", "migrate": "node dist/functions/migrate.js", diff --git a/test/fixtures/hook-source-fetch.mjs b/test/fixtures/hook-source-fetch.mjs new file mode 100644 index 0000000000..96c49ed4cf --- /dev/null +++ b/test/fixtures/hook-source-fetch.mjs @@ -0,0 +1,32 @@ +import { appendFileSync } from "node:fs"; + +globalThis.fetch = async (url, init = {}) => { + appendFileSync( + process.env.AGENTMEMORY_HOOK_TEST_REQUESTS, + `${JSON.stringify({ + url: String(url), + method: init.method, + headers: Object.fromEntries(new Headers(init.headers)), + body: JSON.parse(init.body), + })}\n`, + ); + + const mode = process.env.AGENTMEMORY_HOOK_TEST_FETCH; + if (mode === "unreachable") throw new TypeError("fetch failed"); + if (mode === "hanging") { + return new Promise((_resolve, reject) => { + const timer = setTimeout(() => reject(new Error("unaborted fetch")), 10_000); + const abort = () => { + clearTimeout(timer); + reject(init.signal.reason); + }; + if (init.signal.aborted) abort(); + else init.signal.addEventListener("abort", abort, { once: true }); + }); + } + + return new Response(JSON.stringify({ context: "source-hook recalled context" }), { + status: String(url).endsWith("/observe") ? 201 : 200, + headers: { "content-type": "application/json" }, + }); +}; diff --git a/test/helpers/hook-source.ts b/test/helpers/hook-source.ts new file mode 100644 index 0000000000..26397271a3 --- /dev/null +++ b/test/helpers/hook-source.ts @@ -0,0 +1,180 @@ +import { spawn } from "node:child_process"; +import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs"; +import { createRequire } from "node:module"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { expect, it } from "vitest"; + +type HookName = "session-start" | "prompt-submit" | "pre-tool-use" | "post-tool-use" | "stop"; + +interface Request { + url: string; + method: string; + headers: Record; + body: Record; +} + +interface SpoolRecord { + eventId: string; + reason: string; + body: Record; +} + +interface HookResult { + code: number | null; + signal: NodeJS.Signals | null; + elapsedMs: number; + stdout: string; + stderr: string; + requests: Request[]; + spooled: SpoolRecord[]; +} + +const root = fileURLToPath(new URL("../../", import.meta.url)); +const loader = pathToFileURL(createRequire(import.meta.url).resolve("tsx")).href; +const preload = pathToFileURL(join(root, "test/fixtures/hook-source-fetch.mjs")).href; + +function lines(path: string): T[] { + return existsSync(path) + ? readFileSync(path, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line) as T) + : []; +} + +export async function runSourceHook( + name: HookName, + payload: Record | string, + options: { + fetch?: "success" | "unreachable" | "hanging"; + env?: Record; + } = {}, +): Promise { + const dir = mkdtempSync(join(tmpdir(), "am-hook-source-")); + const requests = join(dir, "requests.jsonl"); + const spool = join(dir, "spool"); + const startedAt = performance.now(); + try { + const result = await new Promise>((done, reject) => { + const child = spawn(process.execPath, [ + "--import", loader, + "--import", preload, + resolve(root, "src/hooks", `${name}.ts`), + ], { + cwd: dir, + env: { + PATH: process.env["PATH"], + SystemRoot: process.env["SystemRoot"], + HOME: dir, + USERPROFILE: dir, + APPDATA: dir, + LOCALAPPDATA: dir, + TEMP: dir, + TMP: dir, + AGENTMEMORY_URL: "http://127.0.0.1:3111", + AGENTMEMORY_SECRET: "source-hook-test-secret", + AGENTMEMORY_PROJECT_NAME: "source-hook-project", + AGENTMEMORY_INJECT_CONTEXT: "true", + AGENTMEMORY_CAPTURE_SPOOL_DIR: spool, + AGENTMEMORY_HOOK_TEST_FETCH: options.fetch ?? "success", + AGENTMEMORY_HOOK_TEST_REQUESTS: requests, + ...options.env, + }, + stdio: ["pipe", "pipe", "pipe"], + }); + let stdout = ""; + let stderr = ""; + const timer = setTimeout(() => { + child.kill("SIGKILL"); + reject(new Error(`${name} exceeded the test deadline: ${stderr}`)); + }, 7000); + child.stdout.on("data", (chunk) => { stdout += chunk; }); + child.stderr.on("data", (chunk) => { stderr += chunk; }); + child.on("error", (error) => { + clearTimeout(timer); + reject(error); + }); + child.on("close", (code, signal) => { + clearTimeout(timer); + done({ code, signal, stdout, stderr, elapsedMs: performance.now() - startedAt }); + }); + child.stdin.on("error", () => {}); + child.stdin.end(typeof payload === "string" ? payload : JSON.stringify(payload)); + }); + return { + ...result, + requests: lines(requests), + spooled: existsSync(spool) + ? readdirSync(spool).filter((name) => name.endsWith(".jsonl")) + .flatMap((name) => lines(join(spool, name))) + : [], + }; + } finally { + rmSync(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 }); + } +} + +export function expectExit(result: HookResult, stdout = ""): void { + expect(result.code, result.stderr).toBe(0); + expect(result.signal).toBeNull(); + expect(result.elapsedMs).toBeLessThan(5000); + expect(result.stdout).toBe(stdout); +} + +export function expectPost(request: Request | undefined, path: string): asserts request is Request { + expect(request).toBeDefined(); + expect(request!.url).toBe(`http://127.0.0.1:3111${path}`); + expect(request!.method).toBe("POST"); + expect(request!.headers).toEqual({ + authorization: "Bearer source-hook-test-secret", + "content-type": "application/json", + }); +} + +export function failureCases( + name: HookName, + payload: Record, + options: { capture: boolean; requestPaths: string[] }, +): void { + it.each(["{broken", "null", "7"])("ignores malformed stdin %s", async (input) => { + const result = await runSourceHook(name, input); + expectExit(result); + expect(result.requests).toEqual([]); + expect(result.spooled).toEqual([]); + }); + + it.each(["environment", "payload"])("ignores SDK child context from %s", async (source) => { + const result = await runSourceHook(name, + source === "payload" ? { ...payload, entrypoint: "sdk-ts" } : payload, + { env: source === "environment" ? { AGENTMEMORY_SDK_CHILD: "1" } : {} }, + ); + expectExit(result); + expect(result.requests).toEqual([]); + expect(result.spooled).toEqual([]); + }); + + it.each(["unreachable", "hanging"] as const)("exits successfully when fetch is %s", async (mode) => { + const result = await runSourceHook(name, payload, { fetch: mode }); + expectExit(result); + expect(result.requests.map((request) => new URL(request.url).pathname)).toEqual(options.requestPaths); + if (options.capture) { + expect(result.spooled).toHaveLength(1); + expect(result.spooled[0]).toMatchObject({ + eventId: result.requests[0]!.body["eventId"], + reason: mode === "unreachable" ? "unreachable" : "timeout", + body: result.requests[0]!.body, + }); + } else { + expect(result.spooled).toEqual([]); + } + }, 10_000); + + it("refuses a secret over non-loopback HTTP without fetching or spooling", async () => { + const result = await runSourceHook(name, payload, { + env: { AGENTMEMORY_URL: "http://untrusted.example:3111" }, + }); + expectExit(result); + expect(result.stderr).toContain("must use HTTPS"); + expect(result.requests).toEqual([]); + expect(result.spooled).toEqual([]); + }); +} diff --git a/test/post-tool-use-source.test.ts b/test/post-tool-use-source.test.ts new file mode 100644 index 0000000000..dde12ab797 --- /dev/null +++ b/test/post-tool-use-source.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from "vitest"; +import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; + +const payload = { + session_id: "source-session", cwd: "/fixture/repo", tool_use_id: "tool-1", tool_name: "Read", + tool_input: { file_path: "/fixture/repo/file.ts" }, tool_response: "Literal tool output.", +}; + +describe("post-tool-use source entrypoint", () => { + it("captures the complete tool payload with host event identity", async () => { + const result = await runSourceHook("post-tool-use", payload); + expectExit(result); + expect(result.requests).toHaveLength(1); + expectPost(result.requests[0], "/agentmemory/observe"); + expect(result.requests[0].body).toEqual({ + hookType: "post_tool_use", sessionId: "source-session", project: "source-hook-project", cwd: "/fixture/repo", + timestamp: expect.any(String), eventId: expect.stringMatching(/^evh_[0-9a-f]{32}$/), + data: { tool_name: "Read", tool_input: { file_path: "/fixture/repo/file.ts" }, tool_output: "Literal tool output." }, + }); + expect(result.spooled).toEqual([]); + }); + + it("keeps memory tool calls out of capture", async () => { + const result = await runSourceHook("post-tool-use", { ...payload, tool_name: "mcp__agentmemory__memory_save" }); + expectExit(result); + expect(result.requests).toEqual([]); + }); + + failureCases("post-tool-use", payload, { capture: true, requestPaths: ["/agentmemory/observe"] }); +}); diff --git a/test/pre-tool-use-source.test.ts b/test/pre-tool-use-source.test.ts new file mode 100644 index 0000000000..b2bebbe885 --- /dev/null +++ b/test/pre-tool-use-source.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from "vitest"; +import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; + +const payload = { + session_id: "source-session", cwd: "/fixture/repo", hook_event_name: "PreToolUse", + tool_name: "Read", tool_input: { file_path: "/fixture/repo/file.ts" }, +}; + +describe("pre-tool-use source entrypoint", () => { + it("requests scoped file context and prints the host envelope", async () => { + const result = await runSourceHook("pre-tool-use", payload); + expectExit(result, JSON.stringify({ hookSpecificOutput: { + hookEventName: "PreToolUse", additionalContext: "source-hook recalled context", + } })); + expect(result.requests).toHaveLength(1); + expectPost(result.requests[0], "/agentmemory/enrich"); + expect(result.requests[0].body).toEqual({ + sessionId: "source-session", files: ["/fixture/repo/file.ts"], terms: [], toolName: "Read", project: "source-hook-project", + }); + expect(result.spooled).toEqual([]); + }); + + it("sends nothing when context injection is disabled", async () => { + const result = await runSourceHook("pre-tool-use", payload, { env: { AGENTMEMORY_INJECT_CONTEXT: "false" } }); + expectExit(result); + expect(result.requests).toEqual([]); + }); + + failureCases("pre-tool-use", payload, { capture: false, requestPaths: ["/agentmemory/enrich"] }); +}); diff --git a/test/prompt-submit-source.test.ts b/test/prompt-submit-source.test.ts new file mode 100644 index 0000000000..12eccd1b30 --- /dev/null +++ b/test/prompt-submit-source.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from "vitest"; +import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; + +const payload = { session_id: "source-session", cwd: "/fixture/repo", prompt: "Keep this literal user prompt.", prompt_id: "prompt-1" }; + +describe("prompt-submit source entrypoint", () => { + it("captures the prompt with session, project and host event identity", async () => { + const result = await runSourceHook("prompt-submit", payload); + expectExit(result); + expect(result.requests).toHaveLength(1); + expectPost(result.requests[0], "/agentmemory/observe"); + expect(result.requests[0].body).toEqual({ + hookType: "prompt_submit", sessionId: "source-session", project: "source-hook-project", cwd: "/fixture/repo", + timestamp: expect.any(String), eventId: expect.stringMatching(/^evh_[0-9a-f]{32}$/), + data: { prompt: "Keep this literal user prompt." }, + }); + expect(result.spooled).toEqual([]); + }); + + failureCases("prompt-submit", payload, { capture: true, requestPaths: ["/agentmemory/observe"] }); +}); diff --git a/test/session-start-source.test.ts b/test/session-start-source.test.ts new file mode 100644 index 0000000000..12e2645d82 --- /dev/null +++ b/test/session-start-source.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from "vitest"; +import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; + +const payload = { session_id: "source-session", cwd: "/fixture/repo", hook_event_name: "SessionStart" }; + +describe("session-start source entrypoint", () => { + it("registers the session and prints the host context envelope", async () => { + const result = await runSourceHook("session-start", payload); + expectExit(result, JSON.stringify({ hookSpecificOutput: { + hookEventName: "SessionStart", additionalContext: "source-hook recalled context", + } })); + expect(result.requests).toHaveLength(1); + expectPost(result.requests[0], "/agentmemory/session/start"); + expect(result.requests[0].body).toEqual({ + sessionId: "source-session", project: "source-hook-project", cwd: "/fixture/repo", + }); + expect(result.spooled).toEqual([]); + }); + + it("registers without printing context when injection is disabled", async () => { + const result = await runSourceHook("session-start", payload, { env: { AGENTMEMORY_INJECT_CONTEXT: "false" } }); + expectExit(result); + expect(result.requests).toHaveLength(1); + expectPost(result.requests[0], "/agentmemory/session/start"); + }); + + failureCases("session-start", payload, { capture: false, requestPaths: ["/agentmemory/session/start"] }); +}); diff --git a/test/stop-source.test.ts b/test/stop-source.test.ts new file mode 100644 index 0000000000..148770f5dc --- /dev/null +++ b/test/stop-source.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, it } from "vitest"; +import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; + +const payload = { session_id: "source-session", cwd: "/fixture/repo", last_assistant_message: "Literal assistant reply." }; + +describe("stop source entrypoint", () => { + it("captures the reply before ending the turn", async () => { + const result = await runSourceHook("stop", payload); + expectExit(result); + expect(result.requests).toHaveLength(2); + expectPost(result.requests[0], "/agentmemory/observe"); + expectPost(result.requests[1], "/agentmemory/session/end"); + expect(result.requests[0].body).toEqual({ + hookType: "stop", sessionId: "source-session", project: "source-hook-project", cwd: "/fixture/repo", + timestamp: expect.any(String), eventId: expect.stringMatching(/^evc_[0-9a-f]{32}$/), + data: { last_assistant_message: "Literal assistant reply." }, + }); + expect(result.requests[1].body).toEqual({ sessionId: "source-session", final: false }); + expect(result.spooled).toEqual([]); + }); + + it("ends a turn without capturing a blank reply", async () => { + const result = await runSourceHook("stop", { ...payload, last_assistant_message: " \n" }); + expectExit(result); + expect(result.requests).toHaveLength(1); + expectPost(result.requests[0], "/agentmemory/session/end"); + expect(result.requests[0].body).toEqual({ sessionId: "source-session", final: false }); + }); + + failureCases("stop", payload, { capture: true, requestPaths: ["/agentmemory/observe", "/agentmemory/session/end"] }); +}); diff --git a/tsconfig.hooks.json b/tsconfig.hooks.json new file mode 100644 index 0000000000..c8c07665e9 --- /dev/null +++ b/tsconfig.hooks.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "noEmit": true, + "types": ["node"] + }, + "include": ["src/hooks/**/*.ts"], + "exclude": ["node_modules", "dist", "test"] +} From 5862d55bbac44b57a0a17ea32eed901e7d2b158a Mon Sep 17 00:00:00 2001 From: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:34:06 +0400 Subject: [PATCH 06/10] test(hooks): record unexpected fetch attempts Signed-off-by: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> --- test/fixtures/hook-source-fetch.mjs | 13 +++++++-- test/hook-source-fetch.test.ts | 41 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/hook-source-fetch.test.ts diff --git a/test/fixtures/hook-source-fetch.mjs b/test/fixtures/hook-source-fetch.mjs index 96c49ed4cf..b268a3992a 100644 --- a/test/fixtures/hook-source-fetch.mjs +++ b/test/fixtures/hook-source-fetch.mjs @@ -1,13 +1,22 @@ import { appendFileSync } from "node:fs"; +function observedBody(body) { + if (typeof body !== "string") return body ?? null; + try { + return JSON.parse(body); + } catch { + return body; + } +} + globalThis.fetch = async (url, init = {}) => { appendFileSync( process.env.AGENTMEMORY_HOOK_TEST_REQUESTS, `${JSON.stringify({ url: String(url), - method: init.method, + method: init.method ?? "GET", headers: Object.fromEntries(new Headers(init.headers)), - body: JSON.parse(init.body), + body: observedBody(init.body), })}\n`, ); diff --git a/test/hook-source-fetch.test.ts b/test/hook-source-fetch.test.ts new file mode 100644 index 0000000000..300d0acb82 --- /dev/null +++ b/test/hook-source-fetch.test.ts @@ -0,0 +1,41 @@ +import { execFileSync } from "node:child_process"; +import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; + +const preload = new URL("./fixtures/hook-source-fetch.mjs", import.meta.url).href; + +describe("source-hook fetch observer", () => { + it.each([ + { name: "an unexpected GET without a body", init: undefined, method: "GET", body: null }, + { name: "an unexpected non-JSON POST", init: { method: "POST", body: "not-json" }, method: "POST", body: "not-json" }, + ])("records $name so a no-fetch assertion cannot miss it", ({ init, method, body }) => { + const dir = mkdtempSync(join(tmpdir(), "am-hook-fetch-")); + const requests = join(dir, "requests.jsonl"); + const url = "http://127.0.0.1:3111/unexpected"; + const program = `await fetch(${JSON.stringify(url)}, ${JSON.stringify(init)}).catch(() => {});`; + try { + execFileSync(process.execPath, ["--import", preload, "--input-type=module", "-e", program], { + cwd: fileURLToPath(new URL("../", import.meta.url)), + env: { + PATH: process.env["PATH"], + SystemRoot: process.env["SystemRoot"], + HOME: dir, + USERPROFILE: dir, + AGENTMEMORY_HOOK_TEST_FETCH: "success", + AGENTMEMORY_HOOK_TEST_REQUESTS: requests, + }, + timeout: 5000, + stdio: "pipe", + }); + const observed: unknown[] = existsSync(requests) + ? readFileSync(requests, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line)) + : []; + expect(observed).toEqual([{ url, method, headers: {}, body }]); + } finally { + rmSync(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 }); + } + }); +}); From 5f06a98778db6126cfda4e27efa611c66fd24d11 Mon Sep 17 00:00:00 2001 From: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:12:05 +0400 Subject: [PATCH 07/10] fix(audit): preserve viewer events with independent listeners Signed-off-by: zack-dev-cm <112114097+zack-dev-cm@users.noreply.github.com> --- docs/audit-listeners.md | 36 +++ src/functions/audit.ts | 26 +- test/audit-listeners.test.ts | 483 +++++++++++++++++++++++++++++++++++ 3 files changed, 540 insertions(+), 5 deletions(-) create mode 100644 docs/audit-listeners.md create mode 100644 test/audit-listeners.test.ts diff --git a/docs/audit-listeners.md b/docs/audit-listeners.md new file mode 100644 index 0000000000..281c0ed279 --- /dev/null +++ b/docs/audit-listeners.md @@ -0,0 +1,36 @@ +# Audit listeners + +Audit consumers can subscribe independently with `addAuditListener(listener)` and +unsubscribe with `removeAuditListener(listener)`, exported from +`src/functions/audit.ts`. Keep the same function reference for removal. Registering +the same function twice or removing it more than once is harmless. + +`setAuditRecordedListener(listener)` remains a separate, replaceable legacy slot +used by the viewer. Setting that slot to `null` does not remove independent +subscribers. Removing an independent subscription does not clear the legacy slot, +even when both contain the same function. A function registered through both +mechanisms is called once for each recorded entry. + +`recordAudit()` invokes listeners after its monthly entry write and month-index +update complete. It calls the legacy listener first, followed by independent +listeners in registration order. Removing and registering a listener again moves +it to the end of that order. + +Dispatch uses a snapshot taken after persistence completes. A callback added +during dispatch starts with the next entry; a callback removed during dispatch +still receives the current entry. Legacy replacement or cleanup during dispatch +also takes effect on the next entry. + +The callback type keeps TypeScript's `void` convention, so existing value-returning +handlers such as `(entry) => rows.push(entry)` remain accepted. Return values are +not used, and returned promises have rejection handlers attached. A synchronous +exception or rejected promise in one callback does not prevent the other callbacks +from running or make an already written audit entry fail. Promise completion is +not awaited, so async work can overlap across entries. Callbacks should avoid +blocking synchronous work. Failure of a required entry or month-index operation +still rejects `recordAudit()` before listener delivery; `safeAudit()` keeps its +existing logging boundary. + +Delivery is best effort within the current process. There is no retry queue, +cursor, or durable delivery guarantee. This listener API does not add file/stdout +sinks, backfill exports, or an export payload/deletion policy. diff --git a/src/functions/audit.ts b/src/functions/audit.ts index 3670131109..17322d385e 100644 --- a/src/functions/audit.ts +++ b/src/functions/audit.ts @@ -153,12 +153,23 @@ export async function listAuditMonthsDesc(kv: StateKV): Promise { return [...months].sort().reverse(); } -let auditRecordedListener: ((entry: AuditEntry) => void) | null = null; +export type AuditListener = (entry: AuditEntry) => void; -export function setAuditRecordedListener(listener: ((entry: AuditEntry) => void) | null): void { +let auditRecordedListener: AuditListener | null = null; +const auditListeners = new Set(); + +export function setAuditRecordedListener(listener: AuditListener | null): void { auditRecordedListener = listener; } +export function addAuditListener(listener: AuditListener): void { + auditListeners.add(listener); +} + +export function removeAuditListener(listener: AuditListener): void { + auditListeners.delete(listener); +} + export async function recordAudit( kv: StateKV, operation: AuditEntry["operation"], @@ -181,9 +192,14 @@ export async function recordAudit( const month = auditMonthOf(entry.timestamp); await kv.set(KV.auditMonth(month), entry.id, entry); await markAuditMonth(kv, month); - try { - auditRecordedListener?.(entry); - } catch {} + const listeners = new Set(auditRecordedListener + ? [auditRecordedListener, ...auditListeners] + : auditListeners); + for (const listener of listeners) { + try { + void Promise.resolve(listener(entry)).catch(() => {}); + } catch {} + } return entry; } diff --git a/test/audit-listeners.test.ts b/test/audit-listeners.test.ts new file mode 100644 index 0000000000..9272045512 --- /dev/null +++ b/test/audit-listeners.test.ts @@ -0,0 +1,483 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import * as audit from "../src/functions/audit.js"; +import { KV } from "../src/state/schema.js"; +import { resetViewerCounts } from "../src/state/viewer-counts.js"; +import { registerViewerStreamTriggers } from "../src/triggers/viewer-streams.js"; +import { onLiveEvent, type LiveEvent } from "../src/viewer/live-events.js"; +import type { AuditEntry } from "../src/types.js"; + +type Listener = (entry: AuditEntry) => void | Promise; +const cleanups: Array<() => void> = []; + +function memoryKV() { + const store = new Map>(); + return { + store, + get: vi.fn(async (scope: string, key: string) => store.get(scope)?.get(key) ?? null), + set: vi.fn(async (scope: string, key: string, value: unknown) => { + if (!store.has(scope)) store.set(scope, new Map()); + store.get(scope)!.set(key, structuredClone(value)); + }), + delete: vi.fn(async (scope: string, key: string) => { store.get(scope)?.delete(key); }), + list: vi.fn(async (scope: string) => Array.from(store.get(scope)?.values() ?? [])), + }; +} + +function subscribe(listener: Listener): void { + if ("addAuditListener" in audit && typeof audit.addAuditListener === "function") { + audit.addAuditListener(listener); + cleanups.push(() => { + if ("removeAuditListener" in audit && typeof audit.removeAuditListener === "function") { + audit.removeAuditListener(listener); + } + }); + } else { + audit.setAuditRecordedListener(listener); + cleanups.push(() => audit.setAuditRecordedListener(null)); + } +} + +function viewer(kv: ReturnType) { + const events: LiveEvent[] = []; + cleanups.push(onLiveEvent((event) => events.push(event))); + const sdk = { + registerFunction: vi.fn(), + registerTrigger: vi.fn(), + trigger: vi.fn(async () => ({})), + }; + registerViewerStreamTriggers(sdk as never, kv as never); + return events; +} + +afterEach(() => { + cleanups.splice(0).reverse().forEach((cleanup) => cleanup()); + audit.setAuditRecordedListener(null); + resetViewerCounts(); +}); + +describe("audit listener coexistence through persisted records and real viewer wiring", () => { + it("keeps the viewer when another consumer subscribes after it", async () => { + const kv = memoryKV(); + const events = viewer(kv); + const received: AuditEntry[] = []; + subscribe((entry) => { received.push(entry); }); + const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-one"], { reason: "requested" }); + expect(received).toEqual([entry]); + expect(events.map((event) => ({ type: event.type, data: event.data }))).toEqual([ + { type: "audit.created", data: { id: entry.id, audit: entry } }, + ]); + expect(kv.store.get(KV.auditMonth(audit.auditMonthOf(entry.timestamp)))?.get(entry.id)).toEqual(entry); + }); + + it("keeps an earlier independent consumer when the viewer registers", async () => { + const kv = memoryKV(); + const received: AuditEntry[] = []; + subscribe((entry) => { received.push(entry); }); + const events = viewer(kv); + const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-two"]); + expect(events.map((event) => event.data.audit)).toEqual([entry]); + expect(received).toEqual([entry]); + expect(kv.store.get(KV.auditMonth(audit.auditMonthOf(entry.timestamp)))?.get(entry.id)).toEqual(entry); + }); + + it("preserves the existing viewer event and monthly persistence without a second consumer", async () => { + const kv = memoryKV(); + const events = viewer(kv); + const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-three"], { source: "control" }, 0.7, "test-user"); + const month = audit.auditMonthOf(entry.timestamp); + expect(events).toHaveLength(1); + expect(events[0].type).toBe("audit.created"); + expect(events[0].data).toEqual({ id: entry.id, audit: entry }); + expect(entry).toMatchObject({ operation: "forget", functionId: "mem::forget", targetIds: ["memory-three"], details: { source: "control" }, qualityScore: 0.7, userId: "test-user" }); + expect(kv.set.mock.calls).toEqual([ + [KV.auditMonth(month), entry.id, entry], + [KV.auditMonths, "index", { months: [month] }], + ]); + }); + + it("preserves legacy setter replacement and null cleanup", async () => { + const kv = memoryKV(); + const first = vi.fn(); + const replacement = vi.fn(); + audit.setAuditRecordedListener(first); + audit.setAuditRecordedListener(replacement); + const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-four"]); + expect(first).not.toHaveBeenCalled(); + expect(replacement.mock.calls).toEqual([[entry]]); + audit.setAuditRecordedListener(null); + await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-five"]); + expect(first).not.toHaveBeenCalled(); + expect(replacement.mock.calls).toEqual([[entry]]); + expect(kv.set).toHaveBeenCalledTimes(3); + }); +}); + +function deferred() { + let resolve: () => void = () => {}; + let reject: (error: Error) => void = () => {}; + const promise = new Promise((accept, refuse) => { + resolve = accept; + reject = refuse; + }); + return { promise, resolve, reject }; +} + +describe("independent audit listener lifecycle", () => { + it("deduplicates function identity and makes repeated removal harmless", async () => { + const kv = memoryKV(); + const listener = vi.fn(); + subscribe(listener); + audit.addAuditListener(listener); + const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + expect(listener.mock.calls).toEqual([[entry]]); + audit.removeAuditListener(listener); + audit.removeAuditListener(listener); + await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); + expect(listener.mock.calls).toEqual([[entry]]); + }); + + it("removes only the selected subscriber and ignores an absent function", async () => { + const kv = memoryKV(); + const removed = vi.fn(); + const retained = vi.fn(); + subscribe(removed); + subscribe(retained); + audit.removeAuditListener(removed); + audit.removeAuditListener(() => {}); + const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + expect(removed).not.toHaveBeenCalled(); + expect(retained.mock.calls).toEqual([[entry]]); + }); + + it("replaces and clears only the legacy slot while preserving independent subscribers", async () => { + const kv = memoryKV(); + const independent = vi.fn(); + const oldLegacy = vi.fn(); + const replacement = vi.fn(); + subscribe(independent); + audit.setAuditRecordedListener(oldLegacy); + audit.setAuditRecordedListener(replacement); + const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + audit.setAuditRecordedListener(null); + const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); + expect(oldLegacy).not.toHaveBeenCalled(); + expect(replacement.mock.calls).toEqual([[first]]); + expect(independent.mock.calls).toEqual([[first], [second]]); + }); + + it("invokes a function shared by both slots once and removes each registration independently", async () => { + const kv = memoryKV(); + const shared = vi.fn(); + subscribe(shared); + audit.setAuditRecordedListener(shared); + const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + audit.removeAuditListener(shared); + const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); + audit.setAuditRecordedListener(null); + await audit.recordAudit(kv as never, "forget", "mem::forget", ["three"]); + expect(shared.mock.calls).toEqual([[first], [second]]); + }); + + it("invokes the legacy callback first followed by independent insertion order", async () => { + const kv = memoryKV(); + const calls: string[] = []; + subscribe(() => { calls.push("first"); }); + subscribe(() => { calls.push("second"); }); + audit.setAuditRecordedListener(() => { calls.push("legacy"); }); + await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + expect(calls).toEqual(["legacy", "first", "second"]); + }); + + it("moves a removed and registered subscriber to the end on the next entry", async () => { + const kv = memoryKV(); + const calls: string[] = []; + const first = () => { calls.push("first"); }; + subscribe(first); + subscribe(() => { calls.push("second"); }); + audit.removeAuditListener(first); + audit.addAuditListener(first); + await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + expect(calls).toEqual(["second", "first"]); + }); + + it("uses a dispatch snapshot when a callback removes a later subscriber and adds another", async () => { + const kv = memoryKV(); + const calls: string[] = []; + const later = () => { calls.push("later"); }; + const added = () => { calls.push("added"); }; + subscribe(() => { + calls.push("first"); + audit.removeAuditListener(later); + audit.addAuditListener(added); + }); + subscribe(later); + cleanups.push(() => audit.removeAuditListener(added)); + await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + expect(calls).toEqual(["first", "later"]); + calls.length = 0; + await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); + expect(calls).toEqual(["first", "added"]); + }); + + it("applies a legacy replacement during dispatch only to the next entry", async () => { + const kv = memoryKV(); + const calls: string[] = []; + subscribe(() => { calls.push("independent"); }); + audit.setAuditRecordedListener(() => { + calls.push("old-legacy"); + audit.setAuditRecordedListener(() => { calls.push("new-legacy"); }); + }); + await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); + expect(calls).toEqual(["old-legacy", "independent", "new-legacy", "independent"]); + }); + + it("does not invoke a removed and re-added callback twice in the current dispatch", async () => { + const kv = memoryKV(); + const calls: string[] = []; + const moved = () => { calls.push("moved"); }; + let change = true; + subscribe(() => { + calls.push("first"); + if (change) { + change = false; + audit.removeAuditListener(moved); + audit.addAuditListener(moved); + } + }); + subscribe(moved); + subscribe(() => { calls.push("last"); }); + await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + expect(calls).toEqual(["first", "moved", "last"]); + calls.length = 0; + await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); + expect(calls).toEqual(["first", "last", "moved"]); + }); + + it("isolates synchronous exceptions in both slots without losing another callback or the record", async () => { + const kv = memoryKV(); + const retained = vi.fn(); + audit.setAuditRecordedListener(() => { throw new Error("legacy failed"); }); + subscribe(() => { throw new Error("subscriber failed"); }); + subscribe(retained); + const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + expect(retained.mock.calls).toEqual([[entry]]); + expect(kv.store.get(KV.auditMonth(audit.auditMonthOf(entry.timestamp)))?.get(entry.id)).toEqual(entry); + expect(kv.set).toHaveBeenCalledTimes(2); + }); + + it("observes rejected async callbacks in both slots without unhandled rejection or lost delivery", async () => { + const kv = memoryKV(); + const unhandled = vi.fn(); + process.on("unhandledRejection", unhandled); + cleanups.push(() => { process.off("unhandledRejection", unhandled); }); + audit.setAuditRecordedListener(async () => { throw new Error("legacy async failed"); }); + subscribe(async () => { throw new Error("subscriber async failed"); }); + const retained = vi.fn(); + subscribe(retained); + const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + await new Promise((resolve) => setImmediate(resolve)); + expect(unhandled).not.toHaveBeenCalled(); + expect(retained.mock.calls).toEqual([[entry]]); + expect(kv.set).toHaveBeenCalledTimes(2); + }); + + it("observes a callback promise that rejects after recordAudit has returned", async () => { + const kv = memoryKV(); + const unhandled = vi.fn(); + process.on("unhandledRejection", unhandled); + cleanups.push(() => { process.off("unhandledRejection", unhandled); }); + const pending = deferred(); + subscribe(() => pending.promise); + const retained = vi.fn(); + subscribe(retained); + const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + pending.reject(new Error("late subscriber failure")); + await new Promise((resolve) => setImmediate(resolve)); + expect(unhandled).not.toHaveBeenCalled(); + expect(retained.mock.calls).toEqual([[entry]]); + expect(kv.store.get(KV.auditMonth(audit.auditMonthOf(entry.timestamp)))?.get(entry.id)).toEqual(entry); + }); + + it("does not await a pending callback or block the following subscriber", async () => { + const kv = memoryKV(); + const pending = deferred(); + subscribe(() => pending.promise); + const retained = vi.fn(); + subscribe(retained); + const recording = audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + try { + const outcome = await Promise.race([ + recording.then(() => "recorded"), + new Promise((resolve) => setImmediate(() => resolve("still-pending"))), + ]); + expect(outcome).toBe("recorded"); + expect(retained).toHaveBeenCalledTimes(1); + } finally { + pending.resolve(); + await recording; + } + }); + + it("does not deliver after an entry write failure and preserves the safeAudit logging boundary", async () => { + const kv = memoryKV(); + const failure = new Error("entry write denied"); + kv.set.mockRejectedValue(failure); + const legacy = vi.fn(); + const independent = vi.fn(); + audit.setAuditRecordedListener(legacy); + subscribe(independent); + const { logger } = await import("../src/logger.js"); + const warning = vi.spyOn(logger, "warn").mockImplementation(() => {}); + cleanups.push(() => { warning.mockRestore(); }); + await expect(audit.recordAudit(kv as never, "forget", "mem::forget", ["one"])).rejects.toBe(failure); + await expect(audit.safeAudit(kv as never, "forget", "mem::forget", ["two"])).resolves.toBeUndefined(); + expect(legacy).not.toHaveBeenCalled(); + expect(independent).not.toHaveBeenCalled(); + expect(kv.get).not.toHaveBeenCalled(); + expect(kv.store.size).toBe(0); + expect(warning.mock.calls).toEqual([["audit write failed", { functionId: "mem::forget", operation: "forget", targetIds: ["two"], error: "entry write denied" }]]); + }); + + it("does not deliver after an index read fails even though the entry write succeeded", async () => { + const kv = memoryKV(); + const failure = new Error("month index unreadable"); + kv.get.mockRejectedValueOnce(failure); + const listener = vi.fn(); + subscribe(listener); + await expect(audit.recordAudit(kv as never, "forget", "mem::forget", ["one"])).rejects.toBe(failure); + expect(listener).not.toHaveBeenCalled(); + expect(kv.set).toHaveBeenCalledTimes(1); + const [scope, id, entry] = kv.set.mock.calls[0]; + expect(scope).toMatch(/^mem:audit:\d{4}-\d{2}$/); + expect(kv.store.get(scope)?.get(id)).toEqual(entry); + expect(kv.store.has(KV.auditMonths)).toBe(false); + }); + + it("does not deliver after the month index write fails or hide the already written entry", async () => { + const kv = memoryKV(); + const failure = new Error("month index write denied"); + const persist = kv.set.getMockImplementation()!; + kv.set.mockImplementation(async (scope, key, value) => { + if (scope === KV.auditMonths) throw failure; + await persist(scope, key, value); + }); + const legacy = vi.fn(); + const independent = vi.fn(); + audit.setAuditRecordedListener(legacy); + subscribe(independent); + await expect(audit.recordAudit(kv as never, "forget", "mem::forget", ["one"])).rejects.toBe(failure); + expect(legacy).not.toHaveBeenCalled(); + expect(independent).not.toHaveBeenCalled(); + expect(kv.set).toHaveBeenCalledTimes(2); + const [scope, id, entry] = kv.set.mock.calls[0]; + expect(kv.store.get(scope)?.get(id)).toEqual(entry); + expect(kv.store.has(KV.auditMonths)).toBe(false); + }); + + it("delivers only after the month index write has completed", async () => { + const kv = memoryKV(); + const atIndex = deferred(); + const releaseIndex = deferred(); + const persist = kv.set.getMockImplementation()!; + kv.set.mockImplementation(async (scope, key, value) => { + if (scope === KV.auditMonths) { + atIndex.resolve(); + await releaseIndex.promise; + } + await persist(scope, key, value); + }); + const observedIndexes: unknown[] = []; + const listener = vi.fn(() => { + observedIndexes.push(structuredClone(kv.store.get(KV.auditMonths)?.get("index"))); + }); + subscribe(listener); + const recording = audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + try { + await atIndex.promise; + expect(listener).not.toHaveBeenCalled(); + expect(kv.set).toHaveBeenCalledTimes(2); + } finally { + releaseIndex.resolve(); + } + const entry = await recording; + expect(listener.mock.calls).toEqual([[entry]]); + expect(observedIndexes).toEqual([{ months: [audit.auditMonthOf(entry.timestamp)] }]); + }); + + it("takes the listener snapshot after pending persistence completes", async () => { + const kv = memoryKV(); + const atIndex = deferred(); + const releaseIndex = deferred(); + const persist = kv.set.getMockImplementation()!; + kv.set.mockImplementation(async (scope, key, value) => { + if (scope === KV.auditMonths) { + atIndex.resolve(); + await releaseIndex.promise; + } + await persist(scope, key, value); + }); + const oldLegacy = vi.fn(); + const newLegacy = vi.fn(); + const added = vi.fn(); + audit.setAuditRecordedListener(oldLegacy); + const recording = audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + try { + await atIndex.promise; + audit.setAuditRecordedListener(newLegacy); + subscribe(added); + } finally { + releaseIndex.resolve(); + } + const entry = await recording; + expect(oldLegacy).not.toHaveBeenCalled(); + expect(newLegacy.mock.calls).toEqual([[entry]]); + expect(added.mock.calls).toEqual([[entry]]); + }); + + it("delivers each record once when its month is already indexed without an extra index write", async () => { + const kv = memoryKV(); + const listener = vi.fn(); + subscribe(listener); + const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); + expect(listener.mock.calls).toEqual([[first], [second]]); + expect(kv.set.mock.calls.map(([scope]) => scope)).toEqual([ + KV.auditMonth(audit.auditMonthOf(first.timestamp)), KV.auditMonths, KV.auditMonth(audit.auditMonthOf(second.timestamp)), + ]); + }); + + it("keeps independent delivery through viewer re-registration and legacy viewer cleanup", async () => { + const kv = memoryKV(); + const listener = vi.fn(); + subscribe(listener); + const firstViewer = viewer(kv); + const secondViewer = viewer(kv); + const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + expect(firstViewer.map((event) => event.data.audit)).toEqual([first]); + expect(secondViewer.map((event) => event.data.audit)).toEqual([first]); + audit.setAuditRecordedListener(null); + const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); + expect(firstViewer.map((event) => event.data.audit)).toEqual([first]); + expect(secondViewer.map((event) => event.data.audit)).toEqual([first]); + expect(listener.mock.calls).toEqual([[first], [second]]); + }); +}); + +describe("legacy audit callback source compatibility", () => { + it("accepts value-returning legacy and independent callbacks including an async legacy callback", async () => { + const kv = memoryKV(); + const legacyRows: AuditEntry[] = []; + const independentRows: AuditEntry[] = []; + const independent = (entry: AuditEntry) => independentRows.push(entry); + audit.addAuditListener(independent); + cleanups.push(() => audit.removeAuditListener(independent)); + audit.setAuditRecordedListener((entry) => legacyRows.push(entry)); + const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); + audit.setAuditRecordedListener(async (entry) => legacyRows.push(entry)); + const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); + await new Promise((resolve) => setImmediate(resolve)); + expect(legacyRows).toEqual([first, second]); + expect(independentRows).toEqual([first, second]); + }); +}); From a4bd282b527a550d31cd4971ae75e2b6bb1436d3 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 18 Nov 2026 10:01:12 +0530 Subject: [PATCH 08/10] fix: tighten merged contributor changes and trim their tests - consolidation status: count summaries through StateKV.count instead of a copy of it. - audit listeners: addAuditListener returns its unsubscribe function, entries are frozen copies of the caller's targetIds and details so one listener cannot change what another sees, and listener failures are logged instead of dropped. Removed the internal-only doc. - hooks typecheck: run once in CI (ubuntu, Node 22) instead of in all eight matrix cells. Dropped the tsx source-hook spawn tests: the packaged hooks are already tested, and the HTTPS refusal is covered in directory-review-privacy.test.ts. - Replaced the oversized tests and fixtures for the summary count, durable replay lookup, insight paging and audit listeners with focused cases. Signed-off-by: Rohit Ghumare --- .github/workflows/ci.yml | 3 +- docs/audit-listeners.md | 36 - src/functions/audit.ts | 24 +- src/triggers/api.ts | 16 +- test/audit-listeners.test.ts | 493 +--------- test/consolidation-summary-counts.test.ts | 49 +- test/fixtures/hook-source-fetch.mjs | 41 - test/fixtures/insight-list-memory.ts | 94 -- test/fixtures/insight-list.json | 1061 --------------------- test/helpers/hook-source.ts | 180 ---- test/hook-source-fetch.test.ts | 41 - test/insight-list.test.ts | 130 +-- test/observe-durable-lookup.test.ts | 92 +- test/post-tool-use-source.test.ts | 30 - test/pre-tool-use-source.test.ts | 30 - test/prompt-submit-source.test.ts | 21 - test/session-start-source.test.ts | 28 - test/stop-source.test.ts | 31 - 18 files changed, 121 insertions(+), 2279 deletions(-) delete mode 100644 docs/audit-listeners.md delete mode 100644 test/fixtures/hook-source-fetch.mjs delete mode 100644 test/fixtures/insight-list-memory.ts delete mode 100644 test/fixtures/insight-list.json delete mode 100644 test/helpers/hook-source.ts delete mode 100644 test/hook-source-fetch.test.ts delete mode 100644 test/post-tool-use-source.test.ts delete mode 100644 test/pre-tool-use-source.test.ts delete mode 100644 test/prompt-submit-source.test.ts delete mode 100644 test/session-start-source.test.ts delete mode 100644 test/stop-source.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c5d98bb8ce..b33a27aca5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,9 +71,10 @@ jobs: # in one pass. - run: npm install --legacy-peer-deps --no-audit --no-fund - run: npm run build - - run: npm run typecheck:hooks - run: npm run skills:check - if: matrix.os == 'ubuntu-latest' && matrix.node-version == 22 run: npm run docs:check + - if: matrix.os == 'ubuntu-latest' && matrix.node-version == 22 + run: npm run typecheck:hooks - run: npx tsc --noEmit --project tsconfig.eval.json - run: npm test diff --git a/docs/audit-listeners.md b/docs/audit-listeners.md deleted file mode 100644 index 281c0ed279..0000000000 --- a/docs/audit-listeners.md +++ /dev/null @@ -1,36 +0,0 @@ -# Audit listeners - -Audit consumers can subscribe independently with `addAuditListener(listener)` and -unsubscribe with `removeAuditListener(listener)`, exported from -`src/functions/audit.ts`. Keep the same function reference for removal. Registering -the same function twice or removing it more than once is harmless. - -`setAuditRecordedListener(listener)` remains a separate, replaceable legacy slot -used by the viewer. Setting that slot to `null` does not remove independent -subscribers. Removing an independent subscription does not clear the legacy slot, -even when both contain the same function. A function registered through both -mechanisms is called once for each recorded entry. - -`recordAudit()` invokes listeners after its monthly entry write and month-index -update complete. It calls the legacy listener first, followed by independent -listeners in registration order. Removing and registering a listener again moves -it to the end of that order. - -Dispatch uses a snapshot taken after persistence completes. A callback added -during dispatch starts with the next entry; a callback removed during dispatch -still receives the current entry. Legacy replacement or cleanup during dispatch -also takes effect on the next entry. - -The callback type keeps TypeScript's `void` convention, so existing value-returning -handlers such as `(entry) => rows.push(entry)` remain accepted. Return values are -not used, and returned promises have rejection handlers attached. A synchronous -exception or rejected promise in one callback does not prevent the other callbacks -from running or make an already written audit entry fail. Promise completion is -not awaited, so async work can overlap across entries. Callbacks should avoid -blocking synchronous work. Failure of a required entry or month-index operation -still rejects `recordAudit()` before listener delivery; `safeAudit()` keeps its -existing logging boundary. - -Delivery is best effort within the current process. There is no retry queue, -cursor, or durable delivery guarantee. This listener API does not add file/stdout -sinks, backfill exports, or an export payload/deletion policy. diff --git a/src/functions/audit.ts b/src/functions/audit.ts index 17322d385e..549df78d90 100644 --- a/src/functions/audit.ts +++ b/src/functions/audit.ts @@ -162,12 +162,15 @@ export function setAuditRecordedListener(listener: AuditListener | null): void { auditRecordedListener = listener; } -export function addAuditListener(listener: AuditListener): void { +export function addAuditListener(listener: AuditListener): () => void { auditListeners.add(listener); + return () => { + auditListeners.delete(listener); + }; } -export function removeAuditListener(listener: AuditListener): void { - auditListeners.delete(listener); +function reportListenerFailure(err: unknown): void { + logger.warn("Audit listener failed", { error: err instanceof Error ? err.message : String(err) }); } export async function recordAudit( @@ -185,20 +188,21 @@ export async function recordAudit( operation, userId, functionId, - targetIds, - details, + targetIds: Object.freeze([...targetIds]) as string[], + details: Object.freeze({ ...details }), qualityScore, }; + Object.freeze(entry); const month = auditMonthOf(entry.timestamp); await kv.set(KV.auditMonth(month), entry.id, entry); await markAuditMonth(kv, month); - const listeners = new Set(auditRecordedListener - ? [auditRecordedListener, ...auditListeners] - : auditListeners); + const listeners = auditRecordedListener ? [auditRecordedListener, ...auditListeners] : [...auditListeners]; for (const listener of listeners) { try { - void Promise.resolve(listener(entry)).catch(() => {}); - } catch {} + void Promise.resolve(listener(entry)).catch(reportListenerFailure); + } catch (err) { + reportListenerFailure(err); + } } return entry; } diff --git a/src/triggers/api.ts b/src/triggers/api.ts index 38b32ec9b5..dabdd4f154 100644 --- a/src/triggers/api.ts +++ b/src/triggers/api.ts @@ -482,18 +482,13 @@ export function createStatusReporter(sdk: IIIClient, kv: StateKV, deps: StatusRe } async function countConsolidationSummaries(kv: StateKV): Promise { - if (typeof kv.listKeys === "function") { - try { - return (await kv.listKeys(KV.summaries)).length; - } catch {} - } - return (await kv.list(KV.summaries).catch(() => [])).length; + return typeof kv.count === "function" ? kv.count(KV.summaries) : (await kv.list(KV.summaries)).length; } export function createConsolidationStatusReader(kv: StateKV) { const counts = singleFlight(async () => { const [summaries, memories, semantic, procedural, relations] = await Promise.all([ - countConsolidationSummaries(kv), + countConsolidationSummaries(kv).catch(() => 0), collectAll(kv, KV.memories).catch(() => []), collectAll(kv, KV.semantic).catch(() => []), collectAll(kv, KV.procedural).catch(() => []), @@ -3834,12 +3829,17 @@ export function registerApiTriggers( runId?: string; actionIds?: string[]; project?: string; + agentId?: string; }>, ): Promise => { const authErr = checkAuth(req, secret); if (authErr) return authErr; + const body = req.body ?? {}; try { - const result = await sdk.trigger({ function_id: "mem::flow-compress", payload: req.body || {} }); + const result = await sdk.trigger({ + function_id: "mem::flow-compress", + payload: { runId: body.runId, actionIds: body.actionIds, project: body.project, agentId: body.agentId }, + }); return { status_code: 200, body: result }; } catch { return { diff --git a/test/audit-listeners.test.ts b/test/audit-listeners.test.ts index 9272045512..d32a6a5e2e 100644 --- a/test/audit-listeners.test.ts +++ b/test/audit-listeners.test.ts @@ -1,483 +1,58 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import * as audit from "../src/functions/audit.js"; -import { KV } from "../src/state/schema.js"; -import { resetViewerCounts } from "../src/state/viewer-counts.js"; -import { registerViewerStreamTriggers } from "../src/triggers/viewer-streams.js"; -import { onLiveEvent, type LiveEvent } from "../src/viewer/live-events.js"; +import { addAuditListener, recordAudit, setAuditRecordedListener } from "../src/functions/audit.js"; import type { AuditEntry } from "../src/types.js"; -type Listener = (entry: AuditEntry) => void | Promise; -const cleanups: Array<() => void> = []; +vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } })); -function memoryKV() { +function memoryKV(failWrites = false) { const store = new Map>(); return { - store, - get: vi.fn(async (scope: string, key: string) => store.get(scope)?.get(key) ?? null), - set: vi.fn(async (scope: string, key: string, value: unknown) => { + get: async (scope: string, key: string) => store.get(scope)?.get(key) ?? null, + set: async (scope: string, key: string, value: unknown) => { + if (failWrites) throw new Error("write failed"); if (!store.has(scope)) store.set(scope, new Map()); store.get(scope)!.set(key, structuredClone(value)); - }), - delete: vi.fn(async (scope: string, key: string) => { store.get(scope)?.delete(key); }), - list: vi.fn(async (scope: string) => Array.from(store.get(scope)?.values() ?? [])), - }; -} - -function subscribe(listener: Listener): void { - if ("addAuditListener" in audit && typeof audit.addAuditListener === "function") { - audit.addAuditListener(listener); - cleanups.push(() => { - if ("removeAuditListener" in audit && typeof audit.removeAuditListener === "function") { - audit.removeAuditListener(listener); - } - }); - } else { - audit.setAuditRecordedListener(listener); - cleanups.push(() => audit.setAuditRecordedListener(null)); - } -} - -function viewer(kv: ReturnType) { - const events: LiveEvent[] = []; - cleanups.push(onLiveEvent((event) => events.push(event))); - const sdk = { - registerFunction: vi.fn(), - registerTrigger: vi.fn(), - trigger: vi.fn(async () => ({})), + }, + delete: async () => {}, + list: async (scope: string) => Array.from(store.get(scope)?.values() ?? []), }; - registerViewerStreamTriggers(sdk as never, kv as never); - return events; } +const cleanups: Array<() => void> = []; afterEach(() => { - cleanups.splice(0).reverse().forEach((cleanup) => cleanup()); - audit.setAuditRecordedListener(null); - resetViewerCounts(); + cleanups.splice(0).forEach((cleanup) => cleanup()); + setAuditRecordedListener(null); }); -describe("audit listener coexistence through persisted records and real viewer wiring", () => { - it("keeps the viewer when another consumer subscribes after it", async () => { - const kv = memoryKV(); - const events = viewer(kv); - const received: AuditEntry[] = []; - subscribe((entry) => { received.push(entry); }); - const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-one"], { reason: "requested" }); - expect(received).toEqual([entry]); - expect(events.map((event) => ({ type: event.type, data: event.data }))).toEqual([ - { type: "audit.created", data: { id: entry.id, audit: entry } }, - ]); - expect(kv.store.get(KV.auditMonth(audit.auditMonthOf(entry.timestamp)))?.get(entry.id)).toEqual(entry); +describe("audit listeners", () => { + it("delivers each entry to the viewer slot and every added listener until it unsubscribes", async () => { + const viewer: string[] = []; + const sink: string[] = []; + setAuditRecordedListener((entry) => viewer.push(entry.id)); + const unsubscribe = addAuditListener((entry) => sink.push(entry.id)); + const first = await recordAudit(memoryKV() as never, "remember", "mem::test", ["mem_a"]); + unsubscribe(); + const second = await recordAudit(memoryKV() as never, "remember", "mem::test", ["mem_b"]); + expect(viewer).toEqual([first.id, second.id]); + expect(sink).toEqual([first.id]); }); - it("keeps an earlier independent consumer when the viewer registers", async () => { - const kv = memoryKV(); + it("isolates failing listeners and hands out a frozen entry", async () => { const received: AuditEntry[] = []; - subscribe((entry) => { received.push(entry); }); - const events = viewer(kv); - const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-two"]); - expect(events.map((event) => event.data.audit)).toEqual([entry]); + cleanups.push(addAuditListener(() => { throw new Error("sync"); })); + cleanups.push(addAuditListener(async () => { throw new Error("async"); })); + cleanups.push(addAuditListener((entry) => { received.push(entry); })); + const details = { reason: "test" }; + const entry = await recordAudit(memoryKV() as never, "forget", "mem::test", ["mem_a"], details); expect(received).toEqual([entry]); - expect(kv.store.get(KV.auditMonth(audit.auditMonthOf(entry.timestamp)))?.get(entry.id)).toEqual(entry); - }); - - it("preserves the existing viewer event and monthly persistence without a second consumer", async () => { - const kv = memoryKV(); - const events = viewer(kv); - const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-three"], { source: "control" }, 0.7, "test-user"); - const month = audit.auditMonthOf(entry.timestamp); - expect(events).toHaveLength(1); - expect(events[0].type).toBe("audit.created"); - expect(events[0].data).toEqual({ id: entry.id, audit: entry }); - expect(entry).toMatchObject({ operation: "forget", functionId: "mem::forget", targetIds: ["memory-three"], details: { source: "control" }, qualityScore: 0.7, userId: "test-user" }); - expect(kv.set.mock.calls).toEqual([ - [KV.auditMonth(month), entry.id, entry], - [KV.auditMonths, "index", { months: [month] }], - ]); - }); - - it("preserves legacy setter replacement and null cleanup", async () => { - const kv = memoryKV(); - const first = vi.fn(); - const replacement = vi.fn(); - audit.setAuditRecordedListener(first); - audit.setAuditRecordedListener(replacement); - const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-four"]); - expect(first).not.toHaveBeenCalled(); - expect(replacement.mock.calls).toEqual([[entry]]); - audit.setAuditRecordedListener(null); - await audit.recordAudit(kv as never, "forget", "mem::forget", ["memory-five"]); - expect(first).not.toHaveBeenCalled(); - expect(replacement.mock.calls).toEqual([[entry]]); - expect(kv.set).toHaveBeenCalledTimes(3); + expect(Object.isFrozen(entry) && Object.isFrozen(entry.details) && Object.isFrozen(entry.targetIds)).toBe(true); + expect(Object.isFrozen(details)).toBe(false); }); -}); -function deferred() { - let resolve: () => void = () => {}; - let reject: (error: Error) => void = () => {}; - const promise = new Promise((accept, refuse) => { - resolve = accept; - reject = refuse; - }); - return { promise, resolve, reject }; -} - -describe("independent audit listener lifecycle", () => { - it("deduplicates function identity and makes repeated removal harmless", async () => { - const kv = memoryKV(); + it("notifies nobody when the entry write fails", async () => { const listener = vi.fn(); - subscribe(listener); - audit.addAuditListener(listener); - const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - expect(listener.mock.calls).toEqual([[entry]]); - audit.removeAuditListener(listener); - audit.removeAuditListener(listener); - await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); - expect(listener.mock.calls).toEqual([[entry]]); - }); - - it("removes only the selected subscriber and ignores an absent function", async () => { - const kv = memoryKV(); - const removed = vi.fn(); - const retained = vi.fn(); - subscribe(removed); - subscribe(retained); - audit.removeAuditListener(removed); - audit.removeAuditListener(() => {}); - const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - expect(removed).not.toHaveBeenCalled(); - expect(retained.mock.calls).toEqual([[entry]]); - }); - - it("replaces and clears only the legacy slot while preserving independent subscribers", async () => { - const kv = memoryKV(); - const independent = vi.fn(); - const oldLegacy = vi.fn(); - const replacement = vi.fn(); - subscribe(independent); - audit.setAuditRecordedListener(oldLegacy); - audit.setAuditRecordedListener(replacement); - const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - audit.setAuditRecordedListener(null); - const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); - expect(oldLegacy).not.toHaveBeenCalled(); - expect(replacement.mock.calls).toEqual([[first]]); - expect(independent.mock.calls).toEqual([[first], [second]]); - }); - - it("invokes a function shared by both slots once and removes each registration independently", async () => { - const kv = memoryKV(); - const shared = vi.fn(); - subscribe(shared); - audit.setAuditRecordedListener(shared); - const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - audit.removeAuditListener(shared); - const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); - audit.setAuditRecordedListener(null); - await audit.recordAudit(kv as never, "forget", "mem::forget", ["three"]); - expect(shared.mock.calls).toEqual([[first], [second]]); - }); - - it("invokes the legacy callback first followed by independent insertion order", async () => { - const kv = memoryKV(); - const calls: string[] = []; - subscribe(() => { calls.push("first"); }); - subscribe(() => { calls.push("second"); }); - audit.setAuditRecordedListener(() => { calls.push("legacy"); }); - await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - expect(calls).toEqual(["legacy", "first", "second"]); - }); - - it("moves a removed and registered subscriber to the end on the next entry", async () => { - const kv = memoryKV(); - const calls: string[] = []; - const first = () => { calls.push("first"); }; - subscribe(first); - subscribe(() => { calls.push("second"); }); - audit.removeAuditListener(first); - audit.addAuditListener(first); - await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - expect(calls).toEqual(["second", "first"]); - }); - - it("uses a dispatch snapshot when a callback removes a later subscriber and adds another", async () => { - const kv = memoryKV(); - const calls: string[] = []; - const later = () => { calls.push("later"); }; - const added = () => { calls.push("added"); }; - subscribe(() => { - calls.push("first"); - audit.removeAuditListener(later); - audit.addAuditListener(added); - }); - subscribe(later); - cleanups.push(() => audit.removeAuditListener(added)); - await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - expect(calls).toEqual(["first", "later"]); - calls.length = 0; - await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); - expect(calls).toEqual(["first", "added"]); - }); - - it("applies a legacy replacement during dispatch only to the next entry", async () => { - const kv = memoryKV(); - const calls: string[] = []; - subscribe(() => { calls.push("independent"); }); - audit.setAuditRecordedListener(() => { - calls.push("old-legacy"); - audit.setAuditRecordedListener(() => { calls.push("new-legacy"); }); - }); - await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); - expect(calls).toEqual(["old-legacy", "independent", "new-legacy", "independent"]); - }); - - it("does not invoke a removed and re-added callback twice in the current dispatch", async () => { - const kv = memoryKV(); - const calls: string[] = []; - const moved = () => { calls.push("moved"); }; - let change = true; - subscribe(() => { - calls.push("first"); - if (change) { - change = false; - audit.removeAuditListener(moved); - audit.addAuditListener(moved); - } - }); - subscribe(moved); - subscribe(() => { calls.push("last"); }); - await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - expect(calls).toEqual(["first", "moved", "last"]); - calls.length = 0; - await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); - expect(calls).toEqual(["first", "last", "moved"]); - }); - - it("isolates synchronous exceptions in both slots without losing another callback or the record", async () => { - const kv = memoryKV(); - const retained = vi.fn(); - audit.setAuditRecordedListener(() => { throw new Error("legacy failed"); }); - subscribe(() => { throw new Error("subscriber failed"); }); - subscribe(retained); - const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - expect(retained.mock.calls).toEqual([[entry]]); - expect(kv.store.get(KV.auditMonth(audit.auditMonthOf(entry.timestamp)))?.get(entry.id)).toEqual(entry); - expect(kv.set).toHaveBeenCalledTimes(2); - }); - - it("observes rejected async callbacks in both slots without unhandled rejection or lost delivery", async () => { - const kv = memoryKV(); - const unhandled = vi.fn(); - process.on("unhandledRejection", unhandled); - cleanups.push(() => { process.off("unhandledRejection", unhandled); }); - audit.setAuditRecordedListener(async () => { throw new Error("legacy async failed"); }); - subscribe(async () => { throw new Error("subscriber async failed"); }); - const retained = vi.fn(); - subscribe(retained); - const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - await new Promise((resolve) => setImmediate(resolve)); - expect(unhandled).not.toHaveBeenCalled(); - expect(retained.mock.calls).toEqual([[entry]]); - expect(kv.set).toHaveBeenCalledTimes(2); - }); - - it("observes a callback promise that rejects after recordAudit has returned", async () => { - const kv = memoryKV(); - const unhandled = vi.fn(); - process.on("unhandledRejection", unhandled); - cleanups.push(() => { process.off("unhandledRejection", unhandled); }); - const pending = deferred(); - subscribe(() => pending.promise); - const retained = vi.fn(); - subscribe(retained); - const entry = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - pending.reject(new Error("late subscriber failure")); - await new Promise((resolve) => setImmediate(resolve)); - expect(unhandled).not.toHaveBeenCalled(); - expect(retained.mock.calls).toEqual([[entry]]); - expect(kv.store.get(KV.auditMonth(audit.auditMonthOf(entry.timestamp)))?.get(entry.id)).toEqual(entry); - }); - - it("does not await a pending callback or block the following subscriber", async () => { - const kv = memoryKV(); - const pending = deferred(); - subscribe(() => pending.promise); - const retained = vi.fn(); - subscribe(retained); - const recording = audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - try { - const outcome = await Promise.race([ - recording.then(() => "recorded"), - new Promise((resolve) => setImmediate(() => resolve("still-pending"))), - ]); - expect(outcome).toBe("recorded"); - expect(retained).toHaveBeenCalledTimes(1); - } finally { - pending.resolve(); - await recording; - } - }); - - it("does not deliver after an entry write failure and preserves the safeAudit logging boundary", async () => { - const kv = memoryKV(); - const failure = new Error("entry write denied"); - kv.set.mockRejectedValue(failure); - const legacy = vi.fn(); - const independent = vi.fn(); - audit.setAuditRecordedListener(legacy); - subscribe(independent); - const { logger } = await import("../src/logger.js"); - const warning = vi.spyOn(logger, "warn").mockImplementation(() => {}); - cleanups.push(() => { warning.mockRestore(); }); - await expect(audit.recordAudit(kv as never, "forget", "mem::forget", ["one"])).rejects.toBe(failure); - await expect(audit.safeAudit(kv as never, "forget", "mem::forget", ["two"])).resolves.toBeUndefined(); - expect(legacy).not.toHaveBeenCalled(); - expect(independent).not.toHaveBeenCalled(); - expect(kv.get).not.toHaveBeenCalled(); - expect(kv.store.size).toBe(0); - expect(warning.mock.calls).toEqual([["audit write failed", { functionId: "mem::forget", operation: "forget", targetIds: ["two"], error: "entry write denied" }]]); - }); - - it("does not deliver after an index read fails even though the entry write succeeded", async () => { - const kv = memoryKV(); - const failure = new Error("month index unreadable"); - kv.get.mockRejectedValueOnce(failure); - const listener = vi.fn(); - subscribe(listener); - await expect(audit.recordAudit(kv as never, "forget", "mem::forget", ["one"])).rejects.toBe(failure); + cleanups.push(addAuditListener(listener)); + await expect(recordAudit(memoryKV(true) as never, "remember", "mem::test", ["mem_a"])).rejects.toThrow("write failed"); expect(listener).not.toHaveBeenCalled(); - expect(kv.set).toHaveBeenCalledTimes(1); - const [scope, id, entry] = kv.set.mock.calls[0]; - expect(scope).toMatch(/^mem:audit:\d{4}-\d{2}$/); - expect(kv.store.get(scope)?.get(id)).toEqual(entry); - expect(kv.store.has(KV.auditMonths)).toBe(false); - }); - - it("does not deliver after the month index write fails or hide the already written entry", async () => { - const kv = memoryKV(); - const failure = new Error("month index write denied"); - const persist = kv.set.getMockImplementation()!; - kv.set.mockImplementation(async (scope, key, value) => { - if (scope === KV.auditMonths) throw failure; - await persist(scope, key, value); - }); - const legacy = vi.fn(); - const independent = vi.fn(); - audit.setAuditRecordedListener(legacy); - subscribe(independent); - await expect(audit.recordAudit(kv as never, "forget", "mem::forget", ["one"])).rejects.toBe(failure); - expect(legacy).not.toHaveBeenCalled(); - expect(independent).not.toHaveBeenCalled(); - expect(kv.set).toHaveBeenCalledTimes(2); - const [scope, id, entry] = kv.set.mock.calls[0]; - expect(kv.store.get(scope)?.get(id)).toEqual(entry); - expect(kv.store.has(KV.auditMonths)).toBe(false); - }); - - it("delivers only after the month index write has completed", async () => { - const kv = memoryKV(); - const atIndex = deferred(); - const releaseIndex = deferred(); - const persist = kv.set.getMockImplementation()!; - kv.set.mockImplementation(async (scope, key, value) => { - if (scope === KV.auditMonths) { - atIndex.resolve(); - await releaseIndex.promise; - } - await persist(scope, key, value); - }); - const observedIndexes: unknown[] = []; - const listener = vi.fn(() => { - observedIndexes.push(structuredClone(kv.store.get(KV.auditMonths)?.get("index"))); - }); - subscribe(listener); - const recording = audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - try { - await atIndex.promise; - expect(listener).not.toHaveBeenCalled(); - expect(kv.set).toHaveBeenCalledTimes(2); - } finally { - releaseIndex.resolve(); - } - const entry = await recording; - expect(listener.mock.calls).toEqual([[entry]]); - expect(observedIndexes).toEqual([{ months: [audit.auditMonthOf(entry.timestamp)] }]); - }); - - it("takes the listener snapshot after pending persistence completes", async () => { - const kv = memoryKV(); - const atIndex = deferred(); - const releaseIndex = deferred(); - const persist = kv.set.getMockImplementation()!; - kv.set.mockImplementation(async (scope, key, value) => { - if (scope === KV.auditMonths) { - atIndex.resolve(); - await releaseIndex.promise; - } - await persist(scope, key, value); - }); - const oldLegacy = vi.fn(); - const newLegacy = vi.fn(); - const added = vi.fn(); - audit.setAuditRecordedListener(oldLegacy); - const recording = audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - try { - await atIndex.promise; - audit.setAuditRecordedListener(newLegacy); - subscribe(added); - } finally { - releaseIndex.resolve(); - } - const entry = await recording; - expect(oldLegacy).not.toHaveBeenCalled(); - expect(newLegacy.mock.calls).toEqual([[entry]]); - expect(added.mock.calls).toEqual([[entry]]); - }); - - it("delivers each record once when its month is already indexed without an extra index write", async () => { - const kv = memoryKV(); - const listener = vi.fn(); - subscribe(listener); - const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); - expect(listener.mock.calls).toEqual([[first], [second]]); - expect(kv.set.mock.calls.map(([scope]) => scope)).toEqual([ - KV.auditMonth(audit.auditMonthOf(first.timestamp)), KV.auditMonths, KV.auditMonth(audit.auditMonthOf(second.timestamp)), - ]); - }); - - it("keeps independent delivery through viewer re-registration and legacy viewer cleanup", async () => { - const kv = memoryKV(); - const listener = vi.fn(); - subscribe(listener); - const firstViewer = viewer(kv); - const secondViewer = viewer(kv); - const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - expect(firstViewer.map((event) => event.data.audit)).toEqual([first]); - expect(secondViewer.map((event) => event.data.audit)).toEqual([first]); - audit.setAuditRecordedListener(null); - const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); - expect(firstViewer.map((event) => event.data.audit)).toEqual([first]); - expect(secondViewer.map((event) => event.data.audit)).toEqual([first]); - expect(listener.mock.calls).toEqual([[first], [second]]); - }); -}); - -describe("legacy audit callback source compatibility", () => { - it("accepts value-returning legacy and independent callbacks including an async legacy callback", async () => { - const kv = memoryKV(); - const legacyRows: AuditEntry[] = []; - const independentRows: AuditEntry[] = []; - const independent = (entry: AuditEntry) => independentRows.push(entry); - audit.addAuditListener(independent); - cleanups.push(() => audit.removeAuditListener(independent)); - audit.setAuditRecordedListener((entry) => legacyRows.push(entry)); - const first = await audit.recordAudit(kv as never, "forget", "mem::forget", ["one"]); - audit.setAuditRecordedListener(async (entry) => legacyRows.push(entry)); - const second = await audit.recordAudit(kv as never, "forget", "mem::forget", ["two"]); - await new Promise((resolve) => setImmediate(resolve)); - expect(legacyRows).toEqual([first, second]); - expect(independentRows).toEqual([first, second]); }); }); diff --git a/test/consolidation-summary-counts.test.ts b/test/consolidation-summary-counts.test.ts index e745acefcd..0489798d52 100644 --- a/test/consolidation-summary-counts.test.ts +++ b/test/consolidation-summary-counts.test.ts @@ -6,7 +6,7 @@ import { KV } from "../src/state/schema.js"; type Call = { function_id: string; payload: { scope: string; key?: string } }; type Row = Record | null; -function fixture(backend: StateBackend, options: { rejectSummaryKeys?: boolean; rejectSummaryValues?: boolean } = {}) { +function fixture(backend: StateBackend, options: { rejectSummaryKeys?: boolean } = {}) { const data = new Map>([ [KV.summaries, new Map([["summary-a", { sessionId: "a", content: "a".repeat(32768) }], ["summary-b", { sessionId: "b", content: "b".repeat(32768) }], ["summary-null", null]])], [KV.semantic, new Map([["semantic-a", { id: "a" }], ["semantic-b", { id: "b" }], ["semantic-null", null]])], @@ -33,59 +33,26 @@ function fixture(backend: StateBackend, options: { rejectSummaryKeys?: boolean; return backend === "redis" ? { keys } : keys; } if (call.function_id === "state::list") { - if (call.payload.scope === KV.summaries && options.rejectSummaryValues) throw new Error("summary values unavailable"); return structuredClone([...rows.values()]); } if (call.function_id === "state::get") return structuredClone(rows.get(call.payload.key ?? "") ?? null); throw new Error(`Unexpected fixture invocation: ${call.function_id}`); }), }; - return { kv: new StateKV(sdk as never, { backend }), data, calls }; -} - -function expectCounts(status: Awaited>>, summaries: number) { - expect(status.summaries).toBe(summaries); - expect(status.recurringPatterns).toBe(2); - expect(status.tiers.find((tier) => tier.id === "semantic")?.count).toBe(2); - expect(status.tiers.find((tier) => tier.id === "procedural")?.count).toBe(1); - expect(status.tiers.find((tier) => tier.id === "relations")?.count).toBe(3); + return { kv: new StateKV(sdk as never, { backend }), calls }; } describe("consolidation summary counts", () => { - it.each(["file", "redis"] as const)("counts summaries without transferring their record values on %s", async (backend) => { - const { kv, calls } = fixture(backend); + it("counts summaries by key without reading their values", async () => { + const { kv, calls } = fixture("file"); const status = await createConsolidationStatusReader(kv)({ lastRun: null }); - expectCounts(status, 3); + expect(status.summaries).toBe(3); expect(calls.filter((call) => call.payload.scope === KV.summaries && ["state::list", "state::get"].includes(call.function_id))).toEqual([]); }); - it.each(["file", "redis"] as const)("preserves nullable rows, real pattern filtering and count reuse on %s", async (backend) => { - const { kv, data, calls } = fixture(backend); - const read = createConsolidationStatusReader(kv); - expectCounts(await read({ lastRun: null }), 3); - const before = calls.length; - data.get(KV.summaries)!.set("later", { sessionId: "later" }); - expectCounts(await read({ lastRun: null }), 3); - expect(calls).toHaveLength(before); - expect(calls.some((call) => call.payload.scope === KV.memories && call.function_id === "state::get")).toBe(true); - }); - - it.each(["file", "redis"] as const)("keeps the working value-list fallback when summary key listing rejects on %s", async (backend) => { - const { kv, calls } = fixture(backend, { rejectSummaryKeys: true }); - expectCounts(await createConsolidationStatusReader(kv)({ lastRun: null }), 3); + it("falls back to listing values when key listing fails", async () => { + const { kv, calls } = fixture("redis", { rejectSummaryKeys: true }); + expect((await createConsolidationStatusReader(kv)({ lastRun: null })).summaries).toBe(3); expect(calls.filter((call) => call.payload.scope === KV.summaries && call.function_id === "state::list")).toHaveLength(1); }); - - it("supports list-only adapters without a key-list method", async () => { - const { data } = fixture("file"); - const kv = { - list: async (scope: string) => [...(data.get(scope)?.values() ?? [])].filter((row) => row !== null), - }; - expectCounts(await createConsolidationStatusReader(kv as never)({ lastRun: null }), 2); - }); - - it("retains the empty best-effort summary count when both read paths fail", async () => { - const { kv } = fixture("file", { rejectSummaryKeys: true, rejectSummaryValues: true }); - expectCounts(await createConsolidationStatusReader(kv)({ lastRun: null }), 0); - }); }); diff --git a/test/fixtures/hook-source-fetch.mjs b/test/fixtures/hook-source-fetch.mjs deleted file mode 100644 index b268a3992a..0000000000 --- a/test/fixtures/hook-source-fetch.mjs +++ /dev/null @@ -1,41 +0,0 @@ -import { appendFileSync } from "node:fs"; - -function observedBody(body) { - if (typeof body !== "string") return body ?? null; - try { - return JSON.parse(body); - } catch { - return body; - } -} - -globalThis.fetch = async (url, init = {}) => { - appendFileSync( - process.env.AGENTMEMORY_HOOK_TEST_REQUESTS, - `${JSON.stringify({ - url: String(url), - method: init.method ?? "GET", - headers: Object.fromEntries(new Headers(init.headers)), - body: observedBody(init.body), - })}\n`, - ); - - const mode = process.env.AGENTMEMORY_HOOK_TEST_FETCH; - if (mode === "unreachable") throw new TypeError("fetch failed"); - if (mode === "hanging") { - return new Promise((_resolve, reject) => { - const timer = setTimeout(() => reject(new Error("unaborted fetch")), 10_000); - const abort = () => { - clearTimeout(timer); - reject(init.signal.reason); - }; - if (init.signal.aborted) abort(); - else init.signal.addEventListener("abort", abort, { once: true }); - }); - } - - return new Response(JSON.stringify({ context: "source-hook recalled context" }), { - status: String(url).endsWith("/observe") ? 201 : 200, - headers: { "content-type": "application/json" }, - }); -}; diff --git a/test/fixtures/insight-list-memory.ts b/test/fixtures/insight-list-memory.ts deleted file mode 100644 index 20ca4221e2..0000000000 --- a/test/fixtures/insight-list-memory.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { strict as assert } from "node:assert"; -import { createHash } from "node:crypto"; -import { registerReflectFunctions } from "../../src/functions/reflect.js"; -import { StateKV } from "../../src/state/kv.js"; -import { KV } from "../../src/state/schema.js"; - -const backend = process.argv[2]; -assert(backend === "file" || backend === "redis"); -assert.equal(typeof global.gc, "function", "Run this probe with --expose-gc"); - -const count = 4096; -const functions = new Map unknown>(); -let reads = 0; -let peakHeapBytes = 0; -let modelRequests = 0; - -function sampleHeap() { - global.gc!(); - peakHeapBytes = Math.max(peakHeapBytes, process.memoryUsage().heapUsed); -} - -function record(index: number) { - const bytes = Buffer.alloc(12 * 1024, index % 251); - bytes.writeUInt32LE(index); - const concept = bytes.toString("base64"); - return JSON.parse(JSON.stringify({ - id: `historical-${index.toString().padStart(5, "0")}`, - title: `Historical insight ${index}`, - content: `Complete insight ${index}; Unicode Ж and é remain intact.`, - confidence: (index + 1) / count, - reinforcements: 3, - sourceConceptCluster: [concept], - sourceMemoryIds: [`memory-${index}`], - sourceLessonIds: [], - sourceCrystalIds: [], - tags: [concept], - createdAt: "2026-01-01T00:00:00.000Z", - updatedAt: "2026-01-01T00:00:00.000Z", - decayRate: 0.05, - project: "large-store", - legacy_extra: { index, values: [0, false, null] }, - })); -} - -const sdk = { - registerFunction: (id: string | { id: string }, handler: (data: unknown) => unknown) => { - functions.set(typeof id === "string" ? id : id.id, handler); - }, - trigger: async ({ function_id, payload }: { - function_id: string; - payload: { scope: string; key?: string }; - }) => { - assert.equal(payload.scope, KV.insights); - if (function_id === "state::list_keys") { - return Array.from({ length: count }, (_, index) => String(index)); - } - assert.equal(function_id, "state::get"); - const index = Number(payload.key); - assert(Number.isInteger(index) && index >= 0 && index < count); - // Sample while the handler still owns earlier pages, before the next read. - if (reads % 200 === 0) sampleHeap(); - reads++; - return record(index); - }, -}; - -const unexpectedModel = async () => { - modelRequests++; - throw new Error("Listing insights must not request a model"); -}; -const kv = new StateKV(sdk as never, { backend }); -registerReflectFunctions(sdk as never, kv, { - name: "no-model", - compress: unexpectedModel, - summarize: unexpectedModel, -} as never); - -sampleHeap(); -const baselineHeapBytes = peakHeapBytes; -const result = await functions.get("mem::insight-list")!({ limit: 1, project: "large-store" }); -sampleHeap(); -assert.deepEqual(result, { success: true, insights: [record(count - 1)] }); -assert.equal(reads, count); -assert.equal(modelRequests, 0); -console.log(JSON.stringify({ - backend, - records: count, - reads, - modelRequests, - baselineHeapBytes, - peakHeapBytes, - heapGrowthBytes: peakHeapBytes - baselineHeapBytes, - completeResultSha256: createHash("sha256").update(JSON.stringify(result)).digest("hex"), -})); diff --git a/test/fixtures/insight-list.json b/test/fixtures/insight-list.json deleted file mode 100644 index ae737c2fbd..0000000000 --- a/test/fixtures/insight-list.json +++ /dev/null @@ -1,1061 +0,0 @@ -{ - "records": { - "late": { - "id": "ins_date_late", - "title": "Fixture late", - "content": "Complete historical insight late; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.8, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-04T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "late", - "values": [ - 0, - false, - null - ] - } - }, - "id_late": { - "id": "ins_mjxjdhc0_a", - "title": "Fixture id_late", - "content": "Complete historical insight id_late; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.8, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-01T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "id_late", - "values": [ - 0, - false, - null - ] - } - }, - "high_b": { - "id": "high-b", - "title": "Fixture high_b", - "content": "Complete historical insight high_b; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.95, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-02T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "B", - "legacy_extra": { - "keep": "high_b", - "values": [ - 0, - false, - null - ] - } - }, - "field_early": { - "id": "ins_field_early", - "title": "Fixture field_early", - "content": "Complete historical insight field_early; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.8, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-01T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "field_early", - "values": [ - 0, - false, - null - ] - } - }, - "tie_b": { - "id": "plain-b", - "title": "Fixture tie_b", - "content": "Complete historical insight tie_b; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.8, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-02T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "tie_b", - "values": [ - 0, - false, - null - ] - } - }, - "deleted_a": { - "id": "deleted-a", - "title": "Fixture deleted_a", - "content": "Complete historical insight deleted_a; commas, accents é and Unicode Ж remain intact.", - "confidence": 1, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-02T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "deleted_a", - "values": [ - 0, - false, - null - ] - }, - "deleted": true - }, - "no_time": { - "id": "plain-z", - "title": "Fixture no_time", - "content": "Complete historical insight no_time; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.8, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "not-a-date", - "updatedAt": "not-a-date", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "no_time", - "values": [ - 0, - false, - null - ] - } - }, - "tie_a": { - "id": "plain-a", - "title": "Fixture tie_a", - "content": "Complete historical insight tie_a; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.8, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-02T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "tie_a", - "values": [ - 0, - false, - null - ] - } - }, - "unscoped": { - "id": "unscoped", - "title": "Fixture unscoped", - "content": "Complete historical insight unscoped; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.9, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-02T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "legacy_extra": { - "keep": "unscoped", - "values": [ - 0, - false, - null - ] - } - }, - "id_early": { - "id": "ins_mjt91xc0_z", - "title": "Fixture id_early", - "content": "Complete historical insight id_early; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.8, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-05T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "id_early", - "values": [ - 0, - false, - null - ] - } - }, - "low_a": { - "id": "low-a", - "title": "Fixture low_a", - "content": "Complete historical insight low_a; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.7, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-02T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "low_a", - "values": [ - 0, - false, - null - ] - } - }, - "fallback": { - "id": "fallback", - "title": "Fixture fallback", - "content": "Complete historical insight fallback; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.8, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "not-a-date", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "fallback", - "values": [ - 0, - false, - null - ] - }, - "timestamp": "2026-01-01T00:00:00.000Z", - "startedAt": "2026-01-05T00:00:00.000Z" - }, - "top_a": { - "id": "top-a", - "title": "Fixture top_a", - "content": "Complete historical insight top_a; commas, accents é and Unicode Ж remain intact.", - "confidence": 1, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-02T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "top_a", - "values": [ - 0, - false, - null - ] - } - }, - "zero_a": { - "id": "zero-a", - "title": "Fixture zero_a", - "content": "Complete historical insight zero_a; commas, accents é and Unicode Ж remain intact.", - "confidence": 0, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-02T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "A", - "legacy_extra": { - "keep": "zero_a", - "values": [ - 0, - false, - null - ] - } - }, - "low_b": { - "id": "low-b", - "title": "Fixture low_b", - "content": "Complete historical insight low_b; commas, accents é and Unicode Ж remain intact.", - "confidence": 0.6, - "reinforcements": 3, - "sourceConceptCluster": [ - "alpha", - "comma,inside", - "Ж" - ], - "sourceMemoryIds": [ - "mem-one", - "mem-two" - ], - "sourceLessonIds": [ - "lesson-one" - ], - "sourceCrystalIds": [ - "crystal-one" - ], - "evidenceVersion": 1, - "tags": [ - "alpha", - "same content preserved" - ], - "createdAt": "2026-01-02T00:00:00.000Z", - "updatedAt": "2026-01-09T00:00:00.000Z", - "lastReinforcedAt": "2026-01-08T00:00:00.000Z", - "lastDecayedAt": "2026-01-07T00:00:00.000Z", - "decayRate": 0.05, - "project": "B", - "legacy_extra": { - "keep": "low_b", - "values": [ - 0, - false, - null - ] - } - } - }, - "pages": [ - [ - "late", - "id_late", - "high_b" - ], - [ - "field_early", - "tie_b", - "deleted_a", - "no_time" - ], - [ - "tie_a", - "unscoped", - "id_early", - "low_a" - ], - [ - "fallback", - "top_a", - "zero_a", - "low_b" - ] - ], - "cases": [ - { - "name": "small3_projectA_filters_before_selection", - "request": { - "project": "A", - "limit": 3 - }, - "expected": { - "file": [ - "top_a", - "late", - "id_late" - ], - "redis": [ - "top_a", - "id_early", - "fallback" - ] - } - }, - { - "name": "small5_projectA_confidence_ties", - "request": { - "project": "A", - "limit": 5 - }, - "expected": { - "file": [ - "top_a", - "late", - "id_late", - "field_early", - "tie_b" - ], - "redis": [ - "top_a", - "id_early", - "fallback", - "field_early", - "tie_a" - ] - } - }, - { - "name": "small5_global", - "request": { - "limit": 5 - }, - "expected": { - "file": [ - "top_a", - "high_b", - "unscoped", - "late", - "id_late" - ], - "redis": [ - "top_a", - "high_b", - "unscoped", - "id_early", - "fallback" - ] - } - }, - { - "name": "default_global_all14", - "request": {}, - "expected": { - "file": [ - "top_a", - "high_b", - "unscoped", - "late", - "id_late", - "field_early", - "tie_b", - "no_time", - "tie_a", - "id_early", - "fallback", - "low_a", - "low_b", - "zero_a" - ], - "redis": [ - "top_a", - "high_b", - "unscoped", - "id_early", - "fallback", - "field_early", - "tie_a", - "tie_b", - "id_late", - "late", - "no_time", - "low_a", - "low_b", - "zero_a" - ] - } - }, - { - "name": "empty_project_is_unfiltered", - "request": { - "project": "" - }, - "expected": { - "file": [ - "top_a", - "high_b", - "unscoped", - "late", - "id_late", - "field_early", - "tie_b", - "no_time", - "tie_a", - "id_early", - "fallback", - "low_a", - "low_b", - "zero_a" - ], - "redis": [ - "top_a", - "high_b", - "unscoped", - "id_early", - "fallback", - "field_early", - "tie_a", - "tie_b", - "id_late", - "late", - "no_time", - "low_a", - "low_b", - "zero_a" - ] - } - }, - { - "name": "project_filter_exact_no_trim", - "request": { - "project": " A " - }, - "expected": { - "file": [], - "redis": [] - } - }, - { - "name": "projectB", - "request": { - "project": "B" - }, - "expected": { - "file": [ - "high_b", - "low_b" - ], - "redis": [ - "high_b", - "low_b" - ] - } - }, - { - "name": "projectA_minConfidence_inclusive", - "request": { - "project": "A", - "minConfidence": 0.8 - }, - "expected": { - "file": [ - "top_a", - "late", - "id_late", - "field_early", - "tie_b", - "no_time", - "tie_a", - "id_early", - "fallback" - ], - "redis": [ - "top_a", - "id_early", - "fallback", - "field_early", - "tie_a", - "tie_b", - "id_late", - "late", - "no_time" - ] - } - }, - { - "name": "projectA_confidence1", - "request": { - "project": "A", - "minConfidence": 1 - }, - "expected": { - "file": [ - "top_a" - ], - "redis": [ - "top_a" - ] - } - }, - { - "name": "projectA_limit501_caps500", - "request": { - "project": "A", - "limit": 501 - }, - "expected": { - "file": [ - "top_a", - "late", - "id_late", - "field_early", - "tie_b", - "no_time", - "tie_a", - "id_early", - "fallback", - "low_a", - "zero_a" - ], - "redis": [ - "top_a", - "id_early", - "fallback", - "field_early", - "tie_a", - "tie_b", - "id_late", - "late", - "no_time", - "low_a", - "zero_a" - ] - } - }, - { - "name": "limit_MAX_SAFE_INTEGER_caps500", - "request": { - "limit": 9007199254740991 - }, - "expected": { - "file": [ - "top_a", - "high_b", - "unscoped", - "late", - "id_late", - "field_early", - "tie_b", - "no_time", - "tie_a", - "id_early", - "fallback", - "low_a", - "low_b", - "zero_a" - ], - "redis": [ - "top_a", - "high_b", - "unscoped", - "id_early", - "fallback", - "field_early", - "tie_a", - "tie_b", - "id_late", - "late", - "no_time", - "low_a", - "low_b", - "zero_a" - ] - } - }, - { - "name": "unknown_project_empty", - "request": { - "project": "missing", - "limit": 1 - }, - "expected": { - "file": [], - "redis": [] - } - } - ], - "invalid": [ - { - "request": { - "limit": 0 - }, - "expected": { - "success": false, - "invalid": true, - "error": "limit must be a positive safe integer" - } - }, - { - "request": { - "limit": -1 - }, - "expected": { - "success": false, - "invalid": true, - "error": "limit must be a positive safe integer" - } - }, - { - "request": { - "limit": 1.5 - }, - "expected": { - "success": false, - "invalid": true, - "error": "limit must be a positive safe integer" - } - }, - { - "request": { - "limit": "3" - }, - "expected": { - "success": false, - "invalid": true, - "error": "limit must be a positive safe integer" - } - }, - { - "request": { - "limit": null - }, - "expected": { - "success": false, - "invalid": true, - "error": "limit must be a positive safe integer" - } - }, - { - "request": { - "limit": 9007199254740992 - }, - "expected": { - "success": false, - "invalid": true, - "error": "limit must be a positive safe integer" - } - }, - { - "request": { - "minConfidence": -0.1 - }, - "expected": { - "success": false, - "invalid": true, - "error": "minConfidence must be a finite number between 0 and 1" - } - }, - { - "request": { - "minConfidence": 1.1 - }, - "expected": { - "success": false, - "invalid": true, - "error": "minConfidence must be a finite number between 0 and 1" - } - }, - { - "request": { - "minConfidence": "0.8" - }, - "expected": { - "success": false, - "invalid": true, - "error": "minConfidence must be a finite number between 0 and 1" - } - }, - { - "request": { - "minConfidence": null - }, - "expected": { - "success": false, - "invalid": true, - "error": "minConfidence must be a finite number between 0 and 1" - } - }, - { - "request": { - "limit": 0, - "minConfidence": "bad" - }, - "expected": { - "success": false, - "invalid": true, - "error": "limit must be a positive safe integer" - } - } - ] -} diff --git a/test/helpers/hook-source.ts b/test/helpers/hook-source.ts deleted file mode 100644 index 26397271a3..0000000000 --- a/test/helpers/hook-source.ts +++ /dev/null @@ -1,180 +0,0 @@ -import { spawn } from "node:child_process"; -import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs"; -import { createRequire } from "node:module"; -import { tmpdir } from "node:os"; -import { join, resolve } from "node:path"; -import { fileURLToPath, pathToFileURL } from "node:url"; -import { expect, it } from "vitest"; - -type HookName = "session-start" | "prompt-submit" | "pre-tool-use" | "post-tool-use" | "stop"; - -interface Request { - url: string; - method: string; - headers: Record; - body: Record; -} - -interface SpoolRecord { - eventId: string; - reason: string; - body: Record; -} - -interface HookResult { - code: number | null; - signal: NodeJS.Signals | null; - elapsedMs: number; - stdout: string; - stderr: string; - requests: Request[]; - spooled: SpoolRecord[]; -} - -const root = fileURLToPath(new URL("../../", import.meta.url)); -const loader = pathToFileURL(createRequire(import.meta.url).resolve("tsx")).href; -const preload = pathToFileURL(join(root, "test/fixtures/hook-source-fetch.mjs")).href; - -function lines(path: string): T[] { - return existsSync(path) - ? readFileSync(path, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line) as T) - : []; -} - -export async function runSourceHook( - name: HookName, - payload: Record | string, - options: { - fetch?: "success" | "unreachable" | "hanging"; - env?: Record; - } = {}, -): Promise { - const dir = mkdtempSync(join(tmpdir(), "am-hook-source-")); - const requests = join(dir, "requests.jsonl"); - const spool = join(dir, "spool"); - const startedAt = performance.now(); - try { - const result = await new Promise>((done, reject) => { - const child = spawn(process.execPath, [ - "--import", loader, - "--import", preload, - resolve(root, "src/hooks", `${name}.ts`), - ], { - cwd: dir, - env: { - PATH: process.env["PATH"], - SystemRoot: process.env["SystemRoot"], - HOME: dir, - USERPROFILE: dir, - APPDATA: dir, - LOCALAPPDATA: dir, - TEMP: dir, - TMP: dir, - AGENTMEMORY_URL: "http://127.0.0.1:3111", - AGENTMEMORY_SECRET: "source-hook-test-secret", - AGENTMEMORY_PROJECT_NAME: "source-hook-project", - AGENTMEMORY_INJECT_CONTEXT: "true", - AGENTMEMORY_CAPTURE_SPOOL_DIR: spool, - AGENTMEMORY_HOOK_TEST_FETCH: options.fetch ?? "success", - AGENTMEMORY_HOOK_TEST_REQUESTS: requests, - ...options.env, - }, - stdio: ["pipe", "pipe", "pipe"], - }); - let stdout = ""; - let stderr = ""; - const timer = setTimeout(() => { - child.kill("SIGKILL"); - reject(new Error(`${name} exceeded the test deadline: ${stderr}`)); - }, 7000); - child.stdout.on("data", (chunk) => { stdout += chunk; }); - child.stderr.on("data", (chunk) => { stderr += chunk; }); - child.on("error", (error) => { - clearTimeout(timer); - reject(error); - }); - child.on("close", (code, signal) => { - clearTimeout(timer); - done({ code, signal, stdout, stderr, elapsedMs: performance.now() - startedAt }); - }); - child.stdin.on("error", () => {}); - child.stdin.end(typeof payload === "string" ? payload : JSON.stringify(payload)); - }); - return { - ...result, - requests: lines(requests), - spooled: existsSync(spool) - ? readdirSync(spool).filter((name) => name.endsWith(".jsonl")) - .flatMap((name) => lines(join(spool, name))) - : [], - }; - } finally { - rmSync(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 }); - } -} - -export function expectExit(result: HookResult, stdout = ""): void { - expect(result.code, result.stderr).toBe(0); - expect(result.signal).toBeNull(); - expect(result.elapsedMs).toBeLessThan(5000); - expect(result.stdout).toBe(stdout); -} - -export function expectPost(request: Request | undefined, path: string): asserts request is Request { - expect(request).toBeDefined(); - expect(request!.url).toBe(`http://127.0.0.1:3111${path}`); - expect(request!.method).toBe("POST"); - expect(request!.headers).toEqual({ - authorization: "Bearer source-hook-test-secret", - "content-type": "application/json", - }); -} - -export function failureCases( - name: HookName, - payload: Record, - options: { capture: boolean; requestPaths: string[] }, -): void { - it.each(["{broken", "null", "7"])("ignores malformed stdin %s", async (input) => { - const result = await runSourceHook(name, input); - expectExit(result); - expect(result.requests).toEqual([]); - expect(result.spooled).toEqual([]); - }); - - it.each(["environment", "payload"])("ignores SDK child context from %s", async (source) => { - const result = await runSourceHook(name, - source === "payload" ? { ...payload, entrypoint: "sdk-ts" } : payload, - { env: source === "environment" ? { AGENTMEMORY_SDK_CHILD: "1" } : {} }, - ); - expectExit(result); - expect(result.requests).toEqual([]); - expect(result.spooled).toEqual([]); - }); - - it.each(["unreachable", "hanging"] as const)("exits successfully when fetch is %s", async (mode) => { - const result = await runSourceHook(name, payload, { fetch: mode }); - expectExit(result); - expect(result.requests.map((request) => new URL(request.url).pathname)).toEqual(options.requestPaths); - if (options.capture) { - expect(result.spooled).toHaveLength(1); - expect(result.spooled[0]).toMatchObject({ - eventId: result.requests[0]!.body["eventId"], - reason: mode === "unreachable" ? "unreachable" : "timeout", - body: result.requests[0]!.body, - }); - } else { - expect(result.spooled).toEqual([]); - } - }, 10_000); - - it("refuses a secret over non-loopback HTTP without fetching or spooling", async () => { - const result = await runSourceHook(name, payload, { - env: { AGENTMEMORY_URL: "http://untrusted.example:3111" }, - }); - expectExit(result); - expect(result.stderr).toContain("must use HTTPS"); - expect(result.requests).toEqual([]); - expect(result.spooled).toEqual([]); - }); -} diff --git a/test/hook-source-fetch.test.ts b/test/hook-source-fetch.test.ts deleted file mode 100644 index 300d0acb82..0000000000 --- a/test/hook-source-fetch.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { execFileSync } from "node:child_process"; -import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { fileURLToPath } from "node:url"; -import { describe, expect, it } from "vitest"; - -const preload = new URL("./fixtures/hook-source-fetch.mjs", import.meta.url).href; - -describe("source-hook fetch observer", () => { - it.each([ - { name: "an unexpected GET without a body", init: undefined, method: "GET", body: null }, - { name: "an unexpected non-JSON POST", init: { method: "POST", body: "not-json" }, method: "POST", body: "not-json" }, - ])("records $name so a no-fetch assertion cannot miss it", ({ init, method, body }) => { - const dir = mkdtempSync(join(tmpdir(), "am-hook-fetch-")); - const requests = join(dir, "requests.jsonl"); - const url = "http://127.0.0.1:3111/unexpected"; - const program = `await fetch(${JSON.stringify(url)}, ${JSON.stringify(init)}).catch(() => {});`; - try { - execFileSync(process.execPath, ["--import", preload, "--input-type=module", "-e", program], { - cwd: fileURLToPath(new URL("../", import.meta.url)), - env: { - PATH: process.env["PATH"], - SystemRoot: process.env["SystemRoot"], - HOME: dir, - USERPROFILE: dir, - AGENTMEMORY_HOOK_TEST_FETCH: "success", - AGENTMEMORY_HOOK_TEST_REQUESTS: requests, - }, - timeout: 5000, - stdio: "pipe", - }); - const observed: unknown[] = existsSync(requests) - ? readFileSync(requests, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line)) - : []; - expect(observed).toEqual([{ url, method, headers: {}, body }]); - } finally { - rmSync(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 }); - } - }); -}); diff --git a/test/insight-list.test.ts b/test/insight-list.test.ts index 30a890ac6f..556367ed54 100644 --- a/test/insight-list.test.ts +++ b/test/insight-list.test.ts @@ -1,106 +1,64 @@ -import { readFileSync } from "node:fs"; -import { spawnSync } from "node:child_process"; -import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; import { registerReflectFunctions } from "../src/functions/reflect.js"; import { StateKV } from "../src/state/kv.js"; import { KV } from "../src/state/schema.js"; -type Backend = "file" | "redis"; -type Row = Record; -const fixture = JSON.parse(readFileSync(new URL("./fixtures/insight-list.json", import.meta.url), "utf8")) as { - records: Record; - pages: string[][]; - cases: { name: string; request: Row; expected: Record }[]; - invalid: { request: Row; expected: Row }[]; -}; -const rows = fixture.pages.flat().map((alias) => fixture.records[alias]!); +type Row = { id: string; confidence: number; createdAt: string; project?: string; deleted?: boolean }; -function register(kv: unknown) { - const functions = new Map Promise>(); - const sdk = { - registerFunction: (id: string | { id: string }, handler: (data: unknown) => Promise) => { - functions.set(typeof id === "string" ? id : id.id, handler); - }, - }; - registerReflectFunctions(sdk as never, kv as never, {} as never); - return (data: unknown) => functions.get("mem::insight-list")!(data); -} +const row = (id: string, confidence: number, minute: number, extra: Partial = {}): Row => ({ + id, + confidence, + createdAt: new Date(Date.UTC(2026, 10, 1, 0, minute)).toISOString(), + project: "a", + ...extra, +}); -function paged(backend: Backend) { - let pagesRead = 0; - const kv = { - backend, - async *iterate(scope: string) { - expect(scope).toBe(KV.insights); - for (const page of fixture.pages) { - pagesRead++; - yield page.map((alias) => structuredClone(fixture.records[alias]!)); - } +function insightList(rows: Row[], backend: "file" | "redis") { + const sdk = { + registerFunction: () => {}, + trigger: async ({ function_id, payload }: { function_id: string; payload: { scope: string; key?: string } }) => { + expect(payload.scope).toBe(KV.insights); + if (function_id === "state::list_keys") return rows.map((_, index) => String(index)); + if (function_id === "state::list") return structuredClone(rows); + return structuredClone(rows[Number(payload.key)]); }, }; - return { call: register(kv), pagesRead: () => pagesRead }; + const kv = new StateKV(sdk as never, { backend }); + const iterate = kv.iterate.bind(kv); + kv.iterate = (scope) => iterate(scope, { pageSize: 2 }); + let handler: (data: unknown) => Promise<{ success: boolean; insights: Row[] }>; + const register = { registerFunction: (id: string, fn: typeof handler) => { if (id === "mem::insight-list") handler = fn; } }; + registerReflectFunctions(register as never, kv, {} as never); + return (data: unknown) => handler(data).then((result) => result.insights.map((insight) => insight.id)); } -describe.each(["file", "redis"] as const)("complete historical insight list (%s)", (backend) => { - it.each(fixture.cases)("preserves exact records and order: $name", async ({ request, expected }) => { - const f = paged(backend); - expect(await f.call(request)).toEqual({ - success: true, - insights: expected[backend].map((alias) => fixture.records[alias]), - }); - expect(f.pagesRead()).toBe(fixture.pages.length); - }); +const ties = [row("late", 0.5, 9), row("early", 0.5, 1), row("top", 0.9, 5), row("mid", 0.5, 3)]; - it.each(fixture.invalid)("validates before reading: $request", async ({ request, expected }) => { - const f = paged(backend); - expect(await f.call(request)).toEqual(expected); - expect(f.pagesRead()).toBe(0); +describe("mem::insight-list paging", () => { + it("breaks confidence ties across pages by insertion order on redis", async () => { + expect(await insightList(ties, "redis")({ limit: 3 })).toEqual(["top", "early", "mid"]); }); - it("preserves complete order through real StateKV page reads", async () => { - const sdk = { - trigger: async ({ function_id, payload }: { function_id: string; payload: { scope: string; key?: string } }) => { - expect(payload.scope).toBe(KV.insights); - if (function_id === "state::list_keys") return rows.map((_, index) => String(index)); - expect(function_id).toBe("state::get"); - return structuredClone(rows[Number(payload.key)]); - }, - }; - const kv = new StateKV(sdk as never, { backend }); - const iterate = kv.iterate.bind(kv); - kv.iterate = (scope) => iterate(scope, { pageSize: 4 }); - const expected = fixture.cases.find((item) => item.name === "small5_projectA_confidence_ties")!; - expect(await register(kv)(expected.request)).toEqual({ - success: true, - insights: expected.expected[backend].map((alias) => fixture.records[alias]), - }); + it("breaks confidence ties by read order on the file backend", async () => { + expect(await insightList(ties, "file")({ limit: 3 })).toEqual(["top", "late", "early"]); }); - it("retains the list-only fallback's original tie order", async () => { - const kv = { backend, list: async () => structuredClone(rows) }; - const expected = fixture.cases.find((item) => item.name === "small3_projectA_filters_before_selection")!; - expect(await register(kv)(expected.request)).toEqual({ - success: true, - insights: expected.expected.file.map((alias) => fixture.records[alias]), - }); + it("keeps the top results in scope across pages", async () => { + const rows = [ + row("low", 0.1, 1), + row("edge", 0.4, 2), + row("gone", 0.99, 3, { deleted: true }), + row("other", 0.95, 4, { project: "b" }), + row("high", 0.8, 5), + row("higher", 0.85, 6), + ]; + const list = insightList(rows, "file"); + expect(await list({ limit: 2, project: "a", minConfidence: 0.4 })).toEqual(["higher", "high"]); + expect(await list({ limit: 10, project: "a", minConfidence: 0.4 })).toEqual(["higher", "high", "edge"]); }); - it("propagates a later page error instead of returning a partial top result", async () => { - const error = new Error("later insight page unavailable"); - const kv = { backend, async *iterate() { yield rows.slice(0, 3); throw error; } }; - await expect(register(kv)({ limit: 1 })).rejects.toBe(error); + it("returns an empty list when nothing matches", async () => { + expect(await insightList([], "redis")({ limit: 5 })).toEqual([]); + expect(await insightList(ties, "file")({ limit: 5, project: "missing" })).toEqual([]); }); - - it("keeps a small query below a retained-heap budget for large historical records", () => { - const result = spawnSync(process.execPath, [ - "--max-old-space-size=384", "--expose-gc", "--import", "tsx", - fileURLToPath(new URL("./fixtures/insight-list-memory.ts", import.meta.url)), backend, - ], { encoding: "utf8", timeout: 60_000 }); - expect(result.error).toBeUndefined(); - expect(result.status, result.stderr).toBe(0); - const metrics = JSON.parse(result.stdout.trim().split("\n").at(-1)!); - expect(metrics).toMatchObject({ backend, records: 4096, reads: 4096, modelRequests: 0 }); - expect(metrics.heapGrowthBytes).toBeLessThan(24 * 1024 * 1024); - }, 65_000); }); diff --git a/test/observe-durable-lookup.test.ts b/test/observe-durable-lookup.test.ts index d0d586b085..a7cce6cd4d 100644 --- a/test/observe-durable-lookup.test.ts +++ b/test/observe-durable-lookup.test.ts @@ -1,9 +1,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { registerObserveFunction } from "../src/functions/observe.js"; -import { applyUpdateOps, StateKV, type StateBackend } from "../src/state/kv.js"; +import { applyUpdateOps, StateKV } from "../src/state/kv.js"; import { KV } from "../src/state/schema.js"; import type { CompressedObservation, HookPayload } from "../src/types.js"; -import { SESSION_SOURCE_MAX_BYTES } from "../src/functions/observation-source-budget.js"; const indexed = vi.hoisted(() => new Map()); vi.mock("../src/functions/search.js", () => ({ @@ -40,7 +39,7 @@ function payload(overrides: Partial = {}): HookPayload { }; } -function fixture(options: { backend?: StateBackend; rejectList?: boolean; rejectPointRead?: boolean; cap?: number } = {}) { +function fixture(options: { rejectPointRead?: boolean } = {}) { const data = new Map>([ [SCOPE, new Map([[ID, row()], ["obs_unrelated1", { ...row("obs_unrelated1"), narrative: "x".repeat(32768) }]])], [KV.sessions, new Map([[SESSION, { id: SESSION, project: "fixture", status: "active", observationCount: 2 }]])], @@ -61,13 +60,9 @@ function fixture(options: { backend?: StateBackend; rejectList?: boolean; reject return structuredClone(entries.get(key) ?? null); } if (call.function_id === "state::list") { - if (scope === SCOPE && options.rejectList) throw new Error("whole observation list unavailable"); return structuredClone([...entries.values()]); } - if (call.function_id === "state::list_keys") { - const keys = [...entries.keys()]; - return options.backend === "redis" ? { keys } : keys; - } + if (call.function_id === "state::list_keys") return [...entries.keys()]; if (call.function_id === "state::set") { entries.set(key, structuredClone(call.payload.value)); return call.payload.value; @@ -82,11 +77,11 @@ function fixture(options: { backend?: StateBackend; rejectList?: boolean; reject throw new Error(`Unexpected state invocation: ${call.function_id}`); }, }; - const kv = new StateKV(sdk as never, { backend: options.backend }); - registerObserveFunction(sdk as never, kv, undefined, options.cap); + const kv = new StateKV(sdk as never); + registerObserveFunction(sdk as never, kv); const observe = (body = payload()) => handlers.get("mem::observe")!(body) as Promise>; const observationLists = () => calls.filter((call) => call.payload.scope === SCOPE && call.function_id === "state::list"); - return { data, calls, observe, observationLists }; + return { data, observe, observationLists }; } beforeEach(() => { @@ -96,8 +91,8 @@ beforeEach(() => { afterEach(() => vi.unstubAllEnvs()); describe("durable observation lookup", () => { - it.each(["file", "redis"] as const)("replays the stored record without loading its session on %s", async (backend) => { - const f = fixture({ backend }); + it("replays a stored record without listing its session", async () => { + const f = fixture(); const before = structuredClone(f.data); expect(await f.observe()).toEqual({ observationId: ID, deduplicated: true, existing: true }); expect(indexed.get(ID)).toEqual(row()); @@ -105,91 +100,26 @@ describe("durable observation lookup", () => { expect(f.observationLists()).toEqual([]); }); - it("can replay a point-readable record when the whole session cannot be read", async () => { - const f = fixture({ rejectList: true }); - expect(await f.observe()).toEqual({ observationId: ID, deduplicated: true, existing: true }); - expect(indexed.get(ID)).toEqual(row()); - expect(f.observationLists()).toEqual([]); - }); - - it("records a replayed live prompt and restores its index without reading other observations", async () => { - const f = fixture(); - expect(await f.observe(payload({ hookType: "prompt_submit", data: { prompt: "repair the fixture" } }))).toMatchObject({ existing: true, observationId: ID }); - const ledgers = [...f.data.get(KV.capturePrompts)!.values()] as Array<{ prompts: Record }>; - expect(ledgers).toHaveLength(1); - expect(Object.values(ledgers[0]!.prompts)).toEqual([{ live: [ID], claimed: [] }]); - expect(indexed.get(ID)).toEqual(row()); - expect(f.observationLists()).toEqual([]); - }); - - it("keeps the working list fallback if the point read fails", async () => { + it("falls back to the session list when the point read fails", async () => { const f = fixture({ rejectPointRead: true }); expect(await f.observe()).toMatchObject({ existing: true, observationId: ID }); - expect(indexed.get(ID)).toEqual(row()); - expect(f.observationLists()).toHaveLength(1); - }); - - it("retains legacy ID matching when a row is stored under a different key", async () => { - const f = fixture(); - f.data.get(SCOPE)!.delete(ID); - f.data.get(SCOPE)!.set("legacy-key", row()); - expect(await f.observe()).toMatchObject({ existing: true, observationId: ID }); - expect(indexed.get(ID)).toEqual(row()); - expect([...f.data.get(SCOPE)!.keys()]).toEqual(["obs_unrelated1", "legacy-key"]); expect(f.observationLists()).toHaveLength(1); }); - it("does not mistake a mismatched record at the requested key for the matching observation", async () => { + it("ignores a record with a different id stored at the requested key", async () => { const f = fixture(); f.data.get(SCOPE)!.set(ID, row("obs_wrong_id1")); f.data.get(SCOPE)!.set("legacy-key", row()); expect(await f.observe()).toMatchObject({ existing: true, observationId: ID }); - expect(indexed.get(ID)).toEqual(row()); expect(indexed.has("obs_wrong_id1")).toBe(false); expect(f.observationLists()).toHaveLength(1); }); - it("still reads the session and respects its source budget for a new durable observation", async () => { + it("writes a new durable observation after one session list", async () => { const f = fixture(); f.data.get(SCOPE)!.delete(ID); - const largeSource = { hookType: "post_tool_use", originalBytes: SESSION_SOURCE_MAX_BYTES, truncated: false, toolOutput: "s".repeat(SESSION_SOURCE_MAX_BYTES) }; - f.data.get(SCOPE)!.set("obs_unrelated1", { ...row("obs_unrelated1"), source: largeSource }); expect(await f.observe()).toMatchObject({ observationId: ID }); expect(f.data.get(SCOPE)!.get(ID)).toMatchObject({ id: ID, sessionId: SESSION }); - expect((f.data.get(SCOPE)!.get(ID) as CompressedObservation).source).toBeUndefined(); - expect((f.data.get(SCOPE)!.get("obs_unrelated1") as CompressedObservation).source).toEqual(largeSource); - expect(f.observationLists()).toHaveLength(1); - }); - - it("keeps cap eviction and normal writes for a non-durable observation", async () => { - const f = fixture({ cap: 2 }); - const result = await f.observe(payload({ observationId: undefined })); - expect(result.observationId).toMatch(/^obs_/); - expect(result.deduplicated).toBeUndefined(); - expect(f.data.get(SCOPE)!.size).toBe(2); - expect(f.data.get(SCOPE)!.has(result.observationId as string)).toBe(true); - expect(f.observationLists()).toHaveLength(1); - expect(f.calls.some(call => call.function_id === "state::get" && call.payload.scope === SCOPE)).toBe(false); - }); - - it("keeps an invalid observation ID on the existing non-durable path", async () => { - const f = fixture(); - const result = await f.observe(payload({ observationId: "invalid" })); - expect(result.observationId).toMatch(/^obs_/); - expect(result.observationId).not.toBe("invalid"); - expect(result.deduplicated).toBeUndefined(); expect(f.observationLists()).toHaveLength(1); - expect(f.calls.some(call => call.function_id === "state::get" && call.payload.scope === SCOPE)).toBe(false); - }); - - it("keeps concurrent durable replays idempotent without full-session reads", async () => { - const f = fixture(); - const before = structuredClone(f.data); - expect(await Promise.all(Array.from({ length: 8 }, () => f.observe()))).toEqual( - Array.from({ length: 8 }, () => ({ observationId: ID, deduplicated: true, existing: true })), - ); - expect(f.data).toEqual(before); - expect(indexed.get(ID)).toEqual(row()); - expect(f.observationLists()).toEqual([]); }); }); diff --git a/test/post-tool-use-source.test.ts b/test/post-tool-use-source.test.ts deleted file mode 100644 index dde12ab797..0000000000 --- a/test/post-tool-use-source.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; - -const payload = { - session_id: "source-session", cwd: "/fixture/repo", tool_use_id: "tool-1", tool_name: "Read", - tool_input: { file_path: "/fixture/repo/file.ts" }, tool_response: "Literal tool output.", -}; - -describe("post-tool-use source entrypoint", () => { - it("captures the complete tool payload with host event identity", async () => { - const result = await runSourceHook("post-tool-use", payload); - expectExit(result); - expect(result.requests).toHaveLength(1); - expectPost(result.requests[0], "/agentmemory/observe"); - expect(result.requests[0].body).toEqual({ - hookType: "post_tool_use", sessionId: "source-session", project: "source-hook-project", cwd: "/fixture/repo", - timestamp: expect.any(String), eventId: expect.stringMatching(/^evh_[0-9a-f]{32}$/), - data: { tool_name: "Read", tool_input: { file_path: "/fixture/repo/file.ts" }, tool_output: "Literal tool output." }, - }); - expect(result.spooled).toEqual([]); - }); - - it("keeps memory tool calls out of capture", async () => { - const result = await runSourceHook("post-tool-use", { ...payload, tool_name: "mcp__agentmemory__memory_save" }); - expectExit(result); - expect(result.requests).toEqual([]); - }); - - failureCases("post-tool-use", payload, { capture: true, requestPaths: ["/agentmemory/observe"] }); -}); diff --git a/test/pre-tool-use-source.test.ts b/test/pre-tool-use-source.test.ts deleted file mode 100644 index b2bebbe885..0000000000 --- a/test/pre-tool-use-source.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; - -const payload = { - session_id: "source-session", cwd: "/fixture/repo", hook_event_name: "PreToolUse", - tool_name: "Read", tool_input: { file_path: "/fixture/repo/file.ts" }, -}; - -describe("pre-tool-use source entrypoint", () => { - it("requests scoped file context and prints the host envelope", async () => { - const result = await runSourceHook("pre-tool-use", payload); - expectExit(result, JSON.stringify({ hookSpecificOutput: { - hookEventName: "PreToolUse", additionalContext: "source-hook recalled context", - } })); - expect(result.requests).toHaveLength(1); - expectPost(result.requests[0], "/agentmemory/enrich"); - expect(result.requests[0].body).toEqual({ - sessionId: "source-session", files: ["/fixture/repo/file.ts"], terms: [], toolName: "Read", project: "source-hook-project", - }); - expect(result.spooled).toEqual([]); - }); - - it("sends nothing when context injection is disabled", async () => { - const result = await runSourceHook("pre-tool-use", payload, { env: { AGENTMEMORY_INJECT_CONTEXT: "false" } }); - expectExit(result); - expect(result.requests).toEqual([]); - }); - - failureCases("pre-tool-use", payload, { capture: false, requestPaths: ["/agentmemory/enrich"] }); -}); diff --git a/test/prompt-submit-source.test.ts b/test/prompt-submit-source.test.ts deleted file mode 100644 index 12eccd1b30..0000000000 --- a/test/prompt-submit-source.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; - -const payload = { session_id: "source-session", cwd: "/fixture/repo", prompt: "Keep this literal user prompt.", prompt_id: "prompt-1" }; - -describe("prompt-submit source entrypoint", () => { - it("captures the prompt with session, project and host event identity", async () => { - const result = await runSourceHook("prompt-submit", payload); - expectExit(result); - expect(result.requests).toHaveLength(1); - expectPost(result.requests[0], "/agentmemory/observe"); - expect(result.requests[0].body).toEqual({ - hookType: "prompt_submit", sessionId: "source-session", project: "source-hook-project", cwd: "/fixture/repo", - timestamp: expect.any(String), eventId: expect.stringMatching(/^evh_[0-9a-f]{32}$/), - data: { prompt: "Keep this literal user prompt." }, - }); - expect(result.spooled).toEqual([]); - }); - - failureCases("prompt-submit", payload, { capture: true, requestPaths: ["/agentmemory/observe"] }); -}); diff --git a/test/session-start-source.test.ts b/test/session-start-source.test.ts deleted file mode 100644 index 12e2645d82..0000000000 --- a/test/session-start-source.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; - -const payload = { session_id: "source-session", cwd: "/fixture/repo", hook_event_name: "SessionStart" }; - -describe("session-start source entrypoint", () => { - it("registers the session and prints the host context envelope", async () => { - const result = await runSourceHook("session-start", payload); - expectExit(result, JSON.stringify({ hookSpecificOutput: { - hookEventName: "SessionStart", additionalContext: "source-hook recalled context", - } })); - expect(result.requests).toHaveLength(1); - expectPost(result.requests[0], "/agentmemory/session/start"); - expect(result.requests[0].body).toEqual({ - sessionId: "source-session", project: "source-hook-project", cwd: "/fixture/repo", - }); - expect(result.spooled).toEqual([]); - }); - - it("registers without printing context when injection is disabled", async () => { - const result = await runSourceHook("session-start", payload, { env: { AGENTMEMORY_INJECT_CONTEXT: "false" } }); - expectExit(result); - expect(result.requests).toHaveLength(1); - expectPost(result.requests[0], "/agentmemory/session/start"); - }); - - failureCases("session-start", payload, { capture: false, requestPaths: ["/agentmemory/session/start"] }); -}); diff --git a/test/stop-source.test.ts b/test/stop-source.test.ts deleted file mode 100644 index 148770f5dc..0000000000 --- a/test/stop-source.test.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { expectExit, expectPost, failureCases, runSourceHook } from "./helpers/hook-source.js"; - -const payload = { session_id: "source-session", cwd: "/fixture/repo", last_assistant_message: "Literal assistant reply." }; - -describe("stop source entrypoint", () => { - it("captures the reply before ending the turn", async () => { - const result = await runSourceHook("stop", payload); - expectExit(result); - expect(result.requests).toHaveLength(2); - expectPost(result.requests[0], "/agentmemory/observe"); - expectPost(result.requests[1], "/agentmemory/session/end"); - expect(result.requests[0].body).toEqual({ - hookType: "stop", sessionId: "source-session", project: "source-hook-project", cwd: "/fixture/repo", - timestamp: expect.any(String), eventId: expect.stringMatching(/^evc_[0-9a-f]{32}$/), - data: { last_assistant_message: "Literal assistant reply." }, - }); - expect(result.requests[1].body).toEqual({ sessionId: "source-session", final: false }); - expect(result.spooled).toEqual([]); - }); - - it("ends a turn without capturing a blank reply", async () => { - const result = await runSourceHook("stop", { ...payload, last_assistant_message: " \n" }); - expectExit(result); - expect(result.requests).toHaveLength(1); - expectPost(result.requests[0], "/agentmemory/session/end"); - expect(result.requests[0].body).toEqual({ sessionId: "source-session", final: false }); - }); - - failureCases("stop", payload, { capture: true, requestPaths: ["/agentmemory/observe", "/agentmemory/session/end"] }); -}); From 17536bcef3523bbc018c405a132e7a0743015450 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 18 Nov 2026 10:01:12 +0530 Subject: [PATCH 09/10] fix(memory): index flow-compressed and auto-paged memories on write mem::flow-compress and mem::auto-page wrote memories to KV without indexing them, so search could not find them until a restart rebuilt BM25. Both now go through indexMemory, the funnel consolidate already uses. flow-compress also: - returns success:false and stores nothing when the reply has no summary sections, instead of saving an empty or raw-text memory; - supersedes its earlier memory for the same action set under the mem:remember lock, instead of adding a duplicate; - takes agentId from the caller (falling back to AGENT_ID) and trims project the way mem::remember does; - stamps project only when every compressed action belongs to it; - validates runId and actionIds, and the REST route forwards only runId, actionIds, project and agentId; - accepts summary tags that carry attributes. auto-page titles use the surrogate-safe title helper. The packaged graph gate ignores in-flight .bin.tmp files when listing graph files, so an atomic rename between readdir and stat no longer fails the gate. Co-authored-by: dimitri Signed-off-by: Rohit Ghumare --- scripts/release-gate/graph-index.mjs | 2 +- src/functions/flow-compress.ts | 141 +++++++++++++++++++++------ src/functions/working-memory.ts | 15 ++- test/flow-compress-index.test.ts | 112 +++++++++++++++++++++ 4 files changed, 240 insertions(+), 30 deletions(-) create mode 100644 test/flow-compress-index.test.ts diff --git a/scripts/release-gate/graph-index.mjs b/scripts/release-gate/graph-index.mjs index 47793288b5..8e3add83ee 100644 --- a/scripts/release-gate/graph-index.mjs +++ b/scripts/release-gate/graph-index.mjs @@ -295,7 +295,7 @@ function persisted(p, scope) { } function graphFiles(p) { const dir = join(p.data, "state_store.db"); - return readdirSync(dir).filter((file) => decodeURIComponent(file).startsWith("mem:graph:")).map((file) => ({ scope: decodeURIComponent(file).replace(/\.bin$/, ""), bytes: statSync(join(dir, file)).size })); + return readdirSync(dir).filter((file) => file.endsWith(".bin") && decodeURIComponent(file).startsWith("mem:graph:")).map((file) => ({ scope: decodeURIComponent(file).replace(/\.bin$/, ""), bytes: statSync(join(dir, file)).size })); } function ordinaryReads(p, phase, requestCount = 10) { const summary = p.traceSummary.get(phase); diff --git a/src/functions/flow-compress.ts b/src/functions/flow-compress.ts index 3be1ca6e97..cf058aff01 100644 --- a/src/functions/flow-compress.ts +++ b/src/functions/flow-compress.ts @@ -1,8 +1,24 @@ import type { IIIClient } from "iii-sdk"; -import type { StateKV } from "../state/kv.js"; +import { collectAll, type StateKV } from "../state/kv.js"; import { KV, generateId } from "../state/schema.js"; -import type { Action, ActionEdge, RoutineRun, MemoryProvider } from "../types.js"; -import { recordAudit } from "./audit.js"; +import { withKeyedLock } from "../state/keyed-mutex.js"; +import { sharesMemoryScope, supersededAt } from "../state/memory-utils.js"; +import type { Action, ActionEdge, Memory, RoutineRun, MemoryProvider } from "../types.js"; +import { getAgentId } from "../config.js"; +import { logger } from "../logger.js"; +import { safeAudit } from "./audit.js"; +import { indexMemory, unindexMemory } from "./search.js"; +import { memoryTitleFromContent } from "./remember.js"; + +type FlowMemory = Memory & { metadata?: { flowCompressed?: boolean; actionCount?: number; actionIds?: string[] } }; + +function trimmed(value: unknown, max = 512): string | undefined { + return typeof value === "string" && value.trim() ? value.trim().slice(0, max) : undefined; +} + +function flowKey(ids: string[]): string { + return [...new Set(ids)].sort().join("\n"); +} const FLOW_COMPRESS_SYSTEM = `You are a workflow summarizer. Given a completed action chain, produce a concise summary capturing: 1. The overall goal and outcome @@ -25,7 +41,19 @@ export function registerFlowCompressFunction( provider: MemoryProvider, ): void { sdk.registerFunction("mem::flow-compress", - async (data: { runId?: string; actionIds?: string[]; project?: string }) => { + async (data: { runId?: string; actionIds?: string[]; project?: string; agentId?: string }) => { + if (data.runId !== undefined && typeof data.runId !== "string") { + return { success: false, error: "runId must be a string" }; + } + if ( + data.actionIds !== undefined && + (!Array.isArray(data.actionIds) || data.actionIds.some((id) => typeof id !== "string")) + ) { + return { success: false, error: "actionIds must be an array of strings" }; + } + const requestedProject = trimmed(data.project); + const agentId = trimmed(data.agentId, 128) ?? getAgentId(); + data = { ...data, project: requestedProject }; let actionsToCompress: Action[] = []; if (data.runId) { @@ -81,40 +109,97 @@ export function registerFlowCompressFunction( prompt, ); const summary = parseFlowSummary(response); - const ts = new Date().toISOString(); - - const memory = { - id: generateId("mem"), - createdAt: ts, - updatedAt: ts, - type: "workflow" as const, - title: summary.goal || `Workflow: ${doneActions.length} actions`, - content: formatSummary(summary), - concepts: extractConcepts(doneActions), - files: extractFiles(doneActions), - sessionIds: [], - strength: 1.0, - version: 1, - isLatest: true, - metadata: { - flowCompressed: true, + const content = formatSummary(summary); + if (!content) { + logger.warn("Flow summary had no recognized sections; nothing stored", { actionCount: doneActions.length, - actionIds: doneActions.map((a) => a.id), - }, - }; + }); + return { + success: false, + error: "summary could not be parsed", + compressed: 0, + }; + } + + const actionProjects = new Set(doneActions.map((a) => a.project)); + const project = requestedProject + ? doneActions.every((a) => a.project === requestedProject) ? requestedProject : undefined + : actionProjects.size === 1 ? doneActions[0].project : undefined; + const actionIds = doneActions.map((a) => a.id); + const key = flowKey(actionIds); - await kv.set(KV.memories, memory.id, memory); - await recordAudit(kv, "compress", "mem::flow-compress", [memory.id], { + const { memory, previous } = await withKeyedLock("mem:remember", async () => { + const ts = new Date().toISOString(); + const scope = { project, agentId }; + const memories = await collectAll(kv, KV.memories); + const previous = memories.find((m) => + m.isLatest !== false && + m.metadata?.flowCompressed === true && + Array.isArray(m.metadata.actionIds) && + flowKey(m.metadata.actionIds) === key && + sharesMemoryScope(m, scope) && + (m.project || undefined) === project, + ); + const memory: FlowMemory = { + id: generateId("mem"), + createdAt: ts, + updatedAt: ts, + type: "workflow", + title: summary.goal ? memoryTitleFromContent(summary.goal) : `Workflow: ${doneActions.length} actions`, + content, + concepts: extractConcepts(doneActions), + files: extractFiles(doneActions), + sessionIds: [], + strength: 1.0, + version: previous ? (previous.version ?? 1) + 1 : 1, + isLatest: true, + ...(previous && { parentId: previous.id, supersedes: [previous.id, ...(previous.supersedes ?? [])] }), + ...(agentId && { agentId }), + ...(project && { project }), + metadata: { + flowCompressed: true, + actionCount: doneActions.length, + actionIds, + }, + }; + await kv.set(KV.memories, memory.id, memory); + if (previous) { + try { + await kv.set(KV.memories, previous.id, { + ...previous, + isLatest: false, + updatedAt: supersededAt(previous.updatedAt, ts), + }); + } catch (err) { + await kv.delete(KV.memories, memory.id); + throw err; + } + unindexMemory(previous.id); + } + return { memory, previous }; + }); + + await safeAudit(kv, previous ? "evolve" : "compress", "mem::flow-compress", [memory.id], { action: "compress_flow", flowCompressed: true, actionCount: doneActions.length, - project: data.project, + ...(previous && { oldId: previous.id, newId: memory.id }), + project, }); + try { + await indexMemory(kv, memory); + } catch (err) { + logger.warn("Flow-compressed memory indexing failed", { + memId: memory.id, + error: err instanceof Error ? err.message : String(err), + }); + } return { success: true, compressed: doneActions.length, memoryId: memory.id, + ...(previous && { supersededId: previous.id }), summary, }; } catch (err) { @@ -166,7 +251,7 @@ function parseFlowSummary(response: string): { } { const extract = (tag: string): string => { const match = response.match( - new RegExp(`<${tag}>([\\s\\S]*?)`), + new RegExp(`<${tag}(?:\\s[^>]*)?>([\\s\\S]*?)`), ); return match ? match[1].trim() : ""; }; diff --git a/src/functions/working-memory.ts b/src/functions/working-memory.ts index 6f606faa1e..97ca9720e5 100644 --- a/src/functions/working-memory.ts +++ b/src/functions/working-memory.ts @@ -3,6 +3,8 @@ import type { Memory, CompressedObservation, ContextBlock } from "../types.js"; import { KV, generateId } from "../state/schema.js"; import { StateKV, collectAll } from "../state/kv.js"; import { recordAudit } from "./audit.js"; +import { indexMemory } from "./search.js"; +import { memoryTitleFromContent } from "./remember.js"; import { recordAccessBatch } from "./access-tracker.js"; import { logger } from "../logger.js"; @@ -213,6 +215,7 @@ export function registerWorkingMemoryFunctions( let paged = 0; const pagedIds: string[] = []; + const archived: Memory[] = []; for (const entry of unpinned) { if (totalTokens <= coreBudget) break; const tokens = estimateTokens(entry.content); @@ -222,7 +225,7 @@ export function registerWorkingMemoryFunctions( createdAt: entry.createdAt, updatedAt: new Date().toISOString(), type: "fact", - title: entry.content.slice(0, 80), + title: memoryTitleFromContent(entry.content), content: entry.content, concepts: [], files: [], @@ -233,12 +236,22 @@ export function registerWorkingMemoryFunctions( }; await kv.set(KV.memories, archivalMemory.id, archivalMemory); await kv.delete(CORE_SCOPE, entry.id); + archived.push(archivalMemory); totalTokens -= tokens; paged++; pagedIds.push(entry.id); } + for (const memory of archived) { + await indexMemory(kv, memory).catch((err) => { + logger.warn("Paged memory indexing failed", { + memId: memory.id, + error: err instanceof Error ? err.message : String(err), + }); + }); + } + if (paged > 0) { try { await recordAudit(kv, "auto_page", "mem::auto-page", pagedIds, { diff --git a/test/flow-compress-index.test.ts b/test/flow-compress-index.test.ts new file mode 100644 index 0000000000..3f2b762626 --- /dev/null +++ b/test/flow-compress-index.test.ts @@ -0,0 +1,112 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { registerFlowCompressFunction } from "../src/functions/flow-compress.js"; +import { registerWorkingMemoryFunctions } from "../src/functions/working-memory.js"; +import { getSearchIndex } from "../src/functions/search.js"; +import type { Action, Memory } from "../src/types.js"; + +vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } })); + +const SUMMARY = "Quasar deploy pipelineShippedCache credentials early"; + +function action(id: string, project = "proj-a"): Action { + const ts = "2026-11-20T00:00:00.000Z"; + return { + id, title: `Finish ${id}`, description: `Work for ${id}`, status: "done", priority: 1, + createdAt: ts, updatedAt: ts, createdBy: "agent-a", project, tags: [], + sourceObservationIds: [], sourceMemoryIds: [], result: "shipped", + }; +} + +function store(actions: Action[]) { + const data = new Map>([["mem:actions", new Map(actions.map((a) => [a.id, a]))]]); + const kv = { + get: async (scope: string, id: string) => structuredClone(data.get(scope)?.get(id) ?? null), + set: async (scope: string, id: string, value: unknown) => { + if (!data.has(scope)) data.set(scope, new Map()); + data.get(scope)!.set(id, structuredClone(value)); + return value; + }, + delete: async (scope: string, id: string) => { data.get(scope)?.delete(id); }, + list: async (scope: string) => structuredClone([...(data.get(scope)?.values() ?? [])]), + }; + const memories = () => [...(data.get("mem:memories")?.values() ?? [])] as Memory[]; + return { kv, memories }; +} + +function flowCompress(actions: Action[], reply: string | (() => Promise) = SUMMARY) { + const s = store(actions); + let handler!: (data: object) => Promise>; + const sdk = { registerFunction: (_: string, fn: typeof handler) => { handler = fn; } }; + const summarize = vi.fn(typeof reply === "string" ? async () => reply : reply); + registerFlowCompressFunction(sdk as never, s.kv as never, { summarize } as never); + return { call: handler, ...s }; +} + +afterEach(() => getSearchIndex().clear()); + +describe("mem::flow-compress", () => { + it("indexes the memory so search finds it as soon as the call returns", async () => { + const f = flowCompress([action("act_1")]); + const result = await f.call({ actionIds: ["act_1"], agentId: " agent-x " }); + expect(result.success).toBe(true); + const id = result.memoryId as string; + expect(getSearchIndex().search("quasar deploy pipeline", 5, "memory").map((r) => r.obsId)).toContain(id); + expect(f.memories()[0]).toMatchObject({ id, title: "Quasar deploy pipeline", agentId: "agent-x", project: "proj-a", isLatest: true }); + }); + + it("stores nothing when the reply has no summary sections", async () => { + const f = flowCompress([action("act_1")], "Sorry, I cannot help with that."); + expect(await f.call({ actionIds: ["act_1"] })).toMatchObject({ success: false, error: "summary could not be parsed" }); + expect(f.memories()).toEqual([]); + expect(getSearchIndex().size).toBe(0); + }); + + it("supersedes the earlier memory when the same actions are compressed again", async () => { + const f = flowCompress([action("act_1"), action("act_2")]); + const first = await f.call({ actionIds: ["act_1", "act_2"] }); + const second = await f.call({ actionIds: ["act_2", "act_1"] }); + expect(second.supersededId).toBe(first.memoryId); + const byId = new Map(f.memories().map((m) => [m.id, m])); + expect(byId.get(first.memoryId as string)?.isLatest).toBe(false); + expect(byId.get(second.memoryId as string)).toMatchObject({ isLatest: true, version: 2, parentId: first.memoryId }); + expect(getSearchIndex().has(first.memoryId as string)).toBe(false); + expect(getSearchIndex().has(second.memoryId as string)).toBe(true); + }); + + it("stamps a project only when every compressed action belongs to it", async () => { + const mixed = flowCompress([action("act_1", "proj-a"), action("act_2", "proj-b")]); + await mixed.call({ actionIds: ["act_1", "act_2"] }); + expect(mixed.memories()[0].project).toBeUndefined(); + + const mismatch = flowCompress([action("act_1", "proj-a")]); + await mismatch.call({ actionIds: ["act_1"], project: "proj-b" }); + expect(mismatch.memories()[0].project).toBeUndefined(); + }); + + it("rejects malformed input before calling the provider", async () => { + const f = flowCompress([action("act_1")]); + expect(await f.call({ actionIds: "act_1" })).toMatchObject({ success: false }); + expect(await f.call({ runId: 7 })).toMatchObject({ success: false }); + expect(f.memories()).toEqual([]); + }); +}); + +describe("mem::auto-page", () => { + it("indexes the archival memories it creates", async () => { + const s = store([]); + const now = new Date().toISOString(); + for (let i = 0; i < 3; i++) { + await s.kv.set("mem:core-memory", `core_${i}`, { + id: `core_${i}`, content: `Nebula paging note ${i} `.repeat(40), importance: 1, + pinned: false, accessCount: 0, lastAccessedAt: now, createdAt: now, + }); + } + const handlers: Record Promise>> = {}; + registerWorkingMemoryFunctions({ registerFunction: (id: string, fn: never) => { handlers[id] = fn; } } as never, s.kv as never, 100); + const result = await handlers["mem::auto-page"]({ budget: 100 }); + expect(result.paged).toBeGreaterThan(0); + const archived = s.memories(); + expect(archived).toHaveLength(result.paged as number); + for (const memory of archived) expect(getSearchIndex().has(memory.id)).toBe(true); + }); +}); From de5db98feea9d101aecb2f86c651094e12d29c34 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 18 Nov 2026 10:01:53 +0530 Subject: [PATCH 10/10] docs: sync generated README stats Signed-off-by: Rohit Ghumare --- scripts/docs-sync.state.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/docs-sync.state.json b/scripts/docs-sync.state.json index 007f96d6f0..fa2e64735b 100644 --- a/scripts/docs-sync.state.json +++ b/scripts/docs-sync.state.json @@ -23,13 +23,13 @@ "5000+" ], "functions": [ - "333" + "323" ], "sourceFiles": [ - "268" + "263" ], "loc": [ - "64000" + "62000" ], "kvScopes": [ "65"