@agentswf/lab 0.0.0-stage → 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 (125) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -2
  3. package/package.json +46 -4
  4. package/schema/lab-check.schema.json +64 -0
  5. package/schema/lab-config.schema.json +187 -0
  6. package/schema/lab-final.schema.json +48 -0
  7. package/schema/lab-good-record.schema.json +70 -0
  8. package/schema/lab-grade.schema.json +49 -0
  9. package/schema/lab-key.schema.json +124 -0
  10. package/schema/lab-label.schema.json +32 -0
  11. package/schema/lab-loop-report.schema.json +221 -0
  12. package/schema/lab-loop.schema.json +141 -0
  13. package/schema/lab-plan.schema.json +128 -0
  14. package/schema/lab-previous.schema.json +70 -0
  15. package/schema/lab-proposal.schema.json +52 -0
  16. package/schema/lab-report.schema.json +1128 -0
  17. package/schema/lab-run.schema.json +253 -0
  18. package/schema/lab-runs.schema.json +132 -0
  19. package/schema/lab-score.schema.json +319 -0
  20. package/schema/lab-spread.schema.json +110 -0
  21. package/schema/lab-trial.schema.json +543 -0
  22. package/schema/lab-try.schema.json +113 -0
  23. package/schema/local.json +155 -0
  24. package/src/cli.ts +9 -0
  25. package/src/env/capture.ts +48 -0
  26. package/src/env/compare.ts +201 -0
  27. package/src/env/facts.ts +51 -0
  28. package/src/env/fetch.ts +57 -0
  29. package/src/env/files.ts +228 -0
  30. package/src/env/grade-case.ts +80 -0
  31. package/src/env/grade-helper.ts +352 -0
  32. package/src/env/grader.ts +131 -0
  33. package/src/env/index.ts +727 -0
  34. package/src/env/mock.ts +374 -0
  35. package/src/env/routes.ts +90 -0
  36. package/src/env/sources.ts +19 -0
  37. package/src/env/state.ts +79 -0
  38. package/src/env/trial.ts +185 -0
  39. package/src/format/key.ts +168 -0
  40. package/src/format/loop.ts +179 -0
  41. package/src/format/output.ts +542 -0
  42. package/src/format/records.ts +443 -0
  43. package/src/format/schema-files.ts +64 -0
  44. package/src/format/settings.ts +559 -0
  45. package/src/format/trial-env.ts +67 -0
  46. package/src/format/trial-file.ts +134 -0
  47. package/src/help/awf-lab.txt +29 -0
  48. package/src/help/case.txt +39 -0
  49. package/src/help/config.txt +14 -0
  50. package/src/help/label.txt +12 -0
  51. package/src/help/loop.txt +44 -0
  52. package/src/help/report.txt +20 -0
  53. package/src/help/run.txt +61 -0
  54. package/src/help/runs.txt +11 -0
  55. package/src/help/score.txt +28 -0
  56. package/src/judge/criteria.workflow.ts +237 -0
  57. package/src/judge/version.ts +6 -0
  58. package/src/lab/answer.ts +47 -0
  59. package/src/lab/blame.ts +204 -0
  60. package/src/lab/box.ts +251 -0
  61. package/src/lab/capture-command.ts +302 -0
  62. package/src/lab/capture.ts +234 -0
  63. package/src/lab/check-command.ts +187 -0
  64. package/src/lab/cli.ts +325 -0
  65. package/src/lab/code-graders.ts +108 -0
  66. package/src/lab/columns.ts +138 -0
  67. package/src/lab/command-context.ts +436 -0
  68. package/src/lab/compare.ts +123 -0
  69. package/src/lab/config-command.ts +283 -0
  70. package/src/lab/criteria.ts +69 -0
  71. package/src/lab/dataset.ts +375 -0
  72. package/src/lab/eval-files.ts +196 -0
  73. package/src/lab/expect.ts +79 -0
  74. package/src/lab/facts.ts +59 -0
  75. package/src/lab/families.ts +32 -0
  76. package/src/lab/folders.ts +172 -0
  77. package/src/lab/git.ts +128 -0
  78. package/src/lab/good.ts +224 -0
  79. package/src/lab/grading-stub.txt +8 -0
  80. package/src/lab/grading.ts +332 -0
  81. package/src/lab/inspect.ts +227 -0
  82. package/src/lab/labels.ts +211 -0
  83. package/src/lab/loop-code.ts +255 -0
  84. package/src/lab/loop-command.ts +774 -0
  85. package/src/lab/loop-records.ts +144 -0
  86. package/src/lab/loop.ts +864 -0
  87. package/src/lab/marker.ts +23 -0
  88. package/src/lab/outcome.ts +196 -0
  89. package/src/lab/pooled.ts +106 -0
  90. package/src/lab/project-env.ts +39 -0
  91. package/src/lab/rates.ts +155 -0
  92. package/src/lab/reach.ts +111 -0
  93. package/src/lab/record-copies.ts +202 -0
  94. package/src/lab/refusal.ts +2 -0
  95. package/src/lab/report-command.ts +157 -0
  96. package/src/lab/report.ts +936 -0
  97. package/src/lab/reuse.ts +134 -0
  98. package/src/lab/run-command.ts +958 -0
  99. package/src/lab/run-logs.ts +89 -0
  100. package/src/lab/run-report.ts +286 -0
  101. package/src/lab/runs-command.ts +171 -0
  102. package/src/lab/runs.ts +172 -0
  103. package/src/lab/scaffold.ts +94 -0
  104. package/src/lab/score-command.ts +197 -0
  105. package/src/lab/score.ts +396 -0
  106. package/src/lab/store.ts +383 -0
  107. package/src/lab/target-stub.txt +18 -0
  108. package/src/lab/text.ts +29 -0
  109. package/src/lab/trial-settings.ts +105 -0
  110. package/src/lab/trial.ts +439 -0
  111. package/src/lab/usage.ts +274 -0
  112. package/src/lab/verdict.ts +161 -0
  113. package/src/lab/workflow-graders.ts +164 -0
  114. package/src/lab/workspace.ts +496 -0
  115. package/src/loop/propose.workflow.ts +84 -0
  116. package/src/shared/allowance.ts +185 -0
  117. package/src/shared/compare/index.ts +3 -0
  118. package/src/shared/compare/paired.ts +397 -0
  119. package/src/shared/compare/stats.ts +139 -0
  120. package/src/shared/compare/types.ts +75 -0
  121. package/src/shared/config.ts +15 -0
  122. package/src/shared/runner.ts +321 -0
  123. package/src/shared/validate.ts +181 -0
  124. package/src/shared/version.ts +41 -0
  125. package/src/write-schemas.ts +10 -0
