prime-radiant 0.1.0

Universal coherence engine using sheaf Laplacian mathematics for AI safety, hallucination detection, and structural consistency verification in LLMs and distributed systems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! SheafNode: Entity with fixed-dimensional state vector
//!
//! A node in the sheaf graph represents an entity carrying a state vector (the "stalk"
//! of the sheaf). Nodes are domain-agnostic and can represent:
//!
//! - Facts, hypotheses, beliefs (AI agents)
//! - Trades, positions, signals (finance)
//! - Vitals, diagnoses, treatments (medical)
//! - Sensor readings, goals, plans (robotics)

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// Unique identifier for a node
pub type NodeId = Uuid;

/// State vector type - fixed-dimensional f32 vector
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateVector {
    /// The raw vector data
    data: Vec<f32>,
    /// Dimensionality (cached for fast access)
    dim: usize,
}

impl StateVector {
    /// Create a new state vector from a slice
    #[inline]
    pub fn new(data: impl Into<Vec<f32>>) -> Self {
        let data = data.into();
        let dim = data.len();
        Self { data, dim }
    }

    /// Create a zero vector of given dimension
    #[inline]
    pub fn zeros(dim: usize) -> Self {
        Self {
            data: vec![0.0; dim],
            dim,
        }
    }

    /// Create a random unit vector (useful for initialization)
    pub fn random_unit(dim: usize) -> Self {
        use rand::Rng;
        let mut rng = rand::thread_rng();
        let mut data: Vec<f32> = (0..dim).map(|_| rng.gen::<f32>() - 0.5).collect();

        // Normalize to unit length
        let norm: f32 = data.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm > 1e-10 {
            for x in &mut data {
                *x /= norm;
            }
        }

        Self { data, dim }
    }

    /// Get the dimension of the vector
    #[inline]
    pub fn dim(&self) -> usize {
        self.dim
    }

    /// Get the raw data as a slice
    #[inline]
    pub fn as_slice(&self) -> &[f32] {
        &self.data
    }

    /// Get the raw data as a mutable slice
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [f32] {
        &mut self.data
    }

    /// Compute L2 norm squared (for energy calculations)
    ///
    /// SIMD-optimized: Uses chunks_exact for proper auto-vectorization.
    #[inline]
    pub fn norm_squared(&self) -> f32 {
        // Process 4 elements at a time for auto-vectorization
        let chunks = self.data.chunks_exact(4);
        let remainder = chunks.remainder();

        let mut acc = [0.0f32; 4];
        for chunk in chunks {
            acc[0] += chunk[0] * chunk[0];
            acc[1] += chunk[1] * chunk[1];
            acc[2] += chunk[2] * chunk[2];
            acc[3] += chunk[3] * chunk[3];
        }

        let mut sum = acc[0] + acc[1] + acc[2] + acc[3];
        for &x in remainder {
            sum += x * x;
        }
        sum
    }

    /// Compute L2 norm
    #[inline]
    pub fn norm(&self) -> f32 {
        self.norm_squared().sqrt()
    }

    /// Compute dot product with another vector
    ///
    /// SIMD-optimized: Uses chunks_exact for proper auto-vectorization.
    #[inline]
    pub fn dot(&self, other: &Self) -> f32 {
        debug_assert_eq!(self.dim, other.dim, "Vector dimensions must match");

        // Process 4 elements at a time for auto-vectorization
        let chunks_a = self.data.chunks_exact(4);
        let chunks_b = other.data.chunks_exact(4);
        let remainder_a = chunks_a.remainder();
        let remainder_b = chunks_b.remainder();

        let mut acc = [0.0f32; 4];
        for (ca, cb) in chunks_a.zip(chunks_b) {
            acc[0] += ca[0] * cb[0];
            acc[1] += ca[1] * cb[1];
            acc[2] += ca[2] * cb[2];
            acc[3] += ca[3] * cb[3];
        }

        let mut sum = acc[0] + acc[1] + acc[2] + acc[3];
        for (&a, &b) in remainder_a.iter().zip(remainder_b.iter()) {
            sum += a * b;
        }
        sum
    }

