clm-kernel 0.0.19

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
// evaluator/dispatch/runtime.rs
// ─────────────────────────────────────────────────────────────
// Non-Rust runtime execution and helper utilities.
//
// Functions:
//   evaluate_non_rust_runtime — dispatch to python, node, shell, compose, system, go, llm, etc.
//   run_fallback_internal_reference — source bash test scripts for unregistered modules
//   find_test_script — recursive directory search for test script files
//   resolve_script_body — resolve inline script or script_handle to content
//   find_script_file — recursive search for script files by name + extension
//   run_command_and_capture — execute a Command and capture stdout/stderr
// ─────────────────────────────────────────────────────────────

use std::fs;
use std::path::{Path, PathBuf};
use crate::evaluator::run_evaluation;

// ── Public API (used by orchestrator) ───────────────────────

pub(crate) fn evaluate_non_rust_runtime(
    clm_root: &Path,
    clm_file: &str,
    process_type: &str,
    runtime: &str,
    concrete_impl: &serde_yaml::Value,
    payload: Option<&str>,
    post_rc: Option<i32>,
    quiet: bool,
) -> Result<(i32, String), String> {
    let clm_root_str = clm_root.to_str().ok_or("Invalid CLM_ROOT path")?;
    let process = concrete_impl.get("process")
        .ok_or_else(|| "No process block in concrete_impl".to_string())?;
    
    if process_type == "compose" || runtime == "compose" {
        if !quiet {
            eprintln!("[PAL] Dispatching: type=compose");
        }
        let steps = process.get("steps")
            .and_then(|v| v.as_sequence())
            .ok_or_else(|| "No steps in compose process block".to_string())?;
        
        let sp_batch_name = format!("sp_compose_batch_{:x}", rand::random::<u32>());
        let mut batch_sp = crate::fiber::disposable::TriDbSavepointGuard::with_pillars(
            clm_root_str,
            &sp_batch_name,
            &["content_memory", "agent_identities"],
        )
        .ok();

        let mut exit_code = 0;
        let mut step_outputs = String::new();
        for (i, step_ref_val) in steps.iter().enumerate() {
            if let Some(step_ref) = step_ref_val.as_str() {
                let step_path = if Path::new(step_ref).is_absolute() {
                    PathBuf::from(step_ref)
                } else {
                    let joined = clm_root.join(step_ref);
                    if joined.exists() {
                        joined
                    } else if let Some(suffix) = step_ref.strip_prefix("clm/") {
                        clm_root.join(suffix)
                    } else {
                        joined
                    }
                };
                if !step_path.exists() {
                    if let Some(ref mut sp) = batch_sp {
                        let _ = sp.rollback();
                    }
                    return Err(format!("[COMPOSE_FN] Step {}: failed to load '{}'", i + 1, step_path.to_string_lossy()));
                }
                if !quiet {
                    eprintln!("[COMPOSE_FN]   Step {}: (from {})", i + 1, step_ref);
                }
                let mut overrides = crate::cross_cutting::context::CLMContextOverrides {
                    project_root: Some(clm_root.to_path_buf()),
                    nested_exec: Some(true),
                    ..Default::default()
                };
                if let Some(pay) = payload {
                    overrides.scoped_vars.insert("CLM_PAYLOAD".to_string(), pay.to_string());
                }
                if quiet {
                    overrides.scoped_vars.insert("CLM_QUIET".to_string(), "1".to_string());
                }
                let step_ctx = crate::cross_cutting::context::CLMContext::current().with_child_scope(overrides);
                let sp_step_name = format!("sp_compose_step_{}_{:x}", i + 1, rand::random::<u32>());
                let mut step_sp = crate::fiber::disposable::TriDbSavepointGuard::with_pillars(
                    clm_root_str,
                    &sp_step_name,
                    &["content_memory", "agent_identities"],
                )
                .ok();
                match crate::evaluator::evaluate_card_with_context(&step_ctx, &step_path) {
                    Ok(0) => {
                        if let Some(ref mut sp) = step_sp {
                            let _ = sp.commit();
                        }
                        step_outputs.push_str(&format!("step_{}:ok\n", i + 1));
                    }
                    Ok(rc) => {
                        if let Some(ref mut sp) = step_sp {
                            let _ = sp.rollback();
                        }
                        exit_code = rc;
                        break;
                    }
                    Err(e) => {
                        if let Some(ref mut sp) = step_sp {
                            let _ = sp.rollback();
                        }
                        if let Some(ref mut sp) = batch_sp {
                            let _ = sp.rollback();
                        }
                        return Err(e);
                    }
                }
            }
        }
        if exit_code == 0 {
            if let Some(ref mut sp) = batch_sp {
                let _ = sp.commit();
            }
        } else if let Some(ref mut sp) = batch_sp {
            let _ = sp.rollback();
        }
        return Ok((exit_code, step_outputs));
    }

    if runtime == "polyglot" || process_type == "polyglot" {
        return match crate::polyglot::exec_polyglot(clm_file, payload, clm_root_str) {
            Ok((code, output)) => Ok((code, output)),
            Err(e) => Err(e),
        };
    }

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

    let project_root_str = std::env::var("PROJECT_ROOT").unwrap_or_else(|_| {
        clm_root.parent()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|| clm_root_str.to_string())
    });

    if runtime == "system" && process_type != "python" && process_type != "node" && process_type != "javascript" && process_type != "shell" {
        let op_clm_path = clm_root.join("computable_knowledge/Abstract/layer5_system/bootstrap/governance").join(format!("{}.yaml", entry_point));
        let op_clm_path = if op_clm_path.exists() {
            op_clm_path
        } else {
            PathBuf::from(&project_root_str).join("clm/computable_knowledge/Abstract/layer5_system/bootstrap/governance").join(format!("{}.yaml", entry_point))
        };
        
        if !op_clm_path.exists() {
            return Err(format!("[EXEC] System operation CLM not found: {:?}", op_clm_path));
        }
        
        let mut overrides = crate::cross_cutting::context::CLMContextOverrides {
            target_file: Some(PathBuf::from(clm_file)),
            ..Default::default()
        };
        overrides
            .scoped_vars
            .insert("TARGET_FILE".to_string(), clm_file.to_string());
        let child_ctx = crate::cross_cutting::context::CLMContext::current().with_child_scope(overrides);
        let _target_guard = child_ctx.enter_scope();
        
        if !quiet {
            eprintln!("[clmEval-rs] Dispatching to system governance operation: {} (via CLM evaluate)", entry_point);
        }
        
        let rc = run_evaluation(
            clm_root,
            Some(op_clm_path.to_str().ok_or("Invalid path")?),
            payload,
            None,
            quiet,
            None,
        )?;
        
        return Ok((rc, "".to_string()));
    }

    let identity_db = crate::db::get_db_path("agent_identities", clm_root_str).to_string_lossy().to_string();
    let content_db = crate::db::get_db_path("content_memory", clm_root_str).to_string_lossy().to_string();
    let execution_log_db = crate::db::get_db_path("execution_log", clm_root_str).to_string_lossy().to_string();

    let mlp_json = serde_json::json!({
      "$schema": "https://mcard.org/schemas/mlp-context-v1.json",
      "context": {
        "operation": entry_point,
        "clm_file": clm_file,
        "a_hash": crate::cross_cutting::context::CLMContext::get_scoped_or_env("CLM_ACTIVE_A_HASH").unwrap_or_default(),
        "c_hash": crate::cross_cutting::context::CLMContext::get_scoped_or_env("CLM_ACTIVE_C_HASH").unwrap_or_default(),
        "payload": payload.unwrap_or("")
      },
      "environment": {
        "CLM_ROOT": clm_root_str,
        "PROJECT_ROOT": project_root_str,
        "g_time": crate::fs_utils::compute_g_time()
      },
      "databases": {
        "identity_db": identity_db,
        "content_db": content_db,
        "execution_log_db": execution_log_db
      }
    });
    let mlp_json_str = serde_json::to_string(&mlp_json).unwrap_or_default();

    let mut env_vars = std::collections::HashMap::new();
    if let Some(pay) = payload {
        env_vars.insert("CLM_PAYLOAD".to_string(), pay.to_string());
        env_vars.insert("TARGET_FILE".to_string(), pay.to_string());
    }
    env_vars.insert("CLM_ROOT".to_string(), clm_root_str.to_string());
    env_vars.insert("PROJECT_ROOT".to_string(), project_root_str);
    env_vars.insert("CLM_MLP_CONTEXT".to_string(), mlp_json_str);

    if let Some(val) = crate::cross_cutting::context::CLMContext::get_scoped_or_env("CLM_ACTIVE_A_HASH") {
        env_vars.insert("CLM_ACTIVE_A_HASH".to_string(), val);
    }
    if let Some(val) = crate::cross_cutting::context::CLMContext::get_scoped_or_env("CLM_ACTIVE_C_HASH") {
        env_vars.insert("CLM_ACTIVE_C_HASH".to_string(), val);
    }
    if let Some(val) = crate::cross_cutting::context::CLMContext::get_scoped_or_env("CLM_ACTIVE_B_HASH") {
        env_vars.insert("CLM_ACTIVE_B_HASH".to_string(), val);
    }

    for (k, v) in crate::utils::collect_clm_mock_vars() {
        env_vars.insert(k, v);
    }

    for var_name in crate::layer0::runtime_contract::ENV_PASSTHROUGH_WHITELIST {
        if let Some(val) = crate::cross_cutting::context::CLMContext::get_scoped_or_env(var_name) {
            env_vars.entry(var_name.to_string()).or_insert(val);
        }
    }

    if entry_point.starts_with("test_")
        && (process_type == "shell" || runtime == "shell" || runtime == "system")
    {
        return crate::integration::shell_runner::run_shell_test(clm_file, entry_point, None)
            .map(|out| (0, out));
    }

    if entry_point.starts_with("gate_") || entry_point == "agent_refinement_loop" {
        return evaluate_native_governance_gate(clm_root, entry_point, payload);
    }

    if runtime == "go" || runtime == "llm" || (runtime == "rust" && process_type == "rust") {
        let dispatcher = crate::runtime_adapter::RuntimeDispatcher::with_defaults();
        let adapter_key = if runtime == "rust" { "echo" } else { runtime };
        let input_val = serde_json::Value::String(payload.unwrap_or("").to_string());
        if dispatcher.has(adapter_key) {
            let val = dispatcher.dispatch(adapter_key, entry_point, &input_val)?;
            let out = match val {
                serde_json::Value::String(s) => s,
                other => other.to_string(),
            };
            return Ok((0, out));
        }
        return Ok((0, format!("[ADAPTER:{}] executed {}\n", runtime, entry_point)));
    }

    if runtime == "open_interpreter" {
        let py_cmd = crate::polyglot::resolve_python_cmd(clm_root_str);
        let mut cmd_args = vec![
            "-c".to_string(),
            "import sys; from clm.layer3.open_interpreter_adapter import main_cli; sys.exit(main_cli())".to_string(),
            clm_file.to_string(),
            entry_point.to_string(),
        ];
        if let Some(p) = payload {
            cmd_args.push(p.to_string());
        }
        let mut custom_env_vars = env_vars.clone();
        custom_env_vars.insert("PYTHONPATH".to_string(), format!("{}/kernel/clm_py_core", clm_root_str));
        custom_env_vars.insert("CLM_NO_SANDBOX".to_string(), "true".to_string());
        
        let mut overrides = crate::cross_cutting::context::CLMContextOverrides::default();
        overrides
            .scoped_vars
            .insert("CLM_NO_SANDBOX".to_string(), "true".to_string());
        let child_ctx = crate::cross_cutting::context::CLMContext::current().with_child_scope(overrides);
        let _no_sandbox_guard = child_ctx.enter_scope();
        
        let cmd = crate::polyglot::create_sandboxed_command(&py_cmd, &cmd_args, &custom_env_vars, clm_root_str);
        
        return run_command_and_capture(cmd);
    }

    let runner_type = if process_type == "python" || process_type == "node" || process_type == "javascript" || process_type == "shell" {
        process_type
    } else {
        runtime
    };

    // Otherwise, it is a script-based runtime: python, node, javascript, shell
    let script_content = resolve_script_body(clm_root, process, runner_type)?;

    let mut args: Vec<String> = match payload {
        Some(p) => p.split_whitespace().map(|s| s.to_string()).collect(),
        None => Vec::new(),
    };
    // For post-condition gates, append the exit code so shell gate functions
    // receive both the payload (CLM file) and the evaluation exit code.
    if let Some(rc) = post_rc {
        args.push(rc.to_string());
    }

    match runner_type {
        "python" => {
            let py_cmd = crate::polyglot::resolve_python_cmd(clm_root_str);
            let mut cmd_args = vec!["-c".to_string(), script_content];
            cmd_args.extend(args);
            let mut py_env_vars = env_vars.clone();
            py_env_vars.insert("PYTHONPATH".to_string(), format!("{}/kernel/clm_py_core", clm_root_str));
            let cmd = crate::polyglot::create_sandboxed_command(&py_cmd, &cmd_args, &py_env_vars, clm_root_str);
            run_command_and_capture(cmd)
        }
        "node" | "javascript" => {
            let rand_id = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0);
            let pid = std::process::id();
            let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
            let _ = std::fs::create_dir_all(&temp_dir);
            let temp_file = temp_dir.join(format!("clm_node_inline_{}_{}.cjs", rand_id, pid));
            std::fs::write(&temp_file, &script_content).map_err(|e| format!("Failed to write node inline script: {}", e))?;

            let mut cmd_args = vec!["--trace-uncaught".to_string(), temp_file.to_string_lossy().to_string()];
            cmd_args.extend(args);
            let cmd = crate::polyglot::create_sandboxed_command("node", &cmd_args, &env_vars, clm_root_str);
            let res = run_command_and_capture(cmd);
            let _ = std::fs::remove_file(&temp_file);
            res
        }
        "shell" => {
            let header = format!(
                "clm_root() {{ echo \"{}\"; }}\n\
                 export -f clm_root\n",
                clm_root_str
            );
            let wrapped_body = if !entry_point.is_empty() {
                format!("_clm_run_block() {{\n{}\n}}\n_clm_run_block \"$@\"\nif type -t {1} &>/dev/null; then {1} \"$@\"; fi\n", script_content, entry_point)
            } else {
                format!("_clm_run_block() {{\n{}\n}}\n_clm_run_block \"$@\"\n", script_content)
            };
            let full_script = header + &wrapped_body;
            let mut cmd_args = vec!["-s".to_string(), "--".to_string()];
            cmd_args.extend(args);
            let mut cmd = crate::polyglot::create_sandboxed_command(&crate::fs_utils::get_bash_command(), &cmd_args, &env_vars, clm_root_str);
            let prog = cmd.get_program().to_string_lossy().to_string();
            let args_vec: Vec<String> = cmd.get_args().map(|a| a.to_string_lossy().to_string()).collect();
            crate::cross_cutting::context::CLMContext::check_current_process_policy(&prog, &args_vec)
                .map_err(|e| e.to_string())?;
            cmd.stdin(std::process::Stdio::piped());
            cmd.stdout(std::process::Stdio::piped());
            cmd.stderr(std::process::Stdio::piped());

            let mut child = cmd.spawn().map_err(|e| format!("Failed to spawn bash: {}", e))?;
            if let Some(mut stdin) = child.stdin.take() {
                use std::io::Write;
                stdin.write_all(full_script.as_bytes()).map_err(|e| format!("Failed to write to bash stdin: {}", e))?;
            }

            let output = child.wait_with_output().map_err(|e| format!("Failed to wait for bash: {}", e))?;
            if !output.stderr.is_empty() {
                use std::io::Write;
                std::io::stderr().write_all(&output.stderr).ok();
            }
            let exit_code = output.status.code().unwrap_or(1);
            let stdout_str = String::from_utf8_lossy(&output.stdout).to_string();
            Ok((exit_code, stdout_str))
        }
        other => Err(format!("Unsupported non-rust runtime: {}", other)),
    }
}


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