realizar 0.3.2

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
//! Model Warm-up and Pre-loading
//!
//! Reduces cold start latency by pre-loading models and running warm-up inference.
//!
//! ## Features
//!
//! - Pre-load models into memory before serving
//! - Run warm-up inference to JIT compile and optimize
//! - Validate model integrity before accepting traffic
//! - Background model loading for zero-downtime updates
//!
//! ## Example
//!
//! ```rust,ignore
//! use realizar::warmup::{WarmupConfig, ModelWarmer};
//!
//! let config = WarmupConfig::new()
//!     .with_warmup_iterations(3)
//!     .with_timeout(Duration::from_secs(30));
//!
//! let warmer = ModelWarmer::new(config);
//! warmer.warm_up(&model, &tokenizer).await?;
//! ```
//!
//! ## Toyota Way Principles
//!
//! - Heijunka: Level loading by pre-warming
//! - Jidoka: Validate model before serving
//! - Poka-Yoke: Prevent cold start errors

use std::{
    sync::{
        atomic::{AtomicBool, AtomicU64, Ordering},
        Arc,
    },
    time::{Duration, Instant},
};

use serde::{Deserialize, Serialize};

// ============================================================================
// WARM-001: Configuration
// ============================================================================

/// Configuration for model warm-up
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarmupConfig {
    /// Number of warm-up inference iterations
    pub warmup_iterations: usize,
    /// Timeout for warm-up process
    pub timeout: Duration,
    /// Sample prompt for warm-up inference
    pub sample_prompt: String,
    /// Maximum tokens for warm-up generation
    pub sample_max_tokens: usize,
    /// Validate model output during warm-up
    pub validate_output: bool,
    /// Run garbage collection after warm-up
    pub gc_after_warmup: bool,
    /// Log warm-up progress
    pub verbose: bool,
}

impl Default for WarmupConfig {
    fn default() -> Self {
        Self {
            warmup_iterations: 3,
            timeout: Duration::from_secs(60),
            sample_prompt: "Hello, world!".to_string(),
            sample_max_tokens: 10,
            validate_output: true,
            gc_after_warmup: true,
            verbose: false,
        }
    }
}

impl WarmupConfig {
    /// Create a new warm-up configuration
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set number of warm-up iterations
    #[must_use]
    pub fn with_warmup_iterations(mut self, n: usize) -> Self {
        self.warmup_iterations = n.max(1);
        self
    }

    /// Set warm-up timeout
    #[must_use]
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set sample prompt for warm-up
    #[must_use]
    pub fn with_sample_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.sample_prompt = prompt.into();
        self
    }

    /// Set maximum tokens for warm-up generation
    #[must_use]
    pub fn with_sample_max_tokens(mut self, n: usize) -> Self {
        self.sample_max_tokens = n;
        self
    }

    /// Enable/disable output validation
    #[must_use]
    pub fn with_validate_output(mut self, validate: bool) -> Self {
        self.validate_output = validate;
        self
    }

    /// Enable/disable garbage collection after warm-up
    #[must_use]
    pub fn with_gc_after_warmup(mut self, gc: bool) -> Self {
        self.gc_after_warmup = gc;
        self
    }

    /// Enable/disable verbose logging
    #[must_use]
    pub fn with_verbose(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }
}

// ============================================================================
// WARM-002: Warm-up Status
// ============================================================================

/// Status of warm-up process
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WarmupStatus {
    /// Not yet started
    NotStarted,
    /// Currently warming up
    InProgress,
    /// Warm-up completed successfully
    Ready,
    /// Warm-up failed
    Failed,
    /// Warm-up timed out
    TimedOut,
}

impl WarmupStatus {
    /// Check if model is ready to serve
    #[must_use]
    pub fn is_ready(&self) -> bool {
        matches!(self, Self::Ready)
    }

    /// Check if warm-up is still in progress
    #[must_use]
    pub fn is_in_progress(&self) -> bool {
        matches!(self, Self::InProgress)
    }

    /// Check if warm-up failed
    #[must_use]
    pub fn has_failed(&self) -> bool {
        matches!(self, Self::Failed | Self::TimedOut)
    }
}

// ============================================================================
// WARM-003: Warm-up Result
// ============================================================================

