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
//! Feature extraction from error messages.

use aprender::primitives::Matrix;
use serde::{Deserialize, Serialize};

/// Features extracted from an error message for ML classification.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ErrorFeatures {
    /// Message length (normalized)
    pub message_length: f32,
    /// Number of type-related keywords
    pub type_keywords: f32,
    /// Number of borrow-related keywords
    pub borrow_keywords: f32,
    /// Number of import-related keywords
    pub import_keywords: f32,
    /// Number of lifetime-related keywords
    pub lifetime_keywords: f32,
    /// Number of trait-related keywords
    pub trait_keywords: f32,
    /// Contains line number
    pub has_line_number: f32,
    /// Contains column number
    pub has_column: f32,
    /// Contains backticks (code snippets)
    pub has_code_snippets: f32,
    /// Contains arrow indicators
    pub has_arrows: f32,
    /// Error code present (e.g., E0308)
    pub has_error_code: f32,
    /// Number of suggestions in message
    pub suggestion_count: f32,
}

impl ErrorFeatures {
    /// Feature dimension.
    pub const DIM: usize = 12;

    /// Extract features from an error message.
    #[must_use]
    pub fn from_error_message(message: &str) -> Self {
        let lower = message.to_lowercase();

        Self {
            message_length: (message.len() as f32 / 500.0).min(1.0),

            type_keywords: count_keywords(
                &lower,
                &[
                    "expected",
                    "found",
                    "mismatched",
                    "type",
                    "cannot coerce",
                    "incompatible",
                ],
            ),

            borrow_keywords: count_keywords(
                &lower,
                &[
                    "borrow",
                    "borrowed",
                    "move",
                    "moved",
                    "ownership",
                    "cannot move",
                ],
            ),

            import_keywords: count_keywords(
                &lower,
                &[
                    "not found",
                    "unresolved",
                    "cannot find",
                    "undefined",
                    "undeclared",
                ],
            ),

            lifetime_keywords: count_keywords(
                &lower,
                &[
                    "lifetime",
                    "'a",
                    "'static",
                    "live long enough",
                    "dangling",
                    "borrowed value",
                ],
            ),

            trait_keywords: count_keywords(
                &lower,
                &[
                    "trait",
                    "impl",
                    "not implemented",
                    "bound",
                    "doesn't implement",
                ],
            ),

            has_line_number: if message.contains(':') && message.chars().any(|c| c.is_ascii_digit())
            {
                1.0
            } else {
                0.0
            },

            has_column: if message.matches(':').count() > 1 {
                1.0
            } else {
                0.0
            },

            has_code_snippets: (message.matches('`').count() as f32 / 10.0).min(1.0),

            has_arrows: if message.contains("-->") || message.contains("^^^") {
                1.0
            } else {
                0.0
            },

            has_error_code: if message.contains("E0") || message.contains("[E") {
                1.0
            } else {
                0.0
            },

            suggestion_count: count_keywords(
                &lower,
                &["help:", "suggestion:", "consider", "try", "perhaps"],
            ),
        }
    }

    /// Convert features to a row matrix for ML model.
    #[must_use]
    pub fn to_matrix(&self) -> Matrix<f32> {
        Matrix::from_vec(1, Self::DIM, self.to_vec()).expect("Feature dimensions are correct")
    }

    /// Convert features to a vector.
    #[must_use]
    pub fn to_vec(&self) -> Vec<f32> {
        vec![
            self.message_length,
            self.type_keywords,
            self.borrow_keywords,
            self.import_keywords,
            self.lifetime_keywords,
            self.trait_keywords,
            self.has_line_number,
            self.has_column,
            self.has_code_snippets,
            self.has_arrows,
            self.has_error_code,
            self.suggestion_count,
        ]
    }

    /// Create features from a vector.
    ///
    /// # Panics
    ///
    /// Panics if vector length doesn't match DIM.
    #[must_use]
    pub fn from_vec(v: &[f32]) -> Self {
        assert_eq!(
            v.len(),
            Self::DIM,
            "Feature vector must have {} elements",
            Self::DIM
        );

        Self {
            message_length: v[0],
            type_keywords: v[1],
            borrow_keywords: v[2],
            import_keywords: v[3],
            lifetime_keywords: v[4],
            trait_keywords: v[5],
            has_line_number: v[6],
            has_column: v[7],
            has_code_snippets: v[8],
            has_arrows: v[9],
            has_error_code: v[10],
            suggestion_count: v[11],
        }
    }
}

