import { useEffect, useRef, useState } from "react"; import { Check, ChevronLeft, ChevronRight, GraduationCap, Loader2, Play, X, } from "lucide-react"; import { gloss } from "../lib/glossary"; import { LESSONS_DONE_LS as DONE_LS } from "../lib/learnProgress"; import { LESSONS, type Lesson, type LessonKey } from "../lib/lessons"; import { listRuns } from "../lib/runStore"; import { useApp } from "../lib/store"; import { TipIcon } from "./TipIcon"; /** * "Learn the basics" — the guided-lessons overlay (lib/lessons.ts). * * A small dismissable card, fixed bottom-right (z-40, under the mobile * drawers at z-50 and clear of the phone Run-FAB via bottom-20). It is * NOT a separate mode: each lesson step drives the normal app through * the same pendingRestore/auto-run bridges scenario boots use, and the * card floats over whatever view that produced. Flow per lesson: * question → [Run it] → whatToLookFor once the run archives → next * step / next lesson. Lesson runs carry RunRecord.scenario = "L1"…"L4" * so they never pollute evidence pools (same exclusion the F-scenario * pickers apply). * * Run-completion detection: historyVersion bumps when the auto-run * archives; we then read back the lesson-tagged record (its run_id + * root_seed feed L2's comparison selection and L4's seed replay). * Progress persists in localStorage "quda.lessonsDone". */ function loadDone(): Set { try { const raw = JSON.parse(localStorage.getItem(DONE_LS) ?? "[]"); return new Set(Array.isArray(raw) ? (raw as LessonKey[]) : []); } catch { return new Set(); } } export function LessonCard() { const open = useApp((s) => s.lessonsOpen); const setOpen = useApp((s) => s.setLessonsOpen); const running = useApp((s) => s.running); const historyVersion = useApp((s) => s.historyVersion); const [activeKey, setActiveKey] = useState(null); const [stepIdx, setStepIdx] = useState(0); const [phase, setPhase] = useState<"ask" | "running" | "look">("ask"); const [done, setDone] = useState>(loadDone); const baseHv = useRef(0); // Runs THIS lesson produced (reset on lesson switch) — L2's compare // step and L4's replay step resolve ids/seeds from here. const lessonRuns = useRef<{ run_id: string; root_seed: number | null }[]>([]); const lesson: Lesson | null = activeKey != null ? (LESSONS.find((l) => l.key === activeKey) ?? null) : null; const markDone = (key: LessonKey) => setDone((prev) => { const next = new Set(prev).add(key); try { localStorage.setItem(DONE_LS, JSON.stringify([...next])); } catch { /* private mode — session-scoped progress is fine */ } return next; }); const pickLesson = (key: LessonKey | null) => { setActiveKey(key); setStepIdx(0); setPhase("ask"); lessonRuns.current = []; }; // Handoff bridge: the learn-from-zero track asks us to open directly // on a lesson (Step5Certainty sets pendingLessonKey before flipping // lessonsOpen). Consume-and-clear, one shot. const pendingKey = useApp((s) => s.pendingLessonKey); useEffect(() => { if (!open || pendingKey == null) return; if (LESSONS.some((l) => l.key === pendingKey)) pickLesson(pendingKey as LessonKey); useApp.getState().setPendingLessonKey(null); // eslint-disable-next-line react-hooks/exhaustive-deps }, [open, pendingKey]); const launch = (l: Lesson, idx: number) => { const step = l.steps[idx]; const first = lessonRuns.current[0]; const app = useApp.getState(); // Lessons run in the Pipeline editor — leave the board if needed. app.setWorkspaceMode("compose"); if (step.openGateDiff) app.setGateDiffDefaultOpen(true); app.requestRestore({ graph: step.graph, sampleKey: step.sampleKey, pinSeed: step.seed === "pinned" ? (step.pinSeed ?? null) : step.seed === "replayFirst" ? (first?.root_seed ?? null) : null, // replayFirst threads the ancestor's run_id → forked_from → // the fork edge L4 points at. sourceRunId: step.seed === "replayFirst" ? (first?.run_id ?? null) : null, precisionTarget: null, autoRunAfter: true, scenario: l.key, }); if (step.evidenceTab) app.setPendingEvidenceTab(step.evidenceTab); if (step.expandEvidence) app.bumpHintExpandRightPane(); baseHv.current = app.historyVersion; setStepIdx(idx); setPhase("running"); }; // Completion watcher — see the header comment. useEffect(() => { if (phase !== "running" || lesson == null) return; if (historyVersion <= baseHv.current) return; let cancelled = false; (async () => { const runs = await listRuns(50); const mine = runs .filter((r) => r.scenario === lesson.key) .sort((a, b) => b.created_at - a.created_at); if (cancelled) return; const latest = mine[0]; if (latest) lessonRuns.current.push({ run_id: latest.run_id, root_seed: latest.root_seed ?? null, }); const step = lesson.steps[stepIdx]; if (step.compareLessonRuns && lessonRuns.current.length >= 2) { const ids = lessonRuns.current.slice(-2).map((r) => r.run_id); const app = useApp.getState(); app.setCompareIds(ids); app.setPendingEvidenceTab("compare"); app.bumpHintExpandRightPane(); } if (stepIdx === lesson.steps.length - 1) markDone(lesson.key); setPhase("look"); })(); return () => { cancelled = true; }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [historyVersion, phase, lesson, stepIdx]); if (!open) return null; const nextLesson = LESSONS.find((l) => !done.has(l.key) && l.key !== activeKey); return (
{lesson ? lesson.title : "Learn the basics"} {lesson && ( )}
{lesson == null ? (
{LESSONS.map((l, i) => ( ))}

Four tiny experiments, one question each. Lesson runs are tagged and never count as your evidence.

) : (
{/* back-pointer for learn-from-zero graduates: L1 is the designed landing spot of the step-5 handoff */} {lesson.key === "L1" && (

Fresh from the basics? This one runs the real thing — a live pipeline, archived like any run.

)}

{lesson.question}

{lesson.terms.map((t) => ( {t} ))}
{phase === "look" && (

{lesson.whatToLookFor}

)}
{phase === "running" ? ( Running… ) : phase === "ask" || stepIdx + 1 < lesson.steps.length ? ( ) : nextLesson ? ( ) : ( )}
)}
); }