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
//! GitHub history corpus integration via organizational-intelligence-plugin.
//!
//! Recipe for learning from Git history:
//! 1. Use OIP to extract training data: `oip extract-training-data --repo <path>`
//! 2. Import JSON into depyler-oracle training pipeline
//! 3. Map OIP's DefectCategory to depyler's ErrorCategory
//!
//! OIP DefectCategory → depyler ErrorCategory mapping:
//! - OwnershipBorrow, TraitBounds → BorrowChecker, TraitBound
//! - TypeErrors, TypeAnnotationGaps → TypeMismatch
//! - StdlibMapping, ASTTransform, ConfigurationErrors → MissingImport
//! - MemorySafety → LifetimeError
//! - IteratorChain, ComprehensionBugs → Other

use crate::classifier::ErrorCategory;
use crate::moe_oracle::ExpertDomain;
use crate::training::{TrainingDataset, TrainingSample};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// OIP DefectCategory (18 categories from organizational-intelligence-plugin)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OipDefectCategory {
    // General defect categories (10)
    MemorySafety,
    ConcurrencyBugs,
    LogicErrors,
    ApiMisuse,
    ResourceLeaks,
    TypeErrors,
    ConfigurationErrors,
    SecurityVulnerabilities,
    PerformanceIssues,
    IntegrationFailures,
    // Transpiler-specific categories (8)
    OperatorPrecedence,
    TypeAnnotationGaps,
    StdlibMapping,
    ASTTransform,
    ComprehensionBugs,
    IteratorChain,
    OwnershipBorrow,
    TraitBounds,
}

impl OipDefectCategory {
    /// Map OIP category to depyler ErrorCategory
    #[must_use]
    pub fn to_error_category(self) -> ErrorCategory {
        match self {
            // Ownership/borrowing issues
            Self::OwnershipBorrow | Self::MemorySafety => ErrorCategory::BorrowChecker,

            // Trait bound issues
            Self::TraitBounds => ErrorCategory::TraitBound,

            // Type errors
            Self::TypeErrors | Self::TypeAnnotationGaps => ErrorCategory::TypeMismatch,

            // Missing imports / stdlib mapping
            Self::StdlibMapping | Self::ConfigurationErrors | Self::ASTTransform => {
                ErrorCategory::MissingImport
            }

            // Lifetime errors (from memory safety patterns)
            Self::ResourceLeaks => ErrorCategory::LifetimeError,

            // Other categories
            Self::ConcurrencyBugs
            | Self::LogicErrors
            | Self::ApiMisuse
            | Self::SecurityVulnerabilities
            | Self::PerformanceIssues
            | Self::IntegrationFailures
            | Self::OperatorPrecedence
            | Self::ComprehensionBugs
            | Self::IteratorChain => ErrorCategory::Other,
        }
    }

    /// Map OIP category to MoE ExpertDomain
    #[must_use]
    pub fn to_expert_domain(self) -> ExpertDomain {
        match self {
            // Type system expert
            Self::TypeErrors | Self::TypeAnnotationGaps | Self::TraitBounds => {
                ExpertDomain::TypeSystem
            }

            // Scope resolution expert
            Self::StdlibMapping
            | Self::ConfigurationErrors
            | Self::IntegrationFailures
            | Self::ASTTransform => ExpertDomain::ScopeResolution,

            // Method/field expert
            Self::ApiMisuse | Self::IteratorChain | Self::ComprehensionBugs => {
                ExpertDomain::MethodField
            }

            // Syntax/borrow expert (default for Rust-specific)
            Self::OwnershipBorrow
            | Self::MemorySafety
            | Self::ResourceLeaks
            | Self::ConcurrencyBugs
            | Self::LogicErrors
            | Self::SecurityVulnerabilities
            | Self::PerformanceIssues
            | Self::OperatorPrecedence => ExpertDomain::SyntaxBorrowing,
        }
    }
}

