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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! Energy History Tracking
//!
//! This module provides time-series tracking of coherence energy for trend analysis,
//! anomaly detection, and adaptive threshold tuning.
//!
//! # Features
//!
//! - Rolling window of energy snapshots
//! - Trend detection (increasing, decreasing, stable)
//! - Anomaly detection using statistical methods
//! - Persistence tracking for threshold tuning
//!
//! # Example
//!
//! ```rust,ignore
//! use prime_radiant::coherence::{EnergyHistory, EnergyHistoryConfig};
//!
//! let mut history = EnergyHistory::new(EnergyHistoryConfig::default());
//!
//! // Record energy values
//! history.record(1.0);
//! history.record(1.2);
//! history.record(1.5);
//!
//! // Get trend
//! let trend = history.trend();
//! println!("Energy is {:?}", trend.direction);
//! ```

use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

/// Configuration for energy history tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnergyHistoryConfig {
    /// Maximum number of entries to keep
    pub max_entries: usize,
    /// Window size for trend calculation
    pub trend_window: usize,
    /// Threshold for persistence detection (seconds)
    pub persistence_window_secs: u64,
    /// Number of standard deviations for anomaly detection
    pub anomaly_sigma: f32,
    /// Minimum entries before trend analysis
    pub min_entries: usize,
}

impl Default for EnergyHistoryConfig {
    fn default() -> Self {
        Self {
            max_entries: 1000,
            trend_window: 10,
            persistence_window_secs: 300, // 5 minutes
            anomaly_sigma: 3.0,
            min_entries: 5,
        }
    }
}

/// Direction of energy trend
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TrendDirection {
    /// Energy is increasing
    Increasing,
    /// Energy is decreasing
    Decreasing,
    /// Energy is relatively stable
    Stable,
    /// Not enough data to determine trend
    Unknown,
}

/// Result of trend analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnergyTrend {
    /// Direction of the trend
    pub direction: TrendDirection,
    /// Slope of the trend line (energy units per second)
    pub slope: f32,
    /// R-squared value indicating trend fit quality
    pub r_squared: f32,
    /// Average energy in the window
    pub mean: f32,
    /// Standard deviation in the window
    pub std_dev: f32,
    /// Window size used
    pub window_size: usize,
}

impl EnergyTrend {
    /// Check if the trend is concerning (increasing significantly)
    pub fn is_concerning(&self, threshold: f32) -> bool {
        self.direction == TrendDirection::Increasing && self.slope > threshold
    }

    /// Check if the trend is improving
    pub fn is_improving(&self) -> bool {
        self.direction == TrendDirection::Decreasing && self.r_squared > 0.5
    }
}

/// An entry in the energy history
#[derive(Debug, Clone, Serialize, Deserialize)]
struct HistoryEntry {
    /// Energy value
    energy: f32,
    /// Timestamp
    timestamp: DateTime<Utc>,
    /// Whether this was an anomaly
    is_anomaly: bool,
}

/// Time-series tracker for coherence energy
#[derive(Debug)]
pub struct EnergyHistory {
    /// Configuration
    config: EnergyHistoryConfig,
    /// History entries
    entries: VecDeque<HistoryEntry>,
    /// Running sum for efficient mean calculation
    running_sum: f64,
    /// Running sum of squares for efficient variance
    running_sum_sq: f64,
    /// Last computed trend
    last_trend: Option<EnergyTrend>,
    /// Statistics
    total_entries: u64,
    anomaly_count: u64,
}

impl EnergyHistory {
    /// Create a new energy history tracker
    pub fn new(config: EnergyHistoryConfig) -> Self {
        Self {
            config,
            entries: VecDeque::new(),
            running_sum: 0.0,
            running_sum_sq: 0.0,
            last_trend: None,
            total_entries: 0,
            anomaly_count: 0,
        }
    }

    /// Record a new energy value
    pub fn record(&mut self, energy: f32) {
        self.record_at(energy, Utc::now());
    }

    /// Record an energy value at a specific time
    pub fn record_at(&mut self, energy: f32, timestamp: DateTime<Utc>) {
        // Check for anomaly before updating stats
        let is_anomaly = self.is_anomaly(energy);
        if is_anomaly {
            self.anomaly_count += 1;
        }

        // Create entry
        let entry = HistoryEntry {
            energy,
            timestamp,
            is_anomaly,
        };

        // Update running statistics
        self.running_sum += energy as f64;
        self.running_sum_sq += (energy as f64) * (energy as f64);

        // Add to history
        self.entries.push_back(entry);
        self.total_entries += 1;

        // Trim if necessary
        while self.entries.len() > self.config.max_entries {
            if let Some(old) = self.entries.pop_front() {
                self.running_sum -= old.energy as f64;
                self.running_sum_sq -= (old.energy as f64) * (old.energy as f64);
            }
        }

        // Invalidate cached trend
        self.last_trend = None;
    }

    /// Get the current energy value
    pub fn current(&self) -> Option<f32> {
        self.entries.back().map(|e| e.energy)
    }

