//! [`bin/generation_loop.rs`](../generation_loop.rs)'s counterpart for the margin- //! collapse seam: compiles the hand-written candidates in //! `crates/generation/fixtures/margin-candidates` into one batch crate, runs every //! candidate over the cached corpus, gates each against the permanent regression set //! *and* fact-agreement with `crates/logic`'s rules //! (`generation::margin_runner::evaluate`), 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/` -- shared with //! the width equation, so this binary never starts a browser and never needs its own //! cache binary. //! //! The regression set this binary reads and writes, //! `corpus/margin-regression-set.json`, is its own file, separate from the width //! equation's `corpus/regression-set.json`: the two seams' candidates are evaluated by //! different runners (`generation::runner` vs `generation::margin_runner`) against //! different registry types, so a shared file would mix which seam a passing case id //! belongs to. use std::fs; use anyhow::{bail, Context, Result}; use generation::groundtruth::{load_all, CachedCase}; use generation::margin_runner::{fact_agreement, passing_case_ids, MarginCandidate}; use generation::model::{CandidateSource, DirectoryCandidateSource}; use generation::orchestrate::{repo_root, run_candidates, write_regression_set}; use generation::{admit, candidate_crate, prompt}; use layout::margin::{classify_baseline, combine_baseline}; use provenance::css21_margin_collapse; 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/margin-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/margin-generated"); candidate_crate::write_for(candidate_crate::Seam::MarginCollapse, &crate_dir, &repo_root, &candidates)?; let binary = candidate_crate::compile(&crate_dir, &crate_dir.join("target"))?; let regression_set_path = repo_root.join("corpus/margin-regression-set.json"); let report_out = repo_root.join("corpus/candidates/margin-report.json"); // Seeded the same way `bin/generation_loop.rs` seeds the width equation's: from the // currently-shipped baseline (no collapsing at all) rule's own passing cases, since // nothing has been admitted for this seam yet. let baseline = MarginCandidate { name: "baseline", classify: classify_baseline, combine: combine_baseline }; let baseline_passing: Vec = passing_case_ids(baseline, &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 on both axes: geometry regression *and* fact // agreement with the `crates/logic` rules (`generation::margin_runner`'s module doc) // -- 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_self_collapsing", "bfc_not_checked"]; 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"); // Fact agreement for this driver's own baseline, printed for visibility -- the // correct/seeded-wrong candidates' functions live inside the compiled batch crate, // not this driver, so their own fact agreement is already folded into `admitted` // above (`generation::margin_runner::evaluate`) rather than re-checkable here. let baseline_agreement = fact_agreement(baseline, &cases); println!( "\nbaseline (no collapsing) agrees with the rules' self-collapsing verdict on {}/{} cases", baseline_agreement.agreed, baseline_agreement.checked ); // 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/margin.rs"))?; let built_prompt = prompt::build_margin_collapse(css21_margin_collapse::SPEC_TEXT, &seam_source, &baseline_failing); let prompt_path = repo_root.join("corpus/candidates/margin-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_margin_collapse(correct, "hand-written", &built_prompt.hash, prompt::HAND_WRITTEN_SEED); let provenance_dir = repo_root.join("corpus/candidates/margin-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. 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(()) }