/// OIP TrainingExample format (from organizational-intelligence-plugin)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OipTrainingExample {
    /// Commit message text
    pub message: String,
    /// Defect category label
    pub label: OipDefectCategory,
    /// Classifier confidence (0.0-1.0)
    pub confidence: f32,
    /// Original commit hash
    pub commit_hash: String,
    /// Author name/email
    pub author: String,
    /// Unix timestamp
    pub timestamp: i64,
    /// Lines added in commit
    pub lines_added: usize,
    /// Lines removed in commit
    pub lines_removed: usize,
    /// Number of files changed
    pub files_changed: usize,
}

/// OIP TrainingDataset format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OipTrainingDataset {
    /// Training examples
    pub train: Vec<OipTrainingExample>,
    /// Validation examples
    pub validation: Vec<OipTrainingExample>,
    /// Test examples
    pub test: Vec<OipTrainingExample>,
}

/// Load OIP training data from JSON file
///
/// # Errors
/// Returns error if file cannot be read or parsed
pub fn load_oip_training_data(path: &Path) -> Result<OipTrainingDataset, std::io::Error> {
    let content = fs::read_to_string(path)?;
    serde_json::from_str(&content)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
}

/// Convert OIP training data to depyler TrainingDataset
#[must_use]
pub fn convert_oip_to_depyler(oip_data: &OipTrainingDataset) -> TrainingDataset {
    let mut dataset = TrainingDataset::new();

    // Process all examples (train + validation + test)
    let all_examples: Vec<_> = oip_data
        .train
        .iter()
        .chain(oip_data.validation.iter())
        .chain(oip_data.test.iter())
        .collect();

    for example in all_examples {
        let category = example.label.to_error_category();

        // Extract error pattern from commit message
        // OIP stores commit messages, we need to extract the error pattern
        let error_pattern = extract_error_pattern(&example.message);

        // Create fix suggestion from commit message
        let fix = extract_fix_from_commit(&example.message);

        dataset.add(TrainingSample::with_fix(&error_pattern, category, &fix));
    }

    dataset
}

/// Extract error pattern from commit message
fn extract_error_pattern(message: &str) -> String {
    // Look for error code patterns like error[E0308]
    if let Some(start) = message.find("error[E") {
        if let Some(end) = message[start..].find(']') {
            let error_code = &message[start..start + end + 1];
            // Find the error description after the code
            let rest = &message[start + end + 1..];
            if let Some(desc_end) = rest.find('\n') {
                return format!("{}: {}", error_code, rest[..desc_end].trim());
            }
            return error_code.to_string();
        }
    }

    // Extract from conventional commit format: "fix: <description>"
    if let Some(fix_start) = message.to_lowercase().find("fix:") {
        let rest = &message[fix_start + 4..];
        if let Some(end) = rest.find('\n') {
            return rest[..end].trim().to_string();
        }
        return rest.trim().to_string();
    }

    // Fall back to first line
    message.lines().next().unwrap_or(message).to_string()
}

/// Extract fix suggestion from commit message
fn extract_fix_from_commit(message: &str) -> String {
    // Look for "Fix:" or "Solution:" patterns
    let lower = message.to_lowercase();

    for pattern in &["solution:", "fixed by:", "fix:", "resolved:"] {
        if let Some(idx) = lower.find(pattern) {
            let rest = &message[idx + pattern.len()..];
            if let Some(end) = rest.find('\n') {
                return rest[..end].trim().to_string();
            }
            return rest.trim().to_string();
        }
    }

    // Extract from commit title
    message
        .lines()
        .next()
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|| "See commit for fix details".to_string())
}

/// Build corpus from OIP training data file
///
/// # Errors
/// Returns error if file cannot be loaded
pub fn build_github_corpus(oip_json_path: &Path) -> Result<TrainingDataset, std::io::Error> {
    let oip_data = load_oip_training_data(oip_json_path)?;
    Ok(convert_oip_to_depyler(&oip_data))
}

