Memory System Architecture

Hub: README.md | Full Architecture: ARCHITECTURE.md


Overview

The memory system provides 7 distinct memory types inspired by MIRIX (arXiv:2507.07957):

  1. Core - Agent identity and constraints
  2. Episodic - Task experiences
  3. Semantic - Domain knowledge
  4. Procedural - Skills and workflows
  5. Resource - External references
  6. Vault - Cross-session persistence
  7. Belief - Hindsight belief memory for reasoning agents (arXiv:2512.12818)

This architecture is inspired by MIRIX (arXiv:2507.07957), which reports +35% accuracy vs baseline RAG and 99.9% storage reduction in paper benchmarks. These are research metrics from the paper, not measured on this system.


Core Interface: IMemoryBackend

Hybrid persistence layer with SQLite + Markdown export.

interface IMemoryBackend {
  set<T>(key: string, value: T, metadata?: MemoryMetadata): Promise<void>;
  get<T>(key: string): Promise<T | undefined>;
  has(key: string): Promise<boolean>;
  delete(key: string): Promise<boolean>;
  clear(): Promise<void>;
  keys(): AsyncIterable<string>;
  entries<T>(): AsyncIterable<[string, T]>;
  size(): Promise<number>;
}

type MemoryImportance = 'critical' | 'high' | 'medium' | 'low';
// High-importance memories are also written to Markdown files

Importance-Based Persistence

ImportanceSQLiteMarkdownUse Case
criticalYesYesAgent identity, constraints
highYesYesImportant learnings
mediumYesNoTask context
lowYesNoTemporary data

Typed Memory System (ITypedMemory)

MIRIX-style 7-type memory with specialized sub-interfaces.

interface ITypedMemory {
  readonly core: ICoreMemory; // Agent identity, constraints
  readonly episodic: IEpisodicMemory; // Task experiences
  readonly semantic: ISemanticMemory; // Domain knowledge
  readonly procedural: IProceduralMemory; // Skills, workflows
  readonly resource: IResourceMemory; // External references
  readonly vault: IKnowledgeVault; // Persistent cross-session storage

  queryByType(type: MemoryType, query: string): Promise<Result<TypedMemoryEntry[], MemoryError>>;
  filterByRelevance(role: AgentRole): Promise<Result<TypedMemoryEntry[], MemoryError>>;
  getStats(): Promise<Result<TypedMemoryStats, MemoryError>>;
  pruneExpired(): Promise<Result<TypedMemoryPruneResult, MemoryError>>;
}

type MemoryType = 'core' | 'episodic' | 'semantic' | 'procedural' | 'resource' | 'vault';

Memory Type Details

TypePurposePersistenceExample Content
coreAgent identityPermanentRole, capabilities, limits
episodicTask experiencesSessionWhat worked, what failed
semanticDomain knowledgeLong-termBest practices, patterns
proceduralSkills and workflowsLong-termHow to perform tasks
resourceExternal referencesCachedURLs, file paths, APIs
vaultCross-session storagePermanentLearnings across sessions

Routing Memory (IRoutingMemory)

Bridges memory and routing systems for learning-based model selection.

interface IRoutingMemory {
  // Preference Storage (#148 - Preference-Trained Routing)
  storePreference(
    decision: RoutingDecisionRecord,
    outcome: TaskOutcomeRecord,
    preference?: PreferenceSignal
  ): Promise<Result<void, MemoryError>>;
  getPreferences(
    filter: PreferenceFilter,
    limit: number
  ): Promise<Result<PreferenceRecord[], MemoryError>>;

  // Experience Memory (#149 - MobiMem Evolution)
  storeExperience(experience: ExperienceRecord): Promise<Result<void, MemoryError>>;
  getExperiences(query: string, limit: number): Promise<Result<ExperienceRecord[], MemoryError>>;

  // Action Memory (#149 - MobiMem Evolution)
  storeAction(action: ActionRecord): Promise<Result<void, MemoryError>>;
  getActions(taskType: string, limit: number): Promise<Result<ActionRecord[], MemoryError>>;

  // Export/Import for training
  export(): Promise<Result<RoutingMemoryExport, MemoryError>>;
  import(data: RoutingMemoryExport): Promise<Result<void, MemoryError>>;

  // Statistics
  getStats(): Promise<Result<RoutingMemoryStats, MemoryError>>;
}

