ruchy 4.1.2

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
//! Hunt Planner: Error Classification and Pattern Selection
//!
//! Implements the PLAN phase of Hunt Mode's PDCA cycle.
//! Uses error clustering to identify high-impact failure patterns.
//!
//! # Toyota Way: Heijunka (Level the Workload)
//!
//! Process highest-impact patterns first to ensure maximum improvement per cycle.
//! Pareto principle: 20% of patterns cause 80% of failures.
//!
//! # References
//! - [1] Pareto, V. (1896). 80/20 rule.
//! - [17] Rother & Shook (1999). Value Stream Mapping.

use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap};

/// Error cluster representing a group of similar errors
#[derive(Debug, Clone)]
pub struct ErrorCluster {
    /// Error code (e.g., "E0308")
    pub code: String,

    /// Number of occurrences
    pub count: usize,

    /// Representative error message
    pub representative: String,

    /// Sample file paths
    pub sample_files: Vec<String>,

    /// Severity score (higher = more critical)
    pub severity: f64,
}

impl ErrorCluster {
    /// Create new error cluster
    #[must_use]
    pub fn new(code: impl Into<String>, representative: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            count: 1,
            representative: representative.into(),
            sample_files: Vec::new(),
            severity: 1.0,
        }
    }

    /// Add occurrence to cluster
    pub fn add_occurrence(&mut self, file: Option<&str>) {
        self.count += 1;
        if let Some(f) = file {
            if self.sample_files.len() < 3 {
                self.sample_files.push(f.to_string());
            }
        }
    }

    /// Set severity
    pub fn with_severity(mut self, severity: f64) -> Self {
        self.severity = severity;
        self
    }

    /// Calculate priority score (frequency * severity)
    #[must_use]
    pub fn priority_score(&self) -> f64 {
        self.count as f64 * self.severity
    }
}

/// Failure pattern identified for fixing
#[derive(Debug, Clone)]
pub struct FailurePattern {
    /// Unique pattern ID
    pub id: String,

    /// Error code
    pub error_code: String,

    /// Pattern description
    pub description: String,

    /// Number of files affected
    pub affected_count: usize,

    /// Estimated fix complexity (1-10)
    pub complexity: u8,

    /// Sample code that exhibits the error
    pub sample_code: Option<String>,

    /// Sample error message
    pub sample_error: Option<String>,
}

impl FailurePattern {
    /// Create new failure pattern
    #[must_use]
    pub fn new(id: impl Into<String>, error_code: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            error_code: error_code.into(),
            description: String::new(),
            affected_count: 0,
            complexity: 5,
            sample_code: None,
            sample_error: None,
        }
    }

    /// Set description
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    /// Set affected count
    pub fn with_affected_count(mut self, count: usize) -> Self {
        self.affected_count = count;
        self
    }

    /// Set complexity
    pub fn with_complexity(mut self, complexity: u8) -> Self {
        self.complexity = complexity;
        self
    }

    /// Set sample code
    pub fn with_sample_code(mut self, code: impl Into<String>) -> Self {
        self.sample_code = Some(code.into());
        self
    }

    /// Set sample error
    pub fn with_sample_error(mut self, error: impl Into<String>) -> Self {
        self.sample_error = Some(error.into());
        self
    }
}

/// Prioritized pattern wrapper for heap ordering
#[derive(Debug, Clone)]
pub struct PrioritizedPattern {
    /// The pattern
    pub pattern: FailurePattern,

    /// Priority score (higher = more important)
    pub priority: f64,
}

impl PartialEq for PrioritizedPattern {
    fn eq(&self, other: &Self) -> bool {
        self.priority == other.priority
    }
}

impl Eq for PrioritizedPattern {}

impl PartialOrd for PrioritizedPattern {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for PrioritizedPattern {
    fn cmp(&self, other: &Self) -> Ordering {
        // Higher priority first
        self.priority
            .partial_cmp(&other.priority)
            .unwrap_or(Ordering::Equal)
    }
}

/// Hunt Planner for pattern identification and selection
#[derive(Debug)]
pub struct HuntPlanner {
    /// Error clusters by code
    clusters: HashMap<String, ErrorCluster>,

    /// Priority queue of patterns
    priority_queue: BinaryHeap<PrioritizedPattern>,

    /// Patterns already processed
    processed: Vec<String>,
}

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

impl HuntPlanner {
    /// Create new planner
    #[must_use]
    pub fn new() -> Self {
        Self {
            clusters: HashMap::new(),
            priority_queue: BinaryHeap::new(),
            processed: Vec::new(),
        }
    }

    /// Add error to clustering
    pub fn add_error(&mut self, code: &str, message: &str, file: Option<&str>, severity: f64) {
        if let Some(cluster) = self.clusters.get_mut(code) {
            // Cluster exists, add occurrence
            cluster.add_occurrence(file);
        } else {
            // Create new cluster with initial count of 1
            let mut cluster = ErrorCluster::new(code, message).with_severity(severity);
            if let Some(f) = file {
                cluster.sample_files.push(f.to_string());
            }
            self.clusters.insert(code.to_string(), cluster);
        }
    }

