ccf-core 1.0.1

Canonical CCF core v1 trust-update and runtime-certificate primitives for filed-Prov6 contract enforcement
Documentation
use ccf_core::kappa::{
    classify_dynamic_floor_tick, reject_no_fail_closed_excursion, KappaCertificateStatus,
    KappaFloorTick, KAPPA_DYNAMIC_FLOOR_JOURNEY_ID, KAPPA_DYNAMIC_FLOOR_STORY_ID,
    NO_FAIL_CLOSED_EXCURSION_OLD_WRONG_PATH,
};

const ORACLE_TOLERANCE: f64 = 1.0e-12;
const EXPECTED_FLOOR: f64 = 0.0060;
const EXPECTED_THRESHOLD: f64 = 0.0065;
const EXPECTED_EXCURSION: f64 = 0.0185;

fn wrong_update_tick() -> KappaFloorTick {
    KappaFloorTick {
        tick_id: 3,
        dimension_n: 3,
        epsilon_q: 0.0002,
        delta_alpha_t: 0.03,
        e_t: 0.16,
        policy_margin: 0.0005,
        kappa_hat_t: 0.025,
    }
}

fn assert_close(actual: f64, expected: f64, label: &str) {
    assert!(
        (actual - expected).abs() <= ORACLE_TOLERANCE,
        "{label}: expected {expected:.17e}, got {actual:.17e}"
    );
}

#[test]
fn wrong_update_excursion_fails_closed() {
    eprintln!("journey={KAPPA_DYNAMIC_FLOOR_JOURNEY_ID} story={KAPPA_DYNAMIC_FLOOR_STORY_ID}");

    let report = classify_dynamic_floor_tick(&wrong_update_tick())
        .expect("wrong-update excursion classification is required");

    assert_eq!(
        report.certificate_status,
        KappaCertificateStatus::FailClosedExcursion
    );
    assert!(report.fail_closed);
    assert!(report.kappa_hat_t > report.threshold_t);
    assert_close(report.floor_t, EXPECTED_FLOOR, "floor_t");
    assert_close(report.threshold_t, EXPECTED_THRESHOLD, "threshold_t");
    assert_close(
        report.kappa_hat_t - report.threshold_t,
        EXPECTED_EXCURSION,
        "excursion_over_threshold",
    );
    eprintln!(
        "wrong_update_excursion=PASS tick_id={} kappa_hat_t={:.16e} floor_t={:.16e} threshold_t={:.16e} excursion_over_threshold={:.16e} fail_closed={}",
        report.tick_id,
        report.kappa_hat_t,
        report.floor_t,
        report.threshold_t,
        report.kappa_hat_t - report.threshold_t,
        report.fail_closed
    );
}

#[test]
fn no_fail_closed_wrong_update_path_is_rejected() {
    eprintln!("journey={KAPPA_DYNAMIC_FLOOR_JOURNEY_ID} story={KAPPA_DYNAMIC_FLOOR_STORY_ID}");

    let report = reject_no_fail_closed_excursion(&wrong_update_tick())
        .expect("no-fail-closed negative guard is required");

    assert_eq!(
        report.old_wrong_path,
        NO_FAIL_CLOSED_EXCURSION_OLD_WRONG_PATH
    );
    assert!(report.rejected);
    assert!(report.fail_closed);
    assert_close(report.kappa_hat_t, 0.025, "kappa_hat_t");
    assert_close(report.floor_t, EXPECTED_FLOOR, "floor_t");
    eprintln!(
        "no_fail_closed_wrong_update_negative=PASS old_wrong_path={} fail_closed={}",
        report.old_wrong_path, report.fail_closed
    );
}