Skip to content

Memory Cortex

github-actions[bot] edited this page Jun 16, 2026 · 6 revisions

🧠 Cortex β€” Tier Stores

Biological Analog: The Cerebral Cortex β€” the outer layer of the brain responsible for higher-order cognitive functions. Different cortical regions specialize in different types of memory.


The 4-Tier Architecture

Human memory is not a single system. Cognitive science identifies distinct memory systems with different characteristics, durations, and purposes. Spector mirrors this with four tier stores:

graph TB
    subgraph "TierRouter β€” Polymorphic Registry"
        direction TB
        TR["TierStore interface"]
    end
    
    TR --> WM["πŸ§ͺ Working Memory<br/>WorkingMemoryStore<br/>━━━━━━━━━━━━━━━━━<br/>Prefrontal Cortex<br/>Volatile circular buffer<br/>~100 records"]
    TR --> EM["πŸ“ Episodic Memory<br/>EpisodicMemoryStore<br/>━━━━━━━━━━━━━━━━━<br/>Hippocampus<br/>Time-partitioned mmap<br/>Unbounded"]
    TR --> SE["🧬 Semantic Memory<br/>SemanticMemoryStore<br/>━━━━━━━━━━━━━━━━━<br/>Neocortex<br/>Permanent knowledge<br/>~5,000 records"]
    TR --> PR["βš™οΈ Procedural Memory<br/>ProceduralMemoryStore<br/>━━━━━━━━━━━━━━━━━<br/>Basal Ganglia<br/>Learned procedures<br/>~500 records"]
    
    style WM fill:#e74c3c,color:white
    style EM fill:#3498db,color:white
    style SE fill:#2ecc71,color:white
    style PR fill:#9b59b6,color:white
Loading

Polymorphic Tier Routing

All four stores implement a common TierStore interface. The TierRouter dispatches operations via an EnumMap<MemoryType, TierStore> β€” zero switch statements, fully polymorphic.

Adding a new tier (e.g., FLASH for ultra-fast scratch memory) requires only: (1) implement TierStore, (2) register in TierRouter. No changes needed in SpectorMemory, RecallPipeline, or CognitiveIngestionTarget.


πŸ§ͺ Working Memory (Prefrontal Cortex)

Biological Analog: The Prefrontal Cortex maintains a limited workspace for active processing. It holds ~7Β±2 items in biological systems.

Property Value
Storage Volatile off-heap segment
Capacity Configurable (default: 100)
Eviction Circular buffer β€” oldest entries overwritten
Persistence None β€” lost on JVM shutdown
Use case Current task context, recent conversation

Working memory operates as a circular buffer: when the buffer is full, the oldest entry is overwritten by the newest. This mirrors the human prefrontal cortex where only the most recent context is actively maintained.

Special capability: Synaptic tag search without vector math β€” Working Memory supports finding memories by tag alone using the 64-bit Bloom filter field, useful for fast context lookups without embedding the query.


πŸ“ Episodic Memory (Hippocampus)

Biological Analog: The Hippocampus encodes autobiographical events as time-ordered traces. New events are appended rapidly (one-trial learning), and during sleep the hippocampus replays sequences for consolidation into cortical memory.

Property Value
Storage Memory-mapped files (FileChannel.map())
Capacity Unbounded (1 partition per day, each up to 10,000 records)
Eviction Tombstone + compaction
Persistence Full β€” survives JVM restarts
Use case "What error did we debug yesterday?", "What did the user say last week?"

Partition Lifecycle

Each episodic partition is a memory-mapped file with a 64-byte metadata header:

