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
//! Corpus convergence, mining, fix gaps, org patterns, and schema validation.

use crate::cli::args::CorpusFormatArg;
use crate::models::{Error, Result};
use std::path::PathBuf;

pub(crate) fn corpus_converge_table() -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::convergence;
    use crate::corpus::runner::CorpusRunner;

    let log_path = PathBuf::from(".quality/convergence.log");
    let entries = CorpusRunner::load_convergence_log(&log_path)
        .map_err(|e| Error::Internal(format!("Failed to read convergence log: {e}")))?;
    if entries.is_empty() {
        println!("No convergence history. Run `bashrs corpus run --log` first.");
        return Ok(());
    }

    println!("{BOLD}Multi-Corpus Convergence Table (\u{00a7}11.10.5){RESET}");
    println!();

    let table = convergence::format_convergence_table(&entries);
    for line in table.lines() {
        println!("  {line}");
    }

    Ok(())
}

/// Per-format delta between two iterations (§11.10.5).
pub(crate) fn corpus_converge_diff(from: Option<u32>, to: Option<u32>) -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::convergence;
    use crate::corpus::runner::CorpusRunner;

    let log_path = PathBuf::from(".quality/convergence.log");
    let entries = CorpusRunner::load_convergence_log(&log_path)
        .map_err(|e| Error::Internal(format!("Failed to read convergence log: {e}")))?;
    if entries.len() < 2 {
        println!("Need at least 2 iterations for diff. Run `bashrs corpus run --log` more.");
        return Ok(());
    }

    let from_entry = match from {
        Some(n) => entries
            .iter()
            .find(|e| e.iteration == n)
            .ok_or_else(|| Error::Internal(format!("Iteration #{n} not found in log")))?,
        None => &entries[entries.len() - 2],
    };

    let to_entry = match to {
        Some(n) => entries
            .iter()
            .find(|e| e.iteration == n)
            .ok_or_else(|| Error::Internal(format!("Iteration #{n} not found in log")))?,
        None => entries
            .last()
            .ok_or_else(|| Error::Internal("convergence log is empty".into()))?,
    };

    let diff = convergence::compare_iterations(from_entry, to_entry);

    println!("{BOLD}Convergence Diff (\u{00a7}11.10.5){RESET}");
    println!();

    let table = convergence::format_iteration_diff(&diff);
    for line in table.lines() {
        // Colorize delta arrows
        let colored = line
            .replace('\u{2191}', &format!("{GREEN}\u{2191}{RESET}"))
            .replace('\u{2193}', &format!("{RED}\u{2193}{RESET}"));
        println!("  {colored}");
    }

    Ok(())
}

/// Per-format convergence status with trend (§11.10.5).
pub(crate) fn corpus_converge_status() -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::convergence;
    use crate::corpus::runner::CorpusRunner;

    let log_path = PathBuf::from(".quality/convergence.log");
    let entries = CorpusRunner::load_convergence_log(&log_path)
        .map_err(|e| Error::Internal(format!("Failed to read convergence log: {e}")))?;
    if entries.is_empty() {
        println!("No convergence history. Run `bashrs corpus run --log` first.");
        return Ok(());
    }

    let statuses = convergence::convergence_status(&entries);

    println!("{BOLD}Per-Format Convergence Status (\u{00a7}11.10.5){RESET}");
    println!();

    let output = convergence::format_convergence_status(&statuses);
    for line in output.lines() {
        // Colorize trend arrows and status keywords
        let colored = line
            .replace("CONVERGED", &format!("{BRIGHT_GREEN}CONVERGED{RESET}"))
            .replace("REGRESSING", &format!("{BRIGHT_RED}REGRESSING{RESET}"))
            .replace("IMPROVING", &format!("{YELLOW}IMPROVING{RESET}"))
            .replace(
                "\u{2191} Improving",
                &format!("{GREEN}\u{2191} Improving{RESET}"),
            )
            .replace("\u{2192} Stable", &format!("{CYAN}\u{2192} Stable{RESET}"))
            .replace(
                "\u{2193} Regressing",
                &format!("{RED}\u{2193} Regressing{RESET}"),
            );
        println!("  {colored}");
    }

    Ok(())
}