/// Count keyword occurrences (normalized).
fn count_keywords(text: &str, keywords: &[&str]) -> f32 {
    let count = keywords.iter().filter(|k| text.contains(*k)).count();
    (count as f32 / keywords.len() as f32).min(1.0)
}

// GH-210: Enhanced feature extraction for Code2Vec & GNN upgrade
// Expands from 12 to 73 dimensions for better ML classification

/// Top 25 Rust error codes for one-hot encoding
/// Based on frequency analysis from reprorusted-python-cli corpus
pub const ERROR_CODES: [&str; 25] = [
    "E0308", // mismatched types (most common)
    "E0425", // cannot find value
    "E0433", // failed to resolve
    "E0277", // trait bound not satisfied
    "E0599", // no method named
    "E0382", // use of moved value
    "E0502", // cannot borrow as mutable
    "E0503", // cannot use while mutably borrowed
    "E0505", // cannot move out of borrowed
    "E0506", // cannot assign to borrowed
    "E0507", // cannot move out of
    "E0106", // missing lifetime specifier
    "E0495", // cannot infer lifetime
    "E0621", // explicit lifetime required
    "E0282", // type annotations needed
    "E0283", // type annotations required
    "E0412", // cannot find type
    "E0432", // unresolved import
    "E0603", // private item
    "E0609", // no field
    "E0614", // cannot be dereferenced
    "E0615", // attempted to take value
    "E0616", // field is private
    "E0618", // expected function
    "E0620", // cast to unsized type
];

/// Extended keyword categories for detailed feature extraction
pub const KEYWORD_CATEGORIES: [(&str, &[&str]); 9] = [
    ("type_coercion", &["as", "into", "from", "convert", "cast"]),
    ("ownership", &["owned", "clone", "copy", "drop", "take"]),
    ("reference", &["ref", "&", "deref", "borrow"]),
    ("mutability", &["mut", "immutable", "mutable"]),
    ("generic", &["generic", "parameter", "constraint", "where"]),
    ("async", &["async", "await", "future", "poll"]),
    ("closure", &["closure", "capture", "fn", "move"]),
    ("derive", &["derive", "debug", "clone", "default"]),
    (
        "result_option",
        &["result", "option", "some", "none", "ok", "err", "unwrap"],
    ),
];

/// GH-210: Enhanced error features with 73 dimensions
/// Combines base features (12) + error code one-hot (25) + keyword counts (36)
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EnhancedErrorFeatures {
    /// Base 12 features from ErrorFeatures
    pub base: ErrorFeatures,
    /// One-hot encoding for top 25 error codes
    pub error_code_onehot: Vec<f32>,
    /// Detailed keyword occurrence counts (9 categories × 4 normalized features)
    pub keyword_counts: Vec<f32>,
}

impl Default for EnhancedErrorFeatures {
    fn default() -> Self {
        Self {
            base: ErrorFeatures::default(),
            error_code_onehot: vec![0.0; 25],
            keyword_counts: vec![0.0; 36],
        }
    }
}

impl EnhancedErrorFeatures {
    /// Enhanced feature dimension: 12 + 25 + 36 = 73
    pub const DIM: usize = 73;

