@crowandraven/evalkeeper 0.16.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,12 +20,13 @@ import {
20
20
  FROM_BASE_CASE,
21
21
  isShippedReview,
22
22
  } from "../index.js";
23
- import { Check, ChevronRight, CircleAlert, X } from "lucide-react";
23
+ import { Check, ChevronRight, CircleAlert, Eye, X } from "lucide-react";
24
24
  import type { EvalAppConfig } from "./app.js";
25
25
  import { AnswerInput, FieldValues, Fields } from "./Fields.js";
26
26
  import { AddCaseHeader, OpenLinkedCase } from "./AddCase.js";
27
27
  import { CaseMenu } from "./CaseMenu.js";
28
- import { ConfirmAnswer } from "./ConfirmAnswer.js";
28
+ import { CaseStatusChip, ConfirmAnswer } from "./ConfirmAnswer.js";
29
+ import { ReadableValue } from "./Readable.js";
29
30
 
30
31
  /** A suite's cases in use, with each case's answers newest first. */
31
32
  async function loadCases(
@@ -38,11 +39,17 @@ async function loadCases(
38
39
  }));
39
40
  }
40
41
 
42
+ const CONFIRMED_STATUS = {
43
+ text: "Confirmed",
44
+ tone: "live",
45
+ term: "confirmed",
46
+ } as const satisfies { text: string; tone: ChipTone; term: GlossaryTerm };
47
+
41
48
  const STATUS: Record<
42
49
  string,
43
50
  { text: string; tone: ChipTone; term: GlossaryTerm }
44
51
  > = {
45
- CONFIRMED: { text: "Confirmed", tone: "live", term: "confirmed" },
52
+ CONFIRMED: CONFIRMED_STATUS,
46
53
  PROVISIONAL: { text: "Provisional", tone: "warn", term: "provisional" },
47
54
  PENDING: { text: "Pending", tone: "warn", term: "pending" },
48
55
  };