/// Get MoE training samples from OIP data
///
/// Returns (error_code, context, domain) tuples for MoE training
#[must_use]
pub fn get_moe_samples_from_oip(
    oip_data: &OipTrainingDataset,
) -> Vec<(String, String, ExpertDomain)> {
    let mut samples = Vec::new();

    let all_examples: Vec<_> = oip_data
        .train
        .iter()
        .chain(oip_data.validation.iter())
        .chain(oip_data.test.iter())
        .collect();

    for example in all_examples {
        let domain = example.label.to_expert_domain();
        let error_code = infer_error_code_from_category(example.label);
        let context = example.message.clone();

        samples.push((error_code, context, domain));
    }

    samples
}

/// Infer Rust error code from OIP category
fn infer_error_code_from_category(category: OipDefectCategory) -> String {
    match category {
        OipDefectCategory::TypeErrors | OipDefectCategory::TypeAnnotationGaps => {
            "E0308".to_string()
        }
        OipDefectCategory::TraitBounds => "E0277".to_string(),
        OipDefectCategory::OwnershipBorrow | OipDefectCategory::MemorySafety => "E0382".to_string(),
        OipDefectCategory::StdlibMapping | OipDefectCategory::ASTTransform => "E0433".to_string(),
        OipDefectCategory::ApiMisuse | OipDefectCategory::IteratorChain => "E0599".to_string(),
        OipDefectCategory::ConfigurationErrors | OipDefectCategory::IntegrationFailures => {
            "E0425".to_string()
        }
        OipDefectCategory::ResourceLeaks => "E0106".to_string(),
        OipDefectCategory::ComprehensionBugs => "E0609".to_string(),
        _ => "E0000".to_string(), // Generic error
    }
}

/// Statistics about the GitHub corpus
#[derive(Debug, Default)]
pub struct CorpusStats {
    pub total_examples: usize,
    pub by_category: HashMap<String, usize>,
    pub by_expert: HashMap<ExpertDomain, usize>,
    pub avg_confidence: f32,
}

