clm-kernel 0.0.23

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! INV-597 / DoD-597-05: `parameter_context_hash` is optional in execution receipts, so receipts minted
//! without a ParameterContext keep their Sprint 493 hashes, and one minted with it agrees with Python.

use std::collections::BTreeMap;
use std::path::PathBuf;

use clm_rust_core::cpn::parameter_cards::parameter_context_hash;
use clm_rust_core::evaluator::dispatch::catalog::{compute_receipt_hash, ExecutionReceipt, ExecutionReceiptWithoutHash};
use clm_rust_core::layer0::parameter_context::ParameterContext;
use serde_json::Value;

/// `build_execution_receipt` in Python for Sprint 493 case 0 plus this context's digest.
const PYTHON_RECEIPT_HASH: &str = "blake3:3e98b2492d675598c4a86d4aa77acc824d531e7dad72cba99a24259060256d6a";
const PYTHON_CONTEXT_HASH: &str = "blake3:a83d184120688c06a4794062b0b232de831be53b5d97f4b24c800351a2a0e32b";

fn sprint493_receipts() -> Vec<ExecutionReceipt> {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/conformance/vectors/execution_receipt_vectors.json");
    let doc: Value = serde_json::from_str(&std::fs::read_to_string(path).expect("vectors")).expect("json");
    doc["cases"].as_array().expect("cases").iter().map(|case| serde_json::from_value(case["receipt"].clone()).expect("receipt")).collect()
}

fn base_of(receipt: &ExecutionReceipt, parameter_context_hash: Option<String>) -> ExecutionReceiptWithoutHash {
    ExecutionReceiptWithoutHash {
        agent_did: receipt.agent_did.clone(),
        dispatch_catalog_hash: None,
        error: receipt.error.clone(),
        exit_code: receipt.exit_code,
        g_time: receipt.g_time.clone(),
        handler_key: receipt.handler_key.clone(),
        idempotency_key: receipt.idempotency_key.clone(),
        input_hash: receipt.input_hash.clone(),
        op_id: receipt.op_id.clone(),
        output_hash: receipt.output_hash.clone(),
        parameter_context_hash,
        receipt_id: receipt.receipt_id.clone(),
        schema_version: receipt.schema_version.clone(),
        status: receipt.status.clone(),
        witness_policy: receipt.witness_policy.clone(),
    }
}

#[test]
fn execution_receipt_without_parameter_context_keeps_sprint_493_hashes() {
    for receipt in sprint493_receipts() {
        assert_eq!(receipt.parameter_context_hash, None);
        let rebuilt = ExecutionReceipt::build(base_of(&receipt, None));
        assert_eq!(rebuilt, receipt);
        assert!(!serde_json::to_string(&rebuilt).expect("json").contains("parameter_context_hash"));
    }
}

#[test]
fn execution_receipt_with_parameter_context_matches_python() {
    let context = ParameterContext::default()
        .with_overrides(&BTreeMap::from([("network.gateway_timeout_ms".to_string(), "2500".to_string())]))
        .expect("override");
    let digest = format!("blake3:{}", parameter_context_hash(&context).expect("context hash"));
    assert_eq!(digest, PYTHON_CONTEXT_HASH);
    let receipt = sprint493_receipts().remove(0);
    let base = base_of(&receipt, Some(digest));
    assert_eq!(compute_receipt_hash(&base), PYTHON_RECEIPT_HASH);
    let rebuilt = ExecutionReceipt::build(base);
    assert_eq!(rebuilt.parameter_context_hash.as_deref(), Some(PYTHON_CONTEXT_HASH));
}

#[test]
fn execution_receipt_dispatch_catalog_hash_is_canonical() {
    let registry = clm_rust_core::evaluator::dispatch::catalog::CoreDispatchRegistry::from_canonical_catalog();
    let mirror = std::fs::read(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("types/lattice/dispatch_catalog.json")).expect("catalog");
    let catalog_hash = registry.catalog_blake3_hash();
    assert_eq!(catalog_hash, format!("blake3:{}", blake3::hash(&mirror).to_hex()));
    let receipt = sprint493_receipts().remove(0);
    let mut base = base_of(&receipt, Some(PYTHON_CONTEXT_HASH.to_string()));
    base.dispatch_catalog_hash = Some(catalog_hash.clone());
    // Python hashes the receipt as sorted-key canonical JSON; Rust's field order must match it.
    let value = serde_json::to_value(&base).expect("value");
    let sorted: std::collections::BTreeMap<String, Value> = value.as_object().expect("object").clone().into_iter().collect();
    let canonical = serde_json::to_string(&sorted).expect("json");
    assert_eq!(compute_receipt_hash(&base), format!("blake3:{}", blake3::hash(canonical.as_bytes()).to_hex()));
    assert_eq!(ExecutionReceipt::build(base).dispatch_catalog_hash.as_deref(), Some(catalog_hash.as_str()));
}