use clm_rust_core::fiber::context::{Fiber, FiberState};
use clm_rust_core::invariants::evaluate_invariant;
use clm_rust_core::layer1::{
ActionDispatcher, ActionManifest, GuardVerdict, SafeRuleEvaluator, evaluate_guard,
};
use clm_rust_core::type_interpreter::TypeInterpreter;
use serde_json::{Map, Value, json};
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Instant;
const RUNTIME: &str = "rust";
thread_local! {
static ACTIVE_CASE_ID: RefCell<String> = const { RefCell::new(String::new()) };
static FIBER_TRACE_OUT: RefCell<Option<Vec<Value>>> = const { RefCell::new(None) };
}
fn record_fiber_step(fiber_id: &str, from: &str, to: &str, held_effects: usize) {
FIBER_TRACE_OUT.with(|slot| {
if let Some(records) = slot.borrow_mut().as_mut() {
let case_id = ACTIVE_CASE_ID.with(|c| c.borrow().clone());
records.push(json!({"caseId": case_id, "fiberId": fiber_id, "from": from, "heldEffects": held_effects, "to": to}));
}
});
}
fn trace_fiber_move(fiber: &Fiber, from: &str) {
record_fiber_step(&fiber.id, from, state_name(&fiber.get_state()).0, fiber.disposables.lock().unwrap().len());
}
fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(Path::parent)
.expect("repository root")
.to_path_buf()
}
fn verdict_observation(verdict: &GuardVerdict) -> Value {
let reason = match verdict {
GuardVerdict::Pass => String::new(),
GuardVerdict::Fail(s) | GuardVerdict::Unmatched(s) => s.clone(),
};
json!({ "outcome": verdict.outcome(), "reason": reason })
}
fn state_name(state: &FiberState) -> (&'static str, Option<String>) {
match state {
FiberState::Pending => ("pending", None),
FiberState::Loading => ("loading", None),
FiberState::Active => ("active", None),
FiberState::Paused => ("paused", None),
FiberState::Suspended => ("suspended", None),
FiberState::Unloading => ("unloading", None),
FiberState::Disposed => ("disposed", None),
FiberState::Failed(reason) => ("failed", Some(reason.clone())),
}
}
fn parse_state(name: &str) -> Result<FiberState, String> {
Ok(match name {
"pending" => FiberState::Pending,
"loading" => FiberState::Loading,
"active" => FiberState::Active,
"paused" => FiberState::Paused,
"suspended" => FiberState::Suspended,
"unloading" => FiberState::Unloading,
"disposed" => FiberState::Disposed,
"failed" => FiberState::Failed("characterization".to_string()),
other => return Err(format!("unknown fiber state {other}")),
})
}
fn fiber_state_value(fiber: &Fiber) -> (Value, Value) {
let (name, reason) = state_name(&fiber.get_state());
(json!(name), reason.map_or(Value::Null, Value::String))
}
fn new_fiber(input: &Value) -> Fiber {
let coeffects: Vec<String> = match input.get("coeffects") {
Some(Value::Array(items)) => items.iter().filter_map(|v| v.as_str().map(str::to_string)).collect(),
_ => vec!["characterization".to_string()],
};
Fiber::new("fd", coeffects)
}
fn fiber_pair(input: &Value) -> Result<Value, String> {
let fiber = new_fiber(input);
for step in input["path"].as_array().ok_or("path")? {
let prev = state_name(&fiber.get_state()).0;
fiber.transition_to(parse_state(step.as_str().ok_or("path state")?)?)?;
trace_fiber_move(&fiber, prev);
}
let to = parse_state(input["to"].as_str().ok_or("to")?)?;
let prev = state_name(&fiber.get_state()).0;
let result = fiber.transition_to(to);
if result.is_ok() {
trace_fiber_move(&fiber, prev);
}
let (state, reason) = fiber_state_value(&fiber);
Ok(json!({ "ok": result.is_ok(), "state": state, "failed_reason": reason, "error": result.err() }))
}
fn fiber_script(input: &Value) -> Result<Value, String> {
let fiber = new_fiber(input);
let mut steps = Vec::new();
for step in input["steps"].as_array().ok_or("steps")? {
let kind = step[0].as_str().ok_or("step kind")?;
let arg = step.get(1).and_then(Value::as_str);
let prev = state_name(&fiber.get_state()).0;
let held_before = fiber.disposables.lock().unwrap().len();
let outcome: Result<(), String> = match kind {
"transition" => fiber.transition_to(parse_state(arg.ok_or("transition target")?)?),
"dispose" => fiber.dispose().map_err(|errs| errs.join("; ")),
"activate" => fiber.activate(),
"satisfy" => { fiber.satisfy_coeffect(arg.ok_or("coeffect")?); Ok(()) }
"failing_disposer" => { fiber.register_disposable(|| Err("disposer boom".to_string())); Ok(()) }
other => return Err(format!("unknown fiber step {other}")),
};
let (state, reason) = fiber_state_value(&fiber);
let next = state_name(&fiber.get_state()).0;
if kind == "dispose" && prev != "disposed" {
record_fiber_step(&fiber.id, prev, "unloading", held_before);
trace_fiber_move(&fiber, "unloading");
} else if ((kind == "transition" || kind == "activate") && outcome.is_ok()) || (kind == "satisfy" && next != prev) {
trace_fiber_move(&fiber, prev);
}
let mut entry = Map::new();
entry.insert("step".into(), json!(kind));
entry.insert("ok".into(), json!(outcome.is_ok()));
entry.insert("state".into(), state);
entry.insert("failed_reason".into(), reason);
if let Err(e) = outcome {
entry.insert("error".into(), json!(e));
}
steps.push(Value::Object(entry));
}
let (state, reason) = fiber_state_value(&fiber);
Ok(json!({ "state": state, "failed_reason": reason, "steps": steps }))
}
fn type_judge(input: &Value) -> Result<Value, String> {
let data: Option<Vec<u8>> = match &input["data"] {
Value::Null => None,
Value::String(s) if input["format"] == "hex" => Some(decode_hex(s)?),
Value::String(s) => Some(s.as_bytes().to_vec()),
other => return Err(format!("unsupported data {other}")),
};
let judgment = TypeInterpreter::create_default().judge(
data.as_deref(),
input["filename"].as_str(),
input["ext_hint"].as_str(),
);
let j: Value = serde_json::from_str(&serde_json::to_string(&judgment).map_err(|e| e.to_string())?)
.map_err(|e| e.to_string())?;
Ok(json!({
"mime": j["mime"], "universe": j["universe"], "is_binary": j["is_binary"],
"confidence": j["confidence"], "matched_rule": j["matched_rule"],
"sub_dialect": j.get("sub_dialect").cloned().unwrap_or(Value::Null),
"truncated_signature": j.get("truncated_signature").cloned().unwrap_or(Value::Null),
}))
}
fn decode_hex(s: &str) -> Result<Vec<u8>, String> {
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).map_err(|e| e.to_string()))
.collect()
}
fn action_execute(input: &Value) -> Result<Value, String> {
let manifest = match ActionManifest::from_yaml_str(input["manifest"].as_str().ok_or("manifest")?) {
Ok(m) => m,
Err(e) => return Ok(json!({ "status": "raised", "error": e })),
};
let bindings: HashMap<String, String> = input["bindings"]
.as_object()
.map(|m| m.iter().filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string()))).collect())
.unwrap_or_default();
let res = ActionDispatcher::execute(&manifest, &bindings);
let metrics: BTreeMap<String, Value> =
res.metrics.into_iter().filter(|(k, _)| k != "wall_time_ms").collect();
Ok(json!({
"status": res.status, "error": res.error, "outputs": res.outputs,
"input_hashes": res.input_hashes, "output_hashes": res.output_hashes, "metrics": metrics,
}))
}
#[cfg(unix)]
fn link_entry(src: &Path, dst: &Path) -> std::io::Result<()> {
std::os::unix::fs::symlink(src, dst)
}
#[cfg(windows)]
fn link_entry(src: &Path, dst: &Path) -> std::io::Result<()> {
if src.is_dir() {
std::os::windows::fs::symlink_dir(src, dst)
} else {
std::os::windows::fs::symlink_file(src, dst)
}
}
fn private_state_root(root: &Path) -> Result<tempfile::TempDir, String> {
let dir = tempfile::tempdir().map_err(|e| e.to_string())?;
for entry in std::fs::read_dir(root).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
let name = entry.file_name();
if name == "db" || name == ".git" {
continue;
}
link_entry(&entry.path(), &dir.path().join(&name)).map_err(|e| e.to_string())?;
}
std::fs::create_dir(dir.path().join("db")).map_err(|e| e.to_string())?;
Ok(dir)
}
fn orchestrator_exit(input: &Value) -> Result<Value, String> {
let root = repo_root();
let state = private_state_root(&root)?;
let file = state.path().join(input["clm_file"].as_str().ok_or("clm_file")?);
let mut cmd = Command::new(env!("CARGO_BIN_EXE_clm_rust_core"));
cmd.current_dir(state.path()).env("CLM_ROOT", state.path()).env("CLM_RUNNER", "rust");
for arg in input["args"].as_array().into_iter().flatten() {
let arg = arg.as_str().ok_or("arg")?;
if arg == "{clm_file}" {
cmd.arg(&file);
} else {
cmd.arg(arg);
}
}
let out = cmd.output().map_err(|e| e.to_string())?;
Ok(json!({ "exit_code": out.status.code() }))
}
fn term_normalize(input: &Value) -> Result<Value, String> {
use clm_rust_core::layer0::term::{parse::parse, reduce, render::render};
let source = input["source"].as_str().ok_or("source")?;
let strategy = reduce::Strategy::from_name(input["strategy"].as_str().ok_or("strategy")?).ok_or("unknown strategy")?;
let fuel = u32::try_from(input["fuel"].as_u64().ok_or("fuel")?).map_err(|e| e.to_string())?;
let term = match parse(source) {
Ok(term) => term,
Err(failure) => return Ok(json!({"outcome": "parse_failure", "message": failure.message, "position": failure.position})),
};
Ok(match reduce::normalize(&term, strategy, fuel, &reduce::no_primitives, false) {
reduce::Reduction::Normal(r) => json!({"outcome": "normal", "term": render(&r.term), "steps": r.steps}),
reduce::Reduction::OutOfFuel(r) => json!({"outcome": "out_of_fuel", "term": render(&r.term), "steps": r.steps}),
reduce::Reduction::Stuck(s) => json!({"outcome": "stuck", "term": render(&s.term), "steps": Value::Null}),
})
}
fn handle(op: &str, input: &Value) -> Option<Result<Value, String>> {
let ctx = |key: &str| input.get(key).cloned().filter(|v| !v.is_null()).unwrap_or_else(|| json!({}));
Some(match op {
"guard.evaluate" | "guard.kleene" => Ok(verdict_observation(&evaluate_guard(
input["expression"].as_str().unwrap_or_default(),
&ctx("context"),
))),
"guard.parse" => {
use clm_rust_core::layer0::guard_term_parser::{compile_guard_term, parse_expression_term};
use clm_rust_core::layer0::term::render::render;
let expr = input["expression"].as_str().unwrap_or_default();
let profile = input.get("profile").and_then(Value::as_str).filter(|s| !s.is_empty()).unwrap_or("guard");
let parsed = if input.get("mode").and_then(Value::as_str) == Some("strict") {
parse_expression_term(expr, profile)
} else {
compile_guard_term(expr, profile)
};
Ok(match parsed {
Ok(t) => json!({ "outcome": "ok", "term": render(&t) }),
Err(_) => json!({ "outcome": "parse_failure", "term": Value::Null }),
})
}
"rule.evaluate" => {
let rule = input["rule"].as_str().unwrap_or_default();
let context = ctx("context");
Ok(json!({
"value": SafeRuleEvaluator::evaluate(rule, &context),
"verdict_outcome": SafeRuleEvaluator::evaluate_verdict(rule, &context).outcome(),
}))
}
"rule.verdict" => Ok(verdict_observation(&SafeRuleEvaluator::evaluate_verdict(
input["rule"].as_str().unwrap_or_default(),
&ctx("context"),
))),
"invariant.evaluate" => {
let res = evaluate_invariant(input["expression"].as_str().unwrap_or_default(), ".", &ctx("context"));
Ok(match res {
Ok(b) => json!({ "value": b, "error": null }),
Err(e) => json!({ "value": null, "error": e }),
})
}
"type.judge" => type_judge(input),
"action.execute" => action_execute(input),
"fiber.pair" => fiber_pair(input),
"fiber.script" => fiber_script(input),
"orchestrator.exit" => orchestrator_exit(input),
"term.normalize" => term_normalize(input),
_ => return None,
})
}
fn run_case(case: &Value) -> Value {
if let Some(declared) = case.get("unsupported").and_then(|u| u.get(RUNTIME)) {
return json!({ "status": "unsupported", "missing_entry": declared["missing_entry"] });
}
let op = case["op"].as_str().unwrap_or_default();
match std::panic::catch_unwind(|| handle(op, &case["input"])) {
Ok(None) => json!({ "status": "unsupported", "missing_entry": format!("no rust runner handler for {op}") }),
Ok(Some(Ok(observation))) => json!({ "status": "ok", "observation": observation }),
Ok(Some(Err(e))) => json!({ "status": "error", "error": e }),
Err(_) => json!({ "status": "error", "error": "panic" }),
}
}
fn time_probe(mut f: impl FnMut(), samples: usize) -> Value {
for _ in 0..(samples / 10).max(1) {
f();
}
let mut durations: Vec<u128> = (0..samples)
.map(|_| {
let t0 = Instant::now();
f();
t0.elapsed().as_nanos()
})
.collect();
durations.sort_unstable();
json!({
"samples": samples,
"median_ns": durations[durations.len() / 2],
"p90_ns": durations[durations.len() * 9 / 10],
"min_ns": durations[0],
})
}
const PERF_MANIFEST_CASE: &str = "IU-10.action.fixture.action_07_substitute_tabular";
fn perf(corpus: &Value, out: &Path, samples: usize) {
let cases = corpus["cases"].as_array().expect("cases");
let guards: Vec<(String, Value)> = cases
.iter()
.filter(|c| c["op"] == "guard.evaluate" && c["runtimes"].as_array().is_some_and(|r| r.contains(&json!(RUNTIME))))
.map(|c| {
let ctx = c["input"]["context"].clone();
(c["input"]["expression"].as_str().unwrap().to_string(), if ctx.is_null() { json!({}) } else { ctx })
})
.collect();
let read_fiber = Fiber::new("perf", vec!["c".to_string()]);
let manifest_case = cases.iter().find(|c| c["case_id"] == PERF_MANIFEST_CASE).expect("perf manifest case");
let manifest = ActionManifest::from_yaml_str(manifest_case["input"]["manifest"].as_str().unwrap()).unwrap();
let bindings: HashMap<String, String> = manifest_case["input"]["bindings"]
.as_object()
.unwrap()
.iter()
.map(|(k, v)| (k.clone(), v.as_str().unwrap().to_string()))
.collect();
let mut probes = Map::new();
let mut add = |name: &str, scenario: String, timing: Value| {
let mut entry = timing.as_object().cloned().unwrap_or_default();
entry.insert("status".into(), json!("ok"));
entry.insert("scenario".into(), json!(scenario));
probes.insert(name.into(), Value::Object(entry));
};
add("guard", format!("{} guard.evaluate cases per sample", guards.len()), time_probe(|| {
for (expr, ctx) in &guards { std::hint::black_box(evaluate_guard(expr, ctx)); }
}, samples));
add("guard_cold_compile", format!("{} guard.evaluate expressions compiled fresh without cache per sample", guards.len()), time_probe(|| {
use clm_rust_core::layer0::guard_term_parser::compile_guard_term;
for (expr, _) in &guards { let _ = std::hint::black_box(compile_guard_term(expr, "guard")); }
}, samples));
add("fiber", "Fiber::new + get_state + transition_to(Loading)".into(), time_probe(|| {
let f = Fiber::new("perf", vec!["c".to_string()]);
std::hint::black_box(read_fiber.get_state());
let _ = f.transition_to(FiberState::Loading);
}, samples));
add("manifest", format!("ActionDispatcher::execute on {PERF_MANIFEST_CASE}"), time_probe(|| {
std::hint::black_box(ActionDispatcher::execute(&manifest, &bindings));
}, samples));
let (scenario, timing) = petri_probe(samples);
add("petri", scenario, timing);
std::fs::create_dir_all(out).expect("perf out dir");
let doc = json!({ "runtime": RUNTIME, "profile": if cfg!(debug_assertions) { "debug" } else { "release" }, "probes": probes });
std::fs::write(out.join("perf.json"), format!("{doc}\n")).expect("write perf.json");
}
fn petri_probe(samples: usize) -> (String, Value) {
use clm_rust_core::layer1::{ColoredPetriVM, PetriNet};
let net: PetriNet = serde_json::from_value(json!({
"places": [{ "id": "p_in", "initial_tokens": 1, "initial_color": null }, { "id": "p_out", "initial_tokens": 0, "initial_color": null }],
"transitions": [{ "id": "t", "inputs": ["p_in"], "outputs": ["p_out"] }]
})).expect("perf petri net");
let timing = time_probe(|| {
let mut vm = ColoredPetriVM::new(net.clone());
std::hint::black_box(vm.fire("t")).expect("fire");
}, samples);
("ColoredPetriVM: fire t (p_in -> p_out) on a fresh VM with one token".to_string(), timing)
}
#[test]
fn characterization_runner() {
let root = repo_root();
let corpus_path = std::env::var("CLM_CHARACTERIZATION_CORPUS").map(PathBuf::from).unwrap_or_else(|_| root.join("tests/polyglot/characterization/corpus.json"));
let scratch = tempfile::tempdir().expect("scratch dir");
let out = std::env::var("CLM_CHARACTERIZATION_OUT").map(PathBuf::from).unwrap_or_else(|_| scratch.path().to_path_buf());
let rows: Vec<String> = std::env::var("CLM_CHARACTERIZATION_ROWS").unwrap_or_default().split(',').filter(|s| !s.is_empty()).map(str::to_string).collect();
let text = std::fs::read_to_string(&corpus_path).unwrap_or_else(|e| panic!("cannot read {}: {e}", corpus_path.display()));
let corpus: Value = serde_json::from_str(&text).expect("corpus json");
if let Ok(samples) = std::env::var("CLM_CHARACTERIZATION_PERF") {
perf(&corpus, &out, samples.parse().expect("CLM_CHARACTERIZATION_PERF sample count"));
return;
}
let trace_path = std::env::var("CLM_CHARACTERIZATION_TRACE_JSONL").ok().filter(|s| !s.is_empty()).map(PathBuf::from);
if trace_path.is_some() { FIBER_TRACE_OUT.with(|slot| *slot.borrow_mut() = Some(Vec::new())); }
let mut docs: BTreeMap<String, Map<String, Value>> = BTreeMap::new();
for case in corpus["cases"].as_array().expect("cases") {
if !case["runtimes"].as_array().is_some_and(|r| r.contains(&json!(RUNTIME))) { continue; }
let unit = case["units"][RUNTIME].as_str().expect("unit").to_string();
let row = case["row"].as_str().unwrap_or_default();
if !rows.is_empty() && !rows.iter().any(|r| r == &unit || r == row) { continue; }
let id = case["case_id"].as_str().expect("case_id").to_string();
ACTIVE_CASE_ID.with(|c| *c.borrow_mut() = id.clone());
docs.entry(unit).or_default().insert(id, run_case(case));
ACTIVE_CASE_ID.with(|c| c.borrow_mut().clear());
}
std::fs::create_dir_all(&out).expect("out dir");
let mut errors = Vec::new();
for (unit, cases) in &docs {
for (id, result) in cases { if result["status"] == "error" { errors.push(format!("{id}: {}", result["error"])); } }
std::fs::write(out.join(format!("{unit}.json")), format!("{}\n", json!({ "row": unit, "runtime": RUNTIME, "cases": cases }))).expect("write row doc");
}
if let Some(path) = trace_path {
let records = FIBER_TRACE_OUT.with(|slot| slot.borrow_mut().take()).unwrap_or_default();
if let Some(parent) = path.parent() { std::fs::create_dir_all(parent).expect("trace parent dir"); }
let lines: String = records.iter().map(|r| format!("{}\n", serde_json::to_string(r).expect("trace json"))).collect();
std::fs::write(&path, lines).expect("write fiber trace jsonl");
}
assert!(errors.is_empty(), "runner errors:\n{}", errors.join("\n"));
}