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
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
//! Fix Pattern Library
//!
//! Stores and queries patterns for error fixes.
//!
//! # References
//! - [9] Just et al. (2014). `Defects4J` methodology.

use super::ErrorCategory;
use regex::Regex;
use std::collections::HashMap;

/// A fix suggestion returned by the Oracle
#[derive(Debug, Clone)]
pub struct FixSuggestion {
    /// Human-readable description of the fix
    pub description: String,

    /// The transformation to apply (regex or AST transform)
    pub transformation: String,

    /// Historical success rate (0.0 to 1.0)
    pub success_rate: f64,

    /// Number of times this fix has been applied
    pub times_applied: u32,

    /// Pattern ID for tracking
    pub pattern_id: String,
}

impl FixSuggestion {
    /// Create a new fix suggestion
    #[must_use]
    pub fn new(description: impl Into<String>) -> Self {
        Self {
            description: description.into(),
            transformation: String::new(),
            success_rate: 0.0,
            times_applied: 0,
            pattern_id: String::new(),
        }
    }

    /// Set the transformation
    pub fn with_transformation(mut self, transform: impl Into<String>) -> Self {
        self.transformation = transform.into();
        self
    }

    /// Set the success rate
    pub fn with_success_rate(mut self, rate: f64) -> Self {
        self.success_rate = rate;
        self
    }

    /// Set times applied
    pub fn with_times_applied(mut self, count: u32) -> Self {
        self.times_applied = count;
        self
    }

    /// Set pattern ID
    pub fn with_pattern_id(mut self, id: impl Into<String>) -> Self {
        self.pattern_id = id.into();
        self
    }
}

/// A fix pattern stored in the pattern library
#[derive(Debug, Clone)]
pub struct FixPattern {
    /// Unique pattern identifier
    pub id: String,

    /// Error category this pattern addresses
    pub category: ErrorCategory,

    /// Regex pattern to match error messages
    pub error_pattern: String,

    /// Description of the fix
    pub description: String,

    /// Transformation to apply
    pub transformation: String,

    /// Historical success rate
    pub success_rate: f64,

    /// Number of times applied
    pub usage_count: u32,
}

impl FixPattern {
    /// Create a new fix pattern
    #[must_use]
    pub fn new(id: impl Into<String>, category: ErrorCategory) -> Self {
        Self {
            id: id.into(),
            category,
            error_pattern: String::new(),
            description: String::new(),
            transformation: String::new(),
            success_rate: 0.0,
            usage_count: 0,
        }
    }

    /// Set the error pattern
    pub fn with_error_pattern(mut self, pattern: impl Into<String>) -> Self {
        self.error_pattern = pattern.into();
        self
    }

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

    /// Set the transformation
    pub fn with_transformation(mut self, transform: impl Into<String>) -> Self {
        self.transformation = transform.into();
        self
    }

    /// Set the success rate
    pub fn with_success_rate(mut self, rate: f64) -> Self {
        self.success_rate = rate;
        self
    }

    /// Check if pattern matches an error message
    #[must_use]
    pub fn matches(&self, error_message: &str) -> bool {
        if self.error_pattern.is_empty() {
            return false;
        }

        // Try regex match
        if let Ok(regex) = Regex::new(&self.error_pattern) {
            return regex.is_match(error_message);
        }

        // Fallback to simple contains
        error_message.contains(&self.error_pattern)
    }

    /// Convert to fix suggestion
    #[must_use]
    pub fn to_suggestion(&self) -> FixSuggestion {
        FixSuggestion {
            description: self.description.clone(),
            transformation: self.transformation.clone(),
            success_rate: self.success_rate,
            times_applied: self.usage_count,
            pattern_id: self.id.clone(),
        }
    }
}

/// Pattern store for fix suggestions
#[derive(Debug)]
pub struct PatternStore {
    /// Patterns indexed by category
    patterns: HashMap<ErrorCategory, Vec<FixPattern>>,

    /// Total pattern count
    count: usize,
}

impl PatternStore {
    /// Create a new pattern store with default patterns
    #[must_use]
    pub fn new() -> Self {
        let mut store = Self {
            patterns: HashMap::new(),
            count: 0,
        };

        // Load default patterns
        store.load_default_patterns();

        store
    }

