bashrs-oracle 7.4.5

ML-powered error classification oracle for bashrs using aprender (GPU-accelerated)
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
//! Feature extraction for ML model.
//!
//! 64 features carefully chosen to capture error patterns:
//! - Numeric features normalized to [0, 1]
//! - Categorical features one-hot encoded
//! - Text features converted to bag-of-words indicators
#![allow(clippy::indexing_slicing)] // Test assertions use direct indexing for clarity

/// Feature vector for ML model (64 features).
#[derive(Debug, Clone)]
pub struct ErrorFeatures {
    /// Feature vector (always 64 elements).
    pub features: Vec<f32>,
}

impl ErrorFeatures {
    /// Feature vector size.
    pub const SIZE: usize = 64;

    /// Extract features from error message and context.
    #[must_use]
    pub fn extract(exit_code: i32, stderr: &str, command: Option<&str>) -> Self {
        let mut features = Vec::with_capacity(Self::SIZE);

        // === Exit code features (0-5) ===
        features.push(exit_code as f32 / 255.0); // Normalized exit code
        features.push(if exit_code == 1 { 1.0 } else { 0.0 }); // General error
        features.push(if exit_code == 2 { 1.0 } else { 0.0 }); // Misuse
        features.push(if exit_code == 126 { 1.0 } else { 0.0 }); // Permission denied
        features.push(if exit_code == 127 { 1.0 } else { 0.0 }); // Command not found
        features.push(if exit_code == 128 { 1.0 } else { 0.0 }); // Signal base

        // === Signal features (6-9) ===
        features.push(if exit_code == 130 { 1.0 } else { 0.0 }); // SIGINT (Ctrl+C)
        features.push(if exit_code == 137 { 1.0 } else { 0.0 }); // SIGKILL
        features.push(if exit_code == 141 { 1.0 } else { 0.0 }); // SIGPIPE
        features.push(if exit_code == 143 { 1.0 } else { 0.0 }); // SIGTERM

        // === Error message length features (10-11) ===
        let stderr_len = stderr.len();
        features.push((stderr_len as f32 / 1000.0).min(1.0)); // Normalized length
        features.push((stderr.lines().count() as f32 / 10.0).min(1.0)); // Line count

        // === Keyword indicators - bag of words (12-31) ===
        let stderr_lower = stderr.to_lowercase();

        // File/path related
        features.push(if stderr_lower.contains("not found") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("no such file") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("permission denied") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("is a directory") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("not a directory") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("too many open") {
            1.0
        } else {
            0.0
        });

        // Syntax related
        features.push(if stderr_lower.contains("syntax error") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("unexpected") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("unmatched") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("unterminated") {
            1.0
        } else {
            0.0
        });

        // Variable related
        features.push(if stderr_lower.contains("unbound variable") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("bad substitution") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("readonly") {
            1.0
        } else {
            0.0
        });

        // Command related
        features.push(if stderr_lower.contains("command not found") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("invalid option") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("missing") {
            1.0
        } else {
            0.0
        });

        // Process/pipe related
        features.push(if stderr_lower.contains("broken pipe") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("killed") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("timeout") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("timed out") {
            1.0
        } else {
            0.0
        });

        // === Quote/bracket analysis (32-37) ===
        let single_quotes = stderr.matches('\'').count();
        let double_quotes = stderr.matches('"').count();
        let parens = stderr.matches('(').count() + stderr.matches(')').count();
        let brackets = stderr.matches('[').count() + stderr.matches(']').count();
        let braces = stderr.matches('{').count() + stderr.matches('}').count();

        features.push((single_quotes as f32 / 10.0).min(1.0));
        features.push((double_quotes as f32 / 10.0).min(1.0));
        features.push(if !single_quotes.is_multiple_of(2) {
            1.0
        } else {
            0.0
        }); // Odd = mismatch
        features.push(if !double_quotes.is_multiple_of(2) {
            1.0
        } else {
            0.0
        }); // Odd = mismatch
        features.push(((parens + brackets + braces) as f32 / 20.0).min(1.0));
        features.push(if !(parens + brackets + braces).is_multiple_of(2) {
            1.0
        } else {
            0.0
        }); // Odd = mismatch

        // === Line position features (38-41) ===
        let has_line_num = stderr_lower.contains("line ");
        let has_column = stderr_lower.contains("column ") || stderr_lower.contains("col ");
        features.push(if has_line_num { 1.0 } else { 0.0 });
        features.push(if has_column { 1.0 } else { 0.0 });
        features.push(if stderr_lower.contains("near") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("expected") {
            1.0
        } else {
            0.0
        });

        // === Command features (42-49) ===
        if let Some(cmd) = command {
            let cmd_len = cmd.len();
            features.push((cmd_len as f32 / 100.0).min(1.0)); // Command length
            features.push(if cmd.contains('|') { 1.0 } else { 0.0 }); // Pipeline
            features.push(if cmd.contains('>') { 1.0 } else { 0.0 }); // Output redirect
            features.push(if cmd.contains('<') { 1.0 } else { 0.0 }); // Input redirect
            features.push(if cmd.contains("2>") { 1.0 } else { 0.0 }); // Stderr redirect
            features.push(if cmd.starts_with("sudo") { 1.0 } else { 0.0 }); // Sudo
            features.push(if cmd.contains("&&") || cmd.contains("||") {
                1.0
            } else {
                0.0
            }); // Compound
            features.push(if cmd.contains('$') { 1.0 } else { 0.0 }); // Variables
        } else {
            features.extend([0.0; 8]);
        }

        // === Shell-specific keywords (50-57) ===
        features.push(if stderr_lower.contains("bash:") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("sh:") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("zsh:") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("dash:") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("ksh:") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("fish:") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("cannot") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("failed") {
            1.0
        } else {
            0.0
        });

        // === Additional error indicators (58-63) ===
        features.push(if stderr_lower.contains("error") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("warning") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("fatal") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("abort") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("segmentation") {
            1.0
        } else {
            0.0
        });
        features.push(if stderr_lower.contains("core dump") {
            1.0
        } else {
            0.0
        });

        // Ensure exactly 64 features
        debug_assert_eq!(features.len(), Self::SIZE, "Feature count mismatch");

        Self { features }
    }

