use serde_json::Value;
use std::path::Path;
#[derive(Default, Debug)]
struct RelationOptions {
normalize_newlines: bool,
trim_whitespace: bool,
ignore_ansi: bool,
ignore_case: bool,
stop_on_first_mismatch: bool,
required_degree: Option<f64>,
}
fn strip_ansi(s: &str) -> String {
let re = regex::Regex::new(r"\x1B\[[0-9;]*m").unwrap_or_else(|_| regex::Regex::new(r"").unwrap());
re.replace_all(s, "").into_owned()
}
fn normalize_output(s: &str, opts: &RelationOptions) -> String {
let mut out = s.to_string();
if opts.normalize_newlines {
out = out.replace("\r\n", "\n").replace('\r', "\n");
}
if opts.trim_whitespace {
out = out.trim().to_string();
}
if opts.ignore_ansi {
out = strip_ansi(&out);
}
if opts.ignore_case {
out = out.to_lowercase();
}
out
}
fn find_divergence(a: &str, b: &str) -> Option<(usize, char, char)> {
let a_chars: Vec<char> = a.chars().collect();
let b_chars: Vec<char> = b.chars().collect();
for i in 0..a_chars.len().min(b_chars.len()) {
if a_chars[i] != b_chars[i] {
return Some((i, a_chars[i], b_chars[i]));
}
}
if a_chars.len() != b_chars.len() {
let idx = a_chars.len().max(b_chars.len()) - 1;
let ac = a_chars.get(idx).copied().unwrap_or('\0');
let bc = b_chars.get(idx).copied().unwrap_or('\0');
return Some((idx, ac, bc));
}
None
}
pub fn parse_expression(expr: &str) -> Result<(String, String, Vec<String>), String> {
let expr = expr.trim();
let colon_idx = expr.find(':')
.ok_or_else(|| format!("Invalid expression format (missing prefix): {}", expr))?;
let prefix = expr[0..colon_idx].trim().to_string();
let rest = &expr[colon_idx + 1..].trim();
if let Some(paren_start) = rest.find('(') {
let paren_end = rest.rfind(')')
.ok_or_else(|| format!("Missing closing parenthesis in expression: {}", expr))?;
let func_name = rest[0..paren_start].trim().to_string();
let args_str = rest[paren_start + 1..paren_end].trim();
let mut args = Vec::new();
if !args_str.is_empty() {
for arg in args_str.split(',') {
args.push(arg.trim().to_string());
}
}
Ok((prefix, func_name, args))
} else {
let func_name = rest.to_string();
Ok((prefix, func_name, Vec::new()))
}
}
pub fn evaluate_invariant(expr: &str, clm_root: &str, context: &Value) -> Result<bool, String> {
let expr = expr.trim();
if expr.is_empty() {
return Ok(true);
}
let is_prefix = expr.contains(':')
&& !expr.contains('>')
&& !expr.contains('<')
&& !expr.contains("==")
&& !expr.contains("!=")
&& !expr.contains('%');
if is_prefix {
let (prefix, func_name, args) = parse_expression(expr)?;
let mut resolved_args = Vec::new();
for arg in &args {
if arg.starts_with('"') && arg.ends_with('"') {
resolved_args.push(arg[1..arg.len() - 1].to_string());
} else if arg.starts_with("inputs.") || arg.starts_with("outputs.") {
let val = resolve_value(arg, context)
.ok_or_else(|| format!("Could not resolve path reference: {}", arg))?;
resolved_args.push(val.as_str().map(|s| s.to_string()).unwrap_or_else(|| val.to_string()));
} else {
if let Some(val) = resolve_value(arg, context) {
resolved_args.push(val.as_str().map(|s| s.to_string()).unwrap_or_else(|| val.to_string()));
} else {
resolved_args.push(arg.clone());
}
}
}
match (prefix.as_str(), func_name.as_str()) {
("gate", "file_exists") => {
if resolved_args.is_empty() {
return Err("gate:file_exists requires at least 1 argument".to_string());
}
let path_str = &resolved_args[0];
let path = Path::new(path_str);
let resolved_path = if path.is_absolute() {
path.to_path_buf()
} else {
Path::new(clm_root).join(path_str)
};
Ok(resolved_path.exists())
}
("gate", "is_sha256") => {
if resolved_args.is_empty() {
return Err("gate:is_sha256 requires at least 1 argument".to_string());
}
let hash_str = &resolved_args[0];
if !hash_str.starts_with(crate::hash::DEFAULT_HASH_PREFIX) || hash_str.len() != 71 {
return Ok(false);
}
let hex_part = crate::hash::strip_prefix(hash_str);
Ok(hex_part.chars().all(|c| c.is_ascii_hexdigit()))
}
("gate", "payload_nonempty") => {
let payload = if resolved_args.is_empty() {
std::env::var("CLM_PAYLOAD").unwrap_or_default()
} else {
resolved_args[0].clone()
};
Ok(!payload.trim().is_empty())
}
("gate", "result_sealed") => {
let hash = if resolved_args.is_empty() {
if let Some(v) = resolve_value("outputs.a_hash", context) {
v.as_str().unwrap_or("").to_string()
} else if let Some(v) = resolve_value("outputs.c_hash", context) {
v.as_str().unwrap_or("").to_string()
} else {
std::env::var("CLM_ACTIVE_C_HASH").unwrap_or_default()
}
} else {
resolved_args[0].clone()
};
if hash.is_empty() {
return Ok(false);
}
let exists = crate::db::card_exists("content_memory", clm_root, &hash)
.map_err(|e| format!("Database check failed: {}", e))?;
Ok(exists)
}
("conservation", "noetherian_check") => {
for hash in &resolved_args {
let exists = crate::db::card_exists("content_memory", clm_root, hash)
.map_err(|e| format!("Database check failed: {}", e))?;
if !exists {
return Ok(false);
}
}
Ok(true)
}
_ => Err(format!("Unknown invariant function: {}:{}", prefix, func_name)),
}
} else {
evaluate_infix_expression(expr, context)
}
}
fn evaluate_infix_expression(expr: &str, context: &Value) -> Result<bool, String> {
let expr = expr.trim();
let operators = vec!["==", "!=", ">=", "<=", ">", "<"];
let mut found_op = None;
for op in &operators {
if let Some(idx) = expr.find(op) {
found_op = Some((*op, idx));
break;
}
}
if let Some((op, idx)) = found_op {
let lhs_str = expr[0..idx].trim();
let rhs_str = expr[idx + op.len()..].trim();
let lhs_val = evaluate_arithmetic(lhs_str, context)?;
let rhs_val = evaluate_arithmetic(rhs_str, context)?;
match op {
"==" => Ok(lhs_val == rhs_val),
"!=" => Ok(lhs_val != rhs_val),
">" => {
let l = lhs_val.as_f64().ok_or_else(|| format!("LHS is not a number: {}", lhs_str))?;
let r = rhs_val.as_f64().ok_or_else(|| format!("RHS is not a number: {}", rhs_str))?;
Ok(l > r)
}
">=" => {
let l = lhs_val.as_f64().ok_or_else(|| format!("LHS is not a number: {}", lhs_str))?;
let r = rhs_val.as_f64().ok_or_else(|| format!("RHS is not a number: {}", rhs_str))?;
Ok(l >= r)
}
"<" => {
let l = lhs_val.as_f64().ok_or_else(|| format!("LHS is not a number: {}", lhs_str))?;
let r = rhs_val.as_f64().ok_or_else(|| format!("RHS is not a number: {}", rhs_str))?;
Ok(l < r)
}
"<=" => {
let l = lhs_val.as_f64().ok_or_else(|| format!("LHS is not a number: {}", lhs_str))?;
let r = rhs_val.as_f64().ok_or_else(|| format!("RHS is not a number: {}", rhs_str))?;
Ok(l <= r)
}
_ => unreachable!(),
}
} else {
let val = evaluate_arithmetic(expr, context)?;
if let Some(b) = val.as_bool() {
Ok(b)
} else if let Some(n) = val.as_i64() {
Ok(n != 0)
} else if let Some(s) = val.as_str() {
Ok(!s.is_empty())
} else {
Ok(!val.is_null())
}
}
}
fn evaluate_arithmetic(expr: &str, context: &Value) -> Result<Value, String> {
let expr = expr.trim();
if let Some(idx) = expr.find('%') {
let lhs_str = expr[0..idx].trim();
let rhs_str = expr[idx + 1..].trim();
let lhs_val = evaluate_arithmetic(lhs_str, context)?;
let rhs_val = evaluate_arithmetic(rhs_str, context)?;
let l = lhs_val.as_i64().ok_or_else(|| format!("LHS of % is not an integer: {}", lhs_str))?;
let r = rhs_val.as_i64().ok_or_else(|| format!("RHS of % is not an integer: {}", rhs_str))?;
if r == 0 {
return Err("Modulo by zero".to_string());
}
return Ok(Value::from(l % r));
}
if expr.starts_with('"') && expr.ends_with('"') {
return Ok(Value::String(expr[1..expr.len() - 1].to_string()));
}
if let Ok(n) = expr.parse::<i64>() {
return Ok(Value::from(n));
}
if let Ok(f) = expr.parse::<f64>() {
return Ok(Value::from(f));
}
if expr == "true" {
return Ok(Value::Bool(true));
}
if expr == "false" {
return Ok(Value::Bool(false));
}
if let Some(val) = resolve_value(expr, context) {
return Ok(val.clone());
}
Err(format!("Could not resolve token or value: {}", expr))
}
fn resolve_value<'a>(path: &str, context: &'a Value) -> Option<&'a Value> {
let parts: Vec<&str> = path.split('.').collect();
let mut current = context;
for part in parts {
{
let val = current.get(part)?;
current = val;
}
}
Some(current)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_parse_expression() {
let (p, f, args) = parse_expression("gate:file_exists(inputs.clm_file)").unwrap();
assert_eq!(p, "gate");
assert_eq!(f, "file_exists");
assert_eq!(args, vec!["inputs.clm_file"]);
let (p2, f2, args2) = parse_expression("gate:payload_nonempty").unwrap();
assert_eq!(p2, "gate");
assert_eq!(f2, "payload_nonempty");
assert_eq!(args2.len(), 0);
}
#[test]
fn test_evaluate_invariant_prefix() {
let context = json!({
"inputs": {
"clm_file": "Cargo.toml"
}
});
let res = evaluate_invariant("gate:file_exists(inputs.clm_file)", ".", &context).unwrap();
assert!(res);
let res2 = evaluate_invariant("gate:file_exists(\"non_existent_file.yaml\")", ".", &context).unwrap();
assert!(!res2);
}
#[test]
fn test_evaluate_invariant_infix_math() {
let context = json!({
"inputs": {
"val": 5
},
"outputs": {
"result": 8
}
});
let res1 = evaluate_invariant("inputs.val > 0", ".", &context).unwrap();
assert!(res1);
let res2 = evaluate_invariant("inputs.val < 0", ".", &context).unwrap();
assert!(!res2);
let res3 = evaluate_invariant("outputs.result % 2 == 0", ".", &context).unwrap();
assert!(res3);
let res4 = evaluate_invariant("outputs.result % 3 == 0", ".", &context).unwrap();
assert!(!res4);
}
}
pub fn assess_equivalence(clm_root: &str, clm_file: &str) -> Result<(i32, String), String> {
use std::fs;
use std::path::{Path, PathBuf};
let content = fs::read_to_string(clm_file)
.map_err(|e| format!("Failed to read equivalence spec: {}", e))?;
let parsed: serde_yaml::Value = serde_yaml::from_str(&content)
.map_err(|e| format!("Failed to parse equivalence YAML: {}", e))?;
let clm_val = parsed.get("clm").unwrap_or(&parsed);
let process_val = clm_val.get("concrete_impl")
.and_then(|v| v.get("process"))
.or_else(|| clm_val.get("process"))
.ok_or_else(|| "No process block found in equivalence spec".to_string())?;
let ref_pcard = process_val.get("process_ref")
.and_then(|v| v.as_str())
.ok_or_else(|| "Missing process_ref in spec".to_string())?;
let cand_pcard = process_val.get("process_cand")
.and_then(|v| v.as_str())
.ok_or_else(|| "Missing process_cand in spec".to_string())?;
let relation_type = process_val.get("relation_type")
.and_then(|v| v.as_str())
.unwrap_or("identity");
let domain_val = process_val.get("domain")
.ok_or_else(|| "Missing domain in spec".to_string())?;
let mut inputs = Vec::new();
if let Some(arr) = domain_val.as_sequence() {
for item in arr {
if let Some(s) = item.as_str() {
inputs.push(s.to_string());
} else if let Some(n) = item.as_i64() {
inputs.push(n.to_string());
} else if let Some(b) = item.as_bool() {
inputs.push(b.to_string());
}
}
}
if inputs.is_empty() {
return Err("Equivalence domain cannot be empty".to_string());
}
let mut opts = RelationOptions::default();
if let Some(ro) = process_val.get("relation_options") {
opts.normalize_newlines = ro.get("normalize_newlines").and_then(|v| v.as_bool()).unwrap_or(false);
opts.trim_whitespace = ro.get("trim_whitespace").and_then(|v| v.as_bool()).unwrap_or(false);
opts.ignore_ansi = ro.get("ignore_ansi").and_then(|v| v.as_bool()).unwrap_or(false);
opts.ignore_case = ro.get("ignore_case").and_then(|v| v.as_bool()).unwrap_or(false);
opts.stop_on_first_mismatch = ro.get("stop_on_first_mismatch").and_then(|v| v.as_bool()).unwrap_or(false);
opts.required_degree = ro.get("required_degree").and_then(|v| v.as_f64());
}
let ref_path = if Path::new(ref_pcard).is_absolute() {
PathBuf::from(ref_pcard)
} else {
Path::new(clm_root).join(ref_pcard)
};
let cand_path = if Path::new(cand_pcard).is_absolute() {
PathBuf::from(cand_pcard)
} else {
Path::new(clm_root).join(cand_pcard)
};
let ref_hash = crate::fs_utils::compute_stripped_file_hash(&ref_path)
.unwrap_or_else(|_| format!("{}unknown_ref", crate::hash::DEFAULT_HASH_PREFIX));
let cand_hash = crate::fs_utils::compute_stripped_file_hash(&cand_path)
.unwrap_or_else(|_| format!("{}unknown_cand", crate::hash::DEFAULT_HASH_PREFIX));
let domain_json = serde_json::to_string(&inputs).unwrap_or_default();
let domain_hash = crate::fs_utils::compute_content_hash(&domain_json);
let mut matched = 0;
let mut total = 0;
let mut witness_lines = Vec::new();
let mut ref_total_ms = 0;
let mut cand_total_ms = 0;
let mut thread_results = Vec::new();
std::thread::scope(|s| {
let mut handles = Vec::new();
for input in &inputs {
let handle_ref = s.spawn(|| {
let start = std::time::Instant::now();
let res = execute_pcard(clm_root, ref_pcard, input);
let elapsed = start.elapsed().as_millis() as u64;
(res, elapsed)
});
let handle_cand = s.spawn(|| {
let start = std::time::Instant::now();
let res = execute_pcard(clm_root, cand_pcard, input);
let elapsed = start.elapsed().as_millis() as u64;
(res, elapsed)
});
handles.push((input, handle_ref, handle_cand));
}
for (input, h_ref, h_cand) in handles {
let (out_ref_res, ref_elapsed) = h_ref.join().unwrap();
let (out_cand_res, cand_elapsed) = h_cand.join().unwrap();
thread_results.push((input, out_ref_res, ref_elapsed, out_cand_res, cand_elapsed));
}
});
for (input, out_ref_res, ref_elapsed, out_cand_res, cand_elapsed) in thread_results {
total += 1;
ref_total_ms += ref_elapsed;
cand_total_ms += cand_elapsed;
let (out_ref, ref_err) = match out_ref_res {
Ok(s) => (s, None),
Err(e) => (String::new(), Some(e)),
};
let (out_cand, cand_err) = match out_cand_res {
Ok(s) => (s, None),
Err(e) => (String::new(), Some(e)),
};
let norm_ref = normalize_output(&out_ref, &opts);
let norm_cand = normalize_output(&out_cand, &opts);
let mut result = "fail";
let mut comp_made = false;
if ref_err.is_none() && cand_err.is_none() {
comp_made = true;
match relation_type {
"identity" => {
if norm_ref == norm_cand {
result = "pass";
matched += 1;
}
}
"structural" => {
if let (Ok(ref_v), Ok(cand_v)) = (parse_json_or_yaml(&norm_ref), parse_json_or_yaml(&norm_cand)) {
let clean_ref = canonical_value(&clean_data(&ref_v));
let clean_cand = canonical_value(&clean_data(&cand_v));
if clean_ref == clean_cand {
result = "pass";
matched += 1;
}
}
}
"numerical" => {
if compare_numerical(&norm_ref, &norm_cand) {
result = "pass";
matched += 1;
}
}
"hash_invariant" => {
if norm_ref == norm_cand {
result = "pass";
matched += 1;
} else {
if let Ok(eq) = check_content_equivalent(clm_root, &norm_ref, &norm_cand)
&& eq {
result = "pass";
matched += 1;
}
}
}
"invariant" => {
if norm_ref == norm_cand {
result = "pass";
matched += 1;
}
}
_ => return Err(format!("Unknown comparison relation: {}", relation_type)),
}
} else {
result = "error";
}
let ref_trunc = if out_ref.len() > 64 { &out_ref[0..64] } else { &out_ref };
let cand_trunc = if out_cand.len() > 64 { &out_cand[0..64] } else { &out_cand };
let mut witness_parts = vec![
format!("\"input\":\"{}\"", input.replace('"', "\\\"")),
format!("\"ref\":\"{}\"", ref_trunc.trim().replace('"', "\\\"").replace('\n', " ")),
format!("\"cand\":\"{}\"", cand_trunc.trim().replace('"', "\\\"").replace('\n', " ")),
format!("\"result\":\"{}\"", result),
];
if let Some(ref_e) = &ref_err {
witness_parts.push(format!("\"ref_error\":\"{}\"", ref_e.replace('"', "\\\"").replace('\n', " ")));
}
if let Some(cand_e) = &cand_err {
witness_parts.push(format!("\"cand_error\":\"{}\"", cand_e.replace('"', "\\\"").replace('\n', " ")));
}
if result == "fail" && comp_made
&& let Some((idx, ac, bc)) = find_divergence(&norm_ref, &norm_cand) {
witness_parts.push(format!("\"diff_at\":{},\"expected_char\":\"{}\",\"actual_char\":\"{}\"", idx, ac, bc));
}
let witness = format!("{{{}}}", witness_parts.join(","));
witness_lines.push(witness);
if opts.stop_on_first_mismatch && result == "fail" {
break;
}
}
let degree = (matched as f64) / (total as f64);
let timestamp = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
let agent_did = std::env::var("CLM_AGENT_DID").unwrap_or_else(|_| "did:clm:bootstrap".to_string());
let vcard_yaml = format!(
"clm:\n equivalence_vcard:\n process_ref_hash: \"{}\"\n process_cand_hash: \"{}\"\n domain_hash: \"{}\"\n relation_type: \"{}\"\n degree_of_equivalence: {:.4}\n witness_trail: [{}]\n performance_differential:\n ref_total_ms: {}\n cand_total_ms: {}\n timestamp: \"{}\"\n agent_did: \"{}\"",
ref_hash,
cand_hash,
domain_hash,
relation_type,
degree,
witness_lines.join(","),
ref_total_ms,
cand_total_ms,
timestamp,
agent_did
);
let vcard_hash = crate::fs_utils::compute_content_hash(&vcard_yaml);
let g_time = crate::fs_utils::compute_g_time();
if let Ok(conn) = crate::db::get_connection("content_memory", clm_root) {
let _ = conn.execute(
"INSERT OR IGNORE INTO card (hash, content, g_time) VALUES (?, ?, ?);",
rusqlite::params![vcard_hash, vcard_yaml, g_time],
);
let handle_name = format!("vcard:equivalence:{}", vcard_hash);
let _ = conn.execute(
"INSERT OR IGNORE INTO handle_registry (handle, current_hash, updated_at) VALUES (?, ?, ?);",
rusqlite::params![handle_name, vcard_hash, timestamp],
);
}
let handle_name = format!("vcard:equivalence:{}", vcard_hash);
crate::cache::setter_mcard_cache_handle(&handle_name, &vcard_hash);
crate::cache::setter_mcard_cache(&vcard_hash, &vcard_yaml);
let zero_fs = crate::utils::is_zero_fs();
if !zero_fs {
let logs_dir = Path::new(clm_root).join("data").join("execution_logs");
let _ = fs::create_dir_all(&logs_dir);
let file_path = logs_dir.join(format!("equiv_{}.yaml", &vcard_hash[0..8]));
let _ = fs::write(&file_path, &vcard_yaml);
}
let threshold = opts.required_degree.unwrap_or(1.0);
let exit_code = if degree >= threshold { 0 } else { 1 };
Ok((exit_code, format!("{}\n", vcard_hash)))
}
fn extract_inline_shell_script(clm_root: &str, clm_file: &str) -> Option<String> {
let path = if std::path::Path::new(clm_file).is_absolute() {
std::path::PathBuf::from(clm_file)
} else {
std::path::Path::new(clm_root).join(clm_file)
};
let content = std::fs::read_to_string(&path).ok()?;
let parsed: serde_yaml::Value = serde_yaml::from_str(&content).ok()?;
let clm_val = parsed.get("clm").unwrap_or(&parsed);
let process = clm_val
.get("concrete_impl")
.and_then(|v| v.get("process"))
.or_else(|| clm_val.get("process"))?;
let shell_map = match process {
serde_yaml::Value::Mapping(_) => Some(process),
serde_yaml::Value::Sequence(seq) => seq.iter().find(|v| {
v.get("type").and_then(|t| t.as_str()) == Some("shell")
}),
_ => None,
}?;
let ptype = shell_map.get("type").and_then(|t| t.as_str()).unwrap_or("shell");
if ptype != "shell" {
return None;
}
if shell_map.get("script_handle").and_then(|v| v.as_str()).is_some() {
return None;
}
let script = shell_map.get("script").and_then(|v| v.as_str())?;
Some(script.to_string())
}
fn execute_pcard(clm_root: &str, clm_file: &str, payload: &str) -> Result<String, String> {
if let Some(script) = extract_inline_shell_script(clm_root, clm_file) {
let bash = crate::fs_utils::get_bash_command();
let mut cmd = std::process::Command::new(bash);
cmd.current_dir(clm_root)
.arg("-c")
.arg(&script)
.arg("clm_pcard") .arg(payload) .env("CLM_PAYLOAD", payload)
.env("CLM_NESTED_EXEC", "1")
.stdin(std::process::Stdio::null());
let output = cmd.output()
.map_err(|e| format!("Failed to execute inline shell PCard: {}", e))?;
if output.status.success() {
let stdout_str = String::from_utf8_lossy(&output.stdout).into_owned();
return Ok(stdout_str.trim().to_string());
} else {
let code = output.status.code().unwrap_or(-1);
let stderr_str = String::from_utf8_lossy(&output.stderr).trim().to_string();
let msg = if stderr_str.is_empty() {
format!("inline shell PCard exited with code {}", code)
} else {
format!("exit {}: {}", code, stderr_str)
};
return Err(msg);
}
}
let self_exe = std::env::current_exe()
.map_err(|e| format!("Failed to get current executable path: {}", e))?;
let mut cmd = std::process::Command::new(self_exe);
cmd.current_dir(clm_root)
.arg("evaluate")
.arg(clm_file)
.arg(payload)
.arg("--quiet");
cmd.envs(std::env::vars());
cmd.env("CLM_PAYLOAD", payload);
cmd.env("CLM_NESTED_EXEC", "1");
cmd.env("CLM_ZERO_FS", "true"); cmd.env("CLM_SKIP_GRAMMAR", "1"); cmd.stdin(std::process::Stdio::null());
let output = cmd.output()
.map_err(|e| format!("Failed to execute process: {}", e))?;
if output.status.success() {
let stdout_str = String::from_utf8_lossy(&output.stdout).into_owned();
let out = if let Some(idx) = stdout_str.find("[POST] V_post:") {
stdout_str[..idx].to_string()
} else {
stdout_str
};
let log_prefixes: &[&str] = &[
"[clmEval", "[VERIFY]", "[VCARD POST]", "[MCARD_KERNEL]",
"[COMPOSE]", "[DEBUG", "[GATE]", "[TYPE", "[POST]", "[PAL]",
"PASS:", "━━", "clmEval —", "Evaluating:", "SCRIPT CONTENT",
];
let filtered: Vec<&str> = out.lines()
.filter(|line| {
let trimmed = line.trim();
!trimmed.starts_with('[') && !trimmed.starts_with("SCRIPT CONTENT") && !log_prefixes.iter().any(|prefix| trimmed.starts_with(prefix))
})
.collect();
let result = filtered.join("\n");
Ok(result.trim().to_string())
} else {
Err(String::from_utf8_lossy(&output.stderr).to_string())
}
}
fn parse_json_or_yaml(s: &str) -> Result<Value, String> {
let trimmed = s.trim();
if trimmed.is_empty() {
return Ok(Value::Null);
}
if let Ok(val) = serde_json::from_str::<Value>(trimmed) {
return Ok(val);
}
if let Ok(yaml_val) = serde_yaml::from_str::<serde_yaml::Value>(trimmed) {
let json_str = serde_json::to_string(&yaml_val)
.map_err(|e| format!("Failed to serialize YAML value: {}", e))?;
let val = serde_json::from_str::<Value>(&json_str)
.map_err(|e| format!("Failed to parse deserialized YAML: {}", e))?;
return Ok(val);
}
Err("Not valid JSON or YAML".to_string())
}
fn clean_data(val: &Value) -> Value {
match val {
Value::Object(obj) => {
let mut cleaned = serde_json::Map::new();
for (k, v) in obj {
if k == "timestamp"
|| k == "g_time"
|| k == "run_id"
|| k == "previous_hash"
|| k == "changed_at"
|| k == "created_at"
|| k == "updated_at"
|| k == "witness_trail"
|| k == "performance_differential"
|| k == "agent_did"
|| k == "uuid"
|| k == "g_time_note"
|| k == "genesis_hash"
|| k == "genesis_note"
{
continue;
}
cleaned.insert(k.clone(), clean_data(v));
}
Value::Object(cleaned)
}
Value::Array(arr) => {
let mut cleaned_list = Vec::new();
for item in arr {
cleaned_list.push(clean_data(item));
}
cleaned_list.sort_by_key(|v| serde_json::to_string(v).unwrap_or_default());
Value::Array(cleaned_list)
}
_ => val.clone(),
}
}
fn canonical_value(val: &Value) -> Value {
match val {
Value::Object(obj) => {
let mut sorted = std::collections::BTreeMap::new();
for (k, v) in obj {
sorted.insert(k.clone(), canonical_value(v));
}
let mut res = serde_json::Map::new();
for (k, v) in sorted {
res.insert(k, v);
}
Value::Object(res)
}
Value::Array(arr) => {
let mut res = Vec::new();
for item in arr {
res.push(canonical_value(item));
}
Value::Array(res)
}
_ => val.clone(),
}
}
fn compare_numerical(s_a: &str, s_b: &str) -> bool {
let val_a = s_a.trim().parse::<f64>();
let val_b = s_b.trim().parse::<f64>();
match (val_a, val_b) {
(Ok(a), Ok(b)) => (a - b).abs() <= 1e-6,
_ => false,
}
}
fn check_content_equivalent(clm_root: &str, hash_a: &str, hash_b: &str) -> Result<bool, String> {
if hash_a == hash_b {
return Ok(true);
}
let was_sandbox = crate::db::TEST_SANDBOX_ACTIVE.with(|a| a.get());
let prev_id = crate::db::TEST_SANDBOX_DB_ID.with(|id| id.borrow().clone());
let prev_depth = crate::db::thread_local_depth_get();
crate::db::TEST_SANDBOX_ACTIVE.with(|a| a.set(false));
crate::db::TEST_SANDBOX_DB_ID.with(|id| *id.borrow_mut() = None);
crate::db::set_sandbox_depth(0);
let result = (|| {
let content_a = crate::db::get_card_content("content_memory", clm_root, hash_a)
.map_err(|e| e.to_string())?;
let content_b = crate::db::get_card_content("content_memory", clm_root, hash_b)
.map_err(|e| e.to_string())?;
Ok(match (content_a, content_b) {
(Some(a), Some(b)) => a == b,
_ => false,
})
})();
crate::db::TEST_SANDBOX_ACTIVE.with(|a| a.set(was_sandbox));
crate::db::TEST_SANDBOX_DB_ID.with(|id| *id.borrow_mut() = prev_id);
crate::db::set_sandbox_depth(prev_depth);
result
}