depyler 4.1.1

A Python-to-Rust transpiler focusing on energy-efficient, safe code generation with progressive verification
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
//! Compilation Trainer Shim - pure logic separated from I/O
//!
//! Extracts testable logic from compilation_trainer.rs

use std::collections::HashMap;

/// Diagnostic verbosity tier for CITL training
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DiagnosticTier {
    #[default]
    Tier1,
    Tier2,
    Tier3,
    Tier4,
}

impl DiagnosticTier {
    /// Convert tier number (1-4) to enum
    pub fn from_level(level: u8) -> Self {
        match level {
            1 => Self::Tier1,
            2 => Self::Tier2,
            3 => Self::Tier3,
            4.. => Self::Tier4,
            _ => Self::Tier1,
        }
    }

    /// Get tier number (1-4)
    pub fn level(&self) -> u8 {
        match self {
            Self::Tier1 => 1,
            Self::Tier2 => 2,
            Self::Tier3 => 3,
            Self::Tier4 => 4,
        }
    }

    /// Get overhead estimate for this tier
    pub fn overhead_percent(&self) -> u8 {
        match self {
            Self::Tier1 => 5,
            Self::Tier2 => 10,
            Self::Tier3 => 25,
            Self::Tier4 => 50,
        }
    }

    /// Get estimated log size per failed file (KB)
    pub fn log_size_kb(&self) -> usize {
        match self {
            Self::Tier1 => 2,
            Self::Tier2 => 5,
            Self::Tier3 => 20,
            Self::Tier4 => 100,
        }
    }
}

/// Clippy lint level configuration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ClippyLevel {
    Standard,
    Pedantic,
    #[default]
    Nursery,
    Full,
}

impl ClippyLevel {
    /// Parse from CLI argument string
    pub fn from_cli_arg(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "standard" | "all" => Self::Standard,
            "pedantic" => Self::Pedantic,
            "nursery" => Self::Nursery,
            "full" | "cargo" => Self::Full,
            _ => Self::Nursery,
        }
    }

    /// Get clippy flags for this level
    pub fn flags(&self) -> Vec<&'static str> {
        let mut flags = vec!["clippy::all"];
        if matches!(self, Self::Pedantic | Self::Nursery | Self::Full) {
            flags.push("clippy::pedantic");
        }
        if matches!(self, Self::Nursery | Self::Full) {
            flags.push("clippy::nursery");
        }
        if matches!(self, Self::Full) {
            flags.push("clippy::cargo");
        }
        flags
    }

    /// Get approximate lint count
    pub fn lint_count(&self) -> usize {
        match self {
            Self::Standard => 500,
            Self::Pedantic => 600,
            Self::Nursery => 650,
            Self::Full => 700,
        }
    }
}

/// Verbosity configuration for diagnostic capture (pure data)
#[derive(Debug, Clone)]
pub struct VerbosityConfig {
    pub tier: DiagnosticTier,
    pub clippy_level: ClippyLevel,
    pub trace_errors: Vec<String>,
    pub max_log_size: usize,
    pub timeout_secs: u64,
    pub adaptive: bool,
}

impl Default for VerbosityConfig {
    fn default() -> Self {
        Self {
            tier: DiagnosticTier::Tier1,
            clippy_level: ClippyLevel::Nursery,
            trace_errors: vec![
                "E0308".to_string(),
                "E0277".to_string(),
                "E0382".to_string(),
            ],
            max_log_size: 1_000_000,
            timeout_secs: 300,
            adaptive: true,
        }
    }
}

impl VerbosityConfig {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_tier(mut self, tier: DiagnosticTier) -> Self {
        self.tier = tier;
        self
    }

    pub fn with_clippy_level(mut self, level: ClippyLevel) -> Self {
        self.clippy_level = level;
        self
    }

    pub fn with_trace_errors(mut self, errors: Vec<String>) -> Self {
        self.trace_errors = errors;
        self
    }

    pub fn with_adaptive(mut self, adaptive: bool) -> Self {
        self.adaptive = adaptive;
        self
    }

