From e821c3277dcdb38ff8e881044b1e0fa50bfe67f2 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Sat, 12 Sep 2026 04:45:58 +0300 Subject: [PATCH] fix: multi-select Other coexistence + the malformed-assessment probe-done crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two live bugs from the modal-realism stress runs: 1. Multi-select ask: picking the custom 'Other…' row cleared every already-picked option, and picking any option cleared a typed custom answer — single-select displacement logic applied to both modes. The dock's selection is now a pure state machine in question-core (toggleOption/toggleOther over DockSelection, unit-tested): single displaces (unchanged), multi coexists — neither channel clears the other; the typed text always survives. 2. ProbeDoneCard crashed on a REJECTED probe_done call: the model stringified 'assessment' (the same double-encoding habit as the options bug), zod rejected the call, the model retried — but the card's input-preview fallback trusted the errored part's unvalidated input shape and died on assessment.strands.map. The preview now reads input only while the call is pending or streaming (guarded with Array.isArray); an output-error part renders a quiet malformed-and-retried note instead of the garbage. Pinned: question-core.test.ts (3 dock-selection machine cases), render-cards.test.tsx (4 probe-done card cases incl. the exact live stringified shape, plus the multi-select ask markup pin). Gates: tsc, lint, build, 315/315. --- components/artifacts/probe-done-card.tsx | 28 +++++++-- components/question-dock.tsx | 71 ++++++++++------------- src/core/question-core.ts | 38 ++++++++++++ tests/question-core.test.ts | 50 ++++++++++++++++ tests/render-cards.test.tsx | 74 ++++++++++++++++++++++++ 5 files changed, 215 insertions(+), 46 deletions(-) diff --git a/components/artifacts/probe-done-card.tsx b/components/artifacts/probe-done-card.tsx index 6ee92ae..33b7e10 100644 --- a/components/artifacts/probe-done-card.tsx +++ b/components/artifacts/probe-done-card.tsx @@ -12,13 +12,27 @@ import type { ProbeDoneResult, ProbeDoneInput } from "@/src/core"; * the prober retried on (rendered quiet and small — it is model-facing * bookkeeping, kept only for transcript honesty). * + * The input preview is only trustworthy while the call is pending — an + * input-available input cleared schema validation at the boundary, and a + * mid-stream partial is guarded below. An output-error part's input was + * REJECTED by validation and can be any shape (live crash: the model + * stringified `assessment`); never render it. + * * Deliberately plain v1: the proper assessment viewer design is deferred to * the UI wave; this card just guarantees the data is never invisible. */ -export function ProbeDoneCard({ part }: { part: { input?: unknown; output?: unknown } }) { +export function ProbeDoneCard({ part }: { part: { input?: unknown; output?: unknown; state?: unknown } }) { const input = part.input as ProbeDoneInput | undefined; const output = part.output as ProbeDoneResult | undefined; - const assessment = output?.ok ? output.assessment : (input?.assessment ?? null); + const state = typeof part.state === "string" ? part.state : ""; + + const assessment = + output?.ok + ? output.assessment + : state === "output-error" + ? null // the rejected call — its input is unvalidated garbage + : (input?.assessment ?? null); + const strands = assessment && Array.isArray(assessment.strands) ? assessment.strands : null; return (
@@ -28,15 +42,19 @@ export function ProbeDoneCard({ part }: { part: { input?: unknown; output?: unkn
{output && !output.ok ? (

The prober retried: {output.error}

- ) : assessment ? ( + ) : state === "output-error" ? ( +

+ The assessment was malformed — the prober retried with a corrected one. +

+ ) : strands ? (
- {assessment.strands.map((s, i) => ( + {strands.map((s, i) => (
{s.strand}
{s.assessment}
))} - {assessment.summary && ( + {assessment?.summary && (

{assessment.summary}

)}
diff --git a/components/question-dock.tsx b/components/question-dock.tsx index daa18fe..789ae31 100644 --- a/components/question-dock.tsx +++ b/components/question-dock.tsx @@ -8,16 +8,21 @@ import { cn } from "@/lib/utils"; import { buildAnswer, coerceCorrectAnswer, + emptyDockSelection, normalizeOptions, resolveCorrect, seededRandom, shuffleOptions, + toggleOption, + toggleOther, type AnswerRequest, type AskToolInput, + type DockSelection, type QuestionAnswerData, type QuestionData, type QuestionMode, type QuizToolInput, + type SelectMode, type Phase3, } from "@/src/core"; @@ -211,42 +216,30 @@ function QuestionDockForm({ return q; }, [toolCallId, kind, input]); - const [selected, setSelected] = useState>(new Set()); // 0-based indices - const [dontKnow, setDontKnow] = useState(false); - // "Other" is selected by typing into its inline input (or clicking its - // circle). Picking a sibling option deselects it, but the typed text - // survives — the learner's words are never thrown away. - const [otherSelected, setOtherSelected] = useState(false); + // The selection state machine lives in question-core (pure, tested): + // single-select displaces, multi-select coexists — picking the ask + // "Other…" row in a multi-select must never clear already-picked + // options, and picking an option must never clear a typed custom answer. + const [sel, setSel] = useState(emptyDockSelection()); // The learner's transfer channel — thinking, never "a note". const [thinking, setThinking] = useState(""); const [other, setOther] = useState(""); const [text, setText] = useState(""); + const selectMode: SelectMode = qd.mode === "multi-select" ? "multi-select" : "single-select"; - const toggle = (idx: number) => { - setDontKnow(false); - setOtherSelected(false); - setSelected((prev) => { - if (qd.mode === "multi-select") { - const next = new Set(prev); - if (next.has(idx)) next.delete(idx); - else next.add(idx); - return next; - } - return new Set([idx]); - }); - }; + const toggle = (idx: number) => setSel(toggleOption(selectMode, sel, idx)); const canSubmit = qd.mode === "text" ? text.trim().length > 0 - : dontKnow || (otherSelected && other.trim().length > 0) || selected.size > 0; + : sel.dontKnow || (sel.otherSelected && other.trim().length > 0) || sel.selected.size > 0; const submit = () => { if (!canSubmit) return; const req: AnswerRequest = { - dontKnow, - values: [...selected].map((i) => qd.options[i]!.value), - other: otherSelected ? other : undefined, + dontKnow: sel.dontKnow, + values: [...sel.selected].map((i) => qd.options[i]!.value), + other: sel.otherSelected ? other : undefined, text: qd.mode === "text" ? text : undefined, note: qd.mode !== "text" ? thinking.trim() || undefined : undefined, }; @@ -255,11 +248,7 @@ function QuestionDockForm({ // Selecting Other never clears the thinking field — two channels, the // answer and the reasoning behind it, side by side. - const selectOther = () => { - setDontKnow(false); - setSelected(new Set()); - setOtherSelected(true); - }; + const selectOther = () => setSel(toggleOther(selectMode, sel)); return (
@@ -281,7 +270,7 @@ function QuestionDockForm({ {qd.mode !== "text" && (
{qd.options.map((opt, i) => { - const isSel = selected.has(i); + const isSel = sel.selected.has(i); return (