pub mod rules {
use std::collections::BTreeSet;
use crate::enforce::reference_trust;
use crate::proof::algebra::formal::loader;
use crate::spec::OpSpec;
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct SelfAuditFinding {
pub rule: u8,
pub op_id: Option<String>,
pub message: String,
}
#[inline]
pub fn rule_3_cpu_reference_trust(specs: &[OpSpec]) -> Vec<SelfAuditFinding> {
specs
.iter()
.flat_map(|spec| {
reference_trust::validate_spec(spec)
.into_iter()
.map(|finding| SelfAuditFinding {
rule: 3,
op_id: Some(finding.op_id.clone()),
message: format!(
"{} Fix: implement real CPU semantics before this op can be registered.",
finding.message()
),
})
})
.collect()
}
#[inline]
pub fn rule_9_reference_trust_covers_registry(specs: &[OpSpec]) -> Vec<SelfAuditFinding> {
let report = reference_trust::enforce_registry(specs);
let checked: BTreeSet<&str> = report.checked_ops.iter().map(String::as_str).collect();
specs
.iter()
.filter(|spec| !checked.contains(spec.id))
.map(|spec| SelfAuditFinding {
rule: 9,
op_id: Some(spec.id.to_string()),
message: format!(
"reference_trust did not visit `{}`. Fix: route every registry op through reference_trust::enforce_registry before admission.",
spec.id
),
})
.collect()
}
#[inline]
pub fn rule_10_algebraic_laws_have_formal_specs() -> Vec<SelfAuditFinding> {
match loader::load_catalog() {
Ok(_) => Vec::new(),
Err(error) => vec![SelfAuditFinding {
rule: 10,
op_id: None,
message: format!(
"{error}. Fix: add or repair the matching conform/src/algebra/formal/*.formal file."
),
}],
}
}
}
pub struct SelfAuditEnforcer;
impl crate::enforce::EnforceGate for SelfAuditEnforcer {
fn id(&self) -> &'static str {
"self_audit"
}
fn name(&self) -> &'static str {
"self_audit"
}
fn run(&self, ctx: &crate::enforce::EnforceCtx<'_>) -> Vec<crate::enforce::Finding> {
let mut findings = rules::rule_3_cpu_reference_trust(ctx.specs);
findings.extend(rules::rule_9_reference_trust_covers_registry(ctx.specs));
findings.extend(rules::rule_10_algebraic_laws_have_formal_specs());
crate::enforce::finding_result(
self.id(),
findings
.into_iter()
.map(|finding| finding.message)
.collect(),
)
}
}
pub const REGISTERED: SelfAuditEnforcer = SelfAuditEnforcer;