@agentswf/lab 0.3.0 → 0.4.0

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.
Files changed (88) hide show
  1. package/package.json +4 -4
  2. package/schema/{case-check.schema.json → lab-check.schema.json} +1 -1
  3. package/schema/{case-config.schema.json → lab-config.schema.json} +1 -1
  4. package/schema/{case-final.schema.json → lab-final.schema.json} +6 -6
  5. package/schema/{case-grade.schema.json → lab-grade.schema.json} +1 -1
  6. package/schema/{case-key.schema.json → lab-key.schema.json} +7 -0
  7. package/schema/{case-label.schema.json → lab-label.schema.json} +3 -3
  8. package/schema/{case-loop-report.schema.json → lab-loop-report.schema.json} +26 -15
  9. package/schema/{case-loop.schema.json → lab-loop.schema.json} +1 -1
  10. package/schema/{case-plan.schema.json → lab-plan.schema.json} +14 -3
  11. package/schema/{case-proposal.schema.json → lab-proposal.schema.json} +3 -3
  12. package/schema/{case-report.schema.json → lab-report.schema.json} +272 -36
  13. package/schema/{case-run.schema.json → lab-run.schema.json} +4 -4
  14. package/schema/{case-runs.schema.json → lab-runs.schema.json} +8 -1
  15. package/schema/{case-score.schema.json → lab-score.schema.json} +2 -2
  16. package/schema/{case-spread.schema.json → lab-spread.schema.json} +6 -6
  17. package/schema/{case-trial.schema.json → lab-trial.schema.json} +9 -9
  18. package/schema/{case-try.schema.json → lab-try.schema.json} +6 -6
  19. package/schema/local.json +3 -3
  20. package/src/env/capture.ts +1 -1
  21. package/src/env/compare.ts +7 -7
  22. package/src/env/fetch.ts +1 -1
  23. package/src/env/files.ts +14 -14
  24. package/src/env/grade-helper.ts +15 -15
  25. package/src/env/grader.ts +5 -2
  26. package/src/env/index.ts +18 -18
  27. package/src/env/trial.ts +5 -2
  28. package/src/format/key.ts +24 -0
  29. package/src/format/loop.ts +20 -19
  30. package/src/format/output.ts +120 -59
  31. package/src/format/records.ts +23 -20
  32. package/src/format/schema-files.ts +30 -30
  33. package/src/format/settings.ts +20 -13
  34. package/src/format/trial-env.ts +9 -6
  35. package/src/help/awf-lab.txt +1 -1
  36. package/src/help/case.txt +5 -4
  37. package/src/help/config.txt +2 -2
  38. package/src/help/label.txt +2 -2
  39. package/src/help/loop.txt +10 -10
  40. package/src/help/report.txt +1 -1
  41. package/src/help/run.txt +11 -10
  42. package/src/help/runs.txt +4 -3
  43. package/src/help/score.txt +4 -3
  44. package/src/lab/blame.ts +1 -1
  45. package/src/lab/box.ts +7 -5
  46. package/src/lab/capture-command.ts +11 -7
  47. package/src/lab/capture.ts +1 -1
  48. package/src/lab/check-command.ts +3 -3
  49. package/src/lab/cli.ts +9 -8
  50. package/src/lab/columns.ts +5 -5
  51. package/src/lab/command-context.ts +7 -7
  52. package/src/lab/compare.ts +8 -8
  53. package/src/lab/config-command.ts +25 -20
  54. package/src/lab/dataset.ts +37 -18
  55. package/src/lab/eval-files.ts +27 -16
  56. package/src/lab/folders.ts +26 -7
  57. package/src/lab/good.ts +1 -1
  58. package/src/lab/grading-stub.txt +2 -2
  59. package/src/lab/inspect.ts +13 -12
  60. package/src/lab/labels.ts +16 -11
  61. package/src/lab/loop-command.ts +68 -64
  62. package/src/lab/loop-records.ts +5 -5
  63. package/src/lab/loop.ts +105 -100
  64. package/src/lab/rates.ts +11 -6
  65. package/src/lab/record-copies.ts +1 -1
  66. package/src/lab/report-command.ts +8 -7
  67. package/src/lab/report.ts +172 -74
  68. package/src/lab/reuse.ts +29 -21
  69. package/src/lab/run-command.ts +304 -199
  70. package/src/lab/run-report.ts +49 -22
  71. package/src/lab/runs-command.ts +35 -27
  72. package/src/lab/scaffold.ts +19 -16
  73. package/src/lab/score-command.ts +72 -64
  74. package/src/lab/score.ts +1 -1
  75. package/src/lab/store.ts +3 -3
  76. package/src/lab/{trial-stub.txt → target-stub.txt} +3 -3
  77. package/src/lab/trial-settings.ts +17 -12
  78. package/src/lab/trial.ts +19 -23
  79. package/src/lab/usage.ts +1 -0
  80. package/src/lab/workflow-graders.ts +4 -1
  81. package/src/lab/workspace.ts +16 -9
  82. package/src/loop/propose.workflow.ts +1 -1
  83. package/src/shared/compare/index.ts +2 -2
  84. package/src/shared/compare/paired.ts +20 -20
  85. package/src/shared/compare/types.ts +4 -4
  86. package/src/write-schemas.ts +3 -3
  87. /package/schema/{case-good-record.schema.json → lab-good-record.schema.json} +0 -0
  88. /package/schema/{case-previous.schema.json → lab-previous.schema.json} +0 -0
