omega-runtime 1.1.0

Production runtime orchestrator integrating all ExoGenesis Omega subsystems with health monitoring
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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Health Monitoring System
//!
//! Provides comprehensive health monitoring for subsystems with configurable
//! thresholds, automatic degradation detection, and aggregated health status.

use parking_lot::RwLock;
use std::collections::HashMap;
use std::time::{Duration, Instant};
use thiserror::Error;
use tracing::{debug, warn};

/// Health status of a subsystem
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HealthStatus {
    /// Subsystem is operating normally
    Healthy,
    /// Subsystem is degraded but operational
    Degraded,
    /// Subsystem is unhealthy or non-operational
    Unhealthy,
}

impl HealthStatus {
    /// Check if status is healthy
    pub fn is_healthy(&self) -> bool {
        matches!(self, HealthStatus::Healthy)
    }

    /// Check if status is degraded
    pub fn is_degraded(&self) -> bool {
        matches!(self, HealthStatus::Degraded)
    }

    /// Check if status is unhealthy
    pub fn is_unhealthy(&self) -> bool {
        matches!(self, HealthStatus::Unhealthy)
    }

    /// Get numeric score (0-100)
    pub fn score(&self) -> u8 {
        match self {
            HealthStatus::Healthy => 100,
            HealthStatus::Degraded => 50,
            HealthStatus::Unhealthy => 0,
        }
    }
}

/// Health information for a subsystem
#[derive(Debug, Clone)]
pub struct SubsystemHealth {
    /// Subsystem name
    pub name: String,
    /// Current health status
    pub status: HealthStatus,
    /// Last health check timestamp
    pub last_check: Instant,
    /// Consecutive failure count
    pub consecutive_failures: u32,
    /// Consecutive success count
    pub consecutive_successes: u32,
    /// Total check count
    pub total_checks: u64,
    /// Total failure count
    pub total_failures: u64,
    /// Optional metadata
    pub metadata: HashMap<String, String>,
}

impl SubsystemHealth {
    /// Create new subsystem health
    pub fn new(name: String) -> Self {
        Self {
            name,
            status: HealthStatus::Healthy,
            last_check: Instant::now(),
            consecutive_failures: 0,
            consecutive_successes: 0,
            total_checks: 0,
            total_failures: 0,
            metadata: HashMap::new(),
        }
    }

    /// Get time since last check
    pub fn time_since_check(&self) -> Duration {
        self.last_check.elapsed()
    }

    /// Get failure rate as percentage
    pub fn failure_rate(&self) -> f64 {
        if self.total_checks == 0 {
            0.0
        } else {
            (self.total_failures as f64 / self.total_checks as f64) * 100.0
        }
    }

    /// Check if subsystem is stale (hasn't been checked recently)
    pub fn is_stale(&self, threshold: Duration) -> bool {
        self.time_since_check() > threshold
    }
}

/// Health monitor configuration
#[derive(Debug, Clone)]
pub struct HealthMonitorConfig {
    /// Number of consecutive failures before marking as degraded
    pub degraded_threshold: u32,
    /// Number of consecutive failures before marking as unhealthy
    pub unhealthy_threshold: u32,
    /// Number of consecutive successes needed to recover from degraded
    pub recovery_threshold: u32,
    /// Duration after which a subsystem is considered stale
    pub stale_threshold: Duration,
}

impl Default for HealthMonitorConfig {
    fn default() -> Self {
        Self {
            degraded_threshold: 3,
            unhealthy_threshold: 5,
            recovery_threshold: 5,
            stale_threshold: Duration::from_secs(300), // 5 minutes
        }
    }
}

/// Health monitoring error types
#[derive(Debug, Error)]
pub enum HealthError {
    #[error("Subsystem not found: {0}")]
    SubsystemNotFound(String),
    #[error("Invalid health status transition: {0}")]
    InvalidTransition(String),
}

