diff --git a/.env.example b/.env.example index 1a1c5c6c3a..80fc1de8c4 100644 --- a/.env.example +++ b/.env.example @@ -55,6 +55,12 @@ # embedding). The OpenAI LLM path also honours the OpenAI-scoped # OPENAI_TIMEOUT_MS alias for back-compat with v0.9.17 (precedence). # AGENTMEMORY_LLM_TIMEOUT_MS=60000 # Default: 60 000 ms (60 s) +# Most LLM calls (compress, summarize, image description) in flight at once +# across the whole process (default 4). Calls over the cap wait for a slot. +# Lower it when a self-hosted backend answers 429 "queue full". A 429 or 503 +# never opens the circuit breaker; the call honors Retry-After within its +# timeout budget instead. +# AGENTMEMORY_LLM_MAX_CONCURRENCY=4 # Opt-in Claude-subscription fallback (spawns @anthropic-ai/claude-agent-sdk # child sessions). Off by default — the agent-sdk fallback can trigger @@ -118,7 +124,7 @@ # VECTOR_WEIGHT=0.6 # Hybrid search weight for vector leg # AGENTMEMORY_GRAPH_WEIGHT=0.2 # Graph traversal bonus on smart-search ranking # TOKEN_BUDGET=2000 # Max tokens injected via mem::context per session -# MAX_OBS_PER_SESSION=500 # Per-session observation cap before consolidation kicks in +# MAX_OBS_PER_SESSION=500 # Per-session observation cap; at the cap the least important observation is evicted to admit the new one # SUMMARIZE_CHUNK_TOKENS=50000 # Prompt-token budget per chunk when mem::summarize map-reduces a large session (chunk-summarize → reduce-merge). Observations are measured with the provider's tokenize route when it has one (llama.cpp-style POST /tokenize) and estimated at chars/3 otherwise; the ~400-token system prompt is reserved inside this budget. A 50k chunk summarizes cold in 60–89s on a local 35B model. # SUMMARIZE_CHUNK_CONCURRENCY=2 # Parallel chunk LLM calls during chunked summarize. A local broker serialises chunks on one GPU, so more concurrency only stretches each call: AGENTMEMORY_LLM_TIMEOUT_MS is sized for 2. Raise it for hosted providers that run chunks in parallel, and re-measure the timeout if you do. @@ -133,7 +139,7 @@ # AGENTMEMORY_MEMORY_LAYER_BOOST=1.25 # Multiplicative boost on the fused score for saved memories (id prefix mem_, or sessionId "memory"). Multiplicative so it needs no recalibration when the weights, RRF_K or the fusion mode move. Keep it a tiebreak, not an override — it is bounded only by the supersede de-index and the isLatest skip. # AGENTMEMORY_RETRIEVAL_DEPTH=100 # Minimum candidates fetched from EACH leg before fusion, independent of the caller's page size. Legs were previously fetched at limit*2, which made results non-monotonic in limit: a document deep in the vector leg fell outside the fetch at small limits and forfeited its vector term, and under RRF that is unrecoverable (the vector term is worth up to 0.6/61 while the whole BM25 term tops out at 0.4/61). Curated memories are exactly the long, diffuse documents that sit deep in the vector leg. Nearly free — the vector scan is full regardless of limit; only the sort grows (~+8ms at 25k vectors). This is a CANDIDATE POOL size, not a page size - the caller still receives exactly `limit` rows. Set to 0 to disable the floor and restore the old limit*2 fetch. # AGENTMEMORY_PREFIX_MATCH=max # How the prefix-expansion leg combines. 'max' (default) treats all index terms sharing a query-token prefix as ONE term and takes the best, bounding the contribution by construction. 'sum' restores the historical additive behaviour, in which each distinct sibling added its own full BM25 contribution — a document listing v1.0.0 through v1.40.0 accumulated 40 contributions for the single query token "v1" and outscored exact matches (measured 110.01 at 80 siblings against a ~22 single-term ceiling). -# AGENTMEMORY_INDEX_EXCLUDE_TOOL_PREFIXES=mcp__plugin_agentmemory_agentmemory__ # Comma-separated toolName prefixes whose observations are STORED but never INDEXED. Aimed at retrieval echoes — the daemon's own record of "the agent searched for X" contains the query verbatim, making it a near-perfect match for exactly the query that created it. The records stay fully readable via timeline, session history, the viewer and expandIds. +# AGENTMEMORY_INDEX_EXCLUDE_TOOL_PREFIXES=mcp__plugin_agentmemory_agentmemory__,mcp__agentmemory__ # Default shown; set empty to index everything. Comma-separated toolName prefixes whose observations are STORED but never INDEXED. Aimed at retrieval echoes — the daemon's own record of "the agent searched for X" contains the query verbatim, making it a near-perfect match for exactly the query that created it. The records stay fully readable via timeline, session history, the viewer and expandIds. # --- Memory chunking --------------------------------------------------- # AGENTMEMORY_MEMORY_CHUNKING=true # Embed each saved memory as several vectors, one per ~section, instead of one vector for the whole thing. A long multi-topic memory otherwise averages out to a point near the corpus centroid and matches nothing. Also puts the memory's concepts into the embedded text, which the single-vector path never did. Requires a full index rebuild to take effect on existing memories. @@ -151,6 +157,7 @@ # AGENTMEMORY_INJECT_CONTEXT=true # Inject recalled memories back into agent prompts (#143). Default off — hooks capture observations but do not modify conversation. # CONSOLIDATION_ENABLED=true # Run the 4-tier consolidation pipeline (memories → semantic → procedural). Default off — opt in once you've measured the LLM cost. # CONSOLIDATION_DECAY_DAYS=30 # Age (days) after which non-reinforced memories decay during consolidation +# EVICTION_ENABLED=false # Opt out of the daily Eviction sweep (stale unsummarized sessions, old low-importance observations, per-project cap) # GRAPH_EXTRACTION_ENABLED=true # Extract concept-graph edges on remember; powers the graph-traversal recall path # GRAPH_EXTRACTION_BATCH_SIZE=8 # Memories per graph-extraction batch @@ -231,4 +238,5 @@ # AGENTMEMORY_SQLITE_PATH=/var/lib/agentmemory/agentmemory.sqlite # The store (WAL mode); its directory must exist # AGENTMEMORY_AUDIT_STORE=off # Emit audit entries as `[agentmemory] audit ` log lines instead of KV rows; memory_audit then answers an empty list # AGENTMEMORY_AUDIT_LOG=all # With AUDIT_STORE=off: all | deletions (forget, delete, lesson_delete, slot_delete, core_remove, sketch_discard only) | off +# AGENTMEMORY_AUDIT_MAX=50000 # Newest audit rows the daily Eviction keeps in KV # AGENTMEMORY_LOG_LEVEL=info # Lowest logger level written: info | warn | error | off. Audit lines bypass it. diff --git a/docs/upstream-issue-triage.yaml b/docs/upstream-issue-triage.yaml index 079672b731..8665322735 100644 --- a/docs/upstream-issue-triage.yaml +++ b/docs/upstream-issue-triage.yaml @@ -699,7 +699,9 @@ issues: difficulty: low kind: bug note: "Hook resolveProject is not worktree-aware, so each worktree is its own memory silo." - disposition: candidate + disposition: already-fixed + status: "fixed-here — 1dc4d43: resolveProject names a linked worktree after its main checkout (parent of --git-common-dir when it is a .git folder), falling back to the toplevel basename. Sessions already filed under a worktree name are not migrated." + fixed_by: "1dc4d43" url: https://github.com/rohitg00/agentmemory/issues/728 - number: 730 title: "feat: add Nix flake support for one-command installation" @@ -1247,7 +1249,8 @@ issues: difficulty: med kind: bug note: "Replace-import wipes KV but leaves the search indices stale until restart." - disposition: candidate + disposition: already-fixed + fixed_by: "Replace-import deletes through deleteIndexed and merge/replace index what they write via indexRecords (src/functions/export-import.ts)." url: https://github.com/rohitg00/agentmemory/issues/938 - number: 944 title: "[viewer] Agentmemory Token Savings always 0% — estInjected counts all sessions, not just active" @@ -1325,7 +1328,9 @@ issues: difficulty: med kind: bug note: "Observations containing CJK text hang import and live ingestion indefinitely." - disposition: candidate + disposition: already-fixed + status: "fixed-here — no hang found: the CJK tokenizer makes one forward-only pass (src/state/cjk-segmenter.ts) and truncation cuts by string length; no loop over text can stall." + fixed_by: "src/state/cjk-segmenter.ts makes one forward-only pass; no loop over text can stall." url: https://github.com/rohitg00/agentmemory/issues/969 - number: 972 title: "Add an optional HVTracker trust badge to the README" @@ -1421,7 +1426,9 @@ issues: difficulty: low kind: bug note: "post-tool-use records agentmemory's own MCP calls, polluting recall." - disposition: candidate + disposition: already-fixed + status: "fixed-here — b082b94: AGENTMEMORY_INDEX_EXCLUDE_TOOL_PREFIXES defaults to mcp__plugin_agentmemory_agentmemory__,mcp__agentmemory__, so agentmemory's own MCP calls are stored but not indexed; set it empty to index everything." + fixed_by: "b082b94" url: https://github.com/rohitg00/agentmemory/issues/993 fix_prs: [994, 1044] - number: 996 @@ -1508,7 +1515,8 @@ issues: difficulty: low kind: bug note: "ResilientProvider renames noop, so the summarize short-circuit can never fire." - disposition: candidate + disposition: already-fixed + fixed_by: "isNoopProvider unwraps resilient(...) names (src/providers/noop.ts:22-34), and the summarize and graph gates use it." url: https://github.com/rohitg00/agentmemory/issues/1020 fix_prs: [1040] - number: 1022 @@ -1600,7 +1608,8 @@ issues: difficulty: low kind: bug note: "Hooks crash with exit 1 when the payload JSON is literal null (unguarded data access)." - disposition: candidate + disposition: already-fixed + fixed_by: "Every hook returns on a null or non-object payload right after parsing (e.g. src/hooks/post-tool-use.ts:35, src/hooks/pre-tool-use.ts:55)." url: https://github.com/rohitg00/agentmemory/issues/1047 - number: 1048 title: "feat: add support for gemini-embedding-2 and make Gemini embedding model configurable" @@ -1670,7 +1679,9 @@ issues: difficulty: low kind: bug note: "Claude Code summarizer and Warmup transcripts ingested as real phantom sessions." - disposition: candidate + disposition: already-fixed + status: "fixed-here — 2f99c65: import-jsonl skips a transcript whose first prompt is Warmup or the conversation-list summary instruction. Live store had 0 such sessions; hooks already drop SDK child runs via isSdkChildContext." + fixed_by: "2f99c65" url: https://github.com/rohitg00/agentmemory/issues/1064 - number: 1065 title: "lesson-recall default minConfidence (0.1) + decay + auto-prune make most auto-consolidated lessons invisible, with no config override" @@ -1678,8 +1689,9 @@ issues: difficulty: low kind: bug note: "Default minConfidence plus decay hides 438 of 439 lessons from recall, with no override." - disposition: candidate - status: "present-here — mem::lesson-recall defaults minConfidence to 0.1 and drops anything below it (src/functions/lessons.ts:159,173). The decay sweep floors confidence at 0.05 (src/functions/lessons.ts:320), so a decayed lesson lands in 0.05-0.1 and is invisible at the default. Upstream's \"no override\" is wrong — the param is exposed." + disposition: already-fixed + status: "fixed-here — 98d246c: mem::lesson-recall defaults minConfidence to 0, so a lesson decayed to the 0.05 floor is still recalled." + fixed_by: "98d246c" url: https://github.com/rohitg00/agentmemory/issues/1065 - number: 1066 title: "memory_heal claims \"fixable\": true for project-scope backfill but the heal step does nothing ({\"fixed\":0,\"skipped\":0})" @@ -2015,7 +2027,8 @@ issues: difficulty: med kind: bug note: "Imported observations are never added to the BM25 index and have no reindex path." - disposition: candidate + disposition: already-fixed + fixed_by: "Imports index what they write via indexRecords (src/functions/export-import.ts, src/functions/replay.ts); BM25 is rebuilt from content rows on every boot." url: https://github.com/rohitg00/agentmemory/issues/1163 mentioned_by_prs: [1187] - number: 1165 @@ -2103,7 +2116,8 @@ issues: difficulty: med kind: bug note: "Nothing schedules an index save after boot, so runtime indexing is lost unless the process exits cleanly." - disposition: candidate + disposition: already-fixed + fixed_by: "Vectors are SQLite rows written in the store transaction, and BM25 is rebuilt from content rows at boot (src/index.ts), so no index save is needed (ADR 0001)." url: https://github.com/rohitg00/agentmemory/issues/1180 - number: 1181 title: "mem:audit is 71% index_persist rows the audit policy does not cover" @@ -2242,8 +2256,9 @@ issues: difficulty: low kind: bug note: "0.9.29's default OpenAI model is a reasoning model but the provider still sends max_tokens; every compress and summarize 400s." - disposition: candidate - status: "present-here — Default model is gpt-5.6-luna (src/providers/openai.ts:12, src/config.ts:95) and the chat body always sends max_tokens (src/providers/openai.ts:82) while also sending reasoning_effort (src/providers/openai.ts:95) — the two are mutually exclusive on OpenAI's chat-completions API. Nothing maps to max_completion_tokens." + disposition: already-fixed + status: "fixed-here — b0b25f9: OpenAIProvider sends max_completion_tokens when the base URL is api.openai.com and max_tokens to compatible servers." + fixed_by: "b0b25f9" url: https://github.com/rohitg00/agentmemory/issues/1219 fix_prs: [1275] - number: 1221 @@ -2452,7 +2467,9 @@ issues: difficulty: med kind: bug note: "PreToolUse injection uses raw stdout instead of the hook envelope, against a 2s budget on a 6-29s endpoint." - disposition: candidate + disposition: already-fixed + status: "fixed-here — 895d01c: pre-tool-use emits {hookSpecificOutput:{hookEventName: PreToolUse, additionalContext}} when the payload names PreToolUse, plain text otherwise; live /enrich answers in under 0.2s, so the 2s bound stays. Part 3 is not addressed: the hook still reads project only from the payload, so its enrich calls stay unscoped; upstream warns that scoping them must be measured first." + fixed_by: "895d01c" url: https://github.com/rohitg00/agentmemory/issues/1278 - number: 1279 title: "[viewer] Dashboard \"Memories\" stat card caps at 500 — uses .length of a limited fetch instead of the total field" @@ -2469,7 +2486,9 @@ issues: difficulty: med kind: bug note: "deriveCrystalAndLessons slices lessons mid-sentence; 775 of 981 unusable and some invert meaning." - disposition: candidate + disposition: already-fixed + status: "fixed-here — 0a4874a: lessons are whole sentences that open with a trigger, have balanced markup and fit 220 chars; a re-import no longer bumps reinforcements or lastReinforcedAt, so a re-derived lesson can still decay out." + fixed_by: "0a4874a" url: https://github.com/rohitg00/agentmemory/issues/1292 - number: 1297 title: "Feature request: signal deletion / TTL for signals" @@ -2588,7 +2607,8 @@ issues: difficulty: med kind: bug note: "The shutdown index save is lost to a teardown race and boot only reconciles a fully empty index." - disposition: candidate + disposition: already-fixed + fixed_by: "No shutdown index save exists to lose: vectors persist per write and BM25 is rebuilt at boot; a boot and hourly fill pass repairs a partial vector index (src/index.ts)." url: https://github.com/rohitg00/agentmemory/issues/1335 - number: 1338 title: "fix(providers): createFallbackProvider drops baseURL — custom-endpoint fallbacks (DeepSeek/Kimi/etc.) hit the wrong API host" @@ -2623,7 +2643,8 @@ issues: difficulty: low kind: bug note: "Dead noop gate causes a summarize failure storm when no LLM key is set." - disposition: candidate + disposition: already-fixed + fixed_by: "isNoopProvider unwraps resilient(...) names (src/providers/noop.ts:22-34), and the summarize and graph gates use it." url: https://github.com/rohitg00/agentmemory/issues/1341 mentioned_by_prs: [1342] - number: 1360 diff --git a/docs/upstream-pr-triage.yaml b/docs/upstream-pr-triage.yaml index 57262516e8..6e2326fe71 100644 --- a/docs/upstream-pr-triage.yaml +++ b/docs/upstream-pr-triage.yaml @@ -287,8 +287,8 @@ prs: paths: - src/functions/evict.ts note: "Evicted observations are never removed from the BM25 or vector index, so search returns ids whose KV lookup is null and silently under-fills pages." - disposition: candidate - status: "present-here — src/functions/evict.ts deletes from KV without touching the BM25 or vector index." + disposition: already-fixed + fixed_by: "Every evict.ts delete goes through deleteIndexed, which removes the row, its vector and its BM25 entry (src/functions/search.ts:153-174)." url: https://github.com/rohitg00/agentmemory/pull/1005 - number: 1039 title: "fix: schedule periodic snapshot timer (#1006)" @@ -311,8 +311,9 @@ prs: paths: - src/functions/observe.ts note: "At MAX_OBS_PER_SESSION every further observation is discarded; evicts the lowest-value one instead of dropping the newest." - disposition: candidate - status: "present-here — At MAX_OBS_PER_SESSION mem::observe refuses every further observation outright (src/functions/observe.ts:147-154). The newest work is dropped rather than the least valuable one evicted." + disposition: already-fixed + status: "fixed-here — db9f5db: at the cap mem::observe evicts the lowest-importance observation, oldest first on a tie, from KV and both indexes, and admits the new one." + fixed_by: "db9f5db" url: https://github.com/rohitg00/agentmemory/pull/1174 - number: 1175 title: "feat(slots): reject stale slot overwrites, keep an undo copy, report headroom" @@ -338,8 +339,9 @@ prs: paths: - src/functions/audit.ts note: "recordAudit has no retention and the KV adapter rewrites the whole scope per write, so audit cost compounds on long-lived installs." - disposition: candidate - status: "present-here — recordAudit has no retention bound." + disposition: already-fixed + status: "fixed-here — cc30b8e: pruneAudit keeps the newest AGENTMEMORY_AUDIT_MAX rows (default 50000), run daily." + fixed_by: "cc30b8e" url: https://github.com/rohitg00/agentmemory/pull/1176 - number: 1210 title: "fix(providers): replace retired Anthropic default model identifiers" @@ -389,8 +391,9 @@ prs: paths: - src/providers/resilient.ts note: "No concurrency bound on the provider path and 429s count as circuit-breaker failures, so load trips the breaker." - disposition: candidate - status: "partly-present-here — 429 and 503 are already retried with Retry-After before reaching the breaker (src/providers/_fetch.ts:13,76-82). A concurrency cap exists, but only on the agent-sdk provider (src/providers/agent-sdk.ts:99) — the HTTP providers have none." + disposition: already-fixed + status: "fixed-here — 042f3a1: a 429/503 that exhausts its retries no longer opens the breaker, Retry-After is honoured uncapped within the timeout budget, and AGENTMEMORY_LLM_MAX_CONCURRENCY (default 4) bounds in-flight LLM calls." + fixed_by: "042f3a1" url: https://github.com/rohitg00/agentmemory/pull/1259 - number: 1260 title: "fix(mcp): read graph stats from the snapshot instead of enumerating the graph" @@ -418,8 +421,9 @@ prs: - src/index.ts - test/evict.test.ts note: "mem::evict is fully implemented but nothing schedules it — Eviction never runs." - disposition: candidate - status: "present-here — mem::evict is registered (src/functions/evict.ts:109) but src/index.ts schedules only auto-forget and consolidate-pipeline." + disposition: already-fixed + status: "fixed-here — 3b2545e: src/index.ts runs mem::evict every 24h unless EVICTION_ENABLED=false." + fixed_by: "3b2545e" url: https://github.com/rohitg00/agentmemory/pull/1287 - number: 1307 title: "fix(sessions): close idle sessions no client ever ended" @@ -528,8 +532,9 @@ prs: - test/consolidation-pipeline.test.ts - test/reflect.test.ts note: "Reflection and consolidation are not project-scoped, so concept clusters and insights cross-contaminate between repos." - disposition: candidate - status: "partly-present-here — mem::reflect accepts and filters on project (src/functions/reflect.ts:187,354,385), so the capability is present; it is opt-in, and the scheduled run passes no project." + disposition: already-fixed + status: "fixed-here — 6575de0: an unscoped consolidation run merges and reflects each project with new summaries separately, and mem::reflect with a project reads only that project's facts, crystals, lessons and graph concepts. Existing unscoped insights are left as they are." + fixed_by: "6575de0" url: https://github.com/rohitg00/agentmemory/pull/1344 - number: 1350 title: "fix(providers): clear the circuit breaker failure count on success" diff --git a/plugin/scripts/notification.mjs b/plugin/scripts/notification.mjs index 0b7e188fca..3c66a6b003 100755 --- a/plugin/scripts/notification.mjs +++ b/plugin/scripts/notification.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/scripts/post-tool-failure.mjs b/plugin/scripts/post-tool-failure.mjs index 354a620dc4..37e5bedc9e 100755 --- a/plugin/scripts/post-tool-failure.mjs +++ b/plugin/scripts/post-tool-failure.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/scripts/post-tool-use.mjs b/plugin/scripts/post-tool-use.mjs index 2d8341e67a..c8c4c17f86 100755 --- a/plugin/scripts/post-tool-use.mjs +++ b/plugin/scripts/post-tool-use.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/scripts/pre-compact.mjs b/plugin/scripts/pre-compact.mjs index c4bd90a4ff..3d5af46c46 100755 --- a/plugin/scripts/pre-compact.mjs +++ b/plugin/scripts/pre-compact.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/scripts/pre-tool-use.mjs b/plugin/scripts/pre-tool-use.mjs index 0146f69926..d10072e16e 100755 --- a/plugin/scripts/pre-tool-use.mjs +++ b/plugin/scripts/pre-tool-use.mjs @@ -44,6 +44,13 @@ function isSdkChildContext(payload) { const INJECT_CONTEXT = process.env["AGENTMEMORY_INJECT_CONTEXT"] === "true"; const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111"; const SECRET = process.env["AGENTMEMORY_SECRET"] || ""; +function contextPayload(data, context) { + if (data.hook_event_name === "PreToolUse") return JSON.stringify({ hookSpecificOutput: { + hookEventName: "PreToolUse", + additionalContext: context + } }); + return context; +} function authHeaders() { const h = { "Content-Type": "application/json" }; if (SECRET) h["Authorization"] = `Bearer ${SECRET}`; @@ -110,7 +117,7 @@ async function main() { }); if (res.ok) { const result = await res.json(); - if (result.context) process.stdout.write(result.context); + if (result.context) process.stdout.write(contextPayload(data, result.context)); } } catch {} } diff --git a/plugin/scripts/prompt-submit.mjs b/plugin/scripts/prompt-submit.mjs index 0f9c2eaefe..517d18227a 100755 --- a/plugin/scripts/prompt-submit.mjs +++ b/plugin/scripts/prompt-submit.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/scripts/session-end.mjs b/plugin/scripts/session-end.mjs index a91c151cc9..72cfef97c4 100755 --- a/plugin/scripts/session-end.mjs +++ b/plugin/scripts/session-end.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/scripts/session-start.mjs b/plugin/scripts/session-start.mjs index 1221981615..3c07317d02 100755 --- a/plugin/scripts/session-start.mjs +++ b/plugin/scripts/session-start.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/scripts/subagent-start.mjs b/plugin/scripts/subagent-start.mjs index df43650c82..99db966049 100755 --- a/plugin/scripts/subagent-start.mjs +++ b/plugin/scripts/subagent-start.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/scripts/subagent-stop.mjs b/plugin/scripts/subagent-stop.mjs index 88ab8609ba..4afb64136b 100755 --- a/plugin/scripts/subagent-stop.mjs +++ b/plugin/scripts/subagent-stop.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/scripts/task-completed.mjs b/plugin/scripts/task-completed.mjs index df78a6f373..f876edce4a 100755 --- a/plugin/scripts/task-completed.mjs +++ b/plugin/scripts/task-completed.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; import { execSync } from "node:child_process"; //#region src/hooks/_env.ts function parseEnvFile(content) { @@ -41,7 +41,7 @@ function resolveProject(cwd) { if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: [ "ignore", @@ -49,7 +49,8 @@ function resolveProject(cwd) { "ignore" ], timeout: 500 - }).toString().trim(); + }).toString().trim().split("\n"); + if (commonDir && basename(commonDir) === ".git") return basename(dirname(resolve(dir, commonDir))); if (top) return basename(top); } catch {} return basename(dir); diff --git a/plugin/skills/agentmemory-config/REFERENCE.md b/plugin/skills/agentmemory-config/REFERENCE.md index 7d14662916..59cdd5a343 100644 --- a/plugin/skills/agentmemory-config/REFERENCE.md +++ b/plugin/skills/agentmemory-config/REFERENCE.md @@ -3,13 +3,14 @@ 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). 70 recognized variables: +Configuration is read from the environment and from `~/.agentmemory/.env` (no `export` prefix). 72 recognized variables: - `AGENTMEMORY_AGENT_SCOPE` - `AGENTMEMORY_AGENT_SDK_MAX_CONCURRENCY` - `AGENTMEMORY_AGENT_SDK_TIMEOUT_MS` - `AGENTMEMORY_ALLOW_AGENT_SDK` - `AGENTMEMORY_AUDIT_LOG` +- `AGENTMEMORY_AUDIT_MAX` - `AGENTMEMORY_AUDIT_STORE` - `AGENTMEMORY_AUTO_COMPRESS` - `AGENTMEMORY_CHUNK_MAX_CHARS` @@ -45,6 +46,7 @@ Configuration is read from the environment and from `~/.agentmemory/.env` (no `e - `AGENTMEMORY_INDEX_EXCLUDE_TOOL_PREFIXES` - `AGENTMEMORY_INDEX_REBUILD` - `AGENTMEMORY_INJECT_CONTEXT` +- `AGENTMEMORY_LLM_MAX_CONCURRENCY` - `AGENTMEMORY_LLM_NOTHINK` - `AGENTMEMORY_LLM_TIMEOUT_MS` - `AGENTMEMORY_LOG_LEVEL` diff --git a/src/functions/audit.ts b/src/functions/audit.ts index 5ba639d4c2..c382c3a539 100644 --- a/src/functions/audit.ts +++ b/src/functions/audit.ts @@ -129,6 +129,30 @@ export async function safeAudit( } } +const AUDIT_MAX_DEFAULT = 50_000; + +function auditMax(): number { + const raw = process.env.AGENTMEMORY_AUDIT_MAX?.trim(); + const n = raw && /^\d+$/.test(raw) ? Number(raw) : 0; + return n > 0 ? n : AUDIT_MAX_DEFAULT; +} + +// Retention for the audit store: keeps the newest AGENTMEMORY_AUDIT_MAX +// entries. Evicting from the log is not itself audited - that would write the +// rows it exists to bound - so it leaves one log line instead. +export async function evictOldestAudit(kv: StateKV): Promise { + if (auditStoreOff()) return 0; + const max = auditMax(); + const all = await kv.list(KV.audit); + if (all.length <= max) return 0; + const oldest = [...all] + .sort((a, b) => a.timestamp.localeCompare(b.timestamp)) + .slice(0, all.length - max); + for (const entry of oldest) await kv.delete(KV.audit, entry.id); + logger.info("Audit log evicted beyond cap", { removed: oldest.length, kept: max }); + return oldest.length; +} + export async function queryAudit( kv: StateKV, filter?: { diff --git a/src/functions/consolidation-pipeline.ts b/src/functions/consolidation-pipeline.ts index 53259740c3..dfe554d3fa 100644 --- a/src/functions/consolidation-pipeline.ts +++ b/src/functions/consolidation-pipeline.ts @@ -70,100 +70,137 @@ export function registerConsolidationPipelineFunction( }, ); - async function run(data?: { tier?: string; project?: string }) { - const tier = data?.tier || "all"; - const decayDays = getConsolidationDecayDays(); - const results: Record = {}; + // Summaries and insights from one project must never feed another's + // consolidation (#1344): an unscoped run consolidates each project that has + // new summaries since the last unscoped run, one project at a time. A project + // whose merge or reflect failed is retried on the next unscoped run. + let lastUnscopedRunAt = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(); + const retryProjects = new Set(); - if (tier === "all" || tier === "semantic") { - const summaries = await kv.list(KV.summaries); - const existingSemantic = await kv.list(KV.semantic); + async function consolidateSemantic(summaries: SessionSummary[]) { + if (summaries.length < 5) { + return { skipped: true, reason: "fewer than 5 summaries" }; + } + const existingSemantic = await kv.list(KV.semantic); + const recentSummaries = summaries + .sort( + (a, b) => + new Date(b.createdAt).getTime() - + new Date(a.createdAt).getTime(), + ) + .slice(0, 20); - if (summaries.length >= 5) { - const recentSummaries = summaries - .sort( - (a, b) => - new Date(b.createdAt).getTime() - - new Date(a.createdAt).getTime(), - ) - .slice(0, 20); + const prompt = buildSemanticMergePrompt( + recentSummaries.map((s) => ({ + title: s.title, + narrative: s.narrative, + concepts: s.concepts, + })), + ); - const prompt = buildSemanticMergePrompt( - recentSummaries.map((s) => ({ - title: s.title, - narrative: s.narrative, - concepts: s.concepts, - })), - ); + try { + const response = await provider.summarize( + SEMANTIC_MERGE_SYSTEM, + prompt, + ); - try { - const response = await provider.summarize( - SEMANTIC_MERGE_SYSTEM, - prompt, - ); + const factRegex = /([^<]+)<\/fact>/g; + let match; + let newFacts = 0; + const now = new Date().toISOString(); - const factRegex = /([^<]+)<\/fact>/g; - let match; - let newFacts = 0; - const now = new Date().toISOString(); - - while ((match = factRegex.exec(response)) !== null) { - const parsedConf = parseFloat(match[1]); - const confidence = Number.isNaN(parsedConf) ? 0.5 : parsedConf; - const fact = match[2].trim(); + while ((match = factRegex.exec(response)) !== null) { + const parsedConf = parseFloat(match[1]); + const confidence = Number.isNaN(parsedConf) ? 0.5 : parsedConf; + const fact = match[2].trim(); - const existing = existingSemantic.find( - (s) => s.fact.toLowerCase() === fact.toLowerCase(), - ); - if (existing) { - existing.accessCount++; - existing.lastAccessedAt = now; - existing.updatedAt = now; - existing.confidence = Math.max(existing.confidence, confidence); - await kv.set(KV.semantic, existing.id, existing); - } else { - const sem: SemanticMemory = { - id: generateId("sem"), - fact, - confidence, - sourceSessionIds: recentSummaries.map((s) => s.sessionId), - sourceMemoryIds: [], - accessCount: 1, - lastAccessedAt: now, - strength: confidence, - createdAt: now, - updatedAt: now, - }; - await kv.set(KV.semantic, sem.id, sem); - newFacts++; - } - } - results.semantic = { newFacts, totalSummaries: summaries.length }; - } catch (err) { - const msg = err instanceof Error ? err.message : String(err); - logger.error("Semantic consolidation failed", { error: msg }); - results.semantic = { error: msg }; + const existing = existingSemantic.find( + (s) => s.fact.toLowerCase() === fact.toLowerCase(), + ); + if (existing) { + existing.accessCount++; + existing.lastAccessedAt = now; + existing.updatedAt = now; + existing.confidence = Math.max(existing.confidence, confidence); + existing.sourceSessionIds = [ + ...new Set([...existing.sourceSessionIds, ...recentSummaries.map((s) => s.sessionId)]), + ]; + await kv.set(KV.semantic, existing.id, existing); + } else { + const sem: SemanticMemory = { + id: generateId("sem"), + fact, + confidence, + sourceSessionIds: recentSummaries.map((s) => s.sessionId), + sourceMemoryIds: [], + accessCount: 1, + lastAccessedAt: now, + strength: confidence, + createdAt: now, + updatedAt: now, + }; + await kv.set(KV.semantic, sem.id, sem); + newFacts++; } - } else { - results.semantic = { - skipped: true, - reason: "fewer than 5 summaries", - }; } + return { newFacts, totalSummaries: summaries.length }; + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + logger.error("Semantic consolidation failed", { error: msg }); + return { error: msg }; } + } - if (tier === "all" || tier === "reflect") { - try { - const reflectResult = await sdk.trigger({ function_id: "mem::reflect", payload: { - maxClusters: 10, - project: data?.project, - } }); - results.reflect = reflectResult; - } catch (err) { - const msg = err instanceof Error ? err.message : String(err); - logger.warn("Reflect tier failed", { error: msg }); - results.reflect = { error: msg }; + async function reflect(project: string) { + try { + return await sdk.trigger({ function_id: "mem::reflect", payload: { + maxClusters: 10, + project, + } }); + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + logger.warn("Reflect tier failed", { error: msg, project }); + return { error: msg }; + } + } + + async function run(data?: { tier?: string; project?: string }) { + const tier = data?.tier || "all"; + const decayDays = getConsolidationDecayDays(); + const results: Record = {}; + + const runsSemantic = tier === "all" || tier === "semantic"; + const runsReflect = tier === "all" || tier === "reflect"; + if (runsSemantic || runsReflect) { + const startedAt = new Date().toISOString(); + const summaries = await kv.list(KV.summaries); + const projects = data?.project + ? [data.project] + : [...new Set([ + ...retryProjects, + ...summaries + .filter((s) => s.project && s.createdAt > lastUnscopedRunAt) + .map((s) => s.project), + ])]; + const semanticByProject: Record = {}; + const reflectByProject: Record = {}; + for (const project of projects) { + if (runsSemantic) { + semanticByProject[project] = await consolidateSemantic( + summaries.filter((s) => s.project === project), + ); + } + if (runsReflect) reflectByProject[project] = await reflect(project); + const failed = [semanticByProject[project], reflectByProject[project]].some( + (r) => r !== undefined && r !== null && typeof r === "object" && "error" in r, + ); + if (data?.project) continue; + if (failed) retryProjects.add(project); + else retryProjects.delete(project); } + if (runsSemantic) results.semantic = semanticByProject; + if (runsReflect) results.reflect = reflectByProject; + if (!data?.project) lastUnscopedRunAt = startedAt; } if (tier === "all" || tier === "procedural") { diff --git a/src/functions/lessons.ts b/src/functions/lessons.ts index 1dbae9123b..b1b8ad3a03 100644 --- a/src/functions/lessons.ts +++ b/src/functions/lessons.ts @@ -158,11 +158,13 @@ export function registerLessonsFunctions(sdk: ISdk, kv: StateKV): void { return { success: false, error: "query is required" }; } - const minConfidence = data.minConfidence ?? 0.1; + // Soft-delete is the only default gate: a reinforced lesson decays to + // the 0.05 floor and stays live, and confidence already ranks it low. + const minConfidence = data.minConfidence ?? 0; const limit = data.limit ?? 10; const idx = await ensureLessonIndex(kv); - const filtering = !!data.project || minConfidence > 0.1; + const filtering = !!data.project || minConfidence > 0; const fetchLimit = filtering ? Math.max(limit * 10, 100) : Math.max(limit * 5, 50); diff --git a/src/functions/observe.ts b/src/functions/observe.ts index f9214af090..709804eda7 100644 --- a/src/functions/observe.ts +++ b/src/functions/observe.ts @@ -9,7 +9,9 @@ import { DedupMap } from "./dedup.js"; import { withKeyedLock } from "../state/keyed-mutex.js"; import { isAutoCompressEnabled } from "../config.js"; import { buildSyntheticCompression } from "./compress-synthetic.js"; -import { getSearchIndex, vectorIndexAddGuarded, isIndexExcluded } from "./search.js"; +import { getSearchIndex, vectorIndexAddGuarded, isIndexExcluded, deleteIndexed } from "./search.js"; +import { safeAudit } from "./audit.js"; +import { decrementImageRef } from "./image-refs.js"; import { getAgentId } from "../config.js"; import { logger } from "../logger.js"; import { saveImageToDisk } from "../utils/image-store.js"; @@ -37,6 +39,64 @@ export function extractImage(d: unknown): string | undefined { return undefined; } +// Uncompressed rows carry no importance yet; rank them just below the +// default so a scored row of average value outlives an unscored one. +const UNSCORED_IMPORTANCE = 3; + +// A session at its cap still admits the newest observation: the work at the +// end of a long session is what a later one most often needs. The least +// important rows go first, oldest breaking ties (PR#1174). +async function evictToAdmitOne( + sdk: ISdk, + kv: StateKV, + sessionId: string, + cap: number, +): Promise { + const scope = KV.observations(sessionId); + const existing = await kv.list<{ + id: string; + timestamp?: string; + importance?: number; + imageData?: string; + imageRef?: string; + }>(scope); + const excess = existing.length - cap + 1; + if (excess <= 0) return; + const victims = existing + .sort( + (a, b) => + (a.importance ?? UNSCORED_IMPORTANCE) - (b.importance ?? UNSCORED_IMPORTANCE) || + (a.timestamp ?? "").localeCompare(b.timestamp ?? ""), + ) + .slice(0, excess); + let evicted = 0; + for (const obs of victims) { + try { + await deleteIndexed(kv, scope, obs.id); + evicted++; + } catch (err) { + logger.warn("Session cap eviction failed", { + sessionId, + obsId: obs.id, + error: err instanceof Error ? err.message : String(err), + }); + continue; + } + if (obs.imageData) await decrementImageRef(kv, sdk, obs.imageData); + if (obs.imageRef && obs.imageRef !== obs.imageData) await decrementImageRef(kv, sdk, obs.imageRef); + await safeAudit(kv, "delete", "mem::observe", [obs.id], { + resource: "observation", + reason: "session_observation_cap", + sessionId, + }); + } + logger.warn("Session observation cap reached; evicted least important", { + sessionId, + cap, + evicted, + }); +} + export function registerObserveFunction( sdk: ISdk, kv: StateKV, @@ -145,13 +205,7 @@ export function registerObserveFunction( return withKeyedLock(`obs:${payload.sessionId}`, async () => { if (maxObservationsPerSession && maxObservationsPerSession > 0) { - const existing = await kv.list(KV.observations(payload.sessionId)); - if (existing.length >= maxObservationsPerSession) { - return { - success: false, - error: `Session observation limit reached (${maxObservationsPerSession})`, - }; - } + await evictToAdmitOne(sdk, kv, payload.sessionId, maxObservationsPerSession); } // Existing session is the source of truth for agentId (even diff --git a/src/functions/reflect.ts b/src/functions/reflect.ts index a77b659d6c..5fae62eb8a 100644 --- a/src/functions/reflect.ts +++ b/src/functions/reflect.ts @@ -9,6 +9,7 @@ import type { Lesson, Crystal, MemoryProvider, + Session, } from "../types.js"; import { recordAudit } from "./audit.js"; import { REFLECT_SYSTEM, buildReflectPrompt } from "../prompts/reflect.js"; @@ -184,20 +185,42 @@ export function registerReflectFunctions( ]); let activeLessons = lessons.filter((l) => !l.deleted); + let scopedSemantic = semanticMemories; + let scopedCrystals = crystals; + let scopedNodes = graphNodes; + let scopedEdges = graphEdges; + // One project's clusters must never borrow another's facts, crystals or + // concepts: an insight is stamped with the project it was built for (#1344). if (data?.project) { - activeLessons = activeLessons.filter((l) => l.project === data.project); + const project = data.project; + const sessions = await kv.list(KV.sessions); + const projectSessionIds = new Set( + sessions.filter((s) => s.project === project).map((s) => s.id), + ); + activeLessons = activeLessons.filter((l) => l.project === project); + scopedSemantic = semanticMemories.filter((m) => + m.sourceSessionIds.some((id) => projectSessionIds.has(id)), + ); + scopedCrystals = crystals.filter((c) => c.project === project); + scopedNodes = graphNodes.filter( + (n) => n.sessionId !== undefined && projectSessionIds.has(n.sessionId), + ); + const nodeIds = new Set(scopedNodes.map((n) => n.id)); + scopedEdges = graphEdges.filter( + (e) => nodeIds.has(e.sourceNodeId) && nodeIds.has(e.targetNodeId), + ); } let conceptClusters = buildGraphClusters( - graphNodes, - graphEdges, + scopedNodes, + scopedEdges, maxClusters, ); const usedFallback = conceptClusters.length === 0; if (usedFallback) { conceptClusters = buildJaccardClusters( - semanticMemories, + scopedSemantic, activeLessons, maxClusters, ); @@ -213,7 +236,7 @@ export function registerReflectFunctions( const conceptSet = new Set(conceptNames.map((c) => c.toLowerCase())); - const clusterFacts = semanticMemories.filter((s) => { + const clusterFacts = scopedSemantic.filter((s) => { const factTerms = s.fact.toLowerCase().split(/\s+/); return factTerms.some((t) => conceptSet.has(t)); }); @@ -225,7 +248,7 @@ export function registerReflectFunctions( ), ); - const clusterCrystals = crystals.filter((c) => + const clusterCrystals = scopedCrystals.filter((c) => (c.lessons || []).some((l) => conceptNames.some((cn) => l.toLowerCase().includes(cn.toLowerCase()), diff --git a/src/functions/replay.ts b/src/functions/replay.ts index 3d1f5e9906..6432799b5f 100644 --- a/src/functions/replay.ts +++ b/src/functions/replay.ts @@ -35,6 +35,15 @@ const SENSITIVE_PATH_PATTERNS: RegExp[] = [ /(^|[\\/])api[_-]?token([\\/_.-]|$)/i, ]; +// Claude Code writes its own background jobs to ~/.claude/projects beside +// real work: a Warmup probe and the conversation-list summary job (#1064). +function isHarnessPrompt(firstPrompt: string): boolean { + return ( + firstPrompt === "Warmup" || + firstPrompt.startsWith("Context: This summary will be shown in a list") + ); +} + export function isSensitive(path: string): boolean { return SENSITIVE_PATH_PATTERNS.some((re) => re.test(path)); } @@ -63,10 +72,45 @@ function rawFromCompressed(obs: CompressedObservation): RawObservation { }; } -const LESSON_PATTERNS: RegExp[] = [ - /\b(always|never|don'?t|do not|make sure|remember to|note:|caveat:|warning:)\b[^.\n]{10,200}[.!\n]/gi, - /\b(prefer|avoid)\s[^.\n]{10,200}[.!\n]/gi, -]; +// A lesson is a whole sentence that opens with a trigger. A trigger found +// mid-sentence would drop the subject and can invert the meaning (#1292). +const LESSON_START = + /^(always|never|don['’]?t|do not|make sure|remember to|prefer|avoid|note:|caveat:|warning:)\s/i; +const LIST_MARKER = /^(?:[-*+>]|\d+[.)])\s+/; +const SENTENCE_BREAK = /\n+|(?<=[.!?])\s+/; +const MIN_LESSON_CHARS = 20; +const MAX_LESSON_CHARS = 220; +const MAX_LESSONS = 20; + +function isBalanced(sentence: string): boolean { + const count = (token: string) => sentence.split(token).length - 1; + return ( + count("**") % 2 === 0 && + count("`") % 2 === 0 && + count("[[") === count("]]") + ); +} + +export function extractLessons(texts: string[]): string[] { + const lessons = new Map(); + for (const text of texts) { + for (const raw of text.split(SENTENCE_BREAK)) { + const sentence = raw.trim().replace(LIST_MARKER, "").replace(/\s+/g, " "); + if ( + sentence.length < MIN_LESSON_CHARS || + sentence.length > MAX_LESSON_CHARS || + !LESSON_START.test(sentence) || + !isBalanced(sentence) + ) { + continue; + } + const key = sentence.toLowerCase(); + if (!lessons.has(key)) lessons.set(key, sentence); + if (lessons.size >= MAX_LESSONS) return Array.from(lessons.values()); + } + } + return Array.from(lessons.values()); +} async function deriveCrystalAndLessons( kv: StateKV, @@ -97,22 +141,9 @@ async function deriveCrystalAndLessons( } } - const lessonMatches = new Map(); - for (const text of assistantTexts.concat(userPrompts).slice(0, 200)) { - for (const pat of LESSON_PATTERNS) { - pat.lastIndex = 0; - let m: RegExpExecArray | null; - while ((m = pat.exec(text)) !== null && lessonMatches.size < 40) { - const snippet = m[0].replace(/\s+/g, " ").trim(); - if (snippet.length >= 20 && snippet.length <= 220) { - const key = snippet.toLowerCase(); - if (!lessonMatches.has(key)) lessonMatches.set(key, snippet); - } - } - } - } - - const lessonEntries = Array.from(lessonMatches.values()).slice(0, 20); + const lessonEntries = extractLessons( + assistantTexts.concat(userPrompts).slice(0, 200), + ); const lessonIds: string[] = []; for (const content of lessonEntries) { // Content-addressed ID so re-importing the same JSONL does not @@ -134,9 +165,7 @@ async function deriveCrystalAndLessons( ...existing, sourceIds: mergedSources, tags: mergedTags, - reinforcements: (existing.reinforcements || 0) + 1, updatedAt: createdAt, - lastReinforcedAt: createdAt, }; await kv.set(KV.lessons, lessonId, merged); } else { @@ -393,6 +422,7 @@ export function registerReplayFunctions(sdk: ISdk, kv: StateKV): void { const firstPrompt = firstPromptObs?.userPrompt ? firstPromptObs.userPrompt.replace(/\s+/g, " ").trim().slice(0, 200) : undefined; + if (firstPrompt && isHarnessPrompt(firstPrompt)) continue; const existing = await kv.get(KV.sessions, parsed.sessionId); if (existing) { diff --git a/src/functions/summarize.ts b/src/functions/summarize.ts index 54ff691350..38fc9b6d5e 100644 --- a/src/functions/summarize.ts +++ b/src/functions/summarize.ts @@ -81,11 +81,23 @@ async function mapWithConcurrency( return out; } +function fitsOneChunk(counts: number[], budget: number): boolean { + const payload = counts.reduce((sum, n) => sum + n + SEPARATOR_TOKENS, 0); + return payload <= budget - PROMPT_OVERHEAD_TOKENS; +} + +// A token is rarely shorter than one character, so a Session whose character +// count already fits one chunk is safe to skip measuring: on a broker that queues +// tokenize behind generation, one request per Observation is load the summary +// itself has to wait behind. async function countObservationTokens( provider: MemoryProvider, texts: string[], + budget: number, sessionId: string, ): Promise { + const estimates = texts.map(estimateTokens); + if (fitsOneChunk(texts.map((t) => t.length), budget)) return estimates; if (provider.countTokens) { try { return await mapWithConcurrency(texts, COUNT_CONCURRENCY, (t) => @@ -98,7 +110,7 @@ async function countObservationTokens( }); } } - return texts.map(estimateTokens); + return estimates; } // Greedy in-order packing. An Observation that alone exceeds the budget @@ -184,7 +196,7 @@ async function planChunks( ): Promise { const budget = getChunkTokens(); const texts = compressed.map(renderSummaryObservation); - const counts = await countObservationTokens(provider, texts, sessionId); + const counts = await countObservationTokens(provider, texts, budget, sessionId); const chunks = packChunks(compressed, counts, budget, sessionId); if (chunks.length > 1) { logger.info("Summarize chunking session", { diff --git a/src/hooks/_project.ts b/src/hooks/_project.ts index 9f0320c5ce..06ce7a6d16 100644 --- a/src/hooks/_project.ts +++ b/src/hooks/_project.ts @@ -1,19 +1,24 @@ import { execSync } from "node:child_process"; -import { basename } from "node:path"; +import { basename, dirname, resolve } from "node:path"; -// Resolution order: AGENTMEMORY_PROJECT_NAME env → git toplevel basename → cwd basename. +// Resolution order: AGENTMEMORY_PROJECT_NAME env → main checkout basename (so every +// git worktree of a repo shares one project) → git toplevel basename → cwd basename. export function resolveProject(cwd?: string): string { const explicit = process.env["AGENTMEMORY_PROJECT_NAME"]; if (explicit && explicit.trim()) return explicit.trim(); const dir = cwd && cwd.trim() ? cwd : process.cwd(); try { - const top = execSync("git rev-parse --show-toplevel", { + const [commonDir, top] = execSync("git rev-parse --git-common-dir --show-toplevel", { cwd: dir, stdio: ["ignore", "pipe", "ignore"], timeout: 500, }) .toString() - .trim(); + .trim() + .split("\n"); + if (commonDir && basename(commonDir) === ".git") { + return basename(dirname(resolve(dir, commonDir))); + } if (top) return basename(top); } catch {} return basename(dir); diff --git a/src/hooks/pre-tool-use.ts b/src/hooks/pre-tool-use.ts index 8e2383fa88..0900c10fc0 100644 --- a/src/hooks/pre-tool-use.ts +++ b/src/hooks/pre-tool-use.ts @@ -13,8 +13,7 @@ function isSdkChildContext(payload: unknown): boolean { // // THIS HOOK IS A NO-OP BY DEFAULT AS OF 0.8.10 (#143). Previously it // fired /agentmemory/enrich on every Edit/Write/Read/Glob/Grep tool call -// and wrote up to 4000 chars of context to stdout. Claude Code reads -// PreToolUse stdout and prepends it to the model's next turn, which meant +// and wrote up to 4000 chars of context to stdout for the model's next turn, which meant // agentmemory was silently injecting ~1000 tokens into every tool turn // via the user's Claude Code session. On Claude Pro that burned entire // allocations in a handful of messages (@adrianricardo, #143). @@ -29,6 +28,21 @@ const INJECT_CONTEXT = process.env["AGENTMEMORY_INJECT_CONTEXT"] === "true"; const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111"; const SECRET = process.env["AGENTMEMORY_SECRET"] || ""; +// Claude Code drops plain PreToolUse stdout into the debug log; only the +// hookSpecificOutput envelope reaches the model. Hosts that send no +// hook_event_name keep the plain text they have always read. +function contextPayload(data: Record, context: string): string { + if (data.hook_event_name === "PreToolUse") { + return JSON.stringify({ + hookSpecificOutput: { + hookEventName: "PreToolUse", + additionalContext: context, + }, + }); + } + return context; +} + function authHeaders(): Record { const h: Record = { "Content-Type": "application/json" }; if (SECRET) h["Authorization"] = `Bearer ${SECRET}`; @@ -120,7 +134,7 @@ async function main() { if (res.ok) { const result = (await res.json()) as { context?: string }; if (result.context) { - process.stdout.write(result.context); + process.stdout.write(contextPayload(data, result.context)); } } } catch { diff --git a/src/index.ts b/src/index.ts index 32a8f13a89..1f1d104db9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,6 +59,7 @@ import { registerConsolidateFunction } from "./functions/consolidate.js"; import { registerPatternsFunction } from "./functions/patterns.js"; import { registerRememberFunction } from "./functions/remember.js"; import { registerEvictFunction } from "./functions/evict.js"; +import { evictOldestAudit } from "./functions/audit.js"; import { registerRelationsFunction } from "./functions/relations.js"; import { registerTimelineFunction } from "./functions/timeline.js"; import { registerSmartSearchFunction } from "./functions/smart-search.js"; @@ -119,6 +120,8 @@ import { mkdirSync, writeFileSync, unlinkSync } from "node:fs"; import { dirname, join } from "node:path"; import { homedir } from "node:os"; +const FIRST_SWEEP_DELAY_MS = 5 * 60_000; + // #640 + #474: record this process's pid so `agentmemory stop` and // `agentmemory doctor` can identify the daemon. Without it they can only // scan the REST port, which cannot tell our `node` process apart from a @@ -608,6 +611,27 @@ async function main() { bootLog(`Lesson decay sweep: enabled (every 24h)`); } + if (process.env.EVICTION_ENABLED !== "false") { + // First sweep shortly after boot: a daemon restarted more often than daily + // would otherwise never reach its first 24h tick. + const runEviction = async () => { + try { + await sdk.trigger({ function_id: "mem::evict", payload: { dryRun: false } }); + } catch {} + }; + setTimeout(runEviction, FIRST_SWEEP_DELAY_MS).unref(); + setInterval(runEviction, 86400000).unref(); + bootLog(`Eviction sweep: enabled (5 min after boot, then every 24h)`); + } + + const runAuditEviction = async () => { + try { + await evictOldestAudit(kv); + } catch {} + }; + setTimeout(runAuditEviction, FIRST_SWEEP_DELAY_MS).unref(); + setInterval(runAuditEviction, 86400000).unref(); + if (process.env.INSIGHT_DECAY_ENABLED !== "false") { const insightDecayTimer = setInterval(async () => { try { diff --git a/src/mcp/tools-registry.ts b/src/mcp/tools-registry.ts index 849e1f2f36..0288100f35 100644 --- a/src/mcp/tools-registry.ts +++ b/src/mcp/tools-registry.ts @@ -836,7 +836,7 @@ export const V070_TOOLS: McpToolDef[] = [ project: { type: "string", description: "Filter by project" }, minConfidence: { type: "number", - description: "Minimum confidence threshold (default 0.1)", + description: "Minimum confidence threshold (default 0)", }, limit: { type: "number", description: "Max results (default 10)" }, }, diff --git a/src/providers/_fetch.ts b/src/providers/_fetch.ts index 3b3b9041b5..ac36e55ee8 100644 --- a/src/providers/_fetch.ts +++ b/src/providers/_fetch.ts @@ -3,15 +3,34 @@ import { getEnvVar } from "../config.js"; // Bounded retry for transient rate-limit / unavailable responses. Attempts is // total tries (initial + retries). Retries are bounded by a TOTAL elapsed // deadline — not per-attempt — so the worst case never blows past the caller's -// timeout budget. A single retry delay is capped low so a hostile Retry-After -// header can't dominate the budget. +// timeout budget. A Retry-After is honored in full when it fits that budget: +// every LLM call is background work, and retrying a busy server sooner than it +// asked only earns another 429. const MAX_ATTEMPTS = 3; -const MAX_RETRY_DELAY_MS = 5000; // A retry only makes sense if there's room for at least a token attempt after // the sleep; without this floor we'd sleep, fire, and get instantly cut off. const MIN_ATTEMPT_FLOOR_MS = 100; const RETRY_STATUS = new Set([429, 503]); +// A non-2xx provider response, carrying its status so callers can tell a busy +// provider from a broken one. SDK errors (Anthropic) carry `status` the same way. +export class ProviderHttpError extends Error { + constructor( + message: string, + readonly status: number, + ) { + super(message); + } +} + +// OpenAI answers an exhausted quota with 429 too; that never clears by waiting, +// so it counts as broken and opens the breaker. +export function isProviderBusy(err: unknown): boolean { + const status = (err as { status?: unknown } | null)?.status; + if (typeof status !== "number" || !RETRY_STATUS.has(status)) return false; + return !/insufficient_quota/.test(err instanceof Error ? err.message : ""); +} + const sleep = (ms: number): Promise => new Promise((resolve) => setTimeout(resolve, ms)); @@ -73,11 +92,11 @@ export async function fetchWithTimeout( const retryAfter = parseRetryAfter(response.headers.get("Retry-After")); // Exponential backoff fallback when no Retry-After: 500ms, 1000ms, ... const backoff = 500 * 2 ** (attempt - 1); - const delay = Math.min(retryAfter ?? backoff, MAX_RETRY_DELAY_MS); + const delay = retryAfter ?? backoff; // Stop retrying if the sleep plus a minimal attempt would overrun the total - // budget — a hostile Retry-After that alone exceeds the remaining budget - // returns the last response instead of stalling the caller. + // budget — a Retry-After that alone exceeds the remaining budget returns + // the last response instead of stalling the caller. const elapsed = Date.now() - start; const remaining = ms - elapsed; if (delay + MIN_ATTEMPT_FLOOR_MS > remaining) return response; diff --git a/src/providers/minimax.ts b/src/providers/minimax.ts index 77c0dcd27c..22ca125187 100644 --- a/src/providers/minimax.ts +++ b/src/providers/minimax.ts @@ -1,6 +1,6 @@ import type { MemoryProvider } from '../types.js' import { getEnvVar } from '../config.js' -import { fetchWithTimeout } from './_fetch.js' +import { fetchWithTimeout, ProviderHttpError } from './_fetch.js' /** * MiniMax provider using raw fetch to call MiniMax's Anthropic-compatible API. @@ -58,7 +58,7 @@ export class MinimaxProvider implements MemoryProvider { if (!response.ok) { const text = await response.text() - throw new Error(`MiniMax API error ${response.status}: ${text}`) + throw new ProviderHttpError(`MiniMax API error ${response.status}: ${text}`, response.status) } const data = (await response.json()) as { diff --git a/src/providers/openai.ts b/src/providers/openai.ts index 13aeccaa2f..299e70f10d 100644 --- a/src/providers/openai.ts +++ b/src/providers/openai.ts @@ -1,6 +1,6 @@ import type { MemoryProvider } from "../types.js"; import { getEnvVar } from "../config.js"; -import { fetchWithTimeout } from "./_fetch.js"; +import { fetchWithTimeout, ProviderHttpError } from "./_fetch.js"; import { DEFAULT_AZURE_API_VERSION, buildAuthHeaders, @@ -56,6 +56,10 @@ export class OpenAIProvider implements MemoryProvider { private isAzure: boolean; private azureApiVersion: string; private tokenizeUnavailable = false; + // api.openai.com rejects max_tokens on reasoning models and accepts + // max_completion_tokens on every model; compatible servers may only know + // max_tokens (#1219). + private outputTokenParam: "max_tokens" | "max_completion_tokens"; constructor(apiKey: string, model: string, maxTokens: number, baseURL?: string) { this.apiKey = apiKey; @@ -67,6 +71,9 @@ export class OpenAIProvider implements MemoryProvider { this.azureApiVersion = getEnvVar("OPENAI_API_VERSION") || DEFAULT_AZURE_API_VERSION; this.isAzure = detectAzure(this.baseUrl); + this.outputTokenParam = /^https:\/\/api\.openai\.com(\/|$)/.test(this.baseUrl) + ? "max_completion_tokens" + : "max_tokens"; } async compress(systemPrompt: string, userPrompt: string): Promise { @@ -112,7 +119,7 @@ export class OpenAIProvider implements MemoryProvider { const url = buildChatUrl(this.baseUrl, this.isAzure, this.azureApiVersion); const body: Record = { model: this.model, - max_tokens: this.maxTokens, + [this.outputTokenParam]: this.maxTokens, // OpenAI API spec defines `stream` as defaulting to false, so omitting // it should yield a JSON response. Some OpenAI-compatible proxies // (notably 9Router < 0.4.56 — see decolua/9router#1260) default to @@ -158,7 +165,7 @@ export class OpenAIProvider implements MemoryProvider { if (!response.ok) { const text = await response.text(); - throw new Error(`OpenAI API error (${response.status}): ${text}`); + throw new ProviderHttpError(`OpenAI API error (${response.status}): ${text}`, response.status); } const data = (await response.json()) as { diff --git a/src/providers/openrouter.ts b/src/providers/openrouter.ts index 5c47bb0a87..2dd8129594 100644 --- a/src/providers/openrouter.ts +++ b/src/providers/openrouter.ts @@ -1,5 +1,5 @@ import type { MemoryProvider } from "../types.js"; -import { fetchWithTimeout } from "./_fetch.js"; +import { fetchWithTimeout, ProviderHttpError } from "./_fetch.js"; export class OpenRouterProvider implements MemoryProvider { name: string; @@ -54,7 +54,7 @@ export class OpenRouterProvider implements MemoryProvider { if (!response.ok) { const text = await response.text(); - throw new Error(`${this.name} API error (${response.status}): ${text}`); + throw new ProviderHttpError(`${this.name} API error (${response.status}): ${text}`, response.status); } const data = (await response.json()) as Record; diff --git a/src/providers/resilient.ts b/src/providers/resilient.ts index 698c48c2c4..88a7ff98bf 100644 --- a/src/providers/resilient.ts +++ b/src/providers/resilient.ts @@ -1,10 +1,20 @@ import type { MemoryProvider, CircuitBreakerState } from "../types.js"; import { CircuitBreaker } from "./circuit-breaker.js"; +import { isProviderBusy } from "./_fetch.js"; +import { getEnvVar } from "../config.js"; export type ProviderOperation = "compress" | "summarize" | "describeImage"; const OPERATIONS: ProviderOperation[] = ["compress", "summarize", "describeImage"]; +const MAX_CONCURRENCY_DEFAULT = 4; + +function maxConcurrency(): number { + const raw = getEnvVar("AGENTMEMORY_LLM_MAX_CONCURRENCY")?.trim(); + const n = raw && /^\d+$/.test(raw) ? Number(raw) : 0; + return n > 0 ? n : MAX_CONCURRENCY_DEFAULT; +} + const SEVERITY: Record = { closed: 0, "half-open": 1, @@ -17,6 +27,8 @@ export class ResilientProvider implements MemoryProvider { private breakers = Object.fromEntries( OPERATIONS.map((op) => [op, new CircuitBreaker()]), ) as Record; + private inFlight = 0; + private waiters: Array<() => void> = []; name: string; // A failed count is a fallback for the caller, not a provider failure, // so it is forwarded outside the breaker. @@ -36,6 +48,23 @@ export class ResilientProvider implements MemoryProvider { } } + // One cap across every generating operation: a busy provider is busy for all + // of them, and fanning out past it only turns queued work into 429s. + // countTokens stays outside it; summarize bounds those with COUNT_CONCURRENCY. + private async acquireSlot(): Promise { + if (this.inFlight < maxConcurrency()) { + this.inFlight++; + return; + } + await new Promise((resolve) => this.waiters.push(resolve)); + } + + private releaseSlot(): void { + const next = this.waiters.shift(); + if (next) next(); + else this.inFlight--; + } + private async call( operation: ProviderOperation, fn: () => Promise, @@ -44,13 +73,18 @@ export class ResilientProvider implements MemoryProvider { if (!breaker.isAllowed) { throw new Error("circuit_breaker_open"); } + await this.acquireSlot(); try { const result = await fn(); breaker.recordSuccess(); return result; } catch (err) { - breaker.recordFailure(); + // A provider that says "busy" is healthy; opening the breaker on it + // would fail unrelated work for the whole cooldown. + if (!isProviderBusy(err)) breaker.recordFailure(); throw err; + } finally { + this.releaseSlot(); } } diff --git a/src/state/index-corpus.ts b/src/state/index-corpus.ts index d813f30680..4fe960a188 100644 --- a/src/state/index-corpus.ts +++ b/src/state/index-corpus.ts @@ -60,10 +60,16 @@ export function embedInputHash(text: string): string { // timeline, session history, the viewer and expandIds — they just stop // competing in search. That is how "observations are kept" is satisfied // structurally rather than by promise. +// Unset means agentmemory's own MCP tools, under both the plugin and the +// standalone server name; set it empty to index everything. +const DEFAULT_EXCLUDED_TOOL_PREFIXES = + "mcp__plugin_agentmemory_agentmemory__,mcp__agentmemory__"; let excludeCache: { raw: string; prefixes: string[] } | null = null; function excludedToolPrefixes(): string[] { - const raw = process.env.AGENTMEMORY_INDEX_EXCLUDE_TOOL_PREFIXES ?? ""; + const raw = + process.env.AGENTMEMORY_INDEX_EXCLUDE_TOOL_PREFIXES ?? + DEFAULT_EXCLUDED_TOOL_PREFIXES; if (!excludeCache || excludeCache.raw !== raw) { excludeCache = { raw, diff --git a/test/audit.test.ts b/test/audit.test.ts index 12cb66104f..214ec7fbfe 100644 --- a/test/audit.test.ts +++ b/test/audit.test.ts @@ -4,7 +4,7 @@ vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, })); -import { recordAudit, queryAudit } from "../src/functions/audit.js"; +import { recordAudit, queryAudit, evictOldestAudit } from "../src/functions/audit.js"; function mockKV() { const store = new Map>(); @@ -103,4 +103,27 @@ describe("Audit Functions", () => { const entries = await queryAudit(kv as never, { limit: 3 }); expect(entries.length).toBe(3); }); + + it("evictOldestAudit keeps the newest AGENTMEMORY_AUDIT_MAX entries", async () => { + process.env.AGENTMEMORY_AUDIT_MAX = "2"; + try { + for (let i = 0; i < 4; i++) { + await kv.set("mem:audit", `aud_${i}`, { + id: `aud_${i}`, + timestamp: new Date(Date.UTC(2026, 0, 1 + i)).toISOString(), + operation: "observe", + functionId: "mem::observe", + targetIds: [], + details: {}, + }); + } + + expect(await evictOldestAudit(kv as never)).toBe(2); + + const kept = (await kv.list<{ id: string }>("mem:audit")).map((e) => e.id).sort(); + expect(kept).toEqual(["aud_2", "aud_3"]); + } finally { + delete process.env.AGENTMEMORY_AUDIT_MAX; + } + }); }); diff --git a/test/consolidation-pipeline.test.ts b/test/consolidation-pipeline.test.ts index 4852191572..ea0ad8b1a2 100644 --- a/test/consolidation-pipeline.test.ts +++ b/test/consolidation-pipeline.test.ts @@ -119,7 +119,7 @@ describe("Consolidation Pipeline", () => { })) as { success: boolean; results: Record }; expect(result.success).toBe(true); - const semantic = result.results.semantic as { skipped: boolean; reason: string }; + const semantic = (result.results.semantic as Record)["test-project"]; expect(semantic.skipped).toBe(true); expect(semantic.reason).toContain("fewer than 5"); expect(provider.summarize).not.toHaveBeenCalled(); @@ -168,7 +168,7 @@ describe("Consolidation Pipeline", () => { })) as { success: boolean; results: Record }; expect(result.success).toBe(true); - const semantic = result.results.semantic as { newFacts: number }; + const semantic = (result.results.semantic as Record)["test-project"]; expect(semantic.newFacts).toBe(1); const stored = await kv.list("mem:semantic"); @@ -337,3 +337,107 @@ describe("Consolidation Pipeline: decay writes and single flight", () => { expect(third.success).toBe(true); }); }); + +describe("Consolidation Pipeline: per-project scope (#1344)", () => { + function summaryFor(project: string, i: number): SessionSummary { + return { ...makeSummary(i), sessionId: `${project}_${i}`, project, narrative: `${project} work ${i}` }; + } + + it("an unscoped run consolidates each project with new summaries on its own", async () => { + const sdk = mockSdk(); + const kv = mockKV(); + const provider = { name: "test", compress: vi.fn(), summarize: vi.fn().mockResolvedValue("") }; + const reflected: unknown[] = []; + sdk.registerFunction("mem::reflect", async (payload: unknown) => { + reflected.push(payload); + return { success: true }; + }); + registerConsolidationPipelineFunction(sdk as never, kv as never, provider as never); + for (let i = 0; i < 5; i++) { + await kv.set("mem:summaries", `alpha_${i}`, summaryFor("alpha", i)); + await kv.set("mem:summaries", `beta_${i}`, summaryFor("beta", i)); + } + + const result = (await sdk.trigger("mem::consolidate-pipeline", {})) as { + results: { semantic: Record }; + }; + + expect(Object.keys(result.results.semantic).sort()).toEqual(["alpha", "beta"]); + expect(reflected).toEqual([ + { maxClusters: 10, project: "alpha" }, + { maxClusters: 10, project: "beta" }, + ]); + for (const [, prompt] of provider.summarize.mock.calls) { + const mentionsAlpha = String(prompt).includes("alpha work"); + const mentionsBeta = String(prompt).includes("beta work"); + expect(mentionsAlpha !== mentionsBeta).toBe(true); + } + }); + + it("a later unscoped run skips projects with no new summaries since the last one", async () => { + const sdk = mockSdk(); + const kv = mockKV(); + const provider = { name: "test", compress: vi.fn(), summarize: vi.fn().mockResolvedValue("") }; + sdk.registerFunction("mem::reflect", async () => ({ success: true })); + registerConsolidationPipelineFunction(sdk as never, kv as never, provider as never); + for (let i = 0; i < 5; i++) await kv.set("mem:summaries", `alpha_${i}`, summaryFor("alpha", i)); + + await sdk.trigger("mem::consolidate-pipeline", {}); + const second = (await sdk.trigger("mem::consolidate-pipeline", {})) as { + results: { semantic: Record; reflect: Record }; + }; + + expect(second.results.semantic).toEqual({}); + expect(second.results.reflect).toEqual({}); + expect(provider.summarize).toHaveBeenCalledTimes(1); + }); + + it("retries a project whose reflect failed on the next unscoped run", async () => { + const sdk = mockSdk(); + const kv = mockKV(); + const provider = { name: "test", compress: vi.fn(), summarize: vi.fn().mockResolvedValue("") }; + let calls = 0; + sdk.registerFunction("mem::reflect", async () => { + calls++; + if (calls === 1) throw new Error("provider busy"); + return { success: true }; + }); + registerConsolidationPipelineFunction(sdk as never, kv as never, provider as never); + for (let i = 0; i < 5; i++) await kv.set("mem:summaries", `alpha_${i}`, summaryFor("alpha", i)); + + await sdk.trigger("mem::consolidate-pipeline", { tier: "reflect" }); + const second = (await sdk.trigger("mem::consolidate-pipeline", { tier: "reflect" })) as { + results: { reflect: Record }; + }; + const third = (await sdk.trigger("mem::consolidate-pipeline", { tier: "reflect" })) as { + results: { reflect: Record }; + }; + + expect(second.results.reflect).toEqual({ alpha: { success: true } }); + expect(third.results.reflect).toEqual({}); + }); + + it("credits a fact another project already holds to this project's Sessions too", async () => { + const sdk = mockSdk(); + const kv = mockKV(); + const provider = { + name: "test", + compress: vi.fn(), + summarize: vi.fn().mockResolvedValue(`Use node:sqlite`), + }; + sdk.registerFunction("mem::reflect", async () => ({ success: true })); + registerConsolidationPipelineFunction(sdk as never, kv as never, provider as never); + for (let i = 0; i < 5; i++) { + await kv.set("mem:summaries", `alpha_${i}`, summaryFor("alpha", i)); + await kv.set("mem:summaries", `beta_${i}`, summaryFor("beta", i)); + } + + await sdk.trigger("mem::consolidate-pipeline", { tier: "semantic" }); + + const facts = await kv.list("mem:semantic"); + expect(facts).toHaveLength(1); + expect(facts[0].sourceSessionIds).toEqual( + expect.arrayContaining(["alpha_0", "beta_0"]), + ); + }); +}); diff --git a/test/context-injection.test.ts b/test/context-injection.test.ts index 0b1148e80d..92ce71c10f 100644 --- a/test/context-injection.test.ts +++ b/test/context-injection.test.ts @@ -1,4 +1,5 @@ -import { describe, it, expect } from "vitest"; +import { describe, it, expect, beforeAll, afterAll } from "vitest"; +import { createServer, type Server } from "node:http"; import { spawn } from "node:child_process"; import { join } from "node:path"; @@ -113,6 +114,55 @@ describe("pre-tool-use hook — context injection gate (#143)", () => { }); }); +describe("pre-tool-use hook — context envelope (#1278)", () => { + let server: Server; + let url = ""; + + beforeAll(async () => { + server = createServer((_req, res) => { + res.setHeader("Content-Type", "application/json"); + res.end(JSON.stringify({ context: "remembered about foo.ts" })); + }); + await new Promise((r) => server.listen(0, "127.0.0.1", r)); + const addr = server.address(); + url = `http://127.0.0.1:${typeof addr === "object" && addr ? addr.port : 0}`; + }); + + afterAll(() => new Promise((r) => server.close(() => r()))); + + it("wraps context in the hookSpecificOutput envelope for Claude Code", async () => { + const payload = JSON.stringify({ + session_id: "ses_test", + hook_event_name: "PreToolUse", + tool_name: "Read", + tool_input: { file_path: "src/foo.ts" }, + }); + const result = await runHook("pre-tool-use.mjs", payload, { + AGENTMEMORY_INJECT_CONTEXT: "true", + AGENTMEMORY_URL: url, + }); + expect(JSON.parse(result.stdout)).toEqual({ + hookSpecificOutput: { + hookEventName: "PreToolUse", + additionalContext: "remembered about foo.ts", + }, + }); + }); + + it("keeps plain text for hosts that send no hook_event_name", async () => { + const payload = JSON.stringify({ + conversation_id: "ses_test", + toolName: "read", + toolArgs: { path: "src/foo.ts" }, + }); + const result = await runHook("pre-tool-use.mjs", payload, { + AGENTMEMORY_INJECT_CONTEXT: "true", + AGENTMEMORY_URL: url, + }); + expect(result.stdout).toBe("remembered about foo.ts"); + }); +}); + describe("session-start hook — context injection gate (#143)", () => { it("registers the session but writes nothing to stdout when AGENTMEMORY_INJECT_CONTEXT is unset", async () => { // Session registration POST will fail against the unreachable URL, diff --git a/test/fetch-timeout.test.ts b/test/fetch-timeout.test.ts index f761050196..1d0b24c457 100644 --- a/test/fetch-timeout.test.ts +++ b/test/fetch-timeout.test.ts @@ -184,10 +184,9 @@ describe("fetchWithTimeout bounded retry (total deadline)", () => { expect(q.calls()).toBe(2); }); - // 2. 429 with a hostile Retry-After (100000s) must NOT wait that long. The - // delay collapses to the low per-delay cap (5000ms) so the retry fires - // quickly and total elapsed stays nowhere near 100000s. - it("does not honour a hostile Retry-After literally — caps it and stays within budget", async () => { + // 2. 429 with a hostile Retry-After (100000s) must NOT wait that long: it + // cannot fit the budget, so the 429 comes back without a retry. + it("returns a hostile Retry-After's 429 at once instead of waiting it out", async () => { const q = queuedFetch([ { status: 429, headers: { "Retry-After": "100000" } }, // 100000s { status: 200 }, @@ -197,23 +196,18 @@ describe("fetchWithTimeout bounded retry (total deadline)", () => { const start = Date.now(); const p = fetchWithTimeout("https://example.com", {}, 60000); - // runAllTimers drains every scheduled sleep; if the code honored 100000s - // literally this would advance 100_000_000ms of simulated time. await vi.runAllTimersAsync(); const res = await p; const elapsed = Date.now() - start; - expect(res.status).toBe(200); - expect(q.calls()).toBe(2); - // The only sleep was the capped 5000ms delay — total elapsed is bounded to - // the cap, nowhere near the 100_000_000ms the header requested. - expect(elapsed).toBeLessThanOrEqual(5000); + expect(res.status).toBe(429); + expect(q.calls()).toBe(1); expect(elapsed).toBeLessThan(60000); }); - // 2c. When the capped delay still cannot fit inside a small budget, we do NOT + // 2c. When the requested delay cannot fit inside a small budget, we do NOT // retry and return the last 429 within bound. - it("returns the last 429 without retrying when even the capped delay overruns a small budget", async () => { + it("returns the last 429 without retrying when the delay overruns a small budget", async () => { const q = queuedFetch([ { status: 429, headers: { "Retry-After": "100000" } }, { status: 200 }, @@ -222,7 +216,7 @@ describe("fetchWithTimeout bounded retry (total deadline)", () => { vi.useFakeTimers(); const start = Date.now(); - // Budget 1000ms: capped delay 5000ms + floor 100ms > remaining, no retry. + // Budget 1000ms: delay + floor 100ms > remaining, no retry. const p = fetchWithTimeout("https://example.com", {}, 1000); await vi.runAllTimersAsync(); const res = await p; @@ -233,20 +227,20 @@ describe("fetchWithTimeout bounded retry (total deadline)", () => { expect(elapsed).toBeLessThan(1000); }); - // 2b. Retry-After present but LARGER than the low per-delay cap collapses to - // MAX_RETRY_DELAY_MS (5000), still bounded, still retries when budget - // allows. - it("caps an oversized Retry-After to the max delay and still retries within budget", async () => { + // 2b. A long Retry-After that fits the budget is waited out in full — a busy + // server asking for 30s gets 30s, not an early retry that 429s again. + it("honors a long Retry-After in full when it fits the budget", async () => { const q = queuedFetch([ - { status: 503, headers: { "Retry-After": "60" } }, // 60s requested + { status: 503, headers: { "Retry-After": "30" } }, { status: 200 }, ]); vi.spyOn(globalThis, "fetch").mockImplementation(q.fetch); vi.useFakeTimers(); const p = fetchWithTimeout("https://example.com", {}, 60000); - // Requested 60s, but the honored delay is capped at 5000ms. - await vi.advanceTimersByTimeAsync(5000); + await vi.advanceTimersByTimeAsync(29_999); + expect(q.calls()).toBe(1); + await vi.advanceTimersByTimeAsync(1); const res = await p; expect(res.status).toBe(200); @@ -294,10 +288,8 @@ describe("fetchWithTimeout bounded retry (total deadline)", () => { expect(q.calls()).toBe(1); }); - // 5. Retry-After as an HTTP-date is parsed, capped to the max delay, and the - // retry fires within budget. - it("parses an HTTP-date Retry-After and caps the honored delay", async () => { - // 2s in the future — under the max delay cap, so honored as-is. + // 5. Retry-After as an HTTP-date is parsed and the retry fires within budget. + it("parses an HTTP-date Retry-After", async () => { const future = new Date(Date.now() + 2000).toUTCString(); const q = queuedFetch([ { status: 429, headers: { "Retry-After": future } }, @@ -314,8 +306,8 @@ describe("fetchWithTimeout bounded retry (total deadline)", () => { expect(q.calls()).toBe(2); }); - // 5b. A far-future HTTP-date collapses to the capped delay (5000ms), which - // still exceeds a small budget → no retry, last 503 returned in bound. + // 5b. A far-future HTTP-date exceeds a small budget → no retry, last 503 + // returned in bound. it("does not retry on a far-future HTTP-date Retry-After that overruns a small budget", async () => { const farFuture = new Date(Date.now() + 3600_000).toUTCString(); // 1h out const q = queuedFetch([ @@ -326,7 +318,7 @@ describe("fetchWithTimeout bounded retry (total deadline)", () => { vi.useFakeTimers(); const start = Date.now(); - // Budget 1000ms: capped delay 5000ms + floor > remaining, so no retry. + // Budget 1000ms: delay + floor > remaining, so no retry. const p = fetchWithTimeout("https://example.com", {}, 1000); await vi.runAllTimersAsync(); const res = await p; diff --git a/test/hook-project.test.ts b/test/hook-project.test.ts index 37d0e424de..9072ba0847 100644 --- a/test/hook-project.test.ts +++ b/test/hook-project.test.ts @@ -66,6 +66,16 @@ describe("resolveProject — hook project basename resolver", () => { expect(resolveProject(nestedDir)).toBe(REPO_NAME); }); + it("names a linked worktree after its main checkout (#728)", () => { + const git = (...args: string[]) => execFileSync("git", args, { cwd: repoDir, stdio: "ignore" }); + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "--allow-empty", "--quiet", "-m", "init"); + const worktree = join(tmpRoot, "goosefish"); + git("worktree", "add", "--quiet", worktree); + mkdirSync(join(worktree, "src"), { recursive: true }); + expect(resolveProject(worktree)).toBe(REPO_NAME); + expect(resolveProject(join(worktree, "src"))).toBe(REPO_NAME); + }); + it("falls back to basename(cwd) when not in a git repo", () => { // mkdtemp lands under os.tmpdir(), which is not always outside a repository — // TMPDIR pointed at a working directory makes git walk up and find one, and the diff --git a/test/index-echo-exclusion.test.ts b/test/index-echo-exclusion.test.ts index c6b487095d..4b60c07e31 100644 --- a/test/index-echo-exclusion.test.ts +++ b/test/index-echo-exclusion.test.ts @@ -70,8 +70,15 @@ describe("index exclusion by toolName prefix", () => { setEmbeddingProvider(null); }); - it("excludes nothing when the flag is unset (default behaviour)", () => { - expect(isIndexExcluded(obs("obs_1", `${ECHO_PREFIX}memory_smart_search`))).toBe(false); + it("excludes agentmemory's own MCP tools when the flag is unset (#993)", () => { + expect(isIndexExcluded(obs("o", `${ECHO_PREFIX}memory_smart_search`))).toBe(true); + expect(isIndexExcluded(obs("o", "mcp__agentmemory__memory_recall"))).toBe(true); + expect(isIndexExcluded(obs("o", "Bash"))).toBe(false); + }); + + it("excludes nothing when the flag is set empty", () => { + process.env[FLAG] = ""; + expect(isIndexExcluded(obs("o", `${ECHO_PREFIX}memory_smart_search`))).toBe(false); }); it("matches on prefix, and only on the configured prefixes", () => { diff --git a/test/lesson-index-recall.test.ts b/test/lesson-index-recall.test.ts index d9bb8f0f2d..fc7d5441ac 100644 --- a/test/lesson-index-recall.test.ts +++ b/test/lesson-index-recall.test.ts @@ -61,6 +61,29 @@ describe("lesson recall through the lesson index", () => { expect(res.lessons[0].score).toBeGreaterThan(res.lessons[1].score); }); + it("recalls a reinforced lesson that decayed to the confidence floor", async () => { + const { sdk, kv } = await setup(); + await kv.set("mem:lessons", "lsn_floor", { + id: "lsn_floor", + content: "never force-push a shared branch", + context: "", + confidence: 0.05, + reinforcements: 1, + source: "manual", + sourceIds: [], + tags: ["git"], + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + decayRate: 0.05, + }); + + const res = (await sdk.trigger("mem::lesson-recall", { + query: "force-push shared branch", + })) as { lessons: Array<{ id: string }> }; + + expect(res.lessons.map((l) => l.id)).toEqual(["lsn_floor"]); + }); + it("recalls lessons that existed before the index was built (lazy rebuild)", async () => { const { sdk, kv } = await setup(); await kv.set("mem:lessons", "lsn_pre", { diff --git a/test/observe-session-cap.test.ts b/test/observe-session-cap.test.ts new file mode 100644 index 0000000000..232903fa07 --- /dev/null +++ b/test/observe-session-cap.test.ts @@ -0,0 +1,111 @@ +import { describe, it, expect, vi } from "vitest"; + +vi.mock("../src/logger.js", () => ({ + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, +})); + +import { registerObserveFunction } from "../src/functions/observe.js"; +import { getSearchIndex } from "../src/functions/search.js"; +import { KV } from "../src/state/schema.js"; + +function mockKV() { + const store = new Map>(); + return { + get: async (scope: string, key: string): Promise => + (store.get(scope)?.get(key) as T) ?? null, + set: async (scope: string, key: string, data: T): Promise => { + if (!store.has(scope)) store.set(scope, new Map()); + store.get(scope)!.set(key, data); + return data; + }, + delete: async (scope: string, key: string) => { + store.get(scope)?.delete(key); + }, + list: async (scope: string): Promise => + Array.from(store.get(scope)?.values() ?? []) as T[], + }; +} + +function mockSdk() { + const fns = new Map(); + return { + registerFunction: (id: string, fn: Function) => fns.set(id, fn), + registerTrigger: () => {}, + trigger: async (input: { function_id: string; payload?: unknown }) => + fns.get(input.function_id)?.(input.payload) ?? null, + }; +} + +const SESSION = "ses_cap"; + +function stored(id: string, importance: number | undefined, timestamp: string) { + return { + id, + sessionId: SESSION, + timestamp, + title: `obs ${id}`, + type: "file_read", + facts: [], + narrative: "", + concepts: [], + files: [], + ...(importance === undefined ? {} : { importance }), + }; +} + +async function setup(rows: ReturnType[], cap: number) { + const kv = mockKV(); + const sdk = mockSdk(); + for (const r of rows) await kv.set(KV.observations(SESSION), r.id, r); + registerObserveFunction(sdk as never, kv as never, undefined, cap); + const result = (await sdk.trigger({ + function_id: "mem::observe", + payload: { + sessionId: SESSION, + hookType: "post_tool_use", + timestamp: "2026-05-01T00:00:00Z", + data: { tool_name: "Read", tool_input: { file_path: "new.ts" } }, + }, + })) as { observationId?: string; success?: boolean }; + const ids = (await kv.list<{ id: string }>(KV.observations(SESSION))).map((o) => o.id); + return { result, ids, kv }; +} + +describe("mem::observe at MAX_OBS_PER_SESSION (PR#1174)", () => { + it("admits the new observation by evicting the least important one", async () => { + const { result, ids, kv } = await setup( + [ + stored("keep", 8, "2026-01-01T00:00:00Z"), + stored("drop", 2, "2026-01-02T00:00:00Z"), + stored("raw", undefined, "2026-01-03T00:00:00Z"), + ], + 3, + ); + expect(result.observationId).toBeTruthy(); + expect(ids).toHaveLength(3); + expect(ids).toContain("keep"); + expect(ids).toContain("raw"); + expect(ids).not.toContain("drop"); + const audits = await kv.list<{ operation: string; functionId: string; targetIds: string[] }>(KV.audit); + expect(audits).toContainEqual( + expect.objectContaining({ operation: "delete", functionId: "mem::observe", targetIds: ["drop"] }), + ); + }); + + it("evicts the older row on an importance tie and drops it from search", async () => { + getSearchIndex().add(stored("older", 5, "2026-01-01T00:00:00Z") as never); + const { ids } = await setup( + [stored("older", 5, "2026-01-01T00:00:00Z"), stored("newer", 5, "2026-01-02T00:00:00Z")], + 2, + ); + expect(ids).not.toContain("older"); + expect(ids).toContain("newer"); + expect(getSearchIndex().has("older")).toBe(false); + }); + + it("evicts nothing under the cap", async () => { + const { ids } = await setup([stored("a", 1, "2026-01-01T00:00:00Z")], 3); + expect(ids).toHaveLength(2); + expect(ids).toContain("a"); + }); +}); diff --git a/test/openai-token-param.test.ts b/test/openai-token-param.test.ts new file mode 100644 index 0000000000..7a005567e8 --- /dev/null +++ b/test/openai-token-param.test.ts @@ -0,0 +1,40 @@ +import { describe, it, expect, vi, afterEach } from "vitest"; +import { OpenAIProvider } from "../src/providers/openai.js"; + +function sentBody(fetchSpy: ReturnType): Record { + const [, init] = fetchSpy.mock.calls[0] as [string, RequestInit]; + return JSON.parse(init.body as string); +} + +describe("OpenAIProvider output-token parameter (#1219)", () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + function stubFetch() { + return vi.spyOn(globalThis, "fetch").mockResolvedValue( + new Response(JSON.stringify({ choices: [{ message: { content: "ok" } }] }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }), + ); + } + + it("sends max_completion_tokens to api.openai.com, which rejects max_tokens on reasoning models", async () => { + const fetchSpy = stubFetch(); + await new OpenAIProvider("key", "gpt-5.6-luna", 4096, "https://api.openai.com").summarize("s", "u"); + + const body = sentBody(fetchSpy); + expect(body.max_completion_tokens).toBe(4096); + expect(body).not.toHaveProperty("max_tokens"); + }); + + it("keeps max_tokens for OpenAI-compatible servers", async () => { + const fetchSpy = stubFetch(); + await new OpenAIProvider("key", "general", 8192, "http://broker:4010/v1").summarize("s", "u"); + + const body = sentBody(fetchSpy); + expect(body.max_tokens).toBe(8192); + expect(body).not.toHaveProperty("max_completion_tokens"); + }); +}); diff --git a/test/reflect.test.ts b/test/reflect.test.ts index 3219afc7c5..5246c18aa4 100644 --- a/test/reflect.test.ts +++ b/test/reflect.test.ts @@ -360,3 +360,34 @@ describe("Reflect", () => { }); }); }); + +describe("mem::reflect project scope (#1344)", () => { + it("builds a project's insights only from that project's facts, crystals and concepts", async () => { + const sdk = mockSdk(); + const kv = mockKV(); + const provider = { name: "test", compress: vi.fn(), summarize: vi.fn().mockResolvedValue(XML_RESPONSE) }; + registerReflectFunctions(sdk as never, kv as never, provider as never); + await kv.set("mem:sessions", "ses_a", { id: "ses_a", project: "alpha" }); + await kv.set("mem:sessions", "ses_b", { id: "ses_b", project: "beta" }); + for (const [name, sessionId] of [["security", "ses_a"], ["validation", "ses_a"], ["beta", "ses_b"]]) { + await kv.set("mem:graph:nodes", `node_${name}`, { ...makeConceptNode(name), sessionId }); + } + await kv.set("mem:graph:edges", "edge_1", makeEdge("security", "validation")); + await kv.set("mem:graph:edges", "edge_2", makeEdge("security", "beta")); + const alphaFacts = ["Always validate security inputs", "Testing improves security coverage", "Validation prevents injection"]; + for (const [i, fact] of alphaFacts.entries()) { + await kv.set("mem:semantic", `sem_a${i}`, { ...makeSemantic(fact, `sem_a${i}`), sourceSessionIds: ["ses_a"] }); + } + await kv.set("mem:semantic", "sem_b", { ...makeSemantic("Beta security keys live in vault", "sem_b"), sourceSessionIds: ["ses_b"] }); + await kv.set("mem:crystals", "crys_b", { ...makeCrystal("beta work", ["Beta security rotates weekly"]), project: "beta" }); + + await sdk.trigger("mem::reflect", { project: "alpha" }); + + expect(provider.summarize).toHaveBeenCalled(); + for (const [, prompt] of provider.summarize.mock.calls) { + expect(String(prompt)).not.toMatch(/beta/i); + } + const insights = await kv.list("mem:insights"); + expect(insights.every((i) => i.project === "alpha")).toBe(true); + }); +}); diff --git a/test/replay-import-key.test.ts b/test/replay-import-key.test.ts index 1c1c991f3e..c125966534 100644 --- a/test/replay-import-key.test.ts +++ b/test/replay-import-key.test.ts @@ -246,3 +246,43 @@ describe("import-jsonl indexes observations into BM25 AND vector", () => { expect(getSearchIndex().size).toBeGreaterThan(0); }); }); + +describe("import-jsonl skips Claude Code's own background transcripts (#1064)", () => { + function writeTranscript(root: string, sessionId: string, prompt: string) { + const dir = join(root, "proj"); + require("node:fs").mkdirSync(dir, { recursive: true }); + const ts = "2026-04-17T10:00:00.000Z"; + writeFileSync( + join(dir, `${sessionId}.jsonl`), + [ + { type: "user", uuid: "u1", sessionId, timestamp: ts, cwd: root, + message: { role: "user", content: [{ type: "text", text: prompt }] } }, + { type: "assistant", uuid: "a1", sessionId, timestamp: ts, + message: { role: "assistant", content: [{ type: "text", text: "ok" }] } }, + ].map((l) => JSON.stringify(l)).join("\n") + "\n", + ); + } + + it("imports the real session and neither the Warmup probe nor the summary job", async () => { + const root = mkdtempSync(join(tmpdir(), "replay-harness-")); + writeTranscript(root, "sess-real", "Fix the login bug"); + writeTranscript(root, "sess-warmup", "Warmup"); + writeTranscript( + root, + "sess-summary", + "Context: This summary will be shown in a list to help users and Claude choose which conversations are relevant.", + ); + const kv = mockKV(); + const sdk = mockSdk(kv); + registerReplayFunctions(sdk, kv as never); + + const result = (await sdk.trigger("mem::replay::import-jsonl", { + path: root, + })) as { sessionIds: string[] }; + + expect(result.sessionIds).toEqual(["sess-real"]); + expect(await kv.get(KV.sessions, "sess-warmup")).toBeNull(); + expect(await kv.get(KV.sessions, "sess-summary")).toBeNull(); + rmSync(root, { recursive: true, force: true }); + }); +}); diff --git a/test/replay-lessons.test.ts b/test/replay-lessons.test.ts new file mode 100644 index 0000000000..904ec33071 --- /dev/null +++ b/test/replay-lessons.test.ts @@ -0,0 +1,120 @@ +import { describe, it, expect, vi } from "vitest"; +import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +vi.mock("../src/logger.js", () => ({ + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, +})); + +import { extractLessons, registerReplayFunctions } from "../src/functions/replay.js"; +import { KV } from "../src/state/schema.js"; +import type { Lesson } from "../src/types.js"; + +function mockKV() { + const store = new Map>(); + return { + get: async (scope: string, key: string): Promise => + (store.get(scope)?.get(key) as T) ?? null, + set: async (scope: string, key: string, value: T): Promise => { + if (!store.has(scope)) store.set(scope, new Map()); + store.get(scope)!.set(key, value); + return value; + }, + delete: async (scope: string, key: string) => { + store.get(scope)?.delete(key); + }, + list: async (scope: string): Promise => + Array.from(store.get(scope)?.values() ?? []) as T[], + }; +} + +function mockSdk() { + const fns = new Map(); + return { + registerFunction: (id: string, handler: Function) => fns.set(id, handler), + registerTrigger: () => {}, + trigger: async (input: { function_id: string; payload?: unknown }) => + fns.get(input.function_id)?.(input.payload) ?? { success: true }, + } as any; +} + +describe("extractLessons (#1292)", () => { + it("keeps a whole sentence that opens with a trigger", () => { + expect( + extractLessons(["Done. Always run the migration before seeding the db."]), + ).toEqual(["Always run the migration before seeding the db."]); + }); + + it("accepts a curly apostrophe in the trigger", () => { + expect(extractLessons(["Don’t commit the generated bundle by hand."])).toEqual([ + "Don’t commit the generated bundle by hand.", + ]); + }); + + it("drops a trigger that sits mid-sentence and would lose its subject", () => { + expect( + extractLessons(["The watchdog must never restart on ROUTE_MISSING_404."]), + ).toEqual([]); + }); + + it("does not stop at a dot inside a filename", () => { + expect( + extractLessons(["Never edit config.ts by hand, regenerate it instead."]), + ).toEqual(["Never edit config.ts by hand, regenerate it instead."]); + }); + + it("accepts a trigger after a list marker", () => { + expect(extractLessons(["- Prefer node:sqlite over a native addon here."])).toEqual([ + "Prefer node:sqlite over a native addon here.", + ]); + }); + + it("ignores triggers inside identifiers and unbalanced markup", () => { + expect( + extractLessons([ + "dont-repeat-yourself]]/some-page is linked here", + "Never mix** the statistical constraint with the other one", + ]), + ).toEqual([]); + }); + + it("rejects a sentence longer than the cap instead of truncating it", () => { + expect(extractLessons([`Always ${"x ".repeat(120)}end.`])).toEqual([]); + }); + + it("returns each lesson once", () => { + expect( + extractLessons(["Avoid global state in hooks.", "avoid global state in hooks."]), + ).toEqual(["Avoid global state in hooks."]); + }); +}); + +describe("import-jsonl lesson re-derivation (#1292)", () => { + it("does not reinforce a lesson just because a re-import matched it again", async () => { + const root = mkdtempSync(join(tmpdir(), "replay-lessons-")); + const dir = join(root, "proj"); + mkdirSync(dir); + const ts = "2026-04-17T10:00:00.000Z"; + writeFileSync( + join(dir, "sess-lesson.jsonl"), + [ + { type: "user", sessionId: "sess-lesson", timestamp: ts, cwd: root, + message: { role: "user", content: [{ type: "text", text: "hello" }] } }, + { type: "assistant", sessionId: "sess-lesson", timestamp: ts, + message: { role: "assistant", content: [{ type: "text", text: "Always run the migration before seeding." }] } }, + ].map((l) => JSON.stringify(l)).join("\n") + "\n", + ); + const kv = mockKV(); + const sdk = mockSdk(); + registerReplayFunctions(sdk, kv as never); + + await sdk.trigger({ function_id: "mem::replay::import-jsonl", payload: { path: root } }); + await sdk.trigger({ function_id: "mem::replay::import-jsonl", payload: { path: root } }); + + const lessons = await kv.list(KV.lessons); + expect(lessons).toHaveLength(1); + expect(lessons[0].reinforcements).toBe(0); + expect(lessons[0].lastReinforcedAt).toBeUndefined(); + }); +}); diff --git a/test/resilient-provider.test.ts b/test/resilient-provider.test.ts index 15c7630dc1..6cf2cca1c7 100644 --- a/test/resilient-provider.test.ts +++ b/test/resilient-provider.test.ts @@ -1,6 +1,7 @@ -import { describe, it, expect } from "vitest"; +import { describe, it, expect, afterEach } from "vitest"; import { ResilientProvider } from "../src/providers/resilient.js"; import type { MemoryProvider } from "../src/types.js"; +import { ProviderHttpError } from "../src/providers/_fetch.js"; function fakeProvider(overrides: Partial = {}): MemoryProvider { return { @@ -75,4 +76,95 @@ describe("ResilientProvider per-operation breakers", () => { expect(vision.circuitStates.compress.state).toBe("closed"); await expect(vision.compress("s", "u")).resolves.toBe("compressed"); }); + + it("does not open the breaker when the provider only says it is busy", async () => { + const provider = new ResilientProvider( + fakeProvider({ + summarize: async () => { + throw new ProviderHttpError("OpenAI API error (429): queue_full", 429); + }, + }), + ); + + await failTimes(() => provider.summarize("s", "u"), 5); + + expect(provider.circuitStates.summarize.state).toBe("closed"); + }); + + it("opens the breaker on a 429 that reports an exhausted quota", async () => { + const provider = new ResilientProvider( + fakeProvider({ + summarize: async () => { + throw new ProviderHttpError('OpenAI API error (429): {"error":{"code":"insufficient_quota"}}', 429); + }, + }), + ); + + await failTimes(() => provider.summarize("s", "u"), 5); + + expect(provider.circuitStates.summarize.state).toBe("open"); + }); + + it("treats an SDK error carrying status 503 as busy", async () => { + const provider = new ResilientProvider( + fakeProvider({ + compress: async () => { + throw Object.assign(new Error("overloaded"), { status: 503 }); + }, + }), + ); + + await failTimes(() => provider.compress("s", "u"), 5); + + expect(provider.circuitStates.compress.state).toBe("closed"); + }); +}); + +describe("ResilientProvider concurrency cap", () => { + afterEach(() => { + delete process.env.AGENTMEMORY_LLM_MAX_CONCURRENCY; + }); + + it("holds calls past AGENTMEMORY_LLM_MAX_CONCURRENCY until a slot frees, across operations", async () => { + process.env.AGENTMEMORY_LLM_MAX_CONCURRENCY = "1"; + let running = 0; + let peak = 0; + const releases: Array<() => void> = []; + const gated = () => + new Promise((resolve) => { + running++; + peak = Math.max(peak, running); + releases.push(() => { + running--; + resolve("ok"); + }); + }); + const provider = new ResilientProvider( + fakeProvider({ compress: gated, summarize: gated }), + ); + + const first = provider.compress("s", "u"); + const second = provider.summarize("s", "u"); + await new Promise((r) => setTimeout(r, 0)); + expect(releases).toHaveLength(1); + + releases[0](); + await first; + await new Promise((r) => setTimeout(r, 0)); + expect(releases).toHaveLength(2); + releases[1](); + await second; + + expect(peak).toBe(1); + }); + + it("frees the slot when a call throws", async () => { + process.env.AGENTMEMORY_LLM_MAX_CONCURRENCY = "1"; + const provider = new ResilientProvider( + fakeProvider({ summarize: async () => { throw new Error("boom"); } }), + ); + + await expect(provider.summarize("s", "u")).rejects.toThrow("boom"); + await expect(provider.compress("s", "u")).resolves.toBe("compressed"); + }); }); diff --git a/test/summarize.test.ts b/test/summarize.test.ts index f25f8a7cac..3a1b6c9d4e 100644 --- a/test/summarize.test.ts +++ b/test/summarize.test.ts @@ -544,6 +544,9 @@ describe("mem::summarize chunking", () => { }); it("measures every Observation once, even across the summarize retry", async () => { + // The chars/3 estimate (~31 per Observation with separator) overruns this + // budget, so the Session is measured; the measured 14 each fits exactly. + process.env.SUMMARIZE_CHUNK_TOKENS = budgetFor(5); let counted = 0; const provider = makeProvider(["garbage", summaryXml({ title: "second-attempt" })]); provider.countTokens = async () => { @@ -559,6 +562,36 @@ describe("mem::summarize chunking", () => { expect(counted).toBe(5); }); + it("still measures when chars/3 fits but the characters do not (dense text)", async () => { + process.env.SUMMARIZE_CHUNK_TOKENS = String(PROMPT_OVERHEAD + 200); + let counted = 0; + const provider = makeProvider([summaryXml({ title: "dense" })]); + provider.countTokens = async () => { + counted += 1; + return TOKENS_PER_OBS; + }; + const { handler } = await setupHandler({ sessionId: "ses_dense", obsCount: 5, provider }); + + await handler({ sessionId: "ses_dense" }); + + expect(counted).toBe(5); + }); + + it("skips measuring when the estimate already fits one chunk", async () => { + let counted = 0; + const provider = makeProvider([summaryXml({ title: "small" })]); + provider.countTokens = async () => { + counted += 1; + return TOKENS_PER_OBS; + }; + const { handler } = await setupHandler({ sessionId: "ses_fits", obsCount: 5, provider }); + + const result: any = await handler({ sessionId: "ses_fits" }); + + expect(result.success).toBe(true); + expect(counted).toBe(0); + }); + it("returns parse_failed only after both attempts fail", async () => { const provider = makeProvider([ "garbage one",