Files
learnharness/components/artifacts/probe-done-card.tsx
T
amosblomqvist 4ba594bab5 fix(arch): parse, don't cast — the UI parse seam for model-authored input
The architectural answer to the validation-crash question: validation IS
first at the model-facing boundary (pi validates arguments before
execute; failures return to the model same-turn — all three live
incidents retried successfully). But the same bytes also exist UNVALIDATED
by design in two other places: the live wire streams raw tool-input
deltas DURING generation, and the transcript keeps the raw input of
REJECTED calls (honesty + the replay invariant). The consumers were
supposed to trust part STATES — but that trust was enforced by
TypeScript casts ('part.input as X'), which are compile-time lies with
no runtime check behind them. That is the hole both crashes came
through, and it should not have been possible.

The structural rule now: model-authored INPUT is consumed only through
parsers running the SAME zod schema the model faced — boundary-valid ⇔
consumer-parseable, mechanically. Server/learner-authored OUTPUT stays
trusted (our code authors it).

- src/core/tool-parts.ts (new): parseQuizInput (display parse over the
  shared quizInputBase — strips the teacher-only nodeId, accepts both
  prober and teacher boundary shapes), parseAskInput, parseProbeDoneInput.
- contracts/probe.ts: probeDoneInputSchema is now core-owned zod — the
  probe_done tool declares it (single source of truth), the seam runs
  the literal same schema.
- question-dock.tsx: the dock parses its input at entry; unparsable
  renders the quiet malformed-question fallback, never a crash.
- probe-done-card.tsx: the input preview goes through the parse; the
  old state reasoning and shape guards collapse into it.

Pinned in tests/tool-parts.test.ts: both exact live crash shapes
(stringified quiz options, stringified assessment), boundary-parity
cases for all three schemas, nodeId stripping, mid-stream partials;
render-cards pins the dock fallback markup.

Gates: tsc, lint, build, 328/328.
2026-09-12 04:51:39 +03:00

68 lines
3.1 KiB
TypeScript

"use client";
import { ClipboardCheckIcon } from "lucide-react";
import { parseProbeDoneInput, type ProbeDoneResult } from "@/src/core";
/**
* The probe conclusion card (docs/probe-teach-split.md §2): renders the
* prober's probe_done call. The INPUT is the learner assessment — strand by
* strand, where his edge sits — and it belongs to the learner as much as to
* the transcript: it streams live while the prober writes it and stays as
* the handoff artifact. The OUTPUT is the seam's ack (ok) or the refusal
* 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; state?: unknown } }) {
const output = part.output as ProbeDoneResult | undefined;
const state = typeof part.state === "string" ? part.state : "";
// Parse, don't cast (the seam rule, src/core/tool-parts.ts): the model
// authored this input — it is consumer-trustworthy only if the SAME
// schema the tool declared says so. The parse subsumes the old state
// reasoning and shape guards: an errored call's stringified assessment,
// a mid-stream partial — anything that doesn't parse falls to the
// fallback branch. Output stays a cast: OUR execute authored it.
const input = parseProbeDoneInput(part.input);
const assessment = output?.ok ? output.assessment : input?.assessment;
const strands = assessment && Array.isArray(assessment.strands) ? assessment.strands : null;
return (
<div data-testid="probe-done-card" className="my-3 rounded-xl border bg-card/60 p-3">
<div className="mb-2 flex items-center gap-2 text-xs font-medium uppercase tracking-wide text-muted-foreground">
<ClipboardCheckIcon className="size-3.5 text-primary" />
Probe concluded — your assessment
</div>
{output && !output.ok ? (
<p className="text-sm text-muted-foreground">The prober retried: {output.error}</p>
) : state === "output-error" ? (
<p className="text-sm text-muted-foreground">
The assessment was malformed — the prober retried with a corrected one.
</p>
) : strands ? (
<div className="space-y-2">
{strands.map((s, i) => (
<div key={i} className="rounded-lg bg-background/60 px-3 py-2">
<div className="text-sm font-medium">{s.strand}</div>
<div className="mt-0.5 text-sm text-muted-foreground">{s.assessment}</div>
</div>
))}
{assessment?.summary && (
<p className="px-1 text-sm text-muted-foreground">{assessment.summary}</p>
)}
</div>
) : (
<p className="text-sm text-muted-foreground">Writing the assessment…</p>
)}
</div>
);
}