clm-kernel 0.0.11

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use serde_json::Value;
use crate::memorize;
use crate::yaml;
use crate::db;

#[cfg(unix)]
#[repr(C)]
#[derive(Clone, Copy)]
pub struct timeval {
    pub tv_sec: i64,
    pub tv_usec: i32,
    #[cfg(target_os = "macos")]
    pub _padding: i32,
}

#[cfg(unix)]
#[repr(C)]
pub struct rusage {
    pub ru_utime: timeval,
    pub ru_stime: timeval,
    pub ru_maxrss: i64,
    pub ru_ixrss: i64,
    pub ru_idrss: i64,
    pub ru_isrss: i64,
    pub ru_minflt: i64,
    pub ru_majflt: i64,
    pub ru_nswap: i64,
    pub ru_inblock: i64,
    pub ru_oublock: i64,
    pub ru_msgsnd: i64,
    pub ru_msgrcv: i64,
    pub ru_nsignals: i64,
    pub ru_nvcsw: i64,
    pub ru_nivcsw: i64,
}

#[cfg(unix)]
const RUSAGE_SELF: i32 = 0;
#[cfg(unix)]
const RUSAGE_CHILDREN: i32 = -1;

#[cfg(unix)]
#[allow(dead_code)]
unsafe extern "C" {
    fn dup(fd: i32) -> i32;
    fn dup2(src: i32, dst: i32) -> i32;
    fn close(fd: i32) -> i32;
    fn getrusage(who: i32, usage: *mut rusage) -> i32;
}

#[cfg(unix)]
pub fn get_peak_rss_kb() -> i64 {
    let mut usage = unsafe { std::mem::zeroed::<rusage>() };
    let res = unsafe { getrusage(RUSAGE_SELF, &mut usage) };
    if res == 0 {
        #[cfg(target_os = "macos")]
        {
            usage.ru_maxrss / 1024
        }
        #[cfg(not(target_os = "macos"))]
        {
            usage.ru_maxrss
        }
    } else {
        0
    }
}

#[cfg(unix)]
pub fn get_children_peak_rss_kb() -> i64 {
    let mut usage = unsafe { std::mem::zeroed::<rusage>() };
    let res = unsafe { getrusage(RUSAGE_CHILDREN, &mut usage) };
    if res == 0 {
        #[cfg(target_os = "macos")]
        {
            usage.ru_maxrss / 1024
        }
        #[cfg(not(target_os = "macos"))]
        {
            usage.ru_maxrss
        }
    } else {
        0
    }
}

#[cfg(windows)]
#[repr(C)]
#[allow(non_snake_case)]
struct PROCESS_MEMORY_COUNTERS {
    cb: u32,
    PageFaultCount: u32,
    PeakWorkingSetSize: usize,
    WorkingSetSize: usize,
    QuotaPeakPagedPoolUsage: usize,
    QuotaPagedPoolUsage: usize,
    QuotaPeakNonPagedPoolUsage: usize,
    QuotaNonPagedPoolUsage: usize,
    PagefileUsage: usize,
    PeakPagefileUsage: usize,
}

#[cfg(windows)]
#[link(name = "psapi")]
unsafe extern "system" {
    fn GetCurrentProcess() -> *mut std::ffi::c_void;
    fn GetProcessMemoryInfo(
        process: *mut std::ffi::c_void,
        counters: *mut PROCESS_MEMORY_COUNTERS,
        cb: u32,
    ) -> i32;
}

#[cfg(windows)]
pub fn get_peak_rss_kb() -> i64 {
    let mut counters = unsafe { std::mem::zeroed::<PROCESS_MEMORY_COUNTERS>() };
    counters.cb = std::mem::size_of::<PROCESS_MEMORY_COUNTERS>() as u32;
    let res = unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut counters, counters.cb) };
    if res != 0 {
        (counters.PeakWorkingSetSize / 1024) as i64
    } else {
        0
    }
}

