use std::path::Path;
use vitrify_core::CoreError;
use vitrify_core::audit;
use vitrify_core::config::Overrides;
use vitrify_core::contract::State;
use vitrify_core::graph::{self, Entry, Node, UnitGraph, render_path};
use vitrify_core::model::{self, Instance, SddHandle, Shape, is_empty_marker};
use vitrify_core::{changelog, contract, journal, questions, readset, tdd};
use crate::out::{J, emit};
pub const CHECKS: &str = "L1–L12";
pub struct Finding {
pub check: &'static str,
pub subject: String,
pub cause: String,
pub remediation: String,
}
fn f(
check: &'static str,
subject: impl Into<String>,
cause: impl Into<String>,
remediation: impl Into<String>,
) -> Finding {
Finding {
check,
subject: subject.into(),
cause: cause.into(),
remediation: remediation.into(),
}
}
impl Finding {
pub fn line(&self) -> String {
format!(
"[{}] {}: {} → {}",
self.check, self.subject, self.cause, self.remediation
)
}
}
pub struct Flags {
pub sdd: Option<String>,
pub all: bool,
pub built: bool,
pub json: bool,
}
fn findings_json(findings: &[Finding]) -> Vec<(String, J)> {
let jf: Vec<J> = findings
.iter()
.map(|x| {
J::Obj(vec![
("check".into(), J::s(x.check)),
("subject".into(), J::s(x.subject.clone())),
("cause".into(), J::s(x.cause.clone())),
("remediation".into(), J::s(x.remediation.clone())),
])
})
.collect();
vec![
("clean".into(), J::Bool(findings.is_empty())),
("findings".into(), J::Arr(jf)),
]
}
pub fn run(flags: Flags) -> Result<u8, CoreError> {
let root = super::repo_root();
let sdds = super::targets(&root, flags.sdd.as_deref(), flags.all, &super::cwd())?;
let budget = super::load_config(&Overrides::default())?
.lint
.readset_budget_bytes;
let lint = |sdd: &SddHandle| -> Result<Vec<Finding>, CoreError> {
let inst = Instance::load(sdd.clone())?;
if flags.built {
pre_run_findings(&inst, &root, budget)
} else {
check_all(&inst, &root, budget)
}
};
if !flags.all {
let findings = lint(&sdds[0])?;
if flags.json {
emit(&J::Obj(findings_json(&findings)).dumps_indent2());
} else {
if findings.is_empty() {
emit(&format!("lint clean ({CHECKS})"));
}
for x in &findings {
emit(&x.line());
}
}
return Ok(if findings.is_empty() { 0 } else { 2 });
}
let mut reports: Vec<J> = Vec::new();
let code = super::each(&root, &sdds, |sdd| {
let findings = lint(sdd)?;
let name = super::label(sdd, &root);
if flags.json {
let mut pairs = vec![
("sdd".into(), J::s(name)),
("root".into(), J::s(super::shown(&root, &sdd.root))),
];
pairs.extend(findings_json(&findings));
reports.push(J::Obj(pairs));
} else {
if findings.is_empty() {
emit(&format!("{name}: lint clean ({CHECKS})"));
}
for x in &findings {
emit(&format!("{name}: {}", x.line()));
}
}
Ok(if findings.is_empty() { 0 } else { 2 })
});
if flags.json {
emit(&J::Arr(reports).dumps_indent2());
}
Ok(code)
}
fn listed(ids: &[String]) -> String {
match ids {
[] => String::new(),
[one] => one.clone(),
[init @ .., last] => format!("{} and {last}", init.join(", ")),
}
}
pub fn check_all(inst: &Instance, root: &Path, budget: i64) -> Result<Vec<Finding>, CoreError> {
let mut findings: Vec<Finding> = Vec::new();
let units = &inst.units;
let unit_ids: Vec<&str> = units.iter().map(|u| u.unit.as_str()).collect();
let at = super::shown(root, &inst.root);
let handle = inst.handle();
for ue in graph::classify(inst)? {
for (d, entry) in &ue.entries {
if let Entry::Invalid(p) = entry {
findings.push(f(
"L1",
ue.unit.clone(),
p.cause(&ue.unit, d),
p.remediation(&ue.unit, d),
));
}
}
}
if let Some(cycle) = find_cycle(units) {
findings.push(f(
"L1",
cycle.join(" → "),
"depends_on graph contains a cycle",
"remove one of the cycle's depends_on edges",
));
}
let owns: Vec<(String, Vec<String>)> = units
.iter()
.map(|u| (u.unit.clone(), u.meta.get_list("owns")))
.collect();
for (i, (ua, gsa)) in owns.iter().enumerate() {
for (ub, gsb) in owns.iter().skip(i + 1) {
for ga in gsa {
for gb in gsb {
if globs_overlap(ga, gb) {
findings.push(f(
"L2",
format!("{ua}/{ub}"),
format!("owns globs can match a common path: `{ga}` vs `{gb}`"),
format!("narrow {ua}'s or {ub}'s owns so no path matches both"),
));
}
}
}
}
}
let core_text = model::read_text(&inst.file)?;
let unmanaged = unmanaged_globs(&core_text);
let scope_files = super::governed_files(root, inst)
.unwrap_or_else(|| audit::walk_scope(root, &inst.meta.get_list("scope_roots")));
for file in &scope_files {
let owned = owns
.iter()
.any(|(_, gs)| gs.iter().any(|g| audit::matches(file, g)))
|| unmanaged.iter().any(|g| audit::matches(file, g));
if !owned {
findings.push(f(
"L2",
file.clone(),
"file under scope_roots is neither owned nor unmanaged",
"add a glob that matches it to one unit's owns, or list it under unmanaged: in System Layout & Ownership",
));
}
}
for u in units {
let group = u.meta.get_str_or("parallel_group", "");
if is_empty_marker(group.trim()) {
continue;
}
for d in u.meta.get_list("depends_on") {
if let Some(dep) = units.iter().find(|x| x.unit == d)
&& dep.meta.get_str_or("parallel_group", "") == group
{
findings.push(f(
"L3",
u.unit.clone(),
format!("depends on {d} inside its own parallel group {group}"),
format!(
"move {} or {d} to another parallel_group, or drop the dependency",
u.unit
),
));
}
}
}
for u in units {
for cid in u.meta.get_list("consumes") {
if model::contract_text(&handle, &cid)?.is_none() {
findings.push(f(
"L4",
u.unit.clone(),
format!("consumes unresolvable contract {cid}"),
format!(
"write contract {cid}, or remove it from {}'s consumes",
u.unit
),
));
}
}
}
for (cid, cref) in audit::contract_refs(inst)? {
let mut refs: Vec<&String> = Vec::new();
if let Some(p) = &cref.producer {
refs.push(p);
}
refs.extend(cref.consumers.iter());
for uid in refs {
if !unit_ids.contains(&uid.as_str()) {
findings.push(f(
"L4",
cid.clone(),
format!("references nonexistent unit {uid}"),
format!(
"make {cid}'s Producer and Consumers lines name only units the SDD has"
),
));
}
}
}
let contracts = contract::list(&handle)?;
for c in &contracts {
if let Some((header_only, consumes_only)) = contract::consumer_mismatch(c, units) {
let mut parts: Vec<String> = Vec::new();
if !header_only.is_empty() {
parts.push(format!(
"names {}, which {} not list it in `consumes`",
listed(&header_only),
if header_only.len() == 1 { "does" } else { "do" }
));
}
if !consumes_only.is_empty() {
parts.push(format!(
"omits {}, which {} it",
listed(&consumes_only),
if consumes_only.len() == 1 {
"consumes"
} else {
"consume"
}
));
}
findings.push(f(
"L4",
c.id.clone(),
format!("its Consumers line {}", parts.join(" and ")),
format!(
"make {}'s Consumers line name exactly the units that list it in consumes",
c.id
),
));
}
}
let peers = super::peers(root);
for u in units {
match readset::resolve(inst, &u.unit, &peers) {
Ok(items) => {
let total: i64 = items.iter().map(|i| i.bytes as i64).sum();
if total > budget {
findings.push(f(
"L5",
u.unit.clone(),
format!("reading set totals {total} bytes, over budget {budget}"),
format!(
"split {} or shrink what it reads (vitrify readset --unit {} --sdd {at} lists it)",
u.unit, u.unit
),
));
}
}
Err(e)
if e.to_string().lines().all(|l| {
l.contains("consumed contract missing") || l.contains(readset::PIN_PROBLEM)
}) => {}
Err(e) => findings.push(f(
"L5",
u.unit.clone(),
format!(
"reading set does not resolve: {}",
e.to_string().replace('\n', "; ")
),
format!(
"make every path {}'s reading set names exist (vitrify readset --unit {} --sdd {at})",
u.unit, u.unit
),
)),
}
}
for u in units {
let text = unit_text(inst, u)?;
let declared = declared_requirements(&text, &u.unit);
let covers = covers_tokens(&text);
let covers_all = covers.iter().any(|t| t.eq_ignore_ascii_case("all"));
for r in &declared {
let short = format!("R{r}");
let full = format!("{}.R{r}", u.unit);
if !covers_all && !covers.contains(&short) && !covers.contains(&full) {
findings.push(f(
"L6",
u.unit.clone(),
format!("requirement {full} not covered by any Verification row"),
format!(
"add a Verification row whose Covers column names {short}, or remove {full}"
),
));
}
}
}
let built = journal::read(&handle)?.1;
let l7 = |unit: &str, cause: &str, clear: &str| {
let remediation = if built {
format!("the SDD is being built: vitrify lint --built --sdd {at}")
} else {
format!("{clear} in {unit}'s entry: the build sets it")
};
f("L7", unit, cause, remediation)
};
for u in units {
let status = u.meta.get_str_or("status", "Draft");
if status != "Draft" && status != "Ready" {
findings.push(l7(
&u.unit,
&format!("status {status} is beyond Ready before the build"),
"set status to Draft or Ready",
));
}
if !is_empty_marker(u.meta.get_str_or("claimed_by", "").trim()) {
findings.push(l7(
&u.unit,
"claimed_by set before the build",
"set claimed_by to —",
));
}
if !is_empty_marker(u.meta.get_str_or("claim_expires", "").trim()) {
findings.push(l7(
&u.unit,
"claim_expires set before the build",
"set claim_expires to —",
));
}
if u.meta.get_map("trace").is_some_and(|m| !m.is_empty()) {
findings.push(l7(
&u.unit,
"trace recorded before the build",
"remove the trace map",
));
}
}
let asked: Vec<String> = questions::of(&handle)?.into_iter().map(|q| q.id).collect();
for u in units {
let text = unit_text(inst, u)?;
let markers = questions::markers(&text);
if u.meta.get_str_or("status", "Draft") == "Ready" && text.contains("OPEN(Q-") {
let named = if markers.is_empty() {
"the question".to_string()
} else {
listed(&markers)
};
findings.push(f(
"L8",
u.unit.clone(),
"OPEN(Q-###) marker inside a Ready unit's region",
format!(
"resolve {named} in the Decision Log and remove the marker, or set {} back to Draft",
u.unit
),
));
}
for q in markers {
if !asked.contains(&q) {
findings.push(f(
"L8",
u.unit.clone(),
format!("OPEN({q}) names a question with no entry in the Decision Log"),
format!("add {q} to the Decision Log, or correct the marker"),
));
}
}
}
for (subject, cause, remediation) in pin_findings(inst, &peers, &at)? {
findings.push(f("L9", subject, cause, remediation));
}
let entries = changelog::entries(&handle)?;
let last = entries.iter().max_by_key(|e| e.number);
for (subject, cause) in changelog::findings(
&entries,
inst.meta.get_str_or("version", ""),
inst.meta.get_str_or("updated", ""),
) {
let remediation = match (subject.as_str(), last) {
("version" | "updated", Some(last)) => format!(
"set version and updated in the front matter to {}'s ({} and {})",
last.id,
last.to.as_deref().unwrap_or("its target version"),
last.date.as_deref().unwrap_or("its date")
),
_ => format!(
"give {subject} the next unused id: change entries are appended in id order, newest last"
),
};
findings.push(f("L10", subject, cause, remediation));
}
for (id, hashes) in changelog::misplaced(&handle)? {
let (cause, remediation) = changelog::misplaced_finding(&id, hashes);
findings.push(f("L10", id, cause, remediation));
}
for (subject, cause) in
tdd::findings(&inst.meta, &inst.file, root, |p| model::read_text(p).ok())
{
let remediation = match subject.as_str() {
"tdd_version" => {
"re-pin tdd_version to the TDD's Revision in a change entry (vitrify brief change)"
}
_ if cause.contains("names no file") => {
"set tdd: to the TDD's path, from the SDD's directory or the repository root"
}
_ => "approve the TDD (Status: Approved), or keep the SDD Draft until it is",
};
findings.push(f("L11", subject, cause, remediation));
}
if graph::classify(inst)?
.iter()
.any(|ue| ue.entries.iter().any(|(_, e)| matches!(e, Entry::Pin(_))))
{
let unit_graph = super::repo_graph(root, Some(inst))?;
findings.extend(graph_findings(inst, &unit_graph));
}
Ok(findings)
}
pub fn graph_findings(inst: &Instance, unit_graph: &UnitGraph) -> Vec<Finding> {
let slug = graph::slug_of(inst);
let mut findings: Vec<Finding> = unit_graph
.problems()
.iter()
.filter(|p| p.slug == slug && p.check() == "L12")
.map(|p| f("L12", p.unit.clone(), p.cause(), p.remediation()))
.collect();
let Err(cycles) = unit_graph.resolve() else {
return findings;
};
let crosses = |path: &[Node]| path.iter().any(|n| n.slug != path[0].slug);
let through_pins: Vec<&Vec<Node>> = cycles
.cycles
.iter()
.filter(|path| crosses(path) || path.windows(2).any(|w| pin_only(unit_graph, &w[0], &w[1])))
.collect();
for path in &through_pins {
if path.iter().any(|n| n.slug == slug) {
let cause = if crosses(path) {
"depends_on graph across SDDs contains a cycle through pins"
} else {
"depends_on graph contains a cycle through a pin of this SDD's own contract"
};
findings.push(f(
"L12",
render_path(path),
cause,
"remove one of the cycle's depends_on edges — a pin a unit only reads stays in consumes alone",
));
}
}
for n in cycles.waiting.iter().filter(|n| n.slug == slug) {
if let Some(path) = through_pins
.iter()
.find(|path| waits_on(unit_graph, n, path))
{
findings.push(f(
"L12",
n.unit.clone(),
format!("waits behind the cycle {}", render_path(path)),
"break the cycle its own L12 finding names",
));
}
}
findings
}
fn pin_only(unit_graph: &UnitGraph, from: &Node, to: &Node) -> bool {
let edges: Vec<_> = unit_graph
.edges()
.iter()
.filter(|e| &e.from == from && &e.to == to)
.collect();
!edges.is_empty() && edges.iter().all(|e| e.via.is_some())
}
fn waits_on(unit_graph: &UnitGraph, n: &Node, path: &[Node]) -> bool {
let mut seen: Vec<&Node> = Vec::new();
let mut stack: Vec<&Node> = vec![n];
while let Some(x) = stack.pop() {
for d in unit_graph.dependencies(x) {
if path.contains(d) {
return true;
}
if !seen.contains(&d) {
seen.push(d);
stack.push(d);
}
}
}
false
}
pub fn pin_findings(
inst: &Instance,
peers: &contract::Peers,
at: &str,
) -> Result<Vec<(String, String, String)>, CoreError> {
Ok(contract::examine(inst, peers)?
.into_iter()
.filter(|ext| ext.state.is_finding())
.map(|ext| {
let remediation = match ext.state {
State::Stale => format!("vitrify pin sync --sdd {at}"),
State::MissingAuthority => format!(
"make {}'s Pin: line name an SDD and contract this repository has (vitrify pin list --all)",
ext.id
),
_ => format!(
"correct {}'s header as its cause says (vitrify schema show, Interface Contracts)",
ext.id
),
};
(ext.id, ext.detail, remediation)
})
.collect())
}
pub fn pre_run_findings(
inst: &Instance,
root: &Path,
budget: i64,
) -> Result<Vec<Finding>, CoreError> {
Ok(check_all(inst, root, budget)?
.into_iter()
.filter(|x| x.check != "L7")
.collect())
}
fn find_cycle(units: &[model::Unit]) -> Option<Vec<String>> {
fn visit(
uid: &str,
units: &[model::Unit],
path: &mut Vec<String>,
done: &mut Vec<String>,
) -> Option<Vec<String>> {
if done.iter().any(|d| d == uid) {
return None;
}
if let Some(pos) = path.iter().position(|p| p == uid) {
let mut cycle = path[pos..].to_vec();
cycle.push(uid.to_string());
return Some(cycle);
}
path.push(uid.to_string());
if let Some(u) = units.iter().find(|u| u.unit == uid) {
for d in u.meta.get_list("depends_on") {
if let Some(c) = visit(&d, units, path, done) {
return Some(c);
}
}
}
path.pop();
done.push(uid.to_string());
None
}
let mut done = Vec::new();
for u in units {
if let Some(c) = visit(&u.unit, units, &mut Vec::new(), &mut done) {
return Some(c);
}
}
None
}
fn globs_overlap(a: &str, b: &str) -> bool {
let lit = |g: &str| -> (String, bool) {
match g.find(['*', '?', '[']) {
Some(i) => (g[..i].to_string(), true),
None => (g.trim_end_matches('/').to_string(), false),
}
};
let (pa, wild_a) = lit(a);
let (pb, wild_b) = lit(b);
if !wild_a && !wild_b {
return pa == pb || pa.starts_with(&format!("{pb}/")) || pb.starts_with(&format!("{pa}/"));
}
pa.starts_with(&pb) || pb.starts_with(&pa)
}
fn unmanaged_globs(core_text: &str) -> Vec<String> {
let sec = model::section(core_text, "System Layout & Ownership");
let mut out = Vec::new();
let mut in_block = false;
for line in sec.lines() {
if !in_block {
if line.trim() == "unmanaged:" {
in_block = true;
}
continue;
}
let trimmed = line.trim_start();
if let Some(item) = trimmed.strip_prefix("- ")
&& line.starts_with(|c: char| c.is_whitespace())
{
out.push(item.trim().to_string());
} else {
break;
}
}
out
}
fn unit_text(inst: &Instance, u: &model::Unit) -> Result<String, CoreError> {
match inst.shape {
Shape::Bundle => model::read_text(&u.file),
Shape::Single => {
let text = model::read_text(&inst.file)?;
Ok(model::find_unit_span(&text, &u.unit)
.map(|(s, e)| text[s..e].to_string())
.unwrap_or_default())
}
}
}
fn declared_requirements(text: &str, unit_id: &str) -> Vec<u32> {
let needle = format!("{unit_id}.R");
let mut out: Vec<u32> = Vec::new();
let mut search = text;
while let Some(i) = search.find(&needle) {
let rest = &search[i + needle.len()..];
let digits: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
if let Ok(n) = digits.parse::<u32>()
&& !out.contains(&n)
{
out.push(n);
}
search = &search[i + needle.len()..];
}
out
}
fn covers_tokens(text: &str) -> Vec<String> {
let mut tokens = Vec::new();
let mut in_section = false;
for line in text.lines() {
let is_heading = line.starts_with("##");
if is_heading {
in_section = line
.trim_start_matches('#')
.trim()
.eq_ignore_ascii_case("Verification");
continue;
}
if !in_section || !line.trim_start().starts_with('|') {
continue;
}
let stripped = line.trim().trim_matches('|');
let cells: Vec<&str> = stripped.split('|').map(str::trim).collect();
if cells.is_empty() {
continue;
}
let first = cells[0];
if first.chars().all(|c| c == '-' || c == ' ' || c == ':')
|| first.eq_ignore_ascii_case("covers")
{
continue;
}
for tok in first.split([',', '·']) {
let t = tok.trim().trim_matches('*').trim();
if t.is_empty() {
continue;
}
if let Some((lo, hi)) = parse_range(t) {
for n in lo..=hi {
tokens.push(format!("R{n}"));
}
} else {
tokens.push(t.to_string());
}
}
}
tokens
}
fn parse_range(token: &str) -> Option<(u32, u32)> {
for sep in ['\u{2013}', '-'] {
if let Some((a, b)) = token.split_once(sep) {
let lo = a.trim().strip_prefix('R')?.parse().ok()?;
let hi = b.trim().strip_prefix('R')?.parse().ok()?;
return Some((lo, hi));
}
}
None
}