clm-kernel 0.0.14

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
use std::env;
use std::fs;
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};

pub mod cli;
pub mod dispatch;
pub mod compose;
pub mod verify;

pub(crate) use dispatch::{get_internal_registry, try_internal_dispatch};


pub use crate::db::{thread_local_depth_get, thread_local_depth_increment, thread_local_depth_decrement};

fn copy_directory_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
    fs::create_dir_all(dst)?;
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let source = entry.path();
        let target = dst.join(entry.file_name());
        if source.is_dir() {
            copy_directory_recursive(&source, &target)?;
        } else {
            fs::copy(source, target)?;
        }
    }
    Ok(())
}

#[cfg(unix)]
fn create_symlink(src: &Path, dst: &Path) -> std::io::Result<()> {
    std::os::unix::fs::symlink(src, dst)
}

#[cfg(windows)]
fn create_symlink(src: &Path, dst: &Path) -> std::io::Result<()> {
    if src.is_dir() {
        std::os::windows::fs::symlink_dir(src, dst).or_else(|_| {
            Command::new("cmd")
                .args(["/c", "mklink", "/j", &dst.to_string_lossy(), &src.to_string_lossy()])
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status()
                .map(|_| ())
        })
    } else {
        std::os::windows::fs::symlink_file(src, dst)
            .or_else(|_| fs::hard_link(src, dst))
            .or_else(|_| fs::copy(src, dst).map(|_| ()))
    }
}

pub fn resolve_clm_root() -> Result<PathBuf, String> {
    if let Ok(val) = env::var("CLM_ROOT") {
        let path = crate::fs_utils::normalize_windows_path(Path::new(&val));
        if path.is_absolute() {
            return Ok(path);
        } else if let Ok(curr) = env::current_dir() {
            return Ok(curr.join(path));
        } else {
            return Ok(path);
        }
    }

    let mut dir = env::current_dir().map_err(|e| format!("Failed to get current dir: {}", e))?;
    loop {
        if dir.join("computable_knowledge/Concrete/layer5_system/bootstrap/evaluator").is_dir()
            || dir.join("computable_knowledge/Abstract/layer5_system/bootstrap/evaluator").is_dir() {
            return Ok(dir);
        }
        if dir.join("clm/computable_knowledge/Concrete/layer5_system/bootstrap/evaluator").is_dir()
            || dir.join("clm/computable_knowledge/Abstract/layer5_system/bootstrap/evaluator").is_dir() {
            return Ok(dir.join("clm"));
        }
        if let Some(parent) = dir.parent() {
            dir = parent.to_path_buf();
        } else {
            break;
        }
    }
    Err("Cannot resolve CLM_ROOT. Please set CLM_ROOT environment variable.".to_string())
}

pub fn run_batch_evaluation(
    clm_root: &Path,
    files: &[String],
    quiet: bool,
) -> Result<i32, String> {
    run_batch_evaluation_impl(clm_root, files, quiet, 1)
}

pub fn run_batch_evaluation_parallel(
    clm_root: &Path,
    files: &[String],
    quiet: bool,
    threads: usize,
) -> Result<i32, String> {
    run_batch_evaluation_impl(clm_root, files, quiet, threads)
}