/// Result of warm-up process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarmupResult {
    /// Final status
    pub status: WarmupStatus,
    /// Number of iterations completed
    pub iterations_completed: usize,
    /// Total warm-up duration
    pub total_duration: Duration,
    /// Average inference latency during warm-up
    pub avg_latency: Duration,
    /// First inference latency (cold)
    pub first_latency: Duration,
    /// Last inference latency (warm)
    pub last_latency: Duration,
    /// Speedup factor (first / last)
    pub speedup_factor: f64,
    /// Error message if failed
    pub error: Option<String>,
}

impl WarmupResult {
    /// Create a successful result
    #[must_use]
    pub fn success(iterations: usize, duration: Duration, latencies: &[Duration]) -> Self {
        let first = latencies.first().copied().unwrap_or(Duration::ZERO);
        let last = latencies.last().copied().unwrap_or(Duration::ZERO);
        let avg = if latencies.is_empty() {
            Duration::ZERO
        } else {
            Duration::from_nanos(
                latencies.iter().map(|d| d.as_nanos() as u64).sum::<u64>() / latencies.len() as u64,
            )
        };

        let speedup = if last.as_nanos() > 0 {
            first.as_nanos() as f64 / last.as_nanos() as f64
        } else {
            1.0
        };

        Self {
            status: WarmupStatus::Ready,
            iterations_completed: iterations,
            total_duration: duration,
            avg_latency: avg,
            first_latency: first,
            last_latency: last,
            speedup_factor: speedup,
            error: None,
        }
    }

    /// Create a failed result
    #[must_use]
    pub fn failed(error: impl Into<String>, iterations: usize, duration: Duration) -> Self {
        Self {
            status: WarmupStatus::Failed,
            iterations_completed: iterations,
            total_duration: duration,
            avg_latency: Duration::ZERO,
            first_latency: Duration::ZERO,
            last_latency: Duration::ZERO,
            speedup_factor: 0.0,
            error: Some(error.into()),
        }
    }

    /// Create a timed out result
    #[must_use]
    pub fn timed_out(iterations: usize, duration: Duration) -> Self {
        Self {
            status: WarmupStatus::TimedOut,
            iterations_completed: iterations,
            total_duration: duration,
            avg_latency: Duration::ZERO,
            first_latency: Duration::ZERO,
            last_latency: Duration::ZERO,
            speedup_factor: 0.0,
            error: Some("Warm-up timed out".to_string()),
        }
    }
}

// ============================================================================
// WARM-004: Model Health
// ============================================================================

/// Model health status for readiness probes
#[derive(Debug, Clone)]
pub struct ModelHealth {
    /// Whether model is ready
    ready: Arc<AtomicBool>,
    /// Warm-up status
    status: Arc<std::sync::RwLock<WarmupStatus>>,
    /// Total requests served
    requests_served: Arc<AtomicU64>,
    /// Failed requests
    requests_failed: Arc<AtomicU64>,
    /// Last health check timestamp
    last_health_check: Arc<std::sync::RwLock<Instant>>,
    /// Model load timestamp
    loaded_at: Instant,
}

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

impl ModelHealth {
    /// Create new health tracker
    #[must_use]
    pub fn new() -> Self {
        Self {
            ready: Arc::new(AtomicBool::new(false)),
            status: Arc::new(std::sync::RwLock::new(WarmupStatus::NotStarted)),
            requests_served: Arc::new(AtomicU64::new(0)),
            requests_failed: Arc::new(AtomicU64::new(0)),
            last_health_check: Arc::new(std::sync::RwLock::new(Instant::now())),
            loaded_at: Instant::now(),
        }
    }

    /// Check if model is ready
    #[must_use]
    pub fn is_ready(&self) -> bool {
        self.ready.load(Ordering::Acquire)
    }

    /// Set ready status
    pub fn set_ready(&self, ready: bool) {
        self.ready.store(ready, Ordering::Release);
    }

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

    /// Set status
    pub fn set_status(&self, status: WarmupStatus) {
        *self.status.write().unwrap() = status;
        if status == WarmupStatus::Ready {
            self.set_ready(true);
        }
    }

