@agentswf/lab 0.0.0-stage → 0.3.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/case-check.schema.json +64 -0
  5. package/schema/case-config.schema.json +187 -0
  6. package/schema/case-final.schema.json +48 -0
  7. package/schema/case-good-record.schema.json +70 -0
  8. package/schema/case-grade.schema.json +49 -0
  9. package/schema/case-key.schema.json +117 -0
  10. package/schema/case-label.schema.json +32 -0
  11. package/schema/case-loop-report.schema.json +210 -0
  12. package/schema/case-loop.schema.json +141 -0
  13. package/schema/case-plan.schema.json +117 -0
  14. package/schema/case-previous.schema.json +70 -0
  15. package/schema/case-proposal.schema.json +52 -0
  16. package/schema/case-report.schema.json +892 -0
  17. package/schema/case-run.schema.json +253 -0
  18. package/schema/case-runs.schema.json +125 -0
  19. package/schema/case-score.schema.json +319 -0
  20. package/schema/case-spread.schema.json +110 -0
  21. package/schema/case-trial.schema.json +543 -0
  22. package/schema/case-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 +128 -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 +182 -0
  39. package/src/format/key.ts +144 -0
  40. package/src/format/loop.ts +178 -0
  41. package/src/format/output.ts +481 -0
  42. package/src/format/records.ts +440 -0
  43. package/src/format/schema-files.ts +64 -0
  44. package/src/format/settings.ts +552 -0
  45. package/src/format/trial-env.ts +64 -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 +38 -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 +60 -0
  54. package/src/help/runs.txt +10 -0
  55. package/src/help/score.txt +27 -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 +249 -0
  61. package/src/lab/capture-command.ts +298 -0
  62. package/src/lab/capture.ts +234 -0
  63. package/src/lab/check-command.ts +187 -0
  64. package/src/lab/cli.ts +324 -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 +278 -0
  70. package/src/lab/criteria.ts +69 -0
  71. package/src/lab/dataset.ts +356 -0
  72. package/src/lab/eval-files.ts +185 -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 +153 -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 +226 -0
  82. package/src/lab/labels.ts +206 -0
  83. package/src/lab/loop-code.ts +255 -0
  84. package/src/lab/loop-command.ts +770 -0
  85. package/src/lab/loop-records.ts +144 -0
  86. package/src/lab/loop.ts +859 -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 +150 -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 +156 -0
  96. package/src/lab/report.ts +838 -0
  97. package/src/lab/reuse.ts +126 -0
  98. package/src/lab/run-command.ts +853 -0
  99. package/src/lab/run-logs.ts +89 -0
  100. package/src/lab/run-report.ts +259 -0
  101. package/src/lab/runs-command.ts +163 -0
  102. package/src/lab/runs.ts +172 -0
  103. package/src/lab/scaffold.ts +91 -0
  104. package/src/lab/score-command.ts +189 -0
  105. package/src/lab/score.ts +396 -0
  106. package/src/lab/store.ts +383 -0
  107. package/src/lab/text.ts +29 -0
  108. package/src/lab/trial-settings.ts +100 -0
  109. package/src/lab/trial-stub.txt +18 -0
  110. package/src/lab/trial.ts +443 -0
  111. package/src/lab/usage.ts +273 -0
  112. package/src/lab/verdict.ts +161 -0
  113. package/src/lab/workflow-graders.ts +161 -0
  114. package/src/lab/workspace.ts +489 -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,278 @@
