pepita 0.1.0

Tiny First-Principles Rust Kernel for Sovereign AI - io_uring/ublk/blk-mq interfaces
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! Fault tolerance mechanisms for distributed execution.
//!
//! This module implements fault detection and recovery strategies
//! based on the Chandra-Toueg failure detector model.
//!
//! ## Features
//!
//! - Heartbeat-based failure detection
//! - Retry with exponential backoff
//! - Worker health tracking
//! - Task migration on failure
//!
//! ## Reference
//!
//! Chandra, T. D., & Toueg, S. (1996). "Unreliable Failure Detectors
//! for Reliable Distributed Systems." Journal of the ACM, 43(2).

use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::RwLock;
use std::time::{Duration, Instant};

use crate::scheduler::WorkerId;

// ============================================================================
// RETRY POLICY
// ============================================================================

/// Retry policy for failed tasks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RetryPolicy {
    /// Maximum retry attempts
    pub max_retries: u32,
    /// Initial delay between retries
    pub initial_delay: Duration,
    /// Maximum delay between retries
    pub max_delay: Duration,
    /// Backoff multiplier
    pub backoff_factor: u32,
    /// Whether to use jitter
    pub use_jitter: bool,
}

impl RetryPolicy {
    /// Create a new retry policy with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a policy with no retries.
    #[must_use]
    pub fn no_retry() -> Self {
        Self {
            max_retries: 0,
            ..Self::default()
        }
    }

    /// Create a policy for critical tasks (more retries, longer delays).
    #[must_use]
    pub fn critical() -> Self {
        Self {
            max_retries: 10,
            initial_delay: Duration::from_millis(500),
            max_delay: Duration::from_secs(60),
            backoff_factor: 2,
            use_jitter: true,
        }
    }

    /// Set maximum retries.
    #[must_use]
    pub const fn with_max_retries(mut self, retries: u32) -> Self {
        self.max_retries = retries;
        self
    }

    /// Set initial delay.
    #[must_use]
    pub const fn with_initial_delay(mut self, delay: Duration) -> Self {
        self.initial_delay = delay;
        self
    }

    /// Set maximum delay.
    #[must_use]
    pub const fn with_max_delay(mut self, delay: Duration) -> Self {
        self.max_delay = delay;
        self
    }

    /// Calculate delay for a given retry attempt.
    #[must_use]
    pub fn delay_for_attempt(&self, attempt: u32) -> Duration {
        if attempt == 0 {
            return Duration::ZERO;
        }

        let base_delay = self.initial_delay.as_millis() as u64;
        let factor = self.backoff_factor.pow(attempt.saturating_sub(1)) as u64;
        let delay_ms = base_delay.saturating_mul(factor);

        let capped_ms = delay_ms.min(self.max_delay.as_millis() as u64);

        Duration::from_millis(capped_ms)
    }

    /// Check if should retry given current attempt count.
    #[must_use]
    pub const fn should_retry(&self, current_attempts: u32) -> bool {
        current_attempts < self.max_retries
    }
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_retries: 3,
            initial_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(30),
            backoff_factor: 2,
            use_jitter: true,
        }
    }
}

// ============================================================================
// RETRY STATE
// ============================================================================

/// State for tracking retry attempts.
#[derive(Debug, Clone)]
pub struct RetryState {
    /// Number of attempts made
    pub attempts: u32,
    /// Last failure time
    pub last_failure: Option<Instant>,
    /// Last error message
    pub last_error: Option<String>,
}

impl RetryState {
    /// Create a new retry state.
    #[must_use]
    pub fn new() -> Self {
        Self {
            attempts: 0,
            last_failure: None,
            last_error: None,
        }
    }

    /// Record a failure.
    pub fn record_failure(&mut self, error: impl Into<String>) {
        self.attempts += 1;
        self.last_failure = Some(Instant::now());
        self.last_error = Some(error.into());
    }

    /// Reset the state (e.g., after success).
    pub fn reset(&mut self) {
        self.attempts = 0;
        self.last_failure = None;
        self.last_error = None;
    }