/// Health monitor for tracking subsystem health
pub struct HealthMonitor {
    subsystems: RwLock<HashMap<String, SubsystemHealth>>,
    config: HealthMonitorConfig,
}

impl HealthMonitor {
    /// Create a new health monitor
    pub fn new(config: HealthMonitorConfig) -> Self {
        Self {
            subsystems: RwLock::new(HashMap::new()),
            config,
        }
    }

    /// Create with default configuration
    pub fn with_default_config() -> Self {
        Self::new(HealthMonitorConfig::default())
    }

    /// Register a new subsystem for monitoring
    pub fn register_subsystem(&self, name: String) {
        let mut subsystems = self.subsystems.write();
        if !subsystems.contains_key(&name) {
            debug!("Registering subsystem for health monitoring: {}", name);
            subsystems.insert(name.clone(), SubsystemHealth::new(name));
        }
    }

    /// Unregister a subsystem
    pub fn unregister_subsystem(&self, name: &str) -> Result<(), HealthError> {
        let mut subsystems = self.subsystems.write();
        subsystems
            .remove(name)
            .ok_or_else(|| HealthError::SubsystemNotFound(name.to_string()))?;
        debug!("Unregistered subsystem: {}", name);
        Ok(())
    }

    /// Update health status based on check result
    pub fn update_health(&self, name: &str, is_healthy: bool) -> Result<(), HealthError> {
        let mut subsystems = self.subsystems.write();
        let health = subsystems
            .get_mut(name)
            .ok_or_else(|| HealthError::SubsystemNotFound(name.to_string()))?;

        health.last_check = Instant::now();
        health.total_checks += 1;

        if is_healthy {
            health.consecutive_successes += 1;
            health.consecutive_failures = 0;

            // Update status based on recovery
            match health.status {
                HealthStatus::Unhealthy | HealthStatus::Degraded => {
                    if health.consecutive_successes >= self.config.recovery_threshold {
                        debug!("Subsystem {} recovered to healthy", name);
                        health.status = HealthStatus::Healthy;
                    }
                }
                HealthStatus::Healthy => {}
            }
        } else {
            health.consecutive_failures += 1;
            health.consecutive_successes = 0;
            health.total_failures += 1;

            // Update status based on failures
            let old_status = health.status;
            if health.consecutive_failures >= self.config.unhealthy_threshold {
                health.status = HealthStatus::Unhealthy;
                if old_status != HealthStatus::Unhealthy {
                    warn!("Subsystem {} marked as unhealthy", name);
                }
            } else if health.consecutive_failures >= self.config.degraded_threshold {
                health.status = HealthStatus::Degraded;
                if old_status != HealthStatus::Degraded {
                    warn!("Subsystem {} marked as degraded", name);
                }
            }
        }

        Ok(())
    }

    /// Manually set health status
    pub fn set_health(
        &self,
        name: &str,
        status: HealthStatus,
    ) -> Result<(), HealthError> {
        let mut subsystems = self.subsystems.write();
        let health = subsystems
            .get_mut(name)
            .ok_or_else(|| HealthError::SubsystemNotFound(name.to_string()))?;

        health.status = status;
        health.last_check = Instant::now();

        Ok(())
    }

    /// Check health of a specific subsystem
    pub fn check_health(&self, name: &str) -> Result<SubsystemHealth, HealthError> {
        let subsystems = self.subsystems.read();
        subsystems
            .get(name)
            .cloned()
            .ok_or_else(|| HealthError::SubsystemNotFound(name.to_string()))
    }

