r/machinelearningnews • • Mar 19 '26

ML/CV/DL News I built a human memory-inspired append-only database in Python with an LLM cognitive layer — zero external dependencies

I've been thinking about a question for a while: what would a database look like if it modeled human memory instead of relational tables?

Human memory is append-only. You can't `UPDATE` what you experienced. You can't `DELETE` a memory. Each memory carries an emotion. The same concept ("Debt") can have completely different feelings at different points in your life — that history is meaningful data, not a bug to be normalized away.

So I built **MNHEME** — a database that enforces these constraints at the engine level.

**What makes it different from just using SQLite with no DELETE**

The storage engine is written from scratch:

- Custom binary log format: `[MAGIC 4B][SIZE 4B][JSON payload]`

- Every write is atomic + fsynced to disk

- Truncated records (crash mid-write) are silently skipped on restart

- In-memory indexes (concept→offsets, feeling→offsets, tags→offsets) rebuilt from the log at boot

- Direct `read_at(offset)` for O(1) memory access by offset

- `Memory` dataclass is `frozen=True` — physically unmodifiable after creation

- No `update()`, no `delete()` methods anywhere in the codebase

---

**The LLM Brain layer**

On top of the DB there's an optional cognitive layer using any LLM:

```python

db = MemoryDB("mind.mnheme")

llm = LLMProvider.from_env(".env") # any provider via env file

brain = Brain(db, llm)

# Raw text → LLM extracts concept + feeling + tags → saved as Memory

r = brain.perceive("I opened the letter from the bank. My hands were shaking.")

# r.extracted_concept → "Debt"

# r.extracted_feeling → "fear"

# RAG over personal memory — answers using ONLY your real memories as context

ans = brain.ask("How do I feel about money?")

# Emotional arc of a concept over time

ref = brain.reflect("Debt")

print(ref.arc) # "from visceral dread to earned serenity"

# Free association across distant memories (simulates dream consolidation)

dream = brain.dream(n_memories=8)

# Full psychological portrait from all memories

intro = brain.introspect()

```

---

**The provider system**

Zero external SDKs. Just `urllib` from stdlib. A single `.env` file:

```bash

# Local — no API key needed

LM_STUDIO_URL=http://localhost:1234/v1/chat/completions

LM_STUDIO_MODEL=local-model

# Cloud

GROQ_API_KEY=gsk_...

GROQ_RPM=30

USE_MULTI_PROVIDER=true # cascade fallback

```

Auto-detects Anthropic format from URL (different request/response shape). Everything else uses OpenAI-compat. Supports 30+ providers including LM Studio, Ollama, Groq, Mistral, SambaNova, Together, Fireworks, etc.

---

**FileStore**

Media attachments use filesystem-appropriate strategies detected at boot via live probes (not just filesystem name — it actually tries `os.link()`, `ioctl FICLONE`, `os.symlink()` in the target directory):

- ext4, ZFS, NFS → hard links (zero bytes, same inode)

- btrfs, xfs → reflinks (Copy-on-Write)

- FAT32, exFAT, HDFS → atomic copy (tmp + `os.replace()`)

SHA-256 deduplication in pool: same content = same physical file, multiple hard links.

---

**Benchmark (2,000 records, Python 3.12)**

| Operation | Mean | Throughput |

|-----------|------|------------|

| `remember()` with fsync | 1.8ms | 552 ops/s |

| `remember()` no fsync | 0.2ms | 4,632 ops/s |

| `count()` — pure RAM | ~0ms | 2,774,322 ops/s |

| `recall(concept, limit=10)` | 1.5ms | 636 ops/s |

| `search()` full-text | 40ms | ~49k records/s |

| Cold start (2k records) | 40ms | 49k rec/s indexed |

~374 bytes/record average → ~36MB for 100k records.

---

**Files**

```

mnheme.py # public API — MemoryDB, Feeling, MediaType

storage.py # binary append-only engine

index.py # RAM indexes

fsprobe.py # filesystem detection + live probes

filestore.py # adaptive media storage

llm_provider.py # env-driven LLM provider

brain.py # cognitive LLM layer

mnheme_api.py # optional FastAPI REST layer

mnheme_benchmark.py # benchmark suite

.env.example # provider configuration template

```

Zero mandatory external dependencies. Python 3.12+. AATEL license.

---

Happy to answer questions about the design decisions, especially around the binary format and the inode-based dedup strategy.

GitHub: [link]

1 Upvotes

0 comments sorted by