π§ Cuba-Memorys
Persistent memory for AI agents β A Model Context Protocol (MCP) server that gives AI coding assistants long-term memory with a knowledge graph, Hebbian learning, GraphRAG enrichment, and anti-hallucination grounding.
12 tools with Cuban soul. Zero manual setup. Mathematically rigorous. v1.1.0 β God-Tier: RRF fusion, REM Sleep daemon, GraphRAG, conditional pgvector.
Why Cuba-Memorys?
AI agents forget everything between conversations. Cuba-Memorys solves this by giving them:
A knowledge graph β Entities, observations, and relations that persist across sessions
Error memory β Never repeat the same mistake twice (anti-repetition guard)
Hebbian learning β Memories strengthen with use and fade adaptively (FSRS spaced repetition)
Anti-hallucination β Verify claims against stored knowledge with graduated confidence scoring
Semantic search β 4-signal RRF fusion (TF-IDF + pg_trgm + full-text + optional pgvector HNSW)
GraphRAG β Top results enriched with degree-1 graph neighbors for topological context
REM Sleep β Autonomous background consolidation (FSRS decay + prune + PageRank) after 15min idle
Graph intelligence β Personalized PageRank, Louvain community detection, betweenness centrality
Feature | Cuba-Memorys | Basic Memory MCPs |
Knowledge graph with relations | β | β |
Hebbian learning (Oja's rule) | β | β |
FSRS adaptive spaced repetition | β | β |
4-signal RRF fusion search (v1.1) | β | β |
GraphRAG topological enrichment (v1.1) | β | β |
REM Sleep autonomous consolidation (v1.1) | β | β |
Conditional pgvector + HNSW (v1.1) | β | β |
Optional BGE embeddings (ONNX) | β | β |
Contradiction detection | β | β |
Graduated confidence scoring | β | β |
Personalized PageRank | β | β |
Louvain community detection | β | β |
Betweenness centrality (bridges) | β | β |
Shannon entropy (knowledge diversity) | β | β |
Chi-squared concept drift detection | β | β |
Error pattern detection + MTTR | β | β |
Entity duplicate detection (SQL similarity) | β | β |
Observation versioning (audit trail) | β | β |
Temporal validity (valid_from/valid_until) | β | β |
Write-time dedup gate | β | β |
Auto-supersede contradictions | β | β |
Full JSON export/backup (bounded) | β | β |
Fuzzy search (typo-tolerant) | β | β |
Spreading activation | β | β |
Batch observations (10x fewer calls) | β | β |
Entity type validation | β | β |
Graceful shutdown (SIGTERM/SIGINT) | β | β |
Auto-provisions its own DB | β | β |
Quick Start
1. Prerequisites
Python 3.11+
Docker (for PostgreSQL)
2. Install
git clone https://github.com/lENADRO1910/cuba-memorys.git
cd cuba-memorys
docker compose up -d
pip install -e .
# Optional: BGE embeddings for semantic search (~130MB model)
pip install -e ".[embeddings]"3. Configure your AI editor
Add to your MCP configuration:
{
"mcpServers": {
"cuba-memorys": {
"command": "/path/to/cuba_memorys_launcher.sh",
"disabled": false
}
}
}Or run directly:
DATABASE_URL="postgresql://cuba:memorys2026@127.0.0.1:5488/brain" python -m cuba_memorysThe server auto-creates the brain database and all tables on first run.
π¨πΊ Las 12 Herramientas
Every tool is named after Cuban culture β memorable, professional, and meaningful.
Knowledge Graph
Tool | Meaning | Description |
| Alma β soul, essence | CRUD knowledge entities. Types: |
| CrΓ³nica β chronicle | Attach observations to entities with contradiction detection and dedup gate. Supports |
| Puente β bridge | Connect entities with typed relations ( |
Search & Verification
Tool | Meaning | Description |
| Faro β lighthouse | Search with 4-signal RRF fusion (TF-IDF + full-text + trigrams + pgvector). verify mode for anti-hallucination (returns |
Error Memory
Tool | Meaning | Description |
| Alarma β alarm | Report errors immediately. Auto-detects patterns (β₯3 similar = warning). Hebbian boosting for retrieval. |
| Remedio β remedy | Mark an error as resolved. Cross-references similar unresolved errors. |
| Expediente β case file | Search past errors/solutions. Anti-repetition guard: warns if a similar approach previously failed. |
Sessions & Decisions
Tool | Meaning | Description |
| Jornada β workday | Track working sessions with goals and outcomes. |
| Decreto β decree | Record architecture decisions with context, alternatives, and rationale. |
Memory Maintenance
Tool | Meaning | Description |
| Zafra β sugar harvest π― | Memory consolidation: decay (FSRS adaptive), prune, merge, summarize, pagerank (personalized), find_duplicates (SQL similarity), export (bounded JSON backup), stats. |
| Eco β echo | RLHF feedback: positive (Oja's rule boost), negative (decrease), correct (update with versioning). |
| VigΓa β watchman | Graph analytics: summary (counts + token estimate), health (staleness, Shannon entropy, DB size), drift (chi-squared), communities (Louvain), bridges (betweenness centrality). |
Mathematical Foundations
Cuba-Memorys is built on peer-reviewed algorithms, not ad-hoc heuristics:
FSRS Adaptive Decay β Wozniak (1987) / Ye (2022)
stability = fsrs_stability Β· (1 + decay_factor)^rating
R(t) = (1 + t/(9Β·S))^(-1)FSRS (Free Spaced Repetition Scheduler) provides adaptive memory decay. Stability grows with successful reviews β memories that are reinforced survive longer.
Oja's Rule (1982) β Hebbian Learning
Positive: Ξw = Ξ· Β· (1 - wΒ²) β converges to 1.0, cannot explode
Negative: Ξw = Ξ· Β· (1 + wΒ²) β converges to 0.01 (floor)Where Ξ· = 0.05. The wΒ² term provides natural saturation β self-normalizing without explicit clipping.
TF-IDF + RRF Fusion β Salton (1975) / Cormack (2009)
tfidf(t, d) = tf(t, d) Β· log(N / df(t))
RRF(d) = Ξ£ 1/(k + rank_i(d)) where k = 60Reciprocal Rank Fusion combines multiple ranked lists from independent signals into a single robust ranking. In v1.1.0, up to 4 signals are fused: entities (full-text + trigrams), observations (full-text + trigrams + TF-IDF), errors (full-text + trigrams), and optional pgvector (cosine distance on embeddings).
Optional BGE Embeddings β BAAI (2023)
model: Qdrant/bge-small-en-v1.5-onnx-Q (quantized, ~130MB)
runtime: ONNX (no PyTorch dependency)
similarity: cosine(embed(query), embed(observation))Auto-downloads on first use. Falls back to TF-IDF if not installed.
GraphRAG Enrichment β v1.1.0
Top-3 search results are enriched with degree-1 graph neighbors via a single batched SQL query. Each result gets a graph_context array containing neighbor name, entity type, relation type, and Hebbian strength. Provides topological context without N+1 queries.
REM Sleep Daemon β v1.1.0
After 15 minutes of user inactivity, an autonomous consolidation coroutine runs:
FSRS Decay β Applies memory decay using Ye (2022) algorithm
Prune β Removes low-importance (< 0.1), rarely-accessed observations
PageRank β Recalculates personalized importance scores
Cancels immediately on new user activity. Prevents concurrent runs.
Conditional pgvector β v1.1.0
IF pgvector extension detected:
β Migrate embedding column: float4[] β vector(384)
β Create HNSW index (m=16, ef_construction=64, vector_cosine_ops)
β Add vector cosine distance as 4th RRF signal
β Persist embeddings on observation insert
ELSE:
β Graceful degradation: TF-IDF + trigrams (unchanged)Zero-downtime: auto-detects at startup, no configuration needed.
Personalized PageRank β Brin & Page (1998)
PR(v) = (1-Ξ±)/N + Ξ± Β· Ξ£ PR(u)/deg(u) where Ξ± = 0.85
personalization: biased toward recently active entities
final_importance = 0.6Β·PR + 0.4Β·current_importanceGraph-structural importance via networkx. Personalized bias prevents cold-start domination.
Shannon Entropy β Knowledge Diversity
H = -Ξ£ pΒ·logβ(p)
diversity_score = H / H_maxMeasures how evenly distributed your knowledge is across entity/observation types. Score 0β1.
Spreading Activation β Collins & Loftus (1975)
When entity X is accessed, its graph neighbors receive a small importance boost (0.6% per hop, decaying 30% per level).
Chi-Squared Concept Drift β Pearson (1900)
ΟΒ² = Ξ£ (observed - expected)Β² / expected
p-value via scipy.stats.chi2.sf()Detects when the distribution of error types changes significantly β signals that something new is breaking.
Contradiction Detection
conflict = tfidf_similarity(new, existing) > 0.7 AND has_negation(new, existing)Checks for semantic overlap + negation patterns (not, never, instead of, replaced, deprecated) in both English and Spanish.
Architecture
cuba-memorys/
βββ docker-compose.yml # Dedicated PostgreSQL (port 5488)
βββ pyproject.toml # Package metadata + optional deps
βββ README.md
βββ src/cuba_memorys/
βββ __init__.py
βββ __main__.py # Entry point
βββ server.py # 12 MCP tools + REM Sleep daemon (~1850 LOC)
βββ db.py # asyncpg pool + orjson + pgvector detection + Decimal
βββ schema.sql # 5 tables, 15+ indexes, pg_trgm, versioning
βββ hebbian.py # FSRS, Oja's rule, spreading activation
βββ search.py # LRU cache, RRF fusion, NEIGHBORS_SQL, SEARCH_VECTOR_SQL
βββ tfidf.py # TF-IDF semantic search (scikit-learn)
βββ embeddings.py # Optional BGE embeddings (ONNX Runtime)Database Schema
Table | Purpose | Key Features |
| Knowledge graph nodes | tsvector + pg_trgm indexes, importance β [0,1], FSRS stability/difficulty |
| Facts attached to entities | 9 types, provenance, versioning, temporal validity, |
| Graph edges | 5 types, bidirectional delete, Hebbian strength |
| Error memory | JSONB context, synapse weight, MTTR tracking |
| Working sessions | Goals (JSONB), outcome tracking |
Search Pipeline (v1.1.0)
Cuba-Memorys uses Reciprocal Rank Fusion (RRF, k=60) to combine up to 4 independent ranked signals:
# | Signal | Source | Condition |
1 | Entities (ts_rank + trigrams + importance + freshness) |
| Always |
2 | Observations (ts_rank + trigrams + TF-IDF + importance) |
| Always |
3 | Errors (ts_rank + trigrams + synapse_weight) |
| Always |
4 | Vector cosine distance (HNSW) |
| pgvector installed |
Each signal produces an independent ranking. RRF fuses them: score(d) = Ξ£ 1/(60 + rank_i(d)).
Post-fusion enrichment:
Top-3 results receive GraphRAG context (degree-1 neighbors)
Active session goals boost matching results by 15%
Dependencies
Core:
asyncpgβ PostgreSQL async driverorjsonβ Fast JSON serialization (handles UUID/datetime)scikit-learnβ TF-IDF vectorizationnetworkxβ PageRank + Louvain + betweenness centralityscipyβ Chi-squared statistical testsrapidfuzzβ Entity duplicate detectionnumpyβ Numerical operations
Optional (pip install -e ".[embeddings]"):
onnxruntimeβ ONNX model inferencehuggingface-hubβ Auto-download BGE modeltokenizersβ Fast tokenization
Configuration
Environment Variables
Variable | Default | Description |
| β | PostgreSQL connection string (required) |
Docker Compose
Runs a dedicated PostgreSQL 18 Alpine instance:
Port: 5488 (avoids conflicts with 5432/5433)
Resources: 256MB RAM, 0.5 CPU
Restart: always (auto-starts on boot)
Healthcheck:
pg_isreadyevery 10s
How It Works in Practice
1. The agent learns from your project
Agent: I learned that FastAPI endpoints must use async def with response_model.
β cuba_alma(create, "FastAPI", technology)
β cuba_cronica(add, "FastAPI", "All endpoints must be async def with response_model")2. Error memory prevents repeated mistakes
Agent: I got IntegrityError: duplicate key on numero_parte.
β cuba_alarma("IntegrityError", "duplicate key on numero_parte")
β Similar error found! Solution: "Add SELECT EXISTS before INSERT with FOR UPDATE"3. Anti-hallucination grounding
Agent: Let me verify this claim before responding...
β cuba_faro("FastAPI uses Django ORM", mode="verify")
β confidence: 0.0, level: "unknown"
β recommendation: "No supporting evidence found. High hallucination risk."4. Memories adapt with FSRS
Initial stability: S = 1.0 (decays in ~9 days)
After 5 reviews: S = 8.2 (decays in ~74 days)
After 20 reviews: S = 45.0 (survives ~13 months)5. Contradiction detection
Existing: "Project uses PostgreSQL 15"
New: "Project does NOT use PostgreSQL, it uses MySQL instead"
β β οΈ CONFLICT detected (similarity=0.82, negation pattern: "does NOT")6. Graph intelligence
β cuba_zafra(action="pagerank")
β Top entities: FastAPI (0.42), SQLAlchemy (0.38), PostgreSQL (0.35)
β cuba_vigia(metric="communities")
β Community 1: [FastAPI, Pydantic, SQLAlchemy] β Backend cluster
β Community 2: [React, Next.js, TypeScript] β Frontend clusterVerification
Tested with NEMESIS protocol (3-tier) β v1.1.0 results:
π’ Normal (4/4) β RRF fusion (rrf_score on all results), GraphRAG (graph_context with
neighbors), scope=observations with grounding, verify mode (0.7093)
π‘ Pessimist (4/4) β Empty queries, whitespace, dedup gate (similarity=1.0),
graph traversal depth=2 with strength decay
π΄ Extreme (5/5) β SQL injection, XSS, path traversal, min-length content,
transitive inference depth=5Previous v1.0.1 tests (18/18) also verified: Entity CRUD, observe, search, relate, PageRank, communities, decay, find_duplicates, export, health, entropy, Unicode, bidirectional delete.
Performance
Operation | Avg latency |
RRF hybrid search | < 5ms |
Analytics | < 2.5ms |
Entity CRUD | < 1ms |
PageRank (100 entities) | < 50ms |
GraphRAG enrichment | < 2ms |
License
CC BY-NC 4.0 β Free to use and modify, not for commercial use.
Author
Leandro PΓ©rez G.
GitHub: @lENADRO1910
Email: leandropatodo@gmail.com
Credits
Mathematical foundations: Wozniak (1987), Ye (2022, FSRS), Oja (1982), Salton (1975, TF-IDF), Cormack (2009, RRF), Brin & Page (1998, PageRank), Collins & Loftus (1975), Shannon (1948), Pearson (1900, ΟΒ²), Blondel et al. (2008, Louvain), BAAI (2023, BGE embeddings), Malkov & Yashunin (2018, HNSW).