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
//! AutoML-powered hyperparameter tuning using aprender.
//!
//! Leverages aprender's SearchSpace and RandomSearch for
//! automated hyperparameter optimization of the oracle predictor.

use aprender::automl::params::ParamKey;
use aprender::automl::{ParamValue, RandomSearch, SearchSpace, SearchStrategy};
use std::collections::HashMap;

use crate::depyler_training::build_combined_corpus;
use crate::ngram::NgramFixPredictor;
use crate::training::TrainingSample;
use crate::tuning::TuningResult;

/// Oracle-specific parameter keys for AutoML search.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OracleParam {
    /// Minimum similarity threshold (0.0-1.0)
    MinSimilarity,
    /// N-gram minimum range
    NgramMin,
    /// N-gram maximum range
    NgramMax,
    /// Error code weighting factor
    ErrorCodeWeight,
}

impl ParamKey for OracleParam {
    fn name(&self) -> &'static str {
        match self {
            Self::MinSimilarity => "min_similarity",
            Self::NgramMin => "ngram_min",
            Self::NgramMax => "ngram_max",
            Self::ErrorCodeWeight => "error_code_weight",
        }
    }
}

/// Build a search space for oracle hyperparameters.
#[must_use]
pub fn build_oracle_search_space() -> SearchSpace<OracleParam> {
    SearchSpace::new()
        .add_continuous(OracleParam::MinSimilarity, 0.01, 0.3)
        .add(OracleParam::NgramMin, 1..4)
        .add(OracleParam::NgramMax, 2..6)
        .add_continuous(OracleParam::ErrorCodeWeight, 1.0, 5.0)
}

/// Configuration extracted from AutoML trial parameters.
#[derive(Clone, Debug)]
pub struct AutoMLConfig {
    pub min_similarity: f32,
    pub ngram_range: (usize, usize),
    pub error_code_weight: f32,
}

impl AutoMLConfig {
    /// Extract config from AutoML parameter values.
    pub fn from_params(params: &HashMap<OracleParam, ParamValue>) -> Self {
        let min_similarity = params
            .get(&OracleParam::MinSimilarity)
            .and_then(ParamValue::as_f64)
            .unwrap_or(0.1) as f32;

        let ngram_min = params
            .get(&OracleParam::NgramMin)
            .and_then(ParamValue::as_i64)
            .unwrap_or(1) as usize;

        let ngram_max = params
            .get(&OracleParam::NgramMax)
            .and_then(ParamValue::as_i64)
            .unwrap_or(3) as usize;

        let error_code_weight = params
            .get(&OracleParam::ErrorCodeWeight)
            .and_then(ParamValue::as_f64)
            .unwrap_or(2.0) as f32;

        Self {
            min_similarity,
            ngram_range: (ngram_min, ngram_max.max(ngram_min)),
            error_code_weight,
        }
    }
}

/// Evaluate a configuration using leave-one-out cross-validation.
fn evaluate_config(config: &AutoMLConfig, samples: &[TrainingSample]) -> f64 {
    let n = samples.len();
    if n == 0 {
        return 0.0;
    }

    let mut correct = 0;

    for i in 0..n {
        let mut predictor = NgramFixPredictor::new()
            .with_min_similarity(config.min_similarity)
            .with_ngram_range(config.ngram_range.0, config.ngram_range.1);

        // Train on all except i
        for (j, sample) in samples.iter().enumerate() {
            if i != j {
                let fix = sample.fix.as_deref().unwrap_or("Check error");
                // Weight error codes by repeating them
                let weighted_msg = weight_error_codes(&sample.message, config.error_code_weight);
                predictor.learn_pattern(&weighted_msg, fix, sample.category);
            }
        }

        if predictor.fit().is_ok() {
            let test_sample = &samples[i];
            let weighted_test = weight_error_codes(&test_sample.message, config.error_code_weight);
            let suggestions = predictor.predict_fixes(&weighted_test, 1);

            if let Some(top) = suggestions.first() {
                if top.category == test_sample.category {
                    correct += 1;
                }
            }
        }
    }

    correct as f64 / n as f64
}

