bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
//! ShellSafetyBench (SSB) pipeline commands: validation, evaluation, data merging, and conversion.
use crate::models::{Error, Result};
use std::path::{Path, PathBuf};

/// Cross-validate bashrs labels against ShellCheck on corpus samples (Step 7.4e).
pub(crate) fn corpus_shellcheck_validate(samples: usize, seed: u64, json: bool) -> Result<()> {
    use crate::cli::color::*;

    eprintln!(
        "{BOLD}Cross-validating bashrs labels vs ShellCheck ({samples} samples, seed={seed})...{RESET}"
    );

    // Try pre-built splits first (fast path, ~1s vs ~120s for full corpus)
    let splits_path = Path::new("training/shellsafetybench/splits/test.jsonl");
    let entries: Vec<(String, u8)> = if splits_path.exists() {
        let content = std::fs::read_to_string(splits_path).map_err(Error::Io)?;
        content
            .lines()
            .filter(|l| !l.trim().is_empty())
            .filter_map(|l| {
                let v: serde_json::Value = serde_json::from_str(l).ok()?;
                let input = v.get("input")?.as_str()?.to_string();
                let label = v.get("label")?.as_u64()? as u8;
                Some((input, label))
            })
            .collect()
    } else {
        // Fall back to full corpus transpilation (slow)
        use crate::corpus::baselines::corpus_baseline_entries;
        corpus_baseline_entries()
    };

    let total = entries.len();

    // Deterministic sampling using seed
    let step = if samples >= total { 1 } else { total / samples };
    let sampled: Vec<_> = entries
        .iter()
        .enumerate()
        .filter(|(i, _)| i % step == (seed as usize % step))
        .take(samples)
        .map(|(_, e)| e)
        .collect();

    let mut agree = 0u32;
    let mut shellcheck_only = 0u32;
    let mut bashrs_only = 0u32;
    let mut shellcheck_errors = 0u32;
    let checked = sampled.len();

    for (input, label) in &sampled {
        let bashrs_unsafe = *label == 1;

        // Run shellcheck
        let sc_result = std::process::Command::new("shellcheck")
            .args(["-s", "sh", "-f", "json", "-"])
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::null())
            .spawn();

        let sc_has_error = match sc_result {
            Ok(mut child) => {
                if let Some(ref mut stdin) = child.stdin {
                    use std::io::Write;
                    let _ = stdin.write_all(input.as_bytes());
                }
                let output = child.wait_with_output().ok();
                match output {
                    Some(o) => {
                        let stdout = String::from_utf8_lossy(&o.stdout);
                        if let Ok(diags) = serde_json::from_str::<Vec<serde_json::Value>>(&stdout) {
                            diags
                                .iter()
                                .any(|d| d.get("level").and_then(|v| v.as_str()) == Some("error"))
                        } else {
                            false
                        }
                    }
                    None => false,
                }
            }
            Err(_) => {
                shellcheck_errors += 1;
                false
            }
        };

        if bashrs_unsafe == sc_has_error {
            agree += 1;
        } else if sc_has_error {
            shellcheck_only += 1;
        } else {
            bashrs_only += 1;
        }
    }

    let agreement_pct = if checked > 0 {
        agree as f64 / checked as f64 * 100.0
    } else {
        0.0
    };

    if json {
        let result = serde_json::json!({
            "samples": checked,
            "agreement": agree,
            "agreement_pct": agreement_pct,
            "shellcheck_only": shellcheck_only,
            "bashrs_only": bashrs_only,
            "shellcheck_errors": shellcheck_errors,
        });
        println!(
            "{}",
            serde_json::to_string_pretty(&result).unwrap_or_else(|_| "{}".to_string())
        );
    } else {
        println!("{BOLD}=== Cross-Linter Validation (bashrs vs ShellCheck) ==={RESET}\n");
        println!("  Samples checked: {checked}");
        println!("  Agreement:       {agree}/{checked} ({agreement_pct:.1}%)");
        println!("  ShellCheck-only: {shellcheck_only} (SC flags, bashrs doesn't)");
        println!("  bashrs-only:     {bashrs_only} (bashrs flags, SC doesn't)");
        if shellcheck_errors > 0 {
            println!("  SC errors:       {shellcheck_errors}");
        }
        println!();
        println!("  Note: ShellCheck checks general shell quality;");
        println!("        bashrs only flags SEC/DET/IDEM security rules.");
        println!("        Higher ShellCheck-only count is expected.");
    }

    Ok(())
}