    /// Create an empty pattern store
    #[must_use]
    pub fn empty() -> Self {
        Self {
            patterns: HashMap::new(),
            count: 0,
        }
    }

    /// Load default fix patterns
    fn load_default_patterns(&mut self) {
        // TypeMismatch patterns
        self.add_pattern(
            FixPattern::new("FIX-001", ErrorCategory::TypeMismatch)
                .with_error_pattern(r"expected `&str`, found `String`")
                .with_description("Convert String to &str using .as_str()")
                .with_transformation(".as_str()")
                .with_success_rate(0.95),
        );

        self.add_pattern(
            FixPattern::new("FIX-002", ErrorCategory::TypeMismatch)
                .with_error_pattern(r"expected `String`, found `&str`")
                .with_description("Convert &str to String using .to_string()")
                .with_transformation(".to_string()")
                .with_success_rate(0.95),
        );

        self.add_pattern(
            FixPattern::new("FIX-003", ErrorCategory::TypeMismatch)
                .with_error_pattern(r"expected `&\[.*\]`, found `Vec<")
                .with_description("Convert Vec to slice using .as_slice()")
                .with_transformation(".as_slice()")
                .with_success_rate(0.90),
        );

        // BorrowChecker patterns
        self.add_pattern(
            FixPattern::new("FIX-004", ErrorCategory::BorrowChecker)
                .with_error_pattern(r"borrow of moved value")
                .with_description("Clone the value before moving")
                .with_transformation(".clone()")
                .with_success_rate(0.85),
        );

        self.add_pattern(
            FixPattern::new("FIX-005", ErrorCategory::BorrowChecker)
                .with_error_pattern(r"cannot borrow .* as mutable")
                .with_description("Change let to let mut")
                .with_transformation("let mut")
                .with_success_rate(0.90),
        );

        // MissingImport patterns
        self.add_pattern(
            FixPattern::new("FIX-006", ErrorCategory::MissingImport)
                .with_error_pattern(r"cannot find type `HashMap`")
                .with_description("Add use std::collections::HashMap;")
                .with_transformation("use std::collections::HashMap;")
                .with_success_rate(0.99),
        );

        self.add_pattern(
            FixPattern::new("FIX-007", ErrorCategory::MissingImport)
                .with_error_pattern(r"cannot find type `HashSet`")
                .with_description("Add use std::collections::HashSet;")
                .with_transformation("use std::collections::HashSet;")
                .with_success_rate(0.99),
        );

        self.add_pattern(
            FixPattern::new("FIX-008", ErrorCategory::MissingImport)
                .with_error_pattern(r"cannot find type `BTreeMap`")
                .with_description("Add use std::collections::BTreeMap;")
                .with_transformation("use std::collections::BTreeMap;")
                .with_success_rate(0.99),
        );

        // TraitBound patterns
        self.add_pattern(
            FixPattern::new("FIX-009", ErrorCategory::TraitBound)
                .with_error_pattern(r"the trait `Debug` is not implemented")
                .with_description("Add #[derive(Debug)] to the type")
                .with_transformation("#[derive(Debug)]")
                .with_success_rate(0.95),
        );

        self.add_pattern(
            FixPattern::new("FIX-010", ErrorCategory::TraitBound)
                .with_error_pattern(r"the trait `Clone` is not implemented")
                .with_description("Add #[derive(Clone)] to the type")
                .with_transformation("#[derive(Clone)]")
                .with_success_rate(0.95),
        );

        self.add_pattern(
            FixPattern::new("FIX-011", ErrorCategory::TraitBound)
                .with_error_pattern(r"the trait `Default` is not implemented")
                .with_description("Add #[derive(Default)] to the type")
                .with_transformation("#[derive(Default)]")
                .with_success_rate(0.90),
        );

        // MutabilityError patterns
        self.add_pattern(
            FixPattern::new("FIX-012", ErrorCategory::MutabilityError)
                .with_error_pattern(r"cannot assign to .*, as it is not declared as mutable")
                .with_description("Change let to let mut")
                .with_transformation("let mut")
                .with_success_rate(0.95),
        );

        self.add_pattern(
            FixPattern::new("FIX-013", ErrorCategory::MutabilityError)
                .with_error_pattern(
                    r"cannot borrow .* as mutable, as it is not declared as mutable",
                )
                .with_description("Change let to let mut")
                .with_transformation("let mut")
                .with_success_rate(0.95),
        );

        // LifetimeError patterns
        self.add_pattern(
            FixPattern::new("FIX-014", ErrorCategory::LifetimeError)
                .with_error_pattern(r"borrowed value does not live long enough")
                .with_description("Clone the value to extend lifetime")
                .with_transformation(".clone()")
                .with_success_rate(0.70),
        );

        self.add_pattern(
            FixPattern::new("FIX-015", ErrorCategory::LifetimeError)
                .with_error_pattern(r"missing lifetime specifier")
                .with_description("Add lifetime annotation 'a")
                .with_transformation("<'a>")
                .with_success_rate(0.60),
        );

        // SyntaxError patterns (Clippy lints)
        self.add_pattern(
            FixPattern::new("FIX-016", ErrorCategory::SyntaxError)
                .with_error_pattern(r"item in documentation is missing backticks")
                .with_description("Add backticks around type names in docs")
                .with_transformation("`TypeName`")
                .with_success_rate(0.99),
        );

        self.add_pattern(
            FixPattern::new("FIX-017", ErrorCategory::SyntaxError)
                .with_error_pattern(r"called `map\(<f>\)\.unwrap_or\(<a>\)`")
                .with_description("Use map_or() instead of map().unwrap_or()")
                .with_transformation(".map_or(default, |x| ...)")
                .with_success_rate(0.95),
        );

        self.add_pattern(
            FixPattern::new("FIX-018", ErrorCategory::SyntaxError)
                .with_error_pattern(r"redundant closure")
                .with_description("Replace closure with method reference")
                .with_transformation("|x| x.method() → Type::method")
                .with_success_rate(0.90),
        );

        // Module resolution patterns
        self.add_pattern(
            FixPattern::new("FIX-019", ErrorCategory::MissingImport)
                .with_error_pattern(r"Module .* not resolved")
                .with_description("Ensure module file exists in same directory")
                .with_transformation("Create module.ruchy file")
                .with_success_rate(0.85),
        );

        self.add_pattern(
            FixPattern::new("FIX-020", ErrorCategory::MissingImport)
                .with_error_pattern(r"Failed to resolve module declaration")
                .with_description("Check module file path and name")
                .with_transformation("mod name; requires name.ruchy file")
                .with_success_rate(0.80),
        );

        // Method not found patterns
        self.add_pattern(
            FixPattern::new("FIX-021", ErrorCategory::TraitBound)
                .with_error_pattern(r"no method named .* found for struct")
                .with_description("Check method name or add impl block")
                .with_transformation("impl StructName { fn method_name() {...} }")
                .with_success_rate(0.75),
        );

        self.add_pattern(
            FixPattern::new("FIX-022", ErrorCategory::TraitBound)
                .with_error_pattern(r"method not found in")
                .with_description("Import the trait or check method spelling")
                .with_transformation("use TraitName;")
                .with_success_rate(0.70),
        );
    }

