depyler-oracle 4.1.1

ML-powered compile error classification and auto-fixing using aprender models
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
//! Property-based tests for the oracle.
//!
//! Uses proptest to verify invariants across random inputs.

use proptest::prelude::*;

use crate::classifier::{ErrorCategory, ErrorClassifier};
use crate::features::ErrorFeatures;
use crate::ngram::{FixPattern, NgramFixPredictor};
use crate::patterns::{FixTemplate, FixTemplateRegistry};
use crate::tfidf::TfidfFeatureExtractor;
use crate::training::{TrainingDataset, TrainingSample};

// ============================================
// Strategies
// ============================================

/// Generate random error messages.
fn error_message_strategy() -> impl Strategy<Value = String> {
    prop::collection::vec(
        prop::sample::select(vec![
            "error",
            "expected",
            "found",
            "type",
            "mismatch",
            "cannot",
            "move",
            "borrow",
            "borrowed",
            "lifetime",
            "'a",
            "'static",
            "trait",
            "bound",
            "not",
            "implemented",
            "use",
            "import",
            "crate",
            "module",
            "syntax",
            "semicolon",
            "E0308",
            "E0382",
            "E0106",
            "i32",
            "u32",
            "String",
            "&str",
            "Option",
            "Result",
            "Clone",
            "Debug",
            "Send",
            "Sync",
        ]),
        1..20,
    )
    .prop_map(|words| words.join(" "))
}

/// Generate random error categories.
fn category_strategy() -> impl Strategy<Value = ErrorCategory> {
    prop::sample::select(vec![
        ErrorCategory::TypeMismatch,
        ErrorCategory::BorrowChecker,
        ErrorCategory::MissingImport,
        ErrorCategory::SyntaxError,
        ErrorCategory::LifetimeError,
        ErrorCategory::TraitBound,
        ErrorCategory::Other,
    ])
}

/// Generate random fix templates.
fn fix_template_strategy() -> impl Strategy<Value = String> {
    prop::collection::vec(
        prop::sample::select(vec![
            "Use",
            "Add",
            "Remove",
            "Clone",
            "Convert",
            "Import",
            "Derive",
            ".to_string()",
            ".clone()",
            ".into()",
            "as",
            "&",
            "&mut",
            "Box",
            "Arc",
            "Rc",
        ]),
        1..10,
    )
    .prop_map(|words| words.join(" "))
}

// ============================================
// ErrorCategory Properties
// ============================================

proptest! {
    /// Category index roundtrip: index -> from_index -> index
    #[test]
    fn prop_category_index_roundtrip(category in category_strategy()) {
        let idx = category.index();
        let recovered = ErrorCategory::from_index(idx);
        prop_assert_eq!(recovered, category);
    }

    /// All category indices are unique and in valid range
    #[test]
    fn prop_category_indices_valid(category in category_strategy()) {
        let idx = category.index();
        prop_assert!(idx < 7, "Category index {} out of range", idx);
    }

    /// Category names are non-empty
    #[test]
    fn prop_category_names_nonempty(category in category_strategy()) {
        let name = category.name();
        prop_assert!(!name.is_empty(), "Category name should not be empty");
    }
}

// ============================================
// ErrorClassifier Properties
// ============================================

proptest! {
    /// Classification always returns a valid category
    #[test]
    fn prop_classifier_returns_valid_category(msg in error_message_strategy()) {
        let classifier = ErrorClassifier::new();
        let category = classifier.classify_by_keywords(&msg);
        // Verify it's a valid category by checking index
        let _ = category.index(); // Should not panic
    }

    /// Confidence is always in valid range [0.0, 1.0]
    #[test]
    fn prop_classifier_confidence_valid_range(
        msg in error_message_strategy(),
        category in category_strategy()
    ) {
        let classifier = ErrorClassifier::new();
        let conf = classifier.confidence(&msg, category);
        prop_assert!(conf >= 0.0, "Confidence {} < 0.0", conf);
        prop_assert!(conf <= 1.0, "Confidence {} > 1.0", conf);
    }

    /// Classification is deterministic
    #[test]
    fn prop_classifier_deterministic(msg in error_message_strategy()) {
        let classifier = ErrorClassifier::new();
        let cat1 = classifier.classify_by_keywords(&msg);
        let cat2 = classifier.classify_by_keywords(&msg);
        prop_assert_eq!(cat1, cat2);
    }
}

// ============================================
// ErrorFeatures Properties
// ============================================

