Built-in codegraph grows unboundedly: no snapshot retention, no stale-symbol sweep, no SQLite compaction
Usage context
- Daily Memorix user since 2026-03-02 (~8 months), through MCP integrations with several AI coding agents on Windows; pnpm global install; upgraded along 1.9.x → 1.10.2.
- Current store: ~3,400 observations / 2,323 sessions across 23 projects (14 with codegraph data).
- Embeddings mode: off (BM25 full-text only); external CodeGraph: not detected — i.e. the built-in semantic layer did all the indexing.
Problem: the built-in codegraph grows without bounds
memorix.db reached 595 MB (plus an 86 MB WAL) while the actual memory content (observations + sessions) is only ~11 MB. Page-level breakdown:
| table | size | rows |
|---|---|---|
| code_symbols | 177.2 MB | 528,171 — of which 339,115 (64%) were stale = 1 |
| code_state_snapshot_files | 80.1 MB | 380,578 |
| code_edges | 16.7 MB | 67,338 |
| observations | 9.9 MB | 3,408 |
| sessions | 1.2 MB | 2,323 |
~49% of the file was free pages. Root causes (verified in source):
recordCodeStateSnapshot()(src/codegraph/store.ts) copies the full file manifest intocode_state_snapshot_fileson every refresh. 1,068 snapshots had accumulated (up to 278 for a single project) — and there is no DELETE anywhere forcode_state_snapshots/code_state_snapshot_files.- Refresh only flags old symbols
stale = 1; every read filtersstale = 0, so 64% of the symbols table was dead weight that nothing ever sweeps. src/store/sqlite-db.tssetsjournal_mode = WALbut there is noVACUUM/wal_checkpoint/auto_vacuumanywhere, so free pages accumulate.
This is the SQLite twin of the Lance index problem fixed in 1.10.2 (#365 / #369). The maintenance-runner also burns minutes of CPU re-indexing this bloat at startup, which amplifies #278 / #291-style resource pressure.
What we did (safe workaround, documented for others)
With all Memorix processes stopped, per project we kept only the latest 2 snapshots (the existing ON DELETE CASCADE on code_state_snapshot_files makes this schema-safe), deleted stale = 1 symbols, then ran VACUUM + wal_checkpoint(TRUNCATE) after a VACUUM INTO backup:
595 MB → 168 MB (−72%), WAL → ~0. memorix doctor green, retrieval unaffected.
Proposal (feature gap: no closed loop for codegraph data)
- Snapshot retention — cap
code_state_snapshotsper project (latest N or a time window) during/after refresh, or exposememorix codegraph prune [--keep N]. - Stale sweep — periodically delete
code_symbols WHERE stale = 1(reads already filterstale = 0). - Compaction — maintenance-runner: periodic
wal_checkpoint(TRUNCATE)plusVACUUM(orauto_vacuum = INCREMENTAL). - Observability — surface codegraph table sizes in
memorix doctorso growth is visible before it hurts.
Happy to test a PR build against this same workload.
Related: #365, #369, #278, #291.
Follow-up from source-reading v1.10.2: there is also no way to disable the built-in semantic layer's auto refresh. codegraph.externalContext ('auto' | 'off') only gates the external CodeGraph provider (src/codegraph/external-provider.ts), while the built-in path hardcodes refresh: 'auto' in the SessionStart hook (src/hooks/handler.ts) and MCP session_start / project_context tools (refresh: refresh ?? 'auto' in src/server.ts), and enqueueCodegraphRefresh() (src/runtime/lifecycle.ts) enqueues the scan unconditionally — no settings/env gate exists. For setups that don't benefit from the built-in code index (BM25-only, like mine), a config key such as codegraph.autoRefresh = off — mapping to decideRefresh()'s existing 'never' mode (src/codegraph/auto-context.ts) — would close the loop and stop the unbounded growth at the source.
Fixed in main via PR #376 and included in tag v1.10.3. CodeGraph now retains the latest two snapshots, deletes stale symbols and dependent edges after successful refresh, reports stale and snapshot counts in doctor, and runs WAL checkpoint/VACUUM after derived cleanup. Set codegraph.auto_refresh = never for BM25-only large stores that do not need automatic CodeGraph scans. The existing external CodeGraph setting remains separate.