code-context-engine icon indicating copy to clipboard operation
code-context-engine copied to clipboard

feat(memory): adopt deterministic prose compression (cavemem-style grammar)

Open fazleelahhee opened this issue 7 months ago • 0 comments

Summary

Add a deterministic, model-free prose-compression pass on stored memory rows (decisions, turn_summaries, prompts) modelled after cavemem's "caveman grammar." The goal is ~40-55% fewer tokens per stored row while preserving every code block, file path, URL, version, date, and identifier byte-for-byte.

This is an optimisation of the existing extractive pipeline — we still extract the top-K sentences, then run them through the compressor before persisting. Reading still expands transparently for the model.

Motivation

cavemem (https://github.com/JuliusBrussee/cavemem) demonstrates that a pure-JS tokeniser + lexicon achieves measurable token savings on coding- session prose without any LLM call. Their docs claim ≥40% at `full`, ≥55% at `ultra`, with round-trip-tested preservation of structured content. Porting the technique to cce buys us:

  • Smaller `memory.db` (the current `tool_event_payloads` retention is the only bound on growth; compression compounds that win).
  • Fewer tokens injected at SessionStart resume — the budget is already tight when several decisions go in cold.
  • A second answer to "how do you compete with claude-mem's structured observations?" — not by structuring, but by being denser per byte.

Scope

Compress at write time:

  • `decisions.decision` and `decisions.reason`
  • `turn_summaries.summary`
  • (later) `sessions.rollup_summary`

Preserve byte-for-byte:

  • Inline code (`backticks`) and fenced blocks
  • File paths (`src/foo.py`, `/tmp/bar`)
  • URLs (`https://...`)
  • Version strings (`v1.2.3`, `0.3.1`)
  • Dates and ISO timestamps
  • Identifiers (CamelCase / snake_case / dotted.paths)
  • Numbers and units

Drop / abbreviate (only on `prose` tokens):

  • Articles (`a`, `an`, `the`) — but only outside code/identifiers
  • Filler verbs (`is`, `are`, `was`, `were`) where they don't change meaning
  • Hedges (`probably`, `maybe`, `I think`)
  • Common abbreviations from a lexicon (`because` → `b/c`, `with` → `w/`, `without` → `w/o`)

Acceptance criteria

  • [ ] New module `src/context_engine/memory/grammar.py` with `compress(text)` and `expand(text)` functions. Both pure, no IO, no external deps.
  • [ ] Tokeniser classifies tokens (fence / inline-code / url / path / version / date / number / identifier / heading / prose) — only `prose` is transformed.
  • [ ] Round-trip test: `expand(compress(x))` preserves every code block, URL, path, version, date for a corpus of ≥50 real cce-style strings.
  • [ ] Three intensity levels: `lite` (drop only articles), `full` (default, target ≥40%), `ultra` (lexicon, target ≥55%).
  • [ ] Wire `compress(level="full")` into:
    • `record_decision` MCP tool dual-write (mcp_server.py:_handle_record_decision)
    • `compress_turn` after the extractive summary (compressor.py)
  • [ ] `session_recall` and `session_timeline` output run through `expand()` before formatting, so the agent sees natural prose. Schema is unchanged.
  • [ ] Bench update: `scripts/bench_recall.py` reports per-row average byte length before/after compression.
  • [ ] Recall@k must not degrade more than 2 percentage points on the existing bench corpus (compression must preserve enough prose for FTS+vec to find it).

Out of scope

  • Compressing already-stored rows on the existing v3 db. Migration is a follow-up if we want it.
  • Compressing raw `tool_event_payloads.raw_input/raw_output` — those are user-payload bytes; touching them risks losing reproducibility.

References

  • cavemem compression spec: https://github.com/JuliusBrussee/cavemem/blob/main/docs/compression.md
  • cavemem lexicon: `packages/compress/src/lexicon.json` in that repo
  • Comparison that surfaced the idea: PR #7 thread

fazleelahhee avatar Apr 28 '26 09:04 fazleelahhee