@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.
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/lab-check.schema.json +64 -0
  5. package/schema/lab-config.schema.json +187 -0
  6. package/schema/lab-final.schema.json +48 -0
  7. package/schema/lab-good-record.schema.json +70 -0
  8. package/schema/lab-grade.schema.json +49 -0
  9. package/schema/lab-key.schema.json +124 -0
  10. package/schema/lab-label.schema.json +32 -0
  11. package/schema/lab-loop-report.schema.json +221 -0
  12. package/schema/lab-loop.schema.json +141 -0
  13. package/schema/lab-plan.schema.json +128 -0
  14. package/schema/lab-previous.schema.json +70 -0
  15. package/schema/lab-proposal.schema.json +52 -0
  16. package/schema/lab-report.schema.json +1128 -0
  17. package/schema/lab-run.schema.json +253 -0
  18. package/schema/lab-runs.schema.json +132 -0
  19. package/schema/lab-score.schema.json +319 -0
  20. package/schema/lab-spread.schema.json +110 -0
  21. package/schema/lab-trial.schema.json +543 -0
  22. package/schema/lab-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 +131 -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 +185 -0
  39. package/src/format/key.ts +168 -0
  40. package/src/format/loop.ts +179 -0
  41. package/src/format/output.ts +542 -0
  42. package/src/format/records.ts +443 -0
  43. package/src/format/schema-files.ts +64 -0
  44. package/src/format/settings.ts +559 -0
  45. package/src/format/trial-env.ts +67 -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 +39 -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 +61 -0
  54. package/src/help/runs.txt +11 -0
  55. package/src/help/score.txt +28 -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 +251 -0
  61. package/src/lab/capture-command.ts +302 -0
  62. package/src/lab/capture.ts +234 -0
  63. package/src/lab/check-command.ts +187 -0
  64. package/src/lab/cli.ts +325 -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 +283 -0
  70. package/src/lab/criteria.ts +69 -0
  71. package/src/lab/dataset.ts +375 -0
  72. package/src/lab/eval-files.ts +196 -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 +172 -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 +227 -0
  82. package/src/lab/labels.ts +211 -0
  83. package/src/lab/loop-code.ts +255 -0
  84. package/src/lab/loop-command.ts +774 -0
  85. package/src/lab/loop-records.ts +144 -0
  86. package/src/lab/loop.ts +864 -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 +155 -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 +157 -0
  96. package/src/lab/report.ts +936 -0
  97. package/src/lab/reuse.ts +134 -0
  98. package/src/lab/run-command.ts +958 -0
  99. package/src/lab/run-logs.ts +89 -0
  100. package/src/lab/run-report.ts +286 -0
  101. package/src/lab/runs-command.ts +171 -0
  102. package/src/lab/runs.ts +172 -0
  103. package/src/lab/scaffold.ts +94 -0
  104. package/src/lab/score-command.ts +197 -0
  105. package/src/lab/score.ts +396 -0
  106. package/src/lab/store.ts +383 -0
  107. package/src/lab/target-stub.txt +18 -0
  108. package/src/lab/text.ts +29 -0
  109. package/src/lab/trial-settings.ts +105 -0
  110. package/src/lab/trial.ts +439 -0
  111. package/src/lab/usage.ts +274 -0
  112. package/src/lab/verdict.ts +161 -0
  113. package/src/lab/workflow-graders.ts +164 -0
  114. package/src/lab/workspace.ts +496 -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,144 @@