/// Mine fix patterns from git history (§11.9.1).
pub(crate) fn corpus_mine(limit: usize) -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::oip;
    use crate::corpus::registry::CorpusRegistry;

    // Run git log to find fix commits
    let output = std::process::Command::new("git")
        .args([
            "log",
            "--oneline",
            "--all",
            &format!("--max-count={limit}"),
            "--grep=fix:",
            "--format=%h|%as|%s",
        ])
        .output()
        .map_err(|e| Error::Internal(format!("Failed to run git log: {e}")))?;

    let log_text = String::from_utf8_lossy(&output.stdout);
    let mut commits = oip::parse_fix_commits(&log_text);

    // Cross-reference with corpus entries
    let registry = CorpusRegistry::load_full();
    let descriptions: Vec<String> = registry
        .entries
        .iter()
        .map(|e| e.description.clone())
        .collect();

    for commit in &mut commits {
        commit.has_corpus_entry = oip::has_matching_corpus_entry(&commit.message, &descriptions);
    }

    println!("{BOLD}OIP Fix Pattern Mining (\u{00a7}11.9){RESET}");
    println!();

    let table = oip::format_mine_table(&commits);
    for line in table.lines() {
        let colored = line
            .replace("\u{2713}", &format!("{GREEN}\u{2713}{RESET}"))
            .replace("\u{2717}", &format!("{RED}\u{2717}{RESET}"))
            .replace("HIGH", &format!("{BRIGHT_RED}HIGH{RESET}"))
            .replace("ASTTransform", &format!("{CYAN}ASTTransform{RESET}"))
            .replace(
                "SecurityVulnerabilities",
                &format!("{BRIGHT_RED}SecurityVulnerabilities{RESET}"),
            )
            .replace(
                "OperatorPrecedence",
                &format!("{YELLOW}OperatorPrecedence{RESET}"),
            );
        println!("  {colored}");
    }

    Ok(())
}

/// Find fix commits without regression corpus entries (§11.9.3).
pub(crate) fn corpus_fix_gaps(limit: usize) -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::oip;
    use crate::corpus::registry::CorpusRegistry;

    // Run git log to find fix commits
    let output = std::process::Command::new("git")
        .args([
            "log",
            "--oneline",
            "--all",
            &format!("--max-count={limit}"),
            "--grep=fix:",
            "--format=%h|%as|%s",
        ])
        .output()
        .map_err(|e| Error::Internal(format!("Failed to run git log: {e}")))?;

    let log_text = String::from_utf8_lossy(&output.stdout);
    let mut commits = oip::parse_fix_commits(&log_text);

    // Cross-reference with corpus entries
    let registry = CorpusRegistry::load_full();
    let descriptions: Vec<String> = registry
        .entries
        .iter()
        .map(|e| e.description.clone())
        .collect();

    for commit in &mut commits {
        commit.has_corpus_entry = oip::has_matching_corpus_entry(&commit.message, &descriptions);
    }

    let gaps = oip::find_fix_gaps(&commits, &descriptions);

    println!("{BOLD}Fix-Driven Corpus Gaps (\u{00a7}11.9.3){RESET}");
    println!();

    if gaps.is_empty() {
        println!("  {GREEN}No gaps found!{RESET} All high/medium priority fix commits have corpus coverage.");
        return Ok(());
    }

    let table = oip::format_fix_gaps_table(&gaps);
    for line in table.lines() {
        let colored = line
            .replace("HIGH", &format!("{BRIGHT_RED}HIGH{RESET}"))
            .replace("MEDIUM", &format!("{YELLOW}MEDIUM{RESET}"));
        println!("  {colored}");
    }

    Ok(())
}

/// Cross-project defect pattern analysis (§11.9.4).
pub(crate) fn corpus_org_patterns() -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::oip;

    let patterns = oip::known_org_patterns();

    println!("{BOLD}Cross-Project Defect Patterns (\u{00a7}11.9.4){RESET}");
    println!();

    let table = oip::format_org_patterns_table(&patterns);
    for line in table.lines() {
        let colored = line
            .replace("ASTTransform", &format!("{CYAN}ASTTransform{RESET}"))
            .replace(
                "ComprehensionBugs",
                &format!("{YELLOW}ComprehensionBugs{RESET}"),
            )
            .replace(
                "OperatorPrecedence",
                &format!("{YELLOW}OperatorPrecedence{RESET}"),
            )
            .replace(
                "ConfigurationErrors",
                &format!("{YELLOW}ConfigurationErrors{RESET}"),
            )
            .replace("TypeErrors", &format!("{YELLOW}TypeErrors{RESET}"))
            .replace(
                "IntegrationFailures",
                &format!("{YELLOW}IntegrationFailures{RESET}"),
            );
        println!("  {colored}");
    }

    // Show coverage summary
    println!("  {BOLD}Corpus Coverage:{RESET}");
    for p in &patterns {
        let coverage = if p.covered_entries.is_empty() {
            format!("{RED}\u{2717} No coverage{RESET}")
        } else {
            format!("{GREEN}\u{2713} {}{RESET}", p.covered_entries.join(", "))
        };
        println!("    {:<30} {}", p.name, coverage);
    }

    Ok(())
}

