Skip to main content

clm_rust_core/
runtime_dispatch.rs

1//! Stage 1.4 dual-mode runtime dispatch.
2//!
3//! Provides a single `petri_run_net` entry point that selects the WASM-native
4//! executor or the native subprocess-based executor based on the
5//! `CLM_RUNTIME_BACKEND` environment variable.
6//!
7//! - `CLM_RUNTIME_BACKEND=wasm` → in-process WASM executor (`cpn_wasm`).
8//! - unset or any other value → native executor (`cpn::vm`).
9//!
10//! This is the bridge step toward full subprocess fallback removal.
11
12use std::env;
13
14/// Run a Petri net topology, dispatching to the selected runtime backend.
15///
16/// `topo_file` is a filesystem path in native builds. When the WASM backend is
17/// selected, the file is read into memory and passed to the WASM-native
18/// executor, which expects the simplified Stage 1.3 YAML schema.
19pub fn petri_run_net(topo_file: &str, clm_root: &str) -> Result<String, String> {
20    petri_run_net_with_backend(
21        &env::var("CLM_RUNTIME_BACKEND").unwrap_or_default(),
22        topo_file,
23        clm_root,
24    )
25}
26
27fn petri_run_net_with_backend(
28    backend: &str,
29    topo_file: &str,
30    clm_root: &str,
31) -> Result<String, String> {
32    if backend == "wasm" {
33        let yaml_body = std::fs::read_to_string(topo_file)
34            .map_err(|e| format!("failed to read topology for wasm backend: {}", e))?;
35        crate::cpn_wasm::run_net(&yaml_body)
36    } else {
37        crate::cpn::vm::petri_run_net(topo_file, clm_root)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use std::io::Write;
45
46    #[test]
47    fn test_dispatch_wasm_backend() {
48        let dir = std::env::temp_dir().join("clm_runtime_dispatch_wasm_test");
49        std::fs::create_dir_all(&dir).unwrap();
50        let path = dir.join("net.yaml");
51        {
52            let mut f = std::fs::File::create(&path).unwrap();
53            f.write_all(
54                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"
55            )
56            .unwrap();
57        }
58        let output = petri_run_net_with_backend("wasm", path.to_str().unwrap(), "/tmp/clm_root").unwrap();
59        assert!(output.contains("signature"));
60        assert!(output.contains("epiplexity_metrics"));
61    }
62
63    #[test]
64    fn test_dispatch_native_backend_without_env() {
65        // Verify the dispatcher delegates to the native backend. The native
66        // backend will fail to parse an intentionally invalid YAML, which is
67        // expected and proves the native code path is reached.
68        let dir = std::env::temp_dir().join("clm_runtime_dispatch_native_test");
69        std::fs::create_dir_all(&dir).unwrap();
70        let path = dir.join("net.yaml");
71        {
72            let mut f = std::fs::File::create(&path).unwrap();
73            f.write_all(b"not a valid topology\n").unwrap();
74        }
75        let result = petri_run_net_with_backend("native", path.to_str().unwrap(), "/tmp/clm_root");
76        assert!(result.is_err());
77    }
78}