clm-kernel 0.0.22

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Layer 1 PCard Reification and Reflection.

use serde_json::Value;
use super::token_models::TokenMCard;

/// Reifies a versioned serializable PCard descriptor (`clm://layer1/pcard_descriptor/v1`) into a `TokenMCard`.
pub fn reify_pcard_descriptor(
    descriptor: &Value,
    algorithm: Option<&str>,
    timestamp: Option<&str>,
) -> Result<TokenMCard, String> {
    let schema = descriptor.get("schema").and_then(|v| v.as_str()).unwrap_or("");
    if schema != crate::layer0::vocabulary::HANDLE_CLM_LAYER1_PCARD_DESCRIPTOR_V1 {
        return Err(format!("Unsupported PCard descriptor schema: {schema}"));
    }
    if descriptor.get("closure").is_some() || descriptor.get("ambient_capability").is_some() {
        return Err("Non-serializable closure or ambient capability rejected".to_string());
    }
    let id = descriptor.get("id").and_then(|v| v.as_str()).unwrap_or("pcard_0");
    TokenMCard::from_payload(
        descriptor.clone(),
        crate::layer0::vocabulary::HANDLE_CLM_LAYER1_PCARD_DESCRIPTOR_V1,
        &format!("tok_desc_{id}"),
        algorithm,
        timestamp,
    )
}

/// Reflects a `TokenMCard` carrying a `clm://layer1/pcard_descriptor/v1` back into a normalized descriptor.
pub fn reflect_pcard_descriptor(token: &TokenMCard, registered_ops: &[&str]) -> Result<Value, String> {
    if token.color_type != crate::layer0::vocabulary::HANDLE_CLM_LAYER1_PCARD_DESCRIPTOR_V1 {
        return Err(format!("Unsupported token color_type for descriptor reflection: {}", token.color_type));
    }
    let schema = token.payload.get("schema").and_then(|v| v.as_str()).unwrap_or("");
    if schema != crate::layer0::vocabulary::HANDLE_CLM_LAYER1_PCARD_DESCRIPTOR_V1 {
        return Err(format!("Unsupported PCard descriptor schema: {schema}"));
    }
    let variant = token.payload.get("variant").and_then(|v| v.as_str()).unwrap_or("");
    if variant == "dynamic" {
        let op = token.payload.get("operation_id").and_then(|v| v.as_str()).unwrap_or("");
        if !registered_ops.contains(&op) {
            return Err(format!("Unregistered operation_id rejected: {op}"));
        }
    } else if variant != "primitive" && variant != "boolean" {
        return Err(format!("Unsupported PCard variant: {variant}"));
    }
    Ok(token.payload.clone())
}


/// A `PrimitivePCard` `1 -> X` as its constant value and output type (Function-Number Duality).
#[derive(Clone, Debug, PartialEq)]
pub struct PrimitiveConstant {
    /// The constant the nullary morphism returns.
    pub constant_value: Value,
    /// The Type Lattice URI of its codomain.
    pub output_type: String,
}

/// Phi: reifies a primitive PCard into the `TokenMCard` `entity_id`, byte-identical to Python's
/// `reify_pcard_to_mcard` and TypeScript's `reifyPcardToMcard` (INV-597).
pub fn reify_pcard_to_mcard(
    pcard: &PrimitiveConstant,
    entity_id: &str,
    algorithm: Option<&str>,
    timestamp: Option<&str>,
) -> Result<TokenMCard, String> {
    TokenMCard::from_payload(pcard.constant_value.clone(), &pcard.output_type, entity_id, algorithm, timestamp)
}

/// Psi: reflects a `TokenMCard` back into its primitive PCard, so `reflect(reify(p)) == p`.
pub fn reflect_mcard_to_pcard(token: &TokenMCard) -> PrimitiveConstant {
    PrimitiveConstant { constant_value: token.payload.clone(), output_type: token.color_type.clone() }
}