/// Weight error codes by repeating them in the message.
fn weight_error_codes(message: &str, weight: f32) -> String {
    if let Some(code_start) = message.find("error[E") {
        if let Some(code_end) = message[code_start..].find(']') {
            let code = &message[code_start..code_start + code_end + 1];
            let repeat_count = weight.round() as usize;
            let repeated = std::iter::repeat_n(code, repeat_count)
                .collect::<Vec<_>>()
                .join(" ");
            return format!("{} {}", repeated, message);
        }
    }
    message.to_string()
}

/// Result from AutoML optimization.
#[derive(Clone, Debug)]
pub struct AutoMLResult {
    /// Best configuration found
    pub config: AutoMLConfig,
    /// Best accuracy achieved
    pub accuracy: f64,
    /// Number of trials run
    pub trials: usize,
    /// All trial results
    pub history: Vec<(AutoMLConfig, f64)>,
}

/// Run AutoML hyperparameter optimization.
///
/// Uses aprender's RandomSearch to explore the hyperparameter space
/// and find optimal settings for the oracle predictor.
///
/// # Arguments
/// * `n_trials` - Number of random configurations to try
///
/// # Returns
/// Best configuration found and optimization history.
#[must_use]
pub fn automl_optimize(n_trials: usize) -> AutoMLResult {
    let corpus = build_combined_corpus();
    let samples: Vec<_> = corpus.samples().to_vec();

    let search_space = build_oracle_search_space();
    let mut search = RandomSearch::new(n_trials);

    let mut best_config = AutoMLConfig {
        min_similarity: 0.1,
        ngram_range: (1, 3),
        error_code_weight: 2.0,
    };
    let mut best_accuracy = 0.0;
    let mut history = Vec::new();

    // Run random search using aprender's suggest API
    let trials = search.suggest(&search_space, n_trials);
    for trial in trials {
        let config = AutoMLConfig::from_params(&trial.values);
        let accuracy = evaluate_config(&config, &samples);

        history.push((config.clone(), accuracy));

        if accuracy > best_accuracy {
            best_accuracy = accuracy;
            best_config = config;
        }
    }

    AutoMLResult {
        config: best_config,
        accuracy: best_accuracy,
        trials: n_trials,
        history,
    }
}

/// Quick AutoML optimization with fewer trials.
#[must_use]
pub fn automl_quick() -> AutoMLResult {
    automl_optimize(20)
}

/// Full AutoML optimization with comprehensive search.
#[must_use]
pub fn automl_full() -> AutoMLResult {
    automl_optimize(100)
}

/// Extended AutoML optimization with thorough search.
#[must_use]
pub fn automl_extended() -> AutoMLResult {
    automl_optimize(300)
}

