Skip to main content

clm_rust_core/
polyglot.rs

1use std::collections::HashMap;
2use std::path::{Path, PathBuf};
3use serde_json::Value;
4use crate::memorize;
5use crate::yaml;
6use crate::db;
7
8#[cfg(unix)]
9#[repr(C)]
10#[derive(Clone, Copy)]
11pub struct timeval {
12    pub tv_sec: i64,
13    pub tv_usec: i32,
14    #[cfg(target_os = "macos")]
15    pub _padding: i32,
16}
17
18#[cfg(unix)]
19#[repr(C)]
20pub struct rusage {
21    pub ru_utime: timeval,
22    pub ru_stime: timeval,
23    pub ru_maxrss: i64,
24    pub ru_ixrss: i64,
25    pub ru_idrss: i64,
26    pub ru_isrss: i64,
27    pub ru_minflt: i64,
28    pub ru_majflt: i64,
29    pub ru_nswap: i64,
30    pub ru_inblock: i64,
31    pub ru_oublock: i64,
32    pub ru_msgsnd: i64,
33    pub ru_msgrcv: i64,
34    pub ru_nsignals: i64,
35    pub ru_nvcsw: i64,
36    pub ru_nivcsw: i64,
37}
38
39#[cfg(unix)]
40const RUSAGE_SELF: i32 = 0;
41#[cfg(unix)]
42const RUSAGE_CHILDREN: i32 = -1;
43
44#[cfg(unix)]
45#[allow(dead_code)]
46unsafe extern "C" {
47    fn dup(fd: i32) -> i32;
48    fn dup2(src: i32, dst: i32) -> i32;
49    fn close(fd: i32) -> i32;
50    fn getrusage(who: i32, usage: *mut rusage) -> i32;
51}
52
53#[cfg(unix)]
54pub fn get_peak_rss_kb() -> i64 {
55    let mut usage = unsafe { std::mem::zeroed::<rusage>() };
56    let res = unsafe { getrusage(RUSAGE_SELF, &mut usage) };
57    if res == 0 {
58        #[cfg(target_os = "macos")]
59        {
60            usage.ru_maxrss / 1024
61        }
62        #[cfg(not(target_os = "macos"))]
63        {
64            usage.ru_maxrss
65        }
66    } else {
67        0
68    }
69}
70
71#[cfg(unix)]
72pub fn get_children_peak_rss_kb() -> i64 {
73    let mut usage = unsafe { std::mem::zeroed::<rusage>() };
74    let res = unsafe { getrusage(RUSAGE_CHILDREN, &mut usage) };
75    if res == 0 {
76        #[cfg(target_os = "macos")]
77        {
78            usage.ru_maxrss / 1024
79        }
80        #[cfg(not(target_os = "macos"))]
81        {
82            usage.ru_maxrss
83        }
84    } else {
85        0
86    }
87}
88
89#[cfg(windows)]
90#[repr(C)]
91#[allow(non_snake_case)]
92struct PROCESS_MEMORY_COUNTERS {
93    cb: u32,
94    PageFaultCount: u32,
95    PeakWorkingSetSize: usize,
96    WorkingSetSize: usize,
97    QuotaPeakPagedPoolUsage: usize,
98    QuotaPagedPoolUsage: usize,
99    QuotaPeakNonPagedPoolUsage: usize,
100    QuotaNonPagedPoolUsage: usize,
101    PagefileUsage: usize,
102    PeakPagefileUsage: usize,
103}
104
105#[cfg(windows)]
106#[link(name = "psapi")]
107unsafe extern "system" {
108    fn GetCurrentProcess() -> *mut std::ffi::c_void;
109    fn GetProcessMemoryInfo(
110        process: *mut std::ffi::c_void,
111        counters: *mut PROCESS_MEMORY_COUNTERS,
112        cb: u32,
113    ) -> i32;
114}
115
116#[cfg(windows)]
117pub fn get_peak_rss_kb() -> i64 {
118    let mut counters = unsafe { std::mem::zeroed::<PROCESS_MEMORY_COUNTERS>() };
119    counters.cb = std::mem::size_of::<PROCESS_MEMORY_COUNTERS>() as u32;
120    let res = unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut counters, counters.cb) };
121    if res != 0 {
122        (counters.PeakWorkingSetSize / 1024) as i64
123    } else {
124        0
125    }
126}
127
128#[cfg(windows)]
129pub fn get_children_peak_rss_kb() -> i64 {
130    get_peak_rss_kb()
131}
132
133pub fn resolve_python_cmd(clm_root: &str) -> String {
134    let root_path = std::path::Path::new(clm_root);
135    
136    // Windows virtual env python
137    let venv_py_win = root_path.join(".venv/Scripts/python.exe");
138    if venv_py_win.exists() {
139        return venv_py_win.to_string_lossy().to_string();
140    }
141    
142    // Unix virtual env python
143    let venv_py_unix = root_path.join(".venv/bin/python3");
144    if venv_py_unix.exists() {
145        return venv_py_unix.to_string_lossy().to_string();
146    }
147    
148    let venv_py_unix_2 = root_path.join(".venv/bin/python");
149    if venv_py_unix_2.exists() {
150        return venv_py_unix_2.to_string_lossy().to_string();
151    }
152
153    if cfg!(target_os = "windows") {
154        for cmd in &["python", "python3"] {
155            if std::process::Command::new(cmd).arg("--version").output().map(|o| o.status.success()).unwrap_or(false) {
156                return cmd.to_string();
157            }
158        }
159    } else {
160        for cmd in &["python3", "python"] {
161            if std::process::Command::new(cmd).arg("--version").output().map(|o| o.status.success()).unwrap_or(false) {
162                return cmd.to_string();
163            }
164        }
165    }
166    
167    if cfg!(target_os = "windows") {
168        "python".to_string()
169    } else {
170        "python3".to_string()
171    }
172}
173
174pub fn get_interpreter_version(interpreter: &str, clm_root: &str) -> Option<String> {
175    let cmd_name = match interpreter {
176        "python" | "python3" => resolve_python_cmd(clm_root),
177        other => other.to_string(),
178    };
179    let output = std::process::Command::new(&cmd_name)
180        .arg("--version")
181        .output()
182        .ok();
183    if let Some(out) = output {
184        let text = String::from_utf8_lossy(&out.stdout).to_string()
185            + String::from_utf8_lossy(&out.stderr).as_ref();
186        let re = regex::Regex::new(r"(\d+\.\d+(?:\.\d+)?)").unwrap();
187        if let Some(caps) = re.captures(&text) {
188            return Some(caps[1].to_string());
189        }
190    }
191    None
192}
193
194pub fn compare_versions(current: &str, op: &str, req: &str) -> bool {
195    let parse_ver = |s: &str| -> (u32, u32, u32) {
196        let parts: Vec<&str> = s.split('.').collect();
197        let major = parts.first().and_then(|v| v.parse().ok()).unwrap_or(0);
198        let minor = parts.get(1).and_then(|v| v.parse().ok()).unwrap_or(0);
199        let patch = parts.get(2).and_then(|v| v.parse().ok()).unwrap_or(0);
200        (major, minor, patch)
201    };
202    let v1 = parse_ver(current);
203    let v2 = parse_ver(req);
204    match op {
205        ">=" => v1 >= v2,
206        "<=" => v1 <= v2,
207        ">" => v1 > v2,
208        "<" => v1 < v2,
209        _ => v1 == v2,
210    }
211}
212
213pub fn check_precondition(req_str: &str, clm_root: &str) -> bool {
214    let re = regex::Regex::new(r"^interpreter:([a-zA-Z0-9_-]+)(>=|<=|>|<|==|=)([0-9.]+)$").unwrap();
215    if let Some(caps) = re.captures(req_str) {
216        let interpreter = &caps[1];
217        let op = &caps[2];
218        let req_ver = &caps[3];
219        if let Some(current_ver) = get_interpreter_version(interpreter, clm_root) {
220            compare_versions(&current_ver, op, req_ver)
221        } else {
222            false
223        }
224    } else {
225        true
226    }
227}
228
229pub fn create_sandboxed_command(
230    program: &str,
231    args: &[String],
232    envs: &HashMap<String, String>,
233    clm_root: &str,
234) -> std::process::Command {
235    let clm_no_sandbox = std::env::var("CLM_NO_SANDBOX").unwrap_or_default() == "true";
236    let has_sandbox_exec = std::process::Command::new("which")
237        .arg("sandbox-exec")
238        .output()
239        .map(|o| o.status.success())
240        .unwrap_or(false);
241
242    let mut cmd = if !clm_no_sandbox && has_sandbox_exec {
243        let home_dir = std::env::var("HOME").unwrap_or_default();
244        let profile = format!(
245            r#"(version 1)
246(deny default)
247(allow file-read-metadata)
248(allow file-read-data)
249(allow file-write-data)
250(allow file-read*
251    (subpath "/usr")
252    (subpath "/System")
253    (subpath "/Library")
254    (subpath "/private")
255    (subpath "/var")
256    (subpath "/tmp")
257    (subpath "/etc")
258    (subpath "/opt")
259    (subpath "/dev")
260    (subpath "{}")
261    (subpath "{}"))
262(allow file-write*
263    (subpath "{}/data")
264    (subpath "{}/db")
265    (subpath "{}/types/meta")
266    (literal "{}/result_open_interpreter.txt")
267    (subpath "{}/.clm")
268    (subpath "/private/var")
269    (subpath "/private/tmp")
270    (subpath "/var")
271    (subpath "/tmp")
272    (literal "/dev/null")
273    (literal "/dev/zero"))
274(allow file-map-executable
275    (subpath "/usr")
276    (subpath "/System")
277    (subpath "/Library")
278    (subpath "/opt")
279    (subpath "{}")
280    (subpath "{}"))
281(allow process-exec
282    (subpath "/bin")
283    (subpath "/usr/bin")
284    (subpath "/usr/local/bin")
285    (subpath "/opt/homebrew")
286    (subpath "/Library/Frameworks/Python.framework")
287    (subpath "/Library/Developer/CommandLineTools")
288    (subpath "/private/var")
289    (subpath "/var")
290    (subpath "/tmp")
291    (subpath "{}")
292    (subpath "{}"))
293(allow process-fork)
294(allow process-info*)
295(allow ipc-posix*)
296(allow sysctl-read)
297(allow mach-lookup)
298(allow dynamic-code-generation)
299(allow network-bind)
300(allow network-inbound)
301(allow network-outbound)
302(deny file-read*
303    (literal "/private/etc/passwd")
304    (literal "/etc/passwd"))
305(deny file-read-data
306    (literal "/private/etc/passwd")
307    (literal "/etc/passwd"))"#,
308            clm_root, home_dir, clm_root, clm_root, clm_root, clm_root, home_dir, clm_root, home_dir, clm_root, home_dir
309        );
310        let mut c = std::process::Command::new("sandbox-exec");
311        c.arg("-p").arg(profile).arg(program);
312        c
313    } else {
314        std::process::Command::new(program)
315    };
316
317    cmd.env_clear();
318    for (k, v) in std::env::vars() {
319        if !k.starts_with("CLM_") {
320            cmd.env(k, v);
321        }
322    }
323    let clm_whitelist = [
324        "CLM_NESTED_EXEC", "CLM_RUST_DAEMON_PORT", "CLM_RUNTIME_BACKEND",
325        "CLM_POLYGLOT_RUNTIME", "CLM_RUN_ID", "CLM_ZERO_FS", "CLM_PETRI_MARKING_DIR", "CLM_EVENT_LOG_PATH",
326        "CLM_ROOT", "PROJECT_ROOT", "CLM_TARGET_FILE", "CLM_PAYLOAD_FILE",
327        "CLM_ACTIVE_A_HASH", "CLM_ACTIVE_C_HASH", "CLM_MLP_CONTEXT", "CLM_PAYLOAD",
328        "CLM_SESSION_DELEGATION", "CLM_KENOTIC_SPONSOR_DID", "CLM_EXECUTION_LOCALE_HASH",
329        "CLM_TRANSPORT_REF", "CLM_PETRI_KENOTIC", "CLM_REPL_TRACE", "CLM_NO_SANDBOX",
330        "CLM_SYMBOL_HANDLES", "CLM_DB_BACKEND", "CLM_MEMBROKER_PORT", "CLM_MEMBROKER_NO_DISK",
331        "CLM_MEMBROKER_SNAP_DIR", "CLM_SCRATCH", "CLM_TEST_FAST_BOOT", "CLM_SKIP_GRAMMAR",
332        "CLM_PETRI_HALT", "CLM_PETRI_VISUAL", "CLM_QUIET",
333        "CLM_NATIVE_PETRI_VM", "CLM_DISPATCH_SOCK", "CLM_SCRIPT_CONTENT",
334        "CLM_ENTRY_POINT", "CLM_FILE", "CLM_RUST_PORT_FILE",
335        "CLM_ACTIVE_B_HASH", "CLM_SKIP_CONDITIONS", "ACTIVE_AGENT_DID",
336        "CLM_TARGET_PATH", "CLM_AGENT_CLASS", "CLM_AGENT_ROLE",
337        "CLM_RISK_LEVEL", "CLM_EOA_APPROVAL", "CLM_AGENT_DID",
338        "CLM_ACTIVE_FILE", "CLM_LAST_MCARD_HASH", "CLM_LAST_MCARD_FILE"
339    ];
340    for var_name in &clm_whitelist {
341        if let Ok(val) = std::env::var(var_name) {
342            cmd.env(var_name, val);
343        }
344    }
345    for (k, v) in envs {
346        cmd.env(k, v);
347    }
348    for a in args {
349        cmd.arg(a);
350    }
351    cmd
352}
353
354pub fn normalize_output(raw_out: &str) -> String {
355    use serde_json::Value;
356    let stripped = raw_out.replace('\r', "");
357    let stripped = stripped.trim_end().to_string();
358    if let Ok(val) = serde_json::from_str::<Value>(&stripped) {
359        serde_json::to_string(&val).unwrap_or(stripped)
360    } else {
361        stripped
362    }
363}
364
365pub fn print_diff(first_norm: &str, second_norm: &str) {
366    let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
367    let f1 = temp_dir.join(format!("norm_f1_{}", std::process::id()));
368    let f2 = temp_dir.join(format!("norm_f2_{}", std::process::id()));
369    let _ = std::fs::write(&f1, first_norm);
370    let _ = std::fs::write(&f2, second_norm);
371    
372    let diff_output = std::process::Command::new("diff")
373        .arg("-u")
374        .arg(&f1)
375        .arg(&f2)
376        .output();
377    if let Ok(out) = diff_output {
378        let text = String::from_utf8_lossy(&out.stdout);
379        eprintln!("{}", text);
380    }
381    
382    let _ = std::fs::remove_file(f1);
383    let _ = std::fs::remove_file(f2);
384}
385
386pub fn resolve_self_exe(clm_root: &str) -> PathBuf {
387    if let Ok(self_exe) = std::env::current_exe() {
388        if self_exe.to_string_lossy().contains("deps") {
389            let alt_exe = Path::new(clm_root).join({
390                #[cfg(windows)] { "kernel/clm_rust_core/target/debug/clm_rust_core.exe" }
391                #[cfg(not(windows))] { "kernel/clm_rust_core/target/debug/clm_rust_core" }
392            });
393            if alt_exe.exists() {
394                return alt_exe;
395            }
396            let alt_exe_release = Path::new(clm_root).join({
397                #[cfg(windows)] { "kernel/clm_rust_core/target/release/clm_rust_core.exe" }
398                #[cfg(not(windows))] { "kernel/clm_rust_core/target/release/clm_rust_core" }
399            });
400            if alt_exe_release.exists() {
401                return alt_exe_release;
402            }
403        }
404        if let Some(parent) = self_exe.parent() {
405            let local_exe = parent.join({
406                #[cfg(windows)] { "clm_rust_core.exe" }
407                #[cfg(not(windows))] { "clm_rust_core" }
408            });
409            if local_exe.exists() {
410                return local_exe;
411            }
412        }
413        return self_exe;
414    }
415    Path::new(clm_root).join({
416        #[cfg(windows)] { "kernel/clm_rust_core/target/release/clm_rust_core.exe" }
417        #[cfg(not(windows))] { "kernel/clm_rust_core/target/release/clm_rust_core" }
418    })
419}
420
421pub fn exec_polyglot(
422    file_path: &str,
423    payload: Option<&str>,
424    clm_root: &str,
425) -> std::result::Result<(i32, String), String> {
426    let payload = payload.filter(|s| !s.is_empty());
427
428    // Register CLM manifest file and its AST cards in the database
429    memorize::check_and_register_file_mcard(file_path, clm_root)?;
430
431    // 1. Load the manifest content as JSON
432    let json_str = yaml::parse_to_mcards(file_path).map_err(|e| e.to_string())?;
433    let parsed: Value = serde_json::from_str(&json_str)
434        .map_err(|e| format!("Failed to parse JSON AST: {}", e))?;
435
436    // 2. Locate polyglot cases
437    let polyglot_cases = yaml::evaluate_jq(&parsed, ".clm.balanced_exp.polyglot_cases");
438    let cases_arr = match polyglot_cases {
439        Value::Array(arr) => arr,
440        _ => return Err("No polyglot_cases found".to_string()),
441    };
442
443    // 3. Find the target cases to run
444    let mut target_cases = Vec::new();
445    if let Some(p) = payload {
446        let mut found = false;
447        for c in cases_arr.iter() {
448            if let Some(name) = c.get("name").and_then(|v| v.as_str())
449                && name == p {
450                    target_cases.push(c.clone());
451                    found = true;
452                    break;
453                }
454        }
455        if !found {
456            return Err(format!("Case matching payload '{}' not found", p));
457        }
458    } else {
459        // Run all cases
460        for c in cases_arr.iter() {
461            target_cases.push(c.clone());
462        }
463    }
464
465    if target_cases.is_empty() {
466        return Err("No cases to execute".to_string());
467    }
468
469    let target_runtime = std::env::var("CLM_POLYGLOT_RUNTIME").ok();
470    if let Some(target) = &target_runtime {
471        eprintln!("[PAL:polyglot] Active runtime choice: {}", target);
472    }
473
474    // Resolve active hashes
475    let active_a_hash = std::env::var("CLM_ACTIVE_A_HASH").unwrap_or_default();
476    let active_c_hash = std::env::var("CLM_ACTIVE_C_HASH").unwrap_or_default();
477
478    let mut last_case_output = String::new();
479    let mut consensus_exit_code = 0;
480
481    for case in &target_cases {
482        let case_name = case.get("name").and_then(|v| v.as_str()).unwrap_or("unnamed_case");
483        let given = case.get("given").ok_or_else(|| format!("No given block found in case '{}'", case_name))?;
484
485        // Extract runtimes list and determine active runtime choice
486        let runtimes = match given.get("runtimes") {
487            Some(Value::Array(arr)) => arr.iter().filter_map(|v| v.as_str()).map(|s| s.to_string()).collect::<Vec<String>>(),
488            _ => return Err(format!("No runtimes found in given block of case '{}'", case_name)),
489        };
490
491        let active_runtimes = if let Some(target) = &target_runtime {
492            vec![target.clone()]
493        } else {
494            runtimes.clone()
495        };
496
497        // Inputs (excluding runtimes and requires)
498        let mut inputs = serde_json::Map::new();
499        let mut env_vars = std::collections::HashMap::new();
500        let mut pos_args = Vec::new();
501        if let Value::Object(map) = given {
502            for (k, v) in map {
503                if k != "runtimes" && k != "requires" {
504                    inputs.insert(k.clone(), v.clone());
505                    let val_str = match v {
506                        Value::String(s) => s.clone(),
507                        other => other.to_string(),
508                    };
509                    env_vars.insert(k.clone(), val_str.clone());
510                    pos_args.push(val_str);
511                }
512            }
513        }
514
515        for (k, v) in crate::utils::collect_clm_mock_vars() {
516            env_vars.insert(k, v);
517        }
518
519        // Mutual exclusivity checks
520        let then = case.get("then").ok_or_else(|| format!("No then block found in case '{}'", case_name))?;
521        let internal_ref = then.get("internal_reference").and_then(|v| v.as_str());
522        let native_harness = then.get("native_harness");
523        
524        if internal_ref.is_some() && native_harness.is_some() {
525            return Err(format!("Schema validation failed in case '{}': internal_reference and native_harness are mutually exclusive", case_name));
526        }
527
528        // SLA specifications
529        let sla = then.get("performance_sla");
530        let max_duration_ms = sla.and_then(|s| s.get("max_duration_ms")).and_then(|v| v.as_i64());
531        let max_rss_kb = sla.and_then(|s| s.get("max_rss_kb")).and_then(|v| v.as_i64());
532        let max_forks = sla.and_then(|s| s.get("max_forks")).and_then(|v| v.as_i64());
533
534        // Validate max_forks: 0 for internal_reference
535        if internal_ref.is_some()
536            && let Some(mf) = max_forks
537                && mf != 0 {
538                    return Err(format!("Schema validation failed in case '{}': max_forks must be 0 for internal_reference cases", case_name));
539                }
540
541        let mut executed_runtimes = Vec::new();
542        let mut runtime_outputs = Vec::new();
543        let mut runtime_exit_codes = Vec::new();
544        let mut runtime_exec_hashes = Vec::new();
545
546        for runtime in &active_runtimes {
547            // Enforce Gherkin cover check
548            if !runtimes.contains(runtime) {
549                eprintln!("[PAL:polyglot] ⏭️ Target runtime '{}' not requested in Gherkin cover: {:?}", runtime, runtimes);
550                continue;
551            }
552
553            // Verify supported runtimes
554            if runtime != "shell" && runtime != "python" && runtime != "node" && runtime != "rust" {
555                if target_runtime.is_some() {
556                    return Err(format!("Target runtime '{}' is not supported in Sprint 97", runtime));
557                } else {
558                    eprintln!("[PAL:polyglot] ⏭️ Skipping unsupported runtime '{}' in cover", runtime);
559                    continue;
560                }
561            }
562
563            // Check preconditions
564            if let Some(req_guard) = given.get("requires").and_then(|v| v.get(runtime)).and_then(|v| v.as_str())
565                && !check_precondition(req_guard, clm_root) {
566                    eprintln!("[PAL:polyglot] ⏭️ Pruning runtime '{}' (precondition not met: {})", runtime, req_guard);
567                    continue;
568                }
569
570            let mut fork_count = 0;
571            let elapsed_ms;
572            let peak_rss_val;
573
574            let (run_status, stdout_data, stderr_data) = if runtime == "rust" && internal_ref.is_some() {
575                // Zero-overhead in-process execution
576                let ref_path = internal_ref.unwrap();
577                let func = crate::evaluator::get_internal_registry(ref_path)
578                    .ok_or_else(|| format!("No internal function registered for path '{}'", ref_path))?;
579                
580                let start_time = std::time::Instant::now();
581                let _start_rss = get_peak_rss_kb();
582
583                // Setup Sandbox & execute
584                let run_res = {
585                    let _sandbox_guard = crate::db::DatabaseSandboxGuard::activate(case_name);
586                    func(clm_root, file_path)
587                };
588
589                elapsed_ms = start_time.elapsed().as_millis() as i64;
590                let end_rss = get_peak_rss_kb();
591                peak_rss_val = end_rss; // peak of the process self
592
593                let (code, out) = run_res.map_err(|e| format!("Internal dispatch error: {}", e))?;
594                (code, out, String::new())
595            } else if let Some(harness) = native_harness {
596                // Native harness execution
597                let tool = harness.get("tool").and_then(|v| v.as_str()).ok_or("No tool specified in native_harness")?;
598                let cmd_arg = harness.get("command").and_then(|v| v.as_str()).ok_or("No command specified in native_harness")?;
599                
600                fork_count += 1;
601                let full_cmd = format!("{} {}", tool, cmd_arg);
602                let start_time = std::time::Instant::now();
603                let start_rss = get_children_peak_rss_kb();
604
605                let mut cmd = create_sandboxed_command(&crate::fs_utils::get_bash_command(), &["-c".to_string(), full_cmd], &env_vars, clm_root);
606                let output = cmd.output().map_err(|e| format!("Failed to spawn native_harness: {}", e))?;
607                
608                elapsed_ms = start_time.elapsed().as_millis() as i64;
609                let end_rss = get_children_peak_rss_kb();
610                peak_rss_val = if end_rss > start_rss { end_rss - start_rss } else { 0 };
611
612                (
613                    output.status.code().unwrap_or(0),
614                    String::from_utf8_lossy(&output.stdout).into_owned(),
615                    String::from_utf8_lossy(&output.stderr).into_owned(),
616                )
617            } else {
618                // Legacy Fallback Script compilation & execution
619                let impl_path = format!(".clm.concrete_impl.process.implementations.{}", runtime);
620                let script_val = yaml::evaluate_jq(&parsed, &format!("{}.script", impl_path));
621                let handle_val = yaml::evaluate_jq(&parsed, &format!("{}.script_handle", impl_path));
622                let cargo_val = yaml::evaluate_jq(&parsed, &format!("{}.cargo_test", impl_path));
623                
624                let mut script_content = script_val.as_str().unwrap_or("").to_string();
625                let cargo_test_name = cargo_val.as_str().unwrap_or("").to_string();
626
627                if script_content.is_empty()
628                    && let Some(handle) = handle_val.as_str()
629                        && handle.starts_with("script:") {
630                            let s_name = handle.trim_start_matches("script:");
631                            let sh_path = Path::new(clm_root).join("scripts").join(format!("{}.sh", s_name));
632                            if let Ok(content) = std::fs::read_to_string(&sh_path) {
633                                script_content = content;
634                            } else {
635                                // Fallback check test dir
636                                let sh_path = Path::new(clm_root).join("tests").join("00_microkernel").join(format!("{}.sh", s_name));
637                                if let Ok(content) = std::fs::read_to_string(&sh_path) {
638                                    script_content = content;
639                                }
640                            }
641                        }
642
643                if script_content.is_empty() && cargo_test_name.is_empty() {
644                    eprintln!("[PAL:polyglot] Skipping runtime '{}' because no script or cargo_test is defined", runtime);
645                    continue;
646                }
647
648                let entry_point_val = yaml::evaluate_jq(&parsed, ".clm.concrete_impl.process.entry_point");
649                let entry_point = entry_point_val.as_str().unwrap_or("");
650
651                fork_count += 1;
652                let start_time = std::time::Instant::now();
653                let _start_rss = get_children_peak_rss_kb();
654
655                let (code, out, err) = match runtime.as_str() {
656                    "shell" => {
657                        let entry_call = if !entry_point.is_empty() {
658                            let mut call = format!("{} ", entry_point);
659                            for a in &pos_args {
660                                call.push_str(&format!("\"{}\" ", a.replace('\"', "\\\"")));
661                            }
662                            call
663                        } else {
664                            "".to_string()
665                        };
666                        let full_script = format!("{}\n{}", script_content, entry_call);
667                        let mut cmd = create_sandboxed_command(&crate::fs_utils::get_bash_command(), &["-c".to_string(), full_script], &env_vars, clm_root);
668                        let output = cmd.output().map_err(|e| format!("Failed to spawn bash: {}", e))?;
669                        (
670                            output.status.code().unwrap_or(0),
671                            String::from_utf8_lossy(&output.stdout).into_owned(),
672                            String::from_utf8_lossy(&output.stderr).into_owned(),
673                        )
674                    }
675                    "python" => {
676                        let py_cmd = resolve_python_cmd(clm_root);
677                        let mut cmd_args = vec!["-c".to_string(), script_content.to_string()];
678                        cmd_args.extend(pos_args.clone());
679                        let mut py_env_vars = env_vars.clone();
680                        py_env_vars.insert("PYTHONPATH".to_string(), format!("{}/kernel/scripts", clm_root));
681                        let mut cmd = create_sandboxed_command(&py_cmd, &cmd_args, &py_env_vars, clm_root);
682                        let output = cmd.output().map_err(|e| format!("Failed to spawn python: {}", e))?;
683                        (
684                            output.status.code().unwrap_or(0),
685                            String::from_utf8_lossy(&output.stdout).into_owned(),
686                            String::from_utf8_lossy(&output.stderr).into_owned(),
687                        )
688                    }
689                    "node" => {
690                        let rand_id = std::time::SystemTime::now()
691                            .duration_since(std::time::UNIX_EPOCH)
692                            .map(|d| d.as_nanos())
693                            .unwrap_or(0);
694                        let pid = std::process::id();
695                        let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
696                        let _ = std::fs::create_dir_all(&temp_dir);
697                        let temp_file = temp_dir.join(format!("clm_node_inline_{}_{}.cjs", rand_id, pid));
698                        std::fs::write(&temp_file, script_content).map_err(|e| format!("Failed to write node inline script: {}", e))?;
699
700                        let mut cmd_args = vec!["--trace-uncaught".to_string(), temp_file.to_string_lossy().to_string()];
701                        cmd_args.extend(pos_args.clone());
702                        let mut cmd = create_sandboxed_command("node", &cmd_args, &env_vars, clm_root);
703                        // eprintln!("[DEBUG NODE] args={:?} envs={:?}", cmd_args, env_vars);
704                        let output = cmd.output();
705                        let _ = std::fs::remove_file(&temp_file);
706
707                        let output = output.map_err(|e| format!("Failed to spawn node: {}", e))?;
708                        (
709                            output.status.code().unwrap_or(0),
710                            String::from_utf8_lossy(&output.stdout).into_owned(),
711                            String::from_utf8_lossy(&output.stderr).into_owned(),
712                        )
713                    }
714                    "rust" => {
715                        if !cargo_test_name.is_empty() {
716                            let self_exe = resolve_self_exe(clm_root);
717                            let mut cmd = create_sandboxed_command(
718                                &self_exe.to_string_lossy(),
719                                &["run-test".to_string(), cargo_test_name.to_string()],
720                                &env_vars,
721                                clm_root,
722                            );
723                            cmd.current_dir(Path::new(clm_root).join("kernel/clm_rust_core"));
724
725                            let output = cmd.output().map_err(|e| format!("Failed to spawn clm_rust_core: {}", e))?;
726                            (
727                                output.status.code().unwrap_or(if output.status.success() { 0 } else { 1 }),
728                                String::from_utf8_lossy(&output.stdout).into_owned(),
729                                String::from_utf8_lossy(&output.stderr).into_owned(),
730                            )
731                        } else {
732                            let rand_id = std::time::SystemTime::now()
733                                .duration_since(std::time::UNIX_EPOCH)
734                                .map(|d| d.as_nanos())
735                                .unwrap_or(0);
736                            let pid = std::process::id();
737                            let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
738                            let _ = std::fs::create_dir_all(&temp_dir);
739                            let src_file = temp_dir.join(format!("clm_rust_inline_{}_{}.rs", rand_id, pid));
740                            let bin_file = temp_dir.join(format!("clm_rust_bin_{}_{}", rand_id, pid));
741
742                            let full_rust_src = if !script_content.contains("fn main") {
743                                format!(
744                                    "use std::io::{{self, Read}};\nfn main() {{\n{}\n}}",
745                                    script_content
746                                )
747                            } else {
748                                script_content.to_string()
749                            };
750                            std::fs::write(&src_file, full_rust_src).map_err(|e| format!("Failed to write rust inline source: {}", e))?;
751
752                            let compile_output = std::process::Command::new("rustc")
753                                .arg("-O")
754                                .arg("-o")
755                                .arg(&bin_file)
756                                .arg(&src_file)
757                                .output();
758                            let _ = std::fs::remove_file(&src_file);
759
760                            let compile_res = compile_output.map_err(|e| format!("Failed to run rustc: {}", e))?;
761                            if !compile_res.status.success() {
762                                return Err(format!("Rust compilation failed:\n{}", String::from_utf8_lossy(&compile_res.stderr)));
763                            }
764
765                            let mut cmd = std::process::Command::new(&bin_file);
766                            for a in &pos_args {
767                                cmd.arg(a);
768                            }
769                            for (k, v) in &env_vars {
770                                cmd.env(k, v);
771                            }
772                            cmd.current_dir(clm_root);
773
774                            let output = cmd.output();
775                            let _ = std::fs::remove_file(&bin_file);
776                            
777                            let output = output.map_err(|e| format!("Failed to run rust binary: {}", e))?;
778                            (
779                                output.status.code().unwrap_or(0),
780                                String::from_utf8_lossy(&output.stdout).into_owned(),
781                                String::from_utf8_lossy(&output.stderr).into_owned(),
782                            )
783                        }
784                    }
785                    _ => unreachable!(),
786                };
787
788                elapsed_ms = start_time.elapsed().as_millis() as i64;
789                let end_rss = get_children_peak_rss_kb();
790                #[cfg(unix)]
791                {
792                    peak_rss_val = if end_rss > _start_rss { end_rss - _start_rss } else { 0 };
793                }
794                #[cfg(windows)]
795                {
796                    peak_rss_val = end_rss;
797                }
798
799                (code, out, err)
800            };
801
802            if !stderr_data.is_empty() {
803                eprintln!("{}", stderr_data);
804            }
805
806            // Enforce SLA resource usage checks
807            if let Some(max_dur) = max_duration_ms
808                && elapsed_ms > max_dur {
809                    return Err(format!("❌ [SLA_VIOLATION] Case '{}' runtime '{}' exceeded max_duration_ms: {}ms > {}ms", case_name, runtime, elapsed_ms, max_dur));
810                }
811            if let Some(max_rss) = max_rss_kb
812                && peak_rss_val > max_rss {
813                    return Err(format!("❌ [SLA_VIOLATION] Case '{}' runtime '{}' exceeded max_rss_kb: {}KB > {}KB", case_name, runtime, peak_rss_val, max_rss));
814                }
815            if let Some(max_f) = max_forks
816                && fork_count > max_f {
817                    return Err(format!("❌ [SLA_VIOLATION] Case '{}' runtime '{}' exceeded max_forks: {} > {}", case_name, runtime, fork_count, max_f));
818                }
819
820            // Generate exec_hash and register Execution MCard
821            let runtime_facts = std::env::var("CLM_RUNTIME_FACTS").unwrap_or_default();
822            let clean_out = stdout_data.trim_end();
823            let exec_hash_input = format!(
824                "inputs: {} | outputs: {} | exit_code: {} | runtime: {} | runtime_facts: {}",
825                Value::Object(inputs.clone()), clean_out, run_status, runtime, runtime_facts
826            );
827            let exec_hash = crate::fs_utils::compute_content_hash(&exec_hash_input);
828
829            // Format extra fields
830            let mut extra_fields = format!(
831                "exit_code: {}\noutputs:\n  result: \"{}\"\ninputs:\n",
832                run_status, clean_out.replace('\"', "\\\"")
833            );
834            for (k, v) in &inputs {
835                let val_str = match v {
836                    Value::String(s) => s.clone(),
837                    other => other.to_string(),
838                };
839                extra_fields.push_str(&format!("  {}: {}\n", k, val_str));
840            }
841
842            // Write Execution MCard to db (isolated to sandbox if active)
843            let mcard_path = db::emit_exec_mcard(
844                &format!("polyglot_execution_{}", runtime),
845                &exec_hash,
846                &extra_fields,
847                &active_a_hash,
848                &active_c_hash,
849                clm_root,
850            ).map_err(|e| e.to_string())?;
851
852            eprintln!("[PAL:polyglot] ✅ Execution MCard ({}) registered: {}", runtime, mcard_path);
853
854            executed_runtimes.push(runtime.clone());
855            runtime_outputs.push(stdout_data);
856            runtime_exit_codes.push(run_status);
857            runtime_exec_hashes.push(exec_hash);
858        }
859
860        let exec_count = executed_runtimes.len();
861        if exec_count == 0 {
862            return Err(format!("No runtimes were successfully executed for case '{}'.", case_name));
863        }
864
865        // 6. Parity check and equivalence verification
866        let first_out = &runtime_outputs[0];
867        let first_code = runtime_exit_codes[0];
868        let first_norm = normalize_output(first_out);
869
870        let mut parity_failed = false;
871        let mut mismatch_rt = "";
872        let mut mismatch_out = String::new();
873
874        for i in 1..exec_count {
875            let r_name = &executed_runtimes[i];
876            let r_out = &runtime_outputs[i];
877            let r_code = runtime_exit_codes[i];
878            let r_norm = normalize_output(r_out);
879
880            if r_code != first_code {
881                parity_failed = true;
882                mismatch_rt = r_name;
883                mismatch_out = format!("Exit code mismatch: {}={} vs {}={}", executed_runtimes[0], first_code, r_name, r_code);
884                break;
885            }
886
887            if r_norm != first_norm {
888                parity_failed = true;
889                mismatch_rt = r_name;
890                mismatch_out = r_out.clone();
891                break;
892            }
893        }
894
895        if parity_failed {
896            let err_msg = format!(
897                "COHOMOLOGICAL OBSTRUCTION: Cross-Runtime Parity Fail\n\
898                 ❌ [PARITY_ERR] Polyglot execution mismatch between '{}' and '{}' in case '{}'!\n\
899                 --- Outputs:\n\
900                 1. {}: {}\n\
901                 2. {}: {}\n\
902                 --- Diff:\n",
903                executed_runtimes[0], mismatch_rt, case_name,
904                executed_runtimes[0], first_out.trim(),
905                mismatch_rt, mismatch_out.trim()
906            );
907            print_diff(&first_norm, &normalize_output(&mismatch_out));
908            return Err(err_msg);
909        }
910
911        if exec_count > 1 {
912            // Write consensus Execution MCard representing the parity check VCard witness
913            let mut sorted_hashes = runtime_exec_hashes.clone();
914            sorted_hashes.sort();
915            let fold_input = sorted_hashes.join("");
916            let parity_hash = crate::fs_utils::compute_sha256_hex(fold_input.as_bytes());
917
918            let clean_first_out = first_out.trim_end();
919            let mut parity_fields = format!(
920                "consensus_exit_code: {}\nconsensus_result: \"{}\"\nactive_runtimes:\n",
921                first_code, clean_first_out.replace('\"', "\\\"")
922            );
923            for r in &executed_runtimes {
924                parity_fields.push_str(&format!("  - {}\n", r));
925            }
926            let parity_file = db::emit_exec_mcard(
927                "polyglot_parity",
928                &parity_hash,
929                &parity_fields,
930                &active_a_hash,
931                &active_c_hash,
932                clm_root,
933            ).map_err(|e| e.to_string())?;
934
935            eprintln!("[PAL:polyglot] ✅ Consensus Parity MCard registered: {}", parity_file);
936        }
937
938        // Log Epiplexity Metrics
939        let shared_points = if exec_count > 1 { 1 } else { 0 };
940        let cross_confirmed = exec_count.saturating_sub(1);
941        eprintln!(
942            "[EPIPLEXITY] Metrics for case '{}': runtimes={}, shared_test_points={}, cross_confirmed_assertions={}",
943            case_name, exec_count, shared_points, cross_confirmed
944        );
945
946        consensus_exit_code = first_code;
947        last_case_output = first_out.clone();
948    }
949
950    Ok((consensus_exit_code, last_case_output))
951}
952
953/// Execute a script in the named runtime and return (exit_code, stdout, stderr).
954///
955/// This is a standalone runtime dispatch function that handles shell, python, node,
956/// and rust runtimes with environment variables, entry points, and cargo test support.
957/// It is called by the `exec-runtime-script` CLI command to enable Python code to
958/// delegate runtime execution to the Rust binary instead of spawning subprocesses.
959#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
960pub struct RuntimeScriptInput {
961    pub runtime: String,
962    pub script: String,
963    #[serde(default)]
964    pub env: HashMap<String, String>,
965    #[serde(default)]
966    pub entry_point: String,
967    #[serde(default)]
968    pub cargo_test: String,
969    #[serde(default)]
970    pub args: Vec<String>,
971    #[serde(default)]
972    pub clm_root: String,
973}
974
975#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
976pub struct RuntimeScriptOutput {
977    pub exit_code: i32,
978    pub stdout: String,
979    pub stderr: String,
980}
981
982pub fn exec_runtime_script(input: &RuntimeScriptInput) -> Result<RuntimeScriptOutput, String> {
983    let clm_root = if input.clm_root.is_empty() {
984        std::env::var("CLM_ROOT").unwrap_or_default()
985    } else {
986        input.clm_root.clone()
987    };
988
989    let runtime = input.runtime.as_str();
990    let script_content = input.script.as_str();
991    let entry_point = input.entry_point.as_str();
992    let cargo_test_name = input.cargo_test.as_str();
993    let pos_args = &input.args;
994
995    let (code, out, err) = match runtime {
996        "shell" => {
997            let entry_call = if !entry_point.is_empty() {
998                let mut call = format!("{} ", entry_point);
999                for a in pos_args {
1000                    call.push_str(&format!("\"{}\" ", a.replace('"', "\\\"")));
1001                }
1002                call
1003            } else {
1004                String::new()
1005            };
1006            let full_script = format!("{}\n{}", script_content, entry_call);
1007            let mut cmd = create_sandboxed_command(
1008                &crate::fs_utils::get_bash_command(),
1009                &["-c".to_string(), full_script],
1010                &input.env,
1011                &clm_root,
1012            );
1013            let output = cmd.output().map_err(|e| format!("Failed to spawn bash: {}", e))?;
1014            (
1015                output.status.code().unwrap_or(0),
1016                String::from_utf8_lossy(&output.stdout).into_owned(),
1017                String::from_utf8_lossy(&output.stderr).into_owned(),
1018            )
1019        }
1020        "python" => {
1021            let py_cmd = resolve_python_cmd(&clm_root);
1022            let mut cmd_args = vec!["-c".to_string(), script_content.to_string()];
1023            cmd_args.extend(pos_args.clone());
1024            let mut py_env = input.env.clone();
1025            py_env.insert("PYTHONPATH".to_string(), format!("{}/kernel/scripts", clm_root));
1026            let mut cmd = create_sandboxed_command(&py_cmd, &cmd_args, &py_env, &clm_root);
1027            let output = cmd.output().map_err(|e| format!("Failed to spawn python: {}", e))?;
1028            (
1029                output.status.code().unwrap_or(0),
1030                String::from_utf8_lossy(&output.stdout).into_owned(),
1031                String::from_utf8_lossy(&output.stderr).into_owned(),
1032            )
1033        }
1034        "node" => {
1035            let rand_id = std::time::SystemTime::now()
1036                .duration_since(std::time::UNIX_EPOCH)
1037                .map(|d| d.as_nanos())
1038                .unwrap_or(0);
1039            let pid = std::process::id();
1040            let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
1041            let _ = std::fs::create_dir_all(&temp_dir);
1042            let temp_file = temp_dir.join(format!("clm_node_inline_{}_{}.cjs", rand_id, pid));
1043            std::fs::write(&temp_file, script_content)
1044                .map_err(|e| format!("Failed to write node inline script: {}", e))?;
1045            let mut cmd_args = vec!["--trace-uncaught".to_string(), temp_file.to_string_lossy().to_string()];
1046            cmd_args.extend(pos_args.clone());
1047            let mut cmd = create_sandboxed_command("node", &cmd_args, &input.env, &clm_root);
1048            let output = cmd.output();
1049            let _ = std::fs::remove_file(&temp_file);
1050            let output = output.map_err(|e| format!("Failed to spawn node: {}", e))?;
1051            (
1052                output.status.code().unwrap_or(0),
1053                String::from_utf8_lossy(&output.stdout).into_owned(),
1054                String::from_utf8_lossy(&output.stderr).into_owned(),
1055            )
1056        }
1057        "rust" => {
1058            if !cargo_test_name.is_empty() {
1059                let self_exe = resolve_self_exe(&clm_root);
1060                let mut cmd = create_sandboxed_command(
1061                    &self_exe.to_string_lossy(),
1062                    &["run-test".to_string(), cargo_test_name.to_string()],
1063                    &input.env,
1064                    &clm_root,
1065                );
1066                cmd.current_dir(Path::new(&clm_root).join("kernel/clm_rust_core"));
1067                let output = cmd.output().map_err(|e| format!("Failed to spawn clm_rust_core: {}", e))?;
1068                (
1069                    output.status.code().unwrap_or(if output.status.success() { 0 } else { 1 }),
1070                    String::from_utf8_lossy(&output.stdout).into_owned(),
1071                    String::from_utf8_lossy(&output.stderr).into_owned(),
1072                )
1073            } else {
1074                let rand_id = std::time::SystemTime::now()
1075                    .duration_since(std::time::UNIX_EPOCH)
1076                    .map(|d| d.as_nanos())
1077                    .unwrap_or(0);
1078                let pid = std::process::id();
1079                let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
1080                let _ = std::fs::create_dir_all(&temp_dir);
1081                let src_file = temp_dir.join(format!("clm_rust_inline_{}_{}.rs", rand_id, pid));
1082                let bin_file = temp_dir.join(format!("clm_rust_bin_{}_{}", rand_id, pid));
1083                let full_rust_src = if !script_content.contains("fn main") {
1084                    format!("use std::io::{{self, Read}};\nfn main() {{\n{}\n}}", script_content)
1085                } else {
1086                    script_content.to_string()
1087                };
1088                std::fs::write(&src_file, full_rust_src)
1089                    .map_err(|e| format!("Failed to write rust inline source: {}", e))?;
1090                let compile_output = std::process::Command::new("rustc")
1091                    .arg("-O")
1092                    .arg("-o")
1093                    .arg(&bin_file)
1094                    .arg(&src_file)
1095                    .output();
1096                let _ = std::fs::remove_file(&src_file);
1097                let compile_res = compile_output.map_err(|e| format!("Failed to run rustc: {}", e))?;
1098                if !compile_res.status.success() {
1099                    return Ok(RuntimeScriptOutput {
1100                        exit_code: 1,
1101                        stdout: String::new(),
1102                        stderr: format!("Rust compilation failed:\n{}", String::from_utf8_lossy(&compile_res.stderr)),
1103                    });
1104                }
1105                let mut cmd = std::process::Command::new(&bin_file);
1106                for a in pos_args {
1107                    cmd.arg(a);
1108                }
1109                for (k, v) in &input.env {
1110                    cmd.env(k, v);
1111                }
1112                cmd.current_dir(&clm_root);
1113                let output = cmd.output();
1114                let _ = std::fs::remove_file(&bin_file);
1115                let output = output.map_err(|e| format!("Failed to run rust binary: {}", e))?;
1116                (
1117                    output.status.code().unwrap_or(0),
1118                    String::from_utf8_lossy(&output.stdout).into_owned(),
1119                    String::from_utf8_lossy(&output.stderr).into_owned(),
1120                )
1121            }
1122        }
1123        _ => {
1124            return Err(format!("Unsupported runtime: {}", runtime));
1125        }
1126    };
1127
1128    Ok(RuntimeScriptOutput {
1129        exit_code: code,
1130        stdout: out,
1131        stderr: err,
1132    })
1133}