use crate::cli::{
active_policy_pair_snapshot, check_json_payload, fail_usage, frontier_dir_for_source,
load_frontier_or_fail, print_json, print_signal_summary, proposal_parity_blocks,
proposal_parity_summary, repository_context_check, scan_for_sensitive_paths,
};
use serde_json::Value;
use std::path::Path;
use vela_edge::conformance;
use vela_edge::lint;
use vela_edge::signals;
use vela_edge::validate;
use vela_protocol::cli_style as style;
use vela_protocol::events;
pub(crate) fn cmd_check(
source: Option<&Path>,
schema: bool,
stats: bool,
conformance_flag: bool,
conformance_dir: &Path,
all: bool,
schema_only: bool,
strict: bool,
fix: bool,
json_output: bool,
) {
if json_output {
let Some(src) = source else {
fail_usage("--json requires a frontier source");
};
let payload = check_json_payload(src, schema_only, strict);
print_json(&payload);
if payload.get("ok").and_then(Value::as_bool) != Some(true) {
std::process::exit(1);
}
return;
}
if strict && let Some(src) = source {
let hits = scan_for_sensitive_paths(src);
if !hits.is_empty() {
eprintln!(
"{} secret-audit: {} sensitive path(s) found under {}",
style::err_prefix(),
hits.len(),
src.display()
);
for hit in &hits {
eprintln!(" - {}", hit.display());
}
eprintln!(
" hint: add `keys/` and `*.key` to .gitignore so these never reach a public repo (see THREAT_MODEL.md A17)"
);
std::process::exit(1);
}
}
let run_all = all || (!schema && !stats && !conformance_flag && !schema_only);
let repository_context = if schema_only {
None
} else {
source.map(repository_context_check)
};
let repository_context_invalid = repository_context
.as_ref()
.is_some_and(|check| check.get("valid").and_then(Value::as_bool) == Some(false));
if let Some(check) = repository_context.as_ref()
&& check.get("status").and_then(Value::as_str) != Some("not_applicable")
{
let status = check
.get("status")
.and_then(Value::as_str)
.unwrap_or("fail");
let generation = check
.get("generation")
.and_then(Value::as_str)
.unwrap_or("unknown");
println!("repository context: {status} ({generation})");
if let Some(code) = check.get("code").and_then(Value::as_str) {
println!("repository context code: {code}");
}
if let Some(error) = check.get("error").and_then(Value::as_str) {
println!("repository context detail: {error}");
}
}
let active_policy_error = if schema_only {
None
} else if let Some(src) = source {
let snapshot = active_policy_pair_snapshot(src);
match vela_protocol::repo::load_from_path(src) {
Ok(frontier) => {
let assessment = vela_protocol::proposals::policy_accept::assess_policy_readiness(
&frontier,
snapshot.as_ref().map_err(String::as_str),
&chrono::Utc::now().to_rfc3339(),
);
println!(
"active policy: {} · Permit {}{}",
assessment.state().as_str(),
assessment.permit_readiness().as_str(),
if assessment.reason_codes().is_empty() {
String::new()
} else {
format!(" ({})", assessment.reason_codes().join(", "))
}
);
if let Some(detail) = assessment.detail() {
println!("active policy detail: {detail}");
}
if assessment.permit_readiness()
== vela_protocol::proposals::policy_accept::PermitReadiness::Blocked
{
Some(
assessment
.detail()
.unwrap_or("policy readiness assessment is blocked")
.to_string(),
)
} else {
None
}
}
Err(_) => snapshot.err(),
}
} else {
None
};
if run_all || schema || schema_only {
let Some(src) = source else {
crate::ui::fail_with(
crate::ui::ErrorKind::Usage,
"check requires a frontier source",
Some("point it at a frontier: `vela check .` (or any repo/frontier.json path)"),
)
};
validate::run(src);
}
if !schema_only && (run_all || stats) {
let Some(src) = source else {
fail_usage("--stats requires a frontier source");
};
let frontier = load_frontier_or_fail(src);
let report = lint::lint(&frontier, None, None);
lint::print_report(&report);
let replay_report = events::replay_report(&frontier);
println!("event replay: {}", replay_report.status);
if !replay_report.conflicts.is_empty() {
for conflict in &replay_report.conflicts {
println!(" - {conflict}");
}
}
let replay_verification = vela_protocol::reducer::verify_replay(&frontier);
println!("replay verification: {}", replay_verification.note);
if !replay_verification.ok {
for diff in replay_verification.diffs.iter().take(20) {
println!(" - {diff}");
}
}
let parity_conflicts = vela_protocol::proposals::verify_proposal_decision_parity(&frontier);
println!(
"review-decision parity: {}",
repository_context.as_ref().map_or_else(
|| format!("{} conflict(s)", parity_conflicts.len()),
|context| proposal_parity_summary(context, parity_conflicts.len()),
)
);
for conflict in parity_conflicts.iter().take(20) {
println!(" - {conflict}");
}
let withdrawal_conflicts = vela_protocol::proposals::verify_proposal_withdrawals(
frontier_dir_for_source(src),
&frontier,
);
println!(
"proposal-withdrawal verification: {}",
if withdrawal_conflicts.is_empty() {
"ok".to_string()
} else {
format!("{} conflict(s)", withdrawal_conflicts.len())
}
);
for conflict in withdrawal_conflicts.iter().take(20) {
println!(" - {conflict}");
}
let policy_lane_errors = if strict {
vela_protocol::proposals::policy_accept::verify_policy_lane_events(
&frontier,
frontier_dir_for_source(src),
)
} else {
Vec::new()
};
if strict {
println!(
"policy-lane replay: {}",
if policy_lane_errors.is_empty() {
"ok".to_string()
} else {
format!("{} conflict(s)", policy_lane_errors.len())
}
);
for conflict in policy_lane_errors.iter().take(20) {
println!(" - {conflict}");
}
}
let activity_leaks = vela_protocol::activity::activity_ids_in_lineage(
&frontier.findings,
&frontier.verifier_attachments,
);
println!(
"activity/state boundary: {}",
if activity_leaks.is_empty() {
"ok".to_string()
} else {
format!("{} leak(s)", activity_leaks.len())
}
);
for (holder, atom) in activity_leaks.iter().take(20) {
println!(" - {holder} references activity id {atom} in lineage");
}
let keyed_reviewers: std::collections::HashSet<&str> = frontier
.actors
.iter()
.filter(|a| a.id.starts_with("reviewer:") && !a.public_key.trim().is_empty())
.map(|a| a.id.as_str())
.collect();
let unsigned_keyed_accepts = frontier
.events
.iter()
.filter(|e| {
e.signature.is_none()
&& keyed_reviewers.contains(e.actor.id.as_str())
&& (e.kind.as_str().ends_with(".reviewed") || e.kind == "finding.asserted")
})
.count();
let mut seen_hashes: std::collections::HashMap<String, &str> =
std::collections::HashMap::new();
let mut collisions = Vec::new();
for f in frontier.findings.iter().filter(|f| !f.flags.superseded) {
let h = vela_protocol::canonical::normalized_statement_hash(&f.assertion.text);
if let Some(prev) = seen_hashes.get(&h) {
collisions.push(format!("{} duplicates {}", f.id, prev));
} else {
seen_hashes.insert(h, f.id.as_str());
}
}
if !collisions.is_empty() {
println!("prior-art collisions: {}", collisions.len());
for c in collisions.iter().take(10) {
println!(" - {c}");
}
}
if unsigned_keyed_accepts > 0 {
println!(
"key custody: {unsigned_keyed_accepts} accept-class event(s) by keyed reviewers carry no signature (history predating key registration; new accepts require --key)"
);
}
let signal_report =
signals::analyze_at(&frontier, &[], source.map(frontier_dir_for_source));
print_signal_summary(&signal_report, strict);
if !replay_report.ok
|| !replay_verification.ok
|| repository_context
.as_ref()
.map_or(!parity_conflicts.is_empty(), |context| {
proposal_parity_blocks(context, parity_conflicts.len())
})
|| active_policy_error.is_some()
|| !policy_lane_errors.is_empty()
|| !activity_leaks.is_empty()
|| (strict && repository_context_invalid)
|| (strict
&& (!signal_report.review_queue.is_empty()
|| signal_report.proof_readiness.status != "ready"))
{
std::process::exit(1);
}
}
if run_all || conformance_flag {
if conformance_flag || conformance_dir.is_dir() {
conformance::run(conformance_dir);
} else {
eprintln!(
" conformance: skipped ({} not present; pass --conformance-dir <path> to point at the source repo's conformance directory)",
conformance_dir.display()
);
}
}
if active_policy_error.is_some() || (strict && repository_context_invalid) {
std::process::exit(1);
}
let _ = fix;
}