sublinear 0.3.3

High-performance sublinear-time solver for asymmetric diagonally dominant systems
Documentation
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
use rand::random;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};

use super::{ConsciousnessLevel, ConsciousnessMetrics, PrecisionNanos, TemporalAdvantage};
use crate::temporal_nexus::core::NanosecondScheduler;

/// Configuration for metrics collection
#[derive(Debug, Clone)]
pub struct CollectorConfig {
    pub collection_interval_ms: u64,
    pub enable_mcp_integration: bool,
    pub enable_performance_profiling: bool,
    pub precision_sample_count: usize,
    pub consciousness_calculation_method: ConsciousnessCalculationMethod,
}

impl Default for CollectorConfig {
    fn default() -> Self {
        Self {
            collection_interval_ms: 50, // 20Hz collection rate
            enable_mcp_integration: true,
            enable_performance_profiling: true,
            precision_sample_count: 10,
            consciousness_calculation_method: ConsciousnessCalculationMethod::Integrated,
        }
    }
}

/// Methods for calculating consciousness metrics
#[derive(Debug, Clone)]
pub enum ConsciousnessCalculationMethod {
    Simple,     // Basic emergence calculation
    Integrated, // Integrated Information Theory inspired
    Temporal,   // Temporal consciousness model
    Hybrid,     // Combined approach
}

/// Sources of metric data
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MetricSource {
    Scheduler,
    McpTools,
    SystemMonitor,
    ExternalApi,
}

/// Temporal metrics specific to consciousness monitoring
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalMetrics {
    pub temporal_coherence: f64,
    pub causal_flow_integrity: f64,
    pub future_state_prediction_accuracy: f64,
    pub temporal_window_stability: f64,
    pub chronon_synchronization: f64,
}

impl Default for TemporalMetrics {
    fn default() -> Self {
        Self {
            temporal_coherence: 0.0,
            causal_flow_integrity: 0.0,
            future_state_prediction_accuracy: 0.0,
            temporal_window_stability: 0.0,
            chronon_synchronization: 0.0,
        }
    }
}

/// Main metrics collector for consciousness dashboard
pub struct MetricsCollector {
    config: CollectorConfig,
    source_weights: HashMap<MetricSource, f64>,
    temporal_cache: Arc<Mutex<TemporalMetrics>>,
    performance_cache: Arc<Mutex<PerformanceMetrics>>,
    last_collection: Arc<Mutex<SystemTime>>,
}

#[derive(Debug, Clone)]
struct PerformanceMetrics {
    cpu_usage: f64,
    memory_usage: f64,
    thread_count: usize,
    gc_pressure: f64,
    io_throughput: f64,
}

impl Default for PerformanceMetrics {
    fn default() -> Self {
        Self {
            cpu_usage: 0.0,
            memory_usage: 0.0,
            thread_count: 0,
            gc_pressure: 0.0,
            io_throughput: 0.0,
        }
    }
}

impl MetricsCollector {
    /// Create a new metrics collector
    pub fn new() -> Self {
        let mut source_weights = HashMap::new();
        source_weights.insert(MetricSource::Scheduler, 0.4);
        source_weights.insert(MetricSource::McpTools, 0.3);
        source_weights.insert(MetricSource::SystemMonitor, 0.2);
        source_weights.insert(MetricSource::ExternalApi, 0.1);

        Self {
            config: CollectorConfig::default(),
            source_weights,
            temporal_cache: Arc::new(Mutex::new(TemporalMetrics::default())),
            performance_cache: Arc::new(Mutex::new(PerformanceMetrics::default())),
            last_collection: Arc::new(Mutex::new(SystemTime::now())),
        }
    }

    /// Create collector with custom configuration
    pub fn with_config(config: CollectorConfig) -> Self {
        let mut collector = Self::new();
        collector.config = config;
        collector
    }