/// Convert AutoML result to TuningResult for compatibility.
impl From<AutoMLResult> for TuningResult {
    fn from(result: AutoMLResult) -> Self {
        TuningResult {
            config: crate::tuning::TuningConfig {
                min_similarity: result.config.min_similarity,
                ngram_range: result.config.ngram_range,
                error_code_weight: result.config.error_code_weight,
            },
            accuracy: result.accuracy as f32,
            correct: (result.accuracy * 27.0) as usize, // Approximate from 27 samples
            total: 27,
        }
    }
}

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

    #[test]
    fn test_oracle_param_names() {
        assert_eq!(OracleParam::MinSimilarity.name(), "min_similarity");
        assert_eq!(OracleParam::NgramMin.name(), "ngram_min");
        assert_eq!(OracleParam::NgramMax.name(), "ngram_max");
        assert_eq!(OracleParam::ErrorCodeWeight.name(), "error_code_weight");
    }

    #[test]
    fn test_oracle_param_eq() {
        assert_eq!(OracleParam::MinSimilarity, OracleParam::MinSimilarity);
        assert_ne!(OracleParam::MinSimilarity, OracleParam::NgramMin);
    }

    #[test]
    fn test_oracle_param_debug() {
        let param = OracleParam::MinSimilarity;
        let debug = format!("{:?}", param);
        assert!(debug.contains("MinSimilarity"));
    }

    #[test]
    fn test_oracle_param_clone() {
        let param = OracleParam::NgramMax;
        let cloned = param;
        assert_eq!(param, cloned);
    }

    #[test]
    fn test_oracle_param_hash() {
        use std::collections::HashSet;
        let mut set = HashSet::new();
        set.insert(OracleParam::MinSimilarity);
        set.insert(OracleParam::NgramMin);
        assert!(set.contains(&OracleParam::MinSimilarity));
        assert!(set.contains(&OracleParam::NgramMin));
        assert!(!set.contains(&OracleParam::NgramMax));
    }

    #[test]
    fn test_build_search_space() {
        let space = build_oracle_search_space();
        assert_eq!(space.len(), 4);
    }

    #[test]
    fn test_automl_config_from_params() {
        let mut params = HashMap::new();
        params.insert(OracleParam::MinSimilarity, ParamValue::Float(0.15));
        params.insert(OracleParam::NgramMin, ParamValue::Int(2));
        params.insert(OracleParam::NgramMax, ParamValue::Int(4));
        params.insert(OracleParam::ErrorCodeWeight, ParamValue::Float(3.0));

        let config = AutoMLConfig::from_params(&params);
        assert!((config.min_similarity - 0.15).abs() < 0.01);
        assert_eq!(config.ngram_range, (2, 4));
        assert!((config.error_code_weight - 3.0).abs() < 0.01);
    }

    #[test]
    fn test_automl_config_from_empty_params() {
        let params = HashMap::new();
        let config = AutoMLConfig::from_params(&params);
        assert!((config.min_similarity - 0.1).abs() < 0.01);
        assert_eq!(config.ngram_range, (1, 3));
        assert!((config.error_code_weight - 2.0).abs() < 0.01);
    }

    #[test]
    fn test_automl_config_ngram_max_min_check() {
        let mut params = HashMap::new();
        params.insert(OracleParam::NgramMin, ParamValue::Int(5));
        params.insert(OracleParam::NgramMax, ParamValue::Int(3));

        let config = AutoMLConfig::from_params(&params);
        // ngram_max should be max(ngram_max, ngram_min)
        assert_eq!(config.ngram_range, (5, 5));
    }

    #[test]
    fn test_automl_config_clone() {
        let config = AutoMLConfig {
            min_similarity: 0.2,
            ngram_range: (1, 4),
            error_code_weight: 2.5,
        };
        let cloned = config.clone();
        assert!((config.min_similarity - cloned.min_similarity).abs() < f32::EPSILON);
        assert_eq!(config.ngram_range, cloned.ngram_range);
    }

    #[test]
    fn test_automl_config_debug() {
        let config = AutoMLConfig {
            min_similarity: 0.1,
            ngram_range: (1, 3),
            error_code_weight: 2.0,
        };
        let debug = format!("{:?}", config);
        assert!(debug.contains("AutoMLConfig"));
    }

    #[test]
    fn test_weight_error_codes_with_code() {
        let msg = "error[E0308]: mismatched types";
        let weighted = weight_error_codes(msg, 2.0);
        assert!(weighted.contains("error[E0308]"));
        assert!(weighted.contains("mismatched types"));
    }

    #[test]
    fn test_weight_error_codes_without_code() {
        let msg = "some generic error";
        let weighted = weight_error_codes(msg, 2.0);
        assert_eq!(weighted, msg);
    }

    #[test]
    fn test_weight_error_codes_higher_weight() {
        let msg = "error[E0599]: no method named `foo` found";
        let weighted = weight_error_codes(msg, 3.0);
        // Should contain the error code multiple times
        let count = weighted.matches("error[E0599]").count();
        assert!(count >= 3);
    }

    #[test]
    fn test_automl_result_debug() {
        let result = AutoMLResult {
            config: AutoMLConfig {
                min_similarity: 0.1,
                ngram_range: (1, 3),
                error_code_weight: 2.0,
            },
            accuracy: 0.85,
            trials: 10,
            history: vec![],
        };
        let debug = format!("{:?}", result);
        assert!(debug.contains("AutoMLResult"));
    }

    #[test]
    fn test_automl_result_clone() {
        let result = AutoMLResult {
            config: AutoMLConfig {
                min_similarity: 0.15,
                ngram_range: (2, 4),
                error_code_weight: 2.5,
            },
            accuracy: 0.9,
            trials: 20,
            history: vec![],
        };
        let cloned = result.clone();
        assert!((result.accuracy - cloned.accuracy).abs() < f64::EPSILON);
        assert_eq!(result.trials, cloned.trials);
    }

    #[test]
    fn test_automl_result_to_tuning_result() {
        let result = AutoMLResult {
            config: AutoMLConfig {
                min_similarity: 0.1,
                ngram_range: (1, 3),
                error_code_weight: 2.0,
            },
            accuracy: 0.8,
            trials: 50,
            history: vec![],
        };
        let tuning: TuningResult = result.into();
        assert!((tuning.accuracy - 0.8).abs() < 0.01);
        assert_eq!(tuning.total, 27);
    }

    #[test]
    #[ignore] // SLOW: AutoML optimization takes >100s
    fn test_automl_quick() {
        // Use fewer trials for coverage runs (DEPYLER_FAST_TESTS=1)
        let fast_mode = std::env::var("DEPYLER_FAST_TESTS").is_ok();
        let trials = if fast_mode { 3 } else { 20 };
        let result = automl_optimize(trials);
        assert!(result.accuracy > 0.0);
        assert_eq!(result.trials, trials);
        assert!(!result.history.is_empty());
        println!(
            "AutoML Quick: {:.2}% accuracy with sim={:.3}, ngram={:?}, weight={:.1}",
            result.accuracy * 100.0,
            result.config.min_similarity,
            result.config.ngram_range,
            result.config.error_code_weight
        );
    }

    #[test]
    #[ignore] // Slow - run manually
    fn test_automl_full() {
        let result = automl_full();
        assert!(result.accuracy > 0.0);
        assert_eq!(result.trials, 100);
        println!(
            "AutoML Full: {:.2}% accuracy with sim={:.3}, ngram={:?}, weight={:.1}",
            result.accuracy * 100.0,
            result.config.min_similarity,
            result.config.ngram_range,
            result.config.error_code_weight
        );
    }

    #[test]
    #[ignore] // Very slow - run manually for best results
    fn test_automl_extended() {
        let result = automl_extended();
        assert!(result.accuracy > 0.0);
        assert_eq!(result.trials, 300);
        println!(
            "AutoML Extended (300 trials): {:.2}% accuracy with sim={:.3}, ngram={:?}, weight={:.1}",
            result.accuracy * 100.0,
            result.config.min_similarity,
            result.config.ngram_range,
            result.config.error_code_weight
        );
        // Print top 5 configurations
        let mut sorted: Vec<_> = result.history.clone();
        sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        println!("\nTop 5 configurations:");
        for (i, (cfg, acc)) in sorted.iter().take(5).enumerate() {
            println!(
                "  {}. {:.2}% - sim={:.3}, ngram=({},{}), weight={:.1}",
                i + 1,
                acc * 100.0,
                cfg.min_similarity,
                cfg.ngram_range.0,
                cfg.ngram_range.1,
                cfg.error_code_weight
            );
        }
    }
}