    /// Record a successful request
    pub fn record_success(&self) {
        self.requests_served.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a failed request
    pub fn record_failure(&self) {
        self.requests_failed.fetch_add(1, Ordering::Relaxed);
    }

    /// Get total requests served
    #[must_use]
    pub fn total_requests(&self) -> u64 {
        self.requests_served.load(Ordering::Relaxed)
    }

    /// Get failed requests
    #[must_use]
    pub fn failed_requests(&self) -> u64 {
        self.requests_failed.load(Ordering::Relaxed)
    }

    /// Get error rate
    #[must_use]
    pub fn error_rate(&self) -> f64 {
        let total = self.total_requests();
        let failed = self.failed_requests();
        if total == 0 {
            0.0
        } else {
            failed as f64 / total as f64
        }
    }

    /// Update health check timestamp
    pub fn touch(&self) {
        *self.last_health_check.write().unwrap() = Instant::now();
    }

    /// Get uptime since model was loaded
    #[must_use]
    pub fn uptime(&self) -> Duration {
        self.loaded_at.elapsed()
    }

    /// Get time since last health check
    #[must_use]
    pub fn time_since_last_check(&self) -> Duration {
        self.last_health_check.read().unwrap().elapsed()
    }

    /// Generate health report
    #[must_use]
    pub fn report(&self) -> HealthReport {
        HealthReport {
            ready: self.is_ready(),
            status: self.status(),
            uptime_secs: self.uptime().as_secs_f64(),
            total_requests: self.total_requests(),
            failed_requests: self.failed_requests(),
            error_rate: self.error_rate(),
            time_since_last_check_secs: self.time_since_last_check().as_secs_f64(),
        }
    }
}

/// Health report for API responses
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthReport {
    /// Is model ready to serve
    pub ready: bool,
    /// Current warm-up status
    pub status: WarmupStatus,
    /// Uptime in seconds
    pub uptime_secs: f64,
    /// Total requests served
    pub total_requests: u64,
    /// Failed requests
    pub failed_requests: u64,
    /// Error rate (0.0 - 1.0)
    pub error_rate: f64,
    /// Time since last health check in seconds
    pub time_since_last_check_secs: f64,
}

// ============================================================================
// WARM-005: Warm-up Executor
// ============================================================================

/// Executes model warm-up process
#[derive(Debug, Clone)]
pub struct WarmupExecutor {
    config: WarmupConfig,
}

impl WarmupExecutor {
    /// Create a new warm-up executor
    #[must_use]
    pub fn new(config: WarmupConfig) -> Self {
        Self { config }
    }

    /// Get configuration
    #[must_use]
    pub fn config(&self) -> &WarmupConfig {
        &self.config
    }

    /// Simulate warm-up (for testing without actual model)
    ///
    /// This runs the warm-up process with test inference delays.
    #[must_use]
    pub fn simulate_warmup(&self) -> WarmupResult {
        let start = Instant::now();
        let mut latencies = Vec::with_capacity(self.config.warmup_iterations);

        // Simulate decreasing latency as model warms up
        for i in 0..self.config.warmup_iterations {
            // First iteration is "cold" (slower)
            let base_latency_us = if i == 0 { 1000 } else { 100 };
            let jitter = (i * 10) as u64;
            let latency = Duration::from_micros(base_latency_us - jitter.min(50));
            latencies.push(latency);
        }

        WarmupResult::success(self.config.warmup_iterations, start.elapsed(), &latencies)
    }

    /// Check if timeout has been exceeded
    #[allow(dead_code)]
    fn check_timeout(&self, start: Instant, iterations: usize) -> Option<WarmupResult> {
        if start.elapsed() > self.config.timeout {
            Some(WarmupResult::timed_out(iterations, start.elapsed()))
        } else {
            None
        }
    }
}

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

// ============================================================================
// WARM-006: Pre-load Configuration
// ============================================================================

/// Configuration for model pre-loading
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PreloadConfig {
    /// Models to pre-load on startup
    pub models: Vec<PreloadModelConfig>,
    /// Load models in parallel
    pub parallel_loading: bool,
    /// Maximum concurrent model loads
    pub max_concurrent: usize,
    /// Fail startup if any model fails to load
    pub fail_fast: bool,
}

impl Default for PreloadConfig {
    fn default() -> Self {
        Self {
            models: Vec::new(),
            parallel_loading: true,
            max_concurrent: 4,
            fail_fast: false,
        }
    }
}

impl PreloadConfig {
    /// Create new pre-load configuration
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a model to pre-load
    #[must_use]
    pub fn with_model(mut self, model: PreloadModelConfig) -> Self {
        self.models.push(model);
        self
    }

    /// Set parallel loading
    #[must_use]
    pub fn with_parallel_loading(mut self, parallel: bool) -> Self {
        self.parallel_loading = parallel;
        self
    }

