clm-kernel 0.0.22

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Layer 2 Sheaf Section & Conflict Report Persistence (Sprint 551).
//!
//! Extracted from `sheaf.rs` to maintain Layer 1/2 boundaries and ensure zero rusqlite in core.

use rusqlite::Connection;
use crate::sheaf::{CohomologyConflictReport, Section};

/// Store a section's Merkle node in the `card` table.
pub fn store_section_merkle(
    section: &Section,
    conn: &Connection,
) -> Result<[u8; 32], String> {
    let yaml = serde_yaml::to_string(section).map_err(|e| e.to_string())?;
    let record = crate::layer0::mcard_record::MCardRecord::create(yaml.as_bytes(),
        Some(crate::layer0::vocabulary::HASH_ALGORITHM_BLAKE3))?;
    crate::layer2::record_store::insert_record(conn, &record).map_err(|error| error.to_string())?;
    hex::decode(record.hash).map_err(|error| error.to_string())?.try_into()
        .map_err(|_| "invalid BLAKE3 digest width".to_string())
}

/// Load a section from the `card` table by its Merkle hash.
pub fn load_section_merkle(
    hash: &[u8; 32],
    conn: &Connection,
) -> Result<Section, String> {
    let hash_hex = hex::encode(hash);
    let content = crate::layer2::record_store::read_text(conn, &hash_hex)
        .map_err(|error| error.to_string())?.ok_or("section is missing")?;
    serde_yaml::from_str(&content).map_err(|e| format!("Failed to deserialize section: {}", e))
}

/// Store a conflict report in the `execution_log` pillar.
pub fn store_conflict_report(
    report: &CohomologyConflictReport,
    conn: &Connection,
) -> Result<String, String> {
    let yaml = report.to_yaml();
    let record = crate::layer0::mcard_record::MCardRecord::create(yaml.as_bytes(), None)?;
    crate::layer2::record_store::insert_record(conn, &record).map_err(|error| error.to_string())?;
    Ok(record.hash)
}