clm-kernel 0.0.24

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Layer 1: Declarative Action Manifest Engine & Baldwin Modularity Manipulators
//! Canonical Path: kernel/clm_rust_core/src/layer1/action.rs

use std::collections::HashMap;
use std::path::Path;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

/// Format of data processed by an Action
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ActionFormat {
    Csv,
    Json,
    Yaml,
    Markdown,
    Text,
    Binary,
    Media,
}

/// Baldwin modularity operators (Baldwin & Clark, 2000)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum BaldwinOperator {
    Split,
    Substitute,
    Augment,
    Exclude,
    Invert,
    Port,
    Custom,
}

impl std::fmt::Display for BaldwinOperator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BaldwinOperator::Split => write!(f, "split"),
            BaldwinOperator::Substitute => write!(f, "substitute"),
            BaldwinOperator::Augment => write!(f, "augment"),
            BaldwinOperator::Exclude => write!(f, "exclude"),
            BaldwinOperator::Invert => write!(f, "invert"),
            BaldwinOperator::Port => write!(f, "port"),
            BaldwinOperator::Custom => write!(f, "custom"),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionMetadata {
    #[serde(default = "default_action_type")]
    pub r#type: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default = "default_face")]
    pub face: String,
    pub operator: BaldwinOperator,
    #[serde(default = "default_author")]
    pub author: String,
}