    /// Set maximum concurrent loads
    #[must_use]
    pub fn with_max_concurrent(mut self, max: usize) -> Self {
        self.max_concurrent = max.max(1);
        self
    }

    /// Set fail-fast behavior
    #[must_use]
    pub fn with_fail_fast(mut self, fail_fast: bool) -> Self {
        self.fail_fast = fail_fast;
        self
    }
}

/// Configuration for a single model to pre-load
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PreloadModelConfig {
    /// Model identifier
    pub model_id: String,
    /// Model URI (pacha://, file://, hf://)
    pub uri: String,
    /// Priority (lower = load first)
    pub priority: u32,
    /// Run warm-up after loading
    pub warmup: bool,
    /// Warm-up configuration
    pub warmup_config: Option<WarmupConfig>,
}

impl PreloadModelConfig {
    /// Create new model pre-load config
    #[must_use]
    pub fn new(model_id: impl Into<String>, uri: impl Into<String>) -> Self {
        Self {
            model_id: model_id.into(),
            uri: uri.into(),
            priority: 100,
            warmup: true,
            warmup_config: None,
        }
    }

    /// Set priority
    #[must_use]
    pub fn with_priority(mut self, priority: u32) -> Self {
        self.priority = priority;
        self
    }

    /// Enable/disable warm-up
    #[must_use]
    pub fn with_warmup(mut self, warmup: bool) -> Self {
        self.warmup = warmup;
        self
    }