    /// Select appropriate tier based on error code and attempt number
    pub fn select_tier_for_error(&self, error_code: &str, attempt: u32) -> DiagnosticTier {
        if !self.adaptive {
            return self.tier;
        }

        // Adaptive tier selection based on error type and attempt
        let is_complex_error = self.trace_errors.iter().any(|e| error_code.contains(e));

        match (is_complex_error, attempt) {
            (_, 0) => DiagnosticTier::Tier1,
            (true, 1) => DiagnosticTier::Tier2,
            (true, 2) => DiagnosticTier::Tier3,
            (true, _) => DiagnosticTier::Tier4,
            (false, 1) => DiagnosticTier::Tier1,
            (false, 2) => DiagnosticTier::Tier2,
            (false, _) => DiagnosticTier::Tier3,
        }
    }

    /// Check if error code should trigger higher verbosity
    pub fn should_escalate(&self, error_code: &str) -> bool {
        self.trace_errors.iter().any(|e| error_code.contains(e))
    }
}

/// Parsed diagnostic features for training corpus
#[derive(Debug, Clone, Default)]
pub struct DiagnosticFeatures {
    pub error_code: Option<String>,
    pub level: String,
    pub message: String,
    pub spans: Vec<DiagnosticSpan>,
    pub suggestions: Vec<String>,
    pub clippy_lints: Vec<String>,
    pub trace_lines: Vec<String>,
    pub backtrace: Option<String>,
}

impl DiagnosticFeatures {
    /// Check if this diagnostic has actionable suggestions
    pub fn has_suggestions(&self) -> bool {
        !self.suggestions.is_empty()
    }

    /// Get primary error code
    pub fn primary_code(&self) -> Option<&str> {
        self.error_code.as_deref()
    }

    /// Count total spans
    pub fn span_count(&self) -> usize {
        self.spans.len()
    }

    /// Check if error level
    pub fn is_error(&self) -> bool {
        self.level == "error"
    }

    /// Check if warning level
    pub fn is_warning(&self) -> bool {
        self.level == "warning"
    }
}

/// Source location span from compiler diagnostic
#[derive(Debug, Clone)]
pub struct DiagnosticSpan {
    pub file_name: String,
    pub line_start: u32,
    pub line_end: u32,
    pub column_start: u32,
    pub column_end: u32,
    pub is_primary: bool,
    pub label: Option<String>,
}

impl DiagnosticSpan {
    /// Check if span is single line
    pub fn is_single_line(&self) -> bool {
        self.line_start == self.line_end
    }

    /// Get span width in characters
    pub fn width(&self) -> u32 {
        if self.is_single_line() {
            self.column_end.saturating_sub(self.column_start)
        } else {
            0
        }
    }

    /// Get line count
    pub fn line_count(&self) -> u32 {
        self.line_end.saturating_sub(self.line_start) + 1
    }
}

/// Training metrics for a compilation batch
#[derive(Debug, Clone, Default)]
pub struct BatchMetrics {
    pub total_files: usize,
    pub passed: usize,
    pub failed: usize,
    pub error_counts: HashMap<String, usize>,
    pub total_time_ms: u64,
}

impl BatchMetrics {
    pub fn new() -> Self {
        Self::default()
    }

    /// Calculate pass rate (0.0 - 1.0)
    pub fn pass_rate(&self) -> f64 {
        if self.total_files == 0 {
            0.0
        } else {
            self.passed as f64 / self.total_files as f64
        }
    }

    /// Calculate fail rate (0.0 - 1.0)
    pub fn fail_rate(&self) -> f64 {
        if self.total_files == 0 {
            0.0
        } else {
            self.failed as f64 / self.total_files as f64
        }
    }

    /// Get most common error code
    pub fn most_common_error(&self) -> Option<(&String, &usize)> {
        self.error_counts.iter().max_by_key(|(_, count)| *count)
    }

