Engineering

From Idea to Algorithm: The Science Behind ZenBrain

Alexander Bering
Alexander Bering
March 27, 2026 · 6 min read

Updated on 31 October 2026. Module and test counts brought to v0.6.1; the description of the layers remains checked against the code of v0.5.1.

Added on 11 October 2026. Corrected against the code of v0.5.1: layers 6 and 7 (Core Memory and Cross-Context, not "Prospective" and "Long-Term"), which mechanisms the MemoryCoordinator uses itself and which are functions in @zensation/algorithms, module and test counts, and the sentence that none of three systems ships sleep consolidation; Letta Code does it as "dreaming".

The Problem with the Status Quo

A vector store is not memory.

The difference? A vector store is a very good index. You store text, convert it to numbers, and find similar text later. That works — for simple use cases.

But imagine your brain were a vector store. You could recall facts that sound similar to your question. But you wouldn't know when you learned them. You couldn't forget. You couldn't form connections between memories that seem unrelated on the surface. You couldn't sleep and consolidate what you've learned.

In short: you'd be a very good search engine. But not a thinking being.

What Neuroscience Teaches Us

Human memory isn't a single system. It's an ensemble of specialized subsystems working together. The key findings that shaped our architecture:

Endel Tulving (1972): The distinction between episodic memory (concrete experiences with time and place) and semantic memory (abstract knowledge). Your brain stores "I was in a meeting with Maria yesterday" differently from "Meetings should have an agenda."

Hermann Ebbinghaus (1885): The forgetting curve. Memories fade exponentially — but each review makes them more stable. This principle is the foundation of spaced repetition.

Stickgold & Walker (2013): Sleep consolidation. While you sleep, your brain replays the day's experiences and decides what gets transferred to long-term memory. Important connections are strengthened, irrelevant ones weakened.

Donald Hebb (1949): "Neurons that fire together, wire together." When two concepts are regularly activated together, their connection strengthens. The foundation of associative learning.

From Papers to Code

We translated these findings into nine foundational algorithms, joined by six Predictive Memory Architecture (PMA) components that govern memory dynamics. The algorithm core is published as @zensation/algorithms on npm, a zero-dependency library of twenty modules:

Ebbinghaus Decay

Every memory has a stability value S (in days) that determines how quickly it fades. The core formula is simple:

Retention = e^(-t / S)

Each successful recall boosts stability, more so when stability is low; each failure drops it to a tenth, but not below half a day. Emotional memories decay up to 3x slower via a decay multiplier. That is how the module in @zensation/algorithms computes it. In the MemoryCoordinator only working memory decays; nothing in long-term memory decays or is deleted on its own, and forget(id, layer) deletes on request.

Hebbian Dynamics

The Hebbian functions in @zensation/algorithms strengthen an edge when two concepts such as "React" and "Performance" appear together. The more often they appear together, the stronger the association.

The same functions normalize connection strengths so no single concept dominates all others, and let unused connections decay. You keep the knowledge graph yourself; @zensation/core has none.

FSRS (Free Spaced Repetition Scheduler)

Based on the SuperMemo algorithm, but modernized. FSRS plans when you should review knowledge to optimally anchor it in long-term memory. Each rating (1-5) adjusts the intervals. ZenBrain’s scheduler is FSRS-style: it follows the form of FSRS’s updates, not FSRS’s fitted parameters.

Bayesian Confidence

Not all information is equally certain. The Bayesian functions in @zensation/algorithms update a statement's confidence when you pass new evidence through a knowledge graph you keep: support raises it, contradiction lowers it. The intervals module adds uncertainty bounds that widen with every propagation step. The MemoryCoordinator uses neither.

The Seven Layers

ZenBrain splits memory into seven layers; of the algorithms above, the MemoryCoordinator uses FSRS-style scheduling, plus emotional tagging:

LayerFunctionInspiration
Working MemoryActive focus (current task)Baddeley & Hitch (1974)
Short-Term MemorySession context (last minutes)George Miller (1956)
Episodic MemoryConcrete experiences with contextTulving (1972)
Long-Term Semantic MemoryFacts and concepts, with an FSRS-style scheduleTulving (1972)
Procedural MemoryProcesses and workflowsSquire (1992)
Core MemoryPinned core facts, in every recallMemGPT (Packer et al., 2023)
Cross-Context MemoryEntities across contexts (a building block, not part of recall)–

The MemoryCoordinator picks the layer when you store, searches up to five layers at once when you recall, and turns emotionally weighted episodes into facts when you call consolidate().

Sleep consolidation in the library

Idle-time consolidation is pursued elsewhere too, for example as “dreaming” in Letta Code. In ZenBrain, sleep consolidation consists of three functions in @zensation/algorithms that you call yourself:

  1. selectForReplay() picks memories by access frequency, emotional weight and recency
  2. simulateReplay() raises their stability and strengthens Hebbian edges
  3. pruneWeakConnections() separates weak edges

consolidate() in the MemoryCoordinator does not call them. In the paper's simulation the sleep loop raised stability by 37 %; the only run on real data showed a loss in retrieval precision (correction notice of 18 September 2026).

Open Source, Zero Dependencies

The foundational algorithms are open source as @zensation/algorithms (Apache 2.0). 20 algorithm modules (10 core, 10 advanced), 539 tests in the package and 948 across all packages (both as of v0.6.1), zero external dependencies. TypeScript-native. The 6 PMA components are described in the paper and are not part of the open-source packages.

Why zero dependencies? Because a memory system is the foundation of your application. It should be stable, auditable, and free from supply chain risks.

npm install @zensation/algorithms @zensation/core

What's Next

Both things we were working on then have shipped since: generateRetentionCurve in @zensation/algorithms returns the data points of an illustrative forgetting curve for a chart, generateScheduleTimeline those of an FSRS-style schedule, and layer 7 (Cross-Context) in @zensation/core is an experimental building block that proposes similarly named entities for merging; it is not yet part of recall.

Neuroscience has much more to offer. We're translating it — one algorithm at a time.

For a deep dive into the technical architecture, read our post on the 7-Layer Memory Architecture. In the next post, we discuss a values decision: why we choose self-hosting over the cloud.