    /// Get the previous energy value
    pub fn previous(&self) -> Option<f32> {
        if self.entries.len() >= 2 {
            self.entries.get(self.entries.len() - 2).map(|e| e.energy)
        } else {
            None
        }
    }

    /// Get the change from previous to current
    pub fn delta(&self) -> Option<f32> {
        match (self.current(), self.previous()) {
            (Some(curr), Some(prev)) => Some(curr - prev),
            _ => None,
        }
    }

    /// Get the mean energy
    pub fn mean(&self) -> f32 {
        if self.entries.is_empty() {
            0.0
        } else {
            (self.running_sum / self.entries.len() as f64) as f32
        }
    }

    /// Get the standard deviation
    pub fn std_dev(&self) -> f32 {
        let n = self.entries.len();
        if n < 2 {
            return 0.0;
        }

        let mean = self.running_sum / n as f64;
        let variance = (self.running_sum_sq / n as f64) - (mean * mean);

        if variance > 0.0 {
            (variance.sqrt()) as f32
        } else {
            0.0
        }
    }

    /// Get the minimum energy value
    pub fn min(&self) -> Option<f32> {
        self.entries
            .iter()
            .map(|e| e.energy)
            .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
    }

    /// Get the maximum energy value
    pub fn max(&self) -> Option<f32> {
        self.entries
            .iter()
            .map(|e| e.energy)
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
    }

    /// Compute the current trend
    pub fn trend(&mut self) -> EnergyTrend {
        if let Some(ref trend) = self.last_trend {
            return trend.clone();
        }

        let trend = self.compute_trend();
        self.last_trend = Some(trend.clone());
        trend
    }

    /// Check if energy has been above threshold for persistence window
    pub fn is_above_threshold_persistent(&self, threshold: f32) -> bool {
        let window = Duration::seconds(self.config.persistence_window_secs as i64);
        let cutoff = Utc::now() - window;

        // Check all entries within the persistence window
        let recent: Vec<_> = self
            .entries
            .iter()
            .rev()
            .take_while(|e| e.timestamp >= cutoff)
            .collect();

        if recent.is_empty() {
            return false;
        }

        // All entries must be above threshold
        recent.iter().all(|e| e.energy > threshold)
    }

    /// Check if energy has been below threshold for persistence window
    pub fn is_below_threshold_persistent(&self, threshold: f32) -> bool {
        let window = Duration::seconds(self.config.persistence_window_secs as i64);
        let cutoff = Utc::now() - window;

        let recent: Vec<_> = self
            .entries
            .iter()
            .rev()
            .take_while(|e| e.timestamp >= cutoff)
            .collect();

        if recent.is_empty() {
            return false;
        }

        recent.iter().all(|e| e.energy < threshold)
    }

    /// Get entries in the persistence window
    pub fn recent_entries(&self, seconds: u64) -> Vec<(f32, DateTime<Utc>)> {
        let window = Duration::seconds(seconds as i64);
        let cutoff = Utc::now() - window;

        self.entries
            .iter()
            .rev()
            .take_while(|e| e.timestamp >= cutoff)
            .map(|e| (e.energy, e.timestamp))
            .collect()
    }

    /// Get the number of entries
    #[inline]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if history is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get total entries ever recorded
    #[inline]
    pub fn total_entries(&self) -> u64 {
        self.total_entries
    }

    /// Get anomaly count
    #[inline]
    pub fn anomaly_count(&self) -> u64 {
        self.anomaly_count
    }

    /// Get anomaly rate
    pub fn anomaly_rate(&self) -> f32 {
        if self.total_entries > 0 {
            self.anomaly_count as f32 / self.total_entries as f32
        } else {
            0.0
        }
    }

    /// Clear all history
    pub fn clear(&mut self) {
        self.entries.clear();
        self.running_sum = 0.0;
        self.running_sum_sq = 0.0;
        self.last_trend = None;
    }

    // Private methods

    fn is_anomaly(&self, energy: f32) -> bool {
        if self.entries.len() < self.config.min_entries {
            return false;
        }

        let mean = self.mean();
        let std_dev = self.std_dev();

        if std_dev < 1e-10 {
            return false;
        }

        let z_score = ((energy - mean) / std_dev).abs();
        z_score > self.config.anomaly_sigma
    }

