ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! 5-Phase Corpus Pipeline with Blocker Priority [8]
//!
//! Implements a structured pipeline for corpus processing with
//! blocker priority integration to focus on critical failures.
//!
//! # Phases
//! 1. Parse (syntax validation)
//! 2. Type check
//! 3. Transpile to Rust
//! 4. Compile Rust
//! 5. Execute & validate
//!
//! # Academic Foundation
//! - [5] Ohno (1988). Toyota Production System. Flow efficiency.
//! - [7] Imai (1986). Kaizen. Continuous improvement through phases.

use std::collections::HashMap;
use std::fmt;
use std::time::{Duration, Instant};

use crate::reporting::pareto::BlockerPriority;

/// Pipeline phase
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Phase {
    /// Parse source code (syntax)
    Parse = 0,
    /// Type check AST
    TypeCheck = 1,
    /// Transpile to Rust
    Transpile = 2,
    /// Compile Rust code
    Compile = 3,
    /// Execute and validate
    Execute = 4,
}

impl Phase {
    /// Get all phases in order
    #[must_use]
    pub fn all() -> &'static [Phase] {
        &[
            Phase::Parse,
            Phase::TypeCheck,
            Phase::Transpile,
            Phase::Compile,
            Phase::Execute,
        ]
    }

    /// Get phase name
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Phase::Parse => "Parse",
            Phase::TypeCheck => "TypeCheck",
            Phase::Transpile => "Transpile",
            Phase::Compile => "Compile",
            Phase::Execute => "Execute",
        }
    }

    /// Get phase number (1-indexed for display)
    #[must_use]
    pub fn number(&self) -> usize {
        (*self as usize) + 1
    }

    /// Get next phase
    #[must_use]
    pub fn next(&self) -> Option<Phase> {
        match self {
            Phase::Parse => Some(Phase::TypeCheck),
            Phase::TypeCheck => Some(Phase::Transpile),
            Phase::Transpile => Some(Phase::Compile),
            Phase::Compile => Some(Phase::Execute),
            Phase::Execute => None,
        }
    }

    /// Get icon for phase
    #[must_use]
    pub fn icon(&self) -> &'static str {
        match self {
            Phase::Parse => "📝",
            Phase::TypeCheck => "🔍",
            Phase::Transpile => "🔄",
            Phase::Compile => "🔨",
            Phase::Execute => "",
        }
    }
}

impl fmt::Display for Phase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

/// Result of a single phase execution
#[derive(Debug, Clone)]
pub struct PhaseResult {
    /// Phase that was executed
    pub phase: Phase,
    /// Whether phase succeeded
    pub success: bool,
    /// Error message if failed
    pub error: Option<String>,
    /// Duration of phase execution
    pub duration: Duration,
    /// Output artifact (if any)
    pub artifact: Option<String>,
}

impl PhaseResult {
    /// Create successful phase result
    #[must_use]
    pub fn success(phase: Phase, duration: Duration) -> Self {
        Self {
            phase,
            success: true,
            error: None,
            duration,
            artifact: None,
        }
    }

    /// Create failed phase result
    #[must_use]
    pub fn failure(phase: Phase, error: impl Into<String>, duration: Duration) -> Self {
        Self {
            phase,
            success: false,
            error: Some(error.into()),
            duration,
            artifact: None,
        }
    }

    /// Add artifact to result
    pub fn with_artifact(mut self, artifact: impl Into<String>) -> Self {
        self.artifact = Some(artifact.into());
        self
    }

    /// Get status icon
    #[must_use]
    pub fn status_icon(&self) -> &'static str {
        if self.success {
            ""
        } else {
            ""
        }
    }
}

/// Pipeline execution result for a single file
#[derive(Debug, Clone)]
pub struct PipelineExecution {
    /// File path
    pub path: String,
    /// Phase results
    pub phases: Vec<PhaseResult>,
    /// Last successful phase
    pub last_success: Option<Phase>,
    /// Failing phase (if any)
    pub failed_at: Option<Phase>,
    /// Total duration
    pub total_duration: Duration,
    /// Blocker priority (if failed)
    pub priority: Option<BlockerPriority>,
}