    /// Build priority queue from clusters
    pub fn build_priority_queue(&mut self) {
        self.priority_queue.clear();

        for cluster in self.clusters.values() {
            // Skip already processed patterns
            if self.processed.contains(&cluster.code) {
                continue;
            }

            let pattern = FailurePattern::new(format!("PAT-{}", cluster.code), &cluster.code)
                .with_description(&cluster.representative)
                .with_affected_count(cluster.count);

            let priority = cluster.priority_score();

            self.priority_queue
                .push(PrioritizedPattern { pattern, priority });
        }
    }

    /// Select next target pattern (Heijunka - highest impact first)
    #[must_use]
    pub fn select_next_target(&mut self) -> Option<FailurePattern> {
        // Build queue if empty
        if self.priority_queue.is_empty() {
            self.build_priority_queue();
        }

        self.priority_queue.pop().map(|p| {
            self.processed.push(p.pattern.error_code.clone());
            p.pattern
        })
    }

    /// Get all clusters
    #[must_use]
    pub fn clusters(&self) -> &HashMap<String, ErrorCluster> {
        &self.clusters
    }

    /// Get top N clusters by priority
    #[must_use]
    pub fn top_clusters(&self, n: usize) -> Vec<&ErrorCluster> {
        let mut clusters: Vec<_> = self.clusters.values().collect();
        clusters.sort_by(|a, b| {
            b.priority_score()
                .partial_cmp(&a.priority_score())
                .unwrap_or(Ordering::Equal)
        });
        clusters.into_iter().take(n).collect()
    }

    /// Get total error count
    #[must_use]
    pub fn total_errors(&self) -> usize {
        self.clusters.values().map(|c| c.count).sum()
    }

    /// Get unique error count
    #[must_use]
    pub fn unique_errors(&self) -> usize {
        self.clusters.len()
    }

    /// Clear all data
    pub fn clear(&mut self) {
        self.clusters.clear();
        self.priority_queue.clear();
        self.processed.clear();
    }

    /// Reset processed patterns (for retry)
    pub fn reset_processed(&mut self) {
        self.processed.clear();
    }
}

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

    // ============================================================================
    // EXTREME TDD: RED PHASE - ErrorCluster Tests
    // ============================================================================

    #[test]
    fn test_error_cluster_new() {
        let cluster = ErrorCluster::new("E0308", "mismatched types");
        assert_eq!(cluster.code, "E0308");
        assert_eq!(cluster.count, 1);
    }

    #[test]
    fn test_error_cluster_add_occurrence() {
        let mut cluster = ErrorCluster::new("E0308", "mismatched types");
        cluster.add_occurrence(Some("test.rs"));
        assert_eq!(cluster.count, 2);
        assert_eq!(cluster.sample_files.len(), 1);
    }

    #[test]
    fn test_error_cluster_max_samples() {
        let mut cluster = ErrorCluster::new("E0308", "mismatched types");
        for i in 0..5 {
            cluster.add_occurrence(Some(&format!("test{i}.rs")));
        }
        assert_eq!(cluster.sample_files.len(), 3);
    }