/// Run eval harness on benchmark predictions (Step 7.7).
pub(crate) fn corpus_eval_benchmark(
    predictions_path: std::path::PathBuf,
    json: bool,
) -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::eval_harness::{evaluate_predictions, EvalPrediction};

    eprintln!(
        "{BOLD}Running ShellSafetyBench eval harness on {}...{RESET}",
        predictions_path.display()
    );

    let content = std::fs::read_to_string(&predictions_path).map_err(|e| {
        Error::Validation(format!(
            "Failed to read {}: {e}",
            predictions_path.display()
        ))
    })?;

    let predictions: Vec<EvalPrediction> = content
        .lines()
        .filter(|l| !l.trim().is_empty())
        .filter_map(|l| serde_json::from_str(l).ok())
        .collect();

    if predictions.is_empty() {
        return Err(Error::Validation(
            "No valid predictions found in input file".to_string(),
        ));
    }

    let results = evaluate_predictions(&predictions);

    if json {
        let json_str = serde_json::to_string_pretty(&results).unwrap_or_else(|_| "{}".to_string());
        println!("{json_str}");
    } else {
        println!("{BOLD}=== ShellSafetyBench Eval Results ==={RESET}\n");
        println!("  Predictions: {}", predictions.len());
        println!("  Weighted Score: {:.1}%\n", results.weighted_score * 100.0);

        println!("  {:<20} {:>8} {:>8}", "Metric", "Score", "Weight");
        println!("  {}", "-".repeat(40));
        println!(
            "  {:<20} {:>7.1}% {:>7.0}%",
            "Detection F1",
            results.detection_f1 * 100.0,
            25.0
        );
        println!(
            "  {:<20} {:>7.1}% {:>7.0}%",
            "Rule Citation",
            results.rule_citation * 100.0,
            20.0
        );
        println!(
            "  {:<20} {:>7.1}% {:>7.0}%",
            "CWE Mapping",
            results.cwe_mapping * 100.0,
            10.0
        );
        println!(
            "  {:<20} {:>7.1}% {:>7.0}%",
            "Fix Validity",
            results.fix_validity * 100.0,
            15.0
        );
        println!(
            "  {:<20} {:>7.1}% {:>7.0}%",
            "Explanation",
            results.explanation_quality * 100.0,
            15.0
        );
        println!(
            "  {:<20} {:>7.1}% {:>7.0}%",
            "OOD Generalization",
            results.ood_generalization * 100.0,
            15.0
        );
    }

    Ok(())
}