proptest! {
    /// Features are always valid (no NaN, no infinite)
    #[test]
    fn prop_features_valid_values(msg in error_message_strategy()) {
        let features = ErrorFeatures::from_error_message(&msg);
        let vec = features.to_vec();

        for (i, &v) in vec.iter().enumerate() {
            prop_assert!(!v.is_nan(), "Feature {} is NaN", i);
            prop_assert!(!v.is_infinite(), "Feature {} is infinite", i);
        }
    }

    /// Features are in expected ranges [0.0, 1.0]
    #[test]
    fn prop_features_in_range(msg in error_message_strategy()) {
        let features = ErrorFeatures::from_error_message(&msg);
        let vec = features.to_vec();

        for (i, &v) in vec.iter().enumerate() {
            prop_assert!(v >= 0.0, "Feature {} is negative: {}", i, v);
            prop_assert!(v <= 1.0, "Feature {} > 1.0: {}", i, v);
        }
    }

    /// Feature vector has correct dimension
    #[test]
    fn prop_features_correct_dim(msg in error_message_strategy()) {
        let features = ErrorFeatures::from_error_message(&msg);
        let vec = features.to_vec();
        prop_assert_eq!(vec.len(), ErrorFeatures::DIM);
    }

    /// Feature roundtrip: to_vec -> from_vec
    #[test]
    fn prop_features_roundtrip(msg in error_message_strategy()) {
        let features = ErrorFeatures::from_error_message(&msg);
        let vec = features.to_vec();
        let restored = ErrorFeatures::from_vec(&vec);
        let restored_vec = restored.to_vec();

        for (i, (&orig, &rest)) in vec.iter().zip(restored_vec.iter()).enumerate() {
            prop_assert!(
                (orig - rest).abs() < 1e-6,
                "Feature {} mismatch: {} vs {}",
                i,
                orig,
                rest
            );
        }
    }
}

// ============================================
// FixPattern Properties
// ============================================

proptest! {
    /// Pattern frequency only increases
    #[test]
    fn prop_pattern_frequency_monotonic(
        msg in error_message_strategy(),
        fix in fix_template_strategy(),
        increments in 0..100usize
    ) {
        let mut pattern = FixPattern::new(&msg, &fix, ErrorCategory::Other);
        let initial = pattern.frequency;

        for _ in 0..increments {
            pattern.increment();
        }

        prop_assert_eq!(pattern.frequency, initial + increments);
    }

    /// Success rate stays in [0.0, 1.0]
    #[test]
    fn prop_pattern_success_rate_bounded(
        msg in error_message_strategy(),
        successes in prop::collection::vec(any::<bool>(), 0..50)
    ) {
        let mut pattern = FixPattern::new(&msg, "fix", ErrorCategory::Other);

        for success in successes {
            pattern.update_success(success);
        }

        prop_assert!(pattern.success_rate >= 0.0);
        prop_assert!(pattern.success_rate <= 1.0);
    }
}

// ============================================
// NgramFixPredictor Properties
// ============================================

proptest! {
    /// Predictions are sorted by confidence (descending)
    #[test]
    fn prop_predictions_sorted_by_confidence(
        patterns in prop::collection::vec(
            (error_message_strategy(), fix_template_strategy(), category_strategy()),
            1..10
        ),
        query in error_message_strategy()
    ) {
        let mut predictor = NgramFixPredictor::new();

        for (msg, fix, cat) in &patterns {
            predictor.learn_pattern(msg, fix, *cat);
        }

        if predictor.fit().is_ok() {
            let suggestions = predictor.predict_fixes(&query, 10);

            for window in suggestions.windows(2) {
                prop_assert!(
                    window[0].confidence >= window[1].confidence,
                    "Predictions not sorted: {} < {}",
                    window[0].confidence,
                    window[1].confidence
                );
            }
        }
    }

    /// Prediction confidence is always valid
    #[test]
    fn prop_prediction_confidence_valid(
        patterns in prop::collection::vec(
            (error_message_strategy(), fix_template_strategy(), category_strategy()),
            1..10
        ),
        query in error_message_strategy()
    ) {
        let mut predictor = NgramFixPredictor::new();

        for (msg, fix, cat) in &patterns {
            predictor.learn_pattern(msg, fix, *cat);
        }

        if predictor.fit().is_ok() {
            let suggestions = predictor.predict_fixes(&query, 10);

            for s in suggestions {
                prop_assert!(s.confidence >= 0.0, "Confidence negative: {}", s.confidence);
                prop_assert!(s.confidence <= 1.0, "Confidence > 1: {}", s.confidence);
            }
        }
    }

    /// Pattern count is consistent
    #[test]
    fn prop_pattern_count_consistent(
        patterns in prop::collection::vec(
            (error_message_strategy(), fix_template_strategy(), category_strategy()),
            0..20
        )
    ) {
        let mut predictor = NgramFixPredictor::new();

        for (msg, fix, cat) in &patterns {
            predictor.learn_pattern(msg, fix, *cat);
        }

        // Count should be <= input patterns (duplicates merged)
        prop_assert!(predictor.pattern_count() <= patterns.len());
    }
}

// ============================================
// FixTemplateRegistry Properties
// ============================================

proptest! {
    /// All registered templates can be retrieved
    #[test]
    fn prop_registry_retrieval(
        n_templates in 1..10usize
    ) {
        let mut registry = FixTemplateRegistry::new();

        for i in 0..n_templates {
            let template = FixTemplate::builder(
                &format!("test-{}", i),
                "Test",
                ErrorCategory::Other
            )
            .with_keywords(&["test"])
            .build();

            registry.register(template);
        }

        prop_assert_eq!(registry.template_count(), n_templates);
    }

    /// Match scores are non-negative
    #[test]
    fn prop_template_match_scores_nonnegative(msg in error_message_strategy()) {
        let registry = FixTemplateRegistry::with_rust_defaults();

        for template in registry.all_templates() {
            let score = template.match_score(&msg);
            prop_assert!(score >= 0.0, "Score negative: {}", score);
        }
    }
}