    /// Get overall system health status
    pub fn overall_status(&self) -> HealthStatus {
        let subsystems = self.subsystems.read();

        if subsystems.is_empty() {
            return HealthStatus::Healthy;
        }

        let mut has_unhealthy = false;
        let mut has_degraded = false;

        for health in subsystems.values() {
            match health.status {
                HealthStatus::Unhealthy => has_unhealthy = true,
                HealthStatus::Degraded => has_degraded = true,
                HealthStatus::Healthy => {}
            }

            // Check for stale subsystems
            if health.is_stale(self.config.stale_threshold) {
                warn!("Subsystem {} is stale (last check {:?} ago)", health.name, health.time_since_check());
                has_degraded = true;
            }
        }

        if has_unhealthy {
            HealthStatus::Unhealthy
        } else if has_degraded {
            HealthStatus::Degraded
        } else {
            HealthStatus::Healthy
        }
    }

    /// Get all subsystem health statuses
    pub fn all_subsystems(&self) -> HashMap<String, SubsystemHealth> {
        self.subsystems.read().clone()
    }

    /// Get count of subsystems by status
    pub fn status_counts(&self) -> HashMap<HealthStatus, usize> {
        let subsystems = self.subsystems.read();
        let mut counts = HashMap::new();

        for health in subsystems.values() {
            *counts.entry(health.status).or_insert(0) += 1;
        }

        counts
    }

    /// Get unhealthy subsystems
    pub fn unhealthy_subsystems(&self) -> Vec<String> {
        let subsystems = self.subsystems.read();
        subsystems
            .values()
            .filter(|h| h.status.is_unhealthy())
            .map(|h| h.name.clone())
            .collect()
    }

    /// Get degraded subsystems
    pub fn degraded_subsystems(&self) -> Vec<String> {
        let subsystems = self.subsystems.read();
        subsystems
            .values()
            .filter(|h| h.status.is_degraded())
            .map(|h| h.name.clone())
            .collect()
    }

    /// Update subsystem metadata
    pub fn update_metadata(
        &self,
        name: &str,
        key: String,
        value: String,
    ) -> Result<(), HealthError> {
        let mut subsystems = self.subsystems.write();
        let health = subsystems
            .get_mut(name)
            .ok_or_else(|| HealthError::SubsystemNotFound(name.to_string()))?;

        health.metadata.insert(key, value);
        Ok(())
    }

    /// Get subsystem count
    pub fn subsystem_count(&self) -> usize {
        self.subsystems.read().len()
    }

