| 448 | // Repeated A/A comparisons check the intervals empirically on this machine: with no |
| 449 | // real difference, an interval should exclude zero at most at its nominal error rate. |
| 450 | export async function validate(options) { |
| 451 | if (options.blocks < 6) { |
| 452 | throw new Error('validate needs at least 6 blocks, the fewest that can form a 95% interval.'); |
| 453 | } |
| 454 | let runs = []; |
| 455 | for (let i = 0; i < options.runs; i++) { |
| 456 | console.log(`\nA/A validation run ${i + 1}/${options.runs}`); |
| 457 | // A different seed per run varies the arm order and the gaps between launches. |
| 458 | let {result, dir} = await run({...options, candidate: options.base, seed: options.seed + i}); |
| 459 | runs.push({dir, comparisons: result.comparisons}); |
| 460 | } |
| 461 | let intervals = runs.flatMap(run => |
| 462 | Object.entries(run.comparisons).map(([scenario, comparison]) => ({scenario, ...comparison})) |
| 463 | ); |
| 464 | let excluded = intervals.filter(({interval}) => interval[0] > 0 || interval[1] < 0); |
| 465 | let expected = |
| 466 | intervals.reduce((total, {confidence}) => total + (1 - confidence), 0) / intervals.length; |
| 467 | let tail = binomialTail(excluded.length, intervals.length, expected); |
| 468 | let percent = value => `${(value * 100).toFixed(1)}%`; |
| 469 | let lines = [ |
| 470 | '# A/A interval validation', |
| 471 | '', |
| 472 | `${options.runs} A/A runs of ${options.base} · ${options.blocks} blocks × ${options.samples} trials · ${options.cpu}× CPU · ${options.scale}× workload`, |
| 473 | '', |
| 474 | `Intervals excluding zero: ${excluded.length} of ${intervals.length} (${percent(excluded.length / intervals.length)}); expected at most ${percent(expected)}.`, |
| 475 | '', |
| 476 | `Probability of at least this many by chance: ${tail.toFixed(3)}. ${tail < 0.05 ? '**The intervals look too narrow on this machine; treat verdicts with caution and investigate order or position effects.**' : 'Consistent with the nominal error rate.'}`, |
| 477 | '', |
| 478 | 'Scenarios in the same run share browser launches, so they are not fully independent and the probability is approximate.', |
| 479 | '', |
| 480 | '| Scenario | Excluded zero | Within threshold | Inconclusive | Improvement | Regression |', |
| 481 | '|---|---:|---:|---:|---:|---:|' |
| 482 | ]; |
| 483 | for (let scenario of [...new Set(intervals.map(({scenario}) => scenario))]) { |
| 484 | let rows = intervals.filter(row => row.scenario === scenario); |
| 485 | let count = verdict => rows.filter(row => row.verdict === verdict).length; |
| 486 | lines.push( |
| 487 | `| ${scenario} | ${excluded.filter(row => row.scenario === scenario).length}/${rows.length} | ${count('within practical threshold')} | ${count('inconclusive')} | ${count('improvement detected')} | ${count('regression detected')} |` |
| 488 | ); |
| 489 | } |
| 490 | lines.push('', 'Runs:', '', ...runs.map(run => `- ${run.dir}`), ''); |
| 491 | let dir = path.join( |
| 492 | local, |
| 493 | 'results', |
| 494 | `validation-${new Date().toISOString().replaceAll(/[:.]/g, '-')}` |
| 495 | ); |
| 496 | await fs.mkdir(dir, {recursive: true}); |
| 497 | let markdown = lines.join('\n'); |
| 498 | await fs.writeFile(path.join(dir, 'summary.md'), markdown); |
| 499 | console.log(`\n${markdown}\nSaved ${dir}`); |
| 500 | return {intervals, excluded: excluded.length, expected, tail, dir}; |
| 501 | } |
| 502 | |
| 503 | // Runs every selected scenario through the full comparison path, as an A/A compare, |
| 504 | // to check that each action, completion condition, and report section still works. |