    /// Extract enhanced features from error message
    #[must_use]
    pub fn from_error_message(message: &str) -> Self {
        let lower = message.to_lowercase();

        // Base features
        let base = ErrorFeatures::from_error_message(message);

        // Error code one-hot encoding
        let mut error_code_onehot = vec![0.0f32; 25];
        for (i, code) in ERROR_CODES.iter().enumerate() {
            if message.contains(code) {
                error_code_onehot[i] = 1.0;
                break; // Only one error code per message
            }
        }

        // Extended keyword counts (9 categories × 4 features each)
        let mut keyword_counts = vec![0.0f32; 36];
        for (i, (_name, keywords)) in KEYWORD_CATEGORIES.iter().enumerate() {
            let base_idx = i * 4;
            // Feature 1: presence (0 or 1)
            let present = keywords.iter().any(|k| lower.contains(k));
            keyword_counts[base_idx] = if present { 1.0 } else { 0.0 };

            // Feature 2: count ratio (normalized)
            let count = keywords.iter().filter(|k| lower.contains(*k)).count();
            keyword_counts[base_idx + 1] = (count as f32 / keywords.len() as f32).min(1.0);

            // Feature 3: first occurrence position (normalized by message length)
            let first_pos = keywords
                .iter()
                .filter_map(|k| lower.find(k))
                .min()
                .unwrap_or(lower.len());
            keyword_counts[base_idx + 2] = 1.0 - (first_pos as f32 / lower.len().max(1) as f32);

            // Feature 4: keyword density (occurrences per 100 chars)
            let total_occurrences: usize = keywords.iter().map(|k| lower.matches(k).count()).sum();
            keyword_counts[base_idx + 3] =
                (total_occurrences as f32 * 100.0 / lower.len().max(1) as f32).min(1.0);
        }

        Self {
            base,
            error_code_onehot,
            keyword_counts,
        }
    }

    /// Convert to feature vector for ML model
    #[must_use]
    pub fn to_vec(&self) -> Vec<f32> {
        let mut vec = Vec::with_capacity(Self::DIM);
        vec.extend(self.base.to_vec());
        vec.extend(self.error_code_onehot.iter());
        vec.extend(self.keyword_counts.iter());
        vec
    }

    /// Convert to matrix for ML model
    #[must_use]
    pub fn to_matrix(&self) -> Matrix<f32> {
        Matrix::from_vec(1, Self::DIM, self.to_vec()).expect("Feature dimensions are correct")
    }
}

/// Batch extraction for enhanced features
pub struct EnhancedFeatureExtractor;

impl EnhancedFeatureExtractor {
    /// Extract enhanced features from multiple error messages
    #[must_use]
    pub fn extract_batch(messages: &[&str]) -> Matrix<f32> {
        let features: Vec<f32> = messages
            .iter()
            .flat_map(|msg| EnhancedErrorFeatures::from_error_message(msg).to_vec())
            .collect();

        Matrix::from_vec(messages.len(), EnhancedErrorFeatures::DIM, features)
            .expect("Feature batch dimensions are correct")
    }
}

/// Batch feature extraction for training data.
pub struct FeatureExtractor;