    fn compute_trend(&self) -> EnergyTrend {
        let window_size = self.config.trend_window.min(self.entries.len());

        if window_size < self.config.min_entries {
            return EnergyTrend {
                direction: TrendDirection::Unknown,
                slope: 0.0,
                r_squared: 0.0,
                mean: self.mean(),
                std_dev: self.std_dev(),
                window_size,
            };
        }

        // Get recent entries
        let recent: Vec<_> = self.entries.iter().rev().take(window_size).collect();

        // Linear regression: y = mx + b
        // x is the index, y is the energy value
        let n = recent.len() as f64;
        let mut sum_x = 0.0;
        let mut sum_y = 0.0;
        let mut sum_xy = 0.0;
        let mut sum_xx = 0.0;

        for (i, entry) in recent.iter().rev().enumerate() {
            let x = i as f64;
            let y = entry.energy as f64;
            sum_x += x;
            sum_y += y;
            sum_xy += x * y;
            sum_xx += x * x;
        }

        // Compute slope
        let slope = if (n * sum_xx - sum_x * sum_x).abs() > 1e-10 {
            ((n * sum_xy - sum_x * sum_y) / (n * sum_xx - sum_x * sum_x)) as f32
        } else {
            0.0
        };

        // Compute R-squared
        let mean_y = sum_y / n;
        let mut ss_tot = 0.0;
        let mut ss_res = 0.0;

        let b = (sum_y - slope as f64 * sum_x) / n;

        for (i, entry) in recent.iter().rev().enumerate() {
            let x = i as f64;
            let y = entry.energy as f64;
            let y_pred = slope as f64 * x + b;

            ss_tot += (y - mean_y).powi(2);
            ss_res += (y - y_pred).powi(2);
        }

        let r_squared = if ss_tot > 1e-10 {
            (1.0 - ss_res / ss_tot) as f32
        } else {
            0.0
        };

        // Determine direction
        let direction = if slope.abs() < 0.001 {
            TrendDirection::Stable
        } else if slope > 0.0 {
            TrendDirection::Increasing
        } else {
            TrendDirection::Decreasing
        };

        // Compute window stats
        let window_sum: f64 = recent.iter().map(|e| e.energy as f64).sum();
        let window_mean = (window_sum / n) as f32;

        let window_var: f64 = recent
            .iter()
            .map(|e| {
                let diff = e.energy as f64 - window_sum / n;
                diff * diff
            })
            .sum::<f64>()
            / n;
        let window_std_dev = (window_var.sqrt()) as f32;

        EnergyTrend {
            direction,
            slope,
            r_squared,
            mean: window_mean,
            std_dev: window_std_dev,
            window_size,
        }
    }
}

impl Default for EnergyHistory {
    fn default() -> Self {
        Self::new(EnergyHistoryConfig::default())
    }
}

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

    #[test]
    fn test_history_creation() {
        let history = EnergyHistory::default();
        assert!(history.is_empty());
        assert_eq!(history.len(), 0);
    }

    #[test]
    fn test_record_energy() {
        let mut history = EnergyHistory::default();

        history.record(1.0);
        history.record(2.0);
        history.record(3.0);

        assert_eq!(history.len(), 3);
        assert_eq!(history.current(), Some(3.0));
        assert_eq!(history.previous(), Some(2.0));
        assert_eq!(history.delta(), Some(1.0));
    }

    #[test]
    fn test_statistics() {
        let mut history = EnergyHistory::default();

        history.record(1.0);
        history.record(2.0);
        history.record(3.0);
        history.record(4.0);
        history.record(5.0);

        assert_eq!(history.mean(), 3.0);
        assert_eq!(history.min(), Some(1.0));
        assert_eq!(history.max(), Some(5.0));
    }

    #[test]
    fn test_trend_increasing() {
        let mut history = EnergyHistory::new(EnergyHistoryConfig {
            min_entries: 3,
            trend_window: 5,
            ..Default::default()
        });

        for i in 0..10 {
            history.record(i as f32);
        }

        let trend = history.trend();
        assert_eq!(trend.direction, TrendDirection::Increasing);
        assert!(trend.slope > 0.0);
    }

    #[test]
    fn test_trend_decreasing() {
        let mut history = EnergyHistory::new(EnergyHistoryConfig {
            min_entries: 3,
            trend_window: 5,
            ..Default::default()
        });

        for i in (0..10).rev() {
            history.record(i as f32);
        }

        let trend = history.trend();
        assert_eq!(trend.direction, TrendDirection::Decreasing);
        assert!(trend.slope < 0.0);
    }

    #[test]
    fn test_trend_stable() {
        let mut history = EnergyHistory::new(EnergyHistoryConfig {
            min_entries: 3,
            trend_window: 5,
            ..Default::default()
        });

        for _ in 0..10 {
            history.record(5.0);
        }

        let trend = history.trend();
        assert_eq!(trend.direction, TrendDirection::Stable);
        assert!(trend.slope.abs() < 0.01);
    }

    #[test]
    fn test_anomaly_detection() {
        let config = EnergyHistoryConfig {
            anomaly_sigma: 2.0,
            min_entries: 5,
            ..Default::default()
        };
        let mut history = EnergyHistory::new(config);

        // Add normal values
        for _ in 0..10 {
            history.record(5.0);
        }

        // Add anomaly
        history.record(100.0);

        assert!(history.anomaly_count() > 0);
    }

    #[test]
    fn test_history_trimming() {
        let config = EnergyHistoryConfig {
            max_entries: 5,
            ..Default::default()
        };
        let mut history = EnergyHistory::new(config);

        for i in 0..10 {
            history.record(i as f32);
        }

        assert_eq!(history.len(), 5);
        assert_eq!(history.total_entries(), 10);
        // Oldest entries should be trimmed
        assert_eq!(history.min(), Some(5.0));
    }

    #[test]
    fn test_clear() {
        let mut history = EnergyHistory::default();

        history.record(1.0);
        history.record(2.0);
        history.clear();

        assert!(history.is_empty());
        assert_eq!(history.current(), None);
    }
}