๐ง Cortex โ Tier Stores¶
Package:
com.spectrayan.spector.memory.cortexBiological 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
TierStore Interface¶
All four stores implement a common TierStore interface, enabling polymorphic dispatch in the router:
public interface TierStore extends AutoCloseable {
MemoryType type();
int size();
CognitiveRecordLayout layout();
MemorySegment primarySegment();
long write(CognitiveHeader header, byte[] quantizedVec);
}
The TierRouter holds an EnumMap<MemoryType, TierStore> and dispatches all operations polymorphically:
// Zero switch statements โ polymorphic dispatch
public long write(MemoryType type, CognitiveHeader header, byte[] quantized) {
return get(type).write(header, quantized);
}
Adding a new tier (e.g.,
FLASHfor ultra-fast scratch memory) requires only: (1) implementTierStore, (2) register inTierRouter. No changes needed inSpectorMemory,RecallPipeline, orCognitiveIngestionTarget.
AbstractTierStore¶
Three of the four stores (Working, Semantic, Procedural) extend AbstractTierStore, which provides:
- Arena lifecycle:
Arena.ofShared()for thread-safe off-heap access - Segment allocation: 32-byte aligned via
arena.allocate(bytes, 32) - Layout creation from quantized vector byte count
- Capacity tracking and size reporting
- Close/cleanup lifecycle
EpisodicMemoryStore implements TierStore directly because it uses mmap-backed partitions rather than a single Arena-allocated segment.
๐งช 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 | Arena.ofShared() volatile segment |
| Capacity | Configurable (default: 100) |
| Eviction | Circular buffer โ oldest entries overwritten |
| Persistence | None โ lost on JVM shutdown |
| Use case | Current task context, recent conversation |
// Circular buffer write
public long write(CognitiveHeader header, byte[] quantizedVec) {
long offset = (long) (count % capacity) * layout.stride();
layout.writeHeader(segment, offset, header);
MemorySegment.copy(MemorySegment.ofArray(quantizedVec), 0,
segment, layout.vectorOffset(offset), quantizedVec.length);
count++;
return offset;
}
Special capability: Synaptic tag search without vector math. WorkingMemoryStore supports a findByTag(mask) method that scans only the 64-bit Bloom filter field โ useful for fast context lookups.
๐ 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 | FileChannel.map() mmap-backed files |
| 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: 32B header + NB vector] โโโโโโโโโโโโโโโโโโโโโโโค
โโโ [Record 1: 32B 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
๐งฌ Semantic Memory (Neocortex)¶
Biological Analog: The Neocortex stores distilled, permanent world knowledge โ facts, concepts, and generalized rules extracted from repeated experience.
| Property | Value |
|---|---|
| Storage | Header-only slab (Arena.ofShared()) |
| Capacity | Configurable (default: 5,000) |
| Eviction | None (permanent) |
| Persistence | Via WAL replay |
| Use case | "The user prefers dark mode", "Java uses garbage collection" |
Header-Only Storage
Semantic memories store only the 32-byte synaptic header, not the full quantized vector. This enables fast metadata scans (tag match, importance, valence) at minimal memory cost. For vector similarity, the text is re-embedded at query time when needed.
Creation: Semantic memories are created either:
- Directly by the user (
MemoryType.SEMANTIC) - By consolidation โ the
ReflectDaemonclusters similar episodic memories during "sleep" and promotes the cluster centroid to semantic memory
โ๏ธ 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 | Arena.ofShared() linear 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.
TierRouter¶
The TierRouter dispatches all operations to the appropriate store via an EnumMap:
public final class TierRouter implements AutoCloseable {
private final EnumMap<MemoryType, TierStore> stores = new EnumMap<>(MemoryType.class);
// Polymorphic dispatch โ zero switch statements
public long write(MemoryType type, CognitiveHeader header, byte[] quantized) {
return get(type).write(header, quantized);
}
public MemorySegment segmentFor(MemoryType type) {
return get(type).primarySegment();
}
public static boolean shouldScan(MemoryType type, MemoryType[] targetTypes) {
if (targetTypes == null || targetTypes.length == 0) return true;
for (MemoryType t : targetTypes) if (t == type) return true;
return false;
}
}
Next Steps¶
- Hippocampus โ Sleep Consolidation โ how episodic memories are consolidated into semantic knowledge
- Synapse โ Tags & Scoring โ the 32-byte header and Bloom filter
- 6-Phase Scoring Pipeline โ the SIMD hot-loop