use super::*;
#[allow(clippy::too_many_arguments)]
pub fn scan_for_sensitive_paths(root: &Path) -> Vec<PathBuf> {
let mut hits: Vec<PathBuf> = Vec::new();
let skip_dirs: &[&str] = &[".git", "target", "node_modules", "dist", "build"];
let bad_exts: &[&str] = &["key", "pem", "p12", "pfx"];
let bad_substrings: &[&str] = &["private", "secret", "credential"];
let mut stack: Vec<PathBuf> = vec![root.to_path_buf()];
while let Some(dir) = stack.pop() {
let Ok(entries) = std::fs::read_dir(&dir) else {
continue;
};
for entry in entries.flatten() {
let path = entry.path();
let name_os = path.file_name();
let Some(name) = name_os.and_then(|n| n.to_str()) else {
continue;
};
let lower = name.to_lowercase();
if path.is_dir() {
if skip_dirs.contains(&name) {
continue;
}
stack.push(path);
continue;
}
if lower.ends_with(".pub") || lower.ends_with(".pubkey") {
continue;
}
if lower == "public.key" {
continue;
}
let ext = path
.extension()
.and_then(|e| e.to_str())
.map(str::to_lowercase)
.unwrap_or_default();
let mut hit = false;
if bad_exts.iter().any(|x| ext == *x) {
hit = true;
}
if bad_substrings.iter().any(|s| lower.contains(s)) {
hit = true;
}
if hit {
hits.push(path);
}
}
}
hits.sort();
hits
}
pub(crate) fn frontier_dir_for_source(source: &Path) -> &Path {
if source.is_dir() {
source
} else {
source
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."))
}
}
pub(crate) fn active_policy_pair_snapshot(
source: &Path,
) -> Result<vela_protocol::acceptance_policy::ActivePolicySnapshot, String> {
vela_protocol::acceptance_policy::load_active_policy_snapshot(frontier_dir_for_source(source))
}
pub(crate) fn check_json_payload(src: &Path, schema_only: bool, strict: bool) -> Value {
let report = validate::validate(src);
let loaded = repo::load_from_path(src).ok();
let (method_report, graph_report) = if schema_only {
(None, None)
} else if let Some(frontier) = loaded.as_ref() {
(
Some(lint::lint(frontier, None, None)),
Some(lint::lint_frontier(frontier)),
)
} else {
(None, None)
};
let source_hash = hash_path(src).unwrap_or_else(|_| "unavailable".to_string());
let mut diagnostics = Vec::new();
diagnostics.extend(report.errors.iter().map(|e| {
json!({
"severity": "error",
"rule_id": "schema",
"finding_id": null,
"file": &e.file,
"field_path": null,
"message": &e.error,
"suggestion": schema_error_suggestion(&e.error),
"fixable": schema_error_fix(&e.error),
"normalize_action": schema_error_action(&e.error),
})
}));
for (check_id, lint_report) in [
("methodology", method_report.as_ref()),
("frontier_graph", graph_report.as_ref()),
] {
if let Some(lint_report) = lint_report {
diagnostics.extend(lint_report.diagnostics.iter().map(|d| {
json!({
"severity": d.severity.to_string(),
"rule_id": &d.rule_id,
"check": check_id,
"finding_id": &d.finding_id,
"field_path": null,
"message": &d.message,
"suggestion": &d.suggestion,
"fixable": false,
"normalize_action": null,
})
}));
}
}
let method_errors = method_report.as_ref().map_or(0, |r| r.errors);
let method_warnings = method_report.as_ref().map_or(0, |r| r.warnings);
let method_infos = method_report.as_ref().map_or(0, |r| r.infos);
let graph_errors = graph_report.as_ref().map_or(0, |r| r.errors);
let graph_warnings = graph_report.as_ref().map_or(0, |r| r.warnings);
let graph_infos = graph_report.as_ref().map_or(0, |r| r.infos);
let replay_report = loaded.as_ref().map(events::replay_report);
let state_integrity_report = if schema_only {
loaded.as_ref().map(state_integrity::analyze)
} else {
state_integrity::analyze_path(src).ok()
};
if let Some(replay) = replay_report.as_ref()
&& !replay.ok
{
diagnostics.extend(replay.conflicts.iter().map(|conflict| {
json!({
"severity": "error",
"rule_id": "event_replay",
"check": "events",
"finding_id": null,
"field_path": null,
"message": conflict,
"suggestion": "Inspect canonical state events and repair the frontier event log before proof export.",
"fixable": false,
"normalize_action": null,
})
}));
}
let parity_conflicts: Vec<String> = loaded
.as_ref()
.map(vela_protocol::proposals::verify_proposal_decision_parity)
.unwrap_or_default();
if !parity_conflicts.is_empty() {
diagnostics.extend(parity_conflicts.iter().map(|conflict| {
json!({
"severity": "error",
"rule_id": "review_decision_parity",
"check": "proposals",
"finding_id": null,
"field_path": null,
"message": conflict,
"suggestion": "Every decided proposal must have a signed review.* event (or, for accepts, its domain event). Re-issue the decision through `vela sign`.",
"fixable": false,
"normalize_action": null,
})
}));
}
let withdrawal_conflicts: Vec<String> = loaded
.as_ref()
.map(|frontier| {
vela_protocol::proposals::verify_proposal_withdrawals(
frontier_dir_for_source(src),
frontier,
)
})
.unwrap_or_default();
diagnostics.extend(withdrawal_conflicts.iter().map(|conflict| {
json!({
"severity": "error",
"rule_id": "invalid_proposal_withdrawal",
"check": "proposals",
"finding_id": null,
"field_path": null,
"message": conflict,
"suggestion": "Treat the proposal as pending and restore the exact Receipt-bound signed withdrawal bytes.",
"fixable": false,
"normalize_action": null,
})
}));
let frontier_dir = frontier_dir_for_source(src);
let active_policy_result = if !schema_only && loaded.is_some() {
Some(active_policy_pair_snapshot(src))
} else {
None
};
let observed_at = chrono::Utc::now().to_rfc3339();
let active_policy_assessment =
active_policy_result
.as_ref()
.zip(loaded.as_ref())
.map(|(result, frontier)| {
vela_protocol::proposals::policy_accept::assess_policy_readiness(
frontier,
result.as_ref().map_err(String::as_str),
&observed_at,
)
});
let active_policy_errors = active_policy_result
.as_ref()
.and_then(|result| result.as_ref().err())
.cloned()
.into_iter()
.collect::<Vec<_>>();
let policy_readiness_errors = active_policy_assessment
.as_ref()
.filter(|assessment| {
assessment.state() != vela_protocol::proposals::policy_accept::PolicyState::Broken
&& assessment.permit_readiness()
== vela_protocol::proposals::policy_accept::PermitReadiness::Blocked
})
.map(|assessment| {
assessment
.detail()
.unwrap_or("policy readiness assessment is blocked")
.to_string()
})
.into_iter()
.collect::<Vec<_>>();
diagnostics.extend(active_policy_errors.iter().map(|error| {
json!({
"severity": "error",
"rule_id": "active_policy_integrity",
"check": "active_policy",
"finding_id": null,
"field_path": ".vela/policies/{active.json,active.sig.json}",
"message": error,
"suggestion": "Restore the exact active-policy pair or retire legacy bytes through the signed human governance ceremony; invalid policy bytes never fail open.",
"fixable": false,
"normalize_action": null,
})
}));
diagnostics.extend(policy_readiness_errors.iter().map(|error| {
json!({
"severity": "error",
"rule_id": "policy_head_integrity",
"check": "policy_readiness",
"finding_id": null,
"field_path": "events[governance.policy_head]",
"message": error,
"suggestion": "Repair the signed policy-head chain through the human governance ceremony; malformed authority history never fails open.",
"fixable": false,
"normalize_action": null,
})
}));
let policy_lane_errors = if strict && !schema_only {
loaded
.as_ref()
.map(|frontier| {
vela_protocol::proposals::policy_accept::verify_policy_lane_events(
frontier,
frontier_dir,
)
})
.unwrap_or_default()
} else {
Vec::new()
};
diagnostics.extend(policy_lane_errors.iter().map(|conflict| {
json!({
"severity": "error",
"rule_id": "policy_lane_replay",
"check": "policy_lane",
"finding_id": null,
"field_path": "events[].payload.policy_lane",
"message": conflict,
"suggestion": "Restore the exact retained receipt, review material, signed policy snapshot, and causal policy-lane event; do not hand-edit accepted state.",
"fixable": false,
"normalize_action": null,
})
}));
let activity_leaks: Vec<(String, String)> = loaded
.as_ref()
.map(|f| {
vela_protocol::activity::activity_ids_in_lineage(&f.findings, &f.verifier_attachments)
})
.unwrap_or_default();
diagnostics.extend(activity_leaks.iter().map(|(holder, atom)| {
json!({
"severity": "error",
"rule_id": "activity_state_boundary",
"check": "lineage",
"finding_id": holder,
"field_path": null,
"message": format!(
"{holder} references activity-plane id {atom} in a lineage-bearing position; activity is non-authoritative and cannot enter accepted lineage"
),
"suggestion": "Remove the activity id from the finding link / verifier attachment; reference the trace by content address in the activity plane instead.",
"fixable": false,
"normalize_action": null,
})
}));
let activity_leak_errors = activity_leaks.len();
let event_errors = replay_report
.as_ref()
.map_or(0, |replay| usize::from(!replay.ok))
+ usize::from(!parity_conflicts.is_empty())
+ policy_lane_errors.len();
let state_integrity_errors = state_integrity_report
.as_ref()
.map_or(0, |report| report.structural_errors.len());
let (source_registry, evidence_atoms, conditions, proposal_summary, proof_state) = loaded
.as_ref()
.map(|frontier| {
(
sources::source_summary(frontier),
sources::evidence_summary(frontier),
sources::condition_summary(frontier),
proposals::summary(frontier),
proposals::proof_state_json(&frontier.proof_state),
)
})
.unwrap_or_else(|| {
(
sources::SourceRegistrySummary::default(),
sources::EvidenceAtomSummary::default(),
sources::ConditionSummary::default(),
proposals::ProposalSummary::default(),
Value::Null,
)
});
if let Some(frontier) = loaded.as_ref()
&& !schema_only
{
let projection = sources::derive_projection(frontier);
let existing_sources = frontier
.sources
.iter()
.map(|source| source.id.as_str())
.collect::<std::collections::BTreeSet<_>>();
let existing_atoms = frontier
.evidence_atoms
.iter()
.map(|atom| atom.id.as_str())
.collect::<std::collections::BTreeSet<_>>();
let existing_conditions = frontier
.condition_records
.iter()
.map(|record| record.id.as_str())
.collect::<std::collections::BTreeSet<_>>();
for source in projection
.sources
.iter()
.filter(|source| !existing_sources.contains(source.id.as_str()))
{
diagnostics.push(json!({
"severity": "warning",
"rule_id": "missing_source_record",
"check": "source_registry",
"finding_id": source.finding_ids.first(),
"field_path": "sources",
"message": format!("Source record {} is derivable but not materialized in frontier state.", source.id),
"suggestion": "Run `vela frontier materialize` to regenerate derived views before proof export.",
"fixable": true,
"normalize_action": "materialize_source_record",
}));
}
for atom in projection
.evidence_atoms
.iter()
.filter(|atom| !existing_atoms.contains(atom.id.as_str()))
{
diagnostics.push(json!({
"severity": "warning",
"rule_id": "missing_evidence_atom",
"check": "evidence_atoms",
"finding_id": atom.finding_id,
"field_path": "evidence_atoms",
"message": format!("Evidence atom {} is derivable but not materialized in frontier state.", atom.id),
"suggestion": "Run `vela frontier materialize .` to rebuild evidence atoms before proof export.",
"fixable": true,
"normalize_action": "materialize_evidence_atom",
}));
}
for condition in projection
.condition_records
.iter()
.filter(|condition| !existing_conditions.contains(condition.id.as_str()))
{
diagnostics.push(json!({
"severity": "warning",
"rule_id": "condition_record_missing",
"check": "conditions",
"finding_id": condition.finding_id,
"field_path": "condition_records",
"message": format!("Condition record {} is derivable but not materialized in frontier state.", condition.id),
"suggestion": "Run `vela frontier materialize .` to rebuild condition boundaries before proof export.",
"fixable": true,
"normalize_action": "materialize_condition_record",
}));
}
for proposal in frontier.proposals.iter().filter(|proposal| {
matches!(proposal.status.as_str(), "accepted" | "applied")
&& proposal
.reviewed_by
.as_deref()
.is_none_or(proposals::is_placeholder_reviewer)
}) {
diagnostics.push(json!({
"severity": "error",
"rule_id": "reviewer_identity_missing",
"check": "proposals",
"finding_id": proposal.target.id,
"field_path": "proposals[].reviewed_by",
"message": format!("Accepted or applied proposal {} uses a missing or placeholder reviewer identity.", proposal.id),
"suggestion": "Accept the proposal with a stable named reviewer id before strict proof use.",
"fixable": false,
"normalize_action": null,
}));
}
}
let signal_report = loaded
.as_ref()
.map(|frontier| {
signals::analyze_at(frontier, &diagnostics, Some(frontier_dir_for_source(src)))
})
.unwrap_or_else(empty_signal_report);
let errors = report.errors.len()
+ method_errors
+ graph_errors
+ event_errors
+ state_integrity_errors
+ active_policy_errors.len()
+ policy_readiness_errors.len()
+ activity_leak_errors;
let warnings = method_warnings + graph_warnings + signal_report.proof_readiness.warnings;
let infos = method_infos + graph_infos;
let strict_blockers = signal_report
.signals
.iter()
.filter(|signal| signal.blocks.iter().any(|block| block == "strict_check"))
.count();
let fixable = diagnostics
.iter()
.filter(|d| d.get("fixable").and_then(Value::as_bool).unwrap_or(false))
.count();
let ok = errors == 0 && (!strict || (warnings == 0 && strict_blockers == 0));
json!({
"ok": ok,
"command": "check",
"schema_version": project::VELA_SCHEMA_VERSION,
"source": {
"path": src.display().to_string(),
"hash": format!("sha256:{source_hash}"),
},
"summary": {
"status": if ok { "pass" } else { "fail" },
"checked_findings": report.total_files,
"valid_findings": report.valid,
"invalid_findings": report.invalid,
"errors": errors,
"warnings": warnings,
"info": infos,
"fixable": fixable,
"strict": strict,
"schema_only": schema_only,
},
"checks": [
{
"id": "schema",
"status": if report.invalid == 0 { "pass" } else { "fail" },
"checked": report.total_files,
"failed": report.invalid,
"errors": report.errors.iter().map(|e| json!({
"file": e.file,
"message": e.error,
})).collect::<Vec<_>>(),
},
{
"id": "methodology",
"status": if method_errors == 0 { "pass" } else { "fail" },
"checked": method_report.as_ref().map_or(0, |r| r.findings_checked),
"failed": method_errors,
"warnings": method_warnings,
"info": method_infos,
"skipped": schema_only,
},
{
"id": "frontier_graph",
"status": if graph_errors == 0 { "pass" } else { "fail" },
"checked": graph_report.as_ref().map_or(0, |r| r.findings_checked),
"failed": graph_errors,
"warnings": graph_warnings,
"info": graph_infos,
"skipped": schema_only,
},
{
"id": "signals",
"status": if strict_blockers == 0 { "pass" } else { "fail" },
"checked": signal_report.signals.len(),
"failed": strict_blockers,
"warnings": signal_report.proof_readiness.warnings,
"skipped": loaded.is_none(),
"blockers": signal_report.signals.iter()
.filter(|s| s.blocks.iter().any(|b| b == "strict_check"))
.map(|s| json!({
"id": s.id,
"kind": s.kind,
"severity": s.severity,
"reason": s.reason,
}))
.collect::<Vec<_>>(),
},
{
"id": "events",
"status": if replay_report.as_ref().is_none_or(|replay| replay.ok) { "pass" } else { "fail" },
"checked": replay_report.as_ref().map_or(0, |replay| replay.event_log.count),
"failed": event_errors,
"skipped": schema_only || loaded.is_none(),
},
{
"id": "state_integrity",
"status": if state_integrity_report.as_ref().is_none_or(|report| report.status != "fail") { "pass" } else { "fail" },
"checked": state_integrity_report.as_ref().map_or(0, |report| report.summary.get("events").copied().unwrap_or_default()),
"failed": state_integrity_errors,
"skipped": schema_only || loaded.is_none(),
},
{
"id": "active_policy",
"status": if active_policy_errors.is_empty() { "pass" } else { "fail" },
"checked": usize::from(active_policy_result.is_some()),
"failed": active_policy_errors.len(),
"errors": active_policy_errors,
"state": active_policy_assessment.as_ref().map(|assessment| assessment.state().as_str()),
"permit_readiness": active_policy_assessment.as_ref().map(|assessment| assessment.permit_readiness().as_str()),
"reason_codes": active_policy_assessment.as_ref().map(|assessment| assessment.reason_codes()).unwrap_or_default(),
"skipped": active_policy_result.is_none(),
},
{
"id": "policy_readiness",
"status": if policy_readiness_errors.is_empty() { "pass" } else { "fail" },
"checked": usize::from(active_policy_assessment.is_some()),
"failed": policy_readiness_errors.len(),
"errors": policy_readiness_errors,
"state": active_policy_assessment.as_ref().map(|assessment| assessment.state().as_str()),
"permit_readiness": active_policy_assessment.as_ref().map(|assessment| assessment.permit_readiness().as_str()),
"reason_codes": active_policy_assessment.as_ref().map(|assessment| assessment.reason_codes()).unwrap_or_default(),
"skipped": active_policy_assessment.as_ref().is_none_or(|assessment| assessment.state()
== vela_protocol::proposals::policy_accept::PolicyState::Broken),
},
{
"id": "policy_lane",
"status": if policy_lane_errors.is_empty() { "pass" } else { "fail" },
"checked": loaded.as_ref().map_or(0, |frontier| frontier.events.iter()
.filter(|event| event.payload.get(vela_protocol::proposals::policy_accept::POLICY_LANE_PAYLOAD_KEY).is_some())
.count()),
"failed": policy_lane_errors.len(),
"errors": policy_lane_errors,
"skipped": !strict || schema_only || loaded.is_none(),
}
],
"event_log": replay_report.as_ref().map(|replay| &replay.event_log),
"replay": replay_report,
"state_integrity": state_integrity_report,
"source_registry": source_registry,
"evidence_atoms": evidence_atoms,
"conditions": conditions,
"proposals": proposal_summary,
"proof_state": proof_state,
"diagnostics": diagnostics,
"signals": signal_report.signals,
"review_queue": signal_report.review_queue,
"proof_readiness": signal_report.proof_readiness,
"repair_plan": build_repair_plan(&diagnostics),
})
}
pub(crate) fn save_recorded_proof_state(
frontier: &Path,
loaded: &project::Project,
) -> Result<(), String> {
let repo_dir = if frontier.is_dir() && frontier.join(".vela").is_dir() {
Some(frontier.to_path_buf())
} else if frontier.is_file()
&& frontier.file_name().is_some_and(|n| n == "frontier.json")
&& frontier.parent().is_some_and(|p| p.join(".vela").is_dir())
{
frontier.parent().map(Path::to_path_buf)
} else {
None
};
if let Some(dir) = repo_dir {
return repo::save_to_path(&dir, loaded);
}
if !frontier.is_file() {
return repo::save_to_path(frontier, loaded);
}
let raw = std::fs::read_to_string(frontier)
.map_err(|e| format!("Failed to read frontier '{}': {e}", frontier.display()))?;
let proof_state = serde_json::to_value(&loaded.proof_state)
.map_err(|e| format!("serialize proof_state: {e}"))?;
let stats = serde_json::to_value(&loaded.stats).map_err(|e| format!("serialize stats: {e}"))?;
let updated = replace_top_level_json_field(&raw, "proof_state", &proof_state)
.and_then(|next| replace_top_level_json_field(&next, "stats", &stats))?;
let rendered = if updated.ends_with('\n') {
updated
} else {
format!("{updated}\n")
};
std::fs::write(frontier, rendered)
.map_err(|e| format!("Failed to write frontier '{}': {e}", frontier.display()))
}
pub(crate) fn hash_path(path: &Path) -> Result<String, String> {
let mut hasher = Sha256::new();
if path.is_file() {
let bytes = std::fs::read(path)
.map_err(|e| format!("Failed to read {} for hashing: {e}", path.display()))?;
hasher.update(&bytes);
} else if path.is_dir() {
let mut files = Vec::new();
collect_hash_files(path, path, &mut files)?;
files.sort();
for rel in files {
hasher.update(rel.to_string_lossy().as_bytes());
let bytes = std::fs::read(path.join(&rel))
.map_err(|e| format!("Failed to read {} for hashing: {e}", rel.display()))?;
hasher.update(bytes);
}
} else {
return Err(format!("Cannot hash missing path {}", path.display()));
}
Ok(format!("{:x}", hasher.finalize()))
}
pub(crate) fn load_frontier_or_fail(path: &Path) -> project::Project {
repo::load_from_path(path).unwrap_or_else(|e| {
fail_return(&format!(
"Failed to load frontier '{}': {e}",
path.display()
))
})
}
pub(crate) fn hash_path_or_fail(path: &Path) -> String {
hash_path(path).unwrap_or_else(|e| {
fail_return(&format!(
"Failed to hash frontier '{}': {e}",
path.display()
))
})
}
fn collect_hash_files(root: &Path, dir: &Path, files: &mut Vec<PathBuf>) -> Result<(), String> {
for entry in
std::fs::read_dir(dir).map_err(|e| format!("Failed to read {}: {e}", dir.display()))?
{
let entry = entry.map_err(|e| format!("Failed to read directory entry: {e}"))?;
let path = entry.path();
if path.is_dir() {
collect_hash_files(root, &path, files)?;
} else if path.is_file() {
files.push(
path.strip_prefix(root)
.map_err(|e| e.to_string())?
.to_path_buf(),
);
}
}
Ok(())
}
fn schema_error_suggestion(error: &str) -> &'static str {
if schema_error_action(error).is_some() {
"Run `vela frontier materialize .` to rebuild deterministic frontier state."
} else {
"Inspect and correct the referenced frontier field."
}
}
fn schema_error_fix(error: &str) -> bool {
schema_error_action(error).is_some()
}
fn schema_error_action(error: &str) -> Option<&'static str> {
if error.contains("stats.findings")
|| error.contains("stats.links")
|| error.contains("Invalid compiler")
|| error.contains("Invalid vela_version")
|| error.contains("Invalid schema")
{
Some("normalize_metadata_and_stats")
} else if error.contains("does not match content-address") {
Some("rewrite_ids")
} else {
None
}
}
fn build_repair_plan(diagnostics: &[Value]) -> Vec<Value> {
let mut actions = std::collections::BTreeMap::<String, usize>::new();
for diagnostic in diagnostics {
if let Some(action) = diagnostic.get("normalize_action").and_then(Value::as_str) {
*actions.entry(action.to_string()).or_default() += 1;
}
}
actions
.into_iter()
.map(|(action, count)| {
let command = if action == "rewrite_ids" {
"vela normalize <frontier> --write --rewrite-ids --id-map id-map.json"
} else {
"vela normalize <frontier> --write"
};
json!({
"action": action,
"count": count,
"command": command,
})
})
.collect()
}
fn empty_signal_report() -> signals::SignalReport {
signals::SignalReport {
schema: "vela.signals.v0".to_string(),
frontier: "unavailable".to_string(),
signals: Vec::new(),
review_queue: Vec::new(),
proof_readiness: signals::ProofReadiness {
status: "unavailable".to_string(),
blockers: 0,
warnings: 0,
caveats: vec!["Frontier could not be loaded for signal analysis.".to_string()],
},
}
}