impl PipelineExecution {
    /// Create new execution for file
    #[must_use]
    pub fn new(path: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            phases: Vec::new(),
            last_success: None,
            failed_at: None,
            total_duration: Duration::ZERO,
            priority: None,
        }
    }

    /// Record phase result
    pub fn record(&mut self, result: PhaseResult) {
        self.total_duration += result.duration;

        if result.success {
            self.last_success = Some(result.phase);
        } else {
            self.failed_at = Some(result.phase);
        }

        self.phases.push(result);
    }

    /// Set blocker priority
    pub fn with_priority(mut self, priority: BlockerPriority) -> Self {
        self.priority = Some(priority);
        self
    }

    /// Check if all phases passed
    #[must_use]
    pub fn all_passed(&self) -> bool {
        self.failed_at.is_none() && self.last_success == Some(Phase::Execute)
    }

    /// Get completion percentage (phases passed / total phases)
    #[must_use]
    pub fn completion_percent(&self) -> f64 {
        let passed = self.phases.iter().filter(|p| p.success).count();
        (passed as f64 / 5.0) * 100.0
    }

    /// Get failure stage as string
    #[must_use]
    pub fn failure_stage(&self) -> Option<String> {
        self.failed_at
            .map(|p| format!("Phase {} ({})", p.number(), p.name()))
    }
}

/// Statistics for a single phase across corpus
#[derive(Debug, Clone, Default)]
pub struct PhaseStatistics {
    /// Phase being tracked
    pub phase: Option<Phase>,
    /// Total files processed
    pub total: usize,
    /// Files that passed this phase
    pub passed: usize,
    /// Files that failed at this phase
    pub failed_at: usize,
    /// Total duration across all files
    pub total_duration: Duration,
    /// Average duration per file
    pub avg_duration: Duration,
    /// Error counts by message
    pub error_counts: HashMap<String, usize>,
}

impl PhaseStatistics {
    /// Create new statistics for phase
    #[must_use]
    pub fn new(phase: Phase) -> Self {
        Self {
            phase: Some(phase),
            ..Default::default()
        }
    }

    /// Record a phase result
    pub fn record(&mut self, result: &PhaseResult) {
        self.total += 1;
        self.total_duration += result.duration;

        if result.success {
            self.passed += 1;
        } else {
            self.failed_at += 1;
            if let Some(ref error) = result.error {
                // Extract first line of error for grouping
                let key = error.lines().next().unwrap_or("Unknown error").to_string();
                *self.error_counts.entry(key).or_insert(0) += 1;
            }
        }

        // Update average
        if self.total > 0 {
            self.avg_duration = self.total_duration / self.total as u32;
        }
    }

    /// Get pass rate as percentage
    #[must_use]
    pub fn pass_rate(&self) -> f64 {
        if self.total == 0 {
            0.0
        } else {
            (self.passed as f64 / self.total as f64) * 100.0
        }
    }

    /// Get top errors (most frequent)
    #[must_use]
    pub fn top_errors(&self, limit: usize) -> Vec<(&str, usize)> {
        let mut errors: Vec<_> = self.error_counts.iter().collect();
        errors.sort_by(|a, b| b.1.cmp(a.1));
        errors
            .into_iter()
            .take(limit)
            .map(|(k, v)| (k.as_str(), *v))
            .collect()
    }
}

/// Pipeline tracker for corpus processing
#[derive(Debug, Clone)]
pub struct CorpusPipeline {
    /// All executions
    executions: Vec<PipelineExecution>,
    /// Phase statistics
    phase_stats: HashMap<Phase, PhaseStatistics>,
    /// Start time
    start_time: Option<Instant>,
    /// Total files to process
    pub total_files: usize,
    /// Files processed so far
    pub processed: usize,
}

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

impl CorpusPipeline {
    /// Create new pipeline
    #[must_use]
    pub fn new() -> Self {
        let mut phase_stats = HashMap::new();
        for phase in Phase::all() {
            phase_stats.insert(*phase, PhaseStatistics::new(*phase));
        }

        Self {
            executions: Vec::new(),
            phase_stats,
            start_time: None,
            total_files: 0,
            processed: 0,
        }
    }

    /// Set total files for progress tracking
    pub fn with_total(mut self, total: usize) -> Self {
        self.total_files = total;
        self
    }

    /// Start pipeline processing
    pub fn start(&mut self) {
        self.start_time = Some(Instant::now());
    }

