@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.
Files changed (125) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -2
  3. package/package.json +46 -4
  4. package/schema/case-check.schema.json +64 -0
  5. package/schema/case-config.schema.json +187 -0
  6. package/schema/case-final.schema.json +48 -0
  7. package/schema/case-good-record.schema.json +70 -0
  8. package/schema/case-grade.schema.json +49 -0
  9. package/schema/case-key.schema.json +117 -0
  10. package/schema/case-label.schema.json +32 -0
  11. package/schema/case-loop-report.schema.json +210 -0
  12. package/schema/case-loop.schema.json +141 -0
  13. package/schema/case-plan.schema.json +117 -0
  14. package/schema/case-previous.schema.json +70 -0
  15. package/schema/case-proposal.schema.json +52 -0
  16. package/schema/case-report.schema.json +892 -0
  17. package/schema/case-run.schema.json +253 -0
  18. package/schema/case-runs.schema.json +125 -0
  19. package/schema/case-score.schema.json +319 -0
  20. package/schema/case-spread.schema.json +110 -0
  21. package/schema/case-trial.schema.json +543 -0
  22. package/schema/case-try.schema.json +113 -0
  23. package/schema/local.json +155 -0
  24. package/src/cli.ts +9 -0
  25. package/src/env/capture.ts +48 -0
  26. package/src/env/compare.ts +201 -0
  27. package/src/env/facts.ts +51 -0
  28. package/src/env/fetch.ts +57 -0
  29. package/src/env/files.ts +228 -0
  30. package/src/env/grade-case.ts +80 -0
  31. package/src/env/grade-helper.ts +352 -0
  32. package/src/env/grader.ts +128 -0
  33. package/src/env/index.ts +727 -0
  34. package/src/env/mock.ts +374 -0
  35. package/src/env/routes.ts +90 -0
  36. package/src/env/sources.ts +19 -0
  37. package/src/env/state.ts +79 -0
  38. package/src/env/trial.ts +182 -0
  39. package/src/format/key.ts +144 -0
  40. package/src/format/loop.ts +178 -0
  41. package/src/format/output.ts +481 -0
  42. package/src/format/records.ts +440 -0
  43. package/src/format/schema-files.ts +64 -0
  44. package/src/format/settings.ts +552 -0
  45. package/src/format/trial-env.ts +64 -0
  46. package/src/format/trial-file.ts +134 -0
  47. package/src/help/awf-lab.txt +29 -0
  48. package/src/help/case.txt +38 -0
  49. package/src/help/config.txt +14 -0
  50. package/src/help/label.txt +12 -0
  51. package/src/help/loop.txt +44 -0
  52. package/src/help/report.txt +20 -0
  53. package/src/help/run.txt +60 -0
  54. package/src/help/runs.txt +10 -0
  55. package/src/help/score.txt +27 -0
  56. package/src/judge/criteria.workflow.ts +237 -0
  57. package/src/judge/version.ts +6 -0
  58. package/src/lab/answer.ts +47 -0
  59. package/src/lab/blame.ts +204 -0
  60. package/src/lab/box.ts +249 -0
  61. package/src/lab/capture-command.ts +298 -0
  62. package/src/lab/capture.ts +234 -0
  63. package/src/lab/check-command.ts +187 -0
  64. package/src/lab/cli.ts +324 -0
  65. package/src/lab/code-graders.ts +108 -0
  66. package/src/lab/columns.ts +138 -0
  67. package/src/lab/command-context.ts +436 -0
  68. package/src/lab/compare.ts +123 -0
  69. package/src/lab/config-command.ts +278 -0
  70. package/src/lab/criteria.ts +69 -0
  71. package/src/lab/dataset.ts +356 -0
  72. package/src/lab/eval-files.ts +185 -0
  73. package/src/lab/expect.ts +79 -0
  74. package/src/lab/facts.ts +59 -0
  75. package/src/lab/families.ts +32 -0
  76. package/src/lab/folders.ts +153 -0
  77. package/src/lab/git.ts +128 -0
  78. package/src/lab/good.ts +224 -0
  79. package/src/lab/grading-stub.txt +8 -0
  80. package/src/lab/grading.ts +332 -0
  81. package/src/lab/inspect.ts +226 -0
  82. package/src/lab/labels.ts +206 -0
  83. package/src/lab/loop-code.ts +255 -0
  84. package/src/lab/loop-command.ts +770 -0
  85. package/src/lab/loop-records.ts +144 -0
  86. package/src/lab/loop.ts +859 -0
  87. package/src/lab/marker.ts +23 -0
  88. package/src/lab/outcome.ts +196 -0
  89. package/src/lab/pooled.ts +106 -0
  90. package/src/lab/project-env.ts +39 -0
  91. package/src/lab/rates.ts +150 -0
  92. package/src/lab/reach.ts +111 -0
  93. package/src/lab/record-copies.ts +202 -0
  94. package/src/lab/refusal.ts +2 -0
  95. package/src/lab/report-command.ts +156 -0
  96. package/src/lab/report.ts +838 -0
  97. package/src/lab/reuse.ts +126 -0
  98. package/src/lab/run-command.ts +853 -0
  99. package/src/lab/run-logs.ts +89 -0
  100. package/src/lab/run-report.ts +259 -0
  101. package/src/lab/runs-command.ts +163 -0
  102. package/src/lab/runs.ts +172 -0
  103. package/src/lab/scaffold.ts +91 -0
  104. package/src/lab/score-command.ts +189 -0
  105. package/src/lab/score.ts +396 -0
  106. package/src/lab/store.ts +383 -0
  107. package/src/lab/text.ts +29 -0
  108. package/src/lab/trial-settings.ts +100 -0
  109. package/src/lab/trial-stub.txt +18 -0
  110. package/src/lab/trial.ts +443 -0
  111. package/src/lab/usage.ts +273 -0
  112. package/src/lab/verdict.ts +161 -0
  113. package/src/lab/workflow-graders.ts +161 -0
  114. package/src/lab/workspace.ts +489 -0
  115. package/src/loop/propose.workflow.ts +84 -0
  116. package/src/shared/allowance.ts +185 -0
  117. package/src/shared/compare/index.ts +3 -0
  118. package/src/shared/compare/paired.ts +397 -0
  119. package/src/shared/compare/stats.ts +139 -0
  120. package/src/shared/compare/types.ts +75 -0
  121. package/src/shared/config.ts +15 -0
  122. package/src/shared/runner.ts +321 -0
  123. package/src/shared/validate.ts +181 -0
  124. package/src/shared/version.ts +41 -0
  125. package/src/write-schemas.ts +10 -0