    /// Subtract another vector (for residual calculation)
    ///
    /// SIMD-optimized: Processes elements in order for vectorization.
    #[inline]
    pub fn subtract(&self, other: &Self) -> Self {
        debug_assert_eq!(self.dim, other.dim, "Vector dimensions must match");

        let data: Vec<f32> = self
            .data
            .iter()
            .zip(other.data.iter())
            .map(|(&a, &b)| a - b)
            .collect();

        Self {
            data,
            dim: self.dim,
        }
    }

    /// Add another vector
    #[inline]
    pub fn add(&self, other: &Self) -> Self {
        debug_assert_eq!(self.dim, other.dim, "Vector dimensions must match");

        let data: Vec<f32> = self
            .data
            .iter()
            .zip(other.data.iter())
            .map(|(&a, &b)| a + b)
            .collect();

        Self {
            data,
            dim: self.dim,
        }
    }

    /// Scale the vector
    #[inline]
    pub fn scale(&self, factor: f32) -> Self {
        let data: Vec<f32> = self.data.iter().map(|&x| x * factor).collect();
        Self {
            data,
            dim: self.dim,
        }
    }

    /// Update the vector in place (for incremental updates)
    #[inline]
    pub fn update(&mut self, new_data: &[f32]) {
        debug_assert_eq!(new_data.len(), self.dim, "Update must match dimension");
        self.data.copy_from_slice(new_data);
    }

    /// Compute hash for fingerprinting (using Blake3 would be better but keep it simple)
    pub fn content_hash(&self) -> u64 {
        use std::hash::{Hash, Hasher};
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        for &x in &self.data {
            x.to_bits().hash(&mut hasher);
        }
        hasher.finish()
    }
}

impl From<Vec<f32>> for StateVector {
    fn from(data: Vec<f32>) -> Self {
        Self::new(data)
    }
}

impl From<&[f32]> for StateVector {
    fn from(data: &[f32]) -> Self {
        Self::new(data.to_vec())
    }
}

impl AsRef<[f32]> for StateVector {
    fn as_ref(&self) -> &[f32] {
        &self.data
    }
}

/// Metadata associated with a node
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NodeMetadata {
    /// Human-readable label/name
    pub label: Option<String>,
    /// Node type for filtering (e.g., "fact", "hypothesis", "belief")
    pub node_type: Option<String>,
    /// Namespace/scope for multi-tenant isolation
    pub namespace: Option<String>,
    /// Tags for categorization
    pub tags: Vec<String>,
    /// Arbitrary key-value properties
    pub properties: HashMap<String, serde_json::Value>,
    /// Source/provenance information
    pub source: Option<String>,
    /// Confidence score (0.0-1.0) if applicable
    pub confidence: Option<f32>,
}

impl NodeMetadata {
    /// Create empty metadata
    pub fn new() -> Self {
        Self::default()
    }

    /// Create metadata with a label
    pub fn with_label(label: impl Into<String>) -> Self {
        Self {
            label: Some(label.into()),
            ..Default::default()
        }
    }

    /// Check if node belongs to a namespace
    pub fn in_namespace(&self, namespace: &str) -> bool {
        self.namespace.as_deref() == Some(namespace)
    }

    /// Check if node has a specific tag
    pub fn has_tag(&self, tag: &str) -> bool {
        self.tags.iter().any(|t| t == tag)
    }
}

/// A node in the sheaf graph carrying a fixed-dimensional state vector
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SheafNode {
    /// Unique node identifier
    pub id: NodeId,
    /// Fixed-dimensional state vector (stalk of the sheaf)
    pub state: StateVector,
    /// Metadata for filtering and governance
    pub metadata: NodeMetadata,
    /// Timestamp of creation
    pub created_at: DateTime<Utc>,
    /// Timestamp of last state update
    pub updated_at: DateTime<Utc>,
    /// Version counter for optimistic concurrency
    pub version: u64,
}

