use libfse::{FseMap, FseOpcode, FseScanner, Rule, RuleId};
use libfse::metrics::shannon_entropy_from_logits;
const RULE_HIGH_ENTROPY: RuleId = 1;
const SIGNAL_HIGH_ENTROPY: u8 = 0xFF;
const SIGNAL_OK: u8 = 0x00;
const ENTROPY_THRESHOLD: f32 = 2.0;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== Fail-Fast Experiment: Entropy Detection ===");
let rules = vec![
Rule::new(&[SIGNAL_HIGH_ENTROPY], FseOpcode::Reject(RULE_HIGH_ENTROPY)),
];
let map = FseMap::compile(rules)?;
let mut scanner = FseScanner::new(&map)?;
let logits_safe = vec![10.0f32, 0.0, 0.0, -5.0, -5.0];
let logits_risky = vec![1.0f32, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0];
run_step(&mut scanner, "Token 1 (Safe)", &logits_safe)?;
match run_step(&mut scanner, "Token 2 (Risky)", &logits_risky) {
Ok(_) => println!("WARNING: High entropy token was accepted! (Test Failed)"),
Err(e) => {
println!("\n!!! FAIL-FAST TRIGGERED !!!");
println!(" Violation: {:?}", e);
println!(" Action: Dropping generic note/log as requested.");
println!(" System State: HALTED.");
}
}
Ok(())
}
fn run_step(scanner: &mut FseScanner, label: &str, logits: &[f32]) -> Result<(), libfse::Violation> {
let ent = shannon_entropy_from_logits(logits);
print!("{:<15} | Entropy: {:.4} | ", label, ent);
let signal = if ent > ENTROPY_THRESHOLD {
print!("Status: HIGH RISK -> ");
SIGNAL_HIGH_ENTROPY
} else {
print!("Status: OK -> ");
SIGNAL_OK
};
let summary = scanner.scan(&[signal])?;
println!("FSE: Accepted (Recorded: {})", summary.rules_recorded);
Ok(())
}