import { useEffect, useRef, useState } from "react"; import { Eye, GraduationCap, RotateCcw, Target } from "lucide-react"; import { gloss } from "../../lib/glossary"; import { markLearnLabDone } from "../../lib/learnProgress"; import { applyCX, applyH2, applyReadoutNoise, measureOnce, mulberry32, zero2, } from "../../lib/quantumToy"; import { wilson95 } from "../../lib/stats"; import { useApp } from "../../lib/store"; import { TipIcon } from "../TipIcon"; /** * Step 5 — "How many looks is enough?" Step 4's noisy Bell agreement * score (eps pinned at 6%) becomes the quantity under estimation, and * the display is a LIVE mini funnel in the Evidence Theater's exact * visual grammar — per-batch vertical 95% Wilson interval bars * (lib/stats.ts wilson95, THE system-wide interval), a shaded * convergence envelope, an ink point path, opacity ramping toward * now — at postcard size (460×200 viewBox), so when the learner meets * the real funnel it already reads as a known object. Fixed y-domain: * the axis never re-zooms under the viewer (same theater rule). * * "auto to ±2pp" is optional stopping made tactile: batches of 50 * until the Wilson half-width ≤ 2pp (at the true score ≈ 88.4% that * lands near 1000 looks, comfortably inside the 2000-look budget), * then a stop line — the workbench's precision target, one step * early. Reduced motion: the auto run lands in one synchronous * update. Seeded rng → the whole trajectory replays exactly. * * The handoff footer (marker: learn-complete) closes the loop: mark * quda.learnLabDone, then either open the guided lessons directly on * L1 (store bridge pendingLessonKey) or just close the overlay. */ const SEED = 20260705; const EPS = 0.06; const BATCH = 50; const BUDGET = 2000; const TARGET_HALF = 0.02; const BELL = applyCX(applyH2(zero2(), 0)); interface Frame { looks: number; point: number; lo: number; hi: number; } // funnel geometry — theater grammar at postcard size const W = 460; const H = 200; const M = { l: 44, r: 12, t: 12, b: 26 }; const Y_LO = 0.7; const Y_HI = 1.0; const xOf = (looks: number) => M.l + (looks / BUDGET) * (W - M.l - M.r); const yOf = (p: number) => M.t + (1 - (Math.min(Y_HI, Math.max(Y_LO, p)) - Y_LO) / (Y_HI - Y_LO)) * (H - M.t - M.b); const fmtPp = (half: number) => (half * 100).toFixed(1); export function Step5Certainty() { const [frames, setFrames] = useState([]); const [stopped, setStopped] = useState<{ looks: number; reached: boolean; } | null>(null); const [auto, setAuto] = useState(false); const rng = useRef(mulberry32(SEED)); const matchesRef = useRef(0); const looksRef = useRef(0); const timer = useRef | null>(null); useEffect( () => () => { if (timer.current) clearInterval(timer.current); }, [], ); const oneBatch = (): Frame => { for (let i = 0; i < BATCH; i++) { const ideal = measureOnce(BELL, rng.current); const shown = applyReadoutNoise(ideal, EPS, rng.current); if (shown === ideal) matchesRef.current += 1; } looksRef.current += BATCH; const [lo, hi] = wilson95(matchesRef.current, looksRef.current); return { looks: looksRef.current, point: matchesRef.current / looksRef.current, lo, hi, }; }; const spent = looksRef.current >= BUDGET; const look50 = () => { if (auto || spent) return; const fr = oneBatch(); setFrames((f) => [...f, fr]); }; const autoTo2pp = () => { if (auto || spent || stopped != null) return; const step = (): boolean => { const fr = oneBatch(); setFrames((f) => [...f, fr]); const half = (fr.hi - fr.lo) / 2; if (half <= TARGET_HALF || fr.looks >= BUDGET) { setStopped({ looks: fr.looks, reached: half <= TARGET_HALF }); return true; } return false; }; const reduced = typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches; if (reduced) { // Sync path: the whole accumulation in one update. while (!step()) { /* batches until the target or the budget */ } return; } setAuto(true); timer.current = setInterval(() => { if (step()) { if (timer.current) clearInterval(timer.current); timer.current = null; setAuto(false); } }, 120); }; const reset = () => { if (timer.current) clearInterval(timer.current); timer.current = null; setAuto(false); setFrames([]); setStopped(null); rng.current = mulberry32(SEED); matchesRef.current = 0; looksRef.current = 0; }; const handoff = (toLessons: boolean) => { markLearnLabDone(); const app = useApp.getState(); app.setLearnLabOpen(false); if (toLessons) { app.setPendingLessonKey("L1"); app.setLessonsOpen(true); } }; const last = frames.length > 0 ? frames[frames.length - 1] : null; const lastHalf = last ? (last.hi - last.lo) / 2 : null; const env = frames.length > 1 ? frames.map((f) => `${xOf(f.looks)},${yOf(f.hi)}`).join(" ") + " " + [...frames] .reverse() .map((f) => `${xOf(f.looks)},${yOf(f.lo)}`) .join(" ") : ""; return (
agreement score vs looks — noisy machine, 6% noise{" "}
{/* y gridlines, fixed domain (the axis never re-zooms) */} {[0.7, 0.8, 0.9, 1.0].map((g) => ( {Math.round(g * 100)}% ))} {/* x axis: the looks budget */} {[0, 500, 1000, 1500, 2000].map((t) => ( {t === 0 ? "0 looks" : t} ))} {/* convergence envelope — the funnel silhouette */} {frames.length > 1 && ( )} {/* per-batch Wilson intervals, opacity ramping toward now */} {frames.map((f, i) => ( {`after ${f.looks} looks: ${(f.point * 100).toFixed(1)}%, 95% interval ± ${fmtPp((f.hi - f.lo) / 2)}pp`} ))} {/* point path */} {frames.length > 1 && ( `${xOf(f.looks)},${yOf(f.point)}`) .join(" ")} fill="none" stroke="rgb(var(--color-ink))" strokeWidth={1} opacity={0.55} /> )} {frames.map((f, i) => ( ))} {/* the stop line — optional stopping made visible */} {stopped?.reached && ( stopped: ±2pp )}

{last == null ? ( <>no looks yet — every look costs, so buy them in batches of 50 ) : stopped?.reached ? ( <> stopped: ±2pp reached after{" "} {stopped.looks} looks — the rest of the budget is yours to keep ) : stopped != null ? ( <> budget spent at {stopped.looks} looks before ±2pp — sometimes certainty costs more than you have ) : ( <> after {last.looks}{" "} looks: {Math.round(last.point * 100)}% {" "} ± {fmtPp(lastHalf ?? 0)} pp — still narrowing )}

{/* handoff — the whole track lands here (marker: learn-complete) */}

That is the whole workbench:{" "} real pipelines, honest intervals, evidence you can replay.

); }