// ============================================
// TfidfFeatureExtractor Properties
// ============================================

proptest! {
    /// TF-IDF values are non-negative
    #[test]
    fn prop_tfidf_nonnegative(
        docs in prop::collection::vec(error_message_strategy(), 2..10)
    ) {
        let mut extractor = TfidfFeatureExtractor::new()
            .with_max_features(50);

        if extractor.fit(&docs).is_ok() {
            if let Ok(matrix) = extractor.transform(&docs) {
                for i in 0..matrix.n_rows() {
                    for j in 0..matrix.n_cols() {
                        let val = matrix.get(i, j);
                        prop_assert!(val >= 0.0, "TF-IDF negative at ({}, {}): {}", i, j, val);
                    }
                }
            }
        }
    }

    /// Vocabulary size is bounded by max_features
    #[test]
    fn prop_vocabulary_bounded(
        docs in prop::collection::vec(error_message_strategy(), 2..10),
        max_features in 10..100usize
    ) {
        let mut extractor = TfidfFeatureExtractor::new()
            .with_max_features(max_features);

        if extractor.fit(&docs).is_ok() {
            prop_assert!(
                extractor.vocabulary_size() <= max_features,
                "Vocabulary {} > max_features {}",
                extractor.vocabulary_size(),
                max_features
            );
        }
    }
}

// ============================================
// TrainingDataset Properties
// ============================================

proptest! {
    /// Dataset length is consistent
    #[test]
    fn prop_dataset_length_consistent(
        samples in prop::collection::vec(
            (error_message_strategy(), category_strategy()),
            0..50
        )
    ) {
        let mut dataset = TrainingDataset::new();

        for (msg, cat) in &samples {
            dataset.add(TrainingSample::new(msg, *cat));
        }

        prop_assert_eq!(dataset.len(), samples.len());
        prop_assert_eq!(dataset.messages().len(), samples.len());
        prop_assert_eq!(dataset.labels().len(), samples.len());
    }

    /// Labels match category indices
    #[test]
    fn prop_dataset_labels_match(
        samples in prop::collection::vec(
            (error_message_strategy(), category_strategy()),
            1..20
        )
    ) {
        let mut dataset = TrainingDataset::new();

        for (msg, cat) in &samples {
            dataset.add(TrainingSample::new(msg, *cat));
        }

        let labels = dataset.labels();

        for (i, (_, cat)) in samples.iter().enumerate() {
            prop_assert_eq!(
                labels[i],
                cat.index(),
                "Label mismatch at index {}",
                i
            );
        }
    }
}

// ============================================
// Cross-Component Properties
// ============================================

proptest! {
    /// Classifier and features are consistent
    #[test]
    fn prop_classifier_features_consistency(msg in error_message_strategy()) {
        let classifier = ErrorClassifier::new();
        let features = ErrorFeatures::from_error_message(&msg);
        let category = classifier.classify_by_keywords(&msg);

        // If classifier detects strong type keywords, features should too
        if category == ErrorCategory::TypeMismatch {
            // Type keywords feature should be non-zero (most of the time)
            // This is a soft check due to threshold differences
        }

        // Both should handle same input without panicking
        let _ = features.to_vec();
        let _ = classifier.confidence(&msg, category);
    }
}

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

    proptest! {
        /// Full pipeline: classify -> extract features -> predict fix
        #[test]
        fn prop_full_pipeline(
            training in prop::collection::vec(
                (error_message_strategy(), fix_template_strategy(), category_strategy()),
                5..20
            ),
            query in error_message_strategy()
        ) {
            // Setup
            let classifier = ErrorClassifier::new();
            let mut predictor = NgramFixPredictor::new();
            let mut tfidf = TfidfFeatureExtractor::new().with_max_features(50);

            // Train predictor
            for (msg, fix, cat) in &training {
                predictor.learn_pattern(msg, fix, *cat);
            }
            let _ = predictor.fit();

            // Train TF-IDF
            let messages: Vec<&str> = training.iter().map(|(m, _, _)| m.as_str()).collect();
            let _ = tfidf.fit(&messages);

            // Classify query
            let category = classifier.classify_by_keywords(&query);
            let confidence = classifier.confidence(&query, category);
            prop_assert!((0.0..=1.0).contains(&confidence));

            // Extract features
            let features = ErrorFeatures::from_error_message(&query);
            let vec = features.to_vec();
            prop_assert_eq!(vec.len(), ErrorFeatures::DIM);

            // Predict fixes (may be empty if no good matches)
            let suggestions = predictor.predict_fixes(&query, 5);
            for s in &suggestions {
                prop_assert!(s.confidence >= 0.0 && s.confidence <= 1.0);
            }
        }
    }
}