@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,332 @@
|
|
|
1
|
+
import { cpSync, existsSync, readdirSync, readFileSync, rmSync } from "node:fs";
|
|
2
|
+
import { join, relative } from "node:path";
|
|
3
|
+
import type { Reads } from "../env/grader";
|
|
4
|
+
import type { Errored, GraderEntry, Previous } from "../format/records";
|
|
5
|
+
import type { Agent, Fact, Key } from "../format/settings";
|
|
6
|
+
import { versionKey } from "../shared/version";
|
|
7
|
+
import type { Answer } from "./answer";
|
|
8
|
+
import { type CaseInfo, sha256 } from "./dataset";
|
|
9
|
+
import { gradeExpect } from "./expect";
|
|
10
|
+
|
|
11
|
+
/** `expect`'s version: its minor goes up when a kept grade would change. */
|
|
12
|
+
export const EXPECT_VERSION = "1.1.0";
|
|
13
|
+
|
|
14
|
+
/** What a grader is called with, during a trial or under `score`. */
|
|
15
|
+
export type GradeCall = {
|
|
16
|
+
case: {
|
|
17
|
+
name: string;
|
|
18
|
+
/** The built case folder: the trial's, or the copy kept in its `input/`. */
|
|
19
|
+
dir: string;
|
|
20
|
+
input: unknown;
|
|
21
|
+
key: Omit<Key, "marker">;
|
|
22
|
+
/** The case's `key/` folder, when it has one. */
|
|
23
|
+
keyDir?: string;
|
|
24
|
+
};
|
|
25
|
+
facts: Record<string, Fact>;
|
|
26
|
+
stages: Record<string, unknown>;
|
|
27
|
+
result: unknown;
|
|
28
|
+
/** The trial's working folder; null under `score`. */
|
|
29
|
+
scratch: string | null;
|
|
30
|
+
/** Where the files `evidence` named were saved. */
|
|
31
|
+
kept: string;
|
|
32
|
+
/** A folder of the lab's, outside the case and scratch folders, for the grader's own files. */
|
|
33
|
+
work: string;
|
|
34
|
+
env: Record<string, string>;
|
|
35
|
+
/** The trial's parameters. */
|
|
36
|
+
params: Record<string, string>;
|
|
37
|
+
/** This grader's earlier parts, when they still count. */
|
|
38
|
+
previous?: Previous;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** What a grader gives back: its answer, and what its own run took. */
|
|
42
|
+
export type Graded = Answer &
|
|
43
|
+
Pick<GraderEntry, "version" | "runId" | "models" | "estimate" | "ms" | "agent">;
|
|
44
|
+
|
|
45
|
+
/** A grader the lab runs after `expect`, in `grading.ts`'s order: a dataset's own, or one a test adds. */
|
|
46
|
+
export type LabGrader = {
|
|
47
|
+
name: string;
|
|
48
|
+
/** Its declared version, semver: grades belong to its major, minor and pre-release tag. Absent: unknown. */
|
|
49
|
+
version?: string;
|
|
50
|
+
/** It runs agents: a grader workflow or a `sentences`, priced and stopped before by a budget. */
|
|
51
|
+
costly?: boolean;
|
|
52
|
+
/** Run only by `score --grader`; never decides a verdict. */
|
|
53
|
+
manual?: boolean;
|
|
54
|
+
/** The part of the key it reads, as a digest; absent, the whole key with `key/`. */
|
|
55
|
+
keyOf?: (info: Pick<CaseInfo, "dir" | "key" | "keyFiles">) => string;
|
|
56
|
+
/** Whether it grades a case with this key at all; absent, every case. */
|
|
57
|
+
appliesTo?: (key: Key) => boolean;
|
|
58
|
+
/** The agent it judges with, when that is a setting: a `sentences` grader's. */
|
|
59
|
+
agent?: Agent;
|
|
60
|
+
/** `"trial"`: it is given the record's values and the saved files too, beside the facts. */
|
|
61
|
+
reads?: Reads;
|
|
62
|
+
/** Its settings, as values: a grade of it stands only while they are the same. */
|
|
63
|
+
settings?: unknown;
|
|
64
|
+
/** Throws grading's error, with the grader's `version` on the error when it was read. */
|
|
65
|
+
grade(call: GradeCall): Promise<Graded>;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** JSON with its keys in order, for a digest that is the same however the value was built. */
|
|
69
|
+
export function canonical(value: unknown): string {
|
|
70
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
71
|
+
if (value && typeof value === "object") {
|
|
72
|
+
const entries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b));
|
|
73
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
|
|
74
|
+
}
|
|
75
|
+
return JSON.stringify(value) ?? "null";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The part of the key `expect` reads: its values. */
|
|
79
|
+
export const expectDigestOf = (key: Key) => sha256(canonical(key.expect ?? {}));
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The part of the key a dataset's grader reads: the whole key but its marker and tags, and each
|
|
83
|
+
* `key/` file by its digest. A marker or tag change grades nothing again; a `key/` edit does.
|
|
84
|
+
*/
|
|
85
|
+
function keyDigestOf(info: Pick<CaseInfo, "dir" | "key" | "keyFiles">): string {
|
|
86
|
+
const { marker: _, tags: __, ...key } = info.key;
|
|
87
|
+
const files = Object.fromEntries(
|
|
88
|
+
info.keyFiles.map((f) => [relative(join(info.dir, "key"), f.path), f.digest]),
|
|
89
|
+
);
|
|
90
|
+
return sha256(canonical({ key, files }));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const hasExpect = (key: Key) => Object.keys(key.expect ?? {}).length > 0;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The graders whose grades decide a case's trials now: `expect` when its key has values, and
|
|
97
|
+
* every grader of `grading.ts` not marked `manual`. A grade by any other (a grader removed, made
|
|
98
|
+
* manual, an `expect` taken out of the key) is shown, never decisive.
|
|
99
|
+
*/
|
|
100
|
+
export function decidersOf(
|
|
101
|
+
key: Key,
|
|
102
|
+
graders: readonly Pick<LabGrader, "name" | "manual" | "appliesTo">[],
|
|
103
|
+
) {
|
|
104
|
+
return new Set([
|
|
105
|
+
...(hasExpect(key) ? ["expect"] : []),
|
|
106
|
+
...graders.filter((g) => !g.manual && (g.appliesTo?.(key) ?? true)).map((g) => g.name),
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* What a grader was given beside the key, as the digest its grades are kept against: its settings
|
|
112
|
+
* and the facts, which every grader is given. The rest of the record and the saved files are the
|
|
113
|
+
* trial's, the same for as long as it is kept.
|
|
114
|
+
*/
|
|
115
|
+
export const givenFor = (grader: Pick<LabGrader, "settings">, facts: Record<string, Fact>) =>
|
|
116
|
+
sha256(canonical({ settings: grader.settings ?? null, facts }));
|
|
117
|
+
|
|
118
|
+
/** What `expect` was given: the facts and the run facts. */
|
|
119
|
+
export const expectGivenOf = (facts: Record<string, Fact>, runFacts?: Record<string, number>) =>
|
|
120
|
+
sha256(canonical({ facts, runFacts: runFacts ?? {} }));
|
|
121
|
+
|
|
122
|
+
/** The key digest a grader's grades are kept against. */
|
|
123
|
+
export const keyFor = (grader: LabGrader, info: Pick<CaseInfo, "dir" | "key" | "keyFiles">) =>
|
|
124
|
+
grader.keyOf ? grader.keyOf(info) : keyDigestOf(info);
|
|
125
|
+
|
|
126
|
+
const sameAgent = (a: Agent | undefined, b: Agent | undefined) =>
|
|
127
|
+
a?.harness === b?.harness && a?.model === b?.model && (a?.voters ?? 3) === (b?.voters ?? 3);
|
|
128
|
+
|
|
129
|
+
/** Whether these entries fail the trial: a deciding grader's `failed`. */
|
|
130
|
+
export function failsIn(entries: readonly GraderEntry[], deciders: ReadonlySet<string>): boolean {
|
|
131
|
+
return entries.some(
|
|
132
|
+
(e) =>
|
|
133
|
+
deciders.has(e.grader) && !e.skipped && !e.errored && !e.notApplies && e.verdict === "failed",
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Why a grader's newest grade no longer stands, so `score` grades it again: none yet, it errored,
|
|
139
|
+
* the part of the key it reads changed, its major, minor or pre-release tag did, or what it was
|
|
140
|
+
* given beside the key, its settings and the facts it read. Undefined: it stands. A skipped grade
|
|
141
|
+
* is the caller's to weigh, as it depends on the graders before it.
|
|
142
|
+
*/
|
|
143
|
+
export function staleBecause(
|
|
144
|
+
entry: GraderEntry | undefined,
|
|
145
|
+
key: string,
|
|
146
|
+
version: string | undefined,
|
|
147
|
+
agent?: Agent,
|
|
148
|
+
given?: string,
|
|
149
|
+
): string | undefined {
|
|
150
|
+
if (!entry) return "not graded";
|
|
151
|
+
if (entry.errored) return "errored";
|
|
152
|
+
if (entry.key !== key) return "key changed";
|
|
153
|
+
if (version !== undefined && versionKey(entry.version) !== versionKey(version)) return "version";
|
|
154
|
+
if (agent && !sameAgent(entry.agent, agent)) return "agent changed";
|
|
155
|
+
if (given !== undefined && entry.given !== given) return "facts or settings changed";
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* A grader's earlier parts, given as `previous.json`: from its newest grade when that one did not
|
|
161
|
+
* error and has the grader's major, minor and pre-release tag, so the grader judges only what the key's change
|
|
162
|
+
* touched.
|
|
163
|
+
*/
|
|
164
|
+
export function previousFrom(
|
|
165
|
+
entry: GraderEntry | undefined,
|
|
166
|
+
version: string | undefined,
|
|
167
|
+
agent?: Agent,
|
|
168
|
+
/** What the grader is given now: parts judged on other facts or settings don't count. */
|
|
169
|
+
given?: string,
|
|
170
|
+
): Previous | undefined {
|
|
171
|
+
if (!entry?.parts || entry.errored || entry.skipped) return undefined;
|
|
172
|
+
if (version !== undefined && versionKey(entry.version) !== versionKey(version)) return undefined;
|
|
173
|
+
if (agent && !sameAgent(entry.agent, agent)) return undefined;
|
|
174
|
+
if (given !== undefined && entry.given !== given) return undefined;
|
|
175
|
+
return { key: entry.key, parts: entry.parts };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The `key/` a grader is given: the case's own, or with a known good record a copy in `work`
|
|
180
|
+
* without `key/good/`, which no grader reads, so adding one grades nothing again.
|
|
181
|
+
*/
|
|
182
|
+
function gradersKeyDir(info: Pick<CaseInfo, "dir" | "good">, work: string): string | undefined {
|
|
183
|
+
const keyDir = join(info.dir, "key");
|
|
184
|
+
if (!existsSync(keyDir)) return undefined;
|
|
185
|
+
if (!info.good) return keyDir;
|
|
186
|
+
const good = info.good.dir;
|
|
187
|
+
const copy = join(work, "key");
|
|
188
|
+
rmSync(copy, { recursive: true, force: true });
|
|
189
|
+
cpSync(keyDir, copy, {
|
|
190
|
+
recursive: true,
|
|
191
|
+
filter: (from) => from !== good && !from.startsWith(`${good}/`),
|
|
192
|
+
});
|
|
193
|
+
return readdirSync(copy).length > 0 ? copy : undefined;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export type GradeTrial = {
|
|
197
|
+
info: CaseInfo;
|
|
198
|
+
facts: Record<string, Fact>;
|
|
199
|
+
/** The run facts, which only `expect` reads. */
|
|
200
|
+
runFacts?: Record<string, number>;
|
|
201
|
+
stages: Record<string, unknown>;
|
|
202
|
+
result: unknown;
|
|
203
|
+
caseDir: string;
|
|
204
|
+
scratch: string | null;
|
|
205
|
+
kept: string;
|
|
206
|
+
work: string;
|
|
207
|
+
env: Record<string, string>;
|
|
208
|
+
params: Record<string, string>;
|
|
209
|
+
/** Grade `expect` too. */
|
|
210
|
+
expect: boolean;
|
|
211
|
+
/** A known good record: `expect` leaves out the key's run facts, which only a trial's run has. */
|
|
212
|
+
good?: boolean;
|
|
213
|
+
/** The graders to run, in `grading.ts`'s order. */
|
|
214
|
+
graders: readonly LabGrader[];
|
|
215
|
+
/** Whose grades decide this case's verdict. */
|
|
216
|
+
deciders: ReadonlySet<string>;
|
|
217
|
+
/**
|
|
218
|
+
* The trial's newest grades left as they are: a grader is skipped when one of a grader listed
|
|
219
|
+
* before it, or `expect`'s, fails the trial.
|
|
220
|
+
*/
|
|
221
|
+
standing?: readonly GraderEntry[];
|
|
222
|
+
/** Every grader's name in `grading.ts`'s order, which `standing` is read in; `graders`' unless given. */
|
|
223
|
+
order?: readonly string[];
|
|
224
|
+
previousOf?: (grader: string) => Previous | undefined;
|
|
225
|
+
/**
|
|
226
|
+
* Asked before each costly grader, with the grades made so far on this trial: false stops
|
|
227
|
+
* grading there, for a budget.
|
|
228
|
+
*/
|
|
229
|
+
mayContinue?: (made: readonly GraderEntry[]) => boolean;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export type TrialGrades =
|
|
233
|
+
| { entries: GraderEntry[]; stopped?: true }
|
|
234
|
+
| { errored: Pick<Errored, "code" | "reason"> };
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* `expect`, then the graders in `grading.ts`'s order, each skipped once one before it failed the
|
|
238
|
+
* trial; a `manual` grader decides nothing, so never stops the rest. An `expect` that can't
|
|
239
|
+
* compare a fact errors the trial as the dataset's.
|
|
240
|
+
*/
|
|
241
|
+
export async function gradeTrial(trial: GradeTrial): Promise<TrialGrades> {
|
|
242
|
+
const { info } = trial;
|
|
243
|
+
const entries: GraderEntry[] = [];
|
|
244
|
+
const expected = trial.good
|
|
245
|
+
? Object.fromEntries(
|
|
246
|
+
Object.entries(info.key.expect ?? {}).filter(([name]) => !name.startsWith("_")),
|
|
247
|
+
)
|
|
248
|
+
: (info.key.expect ?? {});
|
|
249
|
+
// A good record's `expect` naming only run facts checks nothing of it: no entry, not a pass.
|
|
250
|
+
if (trial.expect && (!trial.good || Object.keys(expected).length > 0)) {
|
|
251
|
+
const started = Date.now();
|
|
252
|
+
const result = gradeExpect(expected, trial.facts, trial.runFacts);
|
|
253
|
+
if ("errored" in result) return { errored: { code: result.errored, reason: result.reason } };
|
|
254
|
+
entries.push({
|
|
255
|
+
grader: "expect",
|
|
256
|
+
version: EXPECT_VERSION,
|
|
257
|
+
key: expectDigestOf(info.key),
|
|
258
|
+
given: expectGivenOf(trial.facts, trial.runFacts),
|
|
259
|
+
verdict: result.verdict,
|
|
260
|
+
reasons: result.reasons,
|
|
261
|
+
ms: Date.now() - started,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
const { marker: _, ...bare } = info.key;
|
|
265
|
+
const inputFile = join(trial.caseDir, "input.json");
|
|
266
|
+
const keyDir = gradersKeyDir(info, trial.work);
|
|
267
|
+
const order = trial.order ?? trial.graders.map((g) => g.name);
|
|
268
|
+
/** The standing grades of `expect` and of the graders listed before this one. */
|
|
269
|
+
const standingBefore = (name: string) =>
|
|
270
|
+
(trial.standing ?? []).filter((e) => {
|
|
271
|
+
if (e.grader === "expect") return true;
|
|
272
|
+
const at = order.indexOf(e.grader);
|
|
273
|
+
return at >= 0 && at < order.indexOf(name);
|
|
274
|
+
});
|
|
275
|
+
for (const grader of trial.graders) {
|
|
276
|
+
if (grader.appliesTo && !grader.appliesTo(info.key)) continue;
|
|
277
|
+
const base = {
|
|
278
|
+
grader: grader.name,
|
|
279
|
+
key: keyFor(grader, info),
|
|
280
|
+
given: givenFor(grader, trial.facts),
|
|
281
|
+
};
|
|
282
|
+
if (failsIn([...standingBefore(grader.name), ...entries], trial.deciders)) {
|
|
283
|
+
entries.push({ ...base, reasons: [], skipped: true });
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (grader.costly && trial.mayContinue && !trial.mayContinue(entries)) {
|
|
287
|
+
return { entries, stopped: true };
|
|
288
|
+
}
|
|
289
|
+
const started = Date.now();
|
|
290
|
+
try {
|
|
291
|
+
const previous = trial.previousOf?.(grader.name);
|
|
292
|
+
const out = await grader.grade({
|
|
293
|
+
case: {
|
|
294
|
+
name: info.name,
|
|
295
|
+
dir: trial.caseDir,
|
|
296
|
+
input: existsSync(inputFile) ? JSON.parse(readFileSync(inputFile, "utf8")) : {},
|
|
297
|
+
key: bare,
|
|
298
|
+
...(keyDir ? { keyDir } : {}),
|
|
299
|
+
},
|
|
300
|
+
facts: trial.facts,
|
|
301
|
+
stages: trial.stages,
|
|
302
|
+
result: trial.result ?? null,
|
|
303
|
+
scratch: trial.scratch,
|
|
304
|
+
kept: trial.kept,
|
|
305
|
+
work: trial.work,
|
|
306
|
+
env: trial.env,
|
|
307
|
+
params: trial.params,
|
|
308
|
+
...(previous ? { previous } : {}),
|
|
309
|
+
});
|
|
310
|
+
const version = out.version ?? grader.version;
|
|
311
|
+
entries.push({
|
|
312
|
+
...base,
|
|
313
|
+
...out,
|
|
314
|
+
...(version ? { version } : {}),
|
|
315
|
+
ms: out.ms ?? Date.now() - started,
|
|
316
|
+
});
|
|
317
|
+
} catch (error) {
|
|
318
|
+
const extra = error as { version?: unknown; runId?: unknown; estimate?: unknown };
|
|
319
|
+
const version = typeof extra.version === "string" ? extra.version : grader.version;
|
|
320
|
+
entries.push({
|
|
321
|
+
...base,
|
|
322
|
+
...(version ? { version } : {}),
|
|
323
|
+
...(typeof extra.runId === "string" ? { runId: extra.runId } : {}),
|
|
324
|
+
...(typeof extra.estimate === "number" ? { estimate: extra.estimate } : {}),
|
|
325
|
+
reasons: [],
|
|
326
|
+
errored: { reason: (error as Error).message },
|
|
327
|
+
ms: Date.now() - started,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return { entries };
|
|
332
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdtempSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
realpathSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from "node:fs";
|
|
10
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
11
|
+
import { OUTPUT_RECORD_VERSION } from "@agentswf/contract/records";
|
|
12
|
+
import { type AwfBin, probeRecordVersion } from "../shared/runner";
|
|
13
|
+
import { GRADING_FILE, refuseStub, TARGET_FILE } from "./eval-files";
|
|
14
|
+
import { Refusal } from "./refusal";
|
|
15
|
+
import { LAB_VERSION } from "./reuse";
|
|
16
|
+
import { inside } from "./workspace";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The oldest `agentswf` this lab runs with: its own release, as the two are released together. An
|
|
20
|
+
* older one is refused, never adapted to: before 0.3.0 there is no `awf run --in`, the lab's box.
|
|
21
|
+
*/
|
|
22
|
+
export const MIN_AGENTSWF = `${LAB_VERSION.split(/[.-]/).slice(0, 2).join(".")}.0`;
|
|
23
|
+
|
|
24
|
+
/** This lab's own `@agentswf/lab` and its preload, which a folder must resolve to. */
|
|
25
|
+
const OWN_ENTRIES: Readonly<Record<string, string>> = {
|
|
26
|
+
"@agentswf/lab": join(import.meta.dir, "../env/index.ts"),
|
|
27
|
+
"@agentswf/lab/fetch": join(import.meta.dir, "../env/fetch.ts"),
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** What a folder of code runs its trials with, once checked. */
|
|
31
|
+
export type FolderSetup = {
|
|
32
|
+
root: string;
|
|
33
|
+
awf: Required<AwfBin>;
|
|
34
|
+
version: string;
|
|
35
|
+
/** Its `node_modules/.bin`, first on a trial's `PATH`. */
|
|
36
|
+
bin: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** The eval folder's `target.ts` is there and filled in. */
|
|
40
|
+
export function checkTargetScript(evalDir: string): string {
|
|
41
|
+
const file = join(evalDir, TARGET_FILE);
|
|
42
|
+
if (!existsSync(file)) {
|
|
43
|
+
throw new Refusal(`${file} is missing: an eval folder's target.ts sets up each case's world`);
|
|
44
|
+
}
|
|
45
|
+
refuseStub(file);
|
|
46
|
+
return file;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The first `node_modules/{path}` at or above `dir`. */
|
|
50
|
+
function upward(dir: string, path: string): string | undefined {
|
|
51
|
+
for (let at = resolve(dir); ; at = dirname(at)) {
|
|
52
|
+
const candidate = join(at, "node_modules", path);
|
|
53
|
+
if (existsSync(candidate)) return candidate;
|
|
54
|
+
if (dirname(at) === at) return undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** `a.b.c` against `x.y.z`, a prerelease below its release. */
|
|
59
|
+
function compareVersions(a: string, b: string): number {
|
|
60
|
+
const parse = (v: string) => {
|
|
61
|
+
const [core = "", pre] = v.split("-", 2);
|
|
62
|
+
return { parts: core.split(".").map((n) => Number(n) || 0), pre };
|
|
63
|
+
};
|
|
64
|
+
const x = parse(a);
|
|
65
|
+
const y = parse(b);
|
|
66
|
+
for (let i = 0; i < 3; i++) {
|
|
67
|
+
const d = (x.parts[i] ?? 0) - (y.parts[i] ?? 0);
|
|
68
|
+
if (d !== 0) return d;
|
|
69
|
+
}
|
|
70
|
+
if (x.pre === y.pre) return 0;
|
|
71
|
+
if (x.pre === undefined) return 1;
|
|
72
|
+
if (y.pre === undefined) return -1;
|
|
73
|
+
return x.pre.localeCompare(y.pre, undefined, { numeric: true });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The checks on a folder before the first trial: its own `awf`, at least the `agentswf` this lab
|
|
78
|
+
* reads; `@agentswf/lab` and its `fetch` resolving there; and `target.ts` and `grading.ts`
|
|
79
|
+
* type-checked by the folder's own `tsc`, only errors in the eval folder counting.
|
|
80
|
+
*/
|
|
81
|
+
export async function inspectFolder(root: string, targetScript: string): Promise<FolderSetup> {
|
|
82
|
+
const problems: string[] = [];
|
|
83
|
+
const manifest = upward(root, "agentswf/package.json");
|
|
84
|
+
let awf: Required<AwfBin> | undefined;
|
|
85
|
+
let version = "";
|
|
86
|
+
if (!manifest) {
|
|
87
|
+
problems.push(`no agentswf installed in ${root}: bun add agentswf there`);
|
|
88
|
+
} else {
|
|
89
|
+
const pkg = JSON.parse(readFileSync(manifest, "utf8")) as {
|
|
90
|
+
version: string;
|
|
91
|
+
bin?: string | Record<string, string>;
|
|
92
|
+
};
|
|
93
|
+
version = pkg.version;
|
|
94
|
+
const bin = typeof pkg.bin === "string" ? pkg.bin : pkg.bin?.awf;
|
|
95
|
+
if (compareVersions(version, MIN_AGENTSWF) < 0) {
|
|
96
|
+
problems.push(
|
|
97
|
+
`agentswf ${version} in ${root} is older than ${MIN_AGENTSWF}, the oldest this lab reads: upgrade agentswf there`,
|
|
98
|
+
);
|
|
99
|
+
} else if (!bin) {
|
|
100
|
+
problems.push(`${manifest} names no awf bin`);
|
|
101
|
+
} else {
|
|
102
|
+
const preload = resolveFrom(root, "@agentswf/lab/fetch");
|
|
103
|
+
awf = { bin: join(dirname(manifest), bin), dir: root, preload: preload ?? "" };
|
|
104
|
+
const probed = await probe(awf, root);
|
|
105
|
+
if (probed) problems.push(probed);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const [spec, own] of Object.entries(OWN_ENTRIES)) {
|
|
109
|
+
const found = resolveFrom(root, spec);
|
|
110
|
+
if (!found) {
|
|
111
|
+
problems.push(`${spec} does not resolve from ${root}: bun add -d @agentswf/lab there`);
|
|
112
|
+
} else if (realpathSync(found) !== realpathSync(own)) {
|
|
113
|
+
problems.push(
|
|
114
|
+
`${spec} resolves from ${root} to ${found}, not this lab's ${own}: bun add -d @agentswf/lab there`,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const tsc = upward(root, ".bin/tsc");
|
|
119
|
+
if (!tsc)
|
|
120
|
+
problems.push(`no tsc in ${root}: install typescript there, for the check of target.ts`);
|
|
121
|
+
else problems.push(...(await typeCheck(root, tsc, targetScript)));
|
|
122
|
+
if (problems.length > 0 || !awf) {
|
|
123
|
+
throw new Refusal(`${root} can't run trials:\n${problems.map((p) => ` ${p}`).join("\n")}`);
|
|
124
|
+
}
|
|
125
|
+
return { root, awf, version, bin: dirname(tsc ?? join(root, "node_modules/.bin/tsc")) };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The folder's `awf`, started once on a workflow that does nothing: the record version it prints. */
|
|
129
|
+
async function probe(awf: Required<AwfBin>, root: string): Promise<string | undefined> {
|
|
130
|
+
const dir = mkdtempSync(join(root, ".awf-lab-probe-"));
|
|
131
|
+
try {
|
|
132
|
+
const workflow = join(dir, "probe.ts");
|
|
133
|
+
writeFileSync(workflow, PROBE);
|
|
134
|
+
const printed = await probeRecordVersion(awf, workflow, dir);
|
|
135
|
+
if ("unread" in printed) return `${awf.bin} can't be read: ${printed.unread}`;
|
|
136
|
+
if (printed.version !== OUTPUT_RECORD_VERSION) {
|
|
137
|
+
return `${awf.bin} prints run records of version ${String(printed.version)}; this lab reads version ${OUTPUT_RECORD_VERSION}: upgrade agentswf there, or the lab`;
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
140
|
+
} finally {
|
|
141
|
+
rmSync(dir, { recursive: true, force: true });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The engine serves its author surface to the workflows it loads; the lab never imports it. */
|
|
146
|
+
const SURFACE = "agentswf/workflow";
|
|
147
|
+
const PROBE = [
|
|
148
|
+
`import { defineWorkflow } from ${JSON.stringify(SURFACE)};`,
|
|
149
|
+
"",
|
|
150
|
+
"export default defineWorkflow({",
|
|
151
|
+
' meta: { name: "awf-lab-probe", description: "Does nothing: awf-lab reads the record it prints." },',
|
|
152
|
+
" run: async () => null,",
|
|
153
|
+
"});",
|
|
154
|
+
"",
|
|
155
|
+
].join("\n");
|
|
156
|
+
|
|
157
|
+
function resolveFrom(root: string, spec: string): string | undefined {
|
|
158
|
+
try {
|
|
159
|
+
return Bun.resolveSync(spec, root);
|
|
160
|
+
} catch {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** `tsc --noEmit` on the eval folder's files alone, through a tsconfig beside the folder's own, extending it. */
|
|
166
|
+
async function typeCheck(root: string, tsc: string, targetScript: string): Promise<string[]> {
|
|
167
|
+
const config = join(root, `tsconfig.awf-lab-${randomBytes(4).toString("hex")}.json`);
|
|
168
|
+
const own = join(root, "tsconfig.json");
|
|
169
|
+
writeFileSync(
|
|
170
|
+
config,
|
|
171
|
+
JSON.stringify({
|
|
172
|
+
...(existsSync(own) ? { extends: "./tsconfig.json" } : {}),
|
|
173
|
+
compilerOptions: {
|
|
174
|
+
noEmit: true,
|
|
175
|
+
incremental: false,
|
|
176
|
+
composite: false,
|
|
177
|
+
...(existsSync(own)
|
|
178
|
+
? {}
|
|
179
|
+
: {
|
|
180
|
+
target: "ESNext",
|
|
181
|
+
module: "ESNext",
|
|
182
|
+
moduleResolution: "bundler",
|
|
183
|
+
strict: true,
|
|
184
|
+
skipLibCheck: true,
|
|
185
|
+
allowImportingTsExtensions: true,
|
|
186
|
+
}),
|
|
187
|
+
},
|
|
188
|
+
files: [targetScript, join(dirname(targetScript), GRADING_FILE)]
|
|
189
|
+
.filter((file) => existsSync(file))
|
|
190
|
+
.map((file) => relative(root, file)),
|
|
191
|
+
include: [],
|
|
192
|
+
}),
|
|
193
|
+
);
|
|
194
|
+
try {
|
|
195
|
+
const child = Bun.spawn([process.execPath, tsc, "-p", config, "--pretty", "false"], {
|
|
196
|
+
cwd: root,
|
|
197
|
+
stdout: "pipe",
|
|
198
|
+
stderr: "pipe",
|
|
199
|
+
stdin: "ignore",
|
|
200
|
+
timeout: 300_000,
|
|
201
|
+
});
|
|
202
|
+
const [out, err] = await Promise.all([
|
|
203
|
+
new Response(child.stdout).text(),
|
|
204
|
+
new Response(child.stderr).text(),
|
|
205
|
+
child.exited,
|
|
206
|
+
]);
|
|
207
|
+
return errorsIn(`${out}\n${err}`, root, dirname(targetScript));
|
|
208
|
+
} finally {
|
|
209
|
+
rmSync(config, { force: true });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** tsc's errors in files under `dir`, each with its continuation lines. */
|
|
214
|
+
function errorsIn(output: string, root: string, dir: string): string[] {
|
|
215
|
+
const errors: { file: string; text: string }[] = [];
|
|
216
|
+
for (const line of output.split("\n")) {
|
|
217
|
+
const match = /^(.+?)\(\d+,\d+\): error TS\d+:/.exec(line);
|
|
218
|
+
if (match) errors.push({ file: resolve(root, match[1]!), text: line });
|
|
219
|
+
else if (/^\s+\S/.test(line) && errors.length > 0) errors.at(-1)!.text += `\n${line}`;
|
|
220
|
+
}
|
|
221
|
+
return errors
|
|
222
|
+
.filter((error) => inside(dir, error.file))
|
|
223
|
+
.map(
|
|
224
|
+
(error) =>
|
|
225
|
+
`type error: ${error.text.replace(`${root}/`, "").replace(/: error (TS\d+):/, ": $1:")}`,
|
|
226
|
+
);
|
|
227
|
+
}
|