Back to Ruflo

ADR-006: Unified Memory Service

v3/implementation/adrs/ADR-006-UNIFIED-MEMORY.md

3.6.305.0 KB
Original Source

ADR-006: Unified Memory Service

Status: Implemented Date: 2026-01-03

Context

v2 has 6 memory implementations: MemoryManager, DistributedMemory, SwarmMemory, AdvancedMemoryManager, SQLiteBackend, MarkdownBackend.

Decision

Single MemoryService with pluggable backends.

typescript
class MemoryService {
  constructor(
    private backend: IMemoryBackend, // SQLite, AgentDB, or Hybrid
    private cache: MemoryCache,
    private indexer: MemoryIndexer
  ) {}
}

// Backend selection via config
{
  memory: {
    backend: 'hybrid', // 'sqlite' | 'agentdb' | 'hybrid'
    cacheSize: 100,
    indexing: true
  }
}

Backend Selection

BackendUse CaseProsCons
SQLiteStructured queries, ACIDFast, reliableNo vector search
AgentDBSemantic search, RAGVector similarityRequires setup
HybridGeneral purposeBest of bothHigher memory

Implementation

Memory Service Interface:

typescript
interface IMemoryService {
  // Core operations
  store(entry: MemoryEntry): Promise<string>;
  retrieve(id: string): Promise<MemoryEntry | null>;
  delete(id: string): Promise<boolean>;

  // Query operations
  search(query: MemoryQuery): Promise<MemoryEntry[]>;
  searchSemantic(text: string, k: number): Promise<MemoryEntry[]>;

  // Namespace operations
  listNamespaces(): Promise<string[]>;
  clearNamespace(namespace: string): Promise<void>;
}

// Memory entry with embedding support
interface MemoryEntry {
  id: string;
  namespace: string;
  content: string;
  type: 'episodic' | 'semantic' | 'procedural' | 'working';
  metadata?: Record<string, unknown>;
  embedding?: Float32Array;
  createdAt: Date;
  ttl?: number;
}

AgentDB Integration:

typescript
class AgentDBBackend implements IMemoryBackend {
  private db: AgentDB;

  constructor(config: AgentDBConfig) {
    this.db = new AgentDB({
      dimensions: config.dimensions,
      indexType: 'HNSW',
      hnswM: 16,
      hnswEfConstruction: 200,
    });
  }

  async searchSemantic(embedding: Float32Array, k: number): Promise<MemoryEntry[]> {
    // Uses HNSW for 150x-12,500x faster search
    return this.db.search(embedding, k);
  }
}

Performance Targets

  • HNSW Search: 150x-12,500x faster than linear scan
  • Query latency: <100ms for 1M+ entries
  • Memory overhead: <500MB for 100K entries
  • Cache hit rate: >80%

Success Metrics

  • Single MemoryService interface
  • 3 backend implementations (SQLite, AgentDB, Hybrid)
  • 90% reduction in memory code
  • Migration from v2 data

Updates (2026-01-07)

Batch Operations Optimization

Added optimized bulk operations to AgentDBAdapter for 2-3x faster batch processing:

typescript
// 4-phase optimized bulk insert
async bulkInsert(entries: MemoryEntry[], options?: { batchSize?: number }): Promise<void> {
  // Phase 1: Parallel embedding generation in batches
  // Phase 2: Store all entries (skip individual cache updates)
  // Phase 3: Batch index embeddings
  // Phase 4: Batch cache update (only populate hot entries)
}

// Parallel bulk retrieval
async bulkGet(ids: string[]): Promise<Map<string, MemoryEntry | null>>;

// Batch updates with parallel processing
async bulkUpdate(updates: Array<{ id: string; update: MemoryEntryUpdate }>): Promise<Map<string, MemoryEntry | null>>;

// Parallel deletion
async bulkDelete(ids: string[]): Promise<Map<string, boolean>>;

Performance Improvements:

  • Bulk insert: 2-3x faster via parallel embedding generation
  • Bulk get: 2x faster via Promise.all()
  • Bulk delete: 2x faster via parallel processing

Package Version


Updates (2026-01-08)

CLI Memory Init Command

Added memory init command to CLI (@claude-flow/[email protected]) using sql.js (WASM SQLite) for cross-platform compatibility without native compilation.

bash
# Initialize memory database
npx @claude-flow/cli@latest memory init

# Options
npx @claude-flow/cli@latest memory init --backend sqlite  # Default
npx @claude-flow/cli@latest memory init --path ./data/custom.db
npx @claude-flow/cli@latest memory init --force  # Overwrite existing

Schema (6 tables):

TableSchema
memory_entriesid, namespace, key, value, metadata, created_at, updated_at, ttl
vectorsid, entry_id, embedding (768-dim), norm
patternsid, name, pattern_data, confidence, created_at, updated_at
sessionsid, session_data, started_at, ended_at, status
trajectoriesid, session_id, step, state, action, reward, next_state, created_at
metadatakey, value, updated_at

Why sql.js:

  • ✅ Cross-platform (WASM, no native compilation)
  • ✅ Works in GitHub Codespaces, Docker, CI
  • ✅ No better-sqlite3 native binding issues
  • ✅ Persistent storage via file sync

Locations:

  • .swarm/memory.db - Primary database
  • .claude/memory.db - Sync location for Claude Code hooks

Implementation Date: 2026-01-04 Last Updated: 2026-01-08 Status: ✅ Complete (with CLI init)