fn run_batch_evaluation_impl(
    clm_root: &Path,
    files: &[String],
    quiet: bool,
    threads: usize,
) -> Result<i32, String> {
    use std::time::Instant;
    use std::thread;

    #[cfg(windows)]
    let _ = threads;
    #[cfg(windows)]
    let threads = 1;

    if let Ok(mut lock) = crate::cpn::vm::EVAL_CALLBACK.write() {
        *lock = Some(run_evaluation);
    }

    // Hoist per-invocation overhead: integrity + cache run once for the entire batch
    verify::verify_integrity(clm_root)?;
    cli::compile_evaluator_cache(clm_root, quiet)?;

    if threads <= 1 {
        // Sequential batch mode
        let mut passed = 0usize;
        let mut failed = 0usize;
        let mut first_failure_code = 1;

        for file in files {
            let start = Instant::now();
            let file_name = file.rsplit('/').next().unwrap_or(file);

            match dispatch::try_internal_dispatch(clm_root, file, None, None, true) {
                Ok(exit_code) => {
                    let elapsed = start.elapsed();
                    if exit_code == 0 {
                        if !quiet {
                            eprintln!("[BATCH] ✅ PASS  {}  ({:.0}ms)", file_name, elapsed.as_millis());
                        }
                        passed += 1;
                    } else {
                        if !quiet {
                            eprintln!("[BATCH] ❌ FAIL  {}  rc={}  ({:.0}ms)", file_name, exit_code, elapsed.as_millis());
                        }
                        failed += 1;
                        if first_failure_code == 1 {
                            first_failure_code = exit_code;
                        }
                    }
                }
                Err(_) => {
                    let child_ctx = crate::cross_cutting::context::CLMContext::current()
                        .with_nested_exec(true);
                    let _nested_guard = child_ctx.enter_scope();
                    match run_evaluation(clm_root, Some(file), None, None, true, None) {
                        Ok(exit_code) => {
                            let elapsed = start.elapsed();
                            if exit_code == 0 {
                                if !quiet {
                                    eprintln!("[BATCH] ✅ PASS  {}  ({:.0}ms, fallback)", file_name, elapsed.as_millis());
                                }
                                passed += 1;
                            } else {
                                if !quiet {
                                    eprintln!("[BATCH] ❌ FAIL  {}  rc={}  ({:.0}ms, fallback)", file_name, exit_code, elapsed.as_millis());
                                }
                                failed += 1;
                                if first_failure_code == 1 {
                                    first_failure_code = exit_code;
                                }
                            }
                        }
                        Err(e) => {
                            let elapsed = start.elapsed();
                            if !quiet {
                                eprintln!("[BATCH] ❌ ERROR  {}  {}  ({:.0}ms, fallback)", file_name, e, elapsed.as_millis());
                            }
                            failed += 1;
                        }
                    }
                }
            }
        }

        let total = passed + failed;
        eprintln!("[BATCH] Done: {} passed, {} failed  (of {})", passed, failed, total);

        if failed > 0 {
            Ok(first_failure_code)
        } else {
            Ok(0)
        }
    } else {
        // Process-parallel batch mode: spawn N subprocesses, each running a sequential batch.
        // This gives true isolation (separate env vars, DBs, global state) without thread races.
        let clm_root = clm_root.to_path_buf();
        let self_exe = env::current_exe().unwrap_or_else(|_| std::path::PathBuf::from("clmEval-rs"));
        let pid = std::process::id();
        let chunk_size = files.len().div_ceil(threads);
        let mut handles = Vec::new();

        for (idx, chunk) in files.chunks(chunk_size).enumerate() {
            let chunk = chunk.to_vec();
            let clm_root = clm_root.clone();
            let self_exe = self_exe.clone();

            let handle = thread::spawn(move || {
                let snap_dir = std::env::temp_dir()
                    .join(format!("clm_batch_proc_{}_{}", pid, idx))
                    .to_string_lossy()
                    .replace('\\', "/");
                let _ = fs::remove_dir_all(&snap_dir);
                let _ = fs::create_dir_all(&snap_dir);

                // Build per-process sandbox: symlink everything from clm_root,
                // but copy db/ for DB isolation and create writable data/ subdirs.
                if let Ok(entries) = fs::read_dir(&clm_root) {
                    for entry in entries.flatten() {
                        let name = entry.file_name();
                        let name_str = name.to_string_lossy();
                        if name_str == "db" {
                            let src_db = clm_root.join("db");
                            let dst_db = format!("{}/db", snap_dir);
                            let _ = fs::create_dir_all(&dst_db);
                            if let Ok(db_entries) = fs::read_dir(&src_db) {
                                for db_entry in db_entries.flatten() {
                                    let _ = fs::copy(
                                        db_entry.path(),
                                        format!("{}/{}", dst_db, db_entry.file_name().to_string_lossy()),
                                    );
                                }
                            }
                        } else if name_str == "data" {
                            let src_data = clm_root.join("data");
                            let dst_data = format!("{}/data", snap_dir);
                            let _ = fs::create_dir_all(&dst_data);
                            if let Ok(data_entries) = fs::read_dir(&src_data) {
                                for data_entry in data_entries.flatten() {
                                    let dname = data_entry.file_name();
                                    let dname_str = dname.to_string_lossy();
                                    let is_writable = dname_str == "execution_logs"
                                        || dname_str == "execution_data"
                                        || dname_str == "ipc"
                                        || dname_str == "logs"
                                        || dname_str == "content_memory"
                                        || dname_str == "agent_identity";
                                    if is_writable {
                                        let dst_sub = format!("{}/{}", dst_data, dname_str);
                                        let _ = fs::create_dir_all(&dst_sub);
                                        if dname_str == "execution_data" {
                                            let _ = fs::create_dir_all(format!("{}/logs/.tmp", dst_sub));
                                        } else if dname_str == "ipc" {
                                            let _ = copy_directory_recursive(&data_entry.path(), &PathBuf::from(&dst_sub));
                                        }
                                    } else {
                                        let _ = create_symlink(
                                            &data_entry.path(),
                                            &PathBuf::from(format!("{}/{}", dst_data, dname_str)),
                                        );
                                    }
                                }
                            }
                        } else if name_str == "types" {
                            let src_types = clm_root.join("types");
                            let dst_types = PathBuf::from(&snap_dir).join("types");
                            let _ = copy_directory_recursive(&src_types, &dst_types);
                        } else {
                            let _ = create_symlink(
                                &entry.path(),
                                &PathBuf::from(format!("{}/{}", snap_dir, name_str)),
                            );
                        }
                    }
                }

                let snap_path = PathBuf::from(&snap_dir);
                let mut cmd = Command::new(&self_exe);
                cmd.current_dir(&snap_path);
                cmd.arg("evaluate").arg("--batch");
                for f in &chunk {
                    cmd.arg(f);
                }
                if quiet { cmd.arg("--quiet"); }
                cmd.env("CLM_CACHE_FRESH", "1");
                cmd.env("CLM_ROOT", &snap_dir);
                cmd.env("CLM_NESTED_EXEC", "");
                if let Ok(v) = env::var("CLM_PETRI_KENOTIC") { cmd.env("CLM_PETRI_KENOTIC", v); }
                // Capture stderr (BATCH lines) to per-chunk log, inherit stdout
                let log_path = format!("{}/chunk_output.log", snap_dir);
                let log_file = fs::File::create(&log_path).ok();
                if let Some(ref f) = log_file {
                    cmd.stderr(f.try_clone().unwrap_or_else(|_| f.try_clone().unwrap()));
                } else {
                    cmd.stderr(Stdio::inherit());
                }
                cmd.stdout(Stdio::null());

                let start = Instant::now();
                let status = cmd.status();
                let elapsed = start.elapsed();
                let exit_code = status.map(|s| s.code().unwrap_or(1)).unwrap_or(1);

                (idx, exit_code, elapsed.as_millis(), snap_dir)
            });
            handles.push(handle);
        }

        let mut passed = 0usize;
        let mut failed = 0usize;
        let mut first_failure_code = 1;

        for handle in handles {
            match handle.join() {
                Ok((idx, exit_code, elapsed_ms, snap_dir)) => {
                    // Parse per-test results from chunk log
                    let log_path = format!("{}/chunk_output.log", snap_dir);
                    if let Ok(log_content) = fs::read_to_string(&log_path) {
                        for line in log_content.lines() {
                            if line.starts_with("[BATCH]") {
                                if line.starts_with("[BATCH] Done:") {
                                    continue;
                                }
                                if !quiet {
                                    eprintln!("{}", line);
                                }
                                if line.contains("✅ PASS") {
                                    passed += 1;
                                } else if line.contains("❌ FAIL") {
                                    failed += 1;
                                    if first_failure_code == 1 {
                                        first_failure_code = exit_code;
                                    }
                                }
                            }
                        }
                    } else if !quiet {
                        eprintln!("[BATCH] ❌ Chunk {} — no output log", idx);
                    }
                    if !quiet && exit_code != 0 {
                        eprintln!("[BATCH] Chunk {} finished  rc={}  ({:.0}ms)", idx, exit_code, elapsed_ms);
                    }
                    let _ = fs::remove_dir_all(&snap_dir);
                }
                Err(_) => {
                    if !quiet {
                        eprintln!("[BATCH] ❌ Chunk thread panicked");
                    }
                    failed += 1;
                }
            }
        }

        eprintln!("[BATCH] Done: {} passed, {} failed  ({} processes)", passed, failed, threads);

        if failed > 0 {
            Ok(first_failure_code)
        } else {
            Ok(0)
        }
    }
}


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