    /// Check if should retry given a policy.
    #[must_use]
    pub fn should_retry(&self, policy: &RetryPolicy) -> bool {
        policy.should_retry(self.attempts)
    }

    /// Get delay before next retry.
    #[must_use]
    pub fn next_delay(&self, policy: &RetryPolicy) -> Duration {
        policy.delay_for_attempt(self.attempts)
    }

    /// Check if currently in backoff period.
    #[must_use]
    pub fn in_backoff(&self, policy: &RetryPolicy) -> bool {
        if let Some(last) = self.last_failure {
            let delay = self.next_delay(policy);
            last.elapsed() < delay
        } else {
            false
        }
    }
}

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

// ============================================================================
// WORKER HEALTH
// ============================================================================

/// Health status of a worker.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HealthStatus {
    /// Worker is healthy
    Healthy,
    /// Worker is suspected (missed some heartbeats)
    Suspected,
    /// Worker is marked as failed
    Failed,
    /// Worker is unknown/new
    Unknown,
}

impl HealthStatus {
    /// Check if worker is considered available.
    #[must_use]
    pub const fn is_available(self) -> bool {
        matches!(self, Self::Healthy | Self::Unknown)
    }
}

/// Health information for a worker.
#[derive(Debug, Clone)]
pub struct WorkerHealth {
    /// Worker ID
    pub worker_id: WorkerId,
    /// Current status
    pub status: HealthStatus,
    /// Last heartbeat time
    pub last_heartbeat: Option<Instant>,
    /// Consecutive failures
    pub consecutive_failures: u32,
    /// Total tasks completed
    pub tasks_completed: u64,
    /// Total tasks failed
    pub tasks_failed: u64,
}

impl WorkerHealth {
    /// Create a new health record.
    #[must_use]
    pub fn new(worker_id: WorkerId) -> Self {
        Self {
            worker_id,
            status: HealthStatus::Unknown,
            last_heartbeat: None,
            consecutive_failures: 0,
            tasks_completed: 0,
            tasks_failed: 0,
        }
    }

    /// Record a heartbeat.
    pub fn record_heartbeat(&mut self) {
        self.last_heartbeat = Some(Instant::now());
        self.status = HealthStatus::Healthy;
        self.consecutive_failures = 0;
    }

    /// Record a task completion.
    pub fn record_task_completion(&mut self, success: bool) {
        if success {
            self.tasks_completed += 1;
            self.consecutive_failures = 0;
        } else {
            self.tasks_failed += 1;
            self.consecutive_failures += 1;
        }
    }

    /// Update status based on heartbeat timeout.
    pub fn update_status(&mut self, heartbeat_timeout: Duration, failure_threshold: u32) {
        if let Some(last) = self.last_heartbeat {
            let elapsed = last.elapsed();
            if elapsed > heartbeat_timeout * 3 {
                self.status = HealthStatus::Failed;
            } else if elapsed > heartbeat_timeout {
                self.status = HealthStatus::Suspected;
            }
        }

        if self.consecutive_failures >= failure_threshold {
            self.status = HealthStatus::Failed;
        }
    }

    /// Get success rate.
    #[must_use]
    pub fn success_rate(&self) -> f64 {
        let total = self.tasks_completed + self.tasks_failed;
        if total == 0 {
            return 1.0;
        }
        self.tasks_completed as f64 / total as f64
    }
}

// ============================================================================
// FAILURE DETECTOR
// ============================================================================

/// Heartbeat-based failure detector.
///
/// Implements an unreliable failure detector following the
/// Chandra-Toueg model.
#[derive(Debug)]
pub struct FailureDetector {
    /// Worker health records
    workers: RwLock<HashMap<WorkerId, WorkerHealth>>,
    /// Heartbeat interval
    heartbeat_interval: Duration,
    /// Heartbeat timeout (consider suspect after this)
    heartbeat_timeout: Duration,
    /// Failure threshold (consecutive failures before marking failed)
    failure_threshold: u32,
    /// Whether detector is running
    running: AtomicBool,
}

impl FailureDetector {
    /// Create a new failure detector.
    #[must_use]
    pub fn new() -> Self {
        Self::with_config(
            Duration::from_secs(1),
            Duration::from_secs(3),
            3,
        )
    }