    /// Record execution result
    pub fn record(&mut self, execution: PipelineExecution) {
        // Update phase statistics
        for result in &execution.phases {
            if let Some(stats) = self.phase_stats.get_mut(&result.phase) {
                stats.record(result);
            }
        }

        self.executions.push(execution);
        self.processed += 1;
    }

    /// Get elapsed time
    #[must_use]
    pub fn elapsed(&self) -> Duration {
        self.start_time.map_or(Duration::ZERO, |s| s.elapsed())
    }

    /// Get progress percentage
    #[must_use]
    pub fn progress_percent(&self) -> f64 {
        if self.total_files == 0 {
            100.0
        } else {
            (self.processed as f64 / self.total_files as f64) * 100.0
        }
    }

    /// Get phase statistics
    #[must_use]
    pub fn phase_stats(&self, phase: Phase) -> Option<&PhaseStatistics> {
        self.phase_stats.get(&phase)
    }

    /// Get all executions
    #[must_use]
    pub fn executions(&self) -> &[PipelineExecution] {
        &self.executions
    }

    /// Get passing files
    #[must_use]
    pub fn passing(&self) -> Vec<&PipelineExecution> {
        self.executions.iter().filter(|e| e.all_passed()).collect()
    }

    /// Get failing files
    #[must_use]
    pub fn failing(&self) -> Vec<&PipelineExecution> {
        self.executions.iter().filter(|e| !e.all_passed()).collect()
    }

    /// Get files failing at specific phase
    #[must_use]
    pub fn failing_at(&self, phase: Phase) -> Vec<&PipelineExecution> {
        self.executions
            .iter()
            .filter(|e| e.failed_at == Some(phase))
            .collect()
    }

    /// Get failures by blocker priority
    #[must_use]
    pub fn by_priority(&self) -> HashMap<BlockerPriority, Vec<&PipelineExecution>> {
        let mut result: HashMap<BlockerPriority, Vec<_>> = HashMap::new();

        for exec in &self.executions {
            if let Some(priority) = exec.priority {
                result.entry(priority).or_default().push(exec);
            }
        }

        result
    }

    /// Calculate overall success rate
    #[must_use]
    pub fn success_rate(&self) -> f64 {
        if self.processed == 0 {
            0.0
        } else {
            (self.passing().len() as f64 / self.processed as f64) * 100.0
        }
    }

    /// Generate phase funnel visualization
    #[must_use]
    pub fn render_funnel(&self, width: usize) -> String {
        let mut lines = vec![format!(
            "{}{}",
            " ".repeat(2),
            "".repeat(width.saturating_sub(4))
        )];

        lines.push(format!(
            "{}│ {:^width$} │",
            " ".repeat(2),
            "PIPELINE FUNNEL",
            width = width.saturating_sub(6)
        ));

        lines.push(format!(
            "{}{}",
            " ".repeat(2),
            "".repeat(width.saturating_sub(4))
        ));

        let max_count = self.processed.max(1);
        let bar_width = width.saturating_sub(35);

        for phase in Phase::all() {
            if let Some(stats) = self.phase_stats.get(phase) {
                let bar_len = if max_count > 0 {
                    (stats.passed * bar_width) / max_count
                } else {
                    0
                };
                let bar = "".repeat(bar_len);
                let padding = " ".repeat(bar_width.saturating_sub(bar_len));

                lines.push(format!(
                    "{}{} {:10} {:4}/{:4} {}{}",
                    " ".repeat(2),
                    phase.icon(),
                    phase.name(),
                    stats.passed,
                    stats.total.max(stats.passed),
                    bar,
                    padding
                ));
            }
        }

        // Summary
        lines.push(format!(
            "{}{}",
            " ".repeat(2),
            "".repeat(width.saturating_sub(4))
        ));

        let passed = self.passing().len();
        let failed = self.failing().len();
        lines.push(format!(
            "{}│ ✓ Passed: {:4}  ✗ Failed: {:4}  Rate: {:5.1}%{:width$} │",
            " ".repeat(2),
            passed,
            failed,
            self.success_rate(),
            "",
            width = width.saturating_sub(50)
        ));

        lines.push(format!(
            "{}{}",
            " ".repeat(2),
            "".repeat(width.saturating_sub(4))
        ));

        lines.join("\n")
    }