1
+ import { existsSync, readdirSync, statSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { type CaseConfig, CaseConfigSchema, CONFIG_KIND } from "../format/output";
4
+ import { LIMIT_PHASES, type TrialFile } from "../format/settings";
5
+ import { checkSchema, describeProblems } from "../shared/validate";
6
+ import { type Context, helperPlace, shownPath, workspaceOf } from "./command-context";
7
+ import { type DatasetInfo, KEY_FILE, readCases, readDataset } from "./dataset";
8
+ import {
9
+ CONFIG_FILE,
10
+ checkGradingFile,
11
+ checkTrialFile,
12
+ describeFiles,
13
+ GRADING_FILE,
14
+ TRIAL_FILE,
15
+ } from "./eval-files";
16
+ import { ALWAYS_DENIED } from "./loop-code";
17
+ import { Refusal } from "./refusal";
18
+ import { resolveSettings } from "./trial-settings";
19
+ import { type CaseWorkspace, chooseDataset, chooseLoop, DEFAULT, type LoopRef } from "./workspace";
20
+
21
+ // `config`: every file the lab reads, loaded and checked, and where each value came from, as
22
+ // `git config --show-origin` shows it.
23
+
24
+ type Row = [name: string, value: string, from: string];
25
+
26
+ function table(rows: readonly Row[]): string[] {
27
+ const width = (i: 0 | 1) => Math.max(...rows.map((row) => row[i].length));
28
+ const [name, value] = [width(0), width(1)];
29
+ return rows.map(([n, v, f]) => ` ${n.padEnd(name)} ${v.padEnd(value)} ${f}`.trimEnd());
30
+ }
31
+
32
+ export function config(context: Context): number {
33
+ const { options } = context;
34
+ const workspace = workspaceOf(context);
35
+ const place = helperPlace(context, workspace);
36
+ const shown = (path: string) => (path === DEFAULT ? DEFAULT : shownPath(context, path));
37
+ if (
38
+ options.case !== undefined &&
39
+ options.dataset === undefined &&
40
+ workspace.datasets.length > 1
41
+ ) {
42
+ throw new Refusal("config --case {name} needs --dataset {name}, of more than one dataset");
43
+ }
44
+ if (options.loop !== undefined && (options.dataset !== undefined || options.case !== undefined)) {
45
+ throw new Refusal("config --loop {name} shows one loop: it takes no --dataset or --case");
46
+ }
47
+ const listing = {
48
+ project: shown(workspace.project),
49
+ config: workspace.configFile ? shown(workspace.configFile) : null,
50
+ local: workspace.localFile ? shown(workspace.localFile) : null,
51
+ datasets: workspace.datasets.map((d) => ({
52
+ name: d.name,
53
+ eval: shown(d.eval.value),
54
+ data: shown(d.data.value),
55
+ definedIn: d.definedIn.map(shown),
56
+ })),
57
+ loops: workspace.loops.map((l) => ({
58
+ name: l.name,
59
+ dataset: l.dataset.value,
60
+ definedIn: l.definedIn.map(shown),
61
+ })),
62
+ };
63
+ const print = (json: CaseConfig) => {
64
+ const checked = checkSchema(CaseConfigSchema, json);
65
+ if (!checked.ok) throw new Error(describeProblems("config --json", checked.problems));
66
+ context.stdout(JSON.stringify(json, null, 2));
67
+ };
68
+ const rows = (list: readonly Row[]) => list.map(([name, value, from]) => ({ name, value, from }));
69
+ if (options.loop !== undefined) {
70
+ const values = loopValues(chooseLoop(workspace, options.loop), shown);
71
+ if (options.json) {
72
+ print({ kind: CONFIG_KIND, ...listing, loop: { name: options.loop, values: rows(values) } });
73
+ return 0;
74
+ }
75
+ context.stdout([`loop ${options.loop}`, ...table(values)].join("\n"));
76
+ return 0;
77
+ }
78
+ if (options.dataset !== undefined || options.case !== undefined) {
79
+ const info = readDataset(chooseDataset(workspace, options.dataset), place);
80
+ const values = datasetValues(context, workspace, info, shown);
81
+ if (options.json) {
82
+ print({
83
+ kind: CONFIG_KIND,
84
+ ...listing,
85
+ dataset: { name: info.name, values: rows(values.dataset) },
86
+ ...(values.case
87
+ ? { case: { name: values.case.name, values: rows(values.case.rows) } }
88
+ : {}),
89
+ });
90
+ return 0;
91
+ }
92
+ context.stdout(
93
+ [
94
+ `dataset ${info.name}`,
95
+ ...table(values.dataset),
96
+ ...(values.case ? [`case ${values.case.name}`, ...table(values.case.rows)] : []),
97
+ ].join("\n"),
98
+ );
99
+ return 0;
100
+ }
101
+ // Every eval folder is loaded, a dataset's or not, so a mistake in one shows before it is used.
102
+ const used = new Set(workspace.datasets.map((d) => d.eval.value));
103
+ for (const dataset of workspace.datasets) readDataset(dataset, place);
104
+ for (const loop of workspace.loops) loopValues(loop, shown);
105
+ for (const name of existsSync(workspace.evals) ? readdirSync(workspace.evals) : []) {
106
+ const dir = join(workspace.evals, name);
107
+ if (used.has(dir) || !existsSync(join(dir, TRIAL_FILE))) continue;
108
+ const files = {
109
+ trial: join(dir, TRIAL_FILE),
110
+ ...(existsSync(join(dir, GRADING_FILE)) ? { grading: join(dir, GRADING_FILE) } : {}),
111
+ };
112
+ const described = describeFiles(files, place);
113
+ checkTrialFile(files.trial, described.trial);
114
+ if (files.grading) checkGradingFile(files.grading, described.grading);
115
+ }
116
+ if (options.json) {
117
+ print({ kind: CONFIG_KIND, ...listing });
118
+ return 0;
119
+ }
120
+ const lines = [
121
+ `project ${shown(workspace.project)}`,
122
+ ...table([
123
+ ["config", workspace.configFile ? shown(workspace.configFile) : "none", ""],
124
+ ["local", workspace.localFile ? shown(workspace.localFile) : "none", ""],
125
+ ]),
126
+ "datasets",
127
+ ...(workspace.datasets.length > 0
128
+ ? table(
129
+ workspace.datasets.map((d) => [
130
+ d.name,
131
+ d.eval.value === d.data.value
132
+ ? shown(d.eval.value)
133
+ : `eval ${shown(d.eval.value)} · data ${shown(d.data.value)}`,
134
+ d.definedIn.map(shown).join(", "),
135
+ ]),
136
+ )
137
+ : [" none: awf-lab case new {dataset}/{case} makes one"]),
138
+ "loops",
139
+ ...(workspace.loops.length > 0
140
+ ? table(
141
+ workspace.loops.map((l) => [
142
+ l.name,
143
+ `dataset ${l.dataset.value}`,
144
+ l.definedIn.map(shown).join(", "),
145
+ ]),
146
+ )
147
+ : [` none: an entry of loops in evals/${CONFIG_FILE} makes one`]),
148
+ ];
149
+ context.stdout(lines.join("\n"));
150
+ return 0;
151
+ }
152
+
153
+ /**
154
+ * A loop's every value and the file it came from, the edits every loop is denied included; refused
155
+ * when its brief is missing.
156
+ */
157
+ function loopValues(loop: LoopRef, shown: (path: string) => string): Row[] {
158
+ const brief = loop.brief.value;
159
+ if (!existsSync(brief) || !statSync(brief).isFile()) {
160
+ throw new Refusal(`${loop.brief.from}: loop ${loop.name}'s brief ${brief} doesn't exist`);
161
+ }
162
+ const list = (xs: readonly string[]) => (xs.length > 0 ? xs.join(", ") : "none");
163
+ const traced = (name: string, value: string, from: string): Row => [name, value, shown(from)];
164
+ return [
165
+ traced("dataset", loop.dataset.value, loop.dataset.from),
166
+ traced("brief", shown(brief), loop.brief.from),
167
+ traced("edit.allow", list(loop.allow.value), loop.allow.from),
168
+ traced("edit.deny", list(loop.deny.value), loop.deny.from),
169
+ ["edit.always-denied", ALWAYS_DENIED.join(", "), "built in"],
170
+ traced("proposer", loop.proposer.value, loop.proposer.from),
171
+ traced("trials", String(loop.trials.value), loop.trials.from),
172
+ ...Object.entries(loop.params).map(([name, p]) => traced(`params.${name}`, p.value, p.from)),
173
+ loop.cases
174
+ ? traced("cases", list(loop.cases.value), loop.cases.from)
175
+ : ["cases", "every case not held out", DEFAULT],
176
+ loop.tags ? traced("tags", list(loop.tags.value), loop.tags.from) : ["tags", "any", DEFAULT],
177
+ traced("spread", String(loop.spread.value), loop.spread.from),
178
+ ];
179
+ }
180
+
181
+ /** `trial.ts`'s sandbox and limits, each part resolved, from the file or the lab's default. */
182
+ function settingRows(trial: TrialFile, file: string): Row[] {
183
+ const resolved = resolveSettings(trial);
184
+ const { sandbox, limits } = resolved;
185
+ const given = trial.sandbox;
186
+ const list = (xs: readonly string[]) => (xs.length > 0 ? xs.join(", ") : "none");
187
+ const row = (name: string, value: string, set: boolean): Row => [
188
+ name,
189
+ value,
190
+ set ? file : DEFAULT,
191
+ ];
192
+ return [
193
+ row("sandbox.by", sandbox.by, given?.by !== undefined),
194
+ ...(sandbox.by === "lab"
195
+ ? [row("sandbox.provider", sandbox.provider, given?.provider !== undefined)]
196
+ : []),
197
+ row(
198
+ "sandbox.network.agents",
199
+ list(sandbox.network.agents),
200
+ given?.network?.agents !== undefined,
201
+ ),
202
+ row("sandbox.network.code", list(sandbox.network.code), given?.network?.code !== undefined),
203
+ row(
204
+ "sandbox.network.webSearch",
205
+ String(sandbox.network.webSearch),
206
+ given?.network?.webSearch !== undefined,
207
+ ),
208
+ row(
209
+ "sandbox.network.sockets",
210
+ list(sandbox.network.sockets),
211
+ given?.network?.sockets !== undefined,
212
+ ),
213
+ ...LIMIT_PHASES.map((phase) =>
214
+ row(`limits.${phase}`, limits[phase], trial.limits?.[phase] !== undefined),
215
+ ),
216
+ ];
217
+ }
218
+
219
+ /** One dataset's every value and the file it came from, and with `--case` one case's from its key. */
220
+ function datasetValues(
221
+ context: Context,
222
+ workspace: CaseWorkspace,
223
+ info: DatasetInfo,
224
+ shown: (path: string) => string,
225
+ ): { dataset: Row[]; case?: { name: string; rows: Row[] } } {
226
+ const { ref, trial, grading } = info;
227
+ const trialFile = shown(join(info.evalDir, TRIAL_FILE));
228
+ const gradingFile = shown(join(info.evalDir, GRADING_FILE));
229
+ const graders = (grading.graders ?? []).map(
230
+ (g) =>
231
+ `${g.name} (${g.grader === "code" ? "code" : g.grader === "workflow" ? shown(g.file) : `sentences, ${g.agent}`}${"manual" in g && g.manual ? ", manual" : ""})`,
232
+ );
233
+ const rows: Row[] = [
234
+ ["eval", shown(ref.eval.value), shown(ref.eval.from)],
235
+ ["data", shown(ref.data.value), shown(ref.data.from)],
236
+ [
237
+ "records",
238
+ ref.records ? shown(ref.records.value) : "none",
239
+ ref.records ? shown(ref.records.from) : "",
240
+ ],
241
+ ...Object.entries(info.params).map(
242
+ ([name, p]): Row => [`params.${name}`, p.value ?? "required, no value", shown(p.from)],
243
+ ),
244
+ ...settingRows(trial, trialFile),
245
+ ["pass", grading.pass ?? "all", grading.pass ? gradingFile : DEFAULT],
246
+ ["graders", graders.join(", ") || "none", gradingFile],
247
+ ["compare", grading.compare ? "set" : "none", gradingFile],
248
+ ["store", shown(workspace.store), shown(workspace.from.store)],
249
+ ["concurrency", String(workspace.concurrency), shown(workspace.from.concurrency)],
250
+ ["allowance", `${workspace.allowance}%`, shown(workspace.from.allowance)],
251
+ ["reuse.days", String(workspace.reuseDays), shown(workspace.from.reuseDays)],
252
+ ["ignore", workspace.ignore.join(", ") || "none", shown(workspace.from.ignore)],
253
+ ];
254
+ const name = context.options.case;
255
+ if (name === undefined) return { dataset: rows };
256
+ const found = readCases(info).find((c) => c.name === name);
257
+ if (!found) throw new Refusal(`no case ${name} in ${info.name}`);
258
+ const key = shown(join(found.dir, KEY_FILE));
259
+ const flag = (value: true | undefined): [string, string] =>
260
+ value ? ["yes", key] : ["no", DEFAULT];
261
+ return {
262
+ dataset: rows,
263
+ case: {
264
+ name,
265
+ rows: [
266
+ [
267
+ "pass",
268
+ found.key.pass ?? grading.pass ?? "all",
269
+ found.key.pass ? key : grading.pass ? gradingFile : DEFAULT,
270
+ ],
271
+ ["holdout", ...flag(found.key.holdout)],
272
+ ["draft", ...flag(found.key.draft)],
273
+ ["known-failing", ...flag(found.key["known-failing"])],
274
+ ["tags", found.key.tags?.join(", ") ?? "none", found.key.tags ? key : DEFAULT],
275
+ ],
276
+ },
277
+ };
278
+ }
@@ -0,0 +1,69 @@
1
+ import { join } from "node:path";
2
+ import { criteriaOf } from "../format/key";
3
+ import type { Key } from "../format/settings";
4
+ import { JUDGE_VERSION } from "../judge/version";
5
+ import type { GraderSpec } from "./dataset";
6
+ import { sha256 } from "./dataset";
7
+ import { canonical, type LabGrader } from "./grading";
8
+ import { graderWorkflow, type WorkflowGraderLab } from "./workflow-graders";
9
+
10
+ /** The criteria judge's workflow, which this checkout's own `awf` runs. */
11
+ const JUDGE_FILE = join(import.meta.dir, "../judge/criteria.workflow.ts");
12
+
13
+ const hasCriteria = (key: Key) => criteriaOf(key).length > 0;
14
+
15
+ /**
16
+ * A `sentences` grader of `grading.ts`: the criteria judge, a grader workflow the lab ships. It
17
+ * applies to a case whose key has `must` or `must-not`, its grades are kept against those
18
+ * sentences alone, and it records the agent it judged with, so a change of agent judges every
19
+ * criterion again. Its voters read the trial: the result, facts, stages and saved files.
20
+ */
21
+ export function sentencesGrader(
22
+ lab: Pick<WorkflowGraderLab, "runner" | "runRoot" | "now">,
23
+ spec: Extract<GraderSpec, { kind: "sentences" }>,
24
+ ): LabGrader {
25
+ const { name, agent, version } = spec;
26
+ const run = graderWorkflow(
27
+ { ...lab, awf: async () => undefined },
28
+ {
29
+ name,
30
+ file: JUDGE_FILE,
31
+ reads: "trial",
32
+ manual: spec.manual,
33
+ timeout: "20m",
34
+ argv: [
35
+ "--harness",
36
+ agent.harness,
37
+ "--model",
38
+ agent.model,
39
+ ...(agent.voters ? ["--voters", String(agent.voters)] : []),
40
+ ],
41
+ },
42
+ );
43
+ return {
44
+ ...run,
45
+ version,
46
+ agent,
47
+ // The lab's own judge prompt is a setting of it: a new one judges every criterion again.
48
+ settings: {
49
+ agent: { harness: agent.harness, model: agent.model, voters: agent.voters ?? 3 },
50
+ judge: JUDGE_VERSION,
51
+ },
52
+ appliesTo: hasCriteria,
53
+ keyOf: (info) => sha256(canonical(criteriaOf(info.key).map((c) => `${c.field}: ${c.text}`))),
54
+ async grade(call) {
55
+ // Its grade folder holds the criteria alone: no `expect`, no `key/`. Its voters work beside
56
+ // it, so nothing there may be more than what they are shown anyway.
57
+ const { keyDir: _, ...shown } = call.case;
58
+ const key = {
59
+ ...(call.case.key.must ? { must: call.case.key.must } : {}),
60
+ ...(call.case.key["must-not"] ? { "must-not": call.case.key["must-not"] } : {}),
61
+ };
62
+ try {
63
+ return { ...(await run.grade({ ...call, case: { ...shown, key } })), agent };
64
+ } catch (error) {
65
+ throw Object.assign(error as Error, { version });
66
+ }
67
+ },
68
+ };
69
+ }