clm-kernel 0.0.24

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();

    // Reuse the binary Cargo already built for this test crate (INV-614: no rebuild inside test).
    let src_exe = PathBuf::from(env!("CARGO_BIN_EXE_clm_rust_core"));
    assert!(src_exe.exists(), "Prebuilt clm_rust_core binary missing: {:?}", src_exe);
    let bin_path = src_exe.parent().expect("clm_rust_core binary parent dir").to_path_buf();
    let script_path = clm_root.join("kernel/launchers");
    let platform_bin_path = snap_dir.join("bin");
    std::fs::create_dir_all(&platform_bin_path).expect("Failed to create staged bin dir");

    #[cfg(unix)]
    {
        let _ = std::os::unix::fs::symlink(&src_exe, platform_bin_path.join("clmEval-rs"));
    }
    #[cfg(windows)]
    {
        let exe_dest = platform_bin_path.join("clmEval-rs.exe");
        std::fs::copy(&src_exe, &exe_dest).expect("Failed to stage clmEval-rs.exe");
    }

    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);
    }
}