1
+ import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync } from "node:fs";
2
+ import { basename, dirname, join } from "node:path";
3
+ import { git } from "./git";
4
+ import {
5
+ BRIEF_FILE,
6
+ bestSoFarOf,
7
+ CHANGE_FILE,
8
+ codeOf,
9
+ loopDirOf,
10
+ newestLoop,
11
+ readLoop,
12
+ readTries,
13
+ SPREAD_FILE,
14
+ } from "./loop";
15
+ import { copyStart, rebuildTry } from "./loop-code";
16
+ import { Refusal } from "./refusal";
17
+ import type { CaseWorkspace } from "./workspace";
18
+
19
+ // A loop's own records in the records folder, `{records}/loops/{id}/`, beside its runs': what it
20
+ // was started with, each try's outcome, proposal, ledger and change, and each final check. The
21
+ // code is not kept: a store that lacks it builds it again from the start commit and the changes
22
+ // of the tries it needs, so a loop goes on from a clone of the project that ran it.
23
+
24
+ /** The loop's own files, relative to its folder: never its code, bundles or proposer's runs. */
25
+ function ownFiles(dir: string): string[] {
26
+ const files = ["loop.json", BRIEF_FILE, SPREAD_FILE, "spread/runs.json"].filter((f) =>
27
+ existsSync(join(dir, f)),
28
+ );
29
+ const tries = join(dir, "tries");
30
+ if (existsSync(tries)) {
31
+ for (const n of readdirSync(tries)) {
32
+ for (const f of ["try.json", "proposal.json", "runs.json", "proposer.jsonl", CHANGE_FILE]) {
33
+ if (existsSync(join(tries, n, f))) files.push(join("tries", n, f));
34
+ }
35
+ }
36
+ }
37
+ const finals = join(dir, "finals");
38
+ if (existsSync(finals)) {
39
+ for (const f of readdirSync(finals).filter((f) => f.endsWith(".json"))) {
40
+ files.push(join("finals", f));
41
+ }
42
+ }
43
+ return files;
44
+ }
45
+
46
+ /** Writes the loop's own files into the records, as the store has them now. */
47
+ export function writeLoopRecords(dir: string, records: string): void {
48
+ const to = join(records, "loops", basename(dir));
49
+ for (const file of ownFiles(dir)) {
50
+ mkdirSync(dirname(join(to, file)), { recursive: true });
51
+ copyFileSync(join(dir, file), join(to, file));
52
+ }
53
+ }
54
+
55
+ const same = (a: string, b: string) => readFileSync(a, "utf8") === readFileSync(b, "utf8");
56
+
57
+ /**
58
+ * Brings the records' copies of a name's loops into the store, the files each lacks; then the code
59
+ * of the newest one's start and of each try it still needs, built again. A loop of the same id recorded
60
+ * otherwise, or a try decided otherwise, is refused: the two can't both be this loop. One whose
61
+ * `loop.json` doesn't read, as one from before the loop's entry, is left out.
62
+ */
63
+ export function readLoopRecords(
64
+ store: string,
65
+ records: string,
66
+ name: string,
67
+ top: string,
68
+ workspace: CaseWorkspace,
69
+ ): void {
70
+ const root = join(records, "loops");
71
+ if (!existsSync(root)) return;
72
+ for (const id of readdirSync(root)) {
73
+ const from = join(root, id);
74
+ let loop: ReturnType<typeof readLoop>;
75
+ try {
76
+ loop = readLoop(from);
77
+ } catch {
78
+ continue;
79
+ }
80
+ if (!loop || loop.name !== name) continue;
81
+ const dir = loopDirOf(store, id);
82
+ if (
83
+ existsSync(join(dir, "loop.json")) &&
84
+ !same(join(dir, "loop.json"), join(from, "loop.json"))
85
+ ) {
86
+ throw new Refusal(
87
+ `loop ${id} in the records was started elsewhere than this store's: they can't both be it`,
88
+ );
89
+ }
90
+ for (const file of ownFiles(from)) {
91
+ const here = join(dir, file);
92
+ if (!existsSync(here)) {
93
+ mkdirSync(dirname(here), { recursive: true });
94
+ copyFileSync(join(from, file), here);
95
+ } else if (file.endsWith("try.json") && !same(here, join(from, file))) {
96
+ throw new Refusal(`loop ${id}'s ${file} differs in the records: it was decided twice`);
97
+ }
98
+ }
99
+ }
100
+ const newest = newestLoop(store, name);
101
+ if (newest) rebuildCode(newest.dir, basename(newest.dir), top, workspace);
102
+ }
103
+
104
+ /** The start's code, and each kept or undecided try's, where the store lacks them. */
105
+ function rebuildCode(dir: string, name: string, top: string, workspace: CaseWorkspace): void {
106
+ const loop = readLoop(dir);
107
+ if (!loop) return;
108
+ const tries = readTries(dir);
109
+ const decided = new Set(tries.map((t) => t.n));
110
+ const kept = new Set(tries.filter((t) => t.outcome === "kept").map((t) => t.n));
111
+ // An undecided try resumes with its change: its proposal was made and paid for.
112
+ const open = existsSync(join(dir, "tries"))
113
+ ? readdirSync(join(dir, "tries"))
114
+ .map(Number)
115
+ .filter((n) => !decided.has(n) && existsSync(join(dir, "tries", String(n), CHANGE_FILE)))
116
+ : [];
117
+ const needed = [...kept, ...open].sort((a, b) => a - b);
118
+ if (existsSync(codeOf(dir, 0)) && needed.every((n) => existsSync(codeOf(dir, n)))) return;
119
+ if (!existsSync(codeOf(dir, 0))) {
120
+ if (!loop.from.commit || loop.from.dirty) {
121
+ throw new Refusal(
122
+ `loop ${name} started from uncommitted changes, so its code can't be built again here: go on where it started`,
123
+ );
124
+ }
125
+ const checkout = `${codeOf(dir, 0)}.checkout`;
126
+ rmSync(checkout, { recursive: true, force: true });
127
+ // --force: a process cut here before left this checkout registered though gone.
128
+ git(top, ["worktree", "add", "--force", "--detach", "-q", checkout, loop.from.commit]);
129
+ try {
130
+ copyStart(top, codeOf(dir, 0), workspace, checkout);
131
+ } finally {
132
+ git(top, ["worktree", "remove", "--force", checkout]);
133
+ }
134
+ }
135
+ const parentOf = (n: number) =>
136
+ tries.find((t) => t.n === n)?.parent ??
137
+ // An undecided try's parent is the best version so far among the tries decided before it.
138
+ bestSoFarOf(tries.filter((t) => t.n < n));
139
+ for (const n of needed) {
140
+ if (existsSync(codeOf(dir, n))) continue;
141
+ const change = readFileSync(join(dir, "tries", String(n), CHANGE_FILE), "utf8");
142
+ rebuildTry(codeOf(dir, parentOf(n)), codeOf(dir, n), change, top, n);
143
+ }
144
+ }