    /// Create with custom configuration.
    #[must_use]
    pub fn with_config(
        heartbeat_interval: Duration,
        heartbeat_timeout: Duration,
        failure_threshold: u32,
    ) -> Self {
        Self {
            workers: RwLock::new(HashMap::new()),
            heartbeat_interval,
            heartbeat_timeout,
            failure_threshold,
            running: AtomicBool::new(true),
        }
    }

    /// Register a worker.
    pub fn register_worker(&self, worker_id: WorkerId) {
        let mut workers = self.workers.write().unwrap();
        workers.insert(worker_id, WorkerHealth::new(worker_id));
    }

    /// Deregister a worker.
    pub fn deregister_worker(&self, worker_id: WorkerId) {
        let mut workers = self.workers.write().unwrap();
        workers.remove(&worker_id);
    }

    /// Record a heartbeat from a worker.
    pub fn record_heartbeat(&self, worker_id: WorkerId) {
        let mut workers = self.workers.write().unwrap();
        if let Some(health) = workers.get_mut(&worker_id) {
            health.record_heartbeat();
        } else {
            // Auto-register on first heartbeat
            let mut health = WorkerHealth::new(worker_id);
            health.record_heartbeat();
            workers.insert(worker_id, health);
        }
    }

    /// Record task completion.
    pub fn record_task_result(&self, worker_id: WorkerId, success: bool) {
        let mut workers = self.workers.write().unwrap();
        if let Some(health) = workers.get_mut(&worker_id) {
            health.record_task_completion(success);
        }
    }

    /// Get worker health status.
    #[must_use]
    pub fn get_status(&self, worker_id: WorkerId) -> HealthStatus {
        let workers = self.workers.read().unwrap();
        workers
            .get(&worker_id)
            .map(|h| h.status)
            .unwrap_or(HealthStatus::Unknown)
    }

    /// Get worker health record.
    #[must_use]
    pub fn get_health(&self, worker_id: WorkerId) -> Option<WorkerHealth> {
        let workers = self.workers.read().unwrap();
        workers.get(&worker_id).cloned()
    }

    /// Get all healthy workers.
    #[must_use]
    pub fn healthy_workers(&self) -> Vec<WorkerId> {
        let workers = self.workers.read().unwrap();
        workers
            .iter()
            .filter(|(_, h)| h.status == HealthStatus::Healthy)
            .map(|(id, _)| *id)
            .collect()
    }

    /// Get all failed workers.
    #[must_use]
    pub fn failed_workers(&self) -> Vec<WorkerId> {
        let workers = self.workers.read().unwrap();
        workers
            .iter()
            .filter(|(_, h)| h.status == HealthStatus::Failed)
            .map(|(id, _)| *id)
            .collect()
    }

    /// Update all worker statuses based on heartbeat timeouts.
    pub fn update_all_statuses(&self) {
        let mut workers = self.workers.write().unwrap();
        for health in workers.values_mut() {
            health.update_status(self.heartbeat_timeout, self.failure_threshold);
        }
    }

    /// Check if a worker is available.
    #[must_use]
    pub fn is_available(&self, worker_id: WorkerId) -> bool {
        self.get_status(worker_id).is_available()
    }

    /// Get the heartbeat interval.
    #[must_use]
    pub const fn heartbeat_interval(&self) -> Duration {
        self.heartbeat_interval
    }

    /// Stop the detector.
    pub fn stop(&self) {
        self.running.store(false, Ordering::Release);
    }

    /// Check if running.
    #[must_use]
    pub fn is_running(&self) -> bool {
        self.running.load(Ordering::Acquire)
    }
}

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

// ============================================================================
// CIRCUIT BREAKER
// ============================================================================

/// Circuit breaker state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    /// Circuit is closed (normal operation)
    Closed,
    /// Circuit is open (failing fast)
    Open,
    /// Circuit is half-open (testing)
    HalfOpen,
}