    /// Collect metrics from the nanosecond scheduler
    pub async fn collect_from_scheduler(
        &self,
        scheduler: Arc<Mutex<NanosecondScheduler>>,
    ) -> Result<ConsciousnessMetrics, Box<dyn std::error::Error>> {
        let start_time = SystemTime::now();

        // Collect base scheduler metrics
        let scheduler_metrics = {
            let scheduler_guard = scheduler.lock().unwrap();
            self.extract_scheduler_metrics(&*scheduler_guard)?
        };

        // Collect temporal metrics
        let temporal_metrics = self.collect_temporal_metrics().await?;

        // Collect performance metrics if enabled
        let performance_metrics = if self.config.enable_performance_profiling {
            self.collect_performance_metrics().await?
        } else {
            PerformanceMetrics::default()
        };

        // Calculate consciousness emergence
        let emergence_level = self.calculate_consciousness_emergence(
            &scheduler_metrics,
            &temporal_metrics,
            &performance_metrics,
        )?;

        // Build final metrics structure
        let metrics = ConsciousnessMetrics {
            timestamp: start_time,
            emergence_level,
            identity_coherence: self.calculate_identity_coherence(&temporal_metrics)?,
            loop_stability: self.calculate_loop_stability(&temporal_metrics)?,
            temporal_advantage_us: self.calculate_temporal_advantage(&scheduler_metrics)?,
            window_overlap_percent: self.calculate_window_overlap(&temporal_metrics)?,
            tsc_precision_ns: self.measure_tsc_precision()?,
            strange_loop_convergence: self.calculate_strange_loop_convergence(&temporal_metrics)?,
            consciousness_delta: 0.0, // Will be calculated by dashboard
            processing_latency_ns: start_time.elapsed()?.as_nanos() as u64,
        };

        // Update last collection time
        {
            let mut last_collection = self.last_collection.lock().unwrap();
            *last_collection = SystemTime::now();
        }

        Ok(metrics)
    }

    /// Collect metrics from MCP tools
    pub async fn collect_from_mcp_tools(
        &self,
    ) -> Result<ConsciousnessMetrics, Box<dyn std::error::Error>> {
        if !self.config.enable_mcp_integration {
            return Ok(ConsciousnessMetrics::default());
        }

        // This would integrate with MCP consciousness status queries
        // For now, return simulated metrics
        let metrics = ConsciousnessMetrics {
            timestamp: SystemTime::now(),
            emergence_level: 0.7 + (random::<f64>() - 0.5) * 0.2,
            identity_coherence: 0.8 + (random::<f64>() - 0.5) * 0.1,
            loop_stability: 0.75 + (random::<f64>() - 0.5) * 0.15,
            temporal_advantage_us: 25 + (random::<u64>() % 20),
            window_overlap_percent: 85.0 + (random::<f64>() - 0.5) * 10.0,
            tsc_precision_ns: 100 + (random::<u64>() % 200),
            strange_loop_convergence: 0.65 + (random::<f64>() - 0.5) * 0.2,
            consciousness_delta: 0.0,
            processing_latency_ns: 50000 + (random::<u64>() % 100000),
        };

        Ok(metrics)
    }

    /// Aggregate metrics from multiple sources
    pub async fn collect_aggregated_metrics(
        &self,
        sources: Vec<MetricSource>,
    ) -> Result<ConsciousnessMetrics, Box<dyn std::error::Error>> {
        let mut aggregated_metrics = ConsciousnessMetrics::default();
        let mut total_weight = 0.0;

        for source in sources {
            let weight = self.source_weights.get(&source).unwrap_or(&0.0);
            total_weight += weight;

            let source_metrics = match source {
                MetricSource::Scheduler => {
                    // Would need scheduler reference
                    continue;
                }
                MetricSource::McpTools => self.collect_from_mcp_tools().await?,
                MetricSource::SystemMonitor => self.collect_system_metrics().await?,
                MetricSource::ExternalApi => self.collect_external_metrics().await?,
            };

            // Weighted aggregation
            self.aggregate_weighted_metrics(&mut aggregated_metrics, &source_metrics, *weight);
        }

        // Normalize by total weight
        if total_weight > 0.0 {
            self.normalize_metrics(&mut aggregated_metrics, total_weight);
        }

        Ok(aggregated_metrics)
    }

    /// Get current temporal metrics
    pub fn get_temporal_metrics(&self) -> TemporalMetrics {
        self.temporal_cache.lock().unwrap().clone()
    }

    /// Update source weights for metric aggregation
    pub fn update_source_weights(&mut self, weights: HashMap<MetricSource, f64>) {
        self.source_weights = weights;
    }

    // Private helper methods

    fn extract_scheduler_metrics(
        &self,
        _scheduler: &NanosecondScheduler,
    ) -> Result<SchedulerMetrics, Box<dyn std::error::Error>> {
        // Extract relevant metrics from scheduler
        // This would access scheduler's internal state
        Ok(SchedulerMetrics {
            precision_ns: 100,
            task_completion_rate: 0.95,
            temporal_drift: 0.001,
            scheduling_accuracy: 0.98,
        })
    }