    /// Set warm-up configuration
    #[must_use]
    pub fn with_warmup_config(mut self, config: WarmupConfig) -> Self {
        self.warmup_config = Some(config);
        self.warmup = true;
        self
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    // ========================================================================
    // WARM-001: Configuration Tests
    // ========================================================================

    #[test]
    fn test_warmup_config_default() {
        let config = WarmupConfig::default();
        assert_eq!(config.warmup_iterations, 3);
        assert_eq!(config.timeout, Duration::from_secs(60));
        assert_eq!(config.sample_max_tokens, 10);
        assert!(config.validate_output);
    }

    #[test]
    fn test_warmup_config_builder() {
        let config = WarmupConfig::new()
            .with_warmup_iterations(5)
            .with_timeout(Duration::from_secs(120))
            .with_sample_prompt("Test prompt")
            .with_sample_max_tokens(20)
            .with_validate_output(false)
            .with_verbose(true);

        assert_eq!(config.warmup_iterations, 5);
        assert_eq!(config.timeout, Duration::from_secs(120));
        assert_eq!(config.sample_prompt, "Test prompt");
        assert_eq!(config.sample_max_tokens, 20);
        assert!(!config.validate_output);
        assert!(config.verbose);
    }

    #[test]
    fn test_warmup_config_min_iterations() {
        let config = WarmupConfig::new().with_warmup_iterations(0);
        assert_eq!(config.warmup_iterations, 1);
    }

    // ========================================================================
    // WARM-002: Status Tests
    // ========================================================================

    #[test]
    fn test_warmup_status_is_ready() {
        assert!(!WarmupStatus::NotStarted.is_ready());
        assert!(!WarmupStatus::InProgress.is_ready());
        assert!(WarmupStatus::Ready.is_ready());
        assert!(!WarmupStatus::Failed.is_ready());
        assert!(!WarmupStatus::TimedOut.is_ready());
    }

    #[test]
    fn test_warmup_status_is_in_progress() {
        assert!(!WarmupStatus::NotStarted.is_in_progress());
        assert!(WarmupStatus::InProgress.is_in_progress());
        assert!(!WarmupStatus::Ready.is_in_progress());
    }

    #[test]
    fn test_warmup_status_has_failed() {
        assert!(!WarmupStatus::NotStarted.has_failed());
        assert!(!WarmupStatus::InProgress.has_failed());
        assert!(!WarmupStatus::Ready.has_failed());
        assert!(WarmupStatus::Failed.has_failed());
        assert!(WarmupStatus::TimedOut.has_failed());
    }

    // ========================================================================
    // WARM-003: Result Tests
    // ========================================================================

    #[test]
    fn test_warmup_result_success() {
        let latencies = vec![
            Duration::from_millis(100),
            Duration::from_millis(50),
            Duration::from_millis(25),
        ];

        let result = WarmupResult::success(3, Duration::from_millis(200), &latencies);

        assert_eq!(result.status, WarmupStatus::Ready);
        assert_eq!(result.iterations_completed, 3);
        assert_eq!(result.first_latency, Duration::from_millis(100));
        assert_eq!(result.last_latency, Duration::from_millis(25));
        assert!(result.speedup_factor > 1.0);
        assert!(result.error.is_none());
    }

    #[test]
    fn test_warmup_result_failed() {
        let result = WarmupResult::failed("Test error", 2, Duration::from_secs(5));

        assert_eq!(result.status, WarmupStatus::Failed);
        assert_eq!(result.iterations_completed, 2);
        assert_eq!(result.error, Some("Test error".to_string()));
    }

    #[test]
    fn test_warmup_result_timed_out() {
        let result = WarmupResult::timed_out(1, Duration::from_secs(60));

        assert_eq!(result.status, WarmupStatus::TimedOut);
        assert!(result.error.is_some());
        assert!(result.error.unwrap().contains("timed out"));
    }

    #[test]
    fn test_warmup_result_empty_latencies() {
        let result = WarmupResult::success(0, Duration::ZERO, &[]);

        assert_eq!(result.first_latency, Duration::ZERO);
        assert_eq!(result.last_latency, Duration::ZERO);
        assert!((result.speedup_factor - 1.0).abs() < f64::EPSILON);
    }

    // ========================================================================
    // WARM-004: Health Tests
    // ========================================================================

    #[test]
    fn test_model_health_new() {
        let health = ModelHealth::new();
        assert!(!health.is_ready());
        assert_eq!(health.status(), WarmupStatus::NotStarted);
        assert_eq!(health.total_requests(), 0);
    }

    #[test]
    fn test_model_health_set_ready() {
        let health = ModelHealth::new();
        health.set_ready(true);
        assert!(health.is_ready());

        health.set_ready(false);
        assert!(!health.is_ready());
    }

    #[test]
    fn test_model_health_set_status() {
        let health = ModelHealth::new();
        health.set_status(WarmupStatus::InProgress);
        assert_eq!(health.status(), WarmupStatus::InProgress);
        assert!(!health.is_ready());

        health.set_status(WarmupStatus::Ready);
        assert_eq!(health.status(), WarmupStatus::Ready);
        assert!(health.is_ready());
    }

    #[test]
    fn test_model_health_record_requests() {
        let health = ModelHealth::new();

        health.record_success();
        health.record_success();
        health.record_failure();

        assert_eq!(health.total_requests(), 2);
        assert_eq!(health.failed_requests(), 1);
    }

    #[test]
    fn test_model_health_error_rate() {
        let health = ModelHealth::new();

        // No requests yet
        assert!((health.error_rate() - 0.0).abs() < f64::EPSILON);

        // 2 success, 1 failure
        health.record_success();
        health.record_success();
        health.record_failure();

        // Error rate should be 0 since failed_requests/total_requests
        // But total_requests only counts successes
        // So error rate = 1 / 2 = 0.5... wait, let me check the implementation
        // Actually: total_requests counts successes, error_rate = failed / total
        // So 1 / 2 = 0.5
        assert!((health.error_rate() - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_model_health_touch() {
        let health = ModelHealth::new();
        std::thread::sleep(Duration::from_millis(10));
        let before = health.time_since_last_check();
        health.touch();
        let after = health.time_since_last_check();
        assert!(after < before);
    }

    #[test]
    fn test_model_health_report() {
        let health = ModelHealth::new();
        health.set_status(WarmupStatus::Ready);
        health.record_success();

        let report = health.report();
        assert!(report.ready);
        assert_eq!(report.status, WarmupStatus::Ready);
        assert_eq!(report.total_requests, 1);
        assert_eq!(report.failed_requests, 0);
        assert!(report.uptime_secs >= 0.0);
    }

    // ========================================================================
    // WARM-005: Executor Tests
    // ========================================================================

    #[test]
    fn test_warmup_executor_new() {
        let config = WarmupConfig::new().with_warmup_iterations(5);
        let executor = WarmupExecutor::new(config.clone());
        assert_eq!(executor.config().warmup_iterations, 5);
    }

    #[test]
    fn test_warmup_executor_simulate() {
        let config = WarmupConfig::new().with_warmup_iterations(3);
        let executor = WarmupExecutor::new(config);

        let result = executor.simulate_warmup();

        assert_eq!(result.status, WarmupStatus::Ready);
        assert_eq!(result.iterations_completed, 3);
        assert!(result.first_latency > Duration::ZERO);
        assert!(result.last_latency > Duration::ZERO);
        // First (cold) should be slower than last (warm)
        assert!(result.first_latency > result.last_latency);
        assert!(result.speedup_factor > 1.0);
    }

    #[test]
    fn test_warmup_executor_check_timeout() {
        let config = WarmupConfig::new().with_timeout(Duration::from_millis(1));
        let executor = WarmupExecutor::new(config);

        let start = Instant::now();
        std::thread::sleep(Duration::from_millis(10));

        let result = executor.check_timeout(start, 0);
        assert!(result.is_some());
        assert_eq!(result.unwrap().status, WarmupStatus::TimedOut);
    }

    #[test]
    fn test_warmup_executor_default() {
        let executor = WarmupExecutor::default();
        assert_eq!(executor.config().warmup_iterations, 3);
    }

    // ========================================================================
    // WARM-006: Preload Config Tests
    // ========================================================================

    #[test]
    fn test_preload_config_default() {
        let config = PreloadConfig::default();
        assert!(config.models.is_empty());
        assert!(config.parallel_loading);
        assert_eq!(config.max_concurrent, 4);
        assert!(!config.fail_fast);
    }

    #[test]
    fn test_preload_config_builder() {
        let model = PreloadModelConfig::new("llama", "pacha://llama:7b");

        let config = PreloadConfig::new()
            .with_model(model)
            .with_parallel_loading(false)
            .with_max_concurrent(2)
            .with_fail_fast(true);

        assert_eq!(config.models.len(), 1);
        assert!(!config.parallel_loading);
        assert_eq!(config.max_concurrent, 2);
        assert!(config.fail_fast);
    }

    #[test]
    fn test_preload_config_min_concurrent() {
        let config = PreloadConfig::new().with_max_concurrent(0);
        assert_eq!(config.max_concurrent, 1);
    }

    #[test]
    fn test_preload_model_config_new() {
        let model = PreloadModelConfig::new("gpt2", "hf://gpt2");
        assert_eq!(model.model_id, "gpt2");
        assert_eq!(model.uri, "hf://gpt2");
        assert_eq!(model.priority, 100);
        assert!(model.warmup);
        assert!(model.warmup_config.is_none());
    }

    #[test]
    fn test_preload_model_config_builder() {
        let warmup_config = WarmupConfig::new().with_warmup_iterations(5);

        let model = PreloadModelConfig::new("llama", "file://model.gguf")
            .with_priority(10)
            .with_warmup(true)
            .with_warmup_config(warmup_config);

        assert_eq!(model.priority, 10);
        assert!(model.warmup);
        assert!(model.warmup_config.is_some());
        assert_eq!(model.warmup_config.unwrap().warmup_iterations, 5);
    }

    #[test]
    fn test_preload_model_config_with_warmup_enables_warmup() {
        let model = PreloadModelConfig::new("test", "file://test.gguf")
            .with_warmup(false)
            .with_warmup_config(WarmupConfig::new());

        // Setting warmup_config should enable warmup
        assert!(model.warmup);
    }

    // ========================================================================
    // Serialization Tests
    // ========================================================================

    #[test]
    fn test_warmup_config_serialization() {
        let config = WarmupConfig::new()
            .with_warmup_iterations(5)
            .with_sample_prompt("Hello");

        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("5"));
        assert!(json.contains("Hello"));

        let deserialized: WarmupConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.warmup_iterations, 5);
    }

    #[test]
    fn test_warmup_status_serialization() {
        let status = WarmupStatus::Ready;
        let json = serde_json::to_string(&status).unwrap();
        let deserialized: WarmupStatus = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, WarmupStatus::Ready);
    }

    #[test]
    fn test_warmup_result_serialization() {
        let result =
            WarmupResult::success(3, Duration::from_millis(100), &[Duration::from_millis(50)]);

        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("Ready"));
        assert!(json.contains('3'));
    }

    #[test]
    fn test_health_report_serialization() {
        let report = HealthReport {
            ready: true,
            status: WarmupStatus::Ready,
            uptime_secs: 100.0,
            total_requests: 1000,
            failed_requests: 5,
            error_rate: 0.005,
            time_since_last_check_secs: 1.5,
        };

        let json = serde_json::to_string(&report).unwrap();
        assert!(json.contains("true"));
        assert!(json.contains("1000"));
        assert!(json.contains("0.005"));
    }
}