From 8dfdcefadb2bb6b5aa8849ec1f30eefc0f029c56 Mon Sep 17 00:00:00 2001 From: Brett Lawson Date: Thu, 29 Oct 2026 03:45:27 +0100 Subject: [PATCH] smlib: SOLIDCRAFT_KERNEL_ROUTES moves single families; the rig scores one at a time SOLIDCRAFT_KERNEL_ROUTES==[,...] applies on top of SOLIDCRAFT_KERNEL or the preference, read once; bad, empty, overlong or surplus items are logged and ignored, and `file` stays on SMLib. diagnostics.kernel reports it as `overrides`. `cargo xtask kernel-cases --kernel both --route =solidcore[,...]` runs the solidcore side as SMLib with those families on solidcore, labelled solidcore[ops], into /solidcore-/. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SdcgExNEwfR9CX2RAzp2Pt --- crates/engine/src/cmd/diagnostics.rs | 12 +- crates/engine/src/schema/diagnostics_tests.rs | 5 +- crates/smlib/src/route.rs | 207 +++++++++++++++++- tests/kernel-cases/README.md | 12 + xtask/src/kernel_cases.rs | 151 +++++++++++-- xtask/src/main.rs | 6 +- 6 files changed, 360 insertions(+), 33 deletions(-) diff --git a/crates/engine/src/cmd/diagnostics.rs b/crates/engine/src/cmd/diagnostics.rs index 14e0e5446..5837ca9ee 100644 --- a/crates/engine/src/cmd/diagnostics.rs +++ b/crates/engine/src/cmd/diagnostics.rs @@ -41,7 +41,8 @@ pub static COMMANDS: &[CommandSpec] = &[ CommandSpec::new("diagnostics.kernel", "Geometry Kernel", kernel).icon("bug_report").noundo().params( "choice?: routed | smlib | solidcore (sets which kernel answers: routed = each operation on the kernel the routing \ table gives it, smlib = the C++ SMLib kernel for all, solidcore = its Rust port for all; bodies already computed \ - keep their results). SOLIDCRAFT_KERNEL, when set, wins over it. Returns {choice, environment, routes: [{op, \ + keep their results). SOLIDCRAFT_KERNEL, when set, wins over it, and SOLIDCRAFT_KERNEL_ROUTES (op=backend,...) \ + moves single operations on top of both. Returns {choice, environment, overrides: {op: backend}, routes: [{op, \ route, backend}]}: route is the table's kernel for the operation, backend the one it runs on now", ), CommandSpec::new("diagnostics.crash_test", "Crash Test", crash_test).icon("bug_report").noundo().enabled(crash_test_enabled).params( @@ -58,11 +59,10 @@ fn kernel(_: &mut Session, p: &Value) -> Result { let c = v.as_str().and_then(route::Choice::from_id).ok_or_else(|| bad("diagnostics.kernel", "`choice` is routed, smlib or solidcore"))?; route::set_choice(c); } - let routes: Vec = route::Op::ALL - .iter() - .map(|op| json!({ "op": format!("{op:?}").to_lowercase(), "route": op.route().id(), "backend": op.backend().id() })) - .collect(); - Ok(json!({ "choice": route::choice().id(), "environment": route::overridden(), "routes": routes })) + let routes: Vec = + route::Op::ALL.iter().map(|op| json!({ "op": op.id(), "route": op.route().id(), "backend": op.backend().id() })).collect(); + let overrides: serde_json::Map = route::overrides().iter().map(|(op, b)| (op.id().to_string(), json!(b.id()))).collect(); + Ok(json!({ "choice": route::choice().id(), "environment": route::overridden(), "overrides": overrides, "routes": routes })) } fn dir(cmd: &str) -> Result { diff --git a/crates/engine/src/schema/diagnostics_tests.rs b/crates/engine/src/schema/diagnostics_tests.rs index 709b072f1..51aade0b8 100644 --- a/crates/engine/src/schema/diagnostics_tests.rs +++ b/crates/engine/src/schema/diagnostics_tests.rs @@ -100,6 +100,9 @@ fn diagnostics_kernel_reports_and_sets_the_choice() { } let environment = route::overridden(); assert_eq!(r["environment"], json!(environment), "{r}"); + // SOLIDCRAFT_KERNEL_ROUTES, as the families it moves. + let moved: serde_json::Map = route::overrides().iter().map(|(op, b)| (op.id().to_string(), json!(b.id()))).collect(); + assert_eq!(r["overrides"], serde_json::Value::Object(moved), "{r}"); for choice in ["solidcore", "smlib", "routed"] { let c = route::Choice::from_id(choice).unwrap(); // `.smb` files stay on SMLib whatever is chosen. @@ -109,7 +112,7 @@ fn diagnostics_kernel_reports_and_sets_the_choice() { "routed" => op.route().id(), other => other, }; - assert_eq!(op.backend_under(c).id(), want, "{choice}: {op:?}"); + assert_eq!(op.backend_with(c, &route::Overrides::NONE).id(), want, "{choice}: {op:?}"); } if !route::Op::ALL.iter().all(|op| op.backend_under(c) == op.route()) { continue; // it would move other tests' bodies to another kernel diff --git a/crates/smlib/src/route.rs b/crates/smlib/src/route.rs index 387394101..0041193c9 100644 --- a/crates/smlib/src/route.rs +++ b/crates/smlib/src/route.rs @@ -8,6 +8,10 @@ //! `SOLIDCRAFT_KERNEL=smlib|solidcore` sends every operation to one kernel for the whole process //! (the rig, the test matrix); otherwise the preference ([`set_choice`]) does, and its default //! follows the table. +//! +//! `SOLIDCRAFT_KERNEL_ROUTES==[,…]` (for example `boolean=solidcore,measure=solidcore`) +//! then moves single families on top of that choice, so the rig can score one family at a time +//! ([`Overrides`]). `.smb` files stay on SMLib whatever either says. use std::sync::OnceLock; use std::sync::atomic::{AtomicU8, Ordering}; @@ -88,6 +92,36 @@ impl Op { Op::File, ]; + /// The lower-case name `diagnostics.kernel` and `SOLIDCRAFT_KERNEL_ROUTES` spell it with. + pub fn id(self) -> &'static str { + match self { + Op::Convert => "convert", + Op::Primitive => "primitive", + Op::Transform => "transform", + Op::Measure => "measure", + Op::Check => "check", + Op::Boolean => "boolean", + Op::Blend => "blend", + Op::Offset => "offset", + Op::Split => "split", + Op::Sweep => "sweep", + Op::History => "history", + Op::Ssi => "ssi", + Op::Flat => "flat", + Op::File => "file", + } + } + + /// Back from [`Op::id`]; anything else is `None`. + pub fn from_id(id: &str) -> Option { + Op::ALL.into_iter().find(|op| op.id() == id) + } + + /// Where this family sits in [`Op::ALL`]. + fn index(self) -> usize { + Op::ALL.iter().position(|op| *op == self).unwrap_or(0) + } + /// The routing table: the kernel each family runs on when nothing overrides it. Every family /// stays on SMLib until the rig shows solidcore's parity for it. pub fn route(self) -> Backend { @@ -109,18 +143,28 @@ impl Op { } } - /// The kernel this family runs on now: `SOLIDCRAFT_KERNEL`, else the preference, else - /// [`Op::route`]. `.smb` files stay on SMLib whatever is chosen. + /// The kernel this family runs on now: `SOLIDCRAFT_KERNEL_ROUTES` for it, else + /// `SOLIDCRAFT_KERNEL`, else the preference, else [`Op::route`]. `.smb` files stay on SMLib + /// whatever is chosen. pub fn backend(self) -> Backend { self.backend_under(choice()) } /// The kernel this family runs on under `c`, whatever is in force now: what [`Op::backend`] - /// answers once `c` is the choice. `.smb` files stay on SMLib whatever is chosen. + /// answers once `c` is the choice (`SOLIDCRAFT_KERNEL_ROUTES` still applies on top). pub fn backend_under(self, c: Choice) -> Backend { + self.backend_with(c, overrides()) + } + + /// The kernel this family runs on under `c` with the overrides `o`, read from neither the + /// environment nor the preference. `.smb` files stay on SMLib whatever is chosen. + pub fn backend_with(self, c: Choice, o: &Overrides) -> Backend { if self == Op::File { return Backend::Smlib; } + if let Some(b) = o.get(self) { + return b; + } match c { Choice::Routed => self.route(), Choice::Only(b) => b, @@ -166,6 +210,101 @@ fn environment() -> Option { }) } +/// Families moved to a kernel one by one, on top of the choice: `SOLIDCRAFT_KERNEL_ROUTES`. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] +pub struct Overrides([Option; Op::ALL.len()]); + +impl Overrides { + /// No family moved. + pub const NONE: Overrides = Overrides([None; Op::ALL.len()]); + + /// Longest `SOLIDCRAFT_KERNEL_ROUTES` read; a longer one is ignored whole. + pub const MAX_LEN: usize = 4096; + /// Items read; the rest are ignored. + pub const MAX_ITEMS: usize = 64; + /// Longest item read; a longer one is ignored. + const MAX_ITEM_LEN: usize = 64; + + /// The kernel `op` is moved to, if any. + pub fn get(&self, op: Op) -> Option { + self.0.get(op.index()).copied().flatten() + } + + /// Whether no family is moved. + pub fn is_empty(&self) -> bool { + self.0.iter().all(Option::is_none) + } + + /// Every moved family and its kernel, in table order. + pub fn iter(&self) -> impl Iterator + '_ { + Op::ALL.into_iter().filter_map(|op| self.get(op).map(|b| (op, b))) + } + + /// A `SOLIDCRAFT_KERNEL_ROUTES` value: comma-separated `=`, spaces + /// around each part allowed, a later item for the same family winning. Returns the overrides + /// and a warning for each item ignored: an unknown family or kernel, an empty or overlong + /// item, `file` (always SMLib), items past [`Overrides::MAX_ITEMS`], or a whole value longer + /// than [`Overrides::MAX_LEN`]. + pub fn parse(text: &str) -> (Overrides, Vec) { + let mut o = Overrides::NONE; + let mut warnings = Vec::new(); + if text.len() > Self::MAX_LEN { + warnings.push(format!("{} bytes, longer than {}; ignored", text.len(), Self::MAX_LEN)); + return (o, warnings); + } + let mut items = text.split(','); + for item in items.by_ref().take(Self::MAX_ITEMS) { + let item = item.trim(); + let shown: String = item.chars().take(Self::MAX_ITEM_LEN).collect(); + if item.is_empty() { + warnings.push("an empty item; ignored".into()); + continue; + } + if item.len() > Self::MAX_ITEM_LEN { + warnings.push(format!("`{shown}…`: longer than {} bytes; ignored", Self::MAX_ITEM_LEN)); + continue; + } + let Some((op, backend)) = item.split_once('=') else { + warnings.push(format!("`{shown}`: not =; ignored")); + continue; + }; + let Some(op) = Op::from_id(op.trim()) else { + warnings.push(format!("`{shown}`: no operation family `{}`; ignored", op.trim())); + continue; + }; + let Some(backend) = Backend::from_id(backend.trim()) else { + warnings.push(format!("`{shown}`: `{}` is not smlib or solidcore; ignored", backend.trim())); + continue; + }; + if op == Op::File { + warnings.push(format!("`{shown}`: .smb files stay on smlib; ignored")); + continue; + } + if let Some(slot) = o.0.get_mut(op.index()) { + *slot = Some(backend); + } + } + if items.next().is_some() { + warnings.push(format!("more than {} items; the rest ignored", Self::MAX_ITEMS)); + } + (o, warnings) + } +} + +/// `SOLIDCRAFT_KERNEL_ROUTES`, read once; each item it ignores says so in the log. +pub fn overrides() -> &'static Overrides { + static ENV: OnceLock = OnceLock::new(); + ENV.get_or_init(|| { + let Some(v) = std::env::var_os("SOLIDCRAFT_KERNEL_ROUTES") else { return Overrides::NONE }; + let v = v.to_string_lossy(); + let (o, warnings) = Overrides::parse(&v); + for w in warnings { + log::warn!(target: "smlib", "SOLIDCRAFT_KERNEL_ROUTES: {w}"); + } + o + }) +} + static PREFERENCE: AtomicU8 = AtomicU8::new(0); /// The choice in force: `SOLIDCRAFT_KERNEL` when it is set, else the preference. @@ -216,9 +355,63 @@ mod tests { fn each_choice_routes_every_family_without_touching_the_preference() { for op in Op::ALL { let want = |b: Backend| if op == Op::File { Backend::Smlib } else { b }; - assert_eq!(op.backend_under(Choice::Routed), op.route(), "{op:?}"); - assert_eq!(op.backend_under(Choice::Only(Backend::Smlib)), want(Backend::Smlib), "{op:?}"); - assert_eq!(op.backend_under(Choice::Only(Backend::Solidcore)), want(Backend::Solidcore), "{op:?}"); + let none = &Overrides::NONE; + assert_eq!(op.backend_with(Choice::Routed, none), op.route(), "{op:?}"); + assert_eq!(op.backend_with(Choice::Only(Backend::Smlib), none), want(Backend::Smlib), "{op:?}"); + assert_eq!(op.backend_with(Choice::Only(Backend::Solidcore), none), want(Backend::Solidcore), "{op:?}"); + assert_eq!(op.backend_under(Choice::Routed), op.backend_with(Choice::Routed, overrides()), "{op:?}"); + } + } + + #[test] + fn op_ids_are_the_lower_case_names_and_round_trip() { + for op in Op::ALL { + assert_eq!(op.id(), format!("{op:?}").to_lowercase()); + assert_eq!(Op::from_id(op.id()), Some(op)); + } + assert_eq!(Op::from_id("Boolean"), None); + assert_eq!(Op::from_id(""), None); + } + + /// Overrides move only the families they name, on top of any choice; read from a string, not + /// the process's environment. + #[test] + fn overrides_move_single_families_on_top_of_the_choice() { + let (o, warnings) = Overrides::parse("boolean=solidcore, measure = solidcore"); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(o.iter().collect::>(), [(Op::Measure, Backend::Solidcore), (Op::Boolean, Backend::Solidcore)]); + for op in Op::ALL { + let moved = matches!(op, Op::Boolean | Op::Measure); + let want = if moved { Backend::Solidcore } else { op.route() }; + assert_eq!(op.backend_with(Choice::Routed, &o), want, "{op:?}"); + let want = if moved { Backend::Solidcore } else { Backend::Smlib }; + assert_eq!(op.backend_with(Choice::Only(Backend::Smlib), &o), want, "{op:?}"); + } + // Back to SMLib under an all-solidcore choice; the later item wins. + let (o, _) = Overrides::parse("check=solidcore,check=smlib"); + assert_eq!(Op::Check.backend_with(Choice::Only(Backend::Solidcore), &o), Backend::Smlib); + assert_eq!(Op::Sweep.backend_with(Choice::Only(Backend::Solidcore), &o), Backend::Solidcore); + assert!(Overrides::parse("").0.is_empty()); + } + + /// Hostile values: every bad item is ignored with a warning, the good ones still apply, and + /// nothing panics or reads past the caps. + #[test] + fn overrides_ignore_bad_items_with_a_warning() { + let (o, w) = Overrides::parse("file=solidcore,truck=solidcore,boolean=truck,,=,boolean,BOOLEAN=solidcore,blend=solidcore=x,sweep=solidcore"); + assert_eq!(o.iter().collect::>(), [(Op::Sweep, Backend::Solidcore)]); + assert_eq!(w.len(), 8, "{w:?}"); + assert_eq!(Op::File.backend_with(Choice::Routed, &o), Backend::Smlib); + let (o, w) = Overrides::parse(&"x".repeat(Overrides::MAX_LEN + 1)); + assert!(o.is_empty() && w.len() == 1, "{w:?}"); + let (o, w) = Overrides::parse(&format!("{}=solidcore,blend=solidcore", "a".repeat(200))); + assert_eq!((o.get(Op::Blend), w.len()), (Some(Backend::Solidcore), 1), "{w:?}"); + let many = vec!["measure=solidcore"; Overrides::MAX_ITEMS].join(",") + ",blend=solidcore"; + let (o, w) = Overrides::parse(&many); + assert_eq!((o.get(Op::Measure), o.get(Op::Blend), w.len()), (Some(Backend::Solidcore), None, 1), "{w:?}"); + for hostile in [",,,,", "\0=\0", "boolean=solidcore\0", "é=ü,ü", "=solidcore", "boolean="] { + let (o, w) = Overrides::parse(hostile); + assert!(o.is_empty() && !w.is_empty(), "{hostile:?}: {w:?}"); } } @@ -226,7 +419,7 @@ mod tests { /// table does is set here, so no other test's body changes kernel. #[test] fn the_preference_routes_unless_the_environment_overrides() { - if overridden() { + if overridden() || !overrides().is_empty() { return; } let harmless = [Choice::Only(Backend::Smlib), Choice::Only(Backend::Solidcore)] diff --git a/tests/kernel-cases/README.md b/tests/kernel-cases/README.md index 6e8ffb834..26434edb4 100644 --- a/tests/kernel-cases/README.md +++ b/tests/kernel-cases/README.md @@ -23,6 +23,18 @@ backend, keeps `docs/kernel-cases.md` as the SMLib report and writes the two sid [`docs/kernel-cases-backends.md`](../../docs/kernel-cases-backends.md) (cases that differ first) and `/backends.json`. +`--route =solidcore[,…]` (with `--kernel both` or `solidcore`) scores single operation +families: the solidcore side runs on SMLib (`SOLIDCRAFT_KERNEL=smlib`) with only the families +named moved to solidcore (`SOLIDCRAFT_KERNEL_ROUTES`), so an unported family (the body check, say) +doesn't hide the others' score. The smlib side stays plain SMLib. The report labels the side +`solidcore[boolean,measure]`, and its `results.json`, `dumps/`, `report.md`, `backends.json` and +`backends.md` go to `/solidcore-boolean-measure/`, so runs don't overwrite each other. The +families are the lower-case names `diagnostics.kernel` prints (`file` stays on SMLib): + +```sh +cargo xtask kernel-cases --kernel both --route boolean=solidcore --cli target/release/solidcraft-cli +``` + ## Rules - **The answer never comes from a kernel.** Not from truck's output, nor any commercial diff --git a/xtask/src/kernel_cases.rs b/xtask/src/kernel_cases.rs index a2e0d08a2..292cd2785 100644 --- a/xtask/src/kernel_cases.rs +++ b/xtask/src/kernel_cases.rs @@ -12,6 +12,11 @@ //! (`SOLIDCRAFT_KERNEL`). A solidcore run keeps its results, dumps and report in `/solidcore/`; //! `both` runs every case on each, keeps `docs/kernel-cases.md` as the SMLib report and writes the //! side by side to `docs/kernel-cases-backends.md` and `/backends.json`. +//! +//! `--route =solidcore[,…]` scores single operation families instead: the "solidcore" side runs +//! on SMLib (`SOLIDCRAFT_KERNEL=smlib`) with only those families moved to solidcore +//! (`SOLIDCRAFT_KERNEL_ROUTES`), its label names them (`solidcore[boolean,measure]`), and its +//! results, report and `backends.json` / `backends.md` go to `/solidcore-[-…]/`. use std::collections::BTreeMap; use std::path::{Path, PathBuf}; @@ -40,13 +45,62 @@ pub(crate) struct Options { out: PathBuf, /// The backends each case runs on (`--kernel`, default SMLib alone). pub(crate) kernels: Vec<&'static str>, + /// `--route`: the operation families the solidcore side moves to solidcore, on SMLib for the + /// rest; empty, solidcore answers every family. + routes: Vec, } impl Options { - /// A backend's output folder: the run's own for SMLib, `//` for any other. + /// A backend's output folder: the run's own for SMLib, `//` for any other, and + /// `/solidcore-[-…]/` for routed families. fn out_of(&self, kernel: &str) -> PathBuf { - if kernel == "smlib" { self.out.clone() } else { self.out.join(kernel) } + match kernel { + "smlib" => self.out.clone(), + "solidcore" if !self.routes.is_empty() => self.out.join(format!("solidcore-{}", self.routes.join("-"))), + other => self.out.join(other), + } } + + /// What a backend's side is called in the reports: `solidcore[boolean,measure]` for routed + /// families. + fn label(&self, kernel: &str) -> String { + if kernel == "solidcore" && !self.routes.is_empty() { format!("solidcore[{}]", self.routes.join(",")) } else { kernel.to_string() } + } + + /// The environment a backend's case processes get: `SOLIDCRAFT_KERNEL`, and for routed + /// families SMLib plus `SOLIDCRAFT_KERNEL_ROUTES`. + fn env(&self, kernel: &str) -> Vec<(&'static str, String)> { + if kernel == "solidcore" && !self.routes.is_empty() { + let routes = self.routes.iter().map(|op| format!("{op}=solidcore")).collect::>().join(","); + vec![("SOLIDCRAFT_KERNEL", "smlib".into()), ("SOLIDCRAFT_KERNEL_ROUTES", routes)] + } else { + vec![("SOLIDCRAFT_KERNEL", kernel.into())] + } + } +} + +/// The operation families `--route` may name: `Op::ALL` in crates/smlib/src/route.rs, as +/// `Op::id` spells them (a test keeps the two together). `file` is not one: `.smb` files stay on +/// SMLib. +const ROUTE_OPS: [&str; 13] = + ["convert", "primitive", "transform", "measure", "check", "boolean", "blend", "offset", "split", "sweep", "history", "ssi", "flat"]; + +/// `--route boolean=solidcore,measure=solidcore`: the families, in table order, each once. +fn parse_routes(text: &str) -> Result, String> { + let mut ops = Vec::new(); + for item in text.split(',').map(str::trim) { + let Some((op, backend)) = item.split_once('=') else { + return Err(format!("--route: `{item}` is not =solidcore")); + }; + if backend.trim() != "solidcore" { + return Err(format!("--route: `{item}`: only solidcore routes (the other side is SMLib already)")); + } + if !ROUTE_OPS.contains(&op.trim()) { + return Err(format!("--route: no operation family `{}` ({})", op.trim(), ROUTE_OPS.join(", "))); + } + ops.push(op.trim().to_string()); + } + Ok(ROUTE_OPS.iter().filter(|op| ops.iter().any(|o| o == *op)).map(|op| op.to_string()).collect()) } /// The run's options; `out` is its output folder unless `--out` gives one. @@ -61,7 +115,13 @@ pub(crate) fn options(rest: &[&str], out: &str) -> Result { Some("both") => BACKENDS.to_vec(), Some(k) => return Err(format!("--kernel: unknown backend '{k}' (smlib, solidcore or both)")), }; - Ok(Options { cli: arg("--cli"), area: arg("--area"), timeout: Duration::from_secs(secs), jobs, out, kernels }) + let routes = match arg("--route") { + None if rest.contains(&"--route") => return Err("--route: give =solidcore[,…]".into()), + None => Vec::new(), + Some(r) if kernels.contains(&"solidcore") => parse_routes(&r)?, + Some(_) => return Err("--route: needs --kernel solidcore or both".into()), + }; + Ok(Options { cli: arg("--cli"), area: arg("--area"), timeout: Duration::from_secs(secs), jobs, out, kernels, routes }) } /// Every case's full result as JSON, into the run's output folder. @@ -121,7 +181,8 @@ fn run_case(o: &Options, case: &Path, kernel: &str) -> Value { }; let mut dumps = std::ffi::OsString::from("--dumps="); dumps.push(o.out_of(kernel).join("dumps")); - cmd.env("SOLIDCRAFT_KERNEL", kernel) + cmd.env_remove("SOLIDCRAFT_KERNEL_ROUTES") + .envs(o.env(kernel)) .current_dir(root()) .args(["kernel-cases", "--json"]) .arg(dumps) @@ -195,7 +256,7 @@ fn run_on(o: &Options, list: &[PathBuf], kernels: &[&str]) -> Vec> { // Case by case, each on every backend in turn. let (Some(case), Some(kernel)) = (list.get(i / kernels.len().max(1)), kernels.get(i % kernels.len().max(1))) else { return }; let r = run_one(o, case, kernel); - let tag = if kernels.len() > 1 { format!("{kernel:<10}") } else { String::new() }; + let tag = if kernels.len() > 1 { format!("{:<10} ", o.label(kernel)) } else { String::new() }; println!( "{:<11} {tag}{}", r["status"].as_str().unwrap_or("?").to_uppercase(), @@ -293,8 +354,9 @@ fn source_kinds(dir: &Path, cited: &BTreeMap>) -> Result Result { +/// The report of one backend's run into `to` (relative to the repository root, or absolute); +/// `kernel` is the backend's label, `env` what its processes were given. +fn write_report(dir: &Path, list: &[PathBuf], results: &[Value], kernel: &str, env: &str, to: &Path) -> Result { let key = |r: &Value| (r["area"].as_str().unwrap_or("").to_string(), r["case"].as_str().unwrap_or("").to_string()); let by_case: BTreeMap<(String, String), &Value> = results.iter().map(|r| (key(r), r)).collect(); // Coverage: per area, cases and their statuses; per source, its cases. @@ -328,7 +390,7 @@ fn write_report(dir: &Path, list: &[PathBuf], results: &[Value], kernel: &str, t "# Kernel regression rig\n\nGenerated by `cargo xtask kernel-cases`. Every geometry case the old kernel (truck) got wrong, as an issue, a PR or a crash report recorded it, is a use case in [`tests/kernel-cases/`](../tests/kernel-cases/) with its own right answer: numbers from analysis or a hand calculation (**analytic**) where the geometry has them, and otherwise the invariants (**invariants**): the operation succeeds, or refuses cleanly where that is the right answer; each body is one closed shell that passes the record's structure and geometry checks and SMLib's own check; the expected face, edge and vertex counts. Never a kernel's output, and never a commercial kernel's.\n\nThe cases run on SMLib, SolidCraft's kernel. A failing case stays failing until the kernel is fixed; there is no workaround on SolidCraft's side. A failed SMLib call leaves its inputs for replay in the run's output folder (`target/kernel-cases/dumps/-/`). See [`tests/kernel-cases/README.md`](../tests/kernel-cases/README.md) for the format.\n\n", ); if kernel != "smlib" { - md += &format!("**This run: `SOLIDCRAFT_KERNEL={kernel}`.**\n\n"); + md += &format!("**This run: `{env}`.**\n\n"); } md += "## Coverage\n\n| Area | Cases | Results |\n|---|---|---|\n"; for (area, (n, statuses)) in &areas { @@ -400,7 +462,9 @@ fn seconds(r: &Value) -> Option { /// The two backends side by side: per case (area, id) each one's status, time and what went wrong, /// as JSON, and the Markdown report: a summary, then per area the cases that differ first. -fn side_by_side(smlib: &[Value], solidcore: &[Value]) -> (Value, String) { +/// `labels` name the two sides in the report (`solidcore[boolean]` for routed families); the JSON +/// keys stay `smlib` and `solidcore`. +fn side_by_side(smlib: &[Value], solidcore: &[Value], labels: [&str; 2]) -> (Value, String) { let key = |r: &Value| (r["area"].as_str().unwrap_or("").to_string(), r["case"].as_str().unwrap_or("").to_string()); let mut pairs: BTreeMap<(String, String), [Option<&Value>; 2]> = BTreeMap::new(); for (k, rs) in [smlib, solidcore].iter().enumerate() { @@ -428,9 +492,13 @@ fn side_by_side(smlib: &[Value], solidcore: &[Value]) -> (Value, String) { } let differ = pairs.len() - agree; let mut md = String::from( - "# Kernel regression rig: SMLib and solidcore\n\nGenerated by `cargo xtask kernel-cases --kernel both`: every case in [`tests/kernel-cases/`](../tests/kernel-cases/) run on each backend (`SOLIDCRAFT_KERNEL`), graded against its own answer. Times are the commands' own. [`kernel-cases.md`](kernel-cases.md) is the SMLib run in full.\n\n## Summary\n\n| Backend | Pass | Fail | Other | Time (s) |\n|---|---|---|---|---|\n", + "# Kernel regression rig: SMLib and solidcore\n\nGenerated by `cargo xtask kernel-cases --kernel both`: every case in [`tests/kernel-cases/`](../tests/kernel-cases/) run on each backend (`SOLIDCRAFT_KERNEL`), graded against its own answer. Times are the commands' own. [`kernel-cases.md`](kernel-cases.md) is the SMLib run in full.\n\n", ); - for (k, name) in BACKENDS.iter().enumerate() { + if labels[1] != BACKENDS[1] { + md += &format!("**This run: `{}` is SMLib with only those operation families on solidcore (`--route`).**\n\n", labels[1]); + } + md += "## Summary\n\n| Backend | Pass | Fail | Other | Time (s) |\n|---|---|---|---|---|\n"; + for (k, name) in labels.iter().enumerate() { let n = |b: &str| counts[k].get(b).copied().unwrap_or(0); md += &format!("| {name} | {} | {} | {} | {:.1} |\n", n("pass"), n("fail"), n("other"), times[k]); } @@ -445,7 +513,7 @@ fn side_by_side(smlib: &[Value], solidcore: &[Value]) -> (Value, String) { } for (area, mut list) in by_area { list.sort_by_key(|r| (r["agree"].as_bool().unwrap_or(false), r["case"].as_str().unwrap_or("").to_string())); - md += &format!("\n## {area}\n\n| Case | smlib | solidcore | |\n|---|---|---|---|\n"); + md += &format!("\n## {area}\n\n| Case | {} | {} | |\n|---|---|---|---|\n", labels[0], labels[1]); for r in list { let mark = if r["agree"].as_bool().unwrap_or(false) { "" } else { "**differ**" }; md += &format!("| {} | {} | {} | {mark} |\n", r["case"].as_str().unwrap_or(""), side(&r["smlib"]), side(&r["solidcore"])); @@ -457,7 +525,7 @@ fn side_by_side(smlib: &[Value], solidcore: &[Value]) -> (Value, String) { .map(|(k, name)| (name.to_string(), json!({"counts": counts[k], "seconds": times[k]}))) .collect::>() .into(); - (json!({"summary": summary, "agree": agree, "differ": differ, "cases": rows}), md) + (json!({"labels": labels, "summary": summary, "agree": agree, "differ": differ, "cases": rows}), md) } pub fn run(rest: &[&str]) -> Result<(), String> { @@ -478,17 +546,24 @@ pub fn run(rest: &[&str]) -> Result<(), String> { let out = o.out_of(kernel); save_json(&out, "results.json", results)?; let to = if *kernel == "smlib" { PathBuf::from("docs/kernel-cases.md") } else { out.join("report.md") }; - println!("{}", write_report(&dir, &list, results, kernel, &to)?); + let env = o.env(kernel).iter().map(|(k, v)| format!("{k}={v}")).collect::>().join(" "); + println!("{}", write_report(&dir, &list, results, &o.label(kernel), &env, &to)?); } if let [smlib, solidcore] = runs.as_slice() { - let (data, md) = side_by_side(smlib, solidcore); - save_json(&o.out, "backends.json", &data)?; + let labels = [o.label(BACKENDS[0]), o.label(BACKENDS[1])]; + let (data, md) = side_by_side(smlib, solidcore, [&labels[0], &labels[1]]); + // Routed runs keep their side by side with their results, so runs don't overwrite each other. + let into = if o.routes.is_empty() { o.out.clone() } else { o.out_of(BACKENDS[1]) }; + save_json(&into, "backends.json", &data)?; + if !o.routes.is_empty() { + std::fs::write(into.join("backends.md"), &md).map_err(|e| format!("{}: {e}", into.join("backends.md").display()))?; + } std::fs::write(root().join("docs/kernel-cases-backends.md"), md).map_err(|e| format!("docs/kernel-cases-backends.md: {e}"))?; println!( "kernel-cases: {} agree, {} differ → docs/kernel-cases-backends.md, {}", data["agree"], data["differ"], - o.out.join("backends.json").display() + into.join("backends.json").display() ); } Ok(()) @@ -505,7 +580,7 @@ mod tests { let mut hang = json!({"area": "booleans", "case": "b", "status": "hang", "error": "no result after 180 s"}); hang["process_seconds"] = json!(180.0); let solidcore = [r("booleans", "a", "pass", 0.5), hang, r("shell", "c", "fail", 1.5)]; - let (data, md) = side_by_side(&smlib, &solidcore); + let (data, md) = side_by_side(&smlib, &solidcore, ["smlib", "solidcore"]); assert_eq!((data["agree"].as_u64(), data["differ"].as_u64()), (Some(1), Some(2))); assert_eq!(data["summary"]["smlib"]["counts"], json!({"pass": 2, "other": 1})); assert_eq!(data["summary"]["solidcore"]["counts"], json!({"pass": 1, "fail": 1, "other": 1})); @@ -517,5 +592,45 @@ mod tests { assert!(pos_b.is_some() && pos_a.is_some() && pos_b < pos_a, "{md}"); assert!(md.contains("| b | pass (2.00 s) | hang (180.00 s) | **differ** |"), "{md}"); assert!(md.contains("3 cases: 1 agree, 2 differ."), "{md}"); + let (data, md) = side_by_side(&smlib, &[], ["smlib", "solidcore[boolean,measure]"]); + assert_eq!(data["labels"], json!(["smlib", "solidcore[boolean,measure]"])); + assert!(md.contains("| solidcore[boolean,measure] | 0 | 0 | 3 |") && md.contains("| Case | smlib | solidcore[boolean,measure] | |"), "{md}"); + } + + /// `--route` moves the named families on the solidcore side only: SMLib with + /// SOLIDCRAFT_KERNEL_ROUTES, labelled and kept in its own folder. + #[test] + fn routes_label_the_solidcore_side_and_keep_its_own_folder() { + let o = options(&["--kernel", "both", "--route", "measure=solidcore, boolean=solidcore", "--out", "/r"], "x").unwrap(); + assert_eq!(o.routes, ["measure", "boolean"]); + assert_eq!((o.label("smlib"), o.label("solidcore")), ("smlib".into(), "solidcore[measure,boolean]".into())); + assert_eq!(o.env("smlib"), [("SOLIDCRAFT_KERNEL", "smlib".to_string())]); + assert_eq!( + o.env("solidcore"), + [("SOLIDCRAFT_KERNEL", "smlib".to_string()), ("SOLIDCRAFT_KERNEL_ROUTES", "measure=solidcore,boolean=solidcore".to_string())] + ); + assert_eq!((o.out_of("smlib"), o.out_of("solidcore")), (PathBuf::from("/r"), PathBuf::from("/r/solidcore-measure-boolean"))); + let plain = options(&["--kernel", "both", "--out", "/r"], "x").unwrap(); + assert_eq!((plain.label("solidcore"), plain.out_of("solidcore")), ("solidcore".into(), PathBuf::from("/r/solidcore"))); + assert_eq!(plain.env("solidcore"), [("SOLIDCRAFT_KERNEL", "solidcore".to_string())]); + for bad in [ + &["--kernel", "both", "--route", "truck=solidcore"][..], + &["--kernel", "both", "--route", "file=solidcore"], + &["--kernel", "both", "--route", "boolean=smlib"], + &["--kernel", "both", "--route", "boolean"], + &["--kernel", "both", "--route", ""], + &["--kernel", "both", "--route"], + &["--route", "boolean=solidcore"], + ] { + assert!(options(bad, "x").is_err(), "{bad:?}"); + } + } + + /// `--route` knows the families route.rs routes, `file` aside. + #[test] + fn route_ops_are_the_routing_tables_families() { + let text = std::fs::read_to_string(root().join("crates/smlib/src/route.rs")).unwrap_or_default(); + let ids: Vec<&str> = text.lines().filter_map(|l| l.trim().strip_prefix("Op::")?.split_once(" => \"")?.1.strip_suffix("\",")).collect(); + assert_eq!(ids.iter().filter(|id| **id != "file").copied().collect::>(), ROUTE_OPS, "{ids:?}"); } } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 95f7101c0..d67b08c42 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -47,7 +47,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] [--kernel K] + kernel-cases [--area AREA] [--cli BIN] [--timeout S] [--jobs N] [--out DIR] [--kernel K] [--route R] the kernel regression rig: run every case in tests/kernel-cases/// on SMLib (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 @@ -57,6 +57,10 @@ commands: solidcore keeps results.json, dumps/ and report.md in DIR/solidcore/ (not docs/kernel-cases.md); both runs each case on each, and also writes the side by side to docs/kernel-cases-backends.md and DIR/backends.json + --route OP=solidcore[,...] (with --kernel solidcore|both): the solidcore side is + SMLib with only those families on solidcore (SOLIDCRAFT_KERNEL=smlib plus + SOLIDCRAFT_KERNEL_ROUTES), labelled solidcore[OP,...], into + DIR/solidcore-OP[-OP...]/ with its backends.json and backends.md --ladder [--record REASON]: the complexity ladder instead (tests/kernel-cases/ladder/ladder.json, tiers in order), graded and compared with its timing golden (one case at a time unless --jobs), into