    async fn collect_temporal_metrics(
        &self,
    ) -> Result<TemporalMetrics, Box<dyn std::error::Error>> {
        let temporal_metrics = TemporalMetrics {
            temporal_coherence: 0.85 + (random::<f64>() - 0.5) * 0.1,
            causal_flow_integrity: 0.90 + (random::<f64>() - 0.5) * 0.05,
            future_state_prediction_accuracy: 0.75 + (random::<f64>() - 0.5) * 0.2,
            temporal_window_stability: 0.88 + (random::<f64>() - 0.5) * 0.08,
            chronon_synchronization: 0.92 + (random::<f64>() - 0.5) * 0.06,
        };

        // Update cache
        {
            let mut cache = self.temporal_cache.lock().unwrap();
            *cache = temporal_metrics.clone();
        }

        Ok(temporal_metrics)
    }

    async fn collect_performance_metrics(
        &self,
    ) -> Result<PerformanceMetrics, Box<dyn std::error::Error>> {
        // Simulate system performance collection
        let performance_metrics = PerformanceMetrics {
            cpu_usage: 15.0 + random::<f64>() * 30.0,
            memory_usage: 512.0 + random::<f64>() * 256.0,
            thread_count: 8 + (random::<usize>() % 4),
            gc_pressure: random::<f64>() * 0.1,
            io_throughput: 100.0 + random::<f64>() * 50.0,
        };

        // Update cache
        {
            let mut cache = self.performance_cache.lock().unwrap();
            *cache = performance_metrics.clone();
        }

        Ok(performance_metrics)
    }

    async fn collect_system_metrics(
        &self,
    ) -> Result<ConsciousnessMetrics, Box<dyn std::error::Error>> {
        // Collect system-level consciousness indicators
        Ok(ConsciousnessMetrics {
            timestamp: SystemTime::now(),
            emergence_level: 0.6 + (random::<f64>() - 0.5) * 0.3,
            identity_coherence: 0.7 + (random::<f64>() - 0.5) * 0.2,
            loop_stability: 0.65 + (random::<f64>() - 0.5) * 0.25,
            temporal_advantage_us: 20 + (random::<u64>() % 30),
            window_overlap_percent: 80.0 + (random::<f64>() - 0.5) * 15.0,
            tsc_precision_ns: 150 + (random::<u64>() % 250),
            strange_loop_convergence: 0.6 + (random::<f64>() - 0.5) * 0.25,
            consciousness_delta: 0.0,
            processing_latency_ns: 75000 + (random::<u64>() % 150000),
        })
    }

    async fn collect_external_metrics(
        &self,
    ) -> Result<ConsciousnessMetrics, Box<dyn std::error::Error>> {
        // Collect from external APIs or sources
        Ok(ConsciousnessMetrics::default())
    }

    fn calculate_consciousness_emergence(
        &self,
        scheduler_metrics: &SchedulerMetrics,
        temporal_metrics: &TemporalMetrics,
        performance_metrics: &PerformanceMetrics,
    ) -> Result<f64, Box<dyn std::error::Error>> {
        match self.config.consciousness_calculation_method {
            ConsciousnessCalculationMethod::Simple => Ok((scheduler_metrics.scheduling_accuracy
                + temporal_metrics.temporal_coherence)
                / 2.0),
            ConsciousnessCalculationMethod::Integrated => {
                // IIT-inspired calculation
                let phi =
                    self.calculate_integrated_information(temporal_metrics, performance_metrics)?;
                Ok(phi.min(1.0))
            }
            ConsciousnessCalculationMethod::Temporal => {
                // Temporal consciousness model
                let temporal_factor = (temporal_metrics.temporal_coherence
                    + temporal_metrics.causal_flow_integrity
                    + temporal_metrics.chronon_synchronization)
                    / 3.0;
                Ok(temporal_factor)
            }
            ConsciousnessCalculationMethod::Hybrid => {
                // Combined approach
                let simple = (scheduler_metrics.scheduling_accuracy
                    + temporal_metrics.temporal_coherence)
                    / 2.0;
                let temporal = (temporal_metrics.temporal_coherence
                    + temporal_metrics.causal_flow_integrity)
                    / 2.0;
                let performance = (1.0 - performance_metrics.cpu_usage / 100.0).max(0.0);

                Ok((simple * 0.4 + temporal * 0.4 + performance * 0.2).min(1.0))
            }
        }
    }