/// Analyze OIP corpus statistics
#[must_use]
pub fn analyze_corpus(oip_data: &OipTrainingDataset) -> CorpusStats {
    let mut stats = CorpusStats::default();

    let all_examples: Vec<_> = oip_data
        .train
        .iter()
        .chain(oip_data.validation.iter())
        .chain(oip_data.test.iter())
        .collect();

    stats.total_examples = all_examples.len();

    let mut total_confidence = 0.0f32;

    for example in &all_examples {
        // Count by OIP category
        let cat_name = format!("{:?}", example.label);
        *stats.by_category.entry(cat_name).or_default() += 1;

        // Count by expert domain
        let domain = example.label.to_expert_domain();
        *stats.by_expert.entry(domain).or_default() += 1;

        total_confidence += example.confidence;
    }

    if !all_examples.is_empty() {
        stats.avg_confidence = total_confidence / all_examples.len() as f32;
    }

    stats
}

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

    #[test]
    fn test_oip_to_error_category_mapping() {
        assert_eq!(
            OipDefectCategory::OwnershipBorrow.to_error_category(),
            ErrorCategory::BorrowChecker
        );
        assert_eq!(
            OipDefectCategory::TypeErrors.to_error_category(),
            ErrorCategory::TypeMismatch
        );
        assert_eq!(
            OipDefectCategory::TraitBounds.to_error_category(),
            ErrorCategory::TraitBound
        );
        assert_eq!(
            OipDefectCategory::StdlibMapping.to_error_category(),
            ErrorCategory::MissingImport
        );
    }

    #[test]
    fn test_oip_to_expert_domain_mapping() {
        assert_eq!(
            OipDefectCategory::TypeErrors.to_expert_domain(),
            ExpertDomain::TypeSystem
        );
        assert_eq!(
            OipDefectCategory::StdlibMapping.to_expert_domain(),
            ExpertDomain::ScopeResolution
        );
        assert_eq!(
            OipDefectCategory::OwnershipBorrow.to_expert_domain(),
            ExpertDomain::SyntaxBorrowing
        );
        assert_eq!(
            OipDefectCategory::ApiMisuse.to_expert_domain(),
            ExpertDomain::MethodField
        );
    }

    #[test]
    fn test_extract_error_pattern() {
        let msg = "fix: error[E0308]: mismatched types\n\ndetails here";
        let pattern = extract_error_pattern(msg);
        assert!(pattern.contains("E0308"));
    }

    #[test]
    fn test_extract_error_pattern_conventional() {
        let msg = "fix: resolve borrow checker issue with lifetime";
        let pattern = extract_error_pattern(msg);
        assert_eq!(pattern, "resolve borrow checker issue with lifetime");
    }

    #[test]
    fn test_extract_fix_from_commit() {
        let msg = "fix: type mismatch\n\nSolution: Use .into() for conversion";
        let fix = extract_fix_from_commit(msg);
        assert_eq!(fix, "Use .into() for conversion");
    }

    #[test]
    fn test_infer_error_code() {
        assert_eq!(
            infer_error_code_from_category(OipDefectCategory::TypeErrors),
            "E0308"
        );
        assert_eq!(
            infer_error_code_from_category(OipDefectCategory::TraitBounds),
            "E0277"
        );
        assert_eq!(
            infer_error_code_from_category(OipDefectCategory::OwnershipBorrow),
            "E0382"
        );
    }

    #[test]
    fn test_convert_empty_dataset() {
        let oip = OipTrainingDataset {
            train: vec![],
            validation: vec![],
            test: vec![],
        };
        let dataset = convert_oip_to_depyler(&oip);
        assert!(dataset.samples().is_empty());
    }

    #[test]
    fn test_analyze_corpus_empty() {
        let oip = OipTrainingDataset {
            train: vec![],
            validation: vec![],
            test: vec![],
        };
        let stats = analyze_corpus(&oip);
        assert_eq!(stats.total_examples, 0);
    }

    #[test]
    fn test_load_real_oip_data_if_exists() {
        // Try to load real OIP training data if available
        let oip_path = std::path::Path::new(
            "/home/noah/src/organizational-intelligence-plugin/training-data.json",
        );

        if oip_path.exists() {
            let oip_data = load_oip_training_data(oip_path).expect("Should load OIP data");
            let stats = analyze_corpus(&oip_data);

            println!("OIP Corpus Statistics:");
            println!("  Total examples: {}", stats.total_examples);
            println!("  Avg confidence: {:.2}", stats.avg_confidence);
            println!("  By category:");
            for (cat, count) in &stats.by_category {
                println!("    {}: {}", cat, count);
            }
            println!("  By expert domain:");
            for (domain, count) in &stats.by_expert {
                println!("    {:?}: {}", domain, count);
            }

            // Convert to depyler format
            let depyler_dataset = convert_oip_to_depyler(&oip_data);
            println!(
                "  Converted to {} depyler samples",
                depyler_dataset.samples().len()
            );

            assert!(stats.total_examples > 0, "Should have training examples");
        } else {
            println!("OIP training data not found at {:?}, skipping", oip_path);
        }
    }

    #[test]
    fn test_convert_with_sample_data() {
        let oip = OipTrainingDataset {
            train: vec![OipTrainingExample {
                message: "fix: error[E0308]: mismatched types\n\nUse .into()".to_string(),
                label: OipDefectCategory::TypeErrors,
                confidence: 0.85,
                commit_hash: "abc123".to_string(),
                author: "test@example.com".to_string(),
                timestamp: 1234567890,
                lines_added: 10,
                lines_removed: 5,
                files_changed: 2,
            }],
            validation: vec![],
            test: vec![],
        };

        let dataset = convert_oip_to_depyler(&oip);
        assert_eq!(dataset.samples().len(), 1);

        let moe_samples = get_moe_samples_from_oip(&oip);
        assert_eq!(moe_samples.len(), 1);
        assert_eq!(moe_samples[0].0, "E0308"); // Error code
        assert_eq!(moe_samples[0].2, ExpertDomain::TypeSystem); // Expert domain
    }
}