Skip to main content

clm_rust_core/layer4/
formal_contract.rs

1use std::borrow::Cow;
2use std::collections::{BTreeMap, BTreeSet, HashMap};
3use std::path::{Path, PathBuf};
4
5fn default_schema_version() -> String { crate::layer0::vocabulary_identities::VERSION_N_1_0_0.to_string() }
6fn default_true() -> bool { true }
7/// Canonical request payload shared across Python, TypeScript, and Rust formal executors.
8#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
9pub struct FormalExecutionRequest {
10    #[serde(default = "default_schema_version")] pub schema_version: String,
11    pub runtime_id: String,
12    #[serde(default)] pub profile_id: Option<String>,
13    #[serde(default)] pub source_file: Option<String>,
14    #[serde(default)] pub source_text: Option<String>,
15    #[serde(default)] pub source_name: Option<String>,
16    #[serde(default)] pub theorem_name: Option<String>,
17    #[serde(default)] pub entry_point: Option<String>,
18    #[serde(default)] pub extra_args: Vec<String>,
19    #[serde(default)] pub trailing_args: Vec<String>,
20    #[serde(default)] pub config_file: Option<String>,
21    #[serde(default)] pub staged_files: BTreeMap<String, String>,
22    #[serde(default)] pub stdin_text: Option<String>,
23    #[serde(default)] pub strict: bool,
24    #[serde(default)] pub isolated_empty_path: bool,
25    #[serde(default = "default_true")] pub include_system_fallbacks: bool,
26    #[serde(default)] pub timeout_ms: Option<u64>,
27    #[serde(default)] pub probe_timeout_ms: Option<u64>,
28    #[serde(default)] pub clm_root: String,
29}
30impl Default for FormalExecutionRequest {
31    fn default() -> Self {
32        Self {
33            schema_version: default_schema_version(), runtime_id: String::new(), profile_id: None,
34            source_file: None, source_text: None, source_name: None, theorem_name: None,
35            entry_point: None, extra_args: Vec::new(), trailing_args: Vec::new(), config_file: None,
36            staged_files: BTreeMap::new(), stdin_text: None, strict: false,
37            isolated_empty_path: false, include_system_fallbacks: true,
38            timeout_ms: None, probe_timeout_ms: None, clm_root: String::new(),
39        }
40    }
41}
42
43/// Optional fallback runner indirection declared by a registry profile.
44#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
45pub struct ResolvedRunnerSpec {
46    pub runtime_id: String, pub argv_template: Vec<String>,
47}
48
49/// Summary descriptor for an admitted formal runtime in `runtime_types.json`.
50#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
51pub struct FormalRuntimeSummary {
52    pub runtime_id: String, pub command: String, pub category: String, pub boundary: String,
53    pub default_profile: String, pub profiles: Vec<String>, pub aliases: Vec<String>, pub env_allowlist: Vec<String>,
54}
55
56/// Validated registry and lockfile configuration for a single formal runtime profile.
57#[derive(Debug, Clone)]
58pub struct ResolvedFormalTarget {
59    pub clm_root: PathBuf, pub canonical_id: String, pub active_profile: String, pub default_extension: String,
60    pub env_allowlist: Vec<String>, pub argv_template: Vec<String>, pub config_args_template: Vec<String>,
61    pub runner: Option<ResolvedRunnerSpec>, pub accept_predicate: serde_json::Value,
62    pub timeout_ms: u64, pub probe_timeout_ms: u64, pub executable: String, pub probe_args: Vec<String>,
63    pub allow_nonzero_probe: bool, pub version_substrings_all: Vec<String>, pub version_substrings_any: Vec<String>,
64    pub default_jar_relpath: Option<String>, pub nondeterministic_line_patterns: Vec<String>,
65    pub nondeterministic_inline_patterns: Vec<String>, pub lock_runtimes: serde_json::Value,
66    pub known_profiles: BTreeSet<String>,
67}
68
69pub(crate) fn json_str_vec(val: Option<&serde_json::Value>) -> Vec<String> {
70    val.and_then(|v| v.as_array()).map(|a| a.iter().filter_map(|x| x.as_str().map(String::from)).collect()).unwrap_or_default()
71}
72pub(crate) fn read_json_file(path: &Path) -> Result<serde_json::Value, String> {
73    let raw = std::fs::read_to_string(path).map_err(|e| format!("Failed reading {}: {}", path.display(), e))?;
74    serde_json::from_str(&raw).map_err(|e| format!("Invalid JSON {}: {}", path.display(), e))
75}
76
77/// Resolve the repository root containing `schema/runtime_types.json`.
78pub fn resolve_clm_root(explicit_root: &str) -> PathBuf {
79    for cand in [explicit_root.to_string(), crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_ROOT).unwrap_or_default()].into_iter().filter(|s| !s.is_empty()) {
80        let p = PathBuf::from(&cand);
81        if p.join("schema/runtime_types.json").is_file() { return p; }
82        if p.join("clm/schema/runtime_types.json").is_file() { return p.join("clm"); }
83    }
84    let m = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
85    m.parent().and_then(|p| p.parent()).map(PathBuf::from).unwrap_or(m)
86}
87
88/// Sanitize environment variables against `allowlist` and compute effective strictness.
89pub fn sanitize_env_and_strict(
90    req_strict: bool, isolated_empty_path: bool, allowlist: &[String], caller_env: Option<&HashMap<String, String>>,
91) -> (HashMap<String, String>, bool) {
92    let lookup = |k: &str| -> Option<String> {
93        if isolated_empty_path {
94            return if matches!(k, "PATH" | "ELAN_HOME" | "JAVA_HOME") { None } else { caller_env.and_then(|m| m.get(k).cloned()) };
95        }
96        match caller_env { Some(m) => m.get(k).cloned(), None => crate::layer0::environment::var(k).ok() }
97    };
98    let mut sanitized = HashMap::new();
99    for key in allowlist {
100        if let Some(val) = lookup(key).filter(|v| !v.is_empty()) { sanitized.insert(key.clone(), val); }
101    }
102    let strict = req_strict || lookup(crate::layer0::vocabulary_env::ENV_CLM_FORMAL_STRICT).is_some_and(|v| v == "1" || v.eq_ignore_ascii_case("true"));
103    (sanitized, strict)
104}
105
106/// Load catalog summaries for all formal runtimes declared in `schema/runtime_types.json`.
107pub fn load_formal_runtime_summaries(explicit_root: &str) -> Result<Vec<FormalRuntimeSummary>, String> {
108    let clm_root = resolve_clm_root(explicit_root);
109    let rt_val = read_json_file(&clm_root.join("schema/runtime_types.json"))?;
110    let aliases_obj = rt_val.get("aliases").and_then(|v| v.as_object());
111    let mut out = Vec::new();
112    for entry in rt_val.get("runtimes").and_then(|v| v.as_array()).into_iter().flatten() {
113        if entry.get("formal").and_then(|v| v.as_bool()) != Some(true) { continue; }
114        let rt_id = entry.get("id").and_then(|v| v.as_str()).unwrap_or_default().to_string();
115        let reg_val = read_json_file(&clm_root.join(format!("schema/runtime_registry/{}.json", rt_id)))?;
116        let mut profiles: Vec<String> = reg_val.get("profiles").and_then(|v| v.as_object()).map(|m| m.keys().cloned().collect()).unwrap_or_default();
117        profiles.sort();
118        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();
119        aliases.sort();
120        let env_allowlist = json_str_vec(reg_val.get("env_allowlist")).into_iter().collect::<BTreeSet<_>>().into_iter().collect();
121        out.push(FormalRuntimeSummary {
122            command: entry.get("command").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
123            category: entry.get("category").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
124            boundary: entry.get("boundary").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
125            default_profile: reg_val.get("default_profile").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
126            runtime_id: rt_id, profiles, aliases, env_allowlist,
127        });
128    }
129    Ok(out)
130}
131
132/// Resolve runtime alias, profile, registry metadata, and lockfile pin for a request.
133pub fn resolve_formal_target(
134    runtime_id: &str, profile_id: Option<&str>, req_timeout_ms: Option<u64>, req_probe_timeout_ms: Option<u64>, explicit_root: &str,
135) -> Result<ResolvedFormalTarget, String> {
136    let clm_root = resolve_clm_root(explicit_root);
137    let rt_val = read_json_file(&clm_root.join("schema/runtime_types.json"))?;
138    let raw_id = runtime_id.trim().to_ascii_lowercase();
139    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();
140    let is_formal = rt_val.get("runtimes").and_then(|r| r.as_array()).is_some_and(|arr| {
141        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))
142    });
143    if !is_formal { return Err(format!("Unsupported formal runtime: {}", runtime_id)); }
144    let reg_val = read_json_file(&clm_root.join(format!("schema/runtime_registry/{}.json", canonical_id)))?;
145    let lock_val = read_json_file(&clm_root.join("tools/formal/toolchains.lock.json"))?;
146
147    let active_profile = profile_id.map(str::trim).filter(|s| !s.is_empty()).map(String::from)
148        .or_else(|| reg_val.get("alias_profiles").and_then(|m| m.get(&raw_id)).and_then(|v| v.as_str()).map(String::from))
149        .or_else(|| reg_val.get("default_profile").and_then(|v| v.as_str()).map(String::from))
150        .ok_or_else(|| format!("Missing default_profile for {}", canonical_id))?;
151    let profiles_obj = reg_val.get("profiles").and_then(|p| p.as_object()).ok_or_else(|| format!("Missing profiles in {}", canonical_id))?;
152    let known_profiles: BTreeSet<String> = profiles_obj.keys().cloned().collect();
153    let prof_reg = profiles_obj.get(&active_profile).ok_or_else(|| format!("Unknown profile {}:{}", canonical_id, active_profile))?;
154    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))
155        .ok_or_else(|| format!("Missing lock entry {}:{}", canonical_id, active_profile))?;
156
157    let env_allowlist = json_str_vec(reg_val.get("env_allowlist")).into_iter().collect::<BTreeSet<_>>().into_iter().collect();
158    let runner = prof_reg.get("runner").and_then(|r| Some(ResolvedRunnerSpec {
159        runtime_id: r.get("runtime_id")?.as_str()?.to_string(),
160        argv_template: json_str_vec(r.get("argv_template")),
161    }));
162    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);
163    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);
164
165    Ok(ResolvedFormalTarget {
166        clm_root, canonical_id, active_profile,
167        default_extension: reg_val.get("default_extension").and_then(|v| v.as_str()).unwrap_or(".txt").to_string(),
168        env_allowlist, argv_template: json_str_vec(prof_reg.get("argv_template")),
169        config_args_template: json_str_vec(prof_reg.get("config_args_template")), runner,
170        accept_predicate: prof_reg.get("accept_predicate").cloned().unwrap_or(serde_json::Value::Null),
171        timeout_ms, probe_timeout_ms,
172        executable: prof_lock.get("executable").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
173        probe_args: json_str_vec(prof_lock.get("probe_args")),
174        allow_nonzero_probe: prof_lock.get("allow_nonzero_probe").and_then(|v| v.as_bool()).unwrap_or(false),
175        version_substrings_all: json_str_vec(prof_lock.get("version_substrings_all")),
176        version_substrings_any: json_str_vec(prof_lock.get("version_substrings_any")),
177        default_jar_relpath: prof_lock.get("default_jar_relpath").and_then(|v| v.as_str()).map(String::from),
178        nondeterministic_line_patterns: json_str_vec(prof_lock.get("nondeterministic_line_patterns")),
179        nondeterministic_inline_patterns: json_str_vec(prof_lock.get("nondeterministic_inline_patterns")),
180        lock_runtimes: lock_val.get("runtimes").cloned().unwrap_or(serde_json::Value::Null),
181        known_profiles,
182    })
183}
184
185/// Return true if `name` is a bare single-segment filename without separators or traversal.
186pub fn is_bare_filename(name: &str) -> bool {
187    !name.is_empty() && name != "." && name != ".." && !name.contains('/') && !name.contains('\\')
188        && Path::new(name).file_name().and_then(|n| n.to_str()) == Some(name)
189}
190
191/// Extract the theorem stem from a staged filename by removing its final extension.
192pub fn file_stem_label(basename: &str) -> String {
193    Path::new(basename).file_stem().and_then(|s| s.to_str()).unwrap_or(basename).to_string()
194}
195
196/// Expand `canonical_argv` from an argv template with config, extra, and trailing arguments.
197pub fn expand_canonical_argv(
198    argv_template: &[String], config_args_template: &[String], staged_basename: &str, entry_point: &str,
199    staged_cfg_name: Option<&str>, tla2tools_jar: Option<&str>, extra_args: &[String], trailing_args: &[String],
200) -> Vec<String> {
201    let tpl_has_cfg = argv_template.iter().any(|t| t.contains("{config_file}"));
202    let mut argv: Vec<String> = argv_template.iter().map(|tok| {
203        tok.replace("{source_file}", staged_basename).replace("{entry_point}", entry_point)
204            .replace("{output_dir}", "classes").replace("{config_file}", staged_cfg_name.unwrap_or(""))
205            .replace("{tla2tools_jar}", tla2tools_jar.unwrap_or("{tla2tools_jar}"))
206    }).collect();
207    let mut pre = Vec::new();
208    if let Some(cfg) = staged_cfg_name.filter(|_| !tpl_has_cfg && !config_args_template.is_empty()) {
209        pre.extend(config_args_template.iter().map(|t| t.replace("{config_file}", cfg)));
210    }
211    pre.extend(extra_args.iter().cloned());
212    if !pre.is_empty() { let idx = argv.len().saturating_sub(1); argv.splice(idx..idx, pre); }
213    argv.extend(trailing_args.iter().cloned());
214    argv
215}
216
217/// Strip nested `(* ... *)` block comments from Coq/Rocq source text.
218pub fn strip_coq_comments(source: &str) -> String {
219    let (mut out, mut chars, mut depth) = (String::with_capacity(source.len()), source.chars().peekable(), 0usize);
220    while let Some(c) = chars.next() {
221        if c == '(' && chars.peek() == Some(&'*') { chars.next(); depth += 1; }
222        else if depth > 0 && c == '*' && chars.peek() == Some(&')') { chars.next(); depth -= 1; if depth == 0 { out.push(' '); } }
223        else if depth == 0 { out.push(c); }
224        else if c == '\n' { out.push('\n'); }
225    }
226    out
227}
228
229/// Strip nested `/- ... -/` block comments and `--` line comments from Lean source text.
230pub fn strip_lean_comments(source: &str) -> String {
231    let (mut out, mut chars, mut depth) = (String::with_capacity(source.len()), source.chars().peekable(), 0usize);
232    while let Some(c) = chars.next() {
233        if c == '/' && chars.peek() == Some(&'-') { chars.next(); depth += 1; }
234        else if depth > 0 && c == '-' && chars.peek() == Some(&'/') { chars.next(); depth -= 1; if depth == 0 { out.push(' '); } }
235        else if depth == 0 && c == '-' && chars.peek() == Some(&'-') {
236            for nc in chars.by_ref() { if nc == '\n' { out.push('\n'); break; } }
237        } else if depth == 0 { out.push(c); }
238        else if c == '\n' { out.push('\n'); }
239    }
240    out
241}
242
243/// Normalize temp directory paths and nondeterministic lockfile regex patterns in stream output.
244pub fn normalize_formal_output(
245    raw: &str, real_work: &str, raw_work: &str, line_patterns: &[String], inline_patterns: &[String],
246) -> Result<String, String> {
247    let mut text = if !real_work.is_empty() { raw.replace(real_work, "<work_dir>") } else { raw.to_string() };
248    if !raw_work.is_empty() { text = text.replace(raw_work, "<work_dir>"); }
249    let line_res: Vec<regex::Regex> = line_patterns.iter().map(|p| regex::Regex::new(p).map_err(|e| e.to_string())).collect::<Result<_, _>>()?;
250    let inline_res: Vec<regex::Regex> = inline_patterns.iter().map(|p| regex::Regex::new(p).map_err(|e| e.to_string())).collect::<Result<_, _>>()?;
251    let mut kept = Vec::new();
252    for line in text.split('\n') {
253        if line_res.iter().any(|re| re.is_match(line)) { continue; }
254        let mut cleaned = line.to_string();
255        for re in &inline_res { cleaned = re.replace_all(&cleaned, "").into_owned(); }
256        kept.push(cleaned);
257    }
258    Ok(kept.join("\n"))
259}
260fn strip_for_mode<'a>(source: &'a str, mode: Option<&str>) -> Cow<'a, str> {
261    match mode { Some("coq") => Cow::Owned(strip_coq_comments(source)), Some("lean") => Cow::Owned(strip_lean_comments(source)), _ => Cow::Borrowed(source) }
262}
263
264/// Evaluate a declarative `accept_predicate` JSON object over normalized execution outputs.
265pub fn evaluate_accept_predicate(
266    pred: &serde_json::Value, exit_code: i32, stdout: &str, stderr: &str, source_lossy: &str,
267) -> Result<bool, String> {
268    let Some(obj) = pred.as_object() else { return Ok(false); };
269    if obj.get("exit_code").and_then(|v| v.as_i64()).is_some_and(|exp| exit_code as i64 != exp) { return Ok(false); }
270    if !json_str_vec(obj.get("stdout_contains")).iter().all(|s| stdout.contains(s)) { return Ok(false); }
271    if json_str_vec(obj.get("stdout_not_contains")).iter().any(|s| stdout.contains(s)) { return Ok(false); }
272    let combined = format!("{}\n{}", stdout, stderr);
273    if !json_str_vec(obj.get("combined_contains")).iter().all(|s| combined.contains(s)) { return Ok(false); }
274    if json_str_vec(obj.get("combined_not_contains")).iter().any(|s| combined.contains(s)) { return Ok(false); }
275    for item in obj.get("source_regex_absent").and_then(|v| v.as_array()).into_iter().flatten() {
276        let re = regex::Regex::new(item.get("pattern").and_then(|v| v.as_str()).unwrap_or_default()).map_err(|e| e.to_string())?;
277        if re.is_match(&strip_for_mode(source_lossy, item.get("strip_comments").and_then(|v| v.as_str()))) { return Ok(false); }
278    }
279    for item in obj.get("source_implies_stdout_contains").and_then(|v| v.as_array()).into_iter().flatten() {
280        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());
281        if strip_for_mode(source_lossy, item.get("strip_comments").and_then(|v| v.as_str())).contains(sq) && !stdout.contains(oq) { return Ok(false); }
282    }
283    if let Some(scan) = obj.get("json_line_scan").and_then(|v| v.as_str()) {
284        if scan != "lean_messages" { return Ok(false); }
285        for line in stdout.lines().chain(stderr.lines()) {
286            if let Some(t) = Some(line.trim()).filter(|t| t.starts_with('{')) {
287                if let Ok(val) = serde_json::from_str::<serde_json::Value>(t) {
288                    if val.get("kind").and_then(|v| v.as_str()) == Some("hasSorry")
289                        || val.get("hasSorry").and_then(|v| v.as_bool()) == Some(true)
290                        || val.get("severity").and_then(|v| v.as_str()) == Some("error")
291                        || val.get("data").and_then(|v| v.as_str()).is_some_and(|d| d.to_ascii_lowercase().contains("sorry"))
292                    { return Ok(false); }
293                }
294            }
295        }
296    }
297    Ok(true)
298}