    #[test]
    fn test_error_cluster_with_severity() {
        let cluster = ErrorCluster::new("E0308", "mismatched types").with_severity(2.0);
        assert!((cluster.severity - 2.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_error_cluster_priority_score() {
        let mut cluster = ErrorCluster::new("E0308", "mismatched types").with_severity(2.0);
        cluster.add_occurrence(None);
        cluster.add_occurrence(None);
        // count=3, severity=2.0, score=6.0
        assert!((cluster.priority_score() - 6.0).abs() < f64::EPSILON);
    }

    // ============================================================================
    // EXTREME TDD: RED PHASE - FailurePattern Tests
    // ============================================================================

    #[test]
    fn test_failure_pattern_new() {
        let pattern = FailurePattern::new("PAT-001", "E0308");
        assert_eq!(pattern.id, "PAT-001");
        assert_eq!(pattern.error_code, "E0308");
    }

    #[test]
    fn test_failure_pattern_with_description() {
        let pattern =
            FailurePattern::new("PAT-001", "E0308").with_description("Type mismatch error");
        assert_eq!(pattern.description, "Type mismatch error");
    }

    #[test]
    fn test_failure_pattern_with_affected_count() {
        let pattern = FailurePattern::new("PAT-001", "E0308").with_affected_count(10);
        assert_eq!(pattern.affected_count, 10);
    }

    #[test]
    fn test_failure_pattern_with_complexity() {
        let pattern = FailurePattern::new("PAT-001", "E0308").with_complexity(8);
        assert_eq!(pattern.complexity, 8);
    }

    #[test]
    fn test_failure_pattern_with_sample_code() {
        let pattern = FailurePattern::new("PAT-001", "E0308").with_sample_code("fn foo() {}");
        assert_eq!(pattern.sample_code, Some("fn foo() {}".to_string()));
    }

    #[test]
    fn test_failure_pattern_with_sample_error() {
        let pattern =
            FailurePattern::new("PAT-001", "E0308").with_sample_error("expected i32, found String");
        assert_eq!(
            pattern.sample_error,
            Some("expected i32, found String".to_string())
        );
    }

    // ============================================================================
    // EXTREME TDD: RED PHASE - PrioritizedPattern Tests
    // ============================================================================

    #[test]
    fn test_prioritized_pattern_ordering() {
        let p1 = PrioritizedPattern {
            pattern: FailurePattern::new("PAT-001", "E0308"),
            priority: 10.0,
        };
        let p2 = PrioritizedPattern {
            pattern: FailurePattern::new("PAT-002", "E0599"),
            priority: 5.0,
        };
        assert!(p1 > p2);
    }

    #[test]
    fn test_prioritized_pattern_equality() {
        let p1 = PrioritizedPattern {
            pattern: FailurePattern::new("PAT-001", "E0308"),
            priority: 10.0,
        };
        let p2 = PrioritizedPattern {
            pattern: FailurePattern::new("PAT-002", "E0599"),
            priority: 10.0,
        };
        assert_eq!(p1, p2);
    }

    // ============================================================================
    // EXTREME TDD: RED PHASE - HuntPlanner Tests
    // ============================================================================

    #[test]
    fn test_hunt_planner_new() {
        let planner = HuntPlanner::new();
        assert!(planner.clusters().is_empty());
    }

    #[test]
    fn test_hunt_planner_default() {
        let planner = HuntPlanner::default();
        assert!(planner.clusters().is_empty());
    }

    #[test]
    fn test_hunt_planner_add_error() {
        let mut planner = HuntPlanner::new();
        planner.add_error("E0308", "mismatched types", Some("test.rs"), 1.0);
        assert_eq!(planner.unique_errors(), 1);
    }

    #[test]
    fn test_hunt_planner_add_multiple_same_code() {
        let mut planner = HuntPlanner::new();
        planner.add_error("E0308", "mismatched types", Some("test1.rs"), 1.0);
        planner.add_error("E0308", "mismatched types", Some("test2.rs"), 1.0);
        assert_eq!(planner.unique_errors(), 1);
        // First add creates cluster with count=1
        // Second add increments count to 2
        assert_eq!(planner.total_errors(), 2);
    }

    #[test]
    fn test_hunt_planner_add_different_codes() {
        let mut planner = HuntPlanner::new();
        planner.add_error("E0308", "mismatched types", None, 1.0);
        planner.add_error("E0599", "method not found", None, 1.0);
        assert_eq!(planner.unique_errors(), 2);
    }

    #[test]
    fn test_hunt_planner_select_next_target_empty() {
        let mut planner = HuntPlanner::new();
        assert!(planner.select_next_target().is_none());
    }

    #[test]
    fn test_hunt_planner_select_next_target() {
        let mut planner = HuntPlanner::new();
        planner.add_error("E0308", "mismatched types", None, 1.0);
        let pattern = planner.select_next_target();
        assert!(pattern.is_some());
        assert_eq!(pattern.unwrap().error_code, "E0308");
    }

    #[test]
    fn test_hunt_planner_select_highest_priority() {
        let mut planner = HuntPlanner::new();
        planner.add_error("E0308", "mismatched types", None, 1.0);
        planner.add_error("E0599", "method not found", None, 2.0); // Higher severity

        let pattern = planner.select_next_target();
        assert!(pattern.is_some());
        // E0599 should be selected first (higher severity)
        assert_eq!(pattern.unwrap().error_code, "E0599");
    }

    #[test]
    fn test_hunt_planner_top_clusters() {
        let mut planner = HuntPlanner::new();
        for _ in 0..10 {
            planner.add_error("E0308", "mismatched types", None, 1.0);
        }
        for _ in 0..5 {
            planner.add_error("E0599", "method not found", None, 1.0);
        }

        let top = planner.top_clusters(1);
        assert_eq!(top.len(), 1);
        assert_eq!(top[0].code, "E0308");
    }

    #[test]
    fn test_hunt_planner_clear() {
        let mut planner = HuntPlanner::new();
        planner.add_error("E0308", "mismatched types", None, 1.0);
        planner.clear();
        assert!(planner.clusters().is_empty());
    }

    #[test]
    fn test_hunt_planner_reset_processed() {
        let mut planner = HuntPlanner::new();
        planner.add_error("E0308", "mismatched types", None, 1.0);

        // Select and process
        let _ = planner.select_next_target();
        assert!(planner.select_next_target().is_none());

        // Reset
        planner.reset_processed();
        assert!(planner.select_next_target().is_some());
    }
}