//! Compiles the hand-written candidates in `crates/generation/fixtures/candidates` //! (`roadmap.md` -> "Phase 1 — Engine bootstrap" step 4) into one batch crate, runs //! every candidate over the cached corpus, gates each against the permanent regression //! set, writes a provenance record for whichever candidate is admitted, and records the //! per-category pass rates. Requires `ground_truth_cache` to have already populated //! `corpus/ground-truth/` -- this binary never starts a browser. use std::fs; use anyhow::{bail, Context, Result}; use generation::groundtruth::{load_all, CachedCase}; use generation::model::{CandidateSource, DirectoryCandidateSource}; use generation::orchestrate::{repo_root, run_candidates, write_regression_set}; use generation::runner::passing_case_ids; use generation::{admit, candidate_crate, prompt}; use layout::width::{resolve_block_width_baseline, WidthResolver}; use provenance::css21_width; fn main() -> Result<()> { let repo_root = repo_root(); let cache_dir = repo_root.join("corpus/ground-truth"); let cases = load_all(&cache_dir)?; if cases.is_empty() { bail!( "no cached cases under {}; run the ground_truth_cache binary first", cache_dir.display() ); } let fixtures_dir = repo_root.join("crates/generation/fixtures/candidates"); let candidates = DirectoryCandidateSource { dir: fixtures_dir }.candidates()?; println!( "candidates: {}", candidates.iter().map(|c| c.name.as_str()).collect::>().join(", ") ); let crate_dir = repo_root.join("corpus/candidates/generated"); candidate_crate::write(&crate_dir, &repo_root, &candidates)?; let binary = candidate_crate::compile(&crate_dir, &crate_dir.join("target"))?; let regression_set_path = repo_root.join("corpus/regression-set.json"); let report_out = repo_root.join("corpus/candidates/report.json"); // The permanent regression set (`roadmap.md` -> "Failure-weighted sampling, with a // permanent regression set"): every case that has ever passed. The first time this // runs, nothing has been admitted yet, so it is seeded from the currently-shipped // baseline rule's own passing cases -- exactly what "regression" means before any // generated candidate has landed. let baseline_passing: Vec = passing_case_ids(resolve_block_width_baseline as WidthResolver, &cases); if !regression_set_path.exists() { write_regression_set(®ression_set_path, &baseline_passing)?; println!( "seeded the permanent regression set with {} case(s) baseline already passes", baseline_passing.len() ); } let gated = run_candidates(&binary, &cache_dir, Some(®ression_set_path), &report_out)?; println!("\n== per-candidate results =="); for candidate in &gated.candidates { println!( "{:<28} {}/{} passed, admitted={}, regressed={}", candidate.name, candidate.cases_passed, candidate.cases_run, candidate.admitted, candidate.regressed_cases.len(), ); let mut categories: Vec<_> = candidate.per_category.iter().collect(); categories.sort_by_key(|(name, _)| (*name).clone()); for (category, tally) in categories { println!(" {category:<28} {}/{}", tally.passed, tally.total); } } // Prove the gate discriminates: the correct candidate must be // admitted and every seeded-wrong one must not be, given this fixture set. let get = |name: &str| gated.candidates.iter().find(|c| c.name == name); let correct = get("correct").context("no candidate named \"correct\" in the batch")?; let wrong_names = ["ignores_auto_margin", "wrong_side_over_constraint"]; let mut discrimination_ok = correct.admitted; if !correct.admitted { eprintln!("gate did not admit the correct candidate"); } for name in wrong_names { if let Some(wrong) = get(name) { if wrong.admitted { discrimination_ok = false; eprintln!("gate did not reject seeded-wrong candidate {name}"); } } } if !discrimination_ok { bail!("the generation loop did not discriminate correctly between candidates"); } println!("\ngate admitted \"correct\", rejected every seeded-wrong candidate"); // Admit `correct` and write its provenance record. The prompt a model would have // received is built from the cases the rule being replaced (baseline) currently // fails -- what a real generation attempt would have been asked to fix. let baseline_failing: Vec<&CachedCase> = cases.iter().filter(|case| !baseline_passing.contains(&case.case_id)).collect(); let seam_source = fs::read_to_string(repo_root.join("crates/layout/src/width.rs"))?; let built_prompt = prompt::build(css21_width::SPEC_TEXT, &seam_source, &baseline_failing); let prompt_path = repo_root.join("corpus/candidates/prompt.txt"); fs::create_dir_all(prompt_path.parent().expect("path has a parent"))?; fs::write(&prompt_path, &built_prompt.text)?; println!("wrote the assembled prompt ({} bytes) to {}", built_prompt.text.len(), prompt_path.display()); let record = admit::record_for(correct, "hand-written", &built_prompt.hash, prompt::HAND_WRITTEN_SEED); let provenance_dir = repo_root.join("corpus/candidates/provenance"); admit::write(&provenance_dir, "correct", &record)?; println!( "wrote provenance record for \"correct\" to {}", provenance_dir.join("correct.provenance.json").display() ); // Extend the regression set with what `correct` newly passes, so its improvements // are protected going forward too (`roadmap.md` -> "Re-verify": the gate for the // *next* candidate). `passed_case_ids` comes straight from the subprocess report -- // `correct.rs`'s `resolve` runs inside the generated batch crate, not this driver, // so its per-case outcomes are only reachable through what it already reported. let mut extended = baseline_passing; for id in &correct.passed_case_ids { if !extended.contains(id) { extended.push(id.clone()); } } write_regression_set(®ression_set_path, &extended)?; println!("regression set now covers {} case(s)", extended.len()); Ok(()) }