impl SheafNode {
    /// Create a new sheaf node with the given state vector
    pub fn new(state: StateVector) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            state,
            metadata: NodeMetadata::default(),
            created_at: now,
            updated_at: now,
            version: 1,
        }
    }

    /// Create a new node with a specific ID
    pub fn with_id(id: NodeId, state: StateVector) -> Self {
        let now = Utc::now();
        Self {
            id,
            state,
            metadata: NodeMetadata::default(),
            created_at: now,
            updated_at: now,
            version: 1,
        }
    }

    /// Get the dimension of the node's state vector
    #[inline]
    pub fn dim(&self) -> usize {
        self.state.dim()
    }

    /// Update the state vector
    ///
    /// Increments version and updates timestamp.
    pub fn update_state(&mut self, new_state: StateVector) {
        debug_assert_eq!(
            new_state.dim(),
            self.state.dim(),
            "State dimension must not change"
        );
        self.state = new_state;
        self.updated_at = Utc::now();
        self.version += 1;
    }

    /// Update the state vector in place from a slice
    pub fn update_state_from_slice(&mut self, data: &[f32]) {
        self.state.update(data);
        self.updated_at = Utc::now();
        self.version += 1;
    }

    /// Compute a content hash for fingerprinting
    pub fn content_hash(&self) -> u64 {
        use std::hash::{Hash, Hasher};
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        self.id.hash(&mut hasher);
        hasher.write_u64(self.state.content_hash());
        hasher.write_u64(self.version);
        hasher.finish()
    }

    /// Check if node is stale (state hasn't been updated since cutoff)
    pub fn is_stale(&self, cutoff: DateTime<Utc>) -> bool {
        self.updated_at < cutoff
    }
}

/// Builder for constructing SheafNode instances
#[derive(Debug, Default)]
pub struct SheafNodeBuilder {
    id: Option<NodeId>,
    state: Option<StateVector>,
    metadata: NodeMetadata,
}