/// Validate pipeline tooling availability (SSC v12 S14 preflight)
pub(crate) fn corpus_pipeline_check(json: bool) -> Result<()> {
    use std::process::Command;

    const GREEN: &str = "\x1b[32m";
    const RED: &str = "\x1b[31m";
    const YELLOW: &str = "\x1b[33m";
    const BOLD: &str = "\x1b[1m";
    const RESET: &str = "\x1b[0m";

    struct ToolCheck {
        name: &'static str,
        command: &'static str,
        args: &'static [&'static str],
        required: bool,
    }

    let checks = [
        ToolCheck {
            name: "bashrs",
            command: "bashrs",
            args: &["--version"],
            required: true,
        },
        ToolCheck {
            name: "verificar",
            command: "verificar",
            args: &["--version"],
            required: true,
        },
        ToolCheck {
            name: "alimentar",
            command: "alimentar",
            args: &["--version"],
            required: true,
        },
        ToolCheck {
            name: "shellcheck",
            command: "shellcheck",
            args: &["--version"],
            required: true,
        },
        ToolCheck {
            name: "entrenar",
            command: "entrenar",
            args: &["--version"],
            required: false,
        },
        ToolCheck {
            name: "apr-cli",
            command: "apr",
            args: &["--version"],
            required: false,
        },
    ];

    let mut results = Vec::new();
    let mut all_required_pass = true;

    for check in &checks {
        let status = Command::new(check.command).args(check.args).output();

        let (pass, version) = match status {
            Ok(output) if output.status.success() => {
                let ver = String::from_utf8_lossy(&output.stdout)
                    .lines()
                    .next()
                    .unwrap_or("unknown")
                    .trim()
                    .to_string();
                (true, ver)
            }
            _ => {
                if check.required {
                    all_required_pass = false;
                }
                (false, "not found".to_string())
            }
        };

        results.push((check.name, pass, version, check.required));
    }

    // Check config files
    let configs = [
        ("configs/pipeline/ssc.yaml", true),
        ("configs/train/ssc-qwen3-4b-qlora.yaml", true),
        ("configs/qa/ssc-release-v1.yaml", true),
        ("configs/cwe-mapping.yaml", true),
        (
            "provable-contracts/contracts/shellsafetybench-v1.yaml",
            true,
        ),
    ];

    let mut config_results = Vec::new();
    for (path, required) in &configs {
        let exists = std::path::Path::new(path).exists();
        if *required && !exists {
            all_required_pass = false;
        }
        config_results.push((*path, exists));
    }

    // Check data artifacts
    let artifacts = [
        "training/shellsafetybench/conversations.jsonl",
        "training/shellsafetybench/benchmark.jsonl",
    ];

    let mut artifact_results = Vec::new();
    for path in &artifacts {
        let exists = std::path::Path::new(path).exists();
        artifact_results.push((*path, exists));
    }

    if json {
        let tool_json: Vec<_> = results
            .iter()
            .map(|(name, pass, ver, req)| {
                serde_json::json!({
                    "tool": name,
                    "available": pass,
                    "version": ver,
                    "required": req,
                })
            })
            .collect();

        let config_json: Vec<_> = config_results
            .iter()
            .map(|(path, exists)| {
                serde_json::json!({
                    "path": path,
                    "exists": exists,
                })
            })
            .collect();

        let artifact_json: Vec<_> = artifact_results
            .iter()
            .map(|(path, exists)| {
                serde_json::json!({
                    "path": path,
                    "exists": exists,
                })
            })
            .collect();

        let report = serde_json::json!({
            "pipeline_ready": all_required_pass,
            "tools": tool_json,
            "configs": config_json,
            "artifacts": artifact_json,
        });

        println!(
            "{}",
            serde_json::to_string_pretty(&report).unwrap_or_default()
        );
    } else {
        eprintln!("{BOLD}SSC Pipeline Preflight Check{RESET}\n");

        eprintln!("{BOLD}Tools:{RESET}");
        for (name, pass, version, required) in &results {
            let icon = if *pass {
                format!("{GREEN}\u{2713}")
            } else if *required {
                format!("{RED}\u{2717}")
            } else {
                format!("{YELLOW}\u{25cb}")
            };
            let req_tag = if *required { "" } else { " (optional)" };
            eprintln!("  {icon}{RESET} {name}: {version}{req_tag}");
        }

        eprintln!("\n{BOLD}Config files:{RESET}");
        for (path, exists) in &config_results {
            let icon = if *exists {
                format!("{GREEN}\u{2713}")
            } else {
                format!("{RED}\u{2717}")
            };
            eprintln!("  {icon}{RESET} {path}");
        }

        eprintln!("\n{BOLD}Data artifacts:{RESET}");
        for (path, exists) in &artifact_results {
            let icon = if *exists {
                format!("{GREEN}\u{2713}")
            } else {
                format!("{YELLOW}\u{25cb}")
            };
            eprintln!("  {icon}{RESET} {path}");
        }

        if all_required_pass {
            eprintln!("\n{GREEN}\u{2713}{RESET} {BOLD}Pipeline ready{RESET}");
        } else {
            eprintln!("\n{RED}\u{2717}{RESET} {BOLD}Missing required tools or configs{RESET}");
        }
    }

    if all_required_pass {
        Ok(())
    } else {
        Err(crate::Error::Validation(
            "Pipeline preflight check failed: missing required tools or configs".to_string(),
        ))
    }
}

/// Merge corpus conversations + verificar mutations into unified training JSONL.
///
/// Reads the corpus conversations (auto-generated), additional input files
/// (e.g., verificar-labeled.jsonl), normalizes the schema, deduplicates,
/// shuffles deterministically, and writes a single merged JSONL.
pub(crate) fn corpus_merge_data(
    output: std::path::PathBuf,
    extra_inputs: Vec<std::path::PathBuf>,
    seed: u64,
) -> Result<()> {
    use crate::cli::color::*;
    use std::io::Write;

    let mut entries: Vec<serde_json::Value> = Vec::new();

    // 1. Load corpus conversations (labeled)
    let corpus_path = std::path::Path::new("training/shellsafetybench/conversations.jsonl");
    if corpus_path.exists() {
        let file = std::fs::File::open(corpus_path)?;
        let reader = std::io::BufReader::new(file);
        let mut count = 0usize;
        for line in std::io::BufRead::lines(reader) {
            let line = line?;
            if line.trim().is_empty() {
                continue;
            }
            if let Ok(mut val) = serde_json::from_str::<serde_json::Value>(&line) {
                // Ensure source tag
                if let Some(obj) = val.as_object_mut() {
                    obj.entry("source".to_string())
                        .or_insert_with(|| serde_json::json!("bashrs-corpus"));
                }
                entries.push(val);
                count += 1;
            }
        }
        eprintln!("  Loaded {count} entries from corpus conversations");
    } else {
        eprintln!("{YELLOW}  Warning: corpus conversations not found, skipping{RESET}");
    }


}

    include!("corpus_ssb_commands_part2_incl2.rs");