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_fixed_kappa_theorem_threshold,
    reject_no_fail_closed_excursion, reject_periodic_certificate, KappaCertificateStatus,
    KappaFloorTick, FIXED_KAPPA_THEOREM_OLD_WRONG_PATH, KAPPA_DYNAMIC_FLOOR_JOURNEY_ID,
    KAPPA_DYNAMIC_FLOOR_STORY_ID, NO_FAIL_CLOSED_EXCURSION_OLD_WRONG_PATH,
    PERIODIC_CERTIFICATE_OLD_WRONG_PATH,
};

const TEST_SOURCE: &str = include_str!("kappa_dynamic_floor.rs");
const FIXED_THRESHOLD_NEEDLE: &str = concat!("1", "e-9");
const ORACLE_TOLERANCE: f64 = 1.0e-12;
const EXPECTED_CANONICAL_FLOORS: [f64; 2] = [0.0138, 0.0100];
const EXPECTED_CANONICAL_THRESHOLDS: [f64; 2] = [0.0143, 0.0105];
const EXPECTED_STALE_FLOOR: f64 = 0.0060;
const EXPECTED_STALE_THRESHOLD: f64 = 0.0065;
const EXPECTED_STALE_EXCURSION: f64 = 0.0185;

fn canonical_ticks() -> [KappaFloorTick; 2] {
    [
        KappaFloorTick {
            tick_id: 1,
            dimension_n: 3,
            epsilon_q: 0.0002,
            delta_alpha_t: 0.07,
            e_t: 0.18,
            policy_margin: 0.0005,
            kappa_hat_t: 0.0131,
        },
        KappaFloorTick {
            tick_id: 2,
            dimension_n: 3,
            epsilon_q: 0.0002,
            delta_alpha_t: -0.04,
            e_t: 0.22,
            policy_margin: 0.0005,
            kappa_hat_t: 0.0099,
        },
    ]
}

fn stale_substitution_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 dynamic_floor_certificate_is_emitted_for_each_update_tick() {
    eprintln!("journey={KAPPA_DYNAMIC_FLOOR_JOURNEY_ID} story={KAPPA_DYNAMIC_FLOOR_STORY_ID}");

    for (index, tick) in canonical_ticks().into_iter().enumerate() {
        let report = classify_dynamic_floor_tick(&tick)
            .expect("dynamic kappa-floor certificate implementation is required");

        assert_eq!(report.tick_id, tick.tick_id);
        assert_eq!(
            report.certificate_status,
            KappaCertificateStatus::WithinDynamicFloor
        );
        assert!(!report.fail_closed);
        assert!(report.kappa_hat_t <= report.threshold_t);
        assert_close(
            report.floor_t,
            EXPECTED_CANONICAL_FLOORS[index],
            "canonical floor_t",
        );
        assert_close(
            report.threshold_t,
            EXPECTED_CANONICAL_THRESHOLDS[index],
            "canonical threshold_t",
        );
        eprintln!(
            "canonical_dynamic_floor=PASS tick_id={} kappa_hat_t={:.16e} floor_t={:.16e} epsilon_q={:.16e} delta_alpha_t={:.16e} E_t={:.16e} policy_margin={:.16e}",
            report.tick_id,
            report.kappa_hat_t,
            report.floor_t,
            report.epsilon_q,
            report.delta_alpha_t,
            report.e_t,
            report.policy_margin
        );
    }
}

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

    let report = classify_dynamic_floor_tick(&stale_substitution_tick())
        .expect("fail-closed 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_STALE_FLOOR, "stale floor_t");
    assert_close(
        report.threshold_t,
        EXPECTED_STALE_THRESHOLD,
        "stale threshold_t",
    );
    assert_close(
        report.kappa_hat_t - report.threshold_t,
        EXPECTED_STALE_EXCURSION,
        "stale excursion_over_threshold",
    );
    eprintln!(
        "fail_closed_excursion=PASS tick_id={} kappa_hat_t={:.16e} floor_t={:.16e} policy_margin={:.16e}",
        report.tick_id, report.kappa_hat_t, report.floor_t, report.policy_margin
    );
}

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

    let report = reject_fixed_kappa_theorem_threshold(&stale_substitution_tick())
        .expect("fixed-threshold negative guard is required");

    assert_eq!(report.old_wrong_path, FIXED_KAPPA_THEOREM_OLD_WRONG_PATH);
    assert!(report.rejected);
    assert!(report.fail_closed);
    assert_close(report.kappa_hat_t, 0.025, "fixed-threshold kappa_hat_t");
    assert_close(
        report.floor_t,
        EXPECTED_STALE_FLOOR,
        "fixed-threshold floor_t",
    );
    eprintln!(
        "fixed_kappa_theorem_negative=PASS old_wrong_path={} kappa_hat_t={:.16e} floor_t={:.16e}",
        report.old_wrong_path, report.kappa_hat_t, report.floor_t
    );
}

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

    let ticks = canonical_ticks();
    let report = reject_periodic_certificate(&ticks)
        .expect("periodic certificate negative guard is required");

    assert_eq!(report.old_wrong_path, PERIODIC_CERTIFICATE_OLD_WRONG_PATH);
    assert!(report.rejected);
    assert_close(report.kappa_hat_t, 0.0099, "periodic observed kappa_hat_t");
    assert_close(report.floor_t, 0.0100, "periodic observed floor_t");
    eprintln!(
        "periodic_certificate_negative=PASS old_wrong_path={} observed_tick_count=1 expected_tick_count=2",
        report.old_wrong_path
    );
}

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

    let report = reject_no_fail_closed_excursion(&stale_substitution_tick())
        .expect("no fail-closed excursion 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, "no-fail-closed kappa_hat_t");
    assert_close(
        report.floor_t,
        EXPECTED_STALE_FLOOR,
        "no-fail-closed floor_t",
    );
    eprintln!(
        "no_fail_closed_excursion_negative=PASS old_wrong_path={}",
        report.old_wrong_path
    );
}

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

    assert!(
        !TEST_SOURCE.contains(FIXED_THRESHOLD_NEEDLE),
        "the #152 test gate must not embed a fixed theorem threshold"
    );
    eprintln!("fixed_threshold_scan=PASS");
}