clm-kernel 0.0.24

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
use std::env;
use std::fs;
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};

pub mod cli;
pub mod dispatch;
pub mod compose;
pub mod verify;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DispatchError {
    SovereignSubprocessForbidden(crate::cross_cutting::context::SovereignSubprocessForbiddenError),
    Other(String),
}

impl std::fmt::Display for DispatchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SovereignSubprocessForbidden(err) => write!(f, "{err}"),
            Self::Other(msg) => f.write_str(msg),
        }
    }
}

impl std::error::Error for DispatchError {}

impl From<String> for DispatchError {
    fn from(msg: String) -> Self {
        Self::Other(msg)
    }
}

impl From<&str> for DispatchError {
    fn from(msg: &str) -> Self {
        Self::Other(msg.to_string())
    }
}

impl From<crate::cross_cutting::context::SovereignSubprocessForbiddenError> for DispatchError {
    fn from(err: crate::cross_cutting::context::SovereignSubprocessForbiddenError) -> Self {
        Self::SovereignSubprocessForbidden(err)
    }
}

pub(crate) use dispatch::{get_internal_registry, try_internal_dispatch};


pub use crate::db::{thread_local_depth_get, thread_local_depth_increment, thread_local_depth_decrement};

fn copy_directory_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
    fs::create_dir_all(dst)?;
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let source = entry.path();
        let target = dst.join(entry.file_name());
        if source.is_dir() {
            copy_directory_recursive(&source, &target)?;
        } else {
            fs::copy(source, target)?;
        }
    }
    Ok(())
}

#[cfg(unix)]
fn create_symlink(src: &Path, dst: &Path) -> std::io::Result<()> {
    std::os::unix::fs::symlink(src, dst)
}

#[cfg(windows)]
fn create_symlink(src: &Path, dst: &Path) -> std::io::Result<()> {
    if src.is_dir() {
        std::os::windows::fs::symlink_dir(src, dst).or_else(|_| {
            Command::new("cmd")
                .args(["/c", "mklink", "/j", &dst.to_string_lossy(), &src.to_string_lossy()])
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status()
                .map(|_| ())
        })
    } else {
        std::os::windows::fs::symlink_file(src, dst)
            .or_else(|_| fs::hard_link(src, dst))
            .or_else(|_| fs::copy(src, dst).map(|_| ()))
    }
}

pub fn resolve_clm_root() -> Result<PathBuf, String> {
    if let Ok(val) = crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_ROOT) {
        let path = crate::fs_utils::normalize_windows_path(Path::new(&val));
        if path.is_absolute() {
            return Ok(path);
        } else if let Ok(curr) = env::current_dir() {
            return Ok(curr.join(path));
        } else {
            return Ok(path);
        }
    }

    let mut dir = env::current_dir().map_err(|e| format!("Failed to get current dir: {}", e))?;
    loop {
        if dir.join("computable_knowledge/Concrete/layer5_system/bootstrap/evaluator").is_dir()
            || dir.join("computable_knowledge/Abstract/layer5_system/bootstrap/evaluator").is_dir() {
            return Ok(dir);
        }
        if dir.join("clm/computable_knowledge/Concrete/layer5_system/bootstrap/evaluator").is_dir()
            || dir.join("clm/computable_knowledge/Abstract/layer5_system/bootstrap/evaluator").is_dir() {
            return Ok(dir.join("clm"));
        }
        if let Some(parent) = dir.parent() {
            dir = parent.to_path_buf();
        } else {
            break;
        }
    }
    Err("Cannot resolve CLM_ROOT. Please set CLM_ROOT environment variable.".to_string())
}

pub fn run_batch_evaluation(
    clm_root: &Path,
    files: &[String],
    quiet: bool,
) -> Result<i32, String> {
    run_batch_evaluation_impl(clm_root, files, quiet, 1)
}

pub fn run_batch_evaluation_parallel(
    clm_root: &Path,
    files: &[String],
    quiet: bool,
    threads: usize,
) -> Result<i32, String> {
    run_batch_evaluation_impl(clm_root, files, quiet, threads)
}

