use std::fs;
use std::path::{Path, PathBuf};
use crate::evaluator::run_evaluation;
pub(crate) fn evaluate_non_rust_runtime(
clm_root: &Path,
clm_file: &str,
process_type: &str,
runtime: &str,
concrete_impl: &serde_yaml::Value,
payload: Option<&str>,
post_rc: Option<i32>,
quiet: bool,
) -> Result<(i32, String), String> {
eprintln!("[DEBUG] evaluate_non_rust_runtime for clm_file: {}", clm_file);
let clm_root_str = clm_root.to_str().ok_or("Invalid CLM_ROOT path")?;
let process = concrete_impl.get("process")
.ok_or_else(|| "No process block in concrete_impl".to_string())?;
if process_type == "compose" || runtime == "compose" {
if !quiet {
eprintln!("[PAL] Dispatching: type=compose");
}
let steps = process.get("steps")
.and_then(|v| v.as_sequence())
.ok_or_else(|| "No steps in compose process block".to_string())?;
let mut exit_code = 0;
for (i, step_ref_val) in steps.iter().enumerate() {
if let Some(step_ref) = step_ref_val.as_str() {
let step_path = if Path::new(step_ref).is_absolute() {
PathBuf::from(step_ref)
} else {
let joined = clm_root.join(step_ref);
if joined.exists() {
joined
} else if step_ref.starts_with("clm/") {
let suffix = &step_ref[4..];
clm_root.join(suffix)
} else {
joined
}
};
if !step_path.exists() {
return Err(format!("[COMPOSE_FN] Step {}: failed to load '{}'", i + 1, step_path.to_string_lossy()));
}
if !quiet {
eprintln!("[COMPOSE_FN] Step {}: (from {})", i + 1, step_ref);
}
let rc = run_evaluation(
clm_root,
Some(step_path.to_str().unwrap()),
payload,
None,
quiet,
None,
)?;
if rc != 0 {
exit_code = rc;
break;
}
}
}
return Ok((exit_code, "".to_string()));
}
if runtime == "polyglot" || process_type == "polyglot" {
return match crate::polyglot::exec_polyglot(clm_file, payload, clm_root_str) {
Ok((code, output)) => Ok((code, output)),
Err(e) => Err(e),
};
}
let entry_point = process.get("entry_point")
.and_then(|v| v.as_str())
.or_else(|| concrete_impl.get("operation").and_then(|v| v.as_str()))
.unwrap_or("");
let project_root_str = std::env::var("PROJECT_ROOT").unwrap_or_else(|_| {
clm_root.parent()
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|| clm_root_str.to_string())
});
if runtime == "system" && process_type != "python" && process_type != "node" && process_type != "javascript" && process_type != "shell" {
let op_clm_path = clm_root.join("computable_knowledge/Abstract/layer5_system/bootstrap/governance").join(format!("{}.yaml", entry_point));
let op_clm_path = if op_clm_path.exists() {
op_clm_path
} else {
PathBuf::from(&project_root_str).join("clm/computable_knowledge/Abstract/layer5_system/bootstrap/governance").join(format!("{}.yaml", entry_point))
};
if !op_clm_path.exists() {
return Err(format!("[EXEC] System operation CLM not found: {:?}", op_clm_path));
}
crate::utils::set_env_var("TARGET_FILE", clm_file);
if !quiet {
eprintln!("[clmEval-rs] Dispatching to system governance operation: {} (via CLM evaluate)", entry_point);
}
let rc = run_evaluation(
clm_root,
Some(op_clm_path.to_str().ok_or("Invalid path")?),
payload,
None,
quiet,
None,
)?;
return Ok((rc, "".to_string()));
}
let identity_db = crate::db::get_db_path("agent_identities", clm_root_str).to_string_lossy().to_string();
let content_db = crate::db::get_db_path("content_memory", clm_root_str).to_string_lossy().to_string();
let execution_log_db = crate::db::get_db_path("execution_log", clm_root_str).to_string_lossy().to_string();
let mlp_json = serde_json::json!({
"$schema": "https://mcard.org/schemas/mlp-context-v1.json",
"context": {
"operation": entry_point,
"clm_file": clm_file,
"a_hash": std::env::var("CLM_ACTIVE_A_HASH").unwrap_or_default(),
"c_hash": std::env::var("CLM_ACTIVE_C_HASH").unwrap_or_default(),
"payload": payload.unwrap_or("")
},
"environment": {
"CLM_ROOT": clm_root_str,
"PROJECT_ROOT": project_root_str,
"g_time": crate::fs_utils::compute_g_time()
},
"databases": {
"identity_db": identity_db,
"content_db": content_db,
"execution_log_db": execution_log_db
}
});
let mlp_json_str = serde_json::to_string(&mlp_json).unwrap_or_default();
let mut env_vars = std::collections::HashMap::new();
if let Some(pay) = payload {
env_vars.insert("CLM_PAYLOAD".to_string(), pay.to_string());
env_vars.insert("TARGET_FILE".to_string(), pay.to_string());
}
env_vars.insert("CLM_ROOT".to_string(), clm_root_str.to_string());
env_vars.insert("PROJECT_ROOT".to_string(), project_root_str);
env_vars.insert("CLM_MLP_CONTEXT".to_string(), mlp_json_str);
if let Ok(val) = std::env::var("CLM_ACTIVE_A_HASH") {
env_vars.insert("CLM_ACTIVE_A_HASH".to_string(), val);
}
if let Ok(val) = std::env::var("CLM_ACTIVE_C_HASH") {
env_vars.insert("CLM_ACTIVE_C_HASH".to_string(), val);
}
if let Ok(val) = std::env::var("CLM_ACTIVE_B_HASH") {
env_vars.insert("CLM_ACTIVE_B_HASH".to_string(), val);
}
for (k, v) in std::env::vars() {
if k.starts_with("CLM_MOCK_") {
env_vars.insert(k, v);
}
}
let governance_vars = [
"CLM_TARGET_PATH", "CLM_AGENT_CLASS", "CLM_AGENT_ROLE",
"CLM_RISK_LEVEL", "CLM_EOA_APPROVAL", "CLM_AGENT_DID",
"CLM_ACTIVE_FILE", "CLM_LAST_MCARD_HASH", "CLM_LAST_MCARD_FILE",
];
for var_name in &governance_vars {
if let Ok(val) = std::env::var(var_name) {
env_vars.insert(var_name.to_string(), val);
}
}
if runtime == "go" || runtime == "llm" || (runtime == "rust" && process_type == "rust") {
let adapter_name = if runtime == "rust" { "rust" } else { runtime };
let script = format!(
"clm_root() {{ echo \"{}\"; }}\n\
export -f clm_root\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/layer1_cpn_vm/runtime_adapters.sh\"\n\
_adapter_{} \"{}\" \"{}\" \"{}\"\n",
clm_root_str,
clm_root_str,
adapter_name,
clm_file,
entry_point,
payload.unwrap_or("")
);
let old_no_sandbox = std::env::var("CLM_NO_SANDBOX");
if runtime == "llm" {
crate::utils::set_env_var("CLM_NO_SANDBOX", "true");
}
let cmd = crate::polyglot::create_sandboxed_command(&crate::fs_utils::get_bash_command(), &["-c".to_string(), script], &env_vars, clm_root_str);
if runtime == "llm" {
match old_no_sandbox {
Ok(val) => crate::utils::set_env_var("CLM_NO_SANDBOX", &val),
Err(_) => crate::utils::remove_env_var("CLM_NO_SANDBOX"),
}
}
return run_command_and_capture(cmd);
}
if runtime == "open_interpreter" {
let py_cmd = crate::polyglot::resolve_python_cmd(clm_root_str);
let mut cmd_args = vec![
"-c".to_string(),
"import sys; from clm.layer3.open_interpreter_adapter import main_cli; sys.exit(main_cli())".to_string(),
clm_file.to_string(),
entry_point.to_string(),
];
if let Some(p) = payload {
cmd_args.push(p.to_string());
}
let mut custom_env_vars = env_vars.clone();
custom_env_vars.insert("PYTHONPATH".to_string(), format!("{}/kernel/scripts", clm_root_str));
let old_no_sandbox = std::env::var("CLM_NO_SANDBOX");
crate::utils::set_env_var("CLM_NO_SANDBOX", "true");
let cmd = crate::polyglot::create_sandboxed_command(&py_cmd, &cmd_args, &custom_env_vars, clm_root_str);
match old_no_sandbox {
Ok(val) => crate::utils::set_env_var("CLM_NO_SANDBOX", &val),
Err(_) => crate::utils::remove_env_var("CLM_NO_SANDBOX"),
}
return run_command_and_capture(cmd);
}
let runner_type = if process_type == "python" || process_type == "node" || process_type == "javascript" || process_type == "shell" {
process_type
} else {
runtime
};
let script_content = resolve_script_body(clm_root, process, runner_type)?;
eprintln!("[DEBUG] script body resolved: {} bytes, dispatching to {} runtime", script_content.len(), runner_type);
let mut args: Vec<String> = match payload {
Some(p) => p.split_whitespace().map(|s| s.to_string()).collect(),
None => Vec::new(),
};
if let Some(rc) = post_rc {
args.push(rc.to_string());
}
match runner_type {
"python" => {
let py_cmd = crate::polyglot::resolve_python_cmd(clm_root_str);
let mut cmd_args = vec!["-c".to_string(), script_content];
cmd_args.extend(args);
let mut py_env_vars = env_vars.clone();
py_env_vars.insert("PYTHONPATH".to_string(), format!("{}/kernel/scripts", clm_root_str));
let cmd = crate::polyglot::create_sandboxed_command(&py_cmd, &cmd_args, &py_env_vars, clm_root_str);
run_command_and_capture(cmd)
}
"node" | "javascript" => {
let rand_id = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let pid = std::process::id();
let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
let _ = std::fs::create_dir_all(&temp_dir);
let temp_file = temp_dir.join(format!("clm_node_inline_{}_{}.cjs", rand_id, pid));
std::fs::write(&temp_file, &script_content).map_err(|e| format!("Failed to write node inline script: {}", e))?;
let mut cmd_args = vec!["--trace-uncaught".to_string(), temp_file.to_string_lossy().to_string()];
cmd_args.extend(args);
let cmd = crate::polyglot::create_sandboxed_command("node", &cmd_args, &env_vars, clm_root_str);
let res = run_command_and_capture(cmd);
let _ = std::fs::remove_file(&temp_file);
res
}
"shell" => {
let header = format!(
"clm_root() {{ echo \"{}\"; }}\n\
export -f clm_root\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/evaluator/shell_utils.sh\" 2>/dev/null || true\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/layer0_core/mcard_storage.sh\" 2>/dev/null || true\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/layer0_core/mcard_verification.sh\" 2>/dev/null || true\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/layer0_core/clm_algebra.sh\" 2>/dev/null || true\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/layer0_core/clm_bootstrap.sh\" 2>/dev/null || true\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/layer0_core/type_registry.sh\" 2>/dev/null || true\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/governance/grammar_ops.sh\" 2>/dev/null || true\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/governance/define_grammar.sh\" 2>/dev/null || true\n\
source \"{}/computable_knowledge/Concrete/layer5_system/bootstrap/governance/validate_grammar.sh\" 2>/dev/null || true\n\
source \"{}/data/ipc/evaluator_cache.sh\" 2>/dev/null || true\n\
clm_preload 2>/dev/null || true\n",
clm_root_str, clm_root_str, clm_root_str, clm_root_str, clm_root_str, clm_root_str, clm_root_str, clm_root_str, clm_root_str, clm_root_str, clm_root_str
);
let wrapped_body = if !entry_point.is_empty() {
format!("_clm_run_block() {{\n{}\n}}\n_clm_run_block \"$@\"\nif type -t {1} &>/dev/null; then {1} \"$@\"; fi\n", script_content, entry_point)
} else {
format!("_clm_run_block() {{\n{}\n}}\n_clm_run_block \"$@\"\n", script_content)
};
let full_script = header + &wrapped_body;
let mut cmd_args = vec!["-s".to_string(), "--".to_string()];
cmd_args.extend(args);
let mut cmd = crate::polyglot::create_sandboxed_command(&crate::fs_utils::get_bash_command(), &cmd_args, &env_vars, clm_root_str);
cmd.stdin(std::process::Stdio::piped());
cmd.stdout(std::process::Stdio::piped());
cmd.stderr(std::process::Stdio::piped());
let mut child = cmd.spawn().map_err(|e| format!("Failed to spawn bash: {}", e))?;
if let Some(mut stdin) = child.stdin.take() {
use std::io::Write;
stdin.write_all(full_script.as_bytes()).map_err(|e| format!("Failed to write to bash stdin: {}", e))?;
}
let output = child.wait_with_output().map_err(|e| format!("Failed to wait for bash: {}", e))?;
if !output.stderr.is_empty() {
use std::io::Write;
std::io::stderr().write_all(&output.stderr).ok();
}
let exit_code = output.status.code().unwrap_or(1);
let stdout_str = String::from_utf8_lossy(&output.stdout).to_string();
Ok((exit_code, stdout_str))
}
other => Err(format!("Unsupported non-rust runtime: {}", other)),
}
}
pub(crate) fn run_fallback_internal_reference(
clm_root: &str,
_clm_file: &str,
module_path: &str,
_quiet: bool,
) -> Result<(i32, String), String> {
let test_name = module_path.split("::").last().unwrap_or(module_path);
let overrides = [
("test_petri_net_suite", "petri_net_cases.sh"),
("test_petri_binding_suite", "petri_binding_cases.sh"),
("test_petri_invariants", "petri_invariant_cases.sh"),
("test_monadic_pcard_resolve_handle", "test_monadic_pcards.sh"),
("test_monadic_pcard_check_precondition", "test_monadic_pcards.sh"),
("test_monadic_pcard_execute_payload", "test_monadic_pcards.sh"),
("test_monadic_pcard_validate_postcondition", "test_monadic_pcards.sh"),
];
let mut target_filename = format!("{}.sh", test_name);
for &(k, v) in &overrides {
if test_name == k {
target_filename = v.to_string();
break;
}
}
let clm_root_path = Path::new(clm_root);
let target_path = find_test_script(&clm_root_path.join("tests"), &target_filename)
.ok_or_else(|| format!("Test script not found for filename: {}", target_filename))?;
let test_func = match test_name {
"test_petri_math" => "test_petri_math_suite",
"test_petri_invariants" => "test_petri_invariant_suite",
other => other,
};
let script_content = format!(
"export CLM_ROOT=\"{clm_root}\"\n\
export PATH=\"$CLM_ROOT:$CLM_ROOT/kernel/scripts:$PATH\"\n\
clm_root() {{ echo \"$CLM_ROOT\"; }}\n\
export -f clm_root\n\
exit() {{ return \"${{1:-0}}\"; }}\n\
while IFS= read -r f; do\n\
if [[ \"$f\" != *\"clmEval.sh\"* && \"$f\" != *\"main.sh\"* && \"$f\" != *\"evaluator.sh\"* ]]; then\n\
source \"$f\" 2>/dev/null || true\n\
fi\n\
done < <(find \"$CLM_ROOT/computable_knowledge/Concrete/layer5_system/bootstrap\" -name \"node_modules\" -prune -o -name \"*.sh\" -print)\n\
unset -f exit\n\
source \"{script_path}\" && {test_func}\n",
clm_root = clm_root,
script_path = target_path.to_string_lossy(),
test_func = test_func
);
let output = {
let temp_dir = std::env::temp_dir().join("clm_execution_logs/.tmp");
let _ = std::fs::create_dir_all(&temp_dir);
let temp_file = temp_dir.join(format!("clm_fallback_{}_{}.sh", std::process::id(), std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)));
std::fs::write(&temp_file, &script_content)
.map_err(|e| format!("Failed to write temp fallback script: {}", e))?;
#[cfg(windows)]
{
let temp_path_str = temp_file.to_string_lossy().replace('\\', "/");
let temp_path_unix = if temp_path_str.len() >= 2 && temp_path_str.as_bytes()[1] == b':' {
let drive = (temp_path_str.as_bytes()[0] as char).to_ascii_lowercase();
format!("/{}{}", drive, &temp_path_str[2..])
} else {
temp_path_str
};
let mut cmd = std::process::Command::new(crate::fs_utils::get_bash_command());
cmd.current_dir(clm_root);
cmd.arg(&temp_path_unix);
for (k, v) in std::env::vars() {
cmd.env(k, v);
}
let result = cmd.output().map_err(|e| format!("Failed to execute fallback bash command: {}", e));
let _ = std::fs::remove_file(&temp_file);
result?
}
#[cfg(not(windows))]
{
let mut cmd = std::process::Command::new(crate::fs_utils::get_bash_command());
cmd.current_dir(clm_root);
cmd.arg(&temp_file);
for (k, v) in std::env::vars() {
cmd.env(k, v);
}
let result = cmd.output().map_err(|e| format!("Failed to execute fallback bash command: {}", e));
let _ = std::fs::remove_file(&temp_file);
result?
}
};
if !output.stderr.is_empty() {
use std::io::Write;
std::io::stderr().write_all(&output.stderr).ok();
}
let exit_code = output.status.code().unwrap_or(1);
let stdout_str = String::from_utf8_lossy(&output.stdout).to_string();
Ok((exit_code, stdout_str))
}
fn find_test_script(dir: &Path, target: &str) -> Option<PathBuf> {
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() {
if path.file_name().and_then(|s| s.to_str()) == Some(target) {
return Some(path);
}
} else if path.is_dir() {
if let Some(p) = find_test_script(&path, target) {
return Some(p);
}
}
}
}
None
}
fn resolve_script_body(clm_root: &Path, process: &serde_yaml::Value, runner_type: &str) -> Result<String, String> {
eprintln!("[DEBUG] resolve_script_body called with runner_type: {}", runner_type);
eprintln!("[DEBUG] process object: {:?}", process);
if let Some(script) = process.get("script").and_then(|v| v.as_str()) {
eprintln!("[DEBUG] resolve_script_body: found 'script' field, length: {}", script.len());
if !script.is_empty() {
return Ok(script.to_string());
}
}
if let Some(handle) = process.get("script_handle").and_then(|v| v.as_str()) {
eprintln!("[DEBUG] resolve_script_body: trying to resolve handle: {:?}", handle);
if handle.starts_with("script:") {
let handle_name = handle.trim_start_matches("script:");
match crate::db::resolve_handle_content("content_memory", clm_root.to_str().unwrap_or(""), handle) {
Ok(Some(content)) if !content.is_empty() => {
eprintln!("[DEBUG] resolve_script_body: resolved '{}' from content_memory DB ({} bytes)", handle, content.len());
return Ok(content);
}
Ok(_) => eprintln!("[DEBUG] resolve_script_body: '{}' not in DB, falling back to disk search", handle),
Err(e) => eprintln!("[DEBUG] resolve_script_body: DB resolve error for '{}': {}, falling back to disk search", handle, e),
}
let extensions = match runner_type {
"python" => vec![".py"],
"node" | "javascript" => vec![".js", ".cjs"],
"shell" => vec![".sh"],
_ => vec![".sh", ".py", ".js", ".cjs"],
};
eprintln!("[DEBUG] resolve_script_handle: searching for '{}' with extensions {:?}", handle_name, extensions);
let search_dirs = ["scripts", "tests", "kernel/scripts", "computable_knowledge"];
for dir_name in &search_dirs {
let dir_path = clm_root.join(dir_name);
if dir_path.exists() {
if let Some(path) = find_script_file(&dir_path, handle_name, &extensions) {
eprintln!("[DEBUG] found path: {:?}, trying to read...", path);
match fs::read_to_string(&path) {
Ok(content) => return Ok(content),
Err(e) => eprintln!("[DEBUG] failed to read {:?}: {}", path, e),
}
}
}
}
} else {
let gate_lib = clm_root.join("computable_knowledge/Concrete/layer5_system/bootstrap/governance/gate_library.sh");
if gate_lib.exists() {
if let Ok(content) = fs::read_to_string(&gate_lib) {
return Ok(content);
}
}
}
}
Err("Empty script body and failed to resolve script_handle".to_string())
}
fn find_script_file(dir: &Path, script_name: &str, extensions: &[&str]) -> Option<PathBuf> {
let basename = script_name.rsplit('/').next().unwrap_or(script_name);
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() {
if let Some(file_name) = path.file_name().and_then(|s| s.to_str()) {
if file_name.contains(basename) {
eprintln!("[DEBUG] file_name: '{}', script_name: '{}', basename: '{}'", file_name, script_name, basename);
}
for ext in extensions {
if file_name.contains(basename) {
eprintln!("[DEBUG] comparing '{}' with '{}' and '{}'", file_name, format!("{}{}", script_name, ext), format!("{}{}", basename, ext));
}
if file_name == format!("{}{}", script_name, ext)
|| file_name == format!("{}{}", basename, ext)
{
return Some(path);
}
}
}
} else if path.is_dir() {
if let Some(p) = find_script_file(&path, script_name, extensions) {
return Some(p);
}
}
}
}
None
}
fn run_command_and_capture(mut cmd: std::process::Command) -> Result<(i32, String), String> {
eprintln!("[DEBUG] run_command_and_capture: spawning {:?} ({} args), waiting for exit...", cmd.get_program(), cmd.get_args().count());
let output = cmd.output().map_err(|e| format!("Failed to execute process: {}", e))?;
eprintln!("[DEBUG] run_command_and_capture: {:?} exited with {:?}", cmd.get_program(), output.status.code());
if !output.stderr.is_empty() {
use std::io::Write;
std::io::stderr().write_all(&output.stderr).ok();
}
let exit_code = output.status.code().unwrap_or(1);
let stdout_str = String::from_utf8_lossy(&output.stdout).to_string();
Ok((exit_code, stdout_str))
}