aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / crumbs / src / experiment.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! BREADCRUMB-8: the acquisition and reuse experiment. |
| 17 | //! |
| 18 | //! Pre-registered design (fixed in code before any run): |
| 19 | //! |
| 20 | //! * Capabilities 0..=7 are TAUGHT (phase 1 may draw them); 8..=11 are never |
| 21 | //! presented in phase 1. |
| 22 | //! * Phase 1: `phase1` mixer draws over the whole curriculum, restricted to |
| 23 | //! taught capabilities and to the TRAIN composition pairs below. |
| 24 | //! * Phase 2: a fixed list of unseen test crumbs, identical in both |
| 25 | //! conditions, presented with the library frozen: |
| 26 | //! - `heldout-pair` compositions of two taught capabilities never shown as a pair |
| 27 | //! - `triple` compositions of three taught capabilities (never shown) |
| 28 | //! - `untaught` compositions involving an untaught capability |
| 29 | //! - `novel-primitive` the untaught capabilities themselves |
| 30 | //! - `general` ordinary chains (harm check: does a library slow search?) |
| 31 | //! * Conditions: CONTROL (admissions never sent; the learner cannot reuse |
| 32 | //! discovered operations) and LEARNING (verified admissions are sent). |
| 33 | //! Same learner binary, same search budget, same test seeds. |
| 34 | //! |
| 35 | //! The report states whatever happened. Nothing here is tuned to a result. |
| 36 | |
| 37 | use crate::gen::{registry, rng::Rng, FamilySpec}; |
| 38 | use crate::mixer::{Mixer, MixerConfig}; |
| 39 | use crate::promotion::{PromotionConfig, Stage}; |
| 40 | use crate::protocol::SearchConfig; |
| 41 | use crate::sealed::{AdversarialClass, DecoyStatus, Population}; |
| 42 | use crate::session::{CrumbOutcome, Session, SessionConfig}; |
| 43 | use crate::verify::{verifier_digest, EvalClass}; |
| 44 | use serde::Serialize; |
| 45 | use std::collections::BTreeMap; |
| 46 | use std::path::PathBuf; |
| 47 | |
| 48 | pub const TAUGHT: [u16; 8] = [0, 1, 2, 3, 4, 5, 6, 7]; |
| 49 | pub const TRAIN_PAIRS: [&str; 7] = [ |
| 50 | "compose-00-01", |
| 51 | "compose-02-03", |
| 52 | "compose-04-05", |
| 53 | "compose-06-07", |
| 54 | "compose-01-03", |
| 55 | "compose-03-05", |
| 56 | "compose-05-07", |
| 57 | ]; |
| 58 | pub const HELDOUT_PAIRS: [&str; 6] = [ |
| 59 | "compose-01-02", |
| 60 | "compose-03-04", |
| 61 | "compose-05-06", |
| 62 | "compose-00-02", |
| 63 | "compose-02-04", |
| 64 | "compose-04-06", |
| 65 | ]; |
| 66 | pub const TRIPLES: [&str; 4] = [ |
| 67 | "compose-00-01-02", |
| 68 | "compose-03-04-05", |
| 69 | "compose-00-03-06", |
| 70 | "compose-01-04-07", |
| 71 | ]; |
| 72 | pub const UNTAUGHT_PAIRS: [&str; 11] = [ |
| 73 | "compose-07-08", |
| 74 | "compose-08-09", |
| 75 | "compose-09-10", |
| 76 | "compose-10-11", |
| 77 | "compose-11-00", |
| 78 | "compose-06-08", |
| 79 | "compose-07-09", |
| 80 | "compose-08-10", |
| 81 | "compose-09-11", |
| 82 | "compose-10-00", |
| 83 | "compose-11-01", |
| 84 | ]; |
| 85 | pub const NOVEL_PRIMITIVES: [&str; 4] = [ |
| 86 | "capability-08", |
| 87 | "capability-09", |
| 88 | "capability-10", |
| 89 | "capability-11", |
| 90 | ]; |
| 91 | pub const GENERAL: [&str; 4] = [ |
| 92 | "chain-basic-l2-w16", |
| 93 | "chain-basic-l3-w16", |
| 94 | "latent-offset-w16", |
| 95 | "mod-affine-w12", |
| 96 | ]; |
| 97 | pub const GROUPS: [&str; 5] = [ |
| 98 | "heldout-pair", |
| 99 | "triple", |
| 100 | "untaught", |
| 101 | "novel-primitive", |
| 102 | "general", |
| 103 | ]; |
| 104 | |
| 105 | #[derive(Clone, Debug, Serialize)] |
| 106 | pub struct TestItem { |
| 107 | pub group: &'static str, |
| 108 | pub family: String, |
| 109 | pub seed: u64, |
| 110 | } |
| 111 | |
| 112 | pub fn test_plan(run_seed: u64) -> Vec<TestItem> { |
| 113 | let mut out = Vec::new(); |
| 114 | let groups: [(&'static str, &[&str], u64); 5] = [ |
| 115 | ("heldout-pair", &HELDOUT_PAIRS, 3), |
| 116 | ("triple", &TRIPLES, 3), |
| 117 | ("untaught", &UNTAUGHT_PAIRS, 2), |
| 118 | ("novel-primitive", &NOVEL_PRIMITIVES, 2), |
| 119 | ("general", &GENERAL, 2), |
| 120 | ]; |
| 121 | for (gi, (g, fams, reps)) in groups.into_iter().enumerate() { |
| 122 | for (i, f) in fams.iter().enumerate() { |
| 123 | for r in 0..reps { |
| 124 | let seed = Rng::named("experiment-test-seed", &[run_seed, gi as u64, i as u64, r]) |
| 125 | .next_u64(); |
| 126 | out.push(TestItem { |
| 127 | group: g, |
| 128 | family: f.to_string(), |
| 129 | seed, |
| 130 | }); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | out |
| 135 | } |
| 136 | |
| 137 | #[derive(Clone, Debug, Default, Serialize)] |
| 138 | pub struct GroupMetrics { |
| 139 | pub crumbs: u32, |
| 140 | pub supported: u32, |
| 141 | pub solved: u32, |
| 142 | pub verified: u32, |
| 143 | pub solve_rate: f64, |
| 144 | pub interpolation_pass_rate: f64, |
| 145 | pub extrapolation_pass_rate: f64, |
| 146 | pub adversarial_pass_rate: f64, |
| 147 | pub submissions: u32, |
| 148 | pub falsified_hidden: u32, |
| 149 | pub false_generalization_rate: f64, |
| 150 | pub mean_candidates_generated: f64, |
| 151 | pub mean_candidates_evaluated: f64, |
| 152 | pub candidates_per_solution: f64, |
| 153 | pub mean_exec_count: f64, |
| 154 | pub mean_wall_ms: f64, |
| 155 | pub reuse_rate: f64, |
| 156 | } |
| 157 | |
| 158 | fn metrics(outs: &[&CrumbOutcome]) -> GroupMetrics { |
| 159 | let mut m = GroupMetrics { |
| 160 | crumbs: outs.len() as u32, |
| 161 | ..Default::default() |
| 162 | }; |
| 163 | if outs.is_empty() { |
| 164 | return m; |
| 165 | } |
| 166 | let (mut ip, mut it, mut ep, mut et, mut ap, mut at) = (0u64, 0u64, 0u64, 0u64, 0u64, 0u64); |
| 167 | let (mut gen_sum, mut eval_sum, mut exec_sum, mut wall, mut gen_to_solution) = |
| 168 | (0u64, 0u64, 0u64, 0u64, 0u64); |
| 169 | let mut reuse = 0u32; |
| 170 | for o in outs { |
| 171 | m.supported += o.supported as u32; |
| 172 | m.solved += o.accepted as u32; |
| 173 | m.verified += o.verified as u32; |
| 174 | m.submissions += o.evaluations.len() as u32; |
| 175 | m.falsified_hidden += o.falsified_hidden() as u32; |
| 176 | gen_sum += o.stats.generated as u64; |
| 177 | eval_sum += o.stats.evaluated as u64; |
| 178 | exec_sum += o.stats.exec_count; |
| 179 | wall += o.stats.wall_ns; |
| 180 | reuse += o.reused_library() as u32; |
| 181 | if o.accepted { |
| 182 | gen_to_solution += o.stats.candidates_to_solution as u64; |
| 183 | } |
| 184 | // Held-out pass rates of the learner's final committed answer. |
| 185 | if let Some(e) = o.evaluations.last() { |
| 186 | ip += e.interpolation.passed as u64; |
| 187 | it += e.interpolation.total as u64; |
| 188 | ep += e.extrapolation.passed as u64; |
| 189 | et += e.extrapolation.total as u64; |
| 190 | ap += e.adversarial.passed as u64; |
| 191 | at += e.adversarial.total as u64; |
| 192 | } |
| 193 | } |
| 194 | let n = outs.len() as f64; |
| 195 | let r = |a: u64, b: u64| if b == 0 { 0.0 } else { a as f64 / b as f64 }; |
| 196 | m.solve_rate = m.solved as f64 / n; |
| 197 | m.interpolation_pass_rate = r(ip, it); |
| 198 | m.extrapolation_pass_rate = r(ep, et); |
| 199 | m.adversarial_pass_rate = r(ap, at); |
| 200 | m.false_generalization_rate = r(m.falsified_hidden as u64, m.submissions as u64); |
| 201 | m.mean_candidates_generated = gen_sum as f64 / n; |
| 202 | m.mean_candidates_evaluated = eval_sum as f64 / n; |
| 203 | m.candidates_per_solution = if m.solved == 0 { |
| 204 | f64::NAN |
| 205 | } else { |
| 206 | gen_to_solution as f64 / m.solved as f64 |
| 207 | }; |
| 208 | m.mean_exec_count = exec_sum as f64 / n; |
| 209 | m.mean_wall_ms = wall as f64 / n / 1e6; |
| 210 | m.reuse_rate = if m.solved == 0 { |
| 211 | 0.0 |
| 212 | } else { |
| 213 | reuse as f64 / m.solved as f64 |
| 214 | }; |
| 215 | m |
| 216 | } |
| 217 | |
| 218 | /// Curriculum-wide epistemic metrics (phase 1). |
| 219 | #[derive(Clone, Debug, Default, Serialize)] |
| 220 | pub struct CurriculumMetrics { |
| 221 | pub crumbs: u32, |
| 222 | pub by_population: BTreeMap<String, u32>, |
| 223 | pub redistributed_draws: BTreeMap<String, u32>, |
| 224 | pub visible_fit_rate: f64, |
| 225 | pub heldout_verified_rate: f64, |
| 226 | pub rabbit_hole_crumbs: u32, |
| 227 | /// Rabbit holes (classes A-E, G) whose first submitted answer was not held-out-falsified. |
| 228 | pub adversarial_survival_rate: f64, |
| 229 | pub false_generalization_rate: f64, |
| 230 | pub insufficient_evidence_crumbs: u32, |
| 231 | pub ambiguity_recognition_rate: f64, |
| 232 | pub false_ambiguity_rate_on_clean: f64, |
| 233 | pub noisy_crumbs: u32, |
| 234 | pub noisy_verified: u32, |
| 235 | pub operations_tracked: u32, |
| 236 | pub operations_admitted: u32, |
| 237 | } |
| 238 | |
| 239 | #[derive(Serialize)] |
| 240 | pub struct ConditionReport { |
| 241 | pub condition: String, |
| 242 | pub curriculum: CurriculumMetrics, |
| 243 | pub test: BTreeMap<String, GroupMetrics>, |
| 244 | pub test_all: GroupMetrics, |
| 245 | pub ledger_head: String, |
| 246 | pub ledger_records: usize, |
| 247 | pub ledger_verified: bool, |
| 248 | } |
| 249 | |
| 250 | #[derive(Serialize)] |
| 251 | pub struct PairedComparison { |
| 252 | pub group: String, |
| 253 | pub crumbs: u32, |
| 254 | pub solved_control: u32, |
| 255 | pub solved_learning: u32, |
| 256 | pub solved_only_learning: u32, |
| 257 | pub solved_only_control: u32, |
| 258 | pub both_solved: u32, |
| 259 | pub fewer_candidates_learning: u32, |
| 260 | pub fewer_candidates_control: u32, |
| 261 | pub equal_candidates: u32, |
| 262 | /// Geometric mean of learning/control candidates-to-solution over crumbs both solved. |
| 263 | pub geomean_candidate_ratio_both_solved: f64, |
| 264 | pub mean_generated_control: f64, |
| 265 | pub mean_generated_learning: f64, |
| 266 | pub mean_exec_control: f64, |
| 267 | pub mean_exec_learning: f64, |
| 268 | pub mean_wall_ms_control: f64, |
| 269 | pub mean_wall_ms_learning: f64, |
| 270 | pub heldout_correct_control: u32, |
| 271 | pub heldout_correct_learning: u32, |
| 272 | } |
| 273 | |
| 274 | #[derive(Serialize)] |
| 275 | pub struct ExperimentReport { |
| 276 | pub design: &'static str, |
| 277 | pub run_seed: u64, |
| 278 | pub phase1_draws: u64, |
| 279 | pub search: SearchConfig, |
| 280 | pub promotion: PromotionConfig, |
| 281 | pub mixer: MixerConfig, |
| 282 | pub test_plan: Vec<TestItem>, |
| 283 | pub verifier_digest: String, |
| 284 | pub learner_digest: String, |
| 285 | pub registry_version: &'static str, |
| 286 | pub generator_version: &'static str, |
| 287 | pub control: ConditionReport, |
| 288 | pub learning: ConditionReport, |
| 289 | pub paired: Vec<PairedComparison>, |
| 290 | pub aien_next_action_guidance: &'static str, |
| 291 | pub wall_seconds: f64, |
| 292 | } |
| 293 | |
| 294 | fn curriculum_metrics( |
| 295 | outs: &[CrumbOutcome], |
| 296 | ladder: &crate::promotion::Ladder, |
| 297 | mixer: &Mixer, |
| 298 | ) -> CurriculumMetrics { |
| 299 | let ph: Vec<&CrumbOutcome> = outs.iter().filter(|o| o.phase == "phase1").collect(); |
| 300 | let mut c = CurriculumMetrics { |
| 301 | crumbs: ph.len() as u32, |
| 302 | ..Default::default() |
| 303 | }; |
| 304 | for o in &ph { |
| 305 | *c.by_population |
| 306 | .entry(format!("{:?}", o.population)) |
| 307 | .or_insert(0) += 1; |
| 308 | } |
| 309 | for (p, n) in &mixer.redistributed_draws { |
| 310 | c.redistributed_draws.insert(format!("{p:?}"), *n); |
| 311 | } |
| 312 | let n = ph.len().max(1) as f64; |
| 313 | c.visible_fit_rate = ph.iter().filter(|o| o.stats.visible_fits > 0).count() as f64 / n; |
| 314 | c.heldout_verified_rate = ph.iter().filter(|o| o.verified).count() as f64 / n; |
| 315 | let rh: Vec<_> = ph |
| 316 | .iter() |
| 317 | .filter(|o| matches!(o.decoy, DecoyStatus::RabbitHole(k) if k != AdversarialClass::InsufficientEvidence)) |
| 318 | .collect(); |
| 319 | c.rabbit_hole_crumbs = rh.len() as u32; |
| 320 | let survived = rh |
| 321 | .iter() |
| 322 | .filter(|o| o.first_class() != Some(EvalClass::FalsifiedHidden)) |
| 323 | .count(); |
| 324 | c.adversarial_survival_rate = if rh.is_empty() { |
| 325 | f64::NAN |
| 326 | } else { |
| 327 | survived as f64 / rh.len() as f64 |
| 328 | }; |
| 329 | let subs: usize = ph.iter().map(|o| o.evaluations.len()).sum(); |
| 330 | let fh: usize = ph.iter().map(|o| o.falsified_hidden()).sum(); |
| 331 | c.false_generalization_rate = if subs == 0 { |
| 332 | 0.0 |
| 333 | } else { |
| 334 | fh as f64 / subs as f64 |
| 335 | }; |
| 336 | let ie: Vec<_> = ph |
| 337 | .iter() |
| 338 | .filter(|o| { |
| 339 | o.decoy == DecoyStatus::RabbitHole(AdversarialClass::InsufficientEvidence) |
| 340 | && !o.evaluations.is_empty() |
| 341 | }) |
| 342 | .collect(); |
| 343 | c.insufficient_evidence_crumbs = ie.len() as u32; |
| 344 | let recog = ie |
| 345 | .iter() |
| 346 | .filter(|o| o.stats.hypotheses_at_first_submit >= 2) |
| 347 | .count(); |
| 348 | c.ambiguity_recognition_rate = if ie.is_empty() { |
| 349 | f64::NAN |
| 350 | } else { |
| 351 | recog as f64 / ie.len() as f64 |
| 352 | }; |
| 353 | let clean: Vec<_> = ph |
| 354 | .iter() |
| 355 | .filter(|o| o.decoy == DecoyStatus::Clean && !o.evaluations.is_empty()) |
| 356 | .collect(); |
| 357 | let false_amb = clean |
| 358 | .iter() |
| 359 | .filter(|o| o.stats.hypotheses_at_first_submit >= 2) |
| 360 | .count(); |
| 361 | c.false_ambiguity_rate_on_clean = if clean.is_empty() { |
| 362 | f64::NAN |
| 363 | } else { |
| 364 | false_amb as f64 / clean.len() as f64 |
| 365 | }; |
| 366 | let noisy: Vec<_> = ph |
| 367 | .iter() |
| 368 | .filter(|o| o.decoy == DecoyStatus::RabbitHole(AdversarialClass::Noisy)) |
| 369 | .collect(); |
| 370 | c.noisy_crumbs = noisy.len() as u32; |
| 371 | c.noisy_verified = noisy.iter().filter(|o| o.verified).count() as u32; |
| 372 | c.operations_tracked = ladder.ops.len() as u32; |
| 373 | c.operations_admitted = ladder.count(Stage::Admitted) as u32; |
| 374 | c |
| 375 | } |
| 376 | |
| 377 | pub struct ConditionRun { |
| 378 | pub outcomes: Vec<CrumbOutcome>, |
| 379 | pub report: ConditionReport, |
| 380 | pub learner_digest: String, |
| 381 | } |
| 382 | |
| 383 | #[allow(clippy::too_many_arguments)] |
| 384 | pub fn run_condition( |
| 385 | learner: &str, |
| 386 | condition: &str, |
| 387 | library: bool, |
| 388 | run_seed: u64, |
| 389 | phase1: u64, |
| 390 | search: SearchConfig, |
| 391 | mixer_cfg: &MixerConfig, |
| 392 | out_dir: Option<PathBuf>, |
| 393 | ) -> Result<ConditionRun, crate::protocol::ProtocolError> { |
| 394 | let cfg = SessionConfig { |
| 395 | condition: condition.to_string(), |
| 396 | learner: learner.to_string(), |
| 397 | run_id: format!("crumbline-experiment-{run_seed}-{condition}"), |
| 398 | search, |
| 399 | library_enabled: library, |
| 400 | promotion: PromotionConfig::default(), |
| 401 | record_transcript: false, |
| 402 | out_dir, |
| 403 | }; |
| 404 | let mut s = Session::start(cfg)?; |
| 405 | let learner_digest = s.learner_digest.hex(); |
| 406 | let mut mixer = Mixer::new(mixer_cfg.clone(), run_seed); |
| 407 | for _ in 0..phase1 { |
| 408 | let d = mixer.draw(); |
| 409 | let fam = registry().get(d.family_id).expect("registered"); |
| 410 | let o = s.present(fam, d.seed, d.population, "phase1")?; |
| 411 | mixer.record(fam, o.verified); |
| 412 | } |
| 413 | s.freeze_admissions(); |
| 414 | for item in test_plan(run_seed) { |
| 415 | let fam: &FamilySpec = registry() |
| 416 | .by_key(&item.family) |
| 417 | .expect("test family registered"); |
| 418 | s.present(fam, item.seed, Population::Composition, item.group)?; |
| 419 | } |
| 420 | let genesis = s.ledger.genesis; |
| 421 | let (ledger, outcomes, ladder) = s.finish()?; |
| 422 | let verified = crate::ledger::Ledger::verify(genesis, &ledger.records).is_ok(); |
| 423 | let curriculum = curriculum_metrics(&outcomes, &ladder, &mixer); |
| 424 | let mut test = BTreeMap::new(); |
| 425 | for g in GROUPS { |
| 426 | let v: Vec<&CrumbOutcome> = outcomes.iter().filter(|o| o.phase == g).collect(); |
| 427 | test.insert(g.to_string(), metrics(&v)); |
| 428 | } |
| 429 | let all: Vec<&CrumbOutcome> = outcomes.iter().filter(|o| o.phase != "phase1").collect(); |
| 430 | let report = ConditionReport { |
| 431 | condition: condition.to_string(), |
| 432 | curriculum, |
| 433 | test, |
| 434 | test_all: metrics(&all), |
| 435 | ledger_head: ledger.head.hex(), |
| 436 | ledger_records: ledger.records.len(), |
| 437 | ledger_verified: verified, |
| 438 | }; |
| 439 | Ok(ConditionRun { |
| 440 | outcomes, |
| 441 | report, |
| 442 | learner_digest, |
| 443 | }) |
| 444 | } |
| 445 | |
| 446 | pub fn paired( |
| 447 | control: &[CrumbOutcome], |
| 448 | learning: &[CrumbOutcome], |
| 449 | group: &str, |
| 450 | ) -> PairedComparison { |
| 451 | let pick = |o: &&CrumbOutcome| { |
| 452 | if group == "all" { |
| 453 | o.phase != "phase1" |
| 454 | } else { |
| 455 | o.phase == group |
| 456 | } |
| 457 | }; |
| 458 | let c: Vec<&CrumbOutcome> = control.iter().filter(pick).collect(); |
| 459 | let l: Vec<&CrumbOutcome> = learning.iter().filter(pick).collect(); |
| 460 | let mut p = PairedComparison { |
| 461 | group: group.to_string(), |
| 462 | crumbs: c.len() as u32, |
| 463 | solved_control: 0, |
| 464 | solved_learning: 0, |
| 465 | solved_only_learning: 0, |
| 466 | solved_only_control: 0, |
| 467 | both_solved: 0, |
| 468 | fewer_candidates_learning: 0, |
| 469 | fewer_candidates_control: 0, |
| 470 | equal_candidates: 0, |
| 471 | geomean_candidate_ratio_both_solved: f64::NAN, |
| 472 | mean_generated_control: 0.0, |
| 473 | mean_generated_learning: 0.0, |
| 474 | mean_exec_control: 0.0, |
| 475 | mean_exec_learning: 0.0, |
| 476 | mean_wall_ms_control: 0.0, |
| 477 | mean_wall_ms_learning: 0.0, |
| 478 | heldout_correct_control: 0, |
| 479 | heldout_correct_learning: 0, |
| 480 | }; |
| 481 | let mut log_sum = 0.0; |
| 482 | for (a, b) in c.iter().zip(l.iter()) { |
| 483 | assert_eq!( |
| 484 | a.crumb_digest, b.crumb_digest, |
| 485 | "paired crumbs must be identical" |
| 486 | ); |
| 487 | p.solved_control += a.accepted as u32; |
| 488 | p.solved_learning += b.accepted as u32; |
| 489 | p.heldout_correct_control += a.verified as u32; |
| 490 | p.heldout_correct_learning += b.verified as u32; |
| 491 | match (a.accepted, b.accepted) { |
| 492 | (true, true) => { |
| 493 | p.both_solved += 1; |
| 494 | let x = a.stats.candidates_to_solution.max(1) as f64; |
| 495 | let y = b.stats.candidates_to_solution.max(1) as f64; |
| 496 | log_sum += (y / x).ln(); |
| 497 | } |
| 498 | (false, true) => p.solved_only_learning += 1, |
| 499 | (true, false) => p.solved_only_control += 1, |
| 500 | _ => {} |
| 501 | } |
| 502 | let (ga, gb) = (a.stats.generated, b.stats.generated); |
| 503 | if gb < ga { |
| 504 | p.fewer_candidates_learning += 1; |
| 505 | } else if ga < gb { |
| 506 | p.fewer_candidates_control += 1; |
| 507 | } else { |
| 508 | p.equal_candidates += 1; |
| 509 | } |
| 510 | p.mean_generated_control += ga as f64; |
| 511 | p.mean_generated_learning += gb as f64; |
| 512 | p.mean_exec_control += a.stats.exec_count as f64; |
| 513 | p.mean_exec_learning += b.stats.exec_count as f64; |
| 514 | p.mean_wall_ms_control += a.stats.wall_ns as f64 / 1e6; |
| 515 | p.mean_wall_ms_learning += b.stats.wall_ns as f64 / 1e6; |
| 516 | } |
| 517 | let n = c.len().max(1) as f64; |
| 518 | p.mean_generated_control /= n; |
| 519 | p.mean_generated_learning /= n; |
| 520 | p.mean_exec_control /= n; |
| 521 | p.mean_exec_learning /= n; |
| 522 | p.mean_wall_ms_control /= n; |
| 523 | p.mean_wall_ms_learning /= n; |
| 524 | if p.both_solved > 0 { |
| 525 | p.geomean_candidate_ratio_both_solved = (log_sum / p.both_solved as f64).exp(); |
| 526 | } |
| 527 | p |
| 528 | } |
| 529 | |
| 530 | pub fn run( |
| 531 | learner: &str, |
| 532 | run_seed: u64, |
| 533 | phase1: u64, |
| 534 | out: Option<PathBuf>, |
| 535 | ) -> Result<ExperimentReport, crate::protocol::ProtocolError> { |
| 536 | let t0 = std::time::Instant::now(); |
| 537 | let search = SearchConfig::default(); |
| 538 | let mixer_cfg = MixerConfig { |
| 539 | capability_subset: Some(TAUGHT.to_vec()), |
| 540 | composition_keys: Some(TRAIN_PAIRS.iter().map(|s| s.to_string()).collect()), |
| 541 | ..MixerConfig::default() |
| 542 | }; |
| 543 | let sub = |name: &str| out.as_ref().map(|d| d.join(name)); |
| 544 | let control = run_condition( |
| 545 | learner, |
| 546 | "control", |
| 547 | false, |
| 548 | run_seed, |
| 549 | phase1, |
| 550 | search, |
| 551 | &mixer_cfg, |
| 552 | sub("control"), |
| 553 | )?; |
| 554 | let learning = run_condition( |
| 555 | learner, |
| 556 | "learning", |
| 557 | true, |
| 558 | run_seed, |
| 559 | phase1, |
| 560 | search, |
| 561 | &mixer_cfg, |
| 562 | sub("learning"), |
| 563 | )?; |
| 564 | let mut paired_v = Vec::new(); |
| 565 | for g in GROUPS.iter().copied().chain(["all"]) { |
| 566 | paired_v.push(paired(&control.outcomes, &learning.outcomes, g)); |
| 567 | } |
| 568 | let report = ExperimentReport { |
| 569 | design: "pre-registered: taught caps 0-7, train pairs fixed, 68 unseen test crumbs, library frozen before test, identical budgets", |
| 570 | run_seed, |
| 571 | phase1_draws: phase1, |
| 572 | search, |
| 573 | promotion: PromotionConfig::default(), |
| 574 | mixer: mixer_cfg, |
| 575 | test_plan: test_plan(run_seed), |
| 576 | verifier_digest: verifier_digest().hex(), |
| 577 | learner_digest: control.learner_digest.clone(), |
| 578 | registry_version: crate::gen::registry::REGISTRY_VERSION, |
| 579 | generator_version: crate::gen::GENERATOR_VERSION, |
| 580 | control: control.report, |
| 581 | learning: learning.report, |
| 582 | paired: paired_v, |
| 583 | aien_next_action_guidance: "not available: no trained AIEN_0 policy exists yet; the trace corpus produced here is its training input", |
| 584 | wall_seconds: t0.elapsed().as_secs_f64(), |
| 585 | }; |
| 586 | if let Some(d) = &out { |
| 587 | let _ = std::fs::create_dir_all(d); |
| 588 | let _ = std::fs::write( |
| 589 | d.join("report.json"), |
| 590 | serde_json::to_string_pretty(&report).unwrap(), |
| 591 | ); |
| 592 | for (name, outs) in [ |
| 593 | ("control", &control.outcomes), |
| 594 | ("learning", &learning.outcomes), |
| 595 | ] { |
| 596 | let _ = std::fs::write( |
| 597 | d.join(format!("{name}-outcomes.json")), |
| 598 | serde_json::to_string(outs).unwrap(), |
| 599 | ); |
| 600 | } |
| 601 | } |
| 602 | Ok(report) |
| 603 | } |
| 604 | |
| 605 | /// Pooled comparison over independent replicates (different curriculum draws |
| 606 | /// and different test seeds per replicate; same design). |
| 607 | #[derive(Serialize)] |
| 608 | pub struct ReplicateReport { |
| 609 | pub run_seeds: Vec<u64>, |
| 610 | pub phase1_draws: u64, |
| 611 | pub pooled: Vec<PairedComparison>, |
| 612 | pub per_seed_all: Vec<PairedComparison>, |
| 613 | pub control_curriculum: Vec<CurriculumMetrics>, |
| 614 | pub learning_curriculum: Vec<CurriculumMetrics>, |
| 615 | pub wall_seconds: f64, |
| 616 | } |
| 617 | |
| 618 | pub fn replicate( |
| 619 | learner: &str, |
| 620 | first_seed: u64, |
| 621 | k: u64, |
| 622 | phase1: u64, |
| 623 | out: Option<PathBuf>, |
| 624 | ) -> Result<ReplicateReport, crate::protocol::ProtocolError> { |
| 625 | let t0 = std::time::Instant::now(); |
| 626 | let search = SearchConfig::default(); |
| 627 | let mixer_cfg = MixerConfig { |
| 628 | capability_subset: Some(TAUGHT.to_vec()), |
| 629 | composition_keys: Some(TRAIN_PAIRS.iter().map(|s| s.to_string()).collect()), |
| 630 | ..MixerConfig::default() |
| 631 | }; |
| 632 | let (mut all_c, mut all_l) = (Vec::new(), Vec::new()); |
| 633 | let mut per_seed = Vec::new(); |
| 634 | let (mut cc, mut lc) = (Vec::new(), Vec::new()); |
| 635 | let seeds: Vec<u64> = (first_seed..first_seed + k).collect(); |
| 636 | for &seed in &seeds { |
| 637 | let sub = |name: &str| { |
| 638 | out.as_ref() |
| 639 | .map(|d| d.join(format!("seed-{seed}")).join(name)) |
| 640 | }; |
| 641 | let c = run_condition( |
| 642 | learner, |
| 643 | "control", |
| 644 | false, |
| 645 | seed, |
| 646 | phase1, |
| 647 | search, |
| 648 | &mixer_cfg, |
| 649 | sub("control"), |
| 650 | )?; |
| 651 | let l = run_condition( |
| 652 | learner, |
| 653 | "learning", |
| 654 | true, |
| 655 | seed, |
| 656 | phase1, |
| 657 | search, |
| 658 | &mixer_cfg, |
| 659 | sub("learning"), |
| 660 | )?; |
| 661 | per_seed.push(paired(&c.outcomes, &l.outcomes, "all")); |
| 662 | cc.push(c.report.curriculum.clone()); |
| 663 | lc.push(l.report.curriculum.clone()); |
| 664 | all_c.extend(c.outcomes); |
| 665 | all_l.extend(l.outcomes); |
| 666 | } |
| 667 | let pooled = GROUPS |
| 668 | .iter() |
| 669 | .copied() |
| 670 | .chain(["all"]) |
| 671 | .map(|g| paired(&all_c, &all_l, g)) |
| 672 | .collect(); |
| 673 | let rep = ReplicateReport { |
| 674 | run_seeds: seeds, |
| 675 | phase1_draws: phase1, |
| 676 | pooled, |
| 677 | per_seed_all: per_seed, |
| 678 | control_curriculum: cc, |
| 679 | learning_curriculum: lc, |
| 680 | wall_seconds: t0.elapsed().as_secs_f64(), |
| 681 | }; |
| 682 | if let Some(d) = &out { |
| 683 | let _ = std::fs::create_dir_all(d); |
| 684 | let _ = std::fs::write( |
| 685 | d.join("replicates.json"), |
| 686 | serde_json::to_string_pretty(&rep).unwrap(), |
| 687 | ); |
| 688 | } |
| 689 | Ok(rep) |
| 690 | } |
| 691 | |
| 692 | pub fn main(args: &[String]) -> i32 { |
| 693 | let get = |n: &str| { |
| 694 | args.iter() |
| 695 | .position(|a| a == n) |
| 696 | .and_then(|i| args.get(i + 1).cloned()) |
| 697 | }; |
| 698 | let learner = get("--learner").expect("--learner PATH"); |
| 699 | let seed = get("--seed").map_or(20260927, |v| v.parse().unwrap()); |
| 700 | let phase1 = get("--phase1").map_or(120, |v| v.parse().unwrap()); |
| 701 | let out = get("--out").map(PathBuf::from); |
| 702 | if let Some(k) = get("--replicates") { |
| 703 | return match replicate(&learner, seed, k.parse().unwrap(), phase1, out) { |
| 704 | Ok(r) => { |
| 705 | println!("{}", serde_json::to_string_pretty(&r.pooled).unwrap()); |
| 706 | println!("wall {:.1}s", r.wall_seconds); |
| 707 | 0 |
| 708 | } |
| 709 | Err(e) => { |
| 710 | eprintln!("{e}"); |
| 711 | 1 |
| 712 | } |
| 713 | }; |
| 714 | } |
| 715 | match run(&learner, seed, phase1, out) { |
| 716 | Ok(r) => { |
| 717 | println!("{}", serde_json::to_string_pretty(&r.paired).unwrap()); |
| 718 | println!("wall {:.1}s", r.wall_seconds); |
| 719 | 0 |
| 720 | } |
| 721 | Err(e) => { |
| 722 | eprintln!("{e}"); |
| 723 | 1 |
| 724 | } |
| 725 | } |
| 726 | } |