/// Circuit breaker for protecting against cascading failures.
#[derive(Debug)]
pub struct CircuitBreaker {
    /// Current state
    state: RwLock<CircuitState>,
    /// Failure count
    failure_count: AtomicU64,
    /// Success count (for half-open)
    success_count: AtomicU64,
    /// Failure threshold to open
    failure_threshold: u64,
    /// Success threshold to close (from half-open)
    success_threshold: u64,
    /// Time to wait in open state before half-open
    reset_timeout: Duration,
    /// Time circuit was opened
    opened_at: RwLock<Option<Instant>>,
}

impl CircuitBreaker {
    /// Create a new circuit breaker.
    #[must_use]
    pub fn new(failure_threshold: u64, success_threshold: u64, reset_timeout: Duration) -> Self {
        Self {
            state: RwLock::new(CircuitState::Closed),
            failure_count: AtomicU64::new(0),
            success_count: AtomicU64::new(0),
            failure_threshold,
            success_threshold,
            reset_timeout,
            opened_at: RwLock::new(None),
        }
    }

    /// Get current state.
    #[must_use]
    pub fn state(&self) -> CircuitState {
        *self.state.read().unwrap()
    }

    /// Check if circuit allows execution.
    #[must_use]
    pub fn allows_execution(&self) -> bool {
        let state = *self.state.read().unwrap();
        match state {
            CircuitState::Closed => true,
            CircuitState::HalfOpen => true,
            CircuitState::Open => {
                // Check if we should transition to half-open
                let opened_at = self.opened_at.read().unwrap();
                if let Some(opened) = *opened_at {
                    if opened.elapsed() >= self.reset_timeout {
                        drop(opened_at);
                        self.transition_to_half_open();
                        return true;
                    }
                }
                false
            }
        }
    }

    /// Record a success.
    pub fn record_success(&self) {
        let state = *self.state.read().unwrap();
        match state {
            CircuitState::Closed => {
                self.failure_count.store(0, Ordering::Release);
            }
            CircuitState::HalfOpen => {
                let count = self.success_count.fetch_add(1, Ordering::AcqRel) + 1;
                if count >= self.success_threshold {
                    self.transition_to_closed();
                }
            }
            CircuitState::Open => {}
        }
    }

    /// Record a failure.
    pub fn record_failure(&self) {
        let state = *self.state.read().unwrap();
        match state {
            CircuitState::Closed => {
                let count = self.failure_count.fetch_add(1, Ordering::AcqRel) + 1;
                if count >= self.failure_threshold {
                    self.transition_to_open();
                }
            }
            CircuitState::HalfOpen => {
                self.transition_to_open();
            }
            CircuitState::Open => {}
        }
    }

    fn transition_to_open(&self) {
        let mut state = self.state.write().unwrap();
        *state = CircuitState::Open;
        let mut opened_at = self.opened_at.write().unwrap();
        *opened_at = Some(Instant::now());
        self.success_count.store(0, Ordering::Release);
    }

    fn transition_to_half_open(&self) {
        let mut state = self.state.write().unwrap();
        *state = CircuitState::HalfOpen;
        self.success_count.store(0, Ordering::Release);
        self.failure_count.store(0, Ordering::Release);
    }

    fn transition_to_closed(&self) {
        let mut state = self.state.write().unwrap();
        *state = CircuitState::Closed;
        let mut opened_at = self.opened_at.write().unwrap();
        *opened_at = None;
        self.failure_count.store(0, Ordering::Release);
        self.success_count.store(0, Ordering::Release);
    }

    /// Reset the circuit breaker.
    pub fn reset(&self) {
        self.transition_to_closed();
    }
}

impl Default for CircuitBreaker {
    fn default() -> Self {
        Self::new(5, 3, Duration::from_secs(30))
    }
}

