fix(quiz): report quiz results once and keep one authoritative badge

The quiz-results report no longer depends on judge verdicts, so a verdict
that arrives after the quiz is complete no longer posts another
[Quiz Performance] chat message. The verdict is stored through the entry
PATCH, and the server now keeps that judged grade on later re-posts.

When exact matching decided the grade, the verdict summary hides its result
badge and points, so 'Correct' and 'Incorrect' no longer show together.

parseQuizVerdict now only checks the shape (known result, boolean final,
usable steps) and trusts the server's final/is_correct/points.

Signed-off-by: Benjamin Schwing <63053132+BenjiAvH@users.noreply.github.com>
This commit is contained in:
Benjamin Schwing
2026-11-24 21:59:38 +00:00
parent 67db2e8dd7
commit 56bb06e6af
5 changed files with 98 additions and 54 deletions

View File

@@ -32,29 +32,34 @@ const REASON_LABELS: Record<QuizVerdictReason, string> = {
export default function QuizVerdictSummary({
verdict,
showResult = true,
}: {
verdict: QuizVerdict;
/** False when exact matching, not the judge, decided the grade. */
showResult?: boolean;
}) {
const { t } = useTranslation();
const hasPoints =
verdict.awarded_points !== undefined && verdict.max_points !== undefined;
return (
<div className="mb-2 space-y-1.5" data-testid="quiz-verdict">
<div className="flex flex-wrap items-center gap-2">
<span
className={`rounded-md px-2 py-0.5 text-[11px] font-semibold ${RESULT_STYLES[verdict.result]}`}
>
{t(RESULT_LABELS[verdict.result])}
</span>
{hasPoints && (
<span className="text-[11px] font-medium text-[var(--muted-foreground)]">
{t("{{awarded}} / {{max}} points", {
awarded: verdict.awarded_points,
max: verdict.max_points,
})}
{showResult && (
<div className="flex flex-wrap items-center gap-2">
<span
className={`rounded-md px-2 py-0.5 text-[11px] font-semibold ${RESULT_STYLES[verdict.result]}`}
>
{t(RESULT_LABELS[verdict.result])}
</span>
)}
</div>
{hasPoints && (
<span className="text-[11px] font-medium text-[var(--muted-foreground)]">
{t("{{awarded}} / {{max}} points", {
awarded: verdict.awarded_points,
max: verdict.max_points,
})}
</span>
)}
</div>
)}
{verdict.reason && (
<div className="text-[11px] text-[var(--muted-foreground)]">
{t(REASON_LABELS[verdict.reason])}

View File

@@ -453,6 +453,9 @@ export default function QuizViewer({
? getAssessment(q, ans, judgments[idx]?.verdict).result
: undefined;
// Reported once per set of answers, with the submit-time grade: each post
// adds a ``[Quiz Performance]`` chat message, so a verdict that arrives
// later is stored through the entry PATCH only (the server keeps it).
const submittedResults = useMemo(
() =>
questions.flatMap((question, questionIdx) => {
@@ -468,15 +471,11 @@ export default function QuizViewer({
correct_answer: question.correct_answer,
explanation: question.explanation ?? "",
difficulty: question.difficulty ?? "",
...getAssessment(
question,
answer,
judgments[questionIdx]?.verdict,
),
...getAssessment(question, answer, null),
},
];
}),
[answers, judgments, questions],
[answers, questions],
);
useEffect(() => {
@@ -1417,7 +1416,11 @@ export default function QuizViewer({
)}
</div>
{judgment.verdict && !judgment.isStreaming && (
<QuizVerdictSummary verdict={judgment.verdict} />
<QuizVerdictSummary
verdict={judgment.verdict}
// Exact matching decided: no contradicting badge.
showResult={judgedResult !== undefined}
/>
)}
{judgment.error ? (
<div className="rounded-md border border-red-200 bg-red-50 px-2 py-1 text-[12px] text-red-700 dark:border-red-950/50 dark:bg-red-950/20 dark:text-red-300">

View File

@@ -42,12 +42,20 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
/** Validate a verdict from the wire or the notebook; ``null`` if unusable. */
/**
* Check the shape of a verdict from the wire or the notebook; ``null`` if
* unusable. The server owns ``final``/``is_correct``/points (and the entry
* PATCH rejects inconsistent verdicts), so they are not re-derived here.
*/
export function parseQuizVerdict(value: unknown): QuizVerdict | null {
if (!isRecord(value) || typeof value.result !== "string") return null;
if (!RESULTS.has(value.result)) return null;
const result = value.result as QuizVerdictResult;
const final = value.final === true && result !== "ungraded";
if (
!isRecord(value) ||
typeof value.result !== "string" ||
!RESULTS.has(value.result) ||
typeof value.final !== "boolean"
) {
return null;
}
const steps = Array.isArray(value.steps)
? value.steps.flatMap((step) =>
isRecord(step) &&
@@ -64,15 +72,9 @@ export function parseQuizVerdict(value: unknown): QuizVerdict | null {
)
: [];
const verdict: QuizVerdict = {
result: final ? result : "ungraded",
// Same mapping as ``Verdict.receipt``: partial is neither right nor wrong.
is_correct:
final && result === "correct"
? true
: final && result === "incorrect"
? false
: null,
final,
result: value.result as QuizVerdictResult,
is_correct: typeof value.is_correct === "boolean" ? value.is_correct : null,
final: value.final,
confidence: typeof value.confidence === "number" ? value.confidence : 0,
steps,
};
@@ -80,12 +82,8 @@ export function parseQuizVerdict(value: unknown): QuizVerdict | null {
verdict.feedback = value.feedback;
}
if (
final &&
typeof value.awarded_points === "number" &&
typeof value.max_points === "number" &&
value.max_points > 0 &&
value.awarded_points >= 0 &&
value.awarded_points <= value.max_points
typeof value.max_points === "number"
) {
verdict.awarded_points = value.awarded_points;
verdict.max_points = value.max_points;

View File

@@ -397,13 +397,40 @@ describe("QuizViewer smoke", () => {
});
expect(screen.getByTestId("quiz-verdict")).toHaveTextContent("Correct");
expect(screen.getByText("Light reaction")).toBeInTheDocument();
// The single question is complete: results carry the judged result.
await waitFor(() => {
const calls = mocks.recordQuizResults.mock.calls;
expect(calls.at(-1)?.[1]).toEqual([
expect.objectContaining({ is_correct: true, result: "correct" }),
]);
// Results were reported once, when the quiz was complete. The verdict is
// stored through the entry PATCH; it must not post another
// ``[Quiz Performance]`` chat message.
expect(mocks.recordQuizResults).toHaveBeenCalledTimes(1);
expect(mocks.recordQuizResults.mock.calls[0][1]).toEqual([
expect.objectContaining({ is_correct: false, result: "ungraded" }),
]);
});
it("shows one result badge when exact matching and the judge disagree", async () => {
const user = userEvent.setup();
renderViewer([choiceQuestion]);
await user.click(screen.getByRole("button", { name: /Paris/ }));
await user.click(screen.getByRole("button", { name: /Check Answer/ }));
await user.click(screen.getByRole("button", { name: /AI Judge/ }));
act(() => {
mocks.judgeHandlers!.onVerdict!({
result: "incorrect",
is_correct: false,
final: true,
confidence: 0.9,
steps: [{ label: "Capital", ok: false, comment: "Unclear." }],
});
mocks.judgeHandlers!.onDone("Unclear.");
});
// Exact matching accepted the answer, so its badge stays authoritative;
// the verdict keeps its step feedback but no contradicting badge.
await waitFor(() => {
expect(screen.getByText("Capital")).toBeInTheDocument();
});
expect(screen.getAllByText("Correct")).toHaveLength(1);
expect(screen.queryByText("Incorrect")).toBeNull();
});
it("closes in-flight judge handles on unmount", async () => {

View File

@@ -35,20 +35,17 @@ describe("parseQuizVerdict", () => {
expect(parseQuizVerdict(null)).toBeNull();
});
it("never trusts a non-final verdict as graded or its points", () => {
const verdict = parseQuizVerdict({ ...judged, result: "correct", final: false });
expect(verdict).toMatchObject({ result: "ungraded", is_correct: null });
expect(verdict?.awarded_points).toBeUndefined();
it("requires a known result and a boolean final", () => {
expect(parseQuizVerdict({ result: "correct" })).toBeNull();
expect(parseQuizVerdict({ result: "correct", final: "yes" })).toBeNull();
});
it("drops malformed steps and inconsistent points", () => {
it("drops malformed steps", () => {
const verdict = parseQuizVerdict({
...judged,
steps: [{ label: "ok", ok: true }, { label: 3 }, "x"],
awarded_points: 9,
steps: [{ label: "ok", ok: true }, { label: 3 }, { label: "x", ok: null }, "x"],
});
expect(verdict?.steps).toEqual([{ label: "ok", ok: true, comment: "" }]);
expect(verdict?.max_points).toBeUndefined();
});
});
@@ -83,6 +80,13 @@ describe("resolveQuizAssessment", () => {
).toEqual({ is_correct: true, result: "correct" });
});
it("ignores a verdict that is not final", () => {
const unsure = { ...judged, result: "correct" as const, final: false };
expect(resolveQuizAssessment({ ...base, verdict: unsure })).toEqual({
is_correct: false,
});
});
it("keeps photo answers ungraded until a final verdict exists", () => {
expect(
resolveQuizAssessment({ ...base, hasImages: true, verdict: null }),
@@ -105,6 +109,13 @@ describe("resolveQuizAssessment", () => {
});
describe("QuizVerdictSummary", () => {
it("hides the result badge and points when exact matching decided", () => {
render(<QuizVerdictSummary verdict={judged} showResult={false} />);
expect(screen.queryByText("Partially correct")).toBeNull();
expect(screen.queryByText("3 / 5 points")).toBeNull();
expect(screen.getByText("Minority interest")).toBeInTheDocument();
});
it("renders badge, points and step feedback", () => {
render(<QuizVerdictSummary verdict={judged} />);
expect(screen.getByText("Partially correct")).toBeInTheDocument();