    /// Clear all subsystems
    pub fn clear(&self) {
        self.subsystems.write().clear();
    }
}

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

    #[test]
    fn test_health_monitor_creation() {
        let monitor = HealthMonitor::default();
        assert_eq!(monitor.subsystem_count(), 0);
        assert_eq!(monitor.overall_status(), HealthStatus::Healthy);
    }

    #[test]
    fn test_register_subsystem() {
        let monitor = HealthMonitor::default();
        monitor.register_subsystem("test-subsystem".to_string());
        assert_eq!(monitor.subsystem_count(), 1);

        let health = monitor.check_health("test-subsystem").unwrap();
        assert_eq!(health.name, "test-subsystem");
        assert_eq!(health.status, HealthStatus::Healthy);
    }

    #[test]
    fn test_register_duplicate_subsystem() {
        let monitor = HealthMonitor::default();
        monitor.register_subsystem("test".to_string());
        monitor.register_subsystem("test".to_string());
        assert_eq!(monitor.subsystem_count(), 1);
    }

    #[test]
    fn test_unregister_subsystem() {
        let monitor = HealthMonitor::default();
        monitor.register_subsystem("test".to_string());
        assert_eq!(monitor.subsystem_count(), 1);

        monitor.unregister_subsystem("test").unwrap();
        assert_eq!(monitor.subsystem_count(), 0);
    }

    #[test]
    fn test_update_health_success() {
        let monitor = HealthMonitor::default();
        monitor.register_subsystem("test".to_string());

        monitor.update_health("test", true).unwrap();

        let health = monitor.check_health("test").unwrap();
        assert_eq!(health.consecutive_successes, 1);
        assert_eq!(health.total_checks, 1);
        assert_eq!(health.status, HealthStatus::Healthy);
    }

    #[test]
    fn test_update_health_failure() {
        let config = HealthMonitorConfig {
            degraded_threshold: 2,
            unhealthy_threshold: 4,
            ..Default::default()
        };
        let monitor = HealthMonitor::new(config);
        monitor.register_subsystem("test".to_string());

        // First failure - still healthy
        monitor.update_health("test", false).unwrap();
        let health = monitor.check_health("test").unwrap();
        assert_eq!(health.status, HealthStatus::Healthy);

        // Second failure - degraded
        monitor.update_health("test", false).unwrap();
        let health = monitor.check_health("test").unwrap();
        assert_eq!(health.status, HealthStatus::Degraded);

        // More failures - unhealthy
        monitor.update_health("test", false).unwrap();
        monitor.update_health("test", false).unwrap();
        let health = monitor.check_health("test").unwrap();
        assert_eq!(health.status, HealthStatus::Unhealthy);
    }

    #[test]
    fn test_recovery_from_degraded() {
        let config = HealthMonitorConfig {
            degraded_threshold: 2,
            recovery_threshold: 3,
            ..Default::default()
        };
        let monitor = HealthMonitor::new(config);
        monitor.register_subsystem("test".to_string());

        // Make it degraded
        monitor.update_health("test", false).unwrap();
        monitor.update_health("test", false).unwrap();
        assert_eq!(
            monitor.check_health("test").unwrap().status,
            HealthStatus::Degraded
        );

        // Recover with successes
        monitor.update_health("test", true).unwrap();
        monitor.update_health("test", true).unwrap();
        assert_eq!(
            monitor.check_health("test").unwrap().status,
            HealthStatus::Degraded
        );

        monitor.update_health("test", true).unwrap();
        assert_eq!(
            monitor.check_health("test").unwrap().status,
            HealthStatus::Healthy
        );
    }

    #[test]
    fn test_overall_status_healthy() {
        let monitor = HealthMonitor::default();
        monitor.register_subsystem("test1".to_string());
        monitor.register_subsystem("test2".to_string());

        monitor.update_health("test1", true).unwrap();
        monitor.update_health("test2", true).unwrap();

        assert_eq!(monitor.overall_status(), HealthStatus::Healthy);
    }

    #[test]
    fn test_overall_status_degraded() {
        let config = HealthMonitorConfig {
            degraded_threshold: 2,
            ..Default::default()
        };
        let monitor = HealthMonitor::new(config);
        monitor.register_subsystem("test1".to_string());
        monitor.register_subsystem("test2".to_string());

        monitor.update_health("test1", true).unwrap();

        // Make test2 degraded
        monitor.update_health("test2", false).unwrap();
        monitor.update_health("test2", false).unwrap();

        assert_eq!(monitor.overall_status(), HealthStatus::Degraded);
    }

    #[test]
    fn test_overall_status_unhealthy() {
        let config = HealthMonitorConfig {
            unhealthy_threshold: 2,
            ..Default::default()
        };
        let monitor = HealthMonitor::new(config);
        monitor.register_subsystem("test1".to_string());
        monitor.register_subsystem("test2".to_string());

        monitor.update_health("test1", true).unwrap();

        // Make test2 unhealthy
        monitor.update_health("test2", false).unwrap();
        monitor.update_health("test2", false).unwrap();

        assert_eq!(monitor.overall_status(), HealthStatus::Unhealthy);
    }

    #[test]
    fn test_status_counts() {
        let config = HealthMonitorConfig {
            degraded_threshold: 2,
            unhealthy_threshold: 4,
            ..Default::default()
        };
        let monitor = HealthMonitor::new(config);

        monitor.register_subsystem("healthy".to_string());
        monitor.register_subsystem("degraded".to_string());
        monitor.register_subsystem("unhealthy".to_string());

        // Make degraded
        monitor.update_health("degraded", false).unwrap();
        monitor.update_health("degraded", false).unwrap();

        // Make unhealthy
        for _ in 0..4 {
            monitor.update_health("unhealthy", false).unwrap();
        }

        let counts = monitor.status_counts();
        assert_eq!(counts.get(&HealthStatus::Healthy), Some(&1));
        assert_eq!(counts.get(&HealthStatus::Degraded), Some(&1));
        assert_eq!(counts.get(&HealthStatus::Unhealthy), Some(&1));
    }

    #[test]
    fn test_unhealthy_subsystems() {
        let config = HealthMonitorConfig {
            unhealthy_threshold: 2,
            ..Default::default()
        };
        let monitor = HealthMonitor::new(config);

        monitor.register_subsystem("healthy".to_string());
        monitor.register_subsystem("unhealthy1".to_string());
        monitor.register_subsystem("unhealthy2".to_string());

        monitor.update_health("unhealthy1", false).unwrap();
        monitor.update_health("unhealthy1", false).unwrap();
        monitor.update_health("unhealthy2", false).unwrap();
        monitor.update_health("unhealthy2", false).unwrap();

        let unhealthy = monitor.unhealthy_subsystems();
        assert_eq!(unhealthy.len(), 2);
        assert!(unhealthy.contains(&"unhealthy1".to_string()));
        assert!(unhealthy.contains(&"unhealthy2".to_string()));
    }

    #[test]
    fn test_metadata() {
        let monitor = HealthMonitor::default();
        monitor.register_subsystem("test".to_string());

        monitor
            .update_metadata("test", "version".to_string(), "1.0.0".to_string())
            .unwrap();

        let health = monitor.check_health("test").unwrap();
        assert_eq!(health.metadata.get("version"), Some(&"1.0.0".to_string()));
    }

    #[test]
    fn test_failure_rate() {
        let monitor = HealthMonitor::default();
        monitor.register_subsystem("test".to_string());

        monitor.update_health("test", true).unwrap();
        monitor.update_health("test", false).unwrap();
        monitor.update_health("test", true).unwrap();
        monitor.update_health("test", false).unwrap();

        let health = monitor.check_health("test").unwrap();
        assert_eq!(health.failure_rate(), 50.0);
    }

    #[test]
    fn test_stale_detection() {
        let config = HealthMonitorConfig {
            stale_threshold: Duration::from_millis(100),
            ..Default::default()
        };
        let monitor = HealthMonitor::new(config);
        monitor.register_subsystem("test".to_string());

        // Initial state - not stale
        let health = monitor.check_health("test").unwrap();
        assert!(!health.is_stale(Duration::from_millis(100)));

        // Wait and check stale
        sleep(Duration::from_millis(150));
        let health = monitor.check_health("test").unwrap();
        assert!(health.is_stale(Duration::from_millis(100)));
    }

    #[test]
    fn test_set_health_manually() {
        let monitor = HealthMonitor::default();
        monitor.register_subsystem("test".to_string());

        monitor
            .set_health("test", HealthStatus::Degraded)
            .unwrap();

        let health = monitor.check_health("test").unwrap();
        assert_eq!(health.status, HealthStatus::Degraded);
    }

    #[test]
    fn test_clear_subsystems() {
        let monitor = HealthMonitor::default();
        monitor.register_subsystem("test1".to_string());
        monitor.register_subsystem("test2".to_string());
        assert_eq!(monitor.subsystem_count(), 2);

        monitor.clear();
        assert_eq!(monitor.subsystem_count(), 0);
    }

    #[test]
    fn test_health_status_score() {
        assert_eq!(HealthStatus::Healthy.score(), 100);
        assert_eq!(HealthStatus::Degraded.score(), 50);
        assert_eq!(HealthStatus::Unhealthy.score(), 0);
    }

    #[test]
    fn test_subsystem_not_found_error() {
        let monitor = HealthMonitor::default();

        let result = monitor.check_health("nonexistent");
        assert!(result.is_err());
        assert!(matches!(result, Err(HealthError::SubsystemNotFound(_))));
    }
}