clm-kernel 0.0.1

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Stage 1.4 dual-mode runtime dispatch.
//!
//! Provides a single `petri_run_net` entry point that selects the WASM-native
//! executor or the native subprocess-based executor based on the
//! `CLM_RUNTIME_BACKEND` environment variable.
//!
//! - `CLM_RUNTIME_BACKEND=wasm` → in-process WASM executor (`cpn_wasm`).
//! - unset or any other value → native executor (`cpn::vm`).
//!
//! This is the bridge step toward full subprocess fallback removal.

use std::env;

/// Run a Petri net topology, dispatching to the selected runtime backend.
///
/// `topo_file` is a filesystem path in native builds. When the WASM backend is
/// selected, the file is read into memory and passed to the WASM-native
/// executor, which expects the simplified Stage 1.3 YAML schema.
pub fn petri_run_net(topo_file: &str, clm_root: &str) -> Result<String, String> {
    petri_run_net_with_backend(
        &env::var("CLM_RUNTIME_BACKEND").unwrap_or_default(),
        topo_file,
        clm_root,
    )
}

fn petri_run_net_with_backend(
    backend: &str,
    topo_file: &str,
    clm_root: &str,
) -> Result<String, String> {
    if backend == "wasm" {
        let yaml_body = std::fs::read_to_string(topo_file)
            .map_err(|e| format!("failed to read topology for wasm backend: {}", e))?;
        crate::cpn_wasm::run_net(&yaml_body)
    } else {
        crate::cpn::vm::petri_run_net(topo_file, clm_root)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn test_dispatch_wasm_backend() {
        let dir = std::env::temp_dir().join("clm_runtime_dispatch_wasm_test");
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("net.yaml");
        {
            let mut f = std::fs::File::create(&path).unwrap();
            f.write_all(
                b"places:\n  - id: start\n    tokens: 1\n  - id: end\n    tokens: 0\ntransitions:\n  - id: t1\n    inputs: [start]\n    outputs: [end]\n    binding:\n      runtime: echo\n      content: hello\n"
            )
            .unwrap();
        }
        let output = petri_run_net_with_backend("wasm", path.to_str().unwrap(), "/tmp/clm_root").unwrap();
        assert!(output.contains("signature"));
        assert!(output.contains("epiplexity_metrics"));
    }

    #[test]
    fn test_dispatch_native_backend_without_env() {
        // Verify the dispatcher delegates to the native backend. The native
        // backend will fail to parse an intentionally invalid YAML, which is
        // expected and proves the native code path is reached.
        let dir = std::env::temp_dir().join("clm_runtime_dispatch_native_test");
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("net.yaml");
        {
            let mut f = std::fs::File::create(&path).unwrap();
            f.write_all(b"not a valid topology\n").unwrap();
        }
        let result = petri_run_net_with_backend("native", path.to_str().unwrap(), "/tmp/clm_root");
        assert!(result.is_err());
    }
}