fn run_batch_evaluation_impl(
    clm_root: &Path,
    files: &[String],
    quiet: bool,
    threads: usize,
) -> Result<i32, String> {
    use std::time::Instant;
    use std::thread;

    #[cfg(windows)]
    let _ = threads;
    #[cfg(windows)]
    let threads = 1;

    if let Ok(mut lock) = crate::cpn::vm::EVAL_CALLBACK.write() {
        *lock = Some(run_evaluation);
    }

    // Hoist per-invocation overhead: integrity + cache run once for the entire batch
    verify::verify_integrity(clm_root)?;
    cli::compile_evaluator_cache(clm_root, quiet)?;

    if threads <= 1 {
        // Sequential batch mode
        let mut passed = 0usize;
        let mut failed = 0usize;
        let mut first_failure_code = 1;

        for file in files {
            let start = Instant::now();
            let file_name = file.rsplit('/').next().unwrap_or(file);

            match dispatch::try_internal_dispatch(clm_root, file, None, None, true, crate::layer0::gtime::unix_timestamp_nanos) {
                Ok(exit_code) => {
                    let elapsed = start.elapsed();
                    if exit_code == 0 {
                        if !quiet {
                            eprintln!("[BATCH] ✅ PASS  {}  ({:.0}ms)", file_name, elapsed.as_millis());
                        }
                        passed += 1;
                    } else {
                        if !quiet {
                            eprintln!("[BATCH] ❌ FAIL  {}  rc={}  ({:.0}ms)", file_name, exit_code, elapsed.as_millis());
                        }
                        failed += 1;
                        if first_failure_code == 1 {
                            first_failure_code = exit_code;
                        }
                    }
                }
                Err(_) => {
                    let child_ctx = crate::cross_cutting::context::CLMContext::current()
                        .with_nested_exec(true);
                    let _nested_guard = child_ctx.enter_scope();
                    match run_evaluation(clm_root, Some(file), None, None, true, None) {
                        Ok(exit_code) => {
                            let elapsed = start.elapsed();
                            if exit_code == 0 {
                                if !quiet {
                                    eprintln!("[BATCH] ✅ PASS  {}  ({:.0}ms, fallback)", file_name, elapsed.as_millis());
                                }
                                passed += 1;
                            } else {
                                if !quiet {
                                    eprintln!("[BATCH] ❌ FAIL  {}  rc={}  ({:.0}ms, fallback)", file_name, exit_code, elapsed.as_millis());
                                }
                                failed += 1;
                                if first_failure_code == 1 {
                                    first_failure_code = exit_code;
                                }
                            }
                        }
                        Err(e) => {
                            let elapsed = start.elapsed();
                            if !quiet {
                                eprintln!("[BATCH] ❌ ERROR  {}  {}  ({:.0}ms, fallback)", file_name, e, elapsed.as_millis());
                            }
                            failed += 1;
                        }
                    }
                }
            }
        }

        let total = passed + failed;
        eprintln!("[BATCH] Done: {} passed, {} failed  (of {})", passed, failed, total);

        if failed > 0 {
            Ok(first_failure_code)
        } else {
            Ok(0)
        }
    } else {
        // Process-parallel batch mode: spawn N subprocesses, each running a sequential batch.
        // This gives true isolation (separate env vars, DBs, global state) without thread races.
        let clm_root = clm_root.to_path_buf();
        let self_exe = env::current_exe().unwrap_or_else(|_| std::path::PathBuf::from("clmEval-rs"));
        let pid = std::process::id();
        let chunk_size = files.len().div_ceil(threads);
        let mut handles = Vec::new();

        for (idx, chunk) in files.chunks(chunk_size).enumerate() {
            let chunk = chunk.to_vec();
            let clm_root = clm_root.clone();
            let self_exe = self_exe.clone();

            let handle = thread::spawn(move || {
                let snap_dir = std::env::temp_dir()
                    .join(format!("clm_batch_proc_{}_{}", pid, idx))
                    .to_string_lossy()
                    .replace('\\', "/");
                let _ = fs::remove_dir_all(&snap_dir);
                let _ = fs::create_dir_all(&snap_dir);

                // Build per-process sandbox: symlink everything from clm_root,
                // but copy db/ for DB isolation and create writable data/ subdirs.
                if let Ok(entries) = fs::read_dir(&clm_root) {
                    for entry in entries.flatten() {
                        let name = entry.file_name();
                        let name_str = name.to_string_lossy();
                        if name_str == "db" {
                            let src_db = clm_root.join("db");
                            let dst_db = format!("{}/db", snap_dir);
                            let _ = fs::create_dir_all(&dst_db);
                            if let Ok(db_entries) = fs::read_dir(&src_db) {
                                for db_entry in db_entries.flatten() {
                                    let _ = fs::copy(
                                        db_entry.path(),
                                        format!("{}/{}", dst_db, db_entry.file_name().to_string_lossy()),
                                    );
                                }
                            }
                        } else if name_str == "data" {
                            let src_data = clm_root.join("data");
                            let dst_data = format!("{}/data", snap_dir);
                            let _ = fs::create_dir_all(&dst_data);
                            if let Ok(data_entries) = fs::read_dir(&src_data) {
                                for data_entry in data_entries.flatten() {
                                    let dname = data_entry.file_name();
                                    let dname_str = dname.to_string_lossy();
                                    let is_writable = dname_str == "execution_logs"
                                        || dname_str == "execution_data"
                                        || dname_str == "ipc"
                                        || dname_str == "logs"
                                        || dname_str == crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY
                                        || dname_str == "agent_identity";
                                    if is_writable {
                                        let dst_sub = format!("{}/{}", dst_data, dname_str);
                                        let _ = fs::create_dir_all(&dst_sub);
                                        if dname_str == "execution_data" {
                                            let _ = fs::create_dir_all(format!("{}/logs/.tmp", dst_sub));
                                        } else if dname_str == "ipc" {
                                            let _ = copy_directory_recursive(&data_entry.path(), &PathBuf::from(&dst_sub));
                                        }
                                    } else {
                                        let _ = create_symlink(
                                            &data_entry.path(),
                                            &PathBuf::from(format!("{}/{}", dst_data, dname_str)),
                                        );
                                    }
                                }
                            }
                        } else if name_str == "types" {
                            let src_types = clm_root.join("types");
                            let dst_types = PathBuf::from(&snap_dir).join("types");
                            let _ = copy_directory_recursive(&src_types, &dst_types);
                        } else {
                            let _ = create_symlink(
                                &entry.path(),
                                &PathBuf::from(format!("{}/{}", snap_dir, name_str)),
                            );
                        }
                    }
                }

                let snap_path = PathBuf::from(&snap_dir);
                let mut cmd = Command::new(&self_exe);
                cmd.current_dir(&snap_path);
                cmd.arg("evaluate").arg("--batch");
                for f in &chunk {
                    cmd.arg(f);
                }
                if quiet { cmd.arg("--quiet"); }
                cmd.env(crate::layer0::vocabulary_env::ENV_CLM_CACHE_FRESH, "1");
                cmd.env(crate::layer0::vocabulary_env::ENV_CLM_ROOT, &snap_dir);
                cmd.env(crate::layer0::vocabulary_env::ENV_CLM_NESTED_EXEC, "");
                if let Ok(v) = crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_PETRI_KENOTIC) { cmd.env(crate::layer0::vocabulary_env::ENV_CLM_PETRI_KENOTIC, v); }
                // Capture stderr (BATCH lines) to per-chunk log, inherit stdout
                let log_path = format!("{}/chunk_output.log", snap_dir);
                let log_file = fs::File::create(&log_path).ok();
                if let Some(ref f) = log_file {
                    cmd.stderr(f.try_clone().unwrap_or_else(|_| f.try_clone().unwrap()));
                } else {
                    cmd.stderr(Stdio::inherit());
                }
                cmd.stdout(Stdio::null());

                let start = Instant::now();
                let status = cmd.status();
                let elapsed = start.elapsed();
                let exit_code = status.map(|s| s.code().unwrap_or(1)).unwrap_or(1);

                (idx, exit_code, elapsed.as_millis(), snap_dir)
            });
            handles.push(handle);
        }

        let mut passed = 0usize;
        let mut failed = 0usize;
        let mut first_failure_code = 1;

        for handle in handles {
            match handle.join() {
                Ok((idx, exit_code, elapsed_ms, snap_dir)) => {
                    // Parse per-test results from chunk log
                    let log_path = format!("{}/chunk_output.log", snap_dir);
                    if let Ok(log_content) = fs::read_to_string(&log_path) {
                        for line in log_content.lines() {
                            if line.starts_with("[BATCH]") {
                                if line.starts_with("[BATCH] Done:") {
                                    continue;
                                }
                                if !quiet {
                                    eprintln!("{}", line);
                                }
                                if line.contains("✅ PASS") {
                                    passed += 1;
                                } else if line.contains("❌ FAIL") {
                                    failed += 1;
                                    if first_failure_code == 1 {
                                        first_failure_code = exit_code;
                                    }
                                }
                            }
                        }
                    } else if !quiet {
                        eprintln!("[BATCH] ❌ Chunk {} — no output log", idx);
                    }
                    if !quiet && exit_code != 0 {
                        eprintln!("[BATCH] Chunk {} finished  rc={}  ({:.0}ms)", idx, exit_code, elapsed_ms);
                    }
                    let _ = fs::remove_dir_all(&snap_dir);
                }
                Err(_) => {
                    if !quiet {
                        eprintln!("[BATCH] ❌ Chunk thread panicked");
                    }
                    failed += 1;
                }
            }
        }

        eprintln!("[BATCH] Done: {} passed, {} failed  ({} processes)", passed, failed, threads);

        if failed > 0 {
            Ok(first_failure_code)
        } else {
            Ok(0)
        }
    }
}


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