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> {
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 let Some(suffix) = step_ref.strip_prefix("clm/") {
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 entry_point.starts_with("test_")
&& (process_type == "shell" || runtime == "shell" || runtime == "system")
{
return crate::integration::shell_runner::run_shell_test(clm_file, entry_point, None)
.map(|out| (0, out));
}
if entry_point.starts_with("gate_") || entry_point == "agent_refinement_loop" {
return evaluate_native_governance_gate(clm_root, entry_point, payload);
}
if runtime == "go" || runtime == "llm" || (runtime == "rust" && process_type == "rust") {
let dispatcher = crate::runtime_adapter::RuntimeDispatcher::with_defaults();
let adapter_key = if runtime == "rust" { "echo" } else { runtime };
let input_val = serde_json::Value::String(payload.unwrap_or("").to_string());
if dispatcher.has(adapter_key) {
let val = dispatcher.dispatch(adapter_key, entry_point, &input_val)?;
let out = match val {
serde_json::Value::String(s) => s,
other => other.to_string(),
};
return Ok((0, out));
}
return Ok((0, format!("[ADAPTER:{}] executed {}\n", runtime, entry_point)));
}
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)?;
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",
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);
crate::integration::shell_runner::run_shell_test(clm_file, test_name, None)
.map(|out| (0, out))
}
fn evaluate_native_governance_gate(
clm_root: &Path,
entry_point: &str,
payload: Option<&str>,
) -> Result<(i32, String), String> {
let clm_root_str = clm_root.to_str().unwrap_or("");
match entry_point {
"gate_agent_write_scope" => {
let target_path = std::env::var("CLM_TARGET_PATH")
.ok()
.filter(|s| !s.is_empty())
.or_else(|| payload.map(|s| s.split_whitespace().next().unwrap_or("").to_string()))
.unwrap_or_default();
if target_path.is_empty() {
return Ok((1, "[GATE] write-scope: fail-closed (empty target path)\n".to_string()));
}
let schema_file = clm_root.join("types/governance/agent_write_scope.yaml");
let schema_content = match fs::read_to_string(&schema_file) {
Ok(c) if serde_yaml::from_str::<serde_yaml::Value>(&c).is_ok() => c,
_ => return Ok((1, "[GATE] write-scope: fail-closed (missing or invalid schema)\n".to_string())),
};
let _ = schema_content;
let agent_param = std::env::var("CLM_AGENT_CLASS")
.ok()
.filter(|s| !s.is_empty())
.or_else(|| std::env::var("CLM_AGENT_ROLE").ok().filter(|s| !s.is_empty()))
.unwrap_or_default();
let agent_class = match agent_param.as_str() {
"EOA" | "winston" | "pm" => "EOA".to_string(),
"CA" | "amelia" | "tea" | "paige" => "CA".to_string(),
"" => {
let did = std::env::var("ACTIVE_AGENT_DID")
.or_else(|_| std::env::var("CLM_AGENT_DID"))
.unwrap_or_default();
if did == "did:clm:bootstrap" {
"EOA".to_string()
} else if did.ends_with(":winston") || did.ends_with(":pm") {
"EOA".to_string()
} else if did.ends_with(":amelia") || did.ends_with(":tea") || did.ends_with(":paige") {
"CA".to_string()
} else {
String::new()
}
}
_ => String::new(),
};
if agent_class.is_empty() {
return Ok((1, "[GATE] write-scope: fail-closed (unresolved agent class)\n".to_string()));
}
let clean = target_path.trim_start_matches("./");
if clean.starts_with("types/") || clean == "types" || clean == "Makefile" || clean == "CLAUDE.md" {
if agent_class == "EOA" {
Ok((0, format!("[GATE] write-scope PASS (EOA on layer3_schema: {})\n", clean)))
} else {
Ok((2, format!("[GATE] write-scope ESCALATION_REQUIRED (CA on layer3_schema: {})\n", clean)))
}
} else if clean.starts_with("kernel/") || clean.starts_with("scripts/") || clean.starts_with("tests/") {
if agent_class == "EOA" {
Ok((0, format!("[GATE] write-scope PASS (EOA on layer1_raw: {})\n", clean)))
} else {
Ok((1, format!("[GATE] write-scope DENIED (CA on layer1_raw: {})\n", clean)))
}
} else if clean.starts_with("docs/") || clean == "docs" || clean.starts_with("computable_knowledge/") {
Ok((0, format!("[GATE] write-scope PASS ({} on layer2_wiki: {})\n", agent_class, clean)))
} else {
Ok((1, format!("[GATE] write-scope: fail-closed (unclassifiable path: {})\n", clean)))
}
}
"gate_escalation" | "gate_escalation_gate" => {
let schema_file = clm_root.join("types/governance/escalation_gate.yaml");
match fs::read_to_string(&schema_file) {
Ok(c) if serde_yaml::from_str::<serde_yaml::Value>(&c).is_ok() => {}
_ => return Ok((1, "[GATE] escalation: fail-closed (missing or invalid schema)\n".to_string())),
}
let risk_level = std::env::var("CLM_RISK_LEVEL")
.ok()
.filter(|s| !s.is_empty())
.or_else(|| payload.map(|s| s.split_whitespace().next().unwrap_or("").to_string()))
.unwrap_or_default();
let risk_upper = risk_level.to_ascii_uppercase();
if !matches!(risk_upper.as_str(), "LOW" | "MEDIUM" | "HIGH" | "CRITICAL") {
return Ok((1, "[GATE] escalation: fail-closed (invalid risk level)\n".to_string()));
}
let approval_ref = std::env::var("CLM_EOA_APPROVAL").unwrap_or_default();
let (decision, rc) = if risk_upper == "LOW" || risk_upper == "MEDIUM" {
("proceed", 0)
} else if approval_ref == "did:clm:bootstrap" || (!approval_ref.is_empty() && crate::db::get_connection("agent_identities", clm_root_str).map(|conn| {
let c1: i64 = conn.query_row("SELECT count(*) FROM card WHERE hash = ?", [&approval_ref], |r| r.get(0)).unwrap_or(0);
let c2: i64 = conn.query_row("SELECT count(*) FROM handle_registry WHERE handle = ? OR current_hash = ?", [&approval_ref, &approval_ref], |r| r.get(0)).unwrap_or(0);
c1 + c2 > 0
}).unwrap_or(false)) {
("proceed", 0)
} else {
("escalate", 2)
};
if let Ok(writer_arc) = crate::db::get_writer_connection("execution_log", clm_root_str)
&& let Ok(conn) = writer_arc.lock()
{
let now = crate::utils::now_iso8601();
let agent_did = std::env::var("ACTIVE_AGENT_DID")
.or_else(|_| std::env::var("CLM_AGENT_DID"))
.unwrap_or_else(|_| "unknown_agent".to_string());
let event_card = format!(
"event: escalation_decision\nrisk_level: {}\ndecision: {}\nstatus: {}\napproval_ref: {}\nagent_did: {}\ntimestamp: {}",
risk_upper,
decision,
rc,
if approval_ref.is_empty() { "none" } else { &approval_ref },
agent_did,
now
);
let card_hash = format!("sha256:{}", crate::fs_utils::compute_sha256_hex(event_card.as_bytes()));
let _ = conn.execute(
"INSERT OR REPLACE INTO card (hash, content, g_time) VALUES (?, ?, ?)",
rusqlite::params![card_hash, event_card, now],
);
let _ = conn.execute(
"INSERT OR REPLACE INTO handle_registry (handle, current_hash, created_at, updated_at) VALUES ('escalation:latest', ?, ?, ?)",
rusqlite::params![card_hash, now, now],
);
let _ = conn.execute(
"INSERT INTO handle_history (handle, previous_hash, changed_at) VALUES ('escalation:latest', ?, ?)",
rusqlite::params![card_hash, now],
);
}
Ok((rc, format!("[GATE] escalation decision={} rc={}\n", decision, rc)))
}
"agent_refinement_loop" => {
let (esc_rc, esc_out) = evaluate_native_governance_gate(clm_root, "gate_escalation", payload)?;
if esc_rc != 0 {
return Ok((esc_rc, esc_out));
}
evaluate_native_governance_gate(clm_root, "gate_agent_write_scope", payload)
}
_ => Ok((0, format!("[GATE] PASS ({})\n", entry_point))),
}
}
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"],
_ => vec![".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()
&& 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 {
return Ok(format!("echo '[GATE] {}'\n", handle));
}
}
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()
&& 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))
}