@@ -0,0 +1,552 @@
1
+ import type { TurnRecord } from "@agentswf/contract/records";
2
+ import Type from "typebox";
3
+ import { SEMVER } from "../shared/version";
4
+
5
+ // The files a person writes: `trial.ts`, `grading.ts`, `awf-lab.config.ts` and a case's `key.md`,
6
+ // with no format marker (story 044, principle 8), and the one JSON file, `awf-lab.local.json`,
7
+ // checked by its `$schema`. The TypeScript files are loaded in a helper process in your checkout,
8
+ // which hands their values over as JSON: a function as `{ "$function": true }`, a `required()`
9
+ // parameter as `{ "$required": true }`, and each grader as what made it.
10
+
11
+ const Text = Type.String({ minLength: 1 });
12
+
13
+ const DURATION_PATTERN = "^[1-9][0-9]*(ms|s|m|h)$";
14
+ const PASS_PATTERN = "^(all|[1-9][0-9]*/[1-9][0-9]*)$";
15
+ const PARAM_PATTERN = "^[A-Za-z][A-Za-z0-9_-]*$";
16
+ const NAME_PATTERN = "^[A-Za-z0-9][A-Za-z0-9._-]*$";
17
+ const GRADER_NAME_PATTERN = "^[A-Za-z0-9][A-Za-z0-9_-]*$";
18
+ const AGENT_PATTERN = "^[^/\\s]+/[^/\\s]+$";
19
+ const TAG_PATTERN = "^[a-z0-9][a-z0-9-]*$";
20
+
21
+ /** A declared version: semver, with an optional pre-release tag and build part. */
22
+ export const VersionSchema = Type.String({
23
+ pattern: SEMVER.source,
24
+ description:
25
+ "Semver, such as 3.2.0, 3.3.0-tone-rubric or 3.2.1+typo: matched on its major, minor and pre-release tag.",
26
+ });
27
+
28
+ /** `"30m"`, `"90s"`, `"2h"`: a duration as `awf run --timeout` takes it. */
29
+ export const DurationSchema = Type.String({ pattern: DURATION_PATTERN });
30
+
31
+ /** `"all"`, or a fraction of a case's trials that must pass, such as `"2/3"`. */
32
+ export const PassSchema = Type.String({
33
+ pattern: PASS_PATTERN,
34
+ description: '"all", or the fraction of trials that must pass, such as "2/3".',
35
+ });
36
+
37
+ export { type Agent, AgentSchema } from "../shared/config";
38
+
39
+ /** A domain as a sandbox spec names it: `example.com`, or `*.example.com` for its subdomains. */
40
+ const DomainSchema = Type.String({ pattern: "^(\\*\\.)?[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$" });
41
+
42
+ /** Where a trial may go, each closed when absent. */
43
+ export const NetworkSchema = Type.Object(
44
+ {
45
+ agents: Type.Optional(
46
+ Type.Array(DomainSchema, {
47
+ description:
48
+ "Domains an agent's own commands may reach besides its model's; none when absent.",
49
+ }),
50
+ ),
51
+ code: Type.Optional(
52
+ Type.Array(DomainSchema, {
53
+ description:
54
+ "Hosts the workflow's own code may fetch unfaked, beyond the engine's; none when absent.",
55
+ }),
56
+ ),
57
+ webSearch: Type.Optional(
58
+ Type.Boolean({ description: "Whether agents keep their web search; false when absent." }),
59
+ ),
60
+ sockets: Type.Optional(
61
+ Type.Array(Text, {
62
+ description:
63
+ "Host unix sockets an agent may connect to, each a socket or a folder of them; none when absent.",
64
+ }),
65
+ ),
66
+ },
67
+ { additionalProperties: false },
68
+ );
69
+
70
+ /** `trial.ts`'s `sandbox`: who isolates a trial, with what, and where it may go. */
71
+ export const SandboxSchema = Type.Object(
72
+ {
73
+ by: Type.Optional(
74
+ Type.Union([Type.Literal("lab"), Type.Literal("workflow")], {
75
+ description:
76
+ '"lab", the default: the whole trial in the lab\'s sandbox, reading no key. "workflow": the workflow\'s own sandboxes, its network checked after each trial.',
77
+ }),
78
+ ),
79
+ provider: Type.Optional(
80
+ Type.Literal("srt", {
81
+ description: 'The lab\'s sandbox, "srt" by default; used only with by: "lab".',
82
+ }),
83
+ ),
84
+ network: Type.Optional(NetworkSchema),
85
+ },
86
+ { additionalProperties: false },
87
+ );
88
+ export type Sandbox = Type.Static<typeof SandboxSchema>;
89
+
90
+ export const LIMIT_PHASES = ["setup", "run", "after"] as const;
91
+ export type LimitPhase = (typeof LIMIT_PHASES)[number];
92
+
93
+ /** `trial.ts`'s `limits`: each phase's, as a duration. */
94
+ export const LimitsSchema = Type.Object(
95
+ {
96
+ setup: Type.Optional({ ...DurationSchema, description: 'setup\'s limit; "10m" when absent.' }),
97
+ run: Type.Optional({
98
+ ...DurationSchema,
99
+ description: 'The workflow\'s limit; "30m" when absent.',
100
+ }),
101
+ after: Type.Optional({
102
+ ...DurationSchema,
103
+ description: "evidence's and cleanup's limit; \"10m\" when absent.",
104
+ }),
105
+ },
106
+ { additionalProperties: false },
107
+ );
108
+ export type Limits = Type.Static<typeof LimitsSchema>;
109
+
110
+ /** A trial's network, resolved: every part given. */
111
+ const ResolvedNetworkSchema = Type.Object(
112
+ {
113
+ agents: Type.Array(DomainSchema),
114
+ code: Type.Array(DomainSchema),
115
+ webSearch: Type.Boolean(),
116
+ sockets: Type.Array(Text),
117
+ },
118
+ { additionalProperties: false },
119
+ );
120
+ export type ResolvedNetwork = Type.Static<typeof ResolvedNetworkSchema>;
121
+
122
+ /** The sandbox a trial ran under, resolved: `provider` only with `by: "lab"`. */
123
+ export const ResolvedSandboxSchema = Type.Union([
124
+ Type.Object(
125
+ { by: Type.Literal("lab"), provider: Type.Literal("srt"), network: ResolvedNetworkSchema },
126
+ { additionalProperties: false },
127
+ ),
128
+ Type.Object(
129
+ { by: Type.Literal("workflow"), network: ResolvedNetworkSchema },
130
+ { additionalProperties: false },
131
+ ),
132
+ ]);
133
+ export type ResolvedSandbox = Type.Static<typeof ResolvedSandboxSchema>;
134
+
135
+ /** The limits a trial ran under, resolved: every phase's, `--limit` over `trial.ts` over the defaults. */
136
+ export const ResolvedLimitsSchema = Type.Object(
137
+ { setup: DurationSchema, run: DurationSchema, after: DurationSchema },
138
+ { additionalProperties: false },
139
+ );
140
+ export type ResolvedLimits = Type.Static<typeof ResolvedLimitsSchema>;
141
+
142
+ /** A function of a TypeScript file, as the helper hands it over. */
143
+ const FunctionSchema = Type.Object(
144
+ { $function: Type.Literal(true) },
145
+ { additionalProperties: false, description: "a function" },
146
+ );
147
+
148
+ /** A dataset's or eval folder's name, and a case's. */
149
+ export const NameSchema = Type.String({ pattern: NAME_PATTERN });
150
+
151
+ /** `trial.ts`'s `defineTrial`: everything that shapes a trial. */
152
+ export const TrialFileSchema = Type.Object(
153
+ {
154
+ /** As declared, or as its function returned it in this checkout. */
155
+ version: VersionSchema,
156
+ workflow: FunctionSchema,
157
+ setup: FunctionSchema,
158
+ params: Type.Optional(
159
+ Type.Record(
160
+ Type.String({ pattern: PARAM_PATTERN }),
161
+ Type.Union([
162
+ Type.String({ minLength: 1 }),
163
+ Type.Object({ $required: Type.Literal(true) }, { additionalProperties: false }),
164
+ ]),
165
+ {
166
+ description:
167
+ "Every parameter a trial takes, each with its default, or required() for none.",
168
+ },
169
+ ),
170
+ ),
171
+ sandbox: Type.Optional(SandboxSchema),
172
+ limits: Type.Optional(LimitsSchema),
173
+ },
174
+ { additionalProperties: false },
175
+ );
176
+
177
+ export type TrialFile = Type.Static<typeof TrialFileSchema>;
178
+
179
+ /** What a grader reads: the facts alone, or the trial's record and saved files too. */
180
+ const Reads = Type.Union([Type.Literal("facts"), Type.Literal("trial")]);
181
+ const GraderName = Type.String({ pattern: GRADER_NAME_PATTERN });
182
+
183
+ /** A grader of `grading.ts`, as what made it says: `defineGrader`, `workflowGrader` or `sentences`. */
184
+ export const GraderEntrySchema = Type.Union([
185
+ Type.Object(
186
+ {
187
+ grader: Type.Literal("code"),
188
+ name: GraderName,
189
+ version: VersionSchema,
190
+ reads: Reads,
191
+ manual: Type.Boolean(),
192
+ },
193
+ { additionalProperties: false },
194
+ ),
195
+ Type.Object(
196
+ {
197
+ grader: Type.Literal("workflow"),
198
+ name: GraderName,
199
+ version: VersionSchema,
200
+ /** The workflow's file, as its import resolved. */
201
+ file: Text,
202
+ reads: Reads,
203
+ manual: Type.Boolean(),
204
+ timeout: Type.Optional(DurationSchema),
205
+ },
206
+ { additionalProperties: false },
207
+ ),
208
+ Type.Object(
209
+ {
210
+ grader: Type.Literal("sentences"),
211
+ name: GraderName,
212
+ version: VersionSchema,
213
+ /** `{harness}/{model}`. */
214
+ agent: Type.String({ pattern: AGENT_PATTERN }),
215
+ voters: Type.Optional(Type.Integer({ minimum: 1, maximum: 9 })),
216
+ manual: Type.Boolean(),
217
+ },
218
+ { additionalProperties: false },
219
+ ),
220
+ ]);
221
+
222
+ export type GraderEntry = Type.Static<typeof GraderEntrySchema>;
223
+
224
+ /** `grading.ts`'s `defineGrading`: everything that decides a grade. */
225
+ export const GradingFileSchema = Type.Object(
226
+ {
227
+ /** Turns a trial record into the flat facts the key's `expect` checks and graders read. */
228
+ facts: Type.Optional(FunctionSchema),
229
+ graders: Type.Optional(Type.Array(GraderEntrySchema)),
230
+ pass: Type.Optional(PassSchema),
231
+ compare: Type.Optional(Type.Object({}, { additionalProperties: false })),
232
+ },
233
+ { additionalProperties: false },
234
+ );
235
+
236
+ export type GradingFile = Type.Static<typeof GradingFileSchema>;
237
+
238
+ /** A dataset named in the config or the local file: an eval folder and a data folder. */
239
+ export const DatasetEntrySchema = Type.Object(
240
+ {
241
+ eval: Type.Optional(
242
+ Type.String({
243
+ minLength: 1,
244
+ description:
245
+ "The eval folder, holding trial.ts and grading.ts, relative to evals/; the folder of the dataset's name when absent.",
246
+ }),
247
+ ),
248
+ data: Type.Optional(
249
+ Type.String({
250
+ minLength: 1,
251
+ description:
252
+ "The data folder, holding cases/ and optionally shared/, relative to evals/; the folder of the dataset's name when absent.",
253
+ }),
254
+ ),
255
+ params: Type.Optional(
256
+ Type.Record(Type.String({ pattern: PARAM_PATTERN }), Type.String({ minLength: 1 }), {
257
+ description: "Values over trial.ts's defaults, each a parameter it declares.",
258
+ }),
259
+ ),
260
+ records: Type.Optional(
261
+ Type.String({
262
+ minLength: 1,
263
+ description:
264
+ "A folder, usually in the data's repository, holding a copy of the dataset's records under {records}/{the project's first commit}/, so another clone reuses its trials.",
265
+ }),
266
+ ),
267
+ },
268
+ { additionalProperties: false },
269
+ );
270
+
271
+ export type DatasetEntry = Type.Static<typeof DatasetEntrySchema>;
272
+
273
+ const Datasets = Type.Record(Type.String({ pattern: NAME_PATTERN }), DatasetEntrySchema);
274
+
275
+ const GLOB_DESCRIPTION = "relative to the project, as git shows paths";
276
+
277
+ /**
278
+ * A loop named in the config or the local file: what defines it. Every field may be left out of
279
+ * one file, and a local entry adds to the config's of its name, field by field; once merged, a
280
+ * loop needs `dataset`, `brief` and `edit.allow`.
281
+ */
282
+ export const LoopEntrySchema = Type.Object(
283
+ {
284
+ dataset: Type.Optional(
285
+ Type.String({ pattern: NAME_PATTERN, description: "The dataset the loop tunes on." }),
286
+ ),
287
+ brief: Type.Optional(
288
+ Type.String({
289
+ minLength: 1,
290
+ description:
291
+ "Plain prose for the proposer, relative to evals/: what to improve, and what is known about the problem.",
292
+ }),
293
+ ),
294
+ edit: Type.Optional(
295
+ Type.Object(
296
+ {
297
+ allow: Type.Optional(
298
+ Type.Array(Text, {
299
+ minItems: 1,
300
+ uniqueItems: true,
301
+ description: `Globs of the files a try may edit, ${GLOB_DESCRIPTION}; ["**"] for anything.`,
302
+ }),
303
+ ),
304
+ deny: Type.Optional(
305
+ Type.Array(Text, {
306
+ uniqueItems: true,
307
+ description: `Globs of files a try may not edit though allow matches them, ${GLOB_DESCRIPTION}.`,
308
+ }),
309
+ ),
310
+ },
311
+ {
312
+ additionalProperties: false,
313
+ description:
314
+ "Which files a try may edit: those allow matches and deny doesn't. evals/, node_modules, files git ignores and a key's text copied into code are always denied.",
315
+ },
316
+ ),
317
+ ),
318
+ proposer: Type.Optional(
319
+ Type.String({
320
+ pattern: AGENT_PATTERN,
321
+ description:
322
+ "{harness}/{model} of the loop's proposer; claude/claude-sonnet-5-5 when absent.",
323
+ }),
324
+ ),
325
+ trials: Type.Optional(
326
+ Type.Integer({
327
+ minimum: 1,
328
+ description: "Trials per case in each try's run; 1 when absent.",
329
+ }),
330
+ ),
331
+ params: Type.Optional(
332
+ Type.Record(Type.String({ pattern: PARAM_PATTERN }), Type.String({ minLength: 1 }), {
333
+ description: "Values over trial.ts's and the dataset entry's, for every trial of the loop.",
334
+ }),
335
+ ),
336
+ cases: Type.Optional(
337
+ Type.Array(Type.String({ pattern: NAME_PATTERN }), {
338
+ minItems: 1,
339
+ uniqueItems: true,
340
+ description:
341
+ "Tune on these cases only; every case the dataset doesn't hold out when absent.",
342
+ }),
343
+ ),
344
+ tags: Type.Optional(
345
+ Type.Array(Type.String({ pattern: TAG_PATTERN }), {
346
+ minItems: 1,
347
+ uniqueItems: true,
348
+ description: "Tune only on the cases with one of these tags.",
349
+ }),
350
+ ),
351
+ spread: Type.Optional(
352
+ Type.Boolean({
353
+ description:
354
+ "Whether the start runs twice on the tuning cases before the first try, to show its noise; true when absent.",
355
+ }),
356
+ ),
357
+ },
358
+ { additionalProperties: false },
359
+ );
360
+
361
+ export type LoopEntry = Type.Static<typeof LoopEntrySchema>;
362
+
363
+ const Loops = Type.Record(Type.String({ pattern: GRADER_NAME_PATTERN }), LoopEntrySchema);
364
+
365
+ const StoreSchema = Type.String({
366
+ minLength: 1,
367
+ description:
368
+ "Where every run, trial, label and loop of the project is kept, in place of ~/.awf/lab/{main checkout's folder}-{first commit}; never inside the repository.",
369
+ });
370
+
371
+ /** `evals/awf-lab.config.ts`'s `defineLab`: the project's settings. */
372
+ export const LabConfigSchema = Type.Object(
373
+ {
374
+ store: Type.Optional(StoreSchema),
375
+ reuse: Type.Optional(
376
+ Type.Object(
377
+ {
378
+ days: Type.Integer({
379
+ minimum: 0,
380
+ description:
381
+ "How many days a kept trial may stand in for a new one; 14 when absent, 0 never.",
382
+ }),
383
+ },
384
+ { additionalProperties: false },
385
+ ),
386
+ ),
387
+ ignore: Type.Optional(
388
+ Type.Array(Text, {
389
+ uniqueItems: true,
390
+ description:
391
+ "Globs of files that aren't code under test, such as docs/**, relative to the project: the lab's box hides them and a loop's copy leaves them out.",
392
+ }),
393
+ ),
394
+ datasets: Type.Optional(Datasets),
395
+ loops: Type.Optional(Loops),
396
+ },
397
+ { additionalProperties: false },
398
+ );
399
+
400
+ export type LabConfig = Type.Static<typeof LabConfigSchema>;
401
+
402
+ /** `evals/awf-lab.local.json`: this machine's settings, datasets and loops; read only while git ignores it. */
403
+ export const LocalFileSchema = Type.Object(
404
+ {
405
+ $schema: Type.Optional(Type.String()),
406
+ store: Type.Optional(StoreSchema),
407
+ concurrency: Type.Optional(
408
+ Type.Integer({
409
+ minimum: 1,
410
+ description:
411
+ "How many trials of a run may run at once, across its cases and columns; 1 when absent.",
412
+ }),
413
+ ),
414
+ allowance: Type.Optional(
415
+ Type.Integer({
416
+ minimum: 1,
417
+ maximum: 100,
418
+ description:
419
+ "How full the plan's usage window may get, in percent, before a loop pauses; 90 when absent.",
420
+ }),
421
+ ),
422
+ datasets: Type.Optional(Datasets),
423
+ loops: Type.Optional(Loops),
424
+ },
425
+ {
426
+ additionalProperties: false,
427
+ description: "This machine's awf-lab settings, datasets and loops included; git-ignored.",
428
+ },
429
+ );
430
+
431
+ export type LocalFile = Type.Static<typeof LocalFileSchema>;
432
+
433
+ /** A fact's value: flat, so a key compares it exactly. */
434
+ export const FactSchema = Type.Union([Type.String(), Type.Number(), Type.Boolean(), Type.Null()]);
435
+ export type Fact = Type.Static<typeof FactSchema>;
436
+
437
+ /**
438
+ * A bound a key gives a number fact instead of its exact value: at least `min`, at most `max`, or
439
+ * both. `key.ts` checks that one is given and that `min` is not past `max`.
440
+ */
441
+ export const BoundSchema = Type.Object(
442
+ { min: Type.Optional(Type.Number()), max: Type.Optional(Type.Number()) },
443
+ { additionalProperties: false },
444
+ );
445
+ export type Bound = Type.Static<typeof BoundSchema>;
446
+
447
+ /** What a key's `expect` gives a fact: its exact value, or a bound on a number. */
448
+ export const ExpectedSchema = Type.Union([...FactSchema.anyOf, BoundSchema]);
449
+ export type Expected = Type.Static<typeof ExpectedSchema>;
450
+
451
+ /** How a turn can end, each with a `_turns_{outcome}` run fact, `-` written `_`. */
452
+ export const TURN_OUTCOMES = [
453
+ "answered",
454
+ "unanswered",
455
+ "blocked",
456
+ "timed-out",
457
+ "failed",
458
+ "cancelled",
459
+ ] as const satisfies readonly TurnRecord["outcome"][];
460
+ // An outcome the contract adds and this list lacks fails to compile here.
461
+ const everyOutcome: [Exclude<TurnRecord["outcome"], (typeof TURN_OUTCOMES)[number]>] extends [never]
462
+ ? true
463
+ : never = true;
464
+ void everyOutcome;
465
+
466
+ /** A run fact's name for an outcome or an agent's turns. */
467
+ export const runFactName = {
468
+ outcome: (outcome: string) => `_turns_${outcome.replaceAll("-", "_")}`,
469
+ agent: (agent: string) => `_turns_by_${agent}`,
470
+ };
471
+
472
+ /** An agent's name in its run, as the contract's `AgentKey` says. */
473
+ const AGENT_KEY = /^[a-z0-9][a-z0-9._@-]*$/;
474
+
475
+ /**
476
+ * Why a key may not name `name` as a run fact, or undefined when the lab derives it: `_turns`, an
477
+ * outcome's, or an agent's, whose name the run alone knows.
478
+ */
479
+ export function runFactNameProblem(name: string): string | undefined {
480
+ if (name === "_turns") return undefined;
481
+ if (TURN_OUTCOMES.some((outcome) => name === runFactName.outcome(outcome))) return undefined;
482
+ if (name.startsWith("_turns_by_")) {
483
+ if (AGENT_KEY.test(name.slice("_turns_by_".length))) return undefined;
484
+ return "names no agent: an agent's name is lowercase letters, digits, ., _, @ and -";
485
+ }
486
+ return `is no run fact: the lab gives _turns, ${TURN_OUTCOMES.map(runFactName.outcome).join(", ")} and _turns_by_{agent}`;
487
+ }
488
+
489
+ const Criterion = Type.String({ minLength: 1 });
490
+ const MARKER_PATTERN = "^[a-z]+(-[a-z]+){2,}$";
491
+ const FACT_NAME_PATTERN = "^[A-Za-z][A-Za-z0-9_]*$";
492
+
493
+ export const KeySchema = Type.Object(
494
+ {
495
+ marker: Type.String({ pattern: MARKER_PATTERN }),
496
+ expect: Type.Optional(Type.Record(Type.String({ pattern: FACT_NAME_PATTERN }), ExpectedSchema)),
497
+ must: Type.Optional(Type.Array(Criterion, { minItems: 1 })),
498
+ "must-not": Type.Optional(Type.Array(Criterion, { minItems: 1 })),
499
+ "known-failing": Type.Optional(Type.Literal(true)),
500
+ pass: Type.Optional(PassSchema),
501
+ draft: Type.Optional(Type.Literal(true)),
502
+ holdout: Type.Optional(
503
+ Type.Literal(true, {
504
+ description:
505
+ "Set aside for a final check: run, --failed and history leave it out, run --holdout runs it alone, and a loop's proposer never sees it.",
506
+ }),
507
+ ),
508
+ tags: Type.Optional(
509
+ Type.Array(Type.String({ pattern: TAG_PATTERN }), {
510
+ minItems: 1,
511
+ uniqueItems: true,
512
+ description:
513
+ "Plain words the report groups cases by, and --tags picks them by; no grader reads them.",
514
+ }),
515
+ ),
516
+ captured: Type.Optional(
517
+ Type.Object(
518
+ {
519
+ run: Text,
520
+ mark: Type.Union([Type.Literal("good"), Type.Literal("needs-work")]),
521
+ note: Type.Optional(Type.String()),
522
+ },
523
+ { additionalProperties: false },
524
+ ),
525
+ ),
526
+ },
527
+ { additionalProperties: false, description: "What a good result looks like for one case." },
528
+ );
529
+
530
+ export type Key = Type.Static<typeof KeySchema>;
531
+
532
+ /** Why a `pass` that reads as a fraction is no share of trials: more than all of them. */
533
+ export function passProblem(pass: string | undefined): string | undefined {
534
+ const [n, d] = (pass ?? "all").split("/").map(Number);
535
+ return d !== undefined && n! > d
536
+ ? `pass ${pass} is more than all: give all, or a fraction up to 1, such as 2/3`
537
+ : undefined;
538
+ }
539
+
540
+ /** What each pattern of these files means, for a refusal to say in words. */
541
+ export const SETTINGS_PATTERNS: Readonly<Record<string, string>> = {
542
+ [DURATION_PATTERN]: "a number and a unit, such as 30m or 90s",
543
+ [PASS_PATTERN]: "all, or the share of trials that must pass, such as 2/3",
544
+ [MARKER_PATTERN]: "three or more lowercase words joined by hyphens",
545
+ [TAG_PATTERN]: "lowercase letters, digits and hyphens",
546
+ [PARAM_PATTERN]: "a letter, then letters, digits, - or _",
547
+ [NAME_PATTERN]: "letters, digits, '.', '_' and '-', starting with a letter or digit",
548
+ [GRADER_NAME_PATTERN]: "letters, digits, '_' and '-', starting with a letter or digit",
549
+ [AGENT_PATTERN]: "{harness}/{model}, such as claude/claude-sonnet-5-5",
550
+ [SEMVER.source]: "semver, such as 1.0.0, 1.1.0-{tag} or 1.0.1+{build}",
551
+ [FACT_NAME_PATTERN]: "a letter, then letters, digits or _",
552
+ };
@@ -0,0 +1,64 @@
1
+ import type { Gathered, Refused } from "./trial-file";
2
+
3
+ // What the lab hands a trial's process, and what `defineEnv` gives back: the one seam between the
4
+ // lab's side and `@agentswf/lab`, which runs in the project's process.
5
+
6
+ /** The environment variables the lab sets for a trial's `awf run`. */
7
+ export const TRIAL_ENV = {
8
+ /** The built case folder: the data folder's `shared/` and the case's `input/`. No key. */
9
+ case: "AWF_LAB_CASE",
10
+ /** The trial file `defineTrial` appends to. */
11
+ trial: "AWF_LAB_TRIAL",
12
+ /** The trial's empty scratch folder, also the run's `--cwd`. */
13
+ scratch: "AWF_LAB_SCRATCH",
14
+ /** The code folder: your checkout, or a folder `--against` names. */
15
+ root: "AWF_LAB_ROOT",
16
+ /** `TrialSettings` as JSON. */
17
+ settings: "AWF_LAB_SETTINGS",
18
+ /** The trial's parameters, `{ name: value }` as JSON. */
19
+ params: "AWF_LAB_PARAMS",
20
+ /** A grader workflow's grade folder, which `gradeCase()` reads. */
21
+ grade: "AWF_LAB_GRADE",
22
+ } as const;
23
+
24
+ /** The files of a grade folder, as the lab writes them and `gradeCase()` reads them. */
25
+ export const GRADE_FILES = {
26
+ /** `grade.json`, `kind: awf.case-grade/1`. */
27
+ grade: "grade.json",
28
+ /** The built case folder. */
29
+ case: "case",
30
+ /** The key without its marker. */
31
+ key: "key.json",
32
+ /** The case's `key/` folder, when it has one. */
33
+ keyDir: "key",
34
+ result: "result.json",
35
+ /** `{ facts }`, and `stages` for a grader that reads the trial, which alone gets `result` and `kept`. */
36
+ facts: "facts.json",
37
+ /** The files `evidence` named, saved from the trial's scratch folder. */
38
+ kept: "kept",
39
+ /** `{ key, parts }`: the grader's earlier parts, when they still count. */
40
+ previous: "previous.json",
41
+ } as const;
42
+
43
+ /** From `trial.ts`, resolved by the lab. */
44
+ export type TrialSettings = {
45
+ dataset: string;
46
+ /** The eval folder, whose files are never the workflow's source. */
47
+ datasetDir?: string;
48
+ /** `limits.setup`, in ms. */
49
+ setupMs: number;
50
+ /** `limits.run`, the workflow's limit, in ms. */
51
+ runMs: number;
52
+ /** `limits.after`, `evidence`'s and cleanup's, in ms. */
53
+ afterMs: number;
54
+ /** Hosts the workflow's code may reach for real: `sandbox.network.code` and the engine's own. */
55
+ codeHosts: string[];
56
+ };
57
+
58
+ /** The run's value: `awf run --json`'s `value` for a trial that completed. */
59
+ export type EnvValue = Gathered & {
60
+ /** Absent when the workflow never loaded. */
61
+ workflow?: { name: string; version?: string };
62
+ unused: string[];
63
+ refused: Refused[];
64
+ };