@@ -0,0 +1,134 @@
1
+ import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import type { Params, TrialRecord } from "../format/records";
4
+ import { versionKey } from "../shared/version";
5
+ import { type CaseInfo, folderDigest, sha256 } from "./dataset";
6
+ import { buildCase, makeSnapshotFolder } from "./folders";
7
+ import { canonical } from "./grading";
8
+ import { trialOutcome } from "./outcome";
9
+ import { REUSED_FILE, readableRuns, type StoredTrial, type TrialRef, trialDirOf } from "./store";
10
+ import type { ResolvedSettings } from "./trial-settings";
11
+ import { durationMs } from "./trial-settings";
12
+
13
+ // A stored trial is reused while what it was declared and given matches (lab-config, Versions):
14
+ // the version's major, minor and pre-release tag, the `agentswf` and lab versions, the resolved
15
+ // target settings and parameters as values, and the built case by its content. The lab never looks
16
+ // at the code: a change the person didn't version reuses the trials, and `--fresh` runs them again.
17
+
18
+ export { REUSE_DAYS } from "./workspace";
19
+
20
+ /** This lab's own version, which a trial's record is made by. */
21
+ export const LAB_VERSION: string = JSON.parse(
22
+ readFileSync(join(import.meta.dir, "../../package.json"), "utf8"),
23
+ ).version;
24
+
25
+ export function reuseKey(parts: {
26
+ /** The column's version: its `target.ts`'s, or a loop's id of its code. */
27
+ version: string;
28
+ params: Params;
29
+ input: string;
30
+ /** The column's sandbox and limits, resolved. */
31
+ settings: ResolvedSettings;
32
+ awf: string;
33
+ }): string {
34
+ // A limit is its length, however written: 60s and 1m are one setting.
35
+ const { limits } = parts.settings;
36
+ const settings = {
37
+ ...parts.settings,
38
+ limits: {
39
+ setup: durationMs(limits.setup),
40
+ run: durationMs(limits.workflow),
41
+ after: durationMs(limits.after),
42
+ },
43
+ };
44
+ return sha256(
45
+ canonical({ ...parts, settings, version: versionKey(parts.version), lab: LAB_VERSION }),
46
+ );
47
+ }
48
+
49
+ /** A stored trial that may stand in for a new one: it ran to an end the workflow owns. */
50
+ function reusable(record: TrialRecord, now: Date, days: number): boolean {
51
+ // One whose grading couldn't start is kept: `grading.ts` is what to fix, and grades it again.
52
+ const errored = record.errored && record.errored.phase !== "facts";
53
+ if (!record.reuse || !record.trialId || record.leaked || errored) return false;
54
+ if (record.stopped !== undefined) return false;
55
+ return now.getTime() - Date.parse(record.at) <= days * 86_400_000;
56
+ }
57
+
58
+ /**
59
+ * The stored trials of a dataset each reuse key may take, newest first, one per workflow run (a
60
+ * reused trial is a copy, and never counts twice).
61
+ */
62
+ export function reusableTrials(
63
+ store: string,
64
+ dataset: string,
65
+ now: Date,
66
+ days: number,
67
+ ): Map<string, StoredTrial[]> {
68
+ const byKey = new Map<string, StoredTrial[]>();
69
+ const seen = new Set<string>();
70
+ for (const { trials } of readableRuns(store, { dataset, newest: true })) {
71
+ for (const trial of trials.sort((a, b) => b.record.at.localeCompare(a.record.at))) {
72
+ const { record } = trial;
73
+ if (!reusable(record, now, days) || seen.has(record.trialId!)) continue;
74
+ seen.add(record.trialId!);
75
+ // A trial the lab couldn't grade for its own reasons is run again rather than kept, and so is
76
+ // one whose workflow threw: a fix to the bug should show on the next run, bumped or not.
77
+ if (
78
+ record.workflowError !== undefined ||
79
+ (trialOutcome(record, trial.scores).outcome === "errored" &&
80
+ record.errored?.phase !== "facts")
81
+ )
82
+ continue;
83
+ byKey.set(record.reuse!, [...(byKey.get(record.reuse!) ?? []), trial]);
84
+ }
85
+ }
86
+ return byKey;
87
+ }
88
+
89
+ /** Puts a stored trial in a run as that run's trial: a pointer to where it ran, never a copy. */
90
+ export function reuseTrial(
91
+ store: string,
92
+ run: string,
93
+ from: StoredTrial,
94
+ column: number,
95
+ trial: number,
96
+ ): void {
97
+ const dir = trialDirOf(store, run, from.record.case, column, trial);
98
+ mkdirSync(dir, { recursive: true });
99
+ const ref: TrialRef = from.record.reused ?? {
100
+ labRun: from.record.labRun,
101
+ column: from.record.column,
102
+ trial: from.record.trial,
103
+ };
104
+ writeFileSync(join(dir, REUSED_FILE), `${JSON.stringify(ref)}\n`);
105
+ }
106
+
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(
115
+ dataDir: string,
116
+ cases: readonly CaseInfo[],
117
+ tmp: string,
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 });
122
+ try {
123
+ for (const [i, info] of cases.entries()) {
124
+ const dir = join(scratch, String(i));
125
+ mkdirSync(dir);
126
+ buildCase(dataDir, info, dir);
127
+ built.set(info.name, { input: folderDigest(dir), dir });
128
+ }
129
+ } catch (error) {
130
+ drop();
131
+ throw error;
132
+ }
133
+ return { built, drop };
134
+ }