superlocalmemory icon indicating copy to clipboard operation
superlocalmemory copied to clipboard

CozoGraphBackend.remove_fact() scans every edge (OR on to_id): projection drain slows down with graph size

Open raorthopro-cpu opened this issue 1 month ago • 2 comments

Environment: superlocalmemory 4.1.24 (same code in 4.2.10), pycozo 0.3.0 / cozo_embedded 0.3.0, lancedb 0.30.2, Python 3.14, macOS, Mode B, Scale Engine promoted (Cozo + LanceDB).

Problem: remove_fact() in graph/cozo_backend.py deletes a fact's edges with

?[from_id, to_id, edge_type, weight, metadata, profile_id, created_at] :=
    *edge{from_id, to_id, edge_type, weight, metadata, profile_id, created_at},
    (from_id = $fact_id or to_id = $fact_id)
:rm edge {from_id, to_id, edge_type => weight, metadata, profile_id, created_at}

to_id is not a key prefix of edge, so every call scans the whole relation. ProjectionDrain._project_graph() calls it for every queued fact, so draining costs (queued facts × total edges).

Measured (same query, read-only, on a copy of the store):

  • 4,303,626 edges: 3.98 s per fact
  • 2,914,125 edges: 2.80 s per fact

A backlog of ~22,900 outbox rows therefore needs about 18–25 hours of continuous CPU.

Suggested fix: split the query into one rule bound on the key prefix (from_id = $fact_id) and one served by a reverse relation or an index on to_id. With cozo_embedded 0.3.0, ::index create edge:by_to {to_id, from_id, edge_type} failed with "The query parser has encountered unexpected input / end of input", so a maintained reverse relation may be needed.

Related observations:

  1. The store was auto-promoted on 2026-08-30 while it was empty (stage manifest: 0 edges) and never rebuilt. Cozo had grown to 4.3 M edges while canonical SQLite had 2.9 M. A fresh slm db scale prepare / verify / promote fixed the count.
  2. Running slm db scale promote while the daemon was running corrupted the new Cozo store after the daemon restart (rocksdb::kCorruption, missing SST files). Repeating it with the daemon stopped worked. Suggestion: refuse promote/rollback while the daemon is alive, as slm db compact --offline already does.

raorthopro-cpu avatar Oct 19 '26 21:10 raorthopro-cpu

Thank you, your measurements were exactly right. We confirmed both points on pycozo 0.3.0 / cozo_embedded 0.3.0.

remove_fact() scans every edge. We benchmarked the current query on synthetic stores. Time per fact grows linearly with the edge count: 26 ms at 50k edges, 106 ms at 200k, 428 ms at 800k. Splitting off from_id alone is not enough, because filtering after the *edge{...} atom still scans. Binding from_id = $fact_id before the atom gives a prefix seek, and a maintained reverse relation edge_rev {to_id, from_id, edge_type} covers to_id. Together they took the 800k case from 428 ms to about 0.1–0.2 ms per fact. We also confirmed that ::index create edge:by_to {...} fails to parse on cozo_embedded 0.3.0, so the reverse relation is the route.

slm db scale promote / rollback with the daemon running. Confirmed: neither command checks whether the daemon is alive, unlike slm db compact --offline. The daemon holds the Cozo/RocksDB store open, so promoting underneath it can corrupt the new store, as you saw. Both commands will refuse while the daemon is running.

Both fixes will ship in the next version upgrade. Your note about the store auto-promoting while empty and never being rebuilt is noted too. We will look at whether promote should refuse an empty stage. Until then, the prepare / verify / promote with the daemon stopped that you used is the right recovery.

varun369 avatar Oct 20 '26 18:10 varun369

Fixed in 4.2.11, out now on PyPI and npm: pip install -U superlocalmemory, then slm restart.

  • remove_fact() no longer scans every edge. A reverse-edge mirror (edge_rev) is kept in the same transaction, and ids are bound before the atom. At 800k edges, removal dropped from 428 ms to about 0.1 ms per fact. Existing stores are backfilled once when the store opens.
  • The drift had two sources, and both are fixed. The drain was re-adding edges to archived or quarantined facts, and archive, merge and quarantine never queued a projection (a new trigger, M067, handles that).
  • Stores that have already drifted heal automatically. The daemon compares the copy with the store on every maintenance cycle and rebuilds a profile that differs. slm db regraph (and --check) now works while the daemon is running.
  • slm db scale promote and rollback refuse to run while the daemon is up.

On a real 379k-edge store, the copy went from about 2,000 edges of drift to within a few dozen, and the maintenance check keeps it in step. We're still tracking down one small remaining source of drift and will follow up here. Thanks for the clear report.

varun369 avatar Oct 22 '26 13:10 varun369