clm-kernel 0.0.21

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
#![cfg(feature = "native")]
use std::path::{Path, PathBuf};
use std::env;
use std::sync::Arc;

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_layer0_microkernel_manifests() {
    let tmp_dir = env::temp_dir();
    let snap_dir = tmp_dir.join("clm_test_runner_snapshots");
    let _ = std::fs::remove_dir_all(&snap_dir);
    std::fs::create_dir_all(&snap_dir).expect("Failed to create isolated snap dir");

    unsafe {
        env::set_var("TMPDIR", tmp_dir.to_str().unwrap());
        env::set_var("CLM_IN_MEMORY_DB", "true");
        env::set_var("CLM_ZERO_FS", "true");
        env::set_var("CLM_PETRI_KENOTIC", "true");
        env::set_var("CLM_RUNTIME_BACKEND", "rust");
        env::set_var("CLM_MEMBROKER_SNAP_DIR", snap_dir.to_str().unwrap());
    }

    let current_dir = env::current_dir().expect("Failed to get current dir");
    let clm_root = current_dir.parent().unwrap().parent().unwrap().to_path_buf();

    // Build the CLI binary (explicitly use native target — .cargo/config.toml no longer sets a default)
    let arch = env::consts::ARCH;
    let native_target = match env::consts::OS {
        "macos" => format!("{arch}-apple-darwin"),
        "windows" => format!("{arch}-pc-windows-msvc"),
        _ => format!("{arch}-unknown-linux-gnu"),
    };
    let native_target = native_target.as_str();
    let status = std::process::Command::new("cargo")
        .arg("build")
        .arg("--target")
        .arg(native_target)
        .current_dir(clm_root.join("kernel/clm_rust_core"))
        .status()
        .expect("Failed to run cargo build");
    assert!(status.success(), "Cargo build failed");

    let bin_path = clm_root.join(format!("kernel/clm_rust_core/target/{}/debug", native_target));
    let script_path = clm_root.join("kernel/launchers");
    let platform_bin_path = clm_root.join(format!("kernel/bin/{}", native_target));
    let _symlink_path = platform_bin_path.join("clmEval-rs");

    #[cfg(unix)]
    {
        // Keep the shell script wrapper intact, as it automatically delegates to the compiled debug/release binaries.
    }
    #[cfg(windows)]
    {
        std::fs::create_dir_all(&platform_bin_path).expect("Failed to create platform bin dir");
        let exe_dest = platform_bin_path.join("clmEval-rs.exe");
        let _ = std::fs::remove_file(&exe_dest);
        let src_exe = bin_path.join("clm_rust_core.exe");
        if src_exe.exists() {
            std::fs::copy(&src_exe, &exe_dest).expect("Failed to copy binary");
        }
    }

    let old_path = env::var("PATH").unwrap_or_default();
    let path_sep = if cfg!(target_os = "windows") { ";" } else { ":" };
    let new_path = format!(
        "{}{}{}{}{}{}{}",
        script_path.to_string_lossy(),
        path_sep,
        platform_bin_path.to_string_lossy(),
        path_sep,
        bin_path.to_string_lossy(),
        path_sep,
        old_path
    );
    unsafe { env::set_var("PATH", new_path); }

    let tests_dir = clm_root.join("tests").join("00_microkernel");
    assert!(tests_dir.exists(), "Tests directory not found: {:?}", tests_dir);

    let test_files: Vec<PathBuf> = vec![
        clm_root.join("tests/08_rust_native/test_past_execution_rust.yaml"),
        clm_root.join("tests/08_rust_native/test_init_tridb_rust.yaml"),
        clm_root.join("tests/08_rust_native/test_evaluator_functions_rust.yaml"),
        clm_root.join("tests/08_rust_native/test_mcard_kernel_rust.yaml"),
        clm_root.join("tests/08_rust_native/test_memorize_mcard_rust.yaml"),
        clm_root.join("tests/08_rust_native/test_polyglot_ingest_rust.yaml"),
        clm_root.join("tests/08_rust_native/test_modular_adapters_rust.yaml"),
        clm_root.join("tests/08_rust_native/test_internal_dispatch_zerofs_rust.yaml"),
        clm_root.join("tests/08_rust_native/test_polyglot_sla_sandboxing.yaml"),
    ];

    assert!(!test_files.is_empty(), "No test manifests found");

    let clm_root_arc = Arc::new(clm_root.to_string_lossy().to_string());
    
    // Pre-compile the cache to avoid race conditions when testing concurrently
    let _ = clm_rust_core::evaluator::cli::compile_evaluator_cache(&clm_root, true);

    for file in test_files {
        let root = Arc::clone(&clm_root_arc);
        let file_path = file.to_string_lossy().to_string();
        let file_name = file.file_name().unwrap().to_string_lossy().to_string();

        println!("Running test manifest: {}", file_name);
        
        let ast_result = clm_rust_core::parser::parse_manifest(&file_path);
        assert!(ast_result.is_ok(), "Failed to parse manifest {}: {:?}", file_name, ast_result.err());

        let instance_name = format!("test_{}", file_name);
        let run_result = clm_rust_core::evaluator::run_evaluation(
            Path::new(&*root),
            Some(&file_path),
            None,
            None,
            false, // quiet mode
            Some(instance_name.as_str()),
        );
        
        assert!(run_result.is_ok(), "Execution errored for {}: {:?}", file_name, run_result.err());
        assert_eq!(run_result.unwrap(), 0, "Execution returned non-zero for {}", file_name);
        println!("✅ {} passed", file_name);
    }
}