clm-kernel 0.0.14

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
use std::collections::HashMap;
use super::types::{PetriNetYaml, hash_string};
use super::invariants::check_net_assertions;

pub fn evaluate_engine_assertions(
    net: &PetriNetYaml,
    run_id: &str,
    clm_root: &str,
    fire_count: i64,
    deadlocked: bool,
    place_markings: &HashMap<String, i64>,
    is_kenotic: bool,
    sponsor_did: &str,
    mut out_buf: String,
) -> Result<String, String> {
    let mut assert_pass = 0;
    let mut assert_fail = 0;
    if let Some(assertions) = &net.assertions {
        let (buf, pass, fail) = check_net_assertions(
            assertions,
            run_id,
            clm_root,
            fire_count,
            deadlocked,
            place_markings,
        );
        out_buf.push_str(&buf);
        assert_pass = pass;
        assert_fail = fail;
    }

    if is_kenotic {
        let final_status = if assert_fail == 0 { "completed" } else { "failed" };
        let final_err_msg = if assert_fail > 0 {
            format!("{} assertion(s) failed", assert_fail)
        } else {
            String::new()
        };

        let mut final_marking_hash = String::new();
        if let Ok(Some(hash)) = crate::db::resolve_handle_hash(
            "execution_log",
            clm_root,
            &format!("run:{}:marking", run_id),
        ) {
            final_marking_hash = hash;
        }
        let now = crate::utils::now_iso8601();
        let status_json = if final_status == "completed" {
            serde_json::json!({
                "@type": "RunStatus",
                "status": "completed",
                "final_marking_hash": final_marking_hash,
                "sponsor_did": sponsor_did,
                "completed_at": now
            })
            .to_string()
        } else {
            serde_json::json!({
                "@type": "RunStatus",
                "status": "failed",
                "error": final_err_msg,
                "last_marking_hash": final_marking_hash,
                "sponsor_did": sponsor_did,
                "failed_at": now
            })
            .to_string()
        };

        let status_hash = hash_string(&status_json);
        crate::db::handle_register_batch(
            "execution_log",
            clm_root,
            &format!("run:{}:status", run_id),
            &status_hash,
            &status_json,
        )
        .map_err(|e| e.to_string())?;
    }

    let mut final_marking_map = serde_json::Map::new();
    for (pid, tok) in place_markings {
        final_marking_map.insert(pid.clone(), serde_json::Value::Number((*tok).into()));
    }
    out_buf.push_str(&format!("  [FINAL_MARKING] {}\n", serde_json::Value::Object(final_marking_map)));

    out_buf.push_str(&format!(
        "  → {} passed, {} failed ({} firings)\n",
        assert_pass, assert_fail, fire_count
    ));

    if assert_fail > 0 {
        Err(out_buf)
    } else {
        Ok(out_buf)
    }
}