clm-kernel 0.0.24

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! CROSS-LANGUAGE RUNTIME CONFORMANCE — the Rust binding.
//!
//! Runs the SAME operation sequence as the TypeScript and Python runners and prints ONE JSON
//! object. `scripts/ci/check_runtime_conformance.py` runs all three and compares them.
//!
//! THE SEQUENCE IS FIXED, NOT READ FROM A FILE, and that is deliberate: reading
//! `registry/runtime-conformance-fixtures.json` here would make the comparison depend on THREE
//! JSON parsers agreeing before it could test the tapes. The fixtures file DOCUMENTS the
//! sequence; each runner implements it; the runner that diverges is the finding.
//!
//! A TAPE WRITTEN BY ONE RUNTIME IS READ BY ANOTHER, AND A TAPE DOES NOT KNOW WHICH ONE WROTE
//! IT. Agreement on the schema is not agreement on the RESULTS — this measures the results.

use clm_rust_core::tape;
use rusqlite::Connection;
use sha2::{Digest, Sha256};

/// The Triad, exactly three tables and three columns each (`INV-TAPE-01`).
const DDL: &str = "CREATE TABLE card (hash TEXT PRIMARY KEY, content BLOB NOT NULL, g_time TEXT NOT NULL);
                   CREATE TABLE handle_registry (handle TEXT PRIMARY KEY, current_hash TEXT NOT NULL, updated_at TEXT NOT NULL);
                   CREATE TABLE handle_history (handle TEXT NOT NULL, previous_hash TEXT NOT NULL, changed_at TEXT NOT NULL);";

const AT: &str = "2026-01-01T00:00:00Z";
const HANDLE: &str = "urn:mcard:mcard:content:x";

fn sha256_hex(bytes: &[u8]) -> String {
    let mut h = Sha256::new();
    h.update(bytes);
    format!("{:x}", h.finalize())
}

fn main() {
    let conn = Connection::open_in_memory().expect("an in-memory tape");
    conn.execute_batch(DDL).expect("the Triad");

    // ── the sequence, in the same order as the fixtures and the other two runners ──────────
    let mut out: Vec<String> = Vec::new();

    out.push(format!("\"count_empty\":{}", tape::count(&conn).unwrap()));

    let h_alpha = tape::put(&conn, b"alpha", AT).expect("put alpha");
    out.push(format!("\"hash_alpha\":\"{}\"", h_alpha));

    out.push(format!("\"count_after_alpha\":{}", tape::count(&conn).unwrap()));

    out.push(format!(
        "\"has_alpha\":{}",
        tape::has(&conn, &h_alpha).unwrap()
    ));
    let h_absent = sha256_hex(b"not-written");
    out.push(format!(
        "\"has_absent\":{}",
        tape::has(&conn, &h_absent).unwrap()
    ));

    let got = tape::get(&conn, &h_alpha).unwrap().expect("alpha is there");
    out.push(format!(
        "\"get_alpha\":\"{}\"",
        String::from_utf8_lossy(&got.0)
    ));

    // TD-02: identical content writes NOTHING — content addressing IS the identity
    let h_alpha_again = tape::put(&conn, b"alpha", AT).expect("put alpha again");
    out.push(format!("\"put_alpha_idempotent\":{}", h_alpha_again == h_alpha));
    out.push(format!(
        "\"count_after_reput\":{}",
        tape::count(&conn).unwrap()
    ));

    let h_bravo = tape::put(&conn, b"bravo", AT).expect("put bravo");
    out.push(format!("\"hash_bravo\":\"{}\"", h_bravo));
    out.push(format!("\"count_after_bravo\":{}", tape::count(&conn).unwrap()));

    // TD-04: a NEW binding records exactly one transition
    let moved = tape::register_handle(&conn, HANDLE, &h_alpha, AT).unwrap();
    out.push(format!("\"register_new_moved\":{}", moved));
    out.push(format!(
        "\"history_after_new\":{}",
        tape::handle_history(&conn, HANDLE).unwrap().len()
    ));
    out.push(format!(
        "\"resolve_after_new\":\"{}\"",
        tape::resolve_handle(&conn, HANDLE).unwrap().unwrap_or_default()
    ));

    // TD-03: a no-op binding records NOTHING — a row means the pointer MOVED
    let moved_again = tape::register_handle(&conn, HANDLE, &h_alpha, AT).unwrap();
    out.push(format!("\"register_same_moved\":{}", moved_again));
    out.push(format!(
        "\"history_after_noop\":{}",
        tape::handle_history(&conn, HANDLE).unwrap().len()
    ));

    let moved_bravo = tape::register_handle(&conn, HANDLE, &h_bravo, AT).unwrap();
    out.push(format!("\"register_move_moved\":{}", moved_bravo));
    out.push(format!(
        "\"history_after_move\":{}",
        tape::handle_history(&conn, HANDLE).unwrap().len()
    ));
    out.push(format!(
        "\"resolve_after_move\":\"{}\"",
        tape::resolve_handle(&conn, HANDLE).unwrap().unwrap_or_default()
    ));
    out.push(format!(
        "\"resolve_absent\":\"{}\"",
        tape::resolve_handle(&conn, "urn:mcard:mcard:content:absent")
            .unwrap()
            .unwrap_or_default()
    ));

    // TD-01: exactly three tables
    let n: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'",
            [],
            |r| r.get(0),
        )
        .unwrap();
    out.push(format!("\"tables\":{}", n));

    println!("{{{}}}", out.join(","));
}