impl FeatureExtractor {
    /// Extract features from multiple error messages.
    #[must_use]
    pub fn extract_batch(messages: &[&str]) -> Matrix<f32> {
        let features: Vec<f32> = messages
            .iter()
            .flat_map(|msg| ErrorFeatures::from_error_message(msg).to_vec())
            .collect();

        Matrix::from_vec(messages.len(), ErrorFeatures::DIM, features)
            .expect("Feature batch dimensions are correct")
    }
}

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

    #[test]
    fn test_feature_extraction() {
        let msg = "error[E0308]: mismatched types\n  --> src/main.rs:10:5\n   |\n10 |     foo(bar)\n   |         ^^^ expected `i32`, found `&str`";

        let features = ErrorFeatures::from_error_message(msg);

        assert!(features.message_length > 0.0);
        assert!(features.type_keywords > 0.0);
        assert!(features.has_error_code > 0.0);
        assert!(features.has_line_number > 0.0);
        assert!(features.has_arrows > 0.0);
    }

    #[test]
    fn test_borrow_features() {
        let msg = "error: cannot move out of borrowed content";
        let features = ErrorFeatures::from_error_message(msg);

        assert!(features.borrow_keywords > 0.0);
        assert!((features.type_keywords - 0.0).abs() < 0.1);
    }

    #[test]
    fn test_to_matrix() {
        let msg = "error: expected i32";
        let features = ErrorFeatures::from_error_message(msg);
        let matrix = features.to_matrix();

        assert_eq!(matrix.n_rows(), 1);
        assert_eq!(matrix.n_cols(), ErrorFeatures::DIM);
    }

    #[test]
    fn test_vec_roundtrip() {
        let msg = "error: mismatched types";
        let features = ErrorFeatures::from_error_message(msg);
        let vec = features.to_vec();
        let restored = ErrorFeatures::from_vec(&vec);

        assert!((features.type_keywords - restored.type_keywords).abs() < 1e-6);
    }

    #[test]
    fn test_batch_extraction() {
        let messages = vec![
            "error: expected i32",
            "error: cannot move",
            "error: not found",
        ];

        let matrix = FeatureExtractor::extract_batch(&messages);

        assert_eq!(matrix.n_rows(), 3);
        assert_eq!(matrix.n_cols(), ErrorFeatures::DIM);
    }

    #[test]
    fn test_lifetime_features() {
        let msg = "error: `x` does not live long enough";
        let features = ErrorFeatures::from_error_message(msg);

        assert!(features.lifetime_keywords > 0.0);
    }

    #[test]
    fn test_trait_features() {
        let msg = "error: the trait bound `T: Clone` is not satisfied";
        let features = ErrorFeatures::from_error_message(msg);

        assert!(features.trait_keywords > 0.0);
    }

    #[test]
    fn test_suggestion_count() {
        let msg = "error: type mismatch\nhelp: try using `.into()`\nhelp: consider adding type annotation";
        let features = ErrorFeatures::from_error_message(msg);

        assert!(features.suggestion_count > 0.0);
    }

    // GH-210: Tests for enhanced features

    #[test]
    fn test_enhanced_feature_dimension() {
        let msg = "error[E0308]: mismatched types";
        let features = EnhancedErrorFeatures::from_error_message(msg);
        let vec = features.to_vec();

        assert_eq!(vec.len(), EnhancedErrorFeatures::DIM);
        assert_eq!(vec.len(), 73);
    }

    #[test]
    fn test_enhanced_error_code_onehot() {
        let msg = "error[E0308]: mismatched types\n  --> src/main.rs:10:5";
        let features = EnhancedErrorFeatures::from_error_message(msg);

        // E0308 is at index 0
        assert_eq!(features.error_code_onehot[0], 1.0);
        // All others should be 0
        assert_eq!(features.error_code_onehot[1..].iter().sum::<f32>(), 0.0);
    }

    #[test]
    fn test_enhanced_e0425_onehot() {
        let msg = "error[E0425]: cannot find value `foo` in this scope";
        let features = EnhancedErrorFeatures::from_error_message(msg);

        // E0425 is at index 1
        assert_eq!(features.error_code_onehot[1], 1.0);
    }

    #[test]
    fn test_enhanced_keyword_categories() {
        let msg = "error: cannot convert `&str` into `String`";
        let features = EnhancedErrorFeatures::from_error_message(msg);

        // type_coercion category (index 0-3) should have hits
        // "into" and "convert" are both present
        assert!(features.keyword_counts[0] > 0.0, "type_coercion presence");
        assert!(
            features.keyword_counts[1] > 0.0,
            "type_coercion count ratio"
        );
    }

    #[test]
    fn test_enhanced_result_option_keywords() {
        let msg = "error: cannot call `.unwrap()` on `Result<T, E>`";
        let features = EnhancedErrorFeatures::from_error_message(msg);

        // result_option category is index 8 (8 * 4 = 32-35)
        assert!(features.keyword_counts[32] > 0.0, "result_option presence");
    }

    #[test]
    fn test_enhanced_batch_extraction() {
        let messages = vec![
            "error[E0308]: expected i32, found &str",
            "error[E0382]: use of moved value",
            "error[E0277]: trait bound not satisfied",
        ];

        let matrix = EnhancedFeatureExtractor::extract_batch(&messages);

        assert_eq!(matrix.n_rows(), 3);
        assert_eq!(matrix.n_cols(), EnhancedErrorFeatures::DIM);
    }

    #[test]
    fn test_enhanced_to_matrix() {
        let msg = "error[E0599]: no method named `foo` found";
        let features = EnhancedErrorFeatures::from_error_message(msg);
        let matrix = features.to_matrix();

        assert_eq!(matrix.n_rows(), 1);
        assert_eq!(matrix.n_cols(), EnhancedErrorFeatures::DIM);
    }
}