Repository navigation
Maintenance and Optimization
Tip
Value Proposition: Memory Journal is built for the long haul. Its autonomous pruning and heuristic scoring keep your project's context window lean, performant, and relevant over months of development, preventing token bloat without losing critical history.
The memory journal is designed to operate continuously for months or years without failure. To prevent unbounded disk growth and context window saturation, the journal features a sophisticated maintenance architecture powered by heuristic scoring, an autonomous pruning engine, and interactive optimization workflows.
Entries in the journal are assigned heuristic scores by agents to determine their value, confidence, and blast radius. These scores dictate how the entries are filtered, retrieved, and eventually pruned.
| Metric | Range | Description |
|---|---|---|
impact_score |
0.0 - 1.0 |
Blast Radius. Measures the systemic impact of an entry. For example, 0.1 represents a minor typo fix or simple documentation update, while 0.9 represents a core architectural shift or critical security patch. |
trust_score |
0.0 - 1.0 |
Confidence. Measures how rigorously an entry has been verified. Unverified drafts or assumed implementations sit at a baseline of 0.3 - 0.5. Fully verified code (e.g. passing test suites, peer review) scores 0.8 - 1.0. Failed validations or hallucinations drop to 0.0 - 0.2. |
In addition to decimal scores, entries carry explicit categorical statuses:
-
validation_status: Marks the state of the entry's claims (passed,none, orfailed). -
significance_type: Distinct structural markers used to categorize highly critical entries (milestone,breakthrough,decision,architecture,lesson_learned,blocker_resolved,release,security).
To maintain high performance, the journal uses an automated garbage collection system.
The SQLite adapter (src/database/sqlite-adapter/entries/importance.ts) dynamically computes a live importance score for each entry using four weighted base components:
-
Significance / Impact (
30%):Math.max(impact_score, significance_type ? 0.5 : 0.0) * 0.30. -
Relationships (
35%):Math.min(rel_count / 5, 1.0) * 0.35, reaching its maximum at 5 linked entries. -
Causal Relationships (
20%):Math.min(causal_count / 3, 1.0) * 0.20forblocked_by,resolved, andcausedlinks, reaching its maximum at 3 causal links. -
Recency (
15%):Math.max(0, 1 - daysSince / 90) * 0.15, which provides up to a+0.15boost for newly created entries and decays linearly over a 90-day window.
The sum of these four weighted components (baseScore) is then multiplied by anchorMultiplier * trustMultiplier:
-
anchorMultiplier:0.1if any linked code anchor isinvalidated,0.8ifstale, or1.0otherwise. -
trustMultiplier:0.5 + trust_score + validationMultiplier(wherevalidation_status: "passed"adds+0.5and"failed"subtracts-0.4).
The run_auto_prune administrative tool scans the database for entries that have decayed beneath acceptable importance thresholds.
-
MCP Tool Parameters & Return Fields (
run_auto_prune): Acceptsdry_run(boolean),archive_older_than_days(number),prune_older_than_days(number),hard_delete_older_than_days(number), andimportance_threshold(number). When omitted, thresholds fall back to the server's configured lifecycle options (archiveOlderThanDays,pruneOlderThanDays,hardDeleteOlderThanDays,pruneImportanceThreshold), or default to0(disabled) for age thresholds and0.15forimportance_threshold. Returns{ success: boolean, archivedCount: number, prunedCount: number, hardDeletedCount: number, backupFile?: string, dryRun: boolean }(whendry_runisfalse,runAutoPruneautomatically creates apre-prune-backup-<timestamp>.dbsnapshot and retains the 5 most recent backups viadeleteOldBackups(5)). -
Scheduling & CLI Flags: Startup auto-pruning runs automatically on server startup in both
stdioandhttpmodes whenever any*-older-than-days > 0(--archive-older-than-days,--prune-older-than-days, or--hard-delete-older-than-days) is configured, whereas recurring scheduled pruning via--auto-prune-interval(AUTO_PRUNE_INTERVAL_MINUTES) runs in HTTP transport mode only. Pruning can also be invoked on-demand viamemory-journal-mcp admin prune [--dry-run] [--exclude-tags <tags>]. Additional CLI/startup pruning flags include--prune-importance-threshold,--prune-dry-run, and--prune-exclude-tags(PRUNE_EXCLUDE_TAGS/ CLI--exclude-tags). -
Safety Locks: Startup/scheduled pruning and the CLI
admin prunecommand support tag-based exclusion (--prune-exclude-tags/PRUNE_EXCLUDE_TAGS/--exclude-tags), whereas therun_auto_pruneMCP tool filters by age andimportance_threshold(protecting entries scoring at or aboveimportance_threshold, including entries with a definedsignificance_typeor sufficientimpact_scoreunder default trust/threshold settings, as well asexclude_tagswhen configured via startup/CLI pruning). In the interactiveoptimize-journalworkflow,db.getOrphanCount('*')counts unlinked entries whose computed importance score is strictly below0.2, naturally excluding entries with a definedsignificance_typeor highimpact_score/trust_scorefrom low-importance orphan candidates.
Agents are instructed to generate Session Summaries (entry_type: "retrospective") at the conclusion of work bursts using the session-summary prompt. The prompt structures summaries around ## Accomplished, ## Unfinished / Blocked, and ## Context for Next Session headers (plus a ## Commits section when validated via the direct MCP tools CreateEntrySchema / TeamCreateEntrySchema), alongside the auto_context.commits payload to securely bind session activity to the knowledge graph.
When retrieving historical context to construct these retrospectives, agents execute get_recent_entries utilizing the sort_by: "importance" parameter. Because of the heuristic scoring rules, this guarantees that critical architectural context (impact_score: 0.9) and successfully verified implementations (validation_status: "passed") surface to the absolute top of the context window, bypassing trivial or unverified scratchpad notes.
For exhaustive database maintenance, the server provides the /mcp:memory-journal-mcp:optimize-journal workflow prompt (src/handlers/prompts/optimizer.ts). It accepts six optional arguments:
-
mode:'quick' | 'targeted' | 'full'(default:'quick') -
targetDimension:'unassigned' | 'relationships' | 'scores' | 'tags' | 'all'(default:'all') -
project: Target project name or number (omit to optimize all projects globally) -
limit: Entry processing cap (0= unlimited across all history, or1β50000, default:500or0infull/autoRunmode) -
lookback: Number of recent unverified entries to inspect (1β2000, default:10) -
autoRun:"true" | "false"(default:"false") β execute all phases without interactive stopping
The workflow is structured into three progressive options:
-
Option A (Quick Health & Priority Workspace Unassigned Audit β Executed Automatically): Creates a pre-flight safety backup (
backup_journal+cleanup_backups({ keep_count: 15 })) and scans all workspaces (workspace_id: '*') to immediately migrate any unassigned or'default'entries to their registered project, accompanied by a root-cause investigation. -
Option B (Ground-Truth Verification, Staleness Sweep & Targeted Quality Audit β Executed Automatically): Cross-checks recent entries against Git/GitHub ground truth, runs
mj.git.checkEntryStaleness({ entry_id: id, workspace_id: '*' })across sampled entries, updates stale code anchors viamj.core.markAnchorsStale, and audits the selectedtargetDimension(unassigned,relationships,scores,tags, orall). -
Option C (Comprehensive Multi-Agent Domain Audit, Episodic Compaction & Noise Pruning β Executed Automatically in
full/autoRunMode): Partitions entries by project domain and performs 5-dimension single-pass optimization alongside CMS-02 NREM Episodic-to-Semantic Compaction (when>10granulartest_entryor incrementaldevelopment_noteentries older than14 daysexist for the same feature/session, synthesizing a semanticretrospectiveorproject_decisionentry linked viaevolves_fromand archiving the raw fragments withis_archived: true). Operations are logged withentry_type: 'maintenance'and tags['cms', 'cms-02-nrem-consolidation', 'database-optimizer', 'maintenance'].
Complementing optimize-journal, the /mcp:memory-journal-mcp:cms-rem-supervisor prompt (src/handlers/prompts/cms-supervisor.ts) runs the REM Meta-Cognitive Supervisor & Self-Adaptation workflow (CMS-04) with optional arguments lookbackHours (1β168, default 24) and autoRestore ("true" | "false", default "true"):
-
Phase A (Post-NREM Supervisor Audit & Safety Rollback): Audits recent
CMS-02maintenance runs and soft-deleted entries, automatically restoring (mj.admin.restoreEntry) any high-value entry (significanceType !== null,impactScore >= 0.5, or active relationships) that was erroneously soft-deleted, and verifies zero unassigned entries and vector index parity. -
Phase B (Cross-Run Experience & Telemetry Mining): Mines
logs/agent-issues.jsonl,logs/mcp-audit.jsonl, and recent## Unfinished / Blockedsections to formulate targeted schema/healer/prompt adaptations. -
Phase C (Batched Adaptation & Dirty-Tree Conditional Single-Pass Validation Gate): Applies approved fixes and runs the 5-stage validation gate (
lint,typecheck,build,vitest,playwright) strictly once only if working tree files were modified. -
Phase D (Telemetry Cleanup & Supervisor Journal Entry): Logs the completed supervisor run with tags
['cms', 'cms-04-rem-supervisor', 'meta-learning', 'maintenance'].
-
Prepared Statement LRU Cache:
NativeConnectionManagermaintains a 256-entry FIFO/LRU prepared statement cache (prepare()), avoiding repeated SQL compilation on hot paths (createEntry,searchEntries,getRecentEntries,calculateImportance). -
PRAGMA Tuning: Concurrency, referential integrity, and read/write throughput are optimized via
journal_mode = WAL,synchronous = NORMAL,foreign_keys = ON,cache_size = -64000(64MB),temp_store = MEMORY, andbusy_timeout = 5000. -
Virtual Table Query Planner Pinning: FTS5 (
fts_content) queries useCROSS JOIN memory_journalto pin virtual table index driving, whilesqlite-vec(vec_embeddings) queries drive from thevec0table first usingINNER JOIN memory_journalwithk = limit * 4over-fetching to filter out soft-deleted entries. -
Single-Transaction Mutations & Batched Importance: Write operations execute inside a single
better-sqlite3synchronous transaction, and search/list hydration computes importance scores in a single batched pass (attachImportanceScoresToEntries/getEntriesByIdsWithImportance).
Instead of repeatedly calling search_entries over the top-level MCP tool transport, the workflow instructs the AI to utilize Code Mode (mj_execute_code). The JavaScript snippet executes inside an isolated worker_threads + vm.createContext sandbox and communicates with the host via an allowlisted MessageChannel RPC bridge (mj.* API). This keeps all intermediate loop data and filtering logic inside the sandbox so only the final compacted summary is returned to the LLM context window.
The optimizer tracks global orphan count and density via db.getOrphanCount('*') (entries with 0 active relationships and computed importance < 0.2). In Option B and Option C, orphan entries are linked to their parent specifications, commits, or session summaries via mj.relationships.linkEntries, while zero-value transient noise (significanceType === null and unanchored) is safely soft-deleted via mj.admin.deleteEntry or mj.admin.batchDeleteEntries.
Tip
Value Proposition: Deploy a powerful, persistent external memory system for AI agents. Engineered to preserve deep architectural context, eliminate hallucinations, and effortlessly synchronize multi-agent workflows at scale.
- Quick Start
- Installation
- Configuration
- CLI Reference
- HTTP Transport
- Tool Filtering
- Code Mode
- Tools Reference
- Performance
- Capacity Planning