    /// Calculate throughput (files per second)
    pub fn throughput(&self) -> f64 {
        if self.total_time_ms == 0 {
            0.0
        } else {
            self.total_files as f64 / (self.total_time_ms as f64 / 1000.0)
        }
    }

    /// Record a compilation result
    pub fn record(&mut self, success: bool, error_code: Option<&str>) {
        self.total_files += 1;
        if success {
            self.passed += 1;
        } else {
            self.failed += 1;
            if let Some(code) = error_code {
                *self.error_counts.entry(code.to_string()).or_insert(0) += 1;
            }
        }
    }

    /// Merge another batch's metrics
    pub fn merge(&mut self, other: &BatchMetrics) {
        self.total_files += other.total_files;
        self.passed += other.passed;
        self.failed += other.failed;
        self.total_time_ms += other.total_time_ms;
        for (code, count) in &other.error_counts {
            *self.error_counts.entry(code.clone()).or_insert(0) += count;
        }
    }
}

/// Training progress state
#[derive(Debug, Clone)]
pub struct TrainingProgress {
    pub epoch: usize,
    pub iteration: usize,
    pub current_rate: f64,
    pub best_rate: f64,
    pub patience_remaining: usize,
    pub converged: bool,
}

impl TrainingProgress {
    pub fn new(patience: usize) -> Self {
        Self {
            epoch: 0,
            iteration: 0,
            current_rate: 0.0,
            best_rate: 0.0,
            patience_remaining: patience,
            converged: false,
        }
    }

    /// Update progress with new rate
    pub fn update(&mut self, rate: f64, target_rate: f64) {
        self.iteration += 1;
        self.current_rate = rate;

        if rate > self.best_rate {
            self.best_rate = rate;
            // Reset patience on improvement
            self.patience_remaining = self.patience_remaining.max(3);
        } else {
            self.patience_remaining = self.patience_remaining.saturating_sub(1);
        }

        if rate >= target_rate {
            self.converged = true;
        }
    }

    /// Check if should stop early
    pub fn should_stop(&self) -> bool {
        self.converged || self.patience_remaining == 0
    }

    /// Calculate improvement from initial
    pub fn improvement(&self, initial_rate: f64) -> f64 {
        self.current_rate - initial_rate
    }
}

/// Curriculum difficulty level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CurriculumLevel {
    #[default]
    Easy,
    Medium,
    Hard,
    Expert,
}

impl CurriculumLevel {
    /// Get complexity threshold for this level
    pub fn complexity_threshold(&self) -> f64 {
        match self {
            Self::Easy => 2.0,
            Self::Medium => 5.0,
            Self::Hard => 10.0,
            Self::Expert => f64::MAX,
        }
    }

    /// Advance to next level
    pub fn next(&self) -> Self {
        match self {
            Self::Easy => Self::Medium,
            Self::Medium => Self::Hard,
            Self::Hard => Self::Expert,
            Self::Expert => Self::Expert,
        }
    }

