| Crates.io | prime-radiant |
| lib.rs | prime-radiant |
| version | 0.1.0 |
| created_at | 2026-01-23 02:24:14.163327+00 |
| updated_at | 2026-01-23 02:24:14.163327+00 |
| description | Universal coherence engine using sheaf Laplacian mathematics for AI safety, hallucination detection, and structural consistency verification in LLMs and distributed systems |
| homepage | https://ruv.io/ruvector |
| repository | https://github.com/ruvnet/ruvector |
| max_upload_size | |
| id | 2063188 |
| size | 2,146,318 |
A Real-Time Coherence Gate for Autonomous Systems
Prime-Radiant is infrastructure for AI safety — a mathematical gate that proves whether a system's beliefs, facts, and claims are internally consistent before allowing action.
Instead of asking "How confident am I?" (which can be wrong), Prime-Radiant asks "Are there any contradictions?" — and provides mathematical proof of the answer.
┌─────────────────────────────────────────────────────────────────┐
│ "The meeting is at 3pm" ←──────→ "The meeting is at 4pm" │
│ (Memory A) ✗ (Memory B) │
│ │
│ Energy = 0.92 → HIGH INCOHERENCE → Block / Escalate │
└─────────────────────────────────────────────────────────────────┘
Imagine you have an AI assistant that:
The problem: These pieces can contradict each other. The AI might confidently say something that conflicts with facts it just retrieved. Traditional systems can't detect this reliably.
Prime-Radiant's solution: Model everything as a graph where:
| Traditional AI | Prime-Radiant |
|---|---|
| "I'm 85% confident" | "Zero contradictions found" |
| Can be confidently wrong | Knows when it doesn't know |
| Guesses about the future | Proves consistency right now |
| Trust the model | Trust the math |
Prime-Radiant is built on Sheaf Laplacian mathematics — a rigorous framework for measuring consistency across interconnected data.
E(S) = Σ wₑ · ‖ρᵤ(xᵤ) - ρᵥ(xᵥ)‖²
e∈E
Where:
Node A: "Meeting at 3pm" → embedding: [0.9, 0.1, 0.0]
Node B: "Meeting at 4pm" → embedding: [0.1, 0.9, 0.0]
Edge A→B: Identity map (they should match)
Residual = ρ(A) - ρ(B) = [0.9, 0.1, 0.0] - [0.1, 0.9, 0.0] = [0.8, -0.8, 0.0]
Energy = ‖residual‖² = 0.8² + 0.8² + 0² = 1.28
Threshold (Heavy lane) = 0.4
1.28 > 0.4 → Route to Human review
One line of arithmetic. The contradiction is now a number. The gate has a decision.
Restriction maps encode how information should relate across edges:
| Map Type | Formula | Use Case |
|---|---|---|
| Identity | ρ(x) = x | Direct comparison |
| Diagonal | ρ(x) = diag(d) · x | Weighted dimensions |
| Projection | ρ(x) = P · x | Dimensionality reduction |
| Dense | ρ(x) = A · x + b | Learned transformations |
| Sparse | ρ(x) = S · x | Efficient large-scale |
Low Energy (Coherent) High Energy (Incoherent)
✓ ✗
Fact A ←→ Fact B Fact A ←→ Fact B
↓ ↓ ↓ ✗ ↓
Claim C ←→ Claim D Claim C ←✗→ Claim D
"Everything agrees" "Contradictions detected"
→ Safe to act → Stop, escalate, or refuse
Based on coherence energy, actions are routed to appropriate compute lanes:
┌─────────────────────────────────────────────────────────────────┐
│ Energy │ Lane │ Latency │ Action │
├──────────┼─────────────┼──────────┼─────────────────────────────┤
│ < 0.1 │ Reflex │ < 1ms │ Immediate approval │
│ 0.1-0.4 │ Retrieval │ ~10ms │ Fetch more evidence │
│ 0.4-0.7 │ Heavy │ ~100ms │ Deep analysis │
│ > 0.7 │ Human │ async │ Escalate to human review │
└─────────────────────────────────────────────────────────────────┘
Every decision creates an immutable audit trail:
Specialized layer for LLM coherence checking:
Add to your Cargo.toml:
[dependencies]
# Core coherence engine
prime-radiant = "0.1"
# With LLM integration
prime-radiant = { version = "0.1", features = ["ruvllm"] }
# With GPU acceleration
prime-radiant = { version = "0.1", features = ["gpu"] }
# With SIMD optimizations
prime-radiant = { version = "0.1", features = ["simd"] }
# Everything
prime-radiant = { version = "0.1", features = ["full"] }
use prime_radiant::{
substrate::{SheafGraph, SheafNodeBuilder, SheafEdgeBuilder},
coherence::CoherenceEngine,
execution::{CoherenceGate, PolicyBundleRef},
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a graph of related facts
let graph = SheafGraph::new();
// Add nodes with state vectors (embeddings)
let fact_a = graph.add_node(
SheafNodeBuilder::new()
.state_from_slice(&[1.0, 0.0, 0.0])
.namespace("knowledge")
.metadata("source", "database")
.build()
);
let fact_b = graph.add_node(
SheafNodeBuilder::new()
.state_from_slice(&[0.95, 0.05, 0.0]) // Similar to fact_a
.namespace("knowledge")
.build()
);
// Add edge with identity restriction (they should match)
graph.add_edge(
SheafEdgeBuilder::new(fact_a, fact_b)
.identity_restrictions(3)
.weight(1.0)
.namespace("knowledge")
.build()
);
// Compute coherence energy
let energy = graph.compute_energy();
println!("Total energy: {:.4}", energy.total_energy);
println!("Is coherent: {}", energy.is_coherent(0.1));
// Gate a decision based on energy
let policy = PolicyBundleRef::placeholder();
let mut gate = CoherenceGate::with_defaults(policy);
let decision = gate.evaluate_energy(energy.total_energy);
println!("Decision: {:?}", decision.lane);
println!("Allowed: {}", decision.allow);
Ok(())
}
use prime_radiant::ruvllm_integration::{
SheafCoherenceValidator, ValidationContext, ValidatorConfig,
EdgeWeights,
};
async fn validate_response(
context_embedding: Vec<f32>,
response_embedding: Vec<f32>,
retrieved_facts: Vec<Vec<f32>>,
) -> Result<bool, Box<dyn std::error::Error>> {
// Create validator with custom thresholds
let config = ValidatorConfig {
coherence_threshold: 0.3,
max_edges_per_claim: 10,
..Default::default()
};
let validator = SheafCoherenceValidator::new(config);
// Build validation context
let context = ValidationContext::builder()
.context_embedding(context_embedding)
.response_embedding(response_embedding)
.supporting_facts(retrieved_facts)
.edge_weights(EdgeWeights::default())
.build();
// Validate
let result = validator.validate(&context)?;
println!("Energy: {:.4}", result.energy);
println!("Coherent: {}", result.is_coherent);
println!("Witness ID: {}", result.witness.id);
if !result.is_coherent {
println!("Incoherent claims: {:?}", result.incoherent_edges);
}
Ok(result.is_coherent)
}
use prime_radiant::ruvllm_integration::{
MemoryCoherenceLayer, MemoryCoherenceConfig, MemoryEntry, MemoryType,
};
fn track_conversation_memory() -> Result<(), Box<dyn std::error::Error>> {
let config = MemoryCoherenceConfig {
similarity_threshold: 0.7,
max_memories: 1000,
..Default::default()
};
let mut memory = MemoryCoherenceLayer::new(config);
// Add first memory
let entry1 = MemoryEntry {
id: "mem_1".into(),
memory_type: MemoryType::Working,
embedding: vec![1.0, 0.0, 0.0],
content: "User prefers morning meetings".into(),
timestamp: chrono::Utc::now(),
};
memory.add_with_coherence(entry1)?;
// Add potentially conflicting memory
let entry2 = MemoryEntry {
id: "mem_2".into(),
memory_type: MemoryType::Working,
embedding: vec![-0.9, 0.1, 0.0], // Opposite direction!
content: "User prefers evening meetings".into(),
timestamp: chrono::Utc::now(),
};
let result = memory.add_with_coherence(entry2)?;
if !result.coherent {
println!("Contradiction detected!");
println!("Conflicts with: {:?}", result.conflicts);
println!("Energy: {:.4}", result.energy);
}
Ok(())
}
use prime_radiant::ruvllm_integration::{
CoherenceConfidence, ConfidenceLevel,
};
fn interpret_energy(energy: f32) {
let confidence = CoherenceConfidence::default();
let score = confidence.from_energy(energy);
println!("Confidence: {:.1}%", score.value * 100.0);
println!("Level: {:?}", score.level);
println!("Explanation: {}", score.explanation);
match score.level {
ConfidenceLevel::VeryHigh => println!("Safe to proceed automatically"),
ConfidenceLevel::High => println!("Proceed with logging"),
ConfidenceLevel::Moderate => println!("Consider additional verification"),
ConfidenceLevel::Low => println!("Recommend human review"),
ConfidenceLevel::VeryLow => println!("Block action, require escalation"),
}
}
| Operation | Latency | Throughput |
|---|---|---|
| Single residual | < 1μs | 1M+ ops/sec |
| Graph energy (10K nodes) | < 10ms | 100 graphs/sec |
| Incremental update | < 100μs | 10K updates/sec |
| Gate evaluation | < 500μs | 2K decisions/sec |
Enable with --features simd:
use prime_radiant::simd::{
dot_product_simd, norm_squared_simd, batch_residuals_simd,
};
// Automatic CPU feature detection
let width = prime_radiant::simd::best_simd_width();
println!("Using SIMD width: {:?}", width); // Avx512, Avx2, Sse42, or Scalar
// 4-8x speedup on vector operations
let dot = dot_product_simd(&a, &b);
let norm = norm_squared_simd(&v);
| SIMD Feature | Speedup | Platform |
|---|---|---|
| AVX-512 | 8-16x | Intel Xeon, AMD Zen4+ |
| AVX2 | 4-8x | Most modern x86_64 |
| SSE4.2 | 2-4x | Older x86_64 |
| NEON | 2-4x | ARM64 (Apple M1/M2, etc.) |
Enable with --features gpu:
use prime_radiant::gpu::{GpuCoherenceEngine, GpuConfig};
async fn gpu_compute() -> Result<(), Box<dyn std::error::Error>> {
// Initialize GPU (auto-detects best available)
let config = GpuConfig {
prefer_discrete: true,
max_buffer_size: 256 * 1024 * 1024, // 256MB
..Default::default()
};
let gpu_engine = GpuCoherenceEngine::new(&graph, config).await?;
// Compute on GPU (falls back to CPU if unavailable)
let energy = gpu_engine.compute_energy().await?;
println!("GPU Energy: {:.4}", energy.total_energy);
println!("Backend: {:?}", gpu_engine.backend()); // Vulkan, Metal, DX12, WebGPU
Ok(())
}
| GPU Backend | Supported Platforms |
|---|---|
| Vulkan | Linux, Windows, Android |
| Metal | macOS, iOS |
| DX12 | Windows 10+ |
| WebGPU | Browsers (wasm32) |
GPU Kernels:
compute_residuals.wgsl — Parallel edge residual computationcompute_energy.wgsl — Reduction-based energy aggregationsheaf_attention.wgsl — Batched attention with energy weightingtoken_routing.wgsl — Parallel lane assignmentFast, thread-safe storage for development and testing:
use prime_radiant::storage::{InMemoryStorage, StorageConfig};
let storage = InMemoryStorage::new();
// Or with indexing for fast KNN search:
let indexed = IndexedInMemoryStorage::new();
Persistent storage with Write-Ahead Logging for durability:
use prime_radiant::storage::{FileStorage, StorageFormat};
let storage = FileStorage::new(
"./data/coherence.db",
StorageFormat::Bincode, // Or Json for debugging
)?;
Full ACID compliance with indexed queries:
# Cargo.toml
prime-radiant = { version = "0.1", features = ["postgres"] }
use prime_radiant::storage::PostgresStorage;
let storage = PostgresStorage::connect(
"postgres://user:pass@localhost/coherence"
).await?;
Schema includes:
policy_bundles — Versioned policies with approval trackingwitness_records — Hash-chained audit traillineage_records — Full graph modification historynode_states / edges — Graph storage with vector indexingA complete walkthrough of Prime-Radiant blocking a hallucinated response:
Step 1: RAG retrieves context
┌─────────────────────────────────────────────────────────┐
│ Retrieved Fact: "Company founded in 2019" │
│ Embedding: [0.82, 0.15, 0.03] │
└─────────────────────────────────────────────────────────┘
Step 2: LLM generates response
┌─────────────────────────────────────────────────────────┐
│ Generated Claim: "The company has 15 years of history" │
│ Embedding: [0.11, 0.85, 0.04] │
└─────────────────────────────────────────────────────────┘
Step 3: Prime-Radiant computes coherence
┌─────────────────────────────────────────────────────────┐
│ Edge: Fact → Claim (identity restriction) │
│ Residual: [0.82-0.11, 0.15-0.85, 0.03-0.04] │
│ = [0.71, -0.70, -0.01] │
│ Energy: = 0.71² + 0.70² + 0.01² = 0.996 │
└─────────────────────────────────────────────────────────┘
Step 4: Gate decision
┌─────────────────────────────────────────────────────────┐
│ Energy: 0.996 │
│ Threshold (Human): 0.7 │
│ Decision: BLOCK → Escalate to human review │
│ Witness ID: 7f3a...c921 (cryptographic proof) │
└─────────────────────────────────────────────────────────┘
The hallucination never reaches the user. The decision is auditable forever.
| Application | How It Works |
|---|---|
| LLM Anti-Hallucination | Gate responses when energy exceeds threshold |
| RAG Consistency | Verify retrieved context matches generated claims |
| Trading Throttles | Pause when market signals become structurally inconsistent |
| Compliance Proofs | Cryptographic witness for every automated decision |
| Application | How It Works |
|---|---|
| Autonomous Vehicles | Refuse motion when sensor/plan coherence breaks |
| Medical Monitoring | Escalate only on sustained diagnostic disagreement |
| Zero-Trust Security | Detect authorization graph inconsistencies |
The same math works everywhere — only the interpretation changes:
| Domain | Nodes | Edges | High Energy Means | Gate Action |
|---|---|---|---|---|
| AI Agents | Beliefs, facts | Citations | Hallucination | Refuse generation |
| Finance | Trades, positions | Arbitrage links | Regime change | Throttle trading |
| Medical | Vitals, diagnoses | Physiology | Clinical disagreement | Escalate to doctor |
| Robotics | Sensors, plans | Physics | Motion impossibility | Emergency stop |
| Security | Identities, permissions | Policy rules | Auth violation | Deny access |
| Feature | Description | Default |
|---|---|---|
default |
Core coherence engine | ✓ |
full |
All features enabled | |
simd |
SIMD-optimized operations | |
gpu |
GPU acceleration via wgpu | |
ruvllm |
LLM integration layer | |
postgres |
PostgreSQL storage backend | |
sona |
Self-optimizing threshold tuning | |
learned-rho |
GNN-learned restriction maps | |
hyperbolic |
Poincaré ball energy for hierarchies | |
distributed |
Raft-based multi-node coherence | |
attention |
Coherence-Gated Transformer attention |
┌─────────────────────────────────────────────────────────────────┐
│ APPLICATION LAYER │
│ LLM Guards │ Trading │ Medical │ Robotics │ Security │
├─────────────────────────────────────────────────────────────────┤
│ COHERENCE GATE │
│ Reflex (L0) │ Retrieval (L1) │ Heavy (L2) │ Human (L3) │
├─────────────────────────────────────────────────────────────────┤
│ COHERENCE COMPUTATION │
│ Residuals │ Energy Aggregation │ Spectral Analysis │
├─────────────────────────────────────────────────────────────────┤
│ ACCELERATION LAYER │
│ CPU (Scalar) │ SIMD (AVX/NEON) │ GPU (wgpu) │
├─────────────────────────────────────────────────────────────────┤
│ GOVERNANCE LAYER │
│ Policy Bundles │ Witnesses │ Lineage │ Threshold Tuning│
├─────────────────────────────────────────────────────────────────┤
│ KNOWLEDGE SUBSTRATE │
│ Sheaf Graph │ Nodes │ Edges │ Restriction Maps │
├─────────────────────────────────────────────────────────────────┤
│ STORAGE LAYER │
│ In-Memory │ File (WAL) │ PostgreSQL │
└─────────────────────────────────────────────────────────────────┘
// Graph primitives
SheafGraph // Thread-safe graph container
SheafNode // Node with state vector
SheafEdge // Edge with restriction maps
RestrictionMap // Linear transformation ρ(x) = Ax + b
// Energy computation
CoherenceEnergy // Energy breakdown by edge and scope
CoherenceEngine // Computation engine with caching
// Gating
CoherenceGate // Decision gate with compute ladder
GateDecision // Allow/deny with lane assignment
ComputeLane // Reflex, Retrieval, Heavy, Human
// Governance
PolicyBundle // Threshold configuration
WitnessRecord // Cryptographic audit entry
LineageRecord // Graph modification history
All major types support the builder pattern:
let node = SheafNodeBuilder::new()
.state_from_slice(&[1.0, 0.0, 0.0])
.namespace("facts")
.metadata("source", "api")
.metadata("confidence", "0.95")
.build();
let edge = SheafEdgeBuilder::new(source_id, target_id)
.dense_restriction(&matrix, &bias)
.weight(2.5)
.namespace("citations")
.build();
let policy = PolicyBundleBuilder::new("production-v1")
.with_threshold("default", ThresholdConfig::moderate())
.with_threshold("safety", ThresholdConfig::strict())
.with_required_approvals(2)
.with_approver(ApproverId::new("admin"))
.build();
In Isaac Asimov's Foundation series, the Prime Radiant is a device that displays the mathematical equations of psychohistory — allowing scientists to see how changes propagate through a complex system.
Similarly, this Prime-Radiant shows how consistency propagates (or breaks down) through your AI system's knowledge graph. It doesn't predict the future — it shows you where the present is coherent and where it isn't.
Prime-Radiant is not an LLM feature or a developer library. It is infrastructure — a coherence gate that sits beneath autonomous systems, ensuring they cannot act on contradictory beliefs.
Think of it as a circuit breaker for AI reasoning. When the math says "contradiction," the system stops. No probability. No guessing. Just structure.
This is the kind of primitive that agentic systems will need for the next decade.
MIT License - See LICENSE for details.
Prime-Radiant: A safety primitive for autonomous systems.
"Most systems try to get smarter by making better guesses.
Prime-Radiant takes a different route: systems that stay stable under uncertainty
by proving when the world still fits together — and when it does not."