mirror of
https://github.com/ruvnet/RuVector.git
synced 2026-05-28 09:53:36 +00:00
* docs(coherence-engine): add ADR-014 and DDD for sheaf Laplacian coherence engine Add comprehensive architecture documentation for ruvector-coherence crate: - ADR-014: Sheaf Laplacian-based coherence witnessing architecture - Universal coherence object with domain-agnostic interpretation - 5-layer architecture (Application → Gate → Computation → Governance → Storage) - 4-tier compute ladder (Reflex → Retrieval → Heavy → Human) - Full ruvector ecosystem integration (10+ crates) - 15 internal architectural decisions - DDD: Domain-Driven Design with 10 bounded contexts - Tile Fabric (cognitum-gate-kernel) - Adaptive Learning (sona) - Neural Gating (ruvector-nervous-system) - Learned Restriction Maps (ruvector-gnn) - Hyperbolic Coherence (ruvector-hyperbolic-hnsw) - Incoherence Isolation (ruvector-mincut) - Attention-Weighted Coherence (ruvector-attention) - Distributed Consensus (ruvector-raft) Key concept: "This is not prediction. It is a continuously updated field of coherence that shows where action is safe and where action must stop." Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(prime-radiant): implement sheaf Laplacian coherence engine Implement the complete Prime-Radiant crate based on ADR-014: Core Modules: - substrate/: SheafGraph, SheafNode, SheafEdge, RestrictionMap (SIMD-optimized) - coherence/: CoherenceEngine, energy computation, spectral drift detection - governance/: PolicyBundle, WitnessRecord, LineageRecord (Blake3 hashing) - execution/: CoherenceGate, ComputeLane, ActionExecutor Ecosystem Integrations (feature-gated): - tiles/: cognitum-gate-kernel 256-tile WASM fabric adapter - sona_tuning/: Adaptive threshold learning with EWC++ - neural_gate/: Biologically-inspired gating with HDC encoding - learned_rho/: GNN-based learned restriction maps - attention/: Topology-gated attention, MoE routing, PDE diffusion - distributed/: Raft-based multi-node coherence Testing: - 138 tests (integration, property-based, chaos) - 8 benchmarks covering ADR-014 performance targets Stats: 91 files, ~30K lines of Rust code "This is not prediction. It is a continuously updated field of coherence that shows where action is safe and where action must stop." Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs(adr): add RuvLLM integration to ADR-014 v0.4 - Add coherence-gated LLM inference architecture diagram - Add 5 integration modules with code examples: - SheafCoherenceValidator (replaces heuristic scoring) - UnifiedWitnessLog (merged audit trail) - PatternToRestrictionBridge (ReasoningBank → learned ρ) - MemoryCoherenceLayer (context as sheaf nodes) - CoherenceConfidence (energy → confidence mapping) - Add 7 integration ADRs (ADR-CE-016 through ADR-CE-022) - Add ruvllm to crate integration matrix and dependencies - Add 4 LLM-specific benefits to consequences - Add ruvllm feature flag Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs(adr): add 22 coherence engine internal ADRs Create detailed ADR files for all internal coherence engine decisions: Core Architecture (ADR-CE-001 to ADR-CE-008): - 001: Sheaf Laplacian defines coherence witness - 002: Incremental computation with stored residuals - 003: PostgreSQL + ruvector hybrid storage - 004: Signed event log with deterministic replay - 005: First-class governance objects - 006: Coherence gate controls compute ladder - 007: Thresholds auto-tuned from traces - 008: Multi-tenant isolation boundaries Universal Coherence (ADR-CE-009 to ADR-CE-015): - 009: Single coherence object (one math, many interpretations) - 010: Domain-agnostic nodes and edges - 011: Residual = contradiction energy - 012: Gate = refusal mechanism with witness - 013: Not prediction (coherence field, not forecasting) - 014: Reflex lane default (most ops stay fast) - 015: Adapt without losing control RuvLLM Integration (ADR-CE-016 to ADR-CE-022): - 016: CoherenceValidator uses sheaf energy - 017: Unified audit trail (WitnessLog + governance) - 018: Pattern-to-restriction bridge (ReasoningBank) - 019: Memory as nodes (agentic, working, episodic) - 020: Confidence from energy (sigmoid mapping) - 021: Shared SONA between ruvllm and prime-radiant - 022: Failure learning (ErrorPatternLearner → ρ maps) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(prime-radiant): implement RuvLLM integration layer (ADR-014 v0.4) Implement complete Prime-Radiant + RuvLLM integration per ADR-CE-016 through ADR-CE-022: Core Integration Modules: - coherence_validator.rs: SheafCoherenceValidator using sheaf energy - witness_log.rs: UnifiedWitnessLog with hash chain for tamper evidence - pattern_bridge.rs: PatternToRestrictionBridge learning from verdicts - memory_layer.rs: MemoryCoherenceLayer tracking context as sheaf nodes - confidence.rs: CoherenceConfidence with sigmoid energy→confidence mapping Supporting Infrastructure: - mod.rs: Public API, re-exports, convenience constructors - error.rs: Comprehensive error types for each ADR - config.rs: LlmCoherenceConfig, thresholds, policies - gate.rs: LlmCoherenceGate high-level interface - adapter.rs: RuvLlmAdapter bridging type systems - bridge.rs: PolicyBridge, SonaBridge for synchronization - witness.rs: WitnessAdapter for correlation - traits.rs: Trait definitions for loose coupling Testing: - 22 integration tests covering all modules - Self-contained mock implementations - Feature-gated with #[cfg(feature = "ruvllm")] Feature Flags: - ruvllm feature in Cargo.toml - Optional dependency on ruvllm crate - Added to "full" feature set Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs(prime-radiant): add comprehensive README with examples Add user-friendly documentation covering: - Introduction explaining coherence vs confidence - Core concepts (coherence field, compute ladder) - Features overview (engine, governance, RuvLLM integration) - Quick start code examples: - Basic coherence check - LLM response validation - Memory consistency tracking - Confidence from energy - Application tiers (today, near-term, future) - Domain examples (AI, finance, medical, robotics, security) - Feature flags reference - Performance targets - Architecture diagram Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs(adr): add ADR-015 Coherence-Gated Transformer (Sheaf Attention) Propose novel low-latency transformer architecture using coherence energy: Core Innovation: - Route tokens to compute lanes based on coherence energy, not confidence - Sparse attention using residual energy (skip coherent pairs) - Early exit when energy converges (not confidence threshold) - Restriction maps replace QKV projections Architecture: - Lane 0 (Reflex): 1-2 layers, local attention, <0.1ms - Lane 1 (Standard): 6 layers, sparse sheaf attention, ~1ms - Lane 2 (Deep): 12+ layers, full + MoE, ~5ms - Lane 3 (Escalate): Return uncertainty Performance Targets: - 5-10x latency reduction (10ms → 1-2ms for 128 tokens) - 2.5x memory reduction - <5% quality degradation - Provable coherence bound on output Mathematical Foundation: - Attention weight ∝ exp(-β × residual_energy) - Token routing via E(t) = Σ w_e ||ρ_t(x) - ρ_ctx(x)||² - Early exit when ΔE < ε (energy converged) Target: ruvector-attention crate with sheaf/ and coherence_gated/ modules Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(prime-radiant): implement coherence engine with CGT attention Complete implementation of Prime-Radiant coherence engine and Coherence-Gated Transformer (CGT) sheaf attention module. Core Features: - Sheaf Laplacian energy computation with restriction maps - 4-lane compute ladder (Reflex/Retrieval/Heavy/Human) - Cryptographic witness chains for audit trails - Policy bundles with multi-party approval Storage Backends: - InMemoryStorage with KNN search - FileStorage with Write-Ahead Logging (WAL) - PostgresStorage with full schema (feature-gated) - HybridStorage combining file + optional PostgreSQL CGT Sheaf Attention (ruvector-attention): - RestrictionMap with residual/energy computation - SheafAttention layer: A_ij = exp(-β×E_ij)/Z - TokenRouter with compute lane routing - SparseResidualAttention with energy-based masking - EarlyExit with energy convergence detection Performance Optimizations: - Zero-allocation hot paths (apply_into, compute_residual_norm_sq) - SIMD-friendly 4-way unrolled loops - Branchless lane routing - Pre-allocated buffers for batch operations RuvLLM Integration: - SheafCoherenceValidator for LLM response validation - UnifiedWitnessLog linking inference + coherence - MemoryCoherenceLayer for contradiction detection - CoherenceConfidence for interpretable uncertainty Tests: 202 passing in ruvector-attention, 180+ in prime-radiant Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(prime-radiant): add GPU acceleration, SIMD optimizations, and benchmarks GPU Acceleration (wgpu-rs): - GpuCoherenceEngine with automatic CPU fallback - GpuDevice: adapter/device management with high-perf selection - GpuDispatcher: kernel execution with pipeline caching and buffer pooling - GpuBufferManager: typed buffer management with pooling - Compute kernels: residuals, energy reduction, sheaf attention, token routing WGSL Compute Shaders (6 files, 1,412 lines): - compute_residuals.wgsl: parallel edge residual computation - compute_energy.wgsl: two-phase parallel reduction - sheaf_attention.wgsl: energy-based attention weights A_ij = exp(-beta * E_ij) - token_routing.wgsl: branchless lane assignment - sparse_mask.wgsl: sparse attention mask generation - types.wgsl: shared GPU struct definitions SIMD Optimizations (wide crate): - Runtime CPU feature detection (AVX2, AVX-512, SSE4.2, NEON) - f32x8 vectorized operations - simd/vectors.rs: dot_product_simd, norm_squared_simd, subtract_simd - simd/matrix.rs: matmul_simd, matvec_simd, transpose_simd - simd/energy.rs: batch_residuals_simd, weighted_energy_sum_simd - 38 unit tests verifying SIMD correctness Benchmarks (criterion): - coherence_benchmarks.rs: core operations, graph scaling - simd_benchmarks.rs: SIMD vs naive comparisons - gpu_benchmarks.rs: CPU vs GPU performance Tests: - 18 GPU coherence tests (16 active, 2 perf ignored) - GPU-CPU consistency within 1% relative error - Error handling and fallback verification README improvements: - "What Prime-Radiant is NOT" section - Concrete numeric example with arithmetic - Flagship LLM hallucination refusal walkthrough - Infrastructure positioning Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * perf(prime-radiant): optimize SIMD and core computation patterns SIMD Optimizations: - Replace element-by-element load_f32x8 with try_into for direct memory copy - Fix redundant SIMD comparisons in lane assignment (compute masks once, use blend) - Apply across vectors.rs, matrix.rs, and energy.rs Core Computation Patterns: - Replace i % 4 modulo with chunks_exact() for proper auto-vectorization - Fix edge.rs: residual_norm_squared, residual_with_energy - Fix node.rs: norm_squared, dot product Graph API: - Add get_node_ref() for zero-copy node access via DashMap reference - Add with_node() closure API for efficient read-only operations Benchmark findings: - Incremental updates meet target (<100us): 59us actual - Linear O(n) scaling confirmed - Further SIMD/parallelization needed for <1us/edge target Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * perf(prime-radiant): add CSR sparse matrix, GPU buffer prealloc, thread-local scratch Performance optimizations for Prime-Radiant coherence engine: CSR Sparse Matrix (restriction.rs): - Full CsrMatrix struct with row_ptr, col_indices, values - COO to CSR conversion with from_coo() and from_coo_arrays() - Zero-allocation matvec_into() and matvec_add_into() - SIMD-friendly 4-element loop unrolling - 13 new tests covering all CSR operations GPU Buffer Pre-allocation (engine.rs, kernels.rs): - Pre-allocated params, energy_params, partial_sums, staging buffers - Zero per-frame allocations in compute_energy() - New create_bind_group_raw() methods for raw buffer references - CSR matrix support in convert_restriction_map() Thread-Local Scratch Buffers (edge.rs): - EdgeScratch struct with 3 reusable Vec<f32> buffers - thread_local! SCRATCH for zero-allocation hot paths - residual_norm_squared_no_alloc() and weighted_residual_energy_no_alloc() - 7 new tests for allocation-free energy computation WGSL Vec4 Optimization (compute_residuals.wgsl): - vec4-based processing loop with dot(r_vec, r_vec) - store_residuals flag in GpuParams struct - ~4x GPU throughput improvement README Updates: - Root README: 40 attention mechanisms, Prime-Radiant section, CGT Sheaf Attention - WASM README: CGT Sheaf Attention API documentation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * chore: SEO optimize package metadata for crates.io and npm - prime-radiant: Enhanced description, keywords, categories - ruvector-attention-wasm: Add version to path dep, SEO keywords - package.json: 23 keywords, better description, engines config Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * chore(hyperbolic-hnsw): SEO optimize for crates.io publish * chore(prime-radiant): add version numbers to path dependencies for crates.io publish * fix(prime-radiant): shorten keyword for crates.io compliance Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs(readme): add prime-radiant and ruvector-attention-wasm package references - Add prime-radiant to Quantum Coherence section (sheaf Laplacian AI safety) - Add ruvector-attention-wasm to npm WASM packages (Flash, MoE, Hyperbolic, CGT) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Reuven <cohen@ruv-mac-mini.local> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
353 lines
11 KiB
Rust
353 lines
11 KiB
Rust
//! Memory Coherence Tracking Example
|
|
//!
|
|
//! This example demonstrates how to use Prime-Radiant's MemoryCoherenceLayer
|
|
//! to track and validate memories in an AI agent system.
|
|
//!
|
|
//! The memory system tracks three types of memory:
|
|
//! - Agentic (long-term patterns)
|
|
//! - Working (current context)
|
|
//! - Episodic (conversation history)
|
|
//!
|
|
//! Run with: `cargo run --example memory_tracking --features ruvllm`
|
|
|
|
#[cfg(feature = "ruvllm")]
|
|
use prime_radiant::ruvllm_integration::{
|
|
AgenticMemory, EpisodicMemory, MemoryCoherenceConfig, MemoryCoherenceLayer, MemoryEntry,
|
|
MemoryType, WorkingMemory,
|
|
};
|
|
|
|
#[cfg(feature = "ruvllm")]
|
|
fn main() {
|
|
println!("=== Prime-Radiant: Memory Coherence Tracking Example ===\n");
|
|
|
|
// Example 1: Basic memory operations
|
|
println!("--- Example 1: Basic Memory Operations ---");
|
|
run_basic_memory_example();
|
|
|
|
println!();
|
|
|
|
// Example 2: Contradiction detection
|
|
println!("--- Example 2: Contradiction Detection ---");
|
|
run_contradiction_example();
|
|
|
|
println!();
|
|
|
|
// Example 3: Episodic memory tracking
|
|
println!("--- Example 3: Episodic Memory (Conversation History) ---");
|
|
run_episodic_example();
|
|
|
|
println!();
|
|
|
|
// Example 4: Query related memories
|
|
println!("--- Example 4: Finding Related Memories ---");
|
|
run_related_memory_example();
|
|
}
|
|
|
|
#[cfg(not(feature = "ruvllm"))]
|
|
fn main() {
|
|
println!("This example requires the 'ruvllm' feature.");
|
|
println!("Run with: cargo run --example memory_tracking --features ruvllm");
|
|
}
|
|
|
|
#[cfg(feature = "ruvllm")]
|
|
fn run_basic_memory_example() {
|
|
// Configure the memory layer
|
|
let config = MemoryCoherenceConfig {
|
|
embedding_dim: 8, // Small dimension for demo
|
|
coherence_threshold: 0.5,
|
|
auto_semantic_edges: true,
|
|
semantic_similarity_threshold: 0.7,
|
|
auto_hierarchical_edges: true,
|
|
max_semantic_edges: 3,
|
|
};
|
|
|
|
let mut layer = MemoryCoherenceLayer::with_config(config);
|
|
|
|
println!("Creating MemoryCoherenceLayer with:");
|
|
println!(" Embedding dimension: 8");
|
|
println!(" Coherence threshold: 0.5");
|
|
println!();
|
|
|
|
// Add an agentic (long-term) memory
|
|
let pattern_embedding = vec![1.0, 0.0, 0.5, 0.0, 0.3, 0.0, 0.1, 0.0];
|
|
let entry = MemoryEntry::new(
|
|
"user_prefers_concise",
|
|
pattern_embedding,
|
|
MemoryType::Agentic,
|
|
);
|
|
|
|
println!("Adding agentic memory: 'user_prefers_concise'");
|
|
let result = layer
|
|
.add_with_coherence(entry)
|
|
.expect("Failed to add memory");
|
|
|
|
println!(" Memory ID: {}", result.memory_id);
|
|
println!(" Node ID: {}", result.node_id);
|
|
println!(" Is coherent: {}", result.is_coherent);
|
|
println!(" Total energy: {:.6}", result.energy);
|
|
println!(" Edges created: {}", result.edges_created.len());
|
|
println!();
|
|
|
|
// Add working (current context) memory
|
|
let context_embedding = vec![0.9, 0.1, 0.4, 0.1, 0.2, 0.1, 0.0, 0.1];
|
|
let context = MemoryEntry::new("current_topic_rust", context_embedding, MemoryType::Working);
|
|
|
|
println!("Adding working memory: 'current_topic_rust'");
|
|
let result2 = layer
|
|
.add_with_coherence(context)
|
|
.expect("Failed to add memory");
|
|
|
|
println!(" Memory ID: {}", result2.memory_id);
|
|
println!(" Is coherent: {}", result2.is_coherent);
|
|
println!(" Local energy: {:.6}", result2.local_energy);
|
|
println!();
|
|
|
|
// Check overall coherence
|
|
println!("Memory System State:");
|
|
println!(" Total memories: {}", layer.memory_count());
|
|
println!(" Overall energy: {:.6}", layer.compute_energy());
|
|
println!(" System coherent: {}", layer.is_coherent());
|
|
}
|
|
|
|
#[cfg(feature = "ruvllm")]
|
|
fn run_contradiction_example() {
|
|
let config = MemoryCoherenceConfig {
|
|
embedding_dim: 4,
|
|
coherence_threshold: 0.3, // Strict threshold
|
|
auto_semantic_edges: true,
|
|
semantic_similarity_threshold: 0.5,
|
|
auto_hierarchical_edges: false,
|
|
max_semantic_edges: 5,
|
|
};
|
|
|
|
let mut layer = MemoryCoherenceLayer::with_config(config);
|
|
|
|
println!("Setting up contradiction detection scenario...");
|
|
println!(" Coherence threshold: 0.3 (strict)");
|
|
println!();
|
|
|
|
// Add a fact about user preference
|
|
let pref_a = vec![1.0, 0.0, 0.0, 0.0];
|
|
let entry_a = MemoryEntry::new("user_likes_verbose", pref_a, MemoryType::Agentic);
|
|
layer
|
|
.add_with_coherence(entry_a)
|
|
.expect("Failed to add memory A");
|
|
println!("Added: 'user_likes_verbose' [1.0, 0.0, 0.0, 0.0]");
|
|
|
|
// Add a CONTRADICTORY fact
|
|
let pref_b = vec![-1.0, 0.0, 0.0, 0.0]; // Opposite direction!
|
|
let entry_b = MemoryEntry::new("user_likes_concise", pref_b, MemoryType::Agentic);
|
|
|
|
println!("Adding potentially contradictory memory...");
|
|
println!(" 'user_likes_concise' [-1.0, 0.0, 0.0, 0.0]");
|
|
println!();
|
|
|
|
let result = layer
|
|
.add_with_coherence(entry_b)
|
|
.expect("Failed to add memory B");
|
|
|
|
println!("Contradiction Detection Result:");
|
|
println!(" Is coherent: {}", result.is_coherent);
|
|
println!(" Local energy: {:.6}", result.local_energy);
|
|
println!(" Total system energy: {:.6}", result.energy);
|
|
|
|
if !result.is_coherent {
|
|
println!();
|
|
println!(" WARNING: Memory contradiction detected!");
|
|
println!(
|
|
" Conflicting memories: {} found",
|
|
result.conflicting_memories.len()
|
|
);
|
|
|
|
for conflict_id in &result.conflicting_memories {
|
|
println!(" - Conflicts with: {}", conflict_id);
|
|
}
|
|
|
|
println!();
|
|
println!(" In a real system, you might:");
|
|
println!(" - Ask for clarification");
|
|
println!(" - Prefer the newer memory");
|
|
println!(" - Mark as uncertain/needs-resolution");
|
|
}
|
|
|
|
// Find all incoherent memories
|
|
println!();
|
|
println!("Finding all incoherent memories in the system:");
|
|
let incoherent = layer.find_incoherent_memories();
|
|
for (memory_id, energy) in &incoherent {
|
|
println!(" Memory {}: energy = {:.6}", memory_id, energy);
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ruvllm")]
|
|
fn run_episodic_example() {
|
|
let config = MemoryCoherenceConfig {
|
|
embedding_dim: 4,
|
|
coherence_threshold: 0.5,
|
|
auto_semantic_edges: true,
|
|
semantic_similarity_threshold: 0.6,
|
|
auto_hierarchical_edges: false,
|
|
max_semantic_edges: 2,
|
|
};
|
|
|
|
let mut layer = MemoryCoherenceLayer::with_config(config);
|
|
|
|
println!("Simulating a conversation with episodic memory...");
|
|
println!();
|
|
|
|
// Simulate conversation turns
|
|
let turns = [
|
|
("user_asks_about_rust", vec![1.0, 0.5, 0.0, 0.0]),
|
|
("assistant_explains_ownership", vec![0.9, 0.6, 0.1, 0.0]),
|
|
("user_asks_about_borrowing", vec![0.8, 0.5, 0.3, 0.0]),
|
|
("assistant_explains_references", vec![0.85, 0.55, 0.25, 0.1]),
|
|
("user_thanks", vec![0.2, 0.1, 0.0, 0.9]),
|
|
];
|
|
|
|
for (i, (key, embedding)) in turns.iter().enumerate() {
|
|
let (memory_id, sequence) = layer
|
|
.add_episode(key, embedding)
|
|
.expect("Failed to add episode");
|
|
|
|
println!(
|
|
"Turn {}: {} (seq: {}, id: {})",
|
|
i + 1,
|
|
key,
|
|
sequence,
|
|
memory_id
|
|
);
|
|
}
|
|
|
|
println!();
|
|
println!("Episodic Memory State:");
|
|
println!(" Current sequence: {}", layer.current_sequence());
|
|
println!(" Total memories: {}", layer.memory_count());
|
|
println!(" System coherent: {}", layer.is_coherent());
|
|
println!();
|
|
|
|
// Query recent episodes
|
|
println!("Recent 3 episodes:");
|
|
for (seq, embedding) in layer.recent_episodes(3) {
|
|
println!(
|
|
" Sequence {}: [{:.2}, {:.2}, {:.2}, {:.2}]",
|
|
seq, embedding[0], embedding[1], embedding[2], embedding[3]
|
|
);
|
|
}
|
|
|
|
println!();
|
|
|
|
// Query range
|
|
println!("Episodes in range 2-4:");
|
|
for (seq, embedding) in layer.episodes_in_range(2, 5) {
|
|
println!(
|
|
" Sequence {}: [{:.2}, {:.2}, {:.2}, {:.2}]",
|
|
seq, embedding[0], embedding[1], embedding[2], embedding[3]
|
|
);
|
|
}
|
|
|
|
println!();
|
|
|
|
// Get specific episode
|
|
if let Some(episode_2) = layer.get_episode(2) {
|
|
println!(
|
|
"Episode 2 specifically: [{:.2}, {:.2}, {:.2}, {:.2}]",
|
|
episode_2[0], episode_2[1], episode_2[2], episode_2[3]
|
|
);
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ruvllm")]
|
|
fn run_related_memory_example() {
|
|
let config = MemoryCoherenceConfig {
|
|
embedding_dim: 4,
|
|
coherence_threshold: 0.5,
|
|
auto_semantic_edges: true,
|
|
semantic_similarity_threshold: 0.6,
|
|
auto_hierarchical_edges: true,
|
|
max_semantic_edges: 3,
|
|
};
|
|
|
|
let mut layer = MemoryCoherenceLayer::with_config(config);
|
|
|
|
println!("Building a knowledge base with interconnected memories...");
|
|
println!();
|
|
|
|
// Add agentic patterns (general knowledge)
|
|
let patterns = [
|
|
("pattern_programming", vec![1.0, 0.0, 0.0, 0.0]),
|
|
("pattern_web_dev", vec![0.5, 0.5, 0.0, 0.0]),
|
|
("pattern_databases", vec![0.0, 1.0, 0.0, 0.0]),
|
|
];
|
|
|
|
for (key, emb) in &patterns {
|
|
layer
|
|
.store_pattern(key, emb)
|
|
.expect("Failed to store pattern");
|
|
println!("Stored pattern: {}", key);
|
|
}
|
|
|
|
println!();
|
|
|
|
// Add working context related to programming
|
|
let context_emb = vec![0.9, 0.1, 0.0, 0.0]; // Close to "programming"
|
|
layer
|
|
.set_context("current_focus", &context_emb)
|
|
.expect("Failed to set context");
|
|
println!("Set current context: 'current_focus' (similar to programming)");
|
|
println!();
|
|
|
|
// Add an episode related to databases
|
|
let episode_emb = vec![0.1, 0.95, 0.0, 0.0]; // Close to "databases"
|
|
layer
|
|
.add_episode("discussed_sql", &episode_emb)
|
|
.expect("Failed to add episode");
|
|
println!("Added episode: 'discussed_sql' (similar to databases)");
|
|
println!();
|
|
|
|
// Check system state
|
|
println!("Memory System Analysis:");
|
|
println!(" Total memories: {}", layer.memory_count());
|
|
println!(" Overall energy: {:.6}", layer.compute_energy());
|
|
println!(" System coherent: {}", layer.is_coherent());
|
|
println!();
|
|
|
|
// List all patterns
|
|
println!("Stored patterns:");
|
|
for key in layer.pattern_keys() {
|
|
println!(" - {}", key);
|
|
}
|
|
|
|
println!();
|
|
|
|
// List all context
|
|
println!("Working context:");
|
|
for key in layer.context_keys() {
|
|
if let Some(emb) = layer.get_context(&key) {
|
|
println!(
|
|
" - {}: [{:.2}, {:.2}, {:.2}, {:.2}]",
|
|
key, emb[0], emb[1], emb[2], emb[3]
|
|
);
|
|
}
|
|
}
|
|
|
|
println!();
|
|
|
|
// Find memories that might be incoherent
|
|
let incoherent = layer.find_incoherent_memories();
|
|
if incoherent.is_empty() {
|
|
println!("All memories are coherent!");
|
|
} else {
|
|
println!("Incoherent memories found:");
|
|
for (id, energy) in &incoherent {
|
|
println!(" - {}: energy = {:.6}", id, energy);
|
|
}
|
|
}
|
|
|
|
println!();
|
|
println!("The memory layer automatically creates edges between:");
|
|
println!(" - Semantically similar memories (via embedding similarity)");
|
|
println!(" - Working/Episodic memories and related Agentic patterns (hierarchical)");
|
|
println!(" - Consecutive episodic memories (temporal sequence)");
|
|
println!();
|
|
println!("These edges enable coherence checking across the entire memory graph.");
|
|
}
|