memorix icon indicating copy to clipboard operation
memorix copied to clipboard

Built-in codegraph grows unboundedly: no snapshot retention, no stale-symbol sweep, no SQLite compaction

Open ruan-cat opened this issue 1 month ago • 2 comments

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):

  1. recordCodeStateSnapshot() (src/codegraph/store.ts) copies the full file manifest into code_state_snapshot_files on every refresh. 1,068 snapshots had accumulated (up to 278 for a single project) — and there is no DELETE anywhere for code_state_snapshots / code_state_snapshot_files.
  2. Refresh only flags old symbols stale = 1; every read filters stale = 0, so 64% of the symbols table was dead weight that nothing ever sweeps.
  3. src/store/sqlite-db.ts sets journal_mode = WAL but there is no VACUUM / wal_checkpoint / auto_vacuum anywhere, 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)

  1. Snapshot retention — cap code_state_snapshots per project (latest N or a time window) during/after refresh, or expose memorix codegraph prune [--keep N].
  2. Stale sweep — periodically delete code_symbols WHERE stale = 1 (reads already filter stale = 0).
  3. Compaction — maintenance-runner: periodic wal_checkpoint(TRUNCATE) plus VACUUM (or auto_vacuum = INCREMENTAL).
  4. Observability — surface codegraph table sizes in memorix doctor so growth is visible before it hurts.

Happy to test a PR build against this same workload.

Related: #365, #369, #278, #291.

ruan-cat avatar Nov 01 '26 03:11 ruan-cat

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.

ruan-cat avatar Nov 01 '26 03:11 ruan-cat

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.

AVIDS2 avatar Nov 05 '26 05:11 AVIDS2