pub(crate) fn corpus_schema_validate() -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::registry::CorpusRegistry;
    use crate::corpus::schema_enforcement;

    let registry = CorpusRegistry::load_full();
    let report = schema_enforcement::validate_corpus(&registry);

    println!("{BOLD}Formal Schema Enforcement (\u{00a7}11.8){RESET}");
    println!();

    let table = schema_enforcement::format_schema_report(&report);
    for line in table.lines() {
        let colored = line
            .replace("Bash", &format!("{CYAN}Bash{RESET}"))
            .replace("Makefile", &format!("{YELLOW}Makefile{RESET}"))
            .replace("Dockerfile", &format!("{GREEN}Dockerfile{RESET}"));
        println!("  {colored}");
    }

    // Layer coverage summary
    println!();
    println!("  {BOLD}Layer Coverage:{RESET}");
    for layer in &["L1:Lexical", "L2:Syntactic", "L3:Semantic"] {
        let passed = report
            .results
            .iter()
            .filter(|r| r.layers_passed.iter().any(|l| format!("{l}") == *layer))
            .count();
        let pct = if report.total_entries > 0 {
            (passed as f64 / report.total_entries as f64) * 100.0
        } else {
            0.0
        };
        let color = if pct >= 99.0 {
            GREEN
        } else if pct >= 90.0 {
            YELLOW
        } else {
            RED
        };
        println!(
            "    {:<16} {color}{}/{} ({:.1}%){RESET}",
            layer, passed, report.total_entries, pct,
        );
    }

    println!();
    let status = if report.total_violations == 0 {
        format!("{GREEN}\u{2713} All entries pass schema validation{RESET}")
    } else {
        format!(
            "{YELLOW}\u{26a0} {} violation(s) in {} entries{RESET}",
            report.total_violations,
            report.total_entries - report.valid_entries,
        )
    };
    println!("  {status}");

    Ok(())
}

pub(crate) fn corpus_grammar_errors() -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::registry::CorpusRegistry;
    use crate::corpus::schema_enforcement;

    let registry = CorpusRegistry::load_full();
    let report = schema_enforcement::validate_corpus(&registry);

    println!("{BOLD}Grammar Violations by Category (\u{00a7}11.8.5){RESET}");
    println!();

    let table = schema_enforcement::format_grammar_errors(&report);
    for line in table.lines() {
        let colored = line
            .replace("GRAM-001", &format!("{CYAN}GRAM-001{RESET}"))
            .replace("GRAM-002", &format!("{CYAN}GRAM-002{RESET}"))
            .replace("GRAM-003", &format!("{YELLOW}GRAM-003{RESET}"))
            .replace("GRAM-004", &format!("{GREEN}GRAM-004{RESET}"))
            .replace("GRAM-005", &format!("{YELLOW}GRAM-005{RESET}"))
            .replace("GRAM-006", &format!("{CYAN}GRAM-006{RESET}"))
            .replace("GRAM-007", &format!("{GREEN}GRAM-007{RESET}"))
            .replace("GRAM-008", &format!("{YELLOW}GRAM-008{RESET}"));
        println!("  {colored}");
    }

    // Show fix pattern suggestions
    if !report.violations_by_category.is_empty() {
        println!();
        println!("  {BOLD}Fix Patterns:{RESET}");
        for (cat, count) in &report.violations_by_category {
            if *count > 0 {
                println!(
                    "    {} ({} violations): {}",
                    cat.code(),
                    count,
                    cat.fix_pattern(),
                );
            }
        }
    }

    Ok(())
}

pub(crate) fn corpus_format_grammar(format: CorpusFormatArg) -> Result<()> {
    use crate::cli::color::*;
    use crate::corpus::registry::CorpusFormat;
    use crate::corpus::schema_enforcement;

    let corpus_format = match format {
        CorpusFormatArg::Bash => CorpusFormat::Bash,
        CorpusFormatArg::Makefile => CorpusFormat::Makefile,
        CorpusFormatArg::Dockerfile => CorpusFormat::Dockerfile,
    };

    let spec = schema_enforcement::format_grammar_spec(corpus_format);

    println!("{BOLD}Formal Grammar Specification (\u{00a7}11.8){RESET}");
    println!();

    for line in spec.lines() {
        let colored = if line.contains("Validation Layers:") {
            format!("{BOLD}{line}{RESET}")
        } else if line.starts_with("  L") {
            let parts: Vec<&str> = line.splitn(2, '\u{2014}').collect();
            if parts.len() == 2 {
                format!("{CYAN}{}{RESET}\u{2014}{}", parts[0], parts[1])
            } else {
                format!("{CYAN}{line}{RESET}")
            }
        } else if line.contains("Grammar") {
            format!("{BOLD}{line}{RESET}")
        } else {
            line.to_string()
        };
        println!("  {colored}");
    }

    Ok(())
}