#[cfg(windows)]
pub fn get_children_peak_rss_kb() -> i64 {
    get_peak_rss_kb()
}

pub fn resolve_python_cmd(clm_root: &str) -> String {
    let root_path = std::path::Path::new(clm_root);
    
    // Windows virtual env python
    let venv_py_win = root_path.join(".venv/Scripts/python.exe");
    if venv_py_win.exists() {
        return venv_py_win.to_string_lossy().to_string();
    }
    
    // Unix virtual env python
    let venv_py_unix = root_path.join(".venv/bin/python3");
    if venv_py_unix.exists() {
        return venv_py_unix.to_string_lossy().to_string();
    }
    
    let venv_py_unix_2 = root_path.join(".venv/bin/python");
    if venv_py_unix_2.exists() {
        return venv_py_unix_2.to_string_lossy().to_string();
    }

    if cfg!(target_os = "windows") {
        for cmd in &["python", "python3"] {
            if std::process::Command::new(cmd).arg("--version").output().map(|o| o.status.success()).unwrap_or(false) {
                return cmd.to_string();
            }
        }
    } else {
        for cmd in &["python3", "python"] {
            if std::process::Command::new(cmd).arg("--version").output().map(|o| o.status.success()).unwrap_or(false) {
                return cmd.to_string();
            }
        }
    }
    
    if cfg!(target_os = "windows") {
        "python".to_string()
    } else {
        "python3".to_string()
    }
}

pub fn get_interpreter_version(interpreter: &str, clm_root: &str) -> Option<String> {
    let cmd_name = match interpreter {
        "python" | "python3" => resolve_python_cmd(clm_root),
        other => other.to_string(),
    };
    let output = std::process::Command::new(&cmd_name)
        .arg("--version")
        .output()
        .ok();
    if let Some(out) = output {
        let text = String::from_utf8_lossy(&out.stdout).to_string()
            + String::from_utf8_lossy(&out.stderr).as_ref();
        let re = regex::Regex::new(r"(\d+\.\d+(?:\.\d+)?)").unwrap();
        if let Some(caps) = re.captures(&text) {
            return Some(caps[1].to_string());
        }
    }
    None
}

pub fn compare_versions(current: &str, op: &str, req: &str) -> bool {
    let parse_ver = |s: &str| -> (u32, u32, u32) {
        let parts: Vec<&str> = s.split('.').collect();
        let major = parts.first().and_then(|v| v.parse().ok()).unwrap_or(0);
        let minor = parts.get(1).and_then(|v| v.parse().ok()).unwrap_or(0);
        let patch = parts.get(2).and_then(|v| v.parse().ok()).unwrap_or(0);
        (major, minor, patch)
    };
    let v1 = parse_ver(current);
    let v2 = parse_ver(req);
    match op {
        ">=" => v1 >= v2,
        "<=" => v1 <= v2,
        ">" => v1 > v2,
        "<" => v1 < v2,
        _ => v1 == v2,
    }
}

pub fn check_precondition(req_str: &str, clm_root: &str) -> bool {
    let re = regex::Regex::new(r"^interpreter:([a-zA-Z0-9_-]+)(>=|<=|>|<|==|=)([0-9.]+)$").unwrap();
    if let Some(caps) = re.captures(req_str) {
        let interpreter = &caps[1];
        let op = &caps[2];
        let req_ver = &caps[3];
        if let Some(current_ver) = get_interpreter_version(interpreter, clm_root) {
            compare_versions(&current_ver, op, req_ver)
        } else {
            false
        }
    } else {
        true
    }
}