    /// Generate blocker priority breakdown
    #[must_use]
    pub fn render_blockers(&self, width: usize) -> String {
        let mut lines = vec![format!(
            "{}{}",
            " ".repeat(2),
            "".repeat(width.saturating_sub(4))
        )];

        lines.push(format!(
            "{}│ {:^width$} │",
            " ".repeat(2),
            "BLOCKER PRIORITY BREAKDOWN",
            width = width.saturating_sub(6)
        ));

        lines.push(format!(
            "{}{}",
            " ".repeat(2),
            "".repeat(width.saturating_sub(4))
        ));

        let by_priority = self.by_priority();

        for priority in &[
            BlockerPriority::P0Critical,
            BlockerPriority::P1High,
            BlockerPriority::P2Medium,
            BlockerPriority::P3Low,
        ] {
            let count = by_priority.get(priority).map_or(0, Vec::len);
            let icon = match priority {
                BlockerPriority::P0Critical => "🔴",
                BlockerPriority::P1High => "🟠",
                BlockerPriority::P2Medium => "🟡",
                BlockerPriority::P3Low => "🟢",
            };

            lines.push(format!(
                "{}{} {:12} {:4} files{:width$} │",
                " ".repeat(2),
                icon,
                format!("{:?}", priority),
                count,
                "",
                width = width.saturating_sub(32)
            ));
        }

        lines.push(format!(
            "{}{}",
            " ".repeat(2),
            "".repeat(width.saturating_sub(4))
        ));

        lines.join("\n")
    }
}

/// Pipeline builder for fluent API
#[derive(Debug, Default)]
pub struct PipelineBuilder {
    path: String,
    results: Vec<PhaseResult>,
}