    /// Add a pattern to the store
    pub fn add_pattern(&mut self, pattern: FixPattern) {
        let category = pattern.category;
        self.patterns.entry(category).or_default().push(pattern);
        self.count += 1;
    }

    /// Query patterns for a category and error message
    #[must_use]
    pub fn query(
        &self,
        category: ErrorCategory,
        error_message: &str,
        similarity_threshold: f64,
    ) -> Vec<FixSuggestion> {
        let mut suggestions = Vec::new();

        if let Some(patterns) = self.patterns.get(&category) {
            for pattern in patterns {
                if pattern.matches(error_message) && pattern.success_rate >= similarity_threshold {
                    suggestions.push(pattern.to_suggestion());
                }
            }
        }

        // Sort by success rate (descending)
        suggestions.sort_by(|a, b| {
            b.success_rate
                .partial_cmp(&a.success_rate)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        suggestions
    }

    /// Get pattern count
    #[must_use]
    pub fn count(&self) -> usize {
        self.count
    }

    /// Get patterns for a category
    #[must_use]
    pub fn patterns_for(&self, category: ErrorCategory) -> Option<&Vec<FixPattern>> {
        self.patterns.get(&category)
    }

    /// Get all patterns
    pub fn all_patterns(&self) -> impl Iterator<Item = &FixPattern> {
        self.patterns.values().flatten()
    }

    /// Record pattern usage (increment counter)
    pub fn record_usage(&mut self, pattern_id: &str, success: bool) {
        for patterns in self.patterns.values_mut() {
            for pattern in patterns.iter_mut() {
                if pattern.id == pattern_id {
                    pattern.usage_count += 1;
                    // Update success rate with exponential moving average
                    let alpha = 0.1;
                    let outcome = if success { 1.0 } else { 0.0 };
                    pattern.success_rate = alpha * outcome + (1.0 - alpha) * pattern.success_rate;
                    return;
                }
            }
        }
    }
}

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

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

    // ============================================================================
    // EXTREME TDD: Pattern Store Tests
    // ============================================================================

    #[test]
    fn test_fix_suggestion_new() {
        let suggestion = FixSuggestion::new("Add .clone()");
        assert_eq!(suggestion.description, "Add .clone()");
        assert!(suggestion.transformation.is_empty());
    }

    #[test]
    fn test_fix_suggestion_builder() {
        let suggestion = FixSuggestion::new("Add .clone()")
            .with_transformation(".clone()")
            .with_success_rate(0.9)
            .with_times_applied(100)
            .with_pattern_id("FIX-001");

        assert_eq!(suggestion.transformation, ".clone()");
        assert!((suggestion.success_rate - 0.9).abs() < f64::EPSILON);
        assert_eq!(suggestion.times_applied, 100);
        assert_eq!(suggestion.pattern_id, "FIX-001");
    }

    #[test]
    fn test_fix_pattern_new() {
        let pattern = FixPattern::new("FIX-001", ErrorCategory::TypeMismatch);
        assert_eq!(pattern.id, "FIX-001");
        assert_eq!(pattern.category, ErrorCategory::TypeMismatch);
    }

    #[test]
    fn test_fix_pattern_builder() {
        let pattern = FixPattern::new("FIX-001", ErrorCategory::TypeMismatch)
            .with_error_pattern(r"expected.*found")
            .with_description("Fix type mismatch")
            .with_transformation(".to_string()")
            .with_success_rate(0.9);

        assert!(!pattern.error_pattern.is_empty());
        assert!(!pattern.description.is_empty());
        assert!(!pattern.transformation.is_empty());
    }

    #[test]
    fn test_fix_pattern_matches_regex() {
        let pattern = FixPattern::new("FIX-001", ErrorCategory::TypeMismatch)
            .with_error_pattern(r"expected `&str`, found `String`");

        assert!(pattern.matches("expected `&str`, found `String`"));
        assert!(!pattern.matches("something else"));
    }

    #[test]
    fn test_fix_pattern_matches_empty() {
        let pattern = FixPattern::new("FIX-001", ErrorCategory::TypeMismatch);
        assert!(!pattern.matches("anything"));
    }

    #[test]
    fn test_fix_pattern_to_suggestion() {
        let pattern = FixPattern::new("FIX-001", ErrorCategory::TypeMismatch)
            .with_description("Fix it")
            .with_transformation(".fix()")
            .with_success_rate(0.85);

        let suggestion = pattern.to_suggestion();
        assert_eq!(suggestion.description, "Fix it");
        assert_eq!(suggestion.transformation, ".fix()");
        assert!((suggestion.success_rate - 0.85).abs() < f64::EPSILON);
        assert_eq!(suggestion.pattern_id, "FIX-001");
    }

    #[test]
    fn test_pattern_store_new_has_defaults() {
        let store = PatternStore::new();
        assert!(store.count() > 0);
    }

    #[test]
    fn test_pattern_store_empty() {
        let store = PatternStore::empty();
        assert_eq!(store.count(), 0);
    }

    #[test]
    fn test_pattern_store_add_pattern() {
        let mut store = PatternStore::empty();
        store.add_pattern(FixPattern::new("TEST-001", ErrorCategory::TypeMismatch));
        assert_eq!(store.count(), 1);
    }

    #[test]
    fn test_pattern_store_query_matching() {
        let store = PatternStore::new();

        let suggestions = store.query(
            ErrorCategory::TypeMismatch,
            "expected `&str`, found `String`",
            0.0,
        );

        assert!(!suggestions.is_empty());
    }

    #[test]
    fn test_pattern_store_query_no_match() {
        let store = PatternStore::new();

        let suggestions = store.query(
            ErrorCategory::TypeMismatch,
            "completely unrelated error",
            0.0,
        );

        assert!(suggestions.is_empty());
    }

    #[test]
    fn test_pattern_store_query_threshold() {
        let store = PatternStore::new();

        // Query with high threshold
        let suggestions = store.query(
            ErrorCategory::TypeMismatch,
            "expected `&str`, found `String`",
            0.99, // Very high threshold
        );

        // May or may not have results depending on pattern success rates
        // Just verify no panic
        let _ = suggestions;
    }

    #[test]
    fn test_pattern_store_query_sorted() {
        let mut store = PatternStore::empty();

        // Add patterns with different success rates
        store.add_pattern(
            FixPattern::new("LOW", ErrorCategory::TypeMismatch)
                .with_error_pattern("test")
                .with_success_rate(0.5),
        );
        store.add_pattern(
            FixPattern::new("HIGH", ErrorCategory::TypeMismatch)
                .with_error_pattern("test")
                .with_success_rate(0.9),
        );

        let suggestions = store.query(ErrorCategory::TypeMismatch, "test", 0.0);

        assert_eq!(suggestions.len(), 2);
        assert_eq!(suggestions[0].pattern_id, "HIGH"); // Higher success rate first
        assert_eq!(suggestions[1].pattern_id, "LOW");
    }

    #[test]
    fn test_pattern_store_patterns_for() {
        let store = PatternStore::new();

        let type_patterns = store.patterns_for(ErrorCategory::TypeMismatch);
        assert!(type_patterns.is_some());
        assert!(!type_patterns.unwrap().is_empty());
    }

    #[test]
    fn test_pattern_store_all_patterns() {
        let store = PatternStore::new();
        let all: Vec<_> = store.all_patterns().collect();
        assert!(!all.is_empty());
        assert_eq!(all.len(), store.count());
    }

    #[test]
    fn test_pattern_store_record_usage() {
        let mut store = PatternStore::new();

        // Get initial usage count for FIX-001
        let initial_count = store
            .patterns_for(ErrorCategory::TypeMismatch)
            .and_then(|p| p.first())
            .map_or(0, |p| p.usage_count);

        store.record_usage("FIX-001", true);

        let new_count = store
            .patterns_for(ErrorCategory::TypeMismatch)
            .and_then(|p| p.first())
            .map_or(0, |p| p.usage_count);

        assert_eq!(new_count, initial_count + 1);
    }

    #[test]
    fn test_pattern_store_default() {
        let store = PatternStore::default();
        assert!(store.count() > 0);
    }

    #[test]
    fn test_default_patterns_coverage() {
        let store = PatternStore::new();

        // Check we have patterns for key categories
        assert!(store.patterns_for(ErrorCategory::TypeMismatch).is_some());
        assert!(store.patterns_for(ErrorCategory::BorrowChecker).is_some());
        assert!(store.patterns_for(ErrorCategory::MissingImport).is_some());
        assert!(store.patterns_for(ErrorCategory::TraitBound).is_some());
        assert!(store.patterns_for(ErrorCategory::MutabilityError).is_some());
        assert!(store.patterns_for(ErrorCategory::LifetimeError).is_some());
    }

    #[test]
    fn test_fix_suggestion_clone() {
        let suggestion = FixSuggestion::new("test").with_success_rate(0.9);
        let cloned = suggestion.clone();
        assert_eq!(suggestion.description, cloned.description);
        assert!((suggestion.success_rate - cloned.success_rate).abs() < f64::EPSILON);
    }

    #[test]
    fn test_fix_pattern_clone() {
        let pattern =
            FixPattern::new("FIX-001", ErrorCategory::TypeMismatch).with_success_rate(0.9);
        let cloned = pattern.clone();
        assert_eq!(pattern.id, cloned.id);
        assert_eq!(pattern.category, cloned.category);
    }
}