pub fn create_sandboxed_command(
    program: &str,
    args: &[String],
    envs: &HashMap<String, String>,
    clm_root: &str,
) -> std::process::Command {
    let clm_no_sandbox = envs.get("CLM_NO_SANDBOX").map(|v| v == "true").unwrap_or_else(|| {
        crate::cross_cutting::context::CLMContext::get_scoped_or_env("CLM_NO_SANDBOX")
            .unwrap_or_default()
            == "true"
    });
    let has_sandbox_exec = std::process::Command::new("which")
        .arg("sandbox-exec")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);

    let mut cmd = if !clm_no_sandbox && has_sandbox_exec {
        let home_dir = std::env::var("HOME").unwrap_or_default();
        let profile = format!(
            r#"(version 1)
(deny default)
(allow file-read-metadata)
(allow file-read-data)
(allow file-write-data)
(allow file-read*
    (subpath "/usr")
    (subpath "/System")
    (subpath "/Library")
    (subpath "/private")
    (subpath "/var")
    (subpath "/tmp")
    (subpath "/etc")
    (subpath "/opt")
    (subpath "/dev")
    (subpath "{}")
    (subpath "{}"))
(allow file-write*
    (subpath "{}/data")
    (subpath "{}/db")
    (subpath "{}/types/meta")
    (literal "{}/result_open_interpreter.txt")
    (subpath "{}/.clm")
    (subpath "/private/var")
    (subpath "/private/tmp")
    (subpath "/var")
    (subpath "/tmp")
    (literal "/dev/null")
    (literal "/dev/zero"))
(allow file-map-executable
    (subpath "/usr")
    (subpath "/System")
    (subpath "/Library")
    (subpath "/opt")
    (subpath "{}")
    (subpath "{}"))
(allow process-exec
    (subpath "/bin")
    (subpath "/usr/bin")
    (subpath "/usr/local/bin")
    (subpath "/opt/homebrew")
    (subpath "/Library/Frameworks/Python.framework")
    (subpath "/Library/Developer/CommandLineTools")
    (subpath "/private/var")
    (subpath "/var")
    (subpath "/tmp")
    (subpath "{}")
    (subpath "{}"))
(allow process-fork)
(allow process-info*)
(allow ipc-posix*)
(allow sysctl-read)
(allow mach-lookup)
(allow dynamic-code-generation)
(allow network-bind)
(allow network-inbound)
(allow network-outbound)
(deny file-read*
    (literal "/private/etc/passwd")
    (literal "/etc/passwd"))
(deny file-read-data
    (literal "/private/etc/passwd")
    (literal "/etc/passwd"))"#,
            clm_root, home_dir, clm_root, clm_root, clm_root, clm_root, home_dir, clm_root, home_dir, clm_root, home_dir
        );
        let mut c = std::process::Command::new("sandbox-exec");
        c.arg("-p").arg(profile).arg(program);
        c
    } else {
        std::process::Command::new(program)
    };

    for var_name in crate::layer0::runtime_contract::ENV_PASSTHROUGH_WHITELIST {
        if let Some(val) = crate::cross_cutting::context::CLMContext::get_scoped_or_env(var_name) {
            cmd.env(var_name, val);
        }
    }
    crate::cross_cutting::context::CLMContext::apply_current_to_command(&mut cmd);
    for (k, v) in envs {
        cmd.env(k, v);
    }
    for a in args {
        cmd.arg(a);
    }
    cmd
}

pub fn normalize_output(raw_out: &str) -> String {
    use serde_json::Value;
    let stripped = raw_out.replace('\r', "");
    let stripped = stripped.trim_end().to_string();
    if let Ok(val) = serde_json::from_str::<Value>(&stripped) {
        serde_json::to_string(&val).unwrap_or(stripped)
    } else {
        stripped
    }
}

pub fn print_diff(first_norm: &str, second_norm: &str) {
    let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
    let f1 = temp_dir.join(format!("norm_f1_{}", std::process::id()));
    let f2 = temp_dir.join(format!("norm_f2_{}", std::process::id()));
    let _ = std::fs::write(&f1, first_norm);
    let _ = std::fs::write(&f2, second_norm);
    
    let diff_output = std::process::Command::new("diff")
        .arg("-u")
        .arg(&f1)
        .arg(&f2)
        .output();
    if let Ok(out) = diff_output {
        let text = String::from_utf8_lossy(&out.stdout);
        eprintln!("{}", text);
    }
    
    let _ = std::fs::remove_file(f1);
    let _ = std::fs::remove_file(f2);
}

