Files
Rohit Ghumare 281e10ca8c fix: stop burning Claude tokens on every PostToolUse hook (#138) (#140)
* fix: stop burning Claude tokens on every PostToolUse hook (#138)

mem::observe was firing mem::compress unconditionally on every
PostToolUse hook, which called Claude via the user's ANTHROPIC_API_KEY
to compress each raw observation into a structured summary. On an
active coding session (50-200 tool calls/hour) this silently burned
hundreds of thousands of Claude API tokens within minutes. Reported
by @olcor1 on a fresh Docker install — his token allocation was gone
in 20 minutes.

Make per-observation LLM compression opt-in.

- New env var AGENTMEMORY_AUTO_COMPRESS, default `false`. When false,
  observe.ts builds a zero-LLM synthetic CompressedObservation from
  raw tool I/O (title from toolName, narrative from truncated
  input/output, files extracted from file_path/pattern/etc., type
  inferred from camelCase-normalized tool name) and stores + indexes
  it. Recall and BM25 search still work.
- New src/functions/compress-synthetic.ts with buildSyntheticCompression().
- src/functions/observe.ts gates the compress trigger on the flag and
  writes the synthetic observation + stream events when disabled.
- Startup banner now logs "Auto-compress: OFF (default, #138)" or a
  loud warning when opt-in is on, so the mode is never silent.
- Regression test: test/auto-compress.test.ts covers the default path
  (no mem::compress fired), explicit opt-in, tool-name-to-type
  mapping, file-path extraction, narrative truncation, and the
  post_tool_failure→error mapping.

Bumps to 0.8.8 (main package + shim). 707 tests pass.

**Migration**: users who were relying on LLM-generated summaries set
AGENTMEMORY_AUTO_COMPRESS=true in ~/.agentmemory/.env and restart.
Existing compressed observations on disk are untouched.

* review: address CodeRabbit findings on #140

- compress-synthetic.ts:92 — use the fallback toolName for inferType
  so both title and type derive from the same resolved value
- test/auto-compress.test.ts — vi.resetModules() in beforeEach to
  prevent cached observe.js from seeing stale env var state between
  the default, opt-in, and explicit-off test cases
2026-04-14 15:42:17 +01:00
..