impl PipelineBuilder {
    /// Create new builder for file
    #[must_use]
    pub fn new(path: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            results: Vec::new(),
        }
    }

    /// Record successful phase
    pub fn pass(mut self, phase: Phase, duration: Duration) -> Self {
        self.results.push(PhaseResult::success(phase, duration));
        self
    }

    /// Record failed phase
    pub fn fail(mut self, phase: Phase, error: impl Into<String>, duration: Duration) -> Self {
        self.results
            .push(PhaseResult::failure(phase, error, duration));
        self
    }

    /// Build execution result
    #[must_use]
    pub fn build(self) -> PipelineExecution {
        let mut exec = PipelineExecution::new(self.path);
        for result in self.results {
            exec.record(result);
        }
        exec
    }
}

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

    // ============================================================
    // EXTREME TDD: Phase Tests
    // ============================================================

    #[test]
    fn test_phase_all() {
        assert_eq!(Phase::all().len(), 5);
    }

    #[test]
    fn test_phase_name() {
        assert_eq!(Phase::Parse.name(), "Parse");
        assert_eq!(Phase::Execute.name(), "Execute");
    }

    #[test]
    fn test_phase_number() {
        assert_eq!(Phase::Parse.number(), 1);
        assert_eq!(Phase::Execute.number(), 5);
    }

    #[test]
    fn test_phase_next() {
        assert_eq!(Phase::Parse.next(), Some(Phase::TypeCheck));
        assert_eq!(Phase::Execute.next(), None);
    }

    #[test]
    fn test_phase_display() {
        assert_eq!(format!("{}", Phase::Parse), "Parse");
    }

    #[test]
    fn test_phase_order() {
        assert!(Phase::Parse < Phase::TypeCheck);
        assert!(Phase::TypeCheck < Phase::Transpile);
    }

    // ============================================================
    // EXTREME TDD: PhaseResult Tests
    // ============================================================

    #[test]
    fn test_phase_result_success() {
        let result = PhaseResult::success(Phase::Parse, Duration::from_millis(100));
        assert!(result.success);
        assert!(result.error.is_none());
        assert_eq!(result.status_icon(), "");
    }

    #[test]
    fn test_phase_result_failure() {
        let result = PhaseResult::failure(Phase::Parse, "syntax error", Duration::from_millis(50));
        assert!(!result.success);
        assert_eq!(result.error.as_deref(), Some("syntax error"));
        assert_eq!(result.status_icon(), "");
    }

    #[test]
    fn test_phase_result_with_artifact() {
        let result = PhaseResult::success(Phase::Transpile, Duration::from_millis(200))
            .with_artifact("output.rs");
        assert_eq!(result.artifact.as_deref(), Some("output.rs"));
    }

    // ============================================================
    // EXTREME TDD: PipelineExecution Tests
    // ============================================================

    #[test]
    fn test_pipeline_execution_new() {
        let exec = PipelineExecution::new("test.ruchy");
        assert_eq!(exec.path, "test.ruchy");
        assert!(exec.phases.is_empty());
        assert!(exec.last_success.is_none());
    }

    #[test]
    fn test_pipeline_execution_record() {
        let mut exec = PipelineExecution::new("test.ruchy");
        exec.record(PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(100),
        ));

        assert_eq!(exec.phases.len(), 1);
        assert_eq!(exec.last_success, Some(Phase::Parse));
    }

    #[test]
    fn test_pipeline_execution_all_passed() {
        let mut exec = PipelineExecution::new("test.ruchy");
        for phase in Phase::all() {
            exec.record(PhaseResult::success(*phase, Duration::from_millis(10)));
        }

        assert!(exec.all_passed());
    }

    #[test]
    fn test_pipeline_execution_failed() {
        let mut exec = PipelineExecution::new("test.ruchy");
        exec.record(PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(10),
        ));
        exec.record(PhaseResult::failure(
            Phase::TypeCheck,
            "type error",
            Duration::from_millis(5),
        ));

        assert!(!exec.all_passed());
        assert_eq!(exec.failed_at, Some(Phase::TypeCheck));
        assert_eq!(exec.last_success, Some(Phase::Parse));
    }

    #[test]
    fn test_pipeline_execution_completion() {
        let mut exec = PipelineExecution::new("test.ruchy");
        exec.record(PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(10),
        ));
        exec.record(PhaseResult::success(
            Phase::TypeCheck,
            Duration::from_millis(10),
        ));
        // 2 of 5 phases passed

        assert!((exec.completion_percent() - 40.0).abs() < 0.01);
    }

    #[test]
    fn test_pipeline_execution_failure_stage() {
        let mut exec = PipelineExecution::new("test.ruchy");
        exec.record(PhaseResult::failure(
            Phase::Compile,
            "compile error",
            Duration::from_millis(10),
        ));

        assert_eq!(exec.failure_stage(), Some("Phase 4 (Compile)".to_string()));
    }

    // ============================================================
    // EXTREME TDD: PhaseStatistics Tests
    // ============================================================

    #[test]
    fn test_phase_statistics_new() {
        let stats = PhaseStatistics::new(Phase::Parse);
        assert_eq!(stats.phase, Some(Phase::Parse));
        assert_eq!(stats.total, 0);
    }

    #[test]
    fn test_phase_statistics_record() {
        let mut stats = PhaseStatistics::new(Phase::Parse);
        stats.record(&PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(100),
        ));
        stats.record(&PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(50),
        ));

        assert_eq!(stats.total, 2);
        assert_eq!(stats.passed, 2);
    }

    #[test]
    fn test_phase_statistics_pass_rate() {
        let mut stats = PhaseStatistics::new(Phase::Parse);
        stats.record(&PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(10),
        ));
        stats.record(&PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(10),
        ));
        stats.record(&PhaseResult::failure(
            Phase::Parse,
            "error",
            Duration::from_millis(5),
        ));

        assert!((stats.pass_rate() - 66.67).abs() < 0.1);
    }

    #[test]
    fn test_phase_statistics_top_errors() {
        let mut stats = PhaseStatistics::new(Phase::Parse);
        stats.record(&PhaseResult::failure(
            Phase::Parse,
            "error A",
            Duration::from_millis(5),
        ));
        stats.record(&PhaseResult::failure(
            Phase::Parse,
            "error A",
            Duration::from_millis(5),
        ));
        stats.record(&PhaseResult::failure(
            Phase::Parse,
            "error B",
            Duration::from_millis(5),
        ));

        let top = stats.top_errors(2);
        assert_eq!(top[0].0, "error A");
        assert_eq!(top[0].1, 2);
    }

    // ============================================================
    // EXTREME TDD: CorpusPipeline Tests
    // ============================================================

    #[test]
    fn test_corpus_pipeline_new() {
        let pipeline = CorpusPipeline::new();
        assert_eq!(pipeline.processed, 0);
    }

    #[test]
    fn test_corpus_pipeline_record() {
        let mut pipeline = CorpusPipeline::new();
        let mut exec = PipelineExecution::new("test.ruchy");
        exec.record(PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(10),
        ));

        pipeline.record(exec);
        assert_eq!(pipeline.processed, 1);
    }

    #[test]
    fn test_corpus_pipeline_passing_failing() {
        let mut pipeline = CorpusPipeline::new();

        // All phases pass
        let mut exec1 = PipelineExecution::new("pass.ruchy");
        for phase in Phase::all() {
            exec1.record(PhaseResult::success(*phase, Duration::from_millis(10)));
        }
        pipeline.record(exec1);

        // Fails at compile
        let mut exec2 = PipelineExecution::new("fail.ruchy");
        exec2.record(PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(10),
        ));
        exec2.record(PhaseResult::failure(
            Phase::TypeCheck,
            "error",
            Duration::from_millis(5),
        ));
        pipeline.record(exec2);

        assert_eq!(pipeline.passing().len(), 1);
        assert_eq!(pipeline.failing().len(), 1);
    }

    #[test]
    fn test_corpus_pipeline_failing_at() {
        let mut pipeline = CorpusPipeline::new();

        let mut exec = PipelineExecution::new("test.ruchy");
        exec.record(PhaseResult::failure(
            Phase::Compile,
            "error",
            Duration::from_millis(10),
        ));
        pipeline.record(exec);

        assert_eq!(pipeline.failing_at(Phase::Compile).len(), 1);
        assert_eq!(pipeline.failing_at(Phase::Parse).len(), 0);
    }

    #[test]
    fn test_corpus_pipeline_success_rate() {
        let mut pipeline = CorpusPipeline::new();

        // 2 passing
        for _ in 0..2 {
            let mut exec = PipelineExecution::new("pass.ruchy");
            for phase in Phase::all() {
                exec.record(PhaseResult::success(*phase, Duration::from_millis(10)));
            }
            pipeline.record(exec);
        }

        // 1 failing
        let mut exec = PipelineExecution::new("fail.ruchy");
        exec.record(PhaseResult::failure(
            Phase::Parse,
            "error",
            Duration::from_millis(5),
        ));
        pipeline.record(exec);

        assert!((pipeline.success_rate() - 66.67).abs() < 0.1);
    }

    #[test]
    fn test_corpus_pipeline_by_priority() {
        let mut pipeline = CorpusPipeline::new();

        let mut exec = PipelineExecution::new("critical.ruchy");
        exec.record(PhaseResult::failure(
            Phase::Parse,
            "error",
            Duration::from_millis(5),
        ));
        let exec = exec.with_priority(BlockerPriority::P0Critical);
        pipeline.record(exec);

        let by_priority = pipeline.by_priority();
        assert_eq!(
            by_priority.get(&BlockerPriority::P0Critical).map(Vec::len),
            Some(1)
        );
    }

    #[test]
    fn test_corpus_pipeline_render_funnel() {
        let mut pipeline = CorpusPipeline::new();

        let mut exec = PipelineExecution::new("test.ruchy");
        exec.record(PhaseResult::success(
            Phase::Parse,
            Duration::from_millis(10),
        ));
        pipeline.record(exec);

        let output = pipeline.render_funnel(60);
        assert!(output.contains("PIPELINE FUNNEL"));
        assert!(output.contains("Parse"));
    }

    #[test]
    fn test_corpus_pipeline_render_blockers() {
        let mut pipeline = CorpusPipeline::new();

        let mut exec = PipelineExecution::new("test.ruchy");
        exec.record(PhaseResult::failure(
            Phase::Parse,
            "error",
            Duration::from_millis(5),
        ));
        let exec = exec.with_priority(BlockerPriority::P1High);
        pipeline.record(exec);

        let output = pipeline.render_blockers(60);
        assert!(output.contains("BLOCKER PRIORITY"));
    }

    // ============================================================
    // EXTREME TDD: PipelineBuilder Tests
    // ============================================================

    #[test]
    fn test_pipeline_builder() {
        let exec = PipelineBuilder::new("test.ruchy")
            .pass(Phase::Parse, Duration::from_millis(10))
            .pass(Phase::TypeCheck, Duration::from_millis(20))
            .fail(
                Phase::Transpile,
                "transpile error",
                Duration::from_millis(5),
            )
            .build();

        assert_eq!(exec.path, "test.ruchy");
        assert_eq!(exec.phases.len(), 3);
        assert_eq!(exec.last_success, Some(Phase::TypeCheck));
        assert_eq!(exec.failed_at, Some(Phase::Transpile));
    }
}