Varied question openers, readable rationales, plain practice score
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 30s

- Stems no longer all open the same way: the writing rules, batch prompt and
  planner ask for varied openings (age openings stay part of the mix), and when
  one opener covers more than a quarter of a quiz the excess stems have only
  their opening reworded, keeping every number, before the check.
- Rationales are asked for as Markdown (a bold key point, then short bullets);
  older one-paragraph rationales are laid out the same way on display.
- The practice score in the quiz header is a plain "x/y correct", with no
  percentage or colour.
- Fix a flaky assertion: the mock's answer-key fix is a no-op when the shuffle
  already put the answer in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-16 12:05:46 -07:00
parent 10be5c39da
commit 517278be71
15 changed files with 382 additions and 24 deletions

View file

@ -54,7 +54,7 @@ async function sse(url: string, body: unknown): Promise<any[]> {
const isCheck = (r: { body: any }) => (r.body?.tools ?? []).some((t: any) => t.function?.name === 'submit_review');
/** The quick call that groups topic labels after a quiz is written. */
const isGrouping = (r: { body: any }) => (r.body?.tools ?? []).some((t: any) => t.function?.name === 'submit_topics');
const isNaming = (r: { body: any }) => ['name_set', 'submit_proofread', 'plan_questions'].includes(r.body?.tool_choice?.function?.name);
const isNaming = (r: { body: any }) => ['name_set', 'submit_proofread', 'plan_questions', 'submit_stems'].includes(r.body?.tool_choice?.function?.name);
async function waitForDraft(id: string) {
for (let i = 0; i < 200; i += 1) {
@ -213,7 +213,12 @@ describe('generation', () => {
expect(pythonResult.ok).toBe(true);
expect(pythonResult.stdout).toMatch(/^12 12 \d+ \[(?:[12], ){11}[12]\]\n$/);
expect(draft.payload.questions[1].options.map((o: any) => o.correct)).toEqual([false, true, false, false]);
expect(draft.meta.notes.join(' ')).toMatch(/Checked with Python \(1 run\): 2 of 2 checks passed, 1 fix applied/);
// The mock's fix keys option 2; when the shuffle already put the answer there it changes
// nothing and is skipped, so "1 fix applied" only shows when it did something.
expect(draft.meta.notes.join(' ')).toMatch(/Checked with Python \(1 run\): 2 of 2 checks passed(?:, 1 fix applied|\.)/);
// Every mock stem opens "Mock question…": the excess ones were reworded (stem only) before the check.
expect(draft.meta.notes.join(' ')).toMatch(/Reworded how \d+ questions open/);
expect(draft.payload.questions.filter((q: any) => q.stem.startsWith('On the unit, Mock question')).length).toBeGreaterThan(4);
expect(JSON.stringify(calls[0].body.messages)).toMatch(/SOURCE EXCERPTS/);
// The questions are planned first; both batches then run at the same time, each with its own planned angles.
expect(all.filter((r) => r.body?.tool_choice?.function?.name === 'plan_questions')).toHaveLength(1);

View file

@ -105,6 +105,9 @@ function toolArgs(name: string, userText: string, all: string): Record<string, u
return { cards: Array.from({ length: count }, (_, i) => ({ front: `Mock term ${i + 1 + offset}`, back: `Mock definition ${i + 1 + offset}`, tags: ['mock'] })) };
case 'plan_questions':
return { angles: Array.from({ length: count }, (_, k) => `Mock angle ${k + 1 + offset}`) };
case 'submit_stems':
// Rewords each listed stem's opening, keeping the rest of it.
return { stems: [...userText.matchAll(/^Q(\d+): (.+)$/gm)].map((m) => ({ question: Number(m[1]), stem: `On the unit, ${m[2]}` })) };
case 'name_set':
return /quiz of/i.test(userText) ? { title: 'Heart Failure and Pharmacology', description: 'Covers heart failure care and the drugs used for it.' } : { title: 'Mock Terms', description: 'Covers the mock terms.' };
case 'generate_quiz':

47
tests/openers.test.ts Normal file
View file

@ -0,0 +1,47 @@
import { describe, expect, it } from 'vitest';
import { acceptRewording, openerAdvice, repeatedOpeners, stemOpener } from '../server/ai/openers.ts';
const q = (stem: string, type: 'single' | 'multi' | 'numeric' = 'single') => ({ stem, type });
describe('question openers', () => {
it('folds ages and numbers so "A 72-year-old" and "A 45 year old" count as one opener', () => {
expect(stemOpener('A 72-year-old client with heart failure…')).toBe('a #-year-old');
expect(stemOpener('**A 45 year old** patient reports…')).toBe('a #-year-old');
expect(stemOpener('The nurse is caring for…')).toBe('the nurse');
});
it('flags an opener used by more than a quarter of the quiz and keeps a spread-out share of it', () => {
const stems = [
...Array.from({ length: 16 }, (_, i) => q(`A ${40 + i}-year-old client with condition ${i} reports pain. Which action is first?`)),
...Array.from({ length: 4 }, (_, i) => q(`The nurse is caring for client ${i}. Which finding requires follow-up?`)),
];
const groups = repeatedOpeners(stems);
expect(groups).toHaveLength(1);
expect(groups[0]!.opener).toBe('a #-year-old');
expect(groups[0]!.total).toBe(16);
// 20 questions allow 5 of one opener: 11 are reworded, the 5 kept are spread out.
expect(groups[0]!.reword).toHaveLength(11);
expect(groups[0]!.reword).not.toContain(0);
expect(openerAdvice(stems)).toMatch(/"A 40-year-old client…"/);
});
it('leaves varied quizzes, short quizzes and calculations alone', () => {
const varied = ['The nurse', 'A client', 'In the', 'During discharge', 'Which finding', 'The charge', 'After receiving', 'The provider'].map((o, i) => q(`${o} ${i} … what is the priority?`));
expect(repeatedOpeners(varied)).toEqual([]);
expect(repeatedOpeners(Array.from({ length: 6 }, (_, i) => q(`A ${i}-year-old client…`)))).toEqual([]);
const math = Array.from({ length: 10 }, (_, i) => q(`The provider orders ${i + 2} mg. How many mL?`, 'numeric'));
expect(repeatedOpeners(math)).toEqual([]);
});
it('accepts a rewording only when it keeps every number and the select-all ending', () => {
const original = q('A 72-year-old client has a potassium of 3.1 mEq/L and a heart rate of 52. Select all that apply.', 'multi');
const repeated = new Set(['a #-year-old']);
expect(acceptRewording(original, 'The nurse reviews the laboratory results of a 72-year-old client: potassium 3.1 mEq/L, heart rate 52.', repeated)).toBe(
'The nurse reviews the laboratory results of a 72-year-old client: potassium 3.1 mEq/L, heart rate 52. Select all that apply.',
);
// A number was lost or changed.
expect(acceptRewording(original, 'The nurse reviews the laboratory results of a 72-year-old client: potassium 3.4 mEq/L, heart rate 52.', repeated)).toBeNull();
// Still opens the repeated way.
expect(acceptRewording(original, 'A 72-year-old client on the unit has a potassium of 3.1 mEq/L and a heart rate of 52.', repeated)).toBeNull();
});
});

34
tests/rationale.test.ts Normal file
View file

@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest';
import { formatRationale, splitSentences } from '../shared/rationale.ts';
describe('rationale layout', () => {
it('keeps decimals and abbreviations inside a sentence', () => {
expect(splitSentences('Give 2.5 mg now, e.g. with food. Recheck the level in 6 hr. Then report it.')).toEqual(['Give 2.5 mg now, e.g. with food.', 'Recheck the level in 6 hr. Then report it.']);
});
it('breaks a long paragraph into a lead sentence and short paragraphs', () => {
const brick =
'Potassium of 2.9 mEq/L is hypokalemia caused by loop-diuretic potassium loss. It explains the cramps, weakness and palpitations the client reports. Hypokalemia destabilises cardiac membranes and can cause dysrhythmias. This is more dangerous in a client who also takes digoxin. The nurse notifies the provider and anticipates potassium replacement. The weight loss and urine output show the drug is working as intended.';
const out = formatRationale(brick);
const blocks = out.split('\n\n');
expect(blocks[0]).toBe('**Potassium of 2.9 mEq/L is hypokalemia caused by loop-diuretic potassium loss.**');
expect(blocks.length).toBeGreaterThanOrEqual(3);
expect(out.replace(/\*\*/g, '').replace(/\s+/g, ' ')).toBe(brick);
});
it('turns a walk-through of the choices into a list', () => {
const brick =
'The priority is airway, so suctioning comes first in this situation for the client. Option A is wrong because repositioning does not clear secretions. It also delays care. Option B is wrong because oxygen will not help a blocked airway. Option D can wait until the airway is clear and the client is stable. The nurse reassesses breath sounds after suctioning.';
const out = formatRationale(brick);
expect(out).toMatch(/\n- Option A is wrong because repositioning does not clear secretions\. It also delays care\.\n/);
expect(out).toMatch(/\n- Option B is wrong/);
// A closing sentence on a new subject ends the list.
expect(out).toMatch(/stable\.\n\nThe nurse reassesses/);
});
it('leaves short or already structured rationales alone', () => {
expect(formatRationale('Short and clear.')).toBe('Short and clear.');
const md = '**Furosemide is a loop diuretic.**\n\n- Check potassium before each dose and report any low value.\n- Weigh daily at the same time with the same scale and clothing.\n- Give it in the morning to prevent nocturia and falls overnight at home.';
expect(formatRationale(md)).toBe(md);
});
});