    /// Convert to slice for ML model.
    #[must_use]
    pub fn as_slice(&self) -> &[f32] {
        &self.features
    }

    /// Get feature by index with name for debugging.
    #[must_use]
    pub fn feature_name(index: usize) -> &'static str {
        const NAMES: [&str; 64] = [
            "exit_code_normalized",
            "exit_code_is_1",
            "exit_code_is_2",
            "exit_code_is_126",
            "exit_code_is_127",
            "exit_code_is_128",
            "signal_sigint",
            "signal_sigkill",
            "signal_sigpipe",
            "signal_sigterm",
            "stderr_length",
            "stderr_line_count",
            "kw_not_found",
            "kw_no_such_file",
            "kw_permission_denied",
            "kw_is_directory",
            "kw_not_directory",
            "kw_too_many_open",
            "kw_syntax_error",
            "kw_unexpected",
            "kw_unmatched",
            "kw_unterminated",
            "kw_unbound_variable",
            "kw_bad_substitution",
            "kw_readonly",
            "kw_command_not_found",
            "kw_invalid_option",
            "kw_missing",
            "kw_broken_pipe",
            "kw_killed",
            "kw_timeout",
            "kw_timed_out",
            "single_quote_count",
            "double_quote_count",
            "single_quote_mismatch",
            "double_quote_mismatch",
            "bracket_count",
            "bracket_mismatch",
            "has_line_number",
            "has_column",
            "has_near",
            "has_expected",
            "cmd_length",
            "cmd_has_pipe",
            "cmd_has_output_redirect",
            "cmd_has_input_redirect",
            "cmd_has_stderr_redirect",
            "cmd_has_sudo",
            "cmd_is_compound",
            "cmd_has_variables",
            "shell_bash",
            "shell_sh",
            "shell_zsh",
            "shell_dash",
            "shell_ksh",
            "shell_fish",
            "kw_cannot",
            "kw_failed",
            "kw_error",
            "kw_warning",
            "kw_fatal",
            "kw_abort",
            "kw_segmentation",
            "kw_core_dump",
        ];
        NAMES.get(index).copied().unwrap_or("unknown")
    }
}

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

    #[test]
    fn test_feature_vector_size() {
        let features = ErrorFeatures::extract(1, "test error", None);
        assert_eq!(features.features.len(), ErrorFeatures::SIZE);
    }

    #[test]
    fn test_exit_code_127_features() {
        let features = ErrorFeatures::extract(127, "bash: foobar: command not found", None);
        assert!((features.features[4] - 1.0).abs() < f32::EPSILON); // exit_code_is_127
        assert!((features.features[25] - 1.0).abs() < f32::EPSILON); // kw_command_not_found
    }

    #[test]
    fn test_exit_code_126_features() {
        let features = ErrorFeatures::extract(126, "bash: ./script.sh: Permission denied", None);
        assert!((features.features[3] - 1.0).abs() < f32::EPSILON); // exit_code_is_126
        assert!((features.features[14] - 1.0).abs() < f32::EPSILON); // kw_permission_denied
    }

    #[test]
    fn test_syntax_error_features() {
        let features =
            ErrorFeatures::extract(1, "bash: syntax error near unexpected token 'done'", None);
        assert!((features.features[18] - 1.0).abs() < f32::EPSILON); // kw_syntax_error
        assert!((features.features[19] - 1.0).abs() < f32::EPSILON); // kw_unexpected
        assert!((features.features[40] - 1.0).abs() < f32::EPSILON); // has_near
    }

    #[test]
    fn test_quote_mismatch_detection() {
        let features = ErrorFeatures::extract(1, "unexpected EOF looking for matching '\"'", None);
        assert!((features.features[35] - 1.0).abs() < f32::EPSILON); // double_quote_mismatch (odd count)
    }

    #[test]
    fn test_command_features() {
        let features =
            ErrorFeatures::extract(1, "error", Some("cat file.txt | grep 'test' > output.txt"));
        assert!(features.features[43] > 0.0); // cmd_has_pipe
        assert!(features.features[44] > 0.0); // cmd_has_output_redirect
    }

    #[test]
    fn test_signal_features() {
        let features = ErrorFeatures::extract(141, "", None); // SIGPIPE
        assert!((features.features[8] - 1.0).abs() < f32::EPSILON); // signal_sigpipe
    }

    #[test]
    fn test_shell_detection() {
        let features_bash = ErrorFeatures::extract(1, "bash: error", None);
        assert!((features_bash.features[50] - 1.0).abs() < f32::EPSILON); // shell_bash

        let features_zsh = ErrorFeatures::extract(1, "zsh: error", None);
        assert!((features_zsh.features[52] - 1.0).abs() < f32::EPSILON); // shell_zsh
    }

    #[test]
    fn test_file_not_found_features() {
        let features =
            ErrorFeatures::extract(1, "cat: /nonexistent: No such file or directory", None);
        assert!((features.features[13] - 1.0).abs() < f32::EPSILON); // kw_no_such_file
    }

    #[test]
    fn test_unbound_variable_features() {
        let features = ErrorFeatures::extract(1, "bash: VAR: unbound variable", None);
        assert!((features.features[22] - 1.0).abs() < f32::EPSILON); // kw_unbound_variable
    }

    #[test]
    fn test_feature_names_coverage() {
        for i in 0..ErrorFeatures::SIZE {
            let name = ErrorFeatures::feature_name(i);
            assert_ne!(name, "unknown", "Feature {i} has no name");
        }
    }

    #[test]
    fn test_normalization_bounds() {
        // Very long error message
        let long_stderr = "x".repeat(10000);
        let features = ErrorFeatures::extract(255, &long_stderr, Some(&"x".repeat(1000)));

        for (i, &val) in features.features.iter().enumerate() {
            assert!(
                (0.0..=1.0).contains(&val),
                "Feature {i} ({}) out of bounds: {val}",
                ErrorFeatures::feature_name(i)
            );
        }
    }
}