clm-kernel 0.0.15

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::{section_to_merkle, 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 node = section_to_merkle(section);
    let yaml = serde_yaml::to_string(section).map_err(|e| e.to_string())?;
    let hash_hex = hex::encode(node.hash);
    // Canonical coordinate rather than a bare ISO timestamp (charter L2).
    let now = crate::fs_utils::compute_g_time();
    conn.execute(
        "INSERT OR IGNORE INTO card (hash, content, g_time) VALUES (?, ?, ?)",
        rusqlite::params![hash_hex, yaml, now],
    )
    .map_err(|e| e.to_string())?;
    Ok(node.hash)
}

/// 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: String = conn
        .query_row(
            "SELECT content FROM card WHERE hash = ?",
            [&hash_hex],
            |row| row.get(0),
        )
        .map_err(|e| format!("Failed to load section: {}", e))?;
    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 hash = report.content_hash();
    // Canonical coordinate naming the algorithm that produced `hash` (charter L2).
    let now = crate::fs_utils::compute_g_time();
    conn.execute(
        "INSERT OR IGNORE INTO card (hash, content, g_time) VALUES (?, ?, ?)",
        rusqlite::params![hash, yaml, now],
    )
    .map_err(|e| e.to_string())?;
    Ok(hash)
}