clm-kernel 0.0.7

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
// evaluator/dispatch/orchestrator.rs
// ─────────────────────────────────────────────────────────────
// Central CLM YAML → execution router.
//
// try_internal_dispatch parses a CLM YAML file, determines the
// execution strategy (native Rust, non-Rust runtime, fallback shell),
// and orchestrates pre/postcondition verification and VCard emission.
// ─────────────────────────────────────────────────────────────

use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use serde_json::Value;
use crate::fs_utils::compute_sha256_hex;
use super::{get_clm_args, get_internal_registry};
use super::runtime;

/// Emit a CLM_TRACE_JSON line to stdout when running in gateway mode (CLM_DISPATCH_SOCK set).
/// This allows the WebSocket gateway to stream real-time Petri Net trace events to clients.
fn emit_gateway_trace(transition: &str, phase: &str, exit_code: i32) {
    if crate::cross_cutting::context::CLMContext::get_scoped_or_env("CLM_DISPATCH_SOCK").is_some()
        && crate::cross_cutting::context::CLMContext::get_scoped_or_env("CLM_NESTED_EXEC").is_none()
    {
        let trace = serde_json::json!({
            "type": "trace",
            "transition": transition,
            "phase": phase,
            "exit_code": exit_code,
            "agent": crate::cross_cutting::context::CLMContext::get_scoped_or_env("ACTIVE_AGENT_DID")
                .unwrap_or_else(|| "did:clm:bootstrap".to_string()),
        });
        println!("CLM_TRACE_JSON:{}", trace);
        use std::io::Write;
        std::io::stdout().flush().ok();
    }
}

pub(crate) fn try_internal_dispatch(
    clm_root: &Path,
    clm_file: &str,
    payload: Option<&str>,
    post_rc: Option<i32>,
    quiet: bool,
) -> Result<i32, String> {
    let root_ctx = crate::fiber::Context::root();
    try_internal_dispatch_with_context(&root_ctx, clm_root, clm_file, payload, post_rc, quiet)
}