pub fn resolve_self_exe(clm_root: &str) -> PathBuf {
    if let Ok(self_exe) = std::env::current_exe() {
        if self_exe.to_string_lossy().contains("deps") {
            let alt_exe = Path::new(clm_root).join({
                #[cfg(windows)] { "kernel/clm_rust_core/target/debug/clm_rust_core.exe" }
                #[cfg(not(windows))] { "kernel/clm_rust_core/target/debug/clm_rust_core" }
            });
            if alt_exe.exists() {
                return alt_exe;
            }
            let alt_exe_release = Path::new(clm_root).join({
                #[cfg(windows)] { "kernel/clm_rust_core/target/release/clm_rust_core.exe" }
                #[cfg(not(windows))] { "kernel/clm_rust_core/target/release/clm_rust_core" }
            });
            if alt_exe_release.exists() {
                return alt_exe_release;
            }
        }
        if let Some(parent) = self_exe.parent() {
            let local_exe = parent.join({
                #[cfg(windows)] { "clm_rust_core.exe" }
                #[cfg(not(windows))] { "clm_rust_core" }
            });
            if local_exe.exists() {
                return local_exe;
            }
        }
        return self_exe;
    }
    Path::new(clm_root).join({
        #[cfg(windows)] { "kernel/clm_rust_core/target/release/clm_rust_core.exe" }
        #[cfg(not(windows))] { "kernel/clm_rust_core/target/release/clm_rust_core" }
    })
}

pub fn exec_polyglot(
    file_path: &str,
    payload: Option<&str>,
    clm_root: &str,
) -> std::result::Result<(i32, String), String> {
    let payload = payload.filter(|s| !s.is_empty());

    // Register CLM manifest file and its AST cards in the database
    memorize::check_and_register_file_mcard(file_path, clm_root)?;

    // 1. Load the manifest content as JSON
    let json_str = yaml::parse_to_mcards(file_path).map_err(|e| e.to_string())?;
    let parsed: Value = serde_json::from_str(&json_str)
        .map_err(|e| format!("Failed to parse JSON AST: {}", e))?;

    // 2. Locate polyglot cases
    let polyglot_cases = yaml::evaluate_jq(&parsed, ".clm.balanced_exp.polyglot_cases");
    let cases_arr = match polyglot_cases {
        Value::Array(arr) => arr,
        _ => return Err("No polyglot_cases found".to_string()),
    };

    // 3. Find the target cases to run
    let mut target_cases = Vec::new();
    if let Some(p) = payload {
        let mut found = false;
        for c in cases_arr.iter() {
            if let Some(name) = c.get("name").and_then(|v| v.as_str())
                && name == p {
                    target_cases.push(c.clone());
                    found = true;
                    break;
                }
        }
        if !found {
            return Err(format!("Case matching payload '{}' not found", p));
        }
    } else {
        // Run all cases
        for c in cases_arr.iter() {
            target_cases.push(c.clone());
        }
    }

    if target_cases.is_empty() {
        return Err("No cases to execute".to_string());
    }

    let target_runtime = std::env::var("CLM_POLYGLOT_RUNTIME").ok();
    if let Some(target) = &target_runtime {
        eprintln!("[PAL:polyglot] Active runtime choice: {}", target);
    }

    // Resolve active hashes
    let active_a_hash = std::env::var("CLM_ACTIVE_A_HASH").unwrap_or_default();
    let active_c_hash = std::env::var("CLM_ACTIVE_C_HASH").unwrap_or_default();

    let mut last_case_output = String::new();
    let mut consensus_exit_code = 0;

    include!("polyglot_case_loop.rs")
}

#[path = "polyglot_runtime_script.rs"]
mod polyglot_runtime_script;
#[allow(unused_imports)]
pub use polyglot_runtime_script::*;