@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,375 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, lstatSync, readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { join, relative } from "node:path";
|
|
4
|
+
import type { Reads } from "../env/grader";
|
|
5
|
+
import { parseKey } from "../format/key";
|
|
6
|
+
import type { GradingFile, Key, TargetFile } from "../format/settings";
|
|
7
|
+
import type { Agent } from "../shared/config";
|
|
8
|
+
import { describeProblems } from "../shared/validate";
|
|
9
|
+
import {
|
|
10
|
+
checkGradingFile,
|
|
11
|
+
checkTargetFile,
|
|
12
|
+
describeFiles,
|
|
13
|
+
GRADING_FILE,
|
|
14
|
+
type HelperPlace,
|
|
15
|
+
refuseStub,
|
|
16
|
+
TARGET_FILE,
|
|
17
|
+
} from "./eval-files";
|
|
18
|
+
import { Refusal } from "./refusal";
|
|
19
|
+
import { type DatasetRef, inside, type Traced } from "./workspace";
|
|
20
|
+
|
|
21
|
+
export const KEY_FILE = "key.md";
|
|
22
|
+
/** A case's input: what its trial is given. */
|
|
23
|
+
export const INPUT_DIR = "input";
|
|
24
|
+
/** A data folder's input every case gets, under each case's own. */
|
|
25
|
+
export const SHARED_DIR = "shared";
|
|
26
|
+
|
|
27
|
+
/** A parameter as a dataset resolves it before the command's flags; `undefined` when required. */
|
|
28
|
+
export type ParamValue = Traced<string | undefined>;
|
|
29
|
+
|
|
30
|
+
export type DatasetInfo = {
|
|
31
|
+
name: string;
|
|
32
|
+
/** The eval folder: `target.ts`, `grading.ts`, `graders/`, `capture.ts`. */
|
|
33
|
+
evalDir: string;
|
|
34
|
+
/** The data folder: `cases/` and `shared/`. */
|
|
35
|
+
dataDir: string;
|
|
36
|
+
target: TargetFile;
|
|
37
|
+
grading: GradingFile;
|
|
38
|
+
/** Every parameter `target.ts` declares: its default, under the dataset entries' values. */
|
|
39
|
+
params: Record<string, ParamValue>;
|
|
40
|
+
/** Where a copy of the dataset's records is kept. */
|
|
41
|
+
records?: string;
|
|
42
|
+
ref: DatasetRef;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** A file of a case's `key/`: what the marker search looks for, by digest and blob id. */
|
|
46
|
+
export type KeyFile = { path: string; size: number; digest: string; bytes: Uint8Array };
|
|
47
|
+
|
|
48
|
+
/** A case's known good record and the files saved beside it: what `case check` grades. */
|
|
49
|
+
export const GOOD_DIR = "good";
|
|
50
|
+
/** The known good record itself, in `key/good/`. */
|
|
51
|
+
export const GOOD_RECORD = "record.json";
|
|
52
|
+
|
|
53
|
+
export type CaseInfo = {
|
|
54
|
+
name: string;
|
|
55
|
+
dir: string;
|
|
56
|
+
key: Key;
|
|
57
|
+
note: string;
|
|
58
|
+
/** The files of `key/` but `key/good/`, which graders and the marker search read. */
|
|
59
|
+
keyFiles: KeyFile[];
|
|
60
|
+
/** `key/good/`, when the case has one: never given to a grader as `key/`, nor searched for. */
|
|
61
|
+
good?: { dir: string };
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const sha256 = (bytes: Uint8Array | string) =>
|
|
65
|
+
createHash("sha256").update(bytes).digest("hex");
|
|
66
|
+
|
|
67
|
+
function folders(dir: string): string[] {
|
|
68
|
+
if (!existsSync(dir)) return [];
|
|
69
|
+
return readdirSync(dir, { withFileTypes: true })
|
|
70
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith("."))
|
|
71
|
+
.map((entry) => entry.name)
|
|
72
|
+
.sort();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A dataset: its eval folder's `target.ts` and `grading.ts`, loaded in the helper in your checkout
|
|
77
|
+
* and checked, and its parameters resolved against the dataset's entries.
|
|
78
|
+
*/
|
|
79
|
+
export function readDataset(ref: DatasetRef, place: HelperPlace): DatasetInfo {
|
|
80
|
+
const evalDir = ref.eval.value;
|
|
81
|
+
const targetFile = join(evalDir, TARGET_FILE);
|
|
82
|
+
const gradingFile = join(evalDir, GRADING_FILE);
|
|
83
|
+
if (!existsSync(gradingFile)) {
|
|
84
|
+
throw new Refusal(
|
|
85
|
+
`${evalDir} has no ${GRADING_FILE}: export default defineGrading({}) grades by the keys alone`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
refuseStub(targetFile);
|
|
89
|
+
const described = describeFiles({ target: targetFile, grading: gradingFile }, place);
|
|
90
|
+
const target = checkTargetFile(targetFile, described.target);
|
|
91
|
+
const grading = checkGradingFile(gradingFile, described.grading);
|
|
92
|
+
const params: Record<string, ParamValue> = {};
|
|
93
|
+
for (const [name, declared] of Object.entries(target.params ?? {})) {
|
|
94
|
+
params[name] = {
|
|
95
|
+
value: typeof declared === "string" ? declared : undefined,
|
|
96
|
+
from: targetFile,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
for (const [name, value] of Object.entries(ref.params)) {
|
|
100
|
+
if (!(name in params)) {
|
|
101
|
+
throw new Refusal(
|
|
102
|
+
`${value.from}: dataset ${ref.name} gives parameter ${name}, which ${targetFile} doesn't declare${declaredList(params)}`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
params[name] = value;
|
|
106
|
+
}
|
|
107
|
+
for (const grader of grading.graders ?? []) {
|
|
108
|
+
if (grader.grader === "workflow" && !inside(evalDir, grader.file)) {
|
|
109
|
+
throw new Refusal(
|
|
110
|
+
`${gradingFile}: grader ${grader.name}'s workflow ${grader.file} is outside the eval folder`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
refuseLinks(join(ref.data.value, SHARED_DIR));
|
|
115
|
+
return {
|
|
116
|
+
name: ref.name,
|
|
117
|
+
evalDir,
|
|
118
|
+
dataDir: ref.data.value,
|
|
119
|
+
target,
|
|
120
|
+
grading,
|
|
121
|
+
params,
|
|
122
|
+
...(ref.records ? { records: ref.records.value } : {}),
|
|
123
|
+
ref,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** ` (it declares a, b)`, or that it declares none. */
|
|
128
|
+
export const declaredList = (params: Record<string, unknown>) => {
|
|
129
|
+
const names = Object.keys(params);
|
|
130
|
+
return names.length > 0 ? ` (it declares ${names.join(", ")})` : "; it declares no params";
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/** The eval folder's `grading.ts` when it says what "better" is: a `compare`. */
|
|
134
|
+
export const comparingFile = (info: DatasetInfo) =>
|
|
135
|
+
info.grading.compare ? join(info.evalDir, GRADING_FILE) : undefined;
|
|
136
|
+
|
|
137
|
+
/** A grader as `grading.ts` lists it: a code grader, a grader workflow, or a `sentences`. */
|
|
138
|
+
export type GraderSpec =
|
|
139
|
+
| { kind: "code"; name: string; version: string; reads: Reads; manual: boolean }
|
|
140
|
+
| {
|
|
141
|
+
kind: "workflow";
|
|
142
|
+
name: string;
|
|
143
|
+
version: string;
|
|
144
|
+
/** The workflow's file, inside the eval folder. */
|
|
145
|
+
file: string;
|
|
146
|
+
reads: Reads;
|
|
147
|
+
manual: boolean;
|
|
148
|
+
timeout?: string;
|
|
149
|
+
}
|
|
150
|
+
| { kind: "sentences"; name: string; version: string; agent: Agent; manual: boolean };
|
|
151
|
+
|
|
152
|
+
/** The dataset's graders, in `grading.ts`'s order, which is the order they run in. */
|
|
153
|
+
export function graderSpecs(info: DatasetInfo): GraderSpec[] {
|
|
154
|
+
return (info.grading.graders ?? []).map((grader): GraderSpec => {
|
|
155
|
+
if (grader.grader === "code") {
|
|
156
|
+
return {
|
|
157
|
+
kind: "code",
|
|
158
|
+
name: grader.name,
|
|
159
|
+
version: grader.version,
|
|
160
|
+
reads: grader.reads,
|
|
161
|
+
manual: grader.manual,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (grader.grader === "workflow") {
|
|
165
|
+
return {
|
|
166
|
+
kind: "workflow",
|
|
167
|
+
name: grader.name,
|
|
168
|
+
version: grader.version,
|
|
169
|
+
file: grader.file,
|
|
170
|
+
reads: grader.reads,
|
|
171
|
+
manual: grader.manual,
|
|
172
|
+
...(grader.timeout ? { timeout: grader.timeout } : {}),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
const at = grader.agent.indexOf("/");
|
|
176
|
+
return {
|
|
177
|
+
kind: "sentences",
|
|
178
|
+
name: grader.name,
|
|
179
|
+
version: grader.version,
|
|
180
|
+
agent: {
|
|
181
|
+
harness: grader.agent.slice(0, at),
|
|
182
|
+
model: grader.agent.slice(at + 1),
|
|
183
|
+
...(grader.voters !== undefined ? { voters: grader.voters } : {}),
|
|
184
|
+
},
|
|
185
|
+
manual: grader.manual,
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Whether `grading.ts` lists a `sentences` grader that decides: one a key with sentences needs. */
|
|
191
|
+
export const judgesSentences = (info: DatasetInfo): boolean =>
|
|
192
|
+
graderSpecs(info).some((g) => g.kind === "sentences" && !g.manual);
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A symlink among what a case is built from or its key is read from: a link copied as it is
|
|
196
|
+
* would point the workflow outside the case, perhaps at the key, and one followed would bring in
|
|
197
|
+
* whatever it names.
|
|
198
|
+
*/
|
|
199
|
+
export function refuseLinks(dir: string): void {
|
|
200
|
+
const links = linksUnder(dir);
|
|
201
|
+
if (links.length > 0) {
|
|
202
|
+
throw new Refusal(
|
|
203
|
+
`${links.join(", ")}: a symlink; a case's files and key are plain files, copied or read as they are`,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Every case of a dataset, its key read strictly. */
|
|
209
|
+
export function readCases(dataset: DatasetInfo): CaseInfo[] {
|
|
210
|
+
const root = join(dataset.dataDir, "cases");
|
|
211
|
+
return folders(root).map((name) => readCase(name, join(root, name), dataset.dataDir));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Every case of a dataset, each read or the reason it doesn't: `case check` names them all. */
|
|
215
|
+
export function readEachCase(
|
|
216
|
+
dataset: DatasetInfo,
|
|
217
|
+
): ({ name: string; info: CaseInfo } | { name: string; problem: string })[] {
|
|
218
|
+
const root = join(dataset.dataDir, "cases");
|
|
219
|
+
return folders(root).map((name) => {
|
|
220
|
+
try {
|
|
221
|
+
return { name, info: readCase(name, join(root, name), dataset.dataDir) };
|
|
222
|
+
} catch (error) {
|
|
223
|
+
if (error instanceof Refusal) return { name, problem: error.message };
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* What of the data folder's `shared/` a case is built from, each path relative to it: what its key
|
|
231
|
+
* names, or all of it. A named path `shared/` doesn't hold is refused.
|
|
232
|
+
*/
|
|
233
|
+
export function sharedOf(dataDir: string, info: Pick<CaseInfo, "name" | "key">): string[] {
|
|
234
|
+
const root = join(dataDir, SHARED_DIR);
|
|
235
|
+
const named = info.key.shared;
|
|
236
|
+
if (named === undefined) return existsSync(root) ? ["."] : [];
|
|
237
|
+
for (const path of named) {
|
|
238
|
+
if (!existsSync(join(root, path))) {
|
|
239
|
+
throw new Refusal(
|
|
240
|
+
`case ${info.name}'s ${KEY_FILE} names shared ${path}, which ${root} doesn't hold`,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return named;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function readCase(name: string, dir: string, dataDir: string): CaseInfo {
|
|
248
|
+
const keyFile = join(dir, KEY_FILE);
|
|
249
|
+
refuseLinks(join(dir, INPUT_DIR));
|
|
250
|
+
refuseLinks(join(dir, "key"));
|
|
251
|
+
if (existsSync(keyFile) && lstatSync(keyFile).isSymbolicLink()) refuseLinks(dir);
|
|
252
|
+
if (!existsSync(keyFile)) throw new Refusal(`case ${name} has no ${KEY_FILE}: ${dir}`);
|
|
253
|
+
const read = parseKey(readFileSync(keyFile, "utf8"));
|
|
254
|
+
if (!read.ok) throw new Refusal(describeProblems(keyFile, read.problems));
|
|
255
|
+
const keyFiles: KeyFile[] = [];
|
|
256
|
+
const keyDir = join(dir, "key");
|
|
257
|
+
const goodDir = join(keyDir, GOOD_DIR);
|
|
258
|
+
if (existsSync(join(keyDir, "answer"))) {
|
|
259
|
+
throw new Refusal(
|
|
260
|
+
`${join(keyDir, "answer")}: a known answer is now a known good record, key/good/record.json, in a trial record's shape; case pin {trial id} keeps one`,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
if (existsSync(keyDir)) {
|
|
264
|
+
// A right result may hold a file byte for byte the good record's: no leak, so no needle.
|
|
265
|
+
for (const path of filesUnder(keyDir).filter((f) => !f.startsWith(`${goodDir}/`))) {
|
|
266
|
+
const bytes = new Uint8Array(readFileSync(path));
|
|
267
|
+
if (bytes.byteLength === 0) {
|
|
268
|
+
throw new Refusal(
|
|
269
|
+
`${path} is empty: an empty key file matches every empty file, so the marker search can't use it`,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
keyFiles.push({ path, size: bytes.byteLength, digest: sha256(bytes), bytes });
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const good =
|
|
276
|
+
existsSync(goodDir) && statSync(goodDir).isDirectory() ? { dir: goodDir } : undefined;
|
|
277
|
+
sharedOf(dataDir, { name, key: read.value.key });
|
|
278
|
+
return {
|
|
279
|
+
name,
|
|
280
|
+
dir,
|
|
281
|
+
key: read.value.key,
|
|
282
|
+
note: read.value.note,
|
|
283
|
+
keyFiles,
|
|
284
|
+
...(good ? { good } : {}),
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
type Walk = {
|
|
289
|
+
/** Names left out, with what is under them. */
|
|
290
|
+
skip?: (name: string) => boolean;
|
|
291
|
+
/** Follows symlinks: to a file, read as one; to a folder, walked once by its real path. */
|
|
292
|
+
follow?: boolean;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/** Each entry under `dir`, as a file, a folder or a symlink not followed, depth first. */
|
|
296
|
+
function walk(
|
|
297
|
+
dir: string,
|
|
298
|
+
options: Walk,
|
|
299
|
+
/** Returns false to leave a folder unwalked. */
|
|
300
|
+
visit: (path: string, kind: "file" | "folder" | "link", name: string) => boolean | undefined,
|
|
301
|
+
): void {
|
|
302
|
+
if (!existsSync(dir) || !statSync(dir).isDirectory()) return;
|
|
303
|
+
const seen = new Set<string>();
|
|
304
|
+
const into = (at: string) => {
|
|
305
|
+
const real = realpathSync(at);
|
|
306
|
+
if (seen.has(real)) return;
|
|
307
|
+
seen.add(real);
|
|
308
|
+
for (const entry of readdirSync(at, { withFileTypes: true })) {
|
|
309
|
+
if (options.skip?.(entry.name)) continue;
|
|
310
|
+
const path = join(at, entry.name);
|
|
311
|
+
let kind: "file" | "folder" | "link" | undefined;
|
|
312
|
+
if (entry.isSymbolicLink()) {
|
|
313
|
+
if (!options.follow) kind = "link";
|
|
314
|
+
else {
|
|
315
|
+
try {
|
|
316
|
+
const target = statSync(path);
|
|
317
|
+
kind = target.isDirectory() ? "folder" : target.isFile() ? "file" : undefined;
|
|
318
|
+
} catch {
|
|
319
|
+
kind = "link";
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} else if (entry.isDirectory()) kind = "folder";
|
|
323
|
+
else if (entry.isFile()) kind = "file";
|
|
324
|
+
if (!kind) continue;
|
|
325
|
+
if (visit(path, kind, entry.name) !== false && kind === "folder") into(path);
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
into(dir);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** Every file under `dir`, in order; symlinks followed only when `follow` says. */
|
|
332
|
+
export function filesUnder(dir: string, options: Walk = {}): string[] {
|
|
333
|
+
const found: string[] = [];
|
|
334
|
+
walk(dir, options, (path, kind) => {
|
|
335
|
+
if (kind === "file") found.push(path);
|
|
336
|
+
return undefined;
|
|
337
|
+
});
|
|
338
|
+
return found.sort();
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Every symlink under `dir`. */
|
|
342
|
+
function linksUnder(dir: string): string[] {
|
|
343
|
+
const found: string[] = [];
|
|
344
|
+
walk(dir, {}, (path, kind) => {
|
|
345
|
+
if (kind === "link") found.push(path);
|
|
346
|
+
return undefined;
|
|
347
|
+
});
|
|
348
|
+
return found.sort();
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** Every `.git` under `dir`, a folder or a worktree's file, symlinks followed. */
|
|
352
|
+
export function gitEntriesUnder(dir: string): string[] {
|
|
353
|
+
const found: string[] = [];
|
|
354
|
+
walk(dir, { follow: true }, (path, _kind, name) => {
|
|
355
|
+
if (name !== ".git") return undefined;
|
|
356
|
+
found.push(path);
|
|
357
|
+
return false;
|
|
358
|
+
});
|
|
359
|
+
return found.sort();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** A folder's digest: each file's path and bytes, in order. */
|
|
363
|
+
export function folderDigest(dir: string): string {
|
|
364
|
+
const hash = createHash("sha256");
|
|
365
|
+
for (const file of filesUnder(dir)) {
|
|
366
|
+
hash.update(`${relative(dir, file)}\0`);
|
|
367
|
+
hash.update(sha256(readFileSync(file)));
|
|
368
|
+
hash.update("\n");
|
|
369
|
+
}
|
|
370
|
+
return hash.digest("hex");
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export function fileDigest(file: string): string {
|
|
374
|
+
return sha256(readFileSync(file));
|
|
375
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import type Type from "typebox";
|
|
4
|
+
import {
|
|
5
|
+
type GradingFile,
|
|
6
|
+
GradingFileSchema,
|
|
7
|
+
type LabConfig,
|
|
8
|
+
LabConfigSchema,
|
|
9
|
+
type LocalFile,
|
|
10
|
+
LocalFileSchema,
|
|
11
|
+
passProblem,
|
|
12
|
+
SETTINGS_PATTERNS,
|
|
13
|
+
type TargetFile,
|
|
14
|
+
TargetFileSchema,
|
|
15
|
+
} from "../format/settings";
|
|
16
|
+
import { checkSchema, describeProblems, explainProblems } from "../shared/validate";
|
|
17
|
+
import { Refusal } from "./refusal";
|
|
18
|
+
|
|
19
|
+
// The files a person writes for the lab: `target.ts` and `grading.ts` in an eval folder,
|
|
20
|
+
// `evals/awf-lab.config.ts`, and the one JSON file, `evals/awf-lab.local.json`. The TypeScript ones
|
|
21
|
+
// are read in the grade helper, in your checkout, which runs nothing they define; each comes back
|
|
22
|
+
// as JSON and is checked here, strictly.
|
|
23
|
+
|
|
24
|
+
export const TARGET_FILE = "target.ts";
|
|
25
|
+
export const GRADING_FILE = "grading.ts";
|
|
26
|
+
export const CONFIG_FILE = "awf-lab.config.ts";
|
|
27
|
+
export const LOCAL_FILE = "awf-lab.local.json";
|
|
28
|
+
/** The line `case new` writes into a stub `target.ts`; `run` refuses the file while it is there. */
|
|
29
|
+
export const STUB_LINE = "// awf-lab: a stub.";
|
|
30
|
+
|
|
31
|
+
/** Refuses the stub `case new` wrote, before anything loads it. */
|
|
32
|
+
export function refuseStub(file: string): void {
|
|
33
|
+
if (readFileSync(file, "utf8").includes(STUB_LINE)) {
|
|
34
|
+
throw new Refusal(
|
|
35
|
+
`${file} is the stub awf-lab case new wrote: fill in its workflow and setup, then delete its first line`,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const HELPER = join(import.meta.dir, "../env/grade-helper.ts");
|
|
41
|
+
/** How long the helper may take to load the files: they define, and should run nothing. */
|
|
42
|
+
const DESCRIBE_MS = 120_000;
|
|
43
|
+
|
|
44
|
+
/** Where the helper runs: your checkout, its environment, and a folder for its request. */
|
|
45
|
+
export type HelperPlace = { root: string; env: Record<string, string>; tmp: string };
|
|
46
|
+
|
|
47
|
+
type Described = { target?: unknown; grading?: unknown; lab?: unknown };
|
|
48
|
+
|
|
49
|
+
/** Loads the TypeScript files in the helper, as JSON; refused naming the first that doesn't load. */
|
|
50
|
+
export function describeFiles(
|
|
51
|
+
files: { target?: string; grading?: string; lab?: string },
|
|
52
|
+
place: HelperPlace,
|
|
53
|
+
): Described {
|
|
54
|
+
const work = mkdtempSync(join(place.tmp, "awf-lab-describe-"));
|
|
55
|
+
try {
|
|
56
|
+
const request = join(work, "request.json");
|
|
57
|
+
const out = join(work, "out.json");
|
|
58
|
+
writeFileSync(request, JSON.stringify({ mode: "describe", files, out }));
|
|
59
|
+
const child = Bun.spawnSync([process.execPath, HELPER, request], {
|
|
60
|
+
cwd: place.root,
|
|
61
|
+
stdin: "ignore",
|
|
62
|
+
stdout: "ignore",
|
|
63
|
+
stderr: "pipe",
|
|
64
|
+
env: place.env,
|
|
65
|
+
timeout: DESCRIBE_MS,
|
|
66
|
+
});
|
|
67
|
+
if (!existsSync(out)) {
|
|
68
|
+
const said = child.stderr.toString().trim().split("\n").at(-1);
|
|
69
|
+
const named = Object.values(files).join(", ");
|
|
70
|
+
throw new Refusal(
|
|
71
|
+
child.signalCode
|
|
72
|
+
? `${named} took longer than ${DESCRIBE_MS / 1000}s to load: a file the lab reads should define, not run`
|
|
73
|
+
: `${named} did not load${said ? `: ${said}` : ""}`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
const answer = JSON.parse(readFileSync(out, "utf8")) as Described & { error?: string };
|
|
77
|
+
if (answer.error !== undefined) throw new Refusal(answer.error);
|
|
78
|
+
return answer;
|
|
79
|
+
} finally {
|
|
80
|
+
rmSync(work, { recursive: true, force: true });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function checked<S extends Type.TSchema>(
|
|
85
|
+
schema: S,
|
|
86
|
+
file: string,
|
|
87
|
+
noun: string,
|
|
88
|
+
raw: unknown,
|
|
89
|
+
): Type.Static<S> {
|
|
90
|
+
const result = checkSchema(schema, raw);
|
|
91
|
+
if (!result.ok) {
|
|
92
|
+
const problems = explainProblems(result.problems, {
|
|
93
|
+
schema,
|
|
94
|
+
value: raw,
|
|
95
|
+
noun,
|
|
96
|
+
patterns: SETTINGS_PATTERNS,
|
|
97
|
+
});
|
|
98
|
+
throw new Refusal(describeProblems(file, problems));
|
|
99
|
+
}
|
|
100
|
+
return result.value;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The fields `target.ts` no longer takes, each with what replaced it. */
|
|
104
|
+
const GONE_TARGET_FIELDS: Readonly<Record<string, string>> = {
|
|
105
|
+
agentReach:
|
|
106
|
+
"it is sandbox.network: its network is agents, and webSearch and sockets (now a list of socket paths) moved with it",
|
|
107
|
+
timeout: "it is limits.workflow",
|
|
108
|
+
setupTimeout: "it is limits.setup for setup and limits.after for evidence and cleanup",
|
|
109
|
+
realHosts: "it is sandbox.network.code",
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/** What an old `sandbox` string or `{ lab }` is now. */
|
|
113
|
+
function goneSandbox(sandbox: unknown): string | undefined {
|
|
114
|
+
if (sandbox === "workflow") return 'sandbox "workflow" is sandbox: { by: "workflow" }';
|
|
115
|
+
if (typeof sandbox === "string")
|
|
116
|
+
return `sandbox ${JSON.stringify(sandbox)}: sandbox is an object, { by: "lab" } or { by: "workflow" }`;
|
|
117
|
+
if (sandbox && typeof sandbox === "object" && "lab" in sandbox) {
|
|
118
|
+
return 'sandbox { lab: "srt" } is sandbox: { by: "lab", provider: "srt" }, the default, so it may be left out';
|
|
119
|
+
}
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function checkTargetFile(file: string, raw: unknown): TargetFile {
|
|
124
|
+
if (raw && typeof raw === "object") {
|
|
125
|
+
const gone = Object.keys(raw).find((field) => field in GONE_TARGET_FIELDS);
|
|
126
|
+
if (gone)
|
|
127
|
+
throw new Refusal(`${file}: defineTarget takes no ${gone}: ${GONE_TARGET_FIELDS[gone]}`);
|
|
128
|
+
const sandbox = goneSandbox((raw as { sandbox?: unknown }).sandbox);
|
|
129
|
+
if (sandbox) throw new Refusal(`${file}: ${sandbox}`);
|
|
130
|
+
}
|
|
131
|
+
return checked(TargetFileSchema, file, "defineTarget", raw);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function checkGradingFile(file: string, raw: unknown): GradingFile {
|
|
135
|
+
const grading = checked(GradingFileSchema, file, "defineGrading", raw);
|
|
136
|
+
const pass = passProblem(grading.pass);
|
|
137
|
+
if (pass) throw new Refusal(`${file}: ${pass}`);
|
|
138
|
+
const seen = new Set<string>();
|
|
139
|
+
for (const grader of grading.graders ?? []) {
|
|
140
|
+
if (seen.has(grader.name)) {
|
|
141
|
+
throw new Refusal(
|
|
142
|
+
grader.grader === "sentences" && grader.name === "sentences"
|
|
143
|
+
? `${file}: graders: a second sentences needs its own name: sentences({ name: "…", … })`
|
|
144
|
+
: `${file}: graders: ${grader.name} is named twice; give each its own name`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
seen.add(grader.name);
|
|
148
|
+
// The judge refuses an even count only once it runs, after the trials are paid for.
|
|
149
|
+
if (grader.grader === "sentences" && grader.voters !== undefined && grader.voters % 2 === 0) {
|
|
150
|
+
throw new Refusal(
|
|
151
|
+
`${file}: graders: ${grader.name}'s voters is odd, from 1 to 9, so a majority decides: got ${grader.voters}`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return grading;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function checkLabConfig(file: string, raw: unknown): LabConfig {
|
|
159
|
+
return checked(LabConfigSchema, file, "defineLab", raw);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The local file, read only while git ignores it: it holds private names and paths, and an
|
|
164
|
+
* untracked file isn't enough, since `git add -A` would commit it.
|
|
165
|
+
*/
|
|
166
|
+
export function readLocalFile(file: string): LocalFile {
|
|
167
|
+
const folder = join(file, "..");
|
|
168
|
+
const ignored = Bun.spawnSync(["git", "check-ignore", "-q", file], {
|
|
169
|
+
cwd: folder,
|
|
170
|
+
stdout: "ignore",
|
|
171
|
+
stderr: "pipe",
|
|
172
|
+
stdin: "ignore",
|
|
173
|
+
});
|
|
174
|
+
// 1 is "not ignored"; anything else is git failing, as in a worktree whose repository moved.
|
|
175
|
+
if (ignored.exitCode !== 0 && ignored.exitCode !== 1) {
|
|
176
|
+
const said = ignored.stderr.toString().trim();
|
|
177
|
+
const moved = /not a git repository|does not point to a valid repository/.test(said)
|
|
178
|
+
? "; a worktree whose repository moved: git worktree repair"
|
|
179
|
+
: "";
|
|
180
|
+
throw new Refusal(
|
|
181
|
+
`git check-ignore fails in ${folder} (exit ${ignored.exitCode})${said ? `: ${said}` : ""}; the lab asks git whether ${LOCAL_FILE} is ignored${moved}`,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
if (ignored.exitCode !== 0) {
|
|
185
|
+
throw new Refusal(
|
|
186
|
+
`${file} is not ignored by git, and the lab reads it only then: add ${LOCAL_FILE} to evals/.gitignore`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
let raw: unknown;
|
|
190
|
+
try {
|
|
191
|
+
raw = JSON.parse(readFileSync(file, "utf8"));
|
|
192
|
+
} catch (error) {
|
|
193
|
+
throw new Refusal(`${file}: ${(error as Error).message}`);
|
|
194
|
+
}
|
|
195
|
+
return checked(LocalFileSchema, file, LOCAL_FILE, raw);
|
|
196
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Bound, Expected, Fact } from "../format/settings";
|
|
2
|
+
|
|
3
|
+
export type ExpectResult =
|
|
4
|
+
| { verdict: "passed" | "failed"; reasons: { about: string; text: string }[] }
|
|
5
|
+
| { errored: "facts-missing" | "fact-type"; reason: string };
|
|
6
|
+
|
|
7
|
+
const show = (value: Fact) => JSON.stringify(value);
|
|
8
|
+
|
|
9
|
+
const isBound = (want: Expected): want is Bound => want !== null && typeof want === "object";
|
|
10
|
+
|
|
11
|
+
/** A bound in words: `at most 3`, `at least 1`, `between 1 and 3`. */
|
|
12
|
+
function said({ min, max }: Bound): string {
|
|
13
|
+
if (min !== undefined && max !== undefined) return `between ${min} and ${max}`;
|
|
14
|
+
return min !== undefined ? `at least ${min}` : `at most ${max}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const within = (got: number, { min, max }: Bound) =>
|
|
18
|
+
(min === undefined || got >= min) && (max === undefined || got <= max);
|
|
19
|
+
|
|
20
|
+
/** Why a run fact a key names is missing from a trial kept before the lab read them. */
|
|
21
|
+
export const BEFORE_RUN_FACTS = "kept before the lab read run facts; run it again";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The built-in `expect` grader: every fact the key names equals the key's value exactly, or is a
|
|
25
|
+
* number within the bound the key gives, `{ min, max }`. A name starting `_` is a run fact, read
|
|
26
|
+
* from `runFacts` alone; `runFacts` undefined is a trial kept before the lab read them. A fact the
|
|
27
|
+
* key names that wasn't given, or values of two types neither `null`, is a wiring mistake and the
|
|
28
|
+
* dataset's; `null` against a value or a bound, either way, is the workflow's fail.
|
|
29
|
+
*/
|
|
30
|
+
export function gradeExpect(
|
|
31
|
+
expected: Readonly<Record<string, Expected>>,
|
|
32
|
+
facts: Readonly<Record<string, Fact>>,
|
|
33
|
+
runFacts?: Readonly<Record<string, number>>,
|
|
34
|
+
): ExpectResult {
|
|
35
|
+
const factOf = (name: string): Fact | undefined => {
|
|
36
|
+
const from = name.startsWith("_") ? (runFacts ?? {}) : facts;
|
|
37
|
+
return Object.hasOwn(from, name) ? from[name] : undefined;
|
|
38
|
+
};
|
|
39
|
+
const missing = Object.keys(expected).filter((name) => factOf(name) === undefined);
|
|
40
|
+
if (missing.length > 0) {
|
|
41
|
+
const run = missing.filter((name) => name.startsWith("_"));
|
|
42
|
+
if (run.length > 0 && !runFacts) {
|
|
43
|
+
return { errored: "facts-missing", reason: `${run.join(", ")}: ${BEFORE_RUN_FACTS}` };
|
|
44
|
+
}
|
|
45
|
+
const observed = missing.filter((name) => !name.startsWith("_"));
|
|
46
|
+
const parts = [
|
|
47
|
+
...(observed.length > 0 ? [`facts returned no ${observed.join(", ")}`] : []),
|
|
48
|
+
...(run.length > 0 ? [`the run's record gave no ${run.join(", ")}`] : []),
|
|
49
|
+
];
|
|
50
|
+
return {
|
|
51
|
+
errored: "facts-missing",
|
|
52
|
+
reason: `${parts.join("; ")}, which the key's expect names`,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const reasons: { about: string; text: string }[] = [];
|
|
56
|
+
for (const [name, want] of Object.entries(expected)) {
|
|
57
|
+
const got = factOf(name) as Fact;
|
|
58
|
+
if (isBound(want)) {
|
|
59
|
+
if (typeof got === "number" && within(got, want)) continue;
|
|
60
|
+
if (got !== null && typeof got !== "number") {
|
|
61
|
+
return {
|
|
62
|
+
errored: "fact-type",
|
|
63
|
+
reason: `${name} is ${typeof got} ${show(got)}, the key wants a number ${said(want)}`,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
reasons.push({ about: name, text: `${name} is ${show(got)}, the key wants ${said(want)}` });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (got === want) continue;
|
|
70
|
+
if (got !== null && want !== null && typeof got !== typeof want) {
|
|
71
|
+
return {
|
|
72
|
+
errored: "fact-type",
|
|
73
|
+
reason: `${name} is ${typeof got} ${show(got)}, the key wants ${typeof want} ${show(want)}`,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
reasons.push({ about: name, text: `${name} is ${show(got)}, the key wants ${show(want)}` });
|
|
77
|
+
}
|
|
78
|
+
return { verdict: reasons.length === 0 ? "passed" : "failed", reasons };
|
|
79
|
+
}
|