@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,89 @@
|
|
|
1
|
+
import type { LogEventJson, LogsJson, TurnRecord } from "@agentswf/contract/records";
|
|
2
|
+
import { runFactName, TURN_OUTCOMES } from "../format/settings";
|
|
3
|
+
|
|
4
|
+
/** A turn or compaction of a trial's run, as `awf logs --json` printed it once it settled. */
|
|
5
|
+
export type RunTurn = Pick<
|
|
6
|
+
TurnRecord,
|
|
7
|
+
"kind" | "attempt" | "agent" | "outcome" | "stage" | "label" | "deliveredAt" | "settledAt"
|
|
8
|
+
> & {
|
|
9
|
+
/** A message to the agent started it, not the workflow: the line's `cause`. */
|
|
10
|
+
caused?: true;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The turns and compactions in `awf logs --json`'s page, in the order the page holds them;
|
|
15
|
+
* undefined when the text is no page. An event of a kind or shape this lab doesn't know is
|
|
16
|
+
* skipped, as every reader of the run's logs skips one.
|
|
17
|
+
*/
|
|
18
|
+
export function turnsOf(text: string): RunTurn[] | undefined {
|
|
19
|
+
const events = eventsOf(text);
|
|
20
|
+
if (!events) return undefined;
|
|
21
|
+
return events.flatMap((event: LogEventJson) => {
|
|
22
|
+
if (!event || typeof event !== "object") return [];
|
|
23
|
+
if (event.kind !== "turn" && event.kind !== "compact") return [];
|
|
24
|
+
const line = event as Partial<RunTurn>;
|
|
25
|
+
if (typeof line.agent !== "string" || typeof line.outcome !== "string") return [];
|
|
26
|
+
if (typeof line.attempt !== "number") return [];
|
|
27
|
+
return [
|
|
28
|
+
{
|
|
29
|
+
kind: event.kind,
|
|
30
|
+
attempt: line.attempt,
|
|
31
|
+
agent: line.agent,
|
|
32
|
+
outcome: line.outcome,
|
|
33
|
+
...(typeof line.stage === "string" ? { stage: line.stage } : {}),
|
|
34
|
+
...(typeof line.label === "string" ? { label: line.label } : {}),
|
|
35
|
+
...(typeof line.deliveredAt === "string" ? { deliveredAt: line.deliveredAt } : {}),
|
|
36
|
+
...(typeof line.settledAt === "string" ? { settledAt: line.settledAt } : {}),
|
|
37
|
+
...((line as { cause?: unknown }).cause ? { caused: true as const } : {}),
|
|
38
|
+
},
|
|
39
|
+
];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The agents the run opened, by name, the calling session's left out; undefined for no page. */
|
|
44
|
+
export function agentsOpenedOf(text: string): string[] | undefined {
|
|
45
|
+
const events = eventsOf(text);
|
|
46
|
+
if (!events) return undefined;
|
|
47
|
+
const names = events.flatMap((event) => {
|
|
48
|
+
if (!event || typeof event !== "object" || event.kind !== "agent-opened") return [];
|
|
49
|
+
const line = event as { agent?: unknown; caller?: unknown };
|
|
50
|
+
return typeof line.agent === "string" && !line.caller ? [line.agent] : [];
|
|
51
|
+
});
|
|
52
|
+
return [...new Set(names)];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function eventsOf(text: string): LogEventJson[] | undefined {
|
|
56
|
+
let page: Partial<LogsJson>;
|
|
57
|
+
try {
|
|
58
|
+
page = JSON.parse(text);
|
|
59
|
+
} catch {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
if (!page || typeof page !== "object" || !Array.isArray(page.events)) return undefined;
|
|
63
|
+
return page.events;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The run facts: what the run's own record says of its path, beside `grading.ts`'s facts and under
|
|
68
|
+
* reserved `_` names. Turns only; compactions are left out, and a nudge is part of its turn.
|
|
69
|
+
* `_turns` and each outcome's count every turn; an agent's, the turns the workflow started (not a
|
|
70
|
+
* message's), by its name over every call path and attempt, 0 for an agent opened that took none.
|
|
71
|
+
*/
|
|
72
|
+
export function runFactsOf(
|
|
73
|
+
turns: readonly RunTurn[],
|
|
74
|
+
opened: readonly string[],
|
|
75
|
+
): Record<string, number> {
|
|
76
|
+
const facts: Record<string, number> = { _turns: 0 };
|
|
77
|
+
for (const outcome of TURN_OUTCOMES) facts[runFactName.outcome(outcome)] = 0;
|
|
78
|
+
for (const agent of opened) facts[runFactName.agent(agent)] = 0;
|
|
79
|
+
const add = (name: string) => {
|
|
80
|
+
facts[name] = (facts[name] ?? 0) + 1;
|
|
81
|
+
};
|
|
82
|
+
for (const turn of turns) {
|
|
83
|
+
if (turn.kind !== "turn") continue;
|
|
84
|
+
add("_turns");
|
|
85
|
+
add(runFactName.outcome(turn.outcome));
|
|
86
|
+
if (!turn.caused) add(runFactName.agent(turn.agent));
|
|
87
|
+
}
|
|
88
|
+
return facts;
|
|
89
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { type CaseReport, CaseReportSchema, REPORT_KIND } from "../format/output";
|
|
2
|
+
import type { RunRecord } from "../format/records";
|
|
3
|
+
import { checkSchema, describeProblems } from "../shared/validate";
|
|
4
|
+
import { versionKey } from "../shared/version";
|
|
5
|
+
import { columnLabel } from "./columns";
|
|
6
|
+
import { type Context, envOf, tmpOf } from "./command-context";
|
|
7
|
+
import { type ColumnVerdict, compareColumns } from "./compare";
|
|
8
|
+
import { comparingFile, type DatasetInfo } from "./dataset";
|
|
9
|
+
import type { LabGrader } from "./grading";
|
|
10
|
+
import { agreementOf, judgeLabeled, type PersonAgreement, readLabels } from "./labels";
|
|
11
|
+
import { type CaseRow, newestEntries } from "./outcome";
|
|
12
|
+
import { agreements, poolNumbers } from "./pooled";
|
|
13
|
+
import { pairedDifference, ratesOf } from "./rates";
|
|
14
|
+
import { type RunReport, renderRun, tagRows } from "./report";
|
|
15
|
+
import { type CaseBar, rowsOf } from "./runs";
|
|
16
|
+
import { readableRuns, type StoredTrial } from "./store";
|
|
17
|
+
import { afterMsOf } from "./trial-settings";
|
|
18
|
+
import type { CaseWorkspace } from "./workspace";
|
|
19
|
+
|
|
20
|
+
// The report a run ends with: its rows, the other columns, compare's verdicts and the person's
|
|
21
|
+
// agreement.
|
|
22
|
+
|
|
23
|
+
/** `compare`'s verdict on each column but the reference; undefined when the dataset has none. */
|
|
24
|
+
export async function columnVerdicts(
|
|
25
|
+
context: Context,
|
|
26
|
+
{
|
|
27
|
+
workspace,
|
|
28
|
+
info,
|
|
29
|
+
run,
|
|
30
|
+
trials,
|
|
31
|
+
bars,
|
|
32
|
+
}: {
|
|
33
|
+
workspace: CaseWorkspace;
|
|
34
|
+
info: DatasetInfo;
|
|
35
|
+
run: RunRecord;
|
|
36
|
+
trials: readonly StoredTrial[];
|
|
37
|
+
bars: ReadonlyMap<string, CaseBar>;
|
|
38
|
+
},
|
|
39
|
+
): Promise<ColumnVerdict[] | undefined> {
|
|
40
|
+
return compareColumns({
|
|
41
|
+
run,
|
|
42
|
+
trials,
|
|
43
|
+
bars,
|
|
44
|
+
gradingFile: comparingFile(info),
|
|
45
|
+
root: workspace.root,
|
|
46
|
+
tmp: tmpOf(context),
|
|
47
|
+
env: envOf(context),
|
|
48
|
+
timeoutMs: afterMsOf(info),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The person's labels on a run's trials and how the lab agrees with them, and how many labeled
|
|
54
|
+
* trials the current judge graded across the dataset's runs, for its "checked" mark.
|
|
55
|
+
*/
|
|
56
|
+
function personOf({
|
|
57
|
+
workspace,
|
|
58
|
+
dataset,
|
|
59
|
+
trials,
|
|
60
|
+
bars,
|
|
61
|
+
graders,
|
|
62
|
+
}: {
|
|
63
|
+
workspace: CaseWorkspace;
|
|
64
|
+
dataset: string;
|
|
65
|
+
trials: readonly StoredTrial[];
|
|
66
|
+
bars: ReadonlyMap<string, CaseBar>;
|
|
67
|
+
graders: readonly LabGrader[] | undefined;
|
|
68
|
+
}): Pick<RunReport, "person" | "judgeLabeled" | "manual" | "judges"> {
|
|
69
|
+
const labels = readLabels(workspace.store, dataset);
|
|
70
|
+
const manual = new Set((graders ?? []).filter((g) => g.manual).map((g) => g.name));
|
|
71
|
+
const person = agreementOf(trials, labels, (name) => bars.get(name)?.deciders, manual);
|
|
72
|
+
const judges = graders
|
|
73
|
+
? new Set(graders.filter((g) => g.agent && !g.manual).map((g) => g.name))
|
|
74
|
+
: undefined;
|
|
75
|
+
const shown = { ...(manual.size > 0 ? { manual } : {}), ...(judges ? { judges } : {}) };
|
|
76
|
+
// The judge whose "checked" mark the report gives: the first `sentences` that decides.
|
|
77
|
+
const listed = graders?.find((g) => g.agent && !g.manual);
|
|
78
|
+
const graded = listed
|
|
79
|
+
? undefined
|
|
80
|
+
: trials.flatMap((t) => newestEntries(t.scores)).find((e) => e.agent && !manual.has(e.grader));
|
|
81
|
+
const agent = listed?.agent ?? graded?.agent;
|
|
82
|
+
const name = listed?.name ?? graded?.grader;
|
|
83
|
+
if (!agent || !name) return { ...(person ? { person } : {}), ...shown };
|
|
84
|
+
const runs = [...readableRuns(workspace.store, { dataset })].map((r) => r.trials);
|
|
85
|
+
return {
|
|
86
|
+
...(person ? { person } : {}),
|
|
87
|
+
...shown,
|
|
88
|
+
judgeLabeled: judgeLabeled(runs, labels, {
|
|
89
|
+
grader: name,
|
|
90
|
+
...agent,
|
|
91
|
+
minor: versionKey(listed?.version ?? graded?.version) ?? "",
|
|
92
|
+
}),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The rows of each column but yours, from the second on. */
|
|
97
|
+
function otherColumns(
|
|
98
|
+
store: string,
|
|
99
|
+
run: RunRecord,
|
|
100
|
+
trials: readonly StoredTrial[],
|
|
101
|
+
bars: ReadonlyMap<string, CaseBar>,
|
|
102
|
+
): CaseRow[][] {
|
|
103
|
+
return run.columns.slice(1).map((_, i) => rowsOf(store, run, trials, bars, i + 1));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* How a run, report or score ends: the run's rows and the other columns', the person's agreement
|
|
108
|
+
* and `compare`'s verdicts, rendered, and as `--json` for an exit the caller settles.
|
|
109
|
+
*/
|
|
110
|
+
export async function runReport(
|
|
111
|
+
context: Context,
|
|
112
|
+
workspace: CaseWorkspace,
|
|
113
|
+
args: {
|
|
114
|
+
/** The dataset as it is now; undefined when it no longer reads. */
|
|
115
|
+
info: DatasetInfo | undefined;
|
|
116
|
+
run: RunRecord;
|
|
117
|
+
trials: readonly StoredTrial[];
|
|
118
|
+
bars: ReadonlyMap<string, CaseBar>;
|
|
119
|
+
graders: readonly LabGrader[] | undefined;
|
|
120
|
+
criteria: number;
|
|
121
|
+
stale?: CaseReport["stale"];
|
|
122
|
+
/** The run's plan was printed just before. */
|
|
123
|
+
followsPlan?: boolean;
|
|
124
|
+
},
|
|
125
|
+
) {
|
|
126
|
+
const { info, run, trials, bars, stale } = args;
|
|
127
|
+
const rows = rowsOf(workspace.store, run, trials, bars);
|
|
128
|
+
const others = otherColumns(workspace.store, run, trials, bars);
|
|
129
|
+
const personal = personOf({
|
|
130
|
+
workspace,
|
|
131
|
+
dataset: run.dataset,
|
|
132
|
+
trials,
|
|
133
|
+
bars,
|
|
134
|
+
graders: args.graders,
|
|
135
|
+
});
|
|
136
|
+
const verdicts = info
|
|
137
|
+
? await columnVerdicts(context, { workspace, info, run, trials, bars })
|
|
138
|
+
: undefined;
|
|
139
|
+
const { text, exit } = renderRun({
|
|
140
|
+
run,
|
|
141
|
+
rows,
|
|
142
|
+
others,
|
|
143
|
+
trials: [...trials],
|
|
144
|
+
criteria: args.criteria,
|
|
145
|
+
...(stale !== undefined ? { stale } : {}),
|
|
146
|
+
decidersOf: (name) => bars.get(name)?.deciders,
|
|
147
|
+
tagsOf: (name) => bars.get(name)?.tags ?? [],
|
|
148
|
+
ungradedOf: (name) => bars.get(name)?.ungraded,
|
|
149
|
+
...(args.followsPlan ? { followsPlan: true } : {}),
|
|
150
|
+
...(verdicts ? { verdicts } : {}),
|
|
151
|
+
...personal,
|
|
152
|
+
});
|
|
153
|
+
return {
|
|
154
|
+
rows,
|
|
155
|
+
others,
|
|
156
|
+
person: personal.person,
|
|
157
|
+
verdicts,
|
|
158
|
+
text,
|
|
159
|
+
exit,
|
|
160
|
+
json: (exit: number, scored?: CaseReport["scored"]) =>
|
|
161
|
+
reportJson({
|
|
162
|
+
...(scored ? { scored } : {}),
|
|
163
|
+
run,
|
|
164
|
+
rows,
|
|
165
|
+
trials,
|
|
166
|
+
exit,
|
|
167
|
+
bars,
|
|
168
|
+
others,
|
|
169
|
+
...(stale !== undefined ? { stale } : {}),
|
|
170
|
+
...(personal.person ? { person: personal.person } : {}),
|
|
171
|
+
...(verdicts ? { verdicts } : {}),
|
|
172
|
+
}),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** `report --json`: a run's cases, pooled numbers and agreement, checked against its schema. */
|
|
177
|
+
export function reportJson(args: {
|
|
178
|
+
run: RunRecord;
|
|
179
|
+
rows: readonly CaseRow[];
|
|
180
|
+
trials: readonly StoredTrial[];
|
|
181
|
+
exit: number;
|
|
182
|
+
bars: ReadonlyMap<string, CaseBar>;
|
|
183
|
+
stale?: CaseReport["stale"];
|
|
184
|
+
withTrials?: boolean;
|
|
185
|
+
others?: readonly CaseRow[][];
|
|
186
|
+
person?: PersonAgreement;
|
|
187
|
+
verdicts?: readonly ColumnVerdict[];
|
|
188
|
+
scored?: CaseReport["scored"];
|
|
189
|
+
}): CaseReport {
|
|
190
|
+
const { run, rows, trials, exit, bars } = args;
|
|
191
|
+
const own = trials.filter((t) => t.record.column === 0);
|
|
192
|
+
const pooled = poolNumbers(own);
|
|
193
|
+
const report: CaseReport = {
|
|
194
|
+
kind: REPORT_KIND,
|
|
195
|
+
run: run.id,
|
|
196
|
+
dataset: run.dataset,
|
|
197
|
+
partial: run.finished === undefined || run.broke !== undefined,
|
|
198
|
+
exit,
|
|
199
|
+
params: run.columns[0]!.params,
|
|
200
|
+
reused: trials.filter((t) => t.record.reused).length,
|
|
201
|
+
rates: ratesOf(rows),
|
|
202
|
+
tags: tagRows(rows, (name) => bars.get(name)?.tags ?? []),
|
|
203
|
+
cases: rows.map((row) => ({
|
|
204
|
+
name: row.name,
|
|
205
|
+
verdict: row.tally.verdict ?? null,
|
|
206
|
+
...(row.tally.none ? { none: row.tally.none } : {}),
|
|
207
|
+
passed: row.tally.passed,
|
|
208
|
+
trials: row.tally.trials,
|
|
209
|
+
knownFailing: row.knownFailing,
|
|
210
|
+
outcomes: row.outcomes,
|
|
211
|
+
...(row.why ? { why: row.why } : {}),
|
|
212
|
+
notes: row.notes,
|
|
213
|
+
history: row.history,
|
|
214
|
+
})),
|
|
215
|
+
numbers: Object.fromEntries(
|
|
216
|
+
[...pooled.graders].map(([grader, numbers]) => [grader, Object.fromEntries(numbers)]),
|
|
217
|
+
),
|
|
218
|
+
numbersLeftOut: pooled.left,
|
|
219
|
+
agreement: agreements(own, (name) => bars.get(name)?.deciders),
|
|
220
|
+
...(args.person ? { labels: args.person } : {}),
|
|
221
|
+
...(args.stale !== undefined ? { stale: args.stale } : {}),
|
|
222
|
+
...(args.others && args.others.length > 0
|
|
223
|
+
? {
|
|
224
|
+
columns: args.others.map((column, i) => {
|
|
225
|
+
const { folder: at, params } = run.columns[i + 1]!;
|
|
226
|
+
const folder = run.folders[at]!;
|
|
227
|
+
const theirs = poolNumbers(trials.filter((t) => t.record.column === i + 1));
|
|
228
|
+
const difference = pairedDifference(rows, column);
|
|
229
|
+
return {
|
|
230
|
+
label: columnLabel(run, i + 1),
|
|
231
|
+
...(args.verdicts?.[i] ? { compare: args.verdicts[i] } : {}),
|
|
232
|
+
rates: ratesOf(column),
|
|
233
|
+
...(difference ? { difference } : {}),
|
|
234
|
+
...folder,
|
|
235
|
+
params,
|
|
236
|
+
numbers: Object.fromEntries(
|
|
237
|
+
[...theirs.graders].map(([grader, numbers]) => [
|
|
238
|
+
grader,
|
|
239
|
+
Object.fromEntries(numbers),
|
|
240
|
+
]),
|
|
241
|
+
),
|
|
242
|
+
numbersLeftOut: theirs.left,
|
|
243
|
+
cases: column.map((row) => ({
|
|
244
|
+
name: row.name,
|
|
245
|
+
verdict: row.tally.verdict ?? null,
|
|
246
|
+
passed: row.tally.passed,
|
|
247
|
+
trials: row.tally.trials,
|
|
248
|
+
})),
|
|
249
|
+
};
|
|
250
|
+
}),
|
|
251
|
+
}
|
|
252
|
+
: {}),
|
|
253
|
+
...(args.withTrials ? { trials: trials.map((t) => ({ ...t.record, scores: t.scores })) } : {}),
|
|
254
|
+
...(args.scored ? { scored: args.scored } : {}),
|
|
255
|
+
};
|
|
256
|
+
const checked = checkSchema(CaseReportSchema, report);
|
|
257
|
+
if (!checked.ok) throw new Error(describeProblems("report --json", checked.problems));
|
|
258
|
+
return report;
|
|
259
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
import { type CaseRuns, CaseRunsSchema, RUNS_KIND } from "../format/output";
|
|
3
|
+
import type { RunRecord } from "../format/records";
|
|
4
|
+
import { checkSchema, describeProblems } from "../shared/validate";
|
|
5
|
+
import { type Context, loadDataset, workspaceOf } from "./command-context";
|
|
6
|
+
import { graderSpecs } from "./dataset";
|
|
7
|
+
import { incumbentOf, loopIdOf, readLoopFolders, readTries } from "./loop";
|
|
8
|
+
import { trialOutcome } from "./outcome";
|
|
9
|
+
import { barsOf, type CaseBar } from "./runs";
|
|
10
|
+
import { readRunRecord, readTrials, runIds, type StoredTrial, UnreadableRecord } from "./store";
|
|
11
|
+
import { plural } from "./text";
|
|
12
|
+
import { caseVerdict } from "./verdict";
|
|
13
|
+
import type { CaseWorkspace } from "./workspace";
|
|
14
|
+
|
|
15
|
+
type Listed =
|
|
16
|
+
| { at: string; run: CaseRuns["runs"][number] }
|
|
17
|
+
| { at: string; loop: CaseRuns["loops"][number] }
|
|
18
|
+
| { at: string; unread: CaseRuns["unread"][number]; dataset?: string };
|
|
19
|
+
|
|
20
|
+
/** `runs`: the store's runs and loops, newest first, and any run whose records don't read. */
|
|
21
|
+
export function runs(context: Context): number {
|
|
22
|
+
const workspace = workspaceOf(context);
|
|
23
|
+
const only = context.options.dataset;
|
|
24
|
+
const bars = barsBy(context, workspace);
|
|
25
|
+
const listed: Listed[] = [];
|
|
26
|
+
for (const id of runIds(workspace.store)) {
|
|
27
|
+
let run: RunRecord | undefined;
|
|
28
|
+
let trials: StoredTrial[] = [];
|
|
29
|
+
try {
|
|
30
|
+
run = readRunRecord(workspace.store, id);
|
|
31
|
+
if (run) trials = readTrials(workspace.store, id);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (!(error instanceof UnreadableRecord)) throw error;
|
|
34
|
+
// "{file} is not valid:" then its problems: the file's name and the first problem do.
|
|
35
|
+
const [head = "", first] = (error as Error).message.split("\n");
|
|
36
|
+
const why = `${basename(head.replace(/ is not valid:$/, ""))}${first ? `: ${first.trim()}` : ""}`;
|
|
37
|
+
if (!run || run.dataset === only || only === undefined) {
|
|
38
|
+
listed.push({ at: id, unread: { id, why }, ...(run ? { dataset: run.dataset } : {}) });
|
|
39
|
+
}
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
// A loop's runs are its report's.
|
|
43
|
+
if (!run || run.loop !== undefined) continue;
|
|
44
|
+
if (only !== undefined && run.dataset !== only) continue;
|
|
45
|
+
listed.push({ at: id, run: runOf(run, trials, bars(run.dataset)) });
|
|
46
|
+
}
|
|
47
|
+
for (const found of readLoopFolders(workspace.store)) {
|
|
48
|
+
if ("unread" in found) {
|
|
49
|
+
// A loop from before its entry, or broken: listed with why, never in the way.
|
|
50
|
+
if (only === undefined) {
|
|
51
|
+
const id = basename(found.dir);
|
|
52
|
+
listed.push({ at: id.slice(id.indexOf("-") + 1), unread: { id, why: found.unread } });
|
|
53
|
+
}
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const { dir, loop } = found;
|
|
57
|
+
if (only !== undefined && loop.dataset !== only) continue;
|
|
58
|
+
const tries = readTries(dir);
|
|
59
|
+
listed.push({
|
|
60
|
+
// A loop sorts among runs by when it started, as a run's id does.
|
|
61
|
+
at: loopIdOf(loop).slice(loop.name.length + 1),
|
|
62
|
+
loop: {
|
|
63
|
+
id: loopIdOf(loop),
|
|
64
|
+
name: loop.name,
|
|
65
|
+
dataset: loop.dataset,
|
|
66
|
+
started: loop.started,
|
|
67
|
+
tries: tries.length,
|
|
68
|
+
kept: incumbentOf(tries),
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
listed.sort((a, b) => (a.at < b.at ? 1 : a.at > b.at ? -1 : 0));
|
|
73
|
+
if (context.options.json) {
|
|
74
|
+
const json: CaseRuns = {
|
|
75
|
+
kind: RUNS_KIND,
|
|
76
|
+
runs: listed.flatMap((l) => ("run" in l ? [l.run] : [])),
|
|
77
|
+
loops: listed.flatMap((l) => ("loop" in l ? [l.loop] : [])),
|
|
78
|
+
unread: listed.flatMap((l) => ("unread" in l ? [l.unread] : [])),
|
|
79
|
+
};
|
|
80
|
+
const checked = checkSchema(CaseRunsSchema, json);
|
|
81
|
+
if (!checked.ok) throw new Error(describeProblems("runs --json", checked.problems));
|
|
82
|
+
context.stdout(JSON.stringify(json, null, 2));
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
const unread = listed.flatMap((l) => ("unread" in l ? [l.unread.id] : []));
|
|
86
|
+
const lines = listed.flatMap((l) => {
|
|
87
|
+
if ("unread" in l) return [];
|
|
88
|
+
if ("loop" in l) {
|
|
89
|
+
const { id, dataset, tries, kept } = l.loop;
|
|
90
|
+
return `${id} ${dataset} loop, ${tries} ${tries === 1 ? "try" : "tries"}, ${kept === 0 ? "keeps the start" : `keeps try ${kept}`}`;
|
|
91
|
+
}
|
|
92
|
+
const { id, dataset, cases, trials, columns, state, verdicts } = l.run;
|
|
93
|
+
const counted = (["passed", "failed", "flaky", "unclear", "none"] as const)
|
|
94
|
+
.filter((v) => (verdicts[v] ?? 0) > 0)
|
|
95
|
+
.map((v) => `${v === "none" ? "no verdict" : v} ${verdicts[v]}`);
|
|
96
|
+
return [
|
|
97
|
+
`${id} ${dataset} ${plural(cases, "case")}, ${plural(trials, "trial")}${columns > 1 ? `, ${columns} columns` : ""}`,
|
|
98
|
+
state === "broke" ? "the lab broke" : state,
|
|
99
|
+
...(counted.length > 0 ? [counted.join(" · ")] : []),
|
|
100
|
+
].join(" ");
|
|
101
|
+
});
|
|
102
|
+
// Nearly always an older lab's records: the count is enough here, --json names each and why.
|
|
103
|
+
if (unread.length > 0) {
|
|
104
|
+
lines.push(
|
|
105
|
+
`${unread.length === 1 ? "1 run or loop" : `${unread.length} runs or loops`} whose records don't read, left out (--json names them)`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
context.stdout(
|
|
109
|
+
lines.length > 0
|
|
110
|
+
? lines.join("\n")
|
|
111
|
+
: `no runs yet${only ? ` of ${only}` : ""}: awf-lab run makes one`,
|
|
112
|
+
);
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Each dataset's bars as its keys say now, read once; the default bars for one that doesn't read. */
|
|
117
|
+
function barsBy(
|
|
118
|
+
context: Context,
|
|
119
|
+
workspace: CaseWorkspace,
|
|
120
|
+
): (dataset: string) => ReadonlyMap<string, CaseBar> {
|
|
121
|
+
const read = new Map<string, ReadonlyMap<string, CaseBar>>();
|
|
122
|
+
return (dataset) => {
|
|
123
|
+
let bars = read.get(dataset);
|
|
124
|
+
if (!bars) {
|
|
125
|
+
try {
|
|
126
|
+
const { info, cases } = loadDataset(context, workspace, dataset, "all");
|
|
127
|
+
bars = barsOf(info, cases, graderSpecs(info));
|
|
128
|
+
} catch {
|
|
129
|
+
bars = new Map();
|
|
130
|
+
}
|
|
131
|
+
read.set(dataset, bars);
|
|
132
|
+
}
|
|
133
|
+
return bars;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function runOf(
|
|
138
|
+
run: RunRecord,
|
|
139
|
+
trials: readonly StoredTrial[],
|
|
140
|
+
bars: ReadonlyMap<string, CaseBar>,
|
|
141
|
+
): CaseRuns["runs"][number] {
|
|
142
|
+
const verdicts: CaseRuns["runs"][number]["verdicts"] = {};
|
|
143
|
+
for (const { case: name, trials: asked } of run.asked) {
|
|
144
|
+
const bar = bars.get(name);
|
|
145
|
+
const outcomes = trials
|
|
146
|
+
.filter((t) => t.record.case === name && t.record.column === 0)
|
|
147
|
+
.map((t) => trialOutcome(t.record, t.scores, bar?.deciders).outcome);
|
|
148
|
+
const verdict =
|
|
149
|
+
outcomes.length === 0
|
|
150
|
+
? "none"
|
|
151
|
+
: (caseVerdict(outcomes, asked, bar?.pass ?? "all").verdict ?? "none");
|
|
152
|
+
verdicts[verdict] = (verdicts[verdict] ?? 0) + 1;
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
id: run.id,
|
|
156
|
+
dataset: run.dataset,
|
|
157
|
+
cases: run.asked.length,
|
|
158
|
+
trials: run.asked.reduce((sum, a) => sum + a.trials, 0),
|
|
159
|
+
columns: run.columns.length,
|
|
160
|
+
state: run.broke ? "broke" : run.finished ? "finished" : "partial",
|
|
161
|
+
verdicts,
|
|
162
|
+
};
|
|
163
|
+
}
|
package/src/lab/runs.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { criteriaOf } from "../format/key";
|
|
2
|
+
import type { Params, RunRecord } from "../format/records";
|
|
3
|
+
import { columnLike } from "./columns";
|
|
4
|
+
import type { CaseInfo, DatasetInfo } from "./dataset";
|
|
5
|
+
import { decidersOf, type LabGrader } from "./grading";
|
|
6
|
+
import { type CaseRow, caseRow, trialOutcome } from "./outcome";
|
|
7
|
+
import { readableRuns, type StoredTrial } from "./store";
|
|
8
|
+
import { type CaseVerdict, caseVerdict } from "./verdict";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A case's bar, whether it is known failing, and whose grades decide its trials: all from its key
|
|
12
|
+
* and the dataset as they are now.
|
|
13
|
+
*/
|
|
14
|
+
export type CaseBar = {
|
|
15
|
+
pass: string;
|
|
16
|
+
knownFailing: boolean;
|
|
17
|
+
deciders?: ReadonlySet<string>;
|
|
18
|
+
tags: readonly string[];
|
|
19
|
+
/** Why no grader would grade it, from its key as it is now. */
|
|
20
|
+
ungraded?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** `graders`: every grader that may decide besides `expect`, the dataset's and a test's. */
|
|
24
|
+
export function barsOf(
|
|
25
|
+
dataset: DatasetInfo | undefined,
|
|
26
|
+
cases: readonly CaseInfo[],
|
|
27
|
+
graders?: readonly Pick<LabGrader, "name" | "manual">[],
|
|
28
|
+
): Map<string, CaseBar> {
|
|
29
|
+
return new Map(
|
|
30
|
+
cases.map((c) => [
|
|
31
|
+
c.name,
|
|
32
|
+
{
|
|
33
|
+
pass: c.key.pass ?? dataset?.grading.pass ?? "all",
|
|
34
|
+
knownFailing: c.key["known-failing"] === true,
|
|
35
|
+
tags: c.key.tags ?? [],
|
|
36
|
+
...(graders ? { deciders: decidersOf(c.key, graders) } : {}),
|
|
37
|
+
...ungradedOf(c, graders),
|
|
38
|
+
},
|
|
39
|
+
]),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Why no grader grades a case, when its key says: a draft, or nothing to grade against. */
|
|
44
|
+
function ungradedOf(
|
|
45
|
+
c: CaseInfo,
|
|
46
|
+
graders: readonly Pick<LabGrader, "name" | "manual">[] | undefined,
|
|
47
|
+
): { ungraded?: string } {
|
|
48
|
+
if (c.key.draft) return { ungraded: "the key is a draft" };
|
|
49
|
+
const empty = Object.keys(c.key.expect ?? {}).length === 0 && !c.key.must && !c.key["must-not"];
|
|
50
|
+
if (empty && (!graders || decidersOf(c.key, graders).size === 0)) {
|
|
51
|
+
return { ungraded: "key.md has no expect, must or must-not" };
|
|
52
|
+
}
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const asked = (run: RunRecord, name: string) => run.asked.find((a) => a.case === name)?.trials ?? 0;
|
|
57
|
+
|
|
58
|
+
/** Each case's verdict in one column of a run. */
|
|
59
|
+
function verdictsOf(
|
|
60
|
+
run: RunRecord,
|
|
61
|
+
trials: readonly StoredTrial[],
|
|
62
|
+
bars: ReadonlyMap<string, CaseBar>,
|
|
63
|
+
column: number,
|
|
64
|
+
): Map<string, CaseVerdict | undefined> {
|
|
65
|
+
const verdicts = new Map<string, CaseVerdict | undefined>();
|
|
66
|
+
for (const { case: name } of run.asked) {
|
|
67
|
+
const own = trials.filter((t) => t.record.case === name && t.record.column === column);
|
|
68
|
+
// A run that only reused trials adds nothing new: its verdict was given where they ran.
|
|
69
|
+
if (own.length === 0 || own.every((t) => t.record.reused)) continue;
|
|
70
|
+
const deciders = bars.get(name)?.deciders;
|
|
71
|
+
const outcomes = own.map((t) => trialOutcome(t.record, t.scores, deciders).outcome);
|
|
72
|
+
verdicts.set(
|
|
73
|
+
name,
|
|
74
|
+
caseVerdict(outcomes, asked(run, name), bars.get(name)?.pass ?? "all").verdict,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return verdicts;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Earlier runs of the same dataset with a column of this checkout under these parameters, oldest
|
|
82
|
+
* first, each with that column's verdicts. A run whose records this lab can't read is left out;
|
|
83
|
+
* `runs` names it.
|
|
84
|
+
*/
|
|
85
|
+
function earlier(
|
|
86
|
+
store: string,
|
|
87
|
+
dataset: string,
|
|
88
|
+
root: string,
|
|
89
|
+
params: Params,
|
|
90
|
+
/** Runs before this id; every run when absent. */
|
|
91
|
+
before: string | undefined,
|
|
92
|
+
bars: ReadonlyMap<string, CaseBar>,
|
|
93
|
+
): { run: RunRecord; verdicts: Map<string, CaseVerdict | undefined> }[] {
|
|
94
|
+
return [...readableRuns(store, { dataset, ...(before !== undefined ? { before } : {}) })].flatMap(
|
|
95
|
+
({ run, trials }) => {
|
|
96
|
+
const column = columnLike(run, root, params);
|
|
97
|
+
if (column === undefined) return [];
|
|
98
|
+
return [{ run, verdicts: verdictsOf(run, trials, bars, column) }];
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Up to five earlier verdicts of each case in the run's reference column, newest last. */
|
|
104
|
+
export function historyOf(
|
|
105
|
+
store: string,
|
|
106
|
+
run: RunRecord,
|
|
107
|
+
bars: ReadonlyMap<string, CaseBar>,
|
|
108
|
+
): Map<string, CaseVerdict[]> {
|
|
109
|
+
const history = new Map<string, CaseVerdict[]>();
|
|
110
|
+
const { path } = run.folders[0]!;
|
|
111
|
+
const { params } = run.columns[0]!;
|
|
112
|
+
for (const { verdicts } of earlier(store, run.dataset, path, params, run.id, bars)) {
|
|
113
|
+
for (const [name, verdict] of verdicts) {
|
|
114
|
+
if (!verdict) continue;
|
|
115
|
+
history.set(name, [...(history.get(name) ?? []), verdict].slice(-5));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return history;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* `--failed`: the cases whose newest verdict in the reference column (your checkout under these
|
|
123
|
+
* parameters) is failed, flaky or unclear, each with the trials of the run that gave it.
|
|
124
|
+
*/
|
|
125
|
+
export function failedCases(
|
|
126
|
+
store: string,
|
|
127
|
+
dataset: string,
|
|
128
|
+
root: string,
|
|
129
|
+
params: Params,
|
|
130
|
+
bars: ReadonlyMap<string, CaseBar>,
|
|
131
|
+
): Map<string, number> {
|
|
132
|
+
const newest = new Map<string, { verdict: CaseVerdict | undefined; trials: number }>();
|
|
133
|
+
const runs = earlier(store, dataset, root, params, undefined, bars);
|
|
134
|
+
for (const { run, verdicts } of runs) {
|
|
135
|
+
for (const [name, verdict] of verdicts) newest.set(name, { verdict, trials: asked(run, name) });
|
|
136
|
+
}
|
|
137
|
+
return new Map(
|
|
138
|
+
[...newest]
|
|
139
|
+
.filter(([, v]) => v.verdict === "failed" || v.verdict === "flaky" || v.verdict === "unclear")
|
|
140
|
+
.map(([name, v]) => [name, v.trials]),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** A run's rows for a column, the reference column by default, which alone has history. */
|
|
145
|
+
export function rowsOf(
|
|
146
|
+
store: string,
|
|
147
|
+
run: RunRecord,
|
|
148
|
+
trials: readonly StoredTrial[],
|
|
149
|
+
bars: ReadonlyMap<string, CaseBar>,
|
|
150
|
+
column = 0,
|
|
151
|
+
): CaseRow[] {
|
|
152
|
+
const history = column === 0 ? historyOf(store, run, bars) : new Map<string, CaseVerdict[]>();
|
|
153
|
+
return run.asked.map(({ case: name, trials: count }) => {
|
|
154
|
+
const bar = bars.get(name) ?? { pass: "all", knownFailing: false, tags: [] };
|
|
155
|
+
const own = trials.filter((t) => t.record.case === name && t.record.column === column);
|
|
156
|
+
return caseRow(
|
|
157
|
+
name,
|
|
158
|
+
own,
|
|
159
|
+
count,
|
|
160
|
+
bar.pass,
|
|
161
|
+
bar.knownFailing,
|
|
162
|
+
history.get(name) ?? [],
|
|
163
|
+
bar.deciders,
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function criteriaCount(cases: readonly CaseInfo[], names: readonly string[]): number {
|
|
169
|
+
return cases
|
|
170
|
+
.filter((c) => names.includes(c.name))
|
|
171
|
+
.reduce((n, c) => n + criteriaOf(c.key).length, 0);
|
|
172
|
+
}
|