package/src/lab/report.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import type { LabelRecord, RunRecord } from "../format/records";
2
2
  import { columnLabel, sharedParams } from "./columns";
3
- import type { ColumnVerdict } from "./compare";
3
+ import type { ColumnDecision } from "./compare";
4
4
  import { familyOf } from "./families";
5
5
  import { type Agreement, JUDGE_CHECK, type PersonAgreement } from "./labels";
6
6
  import type { CaseRow, StoredTrialView } from "./outcome";
7
7
  import {
8
8
  factsOf,
9
+ gradingSpend,
9
10
  newestEntries,
10
11
  PRECONDITION_STOP,
11
12
  preconditionFailure,
@@ -32,8 +33,8 @@ export type RunReport = {
32
33
  others?: readonly CaseRow[][];
33
34
  /** The person's labels on this run's trials, and how the lab agrees with them. */
34
35
  person?: PersonAgreement;
35
- /** `compare`'s verdict on each column but the reference, in `columns` order. */
36
- verdicts?: readonly ColumnVerdict[];
36
+ /** `compare`'s decision on each column but the reference, in `columns` order. */
37
+ decisions?: readonly ColumnDecision[];
37
38
  /** Each case's tags, from its key as it is now. */
38
39
  tagsOf?: (caseName: string) => readonly string[];
39
40
  /** The labeled trials the current judge graded, across the dataset's runs. */
@@ -336,7 +337,8 @@ function columnLines(
336
337
  rows: readonly CaseRow[],
337
338
  others: readonly CaseRow[][],
338
339
  trials: readonly StoredTrialView[],
339
- verdicts: readonly ColumnVerdict[] | undefined,
340
+ decisions: readonly ColumnDecision[] | undefined,
341
+ decidersOf: RunReport["decidersOf"],
340
342
  ): string[] {
341
343
  const reference = columnLabel(run, 0);
342
344
  const width = Math.max(14, ...run.columns.map((_, i) => columnLabel(run, i).length + 2));
@@ -345,11 +347,12 @@ function columnLines(
345
347
  ];
346
348
  const row = (label: string, list: readonly CaseRow[], column: number) => {
347
349
  const n = (v: CaseVerdict) => String(list.filter((r) => r.tally.verdict === v).length);
348
- const ran = trials.filter((t) => t.record.column === column && t.record.runId);
349
- const ms = median(ran.map((t) => t.record.ms));
350
- const cost = median(
351
- ran.flatMap((t) => (t.record.estimate !== undefined ? [t.record.estimate] : [])),
350
+ const sums = trialSums(
351
+ trials.filter((t) => t.record.column === column),
352
+ decidersOf,
352
353
  );
354
+ const ms = sums.msMedian;
355
+ const cost = sums.estimateMedian;
353
356
  lines.push(
354
357
  ` ${label.padEnd(width)}${n("passed").padEnd(8)}${n("failed").padEnd(8)}${n("flaky").padEnd(7)}${n("unclear").padEnd(9)}${(ms !== undefined ? seconds(ms) : "-").padEnd(14)}${cost !== undefined ? usd(cost) : "-"}`,
355
358
  );
@@ -387,7 +390,12 @@ function columnLines(
387
390
  const label = columnLabel(run, i + 1);
388
391
  const pairs = pairedRows(rows, column);
389
392
  const differ = cameOutDifferently(pairs);
390
- if (passFail) {
393
+ const more = pairs.filter((p) => p.gap > 0).length;
394
+ const fewer = pairs.filter((p) => p.gap < 0).length;
395
+ // With no case apart, the passes and the difference would only say so again.
396
+ if (passFail && differ.length === 0 && more === 0 && fewer === 0) {
397
+ lines.push(`no case came out differently in ${label}`);
398
+ } else if (passFail) {
391
399
  if (differ.length > 0) {
392
400
  lines.push(`came out differently in ${label}, largest difference first`);
393
401
  for (const { mine, theirs } of differ) {
@@ -396,32 +404,31 @@ function columnLines(
396
404
  );
397
405
  }
398
406
  } else lines.push(`no case came out differently in ${label}`);
399
- const more = pairs.filter((p) => p.gap > 0).length;
400
- const fewer = pairs.filter((p) => p.gap < 0).length;
401
407
  lines.push(
402
408
  `more passes: ${label} on ${plural(more, "case")}, ${reference} on ${plural(fewer, "case")}`,
403
409
  );
404
410
  const difference = pairedDifference(rows, column);
405
411
  if (difference) lines.push(`difference from ${reference}: ${describeDifference(difference)}`);
406
412
  }
407
- const said = verdicts?.[i];
413
+ const said = decisions?.[i];
408
414
  if (said && "error" in said) lines.push(`compare, ${label}: could not tell: ${said.error}`);
409
415
  else if (said) {
410
- const { verdict } = said;
411
416
  lines.push(
412
- `compare, ${label}: ${verdict.verdict}${verdict.stop ? "" : ", more trials may tell"} · ${verdict.reason}`,
417
+ `compare, ${label}: ${said.decision}${said.stop ? "" : ", more trials may tell"} · ${said.reason}`,
413
418
  );
414
419
  }
415
420
  });
416
- if (passFail) {
421
+ const once = run.asked.every((a) => a.trials === 1);
422
+ // One trial a case gives examples, which the line below says: rates would claim more.
423
+ if (passFail && !once) {
417
424
  lines.push(`rates, ${reference}: ${describeRates(ratesOf(rows))}`);
418
425
  for (const [i, column] of others.entries()) {
419
426
  lines.push(`rates, ${columnLabel(run, i + 1)}: ${describeRates(ratesOf(column))}`);
420
427
  }
421
428
  }
422
- if (run.asked.every((a) => a.trials === 1)) {
429
+ if (once) {
423
430
  lines.push("One trial each: these are examples, not rates.");
424
- } else if (!verdicts) {
431
+ } else if (!decisions) {
425
432
  lines.push("No compare in grading.ts says which is better. Read the cases.");
426
433
  }
427
434
  return lines;
@@ -437,9 +444,18 @@ const cell = (row: CaseRow | undefined) =>
437
444
  * by column, each column's rates and its difference and verdict against the reference.
438
445
  */
439
446
  export function markdownOf(report: RunReport): string {
440
- const { run, rows, others = [], verdicts } = report;
447
+ const { run, rows, others = [], decisions } = report;
441
448
  const { exit } = renderRun(report);
442
449
  const labels = run.columns.map((_, i) => columnLabel(run, i));
450
+ const timeOf = (column: number) => {
451
+ const sums = trialSums(
452
+ report.trials.filter((t) => t.record.column === column),
453
+ report.decidersOf,
454
+ );
455
+ if (sums.msMedian === undefined) return "";
456
+ const cost = sums.estimateMedian !== undefined ? `, est. ${usd(sums.estimateMedian)}` : "";
457
+ return `; per trial, median ${seconds(sums.msMedian)}${cost}`;
458
+ };
443
459
  const lines = [
444
460
  `### ${run.dataset} · run ${run.id}`,
445
461
  "",
@@ -452,18 +468,81 @@ export function markdownOf(report: RunReport): string {
452
468
  `| ${row.name} | ${[cell(row), ...others.map((o) => cell(o.find((r) => r.name === row.name)))].join(" | ")} |`,
453
469
  ),
454
470
  "",
455
- `- **${labels[0]}**: ${describeRates(ratesOf(rows))}`,
471
+ `- **${labels[0]}**: ${describeRates(ratesOf(rows))}${timeOf(0)}`,
456
472
  ];
457
473
  for (const [i, column] of others.entries()) {
458
474
  const difference = pairedDifference(rows, column);
459
- const said = verdicts?.[i];
475
+ const said = decisions?.[i];
476
+ lines.push(
477
+ `- **${labels[i + 1]}**: ${describeRates(ratesOf(column))}${difference ? `; ${describeDifference(difference)}` : ""}${said && !("error" in said) ? `; compare: ${said.decision}, ${said.reason}` : ""}${timeOf(i + 1)}`,
478
+ );
479
+ }
480
+ const all = trialSums(report.trials, report.decidersOf);
481
+ if (all.ran && !all.noAgents) {
460
482
  lines.push(
461
- `- **${labels[i + 1]}**: ${describeRates(ratesOf(column))}${difference ? `; ${describeDifference(difference)}` : ""}${said && !("error" in said) ? `; compare: ${said.verdict.verdict}, ${said.verdict.reason}` : ""}`,
483
+ `- **all trials**: est. ${usd(all.estimate - all.grading)}${all.reused > 0 ? `, ${usd(all.reused)} of it reused trials, run before` : ""}${all.unpriced ? ", partly unpriced" : ""}${all.grading > 0 ? `; grading ${usd(all.grading)}` : ""}`,
462
484
  );
463
485
  }
464
486
  return lines.join("\n");
465
487
  }
466
488
 
489
+ /** The trials a run asked for, in every column, that it never ran. */
490
+ export function notRunOf(run: RunRecord, trials: readonly unknown[]): number {
491
+ return Math.max(
492
+ 0,
493
+ run.asked.reduce((n, a) => n + a.trials, 0) * run.columns.length - trials.length,
494
+ );
495
+ }
496
+
497
+ /**
498
+ * Some trials in time and money: the medians of those that ran and didn't error, and the
499
+ * estimates summed, the reused trials' and the grades' included.
500
+ */
501
+ export function trialSums(
502
+ trials: readonly StoredTrialView[],
503
+ decidersOf?: RunReport["decidersOf"],
504
+ ): {
505
+ /** Some trial ran and didn't error, so the medians mean something. */
506
+ ran: boolean;
507
+ msMedian?: number;
508
+ estimateMedian?: number;
509
+ estimate: number;
510
+ reused: number;
511
+ grading: number;
512
+ unpriced: boolean;
513
+ noAgents: boolean;
514
+ } {
515
+ const ran = trials.filter(
516
+ (t) =>
517
+ t.record.trialId &&
518
+ trialOutcome(t.record, t.scores, decidersOf?.(t.record.case)).outcome !== "errored",
519
+ );
520
+ const ms = median(ran.map((t) => t.record.ms));
521
+ const each = median(
522
+ ran.flatMap((t) => (t.record.estimate !== undefined ? [t.record.estimate] : [])),
523
+ );
524
+ const workflow = trials.reduce((sum, t) => sum + (t.record.estimate ?? 0), 0);
525
+ const reused = trials
526
+ .filter((t) => t.record.reused)
527
+ .reduce((sum, t) => sum + (t.record.estimate ?? 0), 0);
528
+ const grading = trials.reduce((sum, t) => sum + gradingSpend(t.scores), 0);
529
+ return {
530
+ ran: ms !== undefined,
531
+ ...(ms !== undefined ? { msMedian: ms } : {}),
532
+ ...(each !== undefined ? { estimateMedian: each } : {}),
533
+ estimate: workflow + grading,
534
+ reused,
535
+ grading,
536
+ // A run with no agents costs nothing to price; one whose agents' count is unknown may.
537
+ unpriced: ran.some(
538
+ (t) =>
539
+ (t.record.estimate === undefined && t.record.agents !== 0) ||
540
+ (t.record.priced ?? 0) < (t.record.agents ?? 0),
541
+ ),
542
+ noAgents: workflow === 0 && ran.every((t) => t.record.agents === 0),
543
+ };
544
+ }
545
+
467
546
  /** `run` and `report`'s text, and the exit code from your checkout's column. */
468
547
  export function renderRun({
469
548
  run,
@@ -480,18 +559,16 @@ export function renderRun({
480
559
  tagsOf = () => [],
481
560
  ungradedOf = () => undefined,
482
561
  followsPlan = false,
483
- verdicts: compared,
562
+ decisions: compared,
484
563
  }: RunReport): {
485
564
  text: string;
486
565
  exit: ExitCode;
487
566
  } {
488
567
  const partial = run.finished === undefined || run.broke !== undefined;
489
568
  const preconditionStop = run.stopped === "precondition";
490
- const exit = exitCodeOf({ cases: rows, partial, preconditionStop });
491
- const notRun = Math.max(
492
- 0,
493
- run.asked.reduce((n, a) => n + a.trials, 0) * run.columns.length - trials.length,
494
- );
569
+ const budgetStop = run.stopped === "budget";
570
+ const exit = exitCodeOf({ cases: rows, partial: partial || budgetStop, preconditionStop });
571
+ const notRun = notRunOf(run, trials);
495
572
  const counts = new Set(run.asked.map((a) => a.trials));
496
573
  const each =
497
574
  counts.size === 1 ? `${plural([...counts][0]!, "trial")} each` : "trials per case as asked";
@@ -529,7 +606,9 @@ export function renderRun({
529
606
  : []),
530
607
  "",
531
608
  ];
609
+ let historyShown = false;
532
610
  const line = (row: CaseRow, extra = "") => {
611
+ if (row.history.length > 0) historyShown = true;
533
612
  const history =
534
613
  row.history.length > 0 ? ` history ${row.history.map((v) => MARK[v]).join("")}` : "";
535
614
  const alsoFails = others.flatMap((column, i) => {
@@ -590,8 +669,14 @@ export function renderRun({
590
669
  return said ? `${head}${why ? "; " : ": "}` : head;
591
670
  },
592
671
  );
672
+ if (historyShown) {
673
+ lines.push(
674
+ `history: the earlier runs' verdicts, oldest first: ${(Object.entries(MARK) as [CaseVerdict, string][]).map(([v, mark]) => `${mark} ${v}`).join(" · ")}`,
675
+ );
676
+ }
593
677
  if (lines.at(-1) !== "") lines.push("");
594
- if (others.length > 0) lines.push(...columnLines(run, rows, others, trials, compared), "");
678
+ if (others.length > 0)
679
+ lines.push(...columnLines(run, rows, others, trials, compared, decidersOf), "");
595
680
 
596
681
  // Each count once, and only when it is some: a zero says nothing.
597
682
  const counted = (label: string, n: number, extra = "") =>
@@ -638,41 +723,23 @@ export function renderRun({
638
723
  ...counted("leaked", views.filter((t) => t.view.outcome === "leaked").length),
639
724
  ].join(" · "),
640
725
  );
641
- const ran = views.filter((t) => t.view.outcome !== "errored" && t.record.runId);
642
- const ms = median(ran.map((t) => t.record.ms));
643
- const priced = ran.filter((t) => t.record.estimate !== undefined);
644
- const cost = median(priced.map((t) => t.record.estimate!));
645
- const total = trials.reduce((sum, t) => sum + (t.record.estimate ?? 0), 0);
646
- const reusedCost = trials
647
- .filter((t) => t.record.reused)
648
- .reduce((sum, t) => sum + (t.record.estimate ?? 0), 0);
649
- const grading = trials.reduce(
650
- (sum, t) =>
651
- sum +
652
- t.scores.reduce(
653
- (s, score) => s + score.entries.reduce((e, x) => e + (x.estimate ?? 0), 0),
654
- 0,
655
- ),
656
- 0,
657
- );
658
- // A run with no agents costs nothing to price; one whose agents' count is unknown may.
659
- const noAgents = total === 0 && ran.every((t) => t.record.agents === 0);
660
- const unpriced = ran.some(
661
- (t) =>
662
- (t.record.estimate === undefined && t.record.agents !== 0) ||
663
- (t.record.priced ?? 0) < (t.record.agents ?? 0),
664
- );
665
- if (ms !== undefined) {
726
+ // With more than one column, the table above gave each column's medians: these would mix them.
727
+ const sums = trialSums(trials, decidersOf);
728
+ if (sums.ran) {
666
729
  lines.push(
667
730
  [
668
- `time per trial, median ${seconds(ms)}`,
669
- ...(noAgents
731
+ ...(others.length === 0 && sums.msMedian !== undefined
732
+ ? [`time per trial, median ${seconds(sums.msMedian)}`]
733
+ : []),
734
+ ...(sums.noAgents
670
735
  ? ["the workflow ran no agents"]
671
736
  : [
672
- ...(cost !== undefined ? [`est. cost per trial, median ${usd(cost)}`] : []),
673
- `all trials ${usd(total)}${reusedCost > 0 ? `, ${usd(reusedCost)} of it reused trials, run before` : ""}${unpriced ? ", partly unpriced" : ""}`,
737
+ ...(others.length === 0 && sums.estimateMedian !== undefined
738
+ ? [`est. cost per trial, median ${usd(sums.estimateMedian)}`]
739
+ : []),
740
+ `all trials ${usd(sums.estimate - sums.grading)}${sums.reused > 0 ? `, ${usd(sums.reused)} of it reused trials, run before` : ""}${sums.unpriced ? ", partly unpriced" : ""}`,
674
741
  ]),
675
- ...(grading > 0 ? [`grading ${usd(grading)}`] : []),
742
+ ...(sums.grading > 0 ? [`grading ${usd(sums.grading)}`] : []),
676
743
  ].join(" · "),
677
744
  );
678
745
  }
@@ -697,15 +764,22 @@ export function renderRun({
697
764
  exit === 3
698
765
  ? preconditionStop
699
766
  ? `the run stopped: ${PRECONDITION_STOP}`
700
- : partial
701
- ? run.broke !== undefined
702
- ? `the run is partial: the lab broke (${run.broke})`
703
- : "the run is partial"
704
- : !rows.some((r) => r.tally.verdict || r.tally.none === "graded by numbers")
705
- ? "no case was graded"
706
- : "a case is flaky or unclear"
767
+ : budgetStop
768
+ ? "stopped at --budget; run again to go on"
769
+ : partial
770
+ ? run.broke !== undefined
771
+ ? `the run is partial: the lab broke (${run.broke})`
772
+ : "the run is partial"
773
+ : !rows.some((r) => r.tally.verdict || r.tally.none === "graded by numbers")
774
+ ? "no case was graded"
775
+ : "a case is flaky or unclear"
707
776
  : EXITS[exit];
708
777
  lines.push(`exit ${exit}: ${why}`);
778
+ // A case unclear only for trials it never ran has nothing to show in detail.
779
+ const first = must.find(
780
+ (r) => failing(r.tally.verdict) && r.outcomes.some((o) => o !== "passed"),
781
+ );
782
+ if (first) lines.push(`detail: awf-lab report ${run.id.slice(-4)} --case ${first.name}`);
709
783
  return { text: lines.join("\n"), exit };
710
784
  }
711
785
 
@@ -722,8 +796,8 @@ export function renderCase({
722
796
  run: RunRecord;
723
797
  row: CaseRow;
724
798
  trials: readonly StoredTrialView[];
725
- /** How to open a trial's logs, by its id. */
726
- logsCommand: (trialId: string) => string;
799
+ /** How to open a trial's logs: the trial's folder by its id, `shown` in the command. */
800
+ logsCommand: (trialId: string, shown: string) => string;
727
801
  deciders?: ReadonlySet<string> | undefined;
728
802
  /** This case's row in each other column. */
729
803
  others?: readonly (CaseRow | undefined)[];
@@ -738,17 +812,17 @@ export function renderCase({
738
812
  ];
739
813
  const stages = stageLine(trials.filter((t) => t.record.column === 0));
740
814
  if (stages) lines.push(`time per stage, median: ${stages}`);
815
+ let unlabeled = false;
741
816
  for (const trial of trials) {
742
817
  const { record } = trial;
743
818
  const view = trialOutcome(record, trial.scores, deciders);
744
819
  lines.push(
745
820
  "",
746
- `trial ${record.trial}${record.column > 0 ? ` (${columnLabel(run, record.column)})` : ""}: ${view.outcome}${record.runId ? ` · id ${record.runId}` : ""} · ${seconds(record.ms)}`,
821
+ `trial ${record.trial}${record.column > 0 ? ` (${columnLabel(run, record.column)})` : ""}: ${view.outcome}${record.trialId ? ` · id ${record.trialId}` : ""} · ${seconds(record.ms)}`,
747
822
  );
748
- const label = record.runId ? labels.get(record.runId) : undefined;
823
+ const label = record.trialId ? labels.get(record.trialId) : undefined;
749
824
  if (label) lines.push(` labeled ${label.mark}${label.note ? `: ${label.note}` : ""}`);
750
- else if (record.runId)
751
- lines.push(` label: awf-lab label ${record.runId} --good | --needs-work "{note}"`);
825
+ else if (record.trialId) unlabeled = true;
752
826
  if (record.errored) {
753
827
  lines.push(
754
828
  ` errored by ${record.errored.by} in ${record.errored.phase}${record.errored.code ? ` (${record.errored.code})` : ""}: ${record.errored.reason}`,
@@ -766,8 +840,19 @@ export function renderCase({
766
840
  }
767
841
  const facts = factsOf(trial.scores);
768
842
  if (facts) lines.push(` facts: ${JSON.stringify(facts)}`);
769
- if (record.runFacts) lines.push(` run facts: ${JSON.stringify(record.runFacts)}`);
843
+ const counted = Object.entries(record.runFacts ?? {}).filter(([, n]) => n !== 0);
844
+ if (counted.length > 0) {
845
+ lines.push(` run facts: ${counted.map(([name, n]) => `${name} ${n}`).join(", ")}`);
846
+ }
770
847
  if (record.result !== undefined) lines.push(` result: ${JSON.stringify(record.result)}`);
848
+ // What the workflow's stages answered is where a failure's reason usually is; a stubbed stage
849
+ // is the case's own input.
850
+ if (view.outcome === "failed") {
851
+ for (const [stage, value] of Object.entries(record.stages ?? {})) {
852
+ if (stage in (record.stubbedStages ?? {})) continue;
853
+ lines.push(` stage ${stage}: ${clipped(JSON.stringify(value) ?? "undefined", 300)}`);
854
+ }
855
+ }
771
856
  for (const entry of newestEntries(trial.scores)) {
772
857
  const verdict = entry.errored
773
858
  ? `errored: ${entry.errored.reason}`
@@ -816,17 +901,30 @@ export function renderCase({
816
901
  }
817
902
  }
818
903
  if (record.files.length > 0) lines.push(` files: ${record.files.join(", ")}`);
819
- if (record.runId) lines.push(` logs: ${logsCommand(record.runId)}`);
820
904
  }
905
+ // Each trial's id is above: the commands that take one are said once.
906
+ const withIds = trials.filter((t) => t.record.trialId);
907
+ const logs = [...new Set(withIds.map((t) => logsCommand(t.record.trialId!, TRIAL_ID_SLOT)))];
908
+ if (unlabeled || logs.length > 0) lines.push("");
909
+ if (unlabeled) {
910
+ lines.push(`label a trial: awf-lab label ${TRIAL_ID_SLOT} --good | --needs-work "{note}"`);
911
+ }
912
+ for (const command of logs) lines.push(`a trial's logs: ${command}`);
821
913
  return lines.join("\n");
822
914
  }
823
915
 
916
+ const TRIAL_ID_SLOT = "{trial id}";
917
+
918
+ /** A text cut to `most` characters, an ellipsis saying it was. */
919
+ const clipped = (text: string, most: number) =>
920
+ text.length > most ? `${text.slice(0, most - 1)}…` : text;
921
+
824
922
  const SANDBOX_TEXT = {
825
923
  lab: "the lab's box around each trial, srt, reading no key; the workflow's own set aside",
826
924
  workflow: "the workflow's own, its agents' reach checked against sandbox.network",
827
925
  } as const;
828
926
 
829
- /** Who isolated the trials: one line, naming each column's when their `trial.ts`s differ. */
927
+ /** Who isolated the trials: one line, naming each column's when their `target.ts`s differ. */
830
928
  function sandboxLine(run: RunRecord): string {
831
929
  const byColumn = run.columns.map((c) => run.folders[c.folder]?.sandbox.by ?? "lab");
832
930
  const kinds = new Set(byColumn);
package/src/lab/reuse.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
1
+ import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import type { Params, TrialRecord } from "../format/records";
4
4
  import { versionKey } from "../shared/version";
5
5
  import { type CaseInfo, folderDigest, sha256 } from "./dataset";
6
- import { buildCase } from "./folders";
6
+ import { buildCase, makeSnapshotFolder } from "./folders";
7
7
  import { canonical } from "./grading";
8
8
  import { trialOutcome } from "./outcome";
9
9
  import { REUSED_FILE, readableRuns, type StoredTrial, type TrialRef, trialDirOf } from "./store";
@@ -12,7 +12,7 @@ import { durationMs } from "./trial-settings";
12
12
 
13
13
  // A stored trial is reused while what it was declared and given matches (lab-config, Versions):
14
14
  // the version's major, minor and pre-release tag, the `agentswf` and lab versions, the resolved
15
- // trial settings and parameters as values, and the built case by its content. The lab never looks
15
+ // target settings and parameters as values, and the built case by its content. The lab never looks
16
16
  // at the code: a change the person didn't version reuses the trials, and `--fresh` runs them again.
17
17
 
18
18
  export { REUSE_DAYS } from "./workspace";
@@ -23,7 +23,7 @@ export const LAB_VERSION: string = JSON.parse(
23
23
  ).version;
24
24
 
25
25
  export function reuseKey(parts: {
26
- /** The column's version: its `trial.ts`'s, or a loop's id of its code. */
26
+ /** The column's version: its `target.ts`'s, or a loop's id of its code. */
27
27
  version: string;
28
28
  params: Params;
29
29
  input: string;
@@ -37,7 +37,7 @@ export function reuseKey(parts: {
37
37
  ...parts.settings,
38
38
  limits: {
39
39
  setup: durationMs(limits.setup),
40
- run: durationMs(limits.run),
40
+ run: durationMs(limits.workflow),
41
41
  after: durationMs(limits.after),
42
42
  },
43
43
  };
@@ -50,7 +50,7 @@ export function reuseKey(parts: {
50
50
  function reusable(record: TrialRecord, now: Date, days: number): boolean {
51
51
  // One whose grading couldn't start is kept: `grading.ts` is what to fix, and grades it again.
52
52
  const errored = record.errored && record.errored.phase !== "facts";
53
- if (!record.reuse || !record.runId || record.leaked || errored) return false;
53
+ if (!record.reuse || !record.trialId || record.leaked || errored) return false;
54
54
  if (record.stopped !== undefined) return false;
55
55
  return now.getTime() - Date.parse(record.at) <= days * 86_400_000;
56
56
  }
@@ -70,8 +70,8 @@ export function reusableTrials(
70
70
  for (const { trials } of readableRuns(store, { dataset, newest: true })) {
71
71
  for (const trial of trials.sort((a, b) => b.record.at.localeCompare(a.record.at))) {
72
72
  const { record } = trial;
73
- if (!reusable(record, now, days) || seen.has(record.runId!)) continue;
74
- seen.add(record.runId!);
73
+ if (!reusable(record, now, days) || seen.has(record.trialId!)) continue;
74
+ seen.add(record.trialId!);
75
75
  // A trial the lab couldn't grade for its own reasons is run again rather than kept, and so is
76
76
  // one whose workflow threw: a fix to the bug should show on the next run, bumped or not.
77
77
  if (
@@ -97,30 +97,38 @@ export function reuseTrial(
97
97
  const dir = trialDirOf(store, run, from.record.case, column, trial);
98
98
  mkdirSync(dir, { recursive: true });
99
99
  const ref: TrialRef = from.record.reused ?? {
100
- run: from.record.run,
100
+ labRun: from.record.labRun,
101
101
  column: from.record.column,
102
102
  trial: from.record.trial,
103
103
  };
104
104
  writeFileSync(join(dir, REUSED_FILE), `${JSON.stringify(ref)}\n`);
105
105
  }
106
106
 
107
- /** Each case built once, as a trial would see it, for its digest. */
108
- export function caseDigests(
107
+ /** A case built as a trial sees it, and its digest. */
108
+ export type BuiltCase = { input: string; dir: string };
109
+
110
+ /**
111
+ * Each case built once as the run starts, which every trial of the run copies: a case edited while
112
+ * the run goes changes none of them. `drop` removes the built folders.
113
+ */
114
+ export function buildCases(
109
115
  dataDir: string,
110
116
  cases: readonly CaseInfo[],
111
117
  tmp: string,
112
- ): Map<string, string> {
113
- const digests = new Map<string, string>();
114
- const scratch = mkdtempSync(join(tmp, "awf-lab-reuse-"));
118
+ ): { built: Map<string, BuiltCase>; drop: () => void } {
119
+ const built = new Map<string, BuiltCase>();
120
+ const scratch = makeSnapshotFolder(tmp);
121
+ const drop = () => rmSync(scratch, { recursive: true, force: true });
115
122
  try {
116
123
  for (const [i, info] of cases.entries()) {
117
- const built = join(scratch, String(i));
118
- mkdirSync(built);
119
- buildCase(dataDir, info, built);
120
- digests.set(info.name, folderDigest(built));
124
+ const dir = join(scratch, String(i));
125
+ mkdirSync(dir);
126
+ buildCase(dataDir, info, dir);
127
+ built.set(info.name, { input: folderDigest(dir), dir });
121
128
  }
122
- } finally {
123
- rmSync(scratch, { recursive: true, force: true });
129
+ } catch (error) {
130
+ drop();
131
+ throw error;
124
132
  }
125
- return digests;
133
+ return { built, drop };
126
134
  }