@agentswf/lab 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +15 -2
- package/package.json +46 -4
- package/schema/case-check.schema.json +64 -0
- package/schema/case-config.schema.json +187 -0
- package/schema/case-final.schema.json +48 -0
- package/schema/case-good-record.schema.json +70 -0
- package/schema/case-grade.schema.json +49 -0
- package/schema/case-key.schema.json +117 -0
- package/schema/case-label.schema.json +32 -0
- package/schema/case-loop-report.schema.json +210 -0
- package/schema/case-loop.schema.json +141 -0
- package/schema/case-plan.schema.json +117 -0
- package/schema/case-previous.schema.json +70 -0
- package/schema/case-proposal.schema.json +52 -0
- package/schema/case-report.schema.json +892 -0
- package/schema/case-run.schema.json +253 -0
- package/schema/case-runs.schema.json +125 -0
- package/schema/case-score.schema.json +319 -0
- package/schema/case-spread.schema.json +110 -0
- package/schema/case-trial.schema.json +543 -0
- package/schema/case-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 +128 -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 +182 -0
- package/src/format/key.ts +144 -0
- package/src/format/loop.ts +178 -0
- package/src/format/output.ts +481 -0
- package/src/format/records.ts +440 -0
- package/src/format/schema-files.ts +64 -0
- package/src/format/settings.ts +552 -0
- package/src/format/trial-env.ts +64 -0
- package/src/format/trial-file.ts +134 -0
- package/src/help/awf-lab.txt +29 -0
- package/src/help/case.txt +38 -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 +60 -0
- package/src/help/runs.txt +10 -0
- package/src/help/score.txt +27 -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 +249 -0
- package/src/lab/capture-command.ts +298 -0
- package/src/lab/capture.ts +234 -0
- package/src/lab/check-command.ts +187 -0
- package/src/lab/cli.ts +324 -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 +278 -0
- package/src/lab/criteria.ts +69 -0
- package/src/lab/dataset.ts +356 -0
- package/src/lab/eval-files.ts +185 -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 +153 -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 +226 -0
- package/src/lab/labels.ts +206 -0
- package/src/lab/loop-code.ts +255 -0
- package/src/lab/loop-command.ts +770 -0
- package/src/lab/loop-records.ts +144 -0
- package/src/lab/loop.ts +859 -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 +150 -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 +156 -0
- package/src/lab/report.ts +838 -0
- package/src/lab/reuse.ts +126 -0
- package/src/lab/run-command.ts +853 -0
- package/src/lab/run-logs.ts +89 -0
- package/src/lab/run-report.ts +259 -0
- package/src/lab/runs-command.ts +163 -0
- package/src/lab/runs.ts +172 -0
- package/src/lab/scaffold.ts +91 -0
- package/src/lab/score-command.ts +189 -0
- package/src/lab/score.ts +396 -0
- package/src/lab/store.ts +383 -0
- package/src/lab/text.ts +29 -0
- package/src/lab/trial-settings.ts +100 -0
- package/src/lab/trial-stub.txt +18 -0
- package/src/lab/trial.ts +443 -0
- package/src/lab/usage.ts +273 -0
- package/src/lab/verdict.ts +161 -0
- package/src/lab/workflow-graders.ts +161 -0
- package/src/lab/workspace.ts +489 -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,853 @@
|
|
|
1
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
2
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
3
|
+
import { type CasePlan, PLAN_KIND } from "../format/output";
|
|
4
|
+
import { type Column, type Params, RUN_KIND, type RunRecord } from "../format/records";
|
|
5
|
+
import type { Limits, TrialFile } from "../format/settings";
|
|
6
|
+
import { versionKey } from "../shared/version";
|
|
7
|
+
import {
|
|
8
|
+
columnLabel,
|
|
9
|
+
columnsOf,
|
|
10
|
+
combinations,
|
|
11
|
+
type ParamPlan,
|
|
12
|
+
readParams,
|
|
13
|
+
withDeclared,
|
|
14
|
+
} from "./columns";
|
|
15
|
+
import {
|
|
16
|
+
budgetExit,
|
|
17
|
+
type Context,
|
|
18
|
+
checkoutFolder,
|
|
19
|
+
confirmPlan,
|
|
20
|
+
datasetFacts,
|
|
21
|
+
datasetGraders,
|
|
22
|
+
envOf,
|
|
23
|
+
externalsOf,
|
|
24
|
+
loadDataset,
|
|
25
|
+
printPlan,
|
|
26
|
+
shownPath,
|
|
27
|
+
tagged,
|
|
28
|
+
tmpOf,
|
|
29
|
+
withLock,
|
|
30
|
+
workspaceOf,
|
|
31
|
+
} from "./command-context";
|
|
32
|
+
import { type CaseInfo, type DatasetInfo, judgesSentences } from "./dataset";
|
|
33
|
+
import { checkTrialFile, describeFiles, TRIAL_FILE } from "./eval-files";
|
|
34
|
+
import type { FactsReader } from "./facts";
|
|
35
|
+
import { clearStale } from "./folders";
|
|
36
|
+
import { gitState } from "./git";
|
|
37
|
+
import type { LabGrader } from "./grading";
|
|
38
|
+
import { checkTrialScript, type FolderSetup, inspectFolder } from "./inspect";
|
|
39
|
+
import { failedPrecondition, gradingSpend, scoresInRun, trialOutcome, trialSpend } from "./outcome";
|
|
40
|
+
import { writeRecords } from "./record-copies";
|
|
41
|
+
import { Refusal } from "./refusal";
|
|
42
|
+
import { caseDigests, reusableTrials, reuseKey, reuseTrial } from "./reuse";
|
|
43
|
+
import { runReport } from "./run-report";
|
|
44
|
+
import { barsOf, type CaseBar, criteriaCount, failedCases } from "./runs";
|
|
45
|
+
import { regradePlan, runScore, type ScoreDone, type ScorePlan } from "./score";
|
|
46
|
+
import {
|
|
47
|
+
claimRun,
|
|
48
|
+
clearStaleTrialFiles,
|
|
49
|
+
readableRuns,
|
|
50
|
+
readTrials,
|
|
51
|
+
runSpan,
|
|
52
|
+
type StoredTrial,
|
|
53
|
+
writeRun,
|
|
54
|
+
writeTrial,
|
|
55
|
+
} from "./store";
|
|
56
|
+
import { describeFolder, median, plural, seconds, usd } from "./text";
|
|
57
|
+
import { runTrial, type TrialDone, type TrialLab } from "./trial";
|
|
58
|
+
import { type ResolvedSettings, readLimits, resolveSettings } from "./trial-settings";
|
|
59
|
+
import { type CaseWorkspace, codeFolderOf, inside } from "./workspace";
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* What a loop asks of `run` beyond its options: the code folders, the reference first, in place of
|
|
63
|
+
* your checkout and `--against`; the cases in this order; each case's trials in every column before
|
|
64
|
+
* the next case; and after each case whether to stop, from the trials so far.
|
|
65
|
+
*/
|
|
66
|
+
export type RunGiven = {
|
|
67
|
+
/** Each with the loop's id of its code, which stands in for a version: no `trial.ts` is asked. */
|
|
68
|
+
folders?: readonly { dir: string; version: string }[];
|
|
69
|
+
order?: readonly string[];
|
|
70
|
+
stop?: (run: RunRecord, trials: readonly StoredTrial[]) => Promise<boolean>;
|
|
71
|
+
/** Told the run's id once it has one. */
|
|
72
|
+
started?: (id: string) => void;
|
|
73
|
+
/** Off, the report is not printed: the loop prints its own line. */
|
|
74
|
+
report?: boolean;
|
|
75
|
+
/** The loop's id, kept on the run's record. */
|
|
76
|
+
loop?: string;
|
|
77
|
+
/** Waits for a store another command holds, instead of being refused: a loop's later runs. */
|
|
78
|
+
waitForStore?: boolean;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** How a run ended: its exit, its id once it started, and why it never did. */
|
|
82
|
+
export type RunDone = { exit: number; id?: string; unstarted?: string };
|
|
83
|
+
|
|
84
|
+
/** The lab broke during a run: exit 4. */
|
|
85
|
+
class LabBroke extends Error {}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A folder of code, its `trial.ts`, the version its trials run under, the sandbox and limits its
|
|
89
|
+
* own `trial.ts` gives with `--limit` over them, and its git state.
|
|
90
|
+
*/
|
|
91
|
+
type CodeFolder = {
|
|
92
|
+
folder: FolderSetup;
|
|
93
|
+
trialScript: string;
|
|
94
|
+
version: string;
|
|
95
|
+
settings: ResolvedSettings;
|
|
96
|
+
state: ReturnType<typeof gitState>;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** The trials kept from earlier runs that stand in for new ones, and what they need graded again. */
|
|
100
|
+
type Reuse = {
|
|
101
|
+
/** Per case and column, the stored trials that are its first ones. */
|
|
102
|
+
slots: Map<string, { column: number; trials: StoredTrial[] }>;
|
|
103
|
+
count: number;
|
|
104
|
+
/** A trial's reuse key in column `n`, from its built case's digest. */
|
|
105
|
+
keyOf: (n: number, input: string) => string;
|
|
106
|
+
/** The reused trials a key or grader changed for since they were graded. */
|
|
107
|
+
regrade?: ScorePlan;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const slotOf = (name: string, column: number) => `${name}\0${column}`;
|
|
111
|
+
|
|
112
|
+
export async function run(context: Context, given: RunGiven = {}): Promise<RunDone> {
|
|
113
|
+
const { options, environment, stderr } = context;
|
|
114
|
+
const workspace = workspaceOf(context);
|
|
115
|
+
const { info, cases } = loadDataset(context, workspace, options.dataset, options.holdout);
|
|
116
|
+
if (options.holdout && cases.length === 0) {
|
|
117
|
+
throw new Refusal(`${info.name} holds out no case: a key sets holdout: true`);
|
|
118
|
+
}
|
|
119
|
+
const now = environment.now ?? (() => new Date());
|
|
120
|
+
const limits = readLimits(options.limit);
|
|
121
|
+
let checkedTrial: string | undefined;
|
|
122
|
+
const trialScriptOf = () => (checkedTrial ??= checkTrialScript(info.evalDir));
|
|
123
|
+
const folderOf = checkoutFolder(context, workspace, trialScriptOf);
|
|
124
|
+
const graders = await datasetGraders(context, workspace, info, folderOf, now);
|
|
125
|
+
const facts = datasetFacts(context, workspace, info);
|
|
126
|
+
const bars = barsOf(info, cases, graders);
|
|
127
|
+
const paramPlan = readParams(options.param, options.vary);
|
|
128
|
+
const combos = withDeclared(info, combinations(paramPlan));
|
|
129
|
+
const chosen = selectCases(context, workspace, info, cases, bars, combos[0]!, given);
|
|
130
|
+
if ("nothing" in chosen) {
|
|
131
|
+
context.stdout(chosen.nothing);
|
|
132
|
+
return { exit: chosen.exit, unstarted: chosen.nothing };
|
|
133
|
+
}
|
|
134
|
+
const { selected, trialsOf } = chosen;
|
|
135
|
+
const trialScript = trialScriptOf();
|
|
136
|
+
const criteria = criteriaCount(
|
|
137
|
+
selected,
|
|
138
|
+
selected.map((c) => c.name),
|
|
139
|
+
);
|
|
140
|
+
if (criteria > 0 && !judgesSentences(info)) {
|
|
141
|
+
throw new Refusal(
|
|
142
|
+
`keys in ${info.name} have must or must-not: list sentences({ agent: "{harness}/{model}", version: "1.0.0" }) in grading.ts's graders, one not manual`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
const codeFolders: CodeFolder[] = [];
|
|
146
|
+
if (given.folders) {
|
|
147
|
+
// A loop's copies hold your checkout's evals/ as the loop started, which no try may change.
|
|
148
|
+
const settings = resolveSettings(info.trial, limits);
|
|
149
|
+
for (const { dir, version } of given.folders) {
|
|
150
|
+
// A loop may name one folder twice: the start against itself, for its spread.
|
|
151
|
+
codeFolders.push(
|
|
152
|
+
await againstFolder(context, workspace, info, dir, [], limits, { version, settings }),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
codeFolders.push({
|
|
157
|
+
folder: await folderOf(),
|
|
158
|
+
trialScript,
|
|
159
|
+
version: info.trial.version,
|
|
160
|
+
settings: resolveSettings(info.trial, limits),
|
|
161
|
+
state: gitState(workspace.root),
|
|
162
|
+
});
|
|
163
|
+
for (const dir of options.against) {
|
|
164
|
+
codeFolders.push(await againstFolder(context, workspace, info, dir, codeFolders, limits));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const columns = columnsOf(codeFolders.length, combos);
|
|
168
|
+
const tmp = tmpOf(context);
|
|
169
|
+
const reuse = await planReuse({
|
|
170
|
+
context,
|
|
171
|
+
workspace,
|
|
172
|
+
info,
|
|
173
|
+
cases,
|
|
174
|
+
selected,
|
|
175
|
+
trialsOf,
|
|
176
|
+
columns,
|
|
177
|
+
codeFolders,
|
|
178
|
+
graders,
|
|
179
|
+
facts,
|
|
180
|
+
tmp,
|
|
181
|
+
now,
|
|
182
|
+
});
|
|
183
|
+
const plan = planText({
|
|
184
|
+
workspace,
|
|
185
|
+
info,
|
|
186
|
+
selected,
|
|
187
|
+
trialsOf,
|
|
188
|
+
codeFolders,
|
|
189
|
+
paramPlan,
|
|
190
|
+
columns,
|
|
191
|
+
reuse,
|
|
192
|
+
...(options.budget !== undefined ? { budget: options.budget } : {}),
|
|
193
|
+
});
|
|
194
|
+
if (options.dryRun) {
|
|
195
|
+
if (options.json) printPlan(context, plan.json);
|
|
196
|
+
else context.stdout(plan.text);
|
|
197
|
+
return { exit: 0, unstarted: "a dry run" };
|
|
198
|
+
}
|
|
199
|
+
const withBudget =
|
|
200
|
+
options.budget !== undefined
|
|
201
|
+
? `${plan.text}\nstops starting trials once their estimates reach ${usd(options.budget)}`
|
|
202
|
+
: plan.text;
|
|
203
|
+
if (!(await confirmPlan(context, withBudget))) {
|
|
204
|
+
stderr("declined; nothing run");
|
|
205
|
+
return { exit: 2, unstarted: "declined" };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const ran = await withLock(
|
|
209
|
+
context,
|
|
210
|
+
workspace,
|
|
211
|
+
async (): Promise<RunRecord | RunDone> => {
|
|
212
|
+
let record: RunRecord | undefined;
|
|
213
|
+
try {
|
|
214
|
+
const id = claimRun(workspace.store, now());
|
|
215
|
+
record = {
|
|
216
|
+
kind: RUN_KIND,
|
|
217
|
+
id,
|
|
218
|
+
dataset: info.name,
|
|
219
|
+
folders: codeFolders.map((c) => ({
|
|
220
|
+
path: c.folder.root,
|
|
221
|
+
...c.state,
|
|
222
|
+
version: c.version,
|
|
223
|
+
...c.settings,
|
|
224
|
+
})),
|
|
225
|
+
columns,
|
|
226
|
+
asked: selected.map((c) => ({ case: c.name, trials: trialsOf(c.name) })),
|
|
227
|
+
...(given.loop ? { loop: given.loop } : {}),
|
|
228
|
+
started: now().toISOString(),
|
|
229
|
+
};
|
|
230
|
+
writeRun(workspace.store, record);
|
|
231
|
+
given.started?.(id);
|
|
232
|
+
clearStale(tmp);
|
|
233
|
+
clearStaleTrialFiles(workspace.labHome);
|
|
234
|
+
const { runner, logs, estimate } = externalsOf(environment);
|
|
235
|
+
const lab: TrialLab = {
|
|
236
|
+
workspace,
|
|
237
|
+
dataset: info,
|
|
238
|
+
run: id,
|
|
239
|
+
runner,
|
|
240
|
+
logs,
|
|
241
|
+
...(estimate ? { estimate } : {}),
|
|
242
|
+
tmp,
|
|
243
|
+
env: environment.env ?? process.env,
|
|
244
|
+
graders,
|
|
245
|
+
facts,
|
|
246
|
+
now,
|
|
247
|
+
};
|
|
248
|
+
try {
|
|
249
|
+
record = await runPasses({
|
|
250
|
+
context,
|
|
251
|
+
lab,
|
|
252
|
+
record,
|
|
253
|
+
selected,
|
|
254
|
+
trialsOf,
|
|
255
|
+
codeFolders,
|
|
256
|
+
reuse,
|
|
257
|
+
bars,
|
|
258
|
+
given,
|
|
259
|
+
});
|
|
260
|
+
} catch (error) {
|
|
261
|
+
record = { ...record, broke: (error as Error).message, finished: now().toISOString() };
|
|
262
|
+
writeRun(workspace.store, record);
|
|
263
|
+
throw new LabBroke(`the lab broke during run ${id}: ${(error as Error).message}`);
|
|
264
|
+
}
|
|
265
|
+
record = { ...record, finished: now().toISOString() };
|
|
266
|
+
writeRun(workspace.store, record);
|
|
267
|
+
return record;
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (error instanceof LabBroke) {
|
|
270
|
+
stderr(`awf-lab: ${error.message}`);
|
|
271
|
+
return { exit: 4, ...(record ? { id: record.id } : {}) };
|
|
272
|
+
}
|
|
273
|
+
throw error;
|
|
274
|
+
} finally {
|
|
275
|
+
if (record && info.records) writeRecords(workspace.store, info.records, record.id);
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
given.waitForStore ?? false,
|
|
279
|
+
);
|
|
280
|
+
if ("exit" in ran) return ran;
|
|
281
|
+
const record = ran;
|
|
282
|
+
const report = await runReport(context, workspace, {
|
|
283
|
+
info,
|
|
284
|
+
run: record,
|
|
285
|
+
trials: readTrials(workspace.store, record.id),
|
|
286
|
+
bars,
|
|
287
|
+
graders,
|
|
288
|
+
criteria,
|
|
289
|
+
followsPlan: true,
|
|
290
|
+
});
|
|
291
|
+
const exit = budgetExit(record.stopped === "budget", report.exit);
|
|
292
|
+
if (given.report !== false) {
|
|
293
|
+
context.stdout(options.json ? JSON.stringify(report.json(exit), null, 2) : report.text);
|
|
294
|
+
}
|
|
295
|
+
return { exit, id: record.id };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The cases to run and each one's trials: `--cases`, a loop's order, `--tags` and `--failed`. */
|
|
299
|
+
function selectCases(
|
|
300
|
+
context: Context,
|
|
301
|
+
workspace: CaseWorkspace,
|
|
302
|
+
info: DatasetInfo,
|
|
303
|
+
cases: readonly CaseInfo[],
|
|
304
|
+
bars: ReadonlyMap<string, CaseBar>,
|
|
305
|
+
reference: Params,
|
|
306
|
+
given: RunGiven,
|
|
307
|
+
):
|
|
308
|
+
| { selected: CaseInfo[]; trialsOf: (name: string) => number }
|
|
309
|
+
| { nothing: string; exit: number } {
|
|
310
|
+
const { options } = context;
|
|
311
|
+
const byName = new Map(cases.map((c) => [c.name, c]));
|
|
312
|
+
for (const name of options.cases ?? []) {
|
|
313
|
+
if (!byName.has(name)) {
|
|
314
|
+
throw new Refusal(
|
|
315
|
+
`no case ${name} in ${info.name}; known: ${cases.map((c) => c.name).join(", ")}`,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
let selected = given.order
|
|
320
|
+
? given.order.flatMap((name) => byName.get(name) ?? [])
|
|
321
|
+
: options.cases
|
|
322
|
+
? cases.filter((c) => options.cases!.includes(c.name))
|
|
323
|
+
: [...cases];
|
|
324
|
+
if (selected.length === 0) {
|
|
325
|
+
return {
|
|
326
|
+
nothing: `no cases in ${shownPath(context, info.dataDir)}; awf-lab case new ${info.name}/{case} makes one`,
|
|
327
|
+
exit: 2,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
if (options.tags) {
|
|
331
|
+
const before = selected;
|
|
332
|
+
selected = selected.filter((c) => tagged(c, options.tags!));
|
|
333
|
+
if (selected.length === 0) {
|
|
334
|
+
const tags = options.tags.join(" or ");
|
|
335
|
+
if (options.cases) {
|
|
336
|
+
return { nothing: `none of --cases ${options.cases.join(",")} has tag ${tags}`, exit: 2 };
|
|
337
|
+
}
|
|
338
|
+
const known = [...new Set(before.flatMap((c) => c.key.tags ?? []))].sort();
|
|
339
|
+
const which = options.holdout ? "held-out case" : "case";
|
|
340
|
+
return {
|
|
341
|
+
nothing: `no ${which} in ${info.name} has tag ${tags}; ${known.length > 0 ? `its tags: ${known.join(", ")}` : `its ${which}s have no tags`}`,
|
|
342
|
+
exit: 2,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
const failed = options.failed
|
|
347
|
+
? failedCases(workspace.store, info.name, workspace.root, reference, bars)
|
|
348
|
+
: undefined;
|
|
349
|
+
// A known-failing case is expected to fail: --failed asks after the cases that shouldn't.
|
|
350
|
+
if (failed) for (const [name] of failed) if (bars.get(name)?.knownFailing) failed.delete(name);
|
|
351
|
+
if (failed) selected = selected.filter((c) => failed.has(c.name));
|
|
352
|
+
if (selected.length === 0) {
|
|
353
|
+
return {
|
|
354
|
+
nothing: "nothing to run: no case's newest verdict is failed, flaky or unclear",
|
|
355
|
+
exit: 0,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
return { selected, trialsOf: (name) => options.trials ?? failed?.get(name) ?? 1 };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Trials that ran before on what shapes a trial now, kept within reuseDays, stand in for new ones.
|
|
363
|
+
* --failed asks for new evidence on what failed: the trials that failed are never its answer.
|
|
364
|
+
*/
|
|
365
|
+
async function planReuse(args: {
|
|
366
|
+
context: Context;
|
|
367
|
+
workspace: CaseWorkspace;
|
|
368
|
+
info: DatasetInfo;
|
|
369
|
+
cases: readonly CaseInfo[];
|
|
370
|
+
selected: readonly CaseInfo[];
|
|
371
|
+
trialsOf: (name: string) => number;
|
|
372
|
+
columns: readonly Column[];
|
|
373
|
+
codeFolders: readonly CodeFolder[];
|
|
374
|
+
graders: readonly LabGrader[];
|
|
375
|
+
facts: FactsReader;
|
|
376
|
+
tmp: string;
|
|
377
|
+
now: () => Date;
|
|
378
|
+
}): Promise<Reuse> {
|
|
379
|
+
const { options } = args.context;
|
|
380
|
+
const { workspace, info, columns, codeFolders } = args;
|
|
381
|
+
const days = options.fresh || options.failed ? 0 : workspace.reuseDays;
|
|
382
|
+
const inputs = caseDigests(info.dataDir, args.selected, args.tmp);
|
|
383
|
+
const keyOf = (n: number, input: string) => {
|
|
384
|
+
const { folder, params } = columns[n]!;
|
|
385
|
+
const { version, settings, folder: setup } = codeFolders[folder]!;
|
|
386
|
+
return reuseKey({ version, params, input, settings, awf: setup.version });
|
|
387
|
+
};
|
|
388
|
+
const stored =
|
|
389
|
+
days > 0
|
|
390
|
+
? reusableTrials(workspace.store, info.name, args.now(), days)
|
|
391
|
+
: new Map<string, StoredTrial[]>();
|
|
392
|
+
const slots: Reuse["slots"] = new Map();
|
|
393
|
+
// One workflow run stands for one trial of one column: two columns that shape trials alike,
|
|
394
|
+
// such as a clone at the same commit, each get trials of their own, or the pair is no check.
|
|
395
|
+
const taken = new Set<string>();
|
|
396
|
+
for (const c of args.selected) {
|
|
397
|
+
for (const n of columns.keys()) {
|
|
398
|
+
const found = (stored.get(keyOf(n, inputs.get(c.name)!)) ?? []).filter(
|
|
399
|
+
(t) => t.record.case === c.name && !taken.has(t.record.runId!),
|
|
400
|
+
);
|
|
401
|
+
const chosen = found.slice(0, args.trialsOf(c.name));
|
|
402
|
+
for (const t of chosen) taken.add(t.record.runId!);
|
|
403
|
+
if (chosen.length > 0) slots.set(slotOf(c.name, n), { column: n, trials: chosen });
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const reused = [...slots.values()].flatMap((s) => s.trials);
|
|
407
|
+
const regrade =
|
|
408
|
+
reused.length > 0
|
|
409
|
+
? await regradePlan({
|
|
410
|
+
trials: reused,
|
|
411
|
+
store: workspace.store,
|
|
412
|
+
info,
|
|
413
|
+
cases: args.cases,
|
|
414
|
+
graders: args.graders,
|
|
415
|
+
facts: args.facts,
|
|
416
|
+
tmp: args.tmp,
|
|
417
|
+
})
|
|
418
|
+
: undefined;
|
|
419
|
+
return {
|
|
420
|
+
slots,
|
|
421
|
+
count: reused.length,
|
|
422
|
+
keyOf,
|
|
423
|
+
...(regrade && regrade.items.length > 0 ? { regrade } : {}),
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** The plan a run asks about: its cases and trials, code, parameters, reuse and estimate; as JSON too. */
|
|
428
|
+
function planText(args: {
|
|
429
|
+
workspace: CaseWorkspace;
|
|
430
|
+
info: DatasetInfo;
|
|
431
|
+
selected: readonly CaseInfo[];
|
|
432
|
+
trialsOf: (name: string) => number;
|
|
433
|
+
codeFolders: readonly CodeFolder[];
|
|
434
|
+
paramPlan: ParamPlan;
|
|
435
|
+
columns: readonly Column[];
|
|
436
|
+
reuse: Reuse;
|
|
437
|
+
budget?: number;
|
|
438
|
+
}): { text: string; json: CasePlan } {
|
|
439
|
+
const { info, selected, trialsOf, columns, reuse } = args;
|
|
440
|
+
const total = selected.reduce((n, c) => n + trialsOf(c.name), 0);
|
|
441
|
+
const counts = new Set(selected.map((c) => trialsOf(c.name)));
|
|
442
|
+
const regrade = reuse.regrade;
|
|
443
|
+
const atOnce = args.workspace.concurrency;
|
|
444
|
+
const toRun = total * columns.length - reuse.count;
|
|
445
|
+
const earlier = earlierTrials(
|
|
446
|
+
args.workspace.store,
|
|
447
|
+
info.name,
|
|
448
|
+
selected.map((c) => c.name),
|
|
449
|
+
);
|
|
450
|
+
const estimated = estimateOf(earlier, toRun, atOnce);
|
|
451
|
+
const text = [
|
|
452
|
+
`${info.name} · ${plural(selected.length, "case")} · ${counts.size === 1 ? `${plural([...counts][0]!, "trial")} each` : plural(total, "trial")}`,
|
|
453
|
+
...args.codeFolders.map(
|
|
454
|
+
(c, i) =>
|
|
455
|
+
`${i === 0 ? "code" : c.folder.root === args.codeFolders[0]!.folder.root ? "the same code again" : `against ${i}`}: ${describeFolder({ path: i === 0 && c.folder.root === args.workspace.root ? undefined : c.folder.root, ...c.state })} · version ${c.version} · agentswf ${c.folder.version}`,
|
|
456
|
+
),
|
|
457
|
+
...paramLines(args.paramPlan, columns.length),
|
|
458
|
+
...(reuse.count > 0
|
|
459
|
+
? [
|
|
460
|
+
`reuses ${reuse.count} of ${plural(total * columns.length, "trial")}: the same version, settings and cases; --fresh runs new ones`,
|
|
461
|
+
]
|
|
462
|
+
: []),
|
|
463
|
+
...(regrade
|
|
464
|
+
? [
|
|
465
|
+
`grades ${plural(regrade.items.length, "reused trial")} again, a key or grader changed${regrade.estimate !== undefined ? `: est. ${usd(regrade.estimate)}${regrade.upTo !== undefined && regrade.upTo > regrade.estimate ? `, up to ${usd(regrade.upTo)}` : ""}` : ""}`,
|
|
466
|
+
]
|
|
467
|
+
: []),
|
|
468
|
+
...(atOnce > 1 ? [`runs up to ${plural(atOnce, "trial")} at once`] : []),
|
|
469
|
+
estimateText(earlier, toRun, atOnce),
|
|
470
|
+
].join("\n");
|
|
471
|
+
const json: CasePlan = {
|
|
472
|
+
kind: PLAN_KIND,
|
|
473
|
+
command: "run",
|
|
474
|
+
dataset: info.name,
|
|
475
|
+
cases: selected.map((c) => ({ case: c.name, trials: trialsOf(c.name) })),
|
|
476
|
+
columns: columns.length,
|
|
477
|
+
trials: toRun,
|
|
478
|
+
reused: reuse.count,
|
|
479
|
+
...(regrade ? { grades: Object.fromEntries(regrade.counts) } : {}),
|
|
480
|
+
...(estimated ? { estimate: estimated } : {}),
|
|
481
|
+
...(args.budget !== undefined ? { budget: args.budget } : {}),
|
|
482
|
+
concurrency: atOnce,
|
|
483
|
+
};
|
|
484
|
+
return { text, json };
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* The run's trials: the reused ones put in place and graded again where a key or grader changed,
|
|
489
|
+
* then trial t of every case, then t + 1; under a loop's `stop`, a case's every trial first. Up to
|
|
490
|
+
* the dataset's `concurrency` run at once, started in that order. Stops starting trials once their
|
|
491
|
+
* estimates reach `--budget`, once trials of two cases in a row failed a precondition, or at the
|
|
492
|
+
* first trial whose run never entered a stage its case named.
|
|
493
|
+
*/
|
|
494
|
+
async function runPasses(args: {
|
|
495
|
+
context: Context;
|
|
496
|
+
lab: TrialLab;
|
|
497
|
+
record: RunRecord;
|
|
498
|
+
selected: readonly CaseInfo[];
|
|
499
|
+
trialsOf: (name: string) => number;
|
|
500
|
+
codeFolders: readonly CodeFolder[];
|
|
501
|
+
reuse: Reuse;
|
|
502
|
+
bars: ReadonlyMap<string, CaseBar>;
|
|
503
|
+
given: RunGiven;
|
|
504
|
+
}): Promise<RunRecord> {
|
|
505
|
+
const { context, lab, selected, trialsOf, codeFolders, reuse, bars, given } = args;
|
|
506
|
+
const { budget } = context.options;
|
|
507
|
+
const { store } = lab.workspace;
|
|
508
|
+
let record = args.record;
|
|
509
|
+
const { columns } = record;
|
|
510
|
+
let estimated = 0;
|
|
511
|
+
const over = () => budget !== undefined && estimated >= budget;
|
|
512
|
+
const counted = (done: TrialDone) => {
|
|
513
|
+
estimated += trialSpend(
|
|
514
|
+
{ record: done.record, scores: done.score ? [done.score] : [] },
|
|
515
|
+
record,
|
|
516
|
+
);
|
|
517
|
+
};
|
|
518
|
+
const shown = (done: TrialDone) =>
|
|
519
|
+
progress(context.stderr, record, done, bars.get(done.record.case)?.deciders);
|
|
520
|
+
const trialIn = (n: number, info: CaseInfo, trial: number) => {
|
|
521
|
+
const column = columns[n]!;
|
|
522
|
+
const code = codeFolders[column.folder]!;
|
|
523
|
+
return runTrial(lab, code.folder, info, n, trial, {
|
|
524
|
+
trialScript: code.trialScript,
|
|
525
|
+
params: column.params,
|
|
526
|
+
version: code.version,
|
|
527
|
+
settings: code.settings,
|
|
528
|
+
reuseOf: (input) => reuse.keyOf(n, input),
|
|
529
|
+
});
|
|
530
|
+
};
|
|
531
|
+
for (const { column, trials } of reuse.slots.values()) {
|
|
532
|
+
for (const [i, from] of trials.entries()) reuseTrial(store, record.id, from, column, i + 1);
|
|
533
|
+
}
|
|
534
|
+
if (reuse.regrade) {
|
|
535
|
+
const done = await gradeReused(context, lab, reuse.regrade);
|
|
536
|
+
estimated += done.estimate;
|
|
537
|
+
if (done.stopped) record = { ...record, stopped: "budget" };
|
|
538
|
+
}
|
|
539
|
+
const passes: { cases: CaseInfo[]; trial: number }[] = [];
|
|
540
|
+
if (given.stop) {
|
|
541
|
+
for (const c of selected) {
|
|
542
|
+
for (let trial = 1; trial <= trialsOf(c.name); trial++) passes.push({ cases: [c], trial });
|
|
543
|
+
}
|
|
544
|
+
} else {
|
|
545
|
+
const most = Math.max(...selected.map((c) => trialsOf(c.name)));
|
|
546
|
+
for (let trial = 1; trial <= most; trial++) {
|
|
547
|
+
passes.push({ cases: selected.filter((c) => trialsOf(c.name) >= trial), trial });
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
const reusedIn = (name: string, n: number) =>
|
|
551
|
+
reuse.slots.get(slotOf(name, n))?.trials.length ?? 0;
|
|
552
|
+
type Unit = { info: CaseInfo; n: number; trial: number; pass: number; retry?: true };
|
|
553
|
+
const units: Unit[] = [];
|
|
554
|
+
for (const [pass, { cases: inPass, trial }] of passes.entries()) {
|
|
555
|
+
for (const info of inPass) {
|
|
556
|
+
for (const n of columns.keys()) {
|
|
557
|
+
if (trial > reusedIn(info.name, n)) units.push({ info, n, trial, pass });
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
// Per case, its trials not yet done, a try again included: a case is whole at none.
|
|
562
|
+
const left = new Map<string, number>();
|
|
563
|
+
for (const u of units) left.set(u.info.name, (left.get(u.info.name) ?? 0) + 1);
|
|
564
|
+
// A provider's failure is tried once more, after the rest of its pass has started.
|
|
565
|
+
const again: Unit[] = [];
|
|
566
|
+
let next = 0;
|
|
567
|
+
const take = (): Unit | undefined => {
|
|
568
|
+
const waiting = again[0];
|
|
569
|
+
if (waiting && (next >= units.length || units[next]!.pass > waiting.pass)) {
|
|
570
|
+
return again.shift();
|
|
571
|
+
}
|
|
572
|
+
return next < units.length ? units[next++] : undefined;
|
|
573
|
+
};
|
|
574
|
+
const hasNext = () => again.length > 0 || next < units.length;
|
|
575
|
+
// Under a loop's `stop`, cases are checked whole in their order: the comparison may stop the rest.
|
|
576
|
+
let checked = 0;
|
|
577
|
+
const checkWhole = async () => {
|
|
578
|
+
while (
|
|
579
|
+
given.stop &&
|
|
580
|
+
!record.stopped &&
|
|
581
|
+
checked < selected.length &&
|
|
582
|
+
(left.get(selected[checked]!.name) ?? 0) === 0
|
|
583
|
+
) {
|
|
584
|
+
checked++;
|
|
585
|
+
const stop = await given.stop(record, readTrials(store, record.id));
|
|
586
|
+
// A trial that ended meanwhile may have stopped the run first: its reason stands.
|
|
587
|
+
if (stop && !record.stopped) record = { ...record, stopped: "compare" };
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
let checks = checkWhole();
|
|
591
|
+
// The cases of the precondition failures in a row, in the order trials end: two cases say the
|
|
592
|
+
// environment isn't ready; one case's own trials say only that case isn't.
|
|
593
|
+
const unready = new Set<string>();
|
|
594
|
+
const play = async (unit: Unit) => {
|
|
595
|
+
const done = await trialIn(unit.n, unit.info, unit.trial);
|
|
596
|
+
counted(done);
|
|
597
|
+
const name = unit.info.name;
|
|
598
|
+
if (failedPrecondition(done.record)) unready.add(name);
|
|
599
|
+
else unready.clear();
|
|
600
|
+
if (unready.size >= 2 && !record.stopped) record = { ...record, stopped: "precondition" };
|
|
601
|
+
// A stage a case named and the run never entered is nearly always misspelt: every case would.
|
|
602
|
+
if (done.record.errored?.code === "stage-not-entered" && !record.stopped) {
|
|
603
|
+
record = { ...record, stopped: "stage" };
|
|
604
|
+
}
|
|
605
|
+
if (unit.retry) {
|
|
606
|
+
const retried = { ...done.record, retried: true };
|
|
607
|
+
writeTrial(done.dir, retried);
|
|
608
|
+
shown({ ...done, record: retried });
|
|
609
|
+
} else {
|
|
610
|
+
if (done.record.errored?.by === "provider") {
|
|
611
|
+
again.push({ ...unit, retry: true });
|
|
612
|
+
left.set(name, left.get(name)! + 1);
|
|
613
|
+
}
|
|
614
|
+
shown(done);
|
|
615
|
+
}
|
|
616
|
+
left.set(name, left.get(name)! - 1);
|
|
617
|
+
checks = checks.then(checkWhole);
|
|
618
|
+
await checks;
|
|
619
|
+
};
|
|
620
|
+
const atOnce = lab.workspace.concurrency;
|
|
621
|
+
const running = new Set<Promise<void>>();
|
|
622
|
+
let failure: { error: unknown } | undefined;
|
|
623
|
+
await checks;
|
|
624
|
+
for (;;) {
|
|
625
|
+
// Stops starting trials once their estimates reach --budget; the ones running finish.
|
|
626
|
+
while (!failure && !record.stopped && running.size < atOnce && hasNext()) {
|
|
627
|
+
if (over()) {
|
|
628
|
+
record = { ...record, stopped: "budget" };
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
const playing: Promise<void> = play(take()!)
|
|
632
|
+
.catch((error) => {
|
|
633
|
+
failure ??= { error };
|
|
634
|
+
})
|
|
635
|
+
.finally(() => running.delete(playing));
|
|
636
|
+
running.add(playing);
|
|
637
|
+
}
|
|
638
|
+
if (running.size === 0) break;
|
|
639
|
+
await Promise.race(running);
|
|
640
|
+
}
|
|
641
|
+
if (failure) throw failure.error;
|
|
642
|
+
return record;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Grades the trials a run reused where a key or grader changed since they were graded, as `score`
|
|
647
|
+
* would, with no question: the run's plan asked. The plan's trials are read as this run's.
|
|
648
|
+
*/
|
|
649
|
+
async function gradeReused(
|
|
650
|
+
context: Context,
|
|
651
|
+
lab: Pick<TrialLab, "workspace" | "run" | "tmp" | "now">,
|
|
652
|
+
planned: ScorePlan,
|
|
653
|
+
): Promise<ScoreDone> {
|
|
654
|
+
const { budget } = context.options;
|
|
655
|
+
const { store } = lab.workspace;
|
|
656
|
+
const { run } = lab;
|
|
657
|
+
const here = new Map(
|
|
658
|
+
readTrials(store, run)
|
|
659
|
+
.filter((t) => t.record.reused)
|
|
660
|
+
.map((t) => [t.dir, t]),
|
|
661
|
+
);
|
|
662
|
+
const plan = {
|
|
663
|
+
...planned,
|
|
664
|
+
items: planned.items.map((item) => ({
|
|
665
|
+
...item,
|
|
666
|
+
trial: here.get(item.trial.dir) ?? item.trial,
|
|
667
|
+
})),
|
|
668
|
+
};
|
|
669
|
+
return runScore({
|
|
670
|
+
plan,
|
|
671
|
+
tmp: lab.tmp,
|
|
672
|
+
run,
|
|
673
|
+
store,
|
|
674
|
+
env: envOf(context),
|
|
675
|
+
...(budget !== undefined ? { budget } : {}),
|
|
676
|
+
atOnce: lab.workspace.concurrency,
|
|
677
|
+
now: lab.now,
|
|
678
|
+
progress: context.stderr,
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/** Earlier trials of some cases, each a median: its time, its cost, and its grading's cost. */
|
|
683
|
+
export type Earlier = { ms: number; usd?: number; grading: number };
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* Earlier trials of these cases: their median time and cost, each with the grading it was given as
|
|
687
|
+
* its run ran, since a judge can cost far more than the workflow it judges.
|
|
688
|
+
*/
|
|
689
|
+
export function earlierTrials(
|
|
690
|
+
store: string,
|
|
691
|
+
dataset: string,
|
|
692
|
+
names: readonly string[],
|
|
693
|
+
): Earlier | undefined {
|
|
694
|
+
const ms: number[] = [];
|
|
695
|
+
const cost: number[] = [];
|
|
696
|
+
const grading: number[] = [];
|
|
697
|
+
for (const { run, trials } of readableRuns(store, { dataset })) {
|
|
698
|
+
const span = runSpan(store, run);
|
|
699
|
+
for (const { record: trial, scores } of trials) {
|
|
700
|
+
if (!names.includes(trial.case) || !trial.runId || trial.reused) continue;
|
|
701
|
+
if (trialOutcome(trial, scores).outcome === "errored") continue;
|
|
702
|
+
const graded = scoresInRun(scores, span);
|
|
703
|
+
ms.push(trial.ms + graded.flatMap((s) => s.entries).reduce((sum, e) => sum + (e.ms ?? 0), 0));
|
|
704
|
+
if (trial.estimate !== undefined) cost.push(trial.estimate);
|
|
705
|
+
grading.push(gradingSpend(graded));
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
const time = median(ms);
|
|
709
|
+
if (time === undefined) return undefined;
|
|
710
|
+
const each = median(cost);
|
|
711
|
+
return { ms: time, ...(each !== undefined ? { usd: each } : {}), grading: median(grading) ?? 0 };
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/** `count` trials at `earlier`'s medians, `atOnce` sharing the time. */
|
|
715
|
+
export function estimateOf(
|
|
716
|
+
earlier: Earlier | undefined,
|
|
717
|
+
count: number,
|
|
718
|
+
atOnce: number,
|
|
719
|
+
): CasePlan["estimate"] | undefined {
|
|
720
|
+
if (!earlier) return undefined;
|
|
721
|
+
const priced =
|
|
722
|
+
earlier.usd !== undefined || earlier.grading > 0
|
|
723
|
+
? { usd: ((earlier.usd ?? 0) + earlier.grading) * count }
|
|
724
|
+
: {};
|
|
725
|
+
return { ms: earlier.ms * Math.ceil(count / atOnce), ...priced };
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/** The plan's estimate line. */
|
|
729
|
+
export function estimateText(
|
|
730
|
+
earlier: Earlier | undefined,
|
|
731
|
+
count: number,
|
|
732
|
+
atOnce: number,
|
|
733
|
+
about = "about ",
|
|
734
|
+
): string {
|
|
735
|
+
if (!earlier) return "no estimate yet: no earlier trials of these cases";
|
|
736
|
+
const { usd: each, grading: grades } = earlier;
|
|
737
|
+
const price =
|
|
738
|
+
each === undefined
|
|
739
|
+
? grades > 0
|
|
740
|
+
? `, grading est. ${usd(grades * count)}, the workflow unpriced`
|
|
741
|
+
: ""
|
|
742
|
+
: `, est. ${usd((each + grades) * count)}${grades > 0 ? `, grading ${usd(grades * count)} of it` : ""}`;
|
|
743
|
+
return `from earlier trials: ${about}${seconds(earlier.ms * Math.ceil(count / atOnce))}${price}`;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/** The plan's lines for parameters: those every column shares, and what varies. */
|
|
747
|
+
function paramLines(plan: ParamPlan, columns: number): string[] {
|
|
748
|
+
const fixed = Object.entries(plan.fixed).map(([name, value]) => `${name}=${value}`);
|
|
749
|
+
return [
|
|
750
|
+
...(fixed.length > 0 ? [`params: ${fixed.join(" ")}`] : []),
|
|
751
|
+
...(plan.varied.length > 0
|
|
752
|
+
? [
|
|
753
|
+
`vary: ${plan.varied.map(([name, values]) => `${name}=${values.join(",")}`).join(" ")} · ${plural(columns, "column")}, the first value of each the reference`,
|
|
754
|
+
]
|
|
755
|
+
: []),
|
|
756
|
+
];
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/**
|
|
760
|
+
* A folder `--against` names: another checkout of the project, holding the dataset at the same
|
|
761
|
+
* path as yours. Only its `trial.ts`, its code and its `awf` are used; its cases, keys and grading
|
|
762
|
+
* are never read. Its `trial.ts` gives its version, sandbox and limits, `--limit` over them.
|
|
763
|
+
* Checked as your checkout is before the first trial.
|
|
764
|
+
*/
|
|
765
|
+
async function againstFolder(
|
|
766
|
+
context: Context,
|
|
767
|
+
workspace: CaseWorkspace,
|
|
768
|
+
info: DatasetInfo,
|
|
769
|
+
given: string,
|
|
770
|
+
taken: readonly CodeFolder[],
|
|
771
|
+
limits: Limits,
|
|
772
|
+
/** A loop's id of the code and its settings, in place of what its `trial.ts` declares. */
|
|
773
|
+
loop?: { version: string; settings: ResolvedSettings },
|
|
774
|
+
): Promise<CodeFolder> {
|
|
775
|
+
const at = resolve(context.cwd, given);
|
|
776
|
+
if (!existsSync(at)) throw new Refusal(`--against ${given}: no such folder`);
|
|
777
|
+
const dir = realpathSync(at);
|
|
778
|
+
const top = workspace.repository ?? workspace.root;
|
|
779
|
+
const evalDir = join(dir, relative(top, info.evalDir));
|
|
780
|
+
const evals = join(dir, relative(top, workspace.evals));
|
|
781
|
+
if (!existsSync(join(evalDir, TRIAL_FILE))) {
|
|
782
|
+
throw new Refusal(
|
|
783
|
+
`--against ${given}: no ${relative(top, join(info.evalDir, TRIAL_FILE))} there; it names another checkout of this project, holding the eval folder at the same path`,
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
if (dir === workspace.root || dir === workspace.repository) {
|
|
787
|
+
throw new Refusal(`--against ${given}: that is your checkout`);
|
|
788
|
+
}
|
|
789
|
+
const root = codeFolderOf(dirname(evals));
|
|
790
|
+
// Its own install, never one above it: a worktree inside another checkout would run that one.
|
|
791
|
+
if (!inside(dir, root) || !existsSync(join(root, "node_modules", "agentswf"))) {
|
|
792
|
+
throw new Refusal(
|
|
793
|
+
`--against ${given}: no agentswf installed in it; run bun install there (its code runs on its own install)`,
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
if (taken.some((c) => c.folder.root === root)) {
|
|
797
|
+
throw new Refusal(`--against ${given}: ${root} is named twice`);
|
|
798
|
+
}
|
|
799
|
+
let trialScript: string;
|
|
800
|
+
try {
|
|
801
|
+
trialScript = checkTrialScript(evalDir);
|
|
802
|
+
} catch (error) {
|
|
803
|
+
throw new Refusal(`--against ${given}: ${(error as Error).message}`);
|
|
804
|
+
}
|
|
805
|
+
const shaped = loop ?? againstTrial(context, root, trialScript, given, taken, limits);
|
|
806
|
+
const folder = await (context.environment.inspect ?? inspectFolder)(root, trialScript);
|
|
807
|
+
return { folder, trialScript, ...shaped, state: gitState(root) };
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* The version, sandbox and limits an `--against` folder's `trial.ts` declares, read in that
|
|
812
|
+
* folder. Its version must differ from every other column's in its major, minor or pre-release
|
|
813
|
+
* tag: the lab never looks at the code, so the same version would share the other's kept trials.
|
|
814
|
+
*/
|
|
815
|
+
function againstTrial(
|
|
816
|
+
context: Context,
|
|
817
|
+
root: string,
|
|
818
|
+
trialScript: string,
|
|
819
|
+
given: string,
|
|
820
|
+
taken: readonly CodeFolder[],
|
|
821
|
+
limits: Limits,
|
|
822
|
+
): { version: string; settings: ResolvedSettings } {
|
|
823
|
+
let trial: TrialFile;
|
|
824
|
+
try {
|
|
825
|
+
const described = describeFiles(
|
|
826
|
+
{ trial: trialScript },
|
|
827
|
+
{ root, env: envOf(context), tmp: tmpOf(context) },
|
|
828
|
+
);
|
|
829
|
+
trial = checkTrialFile(trialScript, described.trial);
|
|
830
|
+
} catch (error) {
|
|
831
|
+
throw new Refusal(`--against ${given}: ${(error as Error).message}`);
|
|
832
|
+
}
|
|
833
|
+
const { version } = trial;
|
|
834
|
+
const same = taken.find((c) => versionKey(c.version) === versionKey(version));
|
|
835
|
+
if (same) {
|
|
836
|
+
throw new Refusal(
|
|
837
|
+
`--against ${given}: both columns are version ${versionKey(version)}, so they would share each other's kept trials: bump it in the one you changed`,
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
return { version, settings: resolveSettings(trial, limits) };
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function progress(
|
|
844
|
+
stderr: (text: string) => void,
|
|
845
|
+
run: RunRecord,
|
|
846
|
+
done: TrialDone,
|
|
847
|
+
deciders: ReadonlySet<string> | undefined,
|
|
848
|
+
): void {
|
|
849
|
+
const view = trialOutcome(done.record, done.score ? [done.score] : [], deciders);
|
|
850
|
+
stderr(
|
|
851
|
+
` ${done.record.case} ${done.record.trial}${done.record.column > 0 ? ` (${columnLabel(run, done.record.column)})` : ""}: ${view.outcome}${done.record.retried ? " (tried again)" : ""} · ${seconds(done.record.ms)}`,
|
|
852
|
+
);
|
|
853
|
+
}
|