#![allow(dead_code, unused_imports)]
use antigen::{antigen, antigen_tolerance, presents};
macro_rules! recurse_marker {
() => {
()
};
}
#[antigen(
name = "unchecked-recursion",
family = "boundary-violation",
fingerprint = r#"all_of([item = fn, body_contains_macro("recurse_marker")])"#,
summary = "Functions that recurse on user-supplied data without explicit \
depth bounds can overflow the stack on adversarial input. \
Discipline requires iterative reformulation, explicit depth \
check with short-circuit, or sidecar-attested tolerance with \
documented input-distribution analysis.",
references = [
"https://github.com/antigen-rs/antigen/blob/main/docs/decisions.md#adr-011",
],
)]
pub struct UncheckedRecursion;
#[presents(UncheckedRecursion)]
#[antigen_tolerance(
UncheckedRecursion,
rationale = "Walks a config tree whose depth is bounded by schema-validation \
at parse time (max depth = 8 per Schema v1.2). Recursion is safe \
by construction of the input domain, not by structural enforcement \
here. If schema-validation is later relaxed, this needs revisit."
)]
pub fn walk_config_tree_vibes_grade(node: &ConfigNode) -> usize {
recurse_marker!(); 1 + node
.children
.iter()
.map(walk_config_tree_vibes_grade)
.sum::<usize>()
}
#[presents(UncheckedRecursion)]
#[antigen_tolerance(
UncheckedRecursion,
requires = all_of([
signers(required = ["math-expert"]),
fresh_within_days(days = 365),
]),
rationale = "Newton-Raphson iteration on a domain provably bounded by \
the Lipschitz constant of the target function. A math-expert \
reviewer is required to attest that the bound calculation \
is correct for the specific function class this is applied to. \
Re-review annually as the function-class set evolves."
)]
pub fn newton_iterate_sidecar_attested(initial: f64, target: f64) -> f64 {
recurse_marker!(); if (initial - target).abs() < 1e-12 {
return initial;
}
let next = f64::midpoint(initial, target);
newton_iterate_sidecar_attested(next, target)
}
pub struct ConfigNode {
pub children: Vec<Self>,
}
fn main() {
println!("antigen tolerance comparison — vibes-grade vs sidecar-attested.");
println!();
println!("Two sites in this file declare tolerance for UncheckedRecursion:");
println!();
println!(" Site A: walk_config_tree_vibes_grade");
println!(" #[antigen_tolerance(UncheckedRecursion, rationale = \"...\")]");
println!(" → audit reports: tolerance-vibes-grade at WitnessTier::None");
println!(" → operator path: \"I considered this and accept it\"");
println!();
println!(" Site B: newton_iterate_sidecar_attested");
println!(" #[antigen_tolerance(UncheckedRecursion, sidecar = true,");
println!(" requires = all_of([signers(required = [\"math-expert\"]),");
println!(" fresh_within_days(days = 365)]))]");
println!(" → audit reports: tolerance-predicate-passed-substrate-current");
println!(" at WitnessTier::Execution + EvidenceKind::SubstrateState");
println!(" → operator path: \"a math-expert reviewed this; here's the sidecar trail\"");
println!();
println!("Both are tolerance; both opt out of immunity. The difference is");
println!("whether the audit can corroborate the opt-out with substrate.");
println!();
println!("Operator workflow to lift Site B from vibes-grade to attested:");
println!();
println!(" 1. cargo antigen tolerate scaffold \\");
println!(" --antigen UncheckedRecursion \\");
println!(" --source-file antigen/examples/tolerance_attested.rs \\");
println!(" --item-path newton_iterate_sidecar_attested \\");
println!(" --fingerprint <use-cargo-antigen-scan-to-get-this>");
println!();
println!(" 2. cargo antigen tolerate sign \\");
println!(" --sidecar antigen/examples/.attest/UncheckedRecursion.json \\");
println!(" --item-path newton_iterate_sidecar_attested \\");
println!(" --signer claire --role math-expert \\");
println!(" --fingerprint <same-as-scaffold> \\");
println!(" --reasoning \"verified Lipschitz bound for this function class\"");
println!();
println!(" 3. cargo antigen audit --root antigen/examples");
let leaf = ConfigNode { children: vec![] };
let _ = walk_config_tree_vibes_grade(&leaf);
let _ = newton_iterate_sidecar_attested(1.0, 1.0);
}