clm-kernel 0.0.23

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
use std::path::Path;
use std::io::Read;
use std::sync::Mutex;
use std::collections::HashMap;
use crate::Commands;

pub static EXEC_MUTEX: Mutex<()> = Mutex::new(());

pub fn resolve_json_ast(json_or_file: &str) -> String {
    let trimmed = json_or_file.trim();
    if trimmed.starts_with('{') || trimmed.starts_with('[') {
        trimmed.to_string()
    } else if Path::new(json_or_file).exists() {
        clm_rust_core::yaml::parse_to_mcards(json_or_file).unwrap_or_else(|_| "null".to_string())
    } else {
        trimmed.to_string()
    }
}

pub fn walk_dir_for_md(root: &Path, current: &Path, files: &mut HashMap<String, String>) {
    if let Ok(entries) = std::fs::read_dir(current) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                walk_dir_for_md(root, &path, files);
            } else if path.extension().map(|e| e == "md").unwrap_or(false)
                && let Ok(content) = std::fs::read_to_string(&path) {
                    let relative = path.strip_prefix(root)
                        .map(|p| p.to_string_lossy().to_string())
                        .unwrap_or_else(|_| path.to_string_lossy().to_string());
                    files.insert(relative, content);
                }
        }
    }
}

pub fn run_logic(command: Commands, clm_root: &str) -> (i32, String, String) {
    let mut stdout = String::new();
    let mut stderr = String::new();

    match command {
        Commands::ExecPolyglot { file, payload } => {
            match clm_rust_core::polyglot::exec_polyglot(&file, payload.as_deref(), clm_root) {
                Ok((exit_code, out)) => {
                    stdout.push_str(&out);
                    (exit_code, stdout, stderr)
                }
                Err(e) => {
                    stderr.push_str(&e);
                    (1, stdout, stderr)
                }
            }
        }
        Commands::ExecRuntimeScript => {
            let mut stdin_json = String::new();
            if let Err(e) = std::io::stdin().read_to_string(&mut stdin_json) {
                stderr.push_str(&format!("Failed to read stdin: {}", e));
                return (1, stdout, stderr);
            }
            match serde_json::from_str::<clm_rust_core::polyglot::RuntimeScriptInput>(&stdin_json) {
                Ok(input) => {
                    match clm_rust_core::polyglot::exec_runtime_script(&input) {
                        Ok(output) => {
                            let json = serde_json::to_string(&output).unwrap_or_else(|e| format!("{{\"exit_code\":1,\"stdout\":\"\",\"stderr\":\"Serialize error: {}\"}}", e));
                            stdout.push_str(&json);
                            (0, stdout, stderr)
                        }
                        Err(e) => {
                            stderr.push_str(&e);
                            (1, stdout, stderr)
                        }
                    }
                }
                Err(e) => {
                    stderr.push_str(&format!("Failed to parse JSON input: {}", e));
                    (1, stdout, stderr)
                }
            }
        }
        Commands::Mcard { args, json } => (handle_mcard_cmd(args, json, &mut stdout, &mut stderr), stdout, stderr),
        Commands::Action { args, json } => (handle_action_cmd(&args, json, &mut stdout, &mut stderr), stdout, stderr),
        Commands::Workflow { args, json } => (handle_workflow_cmd(&args, json, &mut stdout, &mut stderr), stdout, stderr),
        Commands::Layer5 { args, json } => (handle_layer5_cmd(&args, json, &mut stdout, &mut stderr), stdout, stderr),
        other => {
            if let Some(rc) = handle_check_cmd(&other, clm_root, &mut stdout, &mut stderr) {
                return (rc, stdout, stderr);
            }
            if let Some(rc) = handle_memorize_cmd(&other, clm_root, &mut stdout, &mut stderr) {
                return (rc, stdout, stderr);
            }
            if let Some(rc) = handle_db_cmd(&other, clm_root, &mut stdout, &mut stderr) {
                return (rc, stdout, stderr);
            }
            if let Some(rc) = handle_substrate_cmd(&other, clm_root, &mut stdout, &mut stderr) {
                return (rc, stdout, stderr);
            }
            if let Some(rc) = handle_services_cmd(&other, clm_root, &mut stdout, &mut stderr) {
                return (rc, stdout, stderr);
            }
            (0, stdout, stderr)
        }
    }
}

#[path = "helpers_mcard.rs"]
mod helpers_mcard;
use helpers_mcard::*;
#[path = "helpers_db.rs"]
mod helpers_db;
use helpers_db::*;
#[path = "helpers_substrate.rs"]
mod helpers_substrate;
use helpers_substrate::*;
#[path = "helpers_services.rs"]
mod helpers_services;
use helpers_services::*;
#[path = "helpers_action.rs"]
mod helpers_action;
pub use helpers_action::*;
#[path = "helpers_workflow.rs"]
mod helpers_workflow;
pub use helpers_workflow::*;
#[path = "helpers_check.rs"]
mod helpers_check;
use helpers_check::*;
#[path = "helpers_memorize.rs"]
mod helpers_memorize;
use helpers_memorize::*;