Files
lucisv/docs/features/cli-path-r5

Core Path Normalization, CLI Stdin Config, JSON Format Output, and CLI Test Hardening (Round 5A)

Goal

Deliver the Round 5A core path, CLI stdin config discovery, structured format JSON output, and CLI test hardening items (#1, #11, #55):

  1. Unified lexical path normalization (#1): Extract lucisv_core::path::{normalize, normalize_under} in crates/lucisv_core/src/path.rs (re-exported via crates/lucisv_core/src/lib.rs) and replace the duplicate private normalization copies in crates/lucisv_core/src/filelist.rs, crates/lucisv_core/src/config.rs, crates/lucisv_server/src/includes.rs, and crates/lucisv/src/main.rs while preserving exact caller semantics across Unix and Windows prefixes/roots.
  2. CLI stdin config discovery & #55: Ensure CLI stdin mode (- / --stdin-filename <path>) discovers and applies lucisv.toml / .lucisv.toml consistently in both format and check (including [lint] select and [lint] ignore precedence in run_check_stdin, rejecting empty --stdin-filename ""), and strengthen a_stdin_buffer_reads_the_rule_options_too in crates/lucisv/tests/cli.rs to assert explicit exit codes alongside stdout.
  3. Structured --output-format json for CLI format mode (#11): Extend --output-format <text|json> to format mode (lucisv / lucisv --check / lucisv format), emitting a single-line JSON report with top-level status, exit_code, summary, and per-file files entries (filename, status, cached, error: {category, line, column, message} | null) while preserving existing text output and frozen 0/1/2 exit codes. Wire tools/corpus_check.sh and scripts/score.sh cleanly to consume --output-format json when jq is present (with text fallback), preserving identical TSVs and 814/6 corpus output.

Assumptions

  • Lexical normalization must never call std::fs::canonicalize so symlinks stay unfollowed and non-existent paths (--stdin-filename, rename targets) normalize identically to existing ones.
  • Leading/excess .. components on relative paths (and unrooted Windows drive-relative paths like C:a\..\..) are preserved, while .. after a root directory (/ or C:\ or \\server\share\) clamps at the root.
  • In format mode, --output-format json conflicts with --diff, --stdout, and stdin without --check (exiting 2), matching --diff vs --stdout mutual exclusion so stdout is strictly one valid JSON document whenever JSON output is requested.

Handshakes

  • crates/lucisv_core/src/path.rs ↔ crates/lucisv_core/src/{filelist.rs,config.rs}, crates/lucisv_server/src/includes.rs, crates/lucisv/src/main.rs:
    • includes::normalize re-exports lucisv_core::path::normalize so crates/lucisv_server/src/file_ops.rs continues using crate::includes::normalize without cross-lane edits.
    • filelist::Reader, config::Discovery, and lucisv::identity call lucisv_core::path::normalize_under.
  • crates/lucisv/src/{args.rs,main.rs,lint.rs} ↔ tools/corpus_check.sh, scripts/score.sh:
    • Format JSON schema: {"status":"ok|would-reformat|reformatted|error","exit_code":0|1|2,"summary":{"changed":N,"unchanged":N,"cached":N,"errors":N},"files":[{"filename":...,"status":"unchanged|cached|would-reformat|reformatted|error","cached":bool,"error":null|{"category":"parse-error|lex-error|verification-error|io-error|selection-error|panic","line":u32|null,"column":u32|null,"message":...}}]}.
    • Frozen 0/1/2 CLI exit codes, stderr error/summary formatting, and corpus_check.sh / score.sh TSV and stdout formats are preserved identically (814/6, byte-identical TSV rows).
  • crates/lucisv/tests/cli.rs:
    • #55 closed by explicit exit-code assertions (0 under matching config, 1 without config) in a_stdin_buffer_reads_the_rule_options_too, plus regression tests for stdin config discovery (format and check) and format --output-format json.

Status / TODO

  • Implement lucisv_core::path::{normalize, normalize_under} and replace duplicate copies in filelist.rs, config.rs, includes.rs, and main.rs
  • Fix stdin [lint] select/ignore discovery and empty --stdin-filename handling; strengthen #55 and add stdin format/check discovery tests
  • Implement format-mode --output-format json (args.rs, main.rs, sharing lint::quote / lint::pick), wire tools/corpus_check.sh and scripts/score.sh, and add CLI JSON tests
  • Enforce <= 600 non-blank non-comment prod lines before mod tests, 0 .unwrap()/.expect()/panic!/unsafe in library code, author stamps, and full verification gates

Work log

2026-11-26 — dev (Round 5A)

  • Created crates/lucisv_core/src/path.rs (normalize, normalize_under) and exported pub mod path in crates/lucisv_core/src/lib.rs, with Unix, Windows prefix/root/drive-relative, and platform-independent unit tests.
  • Replaced duplicate lexical normalization loops in crates/lucisv_core/src/filelist.rs, crates/lucisv_core/src/config.rs, crates/lucisv_server/src/includes.rs (pub use lucisv_core::path::normalize), and crates/lucisv/src/main.rs (normalize_under).
  • Fixed crates/lucisv/src/main.rs::run_check_stdin to honor [lint] select / [lint] ignore via lint::pick, rejected empty --stdin-filename "" in crates/lucisv/src/args.rs (with .filter(|s| !s.is_empty()) defense in stdin_config), and strengthened a_stdin_buffer_reads_the_rule_options_too (#55) plus new relative/../ stdin config discovery tests in crates/lucisv/tests/cli.rs.
  • Added --output-format json support to format mode (crates/lucisv/src/{args.rs,main.rs,lint.rs}), wired tools/corpus_check.sh and scripts/score.sh to consume --output-format json when jq is present (preserving text fallback and byte-identical TSV / 814/6 output), and added comprehensive CLI JSON tests (unchanged, would-reformat, reformatted, cached, parse-error, lex-error, io-error, stdin --check, and flag conflicts).
  • Verified production line counts before #[cfg(test)]/mod tests (path.rs 39, lib.rs 7, filelist.rs 160, config.rs 313, includes.rs 342, args.rs 444, main.rs 550, lint.rs 495) and 0 .unwrap()/.expect()/panic!/unsafe in non-test production code.

2026-11-26 — dev (Round 5A QA fix)

  • Fixed tools/corpus_check.sh exit-2 fallback when use_json=1 (jq present) receives empty stdout (e.g. a pre-process_file gather error such as a broken lucisv.toml) or empty/unparseable JSON error detail: extracted shared classify_text_error and re-ran "$lucisv" --check "$file" 2>&1 whenever msg is empty, aligning with scripts/score.sh.
  • Verified broken lucisv.toml repro (exit 2, non-empty column-3 error message preserved) and corpus parity both with jq and with jq absent from PATH, plus ./tools/gates.sh --only corpus (814/6), bash -n tools/corpus_check.sh scripts/score.sh, and python3 tools/stamp_author.py --check.

QA log

2026-11-26 — QA (Round 5A)

Verdict: FAIL (one defect in tools/corpus_check.sh exit-2 fallback when use_json=1 and stdout is empty).

  1. Ownership & invariants (PASS):
    • git status --short / git diff --stat: only Lane 5A files touched (crates/lucisv_core/src/{lib.rs,path.rs,filelist.rs,config.rs}, crates/lucisv_server/src/includes.rs, crates/lucisv/src/{args.rs,lint.rs,main.rs}, crates/lucisv/tests/cli.rs, scripts/score.sh, tools/corpus_check.sh, docs/features/cli-path-r5/README.md).
    • Non-blank, non-comment production lines before mod tests: path.rs 38, lib.rs 7, filelist.rs 159, config.rs 312, includes.rs 341, args.rs 443, lint.rs 494, main.rs 549 (all <= 600), with 0 .unwrap(), .expect(), panic!, or unsafe in non-test production code.
  2. Adversarial requirement audit:
    • #1 (lucisv_core::path — PASS): Verified normalize and normalize_under in crates/lucisv_core/src/path.rs (./.. folding without canonicalize, preserving leading/excess .. on relative and Windows drive-relative paths like C:a\..\..\b, clamping .. at Unix/Windows/UNC roots, ./a/.. -> ., "" -> ""), and verified duplicate lexical loops were removed from filelist.rs, config.rs, includes.rs (re-exporting lucisv_core::path::normalize), and main.rs (normalize_under). git grep "Component::" crates/ confirmed no remaining duplicate normalization loops in lucisv_core, lucisv_server, or lucisv.
    • Stdin config & #55 (PASS): Verified run_check_stdin, stdin_config, args.rs, lint::pick, and crates/lucisv/tests/cli.rs (a_stdin_buffer_reads_the_rule_options_too asserts explicit 0/1 exit codes; stdin_discovers_config_relative_to_stdin_filename_across_cwd_and_subcommands covers relative/../ discovery for format and check, [lint] select/ignore, unknown config rule exit 2, and empty --stdin-filename "" rejection). Adversarial binary probes confirmed CLI-over-config precedence (--select, --ignore, --no-config) in stdin check mode.
    • #11 (--output-format json — FAIL on tools/corpus_check.sh fallback):
      • args.rs / main.rs / tests/cli.rs: single-line JSON schema (status, exit_code, summary, files) verified across clean (exit 0), dirty (--check exit 1), in-place (reformatted, exit 0), cached (cached: true, exit 0), parse-error / lex-error / verification-error / io-error / selection-error / panic (exit 2), stdin --check, and conflict rejections (--diff, --stdout, non---check stdin).
      • scripts/score.sh & tools/corpus_check.sh parity: on ~/.cache/lucisv-corpus (820 files, 814/6), both scripts produced byte-identical TSVs and outputs with jq vs text fallback (PATH without jq).
      • Defect (tools/corpus_check.sh:57-102): When use_json=1 (jq present) and lucisv --check --output-format json "$file" 2>/dev/null exits 2 with empty stdout (for example, a pre-process_file gather error such as a broken lucisv.toml in a directory), if [ "$use_json" -eq 1 ] && [ -n "$out" ]; evaluates false and enters the else branch, grepping the empty $out instead of re-running "$lucisv" --check "$file" 2>&1 (unlike scripts/score.sh:184-193). Verified with a broken lucisv.toml probe: scripts/score.sh recorded the full error message in column 4, while tools/corpus_check.sh emitted an empty column 3 (other-error\t<file>\t). Likewise, if jq succeeds with an empty msg or fails (lines 86-92), corpus_check.sh hardcodes outcome=other-error instead of classifying the fallback text output uniformly.
  3. Executed checks (all in worktree):
    • python3 tools/stamp_author.py --check: PASS (would stamp 0 files; left 249 untouched)
    • cargo fmt --all -- --check: PASS
    • bash -n tools/corpus_check.sh scripts/score.sh: PASS
    • cargo clippy -p lucisv_core -p lucisv -p lucisv_server --all-targets -- -D warnings: PASS
    • cargo check --target x86_64-pc-windows-gnu --all-targets -p lucisv_core (with stable-aarch64-apple-darwin rustup toolchain): PASS
    • ./tools/guard.sh -m 8 -t 600 -- cargo test -p lucisv_core -p lucisv -p lucisv_server --all-targets: PASS (lucisv unit 35 passed, cli 117 passed, style_cli 5 passed; lucisv_core 122 passed; lucisv_server unit 2082 passed / 47 ignored, e2e 106 passed / 3 ignored, grammar 3 passed, ifdef_folding 1 passed, opentitan_references 0 passed / 2 ignored)
    • ./tools/gates.sh --only corpus,cross: PASS (corpus 814/6, cross Windows + Linux musl check pass)

2026-11-26 — QA re-verification (Round 5A)

Verdict: PASS.

  • Diff & ownership check: Confirmed git diff --stat changed only tools/corpus_check.sh (+5 lines vs prior audit) and docs/features/cli-path-r5/README.md; all Rust files (crates/lucisv_core/src/**, crates/lucisv_server/src/includes.rs, crates/lucisv/src/**, crates/lucisv/tests/cli.rs) and scripts/score.sh are unchanged since the prior PASS checks.
  • Fix inspection (tools/corpus_check.sh:40-108): Extracted classify_text_error and wired if [ -z "$msg" ]; then to re-run "$lucisv" --check "$file" 2>&1 when use_json=1 and classify via classify_text_error across empty $out, invalid JSON, and empty msg.
  • Repro & parity verification:
    • Broken lucisv.toml (exit 2, empty JSON stdout) with and without jq on PATH: both emitted identical TSV rows (other-error .../a.sv .../lucisv.toml:1: unknown key \bogus` ...), diff -u` clean.
    • Invalid-JSON fallback probe (not-valid-json with exit 2) with and without jq on PATH: both classified identically via classify_text_error (parse-error .../a.sv parse error: 9:1: synthetic fallback error), diff -u clean.
  • Re-executed gates:
    • bash -n tools/corpus_check.sh scripts/score.sh: PASS
    • python3 tools/stamp_author.py --check: PASS (would stamp 0 files; left 249 untouched)
    • ./tools/gates.sh --only corpus: PASS (814/6, corpus pass)

Review

Lead Self-Inspect (2026-11-26): APPROVED for trunk commit.

  • Merged Lane 5A onto trunk after Round 5A QA PASS (including tools/corpus_check.sh classify_text_error fallback fix on exit 2 empty-stdout / bad-JSON cases).
  • Verified #1 (lucisv_core::path::{normalize,normalize_under} replacing filelist, config, includes, main), stdin config + [lint] select/ignore + #55 exit codes, and #11 (--output-format json + byte-identical 814/6 corpus TSV parity).