Skip to main content

clm_rust_core/evaluator/
mod.rs

1use std::env;
2use std::fs;
3use std::io::{BufRead, BufReader, Write};
4use std::path::{Path, PathBuf};
5use std::process::{Child, Command, Stdio};
6
7pub mod cli;
8pub mod dispatch;
9pub mod compose;
10pub mod verify;
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum DispatchError {
14    SovereignSubprocessForbidden(crate::cross_cutting::context::SovereignSubprocessForbiddenError),
15    Other(String),
16}
17
18impl std::fmt::Display for DispatchError {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            Self::SovereignSubprocessForbidden(err) => write!(f, "{err}"),
22            Self::Other(msg) => f.write_str(msg),
23        }
24    }
25}
26
27impl std::error::Error for DispatchError {}
28
29impl From<String> for DispatchError {
30    fn from(msg: String) -> Self {
31        Self::Other(msg)
32    }
33}
34
35impl From<&str> for DispatchError {
36    fn from(msg: &str) -> Self {
37        Self::Other(msg.to_string())
38    }
39}
40
41impl From<crate::cross_cutting::context::SovereignSubprocessForbiddenError> for DispatchError {
42    fn from(err: crate::cross_cutting::context::SovereignSubprocessForbiddenError) -> Self {
43        Self::SovereignSubprocessForbidden(err)
44    }
45}
46
47pub(crate) use dispatch::{get_internal_registry, try_internal_dispatch};
48
49
50pub use crate::db::{thread_local_depth_get, thread_local_depth_increment, thread_local_depth_decrement};
51
52fn copy_directory_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
53    fs::create_dir_all(dst)?;
54    for entry in fs::read_dir(src)? {
55        let entry = entry?;
56        let source = entry.path();
57        let target = dst.join(entry.file_name());
58        if source.is_dir() {
59            copy_directory_recursive(&source, &target)?;
60        } else {
61            fs::copy(source, target)?;
62        }
63    }
64    Ok(())
65}
66
67#[cfg(unix)]
68fn create_symlink(src: &Path, dst: &Path) -> std::io::Result<()> {
69    std::os::unix::fs::symlink(src, dst)
70}
71
72#[cfg(windows)]
73fn create_symlink(src: &Path, dst: &Path) -> std::io::Result<()> {
74    if src.is_dir() {
75        std::os::windows::fs::symlink_dir(src, dst).or_else(|_| {
76            Command::new("cmd")
77                .args(["/c", "mklink", "/j", &dst.to_string_lossy(), &src.to_string_lossy()])
78                .stdout(Stdio::null())
79                .stderr(Stdio::null())
80                .status()
81                .map(|_| ())
82        })
83    } else {
84        std::os::windows::fs::symlink_file(src, dst)
85            .or_else(|_| fs::hard_link(src, dst))
86            .or_else(|_| fs::copy(src, dst).map(|_| ()))
87    }
88}
89
90pub fn resolve_clm_root() -> Result<PathBuf, String> {
91    if let Ok(val) = crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_ROOT) {
92        let path = crate::fs_utils::normalize_windows_path(Path::new(&val));
93        if path.is_absolute() {
94            return Ok(path);
95        } else if let Ok(curr) = env::current_dir() {
96            return Ok(curr.join(path));
97        } else {
98            return Ok(path);
99        }
100    }
101
102    let mut dir = env::current_dir().map_err(|e| format!("Failed to get current dir: {}", e))?;
103    loop {
104        if dir.join("computable_knowledge/Concrete/layer5_system/bootstrap/evaluator").is_dir()
105            || dir.join("computable_knowledge/Abstract/layer5_system/bootstrap/evaluator").is_dir() {
106            return Ok(dir);
107        }
108        if dir.join("clm/computable_knowledge/Concrete/layer5_system/bootstrap/evaluator").is_dir()
109            || dir.join("clm/computable_knowledge/Abstract/layer5_system/bootstrap/evaluator").is_dir() {
110            return Ok(dir.join("clm"));
111        }
112        if let Some(parent) = dir.parent() {
113            dir = parent.to_path_buf();
114        } else {
115            break;
116        }
117    }
118    Err("Cannot resolve CLM_ROOT. Please set CLM_ROOT environment variable.".to_string())
119}
120
121pub fn run_batch_evaluation(
122    clm_root: &Path,
123    files: &[String],
124    quiet: bool,
125) -> Result<i32, String> {
126    run_batch_evaluation_impl(clm_root, files, quiet, 1)
127}
128
129pub fn run_batch_evaluation_parallel(
130    clm_root: &Path,
131    files: &[String],
132    quiet: bool,
133    threads: usize,
134) -> Result<i32, String> {
135    run_batch_evaluation_impl(clm_root, files, quiet, threads)
136}
137
138fn run_batch_evaluation_impl(
139    clm_root: &Path,
140    files: &[String],
141    quiet: bool,
142    threads: usize,
143) -> Result<i32, String> {
144    use std::time::Instant;
145    use std::thread;
146
147    #[cfg(windows)]
148    let _ = threads;
149    #[cfg(windows)]
150    let threads = 1;
151
152    if let Ok(mut lock) = crate::cpn::vm::EVAL_CALLBACK.write() {
153        *lock = Some(run_evaluation);
154    }
155
156    // Hoist per-invocation overhead: integrity + cache run once for the entire batch
157    verify::verify_integrity(clm_root)?;
158    cli::compile_evaluator_cache(clm_root, quiet)?;
159
160    if threads <= 1 {
161        // Sequential batch mode
162        let mut passed = 0usize;
163        let mut failed = 0usize;
164        let mut first_failure_code = 1;
165
166        for file in files {
167            let start = Instant::now();
168            let file_name = file.rsplit('/').next().unwrap_or(file);
169
170            match dispatch::try_internal_dispatch(clm_root, file, None, None, true, crate::layer0::gtime::unix_timestamp_nanos) {
171                Ok(exit_code) => {
172                    let elapsed = start.elapsed();
173                    if exit_code == 0 {
174                        if !quiet {
175                            eprintln!("[BATCH] ✅ PASS  {}  ({:.0}ms)", file_name, elapsed.as_millis());
176                        }
177                        passed += 1;
178                    } else {
179                        if !quiet {
180                            eprintln!("[BATCH] ❌ FAIL  {}  rc={}  ({:.0}ms)", file_name, exit_code, elapsed.as_millis());
181                        }
182                        failed += 1;
183                        if first_failure_code == 1 {
184                            first_failure_code = exit_code;
185                        }
186                    }
187                }
188                Err(_) => {
189                    let child_ctx = crate::cross_cutting::context::CLMContext::current()
190                        .with_nested_exec(true);
191                    let _nested_guard = child_ctx.enter_scope();
192                    match run_evaluation(clm_root, Some(file), None, None, true, None) {
193                        Ok(exit_code) => {
194                            let elapsed = start.elapsed();
195                            if exit_code == 0 {
196                                if !quiet {
197                                    eprintln!("[BATCH] ✅ PASS  {}  ({:.0}ms, fallback)", file_name, elapsed.as_millis());
198                                }
199                                passed += 1;
200                            } else {
201                                if !quiet {
202                                    eprintln!("[BATCH] ❌ FAIL  {}  rc={}  ({:.0}ms, fallback)", file_name, exit_code, elapsed.as_millis());
203                                }
204                                failed += 1;
205                                if first_failure_code == 1 {
206                                    first_failure_code = exit_code;
207                                }
208                            }
209                        }
210                        Err(e) => {
211                            let elapsed = start.elapsed();
212                            if !quiet {
213                                eprintln!("[BATCH] ❌ ERROR  {}  {}  ({:.0}ms, fallback)", file_name, e, elapsed.as_millis());
214                            }
215                            failed += 1;
216                        }
217                    }
218                }
219            }
220        }
221
222        let total = passed + failed;
223        eprintln!("[BATCH] Done: {} passed, {} failed  (of {})", passed, failed, total);
224
225        if failed > 0 {
226            Ok(first_failure_code)
227        } else {
228            Ok(0)
229        }
230    } else {
231        // Process-parallel batch mode: spawn N subprocesses, each running a sequential batch.
232        // This gives true isolation (separate env vars, DBs, global state) without thread races.
233        let clm_root = clm_root.to_path_buf();
234        let self_exe = env::current_exe().unwrap_or_else(|_| std::path::PathBuf::from("clmEval-rs"));
235        let pid = std::process::id();
236        let chunk_size = files.len().div_ceil(threads);
237        let mut handles = Vec::new();
238
239        for (idx, chunk) in files.chunks(chunk_size).enumerate() {
240            let chunk = chunk.to_vec();
241            let clm_root = clm_root.clone();
242            let self_exe = self_exe.clone();
243
244            let handle = thread::spawn(move || {
245                let snap_dir = std::env::temp_dir()
246                    .join(format!("clm_batch_proc_{}_{}", pid, idx))
247                    .to_string_lossy()
248                    .replace('\\', "/");
249                let _ = fs::remove_dir_all(&snap_dir);
250                let _ = fs::create_dir_all(&snap_dir);
251
252                // Build per-process sandbox: symlink everything from clm_root,
253                // but copy db/ for DB isolation and create writable data/ subdirs.
254                if let Ok(entries) = fs::read_dir(&clm_root) {
255                    for entry in entries.flatten() {
256                        let name = entry.file_name();
257                        let name_str = name.to_string_lossy();
258                        if name_str == "db" {
259                            let src_db = clm_root.join("db");
260                            let dst_db = format!("{}/db", snap_dir);
261                            let _ = fs::create_dir_all(&dst_db);
262                            if let Ok(db_entries) = fs::read_dir(&src_db) {
263                                for db_entry in db_entries.flatten() {
264                                    let _ = fs::copy(
265                                        db_entry.path(),
266                                        format!("{}/{}", dst_db, db_entry.file_name().to_string_lossy()),
267                                    );
268                                }
269                            }
270                        } else if name_str == "data" {
271                            let src_data = clm_root.join("data");
272                            let dst_data = format!("{}/data", snap_dir);
273                            let _ = fs::create_dir_all(&dst_data);
274                            if let Ok(data_entries) = fs::read_dir(&src_data) {
275                                for data_entry in data_entries.flatten() {
276                                    let dname = data_entry.file_name();
277                                    let dname_str = dname.to_string_lossy();
278                                    let is_writable = dname_str == "execution_logs"
279                                        || dname_str == "execution_data"
280                                        || dname_str == "ipc"
281                                        || dname_str == "logs"
282                                        || dname_str == crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY
283                                        || dname_str == "agent_identity";
284                                    if is_writable {
285                                        let dst_sub = format!("{}/{}", dst_data, dname_str);
286                                        let _ = fs::create_dir_all(&dst_sub);
287                                        if dname_str == "execution_data" {
288                                            let _ = fs::create_dir_all(format!("{}/logs/.tmp", dst_sub));
289                                        } else if dname_str == "ipc" {
290                                            let _ = copy_directory_recursive(&data_entry.path(), &PathBuf::from(&dst_sub));
291                                        }
292                                    } else {
293                                        let _ = create_symlink(
294                                            &data_entry.path(),
295                                            &PathBuf::from(format!("{}/{}", dst_data, dname_str)),
296                                        );
297                                    }
298                                }
299                            }
300                        } else if name_str == "types" {
301                            let src_types = clm_root.join("types");
302                            let dst_types = PathBuf::from(&snap_dir).join("types");
303                            let _ = copy_directory_recursive(&src_types, &dst_types);
304                        } else {
305                            let _ = create_symlink(
306                                &entry.path(),
307                                &PathBuf::from(format!("{}/{}", snap_dir, name_str)),
308                            );
309                        }
310                    }
311                }
312
313                let snap_path = PathBuf::from(&snap_dir);
314                let mut cmd = Command::new(&self_exe);
315                cmd.current_dir(&snap_path);
316                cmd.arg("evaluate").arg("--batch");
317                for f in &chunk {
318                    cmd.arg(f);
319                }
320                if quiet { cmd.arg("--quiet"); }
321                cmd.env(crate::layer0::vocabulary_env::ENV_CLM_CACHE_FRESH, "1");
322                cmd.env(crate::layer0::vocabulary_env::ENV_CLM_ROOT, &snap_dir);
323                cmd.env(crate::layer0::vocabulary_env::ENV_CLM_NESTED_EXEC, "");
324                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); }
325                // Capture stderr (BATCH lines) to per-chunk log, inherit stdout
326                let log_path = format!("{}/chunk_output.log", snap_dir);
327                let log_file = fs::File::create(&log_path).ok();
328                if let Some(ref f) = log_file {
329                    cmd.stderr(f.try_clone().unwrap_or_else(|_| f.try_clone().unwrap()));
330                } else {
331                    cmd.stderr(Stdio::inherit());
332                }
333                cmd.stdout(Stdio::null());
334
335                let start = Instant::now();
336                let status = cmd.status();
337                let elapsed = start.elapsed();
338                let exit_code = status.map(|s| s.code().unwrap_or(1)).unwrap_or(1);
339
340                (idx, exit_code, elapsed.as_millis(), snap_dir)
341            });
342            handles.push(handle);
343        }
344
345        let mut passed = 0usize;
346        let mut failed = 0usize;
347        let mut first_failure_code = 1;
348
349        for handle in handles {
350            match handle.join() {
351                Ok((idx, exit_code, elapsed_ms, snap_dir)) => {
352                    // Parse per-test results from chunk log
353                    let log_path = format!("{}/chunk_output.log", snap_dir);
354                    if let Ok(log_content) = fs::read_to_string(&log_path) {
355                        for line in log_content.lines() {
356                            if line.starts_with("[BATCH]") {
357                                if line.starts_with("[BATCH] Done:") {
358                                    continue;
359                                }
360                                if !quiet {
361                                    eprintln!("{}", line);
362                                }
363                                if line.contains("✅ PASS") {
364                                    passed += 1;
365                                } else if line.contains("❌ FAIL") {
366                                    failed += 1;
367                                    if first_failure_code == 1 {
368                                        first_failure_code = exit_code;
369                                    }
370                                }
371                            }
372                        }
373                    } else if !quiet {
374                        eprintln!("[BATCH] ❌ Chunk {} — no output log", idx);
375                    }
376                    if !quiet && exit_code != 0 {
377                        eprintln!("[BATCH] Chunk {} finished  rc={}  ({:.0}ms)", idx, exit_code, elapsed_ms);
378                    }
379                    let _ = fs::remove_dir_all(&snap_dir);
380                }
381                Err(_) => {
382                    if !quiet {
383                        eprintln!("[BATCH] ❌ Chunk thread panicked");
384                    }
385                    failed += 1;
386                }
387            }
388        }
389
390        eprintln!("[BATCH] Done: {} passed, {} failed  ({} processes)", passed, failed, threads);
391
392        if failed > 0 {
393            Ok(first_failure_code)
394        } else {
395            Ok(0)
396        }
397    }
398}
399
400
401#[path = "mod_runner.rs"]
402mod mod_runner;
403#[allow(unused_imports)]
404pub use mod_runner::*;