β”Œβ”€β”€β”€ Partition File ─────────────────────────────────────────┐
β”‚ [64B Metadata Header]                                       β”‚
β”‚   β”œβ”€β”€ 4B magic (0x45504943 = "EPIC")                       β”‚
β”‚   β”œβ”€β”€ 4B version (1)                                        β”‚
β”‚   β”œβ”€β”€ 4B count (live records)                               β”‚
β”‚   β”œβ”€β”€ 4B tombstoneCount                                     β”‚
β”‚   β”œβ”€β”€ 4B capacity                                           β”‚
β”‚   β”œβ”€β”€ 4B state (ACTIVE/SEALED/REFLECTABLE/TOMBSTONED/...)  β”‚
β”‚   β”œβ”€β”€ 4B stride                                             β”‚
β”‚   └── 36B reserved                                          β”‚
β”œβ”€β”€ [Record 0: 64B header + NB vector] ───────────────────────
β”œβ”€β”€ [Record 1: 64B header + NB vector] ───────────────────────
β”‚   ...                                                       β”‚
└── [Record N-1]  β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Partition state machine:

stateDiagram-v2
    [*] --> ACTIVE: Create partition
    ACTIVE --> SEALED: Day rolls over
    SEALED --> REFLECTABLE: ReflectDaemon marks eligible
    REFLECTABLE --> TOMBSTONED: High tombstone ratio
    TOMBSTONED --> COMPACTED: TombstoneCompactor rebuilds
    COMPACTED --> [*]: Old partition swapped out
Loading

🧬 Semantic Memory (Neocortex)

Biological Analog: The Neocortex stores distilled, permanent world knowledge β€” facts, concepts, and generalized rules extracted from repeated experience.

Partitioned Mode (default for DISK persistence)

Property Value
Storage Rolling semantic-NNN.mem files
Capacity per partition Configurable (default: 10,000 records)
Total capacity Unbounded (new partitions roll automatically)
Eviction Tombstone + per-partition compaction
Persistence Full β€” mmap-backed files survive restarts
Recall Parallel per-partition scan via virtual threads
Use case "The user prefers dark mode", "Java uses garbage collection"
.spector/memory/semantic/
  semantic-000.mem     ← partition 0 (10K records, oldest)
  semantic-001.mem     ← partition 1 (created when 0 fills up)
  semantic-002.mem     ← partition 2 (active β€” accepts writes)

Concurrency model:

  • Reads: CopyOnWriteArrayList provides lock-free snapshot iteration. Each partition is searched independently on its own virtual thread
  • Writes: ReadWriteLock β€” read lock for normal appends, write lock only when rolling to a new partition
  • Compaction: Per-partition rebuild. Other partitions remain readable during compaction

Creation: Semantic memories are created either:

  1. Directly by the user (MemoryType.SEMANTIC)
  2. By consolidation β€” the ReflectDaemon clusters similar episodic memories during "sleep" and promotes the cluster centroid to semantic memory

Migration: Existing single-file stores are automatically migrated to the partitioned format on first startup.

Single-File Mode (in-memory)

Property Value
Storage Fixed-capacity off-heap slab
Capacity Configurable (default: 5,000)
Use case Small deployments or in-memory mode

βš™οΈ Procedural Memory (Basal Ganglia)

Biological Analog: The Basal Ganglia stores learned motor programs and habitual behaviors β€” "how to ride a bicycle" type knowledge that operates below conscious awareness.

Property Value
Storage Linear off-heap segment
Capacity Configurable (default: 500)
Eviction None (append-only)
Persistence Via WAL replay
Use case "Always use exponential backoff", "Format SQL with uppercase keywords"

Procedural memories represent rules and patterns that the agent has internalized. They are typically higher-importance, persistent, and rarely forgotten.


Next Steps

  • :material-sleep: [[Hippocampus β€” Sleep Consolidation|Memory--Hippocampus]] β€” how episodic memories are consolidated into semantic knowledge
  • :material-flash: [[Synapse β€” Tags & Scoring|Memory--Synapse]] β€” the 64-byte header and Bloom filter
  • :material-lightning-bolt: [[6-Phase Scoring Pipeline|Memory--Scoring-Pipeline]] β€” the SIMD hot-loop

🏠 Home


Clone this wiki locally