// ============================================================================
// TESTS (EXTREME TDD)
// ============================================================================

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

    // ------------------------------------------------------------------------
    // RetryPolicy Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_retry_policy_default() {
        let policy = RetryPolicy::default();
        assert_eq!(policy.max_retries, 3);
        assert!(policy.should_retry(0));
        assert!(policy.should_retry(2));
        assert!(!policy.should_retry(3));
    }

    #[test]
    fn test_retry_policy_no_retry() {
        let policy = RetryPolicy::no_retry();
        assert_eq!(policy.max_retries, 0);
        assert!(!policy.should_retry(0));
    }

    #[test]
    fn test_retry_policy_critical() {
        let policy = RetryPolicy::critical();
        assert_eq!(policy.max_retries, 10);
        assert!(policy.should_retry(9));
        assert!(!policy.should_retry(10));
    }

    #[test]
    fn test_retry_policy_delay() {
        let policy = RetryPolicy::new()
            .with_initial_delay(Duration::from_millis(100))
            .with_backoff_factor(2);

        assert_eq!(policy.delay_for_attempt(0), Duration::ZERO);
        assert_eq!(policy.delay_for_attempt(1), Duration::from_millis(100));
        assert_eq!(policy.delay_for_attempt(2), Duration::from_millis(200));
        assert_eq!(policy.delay_for_attempt(3), Duration::from_millis(400));
    }

    #[test]
    fn test_retry_policy_delay_capped() {
        let policy = RetryPolicy::new()
            .with_initial_delay(Duration::from_secs(1))
            .with_max_delay(Duration::from_secs(5))
            .with_backoff_factor(2);

        // At attempt 4: 1 * 2^3 = 8 seconds, but capped at 5
        assert_eq!(policy.delay_for_attempt(4), Duration::from_secs(5));
    }

    impl RetryPolicy {
        fn with_backoff_factor(mut self, factor: u32) -> Self {
            self.backoff_factor = factor;
            self
        }
    }

    // ------------------------------------------------------------------------
    // RetryState Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_retry_state_new() {
        let state = RetryState::new();
        assert_eq!(state.attempts, 0);
        assert!(state.last_failure.is_none());
        assert!(state.last_error.is_none());
    }

    #[test]
    fn test_retry_state_record_failure() {
        let mut state = RetryState::new();
        state.record_failure("Error 1");
        assert_eq!(state.attempts, 1);
        assert!(state.last_failure.is_some());
        assert_eq!(state.last_error, Some("Error 1".to_string()));

        state.record_failure("Error 2");
        assert_eq!(state.attempts, 2);
        assert_eq!(state.last_error, Some("Error 2".to_string()));
    }

    #[test]
    fn test_retry_state_reset() {
        let mut state = RetryState::new();
        state.record_failure("Error");
        state.reset();
        assert_eq!(state.attempts, 0);
        assert!(state.last_failure.is_none());
    }

    #[test]
    fn test_retry_state_should_retry() {
        let policy = RetryPolicy::new().with_max_retries(2);
        let mut state = RetryState::new();

        assert!(state.should_retry(&policy));
        state.record_failure("Error");
        assert!(state.should_retry(&policy));
        state.record_failure("Error");
        assert!(!state.should_retry(&policy));
    }

    // ------------------------------------------------------------------------
    // WorkerHealth Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_worker_health_new() {
        let health = WorkerHealth::new(WorkerId(1));
        assert_eq!(health.worker_id, WorkerId(1));
        assert_eq!(health.status, HealthStatus::Unknown);
        assert!(health.last_heartbeat.is_none());
    }

    #[test]
    fn test_worker_health_record_heartbeat() {
        let mut health = WorkerHealth::new(WorkerId(1));
        health.record_heartbeat();
        assert_eq!(health.status, HealthStatus::Healthy);
        assert!(health.last_heartbeat.is_some());
    }

    #[test]
    fn test_worker_health_task_completion() {
        let mut health = WorkerHealth::new(WorkerId(1));

        health.record_task_completion(true);
        assert_eq!(health.tasks_completed, 1);
        assert_eq!(health.tasks_failed, 0);

        health.record_task_completion(false);
        assert_eq!(health.tasks_completed, 1);
        assert_eq!(health.tasks_failed, 1);
    }

    #[test]
    fn test_worker_health_success_rate() {
        let mut health = WorkerHealth::new(WorkerId(1));
        assert_eq!(health.success_rate(), 1.0);

        health.record_task_completion(true);
        health.record_task_completion(true);
        health.record_task_completion(false);
        assert!((health.success_rate() - 0.666).abs() < 0.01);
    }

    #[test]
    fn test_health_status_is_available() {
        assert!(HealthStatus::Healthy.is_available());
        assert!(HealthStatus::Unknown.is_available());
        assert!(!HealthStatus::Suspected.is_available());
        assert!(!HealthStatus::Failed.is_available());
    }

    // ------------------------------------------------------------------------
    // FailureDetector Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_failure_detector_new() {
        let detector = FailureDetector::new();
        assert!(detector.is_running());
    }

    #[test]
    fn test_failure_detector_register() {
        let detector = FailureDetector::new();
        detector.register_worker(WorkerId(1));

        assert_eq!(detector.get_status(WorkerId(1)), HealthStatus::Unknown);
    }

    #[test]
    fn test_failure_detector_heartbeat() {
        let detector = FailureDetector::new();
        detector.register_worker(WorkerId(1));
        detector.record_heartbeat(WorkerId(1));

        assert_eq!(detector.get_status(WorkerId(1)), HealthStatus::Healthy);
    }

    #[test]
    fn test_failure_detector_healthy_workers() {
        let detector = FailureDetector::new();
        detector.register_worker(WorkerId(1));
        detector.register_worker(WorkerId(2));

        detector.record_heartbeat(WorkerId(1));

        let healthy = detector.healthy_workers();
        assert_eq!(healthy.len(), 1);
        assert!(healthy.contains(&WorkerId(1)));
    }

    #[test]
    fn test_failure_detector_auto_register() {
        let detector = FailureDetector::new();
        detector.record_heartbeat(WorkerId(99));

        assert_eq!(detector.get_status(WorkerId(99)), HealthStatus::Healthy);
    }

    #[test]
    fn test_failure_detector_deregister() {
        let detector = FailureDetector::new();
        detector.register_worker(WorkerId(1));
        detector.deregister_worker(WorkerId(1));

        assert_eq!(detector.get_status(WorkerId(1)), HealthStatus::Unknown);
    }

    // ------------------------------------------------------------------------
    // CircuitBreaker Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_circuit_breaker_initial_state() {
        let cb = CircuitBreaker::default();
        assert_eq!(cb.state(), CircuitState::Closed);
        assert!(cb.allows_execution());
    }

    #[test]
    fn test_circuit_breaker_opens_on_failures() {
        let cb = CircuitBreaker::new(3, 2, Duration::from_secs(1));

        cb.record_failure();
        cb.record_failure();
        assert_eq!(cb.state(), CircuitState::Closed);

        cb.record_failure();
        assert_eq!(cb.state(), CircuitState::Open);
        assert!(!cb.allows_execution());
    }

    #[test]
    fn test_circuit_breaker_success_resets_count() {
        let cb = CircuitBreaker::new(3, 2, Duration::from_secs(1));

        cb.record_failure();
        cb.record_failure();
        cb.record_success();
        cb.record_failure();
        cb.record_failure();

        // Should still be closed (success reset the count)
        assert_eq!(cb.state(), CircuitState::Closed);
    }

    #[test]
    fn test_circuit_breaker_half_open_closes() {
        let cb = CircuitBreaker::new(1, 2, Duration::from_millis(10));

        cb.record_failure();
        assert_eq!(cb.state(), CircuitState::Open);

        std::thread::sleep(Duration::from_millis(20));
        assert!(cb.allows_execution()); // Should transition to half-open

        cb.record_success();
        cb.record_success();
        assert_eq!(cb.state(), CircuitState::Closed);
    }

    #[test]
    fn test_circuit_breaker_half_open_reopens_on_failure() {
        let cb = CircuitBreaker::new(1, 2, Duration::from_millis(10));

        cb.record_failure();
        std::thread::sleep(Duration::from_millis(20));
        let _ = cb.allows_execution(); // Trigger half-open

        cb.record_failure();
        assert_eq!(cb.state(), CircuitState::Open);
    }

    #[test]
    fn test_circuit_breaker_reset() {
        let cb = CircuitBreaker::new(1, 2, Duration::from_secs(100));

        cb.record_failure();
        assert_eq!(cb.state(), CircuitState::Open);

        cb.reset();
        assert_eq!(cb.state(), CircuitState::Closed);
        assert!(cb.allows_execution());
    }
}