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).to_string();
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.get(0).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                if name == p {
450                    target_cases.push(c.clone());
451                    found = true;
452                    break;
453                }
454            }
455        }
456        if !found {
457            return Err(format!("Case matching payload '{}' not found", p));
458        }
459    } else {
460        // Run all cases
461        for c in cases_arr.iter() {
462            target_cases.push(c.clone());
463        }
464    }
465
466    if target_cases.is_empty() {
467        return Err("No cases to execute".to_string());
468    }
469
470    let target_runtime = std::env::var("CLM_POLYGLOT_RUNTIME").ok();
471    if let Some(target) = &target_runtime {
472        eprintln!("[PAL:polyglot] Active runtime choice: {}", target);
473    }
474
475    // Resolve active hashes
476    let active_a_hash = std::env::var("CLM_ACTIVE_A_HASH").unwrap_or_default();
477    let active_c_hash = std::env::var("CLM_ACTIVE_C_HASH").unwrap_or_default();
478
479    let mut last_case_output = String::new();
480    let mut consensus_exit_code = 0;
481
482    for case in &target_cases {
483        let case_name = case.get("name").and_then(|v| v.as_str()).unwrap_or("unnamed_case");
484        let given = case.get("given").ok_or_else(|| format!("No given block found in case '{}'", case_name))?;
485
486        // Extract runtimes list and determine active runtime choice
487        let runtimes = match given.get("runtimes") {
488            Some(Value::Array(arr)) => arr.iter().filter_map(|v| v.as_str()).map(|s| s.to_string()).collect::<Vec<String>>(),
489            _ => return Err(format!("No runtimes found in given block of case '{}'", case_name)),
490        };
491
492        let active_runtimes = if let Some(target) = &target_runtime {
493            vec![target.clone()]
494        } else {
495            runtimes.clone()
496        };
497
498        // Inputs (excluding runtimes and requires)
499        let mut inputs = serde_json::Map::new();
500        let mut env_vars = std::collections::HashMap::new();
501        let mut pos_args = Vec::new();
502        if let Value::Object(map) = given {
503            for (k, v) in map {
504                if k != "runtimes" && k != "requires" {
505                    inputs.insert(k.clone(), v.clone());
506                    let val_str = match v {
507                        Value::String(s) => s.clone(),
508                        other => other.to_string(),
509                    };
510                    env_vars.insert(k.clone(), val_str.clone());
511                    pos_args.push(val_str);
512                }
513            }
514        }
515
516        for (k, v) in crate::utils::collect_clm_mock_vars() {
517            env_vars.insert(k, v);
518        }
519
520        // Mutual exclusivity checks
521        let then = case.get("then").ok_or_else(|| format!("No then block found in case '{}'", case_name))?;
522        let internal_ref = then.get("internal_reference").and_then(|v| v.as_str());
523        let native_harness = then.get("native_harness");
524        
525        if internal_ref.is_some() && native_harness.is_some() {
526            return Err(format!("Schema validation failed in case '{}': internal_reference and native_harness are mutually exclusive", case_name));
527        }
528
529        // SLA specifications
530        let sla = then.get("performance_sla");
531        let max_duration_ms = sla.and_then(|s| s.get("max_duration_ms")).and_then(|v| v.as_i64());
532        let max_rss_kb = sla.and_then(|s| s.get("max_rss_kb")).and_then(|v| v.as_i64());
533        let max_forks = sla.and_then(|s| s.get("max_forks")).and_then(|v| v.as_i64());
534
535        // Validate max_forks: 0 for internal_reference
536        if internal_ref.is_some() {
537            if let Some(mf) = max_forks {
538                if mf != 0 {
539                    return Err(format!("Schema validation failed in case '{}': max_forks must be 0 for internal_reference cases", case_name));
540                }
541            }
542        }
543
544        let mut executed_runtimes = Vec::new();
545        let mut runtime_outputs = Vec::new();
546        let mut runtime_exit_codes = Vec::new();
547        let mut runtime_exec_hashes = Vec::new();
548
549        for runtime in &active_runtimes {
550            // Enforce Gherkin cover check
551            if !runtimes.contains(runtime) {
552                eprintln!("[PAL:polyglot] ⏭️ Target runtime '{}' not requested in Gherkin cover: {:?}", runtime, runtimes);
553                continue;
554            }
555
556            // Verify supported runtimes
557            if runtime != "shell" && runtime != "python" && runtime != "node" && runtime != "rust" {
558                if target_runtime.is_some() {
559                    return Err(format!("Target runtime '{}' is not supported in Sprint 97", runtime));
560                } else {
561                    eprintln!("[PAL:polyglot] ⏭️ Skipping unsupported runtime '{}' in cover", runtime);
562                    continue;
563                }
564            }
565
566            // Check preconditions
567            if let Some(req_guard) = given.get("requires").and_then(|v| v.get(runtime)).and_then(|v| v.as_str()) {
568                if !check_precondition(req_guard, clm_root) {
569                    eprintln!("[PAL:polyglot] ⏭️ Pruning runtime '{}' (precondition not met: {})", runtime, req_guard);
570                    continue;
571                }
572            }
573
574            let mut fork_count = 0;
575            let elapsed_ms;
576            let peak_rss_val;
577
578            let (run_status, stdout_data, stderr_data) = if runtime == "rust" && internal_ref.is_some() {
579                // Zero-overhead in-process execution
580                let ref_path = internal_ref.unwrap();
581                let func = crate::evaluator::get_internal_registry(ref_path)
582                    .ok_or_else(|| format!("No internal function registered for path '{}'", ref_path))?;
583                
584                let start_time = std::time::Instant::now();
585                let _start_rss = get_peak_rss_kb();
586
587                // Setup Sandbox & execute
588                let run_res = {
589                    let _sandbox_guard = crate::db::DatabaseSandboxGuard::activate(case_name);
590                    func(clm_root, file_path)
591                };
592
593                elapsed_ms = start_time.elapsed().as_millis() as i64;
594                let end_rss = get_peak_rss_kb();
595                peak_rss_val = end_rss; // peak of the process self
596
597                let (code, out) = run_res.map_err(|e| format!("Internal dispatch error: {}", e))?;
598                (code, out, String::new())
599            } else if let Some(harness) = native_harness {
600                // Native harness execution
601                let tool = harness.get("tool").and_then(|v| v.as_str()).ok_or("No tool specified in native_harness")?;
602                let cmd_arg = harness.get("command").and_then(|v| v.as_str()).ok_or("No command specified in native_harness")?;
603                
604                fork_count += 1;
605                let full_cmd = format!("{} {}", tool, cmd_arg);
606                let start_time = std::time::Instant::now();
607                let start_rss = get_children_peak_rss_kb();
608
609                let mut cmd = create_sandboxed_command(&crate::fs_utils::get_bash_command(), &["-c".to_string(), full_cmd], &env_vars, clm_root);
610                let output = cmd.output().map_err(|e| format!("Failed to spawn native_harness: {}", e))?;
611                
612                elapsed_ms = start_time.elapsed().as_millis() as i64;
613                let end_rss = get_children_peak_rss_kb();
614                peak_rss_val = if end_rss > start_rss { end_rss - start_rss } else { 0 };
615
616                (
617                    output.status.code().unwrap_or(0),
618                    String::from_utf8_lossy(&output.stdout).into_owned(),
619                    String::from_utf8_lossy(&output.stderr).into_owned(),
620                )
621            } else {
622                // Legacy Fallback Script compilation & execution
623                let impl_path = format!(".clm.concrete_impl.process.implementations.{}", runtime);
624                let script_val = yaml::evaluate_jq(&parsed, &format!("{}.script", impl_path));
625                let handle_val = yaml::evaluate_jq(&parsed, &format!("{}.script_handle", impl_path));
626                let cargo_val = yaml::evaluate_jq(&parsed, &format!("{}.cargo_test", impl_path));
627                
628                let mut script_content = script_val.as_str().unwrap_or("").to_string();
629                let cargo_test_name = cargo_val.as_str().unwrap_or("").to_string();
630
631                if script_content.is_empty() {
632                    if let Some(handle) = handle_val.as_str() {
633                        if handle.starts_with("script:") {
634                            let s_name = handle.trim_start_matches("script:");
635                            let sh_path = Path::new(clm_root).join("scripts").join(format!("{}.sh", s_name));
636                            if let Ok(content) = std::fs::read_to_string(&sh_path) {
637                                script_content = content;
638                            } else {
639                                // Fallback check test dir
640                                let sh_path = Path::new(clm_root).join("tests").join("00_microkernel").join(format!("{}.sh", s_name));
641                                if let Ok(content) = std::fs::read_to_string(&sh_path) {
642                                    script_content = content;
643                                }
644                            }
645                        }
646                    }
647                }
648
649                if script_content.is_empty() && cargo_test_name.is_empty() {
650                    eprintln!("[PAL:polyglot] Skipping runtime '{}' because no script or cargo_test is defined", runtime);
651                    continue;
652                }
653
654                let entry_point_val = yaml::evaluate_jq(&parsed, ".clm.concrete_impl.process.entry_point");
655                let entry_point = entry_point_val.as_str().unwrap_or("");
656
657                fork_count += 1;
658                let start_time = std::time::Instant::now();
659                let _start_rss = get_children_peak_rss_kb();
660
661                let (code, out, err) = match runtime.as_str() {
662                    "shell" => {
663                        let entry_call = if !entry_point.is_empty() {
664                            let mut call = format!("{} ", entry_point);
665                            for a in &pos_args {
666                                call.push_str(&format!("\"{}\" ", a.replace('\"', "\\\"")));
667                            }
668                            call
669                        } else {
670                            "".to_string()
671                        };
672                        let full_script = format!("{}\n{}", script_content, entry_call);
673                        let mut cmd = create_sandboxed_command(&crate::fs_utils::get_bash_command(), &["-c".to_string(), full_script], &env_vars, clm_root);
674                        let output = cmd.output().map_err(|e| format!("Failed to spawn bash: {}", e))?;
675                        (
676                            output.status.code().unwrap_or(0),
677                            String::from_utf8_lossy(&output.stdout).into_owned(),
678                            String::from_utf8_lossy(&output.stderr).into_owned(),
679                        )
680                    }
681                    "python" => {
682                        let py_cmd = resolve_python_cmd(clm_root);
683                        let mut cmd_args = vec!["-c".to_string(), script_content.to_string()];
684                        cmd_args.extend(pos_args.clone());
685                        let mut py_env_vars = env_vars.clone();
686                        py_env_vars.insert("PYTHONPATH".to_string(), format!("{}/kernel/scripts", clm_root));
687                        let mut cmd = create_sandboxed_command(&py_cmd, &cmd_args, &py_env_vars, clm_root);
688                        let output = cmd.output().map_err(|e| format!("Failed to spawn python: {}", e))?;
689                        (
690                            output.status.code().unwrap_or(0),
691                            String::from_utf8_lossy(&output.stdout).into_owned(),
692                            String::from_utf8_lossy(&output.stderr).into_owned(),
693                        )
694                    }
695                    "node" => {
696                        let rand_id = std::time::SystemTime::now()
697                            .duration_since(std::time::UNIX_EPOCH)
698                            .map(|d| d.as_nanos())
699                            .unwrap_or(0);
700                        let pid = std::process::id();
701                        let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
702                        let _ = std::fs::create_dir_all(&temp_dir);
703                        let temp_file = temp_dir.join(format!("clm_node_inline_{}_{}.cjs", rand_id, pid));
704                        std::fs::write(&temp_file, script_content).map_err(|e| format!("Failed to write node inline script: {}", e))?;
705
706                        let mut cmd_args = vec!["--trace-uncaught".to_string(), temp_file.to_string_lossy().to_string()];
707                        cmd_args.extend(pos_args.clone());
708                        let mut cmd = create_sandboxed_command("node", &cmd_args, &env_vars, clm_root);
709                        // eprintln!("[DEBUG NODE] args={:?} envs={:?}", cmd_args, env_vars);
710                        let output = cmd.output();
711                        let _ = std::fs::remove_file(&temp_file);
712
713                        let output = output.map_err(|e| format!("Failed to spawn node: {}", e))?;
714                        (
715                            output.status.code().unwrap_or(0),
716                            String::from_utf8_lossy(&output.stdout).into_owned(),
717                            String::from_utf8_lossy(&output.stderr).into_owned(),
718                        )
719                    }
720                    "rust" => {
721                        if !cargo_test_name.is_empty() {
722                            let self_exe = resolve_self_exe(clm_root);
723                            let mut cmd = create_sandboxed_command(
724                                &self_exe.to_string_lossy(),
725                                &["run-test".to_string(), cargo_test_name.to_string()],
726                                &env_vars,
727                                clm_root,
728                            );
729                            cmd.current_dir(Path::new(clm_root).join("kernel/clm_rust_core"));
730
731                            let output = cmd.output().map_err(|e| format!("Failed to spawn clm_rust_core: {}", e))?;
732                            (
733                                output.status.code().unwrap_or(if output.status.success() { 0 } else { 1 }),
734                                String::from_utf8_lossy(&output.stdout).into_owned(),
735                                String::from_utf8_lossy(&output.stderr).into_owned(),
736                            )
737                        } else {
738                            let rand_id = std::time::SystemTime::now()
739                                .duration_since(std::time::UNIX_EPOCH)
740                                .map(|d| d.as_nanos())
741                                .unwrap_or(0);
742                            let pid = std::process::id();
743                            let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
744                            let _ = std::fs::create_dir_all(&temp_dir);
745                            let src_file = temp_dir.join(format!("clm_rust_inline_{}_{}.rs", rand_id, pid));
746                            let bin_file = temp_dir.join(format!("clm_rust_bin_{}_{}", rand_id, pid));
747
748                            let full_rust_src = if !script_content.contains("fn main") {
749                                format!(
750                                    "use std::io::{{self, Read}};\nfn main() {{\n{}\n}}",
751                                    script_content
752                                )
753                            } else {
754                                script_content.to_string()
755                            };
756                            std::fs::write(&src_file, full_rust_src).map_err(|e| format!("Failed to write rust inline source: {}", e))?;
757
758                            let compile_output = std::process::Command::new("rustc")
759                                .arg("-O")
760                                .arg("-o")
761                                .arg(&bin_file)
762                                .arg(&src_file)
763                                .output();
764                            let _ = std::fs::remove_file(&src_file);
765
766                            let compile_res = compile_output.map_err(|e| format!("Failed to run rustc: {}", e))?;
767                            if !compile_res.status.success() {
768                                return Err(format!("Rust compilation failed:\n{}", String::from_utf8_lossy(&compile_res.stderr)));
769                            }
770
771                            let mut cmd = std::process::Command::new(&bin_file);
772                            for a in &pos_args {
773                                cmd.arg(a);
774                            }
775                            for (k, v) in &env_vars {
776                                cmd.env(k, v);
777                            }
778                            cmd.current_dir(clm_root);
779
780                            let output = cmd.output();
781                            let _ = std::fs::remove_file(&bin_file);
782                            
783                            let output = output.map_err(|e| format!("Failed to run rust binary: {}", e))?;
784                            (
785                                output.status.code().unwrap_or(0),
786                                String::from_utf8_lossy(&output.stdout).into_owned(),
787                                String::from_utf8_lossy(&output.stderr).into_owned(),
788                            )
789                        }
790                    }
791                    _ => unreachable!(),
792                };
793
794                elapsed_ms = start_time.elapsed().as_millis() as i64;
795                let end_rss = get_children_peak_rss_kb();
796                #[cfg(unix)]
797                {
798                    peak_rss_val = if end_rss > _start_rss { end_rss - _start_rss } else { 0 };
799                }
800                #[cfg(windows)]
801                {
802                    peak_rss_val = end_rss;
803                }
804
805                (code, out, err)
806            };
807
808            if !stderr_data.is_empty() {
809                eprintln!("{}", stderr_data);
810            }
811
812            // Enforce SLA resource usage checks
813            if let Some(max_dur) = max_duration_ms {
814                if elapsed_ms > max_dur {
815                    return Err(format!("❌ [SLA_VIOLATION] Case '{}' runtime '{}' exceeded max_duration_ms: {}ms > {}ms", case_name, runtime, elapsed_ms, max_dur));
816                }
817            }
818            if let Some(max_rss) = max_rss_kb {
819                if peak_rss_val > max_rss {
820                    return Err(format!("❌ [SLA_VIOLATION] Case '{}' runtime '{}' exceeded max_rss_kb: {}KB > {}KB", case_name, runtime, peak_rss_val, max_rss));
821                }
822            }
823            if let Some(max_f) = max_forks {
824                if fork_count > max_f {
825                    return Err(format!("❌ [SLA_VIOLATION] Case '{}' runtime '{}' exceeded max_forks: {} > {}", case_name, runtime, fork_count, max_f));
826                }
827            }
828
829            // Generate exec_hash and register Execution MCard
830            let runtime_facts = std::env::var("CLM_RUNTIME_FACTS").unwrap_or_default();
831            let clean_out = stdout_data.trim_end();
832            let exec_hash_input = format!(
833                "inputs: {} | outputs: {} | exit_code: {} | runtime: {} | runtime_facts: {}",
834                Value::Object(inputs.clone()), clean_out, run_status, runtime, runtime_facts
835            );
836            let exec_hash = crate::fs_utils::compute_content_hash(&exec_hash_input);
837
838            // Format extra fields
839            let mut extra_fields = format!(
840                "exit_code: {}\noutputs:\n  result: \"{}\"\ninputs:\n",
841                run_status, clean_out.replace('\"', "\\\"")
842            );
843            for (k, v) in &inputs {
844                let val_str = match v {
845                    Value::String(s) => s.clone(),
846                    other => other.to_string(),
847                };
848                extra_fields.push_str(&format!("  {}: {}\n", k, val_str));
849            }
850
851            // Write Execution MCard to db (isolated to sandbox if active)
852            let mcard_path = db::emit_exec_mcard(
853                &format!("polyglot_execution_{}", runtime),
854                &exec_hash,
855                &extra_fields,
856                &active_a_hash,
857                &active_c_hash,
858                clm_root,
859            ).map_err(|e| e.to_string())?;
860
861            eprintln!("[PAL:polyglot] ✅ Execution MCard ({}) registered: {}", runtime, mcard_path);
862
863            executed_runtimes.push(runtime.clone());
864            runtime_outputs.push(stdout_data);
865            runtime_exit_codes.push(run_status);
866            runtime_exec_hashes.push(exec_hash);
867        }
868
869        let exec_count = executed_runtimes.len();
870        if exec_count == 0 {
871            return Err(format!("No runtimes were successfully executed for case '{}'.", case_name));
872        }
873
874        // 6. Parity check and equivalence verification
875        let first_out = &runtime_outputs[0];
876        let first_code = runtime_exit_codes[0];
877        let first_norm = normalize_output(first_out);
878
879        let mut parity_failed = false;
880        let mut mismatch_rt = "";
881        let mut mismatch_out = String::new();
882
883        for i in 1..exec_count {
884            let r_name = &executed_runtimes[i];
885            let r_out = &runtime_outputs[i];
886            let r_code = runtime_exit_codes[i];
887            let r_norm = normalize_output(r_out);
888
889            if r_code != first_code {
890                parity_failed = true;
891                mismatch_rt = r_name;
892                mismatch_out = format!("Exit code mismatch: {}={} vs {}={}", executed_runtimes[0], first_code, r_name, r_code);
893                break;
894            }
895
896            if r_norm != first_norm {
897                parity_failed = true;
898                mismatch_rt = r_name;
899                mismatch_out = r_out.clone();
900                break;
901            }
902        }
903
904        if parity_failed {
905            let err_msg = format!(
906                "COHOMOLOGICAL OBSTRUCTION: Cross-Runtime Parity Fail\n\
907                 ❌ [PARITY_ERR] Polyglot execution mismatch between '{}' and '{}' in case '{}'!\n\
908                 --- Outputs:\n\
909                 1. {}: {}\n\
910                 2. {}: {}\n\
911                 --- Diff:\n",
912                executed_runtimes[0], mismatch_rt, case_name,
913                executed_runtimes[0], first_out.trim(),
914                mismatch_rt, mismatch_out.trim()
915            );
916            print_diff(&first_norm, &normalize_output(&mismatch_out));
917            return Err(err_msg);
918        }
919
920        if exec_count > 1 {
921            // Write consensus Execution MCard representing the parity check VCard witness
922            let mut sorted_hashes = runtime_exec_hashes.clone();
923            sorted_hashes.sort();
924            let fold_input = sorted_hashes.join("");
925            let parity_hash = crate::fs_utils::compute_sha256_hex(fold_input.as_bytes());
926
927            let clean_first_out = first_out.trim_end();
928            let mut parity_fields = format!(
929                "consensus_exit_code: {}\nconsensus_result: \"{}\"\nactive_runtimes:\n",
930                first_code, clean_first_out.replace('\"', "\\\"")
931            );
932            for r in &executed_runtimes {
933                parity_fields.push_str(&format!("  - {}\n", r));
934            }
935            let parity_file = db::emit_exec_mcard(
936                "polyglot_parity",
937                &parity_hash,
938                &parity_fields,
939                &active_a_hash,
940                &active_c_hash,
941                clm_root,
942            ).map_err(|e| e.to_string())?;
943
944            eprintln!("[PAL:polyglot] ✅ Consensus Parity MCard registered: {}", parity_file);
945        }
946
947        // Log Epiplexity Metrics
948        let shared_points = if exec_count > 1 { 1 } else { 0 };
949        let cross_confirmed = if exec_count > 1 { exec_count - 1 } else { 0 };
950        eprintln!(
951            "[EPIPLEXITY] Metrics for case '{}': runtimes={}, shared_test_points={}, cross_confirmed_assertions={}",
952            case_name, exec_count, shared_points, cross_confirmed
953        );
954
955        consensus_exit_code = first_code;
956        last_case_output = first_out.clone();
957    }
958
959    Ok((consensus_exit_code, last_case_output))
960}
961
962/// Execute a script in the named runtime and return (exit_code, stdout, stderr).
963///
964/// This is a standalone runtime dispatch function that handles shell, python, node,
965/// and rust runtimes with environment variables, entry points, and cargo test support.
966/// It is called by the `exec-runtime-script` CLI command to enable Python code to
967/// delegate runtime execution to the Rust binary instead of spawning subprocesses.
968#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
969pub struct RuntimeScriptInput {
970    pub runtime: String,
971    pub script: String,
972    #[serde(default)]
973    pub env: HashMap<String, String>,
974    #[serde(default)]
975    pub entry_point: String,
976    #[serde(default)]
977    pub cargo_test: String,
978    #[serde(default)]
979    pub args: Vec<String>,
980    #[serde(default)]
981    pub clm_root: String,
982}
983
984#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
985pub struct RuntimeScriptOutput {
986    pub exit_code: i32,
987    pub stdout: String,
988    pub stderr: String,
989}
990
991pub fn exec_runtime_script(input: &RuntimeScriptInput) -> Result<RuntimeScriptOutput, String> {
992    let clm_root = if input.clm_root.is_empty() {
993        std::env::var("CLM_ROOT").unwrap_or_default()
994    } else {
995        input.clm_root.clone()
996    };
997
998    let runtime = input.runtime.as_str();
999    let script_content = input.script.as_str();
1000    let entry_point = input.entry_point.as_str();
1001    let cargo_test_name = input.cargo_test.as_str();
1002    let pos_args = &input.args;
1003
1004    let (code, out, err) = match runtime {
1005        "shell" => {
1006            let entry_call = if !entry_point.is_empty() {
1007                let mut call = format!("{} ", entry_point);
1008                for a in pos_args {
1009                    call.push_str(&format!("\"{}\" ", a.replace('"', "\\\"")));
1010                }
1011                call
1012            } else {
1013                String::new()
1014            };
1015            let full_script = format!("{}\n{}", script_content, entry_call);
1016            let mut cmd = create_sandboxed_command(
1017                &crate::fs_utils::get_bash_command(),
1018                &["-c".to_string(), full_script],
1019                &input.env,
1020                &clm_root,
1021            );
1022            let output = cmd.output().map_err(|e| format!("Failed to spawn bash: {}", e))?;
1023            (
1024                output.status.code().unwrap_or(0),
1025                String::from_utf8_lossy(&output.stdout).into_owned(),
1026                String::from_utf8_lossy(&output.stderr).into_owned(),
1027            )
1028        }
1029        "python" => {
1030            let py_cmd = resolve_python_cmd(&clm_root);
1031            let mut cmd_args = vec!["-c".to_string(), script_content.to_string()];
1032            cmd_args.extend(pos_args.clone());
1033            let mut py_env = input.env.clone();
1034            py_env.insert("PYTHONPATH".to_string(), format!("{}/kernel/scripts", clm_root));
1035            let mut cmd = create_sandboxed_command(&py_cmd, &cmd_args, &py_env, &clm_root);
1036            let output = cmd.output().map_err(|e| format!("Failed to spawn python: {}", e))?;
1037            (
1038                output.status.code().unwrap_or(0),
1039                String::from_utf8_lossy(&output.stdout).into_owned(),
1040                String::from_utf8_lossy(&output.stderr).into_owned(),
1041            )
1042        }
1043        "node" => {
1044            let rand_id = std::time::SystemTime::now()
1045                .duration_since(std::time::UNIX_EPOCH)
1046                .map(|d| d.as_nanos())
1047                .unwrap_or(0);
1048            let pid = std::process::id();
1049            let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
1050            let _ = std::fs::create_dir_all(&temp_dir);
1051            let temp_file = temp_dir.join(format!("clm_node_inline_{}_{}.cjs", rand_id, pid));
1052            std::fs::write(&temp_file, script_content)
1053                .map_err(|e| format!("Failed to write node inline script: {}", e))?;
1054            let mut cmd_args = vec!["--trace-uncaught".to_string(), temp_file.to_string_lossy().to_string()];
1055            cmd_args.extend(pos_args.clone());
1056            let mut cmd = create_sandboxed_command("node", &cmd_args, &input.env, &clm_root);
1057            let output = cmd.output();
1058            let _ = std::fs::remove_file(&temp_file);
1059            let output = output.map_err(|e| format!("Failed to spawn node: {}", e))?;
1060            (
1061                output.status.code().unwrap_or(0),
1062                String::from_utf8_lossy(&output.stdout).into_owned(),
1063                String::from_utf8_lossy(&output.stderr).into_owned(),
1064            )
1065        }
1066        "rust" => {
1067            if !cargo_test_name.is_empty() {
1068                let self_exe = resolve_self_exe(&clm_root);
1069                let mut cmd = create_sandboxed_command(
1070                    &self_exe.to_string_lossy(),
1071                    &["run-test".to_string(), cargo_test_name.to_string()],
1072                    &input.env,
1073                    &clm_root,
1074                );
1075                cmd.current_dir(Path::new(&clm_root).join("kernel/clm_rust_core"));
1076                let output = cmd.output().map_err(|e| format!("Failed to spawn clm_rust_core: {}", e))?;
1077                (
1078                    output.status.code().unwrap_or(if output.status.success() { 0 } else { 1 }),
1079                    String::from_utf8_lossy(&output.stdout).into_owned(),
1080                    String::from_utf8_lossy(&output.stderr).into_owned(),
1081                )
1082            } else {
1083                let rand_id = std::time::SystemTime::now()
1084                    .duration_since(std::time::UNIX_EPOCH)
1085                    .map(|d| d.as_nanos())
1086                    .unwrap_or(0);
1087                let pid = std::process::id();
1088                let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
1089                let _ = std::fs::create_dir_all(&temp_dir);
1090                let src_file = temp_dir.join(format!("clm_rust_inline_{}_{}.rs", rand_id, pid));
1091                let bin_file = temp_dir.join(format!("clm_rust_bin_{}_{}", rand_id, pid));
1092                let full_rust_src = if !script_content.contains("fn main") {
1093                    format!("use std::io::{{self, Read}};\nfn main() {{\n{}\n}}", script_content)
1094                } else {
1095                    script_content.to_string()
1096                };
1097                std::fs::write(&src_file, full_rust_src)
1098                    .map_err(|e| format!("Failed to write rust inline source: {}", e))?;
1099                let compile_output = std::process::Command::new("rustc")
1100                    .arg("-O")
1101                    .arg("-o")
1102                    .arg(&bin_file)
1103                    .arg(&src_file)
1104                    .output();
1105                let _ = std::fs::remove_file(&src_file);
1106                let compile_res = compile_output.map_err(|e| format!("Failed to run rustc: {}", e))?;
1107                if !compile_res.status.success() {
1108                    return Ok(RuntimeScriptOutput {
1109                        exit_code: 1,
1110                        stdout: String::new(),
1111                        stderr: format!("Rust compilation failed:\n{}", String::from_utf8_lossy(&compile_res.stderr)),
1112                    });
1113                }
1114                let mut cmd = std::process::Command::new(&bin_file);
1115                for a in pos_args {
1116                    cmd.arg(a);
1117                }
1118                for (k, v) in &input.env {
1119                    cmd.env(k, v);
1120                }
1121                cmd.current_dir(&clm_root);
1122                let output = cmd.output();
1123                let _ = std::fs::remove_file(&bin_file);
1124                let output = output.map_err(|e| format!("Failed to run rust binary: {}", e))?;
1125                (
1126                    output.status.code().unwrap_or(0),
1127                    String::from_utf8_lossy(&output.stdout).into_owned(),
1128                    String::from_utf8_lossy(&output.stderr).into_owned(),
1129                )
1130            }
1131        }
1132        _ => {
1133            return Err(format!("Unsupported runtime: {}", runtime));
1134        }
1135    };
1136
1137    Ok(RuntimeScriptOutput {
1138        exit_code: code,
1139        stdout: out,
1140        stderr: err,
1141    })
1142}