@monoes/memory
v2.24.0
Published
Memory module - JSON pattern store and a SQLite + embeddings backend with a size-gated HNSW ANN fast path built in
Readme
@monoes/memory
Persistent memory backends for Monomind agents — SQLite in WAL journal mode (PRAGMA journal_mode = WAL, schema v3.0.0) key-value storage with dense ONNX ModernBERT 768d + Okapi BM25 RRF hybrid search, surface query routing, Open Knowledge Format (OKF) transfer, Cognee-style knowledge graph triplets, and EWC pattern consolidation.
Part of the Monomind ecosystem. The embedded storage layer uses
better-sqlite3withsql.js(WASM zero-compile fallback).
Install
npm install @monoes/memorybetter-sqlite3 ships as a direct dependency (native SQLite; sql.js WASM is the fallback storage engine) — no separate install needed.
Native module install blocked? If initialization throws
Could not locate the bindings file, your npm'sallowScriptspolicy blockedbetter-sqlite3's native build — runnpm install-scripts approve better-sqlite3 && npm rebuild better-sqlite3.
Core Architecture & Schema v3.0.0
Schema Architecture & Storage Engine
- Storage Driver: Operates over SQLite with WAL mode (
PRAGMA journal_mode = WAL) usingbetter-sqlite3with a WASMsql.jsfallback. - Dual Schema Support:
- Standalone
@monoes/memorycore schema (packages/@monomind/memory/src/sql-schema.ts:28):SCHEMA_VERSION = 2managing 4 tables (memory_entries,memory_embeddings,memory_entry_tags,agent_reads). - CLI project memory schema (
packages/@monomind/cli/src/memory/memory-schema.ts:15): Schema version3.0.0managing 9 tables (memory_entries,patterns,pattern_history,trajectories,trajectory_steps,migration_state,sessions,vector_indexes,metadata).
- Standalone
Hybrid Search: ONNX ModernBERT 768d + BM25 RRF
Retrieval uses a dual-arm hybrid search architecture combined with Reciprocal Rank Fusion (RRF):
- Dense Vector Search:
- Model:
Alibaba-NLP/gte-modernbert-base(768 dimensions). - Embedding Pipeline:
@xenova/transformersONNX feature extraction. - Acceleration: Standalone pure-JS
HNSWIndex(dimensions=768,M=16,efConstruction=200,metric='cosine').
- Model:
- Lexical Arm (In-Process Okapi BM25):
- Parameters: $k_1 = 1.2$, $b = 0.75$.
- Tokenizer: Exact parity with the evaluation harness (
text-tokens.ts). - Live-Only Indexing: Built dynamically over live chunks with warning thresholds at 50,000 chunks (
LIVE_CHUNK_WARN_THRESHOLD) and persistent review at 1,000,000 chunks (SCALING_REVIEW_CHUNKS).
- Reciprocal Rank Fusion (RRF):
- Fuses ranked candidate lists using adaptive constant $k = \max(30, \min(60, 20 + 2 \times \text{top_k}))$: $$\text{Score}(d) = \sum_{m} \frac{1}{\text{rrf_k} + \text{rank}_m + 1} \times (0.75 + 0.5 \times \text{importance})$$
Surface Query Router
The QueryRouter (query-router.ts) evaluates queries across 4 target surfaces: chunks (prior 0.5), kg (wt 2), rules (wt 2), and memory (wt 2):
- Negation Gate: Uses a 20-character pre-match window (
NEGATION_RE) to bypass negated query phrases. - Confidence Gate: Requires top surface score $\ge 2 \times$ runner-up score; low-confidence queries query all surfaces and fuse results.
- Telemetry Persistence: Cross-process misroutes are recorded to
.monomind/metrics/route-overrides.json.
Open Knowledge Format (OKF) Bundles
Supports document and memory export/import via Open Knowledge Format (OKF) Markdown files with YAML frontmatter headers:
- Document OKF: Exported via
exportToOKF()with frontmatter (type: Document,title,description,resource,tags,timestamp,contentHash,chunkCount) and anindex.mdmanifest. - Memory Transfer OKF: CLI commands
monomind memory exportandimportexport/restore key-value memory entries across namespace directory trees.
EWC Pattern Consolidation & SONA Router
- EWC Consolidation (
ewc-consolidation.ts): Applies Elastic Weight Consolidation using an EMA-updated Fisher information matrix (computeFisherMatrix) persisted in.swarm/ewc-fisher.jsonto prevent catastrophic forgetting during pattern updates. - SONA Router (
sona-optimizer.ts): Extracts routing patterns fromhooksTrajectoryEndevents into.swarm/sona-patterns.json.
Exported Components
| Export | What it does |
|---|---|
| SQLiteBackend / SqlJsBackend | Structured key-value storage in SQLite WAL mode (native + WASM fallback) |
| HNSWIndex | Standalone pure-JS approximate nearest-neighbor index (768d) |
| chunkDocument, KnowledgeStore, KnowledgeRetriever | Document chunking + retrieval pipeline |
| QueryBuilder / query() | Fluent query construction |
| CacheManager | In-memory LRU caching with size/TTL limits |
| SwarmCheckpointer | Persist/restore swarm agent state snapshots |
| MemoryMigrator | Import from SQLite, JSON, or Markdown sources |
| PromptVersionStore | Prompt version history & prompt experiment tracking |
Cross-Platform Notes
The memory storage engine selects the best available SQLite driver per platform:
better-sqlite3 (native, fastest) with automatic fallback to sql.js (WASM, zero compilation, works
everywhere including Windows without a toolchain).
Links
License
MIT
