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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
jyviko
2026-11-03 06:05:18 -05:00
parent 4bdffef061
commit 9c65a30265
23 changed files with 1148 additions and 58 deletions
+45 -2
View File
@@ -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<Value> = 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/<area>/<case>.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<Value> = 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);
}