diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5b1b5de327..b33a27aca5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -74,5 +74,7 @@ jobs: - 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/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/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" 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/audit.ts b/src/functions/audit.ts index 3670131109..549df78d90 100644 --- a/src/functions/audit.ts +++ b/src/functions/audit.ts @@ -153,12 +153,26 @@ 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); + return () => { + 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( kv: StateKV, operation: AuditEntry["operation"], @@ -174,16 +188,22 @@ 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); - try { - auditRecordedListener?.(entry); - } catch {} + const listeners = auditRecordedListener ? [auditRecordedListener, ...auditListeners] : [...auditListeners]; + for (const listener of listeners) { + try { + void Promise.resolve(listener(entry)).catch(reportListenerFailure); + } catch (err) { + reportListenerFailure(err); + } + } return entry; } 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/observe.ts b/src/functions/observe.ts index a6a20d2f57..815f28d375 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/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/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/src/state/kv.ts b/src/state/kv.ts index 351a2d4d7b..5366ae375d 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/src/triggers/api.ts b/src/triggers/api.ts index 02ee060b72..dabdd4f154 100644 --- a/src/triggers/api.ts +++ b/src/triggers/api.ts @@ -481,17 +481,21 @@ export function createStatusReporter(sdk: IIIClient, kv: StateKV, deps: StatusRe }; } +async function countConsolidationSummaries(kv: StateKV): Promise { + 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([ - kv.list(KV.summaries).catch(() => []), + countConsolidationSummaries(kv).catch(() => 0), 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 && @@ -3825,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 new file mode 100644 index 0000000000..d32a6a5e2e --- /dev/null +++ b/test/audit-listeners.test.ts @@ -0,0 +1,58 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { addAuditListener, recordAudit, setAuditRecordedListener } from "../src/functions/audit.js"; +import type { AuditEntry } from "../src/types.js"; + +vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } })); + +function memoryKV(failWrites = false) { + const store = new Map>(); + return { + 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: async () => {}, + list: async (scope: string) => Array.from(store.get(scope)?.values() ?? []), + }; +} + +const cleanups: Array<() => void> = []; +afterEach(() => { + cleanups.splice(0).forEach((cleanup) => cleanup()); + setAuditRecordedListener(null); +}); + +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("isolates failing listeners and hands out a frozen entry", async () => { + const received: AuditEntry[] = []; + 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(Object.isFrozen(entry) && Object.isFrozen(entry.details) && Object.isFrozen(entry.targetIds)).toBe(true); + expect(Object.isFrozen(details)).toBe(false); + }); + + it("notifies nobody when the entry write fails", async () => { + const listener = vi.fn(); + cleanups.push(addAuditListener(listener)); + await expect(recordAudit(memoryKV(true) as never, "remember", "mem::test", ["mem_a"])).rejects.toThrow("write failed"); + expect(listener).not.toHaveBeenCalled(); + }); +}); diff --git a/test/consolidation-summary-counts.test.ts b/test/consolidation-summary-counts.test.ts new file mode 100644 index 0000000000..0489798d52 --- /dev/null +++ b/test/consolidation-summary-counts.test.ts @@ -0,0 +1,58 @@ +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 } = {}) { + 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") { + 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 }), calls }; +} + +describe("consolidation summary counts", () => { + it("counts summaries by key without reading their values", async () => { + const { kv, calls } = fixture("file"); + const status = await createConsolidationStatusReader(kv)({ lastRun: null }); + expect(status.summaries).toBe(3); + expect(calls.filter((call) => call.payload.scope === KV.summaries && ["state::list", "state::get"].includes(call.function_id))).toEqual([]); + }); + + 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); + }); +}); 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); + }); +}); diff --git a/test/insight-list.test.ts b/test/insight-list.test.ts new file mode 100644 index 0000000000..556367ed54 --- /dev/null +++ b/test/insight-list.test.ts @@ -0,0 +1,64 @@ +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 Row = { id: string; confidence: number; createdAt: string; project?: string; deleted?: boolean }; + +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 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)]); + }, + }; + 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)); +} + +const ties = [row("late", 0.5, 9), row("early", 0.5, 1), row("top", 0.9, 5), row("mid", 0.5, 3)]; + +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("breaks confidence ties by read order on the file backend", async () => { + expect(await insightList(ties, "file")({ limit: 3 })).toEqual(["top", "late", "early"]); + }); + + 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("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([]); + }); +}); diff --git a/test/observe-durable-lookup.test.ts b/test/observe-durable-lookup.test.ts new file mode 100644 index 0000000000..a7cce6cd4d --- /dev/null +++ b/test/observe-durable-lookup.test.ts @@ -0,0 +1,125 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { registerObserveFunction } from "../src/functions/observe.js"; +import { applyUpdateOps, StateKV } from "../src/state/kv.js"; +import { KV } from "../src/state/schema.js"; +import type { CompressedObservation, HookPayload } from "../src/types.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: [], importance: 5, + }; +} + +function payload(overrides: Partial = {}): HookPayload { + return { + 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, + }; +} + +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 }]])], + ]); + 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") { + return structuredClone([...entries.values()]); + } + 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; + } + 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); + 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, observe, observationLists }; +} + +beforeEach(() => { + indexed.clear(); + vi.stubEnv("AGENTMEMORY_AUTO_COMPRESS", "false"); +}); +afterEach(() => vi.unstubAllEnvs()); + +describe("durable observation lookup", () => { + 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()); + expect(f.data).toEqual(before); + expect(f.observationLists()).toEqual([]); + }); + + 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(f.observationLists()).toHaveLength(1); + }); + + 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.has("obs_wrong_id1")).toBe(false); + expect(f.observationLists()).toHaveLength(1); + }); + + it("writes a new durable observation after one session list", async () => { + const f = fixture(); + f.data.get(SCOPE)!.delete(ID); + expect(await f.observe()).toMatchObject({ observationId: ID }); + expect(f.data.get(SCOPE)!.get(ID)).toMatchObject({ id: ID, sessionId: SESSION }); + expect(f.observationLists()).toHaveLength(1); + }); +}); 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"] +}