clm-kernel 0.0.23

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Real core dispatch handlers and the dispatch error type (INV-600).
//!
//! The core registry binds a handler only where one does real work. Every other catalog id is unbound
//! in this runtime: dispatching it is `DispatchError::Unbound` (exit `CONTENT_MISSING`), raised before
//! any `ExecutionReceipt` is minted, instead of a fabricated success string.

use crate::cross_cutting::context::CLMContext;
use crate::layer0::runtime_contract::{ClmExitCode, RUNTIME_CONTRACT_VERSION};
use crate::layer0::vocabulary::HASH_PREFIX_BLAKE3;

pub const CLASSIFIED_CLM_TRIAD: &str = "classified:clm_triad";
pub const CLASSIFIED_UNKNOWN: &str = "classified:unknown";
pub const CLM_DOCUMENT_ROOT: &str = "clm:";

/// Why a core dispatch did not run.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DispatchError {
    /// No catalog id or alias matches the route.
    NotFound(String),
    /// The catalog names the id, but this runtime binds no handler to it.
    Unbound(String),
    /// A Tri-DB savepoint or store write failed.
    Store(String),
}

impl DispatchError {
    /// Process exit code: `CONTENT_MISSING` for a missing or unbound id, `GENERAL_ERROR` for a store failure.
    pub fn exit_code(&self) -> i32 {
        match self {
            Self::NotFound(_) | Self::Unbound(_) => ClmExitCode::ContentMissing.code(),
            Self::Store(_) => ClmExitCode::GeneralError.code(),
        }
    }
}

impl std::fmt::Display for DispatchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotFound(route) => write!(f, "OperationNotFoundError: {route}"),
            Self::Unbound(op_id) => write!(f, "UnboundDispatchError: {op_id}"),
            Self::Store(reason) => f.write_str(reason),
        }
    }
}

impl std::error::Error for DispatchError {}

impl From<String> for DispatchError {
    fn from(reason: String) -> Self {
        Self::Store(reason)
    }
}

/// The content address (`blake3:<hex>`) under which the payload is memorized.
pub fn memorize_content(_ctx: &CLMContext, payload: &str) -> String {
    format!("{HASH_PREFIX_BLAKE3}{}", blake3::hash(payload.as_bytes()).to_hex())
}

/// `classified:clm_triad` for a CLM document (its root key is `clm:`), otherwise `classified:unknown`.
pub fn classify_yaml(_ctx: &CLMContext, payload: &str) -> String {
    let class = if payload.trim_start().starts_with(CLM_DOCUMENT_ROOT) { CLASSIFIED_CLM_TRIAD } else { CLASSIFIED_UNKNOWN };
    class.to_string()
}

/// `clm-core:<runtime contract version>`.
pub fn probe_version(_ctx: &CLMContext, _payload: &str) -> String {
    format!("clm-core:{RUNTIME_CONTRACT_VERSION}")
}