From 9c65a30265d8dd60a0d8e1bd80cea401db0657c2 Mon Sep 17 00:00:00 2001 From: jyviko <2501648+jyviko@users.noreply.github.com> Date: Tue, 3 Nov 2026 06:05:18 -0500 Subject: [PATCH] Kernel rig: export each case's kernel operations as a corpus for solidcore's rig `cargo xtask kernel-cases [--ladder] --export DIR` runs the rig with the kernel's operations traced (`smlib::trace`) and writes one entry per case: the operation, or chain of operations, that made the bodies the case ends with; each operand that came in as a record, as that record; the parameters, with every pick by points also given as the topology index it picked; the fix set; the case's expected values and the rig's result. A case no kernel operation alone accounts for is written too, marked not expressible with its reasons. DIR/manifest.json counts them per area. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ --- apps/solidcraft-cli/src/kernel_cases.rs | 47 ++- .../solidcraft-cli/src/kernel_cases_export.rs | 279 ++++++++++++++++++ apps/solidcraft-cli/src/main.rs | 3 +- crates/kernel/src/lib.rs | 1 + crates/smlib/Cargo.toml | 2 +- crates/smlib/src/core.rs | 1 + crates/smlib/src/core/blend.rs | 2 +- crates/smlib/src/core/dump.rs | 3 +- crates/smlib/src/core/picked.rs | 80 +++++ crates/smlib/src/export.rs | 38 ++- crates/smlib/src/history.rs | 4 +- crates/smlib/src/import.rs | 10 +- crates/smlib/src/lib.rs | 17 +- crates/smlib/src/ops.rs | 138 +++++++-- crates/smlib/src/sheets.rs | 11 +- crates/smlib/src/split.rs | 27 +- crates/smlib/src/sweeps.rs | 94 +++++- crates/smlib/src/trace.rs | 225 ++++++++++++++ crates/smlib/src/trim.rs | 23 +- xtask/src/kernel_cases.rs | 18 +- xtask/src/kernel_corpus.rs | 172 +++++++++++ xtask/src/kernel_ladder.rs | 4 + xtask/src/main.rs | 7 +- 23 files changed, 1148 insertions(+), 58 deletions(-) create mode 100644 apps/solidcraft-cli/src/kernel_cases_export.rs create mode 100644 crates/smlib/src/core/picked.rs create mode 100644 crates/smlib/src/trace.rs create mode 100644 xtask/src/kernel_corpus.rs diff --git a/apps/solidcraft-cli/src/kernel_cases.rs b/apps/solidcraft-cli/src/kernel_cases.rs index 57f4c7c1a..b07c44e9b 100644 --- a/apps/solidcraft-cli/src/kernel_cases.rs +++ b/apps/solidcraft-cli/src/kernel_cases.rs @@ -657,7 +657,34 @@ fn flat_pattern_checks(c: &mut Checks, want: &Value, results: &[Value], rel: f64 } } -fn check_case(dir: &str, dumps: Option<&std::path::Path>, validation: solidcraft_engine::kernel::Validation) -> Value { +/// [`check_case_in`], and with `export` the case's corpus entry written under it +/// ([`crate::kernel_cases_export`]). +fn check_case( + dir: &str, + dumps: Option<&std::path::Path>, + validation: solidcraft_engine::kernel::Validation, + export: Option<&std::path::Path>, +) -> Value { + let mut capture = export.map(|_| crate::kernel_cases_export::Capture::default()); + let out = check_case_in(dir, dumps, validation, capture.as_mut()); + if let (Some(root), Some(capture)) = (export, capture) + && let Err(e) = crate::kernel_cases_export::write(root, dir, &out, capture) + { + let mut out = out; + out["export_error"] = json!(e); + return out; + } + out +} + +/// One case run and graded. With `capture`, the kernel operations its commands make are traced +/// and kept there with the bodies the case ends with. +fn check_case_in( + dir: &str, + dumps: Option<&std::path::Path>, + validation: solidcraft_engine::kernel::Validation, + mut capture: Option<&mut crate::kernel_cases_export::Capture>, +) -> Value { let (area, name) = case_name(dir); let mut out = json!({"case": name, "area": area, "kernel": kernel()}); let want = match read_json(&format!("{dir}/expected.json")) { @@ -711,7 +738,14 @@ fn check_case(dir: &str, dumps: Option<&std::path::Path>, validation: solidcraft let mut s = solidcraft_engine::Session::default(); s.set_validation(validation); let clock = std::time::Instant::now(); + if capture.is_some() { + solidcraft_engine::kernel::smlib_trace::start(); + } let ran = s.run_script(&commands); + // The trace ends with the commands: grading reads the bodies through the kernel too. + if let Some(c) = capture.as_deref_mut() { + c.events = solidcraft_engine::kernel::smlib_trace::finish(); + } // The commands' own time, without process start or grading: the ladder's timing golden. out["seconds"] = json!(clock.elapsed().as_secs_f64()); let answers = match (ran, may_refuse) { @@ -762,6 +796,9 @@ fn check_case(dir: &str, dumps: Option<&std::path::Path>, validation: solidcraft return out; } }; + if let Some(c) = capture.as_deref_mut() { + c.world = world.bodies.iter().map(|b| (b.name.clone(), b.body.record().clone())).collect(); + } let mut bodies = Vec::new(); let mut c = Checks { list: Vec::new(), pass: true }; let want_valid = want["valid"].as_bool().unwrap_or(true); @@ -976,7 +1013,13 @@ pub fn run(args: &[String]) -> Result<(), String> { .ok_or_else(|| format!("kernel-cases: --validation= must be each_step, each_feature or off, not `{m}`"))?, None => solidcraft_engine::kernel::Validation::default(), }; - let results: Vec = dirs.iter().map(|d| check_case(d, dumps.as_deref(), validation)).collect(); + // `--export=DIR`: each case's kernel operations, their input records and its expected values + // go to DIR//.json, the corpus for solidcore's rig. + let export = args.iter().find_map(|a| a.strip_prefix("--export=")).map(std::path::PathBuf::from); + if export.as_ref().is_some_and(|d| d.as_os_str().is_empty()) { + return Err("kernel-cases: --export= needs a folder".into()); + } + let results: Vec = dirs.iter().map(|d| check_case(d, dumps.as_deref(), validation, export.as_deref())).collect(); if dumps.is_some() { solidcraft_engine::kernel::smlib_dump_dir(None); } diff --git a/apps/solidcraft-cli/src/kernel_cases_export.rs b/apps/solidcraft-cli/src/kernel_cases_export.rs new file mode 100644 index 000000000..469497afd --- /dev/null +++ b/apps/solidcraft-cli/src/kernel_cases_export.rs @@ -0,0 +1,279 @@ +//! `kernel-cases --export=DIR`: one kernel rig case as a corpus entry for solidcore's rig. The +//! case's commands run as the rig runs them while the kernel's operations are traced +//! (`smlib_trace`); the entry holds the operation (or the chain of operations) that made the +//! bodies the case ends with, each operand that came in as a record given as that record (the +//! B-rep record, solidcore-record's JSON), the fix set, the case's expected values and the rig's +//! own result. A case whose result no kernel operation alone accounts for is written too, marked +//! not expressible with the reasons, so nothing is dropped silently. +//! +//! An entry is expressible when: +//! - the run failed in a kernel operation (the engine's error carries the operation's), or every +//! body the case ends with is the record a traced operation's output was written as, or an +//! operand some traced operation took, as its record; +//! - its expected values are all the kernel's measures: none of `continuity`, `watertight`, +//! `files`, `results`, `construction` or `flat_pattern`, which SolidCraft computes; +//! - the counts it grades (faces, edges, vertices) are the records' own: the rig counts faces and +//! edges merged across one analytic surface or curve (`merged_topology`, from a mesh), so an +//! entry where those differ is not expressible. + +use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::path::{Path, PathBuf}; + +use serde_json::{Value, json}; +use solidcraft_engine::geom::brep::Brep; +use solidcraft_engine::kernel::smlib_trace::{self, Event}; + +use crate::read_json; + +/// What a traced case leaves for its entry: the trace of its commands and the bodies it ends with. +#[derive(Default)] +pub struct Capture { + pub events: Vec, + pub world: Vec<(String, Brep)>, +} + +/// The expected values SolidCraft computes, never the kernel. +const GRADER_ONLY: [&str; 6] = ["continuity", "watertight", "files", "results", "construction", "flat_pattern"]; + +/// The case directory's area (its path under `tests/kernel-cases/`, a ladder case's tier under +/// `ladder/`) and its id. +fn area_and_id(dir: &str) -> Result<(String, String), String> { + let p = Path::new(dir); + let parts: Vec = p.components().map(|c| c.as_os_str().to_string_lossy().to_string()).collect(); + let at = parts.iter().rposition(|c| c == "kernel-cases").ok_or_else(|| format!("{dir}: not under tests/kernel-cases"))?; + let rest = parts.get(at + 1..).unwrap_or_default(); + match rest.split_last() { + Some((id, area)) if !area.is_empty() => Ok((area.join("/"), id.clone())), + _ => Err(format!("{dir}: no area")), + } +} + +/// The case's line in its area's `KNOWN_FAILING`: why it fails today; `None` when not listed. +fn known_failing(dir: &str, id: &str) -> Option { + let list = Path::new(dir).parent()?.join("KNOWN_FAILING"); + let text = std::fs::read_to_string(list).ok()?; + text.lines().filter(|l| !l.trim_start().starts_with('#')).find_map(|l| { + let (case, why) = l.split_once(char::is_whitespace).unwrap_or((l, "")); + (case == id).then(|| why.trim().to_string()) + }) +} + +/// The trace read as operations and the records that went in and came out. +struct Trace<'a> { + /// Each operation: its name, operands, parameters and outputs or error. + calls: Vec<(&'static str, &'a [u64], &'a Value, &'a Result, String>)>, + /// The record each body made from a record was made from. + imports: HashMap, + /// The records bodies were written as, in order. + exports: Vec<(u64, &'a Brep)>, + /// The operation that made each body. + producer: HashMap, +} + +impl<'a> Trace<'a> { + fn of(events: &'a [Event]) -> Trace<'a> { + let mut t = Trace { calls: Vec::new(), imports: HashMap::new(), exports: Vec::new(), producer: HashMap::new() }; + for e in events { + match e { + Event::Import { id, record, trusted, keeping_splines } => { + t.imports.insert(*id, (record, *trusted, *keeping_splines)); + } + Event::Export { id, record } => t.exports.push((*id, record)), + Event::Call { op, inputs, params, outputs } => { + if let Ok(made) = outputs { + for id in made { + t.producer.insert(*id, t.calls.len()); + } + } + t.calls.push((*op, inputs, params, outputs)); + } + } + } + t + } + + /// Bodies some operation took as an operand. + fn operands(&self) -> BTreeSet { + self.calls.iter().flat_map(|c| c.1.iter().copied()).collect() + } +} + +/// The operations `ends` need, in the order they ran: each body's maker, and its operands' makers +/// in turn, back to bodies made from records. Operands made by nothing traced are listed in +/// `unknown`. +fn chain(t: &Trace<'_>, ends: &[u64], unknown: &mut BTreeSet) -> Vec { + let mut need = BTreeSet::new(); + let mut todo: Vec = ends.to_vec(); + while let Some(id) = todo.pop() { + if t.imports.contains_key(&id) { + continue; + } + match t.producer.get(&id) { + Some(&i) => { + if need.insert(i) + && let Some(c) = t.calls.get(i) + { + todo.extend(c.1.iter().copied()); + } + } + None => { + unknown.insert(id); + } + } + } + need.into_iter().collect() +} + +/// The entry of case `dir`, run with result `out`, into `root//.json`, and the fix set +/// into `root/fixes.json`. +pub fn write(root: &Path, dir: &str, out: &Value, capture: Capture) -> Result<(), String> { + let (area, id) = area_and_id(dir)?; + let want = read_json(&format!("{dir}/expected.json"))?; + let t = Trace::of(&capture.events); + let mut why_not: Vec = Vec::new(); + let status = out["status"].as_str().unwrap_or(""); + + if let Some(missing) = want["missing"].as_str() { + why_not.push(format!("SolidCraft has no command for it yet: {missing}")); + } + let grader: Vec<&str> = GRADER_ONLY.iter().copied().filter(|k| want.get(*k).is_some_and(|v| !v.is_null())).collect(); + if !grader.is_empty() { + why_not.push(format!("graded on what SolidCraft computes: {}", grader.join(", "))); + } + + // The bodies the entry ends with: what each is, by name. + let mut result: Vec = Vec::new(); + let mut ends: Vec = Vec::new(); + let mut failed_call: Option = None; + let error = out["refused_with"].as_str().or_else(|| out["error"].as_str()).filter(|_| out.get("checks").is_none()); + if let Some(e) = error.filter(|_| want.get("missing").is_none()) { + // The run stopped: the kernel operation that failed with the message the engine gave. + failed_call = t.calls.iter().rposition(|c| matches!(c.3, Err(m) if !m.is_empty() && e.contains(m.as_str()))); + match failed_call { + Some(i) => ends.extend(t.calls.get(i).map(|c| c.1.to_vec()).unwrap_or_default()), + None if t.calls.is_empty() => why_not.push(format!("no kernel operation ran before the failure: {e}")), + None => why_not.push(format!("the failure is SolidCraft's, not a kernel operation's: {e}")), + } + } else if want.get("missing").is_none() { + let operands = t.operands(); + let mut made = 0; + for (name, record) in &capture.world { + let from_op = t.exports.iter().rev().find(|(_, r)| *r == record).map(|(id, _)| *id).filter(|id| t.producer.contains_key(id)); + let operand = t.imports.iter().find(|(i, (r, _, _))| *r == record && operands.contains(i)).map(|(i, _)| *i); + match (from_op, operand) { + (Some(id), _) => { + made += 1; + ends.push(id); + result.push(json!({"body": name, "made": format!("b{id}")})); + } + (None, Some(id)) => { + ends.push(id); + result.push(json!({"body": name, "operand": format!("b{id}")})); + } + (None, None) => why_not.push(format!( + "body {name:?} is no kernel operation's output nor an operand: SolidCraft built it or changed the kernel's record" + )), + } + } + if capture.world.is_empty() { + why_not.push("the case ends with no body".into()); + } else if made == 0 && why_not.is_empty() { + why_not.push("no kernel operation made a body the case ends with".into()); + } + // Counts graded as SolidCraft merges them, where that differs from the records'. + let raw = |k: &str| -> u64 { + capture + .world + .iter() + .map(|(_, r)| match k { + "faces" => r.faces.len(), + "edges" => r.edges.len(), + _ => r.vertices.len(), + } as u64) + .sum() + }; + for k in ["faces", "edges", "vertices"] { + let graded = want.get(k).is_some_and(|v| !v.is_null()); + let merged = out["measured"][k].as_u64(); + if graded && merged.is_some_and(|m| m != raw(k)) { + why_not.push(format!("{k} graded merged across analytic surfaces ({}), the records have {}", merged.unwrap_or(0), raw(k))); + } + } + } + + let mut unknown = BTreeSet::new(); + let order = chain(&t, &ends, &mut unknown); + if !unknown.is_empty() { + why_not.push(format!("operands made by nothing traced: {unknown:?}")); + } + let order: Vec = match failed_call { + Some(i) if !order.contains(&i) => order.into_iter().chain([i]).collect(), + _ => order, + }; + // Operand records: every body the chain takes that came in as a record. + let mut bodies = BTreeMap::new(); + let calls: Vec = order + .iter() + .filter_map(|&i| t.calls.get(i)) + .map(|(op, inputs, params, outputs)| { + for id in inputs.iter() { + if let Some((record, trusted, splines)) = t.imports.get(id) { + bodies.insert(format!("b{id}"), json!({"trusted": trusted, "keeping_splines": splines, "record": record})); + } + } + let mut c = json!({"op": op, "inputs": inputs.iter().map(|i| format!("b{i}")).collect::>(), "params": params}); + match outputs { + Ok(made) => c["outputs"] = json!(made.iter().map(|i| format!("b{i}")).collect::>()), + Err(e) => c["error"] = json!(e), + } + c + }) + .collect(); + for (_, record) in &capture.world { + // An operand the case ends with, never operated on in the chain, still comes as its record. + if let Some(id) = t.imports.iter().find(|(_, (r, _, _))| *r == record).map(|(i, _)| *i) + && result.iter().any(|r| r["operand"] == json!(format!("b{id}"))) + && let Some((record, trusted, splines)) = t.imports.get(&id) + { + bodies.insert(format!("b{id}"), json!({"trusted": trusted, "keeping_splines": splines, "record": record})); + } + } + let rig = json!({ + "status": out["status"], "error": out["error"], "refused_with": out["refused_with"], "measured": out["measured"], + "known_failing": known_failing(dir, &id), + }); + let entry = json!({ + "case": id, + "area": area, + "sources": want["sources"], + "what": want["what"], + "expressible": why_not.is_empty(), + "why_not": why_not, + "expected": want, + "rig": rig, + "calls": calls, + "bodies": bodies, + "result": result, + "traced_ops": t.calls.iter().map(|c| c.0).collect::>(), + "status": status, + }); + let folder: PathBuf = root.join(&area); + std::fs::create_dir_all(&folder).map_err(|e| format!("{}: {e}", folder.display()))?; + let path = folder.join(format!("{id}.json")); + let text = serde_json::to_string(&entry).map_err(|e| e.to_string())?; + std::fs::write(&path, text).map_err(|e| format!("{}: {e}", path.display()))?; + let fixes = serde_json::to_string_pretty(&smlib_trace::fixes()).map_err(|e| e.to_string())?; + std::fs::write(root.join("fixes.json"), fixes).map_err(|e| format!("{}: {e}", root.display())) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_area_is_the_path_under_kernel_cases() { + assert_eq!(area_and_id("tests/kernel-cases/booleans/1894-x").unwrap(), ("booleans".into(), "1894-x".into())); + assert_eq!(area_and_id("/r/tests/kernel-cases/ladder/07-fillets/003-y").unwrap(), ("ladder/07-fillets".into(), "003-y".into())); + assert!(area_and_id("/r/other/x").is_err()); + } +} diff --git a/apps/solidcraft-cli/src/main.rs b/apps/solidcraft-cli/src/main.rs index fbed265ad..de03f741d 100644 --- a/apps/solidcraft-cli/src/main.rs +++ b/apps/solidcraft-cli/src/main.rs @@ -35,6 +35,7 @@ mod bench_assistant; mod crashes; mod fuzz; mod kernel_cases; +mod kernel_cases_export; mod logger; mod oracle; mod preview; @@ -85,7 +86,7 @@ const USAGE_RECIPE: &str = " solidcraft-cli recipe \n"; /// `oracle`'s lines in the usage. const USAGE_ORACLE: &str = " solidcraft-cli oracle ... [--json]\n"; /// `kernel-cases`'s lines in the usage. -const USAGE_KERNEL_CASES: &str = " solidcraft-cli kernel-cases ... [--json] [--dumps=DIR] [--validation=each_step|each_feature|off]\n (the kernel regression rig, tests/kernel-cases, on SMLib; --dumps: each case's\n failing SMLib calls leave their inputs for replay in DIR/-; --validation: when SMLib's\n full check runs (diagnostics.validation), default each_feature)\n"; +const USAGE_KERNEL_CASES: &str = " solidcraft-cli kernel-cases ... [--json] [--dumps=DIR] [--export=DIR] [--validation=each_step|each_feature|off]\n (the kernel regression rig, tests/kernel-cases, on SMLib; --dumps: each case's\n failing SMLib calls leave their inputs for replay in DIR/-; --export: each case's\n kernel operations, input records and expected values for solidcore's rig, in DIR//.json; --validation: when SMLib's\n full check runs (diagnostics.validation), default each_feature)\n"; /// `fuzz`'s lines in the usage. const USAGE_FUZZ: &str = " solidcraft-cli fuzz [--from N] [--count N] [--jobs J] [--timeout S] [--steps N] [--minimise] [--out DIR] [--report FILE]\n"; diff --git a/crates/kernel/src/lib.rs b/crates/kernel/src/lib.rs index 480bb6bf0..5921862d2 100644 --- a/crates/kernel/src/lib.rs +++ b/crates/kernel/src/lib.rs @@ -215,6 +215,7 @@ pub use prism_curve::{PlanarPiece, PlanarSpline}; pub use prism_measure::{PrismSection, certified_prism_equivalent, certified_prism_section}; pub use repair::{Hole, RebuiltFace, RefusedFace, RepairOptions, RepairReport, SewnGap, repair}; pub use sew::{EdgeSpec, FaceSpec, SurfSpec, sew, sew_touching}; +pub use solidcraft_smlib::trace as smlib_trace; pub use splitface::{SplitTool, split_body, split_faces, split_faces_with_map}; pub use step::step_export; pub use step_in::{ diff --git a/crates/smlib/Cargo.toml b/crates/smlib/Cargo.toml index 65b1ec063..bbf861635 100644 --- a/crates/smlib/Cargo.toml +++ b/crates/smlib/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] log = { workspace = true } +serde_json = { workspace = true } solidcraft-geom = { workspace = true } solidcraft-profile = { workspace = true } solidcore = { workspace = true } @@ -18,4 +19,3 @@ solidcore = { workspace = true } [dev-dependencies] solidcraft-profile = { workspace = true, features = ["enabled"] } flate2 = "1.1" -serde_json = { workspace = true } diff --git a/crates/smlib/src/core.rs b/crates/smlib/src/core.rs index d31a7cde6..823c4dfe9 100644 --- a/crates/smlib/src/core.rs +++ b/crates/smlib/src/core.rs @@ -37,6 +37,7 @@ mod merge_tests; pub(crate) mod offset; #[cfg(test)] mod offset_tests; +pub(crate) mod picked; pub(crate) mod split; #[cfg(test)] mod split_tests; diff --git a/crates/smlib/src/core/blend.rs b/crates/smlib/src/core/blend.rs index 3d1bcb293..094edfdcd 100644 --- a/crates/smlib/src/core/blend.rs +++ b/crates/smlib/src/core/blend.rs @@ -161,7 +161,7 @@ pub(super) fn pick_edges( } /// `pick_vertices` (`vertex_distance`). -fn pick_vertices(b: &Brep, points: &[[f64; 3]], tol: f64) -> solidcore::Result, Vec), Miss>> { +pub(super) fn pick_vertices(b: &Brep, points: &[[f64; 3]], tol: f64) -> solidcore::Result, Vec), Miss>> { let all = brep_p1::vertices(b, None)?; pick(&all, points, tol, b.tolerance(), |v, p| Ok(Some((b.vertex(v)?.point - p).length()))) } diff --git a/crates/smlib/src/core/dump.rs b/crates/smlib/src/core/dump.rs index 686fae63e..88ac3ff2c 100644 --- a/crates/smlib/src/core/dump.rs +++ b/crates/smlib/src/core/dump.rs @@ -97,7 +97,8 @@ plain!( Brep, solidcore::geom::vector3d::Point3, crate::history::Raw, - crate::MassProperties + crate::MassProperties, + Vec ); impl Answer for Option {} diff --git a/crates/smlib/src/core/picked.rs b/crates/smlib/src/core/picked.rs new file mode 100644 index 000000000..02a6b8d93 --- /dev/null +++ b/crates/smlib/src/core/picked.rs @@ -0,0 +1,80 @@ +//! What a pick by points chose, as indices into the body's own lists: for the operations a trace +//! records ([`crate::trace`]), so the corpus names its edges, faces and corners by topology +//! index as well as by the points the operation was given. Each point is picked alone, by the +//! same picker the operation runs, on the body as the operation received it. + +use std::sync::PoisonError; + +use solidcore::topology::brep::p1 as brep_p1; +use solidcore::topology::{edge, vertex}; + +use super::Body; +use super::blend::{Miss, pick_edges, pick_vertices}; +use super::offset::pick_faces; +use crate::Result; +use crate::trace::{Pick, PickKind}; + +/// The entity each of `points` picks within `tolerance` on `body`, as [`Pick`]s: its index in +/// the body's list of that kind (`None` where the point picks none or is ambiguous) and, for an +/// edge, its end points; for a vertex, its point. +pub(crate) fn picks(body: &Body, kind: PickKind, points: &[[f64; 3]], tolerance: f64) -> Result> { + super::run_on("trace pick", &[], |k| { + let mut b = body.0.lock().unwrap_or_else(PoisonError::into_inner); + let mut out = Vec::with_capacity(points.len()); + for p in points { + let one = std::slice::from_ref(p); + let pick = match kind { + PickKind::Edge => { + let all = brep_p1::edges(&b, None)?; + match pick_edges(k, &b, one, tolerance)? { + Ok((es, _)) => match es.first() { + Some(&e) => { + let mut ends = Vec::with_capacity(2); + for v in [edge::start_vertex(&b, e)?, edge::end_vertex(&b, e)?].into_iter().flatten() { + let q = vertex::point(&b, v)?; + ends.push([q.x, q.y, q.z]); + } + Pick { index: all.iter().position(|&x| x == e), at: ends, miss: None } + } + None => Pick::missed("none"), + }, + Err(m) => Pick::missed(miss(m)), + } + } + PickKind::Face => { + let all = brep_p1::faces(&b, None)?; + match pick_faces(k, &mut b, one, tolerance)? { + Ok(fs) => match fs.first() { + Some(&f) => Pick { index: all.iter().position(|&x| x == f), at: Vec::new(), miss: None }, + None => Pick::missed("none"), + }, + Err(m) => Pick::missed(miss(m)), + } + } + PickKind::Vertex => { + let all = brep_p1::vertices(&b, None)?; + match pick_vertices(&b, one, tolerance)? { + Ok((vs, _)) => match vs.first() { + Some(&v) => { + let q = vertex::point(&b, v)?; + Pick { index: all.iter().position(|&x| x == v), at: vec![[q.x, q.y, q.z]], miss: None } + } + None => Pick::missed("none"), + }, + Err(m) => Pick::missed(miss(m)), + } + } + }; + out.push(pick); + } + b.refresh_cached_topology(); + Ok(out) + }) +} + +fn miss(m: Miss) -> &'static str { + match m { + Miss::None(_) => "none", + Miss::Ambiguous(_) => "ambiguous", + } +} diff --git a/crates/smlib/src/export.rs b/crates/smlib/src/export.rs index 3d17583fc..a9e59eaba 100644 --- a/crates/smlib/src/export.rs +++ b/crates/smlib/src/export.rs @@ -50,23 +50,41 @@ use crate::{Brep, Result}; impl Brep { /// A cylinder about +Z: base centre, radius and height (mm, > 0). pub fn make_cylinder(origin: [f64; 3], radius: f64, height: f64) -> Result { - crate::core::make_cylinder(origin, radius, height).map(Brep::made_core) + let r = crate::core::make_cylinder(origin, radius, height).map(Brep::made_core); + crate::trace::made("make_cylinder", &[], &r, || serde_json::json!({"origin": origin, "radius": radius, "height": height})); + r } /// A sphere: centre and radius (mm, > 0). pub fn make_sphere(center: [f64; 3], radius: f64) -> Result { - crate::core::make_sphere(center, radius).map(Brep::made_core) + let r = crate::core::make_sphere(center, radius).map(Brep::made_core); + crate::trace::made("make_sphere", &[], &r, || serde_json::json!({"center": center, "radius": radius})); + r } /// A cone or frustum about +Z: base centre, base radius (> 0), top radius (≥ 0, not the base /// radius) and height (> 0), in mm. pub fn make_cone(origin: [f64; 3], base_radius: f64, top_radius: f64, height: f64) -> Result { - crate::core::make_cone(origin, base_radius, top_radius, height).map(Brep::made_core) + let r = crate::core::make_cone(origin, base_radius, top_radius, height).map(Brep::made_core); + crate::trace::made( + "make_cone", + &[], + &r, + || serde_json::json!({"origin": origin, "base_radius": base_radius, "top_radius": top_radius, "height": height}), + ); + r } /// A ring torus about +Z: centre, major radius > minor radius > 0 (mm). pub fn make_torus(center: [f64; 3], major_radius: f64, minor_radius: f64) -> Result { - crate::core::make_torus(center, major_radius, minor_radius).map(Brep::made_core) + let r = crate::core::make_torus(center, major_radius, minor_radius).map(Brep::made_core); + crate::trace::made( + "make_torus", + &[], + &r, + || serde_json::json!({"center": center, "major_radius": major_radius, "minor_radius": minor_radius}), + ); + r } /// The flat arrays of this body that the mapping reads. @@ -82,7 +100,11 @@ impl Brep { /// [`Brep::to_native`], with what the mapping kept as SMLib wrote it. pub fn to_native_report(&self) -> std::result::Result<(b::Brep, Report), ExportError> { let arrays = self.arrays().map_err(ExportError::Smlib)?; - native(&arrays) + let r = native(&arrays); + if let Ok((record, _)) = &r { + crate::trace::exported(self, record); + } + r } /// [`Brep::to_native`], with where each of SMLib's faces and edges went in the record's @@ -91,7 +113,11 @@ impl Brep { /// `GetEdges`), the order its history uses. pub fn to_native_ordered(&self) -> std::result::Result<(b::Brep, Order), ExportError> { let arrays = self.arrays().map_err(ExportError::Smlib)?; - native_ordered(&arrays).map(|(brep, _, order)| (brep, order)) + let r = native_ordered(&arrays).map(|(brep, _, order)| (brep, order)); + if let Ok((record, _)) = &r { + crate::trace::exported(self, record); + } + r } } diff --git a/crates/smlib/src/history.rs b/crates/smlib/src/history.rs index a584e9982..b13e0a293 100644 --- a/crates/smlib/src/history.rs +++ b/crates/smlib/src/history.rs @@ -68,7 +68,9 @@ impl Brep { /// SMLib's order, which the copy keeps). Run an operation on tagged copies, then read its /// result's [`Brep::history`]. pub fn tagged(&self, operand: usize) -> Result { - core::history::tag(self.core(), operand).map(Brep::made_core) + let r = core::history::tag(self.core(), operand).map(Brep::made_core); + crate::trace::made("tagged", &[self], &r, || serde_json::json!({"operand": operand})); + r } /// The history of this body, made by an operation from tagged operands: each face's and diff --git a/crates/smlib/src/import.rs b/crates/smlib/src/import.rs index 5968f057d..c3ce8dbb5 100644 --- a/crates/smlib/src/import.rs +++ b/crates/smlib/src/import.rs @@ -673,13 +673,15 @@ impl crate::Brep { /// The SMLib B-rep of a schema 4 record. `trusted` as in [`arrays`]. pub fn from_record(brep: &b::Brep, trusted: bool) -> crate::Result { - let a = arrays(brep, trusted).map_err(crate::Error::Record)?; - crate::Brep::from_arrays(&a) + let r = arrays(brep, trusted).map_err(crate::Error::Record).and_then(|a| crate::Brep::from_arrays(&a)); + crate::trace::imported(&r, brep, trusted, false); + r } /// [`crate::Brep::from_record`] through [`arrays_keeping_splines`]. pub fn from_record_keeping_splines(brep: &b::Brep, trusted: bool) -> crate::Result { - let a = arrays_keeping_splines(brep, trusted).map_err(crate::Error::Record)?; - crate::Brep::from_arrays(&a) + let r = arrays_keeping_splines(brep, trusted).map_err(crate::Error::Record).and_then(|a| crate::Brep::from_arrays(&a)); + crate::trace::imported(&r, brep, trusted, true); + r } } diff --git a/crates/smlib/src/lib.rs b/crates/smlib/src/lib.rs index df250565d..8d04758c8 100644 --- a/crates/smlib/src/lib.rs +++ b/crates/smlib/src/lib.rs @@ -20,6 +20,7 @@ mod sheets; mod split; pub mod ssi; pub mod sweeps; +pub mod trace; pub mod trim; pub use check::{CheckLevel, CheckReport}; @@ -250,6 +251,8 @@ pub fn crash_test() -> Result<()> { /// A B-rep. Immutable: operations return new bodies. pub struct Brep { body: core::Body, + /// The body's number in a trace ([`trace`]). + id: u64, } impl std::fmt::Debug for Brep { @@ -261,7 +264,7 @@ impl std::fmt::Debug for Brep { impl Brep { /// A body solidcore made. fn made_core(body: core::Body) -> Brep { - Brep { body } + Brep { body, id: trace::next_id() } } /// solidcore's body. @@ -271,7 +274,9 @@ impl Brep { /// An axis-aligned box from its lower corner, with positive edge lengths (mm). pub fn make_box(origin: [f64; 3], size: [f64; 3]) -> Result { - core::make_box(origin, size).map(Brep::made_core) + let r = core::make_box(origin, size).map(Brep::made_core); + trace::made("make_box", &[], &r, || serde_json::json!({"origin": origin, "size": size})); + r } /// Reads a native SMLib `.smb` file. @@ -286,7 +291,9 @@ impl Brep { /// A deep copy. pub fn try_clone(&self) -> Result { - core::transform::copy(self.core()).map(Brep::made_core) + let r = core::transform::copy(self.core()).map(Brep::made_core); + trace::made("copy", &[self], &r, || serde_json::json!({})); + r } /// This body moved by the affine `matrix` (column-major: columns 0–2 the images of x, y and @@ -294,7 +301,9 @@ impl Brep { /// mirror it, a non-uniform scale or a shear turns its geometry into NURBS, tolerances scale /// with it. pub fn transformed(&self, matrix: &[[f64; 4]; 4]) -> Result { - core::transform::transform(self.core(), matrix).map(Brep::made_core) + let r = core::transform::transform(self.core(), matrix).map(Brep::made_core); + trace::made("transform", &[self], &r, || serde_json::json!({"matrix": matrix})); + r } /// The enclosed volume (mm³) at `relative_accuracy` (SMLib clamps it to [1e-8, 1e-1]). diff --git a/crates/smlib/src/ops.rs b/crates/smlib/src/ops.rs index 66fdfeaf1..a23292ddf 100644 --- a/crates/smlib/src/ops.rs +++ b/crates/smlib/src/ops.rs @@ -6,6 +6,9 @@ //! edge (face) nearest to it, which must lie within `tolerance` of it and be nearer than any other //! by more than the body's modelling tolerance ([`Error::NoMatch`], [`Error::Ambiguous`]). +use serde_json::json; + +use crate::trace::{self, PickKind}; use crate::{Brep, Error, Result, core}; /// How [`Brep::boolean`] combines two bodies. @@ -17,6 +20,17 @@ pub enum BooleanOp { Intersect, } +impl BooleanOp { + /// The operation's name in a trace ([`crate::trace`]). + pub fn word(self) -> &'static str { + match self { + BooleanOp::Union => "union", + BooleanOp::Subtract => "subtract", + BooleanOp::Intersect => "intersect", + } + } +} + /// What [`Brep::blend`] makes of each selected edge. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Blend { @@ -32,6 +46,18 @@ pub enum Blend { Chamfer, } +impl Blend { + /// The section's name in a trace ([`crate::trace`]). + pub fn word(self) -> &'static str { + match self { + Blend::Round => "round", + Blend::Curvature => "curvature", + Blend::Chord => "chord", + Blend::Chamfer => "chamfer", + } + } +} + /// The second side of [`Brep::chamfer_sides`]. #[derive(Clone, Copy, Debug, PartialEq)] pub enum ChamferSecond { @@ -69,7 +95,9 @@ impl Brep { /// as one body, counted by [`Brep::solid_count`]. Tagged operands ([`Brep::tagged`]) give a /// result whose [`Brep::history`] says where each face and edge came from. pub fn boolean(&self, other: &Brep, op: BooleanOp) -> Result> { - core::boolean::boolean(self.core(), other.core(), op).map(|r| r.map(Brep::made_core)) + let r = core::boolean::boolean(self.core(), other.core(), op).map(|r| r.map(Brep::made_core)); + trace::maybe("boolean", &[self, other], &r, || json!({"op": op.word()})); + r } /// The union of `breps` in one call: they are unioned pairwise, level by level, so none takes @@ -81,21 +109,27 @@ impl Brep { return Err(Error::Argument); } let bodies: Vec<&core::Body> = breps.iter().map(|b| b.core()).collect(); - core::boolean::union_all(&bodies).map(Brep::made_core) + let r = core::boolean::union_all(&bodies).map(Brep::made_core); + trace::made("union_all", breps, &r, || json!({})); + r } /// `self` and `other`, sheets of planar faces on one plane ([`Brep::planar_faces`]), combined /// as regions of that plane by `op`: `None` when the result is empty. The result's edges are /// the inputs' own curves, split where they cross. pub fn boolean2d(&self, other: &Brep, op: BooleanOp) -> Result> { - core::boolean::boolean2d(self.core(), other.core(), op).map(|r| r.map(Brep::made_core)) + let r = core::boolean::boolean2d(self.core(), other.core(), op).map(|r| r.map(Brep::made_core)); + trace::maybe("boolean2d", &[self, other], &r, || json!({"op": op.word()})); + r } /// `self` with each face `tool` meets cut along the intersection, the pieces kept as faces of /// one body: `None` when the tool meets no face. Tagged operands give the pieces' history /// (each piece is a split of the face it was cut from). pub fn imprint(&self, tool: &Brep) -> Result> { - core::boolean::imprint(self.core(), tool.core()).map(|r| r.map(Brep::made_core)) + let r = core::boolean::imprint(self.core(), tool.core()).map(|r| r.map(Brep::made_core)); + trace::maybe("imprint", &[self, tool], &r, || json!({})); + r } /// The number of separate solid regions (voids not counted). @@ -106,14 +140,18 @@ impl Brep { /// The edges nearest to `edges` (each within `tolerance`), exactly those, rounded /// at `radius`. pub fn fillet(&self, edges: &[[f64; 3]], tolerance: f64, radius: f64) -> Result { - self.core_blend(Blend::Round, edges, &[radius], tolerance, &[], &[], core::blend::Corners::Filled) + let r = self.core_blend(Blend::Round, edges, &[radius], tolerance, &[], &[], core::blend::Corners::Filled); + trace::made("fillet", &[self], &r, || json!({"edges": trace::picks(self, PickKind::Edge, edges, tolerance), "radius": radius})); + r } /// The edges nearest to `edges` (each within `tolerance`), exactly those, cut by /// an equal-distance chamfer `distance` back along each face (measured at each edge's /// midpoint), with bevelled corners. pub fn chamfer(&self, edges: &[[f64; 3]], tolerance: f64, distance: f64) -> Result { - self.core_blend(Blend::Chamfer, edges, &[distance], tolerance, &[], &[], core::blend::Corners::Filled) + let r = self.core_blend(Blend::Chamfer, edges, &[distance], tolerance, &[], &[], core::blend::Corners::Filled); + trace::made("chamfer", &[self], &r, || json!({"edges": trace::picks(self, PickKind::Edge, edges, tolerance), "distance": distance})); + r } /// The edges nearest to `edges` (each within `tolerance`), exactly those, blended @@ -122,7 +160,14 @@ impl Brep { if edges.len() > MAX_POINTS || edges.is_empty() || sizes.len() != edges.len() { return Err(Error::Argument); } - self.core_blend(kind, edges, sizes, tolerance, &[], &[], core::blend::Corners::Filled) + let r = self.core_blend(kind, edges, sizes, tolerance, &[], &[], core::blend::Corners::Filled); + trace::made( + "blend", + &[self], + &r, + || json!({"kind": kind.word(), "edges": trace::picks(self, PickKind::Edge, edges, tolerance), "sizes": sizes}), + ); + r } /// [`Brep::blend`], with whether each point picked an edge that a larger round, overflowing @@ -132,7 +177,15 @@ impl Brep { if edges.len() > MAX_POINTS || edges.is_empty() || sizes.len() != edges.len() { return Err(Error::Argument); } - core::blend::blend_dropping(self.core(), kind, edges, sizes, tolerance).map(|(b, d)| (Brep::made_core(b), d)) + let r = core::blend::blend_dropping(self.core(), kind, edges, sizes, tolerance).map(|(b, d)| (Brep::made_core(b), d)); + trace::call( + "blend_dropping", + &[self], + &r, + |(b, _)| vec![b.id], + || json!({"kind": kind.word(), "edges": trace::picks(self, PickKind::Edge, edges, tolerance), "sizes": sizes}), + ); + r } /// [`Brep::blend`] with corner setbacks: each of `corners` picks the vertex nearest it @@ -158,7 +211,12 @@ impl Brep { { return Err(Error::Argument); } - self.core_blend(kind, edges, sizes, tolerance, corners, setbacks, core::blend::Corners::Filled) + let r = self.core_blend(kind, edges, sizes, tolerance, corners, setbacks, core::blend::Corners::Filled); + trace::made("blend_setback", &[self], &r, || { + json!({"kind": kind.word(), "edges": trace::picks(self, PickKind::Edge, edges, tolerance), "sizes": sizes, + "corners": trace::picks(self, PickKind::Vertex, corners, tolerance), "setbacks": setbacks}) + }); + r } /// [`Brep::blend_setback`] with each set-back corner left open: SMLib's patch between the @@ -184,8 +242,19 @@ impl Brep { { return Err(Error::Argument); } - core::blend::blend_holes(self.core(), kind, edges, sizes, tolerance, corners, setbacks, core::blend::Corners::Open) - .map(|(b, holes)| (Brep::made_core(b), holes)) + let r = core::blend::blend_holes(self.core(), kind, edges, sizes, tolerance, corners, setbacks, core::blend::Corners::Open) + .map(|(b, holes)| (Brep::made_core(b), holes)); + trace::call( + "blend_setback_open", + &[self], + &r, + |(b, _)| vec![b.id], + || { + json!({"kind": kind.word(), "edges": trace::picks(self, PickKind::Edge, edges, tolerance), "sizes": sizes, + "corners": trace::picks(self, PickKind::Vertex, corners, tolerance), "setbacks": setbacks}) + }, + ); + r } /// The one edge nearest to `edge` (within `tolerance`) rounded with a radius @@ -193,7 +262,14 @@ impl Brep { /// `radii` over the position along the edge, 0 at its end nearest `start` and 1 at the other /// (the edge curve's parameter scaled to 0..1). pub fn fillet_variable(&self, edge: [f64; 3], tolerance: f64, start: [f64; 3], degree: usize, knots: &[f64], radii: &[f64]) -> Result { - core::blend::fillet_variable(self.core(), edge, tolerance, start, degree, knots, radii).map(Brep::made_core) + let r = core::blend::fillet_variable(self.core(), edge, tolerance, start, degree, knots, radii).map(Brep::made_core); + trace::made( + "fillet_variable", + &[self], + &r, + || json!({"edge": trace::picks(self, PickKind::Edge, &[edge], tolerance), "start": start, "degree": degree, "knots": knots, "radii": radii}), + ); + r } /// The edges nearest to `edges` (each within `tolerance`), exactly those, cut by @@ -204,7 +280,15 @@ impl Brep { if edges.is_empty() || edges.len() > MAX_POINTS { return Err(Error::Argument); } - core::blend::chamfer_sides(self.core(), edges, tolerance, distance, second, first).map(Brep::made_core) + let r = core::blend::chamfer_sides(self.core(), edges, tolerance, distance, second, first).map(Brep::made_core); + trace::made("chamfer_sides", &[self], &r, || { + let second = match second { + ChamferSecond::Distance(d) => json!({"distance": d}), + ChamferSecond::Angle(a) => json!({"angle": a}), + }; + json!({"edges": trace::picks(self, PickKind::Edge, edges, tolerance), "distance": distance, "second": second, "first": first}) + }); + r } /// Hollowed to a wall of `thickness` (negative: inside the original surface, positive: @@ -213,7 +297,14 @@ impl Brep { /// whose walls would just meet (a hole exactly twice the wall from a side) is /// [`Error::WallsTouch`]. pub fn shell(&self, open_faces: &[[f64; 3]], tolerance: f64, thickness: f64) -> Result { - core::offset::shell(self.core(), open_faces, tolerance, thickness).map(Brep::made_core) + let r = core::offset::shell(self.core(), open_faces, tolerance, thickness).map(Brep::made_core); + trace::made( + "shell", + &[self], + &r, + || json!({"open_faces": trace::picks(self, PickKind::Face, open_faces, tolerance), "thickness": thickness}), + ); + r } /// The solid with the faces nearest to `faces` (each within `tolerance`) moved `distance` @@ -221,14 +312,18 @@ impl Brep { /// faces around them extended or trimmed to meet them. Faces moved in past all of the body /// behind them are [`Error::NoRoom`]. pub fn offset_faces(&self, faces: &[[f64; 3]], tolerance: f64, distance: f64) -> Result { - core::offset::offset_faces(self.core(), faces, tolerance, distance).map(Brep::made_core) + let r = core::offset::offset_faces(self.core(), faces, tolerance, distance).map(Brep::made_core); + trace::made("offset_faces", &[self], &r, || json!({"faces": trace::picks(self, PickKind::Face, faces, tolerance), "distance": distance})); + r } /// A sheet thickened into a solid by `thickness` along its surfaces' normals (negative: /// against them), or by half each way when `symmetric`, with ruled walls along its open /// edges. A closed solid is [`Error::Argument`]. pub fn thicken(&self, thickness: f64, symmetric: bool) -> Result { - core::offset::thicken(self.core(), thickness, symmetric).map(Brep::made_core) + let r = core::offset::thicken(self.core(), thickness, symmetric).map(Brep::made_core); + trace::made("thicken", &[self], &r, || json!({"thickness": thickness, "symmetric": symmetric})); + r } /// This body with its topological edges and vertices removed (SMLib's @@ -240,7 +335,9 @@ impl Brep { if keep.len() > MAX_POINTS { return Err(Error::Argument); } - core::merge::merge_faces(self.core(), keep, tolerance).map(Brep::made_core) + let r = core::merge::merge_faces(self.core(), keep, tolerance).map(Brep::made_core); + trace::made("merge_faces", &[self], &r, || json!({"keep": trace::picks(self, PickKind::Edge, keep, tolerance)})); + r } /// The flat faces nearest to `faces` (each within `tolerance`) tilted by `angle` radians @@ -260,7 +357,12 @@ impl Brep { if faces.is_empty() || faces.len() > MAX_POINTS { return Err(Error::Argument); } - core::offset::draft(self.core(), faces, tolerance, neutral_origin, neutral_normal, pull, angle).map(Brep::made_core) + let r = core::offset::draft(self.core(), faces, tolerance, neutral_origin, neutral_normal, pull, angle).map(Brep::made_core); + trace::made("draft", &[self], &r, || { + json!({"faces": trace::picks(self, PickKind::Face, faces, tolerance), "neutral_origin": neutral_origin, + "neutral_normal": neutral_normal, "pull": pull, "angle": angle}) + }); + r } /// [`Brep::blend`] and its kin on solidcore (`core::blend::blend`). diff --git a/crates/smlib/src/sheets.rs b/crates/smlib/src/sheets.rs index c7347995d..f3d2b1b6e 100644 --- a/crates/smlib/src/sheets.rs +++ b/crates/smlib/src/sheets.rs @@ -3,6 +3,9 @@ //! tagged inputs ([`Brep::tagged`]) give results whose [`Brep::history`] says where each face and //! edge came from. +use serde_json::json; + +use crate::trace; use crate::{Brep, Result, core}; impl Brep { @@ -10,12 +13,16 @@ impl Brep { /// outward where they close into a solid, else only made to agree across their shared edges; /// with whether the result is a closed solid. pub fn stitch(&self, tolerance: f64) -> Result<(Brep, bool)> { - core::merge::stitch(self.core(), tolerance).map(|(b, closed)| (Brep::made_core(b), closed)) + let r = core::merge::stitch(self.core(), tolerance).map(|(b, closed)| (Brep::made_core(b), closed)); + trace::call("stitch", &[self], &r, |(b, _)| vec![b.id], || json!({"tolerance": tolerance})); + r } /// This sheet with every face moved onto its surface's offset by `distance` along its normal /// (negative: against it). A closed solid is [`crate::Error::Argument`]. pub fn offset_sheet(&self, distance: f64) -> Result { - core::offset::offset_sheet(self.core(), distance).map(Brep::made_core) + let r = core::offset::offset_sheet(self.core(), distance).map(Brep::made_core); + trace::made("offset_sheet", &[self], &r, || json!({"distance": distance})); + r } } diff --git a/crates/smlib/src/split.rs b/crates/smlib/src/split.rs index cb0fd507c..7be2caeca 100644 --- a/crates/smlib/src/split.rs +++ b/crates/smlib/src/split.rs @@ -3,6 +3,9 @@ //! taken over its whole domain. use crate::ssi::Surface; +use serde_json::json; + +use crate::trace; use crate::{Brep, Error, Result, core}; impl Brep { @@ -15,7 +18,15 @@ impl Brep { return Err(Error::Argument); } let specs: Vec<&crate::ssi::Spec> = tools.iter().map(|t| t.spec()).collect(); - core::split::split_faces(self.core(), faces, &specs).map(|r| r.map(|(b, made)| (Brep::made_core(b), made))) + let r = core::split::split_faces(self.core(), faces, &specs).map(|r| r.map(|(b, made)| (Brep::made_core(b), made))); + trace::call( + "split_faces", + &[self], + &r, + |o| o.iter().map(|(b, _)| b.id).collect(), + || json!({"faces": faces, "tools": specs.iter().map(|s| trace::surface(s)).collect::>()}), + ); + r } /// The manifold solid `self` cut in two by `tool` (whose domain must reach past it): the @@ -24,7 +35,15 @@ impl Brep { /// leaves both parts the whole body, with no face from the tool (tag `self` to tell); `None` /// when a side is left empty. pub fn split_body(&self, tool: &Surface) -> Result> { - core::split::split_body(self.core(), tool.spec()).map(|r| r.map(|(b, f)| (Brep::made_core(b), Brep::made_core(f)))) + let r = core::split::split_body(self.core(), tool.spec()).map(|r| r.map(|(b, f)| (Brep::made_core(b), Brep::made_core(f)))); + trace::call( + "split_body", + &[self], + &r, + |o| o.iter().flat_map(|(b, f)| [b.id, f.id]).collect(), + || json!({"tool": trace::surface(tool.spec())}), + ); + r } /// The manifold solid `self` cut in two by the sheet `sheet` as it is (its faces joined edge @@ -33,6 +52,8 @@ impl Brep { /// normal and the part in front. A `sheet` that is no sheet, or a `face` it does not have, is /// [`Error::Argument`]. pub fn split_body_by_sheet(&self, sheet: &Brep, face: usize) -> Result> { - core::split::split_body_by_sheet(self.core(), sheet.core(), face).map(|r| r.map(|(b, f)| (Brep::made_core(b), Brep::made_core(f)))) + let r = core::split::split_body_by_sheet(self.core(), sheet.core(), face).map(|r| r.map(|(b, f)| (Brep::made_core(b), Brep::made_core(f)))); + trace::call("split_body_by_sheet", &[self, sheet], &r, |o| o.iter().flat_map(|(b, f)| [b.id, f.id]).collect(), || json!({"face": face})); + r } } diff --git a/crates/smlib/src/sweeps.rs b/crates/smlib/src/sweeps.rs index 819f5e2e3..8f7b01891 100644 --- a/crates/smlib/src/sweeps.rs +++ b/crates/smlib/src/sweeps.rs @@ -6,6 +6,9 @@ //! With `cap`, the profile's loops close the ends and the result must be a closed manifold solid //! ([`Error::NotSolid`] otherwise); without, it is the swept sheet. +use serde_json::json; + +use crate::trace; use crate::{Brep, Result, core}; /// A 3D curve of a profile, path or section. @@ -105,7 +108,9 @@ impl Brep { /// A sheet of planar faces bounded by `loops`: closed loops of curves in one plane, in any /// order, nested as outer loops, their holes and islands in the holes. pub fn planar_faces(loops: &[Curve]) -> Result { - core::sweep::planar_faces(loops).map(Brep::made_core) + let r = core::sweep::planar_faces(loops).map(Brep::made_core); + trace::made("planar_faces", &[], &r, || json!({"loops": trace::curves(loops)})); + r } /// The planar `profile` extruded `distance` along the unit `direction` (square to its plane). @@ -116,20 +121,43 @@ impl Brep { /// [`DraftCorner::Curvature`], a curve of a profile with a sharp corner is neither a line nor /// an arc). pub fn extrude(profile: &[Curve], direction: [f64; 3], distance: f64, draft: f64, corner: DraftCorner, cap: bool) -> Result { - core::sweep::extrude(profile, direction, distance, draft, corner, cap).map(Brep::made_core) + let r = core::sweep::extrude(profile, direction, distance, draft, corner, cap).map(Brep::made_core); + trace::made("extrude", &[], &r, || { + let corner = match corner { + DraftCorner::Curvature => "curvature", + DraftCorner::Tangent => "tangent", + DraftCorner::Round => "round", + }; + json!({"profile": trace::curves(profile), "direction": direction, "distance": distance, "draft": draft, "corner": corner, "cap": cap}) + }); + r } /// The planar `profile` turned `angle` radians (0 < angle <= 2 pi) right-handed about the /// axis through `origin` along `direction`, in the profile's plane (touching it at most). pub fn revolve(profile: &[Curve], origin: [f64; 3], direction: [f64; 3], angle: f64, cap: bool) -> Result { - core::sweep::revolve(profile, origin, direction, angle, cap).map(Brep::made_core) + let r = core::sweep::revolve(profile, origin, direction, angle, cap).map(Brep::made_core); + trace::made( + "revolve", + &[], + &r, + || json!({"profile": trace::curves(profile), "origin": origin, "direction": direction, "angle": angle, "cap": cap}), + ); + r } /// `profile` swept along `path` (end-to-end curves), carried with the path's start: the /// profile keeps its place relative to it. The walls are B-spline approximations, one per /// stretch of profile between tangent breaks. pub fn sweep(profile: &[Curve], path: &[Curve], frame: SweepFrame, cap: bool) -> Result { - core::sweep::sweep_placed(profile, path, frame, cap).map(|(b, _)| Brep::made_core(b)) + let r = core::sweep::sweep_placed(profile, path, frame, cap).map(|(b, _)| Brep::made_core(b)); + trace::made( + "sweep", + &[], + &r, + || json!({"profile": trace::curves(profile), "path": trace::curves(path), "frame": frame_word(frame), "cap": cap}), + ); + r } /// [`Brep::sweep`], with where the sweep placed the profile: the rigid motions carrying the @@ -138,7 +166,15 @@ impl Brep { /// out from point to point along the path); a sweep that carries on from where this one left /// the profile starts from the last. pub fn sweep_placed(profile: &[Curve], path: &[Curve], frame: SweepFrame, cap: bool) -> Result<(Brep, [Affine; 3])> { - core::sweep::sweep_placed(profile, path, frame, cap).map(|(b, m)| (Brep::made_core(b), m)) + let r = core::sweep::sweep_placed(profile, path, frame, cap).map(|(b, m)| (Brep::made_core(b), m)); + trace::call( + "sweep", + &[], + &r, + |(b, _)| vec![b.id], + || json!({"profile": trace::curves(profile), "path": trace::curves(path), "frame": frame_word(frame), "cap": cap}), + ); + r } /// The `curves` projected along `direction` (pointing from them toward the face) onto the @@ -173,27 +209,52 @@ impl Brep { /// A loft (skin) through `sections` (at least two closed loops), `degree` (1 ruled, 2 or 3) /// across them. pub fn loft(sections: &[Vec], degree: u32, cap: bool) -> Result { - core::sweep::loft(sections, degree, cap).map(Brep::made_core) + let r = core::sweep::loft(sections, degree, cap).map(Brep::made_core); + trace::made( + "loft", + &[], + &r, + || json!({"sections": sections.iter().map(|s| trace::curves(s)).collect::>(), "degree": degree, "cap": cap}), + ); + r } /// The planar `profile` placed by each of `stations` in turn (2…[`MAX_STATIONS`], each /// section exactly the profile's image), its walls skinned through the sections, cubic /// across them: one wall per profile curve. `ends` says how the ends close. pub fn sweep_stations(profile: &[Curve], stations: &[Affine], ends: Ends) -> Result { - core::sweep::sweep_stations(profile, stations, ends).map(Brep::made_core) + let r = core::sweep::sweep_stations(profile, stations, ends).map(Brep::made_core); + trace::made("sweep_stations", &[], &r, || { + let ends = match ends { + Ends::Open => "open", + Ends::Capped => "capped", + Ends::Joined => "joined", + }; + json!({"profile": trace::curves(profile), "stations": stations.iter().map(trace::affine).collect::>(), "ends": ends}) + }); + r } /// A loft from the planar `section` (one closed loop of lines, in order) to the point `apex` /// off its plane: a pyramid, its base the section and a triangle on each line. pub fn loft_to_point(section: &[Curve], apex: [f64; 3]) -> Result { - core::sweep::loft_to_point(section, apex).map(Brep::made_core) + let r = core::sweep::loft_to_point(section, apex).map(Brep::made_core); + trace::made("loft_to_point", &[], &r, || json!({"section": trace::curves(section), "apex": apex})); + r } /// `profile` carried by a screw motion about the axis through `origin` along `direction`: /// `turns` right-handed turns about it, rising `pitch` (signed) per turn, capped at both ends, /// its walls within about `tolerance` of the true screw surfaces. pub fn screw_sweep(profile: &[Curve], origin: [f64; 3], direction: [f64; 3], pitch: f64, turns: f64, tolerance: f64) -> Result { - core::sweep::screw_sweep(profile, origin, direction, pitch, turns, None, tolerance).map(Brep::made_core) + let r = core::sweep::screw_sweep(profile, origin, direction, pitch, turns, None, tolerance).map(Brep::made_core); + trace::made( + "screw_sweep", + &[], + &r, + || json!({"profile": trace::curves(profile), "origin": origin, "direction": direction, "pitch": pitch, "turns": turns, "tolerance": tolerance}), + ); + r } /// [`screw_sweep`](Self::screw_sweep) with the profile moved off the axis as it turns: at turn @@ -211,7 +272,20 @@ impl Brep { law: &[(f64, f64)], tolerance: f64, ) -> Result { - core::sweep::screw_sweep(profile, origin, direction, pitch, turns, Some(law), tolerance).map(Brep::made_core) + let r = core::sweep::screw_sweep(profile, origin, direction, pitch, turns, Some(law), tolerance).map(Brep::made_core); + trace::made("screw_sweep", &[], &r, || { + json!({"profile": trace::curves(profile), "origin": origin, "direction": direction, "pitch": pitch, "turns": turns, + "law": law, "tolerance": tolerance}) + }); + r + } +} + +/// A sweep frame's name in a trace. +fn frame_word(f: SweepFrame) -> &'static str { + match f { + SweepFrame::Follow => "follow", + SweepFrame::Translate => "translate", } } diff --git a/crates/smlib/src/trace.rs b/crates/smlib/src/trace.rs new file mode 100644 index 000000000..68e61b1e0 --- /dev/null +++ b/crates/smlib/src/trace.rs @@ -0,0 +1,225 @@ +//! A trace of the operations a run makes on this crate's bodies, for the kernel rig's corpus +//! (`solidcraft-cli kernel-cases --export`): each body taken from a record (its record), each +//! operation (its name, its operand bodies, its parameters with every pick by points also given +//! as the topology index it picked, and the bodies it made or why it failed), and each body +//! written back as a record. Off unless [`start`]ed; while off it costs an atomic load per +//! operation. +//! +//! Bodies are named by a number each gets when it is made ([`crate::Brep`]'s id), so a trace +//! says which operation's output went into which operation, and which record each became. + +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::{Mutex, PoisonError}; + +use serde_json::{Value, json}; +use solidcraft_geom::brep as b; + +use crate::sweeps::{Affine, Curve}; +use crate::{Brep, Result}; + +static ON: AtomicBool = AtomicBool::new(false); +static NEXT: AtomicU64 = AtomicU64::new(1); +static LOG: Mutex> = Mutex::new(Vec::new()); + +/// One thing a traced run did, in the order it happened. +#[derive(Clone, Debug)] +pub enum Event { + /// Body `id` was made from `record` ([`Brep::from_record`]). + Import { id: u64, record: Box, trusted: bool, keeping_splines: bool }, + /// An operation: its name, its operand bodies, its parameters, and the bodies it made + /// (`Ok`) or its error's message (`Err`). + Call { op: &'static str, inputs: Vec, params: Value, outputs: std::result::Result, String> }, + /// Body `id` was written as `record` ([`Brep::to_native`] and its kin). + Export { id: u64, record: Box }, +} + +/// Starts a trace, dropping anything an earlier one left. +pub fn start() { + LOG.lock().unwrap_or_else(PoisonError::into_inner).clear(); + ON.store(true, Ordering::SeqCst); +} + +/// Stops the trace and hands over what it recorded. +pub fn finish() -> Vec { + ON.store(false, Ordering::SeqCst); + std::mem::take(&mut *LOG.lock().unwrap_or_else(PoisonError::into_inner)) +} + +/// The names of solidcore's upstream fixes this crate turns on: the fix set every traced +/// operation ran with. +pub fn fixes() -> Vec<&'static str> { + crate::core::fixes().flags().filter(|&(_, on)| on).map(|(name, _)| name).collect() +} + +pub(crate) fn on() -> bool { + ON.load(Ordering::Relaxed) +} + +/// A new body's number. +pub(crate) fn next_id() -> u64 { + NEXT.fetch_add(1, Ordering::Relaxed) +} + +fn push(e: Event) { + LOG.lock().unwrap_or_else(PoisonError::into_inner).push(e); +} + +pub(crate) fn imported(r: &Result, record: &b::Brep, trusted: bool, keeping_splines: bool) { + if !on() { + return; + } + if let Ok(body) = r { + push(Event::Import { id: body.id, record: Box::new(record.clone()), trusted, keeping_splines }); + } +} + +pub(crate) fn exported(body: &Brep, record: &b::Brep) { + if on() { + push(Event::Export { id: body.id, record: Box::new(record.clone()) }); + } +} + +/// Records operation `op` on `inputs`, whose result `r` made the bodies `outputs` reads from it. +/// `params` is only built while tracing, after the operation ran. +pub(crate) fn call(op: &'static str, inputs: &[&Brep], r: &Result, outputs: impl FnOnce(&T) -> Vec, params: impl FnOnce() -> Value) { + if !on() { + return; + } + let outputs = match r { + Ok(v) => Ok(outputs(v)), + Err(e) => Err(e.to_string()), + }; + push(Event::Call { op, inputs: inputs.iter().map(|b| b.id).collect(), params: params(), outputs }); +} + +/// [`call`] for an operation that makes one body. +pub(crate) fn made(op: &'static str, inputs: &[&Brep], r: &Result, params: impl FnOnce() -> Value) { + call(op, inputs, r, |b| vec![b.id], params); +} + +/// [`call`] for an operation that may make no body. +pub(crate) fn maybe(op: &'static str, inputs: &[&Brep], r: &Result>, params: impl FnOnce() -> Value) { + call(op, inputs, r, |b| b.iter().map(|b| b.id).collect(), params); +} + +/// What a pick by points is of. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum PickKind { + Edge, + Face, + Vertex, +} + +/// What one point picked: the entity's index in the body's list of its kind, and where it is +/// (an edge's end points, a vertex's point), or why it picked none. +#[derive(Clone, Debug, PartialEq)] +pub struct Pick { + pub index: Option, + pub at: Vec<[f64; 3]>, + pub miss: Option<&'static str>, +} + +impl Pick { + pub(crate) fn missed(why: &'static str) -> Pick { + Pick { index: None, at: Vec::new(), miss: Some(why) } + } +} + +/// The points of a pick on `body` and what each picked: `{"points", "tolerance", "picked"}`, +/// each of `picked` `{"index", "at"}` or `{"miss"}`. +pub(crate) fn picks(body: &Brep, kind: PickKind, points: &[[f64; 3]], tolerance: f64) -> Value { + let picked = match crate::core::picked::picks(body.core(), kind, points, tolerance) { + Ok(p) => Value::Array( + p.into_iter() + .map(|p| match (p.index, p.miss) { + (Some(i), _) => json!({"index": i, "at": p.at}), + (None, why) => json!({"miss": why.unwrap_or("none")}), + }) + .collect(), + ), + Err(e) => json!({"error": e.to_string()}), + }; + let kind = match kind { + PickKind::Edge => "edge", + PickKind::Face => "face", + PickKind::Vertex => "vertex", + }; + json!({"kind": kind, "points": points, "tolerance": tolerance, "picked": picked}) +} + +/// A profile, path or section curve as data. +pub(crate) fn curve(c: &Curve) -> Value { + match c { + Curve::Line { start, end } => json!({"line": {"start": start, "end": end}}), + Curve::Arc { center, axis, reference, radius, start, end } => { + json!({"arc": {"center": center, "axis": axis, "reference": reference, "radius": radius, "start": start, "end": end}}) + } + Curve::Nurbs { degree, points, knots } => json!({"nurbs": {"degree": degree, "points": points, "knots": knots}}), + } +} + +pub(crate) fn curves(cs: &[Curve]) -> Value { + Value::Array(cs.iter().map(curve).collect()) +} + +pub(crate) fn affine(a: &Affine) -> Value { + json!({"m": a.m, "t": a.t}) +} + +/// A split tool's surface as data. +pub(crate) fn surface(s: &crate::ssi::Spec) -> Value { + use crate::ssi::Spec; + match s { + Spec::Plane { origin, xaxis, yaxis, u, v } => json!({"plane": {"origin": origin, "xaxis": xaxis, "yaxis": yaxis, "u": u, "v": v}}), + Spec::Cylinder { origin, xaxis, yaxis, radius, angle_deg, height } => { + json!({"cylinder": {"origin": origin, "xaxis": xaxis, "yaxis": yaxis, "radius": radius, "angle_deg": angle_deg, "height": height}}) + } + Spec::Cone { apex, xaxis, yaxis, half_angle_deg, height } => { + json!({"cone": {"apex": apex, "xaxis": xaxis, "yaxis": yaxis, "half_angle_deg": half_angle_deg, "height": height}}) + } + Spec::Sphere { center, xaxis, yaxis, radius, lon_deg, lat_deg } => { + json!({"sphere": {"center": center, "xaxis": xaxis, "yaxis": yaxis, "radius": radius, "lon_deg": lon_deg, "lat_deg": lat_deg}}) + } + Spec::Torus { center, xaxis, yaxis, major_radius, minor_radius, major_deg, minor_deg } => json!({"torus": { + "center": center, "xaxis": xaxis, "yaxis": yaxis, "major_radius": major_radius, "minor_radius": minor_radius, + "major_deg": major_deg, "minor_deg": minor_deg}}), + Spec::Nurbs { degree, nu, nv, knots_u, knots_v, points, weights } => json!({"nurbs": { + "degree": degree, "nu": nu, "nv": nv, "knots_u": knots_u, "knots_v": knots_v, "points": points, "weights": weights}}), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A traced fillet names its operand, its output and the edge its point picked, by index and + /// end points; the record its output was written as and the body read back from it are + /// traced under their own numbers. Other tests' operations may land in the trace too: only + /// these bodies' events are read. + #[test] + fn a_fillet_is_traced_with_its_pick_and_records() { + let block = Brep::make_box([0.0; 3], [10.0, 20.0, 30.0]).unwrap(); + start(); + let round = block.fillet(&[[10.0, 10.0, 30.0]], 1e-3, 2.0).unwrap(); + let record = round.to_native().unwrap(); + let again = Brep::from_record(&record, true).unwrap(); + let events = finish(); + let call = events + .iter() + .find_map(|e| match e { + Event::Call { op: "fillet", inputs, params, outputs } if inputs == &vec![block.id] => Some((params, outputs)), + _ => None, + }) + .unwrap(); + assert_eq!(call.1, &Ok(vec![round.id])); + let picked = &call.0["edges"]["picked"][0]; + assert!(picked["index"].as_u64().is_some(), "{picked}"); + // The point (10, 10, 30) is on the top edge x = 10, z = 30, from y = 0 to y = 20. + let mut ends: Vec> = picked["at"].as_array().unwrap().iter().map(|p| serde_json::from_value(p.clone()).unwrap()).collect(); + ends.sort_by(|a, b| a[1].total_cmp(&b[1])); + assert_eq!(ends, vec![vec![10.0, 0.0, 30.0], vec![10.0, 20.0, 30.0]]); + assert!(events.iter().any(|e| matches!(e, Event::Export { id, record: r } if *id == round.id && **r == record))); + assert!(events.iter().any(|e| matches!(e, Event::Import { id, record: r, trusted: true, .. } if *id == again.id && **r == record))); + assert_ne!(again.id, round.id); + } +} diff --git a/crates/smlib/src/trim.rs b/crates/smlib/src/trim.rs index 1195f6b07..601f40e4b 100644 --- a/crates/smlib/src/trim.rs +++ b/crates/smlib/src/trim.rs @@ -1,6 +1,9 @@ //! Untrim and extend of a sheet's faces. Each returns a new body and leaves its input unchanged; //! faces and holes are chosen by points, as [`crate::ops`] chooses them. +use serde_json::json; + +use crate::trace; use crate::{Brep, Result, core}; /// How [`Brep::extend_sheet`] continues a surface past its side. @@ -33,7 +36,14 @@ impl Brep { Holes::All => None, Holes::At(at) => Some(at), }; - core::trim::untrim_face(self.core(), point, tolerance, holes, outer, in_place).map(Brep::made_core) + let r = core::trim::untrim_face(self.core(), point, tolerance, holes, outer, in_place).map(Brep::made_core); + trace::made( + "untrim_face", + &[self], + &r, + || json!({"face": trace::picks(self, trace::PickKind::Face, &[point], tolerance), "holes": holes, "outer": outer, "in_place": in_place}), + ); + r } /// The one-face sheet `self`, whose face is its surface's whole parameter box, continued @@ -41,7 +51,16 @@ impl Brep { /// `how` says: SMLib's extended surface over the grown box, the face and its sides carrying /// the tags of those they continue. A sheet of another shape is [`crate::Error::Unsupported`]. pub fn extend_sheet(&self, point: [f64; 3], tolerance: f64, distance: f64, how: Continuation) -> Result { - core::trim::extend_sheet(self.core(), point, tolerance, distance, how).map(Brep::made_core) + let r = core::trim::extend_sheet(self.core(), point, tolerance, distance, how).map(Brep::made_core); + trace::made("extend_sheet", &[self], &r, || { + let how = match how { + Continuation::Natural => "natural", + Continuation::Tangent => "tangent", + Continuation::Curvature => "curvature", + }; + json!({"edge": trace::picks(self, trace::PickKind::Edge, &[point], tolerance), "distance": distance, "how": how}) + }); + r } /// A point inside each face, in SMLib's face order: on the face's surface, inside its trims diff --git a/xtask/src/kernel_cases.rs b/xtask/src/kernel_cases.rs index 9a1b7db7c..3a600f8da 100644 --- a/xtask/src/kernel_cases.rs +++ b/xtask/src/kernel_cases.rs @@ -35,6 +35,9 @@ pub(crate) struct Options { /// `--validation MODE`: when SMLib's full check runs on the cases' features (each_step, /// each_feature, off; the CLI's default, each_feature, when not given). validation: Option, + /// `--export DIR`: each case's corpus entry for solidcore's rig into DIR (`solidcraft-cli + /// kernel-cases --export`), then DIR/manifest.json; no report is written. + pub(crate) export: Option, } /// The run's options; `out` is its output folder unless `--out` gives one. @@ -54,7 +57,11 @@ pub(crate) fn options(rest: &[&str], out: &str) -> Result { if let Some(v) = validation.as_deref().filter(|v| !["each_step", "each_feature", "off"].contains(v)) { return Err(format!("--validation must be each_step, each_feature or off, not `{v}`")); } - Ok(Options { cli: arg("--cli"), area: arg("--area"), timeout: Duration::from_secs(secs), jobs, out, known_failing, validation }) + let export = arg("--export").map(|p| { + let p = PathBuf::from(p); + if p.is_absolute() { p } else { root().join(p) } + }); + Ok(Options { cli: arg("--cli"), area: arg("--area"), timeout: Duration::from_secs(secs), jobs, out, known_failing, validation, export }) } /// Every case's full result as JSON, into the run's output folder. @@ -151,6 +158,11 @@ fn run_case(o: &Options, case: &Path) -> Value { if let Some(v) = &o.validation { cmd.arg(format!("--validation={v}")); } + if let Some(d) = &o.export { + let mut export = std::ffi::OsString::from("--export="); + export.push(d); + cmd.arg(export); + } cmd.arg(&rel).stdout(Stdio::piped()).stderr(Stdio::piped()); let base = |status: &str, error: String| { let area = case.parent().and_then(|p| p.file_name()).map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); @@ -405,6 +417,10 @@ pub fn run(rest: &[&str]) -> Result<(), String> { } let results = run_all(&o, &list); save_results(&o, &results)?; + if let Some(d) = &o.export { + println!("{}", crate::kernel_corpus::manifest(d, &results, None)?); + return Ok(()); + } if let Some(file) = &o.known_failing { // A gate run grades the run against its baseline, and writes no report; its results.json // is still saved above. diff --git a/xtask/src/kernel_corpus.rs b/xtask/src/kernel_corpus.rs new file mode 100644 index 000000000..82b1af501 --- /dev/null +++ b/xtask/src/kernel_corpus.rs @@ -0,0 +1,172 @@ +//! `cargo xtask kernel-cases [--ladder] --export DIR`: the kernel rig as a corpus for solidcore's +//! own rig. Each case's process writes its entry (`solidcraft-cli kernel-cases --export`, +//! `DIR//.json`: the kernel operations that made its result, their operand records, +//! the fix set, its expected values and the rig's result). This writes what that leaves out: +//! an entry for each case whose process left none (a hang or a crash, so no trace), each ladder +//! case's golden, and `DIR/manifest.json`, the counts per area and every case that is not +//! expressible as kernel input, with its reasons. + +use std::collections::BTreeMap; +use std::path::Path; + +use serde_json::{Value, json}; + +use crate::kernel_cases::read; +use crate::root; + +/// The kind of reason a case is not expressible, for the counts per area. +fn reason_kind(why: &str) -> &'static str { + if why.starts_with("SolidCraft has no command") { + "SolidCraft has no command for it yet" + } else if why.starts_with("graded on what SolidCraft computes") { + "graded on SolidCraft's own measures (continuity, mesh, files, command answers, construction, flat pattern)" + } else if why.contains("graded merged across analytic surfaces") { + "counts graded merged across analytic surfaces (SolidCraft's merged_topology)" + } else if why.starts_with("no kernel operation ran before the failure") || why.starts_with("the failure is SolidCraft's") { + "refused or failed in SolidCraft, not in a kernel operation" + } else if why.contains("is no kernel operation's output nor an operand") { + "a final body built or changed by SolidCraft (its own geometry or a change to the kernel's record)" + } else if why.starts_with("no kernel operation made a body") || why.starts_with("the case ends with no body") { + "no kernel operation makes the result" + } else if why.starts_with("the rig's run") { + "the rig's run hung or crashed (no trace)" + } else { + "other" + } +} + +fn write_json(path: &Path, v: &Value, pretty: bool) -> Result<(), String> { + let text = if pretty { serde_json::to_string_pretty(v) } else { serde_json::to_string(v) }.map_err(|e| e.to_string())?; + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; + } + std::fs::write(path, text).map_err(|e| format!("{}: {e}", path.display())) +} + +/// What `git args` prints in this checkout. +fn git(args: &[&str]) -> String { + std::process::Command::new("git") + .current_dir(root()) + .args(args) + .output() + .ok() + .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()) + .unwrap_or_default() +} + +/// The solidcore rev SolidCraft is pinned to (the workspace `Cargo.toml`). +fn solidcore_pin() -> String { + let text = std::fs::read_to_string(root().join("Cargo.toml")).unwrap_or_default(); + text.lines() + .find(|l| l.starts_with("solidcore =")) + .and_then(|l| l.split("rev = \"").nth(1)) + .and_then(|r| r.split('"').next()) + .unwrap_or_default() + .to_string() +} + +/// Completes the corpus in `dir` after a run with `results` (each with its `path` under +/// `tests/kernel-cases/`); `golden`, the ladder's, for a ladder run. Returns the summary. +pub(crate) fn manifest(dir: &Path, results: &[Value], golden: Option<&Value>) -> Result { + let base = root().join("tests/kernel-cases"); + for r in results { + let rel = r["path"].as_str().unwrap_or(""); + let Some((area, id)) = rel.rsplit_once('/') else { continue }; + let file = dir.join(area).join(format!("{id}.json")); + let mut entry = read(&file); + if entry.is_null() { + // The case's process left no entry: it hung or crashed before writing one. + let want = read(&base.join(rel).join("expected.json")); + entry = json!({ + "case": id, "area": area, "sources": want["sources"], "what": want["what"], + "expressible": false, + "why_not": [format!("the rig's run {}: {}", r["status"].as_str().unwrap_or("?"), r["error"].as_str().unwrap_or(""))], + "expected": want, + "rig": {"status": r["status"], "error": r["error"]}, + "calls": [], "bodies": {}, "result": [], "traced_ops": [], "status": r["status"], + }); + } + if let Some(g) = golden.and_then(|g| g["cases"].get(rel)) { + // The golden's status and counts; never its time. + entry["golden"] = json!({"status": g["status"], "measured": g["measured"], "unstable": g.get("unstable")}); + } + if let Some(e) = r.get("export_error") { + entry["export_error"] = e.clone(); + } + write_json(&file, &entry, false)?; + } + + // Every entry in the corpus, this run's or an earlier one's. + let mut areas: BTreeMap = BTreeMap::new(); + let mut not: Vec = Vec::new(); + let mut stack = vec![dir.to_path_buf()]; + let mut files = Vec::new(); + while let Some(d) = stack.pop() { + for e in std::fs::read_dir(&d).map_err(|e| format!("{}: {e}", d.display()))?.flatten() { + let p = e.path(); + if p.is_dir() { + stack.push(p); + } else if p.extension().is_some_and(|x| x == "json") && p.parent() != Some(dir) { + files.push(p); + } + } + } + files.sort(); + for f in files { + let e = read(&f); + let area = e["area"].as_str().unwrap_or("?").to_string(); + let a = areas.entry(area.clone()).or_insert_with(|| json!({"cases": 0, "expressible": 0, "not_expressible": 0, "chain": {}, "reasons": {}})); + a["cases"] = json!(a["cases"].as_u64().unwrap_or(0) + 1); + if e["expressible"].as_bool() == Some(true) { + a["expressible"] = json!(a["expressible"].as_u64().unwrap_or(0) + 1); + let n = e["calls"].as_array().map_or(0, Vec::len).to_string(); + a["chain"][&n] = json!(a["chain"][&n].as_u64().unwrap_or(0) + 1); + } else { + a["not_expressible"] = json!(a["not_expressible"].as_u64().unwrap_or(0) + 1); + let why: Vec<&str> = e["why_not"].as_array().into_iter().flatten().filter_map(Value::as_str).collect(); + let mut kinds: Vec<&str> = why.iter().map(|w| reason_kind(w)).collect(); + kinds.sort(); + kinds.dedup(); + for k in kinds { + a["reasons"][k] = json!(a["reasons"][k].as_u64().unwrap_or(0) + 1); + } + not.push(json!({"case": format!("{area}/{}", e["case"].as_str().unwrap_or("?")), "why": why})); + } + } + let m = json!({ + "solidcraft": git(&["rev-parse", "HEAD"]), + "solidcore": solidcore_pin(), + "fixes": "fixes.json", + "areas": areas, + "not_expressible": not, + }); + write_json(&dir.join("manifest.json"), &m, true)?; + let mut text = String::from("area cases expressible not\n"); + for (area, a) in &areas { + text += &format!( + "{area:<20} {:>5} {:>11} {:>3}\n", + a["cases"].as_u64().unwrap_or(0), + a["expressible"].as_u64().unwrap_or(0), + a["not_expressible"].as_u64().unwrap_or(0) + ); + } + Ok(text) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_reason_the_export_gives_has_its_kind() { + for (why, kind) in [ + ("graded on what SolidCraft computes: continuity", "graded on SolidCraft's own measures"), + ("faces graded merged across analytic surfaces (6), the records have 8", "counts graded merged"), + ("the failure is SolidCraft's, not a kernel operation's: x", "refused or failed in SolidCraft"), + ("body \"Body1\" is no kernel operation's output nor an operand: SolidCraft built it", "a final body built or changed"), + ("the rig's run hang: no result", "the rig's run hung"), + ] { + assert!(reason_kind(why).starts_with(kind), "{why}: {}", reason_kind(why)); + } + } +} diff --git a/xtask/src/kernel_ladder.rs b/xtask/src/kernel_ladder.rs index b0c2af4c1..538be8b68 100644 --- a/xtask/src/kernel_ladder.rs +++ b/xtask/src/kernel_ladder.rs @@ -295,6 +295,10 @@ pub fn run(rest: &[&str]) -> Result<(), String> { let list: Vec = tiers.iter().flat_map(|t| t.cases.iter().map(|c| base.join(c))).collect(); let all = run_all(&o, &list); save_results(&o, &all)?; + if let Some(d) = &o.export { + println!("{}", crate::kernel_corpus::manifest(d, &all, Some(&read(&golden_path())))?); + return Ok(()); + } let results: BTreeMap = all.into_iter().map(|r| (rel_of(&r), r)).collect(); let golden = read(&golden_path()); if gate_run { diff --git a/xtask/src/main.rs b/xtask/src/main.rs index d0419c82e..126965ccf 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -11,6 +11,7 @@ mod features; mod i18n; mod icons; mod kernel_cases; +mod kernel_corpus; mod kernel_ladder; mod known_failing; mod layers; @@ -49,7 +50,7 @@ commands: replay the reference tests (tests/oracle/*/recipe.json), compare with each case's expected.json and write docs/oracle.md (--cli: a solidcraft-cli already built, instead of cargo run --release) - kernel-cases [--area AREA] [--cli BIN] [--timeout S] [--jobs N] [--out DIR] [--known-failing FILE] [--validation MODE] [--coverage] + kernel-cases [--area AREA] [--cli BIN] [--timeout S] [--jobs N] [--out DIR] [--known-failing FILE] [--validation MODE] [--coverage] [--export DIR] the kernel regression rig: run every case in tests/kernel-cases/// on solidcore (one process each, killed after --timeout, default 180 s), grade it against the case's own answer and write docs/kernel-cases.md; DIR (default @@ -70,6 +71,10 @@ commands: --coverage: instead of running cases, fail on a registry command (the CLI's `commands`, planned ones aside) that no recipe.json under tests/kernel-cases/ uses and tests/kernel-cases/EXEMPT does not list, and on a stale EXEMPT line + --export DIR: instead of the report, the corpus for solidcore's rig: each case's + kernel operations, operand records, fix set and expected values in + DIR//.json and DIR/manifest.json, every case not expressible as + kernel input listed with its reasons; with --ladder too known-failing --junit PATH... [--list FILE] grade a nextest run's junit report (a file, or every .xml under a directory) against the tests known to fail today on the default kernel,