pub(crate) fn try_internal_dispatch_with_context(
    ctx: &crate::fiber::Context,
    clm_root: &Path,
    clm_file: &str,
    payload: Option<&str>,
    post_rc: Option<i32>,
    quiet: bool,
) -> Result<i32, String> {
    let clm_path = if Path::new(clm_file).is_absolute() {
        PathBuf::from(clm_file)
    } else {
        clm_root.join(clm_file)
    };

    if !clm_path.exists() {
        return Err(format!("CLM file not found: {:?}", clm_path));
    }

    let content = fs::read_to_string(&clm_path)
        .map_err(|e| format!("Failed to read CLM file: {}", e))?;
    let data: serde_yaml::Value = match serde_yaml::from_str::<serde_yaml::Value>(&content) {
        Ok(d) => {
            // Check if this is actually a CLM with concrete_impl
            let cd = d.get("clm").unwrap_or(&d);
            if cd.get("concrete_impl").is_some() {
                d
            } else {
                // YAML parsed but no concrete_impl — might be XML or other format
                // that YAML mis-parsed as a plain string
                let normalized_path = crate::normalizer::normalize_syntax(
                    &clm_path.to_string_lossy(),
                    clm_root.to_str().unwrap_or(""),
                ).map_err(|_| "No concrete_impl — not an internal_reference CLM".to_string())?;
                let normalized_content = fs::read_to_string(&normalized_path)
                    .map_err(|e| format!("Failed to read normalized file: {}", e))?;
                serde_yaml::from_str(&normalized_content)
                    .map_err(|e| format!("Failed to parse normalized CLM YAML: {}", e))?
            }
        }
        Err(_) => {
            // Non-YAML file (JSONL, JSON, XML, TXT): normalize to YAML first
            let normalized_path = crate::normalizer::normalize_syntax(
                &clm_path.to_string_lossy(),
                clm_root.to_str().unwrap_or(""),
            ).map_err(|e| format!("Failed to parse CLM YAML: {} (normalization also failed: {})", "YAML parse error", e))?;
            let normalized_content = fs::read_to_string(&normalized_path)
                .map_err(|e| format!("Failed to read normalized file: {}", e))?;
            serde_yaml::from_str(&normalized_content)
                .map_err(|e| format!("Failed to parse normalized CLM YAML: {}", e))?
        }
    };

    let clm_data = data.get("clm").unwrap_or(&data);
    let concrete_impl = match clm_data.get("concrete_impl") {
        Some(val) => val,
        None => return Err("No concrete_impl — not an internal_reference CLM".to_string()),
    };

    let operation = concrete_impl.get("operation")
        .and_then(|v| v.as_str())
        .unwrap_or("");

    // Intercept internal gate CLMs that have no script body on disk.
    // These are handled directly by Rust code.
    if operation == "verify_precondition" {
        if !quiet {
            eprintln!("[clmEval-rs] ⚡ Native gate intercept: verify_precondition (zero-fork)");
        }
        match super::super::verify::verify_precondition_internal(clm_root, clm_file, quiet) {
            Ok(()) => return Ok(0),
            Err(e) => {
                eprintln!("[clmEval-rs] ❌ Pre-Gate Failed: {}", e);
                return Ok(1);
            }
        }
    }
    if operation == "validate_postcondition" {
        if !quiet {
            eprintln!("[clmEval-rs] ⚡ Native gate intercept: validate_postcondition (zero-fork)");
        }
        let rc = post_rc.unwrap_or(0);
        let content = format!("exit code: {}", rc);
        let content_hash = format!("sha256:{}", compute_sha256_hex(content.as_bytes()));
        
        let mut op = "unknown".to_string();
        let mut impl_hash = "unknown".to_string();
        if let Some(pay) = payload {
            let pay_path = if Path::new(pay).is_absolute() {
                PathBuf::from(pay)
            } else {
                clm_root.join(pay)
            };
            
            let resolved_pay_path = if pay_path.exists() {
                pay_path
            } else if pay.starts_with("clm/") {
                clm_root.join(&pay[4..])
            } else {
                pay_path
            };
            
            if resolved_pay_path.exists() {
                if let Ok(c) = fs::read_to_string(&resolved_pay_path)
                    && let Ok(yaml) = serde_yaml::from_str::<serde_yaml::Value>(&c) {
                        let inner = yaml.get("clm").unwrap_or(&yaml);
                        if let Some(concrete) = inner.get("concrete_impl")
                            && let Some(operation_val) = concrete.get("operation").and_then(|v| v.as_str()) {
                                op = operation_val.to_string();
                            }
                    }
                if let Ok(bytes) = fs::read(&resolved_pay_path) {
                    let normalized: Vec<u8> = bytes.into_iter().filter(|&b| b != b'\r').collect();
                    impl_hash = format!("sha256:{}", compute_sha256_hex(&normalized));
                }
            }
        }
        
        let a_hash = env::var("CLM_ACTIVE_A_HASH").unwrap_or_default();
        let c_hash = env::var("CLM_ACTIVE_C_HASH").unwrap_or_default();
        
        let extra_yaml = format!(
            "content: \"{}\"\noperation: \"{}\"\nimpl_hash: \"{}\"",
            content, op, impl_hash
        );
        
        match crate::db::emit_exec_mcard(
            "validate_postcondition",
            &content_hash,
            &extra_yaml,
            &a_hash,
            &c_hash,
            clm_root.to_str().unwrap_or(""),
        ) {
            Ok(exec_mcard_path) => {
                if !quiet {
                    eprintln!("[VCARD POST] ✅ Execution MCard generated: {}", exec_mcard_path);
                    eprintln!("[VCARD POST]   g_time: {}", crate::fs_utils::compute_g_time());
                    eprintln!("[VCARD POST]   content_hash: {}", content_hash);
                }
            }
            Err(e) => {
                eprintln!("[VCARD POST] ❌ Failed to generate execution MCard: {}", e);
                return Ok(1);
            }
        }
        
        return Ok(0);
    }

    if operation.starts_with("ci_validate_") {
        let content_type = concrete_impl.get("content_type");
        let mime = content_type
            .and_then(|ct| ct.get("mime"))
            .and_then(|m| m.as_str())
            .unwrap_or("");
        
        let payload_file = payload.ok_or_else(|| "No payload file specified for validation".to_string())?;
        
        if !quiet {
            eprintln!("[clmEval-rs] ⚡ Native validation intercept: {} for file {} (zero-fork)", operation, payload_file);
        }
        
        if let Err(e) = super::super::verify::verify_precondition_internal(clm_root, clm_file, quiet) {
            eprintln!("CRITICAL: Pre-Gate Failed: {}", e);
            return Ok(1);
        }

        let is_valid = crate::mime::validate(clm_root.to_str().unwrap_or(""), payload_file, Some(mime));
        let exit_code = if is_valid { 0 } else { 1 };

        let output = "";
        let clm_root_str = clm_root.to_str().unwrap_or("");
        
        if exit_code == 0 {
            let a_hash_str = clm_data.get("_meta").and_then(|m| m.get("a_hash")).and_then(|v| v.as_str())
                .map(|s| s.to_string())
                .unwrap_or_else(|| {
                    let spec = clm_data.get("abstract_spec").cloned().unwrap_or(serde_yaml::Value::Null);
                    let serialized = serde_yaml::to_string(&spec).unwrap_or_default();
                    format!("sha256:{}", compute_sha256_hex(serialized.replace('\r', "").as_bytes()))
                });
            
            let c_hash_str = clm_data.get("_meta").and_then(|m| m.get("c_hash")).and_then(|v| v.as_str())
                .map(|s| s.to_string())
                .unwrap_or_else(|| {
                    let spec = clm_data.get("concrete_impl").cloned().unwrap_or(serde_yaml::Value::Null);
                    let serialized = serde_yaml::to_string(&spec).unwrap_or_default();
                    format!("sha256:{}", compute_sha256_hex(serialized.replace('\r', "").as_bytes()))
                });

            let output_hash = format!("sha256:{}", compute_sha256_hex(output.as_bytes()));
            let extra_yaml = format!("content: |\n  {}", output.replace('\n', "\n  "));
            let _ = crate::db::emit_exec_mcard(
                operation,
                &output_hash,
                &extra_yaml,
                &a_hash_str,
                &c_hash_str,
                clm_root_str,
            );
        }
        
        return Ok(exit_code);
    }

    let runtime_str = concrete_impl.get("runtime")
        .and_then(|v| v.as_str())
        .unwrap_or("");

    let process = match concrete_impl.get("process") {
        Some(p) => p,
        None => return Err("No process block in concrete_impl".to_string()),
    };

    let process_type = process.get("type")
        .and_then(|v| v.as_str())
        .unwrap_or("");

    let is_rust = runtime_str == "rust" && process_type == "internal_reference";
    let is_non_rust_runtime = runtime_str == "python"
        || runtime_str == "node"
        || runtime_str == "javascript"
        || runtime_str == "shell"
        || runtime_str == "polyglot"
        || runtime_str == "system"
        || runtime_str == "go"
        || runtime_str == "llm"
        || runtime_str == "open_interpreter"
        || (runtime_str == "rust" && process_type == "rust")
        || (runtime_str == "rust" && !is_rust && process_type != "internal_reference")
        || process_type == "compose"
        || process_type == "polyglot"
        || runtime_str == "compose";

    if !is_rust && !is_non_rust_runtime {
        return Err(format!("runtime is '{}', not 'rust' — falling through to bash", runtime_str));
    }

    if is_rust
        && process_type != "internal_reference" {
            eprintln!("[DEBUG] process_type is '{}', not 'internal_reference'", process_type);
            return Err(format!("process.type is '{}', not 'internal_reference'", process_type));
        }

    let mut module_path_opt = None;
    let mut registered_func = None;

    if is_rust {
        let m_path = process.get("module")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                eprintln!("[DEBUG] No process.module specified");
                "No process.module specified for internal_reference".to_string()
            })?;
        module_path_opt = Some(m_path);
        registered_func = get_internal_registry(m_path);

        if !quiet {
            eprintln!("[clmEval-rs] ⚡ Internal dispatch: {} (zero-overhead)", m_path);
        }
    }

    let _payload_guard = if let Some(pay) = payload {
        let mut overrides = crate::cross_cutting::context::CLMContextOverrides::default();
        overrides
            .scoped_vars
            .insert("CLM_PAYLOAD".to_string(), pay.to_string());
        let child = crate::cross_cutting::context::CLMContext::current().with_child_scope(overrides);
        Some(child.enter_scope())
    } else {
        None
    };

    let skip_conditions = crate::cross_cutting::context::CLMContext::get_scoped_or_env("CLM_SKIP_CONDITIONS")
        .as_deref()
        == Some("1");

    if !skip_conditions {
        emit_gateway_trace("verify_precondition", "pre", 0);
        match crate::evaluator::compose::compose_conditions(clm_root, clm_file, "pre", 0, quiet) {
            Ok(0) => {}
            Ok(rc) => {
                eprintln!("CRITICAL: Pre-Gate Failed.");
                return Ok(rc);
            }
            Err(e) if e.contains("SovereignSubprocessForbiddenError") => {
                return Err(e);
            }
            Err(e) => {
                eprintln!("CRITICAL: Pre-Gate Failed: {}", e);
                return Ok(1);
            }
        }
    }

    let clm_root_str = clm_root.to_str().unwrap_or("");
    let clm_file_str = clm_path.to_str().unwrap_or("");

    include!("orchestrator_post.rs")
}