@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.
- package/LICENSE +21 -0
- package/README.md +15 -2
- package/package.json +46 -4
- package/schema/lab-check.schema.json +64 -0
- package/schema/lab-config.schema.json +187 -0
- package/schema/lab-final.schema.json +48 -0
- package/schema/lab-good-record.schema.json +70 -0
- package/schema/lab-grade.schema.json +49 -0
- package/schema/lab-key.schema.json +124 -0
- package/schema/lab-label.schema.json +32 -0
- package/schema/lab-loop-report.schema.json +221 -0
- package/schema/lab-loop.schema.json +141 -0
- package/schema/lab-plan.schema.json +128 -0
- package/schema/lab-previous.schema.json +70 -0
- package/schema/lab-proposal.schema.json +52 -0
- package/schema/lab-report.schema.json +1128 -0
- package/schema/lab-run.schema.json +253 -0
- package/schema/lab-runs.schema.json +132 -0
- package/schema/lab-score.schema.json +319 -0
- package/schema/lab-spread.schema.json +110 -0
- package/schema/lab-trial.schema.json +543 -0
- package/schema/lab-try.schema.json +113 -0
- package/schema/local.json +155 -0
- package/src/cli.ts +9 -0
- package/src/env/capture.ts +48 -0
- package/src/env/compare.ts +201 -0
- package/src/env/facts.ts +51 -0
- package/src/env/fetch.ts +57 -0
- package/src/env/files.ts +228 -0
- package/src/env/grade-case.ts +80 -0
- package/src/env/grade-helper.ts +352 -0
- package/src/env/grader.ts +131 -0
- package/src/env/index.ts +727 -0
- package/src/env/mock.ts +374 -0
- package/src/env/routes.ts +90 -0
- package/src/env/sources.ts +19 -0
- package/src/env/state.ts +79 -0
- package/src/env/trial.ts +185 -0
- package/src/format/key.ts +168 -0
- package/src/format/loop.ts +179 -0
- package/src/format/output.ts +542 -0
- package/src/format/records.ts +443 -0
- package/src/format/schema-files.ts +64 -0
- package/src/format/settings.ts +559 -0
- package/src/format/trial-env.ts +67 -0
- package/src/format/trial-file.ts +134 -0
- package/src/help/awf-lab.txt +29 -0
- package/src/help/case.txt +39 -0
- package/src/help/config.txt +14 -0
- package/src/help/label.txt +12 -0
- package/src/help/loop.txt +44 -0
- package/src/help/report.txt +20 -0
- package/src/help/run.txt +61 -0
- package/src/help/runs.txt +11 -0
- package/src/help/score.txt +28 -0
- package/src/judge/criteria.workflow.ts +237 -0
- package/src/judge/version.ts +6 -0
- package/src/lab/answer.ts +47 -0
- package/src/lab/blame.ts +204 -0
- package/src/lab/box.ts +251 -0
- package/src/lab/capture-command.ts +302 -0
- package/src/lab/capture.ts +234 -0
- package/src/lab/check-command.ts +187 -0
- package/src/lab/cli.ts +325 -0
- package/src/lab/code-graders.ts +108 -0
- package/src/lab/columns.ts +138 -0
- package/src/lab/command-context.ts +436 -0
- package/src/lab/compare.ts +123 -0
- package/src/lab/config-command.ts +283 -0
- package/src/lab/criteria.ts +69 -0
- package/src/lab/dataset.ts +375 -0
- package/src/lab/eval-files.ts +196 -0
- package/src/lab/expect.ts +79 -0
- package/src/lab/facts.ts +59 -0
- package/src/lab/families.ts +32 -0
- package/src/lab/folders.ts +172 -0
- package/src/lab/git.ts +128 -0
- package/src/lab/good.ts +224 -0
- package/src/lab/grading-stub.txt +8 -0
- package/src/lab/grading.ts +332 -0
- package/src/lab/inspect.ts +227 -0
- package/src/lab/labels.ts +211 -0
- package/src/lab/loop-code.ts +255 -0
- package/src/lab/loop-command.ts +774 -0
- package/src/lab/loop-records.ts +144 -0
- package/src/lab/loop.ts +864 -0
- package/src/lab/marker.ts +23 -0
- package/src/lab/outcome.ts +196 -0
- package/src/lab/pooled.ts +106 -0
- package/src/lab/project-env.ts +39 -0
- package/src/lab/rates.ts +155 -0
- package/src/lab/reach.ts +111 -0
- package/src/lab/record-copies.ts +202 -0
- package/src/lab/refusal.ts +2 -0
- package/src/lab/report-command.ts +157 -0
- package/src/lab/report.ts +936 -0
- package/src/lab/reuse.ts +134 -0
- package/src/lab/run-command.ts +958 -0
- package/src/lab/run-logs.ts +89 -0
- package/src/lab/run-report.ts +286 -0
- package/src/lab/runs-command.ts +171 -0
- package/src/lab/runs.ts +172 -0
- package/src/lab/scaffold.ts +94 -0
- package/src/lab/score-command.ts +197 -0
- package/src/lab/score.ts +396 -0
- package/src/lab/store.ts +383 -0
- package/src/lab/target-stub.txt +18 -0
- package/src/lab/text.ts +29 -0
- package/src/lab/trial-settings.ts +105 -0
- package/src/lab/trial.ts +439 -0
- package/src/lab/usage.ts +274 -0
- package/src/lab/verdict.ts +161 -0
- package/src/lab/workflow-graders.ts +164 -0
- package/src/lab/workspace.ts +496 -0
- package/src/loop/propose.workflow.ts +84 -0
- package/src/shared/allowance.ts +185 -0
- package/src/shared/compare/index.ts +3 -0
- package/src/shared/compare/paired.ts +397 -0
- package/src/shared/compare/stats.ts +139 -0
- package/src/shared/compare/types.ts +75 -0
- package/src/shared/config.ts +15 -0
- package/src/shared/runner.ts +321 -0
- package/src/shared/validate.ts +181 -0
- package/src/shared/version.ts +41 -0
- package/src/write-schemas.ts +10 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { join, relative } from "node:path";
|
|
3
|
+
import { LABEL_KIND, type Mark as LabelMark } from "../format/records";
|
|
4
|
+
import {
|
|
5
|
+
CAPTURE_FILE,
|
|
6
|
+
capturedKey,
|
|
7
|
+
checkCaptured,
|
|
8
|
+
findRealRun,
|
|
9
|
+
findTrial,
|
|
10
|
+
freeName,
|
|
11
|
+
type Mark,
|
|
12
|
+
readRealRun,
|
|
13
|
+
writeCase,
|
|
14
|
+
} from "./capture";
|
|
15
|
+
import { runHelper } from "./code-graders";
|
|
16
|
+
import {
|
|
17
|
+
type Context,
|
|
18
|
+
envOf,
|
|
19
|
+
loadDataset,
|
|
20
|
+
refuseLabRun,
|
|
21
|
+
tmpOf,
|
|
22
|
+
withLock,
|
|
23
|
+
workspaceOf,
|
|
24
|
+
} from "./command-context";
|
|
25
|
+
import { type DatasetInfo, folderDigest, GOOD_RECORD, INPUT_DIR, readCases } from "./dataset";
|
|
26
|
+
import { buildCase } from "./folders";
|
|
27
|
+
import { unpinnable, writeGood } from "./good";
|
|
28
|
+
import { appendLabel, readLabels } from "./labels";
|
|
29
|
+
import { factsOf } from "./outcome";
|
|
30
|
+
import { writeLabels } from "./record-copies";
|
|
31
|
+
import { Refusal } from "./refusal";
|
|
32
|
+
import type { StoredTrial } from "./store";
|
|
33
|
+
import { afterMsOf } from "./trial-settings";
|
|
34
|
+
import { type CaseWorkspace, recordsOf } from "./workspace";
|
|
35
|
+
|
|
36
|
+
// `case capture`, `case pin` and `label`: your mark on a trial, a case made from a trial
|
|
37
|
+
// or a real run, and a trial's record kept as its case's known good record.
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* `case capture {run id}`: a case from a past run of the workflow. A trial's run has its case: its
|
|
41
|
+
* own `input/` is copied, and `--good` drafts `expect` from the trial's facts and keeps its record
|
|
42
|
+
* as the new case's known good record. A real run, from the project's `.awf/runs`, becomes cases
|
|
43
|
+
* through the dataset's `capture.ts`. Every key is a draft, so no grader runs on it until the
|
|
44
|
+
* person finishes it.
|
|
45
|
+
*/
|
|
46
|
+
export async function captureCommand(context: Context, names: readonly string[]): Promise<number> {
|
|
47
|
+
const { options } = context;
|
|
48
|
+
const [given, ...extra] = names;
|
|
49
|
+
if (!given) {
|
|
50
|
+
throw new Refusal(
|
|
51
|
+
"case capture needs a trial's id, which report --case {case} lists, or a real run's awf run id",
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
if (extra.length > 0) {
|
|
55
|
+
throw new Refusal(`case capture takes one id; got ${names.length}: ${names.join(" ")}`);
|
|
56
|
+
}
|
|
57
|
+
if (options.good === (options.needsWork !== undefined)) {
|
|
58
|
+
throw new Refusal(
|
|
59
|
+
options.good
|
|
60
|
+
? 'case capture takes --good or --needs-work "{note}", not both'
|
|
61
|
+
: 'case capture needs --good or --needs-work "{note}": how the run went',
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const mark: Mark = options.good
|
|
65
|
+
? { mark: "good" }
|
|
66
|
+
: { mark: "needs-work", note: options.needsWork! };
|
|
67
|
+
const workspace = workspaceOf(context);
|
|
68
|
+
refuseLabRun(workspace, given, "case capture");
|
|
69
|
+
const { info } = loadDataset(context, workspace, options.dataset);
|
|
70
|
+
return withLock(context, workspace, () => captureLocked(context, workspace, info, given, mark));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function captureLocked(
|
|
74
|
+
context: Context,
|
|
75
|
+
workspace: CaseWorkspace,
|
|
76
|
+
info: DatasetInfo,
|
|
77
|
+
given: string,
|
|
78
|
+
mark: Mark,
|
|
79
|
+
): Promise<number> {
|
|
80
|
+
const say = (dir: string, from: string) =>
|
|
81
|
+
context.stdout(
|
|
82
|
+
`wrote ${relative(context.cwd, dir) || dir}, ${from}: its key is a draft to finish`,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const trial = findTrial(workspace.store, given);
|
|
86
|
+
if (trial) {
|
|
87
|
+
if (trial.record.dataset !== info.name) {
|
|
88
|
+
throw new Refusal(
|
|
89
|
+
`trial ${given} is of dataset ${trial.record.dataset}: --dataset ${trial.record.dataset}`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const source = join(info.dataDir, "cases", trial.record.case);
|
|
93
|
+
if (!existsSync(source))
|
|
94
|
+
throw new Refusal(`case ${trial.record.case} is no longer in ${info.name}`);
|
|
95
|
+
const why = mark.mark === "good" ? unpinnable(trial) : undefined;
|
|
96
|
+
if (why) {
|
|
97
|
+
throw new Refusal(
|
|
98
|
+
`trial ${given} can't be a known good record: ${why}; --needs-work "{note}" captures it`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
const dir = writeCase(info, freeName(info, trial.record.case), {
|
|
102
|
+
filesFrom: join(source, INPUT_DIR),
|
|
103
|
+
key: capturedKey(given, mark, factsOf(trial.scores)),
|
|
104
|
+
});
|
|
105
|
+
if (mark.mark === "good") writeGood(dir, trial);
|
|
106
|
+
say(
|
|
107
|
+
dir,
|
|
108
|
+
`from trial ${trial.record.trial} of case ${trial.record.case} in run ${trial.record.labRun}${mark.mark === "good" ? ", its record the known good record" : ""}`,
|
|
109
|
+
);
|
|
110
|
+
// The case says what a good result is; the trial it came from keeps the person's mark.
|
|
111
|
+
labelTrial(
|
|
112
|
+
workspace,
|
|
113
|
+
trial,
|
|
114
|
+
mark.mark,
|
|
115
|
+
mark.mark === "needs-work" ? mark.note : undefined,
|
|
116
|
+
context.environment.now ?? (() => new Date()),
|
|
117
|
+
);
|
|
118
|
+
if (folderDigestOfCase(info, trial.record.case, tmpOf(context)) !== trial.record.input) {
|
|
119
|
+
context.stderr(
|
|
120
|
+
`note: case ${trial.record.case} changed since that trial ran; its files were copied as they are now`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return 0;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const found = findRealRun(
|
|
127
|
+
[workspace.root, ...(workspace.repository ? [workspace.repository] : [])],
|
|
128
|
+
given,
|
|
129
|
+
);
|
|
130
|
+
if (!found) {
|
|
131
|
+
throw new Refusal(
|
|
132
|
+
`no trial or run ${given}: neither a trial in ${workspace.store} nor a run in ${join(workspace.root, ".awf/runs")}`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
const file = join(info.evalDir, CAPTURE_FILE);
|
|
136
|
+
if (!existsSync(file)) {
|
|
137
|
+
throw new Refusal(
|
|
138
|
+
`run ${given} is a real run: ${relative(context.cwd, file) || file} turns one into cases, and ${info.name} has none`,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
const work = mkdtempSync(join(tmpOf(context), "awf-lab-capture-"));
|
|
142
|
+
let answer: { cases?: unknown; error?: string };
|
|
143
|
+
try {
|
|
144
|
+
answer = (await runHelper(
|
|
145
|
+
{ mode: "capture", file, input: { run: readRealRun(found), root: workspace.root } },
|
|
146
|
+
{
|
|
147
|
+
root: workspace.root,
|
|
148
|
+
work,
|
|
149
|
+
env: envOf(context),
|
|
150
|
+
timeoutMs: afterMsOf(info),
|
|
151
|
+
name: "capture",
|
|
152
|
+
},
|
|
153
|
+
)) as typeof answer;
|
|
154
|
+
} finally {
|
|
155
|
+
rmSync(work, { recursive: true, force: true });
|
|
156
|
+
}
|
|
157
|
+
if (answer.error !== undefined) throw new Refusal(`${file}: ${answer.error}`);
|
|
158
|
+
const run = readRealRun(found);
|
|
159
|
+
if (mark.mark === "good" && run.outcome !== "completed") {
|
|
160
|
+
throw new Refusal(
|
|
161
|
+
`run ${given} of ${run.workflow} ${run.outcome === "running" ? "has not ended" : `ended ${run.outcome}`}: --good takes a completed run; --needs-work "{note}" captures it`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
const taken = new Set<string>();
|
|
165
|
+
for (const each of checkCaptured(file, answer.cases)) {
|
|
166
|
+
const name = freeName(info, each.name, taken);
|
|
167
|
+
taken.add(name);
|
|
168
|
+
const dir = writeCase(info, name, {
|
|
169
|
+
...(each.files ? { files: each.files } : {}),
|
|
170
|
+
...(each.input !== undefined ? { input: each.input } : {}),
|
|
171
|
+
key: capturedKey(given, mark, each.facts),
|
|
172
|
+
});
|
|
173
|
+
say(dir, `from ${run.workflow}'s run ${given}`);
|
|
174
|
+
}
|
|
175
|
+
return 0;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** A case as a trial would be built from it now, for its digest. */
|
|
179
|
+
function folderDigestOfCase(info: DatasetInfo, name: string, under: string): string | undefined {
|
|
180
|
+
const tmp = mkdtempSync(join(under, "awf-lab-capture-case-"));
|
|
181
|
+
try {
|
|
182
|
+
const found = readCases(info).find((c) => c.name === name);
|
|
183
|
+
if (!found) return undefined;
|
|
184
|
+
buildCase(info.dataDir, found, tmp);
|
|
185
|
+
return folderDigest(tmp);
|
|
186
|
+
} finally {
|
|
187
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* `label`: your mark on one trial, found by its trial id, as `capture` finds
|
|
193
|
+
* it. A coding agent the person told what was wrong runs it too, and a web interface would.
|
|
194
|
+
*/
|
|
195
|
+
export function label(context: Context, names: readonly string[]): number {
|
|
196
|
+
const { options } = context;
|
|
197
|
+
const given = [options.good, options.needsWork !== undefined, options.clear].filter(Boolean);
|
|
198
|
+
if (names.length === 0) {
|
|
199
|
+
throw new Refusal("label needs a trial's id, which report --case {case} lists");
|
|
200
|
+
}
|
|
201
|
+
if (names.length > 1) {
|
|
202
|
+
throw new Refusal(`label takes one trial's id; got ${names.length}: ${names.join(" ")}`);
|
|
203
|
+
}
|
|
204
|
+
if (given.length !== 1) {
|
|
205
|
+
throw new Refusal(
|
|
206
|
+
`label ${given.length === 0 ? "needs" : "takes one of"} --good, --needs-work "{note}" or --clear`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
const workspace = workspaceOf(context);
|
|
210
|
+
const trialId = names[0]!;
|
|
211
|
+
refuseLabRun(workspace, trialId, "label");
|
|
212
|
+
const trial = findTrial(workspace.store, trialId);
|
|
213
|
+
if (!trial) {
|
|
214
|
+
throw new Refusal(
|
|
215
|
+
`no trial ${trialId} in ${workspace.store}; awf-lab report --case {case} lists each trial's id`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
const mark = options.good ? "good" : options.clear ? "cleared" : "needs-work";
|
|
219
|
+
if (mark === "cleared" && !readLabels(workspace.store, trial.record.dataset).has(trialId)) {
|
|
220
|
+
context.stdout(`${trialId} had no label`);
|
|
221
|
+
return 0;
|
|
222
|
+
}
|
|
223
|
+
labelTrial(
|
|
224
|
+
workspace,
|
|
225
|
+
trial,
|
|
226
|
+
mark,
|
|
227
|
+
options.needsWork,
|
|
228
|
+
context.environment.now ?? (() => new Date()),
|
|
229
|
+
);
|
|
230
|
+
const { record } = trial;
|
|
231
|
+
context.stdout(
|
|
232
|
+
mark === "cleared"
|
|
233
|
+
? `cleared the label of ${record.case} trial ${record.trial}`
|
|
234
|
+
: `labeled ${record.case} trial ${record.trial} ${mark}${options.needsWork ? `: ${options.needsWork}` : ""}`,
|
|
235
|
+
);
|
|
236
|
+
return 0;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* `case pin {trial id}`: the trial's record as the known good record of the case it ran, with the
|
|
241
|
+
* files its `evidence` saved, and the trial labeled good. A trial that failed is taken: the person
|
|
242
|
+
* vouches for it, and `case check` then fails on the key. One that errored, leaked or threw is not,
|
|
243
|
+
* nor is a known good record replaced without `--replace`.
|
|
244
|
+
*/
|
|
245
|
+
export async function casePin(context: Context, names: readonly string[]): Promise<number> {
|
|
246
|
+
const [given, ...extra] = names;
|
|
247
|
+
if (!given) throw new Refusal("case pin needs a trial's id, which report --case {case} lists");
|
|
248
|
+
if (extra.length > 0) {
|
|
249
|
+
throw new Refusal(`case pin takes one trial's id; got ${names.length}: ${names.join(" ")}`);
|
|
250
|
+
}
|
|
251
|
+
const workspace = workspaceOf(context);
|
|
252
|
+
refuseLabRun(workspace, given, "case pin");
|
|
253
|
+
return withLock(context, workspace, async () => {
|
|
254
|
+
const trial = findTrial(workspace.store, given);
|
|
255
|
+
if (!trial) {
|
|
256
|
+
throw new Refusal(
|
|
257
|
+
`no trial ${given} in ${workspace.store}; awf-lab report --case {case} lists each trial's id`,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
const why = unpinnable(trial);
|
|
261
|
+
if (why) throw new Refusal(`trial ${given} can't be a known good record: ${why}`);
|
|
262
|
+
const { record } = trial;
|
|
263
|
+
const { info, cases } = loadDataset(context, workspace, record.dataset, "all");
|
|
264
|
+
const found = cases.find((c) => c.name === record.case);
|
|
265
|
+
if (!found) throw new Refusal(`case ${record.case} is no longer in ${info.name}`);
|
|
266
|
+
if (found.good && !context.options.replace) {
|
|
267
|
+
throw new Refusal(
|
|
268
|
+
`case ${record.case} has a known good record, ${join(found.good.dir, GOOD_RECORD)}: --replace replaces it`,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
const file = writeGood(found.dir, trial);
|
|
272
|
+
labelTrial(workspace, trial, "good", undefined, context.environment.now ?? (() => new Date()));
|
|
273
|
+
context.stdout(
|
|
274
|
+
`wrote ${relative(context.cwd, file) || file}, trial ${record.trial} of case ${record.case} in run ${record.labRun}, and labeled the trial good`,
|
|
275
|
+
);
|
|
276
|
+
if (folderDigestOfCase(info, record.case, tmpOf(context)) !== record.input) {
|
|
277
|
+
context.stderr(
|
|
278
|
+
`note: case ${record.case} changed since that trial ran: case check grades its record against the key as it is now`,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
return 0;
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function labelTrial(
|
|
286
|
+
workspace: CaseWorkspace,
|
|
287
|
+
trial: StoredTrial,
|
|
288
|
+
mark: LabelMark,
|
|
289
|
+
note: string | undefined,
|
|
290
|
+
now: () => Date,
|
|
291
|
+
): void {
|
|
292
|
+
appendLabel(workspace.store, trial.record.dataset, {
|
|
293
|
+
kind: LABEL_KIND,
|
|
294
|
+
trialId: trial.record.trialId!,
|
|
295
|
+
case: trial.record.case,
|
|
296
|
+
mark,
|
|
297
|
+
...(note ? { note } : {}),
|
|
298
|
+
at: now().toISOString(),
|
|
299
|
+
});
|
|
300
|
+
const records = recordsOf(workspace, trial.record.dataset);
|
|
301
|
+
if (records) writeLabels(workspace.store, records);
|
|
302
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cpSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
mkdtempSync,
|
|
6
|
+
readdirSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
renameSync,
|
|
9
|
+
rmSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from "node:fs";
|
|
12
|
+
import { dirname, isAbsolute, join, normalize } from "node:path";
|
|
13
|
+
import Type from "typebox";
|
|
14
|
+
import { FactSchema } from "../format/settings";
|
|
15
|
+
import { checkSchema, describeProblems } from "../shared/validate";
|
|
16
|
+
import { type DatasetInfo, INPUT_DIR, KEY_FILE } from "./dataset";
|
|
17
|
+
import { newMarker } from "./marker";
|
|
18
|
+
import { Refusal } from "./refusal";
|
|
19
|
+
import { readableRuns, type StoredTrial } from "./store";
|
|
20
|
+
|
|
21
|
+
export const CAPTURE_FILE = "capture.ts";
|
|
22
|
+
|
|
23
|
+
const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
24
|
+
|
|
25
|
+
/** A string fact longer than this is prose, not a value a key can expect exactly. */
|
|
26
|
+
const LONG_TEXT = 60;
|
|
27
|
+
|
|
28
|
+
export type Mark = { mark: "good" } | { mark: "needs-work"; note: string };
|
|
29
|
+
|
|
30
|
+
/** What a dataset's `capture.ts` answers: one case per thing the run handled. */
|
|
31
|
+
const CasesSchema = Type.Array(
|
|
32
|
+
Type.Object(
|
|
33
|
+
{
|
|
34
|
+
name: Type.String({ pattern: NAME.source }),
|
|
35
|
+
input: Type.Optional(Type.Unknown()),
|
|
36
|
+
files: Type.Optional(Type.Record(Type.String({ minLength: 1 }), Type.String())),
|
|
37
|
+
facts: Type.Optional(Type.Record(Type.String(), FactSchema)),
|
|
38
|
+
},
|
|
39
|
+
{ additionalProperties: false },
|
|
40
|
+
),
|
|
41
|
+
{ minItems: 1 },
|
|
42
|
+
);
|
|
43
|
+
export type CapturedCase = Type.Static<typeof CasesSchema>[number];
|
|
44
|
+
|
|
45
|
+
/** The trial whose workflow run had this id, in any run of the store. */
|
|
46
|
+
export function findTrial(store: string, trialId: string): StoredTrial | undefined {
|
|
47
|
+
for (const { trials } of readableRuns(store, { newest: true })) {
|
|
48
|
+
const found = trials.find((t) => t.record.trialId === trialId);
|
|
49
|
+
if (found) return found;
|
|
50
|
+
}
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A real run's folder, `.awf/runs/{workflow}/{id}`, under these project folders. A run id is unique
|
|
56
|
+
* only within its workflow: one found under two workflows is refused, naming both.
|
|
57
|
+
*/
|
|
58
|
+
export function findRealRun(projects: readonly string[], runId: string): string | undefined {
|
|
59
|
+
const found = new Set<string>();
|
|
60
|
+
for (const project of new Set(projects)) {
|
|
61
|
+
const root = join(project, ".awf", "runs");
|
|
62
|
+
if (!existsSync(root)) continue;
|
|
63
|
+
for (const workflow of readdirSync(root)) {
|
|
64
|
+
const dir = join(root, workflow, runId);
|
|
65
|
+
if (existsSync(join(dir, "run.json"))) found.add(dir);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (found.size > 1) {
|
|
69
|
+
throw new Refusal(
|
|
70
|
+
`run ${runId} is in more than one workflow's runs: ${[...found].join(", ")}; capture takes one`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return [...found][0];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A real run as `capture` is given it: its `run.json` and, once it ended, its `out.json`. */
|
|
77
|
+
export function readRealRun(dir: string): {
|
|
78
|
+
id: string;
|
|
79
|
+
workflow: string;
|
|
80
|
+
argv: string[];
|
|
81
|
+
args?: unknown;
|
|
82
|
+
outcome: string;
|
|
83
|
+
result: unknown;
|
|
84
|
+
dir: string;
|
|
85
|
+
} {
|
|
86
|
+
const run = JSON.parse(readFileSync(join(dir, "run.json"), "utf8")) as {
|
|
87
|
+
id: string;
|
|
88
|
+
workflow: string;
|
|
89
|
+
argv?: string[];
|
|
90
|
+
args?: unknown;
|
|
91
|
+
};
|
|
92
|
+
// How it ended is its last attempt's: a run being continued, or whose last attempt was lost,
|
|
93
|
+
// has not ended, whatever an earlier attempt's output says.
|
|
94
|
+
const attempts = existsSync(join(dir, "attempts"))
|
|
95
|
+
? readdirSync(join(dir, "attempts"))
|
|
96
|
+
.map((name) => /^([1-9][0-9]*)\.json$/.exec(name)?.[1])
|
|
97
|
+
.filter((n): n is string => n !== undefined)
|
|
98
|
+
.map(Number)
|
|
99
|
+
: [];
|
|
100
|
+
const last = Math.max(0, ...attempts);
|
|
101
|
+
const attempt = last
|
|
102
|
+
? (JSON.parse(readFileSync(join(dir, "attempts", `${last}.json`), "utf8")) as {
|
|
103
|
+
outcome?: string;
|
|
104
|
+
})
|
|
105
|
+
: {};
|
|
106
|
+
const outFile = ["out.json", "output.json"].map((f) => join(dir, f)).find(existsSync);
|
|
107
|
+
const out = outFile
|
|
108
|
+
? (JSON.parse(readFileSync(outFile, "utf8")) as {
|
|
109
|
+
attempt?: number;
|
|
110
|
+
outcome?: string;
|
|
111
|
+
value?: unknown;
|
|
112
|
+
})
|
|
113
|
+
: {};
|
|
114
|
+
const outcome = attempt.outcome ?? (last ? "running" : (out.outcome ?? "running"));
|
|
115
|
+
const settled = out.attempt === undefined || out.attempt === last;
|
|
116
|
+
return {
|
|
117
|
+
id: run.id,
|
|
118
|
+
workflow: run.workflow,
|
|
119
|
+
argv: run.argv ?? [],
|
|
120
|
+
...(run.args !== undefined ? { args: run.args } : {}),
|
|
121
|
+
outcome,
|
|
122
|
+
result: outcome === "completed" && settled ? (out.value ?? null) : null,
|
|
123
|
+
dir,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** `capture.ts`'s answer, checked: every name a case folder's, every file inside `input/`. */
|
|
128
|
+
export function checkCaptured(file: string, answer: unknown): CapturedCase[] {
|
|
129
|
+
const checked = checkSchema(CasesSchema, answer);
|
|
130
|
+
if (!checked.ok) throw new Refusal(describeProblems(`${file}'s cases`, checked.problems));
|
|
131
|
+
for (const each of checked.value) {
|
|
132
|
+
const paths = Object.keys(each.files ?? {}).map((p) => normalize(p));
|
|
133
|
+
for (const path of paths) {
|
|
134
|
+
if (isAbsolute(path) || path === "." || path.split(/[\\/]/).includes("..")) {
|
|
135
|
+
throw new Refusal(`${file}: case ${each.name}: ${path} is no file inside its input/`);
|
|
136
|
+
}
|
|
137
|
+
if (paths.some((other) => other.startsWith(`${path}/`))) {
|
|
138
|
+
throw new Refusal(`${file}: case ${each.name}: ${path} is a file and a folder`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return checked.value;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The first of `name`, `name-2`, `name-3`… that no case of the dataset has. */
|
|
146
|
+
export function freeName(
|
|
147
|
+
dataset: DatasetInfo,
|
|
148
|
+
name: string,
|
|
149
|
+
taken: Set<string> = new Set(),
|
|
150
|
+
): string {
|
|
151
|
+
const used = (n: string) => taken.has(n) || existsSync(join(dataset.dataDir, "cases", n));
|
|
152
|
+
if (!used(name)) return name;
|
|
153
|
+
for (let n = 2; ; n++) if (!used(`${name}-${n}`)) return `${name}-${n}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A captured case's key: a draft, so no grader runs on it until the person writes what a good
|
|
158
|
+
* result is; `captured` names the run and its mark. With `--good`, `expect` drafts the facts the
|
|
159
|
+
* run gave, `error` left out. Strings are quoted, as the key reader wants.
|
|
160
|
+
*/
|
|
161
|
+
export function capturedKey(
|
|
162
|
+
runId: string,
|
|
163
|
+
mark: Mark,
|
|
164
|
+
facts: Record<string, string | number | boolean | null> | undefined,
|
|
165
|
+
): string {
|
|
166
|
+
const expect =
|
|
167
|
+
mark.mark === "good" && facts
|
|
168
|
+
? Object.entries(facts).filter(([name]) => name !== "error" && !name.startsWith("_"))
|
|
169
|
+
: [];
|
|
170
|
+
const isProse = ([, v]: [string, unknown]) => typeof v === "string" && v.length > LONG_TEXT;
|
|
171
|
+
const plain = expect.filter((e) => !isProse(e));
|
|
172
|
+
const prose = expect.filter(isProse);
|
|
173
|
+
const lines = [
|
|
174
|
+
"---",
|
|
175
|
+
`marker: ${newMarker()}`,
|
|
176
|
+
"draft: true",
|
|
177
|
+
"captured:",
|
|
178
|
+
` run: ${JSON.stringify(runId)}`,
|
|
179
|
+
` mark: ${mark.mark}`,
|
|
180
|
+
...(mark.mark === "needs-work" ? [` note: ${JSON.stringify(mark.note)}`] : []),
|
|
181
|
+
// Prose an agent wrote won't come back word for word: left for the person, commented. With
|
|
182
|
+
// nothing else, `expect:` itself is commented, as an empty one is no key.
|
|
183
|
+
...(plain.length > 0
|
|
184
|
+
? ["expect:", ...plain.map(([n, v]) => ` ${n}: ${JSON.stringify(v)}`)]
|
|
185
|
+
: []),
|
|
186
|
+
...(prose.length > 0
|
|
187
|
+
? [
|
|
188
|
+
...(plain.length > 0 ? [] : ["# expect:"]),
|
|
189
|
+
...prose.map(
|
|
190
|
+
([n, v]) =>
|
|
191
|
+
` # ${n}: ${JSON.stringify(v)} # text: matched exactly, it would fail; a must is likelier`,
|
|
192
|
+
),
|
|
193
|
+
]
|
|
194
|
+
: []),
|
|
195
|
+
"---",
|
|
196
|
+
"",
|
|
197
|
+
mark.mark === "good"
|
|
198
|
+
? `Captured from run ${runId}, a good result: check \`expect\` says what made it good, then remove \`draft: true\`.`
|
|
199
|
+
: `Captured from run ${runId}, which needed work: ${mark.note}. Write what a good result is, then remove \`draft: true\`.`,
|
|
200
|
+
"",
|
|
201
|
+
];
|
|
202
|
+
return lines.join("\n");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Writes a case folder: its `input/`, a capture's `input.json` in it, its draft key. */
|
|
206
|
+
export function writeCase(
|
|
207
|
+
dataset: DatasetInfo,
|
|
208
|
+
name: string,
|
|
209
|
+
parts: { files?: Record<string, string>; filesFrom?: string; input?: unknown; key: string },
|
|
210
|
+
): string {
|
|
211
|
+
const dir = join(dataset.dataDir, "cases", name);
|
|
212
|
+
// Built whole beside the cases, then moved into place: a case is never there without its key.
|
|
213
|
+
const building = mkdtempSync(join(dataset.dataDir, ".capture-"));
|
|
214
|
+
const input = join(building, INPUT_DIR);
|
|
215
|
+
try {
|
|
216
|
+
mkdirSync(input);
|
|
217
|
+
if (parts.filesFrom && existsSync(parts.filesFrom)) {
|
|
218
|
+
cpSync(parts.filesFrom, input, { recursive: true });
|
|
219
|
+
}
|
|
220
|
+
for (const [path, text] of Object.entries(parts.files ?? {})) {
|
|
221
|
+
mkdirSync(dirname(join(input, path)), { recursive: true });
|
|
222
|
+
writeFileSync(join(input, path), text);
|
|
223
|
+
}
|
|
224
|
+
if (parts.input !== undefined) {
|
|
225
|
+
writeFileSync(join(input, "input.json"), `${JSON.stringify(parts.input, null, 2)}\n`);
|
|
226
|
+
}
|
|
227
|
+
writeFileSync(join(building, KEY_FILE), parts.key);
|
|
228
|
+
mkdirSync(join(dataset.dataDir, "cases"), { recursive: true });
|
|
229
|
+
renameSync(building, dir);
|
|
230
|
+
} finally {
|
|
231
|
+
rmSync(building, { recursive: true, force: true });
|
|
232
|
+
}
|
|
233
|
+
return dir;
|
|
234
|
+
}
|