diff --git a/.env.example b/.env.example index fcb32b8a83..79f1112aaa 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,5 @@ -# ============================================================================= -# agentmemory configuration -# ============================================================================= -# +# ======================================================================# agentmemory configuration +# ======================================================================# # Copy this file to `~/.agentmemory/.env` (or to your project root if you # prefer scoped config) and uncomment the lines you want to override. # @@ -225,6 +223,10 @@ # AGENTMEMORY_BINDINGS=auto # bindings mode for HTTP routes and state events: auto (observe when supported), observe, trigger # AGENTMEMORY_STATE_READY_TIMEOUT_MS=60000 # max wait for the state probe reported as runtime.stateWorker in /status (1000-600000) # AGENTMEMORY_ROUTE_MAX_IN_FLIGHT=1024 # max concurrent requests per observer-bound route (1-65536) +# AGENTMEMORY_COMPRESS_MAX_CONCURRENT=2 # max concurrent LLM compressions via the capture observer (1-32) +# AGENTMEMORY_COMPRESSION_SWEEP_MS=600000 # pending-compression recovery sweep interval; 0 disables (0 or 60000-86400000) +# AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS=0 # min ms between per-session graph extractions (0-86400000; 0 = no cooldown) +# AGENTMEMORY_GRAPH_STRUCTURAL=true # heuristic/structural graph pass; false leaves only the LLM pass # Engine usage telemetry (iii). Off unless you set it; true opts in. # III_TELEMETRY_ENABLED=false diff --git a/deploy/coolify/docker-compose.yml b/deploy/coolify/docker-compose.yml index 0f869fc5ba..6e5399f626 100644 --- a/deploy/coolify/docker-compose.yml +++ b/deploy/coolify/docker-compose.yml @@ -64,6 +64,10 @@ services: AGENTMEMORY_BINDINGS: "${AGENTMEMORY_BINDINGS:-}" AGENTMEMORY_STATE_READY_TIMEOUT_MS: "${AGENTMEMORY_STATE_READY_TIMEOUT_MS:-}" AGENTMEMORY_ROUTE_MAX_IN_FLIGHT: "${AGENTMEMORY_ROUTE_MAX_IN_FLIGHT:-}" + AGENTMEMORY_COMPRESS_MAX_CONCURRENT: "${AGENTMEMORY_COMPRESS_MAX_CONCURRENT:-}" + AGENTMEMORY_COMPRESSION_SWEEP_MS: "${AGENTMEMORY_COMPRESSION_SWEEP_MS:-}" + AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS: "${AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS:-}" + AGENTMEMORY_GRAPH_STRUCTURAL: "${AGENTMEMORY_GRAPH_STRUCTURAL:-}" AGENTMEMORY_SECRET: "${AGENTMEMORY_SECRET:-}" AGENT_ID: "${AGENT_ID:-}" TEAM_ID: "${TEAM_ID:-}" diff --git a/deploy/runtime/worker-compose.yaml b/deploy/runtime/worker-compose.yaml index 3d0a343653..8b1bc7e37e 100644 --- a/deploy/runtime/worker-compose.yaml +++ b/deploy/runtime/worker-compose.yaml @@ -123,6 +123,10 @@ containers: AGENTMEMORY_BINDINGS: "${AGENTMEMORY_BINDINGS:-}" AGENTMEMORY_STATE_READY_TIMEOUT_MS: "${AGENTMEMORY_STATE_READY_TIMEOUT_MS:-}" AGENTMEMORY_ROUTE_MAX_IN_FLIGHT: "${AGENTMEMORY_ROUTE_MAX_IN_FLIGHT:-}" + AGENTMEMORY_COMPRESS_MAX_CONCURRENT: "${AGENTMEMORY_COMPRESS_MAX_CONCURRENT:-}" + AGENTMEMORY_COMPRESSION_SWEEP_MS: "${AGENTMEMORY_COMPRESSION_SWEEP_MS:-}" + AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS: "${AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS:-}" + AGENTMEMORY_GRAPH_STRUCTURAL: "${AGENTMEMORY_GRAPH_STRUCTURAL:-}" AGENTMEMORY_SECRET: "${AGENTMEMORY_SECRET:-}" AGENT_ID: "${AGENT_ID:-}" TEAM_ID: "${TEAM_ID:-}" diff --git a/docker-compose.yml b/docker-compose.yml index 23ae9bc97c..03e392603f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -68,6 +68,10 @@ services: AGENTMEMORY_BINDINGS: "${AGENTMEMORY_BINDINGS:-}" AGENTMEMORY_STATE_READY_TIMEOUT_MS: "${AGENTMEMORY_STATE_READY_TIMEOUT_MS:-}" AGENTMEMORY_ROUTE_MAX_IN_FLIGHT: "${AGENTMEMORY_ROUTE_MAX_IN_FLIGHT:-}" + AGENTMEMORY_COMPRESS_MAX_CONCURRENT: "${AGENTMEMORY_COMPRESS_MAX_CONCURRENT:-}" + AGENTMEMORY_COMPRESSION_SWEEP_MS: "${AGENTMEMORY_COMPRESSION_SWEEP_MS:-}" + AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS: "${AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS:-}" + AGENTMEMORY_GRAPH_STRUCTURAL: "${AGENTMEMORY_GRAPH_STRUCTURAL:-}" AGENTMEMORY_SECRET: "${AGENTMEMORY_SECRET:-}" AGENT_ID: "${AGENT_ID:-}" TEAM_ID: "${TEAM_ID:-}" diff --git a/docs/upgrades/iii-0.25.md b/docs/upgrades/iii-0.25.md index 8c84b76d80..79a077f7f1 100644 --- a/docs/upgrades/iii-0.25.md +++ b/docs/upgrades/iii-0.25.md @@ -23,6 +23,10 @@ Existing file state remains at `state_store.db`; this upgrade does not convert o - The viewer remains on port 3113 by default. Live updates use authenticated SSE at `/agentmemory/viewer/events` on the REST port. Port 3112 is no longer used. - `/agentmemory/status` reports the configured engine pin as `service.enginePinnedVersion`, replacing the old `engineVersion` field. This is the supported version, not a probe of an arbitrary remote engine. - HTTP routes and state subscriptions are bound through `src/runtime/bindings.ts` (`getBindings(sdk).http/.state`). In `auto` mode (default) agentmemory attaches native observers to `http::on-request` and `state::on-changed` when the providers advertise them, and falls back to `registerTrigger` otherwise. `AGENTMEMORY_BINDINGS=observe|trigger` forces a mode. `AGENTMEMORY_STATE_READY_TIMEOUT_MS` (1000-600000, default 60000) bounds the state-worker readiness probe before boot reads; late arrival is covered by event-driven re-arm. `AGENTMEMORY_ROUTE_MAX_IN_FLIGHT` (1-65536, default 1024) bounds concurrent deliveries per observer-bound route. + +### Background work runs through observers + +With `AGENTMEMORY_BINDINGS` unset or `auto` (and an SDK that supports `observe`), background work is dispatched by engine observers instead of inline fire-and-forget triggers: per-observation LLM compression follows `mem::observe` output (`agentmemory:pipeline:compress-on-capture`, bounded by `AGENTMEMORY_COMPRESS_MAX_CONCURRENT`, default 2), post-session graph extraction/reflection/consolidation follow `event::session::stopped`, and graph extraction on `mem::remember` is observe-only. `AGENTMEMORY_COMPRESSION_SWEEP_MS` (default 600000, 0 disables) runs a recovery sweep for observations still pending compression. `AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS` (default 0) rate-limits per-session delta extraction, `AGENTMEMORY_GRAPH_STRUCTURAL=false` disables the heuristic graph pass (LLM pass only), and per-session markers in `mem:graph:extract-marks` dedupe extraction to new observations only. In `trigger` mode the existing inline fan-out is unchanged. - iii 0.25 can dispatch calls to a worker's own functions locally. Engine-side spans differ for those calls; SDK tracing and state-worker persistence still apply. - Local dispatch is on by default, so AgentMemory's internal calls to its own functions run in-process and produce only the SDK's execute span, not an engine call span. Set `AGENTMEMORY_LOCAL_DISPATCH=false` for full engine traces at the cost of a round trip per call. diff --git a/plugin/skills/agentmemory-config/REFERENCE.md b/plugin/skills/agentmemory-config/REFERENCE.md index 6a6a7b2a63..1a51f881ea 100644 --- a/plugin/skills/agentmemory-config/REFERENCE.md +++ b/plugin/skills/agentmemory-config/REFERENCE.md @@ -3,7 +3,7 @@ Generated by scanning `src/` for `AGENTMEMORY_*` usage. Do not edit the block below by hand; run `npm run skills:gen` after adding or removing a variable. Internal markers ending in two underscores are excluded. -Configuration is read from the environment and from `~/.agentmemory/.env` (no `export` prefix). 84 recognized variables: +Configuration is read from the environment and from `~/.agentmemory/.env` (no `export` prefix). 88 recognized variables: - `AGENTMEMORY_AGENT_SCOPE` - `AGENTMEMORY_ALLOW_AGENT_SDK` @@ -28,6 +28,8 @@ Configuration is read from the environment and from `~/.agentmemory/.env` (no `e - `AGENTMEMORY_CAPTURE_SPOOL_MAX_AGE_HOURS` - `AGENTMEMORY_CAPTURE_SPOOL_MAX_BYTES` - `AGENTMEMORY_COMMIT_SHA` +- `AGENTMEMORY_COMPRESSION_SWEEP_MS` +- `AGENTMEMORY_COMPRESS_MAX_CONCURRENT` - `AGENTMEMORY_CONSOLIDATION_COOLDOWN_MS` - `AGENTMEMORY_COPILOT_MCP_BLOCK` - `AGENTMEMORY_CREDENTIAL_MODE` @@ -45,6 +47,8 @@ Configuration is read from the environment and from `~/.agentmemory/.env` (no `e - `AGENTMEMORY_FOLLOWUP_WINDOW_SECONDS` - `AGENTMEMORY_FORCE_PROXY` - `AGENTMEMORY_GRAPH_COMPACT_ON_BOOT` +- `AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS` +- `AGENTMEMORY_GRAPH_STRUCTURAL` - `AGENTMEMORY_GRAPH_WEIGHT` - `AGENTMEMORY_III_VERSION` - `AGENTMEMORY_IMAGE_EMBEDDINGS` diff --git a/scripts/docs-sync.state.json b/scripts/docs-sync.state.json index 7fef36b3bd..e0a7521b78 100644 --- a/scripts/docs-sync.state.json +++ b/scripts/docs-sync.state.json @@ -23,16 +23,16 @@ "4600+" ], "functions": [ - "323" + "328" ], "sourceFiles": [ - "263" + "266" ], "loc": [ "61000" ], "kvScopes": [ - "65" + "66" ] }, "engineVersion": "0.25.1" diff --git a/src/cli/compose-project.ts b/src/cli/compose-project.ts index 461d290ea6..9ca0819bb1 100644 --- a/src/cli/compose-project.ts +++ b/src/cli/compose-project.ts @@ -92,6 +92,10 @@ const AGENTMEMORY_WORKER_ENV_ALLOWLIST = new Set([ "AGENTMEMORY_BINDINGS", "AGENTMEMORY_STATE_READY_TIMEOUT_MS", "AGENTMEMORY_ROUTE_MAX_IN_FLIGHT", + "AGENTMEMORY_COMPRESS_MAX_CONCURRENT", + "AGENTMEMORY_COMPRESSION_SWEEP_MS", + "AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS", + "AGENTMEMORY_GRAPH_STRUCTURAL", ]); const COMPOSE_RESERVED_ENV = new Set([ diff --git a/src/config.ts b/src/config.ts index 53823f19c7..df269fd557 100644 --- a/src/config.ts +++ b/src/config.ts @@ -557,6 +557,35 @@ export function isAutoCompressEnabled(): boolean { return getMergedEnv()["AGENTMEMORY_AUTO_COMPRESS"] === "true"; } +export function compressMaxConcurrent(): number { + const raw = getMergedEnv()["AGENTMEMORY_COMPRESS_MAX_CONCURRENT"]; + if (raw === undefined) return 2; + const parsed = Number(raw); + if (!Number.isInteger(parsed) || parsed < 1 || parsed > 32) return 2; + return parsed; +} + +export function compressionSweepMs(): number { + const raw = getMergedEnv()["AGENTMEMORY_COMPRESSION_SWEEP_MS"]; + if (raw === undefined) return 600_000; + const parsed = Number(raw); + if (parsed === 0) return 0; + if (!Number.isInteger(parsed) || parsed < 60_000 || parsed > 86_400_000) return 600_000; + return parsed; +} + +export function graphExtractCooldownMs(): number { + const raw = getMergedEnv()["AGENTMEMORY_GRAPH_EXTRACT_COOLDOWN_MS"]; + if (raw === undefined) return 0; + const parsed = Number(raw); + if (!Number.isInteger(parsed) || parsed < 0 || parsed > 86_400_000) return 0; + return parsed; +} + +export function isGraphStructuralEnabled(): boolean { + return getMergedEnv()["AGENTMEMORY_GRAPH_STRUCTURAL"] !== "false"; +} + // Hook-level context injection into Claude Code's conversation is OFF by // default as of 0.8.10. When disabled, pre-tool-use and // session-start hooks still POST observations for background capture, but diff --git a/src/functions/compression-sweep.ts b/src/functions/compression-sweep.ts new file mode 100644 index 0000000000..d2e222fcaa --- /dev/null +++ b/src/functions/compression-sweep.ts @@ -0,0 +1,59 @@ +import type { IIIClient } from "iii-sdk"; +import type { StateKV } from "../state/kv.js"; +import { collectAll } from "../state/kv.js"; +import { KV } from "../state/schema.js"; +import type { CompressedObservation, Session } from "../types.js"; +import { compressMaxConcurrent, isAutoCompressEnabled } from "../config.js"; +import { needsCompression } from "./compression-state.js"; +import { logger } from "../logger.js"; +import { singleFlight } from "../runtime/recovery.js"; + +const MAX_AGE_MS = 5 * 60_000; +const MAX_PER_RUN = 50; + +export function createCompressionSweep(sdk: IIIClient, kv: StateKV) { + return singleFlight(async () => { + const candidates: Array<{ sessionId: string; observationId: string }> = []; + const cutoff = Date.now() - MAX_AGE_MS; + const sessions = await kv.list(KV.sessions); + for (const session of sessions) { + const observations = await collectAll(kv, KV.observations(session.id)); + for (const obs of observations) { + if (candidates.length >= MAX_PER_RUN) break; + if (!needsCompression(obs)) continue; + if (Date.parse(obs.timestamp) > cutoff) continue; + candidates.push({ sessionId: session.id, observationId: obs.id }); + } + if (candidates.length >= MAX_PER_RUN) break; + } + const concurrency = Math.min(compressMaxConcurrent(), Math.max(1, candidates.length)); + let inFlight = 0; + let dispatched = 0; + await new Promise((resolve) => { + const next = () => { + while (inFlight < concurrency && dispatched < candidates.length) { + const c = candidates[dispatched++]; + inFlight++; + sdk.trigger({ function_id: "mem::compress", payload: c }) + .catch(() => {}) + .finally(() => { inFlight--; if (dispatched >= candidates.length && inFlight === 0) resolve(); else next(); }); + } + if (dispatched >= candidates.length && inFlight === 0) resolve(); + }; + next(); + if (candidates.length === 0) resolve(); + }); + if (candidates.length > 0) logger.info("Compression sweep dispatched stale observations", { count: candidates.length }); + return { dispatched: candidates.length }; + }); +} + +export function registerCompressionSweepFunction(sdk: IIIClient, kv: StateKV): () => Promise<{ dispatched: number }> { + const run = createCompressionSweep(sdk, kv); + sdk.registerFunction("mem::compression-sweep", () => run()); + return run; +} + +export function compressionSweepEnabled(): boolean { + return isAutoCompressEnabled(); +} diff --git a/src/functions/graph.ts b/src/functions/graph.ts index 096a4872ca..668bb6db71 100644 --- a/src/functions/graph.ts +++ b/src/functions/graph.ts @@ -18,7 +18,7 @@ import { GRAPH_EXTRACTION_SYSTEM, buildGraphExtractionPrompt, } from "../prompts/graph-extraction.js"; -import { isGraphExtractionEnabled } from "../config.js"; +import { isGraphExtractionEnabled, isGraphStructuralEnabled } from "../config.js"; import { recordAudit, safeAudit } from "./audit.js"; import { logger } from "../logger.js"; import { scrubRecord } from "./privacy.js"; @@ -730,14 +730,16 @@ export function registerGraphFunction( let nodes: GraphNode[] = []; let edges: GraphEdge[] = []; - try { - const heuristic = extractGraphHeuristics(data.observations); - nodes = heuristic.nodes; - edges = heuristic.edges; - } catch (err) { - logger.warn("heuristic graph extraction failed", { - error: err instanceof Error ? err.message : String(err), - }); + if (isGraphStructuralEnabled()) { + try { + const heuristic = extractGraphHeuristics(data.observations); + nodes = heuristic.nodes; + edges = heuristic.edges; + } catch (err) { + logger.warn("heuristic graph extraction failed", { + error: err instanceof Error ? err.message : String(err), + }); + } } const llmEnabled = diff --git a/src/functions/observe.ts b/src/functions/observe.ts index 818c1258a7..46b1747ffd 100644 --- a/src/functions/observe.ts +++ b/src/functions/observe.ts @@ -10,6 +10,7 @@ import { indexObservationSession } from "../state/obs-index.js"; import { scrubRecord } from "./privacy.js"; import { DedupMap, recordDedupSkip } from "./dedup.js"; import { afterKeyedLock, withKeyedLock } from "../state/keyed-mutex.js"; +import { getBindings } from "../runtime/bindings.js"; import { getFinalizeIdleMs, isAutoCompressEnabled } from "../config.js"; import { buildSyntheticCompression } from "./compress-synthetic.js"; import { isCaptureKey } from "../capture/event-record.js"; @@ -388,7 +389,7 @@ export function registerObserveFunction( }, }); - if (autoCompress) { + if (autoCompress && getBindings(sdk).pipeline !== "observe") { afterKeyedLock(`obs:${payload.sessionId}`, async () => { try { await sdk.trigger({ @@ -408,7 +409,7 @@ export function registerObserveFunction( hook: payload.hookType, compress: autoCompress ? "llm" : "synthetic", }); - return { observationId: obsId }; + return { observationId: obsId, sessionId: payload.sessionId }; }); }, ); diff --git a/src/functions/post-session.ts b/src/functions/post-session.ts new file mode 100644 index 0000000000..88beab12a3 --- /dev/null +++ b/src/functions/post-session.ts @@ -0,0 +1,87 @@ +import type { IIIClient } from "iii-sdk"; +import { TriggerAction } from "iii-sdk"; +import type { StateKV } from "../state/kv.js"; +import { collectAll } from "../state/kv.js"; +import { KV } from "../state/schema.js"; +import type { CompressedObservation, GraphExtractMark, Memory } from "../types.js"; +import { graphExtractCooldownMs, isConsolidationEnabled, detectLlmProviderKind } from "../config.js"; +import { isReflectEnabled } from "./slots.js"; +import { logger } from "../logger.js"; +import { consolidationDue } from "../triggers/events.js"; +import { getGraphGeneration } from "../state/graph-generation.js"; + +const graphStamp = (o: CompressedObservation): string => o.compressedAt ?? o.timestamp; + +const fireVoid = (sdk: IIIClient, function_id: string, payload: unknown, sessionId: string) => + sdk + .trigger({ function_id, payload, action: TriggerAction.Void() }) + .catch((err) => + logger.warn(function_id + " trigger failed", { + sessionId, + error: err instanceof Error ? err.message : String(err), + }), + ); + +export function registerPostSessionFunctions(sdk: IIIClient, kv: StateKV): void { + sdk.registerFunction("mem::post-session-graph", async (data: { sessionId: string }) => { + const stored = await kv.get(KV.graphExtractMarks, data.sessionId); + const graphGeneration = await getGraphGeneration(kv); + const marker = stored && (stored.graphGeneration ?? null) === graphGeneration ? stored : null; + const cooldownMs = graphExtractCooldownMs(); + if (marker && cooldownMs > 0 && Date.now() - Date.parse(marker.updatedAt) < cooldownMs) { + return { skipped: "cooldown", updatedAt: marker.updatedAt }; + } + const since = marker ? Date.parse(marker.lastObservationTimestamp) : 0; + const observations = await collectAll(kv, KV.observations(data.sessionId)); + const fresh = observations.filter((o) => o.title && Date.parse(graphStamp(o)) > since); + if (fresh.length === 0) return { extracted: 0 }; + const result = await sdk.trigger<{ observations: CompressedObservation[] }, { success?: boolean; error?: string } | null>({ + function_id: "mem::graph-extract", + payload: { observations: fresh }, + }); + if (result?.success === false) return { extracted: 0, error: result.error ?? "graph extraction failed" }; + const maxTs = fresh.reduce((max, o) => (graphStamp(o) > max ? graphStamp(o) : max), marker?.lastObservationTimestamp ?? ""); + await kv.set(KV.graphExtractMarks, data.sessionId, { + sessionId: data.sessionId, + lastObservationTimestamp: maxTs, + extractedCount: (marker?.extractedCount ?? 0) + fresh.length, + updatedAt: new Date().toISOString(), + graphGeneration, + } satisfies GraphExtractMark); + return { extracted: fresh.length }; + }); + + sdk.registerFunction("mem::post-session-reflect", async (data: { sessionId: string }) => { + if (!isReflectEnabled()) return { skipped: "reflect_disabled" }; + return sdk.trigger({ function_id: "mem::slot-reflect", payload: { sessionId: data.sessionId } }); + }); + + sdk.registerFunction("mem::post-session-consolidate", async (data: { sessionId: string; skipConsolidation?: boolean }) => { + if (!isConsolidationEnabled()) return { skipped: "consolidation_disabled" }; + if (!(await consolidationDue(kv))) return { skipped: "debounced" }; + fireVoid(sdk, "mem::consolidate-pipeline", { tier: "all", force: true }, data.sessionId); + fireVoid(sdk, "mem::auto-crystallize", { olderThanDays: 0 }, data.sessionId); + if (detectLlmProviderKind() === "llm") { + fireVoid(sdk, "mem::skill-extract", { sessionId: data.sessionId }, data.sessionId); + } + return { fired: true }; + }); + + sdk.registerFunction("mem::post-remember-graph", async (data: { input?: { content?: string; type?: string; concepts?: string[] }; output?: { success?: boolean; memory?: Memory } }) => { + const memory = data.output?.memory; + if (!data.output?.success || !memory) return { skipped: "no_memory" }; + const content = typeof data.input?.content === "string" ? data.input.content : ""; + const observation = { + id: memory.id, + title: memory.title || content.slice(0, 120) || memory.type, + narrative: content, + type: "compressed_observation", + timestamp: memory.createdAt, + sessionId: `memory:${memory.id}`, + hookType: "remember", + concepts: data.input?.concepts ?? memory.concepts ?? [], + files: memory.files ?? [], + } as unknown as CompressedObservation; + return sdk.trigger({ function_id: "mem::graph-extract", payload: { observations: [observation] } }); + }); +} diff --git a/src/index.ts b/src/index.ts index f43b8fc82a..75ae0cb1cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { loadTeamConfig, loadSnapshotConfig, isGraphExtractionEnabled, + isGraphStructuralEnabled, isAutoCompressEnabled, isConsolidationEnabled, getConsolidationIntervalMs, @@ -136,6 +137,10 @@ import { getBindings } from "./runtime/bindings.js"; import { waitForStateWorker, onStateWorkerReturn } from "./runtime/state-worker.js"; import { singleFlight, shouldRearmIndex, rearmIndexRecovery, backfillFailedOutright } from "./runtime/recovery.js"; import { checkDuplicateWorkers } from "./runtime/duplicates.js"; +import { registerPipelineObservers } from "./runtime/pipeline-observers.js"; +import { registerPostSessionFunctions } from "./functions/post-session.js"; +import { registerCompressionSweepFunction } from "./functions/compression-sweep.js"; +import { compressionSweepMs } from "./config.js"; import { ensureServerSecret, explicitSecret, secretFilePath } from "./secret-store.js"; import { mkdirSync, writeFileSync, unlinkSync } from "node:fs"; import { dirname } from "node:path"; @@ -350,7 +355,7 @@ async function main() { const graphIndexMigration = registerGraphIndexMigrationFunctions(sdk, kv); registerGraphImportFunction(sdk, kv); bootLog( - `Knowledge graph: structural extraction on (LLM relations ${isGraphExtractionEnabled() ? "enabled" : "off"})`, + `Knowledge graph: structural extraction ${isGraphStructuralEnabled() ? "on" : "off"} (LLM relations ${isGraphExtractionEnabled() ? "enabled" : "off"})`, ); registerConsolidationPipelineFunction(sdk, kv, provider); @@ -470,14 +475,25 @@ async function main() { markKeywordRebuildPending(); registerApiTriggers(sdk, kv, secret, metricsStore, provider); registerEventTriggers(sdk, kv); + registerPostSessionFunctions(sdk, kv); + const runCompressionSweep = registerCompressionSweepFunction(sdk, kv); registerViewerStreamTriggers(sdk, kv, { secret, metricsStore, provider }); registerViewerSse(sdk); registerMcpEndpoints(sdk, kv, secret); const healthMonitor = registerHealthMonitor(sdk, kv); + registerPipelineObservers(sdk); + const bindings = getBindings(sdk); await bindings.activate(); + if (isAutoCompressEnabled()) { + const sweepMs = compressionSweepMs(); + if (sweepMs > 0) { + const sweepTimer = setInterval(() => { void runCompressionSweep(); }, sweepMs); + sweepTimer.unref(); + } + } bootLog(`Bindings: state=${bindings.modes.state} http=${bindings.modes.http} (${bindings.handles.length} observers)`); await waitForStateWorker(sdk); diff --git a/src/runtime/bindings.ts b/src/runtime/bindings.ts index 7c80a32460..31751afa9d 100644 --- a/src/runtime/bindings.ts +++ b/src/runtime/bindings.ts @@ -29,6 +29,12 @@ interface PendingSpec { spec: HttpTriggerSpec | StateSpec; } +interface PipelineSpec { + source: Record; + target: string; + options?: Record; +} + export interface BindingReport { state: "observe" | "trigger"; http: "observe" | "trigger"; @@ -57,6 +63,8 @@ const OBSERVE_CAPABLE_HTTP_KEYS = new Set([ export class Bindings { private readonly sdk: SdkLike; private readonly pending: PendingSpec[] = []; + private readonly pipelinePending: PipelineSpec[] = []; + private pipelineMode: "observe" | "inline" = "inline"; private activated: Promise | null = null; private readonly boundHandles: ObserverHandle[] = []; private readonly stateBoundHandles: ObserverHandle[] = []; @@ -100,6 +108,25 @@ export class Bindings { this.pending.push({ kind: "state", spec }); } + get pipeline(): "observe" | "inline" { + return this.pipelineMode; + } + + observePipeline(source: Record, target: string, options?: Record): ObserverHandle | null { + if (!this.resolved && !this.immediate()) { + this.pipelinePending.push({ source, target, options }); + return null; + } + if (this.pipelineMode !== "observe" || typeof this.sdk.observe !== "function") return null; + const handle = this.sdk.observe(source, target, options); + this.boundHandles.push(handle); + void handle.ready().catch((err: unknown) => { + const code = (err as { code?: string })?.code; + logger.error(`Pipeline observer failed for ${target}`, { error: code ?? String(err) }); + }); + return handle; + } + private bindHttp(spec: HttpTriggerSpec): void { if (this.modes.http === "observe") return this.observeHttp(spec); this.routesViaTriggerCount++; @@ -237,10 +264,14 @@ export class Bindings { this.activated = this.detectModes().then((modes) => { this.modes = modes; this.resolved = true; + this.pipelineMode = typeof this.sdk.observe === "function" && process.env.AGENTMEMORY_BINDINGS !== "trigger" ? "observe" : "inline"; for (const { spec } of this.pending.splice(0)) { if ("type" in spec && spec.type === "http") this.bindHttp(spec as HttpTriggerSpec); else this.bindState(spec as StateSpec); } + for (const spec of this.pipelinePending.splice(0)) { + this.observePipeline(spec.source, spec.target, spec.options); + } return modes; }); } diff --git a/src/runtime/pipeline-observers.ts b/src/runtime/pipeline-observers.ts new file mode 100644 index 0000000000..cbee0ce8c5 --- /dev/null +++ b/src/runtime/pipeline-observers.ts @@ -0,0 +1,45 @@ +import type { IIIClient } from "iii-sdk"; +import { getBindings } from "./bindings.js"; +import { compressMaxConcurrent, isAutoCompressEnabled, isGraphExtractionEnabled } from "../config.js"; +import { isReflectEnabled } from "../functions/slots.js"; + +export function registerPipelineObservers(sdk: IIIClient): void { + const bindings = getBindings(sdk); + if (isAutoCompressEnabled()) { + bindings.observePipeline( + { + function_id: "mem::observe", + filters: { "output.observationId": { $exists: true }, "output.deduplicated": { $ne: true } }, + capture: ["output"], + deliver: "output", + }, + "mem::compress", + { id: "agentmemory:pipeline:compress-on-capture", max_in_flight: compressMaxConcurrent() }, + ); + } + const sessionFilters = { "output.unchanged": { $ne: true } }; + bindings.observePipeline( + { function_id: "event::session::stopped", filters: sessionFilters, capture: ["input", "output"], deliver: "input" }, + "mem::post-session-graph", + { id: "agentmemory:pipeline:post-session-graph", max_in_flight: 1 }, + ); + if (isReflectEnabled()) { + bindings.observePipeline( + { function_id: "event::session::stopped", filters: sessionFilters, capture: ["input", "output"], deliver: "input" }, + "mem::post-session-reflect", + { id: "agentmemory:pipeline:post-session-reflect", max_in_flight: 1 }, + ); + } + bindings.observePipeline( + { function_id: "event::session::stopped", filters: { ...sessionFilters, "input.skipConsolidation": { $ne: true } }, capture: ["input", "output"], deliver: "input" }, + "mem::post-session-consolidate", + { id: "agentmemory:pipeline:post-session-consolidate", max_in_flight: 1 }, + ); + if (isGraphExtractionEnabled()) { + bindings.observePipeline( + { function_id: "mem::remember", capture: ["input", "output"] }, + "mem::post-remember-graph", + { id: "agentmemory:pipeline:post-remember-graph", max_in_flight: 1 }, + ); + } +} diff --git a/src/state/schema.ts b/src/state/schema.ts index 03bae2f02b..885f772434 100644 --- a/src/state/schema.ts +++ b/src/state/schema.ts @@ -28,6 +28,7 @@ export const KV = { graphRetrievalData: "mem:graph:retrieval-data", graphIndexMigration: "mem:graph:index-migration", graphIndexManifest: "mem:graph:index-manifest", + graphExtractMarks: "mem:graph:extract-marks", semantic: "mem:semantic", procedural: "mem:procedural", teamShared: (teamId: string) => `mem:team:${teamId}:shared`, diff --git a/src/triggers/events.ts b/src/triggers/events.ts index 73a31f1ed1..a6f2a4aedf 100644 --- a/src/triggers/events.ts +++ b/src/triggers/events.ts @@ -38,7 +38,7 @@ async function consolidationDueUnserialized(kv: StateKV): Promise { // check through an in-process chain so exactly one concurrent caller wins. let consolidationCheckChain: Promise = Promise.resolve(); -function consolidationDue(kv: StateKV): Promise { +export function consolidationDue(kv: StateKV): Promise { const result = consolidationCheckChain.then(() => consolidationDueUnserialized(kv), ); @@ -54,6 +54,7 @@ export function registerEventTriggers(sdk: IIIClient, kv: StateKV): void { timeoutMs: getSummaryTriggerTimeoutMs(), }); if (summary?.unchanged === true) return summary; + if (getBindings(sdk).pipeline === "observe") return summary; const fireVoid = (function_id: string, payload: unknown) => sdk .trigger({ function_id, payload, action: TriggerAction.Void() }) diff --git a/src/types.ts b/src/types.ts index 269da0c576..16e43bfd7d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,11 @@ +export interface GraphExtractMark { + sessionId: string; + lastObservationTimestamp: string; + extractedCount: number; + updatedAt: string; + graphGeneration?: string | null; +} + export interface Session { id: string; project: string; diff --git a/test/observer-pipeline.test.ts b/test/observer-pipeline.test.ts new file mode 100644 index 0000000000..7355ea70b9 --- /dev/null +++ b/test/observer-pipeline.test.ts @@ -0,0 +1,328 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; +import { Bindings, getBindings } from "../src/runtime/bindings.js"; +import { registerPipelineObservers } from "../src/runtime/pipeline-observers.js"; +import { KV } from "../src/state/schema.js"; +import type { GraphExtractMark } from "../src/types.js"; + +vi.mock("../src/logger.js", () => ({ + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, +})); +vi.mock("../src/config.js", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, isAutoCompressEnabled: () => envFlags.autoCompress, isReflectEnabled: () => envFlags.reflect, isGraphExtractionEnabled: () => envFlags.graphLlm, isConsolidationEnabled: () => envFlags.consolidation, compressMaxConcurrent: () => envFlags.maxConcurrent, isGraphStructuralEnabled: () => envFlags.structural }; +}); +vi.mock("../src/functions/slots.js", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, isReflectEnabled: () => envFlags.reflect }; +}); + +const envFlags = { autoCompress: true, reflect: false, graphLlm: false, consolidation: true, maxConcurrent: 3, structural: true }; + +function handle() { + return { id: "h", state: "active", ready: () => Promise.resolve(), onStateChange: () => () => {}, dispose: () => Promise.resolve() }; +} + +function fakeSdk(functions: string[] = ["state::get", "http::x", "state::on-changed", "http::on-request"]) { + const observed: Array<{ source: Record; target: string; options?: Record }> = []; + const triggers: Array<{ function_id: string; payload: unknown }> = []; + const sdk = { + observed, + triggers, + registerTrigger: vi.fn(), + registerFunction: vi.fn((id: string, handler: unknown) => { + (sdk as Record)[`fn:${id}`] = handler; + }), + observe: vi.fn((source: Record, target: string, options?: Record) => { + observed.push({ source, target, options }); + return handle(); + }), + trigger: vi.fn(async (req: { function_id: string; payload: unknown }) => { + triggers.push({ function_id: req.function_id, payload: req.payload }); + if (req.function_id === "engine::functions::list") return { functions }; + return {}; + }), + }; + return sdk; +} + +async function activatedBindings(sdk: ReturnType) { + const b = new Bindings(sdk as never); + await b.activate(); + return b; +} + +describe("observer pipeline", () => { + beforeEach(() => { + envFlags.autoCompress = true; envFlags.reflect = false; envFlags.graphLlm = false; + envFlags.consolidation = true; envFlags.maxConcurrent = 3; envFlags.structural = true; + process.env.AGENTMEMORY_BINDINGS = undefined; + }); + afterEach(() => { process.env.AGENTMEMORY_BINDINGS = undefined; }); + + it("pipeline is observe after activation with observe-capable sdk, inline before", async () => { + const sdk = fakeSdk(); + const b = new Bindings(sdk as never); + expect(b.pipeline).toBe("inline"); + await b.activate(); + expect(b.pipeline).toBe("observe"); + }); + + it("pipeline stays inline with AGENTMEMORY_BINDINGS=trigger and without observe", async () => { + process.env.AGENTMEMORY_BINDINGS = "trigger"; + const sdk = fakeSdk(); + const b = new Bindings(sdk as never); + await b.activate(); + expect(b.pipeline).toBe("inline"); + const sdk2 = { registerTrigger: vi.fn(), trigger: vi.fn() }; + const b2 = new Bindings(sdk2 as never); + await b2.activate(); + expect(b2.pipeline).toBe("inline"); + }); + + it("observePipeline buffers until activation then creates the observer", async () => { + const sdk = fakeSdk(); + const b = getBindings(sdk as never); + b.observePipeline({ function_id: "x::y" }, "mem::z", { id: "agentmemory:pipeline:test" }); + expect(sdk.observed).toHaveLength(0); + await b.activate(); + expect(sdk.observed).toHaveLength(1); + expect(sdk.observed[0].target).toBe("mem::z"); + expect(b.handles).toHaveLength(1); + }); + + it("observePipeline is a no-op in inline mode", async () => { + process.env.AGENTMEMORY_BINDINGS = "trigger"; + const sdk = fakeSdk(); + const b = getBindings(sdk as never); + await b.activate(); + expect(b.observePipeline({ function_id: "x::y" }, "mem::z", {})).toBeNull(); + expect(sdk.observed).toHaveLength(0); + }); + + it("registerPipelineObservers issues the compress observer spec", async () => { + const sdk = fakeSdk(); + const b = getBindings(sdk as never); + registerPipelineObservers(sdk as never); + await b.activate(); + const compress = sdk.observed.find((o) => o.options?.id === "agentmemory:pipeline:compress-on-capture")!; + expect(compress.source).toEqual({ + function_id: "mem::observe", + filters: { "output.observationId": { $exists: true }, "output.deduplicated": { $ne: true } }, + capture: ["output"], + deliver: "output", + }); + expect(compress.target).toBe("mem::compress"); + expect(compress.options?.max_in_flight).toBe(3); + }); + + it("compress observer absent when auto-compress disabled", async () => { + envFlags.autoCompress = false; + const sdk = fakeSdk(); + const b = getBindings(sdk as never); + registerPipelineObservers(sdk as never); + await b.activate(); + expect(sdk.observed.find((o) => o.options?.id === "agentmemory:pipeline:compress-on-capture")).toBeUndefined(); + }); + + it("post-session observers: graph always, reflect gated, consolidate filtered", async () => { + envFlags.reflect = true; + const sdk = fakeSdk(); + const b = getBindings(sdk as never); + registerPipelineObservers(sdk as never); + await b.activate(); + const graph = sdk.observed.find((o) => o.options?.id === "agentmemory:pipeline:post-session-graph")!; + expect(graph.source).toMatchObject({ function_id: "event::session::stopped", deliver: "input", filters: { "output.unchanged": { $ne: true } } }); + expect(graph.options?.max_in_flight).toBe(1); + const reflect = sdk.observed.find((o) => o.options?.id === "agentmemory:pipeline:post-session-reflect"); + expect(reflect).toBeTruthy(); + const consolidate = sdk.observed.find((o) => o.options?.id === "agentmemory:pipeline:post-session-consolidate")!; + expect((consolidate.source.filters as Record)["input.skipConsolidation"]).toEqual({ $ne: true }); + const remember = sdk.observed.find((o) => o.options?.id === "agentmemory:pipeline:post-remember-graph"); + expect(remember).toBeUndefined(); + }); + + it("reflect observer absent when reflect disabled; remember observer present when graph extraction enabled", async () => { + envFlags.reflect = false; + envFlags.graphLlm = true; + const sdk = fakeSdk(); + const b = getBindings(sdk as never); + registerPipelineObservers(sdk as never); + await b.activate(); + expect(sdk.observed.find((o) => o.options?.id === "agentmemory:pipeline:post-session-reflect")).toBeUndefined(); + const remember = sdk.observed.find((o) => o.options?.id === "agentmemory:pipeline:post-remember-graph")!; + expect(remember).toBeTruthy(); + expect(remember.source).toMatchObject({ function_id: "mem::remember", capture: ["input", "output"] }); + expect(remember.options?.max_in_flight).toBe(1); + }); +}); + +describe("post-session-graph delta marker", () => { + it("only extracts new observations and advances marker on success", async () => { + const { registerPostSessionFunctions } = await import("../src/functions/post-session.js"); + const sdk = { functions: new Map Promise>(), trigger: vi.fn(async (req: { function_id: string; payload: unknown }) => { + if (req.function_id === "mem::graph-extract") return { nodes: 1 }; + return {}; + }), registerFunction: function (id: string, h: (d: unknown) => Promise) { this.functions.set(id, h); } }; + const marks = new Map(); + const obs = [ + { id: "o1", title: "old", timestamp: "2026-01-01T00:00:00Z" }, + { id: "o2", title: "new1", timestamp: "2026-02-01T00:00:00Z" }, + { id: "o3", title: "new2", timestamp: "2026-03-01T00:00:00Z" }, + { id: "o4", title: "", timestamp: "2026-04-01T00:00:00Z" }, + ]; + const kv = { + get: vi.fn(async (_s: string, k: string) => marks.get(k) ?? null), + set: vi.fn(async (_s: string, k: string, v: GraphExtractMark) => { marks.set(k, v); }), + iterate: async function* () { yield obs; }, + backend: "file", + }; + marks.set("s1", { sessionId: "s1", lastObservationTimestamp: "2026-01-15T00:00:00Z", extractedCount: 1, updatedAt: "2026-01-15T00:00:00Z" }); + registerPostSessionFunctions(sdk as never, kv as never); + const result = await sdk.functions.get("mem::post-session-graph")!({ sessionId: "s1" }); + const call = sdk.trigger.mock.calls.find((c) => c[0].function_id === "mem::graph-extract")!; + expect((call[0].payload as { observations: Array<{ id: string }> }).observations.map((o) => o.id)).toEqual(["o2", "o3"]); + expect(marks.get("s1")!.lastObservationTimestamp).toBe("2026-03-01T00:00:00Z"); + expect(marks.get("s1")!.extractedCount).toBe(3); + expect(result).toEqual({ extracted: 2 }); + }); + + it("marker does not advance when extract fails", async () => { + const { registerPostSessionFunctions } = await import("../src/functions/post-session.js"); + const sdk = { functions: new Map Promise>(), trigger: vi.fn(async () => { throw new Error("boom"); }), registerFunction: function (id: string, h: (d: unknown) => Promise) { this.functions.set(id, h); } }; + const marks = new Map(); + const kv = { + get: vi.fn(async (_s: string, k: string) => marks.get(k) ?? null), + set: vi.fn(async (_s: string, k: string, v: GraphExtractMark) => { marks.set(k, v); }), + iterate: async function* () { yield [{ id: "o1", title: "x", timestamp: "2026-02-01T00:00:00Z" }]; }, + backend: "file", + }; + marks.set("s1", { sessionId: "s1", lastObservationTimestamp: "2026-01-01T00:00:00Z", extractedCount: 0, updatedAt: "2026-01-01T00:00:00Z" }); + registerPostSessionFunctions(sdk as never, kv as never); + await expect(sdk.functions.get("mem::post-session-graph")!({ sessionId: "s1" })).rejects.toThrow("boom"); + expect(marks.get("s1")!.lastObservationTimestamp).toBe("2026-01-01T00:00:00Z"); + }); +}); + +describe("post-session-graph marker validity", () => { + async function setup(opts: { extract?: unknown; generation?: string | null; obs: Array>; mark?: GraphExtractMark }) { + const { registerPostSessionFunctions } = await import("../src/functions/post-session.js"); + const functions = new Map Promise>(); + const sdk = { + trigger: vi.fn(async (req: { function_id: string; payload: unknown }) => (req.function_id === "mem::graph-extract" ? opts.extract ?? { success: true } : {})), + registerFunction: (id: string, h: (d: unknown) => Promise) => { functions.set(id, h); }, + }; + const marks = new Map(); + if (opts.mark) marks.set("s1", opts.mark); + const kv = { + get: vi.fn(async (scope: string, k: string) => (scope === KV.graphExtractMarks ? marks.get(k) ?? null : opts.generation ?? null)), + set: vi.fn(async (_s: string, k: string, v: GraphExtractMark) => { marks.set(k, v); }), + iterate: async function* () { yield opts.obs; }, + backend: "file", + }; + registerPostSessionFunctions(sdk as never, kv as never); + const extracted = () => sdk.trigger.mock.calls + .filter((c) => c[0].function_id === "mem::graph-extract") + .flatMap((c) => (c[0].payload as { observations: Array<{ id: string; files?: string[] }> }).observations); + return { functions, marks, extracted }; + } + const mark = (graphGeneration?: string | null): GraphExtractMark => ({ + sessionId: "s1", lastObservationTimestamp: "2026-02-01T00:00:00Z", extractedCount: 1, updatedAt: "2026-02-01T00:00:00Z", + ...(graphGeneration !== undefined ? { graphGeneration } : {}), + }); + + it("keeps the marker when graph extraction reports success false", async () => { + const t = await setup({ extract: { success: false, error: "llm down" }, obs: [{ id: "o2", title: "x", timestamp: "2026-03-01T00:00:00Z" }], mark: mark() }); + const result = await t.functions.get("mem::post-session-graph")!({ sessionId: "s1" }); + expect(result).toEqual({ extracted: 0, error: "llm down" }); + expect(t.marks.get("s1")!.lastObservationTimestamp).toBe("2026-02-01T00:00:00Z"); + }); + + it("re-extracts the whole session after a graph reset changes the generation", async () => { + const t = await setup({ + generation: "22222222-2222-4222-8222-222222222222", + obs: [{ id: "o1", title: "old", timestamp: "2026-01-01T00:00:00Z" }, { id: "o2", title: "new", timestamp: "2026-03-01T00:00:00Z" }], + mark: mark("11111111-1111-4111-8111-111111111111"), + }); + await t.functions.get("mem::post-session-graph")!({ sessionId: "s1" }); + expect(t.extracted().map((o) => o.id)).toEqual(["o1", "o2"]); + expect(t.marks.get("s1")!.graphGeneration).toBe("22222222-2222-4222-8222-222222222222"); + }); + + it("re-extracts an observation compressed after the marker", async () => { + const t = await setup({ + obs: [{ id: "o1", title: "late", timestamp: "2026-01-01T00:00:00Z", compressedAt: "2026-04-01T00:00:00Z" }], + mark: mark(), + }); + await t.functions.get("mem::post-session-graph")!({ sessionId: "s1" }); + expect(t.extracted().map((o) => o.id)).toEqual(["o1"]); + expect(t.marks.get("s1")!.lastObservationTimestamp).toBe("2026-04-01T00:00:00Z"); + }); + + it("passes a saved memory's files to graph extraction", async () => { + const t = await setup({ obs: [] }); + await t.functions.get("mem::post-remember-graph")!({ + input: { content: "uses the parser" }, + output: { success: true, memory: { id: "m1", title: "t", type: "fact", createdAt: "2026-01-01T00:00:00Z", concepts: [], files: ["src/parser.ts"] } }, + }); + expect(t.extracted()[0].files).toEqual(["src/parser.ts"]); + }); +}); + +describe("compression sweep", () => { + it("selects only old pending/failed observations, bounded, single-flight", async () => { + const { createCompressionSweep } = await import("../src/functions/compression-sweep.js"); + const sent: unknown[] = []; + const sdk = { trigger: vi.fn(async (req: { function_id: string; payload: unknown }) => { sent.push(req.payload); return {}; }) }; + const sessions = [{ id: "s1" }]; + const now = Date.now(); + const obs = Array.from({ length: 60 }, (_, i) => ({ + id: `o${i}`, + title: "t", + timestamp: new Date(now - (i < 55 ? 10 * 60_000 : 60_000)).toISOString(), + compression: { status: "pending", attempts: 0, revision: "r" }, + })); + const kv = { + list: vi.fn(async () => sessions), + iterate: async function* () { yield obs; }, + backend: "file", + }; + const run = createCompressionSweep(sdk as never, kv as never); + const [r1, r2] = await Promise.all([run(), run()]); + expect(r1.dispatched).toBe(50); + expect(r2.dispatched).toBe(50); + expect(sent).toHaveLength(50); + }); +}); + +describe("post-remember-graph", () => { + it("builds an observation-shaped extract payload from the saved memory", async () => { + const { registerPostSessionFunctions } = await import("../src/functions/post-session.js"); + const sdk = { functions: new Map Promise>(), trigger: vi.fn(async () => ({ ok: true })), registerFunction: function (id: string, h: (d: unknown) => Promise) { this.functions.set(id, h); } }; + registerPostSessionFunctions(sdk as never, { get: vi.fn(), set: vi.fn(), iterate: async function* () {}, backend: "file" } as never); + await sdk.functions.get("mem::post-remember-graph")!({ + input: { content: "prefer pnpm over npm", type: "preference", concepts: ["tooling"] }, + output: { success: true, memory: { id: "m1", title: "use pnpm", content: "prefer pnpm over npm", type: "preference", createdAt: "2026-01-01T00:00:00Z", concepts: ["tooling"] } }, + }); + const call = sdk.trigger.mock.calls.find((c) => c[0].function_id === "mem::graph-extract")!; + const obs = (call[0].payload as { observations: Array> }).observations; + expect(obs).toHaveLength(1); + expect(obs[0]).toMatchObject({ id: "m1", title: "use pnpm", narrative: "prefer pnpm over npm", hookType: "remember" }); + }); + + it("skips when remember output has no memory", async () => { + const { registerPostSessionFunctions } = await import("../src/functions/post-session.js"); + const sdk = { functions: new Map Promise>(), trigger: vi.fn(async () => ({})), registerFunction: function (id: string, h: (d: unknown) => Promise) { this.functions.set(id, h); } }; + registerPostSessionFunctions(sdk as never, { get: vi.fn(), set: vi.fn(), backend: "file" } as never); + const result = await sdk.functions.get("mem::post-remember-graph")!({ output: { success: false } }); + expect(result).toEqual({ skipped: "no_memory" }); + expect(sdk.trigger).not.toHaveBeenCalled(); + }); +}); + +describe("env parsing", () => { + it("compressMaxConcurrent default and strict integer", async () => { + envFlags.maxConcurrent = 7; + const mod = await import("../src/config.js"); + expect(mod.compressMaxConcurrent()).toBe(7); + }); +});