benchmarks-mv
Measurement harnesses and evidence bundles for AIEN on the NVIDIA DGX Spark GB10. Speed is reported against the machine's physical limit and my own previous run. Attachment of AienOS.
Brought by drakestapleton
benchmarks-mv / src / prereg.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 | //! Offline checks for benchmark preregistration manifests (`aien-bench-prereg/1`). |
| 17 | //! |
| 18 | //! A preregistration fixes arms, environments, the sealed split, metrics, sample sizes, |
| 19 | //! stopping rules and the Turing profile before any candidate is trained. It is a plan, not |
| 20 | //! evidence: it carries no measurements. Rules, one [`Defect`] per violation: |
| 21 | //! - `schema`, `prereg_id`, `status` ([`PREREG_STATUSES`]) and `results_status` |
| 22 | //! ([`RUN_STATUSES`]) are present; no string anywhere is a placeholder; |
| 23 | //! - `arms` have unique ids, a role in [`ARM_ROLES`], a boolean `pretrained`, and include at |
| 24 | //! least one baseline, one candidate and one control; |
| 25 | //! - `baseline_before_eval.required` is `true`, its `baseline_arms` are exactly the arms whose |
| 26 | //! role is `baseline`, and `phases` runs `preregistration_freeze` before `baseline_run` |
| 27 | //! before `sealed_eval` (with `candidate_training`, if present, after `baseline_run` and |
| 28 | //! before `sealed_eval`); |
| 29 | //! - `turing_profile.G.background` is a non-empty list of `{id, kind, owner, revision}`; |
| 30 | //! every arm that is pretrained or uses a teacher names its background, and every |
| 31 | //! `background_refs` entry is declared in `G`; [`PENDING_PIN`] revisions are allowed only |
| 32 | //! while `status` is `SCOPED`, otherwise a revision is a commit id or `sha256:<64 hex>`; |
| 33 | //! - `turing_profile` declares `B` (a baseline arm), a blinded `D`, all five `P` fields, |
| 34 | //! `T_ideal` and `T_real` separately, and `tj.allowed` is `true` only when |
| 35 | //! `energy.status == "MEASURED"`; |
| 36 | //! - `heldout.sealing` names a mechanism and sets `prediction_commit_before_reveal`, |
| 37 | //! `judge_independent` and `single_use` to `true`; |
| 38 | //! - every `sample_size.studies` entry is arithmetically consistent: `units_planned = |
| 39 | //! tasks_planned * units_per_cluster`, `design_effect = 1 + (units_per_cluster - 1) * icc`, |
| 40 | //! `effective_n_planned = floor(units_planned / design_effect)` and at least |
| 41 | //! `exact_min_effective_n`, `exact_power_at_planned >= power_target`, and |
| 42 | //! `alpha_per_contrast * confirmatory_contrasts <= alpha_familywise`; |
| 43 | //! - `stopping_rules` is a non-empty list of `{id, trigger, action}`; every `claims` status is |
| 44 | //! in [`RUN_STATUSES`]. |
| 45 | |
| 46 | use crate::evidence::Defect; |
| 47 | use crate::evidence::{collect_strings, display_path, get, is_commit, is_placeholder, push, walk}; |
| 48 | use serde_json::{Map, Value}; |
| 49 | use std::collections::BTreeSet; |
| 50 | use std::fs; |
| 51 | use std::path::Path; |
| 52 | |
| 53 | /// The preregistration contract this module validates. |
| 54 | pub const PREREG_SCHEMA: &str = "aien-bench-prereg/1"; |
| 55 | |
| 56 | /// Lifecycle of the preregistration itself. |
| 57 | pub const PREREG_STATUSES: [&str; 3] = ["SCOPED", "FROZEN", "SUPERSEDED"]; |
| 58 | |
| 59 | /// Status of the campaign and of each claim. |
| 60 | pub const RUN_STATUSES: [&str; 5] = ["NOT_RUN", "BLOCKED", "INCONCLUSIVE", "FAIL", "PASS"]; |
| 61 | |
| 62 | /// Arm roles. |
| 63 | pub const ARM_ROLES: [&str; 3] = ["baseline", "candidate", "control"]; |
| 64 | |
| 65 | /// A background revision not yet bound; allowed only while `status` is `SCOPED`. |
| 66 | pub const PENDING_PIN: &str = "PENDING_PIN"; |
| 67 | |
| 68 | const PHASE_FREEZE: &str = "preregistration_freeze"; |
| 69 | const PHASE_BASELINE: &str = "baseline_run"; |
| 70 | const PHASE_TRAINING: &str = "candidate_training"; |
| 71 | const PHASE_EVAL: &str = "sealed_eval"; |
| 72 | |
| 73 | const P_FIELDS: [&str; 5] = [ |
| 74 | "encoding", |
| 75 | "baseline", |
| 76 | "sampling", |
| 77 | "energy", |
| 78 | "statistical_test", |
| 79 | ]; |
| 80 | |
| 81 | const STUDY_NUMBERS: [&str; 12] = [ |
| 82 | "alpha_familywise", |
| 83 | "confirmatory_contrasts", |
| 84 | "alpha_per_contrast", |
| 85 | "power_target", |
| 86 | "exact_min_effective_n", |
| 87 | "tasks_planned", |
| 88 | "units_per_cluster", |
| 89 | "icc_assumed", |
| 90 | "design_effect", |
| 91 | "units_planned", |
| 92 | "effective_n_planned", |
| 93 | "exact_power_at_planned", |
| 94 | ]; |
| 95 | |
| 96 | const EPS: f64 = 1e-9; |
| 97 | |
| 98 | /// Result of a run over one or more paths. |
| 99 | #[derive(Debug, Default)] |
| 100 | pub struct PreregReport { |
| 101 | pub defects: Vec<Defect>, |
| 102 | pub manifests: usize, |
| 103 | /// Informational lines (for example a target that does not exist). |
| 104 | pub notes: Vec<String>, |
| 105 | } |
| 106 | |
| 107 | /// Validate every JSON file under each target (a file or directory relative to `root`). |
| 108 | pub fn verify_prereg_paths(root: &Path, targets: &[String]) -> PreregReport { |
| 109 | let mut report = PreregReport::default(); |
| 110 | for target in targets { |
| 111 | let clean = target.trim_end_matches('/'); |
| 112 | let full = root.join(clean); |
| 113 | if !full.exists() { |
| 114 | report |
| 115 | .notes |
| 116 | .push(format!("{clean}: skipped: not present under the root")); |
| 117 | continue; |
| 118 | } |
| 119 | let mut files = Vec::new(); |
| 120 | if full.is_dir() { |
| 121 | walk(&full, &mut files); |
| 122 | } else { |
| 123 | files.push(full.clone()); |
| 124 | } |
| 125 | files.sort(); |
| 126 | for file in files |
| 127 | .iter() |
| 128 | .filter(|f| f.extension().is_some_and(|e| e == "json")) |
| 129 | { |
| 130 | let display = display_path(root, file); |
| 131 | let value: Value = match fs::read_to_string(file) |
| 132 | .map_err(|e| format!("cannot read: {e}")) |
| 133 | .and_then(|t| serde_json::from_str(&t).map_err(|e| format!("invalid JSON: {e}"))) |
| 134 | { |
| 135 | Ok(v) => v, |
| 136 | Err(reason) => { |
| 137 | push(&mut report.defects, &display, "(file)", reason); |
| 138 | continue; |
| 139 | } |
| 140 | }; |
| 141 | report.manifests += 1; |
| 142 | let mut found = Vec::new(); |
| 143 | check_prereg(&display, &value, &mut found); |
| 144 | found.sort(); |
| 145 | found.dedup(); |
| 146 | report.defects.extend(found); |
| 147 | } |
| 148 | } |
| 149 | report |
| 150 | } |
| 151 | |
| 152 | /// Validate one parsed preregistration. `path` is only used for the defect lines. |
| 153 | pub fn check_prereg(path: &str, v: &Value, out: &mut Vec<Defect>) { |
| 154 | let mut d = |field: &str, reason: String| push(out, path, field, reason); |
| 155 | let Some(top) = v.as_object() else { |
| 156 | d("(file)", "top level is not a JSON object".into()); |
| 157 | return; |
| 158 | }; |
| 159 | match top.get("schema").and_then(Value::as_str) { |
| 160 | None => d("schema", "missing".into()), |
| 161 | Some(s) if s != PREREG_SCHEMA => d("schema", format!("\"{s}\" is not {PREREG_SCHEMA}")), |
| 162 | Some(_) => {} |
| 163 | } |
| 164 | if top.get("prereg_id").and_then(Value::as_str).is_none() { |
| 165 | d("prereg_id", "missing".into()); |
| 166 | } |
| 167 | let status = enum_field(top, "status", &PREREG_STATUSES, &mut d); |
| 168 | enum_field(top, "results_status", &RUN_STATUSES, &mut d); |
| 169 | |
| 170 | let mut strings = Vec::new(); |
| 171 | collect_strings(v, String::new(), &mut strings); |
| 172 | for (field, s) in &strings { |
| 173 | if is_placeholder(s) { |
| 174 | d(field, format!("placeholder value \"{s}\"")); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | let arms = check_arms(top, &mut d); |
| 179 | check_baseline_before_eval(top, &arms, &mut d); |
| 180 | check_turing(top, status, &arms, &mut d); |
| 181 | check_sealing(top, &mut d); |
| 182 | check_sample_size(top, &mut d); |
| 183 | check_stopping_rules(top, &mut d); |
| 184 | if let Some(claims) = top.get("claims") { |
| 185 | match claims.as_array() { |
| 186 | Some(list) => { |
| 187 | for (i, c) in list.iter().enumerate() { |
| 188 | match c.get("status").and_then(Value::as_str) { |
| 189 | Some(s) if RUN_STATUSES.contains(&s) => {} |
| 190 | Some(s) => d( |
| 191 | &format!("claims[{i}].status"), |
| 192 | format!("\"{s}\" is not one of {}", RUN_STATUSES.join(", ")), |
| 193 | ), |
| 194 | None => d(&format!("claims[{i}].status"), "missing".into()), |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | None => d("claims", "must be an array".into()), |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /// One declared arm. |
| 204 | struct Arm { |
| 205 | id: String, |
| 206 | role: Option<String>, |
| 207 | needs_background: bool, |
| 208 | refs: Vec<String>, |
| 209 | } |
| 210 | |
| 211 | fn check_arms(top: &Map<String, Value>, d: &mut impl FnMut(&str, String)) -> Vec<Arm> { |
| 212 | let Some(list) = top.get("arms").and_then(Value::as_array) else { |
| 213 | d("arms", "missing or not an array".into()); |
| 214 | return Vec::new(); |
| 215 | }; |
| 216 | if list.is_empty() { |
| 217 | d("arms", "must declare at least one arm".into()); |
| 218 | } |
| 219 | let mut arms = Vec::new(); |
| 220 | let mut seen = BTreeSet::new(); |
| 221 | for (i, a) in list.iter().enumerate() { |
| 222 | let at = |f: &str| format!("arms[{i}].{f}"); |
| 223 | let Some(id) = a.get("id").and_then(Value::as_str) else { |
| 224 | d(&at("id"), "missing".into()); |
| 225 | continue; |
| 226 | }; |
| 227 | if !seen.insert(id.to_string()) { |
| 228 | d(&at("id"), format!("duplicate arm id \"{id}\"")); |
| 229 | } |
| 230 | let role = match a.get("role").and_then(Value::as_str) { |
| 231 | Some(r) if ARM_ROLES.contains(&r) => Some(r.to_string()), |
| 232 | Some(r) => { |
| 233 | d( |
| 234 | &at("role"), |
| 235 | format!("\"{r}\" is not one of {}", ARM_ROLES.join(", ")), |
| 236 | ); |
| 237 | None |
| 238 | } |
| 239 | None => { |
| 240 | d(&at("role"), "missing".into()); |
| 241 | None |
| 242 | } |
| 243 | }; |
| 244 | let pretrained = match a.get("pretrained") { |
| 245 | Some(Value::Bool(b)) => *b, |
| 246 | Some(_) => { |
| 247 | d(&at("pretrained"), "must be true or false".into()); |
| 248 | false |
| 249 | } |
| 250 | None => { |
| 251 | d(&at("pretrained"), "missing".into()); |
| 252 | false |
| 253 | } |
| 254 | }; |
| 255 | let teacher = a.get("uses_teacher").and_then(Value::as_bool) == Some(true); |
| 256 | let refs = match a.get("background_refs") { |
| 257 | None => Vec::new(), |
| 258 | Some(Value::Array(r)) => r |
| 259 | .iter() |
| 260 | .filter_map(Value::as_str) |
| 261 | .map(str::to_string) |
| 262 | .collect(), |
| 263 | Some(_) => { |
| 264 | d(&at("background_refs"), "must be an array of strings".into()); |
| 265 | Vec::new() |
| 266 | } |
| 267 | }; |
| 268 | arms.push(Arm { |
| 269 | id: id.to_string(), |
| 270 | role, |
| 271 | needs_background: pretrained || teacher, |
| 272 | refs, |
| 273 | }); |
| 274 | } |
| 275 | for role in ARM_ROLES { |
| 276 | if !list.is_empty() && !arms.iter().any(|a| a.role.as_deref() == Some(role)) { |
| 277 | d("arms", format!("no arm has role {role}")); |
| 278 | } |
| 279 | } |
| 280 | arms |
| 281 | } |
| 282 | |
| 283 | fn check_baseline_before_eval( |
| 284 | top: &Map<String, Value>, |
| 285 | arms: &[Arm], |
| 286 | d: &mut impl FnMut(&str, String), |
| 287 | ) { |
| 288 | match top.get("baseline_before_eval") { |
| 289 | None => d( |
| 290 | "baseline_before_eval", |
| 291 | "missing: baselines must be run and frozen before any evaluation".into(), |
| 292 | ), |
| 293 | Some(b) => { |
| 294 | if b.get("required").and_then(Value::as_bool) != Some(true) { |
| 295 | d("baseline_before_eval.required", "must be true".into()); |
| 296 | } |
| 297 | match b.get("baseline_arms").and_then(Value::as_array) { |
| 298 | Some(list) if !list.is_empty() => { |
| 299 | let named: BTreeSet<&str> = list.iter().filter_map(Value::as_str).collect(); |
| 300 | for (i, x) in list.iter().enumerate() { |
| 301 | let field = format!("baseline_before_eval.baseline_arms[{i}]"); |
| 302 | match x.as_str() { |
| 303 | None => d(&field, "must be a string".into()), |
| 304 | Some(id) => match arms.iter().find(|a| a.id == id) { |
| 305 | None => d(&field, format!("\"{id}\" is not a declared arm")), |
| 306 | Some(a) if a.role.as_deref() != Some("baseline") => { |
| 307 | d(&field, format!("arm \"{id}\" does not have role baseline")) |
| 308 | } |
| 309 | Some(_) => {} |
| 310 | }, |
| 311 | } |
| 312 | } |
| 313 | for a in arms { |
| 314 | if a.role.as_deref() == Some("baseline") && !named.contains(a.id.as_str()) { |
| 315 | d( |
| 316 | "baseline_before_eval.baseline_arms", |
| 317 | format!("baseline arm \"{}\" is not listed", a.id), |
| 318 | ); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | _ => d( |
| 323 | "baseline_before_eval.baseline_arms", |
| 324 | "must be a non-empty array of arm ids".into(), |
| 325 | ), |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | let Some(phases) = top.get("phases").and_then(Value::as_array) else { |
| 330 | d("phases", "missing or not an array".into()); |
| 331 | return; |
| 332 | }; |
| 333 | let names: Vec<&str> = phases.iter().filter_map(Value::as_str).collect(); |
| 334 | let pos = |p: &str| names.iter().position(|n| *n == p); |
| 335 | let (freeze, baseline, eval) = (pos(PHASE_FREEZE), pos(PHASE_BASELINE), pos(PHASE_EVAL)); |
| 336 | for (p, at) in [ |
| 337 | (PHASE_FREEZE, freeze), |
| 338 | (PHASE_BASELINE, baseline), |
| 339 | (PHASE_EVAL, eval), |
| 340 | ] { |
| 341 | if at.is_none() { |
| 342 | d("phases", format!("missing {p}")); |
| 343 | } |
| 344 | } |
| 345 | if freeze.is_some_and(|f| f != 0) { |
| 346 | d("phases", format!("{PHASE_FREEZE} must be the first phase")); |
| 347 | } |
| 348 | if let (Some(b), Some(e)) = (baseline, eval) { |
| 349 | if b > e { |
| 350 | d( |
| 351 | "phases", |
| 352 | format!("{PHASE_BASELINE} must come before {PHASE_EVAL}"), |
| 353 | ); |
| 354 | } |
| 355 | } |
| 356 | if let Some(t) = pos(PHASE_TRAINING) { |
| 357 | if baseline.is_some_and(|b| t < b) { |
| 358 | d( |
| 359 | "phases", |
| 360 | format!("{PHASE_TRAINING} must come after {PHASE_BASELINE}"), |
| 361 | ); |
| 362 | } |
| 363 | if eval.is_some_and(|e| t > e) { |
| 364 | d( |
| 365 | "phases", |
| 366 | format!("{PHASE_TRAINING} must come before {PHASE_EVAL}"), |
| 367 | ); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | fn check_turing( |
| 373 | top: &Map<String, Value>, |
| 374 | status: Option<&str>, |
| 375 | arms: &[Arm], |
| 376 | d: &mut impl FnMut(&str, String), |
| 377 | ) { |
| 378 | let Some(t) = top.get("turing_profile") else { |
| 379 | d("turing_profile", "missing".into()); |
| 380 | return; |
| 381 | }; |
| 382 | match get(t, "G.background") { |
| 383 | None => d( |
| 384 | "turing_profile.G.background", |
| 385 | "missing: G must declare every adviser, model and toolchain".into(), |
| 386 | ), |
| 387 | Some(Value::Array(bg)) if bg.is_empty() => d( |
| 388 | "turing_profile.G.background", |
| 389 | "empty: G must declare every adviser, model and toolchain".into(), |
| 390 | ), |
| 391 | Some(Value::Array(bg)) => { |
| 392 | let mut ids = BTreeSet::new(); |
| 393 | for (i, e) in bg.iter().enumerate() { |
| 394 | let at = |f: &str| format!("turing_profile.G.background[{i}].{f}"); |
| 395 | for f in ["id", "kind", "owner", "revision"] { |
| 396 | if e.get(f).and_then(Value::as_str).is_none() { |
| 397 | d(&at(f), "missing".into()); |
| 398 | } |
| 399 | } |
| 400 | if let Some(id) = e.get("id").and_then(Value::as_str) { |
| 401 | if !ids.insert(id.to_string()) { |
| 402 | d(&at("id"), format!("duplicate background id \"{id}\"")); |
| 403 | } |
| 404 | } |
| 405 | if let Some(r) = e.get("revision").and_then(Value::as_str) { |
| 406 | if r == PENDING_PIN { |
| 407 | if status != Some("SCOPED") { |
| 408 | d( |
| 409 | &at("revision"), |
| 410 | format!("{PENDING_PIN} is allowed only while status is SCOPED"), |
| 411 | ); |
| 412 | } |
| 413 | } else if !is_pinned(r) { |
| 414 | d( |
| 415 | &at("revision"), |
| 416 | format!("\"{r}\" is not a commit id or sha256:<64 hex>"), |
| 417 | ); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | for (i, a) in arms.iter().enumerate() { |
| 422 | if a.needs_background && a.refs.is_empty() { |
| 423 | d( |
| 424 | &format!("arms[{i}].background_refs"), |
| 425 | format!( |
| 426 | "arm \"{}\" is pretrained or uses a teacher but declares no background", |
| 427 | a.id |
| 428 | ), |
| 429 | ); |
| 430 | } |
| 431 | for r in &a.refs { |
| 432 | if !ids.contains(r) { |
| 433 | d( |
| 434 | &format!("arms[{i}].background_refs"), |
| 435 | format!("\"{r}\" is not declared in turing_profile.G.background"), |
| 436 | ); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | Some(_) => d("turing_profile.G.background", "must be an array".into()), |
| 442 | } |
| 443 | match get(t, "B.arm").and_then(Value::as_str) { |
| 444 | None => d("turing_profile.B.arm", "missing".into()), |
| 445 | Some(b) => { |
| 446 | if !arms |
| 447 | .iter() |
| 448 | .any(|a| a.id == b && a.role.as_deref() == Some("baseline")) |
| 449 | { |
| 450 | d( |
| 451 | "turing_profile.B.arm", |
| 452 | format!("\"{b}\" is not a declared baseline arm"), |
| 453 | ); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | if get(t, "D.blinded").and_then(Value::as_bool) != Some(true) { |
| 458 | d("turing_profile.D.blinded", "must be true".into()); |
| 459 | } |
| 460 | for f in P_FIELDS { |
| 461 | if get(t, &format!("P.{f}")).and_then(Value::as_str).is_none() { |
| 462 | d(&format!("turing_profile.P.{f}"), "missing".into()); |
| 463 | } |
| 464 | } |
| 465 | for f in ["T_ideal", "T_real"] { |
| 466 | if get(t, &format!("{f}.definition")) |
| 467 | .and_then(Value::as_str) |
| 468 | .is_none() |
| 469 | { |
| 470 | d(&format!("turing_profile.{f}.definition"), "missing".into()); |
| 471 | } |
| 472 | } |
| 473 | match get(t, "tj.allowed") { |
| 474 | Some(Value::Bool(true)) => { |
| 475 | if get(t, "energy.status").and_then(Value::as_str) != Some("MEASURED") { |
| 476 | d( |
| 477 | "turing_profile.tj.allowed", |
| 478 | "T/J requires turing_profile.energy.status MEASURED".into(), |
| 479 | ); |
| 480 | } |
| 481 | } |
| 482 | Some(Value::Bool(false)) => {} |
| 483 | _ => d("turing_profile.tj.allowed", "must be true or false".into()), |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | fn check_sealing(top: &Map<String, Value>, d: &mut impl FnMut(&str, String)) { |
| 488 | let Some(s) = top.get("heldout").and_then(|h| h.get("sealing")) else { |
| 489 | d("heldout.sealing", "missing".into()); |
| 490 | return; |
| 491 | }; |
| 492 | if s.get("mechanism").and_then(Value::as_str).is_none() { |
| 493 | d("heldout.sealing.mechanism", "missing".into()); |
| 494 | } |
| 495 | for f in [ |
| 496 | "prediction_commit_before_reveal", |
| 497 | "judge_independent", |
| 498 | "single_use", |
| 499 | ] { |
| 500 | if s.get(f).and_then(Value::as_bool) != Some(true) { |
| 501 | d(&format!("heldout.sealing.{f}"), "must be true".into()); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | fn check_sample_size(top: &Map<String, Value>, d: &mut impl FnMut(&str, String)) { |
| 507 | let Some(s) = top.get("sample_size") else { |
| 508 | d("sample_size", "missing".into()); |
| 509 | return; |
| 510 | }; |
| 511 | if s.get("computed_with").and_then(Value::as_str).is_none() { |
| 512 | d("sample_size.computed_with", "missing".into()); |
| 513 | } |
| 514 | let studies = match s.get("studies").and_then(Value::as_array) { |
| 515 | Some(l) if !l.is_empty() => l, |
| 516 | _ => { |
| 517 | d("sample_size.studies", "must be a non-empty array".into()); |
| 518 | return; |
| 519 | } |
| 520 | }; |
| 521 | for (i, st) in studies.iter().enumerate() { |
| 522 | let at = |f: &str| format!("sample_size.studies[{i}].{f}"); |
| 523 | let mut n = std::collections::BTreeMap::new(); |
| 524 | for f in STUDY_NUMBERS { |
| 525 | match st.get(f).and_then(Value::as_f64).filter(|x| x.is_finite()) { |
| 526 | Some(x) => { |
| 527 | n.insert(f, x); |
| 528 | } |
| 529 | None => d(&at(f), "missing or not a finite number".into()), |
| 530 | } |
| 531 | } |
| 532 | if n.len() != STUDY_NUMBERS.len() { |
| 533 | continue; |
| 534 | } |
| 535 | if (n["tasks_planned"] * n["units_per_cluster"] - n["units_planned"]).abs() > EPS { |
| 536 | d( |
| 537 | &at("units_planned"), |
| 538 | format!( |
| 539 | "{} != tasks_planned * units_per_cluster = {}", |
| 540 | n["units_planned"], |
| 541 | n["tasks_planned"] * n["units_per_cluster"] |
| 542 | ), |
| 543 | ); |
| 544 | } |
| 545 | let deff = 1.0 + (n["units_per_cluster"] - 1.0) * n["icc_assumed"]; |
| 546 | if (deff - n["design_effect"]).abs() > EPS { |
| 547 | d( |
| 548 | &at("design_effect"), |
| 549 | format!( |
| 550 | "{} != 1 + (units_per_cluster - 1) * icc_assumed = {deff}", |
| 551 | n["design_effect"] |
| 552 | ), |
| 553 | ); |
| 554 | } |
| 555 | let eff = (n["units_planned"] / deff).floor(); |
| 556 | if (eff - n["effective_n_planned"]).abs() > EPS { |
| 557 | d( |
| 558 | &at("effective_n_planned"), |
| 559 | format!( |
| 560 | "{} != floor(units_planned / design_effect) = {eff}", |
| 561 | n["effective_n_planned"] |
| 562 | ), |
| 563 | ); |
| 564 | } |
| 565 | if eff < n["exact_min_effective_n"] { |
| 566 | d( |
| 567 | &at("effective_n_planned"), |
| 568 | format!( |
| 569 | "underpowered: {eff} < exact_min_effective_n {}", |
| 570 | n["exact_min_effective_n"] |
| 571 | ), |
| 572 | ); |
| 573 | } |
| 574 | if n["exact_power_at_planned"] < n["power_target"] { |
| 575 | d( |
| 576 | &at("exact_power_at_planned"), |
| 577 | format!( |
| 578 | "{} < power_target {}", |
| 579 | n["exact_power_at_planned"], n["power_target"] |
| 580 | ), |
| 581 | ); |
| 582 | } |
| 583 | if n["alpha_per_contrast"] * n["confirmatory_contrasts"] > n["alpha_familywise"] + EPS { |
| 584 | d( |
| 585 | &at("alpha_per_contrast"), |
| 586 | format!( |
| 587 | "{} * {} contrasts exceeds alpha_familywise {}", |
| 588 | n["alpha_per_contrast"], n["confirmatory_contrasts"], n["alpha_familywise"] |
| 589 | ), |
| 590 | ); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | fn check_stopping_rules(top: &Map<String, Value>, d: &mut impl FnMut(&str, String)) { |
| 596 | match top.get("stopping_rules").and_then(Value::as_array) { |
| 597 | Some(list) if !list.is_empty() => { |
| 598 | for (i, r) in list.iter().enumerate() { |
| 599 | for f in ["id", "trigger", "action"] { |
| 600 | if r.get(f).and_then(Value::as_str).is_none() { |
| 601 | d(&format!("stopping_rules[{i}].{f}"), "missing".into()); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | _ => d("stopping_rules", "must be a non-empty array".into()), |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | fn enum_field<'a>( |
| 611 | top: &'a Map<String, Value>, |
| 612 | field: &str, |
| 613 | allowed: &[&str], |
| 614 | d: &mut impl FnMut(&str, String), |
| 615 | ) -> Option<&'a str> { |
| 616 | match top.get(field) { |
| 617 | None => { |
| 618 | d(field, "missing".into()); |
| 619 | None |
| 620 | } |
| 621 | Some(Value::String(s)) if allowed.contains(&s.as_str()) => Some(s.as_str()), |
| 622 | Some(Value::String(s)) => { |
| 623 | d( |
| 624 | field, |
| 625 | format!("\"{s}\" is not one of {}", allowed.join(", ")), |
| 626 | ); |
| 627 | None |
| 628 | } |
| 629 | Some(_) => { |
| 630 | d(field, "must be a string".into()); |
| 631 | None |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /// A 40-character commit id or `sha256:` followed by 64 lowercase hex characters. |
| 637 | fn is_pinned(r: &str) -> bool { |
| 638 | is_commit(r) |
| 639 | || r.strip_prefix("sha256:").is_some_and(|h| { |
| 640 | h.len() == 64 && h.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) |
| 641 | }) |
| 642 | } |