@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,139 @@
|
|
|
1
|
+
// The few distributions a paired comparison needs, so the package takes no statistics dependency.
|
|
2
|
+
|
|
3
|
+
export const mean = (xs: readonly number[]) => xs.reduce((a, b) => a + b, 0) / xs.length;
|
|
4
|
+
|
|
5
|
+
/** The sample standard deviation; 0 below two values. */
|
|
6
|
+
export function sd(xs: readonly number[]): number {
|
|
7
|
+
if (xs.length < 2) return 0;
|
|
8
|
+
const m = mean(xs);
|
|
9
|
+
return Math.sqrt(xs.reduce((a, x) => a + (x - m) ** 2, 0) / (xs.length - 1));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Φ, to about 1e-7 (Abramowitz and Stegun 7.1.26). */
|
|
13
|
+
export function normalCdf(z: number): number {
|
|
14
|
+
const x = Math.abs(z) / Math.SQRT2;
|
|
15
|
+
const t = 1 / (1 + 0.3275911 * x);
|
|
16
|
+
const poly =
|
|
17
|
+
t *
|
|
18
|
+
(0.254829592 + t * (-0.284496736 + t * (1.421413741 + t * (-1.453152027 + t * 1.061405429))));
|
|
19
|
+
const erf = 1 - poly * Math.exp(-x * x);
|
|
20
|
+
return z >= 0 ? (1 + erf) / 2 : (1 - erf) / 2;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function logGamma(x: number): number {
|
|
24
|
+
const c = [
|
|
25
|
+
76.18009172947146, -86.5053203294168, 24.01409824083091, -1.231739572450155,
|
|
26
|
+
1.20865097386618e-3, -5.395239384953e-6,
|
|
27
|
+
];
|
|
28
|
+
let y = x;
|
|
29
|
+
const tmp = x + 5.5 - (x + 0.5) * Math.log(x + 5.5);
|
|
30
|
+
let series = 1.00000000019002;
|
|
31
|
+
for (const coefficient of c) series += coefficient / ++y;
|
|
32
|
+
return -tmp + Math.log((Math.sqrt(2 * Math.PI) * series) / x);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The regularized incomplete beta function, by its continued fraction (Numerical Recipes 6.4). */
|
|
36
|
+
function incompleteBeta(x: number, a: number, b: number): number {
|
|
37
|
+
if (x <= 0) return 0;
|
|
38
|
+
if (x >= 1) return 1;
|
|
39
|
+
const front = Math.exp(
|
|
40
|
+
logGamma(a + b) - logGamma(a) - logGamma(b) + a * Math.log(x) + b * Math.log(1 - x),
|
|
41
|
+
);
|
|
42
|
+
const fraction = (x: number, a: number, b: number) => {
|
|
43
|
+
const tiny = 1e-30;
|
|
44
|
+
let c = 1;
|
|
45
|
+
let d = 1 - ((a + b) * x) / (a + 1);
|
|
46
|
+
d = 1 / (Math.abs(d) < tiny ? tiny : d);
|
|
47
|
+
let h = d;
|
|
48
|
+
for (let m = 1; m <= 200; m++) {
|
|
49
|
+
for (const numerator of [
|
|
50
|
+
(m * (b - m) * x) / ((a + 2 * m - 1) * (a + 2 * m)),
|
|
51
|
+
(-(a + m) * (a + b + m) * x) / ((a + 2 * m) * (a + 2 * m + 1)),
|
|
52
|
+
]) {
|
|
53
|
+
d = 1 + numerator * d;
|
|
54
|
+
d = 1 / (Math.abs(d) < tiny ? tiny : d);
|
|
55
|
+
c = 1 + numerator / c;
|
|
56
|
+
if (Math.abs(c) < tiny) c = tiny;
|
|
57
|
+
h *= d * c;
|
|
58
|
+
}
|
|
59
|
+
if (Math.abs(d * c - 1) < 1e-12) break;
|
|
60
|
+
}
|
|
61
|
+
return h;
|
|
62
|
+
};
|
|
63
|
+
return x < (a + 1) / (a + b + 2)
|
|
64
|
+
? (front * fraction(x, a, b)) / a
|
|
65
|
+
: 1 - (front * fraction(1 - x, b, a)) / b;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Student's t CDF with `df` degrees of freedom. */
|
|
69
|
+
function tCdf(t: number, df: number): number {
|
|
70
|
+
const tail = incompleteBeta(df / (df + t * t), df / 2, 0.5) / 2;
|
|
71
|
+
return t >= 0 ? 1 - tail : tail;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The value below which Student's t with `df` degrees of freedom falls with probability `p`. */
|
|
75
|
+
export function tQuantile(p: number, df: number): number {
|
|
76
|
+
let low = -1;
|
|
77
|
+
let high = 1;
|
|
78
|
+
while (tCdf(low, df) > p) low *= 2;
|
|
79
|
+
while (tCdf(high, df) < p) high *= 2;
|
|
80
|
+
for (let i = 0; i < 200 && high - low > 1e-10 * Math.max(1, Math.abs(high)); i++) {
|
|
81
|
+
const middle = (low + high) / 2;
|
|
82
|
+
if (tCdf(middle, df) < p) low = middle;
|
|
83
|
+
else high = middle;
|
|
84
|
+
}
|
|
85
|
+
return (low + high) / 2;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const bounds = new Map<string, number>();
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The O'Brien–Fleming bound for looks at information fractions `fractions` (ascending, the last 1),
|
|
92
|
+
* one-sided at `alpha`: the constant `c` on the score scale, so look k claims at z ≥ c/√t_k. Found
|
|
93
|
+
* by numerical integration of a Brownian motion's crossing probability over a grid, which is how
|
|
94
|
+
* group-sequential software computes it (Armitage, McPherson and Rowe 1969).
|
|
95
|
+
*/
|
|
96
|
+
export function obrienFleming(fractions: readonly number[], alpha: number): number {
|
|
97
|
+
if (!fractions.every((f, i) => f > (fractions[i - 1] ?? 0) && f <= 1) || fractions.at(-1) !== 1) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`information fractions rise within (0, 1] and end at 1: ${fractions.join(", ")}`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
const key = `${fractions.join(",")}@${alpha}`;
|
|
103
|
+
const known = bounds.get(key);
|
|
104
|
+
if (known !== undefined) return known;
|
|
105
|
+
const step = 0.02;
|
|
106
|
+
const crossing = (c: number) => {
|
|
107
|
+
// The density of S_k at cell midpoints below c, having not crossed at any earlier look. The
|
|
108
|
+
// cells hang from c, so the crossing probability moves smoothly with it and bisection can
|
|
109
|
+
// find c between grid points.
|
|
110
|
+
const grid: number[] = [];
|
|
111
|
+
for (let s = c - step / 2; s > -8; s -= step) grid.push(s);
|
|
112
|
+
let density = grid.map((s) => normalDensity(s, fractions[0]!));
|
|
113
|
+
for (let k = 1; k < fractions.length; k++) {
|
|
114
|
+
const variance = fractions[k]! - fractions[k - 1]!;
|
|
115
|
+
density = grid.map((y) => {
|
|
116
|
+
let total = 0;
|
|
117
|
+
for (let i = 0; i < grid.length; i++) {
|
|
118
|
+
total += density[i]! * normalDensity(y - grid[i]!, variance);
|
|
119
|
+
}
|
|
120
|
+
return total * step;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return 1 - density.reduce((a, b) => a + b, 0) * step;
|
|
124
|
+
};
|
|
125
|
+
let lowC = 0.5;
|
|
126
|
+
let highC = 5;
|
|
127
|
+
for (let i = 0; i < 30; i++) {
|
|
128
|
+
const middle = (lowC + highC) / 2;
|
|
129
|
+
if (crossing(middle) > alpha) lowC = middle;
|
|
130
|
+
else highC = middle;
|
|
131
|
+
}
|
|
132
|
+
const c = (lowC + highC) / 2;
|
|
133
|
+
bounds.set(key, c);
|
|
134
|
+
return c;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function normalDensity(x: number, variance: number): number {
|
|
138
|
+
return Math.exp((-x * x) / (2 * variance)) / Math.sqrt(2 * Math.PI * variance);
|
|
139
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// How two columns' per-case numbers become a decision. Knows nothing of one workflow's domain: the
|
|
2
|
+
// caller names the metrics, and this reads only their numbers.
|
|
3
|
+
|
|
4
|
+
/** One number each trial gives. */
|
|
5
|
+
export type MetricSpec = {
|
|
6
|
+
name: string;
|
|
7
|
+
direction: "higher" | "lower";
|
|
8
|
+
/** What a trial whose workflow failed scores, or `missing` to leave it out. */
|
|
9
|
+
onFailure: number | "missing";
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* One trial's numbers. `workflow-failed` is a result, scored by each metric's `onFailure`;
|
|
14
|
+
* `missing` (never run, or not graded) is left out. A comparison should treat any outcome it
|
|
15
|
+
* doesn't know as `missing`: failures may later be told apart by kind.
|
|
16
|
+
*/
|
|
17
|
+
export type CaseScore = {
|
|
18
|
+
case: string;
|
|
19
|
+
trial: number;
|
|
20
|
+
outcome: "scored" | "workflow-failed" | "missing";
|
|
21
|
+
/** `null` where the metric doesn't apply, such as recall on a case with nothing to find. */
|
|
22
|
+
metrics: Readonly<Record<string, number | null>>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A metric as the comparison saw it: the column minus the reference, over the cases both have.
|
|
27
|
+
* `baseline` and `challenger` are the reference's and the column's means; records keep the names.
|
|
28
|
+
*/
|
|
29
|
+
export type ComparedMetric = {
|
|
30
|
+
name: string;
|
|
31
|
+
/** Why the rule looked at it. */
|
|
32
|
+
role: "primary" | "guard" | "tiebreak" | "reported";
|
|
33
|
+
/** Cases both columns have a value for; each case's value is the mean of its trials. */
|
|
34
|
+
cases: number;
|
|
35
|
+
baseline: number | null;
|
|
36
|
+
challenger: number | null;
|
|
37
|
+
difference: number | null;
|
|
38
|
+
/** The two-sided interval on the difference, at the comparison's confidence; absent below 5 cases. */
|
|
39
|
+
interval?: [number, number];
|
|
40
|
+
/** Cases where the column did better, the same, worse, by the metric's direction. */
|
|
41
|
+
won: number;
|
|
42
|
+
tied: number;
|
|
43
|
+
lost: number;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* What a comparison says about a column. `stop` says whether a run should go on with it:
|
|
48
|
+
* `undecided` with `stop` means the plan ran out without an answer, as when too few cases are won,
|
|
49
|
+
* or the rule stopped early because more cases could not give an answer worth having.
|
|
50
|
+
*/
|
|
51
|
+
export type Decision = {
|
|
52
|
+
decision: "better" | "worse" | "tie" | "undecided";
|
|
53
|
+
/** Whether a run should run no more trials of this pair. */
|
|
54
|
+
stop: boolean;
|
|
55
|
+
/** One sentence a person reads first. */
|
|
56
|
+
reason: string;
|
|
57
|
+
metrics: ComparedMetric[];
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type ComparisonInput = {
|
|
61
|
+
/** Every trial so far, per column, of the cases planned. */
|
|
62
|
+
reference: readonly CaseScore[];
|
|
63
|
+
column: readonly CaseScore[];
|
|
64
|
+
metrics: readonly MetricSpec[];
|
|
65
|
+
/**
|
|
66
|
+
* How many cases the plan runs in all, a count: the dataset's size, whose seeded order the
|
|
67
|
+
* cases come in. A rule's looks count towards it, and its last look is when all have run.
|
|
68
|
+
*/
|
|
69
|
+
planned: number;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** A rule over two columns' numbers, which `better.paired` wraps as a comparison export. */
|
|
73
|
+
export type Comparison = {
|
|
74
|
+
compare(input: ComparisonInput): Decision;
|
|
75
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Type from "typebox";
|
|
2
|
+
|
|
3
|
+
const Text = Type.String({ minLength: 1 });
|
|
4
|
+
|
|
5
|
+
/** An agent the lab starts itself, such as the criteria judge: its harness and model. */
|
|
6
|
+
export const AgentSchema = Type.Object(
|
|
7
|
+
{
|
|
8
|
+
harness: Text,
|
|
9
|
+
model: Text,
|
|
10
|
+
/** The criteria judge's voters per criterion, odd: 3 when absent. */
|
|
11
|
+
voters: Type.Optional(Type.Integer({ minimum: 1, maximum: 9, multipleOf: 1 })),
|
|
12
|
+
},
|
|
13
|
+
{ additionalProperties: false },
|
|
14
|
+
);
|
|
15
|
+
export type Agent = Type.Static<typeof AgentSchema>;
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { OUTPUT_RECORD_VERSION, type OutputRecord } from "@agentswf/contract/records";
|
|
4
|
+
|
|
5
|
+
/** An `awf` other than this checkout's: a folder's own installed bin, started in that folder. */
|
|
6
|
+
export type AwfBin = {
|
|
7
|
+
/** The bin's file, `node_modules/agentswf/src/awf.ts`. */
|
|
8
|
+
bin: string;
|
|
9
|
+
/** The working folder it starts in, so it hands the run to no other engine. */
|
|
10
|
+
dir: string;
|
|
11
|
+
/** A module `bun --preload` loads before the bin. */
|
|
12
|
+
preload?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type RunRequest = {
|
|
16
|
+
/** The workflow file. */
|
|
17
|
+
workflow: string;
|
|
18
|
+
/** Where the workflow works: `awf run --cwd`. */
|
|
19
|
+
cwd: string;
|
|
20
|
+
timeout: string;
|
|
21
|
+
/** The workflow's own arguments, after `--`. */
|
|
22
|
+
argv: readonly string[];
|
|
23
|
+
runRoot: string;
|
|
24
|
+
/**
|
|
25
|
+
* The run's id: `awf run --id`. The lab finds a run by its id alone, across workflows, so it gives
|
|
26
|
+
* each one an id unique among all of them.
|
|
27
|
+
*/
|
|
28
|
+
id: string;
|
|
29
|
+
/** A sandbox spec file every agent of the run is put in: `awf run --sandbox`. */
|
|
30
|
+
sandbox?: string;
|
|
31
|
+
/** Absent: this checkout's own engine, from the lab's working folder. */
|
|
32
|
+
awf?: AwfBin;
|
|
33
|
+
/**
|
|
34
|
+
* Added to the run's environment, over the lab's own less the calling session's; a name given
|
|
35
|
+
* undefined is unset, whatever the lab's own environment holds.
|
|
36
|
+
*/
|
|
37
|
+
env?: Readonly<Record<string, string | undefined>>;
|
|
38
|
+
/** `awf run --quiet`. */
|
|
39
|
+
quiet?: boolean;
|
|
40
|
+
/** `awf run --in`: the box the whole run goes in, its spec's file. */
|
|
41
|
+
in?: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** `awf run`'s record: never parked, as `awf run` refuses a workflow with events. */
|
|
45
|
+
export type RunOutput = Exclude<OutputRecord, { outcome: "parked" }>;
|
|
46
|
+
|
|
47
|
+
export type RunResult = {
|
|
48
|
+
exitCode: number;
|
|
49
|
+
record?: RunOutput;
|
|
50
|
+
/** A record was printed that this lab can't read: why. */
|
|
51
|
+
unread?: string;
|
|
52
|
+
stderr: string;
|
|
53
|
+
ms: number;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Runs one workflow as an operator would and returns the record it wrote: the one seam between
|
|
58
|
+
* awf-lab and the engine, so the run record stays the only interface (ADR 0002).
|
|
59
|
+
*/
|
|
60
|
+
export type Runner = (request: RunRequest) => Promise<RunResult>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The `awf` the lab runs when no folder gives its own: the bin of the `agentswf` installed beside
|
|
64
|
+
* the lab, a peer, or in this repository the workspace's. A process, not an import, so the
|
|
65
|
+
* engine's record stays the interface.
|
|
66
|
+
*/
|
|
67
|
+
export const AWF = awfBeside(import.meta.dir);
|
|
68
|
+
|
|
69
|
+
function awfBeside(from: string): string {
|
|
70
|
+
for (let at = from; ; at = dirname(at)) {
|
|
71
|
+
const manifest = join(at, "node_modules", "agentswf", "package.json");
|
|
72
|
+
if (existsSync(manifest)) {
|
|
73
|
+
const { bin } = JSON.parse(readFileSync(manifest, "utf8")) as {
|
|
74
|
+
bin?: string | Record<string, string>;
|
|
75
|
+
};
|
|
76
|
+
const awf = typeof bin === "string" ? bin : bin?.awf;
|
|
77
|
+
if (awf) return join(dirname(manifest), awf);
|
|
78
|
+
}
|
|
79
|
+
if (dirname(at) === at) return join(from, "../../../agentswf/src/awf.ts");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** `awf run`'s arguments for a request: the one argv both the process and a test's runner use. */
|
|
84
|
+
export function awfArgv(request: RunRequest): string[] {
|
|
85
|
+
return [
|
|
86
|
+
"run",
|
|
87
|
+
"--json",
|
|
88
|
+
...(request.quiet ? ["--quiet"] : []),
|
|
89
|
+
"--no-watch",
|
|
90
|
+
"--timeout",
|
|
91
|
+
request.timeout,
|
|
92
|
+
"--run-root",
|
|
93
|
+
request.runRoot,
|
|
94
|
+
"--id",
|
|
95
|
+
request.id,
|
|
96
|
+
"--cwd",
|
|
97
|
+
request.cwd,
|
|
98
|
+
...(request.sandbox ? ["--sandbox", request.sandbox] : []),
|
|
99
|
+
...(request.in ? ["--in", request.in] : []),
|
|
100
|
+
request.workflow,
|
|
101
|
+
"--",
|
|
102
|
+
...request.argv,
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The command that starts an `awf`: as `awf` runs itself, never reading a `.env` where it starts,
|
|
108
|
+
* which could change how every agent logs in.
|
|
109
|
+
*/
|
|
110
|
+
export function awfCommand(awf: AwfBin | undefined, args: readonly string[]): string[] {
|
|
111
|
+
return [
|
|
112
|
+
process.execPath,
|
|
113
|
+
"--no-env-file",
|
|
114
|
+
...(awf?.preload ? ["--preload", awf.preload] : []),
|
|
115
|
+
awf?.bin ?? AWF,
|
|
116
|
+
...args,
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The lab's environment, less the calling session, with `extra` over it. */
|
|
121
|
+
export function runEnvironment(
|
|
122
|
+
extra: Readonly<Record<string, string | undefined>> = {},
|
|
123
|
+
base: Readonly<Record<string, string | undefined>> = process.env,
|
|
124
|
+
): Record<string, string> {
|
|
125
|
+
return Object.fromEntries(
|
|
126
|
+
Object.entries({ ...withoutCallingSession(base), ...extra }).filter(
|
|
127
|
+
(entry): entry is [string, string] => entry[1] !== undefined,
|
|
128
|
+
),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Starts an `awf` and waits for it; stdout and stderr whole. */
|
|
133
|
+
export async function spawnAwf(
|
|
134
|
+
argv: readonly string[],
|
|
135
|
+
options: { cwd?: string; env?: Readonly<Record<string, string | undefined>> } = {},
|
|
136
|
+
): Promise<{ exitCode: number; stdout: string; stderr: string; ms: number }> {
|
|
137
|
+
const started = Date.now();
|
|
138
|
+
const child = Bun.spawn([...argv], {
|
|
139
|
+
stdout: "pipe",
|
|
140
|
+
stderr: "pipe",
|
|
141
|
+
stdin: "ignore",
|
|
142
|
+
...(options.cwd ? { cwd: options.cwd } : {}),
|
|
143
|
+
env: options.env ?? runEnvironment(),
|
|
144
|
+
});
|
|
145
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
146
|
+
new Response(child.stdout).text(),
|
|
147
|
+
new Response(child.stderr).text(),
|
|
148
|
+
child.exited,
|
|
149
|
+
]);
|
|
150
|
+
return { exitCode, stdout, stderr, ms: Date.now() - started };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The runner a plain request starts with: this checkout's `awf`, or a folder's. */
|
|
154
|
+
export function processRunner(): Runner {
|
|
155
|
+
return async (request) => {
|
|
156
|
+
const { exitCode, stdout, stderr, ms } = await spawnAwf(
|
|
157
|
+
awfCommand(request.awf, awfArgv(request)),
|
|
158
|
+
{
|
|
159
|
+
...(request.awf ? { cwd: request.awf.dir } : {}),
|
|
160
|
+
env: runEnvironment(request.env),
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
return { exitCode, stderr, ms, ...parseRecord(stdout) };
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Each harness's session variable, its spec's `sessionEnv`: a case's run started with one would
|
|
169
|
+
* find the operator's session and let its workflow fork it.
|
|
170
|
+
*/
|
|
171
|
+
const SESSION_VARIABLES = [
|
|
172
|
+
"CLAUDE_CODE_SESSION_ID",
|
|
173
|
+
"CODEX_SESSION_ID",
|
|
174
|
+
"PI_SESSION_ID",
|
|
175
|
+
"CURSOR_CONVERSATION_ID",
|
|
176
|
+
];
|
|
177
|
+
|
|
178
|
+
/** `environment` without the session `awf-lab` was started from, if it was started from one. */
|
|
179
|
+
export function withoutCallingSession(
|
|
180
|
+
environment: Readonly<Record<string, string | undefined>>,
|
|
181
|
+
): Record<string, string | undefined> {
|
|
182
|
+
return Object.fromEntries(
|
|
183
|
+
Object.entries(environment).filter(([name]) => !SESSION_VARIABLES.includes(name)),
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* `awf run --json` prints the record, success or not; a run that never started, a refusal. A
|
|
189
|
+
* record this lab can't read, of another version or parked, is `unread`, saying why: the lab's to
|
|
190
|
+
* report, never a run to grade.
|
|
191
|
+
*/
|
|
192
|
+
function parseRecord(stdout: string): { record?: RunOutput; unread?: string } {
|
|
193
|
+
const text = stdout.trim();
|
|
194
|
+
if (!text.startsWith("{")) return {};
|
|
195
|
+
let record: OutputRecord;
|
|
196
|
+
try {
|
|
197
|
+
record = JSON.parse(text) as OutputRecord;
|
|
198
|
+
} catch {
|
|
199
|
+
return {};
|
|
200
|
+
}
|
|
201
|
+
if (typeof record !== "object" || record === null || !("version" in record)) return {};
|
|
202
|
+
if (record.version !== OUTPUT_RECORD_VERSION) {
|
|
203
|
+
return {
|
|
204
|
+
unread: `awf printed a run record of version ${record.version}; this lab reads version ${OUTPUT_RECORD_VERSION}`,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
if (record.outcome === "parked") return { unread: "awf printed a parked run's record" };
|
|
208
|
+
return { record };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Claude's and codex's say `subscription authentication`, cursor's `Cursor authentication`.
|
|
212
|
+
const LOGIN_REFUSED = /authentication is required/;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* awf checks a harness's login as its first agent opens, inside the run: refused there, with no
|
|
216
|
+
* agent opened, the run says nothing about the workflow, as one awf refused at the start.
|
|
217
|
+
*/
|
|
218
|
+
export function loginRefused(record: RunOutput): boolean {
|
|
219
|
+
return (
|
|
220
|
+
record.outcome !== "completed" &&
|
|
221
|
+
record.accounting.totals.agents === 0 &&
|
|
222
|
+
LOGIN_REFUSED.test(record.reason)
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** More events than any trial's run holds: `awf logs` without `--after` gives the newest this many. */
|
|
227
|
+
const ALL_EVENTS = 10_000_000;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Everything `awf logs --json` has for a run, as the text it printed: what the marker search reads.
|
|
231
|
+
* One page without `--after` is the newest `--limit` events, and says when older ones were left
|
|
232
|
+
* out; anything short of the whole run is `unread`, saying why.
|
|
233
|
+
*/
|
|
234
|
+
export async function awfLogs(
|
|
235
|
+
awf: AwfBin | undefined,
|
|
236
|
+
runRoot: string,
|
|
237
|
+
ref: string,
|
|
238
|
+
env: Readonly<Record<string, string | undefined>> = {},
|
|
239
|
+
): Promise<{ text: string } | { unread: string }> {
|
|
240
|
+
const { stdout, stderr, exitCode } = await spawnAwf(
|
|
241
|
+
awfCommand(awf, ["logs", ref, "--json", "--run-root", runRoot, "--limit", String(ALL_EVENTS)]),
|
|
242
|
+
{ ...(awf ? { cwd: awf.dir } : {}), env: runEnvironment(env) },
|
|
243
|
+
);
|
|
244
|
+
let page: { v?: unknown; events?: unknown; more?: { older?: boolean } };
|
|
245
|
+
try {
|
|
246
|
+
page = JSON.parse(stdout);
|
|
247
|
+
} catch {
|
|
248
|
+
const said = stderr.trim().split("\n").at(-1) ?? "";
|
|
249
|
+
return {
|
|
250
|
+
unread: `awf logs ${ref} printed no page (exit ${exitCode})${said ? `: ${said}` : ""}`,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
if (exitCode !== 0 || !Array.isArray(page.events)) {
|
|
254
|
+
return { unread: `awf logs ${ref} refused: ${stdout.trim().slice(0, 300)}` };
|
|
255
|
+
}
|
|
256
|
+
if (page.more?.older) return { unread: `awf logs ${ref} left out the run's oldest events` };
|
|
257
|
+
return { text: stdout };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* What a run's folder says it is estimated at, from `awf runs {ref} --json`: for a run that
|
|
262
|
+
* printed no record, as one cut short does, though its agents may have run. Read from
|
|
263
|
+
* `run.estimate` whatever the page's version, since every version so far keeps it there; it is
|
|
264
|
+
* called after the run, so it never throws. Undefined when the folder can't be read.
|
|
265
|
+
*/
|
|
266
|
+
export async function awfRunEstimate(
|
|
267
|
+
awf: AwfBin | undefined,
|
|
268
|
+
runRoot: string,
|
|
269
|
+
ref: string,
|
|
270
|
+
env: Readonly<Record<string, string | undefined>> = {},
|
|
271
|
+
): Promise<number | undefined> {
|
|
272
|
+
const { stdout, exitCode } = await spawnAwf(
|
|
273
|
+
awfCommand(awf, ["runs", ref, "--json", "--run-root", runRoot]),
|
|
274
|
+
{ ...(awf ? { cwd: awf.dir } : {}), env: runEnvironment(env) },
|
|
275
|
+
);
|
|
276
|
+
if (exitCode !== 0) return undefined;
|
|
277
|
+
let page: { run?: { estimate?: unknown } } | null;
|
|
278
|
+
try {
|
|
279
|
+
page = JSON.parse(stdout);
|
|
280
|
+
} catch {
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
const estimate = page?.run?.estimate;
|
|
284
|
+
return typeof estimate === "number" && Number.isFinite(estimate) ? estimate : undefined;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Whether `awf run` printed the record version this lab reads: the folder's own `awf`, started
|
|
289
|
+
* once in `dir` on a workflow that does nothing, before the first trial. The version it printed,
|
|
290
|
+
* or why it printed none.
|
|
291
|
+
*/
|
|
292
|
+
export async function probeRecordVersion(
|
|
293
|
+
awf: AwfBin,
|
|
294
|
+
workflow: string,
|
|
295
|
+
dir: string,
|
|
296
|
+
): Promise<{ version: unknown } | { unread: string }> {
|
|
297
|
+
const { stdout, stderr, exitCode } = await spawnAwf(
|
|
298
|
+
awfCommand({ bin: awf.bin, dir: awf.dir }, [
|
|
299
|
+
"run",
|
|
300
|
+
"--json",
|
|
301
|
+
"--quiet",
|
|
302
|
+
"--no-watch",
|
|
303
|
+
"--timeout",
|
|
304
|
+
"60s",
|
|
305
|
+
"--run-root",
|
|
306
|
+
join(dir, "runs"),
|
|
307
|
+
"--cwd",
|
|
308
|
+
dir,
|
|
309
|
+
workflow,
|
|
310
|
+
]),
|
|
311
|
+
{ cwd: awf.dir, env: runEnvironment() },
|
|
312
|
+
);
|
|
313
|
+
try {
|
|
314
|
+
const record = JSON.parse(stdout.trim()) as { version?: unknown };
|
|
315
|
+
if (typeof record === "object" && record !== null && "version" in record) {
|
|
316
|
+
return { version: record.version };
|
|
317
|
+
}
|
|
318
|
+
} catch {}
|
|
319
|
+
const said = (stderr.trim() || stdout.trim()).split("\n").slice(-3).join(" ");
|
|
320
|
+
return { unread: `its awf run printed no record (exit ${exitCode})${said ? `: ${said}` : ""}` };
|
|
321
|
+
}
|