bashrs_oracle/lib.rs
1// Allow multiple crate versions from transitive dependencies (aprender -> wgpu -> foldhash)
2#![allow(clippy::multiple_crate_versions)]
3
4//! ML-powered error classification oracle for bashrs.
5//!
6//! Uses aprender Random Forest classifier (GPU-accelerated via trueno/wgpu) to:
7//! - Classify shell errors into actionable categories (24 categories)
8//! - Suggest fixes based on error patterns
9//! - Detect error drift requiring model retraining
10//!
11//! ## GPU Acceleration
12//!
13//! Enable GPU feature for RTX 4090 acceleration via wgpu/trueno:
14//! ```toml
15//! bashrs-oracle = { version = "*", features = ["gpu"] }
16//! ```
17//!
18//! ## Performance Targets (from depyler-oracle)
19//! - Accuracy: >90% (depyler achieved 97.73%)
20//! - Training time: <1s
21//! - Predictions/sec: >1000
22//! - Model size: <1MB (with zstd compression)
23
24use std::collections::HashMap;
25use std::path::{Path, PathBuf};
26
27use aprender::format::{self, Compression, ModelType, SaveOptions};
28use aprender::metrics::drift::{DriftConfig, DriftDetector, DriftStatus};
29use aprender::primitives::Matrix;
30use aprender::tree::RandomForestClassifier;
31use serde::{Deserialize, Serialize};
32
33pub mod categories;
34pub mod classifier;
35pub mod corpus;
36pub mod features;
37
38pub use categories::ErrorCategory;
39pub use classifier::ErrorClassifier;
40pub use corpus::{Corpus, TrainingExample};
41pub use features::ErrorFeatures;
42
43/// Error types for the oracle.
44#[derive(Debug, thiserror::Error)]
45pub enum OracleError {
46 /// Model loading/saving error.
47 #[error("Model error: {0}")]
48 Model(String),
49 /// Feature extraction error.
50 #[error("Feature extraction error: {0}")]
51 Feature(String),
52 /// Training error.
53 #[error("Training error: {0}")]
54 Training(String),
55 /// Classification error.
56 #[error("Classification error: {0}")]
57 Classification(String),
58 /// IO error.
59 #[error("IO error: {0}")]
60 Io(#[from] std::io::Error),
61}
62
63/// Result type for oracle operations.
64pub type Result<T> = std::result::Result<T, OracleError>;
65
66/// Classification result with confidence and suggested fix.
67#[derive(Clone, Debug, Serialize, Deserialize)]
68pub struct ClassificationResult {
69 /// Predicted error category.
70 pub category: ErrorCategory,
71 /// Confidence score (0.0 - 1.0).
72 pub confidence: f32,
73 /// Suggested fix template.
74 pub suggested_fix: Option<String>,
75 /// Related fix patterns.
76 pub related_patterns: Vec<String>,
77}
78
79/// Configuration for the Random Forest classifier.
80#[derive(Clone, Debug)]
81pub struct OracleConfig {
82 /// Number of trees in the forest (default: 100).
83 /// IMPORTANT: 100 is sufficient. 10,000 causes 15+ min training!
84 pub n_estimators: usize,
85 /// Maximum tree depth (default: 10).
86 pub max_depth: usize,
87 /// Random seed for reproducibility.
88 pub random_state: Option<u64>,
89}
90
91impl Default for OracleConfig {
92 fn default() -> Self {
93 Self {
94 n_estimators: 100,
95 max_depth: 10,
96 random_state: Some(42),
97 }
98 }
99}
100
101/// Default model filename.
102const DEFAULT_MODEL_NAME: &str = "bashrs_oracle.apr";
103
104/// ML-powered shell error classification oracle.
105pub struct Oracle {
106 /// Random Forest classifier (GPU-accelerated via aprender).
107 classifier: RandomForestClassifier,
108 /// Configuration used to create the classifier.
109 #[allow(dead_code)]
110 config: OracleConfig,
111 /// Category list for index mapping (kept for model introspection).
112 #[allow(dead_code)]
113 categories: Vec<ErrorCategory>,
114 /// Fix templates per category.
115 fix_templates: HashMap<ErrorCategory, Vec<String>>,
116 /// Drift detector for retraining triggers.
117 drift_detector: DriftDetector,
118 /// Historical performance scores.
119 performance_history: Vec<f32>,
120 /// Whether model has been trained.
121 is_trained: bool,
122}
123
124impl Default for Oracle {
125 fn default() -> Self {
126 Self::new()
127 }
128}
129
130include!("lib_oracle.rs");