impl SheafNodeBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the node ID
    pub fn id(mut self, id: NodeId) -> Self {
        self.id = Some(id);
        self
    }

    /// Set the state vector
    pub fn state(mut self, state: impl Into<StateVector>) -> Self {
        self.state = Some(state.into());
        self
    }

    /// Set the state from a slice
    pub fn state_from_slice(mut self, data: &[f32]) -> Self {
        self.state = Some(StateVector::new(data.to_vec()));
        self
    }

    /// Set a zero state of given dimension
    pub fn zero_state(mut self, dim: usize) -> Self {
        self.state = Some(StateVector::zeros(dim));
        self
    }

    /// Set a random unit state of given dimension
    pub fn random_state(mut self, dim: usize) -> Self {
        self.state = Some(StateVector::random_unit(dim));
        self
    }

    /// Set the label
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.metadata.label = Some(label.into());
        self
    }

    /// Set the node type
    pub fn node_type(mut self, node_type: impl Into<String>) -> Self {
        self.metadata.node_type = Some(node_type.into());
        self
    }

    /// Set the namespace
    pub fn namespace(mut self, namespace: impl Into<String>) -> Self {
        self.metadata.namespace = Some(namespace.into());
        self
    }

    /// Add a tag
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.metadata.tags.push(tag.into());
        self
    }

    /// Add multiple tags
    pub fn tags(mut self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self {
        for tag in tags {
            self.metadata.tags.push(tag.into());
        }
        self
    }

    /// Set a property
    pub fn property(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        self.metadata.properties.insert(key.into(), value.into());
        self
    }

    /// Set the source
    pub fn source(mut self, source: impl Into<String>) -> Self {
        self.metadata.source = Some(source.into());
        self
    }

    /// Set the confidence
    pub fn confidence(mut self, confidence: f32) -> Self {
        self.metadata.confidence = Some(confidence.clamp(0.0, 1.0));
        self
    }

    /// Build the node
    ///
    /// # Panics
    ///
    /// Panics if no state vector was provided.
    pub fn build(self) -> SheafNode {
        let state = self.state.expect("State vector is required");
        let now = Utc::now();

        SheafNode {
            id: self.id.unwrap_or_else(Uuid::new_v4),
            state,
            metadata: self.metadata,
            created_at: now,
            updated_at: now,
            version: 1,
        }
    }

    /// Try to build the node, returning an error if state is missing
    pub fn try_build(self) -> Result<SheafNode, &'static str> {
        let state = self.state.ok_or("State vector is required")?;
        let now = Utc::now();

        Ok(SheafNode {
            id: self.id.unwrap_or_else(Uuid::new_v4),
            state,
            metadata: self.metadata,
            created_at: now,
            updated_at: now,
            version: 1,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_state_vector_creation() {
        let v = StateVector::new(vec![1.0, 2.0, 3.0]);
        assert_eq!(v.dim(), 3);
        assert_eq!(v.as_slice(), &[1.0, 2.0, 3.0]);
    }

    #[test]
    fn test_state_vector_zeros() {
        let v = StateVector::zeros(5);
        assert_eq!(v.dim(), 5);
        assert!(v.as_slice().iter().all(|&x| x == 0.0));
    }

    #[test]
    fn test_state_vector_norm() {
        let v = StateVector::new(vec![3.0, 4.0]);
        assert!((v.norm() - 5.0).abs() < 1e-6);
        assert!((v.norm_squared() - 25.0).abs() < 1e-6);
    }

    #[test]
    fn test_state_vector_dot() {
        let a = StateVector::new(vec![1.0, 2.0, 3.0]);
        let b = StateVector::new(vec![4.0, 5.0, 6.0]);
        assert!((a.dot(&b) - 32.0).abs() < 1e-6);
    }

    #[test]
    fn test_state_vector_subtract() {
        let a = StateVector::new(vec![5.0, 10.0]);
        let b = StateVector::new(vec![2.0, 3.0]);
        let c = a.subtract(&b);
        assert_eq!(c.as_slice(), &[3.0, 7.0]);
    }

    #[test]
    fn test_state_vector_scale() {
        let v = StateVector::new(vec![1.0, 2.0, 3.0]);
        let scaled = v.scale(2.0);
        assert_eq!(scaled.as_slice(), &[2.0, 4.0, 6.0]);
    }

    #[test]
    fn test_node_builder() {
        let node = SheafNodeBuilder::new()
            .state_from_slice(&[1.0, 2.0, 3.0])
            .label("test_node")
            .node_type("fact")
            .namespace("test")
            .tag("important")
            .confidence(0.95)
            .build();

        assert_eq!(node.dim(), 3);
        assert_eq!(node.metadata.label, Some("test_node".to_string()));
        assert_eq!(node.metadata.node_type, Some("fact".to_string()));
        assert_eq!(node.metadata.namespace, Some("test".to_string()));
        assert!(node.metadata.has_tag("important"));
        assert_eq!(node.metadata.confidence, Some(0.95));
    }

    #[test]
    fn test_node_update_state() {
        let mut node = SheafNode::new(StateVector::new(vec![1.0, 2.0]));
        let old_version = node.version;
        let old_updated = node.updated_at;

        std::thread::sleep(std::time::Duration::from_millis(1));
        node.update_state(StateVector::new(vec![3.0, 4.0]));

        assert_eq!(node.version, old_version + 1);
        assert!(node.updated_at > old_updated);
        assert_eq!(node.state.as_slice(), &[3.0, 4.0]);
    }

    #[test]
    fn test_node_content_hash() {
        let node1 = SheafNodeBuilder::new()
            .id(Uuid::new_v4())
            .state_from_slice(&[1.0, 2.0])
            .build();

        let node2 = SheafNodeBuilder::new()
            .id(node1.id)
            .state_from_slice(&[1.0, 2.0])
            .build();

        // Same content should produce same hash (version may differ slightly)
        // This is a simple check - in practice we'd use a proper content hash
        assert_eq!(node1.state.content_hash(), node2.state.content_hash());
    }

    #[test]
    fn test_random_unit_vector() {
        let v = StateVector::random_unit(100);
        assert_eq!(v.dim(), 100);
        // Should be approximately unit length
        assert!((v.norm() - 1.0).abs() < 0.01);
    }
}