pub mod cache;
mod composition_gate;
pub mod config;
pub mod diff;
pub mod duplicate_stems;
pub mod finding;
mod gates;
pub use gates::collect_yaml_files;
mod gates_extended;
pub mod relations_gate;
pub mod rules;
pub mod sarif;
pub mod shapes_gate;
pub mod sigma_gate;
pub mod sigma_symbols;
mod strict_test_binding;
pub mod trend;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::time::Instant;
use serde::Serialize;
use self::finding::LintFinding;
use self::gates::{
load_binding, load_contracts, run_audit_gate, run_score_gate, run_validate_gate,
};
use self::gates_extended::{
check_stale_suppressions, run_enforce_gate, run_enforcement_level_gate,
run_reverse_coverage_gate, run_verify_gate,
};
use self::rules::RuleSeverity;
use crate::ontology::arming::{meet_armed, ArmedGates};
use crate::ontology::verdict::Verdict;
#[derive(Debug, Clone, Serialize)]
pub struct GateResult {
pub name: String,
pub passed: bool,
pub skipped: bool,
pub verdict: Verdict,
pub duration_ms: u64,
pub detail: GateDetail,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extra: Option<GateExtra>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type")]
pub enum GateDetail {
#[serde(rename = "validate")]
Validate {
contracts: usize,
errors: usize,
warnings: usize,
error_messages: Vec<String>,
},
#[serde(rename = "audit")]
Audit {
contracts: usize,
findings: usize,
finding_messages: Vec<String>,
},
#[serde(rename = "score")]
Score {
contracts: usize,
min_score: f64,
mean_score: f64,
threshold: f64,
below_threshold: Vec<String>,
},
#[serde(rename = "verify")]
Verify {
total_refs: usize,
existing: usize,
missing: usize,
},
#[serde(rename = "enforce")]
Enforce {
equations_total: usize,
equations_with_pre: usize,
equations_with_post: usize,
equations_with_lean: usize,
},
#[serde(rename = "reverse_coverage")]
ReverseCoverage {
total_pub_fns: usize,
bound_fns: usize,
unbound_fns: usize,
coverage_pct: f64,
threshold_pct: f64,
},
#[serde(rename = "composition")]
Composition {
edges_checked: usize,
edges_satisfied: usize,
edges_broken: usize,
},
#[serde(rename = "skipped")]
Skipped { reason: String },
}
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum GateExtra {
#[serde(rename = "duplicate_stems")]
DuplicateStems {
divergent: usize,
baselined: usize,
unbaselined: Vec<String>,
stale: Vec<String>,
divergent_stems: Vec<String>,
},
#[serde(rename = "sigma")]
Sigma {
entity_types: usize,
roles: usize,
symbols: usize,
contracts_checked: usize,
violations: usize,
formal_total: usize,
formal_prose: usize,
},
#[serde(rename = "relations")]
Relations {
relations_n: usize,
contracts_with_relations: usize,
roles_used: Vec<String>,
cycles: Vec<String>,
legacy_depends_on: usize,
legacy_unresolved_depends_on: usize,
violations: usize,
},
#[serde(rename = "shapes")]
Shapes {
shapes_n: usize,
focus_nodes_n: usize,
pc_shape: String,
plant_violations: usize,
violations: usize,
warnings: usize,
by_shape: Vec<String>,
triples: usize,
armed_shapes: Vec<String>,
not_armed_shapes: Vec<String>,
unarmed_violations: usize,
by_entity_type: std::collections::BTreeMap<String, usize>,
pc_extract: std::collections::BTreeMap<String, String>,
receipts: usize,
witnesses: usize,
hex_mismatches: usize,
unmeasured_rows: usize,
w3c_cases_passed: usize,
w3c_cases_n: usize,
symbols_resolved: usize,
symbols_unresolved: usize,
lean_statements: usize,
lean_refs_unresolved: usize,
#[serde(skip_serializing_if = "Option::is_none")]
release: Option<Box<crate::ontology::extract::release_evidence::ReleaseStats>>,
},
}
#[derive(Debug, Clone, Serialize)]
pub struct LintReport {
pub passed: bool,
pub gates: Vec<GateResult>,
pub total_duration_ms: u64,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub findings: Vec<LintFinding>,
#[serde(skip)]
pub cache_stats: cache::CacheStats,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub contract_timings: Vec<(String, u64)>,
pub verdict: Verdict,
pub armed_gates: Vec<ArmedGateVerdict>,
pub not_armed: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub armed_monotone: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub armed_shapes_monotone: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ArmedGateVerdict {
pub name: String,
pub verdict: Verdict,
}
impl LintReport {
pub fn arm(&mut self, armed: &ArmedGates) {
let results: Vec<(String, Verdict)> = self
.gates
.iter()
.map(|g| (g.name.clone(), g.verdict))
.collect();
let meet = meet_armed(&results, armed);
self.verdict = meet.verdict;
self.armed_gates = meet
.armed
.into_iter()
.map(|(name, verdict)| ArmedGateVerdict { name, verdict })
.collect();
self.not_armed = meet.not_armed;
}
}
pub struct LintConfig<'a> {
pub contract_dir: &'a Path,
pub binding_path: Option<&'a Path>,
pub min_score: f64,
pub severity_filter: Option<RuleSeverity>,
pub severity_overrides: HashMap<String, RuleSeverity>,
pub suppressed_findings: Vec<String>,
pub suppressed_rules: Vec<String>,
pub suppressed_files: Vec<String>,
pub strict: bool,
pub no_cache: bool,
pub cache_stats: bool,
pub crate_dir: Option<&'a Path>,
pub min_level: Option<crate::schema::EnforcementLevel>,
pub strict_test_binding: bool,
}
impl<'a> LintConfig<'a> {
pub fn new(contract_dir: &'a Path, binding_path: Option<&'a Path>, min_score: f64) -> Self {
Self {
contract_dir,
binding_path,
min_score,
severity_filter: None,
severity_overrides: HashMap::new(),
suppressed_findings: Vec::new(),
suppressed_rules: Vec::new(),
suppressed_files: Vec::new(),
strict: false,
no_cache: false,
cache_stats: false,
crate_dir: None,
min_level: None,
strict_test_binding: false,
}
}
}
fn push_gate<F>(
gates: &mut Vec<GateResult>,
all_findings: &mut Vec<LintFinding>,
run: F,
name: &str,
validation_passed: bool,
) where
F: FnOnce() -> (GateResult, Vec<LintFinding>),
{
let (result, mut findings) = if validation_passed {
run()
} else {
(skipped_gate(name, "validation failed"), Vec::new())
};
gates.push(result);
all_findings.append(&mut findings);
}
fn per_contract_timings(
contracts: &[(String, crate::schema::Contract)],
binding: Option<&crate::binding::BindingRegistry>,
) -> Vec<(String, u64)> {
let mut contract_timings: Vec<(String, u64)> = Vec::with_capacity(contracts.len());
for (stem, contract) in contracts {
let ct_start = Instant::now();
let _ = crate::schema::validate_contract(contract);
let _ = crate::audit::audit_contract(contract);
let _ = crate::scoring::score_contract(contract, binding, stem);
let ct_ms = u64::try_from(ct_start.elapsed().as_micros() / 1000).unwrap_or(0);
contract_timings.push((format!("{stem}.yaml"), ct_ms));
}
contract_timings.sort_by_key(|b| std::cmp::Reverse(b.1));
contract_timings
}
fn store_findings_in_cache(
root: &std::path::Path,
config: &LintConfig,
contracts: &[(String, crate::schema::Contract)],
all_findings: &[LintFinding],
stats: &mut cache::CacheStats,
) {
let rule_cfg = format!("{:?}{:?}", config.severity_overrides, config.strict);
for (stem, _) in contracts {
stats.total += 1;
let yaml_path = config.contract_dir.join(format!("{stem}.yaml"));
let yaml_content = std::fs::read_to_string(&yaml_path).unwrap_or_default();
let hash = cache::content_hash(&yaml_content, &rule_cfg);
if cache::cache_get(root, &hash).is_some() {
stats.hits += 1;
} else {
stats.misses += 1;
let contract_findings: Vec<_> = all_findings
.iter()
.filter(|f| f.contract_stem.as_deref() == Some(stem.as_str()))
.cloned()
.collect();
let _ = cache::cache_put(root, &hash, &contract_findings);
}
}
}
#[allow(clippy::too_many_lines)]
pub fn run_lint(config: &LintConfig) -> LintReport {
let overall_start = Instant::now();
let mut gates = Vec::with_capacity(3);
let mut all_findings = Vec::new();
let mut stats = cache::CacheStats::default();
let mut contract_timings: Vec<(String, u64)> = Vec::new();
let cache_root = if config.no_cache {
None
} else {
Some(cache::cache_dir(config.contract_dir))
};
let (contracts, parse_errors) = load_contracts(config.contract_dir);
let binding = load_binding(config.binding_path);
let (validate_result, mut validate_findings) = run_validate_gate(&contracts, &parse_errors);
let validation_passed = validate_result.passed;
gates.push(validate_result);
push_gate(
&mut gates,
&mut all_findings,
|| run_audit_gate(&contracts),
"audit",
validation_passed,
);
push_gate(
&mut gates,
&mut all_findings,
|| run_score_gate(&contracts, binding.as_ref(), config.min_score),
"score",
validation_passed,
);
push_gate(
&mut gates,
&mut all_findings,
|| {
let project_root = config.contract_dir.parent().unwrap_or(config.contract_dir);
run_verify_gate(&contracts, project_root)
},
"verify",
validation_passed,
);
push_gate(
&mut gates,
&mut all_findings,
|| run_enforce_gate(&contracts),
"enforce",
validation_passed,
);
push_gate(
&mut gates,
&mut all_findings,
|| {
let min_level = config
.min_level
.unwrap_or(crate::schema::EnforcementLevel::Standard);
run_enforcement_level_gate(&contracts, min_level)
},
"enforcement-level",
validation_passed,
);
push_gate(
&mut gates,
&mut all_findings,
|| match (config.binding_path, config.crate_dir) {
(Some(bp), Some(cd)) => run_reverse_coverage_gate(bp, cd),
_ => (
skipped_gate("reverse-coverage", "no --binding or --crate-dir provided"),
Vec::new(),
),
},
"reverse-coverage",
validation_passed,
);
let duplicates = duplicate_stems::scan_duplicate_stems(config.contract_dir);
let ambiguous = duplicate_stems::ambiguous_stems(&duplicates);
push_gate(
&mut gates,
&mut all_findings,
|| {
let project_root = config.contract_dir.parent().unwrap_or(config.contract_dir);
let baseline = duplicate_stems::read_baseline(project_root);
duplicate_stems::run_duplicate_stem_gate(&duplicates, &baseline)
},
"duplicate-stems",
validation_passed,
);
push_gate(
&mut gates,
&mut all_findings,
|| composition_gate::run_composition_gate(&contracts, &ambiguous),
"composition",
validation_passed,
);
let (sigma_gate_result, mut sigma_findings) =
sigma_result(config.contract_dir, validation_passed);
gates.push(sigma_gate_result);
all_findings.append(&mut sigma_findings);
let (relations_gate_result, mut relations_findings) =
relations_result(config.contract_dir, validation_passed);
gates.push(relations_gate_result);
all_findings.append(&mut relations_findings);
let (shapes_gate_result, mut shapes_findings) =
shapes_result(config.contract_dir, validation_passed);
gates.push(shapes_gate_result);
all_findings.append(&mut shapes_findings);
if config.strict_test_binding {
push_gate(
&mut gates,
&mut all_findings,
|| {
let project_root = config.contract_dir.parent().unwrap_or(config.contract_dir);
strict_test_binding::run_strict_test_binding_gate(
&contracts,
project_root,
config.strict,
)
},
"strict-test-binding",
validation_passed,
);
}
all_findings.append(&mut validate_findings);
if validation_passed {
contract_timings = per_contract_timings(&contracts, binding.as_ref());
}
let mut stale_findings = check_stale_suppressions(
&all_findings,
&config.suppressed_rules,
&config.suppressed_findings,
);
all_findings.append(&mut stale_findings);
mark_new_findings(&mut all_findings, config.contract_dir);
if let Some(ref root) = cache_root {
store_findings_in_cache(root, config, &contracts, &all_findings, &mut stats);
}
apply_suppressions(&mut all_findings, config);
apply_severity_overrides(&mut all_findings, config);
if let Some(min_sev) = config.severity_filter {
all_findings.retain(|f| f.severity >= min_sev);
}
let passed = gates.iter().all(|g| g.passed || g.skipped);
let mut report = LintReport {
passed,
gates,
total_duration_ms: u64::try_from(overall_start.elapsed().as_millis()).unwrap_or(u64::MAX),
findings: all_findings,
cache_stats: stats,
contract_timings,
verdict: Verdict::Unknown(crate::ontology::verdict::Reason::NotArmed),
armed_gates: Vec::new(),
not_armed: Vec::new(),
armed_monotone: None,
armed_shapes_monotone: None,
};
report.arm(&ArmedGates::default_set());
report
}
pub enum NamedGateOutcome {
UnknownGate,
Sigma(sigma_gate::SigmaOutcome),
Relations(relations_gate::RelationsOutcome),
Shapes(shapes_gate::ShapesOutcome),
Ran {
result: Box<GateResult>,
findings: Vec<LintFinding>,
},
}
#[must_use]
pub fn run_named_gate(contract_dir: &Path, name: &str) -> NamedGateOutcome {
run_named_gate_with(contract_dir, name, &shapes_gate::ShapesOptions::default())
}
pub fn run_named_gate_with(
contract_dir: &Path,
name: &str,
shapes_opts: &shapes_gate::ShapesOptions,
) -> NamedGateOutcome {
match name {
"relations" => {
NamedGateOutcome::Relations(relations_gate::run_relations_gate(contract_dir))
}
"shapes" => {
NamedGateOutcome::Shapes(shapes_gate::run_shapes_gate_with(contract_dir, shapes_opts))
}
"sigma" => NamedGateOutcome::Sigma(sigma_gate::run_sigma_gate(contract_dir)),
"validate" => {
let (contracts, parse_errors) = load_contracts(contract_dir);
let (result, findings) = run_validate_gate(&contracts, &parse_errors);
NamedGateOutcome::Ran {
result: Box::new(result),
findings,
}
}
_ => NamedGateOutcome::UnknownGate,
}
}
pub const NAMED_GATES: [&str; 4] = ["relations", "shapes", "sigma", "validate"];
fn sigma_result(contract_dir: &Path, validation_passed: bool) -> (GateResult, Vec<LintFinding>) {
if !validation_passed {
return (skipped_gate("sigma", "validation failed"), Vec::new());
}
match sigma_gate::run_sigma_gate(contract_dir) {
sigma_gate::SigmaOutcome::Ran { result, findings } => (*result, findings),
sigma_gate::SigmaOutcome::NoSigma => (
skipped_gate("sigma", "no contracts/ontology.yaml"),
Vec::new(),
),
sigma_gate::SigmaOutcome::Malformed(e) => (
skipped_gate("sigma", &format!("Σ is malformed: {e}")),
Vec::new(),
),
}
}
fn relations_result(
contract_dir: &Path,
validation_passed: bool,
) -> (GateResult, Vec<LintFinding>) {
if !validation_passed {
return (skipped_gate("relations", "validation failed"), Vec::new());
}
match relations_gate::run_relations_gate(contract_dir) {
relations_gate::RelationsOutcome::Ran { result, findings } => (*result, findings),
relations_gate::RelationsOutcome::NoSigma => (
skipped_gate("relations", "no contracts/ontology.yaml"),
Vec::new(),
),
relations_gate::RelationsOutcome::Malformed(e) => (
skipped_gate("relations", &format!("Σ is malformed: {e}")),
Vec::new(),
),
relations_gate::RelationsOutcome::NoRelations {
contracts_checked,
legacy_depends_on,
} => (
skipped_gate(
"relations",
&format!("no typed relations in {contracts_checked} contracts ({legacy_depends_on} legacy metadata.depends_on edges) — R-2: zero is a decline"),
),
Vec::new(),
),
}
}
fn shapes_result(contract_dir: &Path, validation_passed: bool) -> (GateResult, Vec<LintFinding>) {
if !validation_passed {
return (skipped_gate("shapes", "validation failed"), Vec::new());
}
match shapes_gate::run_shapes_gate(contract_dir) {
shapes_gate::ShapesOutcome::Ran { result, findings } => (*result, findings),
shapes_gate::ShapesOutcome::Unsupported(e) => (skipped_gate("shapes", &format!("{e}")), Vec::new()),
shapes_gate::ShapesOutcome::ExtractFailed(e) => (skipped_gate("shapes", &format!("{e}")), Vec::new()),
shapes_gate::ShapesOutcome::NoShapes { contracts_checked } => (
skipped_gate("shapes", &format!("no `shape:` block in {contracts_checked} contracts — R-2: zero is a decline")),
Vec::new(),
),
shapes_gate::ShapesOutcome::NoFocus { shapes_n } => (
skipped_gate("shapes", &format!("{shapes_n} shape(s), no focus node")),
Vec::new(),
),
shapes_gate::ShapesOutcome::WrongCorpus { shapes_n, expected, found, refused } => (
skipped_gate("shapes", &format!("extract:parity-receipt matched {found} focus node(s) and evidence/parity/EXPECTED_RECEIPTS says {expected} ({shapes_n} shape(s)){} — an extractor that saw the wrong corpus reports the same \"no violations\" as one that saw all of it", if refused.is_empty() { String::new() } else { format!("; refused: {}", refused.join("; ")) })),
Vec::new(),
),
shapes_gate::ShapesOutcome::NoReceipts { shapes_n, dir } => (
skipped_gate("shapes", &format!("{shapes_n} shape(s) resolve receipts and the tree holds none under {dir}/ — R-2: unmeasured is a decline")),
Vec::new(),
),
shapes_gate::ShapesOutcome::PositiveControlFailed { shapes_n, focus_nodes_n, which } => (
skipped_gate("shapes", &format!("positive control {which} did not fire ({shapes_n} shape(s), {focus_nodes_n} focus node(s)) — the gate cannot reject")),
Vec::new(),
),
shapes_gate::ShapesOutcome::Differential { shapes_n, focus_nodes_n, passed, n, failed } => {
let mut g = skipped_gate(
"shapes",
&format!(
"W3C SHACL-Core differential: {passed} of {n} vendored case(s) pass; failed: {} ({shapes_n} shape(s), {focus_nodes_n} focus node(s)) — the validator disagrees with the standard, so no corpus verdict",
failed.join(" | ")
),
);
g.verdict = Verdict::Unknown(crate::ontology::verdict::Reason::Differential);
(g, Vec::new())
}
}
}
fn skipped_gate(name: &str, reason: &str) -> GateResult {
GateResult {
name: name.into(),
passed: false,
skipped: true,
verdict: Verdict::from_gate(false, true),
duration_ms: 0,
detail: GateDetail::Skipped {
reason: reason.into(),
},
extra: None,
}
}
fn apply_suppressions(findings: &mut [LintFinding], config: &LintConfig) {
for f in findings.iter_mut() {
if config.suppressed_rules.iter().any(|r| r == &f.rule_id) {
f.suppressed = true;
f.suppression_reason = Some("Suppressed by --suppress-rule".into());
}
if let Some(ref stem) = f.contract_stem {
if config.suppressed_findings.iter().any(|s| s == stem) {
f.suppressed = true;
f.suppression_reason = Some("Suppressed by --suppress".into());
}
}
if config.suppressed_files.iter().any(|p| f.file.contains(p)) {
f.suppressed = true;
f.suppression_reason = Some("Suppressed by --suppress-file".into());
}
}
}
fn pv_state_dir(contract_dir: &Path) -> std::path::PathBuf {
contract_dir.parent().unwrap_or(contract_dir).join(".pv")
}
fn mark_new_findings(findings: &mut [LintFinding], contract_dir: &Path) {
let state_dir = pv_state_dir(contract_dir);
let previous_path = state_dir.join("lint-previous.json");
let previous: HashSet<String> = std::fs::read_to_string(&previous_path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default();
let mut current = HashSet::new();
for f in findings.iter_mut() {
let fp = f.fingerprint();
if !previous.contains(&fp) {
f.is_new = true;
}
current.insert(fp);
}
if let Err(e) = std::fs::create_dir_all(&state_dir) {
eprintln!("pv lint: cannot create {}: {e}", state_dir.display());
return;
}
if let Ok(json) = serde_json::to_string(¤t) {
let _ = std::fs::write(&previous_path, json);
}
}
fn apply_severity_overrides(findings: &mut [LintFinding], config: &LintConfig) {
for f in findings.iter_mut() {
if let Some(&sev) = config.severity_overrides.get(&f.rule_id) {
f.severity = sev;
}
}
if config.strict {
for f in findings.iter_mut() {
if f.severity == RuleSeverity::Warning {
f.severity = RuleSeverity::Error;
}
}
}
}
#[cfg(test)]
#[path = "mod_tests.rs"]
mod tests;