use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::{Path, PathBuf};
fn default_schema_version() -> String { "1.0.0".to_string() }
fn default_true() -> bool { true }
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FormalExecutionRequest {
#[serde(default = "default_schema_version")] pub schema_version: String,
pub runtime_id: String,
#[serde(default)] pub profile_id: Option<String>,
#[serde(default)] pub source_file: Option<String>,
#[serde(default)] pub source_text: Option<String>,
#[serde(default)] pub source_name: Option<String>,
#[serde(default)] pub theorem_name: Option<String>,
#[serde(default)] pub entry_point: Option<String>,
#[serde(default)] pub extra_args: Vec<String>,
#[serde(default)] pub trailing_args: Vec<String>,
#[serde(default)] pub config_file: Option<String>,
#[serde(default)] pub staged_files: BTreeMap<String, String>,
#[serde(default)] pub stdin_text: Option<String>,
#[serde(default)] pub strict: bool,
#[serde(default)] pub isolated_empty_path: bool,
#[serde(default = "default_true")] pub include_system_fallbacks: bool,
#[serde(default)] pub timeout_ms: Option<u64>,
#[serde(default)] pub probe_timeout_ms: Option<u64>,
#[serde(default)] pub clm_root: String,
}
impl Default for FormalExecutionRequest {
fn default() -> Self {
Self {
schema_version: default_schema_version(), runtime_id: String::new(), profile_id: None,
source_file: None, source_text: None, source_name: None, theorem_name: None,
entry_point: None, extra_args: Vec::new(), trailing_args: Vec::new(), config_file: None,
staged_files: BTreeMap::new(), stdin_text: None, strict: false,
isolated_empty_path: false, include_system_fallbacks: true,
timeout_ms: None, probe_timeout_ms: None, clm_root: String::new(),
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ResolvedRunnerSpec {
pub runtime_id: String, pub argv_template: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FormalRuntimeSummary {
pub runtime_id: String, pub command: String, pub category: String, pub boundary: String,
pub default_profile: String, pub profiles: Vec<String>, pub aliases: Vec<String>, pub env_allowlist: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct ResolvedFormalTarget {
pub clm_root: PathBuf, pub canonical_id: String, pub active_profile: String, pub default_extension: String,
pub env_allowlist: Vec<String>, pub argv_template: Vec<String>, pub config_args_template: Vec<String>,
pub runner: Option<ResolvedRunnerSpec>, pub accept_predicate: serde_json::Value,
pub timeout_ms: u64, pub probe_timeout_ms: u64, pub executable: String, pub probe_args: Vec<String>,
pub allow_nonzero_probe: bool, pub version_substrings_all: Vec<String>, pub version_substrings_any: Vec<String>,
pub default_jar_relpath: Option<String>, pub nondeterministic_line_patterns: Vec<String>,
pub nondeterministic_inline_patterns: Vec<String>, pub lock_runtimes: serde_json::Value,
pub known_profiles: BTreeSet<String>,
}
pub(crate) fn json_str_vec(val: Option<&serde_json::Value>) -> Vec<String> {
val.and_then(|v| v.as_array()).map(|a| a.iter().filter_map(|x| x.as_str().map(String::from)).collect()).unwrap_or_default()
}
pub(crate) fn read_json_file(path: &Path) -> Result<serde_json::Value, String> {
let raw = std::fs::read_to_string(path).map_err(|e| format!("Failed reading {}: {}", path.display(), e))?;
serde_json::from_str(&raw).map_err(|e| format!("Invalid JSON {}: {}", path.display(), e))
}
pub fn resolve_clm_root(explicit_root: &str) -> PathBuf {
for cand in [explicit_root.to_string(), std::env::var("CLM_ROOT").unwrap_or_default()].into_iter().filter(|s| !s.is_empty()) {
let p = PathBuf::from(&cand);
if p.join("schema/runtime_types.json").is_file() { return p; }
if p.join("clm/schema/runtime_types.json").is_file() { return p.join("clm"); }
}
let m = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
m.parent().and_then(|p| p.parent()).map(PathBuf::from).unwrap_or(m)
}
pub fn sanitize_env_and_strict(
req_strict: bool, isolated_empty_path: bool, allowlist: &[String], caller_env: Option<&HashMap<String, String>>,
) -> (HashMap<String, String>, bool) {
let lookup = |k: &str| -> Option<String> {
if isolated_empty_path {
return if matches!(k, "PATH" | "ELAN_HOME" | "JAVA_HOME") { None } else { caller_env.and_then(|m| m.get(k).cloned()) };
}
match caller_env { Some(m) => m.get(k).cloned(), None => std::env::var(k).ok() }
};
let mut sanitized = HashMap::new();
for key in allowlist {
if let Some(val) = lookup(key).filter(|v| !v.is_empty()) { sanitized.insert(key.clone(), val); }
}
let strict = req_strict || lookup("CLM_FORMAL_STRICT").is_some_and(|v| v == "1" || v.eq_ignore_ascii_case("true"));
(sanitized, strict)
}
pub fn load_formal_runtime_summaries(explicit_root: &str) -> Result<Vec<FormalRuntimeSummary>, String> {
let clm_root = resolve_clm_root(explicit_root);
let rt_val = read_json_file(&clm_root.join("schema/runtime_types.json"))?;
let aliases_obj = rt_val.get("aliases").and_then(|v| v.as_object());
let mut out = Vec::new();
for entry in rt_val.get("runtimes").and_then(|v| v.as_array()).into_iter().flatten() {
if entry.get("formal").and_then(|v| v.as_bool()) != Some(true) { continue; }
let rt_id = entry.get("id").and_then(|v| v.as_str()).unwrap_or_default().to_string();
let reg_val = read_json_file(&clm_root.join(format!("schema/runtime_registry/{}.json", rt_id)))?;
let mut profiles: Vec<String> = reg_val.get("profiles").and_then(|v| v.as_object()).map(|m| m.keys().cloned().collect()).unwrap_or_default();
profiles.sort();
let mut aliases: Vec<String> = aliases_obj.map(|m| m.iter().filter(|(_, v)| v.as_str() == Some(rt_id.as_str())).map(|(k, _)| k.clone()).collect()).unwrap_or_default();
aliases.sort();
let env_allowlist = json_str_vec(reg_val.get("env_allowlist")).into_iter().collect::<BTreeSet<_>>().into_iter().collect();
out.push(FormalRuntimeSummary {
command: entry.get("command").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
category: entry.get("category").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
boundary: entry.get("boundary").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
default_profile: reg_val.get("default_profile").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
runtime_id: rt_id, profiles, aliases, env_allowlist,
});
}
Ok(out)
}
pub fn resolve_formal_target(
runtime_id: &str, profile_id: Option<&str>, req_timeout_ms: Option<u64>, req_probe_timeout_ms: Option<u64>, explicit_root: &str,
) -> Result<ResolvedFormalTarget, String> {
let clm_root = resolve_clm_root(explicit_root);
let rt_val = read_json_file(&clm_root.join("schema/runtime_types.json"))?;
let raw_id = runtime_id.trim().to_ascii_lowercase();
let canonical_id = rt_val.get("aliases").and_then(|a| a.get(&raw_id)).and_then(|v| v.as_str()).unwrap_or(&raw_id).to_string();
let is_formal = rt_val.get("runtimes").and_then(|r| r.as_array()).is_some_and(|arr| {
arr.iter().any(|e| e.get("id").and_then(|v| v.as_str()) == Some(canonical_id.as_str()) && e.get("formal").and_then(|v| v.as_bool()) == Some(true))
});
if !is_formal { return Err(format!("Unsupported formal runtime: {}", runtime_id)); }
let reg_val = read_json_file(&clm_root.join(format!("schema/runtime_registry/{}.json", canonical_id)))?;
let lock_val = read_json_file(&clm_root.join("tools/formal/toolchains.lock.json"))?;
let active_profile = profile_id.map(str::trim).filter(|s| !s.is_empty()).map(String::from)
.or_else(|| reg_val.get("alias_profiles").and_then(|m| m.get(&raw_id)).and_then(|v| v.as_str()).map(String::from))
.or_else(|| reg_val.get("default_profile").and_then(|v| v.as_str()).map(String::from))
.ok_or_else(|| format!("Missing default_profile for {}", canonical_id))?;
let profiles_obj = reg_val.get("profiles").and_then(|p| p.as_object()).ok_or_else(|| format!("Missing profiles in {}", canonical_id))?;
let known_profiles: BTreeSet<String> = profiles_obj.keys().cloned().collect();
let prof_reg = profiles_obj.get(&active_profile).ok_or_else(|| format!("Unknown profile {}:{}", canonical_id, active_profile))?;
let prof_lock = lock_val.get("runtimes").and_then(|r| r.get(&canonical_id)).and_then(|r| r.get("profiles")).and_then(|p| p.get(&active_profile))
.ok_or_else(|| format!("Missing lock entry {}:{}", canonical_id, active_profile))?;
let env_allowlist = json_str_vec(reg_val.get("env_allowlist")).into_iter().collect::<BTreeSet<_>>().into_iter().collect();
let runner = prof_reg.get("runner").and_then(|r| Some(ResolvedRunnerSpec {
runtime_id: r.get("runtime_id")?.as_str()?.to_string(),
argv_template: json_str_vec(r.get("argv_template")),
}));
let timeout_ms = req_timeout_ms.or_else(|| prof_reg.get("timeout_ms").and_then(|v| v.as_u64())).or_else(|| reg_val.get("timeout_ms").and_then(|v| v.as_u64())).unwrap_or(60_000);
let probe_timeout_ms = req_probe_timeout_ms.or_else(|| lock_val.get("probe_timeout_ms").and_then(|v| v.as_u64())).unwrap_or(5_000);
Ok(ResolvedFormalTarget {
clm_root, canonical_id, active_profile,
default_extension: reg_val.get("default_extension").and_then(|v| v.as_str()).unwrap_or(".txt").to_string(),
env_allowlist, argv_template: json_str_vec(prof_reg.get("argv_template")),
config_args_template: json_str_vec(prof_reg.get("config_args_template")), runner,
accept_predicate: prof_reg.get("accept_predicate").cloned().unwrap_or(serde_json::Value::Null),
timeout_ms, probe_timeout_ms,
executable: prof_lock.get("executable").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
probe_args: json_str_vec(prof_lock.get("probe_args")),
allow_nonzero_probe: prof_lock.get("allow_nonzero_probe").and_then(|v| v.as_bool()).unwrap_or(false),
version_substrings_all: json_str_vec(prof_lock.get("version_substrings_all")),
version_substrings_any: json_str_vec(prof_lock.get("version_substrings_any")),
default_jar_relpath: prof_lock.get("default_jar_relpath").and_then(|v| v.as_str()).map(String::from),
nondeterministic_line_patterns: json_str_vec(prof_lock.get("nondeterministic_line_patterns")),
nondeterministic_inline_patterns: json_str_vec(prof_lock.get("nondeterministic_inline_patterns")),
lock_runtimes: lock_val.get("runtimes").cloned().unwrap_or(serde_json::Value::Null),
known_profiles,
})
}
pub fn is_bare_filename(name: &str) -> bool {
!name.is_empty() && name != "." && name != ".." && !name.contains('/') && !name.contains('\\')
&& Path::new(name).file_name().and_then(|n| n.to_str()) == Some(name)
}
pub fn file_stem_label(basename: &str) -> String {
Path::new(basename).file_stem().and_then(|s| s.to_str()).unwrap_or(basename).to_string()
}
pub fn expand_canonical_argv(
argv_template: &[String], config_args_template: &[String], staged_basename: &str, entry_point: &str,
staged_cfg_name: Option<&str>, tla2tools_jar: Option<&str>, extra_args: &[String], trailing_args: &[String],
) -> Vec<String> {
let tpl_has_cfg = argv_template.iter().any(|t| t.contains("{config_file}"));
let mut argv: Vec<String> = argv_template.iter().map(|tok| {
tok.replace("{source_file}", staged_basename).replace("{entry_point}", entry_point)
.replace("{output_dir}", "classes").replace("{config_file}", staged_cfg_name.unwrap_or(""))
.replace("{tla2tools_jar}", tla2tools_jar.unwrap_or("{tla2tools_jar}"))
}).collect();
let mut pre = Vec::new();
if let Some(cfg) = staged_cfg_name.filter(|_| !tpl_has_cfg && !config_args_template.is_empty()) {
pre.extend(config_args_template.iter().map(|t| t.replace("{config_file}", cfg)));
}
pre.extend(extra_args.iter().cloned());
if !pre.is_empty() { let idx = argv.len().saturating_sub(1); argv.splice(idx..idx, pre); }
argv.extend(trailing_args.iter().cloned());
argv
}
pub fn strip_coq_comments(source: &str) -> String {
let (mut out, mut chars, mut depth) = (String::with_capacity(source.len()), source.chars().peekable(), 0usize);
while let Some(c) = chars.next() {
if c == '(' && chars.peek() == Some(&'*') { chars.next(); depth += 1; }
else if depth > 0 && c == '*' && chars.peek() == Some(&')') { chars.next(); depth -= 1; if depth == 0 { out.push(' '); } }
else if depth == 0 { out.push(c); }
else if c == '\n' { out.push('\n'); }
}
out
}
pub fn strip_lean_comments(source: &str) -> String {
let (mut out, mut chars, mut depth) = (String::with_capacity(source.len()), source.chars().peekable(), 0usize);
while let Some(c) = chars.next() {
if c == '/' && chars.peek() == Some(&'-') { chars.next(); depth += 1; }
else if depth > 0 && c == '-' && chars.peek() == Some(&'/') { chars.next(); depth -= 1; if depth == 0 { out.push(' '); } }
else if depth == 0 && c == '-' && chars.peek() == Some(&'-') {
for nc in chars.by_ref() { if nc == '\n' { out.push('\n'); break; } }
} else if depth == 0 { out.push(c); }
else if c == '\n' { out.push('\n'); }
}
out
}
pub fn normalize_formal_output(
raw: &str, real_work: &str, raw_work: &str, line_patterns: &[String], inline_patterns: &[String],
) -> Result<String, String> {
let mut text = if !real_work.is_empty() { raw.replace(real_work, "<work_dir>") } else { raw.to_string() };
if !raw_work.is_empty() { text = text.replace(raw_work, "<work_dir>"); }
let line_res: Vec<regex::Regex> = line_patterns.iter().map(|p| regex::Regex::new(p).map_err(|e| e.to_string())).collect::<Result<_, _>>()?;
let inline_res: Vec<regex::Regex> = inline_patterns.iter().map(|p| regex::Regex::new(p).map_err(|e| e.to_string())).collect::<Result<_, _>>()?;
let mut kept = Vec::new();
for line in text.split('\n') {
if line_res.iter().any(|re| re.is_match(line)) { continue; }
let mut cleaned = line.to_string();
for re in &inline_res { cleaned = re.replace_all(&cleaned, "").into_owned(); }
kept.push(cleaned);
}
Ok(kept.join("\n"))
}
fn strip_for_mode<'a>(source: &'a str, mode: Option<&str>) -> Cow<'a, str> {
match mode { Some("coq") => Cow::Owned(strip_coq_comments(source)), Some("lean") => Cow::Owned(strip_lean_comments(source)), _ => Cow::Borrowed(source) }
}
pub fn evaluate_accept_predicate(
pred: &serde_json::Value, exit_code: i32, stdout: &str, stderr: &str, source_lossy: &str,
) -> Result<bool, String> {
let Some(obj) = pred.as_object() else { return Ok(false); };
if obj.get("exit_code").and_then(|v| v.as_i64()).is_some_and(|exp| exit_code as i64 != exp) { return Ok(false); }
if !json_str_vec(obj.get("stdout_contains")).iter().all(|s| stdout.contains(s)) { return Ok(false); }
if json_str_vec(obj.get("stdout_not_contains")).iter().any(|s| stdout.contains(s)) { return Ok(false); }
let combined = format!("{}\n{}", stdout, stderr);
if !json_str_vec(obj.get("combined_contains")).iter().all(|s| combined.contains(s)) { return Ok(false); }
if json_str_vec(obj.get("combined_not_contains")).iter().any(|s| combined.contains(s)) { return Ok(false); }
for item in obj.get("source_regex_absent").and_then(|v| v.as_array()).into_iter().flatten() {
let re = regex::Regex::new(item.get("pattern").and_then(|v| v.as_str()).unwrap_or_default()).map_err(|e| e.to_string())?;
if re.is_match(&strip_for_mode(source_lossy, item.get("strip_comments").and_then(|v| v.as_str()))) { return Ok(false); }
}
for item in obj.get("source_implies_stdout_contains").and_then(|v| v.as_array()).into_iter().flatten() {
let (sq, oq) = (item.get("source_contains").and_then(|v| v.as_str()).unwrap_or_default(), item.get("stdout_contains").and_then(|v| v.as_str()).unwrap_or_default());
if strip_for_mode(source_lossy, item.get("strip_comments").and_then(|v| v.as_str())).contains(sq) && !stdout.contains(oq) { return Ok(false); }
}
if let Some(scan) = obj.get("json_line_scan").and_then(|v| v.as_str()) {
if scan != "lean_messages" { return Ok(false); }
for line in stdout.lines().chain(stderr.lines()) {
if let Some(t) = Some(line.trim()).filter(|t| t.starts_with('{')) {
if let Ok(val) = serde_json::from_str::<serde_json::Value>(t) {
if val.get("kind").and_then(|v| v.as_str()) == Some("hasSorry")
|| val.get("hasSorry").and_then(|v| v.as_bool()) == Some(true)
|| val.get("severity").and_then(|v| v.as_str()) == Some("error")
|| val.get("data").and_then(|v| v.as_str()).is_some_and(|d| d.to_ascii_lowercase().contains("sorry"))
{ return Ok(false); }
}
}
}
}
Ok(true)
}