Integration Points

  • Preference-Trained Routing (RouteLLM): Export preference data for training
  • MobiMem Evolution: Experience/action memory for post-deployment learning
  • CompositeRouter: Feeds routing decisions into LinUCB bandit

Graph Memory

Entity relationships extracted from context.

interface IGraphMemory {
  addNode(entity: Entity): Promise<void>;
  addEdge(from: string, to: string, relation: string): Promise<void>;
  query(pattern: GraphPattern): Promise<Entity[]>;
  getRelated(entityId: string, depth?: number): Promise<Entity[]>;
  visualize(): string; // Mermaid diagram
}

Entity Extraction

Automatically extracts:

  • Named entities: Functions, classes, files, concepts
  • Relationships: Uses, extends, imports, references
  • Attributes: Types, visibility, documentation

Use Cases

Query TypeExampleResult
Direct relations”What does UserService depend on?”List of dependencies
Transitive closure”What’s affected if we change Auth?”Impact graph
Pattern match”Find all classes with security bugs”Matching entities

Adaptive Memory

Priority-based retrieval combining recency, importance, and relevance.

interface IAdaptiveMemory {
  store(item: MemoryItem): Promise<void>;
  retrieve(query: string, limit: number): Promise<MemoryItem[]>;
  setDecayRate(rate: number): void;
  getRelevanceScore(item: MemoryItem, context: string): number;
}

interface MemoryItem {
  content: string;
  timestamp: number;
  importance: number; // 0-1
  accessCount: number;
  lastAccessed: number;
}

Retrieval Algorithm

Priority score = (importance × 0.4) + (recency × 0.3) + (relevance × 0.3)

Where:

  • Importance: User-assigned or learned from feedback
  • Recency: Exponential decay from creation time
  • Relevance: Semantic similarity to current query

Session Memory

Cross-session persistence for learning retention (Reflexion pattern).

interface ISessionMemory {
  startSession(sessionId: string): void;
  endSession(): Promise<void>;
  recordLearning(learning: Learning): void;
  recordTask(task: TaskRecord): void;
  recordResolvedError(error: ResolvedError): void;
  loadRelevantLearnings(context: string, minConfidence?: number): Promise<Learning[]>;
  searchLearnings(query: string): Promise<Learning[]>;
  getErrorSolution(errorPattern: string): Promise<ResolvedError | undefined>;
}

Learning Persistence

{
  "id": "learn_001",
  "content": "Always validate user input before database queries",
  "context": "SQL injection prevention",
  "confidence": 0.95,
  "createdAt": "2026-01-15T10:00:00Z",
  "source": "security_review_task"
}

Memory Lifecycle

sequenceDiagram
    participant A as Agent
    participant TM as TypedMemory
    participant AM as AdaptiveMemory
    participant V as Vault

    A->>TM: Query by task type
    TM->>AM: Get relevant items
    AM-->>TM: Priority-sorted results
    TM-->>A: Typed memory entries

    A->>A: Execute task
    A->>TM: Store experience
    TM->>AM: Update priorities

    alt High importance
        TM->>V: Persist to vault
    end

Configuration

memory:
  backend: hybrid # sqlite | markdown | hybrid
  maxSize: 100MB
  pruneThreshold: 80%

  typed:
    enabled: true
    types: [core, episodic, semantic, procedural, resource, vault]

  graph:
    enabled: true
    maxNodes: 10000

  adaptive:
    decayRate: 0.95 # Per-hour decay
    relevanceThreshold: 0.3

Source Files

FilePurpose
src/context/memory-backend.tsBase backend implementation
src/context/typed-memory.tsTyped memory system
src/context/typed-memory-impl.tsType implementations
src/context/graph-memory.tsGraph-based memory
src/context/adaptive-memory.tsAdaptive retrieval
src/context/session-memory.tsSession persistence
src/context/agentic-memory.tsA-MEM implementation
src/context/mobimem.tsMobiMem evolution
src/core/types/routing-memory.tsRouting memory interface

Research Sources

TechniquePaperPaper-Reported Metrics (not measured on this system)
MIRIX Six-TypearXiv:2507.07957+35% accuracy vs baseline RAG, 99.9% storage reduction
Mem0 ArchitecturearXiv:2504.1941391% latency reduction, 90% token reduction (paper benchmark)
MobiMem EvolutionarXiv:2512.15784280x faster than GraphRAG (paper benchmark)
A-MEM Agentic MemoryarXiv:2502.12110Zettelkasten-inspired linking
ReflexionarXiv:2303.1136691% HumanEval (paper benchmark)