    /// Check if at max level
    pub fn is_max(&self) -> bool {
        matches!(self, Self::Expert)
    }
}

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

    #[test]
    fn test_diagnostic_tier_from_level() {
        assert_eq!(DiagnosticTier::from_level(0), DiagnosticTier::Tier1);
        assert_eq!(DiagnosticTier::from_level(1), DiagnosticTier::Tier1);
        assert_eq!(DiagnosticTier::from_level(2), DiagnosticTier::Tier2);
        assert_eq!(DiagnosticTier::from_level(3), DiagnosticTier::Tier3);
        assert_eq!(DiagnosticTier::from_level(4), DiagnosticTier::Tier4);
        assert_eq!(DiagnosticTier::from_level(5), DiagnosticTier::Tier4);
        assert_eq!(DiagnosticTier::from_level(100), DiagnosticTier::Tier4);
    }

    #[test]
    fn test_diagnostic_tier_level() {
        assert_eq!(DiagnosticTier::Tier1.level(), 1);
        assert_eq!(DiagnosticTier::Tier2.level(), 2);
        assert_eq!(DiagnosticTier::Tier3.level(), 3);
        assert_eq!(DiagnosticTier::Tier4.level(), 4);
    }

    #[test]
    fn test_diagnostic_tier_overhead() {
        assert_eq!(DiagnosticTier::Tier1.overhead_percent(), 5);
        assert_eq!(DiagnosticTier::Tier2.overhead_percent(), 10);
        assert_eq!(DiagnosticTier::Tier3.overhead_percent(), 25);
        assert_eq!(DiagnosticTier::Tier4.overhead_percent(), 50);
    }

    #[test]
    fn test_diagnostic_tier_log_size() {
        assert_eq!(DiagnosticTier::Tier1.log_size_kb(), 2);
        assert_eq!(DiagnosticTier::Tier2.log_size_kb(), 5);
        assert_eq!(DiagnosticTier::Tier3.log_size_kb(), 20);
        assert_eq!(DiagnosticTier::Tier4.log_size_kb(), 100);
    }

    #[test]
    fn test_clippy_level_from_cli() {
        assert_eq!(ClippyLevel::from_cli_arg("standard"), ClippyLevel::Standard);
        assert_eq!(ClippyLevel::from_cli_arg("all"), ClippyLevel::Standard);
        assert_eq!(ClippyLevel::from_cli_arg("pedantic"), ClippyLevel::Pedantic);
        assert_eq!(ClippyLevel::from_cli_arg("nursery"), ClippyLevel::Nursery);
        assert_eq!(ClippyLevel::from_cli_arg("full"), ClippyLevel::Full);
        assert_eq!(ClippyLevel::from_cli_arg("cargo"), ClippyLevel::Full);
        assert_eq!(ClippyLevel::from_cli_arg("PEDANTIC"), ClippyLevel::Pedantic);
        assert_eq!(ClippyLevel::from_cli_arg("unknown"), ClippyLevel::Nursery);
    }

    #[test]
    fn test_clippy_level_flags() {
        assert_eq!(ClippyLevel::Standard.flags(), vec!["clippy::all"]);
        assert_eq!(ClippyLevel::Pedantic.flags(), vec!["clippy::all", "clippy::pedantic"]);
        assert_eq!(ClippyLevel::Nursery.flags(), vec!["clippy::all", "clippy::pedantic", "clippy::nursery"]);
        assert_eq!(ClippyLevel::Full.flags(), vec!["clippy::all", "clippy::pedantic", "clippy::nursery", "clippy::cargo"]);
    }

    #[test]
    fn test_clippy_level_lint_count() {
        assert!(ClippyLevel::Standard.lint_count() < ClippyLevel::Pedantic.lint_count());
        assert!(ClippyLevel::Pedantic.lint_count() < ClippyLevel::Nursery.lint_count());
        assert!(ClippyLevel::Nursery.lint_count() < ClippyLevel::Full.lint_count());
    }

    #[test]
    fn test_verbosity_config_default() {
        let config = VerbosityConfig::default();
        assert_eq!(config.tier, DiagnosticTier::Tier1);
        assert_eq!(config.clippy_level, ClippyLevel::Nursery);
        assert!(config.adaptive);
        assert_eq!(config.timeout_secs, 300);
    }

    #[test]
    fn test_verbosity_config_builder() {
        let config = VerbosityConfig::new()
            .with_tier(DiagnosticTier::Tier3)
            .with_clippy_level(ClippyLevel::Full)
            .with_adaptive(false);

        assert_eq!(config.tier, DiagnosticTier::Tier3);
        assert_eq!(config.clippy_level, ClippyLevel::Full);
        assert!(!config.adaptive);
    }

    #[test]
    fn test_verbosity_select_tier_non_adaptive() {
        let config = VerbosityConfig::new()
            .with_tier(DiagnosticTier::Tier2)
            .with_adaptive(false);

        assert_eq!(config.select_tier_for_error("E0308", 0), DiagnosticTier::Tier2);
        assert_eq!(config.select_tier_for_error("E0308", 5), DiagnosticTier::Tier2);
    }

    #[test]
    fn test_verbosity_select_tier_adaptive() {
        let config = VerbosityConfig::default();

        // First attempt always Tier1
        assert_eq!(config.select_tier_for_error("E0308", 0), DiagnosticTier::Tier1);

        // Complex error escalates faster
        assert_eq!(config.select_tier_for_error("E0308", 1), DiagnosticTier::Tier2);
        assert_eq!(config.select_tier_for_error("E0308", 2), DiagnosticTier::Tier3);
        assert_eq!(config.select_tier_for_error("E0308", 3), DiagnosticTier::Tier4);

        // Non-complex error escalates slower
        assert_eq!(config.select_tier_for_error("E0001", 1), DiagnosticTier::Tier1);
        assert_eq!(config.select_tier_for_error("E0001", 2), DiagnosticTier::Tier2);
    }

    #[test]
    fn test_verbosity_should_escalate() {
        let config = VerbosityConfig::default();
        assert!(config.should_escalate("E0308"));
        assert!(config.should_escalate("E0277"));
        assert!(config.should_escalate("E0382"));
        assert!(!config.should_escalate("E0001"));
        assert!(!config.should_escalate("E9999"));
    }

    #[test]
    fn test_diagnostic_features() {
        let features = DiagnosticFeatures {
            error_code: Some("E0308".to_string()),
            level: "error".to_string(),
            message: "mismatched types".to_string(),
            suggestions: vec!["try using &str".to_string()],
            ..Default::default()
        };

        assert!(features.has_suggestions());
        assert_eq!(features.primary_code(), Some("E0308"));
        assert!(features.is_error());
        assert!(!features.is_warning());
    }

    #[test]
    fn test_diagnostic_features_warning() {
        let features = DiagnosticFeatures {
            level: "warning".to_string(),
            ..Default::default()
        };

        assert!(!features.is_error());
        assert!(features.is_warning());
        assert!(!features.has_suggestions());
    }

    #[test]
    fn test_diagnostic_span() {
        let span = DiagnosticSpan {
            file_name: "test.rs".to_string(),
            line_start: 10,
            line_end: 10,
            column_start: 5,
            column_end: 15,
            is_primary: true,
            label: Some("here".to_string()),
        };

        assert!(span.is_single_line());
        assert_eq!(span.width(), 10);
        assert_eq!(span.line_count(), 1);
    }

    #[test]
    fn test_diagnostic_span_multiline() {
        let span = DiagnosticSpan {
            file_name: "test.rs".to_string(),
            line_start: 10,
            line_end: 15,
            column_start: 5,
            column_end: 15,
            is_primary: false,
            label: None,
        };

        assert!(!span.is_single_line());
        assert_eq!(span.width(), 0);
        assert_eq!(span.line_count(), 6);
    }

    #[test]
    fn test_batch_metrics_new() {
        let metrics = BatchMetrics::new();
        assert_eq!(metrics.total_files, 0);
        assert_eq!(metrics.passed, 0);
        assert_eq!(metrics.failed, 0);
        assert_eq!(metrics.pass_rate(), 0.0);
    }

    #[test]
    fn test_batch_metrics_record() {
        let mut metrics = BatchMetrics::new();
        metrics.record(true, None);
        metrics.record(true, None);
        metrics.record(false, Some("E0308"));
        metrics.record(false, Some("E0308"));
        metrics.record(false, Some("E0277"));

        assert_eq!(metrics.total_files, 5);
        assert_eq!(metrics.passed, 2);
        assert_eq!(metrics.failed, 3);
        assert!((metrics.pass_rate() - 0.4).abs() < 0.001);
        assert!((metrics.fail_rate() - 0.6).abs() < 0.001);
    }

    #[test]
    fn test_batch_metrics_most_common_error() {
        let mut metrics = BatchMetrics::new();
        metrics.record(false, Some("E0308"));
        metrics.record(false, Some("E0308"));
        metrics.record(false, Some("E0277"));

        let (code, count) = metrics.most_common_error().unwrap();
        assert_eq!(code, "E0308");
        assert_eq!(*count, 2);
    }

    #[test]
    fn test_batch_metrics_throughput() {
        let mut metrics = BatchMetrics::new();
        metrics.total_files = 100;
        metrics.total_time_ms = 10000; // 10 seconds

        assert!((metrics.throughput() - 10.0).abs() < 0.001);
    }

    #[test]
    fn test_batch_metrics_merge() {
        let mut m1 = BatchMetrics::new();
        m1.record(true, None);
        m1.record(false, Some("E0308"));
        m1.total_time_ms = 100;

        let mut m2 = BatchMetrics::new();
        m2.record(true, None);
        m2.record(false, Some("E0308"));
        m2.total_time_ms = 200;

        m1.merge(&m2);

        assert_eq!(m1.total_files, 4);
        assert_eq!(m1.passed, 2);
        assert_eq!(m1.failed, 2);
        assert_eq!(m1.total_time_ms, 300);
        assert_eq!(*m1.error_counts.get("E0308").unwrap(), 2);
    }

    #[test]
    fn test_training_progress_new() {
        let progress = TrainingProgress::new(5);
        assert_eq!(progress.epoch, 0);
        assert_eq!(progress.iteration, 0);
        assert_eq!(progress.patience_remaining, 5);
        assert!(!progress.converged);
    }

    #[test]
    fn test_training_progress_update_improvement() {
        let mut progress = TrainingProgress::new(3);
        progress.update(0.5, 0.95);

        assert_eq!(progress.iteration, 1);
        assert_eq!(progress.current_rate, 0.5);
        assert_eq!(progress.best_rate, 0.5);
        assert!(!progress.converged);

        progress.update(0.7, 0.95);
        assert_eq!(progress.best_rate, 0.7);
    }

    #[test]
    fn test_training_progress_converged() {
        let mut progress = TrainingProgress::new(3);
        progress.update(0.95, 0.95);

        assert!(progress.converged);
        assert!(progress.should_stop());
    }

    #[test]
    fn test_training_progress_patience_exhausted() {
        let mut progress = TrainingProgress::new(2);
        progress.update(0.5, 0.95);
        progress.patience_remaining = 2;
        progress.update(0.4, 0.95); // No improvement
        progress.update(0.3, 0.95); // No improvement

        assert!(progress.should_stop());
    }

    #[test]
    fn test_training_progress_improvement() {
        let mut progress = TrainingProgress::new(3);
        progress.update(0.7, 0.95);

        assert!((progress.improvement(0.5) - 0.2).abs() < 0.001);
    }

    #[test]
    fn test_curriculum_level_threshold() {
        assert_eq!(CurriculumLevel::Easy.complexity_threshold(), 2.0);
        assert_eq!(CurriculumLevel::Medium.complexity_threshold(), 5.0);
        assert_eq!(CurriculumLevel::Hard.complexity_threshold(), 10.0);
        assert_eq!(CurriculumLevel::Expert.complexity_threshold(), f64::MAX);
    }

    #[test]
    fn test_curriculum_level_next() {
        assert_eq!(CurriculumLevel::Easy.next(), CurriculumLevel::Medium);
        assert_eq!(CurriculumLevel::Medium.next(), CurriculumLevel::Hard);
        assert_eq!(CurriculumLevel::Hard.next(), CurriculumLevel::Expert);
        assert_eq!(CurriculumLevel::Expert.next(), CurriculumLevel::Expert);
    }

    #[test]
    fn test_curriculum_level_is_max() {
        assert!(!CurriculumLevel::Easy.is_max());
        assert!(!CurriculumLevel::Medium.is_max());
        assert!(!CurriculumLevel::Hard.is_max());
        assert!(CurriculumLevel::Expert.is_max());
    }

    #[test]
    fn test_curriculum_level_default() {
        assert_eq!(CurriculumLevel::default(), CurriculumLevel::Easy);
    }
}