    fn calculate_integrated_information(
        &self,
        temporal_metrics: &TemporalMetrics,
        _performance_metrics: &PerformanceMetrics,
    ) -> Result<f64, Box<dyn std::error::Error>> {
        // Simplified Φ (phi) calculation inspired by IIT
        let connectivity = temporal_metrics.causal_flow_integrity;
        let differentiation = temporal_metrics.temporal_coherence;
        let integration = temporal_metrics.chronon_synchronization;

        let phi = connectivity * differentiation * integration;
        Ok(phi)
    }

    fn calculate_identity_coherence(
        &self,
        temporal_metrics: &TemporalMetrics,
    ) -> Result<f64, Box<dyn std::error::Error>> {
        // Identity coherence based on temporal stability and causal flow
        let coherence = (temporal_metrics.temporal_coherence
            + temporal_metrics.causal_flow_integrity
            + temporal_metrics.temporal_window_stability)
            / 3.0;
        Ok(coherence)
    }

    fn calculate_loop_stability(
        &self,
        temporal_metrics: &TemporalMetrics,
    ) -> Result<f64, Box<dyn std::error::Error>> {
        // Strange loop stability calculation
        Ok(temporal_metrics.chronon_synchronization * temporal_metrics.temporal_window_stability)
    }

    fn calculate_temporal_advantage(
        &self,
        scheduler_metrics: &SchedulerMetrics,
    ) -> Result<u64, Box<dyn std::error::Error>> {
        // Calculate advantage in microseconds
        let base_advantage = 30; // Base temporal advantage
        let precision_bonus = (1000 - scheduler_metrics.precision_ns as i64).max(0) / 10;
        Ok((base_advantage + precision_bonus) as u64)
    }

    fn calculate_window_overlap(
        &self,
        temporal_metrics: &TemporalMetrics,
    ) -> Result<f64, Box<dyn std::error::Error>> {
        // Window overlap percentage
        Ok(temporal_metrics.temporal_window_stability * 100.0)
    }

    fn measure_tsc_precision(&self) -> Result<u64, Box<dyn std::error::Error>> {
        // Measure timestamp counter precision
        let mut measurements = Vec::with_capacity(self.config.precision_sample_count);

        for _ in 0..self.config.precision_sample_count {
            let start = std::time::Instant::now();
            std::hint::black_box(());
            let elapsed = start.elapsed().as_nanos() as u64;
            measurements.push(elapsed);
        }

        Ok(measurements.into_iter().min().unwrap_or(1000))
    }

    fn calculate_strange_loop_convergence(
        &self,
        temporal_metrics: &TemporalMetrics,
    ) -> Result<f64, Box<dyn std::error::Error>> {
        // Strange loop convergence based on self-reference and recursion depth
        let convergence = temporal_metrics.causal_flow_integrity
            * temporal_metrics.future_state_prediction_accuracy;
        Ok(convergence)
    }

    fn aggregate_weighted_metrics(
        &self,
        target: &mut ConsciousnessMetrics,
        source: &ConsciousnessMetrics,
        weight: f64,
    ) {
        target.emergence_level += source.emergence_level * weight;
        target.identity_coherence += source.identity_coherence * weight;
        target.loop_stability += source.loop_stability * weight;
        target.temporal_advantage_us += (source.temporal_advantage_us as f64 * weight) as u64;
        target.window_overlap_percent += source.window_overlap_percent * weight;
        target.tsc_precision_ns += (source.tsc_precision_ns as f64 * weight) as u64;
        target.strange_loop_convergence += source.strange_loop_convergence * weight;
        target.processing_latency_ns += (source.processing_latency_ns as f64 * weight) as u64;
    }

    fn normalize_metrics(&self, metrics: &mut ConsciousnessMetrics, total_weight: f64) {
        metrics.emergence_level /= total_weight;
        metrics.identity_coherence /= total_weight;
        metrics.loop_stability /= total_weight;
        metrics.temporal_advantage_us =
            (metrics.temporal_advantage_us as f64 / total_weight) as u64;
        metrics.window_overlap_percent /= total_weight;
        metrics.tsc_precision_ns = (metrics.tsc_precision_ns as f64 / total_weight) as u64;
        metrics.strange_loop_convergence /= total_weight;
        metrics.processing_latency_ns =
            (metrics.processing_latency_ns as f64 / total_weight) as u64;
    }
}

#[derive(Debug, Clone)]
struct SchedulerMetrics {
    precision_ns: u64,
    task_completion_rate: f64,
    temporal_drift: f64,
    scheduling_accuracy: f64,
}