fn default_action_type() -> String { "action".to_string() }
fn default_face() -> String { "C".to_string() }
fn default_author() -> String { crate::layer0::vocabulary_identities::DID_CLM_SYSTEM.to_string() }

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionInput {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    #[serde(default = "default_true")]
    pub required: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

fn default_true() -> bool { true }

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionPrecondition {
    pub invariant: String,
    pub rule: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schema_ref: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionOperation {
    pub operator: BaldwinOperator,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strategy: Option<String>,
    #[serde(default)]
    pub parameters: HashMap<String, Value>,
    #[serde(default = "default_runtime")]
    pub runtime: String,
}

fn default_runtime() -> String { "builtin".to_string() }

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionPostcondition {
    pub invariant: String,
    pub rule: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schema_ref: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionOutput {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionManifest {
    #[serde(default = "default_version")]
    pub version: String,
    pub clm: ActionMetadata,
    pub inputs: Vec<ActionInput>,
    #[serde(default)]
    pub preconditions: Vec<ActionPrecondition>,
    pub operation: ActionOperation,
    #[serde(default)]
    pub postconditions: Vec<ActionPostcondition>,
    pub outputs: Vec<ActionOutput>,
}

fn default_version() -> String { "1.0".to_string() }

impl ActionManifest {
    pub fn from_yaml_str(yaml_str: &str) -> Result<Self, String> {
        let manifest: ActionManifest = serde_yaml::from_str(yaml_str)
            .map_err(|e| format!("Failed to parse YAML ActionManifest: {}", e))?;
        if manifest.clm.operator != manifest.operation.operator {
            return Err(format!(
                "Operator mismatch: clm.operator='{}' != operation.operator='{}'",
                manifest.clm.operator, manifest.operation.operator
            ));
        }
        Ok(manifest)
    }

    pub fn from_yaml_file<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        let content = std::fs::read_to_string(path.as_ref())
            .map_err(|e| format!("Cannot read file {}: {}", path.as_ref().display(), e))?;
        Self::from_yaml_str(&content)
    }

    pub fn to_canonical_json_value(&self) -> Value {
        serde_json::to_value(self).unwrap_or(Value::Null)
    }
}

// ── Canonical Hashing Helpers ────────────────────────────────────────────────

pub use crate::layer0::codec::sort_json_value;

pub fn compute_canonical_serialized(val: &Value) -> String {
    crate::layer0::codec::canonical_json(val).unwrap_or_default()
}

pub fn compute_canonical_hash(val: &Value) -> String {
    let s = compute_canonical_serialized(val);
    blake3::hash(s.as_bytes()).to_hex().to_string()
}

pub fn clm_hash_string(s: &str) -> String {
    blake3::hash(s.as_bytes()).to_hex().to_string()
}

// ── Modality Manipulators ───────────────────────────────────────────────────

pub struct TabularManipulator;

impl TabularManipulator {
    pub fn parse_csv(text: &str) -> (Vec<String>, Vec<Vec<String>>) {
        let mut lines = text.lines().filter(|l| !l.trim().is_empty());
        let header_line = match lines.next() {
            Some(h) => h,
            None => return (vec![], vec![]),
        };
        let headers: Vec<String> = header_line.split(',').map(|s| s.trim().to_string()).collect();
        let mut rows = Vec::new();
        for line in lines {
            let row: Vec<String> = line.split(',').map(|s| s.trim().to_string()).collect();
            rows.push(row);
        }
        (headers, rows)
    }

    pub fn render_csv(headers: &[String], rows: &[Vec<String>]) -> String {
        let mut out = String::new();
        if !headers.is_empty() {
            out.push_str(&headers.join(","));
            out.push('\n');
        }
        for r in rows {
            out.push_str(&r.join(","));
            out.push('\n');
        }
        out
    }

    pub fn split(data: &str, params: &HashMap<String, Value>) -> Result<HashMap<String, String>, String> {
        let (headers, rows) = Self::parse_csv(data);
        if rows.is_empty() {
            let mut res = HashMap::new();
            res.insert("part_1".to_string(), Self::render_csv(&headers, &[]));
            res.insert("part_2".to_string(), Self::render_csv(&headers, &[]));
            return Ok(res);
        }

        if let Some(Value::Object(ratios)) = params.get("ratios") {
            let total = rows.len();
            let mut partitions = HashMap::new();
            let mut current_idx = 0;
            let ratio_list: Vec<(&String, f64)> = ratios
                .iter()
                .map(|(k, v)| (k, v.as_f64().unwrap_or(0.0)))
                .collect();

            for (i, (name, ratio)) in ratio_list.iter().enumerate() {
                let part_rows = if i == ratio_list.len() - 1 {
                    &rows[current_idx..]
                } else {
                    let count = (total as f64 * ratio).round() as usize;
                    let end = (current_idx + count).min(total);
                    let slice = &rows[current_idx..end];
                    current_idx = end;
                    slice
                };
                partitions.insert((*name).clone(), Self::render_csv(&headers, part_rows));
            }
            return Ok(partitions);
        }

        let mid = rows.len() / 2;
        let mut partitions = HashMap::new();
        partitions.insert("part_1".to_string(), Self::render_csv(&headers, &rows[..mid]));
        partitions.insert("part_2".to_string(), Self::render_csv(&headers, &rows[mid..]));
        Ok(partitions)
    }

    pub fn substitute(data: &str, params: &HashMap<String, Value>) -> Result<String, String> {
        let (mut headers, rows) = Self::parse_csv(data);
        if let (Some(target), Some(replacement)) = (
            params.get("target_column").and_then(|v| v.as_str()),
            params.get("replacement_column").and_then(|v| v.as_str()),
        ) {
            for h in &mut headers {
                if h == target {
                    *h = replacement.to_string();
                }
            }
        } else if let Some(Value::Object(rename)) = params.get("rename_columns") {
            for h in &mut headers {
                if let Some(new_name) = rename.get(h).and_then(|v| v.as_str()) {
                    *h = new_name.to_string();
                }
            }
        }
        Ok(Self::render_csv(&headers, &rows))
    }

    pub fn augment(data: &str, params: &HashMap<String, Value>) -> Result<String, String> {
        let (mut headers, rows) = Self::parse_csv(data);
        let col_name = params.get("column_name")
            .or_else(|| params.get("target_column"))
            .and_then(|v| v.as_str())
            .unwrap_or("augmented_col");
        let default_val = params.get("default_value")
            .or_else(|| params.get("constant_value"))
            .map(|v| match v {
                Value::String(s) => s.clone(),
                other => other.to_string(),
            })
            .unwrap_or_else(|| "active".to_string());

        headers.push(col_name.to_string());
        let mut new_rows = Vec::new();
        for r in rows {
            let mut nr = r;
            nr.push(default_val.clone());
            new_rows.push(nr);
        }
        Ok(Self::render_csv(&headers, &new_rows))
    }

    pub fn exclude(data: &str, params: &HashMap<String, Value>) -> Result<String, String> {
        let (headers, rows) = Self::parse_csv(data);
        let mut drop_cols = Vec::new();
        if let Some(Value::Array(cols)) = params.get("columns").or_else(|| params.get("target_columns")) {
            for c in cols {
                if let Some(s) = c.as_str() {
                    drop_cols.push(s.to_string());
                }
            }
        }

        let keep_indices: Vec<usize> = headers
            .iter()
            .enumerate()
            .filter(|(_, h)| !drop_cols.contains(h))
            .map(|(i, _)| i)
            .collect();

        let new_headers: Vec<String> = keep_indices.iter().map(|&i| headers[i].clone()).collect();
        let mut new_rows = Vec::new();
        for r in rows {
            let nr: Vec<String> = keep_indices.iter().filter(|&&i| i < r.len()).map(|&i| r[i].clone()).collect();
            new_rows.push(nr);
        }
        Ok(Self::render_csv(&new_headers, &new_rows))
    }

    pub fn invert(data: &str, params: &HashMap<String, Value>) -> Result<String, String> {
        let (headers, mut rows) = Self::parse_csv(data);
        let strategy = params.get("strategy").and_then(|v| v.as_str()).unwrap_or("reverse_rows");
        if strategy == "transpose" && !headers.is_empty() {
            let mut matrix = vec![headers];
            matrix.extend(rows);
            let cols = matrix[0].len();
            let mut transposed = Vec::new();
            for c in 0..cols {
                let mut row = Vec::new();
                for r in 0..matrix.len() {
                    if c < matrix[r].len() {
                        row.push(matrix[r][c].clone());
                    }
                }
                transposed.push(row);
            }
            let new_headers = transposed.first().cloned().unwrap_or_default();
            let new_rows = if transposed.len() > 1 { transposed[1..].to_vec() } else { vec![] };
            return Ok(Self::render_csv(&new_headers, &new_rows));
        }
        rows.reverse();
        Ok(Self::render_csv(&headers, &rows))
    }

    pub fn port(data: &str, params: &HashMap<String, Value>) -> Result<String, String> {
        let (headers, rows) = Self::parse_csv(data);
        let target_format = params.get("target_format").and_then(|v| v.as_str()).unwrap_or("json").to_lowercase();
        if target_format == "json" {
            let mut records = Vec::new();
            for r in rows {
                let mut obj = serde_json::Map::new();
                for (i, h) in headers.iter().enumerate() {
                    let val = if i < r.len() { r[i].clone() } else { String::new() };
                    obj.insert(h.clone(), Value::String(val));
                }
                records.push(Value::Object(obj));
            }
            return serde_json::to_string_pretty(&records).map_err(|e| e.to_string());
        }
        Ok(Self::render_csv(&headers, &rows))
    }
}


#[path = "action_manipulators.rs"]
mod action_manipulators;
#[allow(unused_imports)]
pub use action_manipulators::*;
#[path = "action_rules.rs"]
mod action_rules;
#[allow(unused_imports)]
pub use action_rules::*;
#[path = "action_sandwich.rs"]
mod action_sandwich;
#[allow(unused_imports)]
pub use action_sandwich::*;