# Ruvector Domain-Driven Design Architecture ## Executive Summary This document describes the comprehensive Domain-Driven Design (DDD) architecture for ruvector, a high-performance vector database ecosystem with neural capabilities, graph algorithms, distributed consensus, and collective intelligence. The architecture defines **nine bounded contexts**, their entities, value objects, aggregates, repositories, domain events, anti-corruption layers, and context maps. ## Table of Contents 1. [Strategic Design Overview](#1-strategic-design-overview) 2. [Bounded Contexts](#2-bounded-contexts) 3. [Domain Entities](#3-domain-entities) 4. [Value Objects](#4-value-objects) 5. [Aggregates and Repositories](#5-aggregates-and-repositories) 6. [Domain Events and Event Sourcing](#6-domain-events-and-event-sourcing) 7. [Anti-Corruption Layers](#7-anti-corruption-layers) 8. [Context Maps](#8-context-maps) 9. [Aggregate Lifecycle Diagrams](#9-aggregate-lifecycle-diagrams) --- ## 1. Strategic Design Overview ### 1.1 Domain Vision Ruvector provides a unified platform for vector-based AI operations spanning: - High-performance vector storage and similarity search - Neural attention mechanisms (MoE, GNN, hyperbolic, sheaf-theoretic) - Adaptive learning (SONA, LoRA, EWC) - Graph algorithms (MinCut, HNSW, Graph Transformers) - Distributed consensus (Raft, replication) - Coherence verification (Prime-Radiant, sheaf cohomology) - Cross-platform deployment (WASM, Node.js, CLI) - Collective intelligence (MCP Brain, witness chains) - LLM serving runtime (RuvLLM) ### 1.2 Domain Classification ```mermaid graph TB subgraph "Core Domain (Differentiating)" SC[Solver Context
Vector Search, HNSW] NC[Neural Context
Attention, MoE, GNN] MC[Memory Context
SONA, LoRA, EWC] end subgraph "Supporting Domain (Essential)" GC[Graph Context
MinCut, Graph DB] CC[Coherence Context
Sheaf Laplacian] DC[Distributed Context
Raft, Replication] end subgraph "Generic Domain (Commodity)" PC[Platform Context
WASM, Node, CLI] BC[Brain Context
MCP, Witness Chains] IC[Inference Context
RuvLLM Serving] end SC --> NC NC --> MC MC --> BC GC --> CC DC --> GC SC --> GC NC --> CC MC --> IC PC --> SC PC --> NC PC --> MC ``` ### 1.3 Core Domain vs Supporting Domains ``` +------------------------------------------------------------------+ | CORE DOMAIN | | +------------------+ +-----------------+ +------------------+ | | | Solver Context | | Neural Context | | Memory Context | | | | (Vector Search) | | (Attention/MoE) | | (SONA/LoRA/EWC) | | | +------------------+ +-----------------+ +------------------+ | +------------------------------------------------------------------+ | SUPPORTING DOMAINS | | +------------------+ +-----------------+ +------------------+ | | | Graph Context | | Coherence Ctx | | Distributed Ctx | | | | (MinCut/GraphDB) | | (Sheaf/Energy) | | (Raft/Replica) | | | +------------------+ +-----------------+ +------------------+ | +------------------------------------------------------------------+ | GENERIC DOMAINS | | +------------------+ +-----------------+ +------------------+ | | | Platform Context | | Brain Context | | Inference Ctx | | | | (WASM/Node/CLI) | | (MCP/Witness) | | (RuvLLM) | | | +------------------+ +-----------------+ +------------------+ | +------------------------------------------------------------------+ ``` ### 1.4 Ubiquitous Language | Term | Definition | |------|------------| | **Vector** | An n-dimensional floating-point array representing semantic content | | **Embedding** | A vector representation of text, images, or other data | | **HNSW** | Hierarchical Navigable Small World graph for approximate nearest neighbor search | | **Quantization** | Compression technique reducing vector precision (Scalar, Product, Binary) | | **Trajectory** | A sequence of execution steps with associated rewards for learning | | **Pattern** | A learned cluster centroid extracted from successful trajectories | | **Witness** | A cryptographic attestation of computation with Ed25519 signatures | | **Attestation** | Proof that a cognitive operation was performed correctly | | **MoE** | Mixture of Experts routing mechanism for specialized attention | | **Sheaf** | Mathematical structure assigning data to open sets with consistency conditions | | **Residual** | Contradiction energy at a graph edge: r_e = rho_u(x_u) - rho_v(x_v) | | **Coherence Energy** | Global incoherence measure: E(S) = sum(w_e * ||r_e||^2) | | **MinCut** | Minimum edge cut partitioning a graph into components | | **Poincare Ball** | Hyperbolic space model for hierarchy-aware embeddings | | **LoRA** | Low-Rank Adaptation for efficient fine-tuning | | **EWC** | Elastic Weight Consolidation for preventing catastrophic forgetting | | **Fisher Information** | Metric measuring parameter importance for consolidation | | **RVF** | Ruvector Format for cognitive containers with witness chains | | **Compute Lane** | Priority tier for coherence-gated execution (Reflex/Retrieval/Heavy/Human) | --- ## 2. Bounded Contexts ### 2.1 Solver Context (Core Algorithms) **Purpose**: High-performance vector storage, indexing, and similarity search operations. **Crates**: - `ruvector-core` - Core vector database functionality - `ruvector-solver` - Iterative sparse linear solvers (Neumann, CG, BMSSP) - `ruvector-router-core` - Vector storage and HNSW indexing - `ruvector-hyperbolic-hnsw` - Poincare ball embeddings with HNSW **Responsibilities**: - HNSW index construction and maintenance - Distance calculations (Cosine, Euclidean, DotProduct, Manhattan, Poincare) - Quantization (Scalar 4x, Int4 8x, Product 8-16x, Binary 32x) - Sparse matrix solving (Neumann, CG, Forward-Push, BMSSP) - Hyperbolic embeddings for hierarchical data - Tangent space pruning for accelerated search **Key Aggregates**: - `VectorDB` - Root aggregate managing vectors, indices, and storage - `HnswIndex` - Navigable small world graph structure - `HyperbolicHnsw` - Poincare ball index with dual-space support **Key Interfaces**: ```rust // Core vector operations pub trait VectorDB { fn insert(&self, entry: VectorEntry) -> Result; fn search(&self, query: SearchQuery) -> Result>; fn delete(&self, id: &VectorId) -> Result<()>; fn batch_insert(&self, entries: Vec) -> Result>; } // Solver operations pub trait SolverEngine { fn solve(&self, matrix: &CsrMatrix, rhs: &[Scalar]) -> SolverResult; } // Hyperbolic operations pub trait HyperbolicIndex { fn insert(&mut self, vector: Vec) -> HyperbolicResult; fn search(&self, query: &[f32], k: usize) -> HyperbolicResult>; fn search_with_pruning(&self, query: &[f32], k: usize) -> HyperbolicResult>; } ``` --- ### 2.2 Neural Context (Attention, MoE, GNN) **Purpose**: Advanced attention mechanisms and neural network operations for AI inference. **Crates**: - `ruvector-attention` - Multi-head, sparse, hyperbolic, sheaf, and MoE attention - `ruvector-gnn` - Graph Neural Network layers, EWC, replay buffer - `ruvector-cnn` - Convolutional operations - `ruvector-graph-transformer` - Graph-aware transformer architecture **Responsibilities**: - Scaled dot-product and multi-head attention computation - Mixture of Experts (MoE) routing with learned routers - Hyperbolic attention in Poincare ball and Lorentz manifolds - Graph attention with edge features and dual-space projections - Sparse attention patterns (Flash, Linear, Local-Global) - Information geometry (Fisher metric, natural gradients) - Sheaf attention with coherence gating (ADR-015) - PDE-based diffusion attention - Optimal transport attention (Sliced Wasserstein) **Key Aggregates**: - `AttentionPipeline` - Composable attention layers - `MoEAttention` - Expert routing and selection - `SheafAttention` - Coherence-gated transformer layers - `TopologyGatedAttention` - Adaptive attention with topology awareness **Key Interfaces**: ```rust // Attention trait hierarchy pub trait Attention { fn compute(&self, query: &[f32], keys: &[&[f32]], values: &[&[f32]]) -> AttentionResult>; } pub trait GeometricAttention: Attention { fn compute_with_geometry(&self, query: &[f32], keys: &[&[f32]], values: &[&[f32]]) -> AttentionResult<(Vec, GeometryMetrics)>; } pub trait TrainableAttention: Attention { fn backward(&mut self, grad_output: &[f32], cache: &ForwardCache) -> Gradients; fn update(&mut self, gradients: &Gradients, learning_rate: f32); } // MoE routing pub trait Router { fn route(&self, input: &[f32]) -> Vec<(usize, f32)>; // (expert_id, weight) } // Sheaf attention (ADR-015) pub trait SheafAttentionLayer { fn forward_with_early_exit(&self, input: &[f32], coherence: f32) -> EarlyExitResult; } ``` --- ### 2.3 Memory Context (SONA, LoRA, EWC) **Purpose**: Adaptive learning, continual learning, and memory consolidation for self-improving AI systems. **Crates**: - `sona` - Self-Optimizing Neural Architecture with ReasoningBank - `ruvector-gnn` (ewc, replay modules) - Elastic Weight Consolidation - `ruvllm` (reasoning_bank, sona, lora modules) - LLM integration with learning **Responsibilities**: - Micro-LoRA (rank 1-2) for instant adaptation (<0.05ms) - Base-LoRA (rank 4-16) for background learning - EWC++ for catastrophic forgetting prevention - Trajectory recording and pattern extraction - ReasoningBank for pattern storage with HNSW indexing - Three-tier learning loops (Instant, Background, Coordination) - Memory distillation and consolidation - Verdict analysis for failed trajectories **Key Aggregates**: - `SonaEngine` - Root aggregate for adaptive learning - `ReasoningBank` - Pattern storage with HNSW search - `TrajectoryBuffer` - Accumulates execution traces - `EwcPlusPlus` - Fisher-weighted parameter consolidation **Key Interfaces**: ```rust // SONA engine pub trait SonaEngine { fn begin_trajectory(&self, embedding: Vec) -> TrajectoryBuilder; fn end_trajectory(&self, builder: TrajectoryBuilder, quality: f32); fn apply_micro_lora(&self, input: &[f32], output: &mut [f32]); } // LoRA operations pub trait LoRALayer { fn forward(&self, input: &[f32]) -> Vec; fn update(&mut self, signal: &LearningSignal); } // EWC for forgetting mitigation pub trait ElasticWeightConsolidation { fn compute_fisher(&self, gradients: &[Vec]) -> TaskFisher; fn consolidate(&mut self, new_params: &[f32], fisher: &TaskFisher); } // ReasoningBank pub trait PatternStore { fn store(&mut self, pattern: Pattern) -> Result; fn search(&self, embedding: &[f32], k: usize) -> Result>; fn consolidate(&mut self, config: ConsolidationConfig) -> Result; } ``` --- ### 2.4 Graph Context (MinCut, GraphDB) **Purpose**: Graph algorithms, dynamic connectivity, and graph-based data storage. **Crates**: - `ruvector-mincut` - Subpolynomial-time dynamic minimum cut - `ruvector-graph` - Property graph database with Cypher - `ruvector-dag` - Directed acyclic graph operations **Responsibilities**: - Exact and approximate minimum cut algorithms - Subpolynomial O(n^{o(1)}) dynamic updates - Graph sparsification and expander decomposition - Link-cut trees and Euler tour trees - Spiking Neural Network integration for graph optimization - Neo4j-compatible Cypher queries - ACID transactions on graphs - Hyperedge support for higher-order relations - Vector-graph hybrid queries **Key Aggregates**: - `DynamicMinCut` - Root aggregate for min-cut operations - `GraphDB` - Property graph database - `SubpolynomialMinCut` - Breakthrough subpoly algorithm - `CognitiveMinCutEngine` - SNN-based optimization **Key Interfaces**: ```rust // Dynamic min-cut pub trait DynamicMinCut { fn insert_edge(&mut self, u: VertexId, v: VertexId, weight: Weight) -> Result; fn delete_edge(&mut self, u: VertexId, v: VertexId) -> Result; fn min_cut_value(&self) -> f64; fn min_cut(&self) -> MinCutResult; } // Graph database pub trait GraphDB { fn create_node(&mut self, labels: Vec