@@ -88,11 +95,17 @@ export async function AnswerKey({
88
95
  ? await app.store.listOutputsFor(runIds)
89
96
  : (await Promise.all(runIds.map((id) => app.store.listOutputs(id)))).flat();
90
97
  const byCase = new Map<string, { model: string; output: OutputRecord }[]>();
98
+ // One line per model: a test with repeats answered each case several
99
+ // times, so a case shows the first repeat the model got wrong, if any.
100
+ const right = (o: OutputRecord) => !o.error && allPassed(o.scores);
91
101
  for (const o of outputs) {
92
- byCase.set(o.caseId, [
93
- ...(byCase.get(o.caseId) ?? []),
94
- { model: modelByRun.get(o.runId) ?? o.runId, output: o },
95
- ]);
102
+ const model = modelByRun.get(o.runId) ?? o.runId;
103
+ const list = byCase.get(o.caseId) ?? [];
104
+ const i = list.findIndex((x) => x.model === model);
105
+ const had = list[i];
106
+ if (!had) list.push({ model, output: o });
107
+ else if (right(had.output) && !right(o)) list[i] = { model, output: o };
108
+ byCase.set(o.caseId, list);
96
109
  }
97
110
  const fields = suite.fields;
98
111
 
@@ -251,7 +264,7 @@ function CaseItem({
251
264
  c: CaseRecord;
252
265
  results: {
253
266
  model: string;
254
- output: { scores: unknown; error: string | null };
267
+ output: { scores: unknown; error: string | null; output?: unknown };
255
268
  }[];
256
269
  }) {
257
270
  const answer =
@@ -283,6 +296,93 @@ function CaseItem({
283
296
  const label = (key: string) =>
284
297
  suite.checks.find((x) => x.key === key)?.label ?? key;
285
298
  const tags = Array.isArray(c.tags) ? c.tags : [];
299
+ // An answer nobody has confirmed: shown first, to check.
300
+ const checking =
301
+ !readOnly &&
302
+ (answer?.status === "PENDING" || answer?.status === "PROVISIONAL");
303
+ const looks: string[] = [];
304
+ let marks: string[] = [];
305
+ try {
306
+ const found = suite.lookAt?.(input, answer?.value ?? null) ?? [];
307
+ looks.push(...found.map((l) => l.note));
308
+ marks = found.flatMap((l) => l.marks ?? []);
309
+ } catch {
310
+ // A suite's pointers are a help, never a reason the case can't show.
311
+ }
312
+ // Models disagreeing with the answer is the surest sign to look closer.
313
+ if (answer && results.length > 1 && right < results.length)
314
+ looks.push(
315
+ right === 0
316
+ ? "no model's latest test agrees with this answer"
317
+ : `models split: ${right} of ${results.length} agree with this answer`,
318
+ );
319
+ // What each model wrote, as the suite's short line (`outputLine`).
320
+ const lineOf = (output: unknown): string | null => {
321
+ try {
322
+ return suite.outputLine?.(output) ?? null;
323
+ } catch {
324
+ return null;
325
+ }
326
+ };
327
+ const inputFallback = suite.describe ? (
328
+ <pre className="whitespace-pre-wrap text-xs">{suite.describe(input)}</pre>
329
+ ) : (
330
+ <ReadableValue value={c.input} marks={marks} />
331
+ );
332
+
333
+ // How each model's latest test did, with what it wrote: after the
334
+ // answer, so a person reads the answer first.
335
+ const modelResults = results.length > 0 && (
336
+ <section className="space-y-2">
337
+ <h4 className="text-xs font-semibold uppercase tracking-wide text-muted">
338
+ Each model's latest test
339
+ </h4>
340
+ <ul className="space-y-1">
341
+ {results.map(({ model, output }) => {
342
+ const checks = (output.scores as CheckResult[] | null) ?? null;
343
+ const failed = checks?.filter((x) => x.pass === false) ?? [];
344
+ return (
345
+ <li key={model} className="flex items-start gap-1.5">
346
+ {output.error ? (
347
+ <CircleAlert
348
+ aria-label="No answer"
349
+ className="mt-0.5 h-4 w-4 shrink-0 text-bad"
350
+ />
351
+ ) : failed.length ? (
352
+ <X
353
+ aria-label="Wrong"
354
+ className="mt-0.5 h-4 w-4 shrink-0 text-warn"
355
+ />
356
+ ) : (
357
+ <Check
358
+ aria-label="Right"
359
+ className="mt-0.5 h-4 w-4 shrink-0 text-good"
360
+ />
361
+ )}
362
+ <span>
363
+ <span className="font-medium">{model}</span>
364
+ {output.error && (
365
+ <span className="text-muted">: {output.error}</span>
366
+ )}
367
+ {failed.map((x) => (
368
+ <span key={x.key} className="text-muted">
369
+ {" "}
370
+ · {label(x.key)}
371
+ {x.note ? `: ${x.note}` : ""}
372
+ </span>
373
+ ))}
374
+ {!output.error && lineOf(output.output) && (
375
+ <span className="readable block">
376
+ {lineOf(output.output)}
377
+ </span>
378
+ )}
379
+ </span>
380
+ </li>
381
+ );
382
+ })}
383
+ </ul>
384
+ </section>
385
+ );
286
386
 
287
387
  return (
288
388
  <details
@@ -302,9 +402,12 @@ function CaseItem({
302
402
  <span className="readable min-w-0">{suite.title(input)}</span>
303
403
  </span>
304
404
  <span className="flex shrink-0 items-center gap-1.5">
305
- <Tip term={status.term} id={`status-${c.id}`}>
306
- <Chip tone={status.tone}>{status.text}</Chip>
307
- </Tip>
405
+ <CaseStatusChip
406
+ labelId={answer?.id ?? null}
407
+ status={status}
408
+ confirmed={CONFIRMED_STATUS}
409
+ id={`status-${c.id}`}
410
+ />
308
411
  {shipped && (
309
412
  <Tip term={shipped.term} id={`reviewed-${c.id}`}>
310
413
  <Chip tone="neutral">{shipped.text}</Chip>
@@ -331,7 +434,94 @@ function CaseItem({
331
434
  </summary>
332
435
 
333
436
  <div className="relative space-y-5 px-3 pb-4 text-sm">
334
- <div className="grid gap-5 md:grid-cols-2">
437
+ {checking && (
438
+ // What to check first, with its buttons: the answer, and what
439
+ // it turns on. Clear of the case menu's button on the right.
440
+ <section
441
+ aria-label="Check this answer"
442
+ className="space-y-3 rounded-md border border-good/25 bg-good/5 p-3 pr-12"
443
+ >
444
+ <div className="space-y-1">
445
+ <h4 className="text-xs font-medium text-good">
446
+ Check this answer
447
+ </h4>
448
+ {suite.fields ? (
449
+ <FieldValues
450
+ fields={suite.fields.label}
451
+ values={answer.value}
452
+ prefix={`${c.id}-ans-`}
453
+ />
454
+ ) : (
455
+ <ReadableValue value={answer.value} />
456
+ )}
457
+ {shipped && answer.note && (
458
+ <p className="readable text-xs text-muted">{answer.note}</p>
459
+ )}
460
+ </div>
461
+ {looks.length > 0 && (
462
+ <div className="readable text-sm">
463
+ <p className="flex items-center gap-1.5 text-xs font-medium">
464
+ <Eye
465
+ aria-hidden
466
+ className="h-3.5 w-3.5 shrink-0 text-muted"
467
+ />
468
+ Look at
469
+ </p>
470
+ <ul className="mt-1 list-disc space-y-0.5 pl-5">
471
+ {looks.map((l) => (
472
+ <li key={l}>{l.charAt(0).toUpperCase() + l.slice(1)}</li>
473
+ ))}
474
+ </ul>
475
+ </div>
476
+ )}
477
+ {
478
+ <CaseMenu
479
+ suite={suite.key}
480
+ caseId={c.id}
481
+ retire={app.actions.retireCase}
482
+ review={
483
+ <ConfirmAnswer
484
+ action={app.actions.confirmAnswer}
485
+ suite={suite.key}
486
+ labelId={answer.id}
487
+ />
488
+ }
489
+ editLabel={
490
+ suite.fields &&
491
+ (suite.labelFromForm || app.answerEditors[suite.key])
492
+ ? "Edit answer"
493
+ : null
494
+ }
495
+ edit={
496
+ suite.fields && (
497
+ <form
498
+ key={answer.updatedAt.toISOString()}
499
+ action={app.actions.editAnswer}
500
+ className="space-y-4"
501
+ >
502
+ <input type="hidden" name="suite" value={suite.key} />
503
+ <input type="hidden" name="case" value={c.id} />
504
+ <AnswerInput
505
+ fields={suite.fields.label}
506
+ editor={app.answerEditors[suite.key]}
507
+ input={c.input}
508
+ values={answer.value}
509
+ prefix={`${c.id}-`}
510
+ />
511
+ <p className="readable text-xs text-muted">
512
+ Saves a new confirmed answer and keeps the old one in
513
+ the history. Tests are rescored for free.
514
+ </p>
515
+ <Submit>Save answer</Submit>
516
+ </form>
517
+ )
518
+ }
519
+ />
520
+ }
521
+ </section>
522
+ )}
523
+
524
+ {checking ? (
335
525
  <section className="space-y-2">
336
526
  <h4 className="text-xs font-semibold uppercase tracking-wide text-muted">
337
527
  Input
@@ -341,30 +531,10 @@ function CaseItem({
341
531
  fields={suite.fields.input}
342
532
  values={c.input}
343
533
  prefix={`${c.id}-in-`}
534
+ marks={marks}
344
535
  />
345
536
  ) : (
346
- <pre className="whitespace-pre-wrap text-xs">
347
- {suite.describe?.(input) ?? JSON.stringify(c.input, null, 2)}
348
- </pre>
349
- )}
350
- </section>
351
- <section className="space-y-2">
352
- <h4 className="text-xs font-semibold uppercase tracking-wide text-muted">
353
- Answer
354
- </h4>
355
- {answer && suite.fields ? (
356
- <FieldValues
357
- fields={suite.fields.label}
358
- values={answer.value}
359
- prefix={`${c.id}-ans-`}
360
- />
361
- ) : (
362
- <pre className="whitespace-pre-wrap text-xs">
363
- {JSON.stringify(answer?.value ?? null, null, 2)}
364
- </pre>
365
- )}
366
- {shipped && answer?.note && (
367
- <p className="readable text-xs text-muted">{answer.note}</p>
537
+ inputFallback
368
538
  )}
369
539
  {(tags.length > 0 || c.note) && (
370
540
  <p className="readable text-xs text-muted">
@@ -373,107 +543,105 @@ function CaseItem({
373
543
  </p>
374
544
  )}
375
545
  </section>
376
- </div>
377
-
378
- {results.length > 0 && (
379
- <section className="space-y-2">
380
- <h4 className="text-xs font-semibold uppercase tracking-wide text-muted">
381
- Each model's latest test
382
- </h4>
383
- <ul className="space-y-1">
384
- {results.map(({ model, output }) => {
385
- const checks = (output.scores as CheckResult[] | null) ?? null;
386
- const failed = checks?.filter((x) => x.pass === false) ?? [];
387
- return (
388
- <li key={model} className="flex items-start gap-1.5">
389
- {output.error ? (
390
- <CircleAlert
391
- aria-label="No answer"
392
- className="mt-0.5 h-4 w-4 shrink-0 text-bad"
393
- />
394
- ) : failed.length ? (
395
- <X
396
- aria-label="Wrong"
397
- className="mt-0.5 h-4 w-4 shrink-0 text-warn"
398
- />
399
- ) : (
400
- <Check
401
- aria-label="Right"
402
- className="mt-0.5 h-4 w-4 shrink-0 text-good"
403
- />
404
- )}
405
- <span>
406
- <span className="font-medium">{model}</span>
407
- {output.error && (
408
- <span className="text-muted">: {output.error}</span>
409
- )}
410
- {failed.map((x) => (
411
- <span key={x.key} className="text-muted">
412
- {" "}
413
- · {label(x.key)}
414
- {x.note ? `: ${x.note}` : ""}
415
- </span>
416
- ))}
417
- </span>
418
- </li>
419
- );
420
- })}
421
- </ul>
422
- </section>
423
- )}
424
-
425
- {!readOnly && (
426
- <CaseMenu
427
- suite={suite.key}
428
- caseId={c.id}
429
- retire={app.actions.retireCase}
430
- startEditing={!answer}
431
- // Pending or provisional: confirm it as it is, or change it.
432
- review={
433
- answer &&
434
- (answer.status === "PENDING" ||
435
- answer.status === "PROVISIONAL") && (
436
- <ConfirmAnswer
437
- action={app.actions.confirmAnswer}
438
- suite={suite.key}
439
- labelId={answer.id}
440
- />
441
- )
442
- }
443
- editLabel={
444
- suite.fields &&
445
- (suite.labelFromForm || app.answerEditors[suite.key])
446
- ? answer
447
- ? "Edit answer"
448
- : "Write the answer"
449
- : null
450
- }
451
- edit={
452
- suite.fields && (
453
- <form
454
- key={answer?.updatedAt.toISOString() ?? "none"}
455
- action={app.actions.editAnswer}
456
- className="space-y-4"
457
- >
458
- <input type="hidden" name="suite" value={suite.key} />
459
- <input type="hidden" name="case" value={c.id} />
460
- <AnswerInput
546
+ ) : (
547
+ <>
548
+ <div className="grid gap-5 md:grid-cols-2">
549
+ <section className="space-y-2">
550
+ <h4 className="text-xs font-semibold uppercase tracking-wide text-muted">
551
+ Input
552
+ </h4>
553
+ {suite.fields ? (
554
+ <FieldValues
555
+ fields={suite.fields.input}
556
+ values={c.input}
557
+ prefix={`${c.id}-in-`}
558
+ marks={marks}
559
+ />
560
+ ) : (
561
+ inputFallback
562
+ )}
563
+ </section>
564
+ <section className="space-y-2">
565
+ <h4 className="text-xs font-semibold uppercase tracking-wide text-muted">
566
+ Answer
567
+ </h4>
568
+ {answer && suite.fields ? (
569
+ <FieldValues
461
570
  fields={suite.fields.label}
462
- editor={app.answerEditors[suite.key]}
463
- input={c.input}
464
- values={answer?.value}
465
- prefix={`${c.id}-`}
571
+ values={answer.value}
572
+ prefix={`${c.id}-ans-`}
466
573
  />
574
+ ) : (
575
+ <ReadableValue value={answer?.value ?? null} />
576
+ )}
577
+ {shipped && answer?.note && (
578
+ <p className="readable text-xs text-muted">{answer.note}</p>
579
+ )}
580
+ {(tags.length > 0 || c.note) && (
467
581
  <p className="readable text-xs text-muted">
468
- Saves a new confirmed answer and keeps the old one in the
469
- history. Tests are rescored for free.
582
+ {tags.length > 0 && `Tags: ${tags.join(", ")}. `}
583
+ {c.note}
470
584
  </p>
471
- <Submit>Save answer</Submit>
472
- </form>
473
- )
474
- }
475
- />
585
+ )}
586
+ </section>
587
+ </div>
588
+ {modelResults}
589
+ {!readOnly && (
590
+ <CaseMenu
591
+ suite={suite.key}
592
+ caseId={c.id}
593
+ retire={app.actions.retireCase}
594
+ startEditing={!answer}
595
+ // Pending or provisional: confirm it as it is, or change it.
596
+ review={
597
+ answer &&
598
+ (answer.status === "PENDING" ||
599
+ answer.status === "PROVISIONAL") && (
600
+ <ConfirmAnswer
601
+ action={app.actions.confirmAnswer}
602
+ suite={suite.key}
603
+ labelId={answer.id}
604
+ />
605
+ )
606
+ }
607
+ editLabel={
608
+ suite.fields &&
609
+ (suite.labelFromForm || app.answerEditors[suite.key])
610
+ ? answer
611
+ ? "Edit answer"
612
+ : "Write the answer"
613
+ : null
614
+ }
615
+ edit={
616
+ suite.fields && (
617
+ <form
618
+ key={answer?.updatedAt.toISOString() ?? "none"}
619
+ action={app.actions.editAnswer}
620
+ className="space-y-4"
621
+ >
622
+ <input type="hidden" name="suite" value={suite.key} />
623
+ <input type="hidden" name="case" value={c.id} />
624
+ <AnswerInput
625
+ fields={suite.fields.label}
626
+ editor={app.answerEditors[suite.key]}
627
+ input={c.input}
628
+ values={answer?.value}
629
+ prefix={`${c.id}-`}
630
+ />
631
+ <p className="readable text-xs text-muted">
632
+ Saves a new confirmed answer and keeps the old one in
633
+ the history. Tests are rescored for free.
634
+ </p>
635
+ <Submit>Save answer</Submit>
636
+ </form>
637
+ )
638
+ }
639
+ />
640
+ )}
641
+ </>
476
642
  )}
643
+ {/* After the input, to compare what models wrote with it. */}
644
+ {checking && modelResults}
477
645
  </div>
478
646
  </details>
479
647
  );
@@ -1,13 +1,69 @@
1
1
  "use client";
2
2
 
3
+ import { useState, useSyncExternalStore, useTransition } from "react";
4
+ import type { GlossaryTerm } from "../index.js";
3
5
  import type { FormAction } from "./app.js";
4
- import { Submit } from "./pieces.js";
6
+ import {
7
+ Button,
8
+ Chip,
9
+ Spinner,
10
+ Tip,
11
+ useNotify,
12
+ type ChipTone,
13
+ } from "./pieces.js";
14
+
15
+ // Answers confirmed here, ahead of the page's refresh.
16
+ const confirmedHere = new Set<string>();
17
+ const listeners = new Set<() => void>();
18
+ function setConfirmedHere(labelId: string, on: boolean) {
19
+ if (on) confirmedHere.add(labelId);
20
+ else confirmedHere.delete(labelId);
21
+ for (const l of listeners) l();
22
+ }
23
+ function useConfirmedHere(labelId: string): boolean {
24
+ return useSyncExternalStore(
25
+ (l) => {
26
+ listeners.add(l);
27
+ return () => listeners.delete(l);
28
+ },
29
+ () => confirmedHere.has(labelId),
30
+ () => false,
31
+ );
32
+ }
33
+
34
+ type Status = { text: string; tone: ChipTone; term: GlossaryTerm };
35
+
36
+ /**
37
+ * A case's answer status, which turns to Confirmed the moment someone
38
+ * confirms it there, before the save and the page's refresh finish.
39
+ */
40
+ export function CaseStatusChip({
41
+ labelId,
42
+ status,
43
+ confirmed,
44
+ id,
45
+ }: {
46
+ /** The answer it describes; null for a case without one. */
47
+ labelId: string | null;
48
+ status: Status;
49
+ /** How a confirmed answer shows. */
50
+ confirmed: Status;
51
+ /** For the tooltip. */
52
+ id: string;
53
+ }) {
54
+ const now = useConfirmedHere(labelId ?? "") && labelId ? confirmed : status;
55
+ return (
56
+ <Tip term={now.term} id={id}>
57
+ <Chip tone={now.tone}>{now.text}</Chip>
58
+ </Tip>
59
+ );
60
+ }
5
61
 
6
62
  /**
7
- * Confirms a case's pending answer and moves straight on: this case closes
8
- * and the next one waiting for a confirmation opens, while the save and
9
- * the page's refresh finish behind it. Reviewing a run of drafts shouldn't
10
- * wait on the whole page each time.
63
+ * Confirms a case's pending answer and moves straight on: the case shows
64
+ * Confirmed and closes, and the next one waiting for a confirmation opens,
65
+ * while the save and the page's refresh finish behind it. If the save
66
+ * fails, the case opens again and says so (or your app's toast does).
11
67
  */
12
68
  export function ConfirmAnswer({
13
69
  suite,
@@ -19,31 +75,65 @@ export function ConfirmAnswer({
19
75
  /** Your confirmAnswer action. */
20
76
  action: FormAction;
21
77
  }) {
22
- return (
23
- <form
24
- action={action}
25
- onSubmit={(e) => {
26
- const here = e.currentTarget.closest("details");
27
- if (!here) return;
28
- here.open = false;
29
- let next = here.nextElementSibling;
30
- while (
31
- next &&
32
- !(
33
- next instanceof HTMLDetailsElement &&
34
- next.dataset.pending === "true"
35
- )
36
- )
37
- next = next.nextElementSibling;
38
- if (next instanceof HTMLDetailsElement) {
39
- next.open = true;
40
- next.scrollIntoView({ block: "nearest" });
78
+ const [saving, start] = useTransition();
79
+ const [failed, setFailed] = useState(false);
80
+ const notify = useNotify();
81
+
82
+ function confirm(button: HTMLElement) {
83
+ setFailed(false);
84
+ setConfirmedHere(labelId, true);
85
+ const here = button.closest("details");
86
+ if (here) {
87
+ here.open = false;
88
+ let next = here.nextElementSibling;
89
+ while (
90
+ next &&
91
+ !(next instanceof HTMLDetailsElement && next.dataset.pending === "true")
92
+ )
93
+ next = next.nextElementSibling;
94
+ if (next instanceof HTMLDetailsElement) {
95
+ next.open = true;
96
+ next.scrollIntoView({ block: "nearest" });
97
+ }
98
+ }
99
+ const form = new FormData();
100
+ form.set("suite", suite);
101
+ form.set("label", labelId);
102
+ start(async () => {
103
+ try {
104
+ await action(form);
105
+ notify?.("success", "Answer confirmed");
106
+ } catch {
107
+ setConfirmedHere(labelId, false);
108
+ if (here) {
109
+ here.open = true;
110
+ here.scrollIntoView({ block: "nearest" });
41
111
  }
42
- }}
43
- >
44
- <input type="hidden" name="suite" value={suite} />
45
- <input type="hidden" name="label" value={labelId} />
46
- <Submit>Confirm this answer</Submit>
47
- </form>
112
+ if (notify)
113
+ notify("error", "Couldn't confirm that answer. Please try again.");
114
+ else setFailed(true);
115
+ }
116
+ });
117
+ }
118
+
119
+ return (
120
+ <>
121
+ <Button
122
+ type="button"
123
+ disabled={saving}
124
+ onClick={(e) => {
125
+ confirm(e.currentTarget);
126
+ }}
127
+ className="inline-flex items-center gap-2 whitespace-nowrap rounded-md bg-accent px-3 py-1.5 text-sm text-white disabled:opacity-60"
128
+ >
129
+ {saving && <Spinner />}
130
+ Confirm this answer
131
+ </Button>
132
+ {failed && (
133
+ <p role="alert" className="w-full text-xs text-bad">
134
+ Couldn&apos;t confirm that answer. Please try again.
135
+ </p>
136
+ )}
137
+ </>
48
138
  );
49
139
  }