@parcha/agentrun-serve 0.1.0-beta.44 → 0.1.0-beta.45

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 (346) hide show
  1. package/CHANGELOG.md +131 -5
  2. package/README.md +371 -300
  3. package/dist/acp-package.d.ts +28 -0
  4. package/dist/acp-package.js +23 -0
  5. package/dist/adapter-facts.d.ts +8 -1
  6. package/dist/adapter-facts.js +9 -1
  7. package/dist/adapters.d.ts +5 -0
  8. package/dist/adapters.js +36 -3
  9. package/dist/agent-delete.d.ts +50 -0
  10. package/dist/agent-delete.js +125 -0
  11. package/dist/agents-store.d.ts +9 -3
  12. package/dist/agents-store.js +22 -8
  13. package/dist/agents.d.ts +5 -1
  14. package/dist/agents.js +8 -4
  15. package/dist/api-only.d.ts +8 -3
  16. package/dist/api-only.js +10 -4
  17. package/dist/archil-routes.d.ts +5 -1
  18. package/dist/archil-routes.js +57 -2
  19. package/dist/browser-wait-ended.d.ts +2 -0
  20. package/dist/browser-wait-ended.js +2 -0
  21. package/dist/calendar/ics-faux.d.ts +3 -1
  22. package/dist/calendar/ics-faux.js +12 -2
  23. package/dist/calendar/tools.js +5 -2
  24. package/dist/chatgpt-plan.js +3 -1
  25. package/dist/claude-login.d.ts +135 -0
  26. package/dist/claude-login.js +244 -0
  27. package/dist/cli.js +52 -16
  28. package/dist/composio-preset.js +69 -7
  29. package/dist/composio-triggers.d.ts +14 -0
  30. package/dist/composio-triggers.js +79 -15
  31. package/dist/computer-attach.d.ts +40 -5
  32. package/dist/computer-attach.js +130 -15
  33. package/dist/computer-keys.d.ts +3 -0
  34. package/dist/computer-keys.js +7 -1
  35. package/dist/computer-live.js +3 -2
  36. package/dist/computers.d.ts +21 -10
  37. package/dist/computers.js +39 -28
  38. package/dist/console/assets/index-3O2Yl6hZ.js +701 -0
  39. package/dist/console/assets/index-DaFsZuzQ.css +1 -0
  40. package/dist/console/index.html +2 -2
  41. package/dist/decisions.d.ts +1 -1
  42. package/dist/dial-relay.d.ts +29 -0
  43. package/dist/dial-relay.js +94 -0
  44. package/dist/email/tools.js +10 -5
  45. package/dist/env-proxy.d.ts +2 -0
  46. package/dist/env-proxy.js +35 -0
  47. package/dist/errors.d.ts +3 -1
  48. package/dist/errors.js +7 -4
  49. package/dist/faux-computers.d.ts +8 -2
  50. package/dist/faux-computers.js +83 -6
  51. package/dist/harness-archive.d.ts +67 -0
  52. package/dist/harness-archive.js +177 -0
  53. package/dist/harness-goose.d.ts +34 -0
  54. package/dist/harness-goose.js +87 -0
  55. package/dist/harness-install.d.ts +21 -0
  56. package/dist/harness-install.js +24 -11
  57. package/dist/harness-opencode/adapter.js +1 -1
  58. package/dist/harness-table.d.ts +9 -1
  59. package/dist/harness-table.js +20 -3
  60. package/dist/harness.d.ts +1 -2
  61. package/dist/harness.js +3 -4
  62. package/dist/host.js +2 -1
  63. package/dist/key-check.js +3 -0
  64. package/dist/learn/audit.d.ts +6 -6
  65. package/dist/learn/audit.js +3 -7
  66. package/dist/learn/check.d.ts +23 -89
  67. package/dist/learn/check.js +57 -335
  68. package/dist/learn/commit.d.ts +63 -90
  69. package/dist/learn/commit.js +124 -316
  70. package/dist/learn/compile.d.ts +53 -50
  71. package/dist/learn/compile.js +87 -102
  72. package/dist/learn/corrections.d.ts +13 -0
  73. package/dist/learn/corrections.js +23 -0
  74. package/dist/learn/delete.d.ts +5 -5
  75. package/dist/learn/delete.js +8 -10
  76. package/dist/learn/folder.d.ts +35 -47
  77. package/dist/learn/folder.js +118 -134
  78. package/dist/learn/index.d.ts +3 -5
  79. package/dist/learn/index.js +6 -9
  80. package/dist/learn/learning.sh +45 -15
  81. package/dist/learn/promote.d.ts +13 -1
  82. package/dist/learn/promote.js +9 -0
  83. package/dist/learn/{skills.d.ts → workflow-draft.d.ts} +30 -38
  84. package/dist/learn/{skills.js → workflow-draft.js} +48 -58
  85. package/dist/learn/workflow-nodes.js +1 -1
  86. package/dist/learned-workflow.js +2 -2
  87. package/dist/learning-cost.d.ts +38 -24
  88. package/dist/learning-cost.js +41 -24
  89. package/dist/learning-routes.d.ts +91 -170
  90. package/dist/learning-routes.js +183 -421
  91. package/dist/learning-run.d.ts +19 -40
  92. package/dist/learning-run.js +23 -93
  93. package/dist/learning-setting.d.ts +4 -4
  94. package/dist/learning-setting.js +6 -6
  95. package/dist/learning-words.d.ts +28 -74
  96. package/dist/learning-words.js +64 -162
  97. package/dist/learning.d.ts +61 -392
  98. package/dist/learning.js +101 -651
  99. package/dist/live-reach.d.ts +25 -2
  100. package/dist/live-reach.js +46 -4
  101. package/dist/llms.txt +1 -1
  102. package/dist/local-models.d.ts +3 -11
  103. package/dist/local-models.js +5 -6
  104. package/dist/mcp-servers-store.d.ts +4 -4
  105. package/dist/model-form.js +6 -5
  106. package/dist/model.d.ts +4 -0
  107. package/dist/model.js +26 -9
  108. package/dist/native/binding.js +1 -1
  109. package/dist/native/connection.js +1 -1
  110. package/dist/native/discovery.js +1 -1
  111. package/dist/native/instructions.d.ts +3 -0
  112. package/dist/native/instructions.js +5 -3
  113. package/dist/native/remote-worker-bridge.js +9 -43
  114. package/dist/native/remote.d.ts +8 -15
  115. package/dist/native/remote.js +3 -3
  116. package/dist/native/schema.js +1 -1
  117. package/dist/native/types.d.ts +2 -2
  118. package/dist/openapi.js +268 -316
  119. package/dist/paid-calls.d.ts +8 -0
  120. package/dist/paid-calls.js +20 -0
  121. package/dist/particle-provider.d.ts +11 -0
  122. package/dist/particle-provider.js +135 -0
  123. package/dist/port-models.d.ts +12 -4
  124. package/dist/port-models.js +28 -11
  125. package/dist/print.d.ts +27 -0
  126. package/dist/print.js +21 -0
  127. package/dist/provider.d.ts +7 -0
  128. package/dist/provider.js +15 -1
  129. package/dist/reflect-range.js +2 -3
  130. package/dist/run-launch.d.ts +14 -28
  131. package/dist/run-launch.js +9 -12
  132. package/dist/run-learning-served.d.ts +15 -89
  133. package/dist/run-listing.d.ts +34 -0
  134. package/dist/run-listing.js +128 -0
  135. package/dist/run-model-key.js +1 -1
  136. package/dist/run-timeline-served.js +5 -3
  137. package/dist/run-trace.d.ts +97 -0
  138. package/dist/run-trace.js +253 -0
  139. package/dist/run-view.d.ts +15 -0
  140. package/dist/run-view.js +9 -0
  141. package/dist/runnable.js +2 -1
  142. package/dist/runner/adapter-loop.d.ts +8 -6
  143. package/dist/runner/adapter-loop.js +16 -12
  144. package/dist/runner/adapter-run-spec.d.ts +16 -4
  145. package/dist/runner/adapter-run-spec.js +64 -58
  146. package/dist/runner/adapters/index.js +22 -0
  147. package/dist/runner/adapters/pi.d.ts +4 -12
  148. package/dist/runner/adapters/pi.js +2 -2
  149. package/dist/runner/audit.d.ts +1 -1
  150. package/dist/runner/audit.js +16 -8
  151. package/dist/runner/beside.d.ts +6 -2
  152. package/dist/runner/beside.js +38 -9
  153. package/dist/runner/claim-secrets.d.ts +18 -2
  154. package/dist/runner/claim-secrets.js +42 -18
  155. package/dist/runner/cli-run-spec.js +55 -51
  156. package/dist/runner/conformance/harness.d.ts +6 -0
  157. package/dist/runner/conformance/harness.js +16 -5
  158. package/dist/runner/dispatch.d.ts +2 -2
  159. package/dist/runner/end-of-run.d.ts +6 -14
  160. package/dist/runner/end-of-run.js +8 -33
  161. package/dist/runner/feedback-round.d.ts +2 -2
  162. package/dist/runner/feedback-round.js +7 -7
  163. package/dist/runner/github-app.js +3 -1
  164. package/dist/runner/harness-loops.d.ts +0 -8
  165. package/dist/runner/harness-loops.js +11 -59
  166. package/dist/runner/held.d.ts +170 -0
  167. package/dist/runner/held.js +57 -0
  168. package/dist/runner/journal-kinds.d.ts +2 -3
  169. package/dist/runner/journal-kinds.js +2 -3
  170. package/dist/runner/learned-first.d.ts +40 -0
  171. package/dist/runner/learned-first.js +32 -0
  172. package/dist/runner/learning-end.d.ts +77 -69
  173. package/dist/runner/learning-end.js +250 -303
  174. package/dist/runner/learning-seed.d.ts +15 -33
  175. package/dist/runner/learning-seed.js +24 -45
  176. package/dist/runner/loop-contract.d.ts +2 -3
  177. package/dist/runner/main.d.ts +17 -9
  178. package/dist/runner/main.js +63 -20
  179. package/dist/runner/mcp-run.d.ts +11 -0
  180. package/dist/runner/mcp-run.js +67 -0
  181. package/dist/runner/memory-claim.d.ts +10 -5
  182. package/dist/runner/memory-claim.js +12 -6
  183. package/dist/runner/model-route.d.ts +7 -4
  184. package/dist/runner/model-route.js +22 -10
  185. package/dist/runner/native-loop.d.ts +0 -7
  186. package/dist/runner/native-loop.js +9 -101
  187. package/dist/runner/promotion.d.ts +8 -6
  188. package/dist/runner/promotion.js +19 -12
  189. package/dist/runner/reflect-hook.d.ts +64 -95
  190. package/dist/runner/reflect-hook.js +112 -225
  191. package/dist/runner/run-channel.d.ts +43 -0
  192. package/dist/runner/run-channel.js +467 -0
  193. package/dist/runner/run-child-host.d.ts +169 -0
  194. package/dist/runner/run-child-host.js +1222 -0
  195. package/dist/runner/run-child.d.ts +1 -0
  196. package/dist/runner/run-child.js +254 -0
  197. package/dist/runner/run-errors.d.ts +2 -0
  198. package/dist/runner/run-errors.js +20 -0
  199. package/dist/runner/runner.js +1 -5
  200. package/dist/runner/sdk-ask.d.ts +78 -0
  201. package/dist/runner/sdk-ask.js +158 -0
  202. package/dist/runner/sdk-loop.d.ts +2 -4
  203. package/dist/runner/sdk-loop.js +29 -49
  204. package/dist/runner/store-secrets.d.ts +5 -3
  205. package/dist/runner/store-secrets.js +5 -1
  206. package/dist/runner/workflow-first.d.ts +5 -0
  207. package/dist/runner/workflow-first.js +4 -0
  208. package/dist/runner/workflow-spec.js +7 -2
  209. package/dist/runs-over-store.d.ts +16 -28
  210. package/dist/runs-over-store.js +31 -38
  211. package/dist/sdk/runs.d.ts +6 -11
  212. package/dist/sdk/runs.js +20 -41
  213. package/dist/sdk/schema.js +3 -4
  214. package/dist/seal-key.d.ts +5 -0
  215. package/dist/seal-key.js +7 -0
  216. package/dist/secrets.d.ts +3 -0
  217. package/dist/secrets.js +16 -3
  218. package/dist/server.d.ts +55 -33
  219. package/dist/server.js +685 -320
  220. package/dist/sign-ins-store.d.ts +8 -0
  221. package/dist/sign-ins-store.js +36 -1
  222. package/dist/skills.js +1 -1
  223. package/dist/store/index.d.ts +2 -2
  224. package/dist/store/index.js +1 -1
  225. package/dist/store/journal/storage.js +3 -0
  226. package/dist/store/postgres/migrations/0017_run_listing.sql +83 -0
  227. package/dist/store/postgres/migrations/0018_trigger_template.sql +6 -0
  228. package/dist/store/postgres/migrations/0019_runner_relays.sql +28 -0
  229. package/dist/store/schema.d.ts +53 -4
  230. package/dist/store/schema.js +137 -6
  231. package/dist/store/sqlite/connection.d.ts +2 -0
  232. package/dist/store/sqlite/connection.js +3 -1
  233. package/dist/store/sqlite/run-patch.d.ts +0 -17
  234. package/dist/store/sqlite/run-patch.js +4 -33
  235. package/dist/store/sqlite/store.js +246 -63
  236. package/dist/store/sqlite/worker.js +25 -0
  237. package/dist/store/store.d.ts +193 -36
  238. package/dist/store/store.js +25 -3
  239. package/dist/store-reads/learning.d.ts +102 -204
  240. package/dist/store-reads/learning.js +159 -321
  241. package/dist/store-reads/loop.d.ts +0 -1
  242. package/dist/store-reads/loop.js +1 -1
  243. package/dist/store-view.d.ts +29 -1
  244. package/dist/store-view.js +97 -31
  245. package/dist/template-eval.d.ts +3 -6
  246. package/dist/template-eval.js +22 -36
  247. package/dist/templates.d.ts +2 -1
  248. package/dist/templates.js +6 -3
  249. package/dist/timeline/index.d.ts +1 -1
  250. package/dist/timeline/index.js +1 -1
  251. package/dist/timeline/timeline.d.ts +5 -1
  252. package/dist/timeline/timeline.js +16 -0
  253. package/dist/tool-gate.d.ts +0 -7
  254. package/dist/tool-gate.js +1 -15
  255. package/dist/tool-keys-store.d.ts +1 -1
  256. package/dist/tool-keys.d.ts +6 -4
  257. package/dist/tool-keys.js +12 -6
  258. package/dist/tools.d.ts +2 -2
  259. package/dist/tools.js +11 -4
  260. package/dist/triggers.d.ts +60 -5
  261. package/dist/triggers.js +123 -18
  262. package/dist/validate.js +1 -1
  263. package/dist/webhooks.d.ts +4 -1
  264. package/dist/webhooks.js +11 -4
  265. package/dist/workspace/agent-computer.d.ts +2 -0
  266. package/dist/workspace/agent-computer.js +2 -0
  267. package/dist/workspace/archil-claim.d.ts +7 -0
  268. package/dist/workspace/archil-claim.js +29 -0
  269. package/dist/workspace/archil.d.ts +9 -1
  270. package/dist/workspace/archil.js +165 -3
  271. package/dist/workspace/attach.d.ts +3 -1
  272. package/dist/workspace/attach.js +2 -0
  273. package/dist/workspace/attached-computers.d.ts +16 -5
  274. package/dist/workspace/attached-computers.js +25 -9
  275. package/dist/workspace/computer-deps.js +1 -1
  276. package/dist/workspace/computer-for-claim.d.ts +7 -3
  277. package/dist/workspace/computer-for-claim.js +119 -22
  278. package/dist/workspace/computer-hold.js +2 -1
  279. package/dist/workspace/computer-kept.d.ts +11 -1
  280. package/dist/workspace/computer-kept.js +24 -3
  281. package/dist/workspace/resolve-computer.d.ts +33 -19
  282. package/dist/workspace/resolve-computer.js +172 -64
  283. package/dist/workspace/revision-skills.d.ts +1 -2
  284. package/dist/workspace/revision-skills.js +3 -4
  285. package/dist/workspace/run-workspace.d.ts +1 -1
  286. package/dist/workspace/run-workspace.js +5 -0
  287. package/dist/workspace/synced-folder.d.ts +85 -0
  288. package/dist/workspace/synced-folder.js +230 -0
  289. package/dist/workspace/workspace.d.ts +4 -0
  290. package/launcher/agentrun-serve +1 -1
  291. package/package.json +10 -10
  292. package/templates/README.md +7 -7
  293. package/templates/inbound-lead/agent.md +3 -3
  294. package/templates/inbound-lead/evals/learned-correction/case.yaml +3 -3
  295. package/templates/inbound-lead/evals/learned-correction/fixtures/corrections.md +1 -0
  296. package/templates/inbound-lead/evals/learned-correction/graders/applies-correction.md +22 -0
  297. package/templates/inbound-lead/evals/learned-correction/graders/learned-origin.md +1 -1
  298. package/templates/inbound-lead/evals/learned-correction/prompt.md +1 -1
  299. package/templates/inbound-lead/lead.schema.json +6 -5
  300. package/templates/inbound-lead/rules/RUBRIC.md +4 -4
  301. package/templates/inbound-lead/skills/lead/SKILL.md +4 -4
  302. package/templates/market-monitor/evals/learned-preference/case.yaml +4 -4
  303. package/templates/market-monitor/evals/learned-preference/fixtures/corrections.md +1 -0
  304. package/templates/market-monitor/evals/learned-preference/graders/omits-junior-post.md +3 -3
  305. package/templates/market-monitor/evals/learned-preference/prompt.md +1 -1
  306. package/templates/release-notes/evals/learned-style/case.yaml +4 -4
  307. package/templates/release-notes/evals/learned-style/fixtures/corrections.md +1 -0
  308. package/templates/release-notes/evals/learned-style/graders/same-changes.md +2 -2
  309. package/templates/release-notes/evals/learned-style/prompt.md +1 -1
  310. package/dist/console/assets/index-BUktRvS-.css +0 -1
  311. package/dist/console/assets/index-nB9kTfIN.js +0 -711
  312. package/dist/learn/grade.d.ts +0 -138
  313. package/dist/learn/grade.js +0 -682
  314. package/dist/learn/notes.d.ts +0 -81
  315. package/dist/learn/notes.js +0 -178
  316. package/dist/learn/reflect.d.ts +0 -77
  317. package/dist/learn/reflect.js +0 -228
  318. package/dist/learn/relevance.d.ts +0 -24
  319. package/dist/learn/relevance.js +0 -91
  320. package/dist/learn/seed-check.d.ts +0 -74
  321. package/dist/learn/seed-check.js +0 -103
  322. package/dist/learning-batch.d.ts +0 -291
  323. package/dist/learning-batch.js +0 -600
  324. package/dist/learning-evals.d.ts +0 -35
  325. package/dist/learning-evals.js +0 -99
  326. package/dist/native/context.d.ts +0 -6
  327. package/dist/native/context.js +0 -22
  328. package/dist/propose.d.ts +0 -52
  329. package/dist/propose.js +0 -224
  330. package/dist/runner/learning-ask.d.ts +0 -47
  331. package/dist/runner/learning-ask.js +0 -128
  332. package/dist/runner/learning-dispatch.d.ts +0 -45
  333. package/dist/runner/learning-dispatch.js +0 -99
  334. package/dist/runner/learning-factory.d.ts +0 -16
  335. package/dist/runner/learning-factory.js +0 -50
  336. package/dist/runner/learning-loop.d.ts +0 -273
  337. package/dist/runner/learning-loop.js +0 -521
  338. package/dist/usage.d.ts +0 -11
  339. package/dist/usage.js +0 -11
  340. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/MEMORY.md +0 -1
  341. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/no-agencies.md +0 -14
  342. package/templates/inbound-lead/evals/learned-correction/graders/applies-note.md +0 -21
  343. package/templates/market-monitor/evals/learned-preference/fixtures/learning/MEMORY.md +0 -1
  344. package/templates/market-monitor/evals/learned-preference/fixtures/learning/skip-junior-job-posts.md +0 -8
  345. package/templates/release-notes/evals/learned-style/fixtures/learning/MEMORY.md +0 -1
  346. package/templates/release-notes/evals/learned-style/fixtures/learning/say-agent-not-bot.md +0 -13
@@ -1,340 +1,148 @@
1
- // learningCommit: the one write API on an agent's learning folder (COMMIT-API.md v1). The runner's learning
2
- // loop calls it for a run's merge, and a caller with a person's whole new text calls it for a person's write. Every
3
- // call is ONE exec of the learning script on the folder's main (learn/folder.ts commitChange, one hold), computed from
4
- // the folder's snapshot and the bodies it names. A main that moved under it (the script's exit 3) is computed again on
5
- // the main that won, as long as no oid the caller read (`graded`, `ifMatch`) changed: a change it did not see is a
6
- // conflict, never written over. A folder with no main yet is made in the same call (the script's init first).
7
- import { isHeld, held, retiredLearned, WORKFLOWS_DIR } from "./skills.js";
8
- import { buildIndex, compiledInto, INDEX_FILE, INDEX_LINES, mergedNote, NOTE_FILE, parseNoteFile, ruleOf, supersede } from "./notes.js";
9
- import { mergeProblems, supersededBy } from "./check.js";
10
- import { FolderRefused, FolderTooLarge, readLearning, readRunDiff, readSnapshot, runDiffKey, snapshotNotes, textsAt, topOf, writeLearning } from "./folder.js";
11
- /** The blob `path` is on the snapshot's main: the index, a note, a learned folder's file. */
1
+ import { CORRECTIONS_CAP, CORRECTIONS_FILE, correctionLine, correctionsProblems } from "./corrections.js";
2
+ import { blobOid, FolderMerged, FolderRefused, FolderTooLarge, readLearning, readRunDiff, readSnapshot, runDiffKey, runMergeOf, writeLearning } from "./folder.js";
3
+ import { held, isHeld } from "./workflow-draft.js";
4
+ /** The blob `path` is on the snapshot's main: corrections.md or a draft's file. */
12
5
  function blobOf(snapshot, path) {
13
6
  if (!snapshot)
14
7
  return undefined;
15
- if (path === INDEX_FILE)
16
- return snapshot.index.oid;
17
- const note = snapshot.notes.find((one) => one.file === path);
18
- if (note)
19
- return note.oid;
20
- for (const item of [...snapshot.skills, ...snapshot.workflows]) {
8
+ if (path === CORRECTIONS_FILE)
9
+ return snapshot.corrections?.oid;
10
+ for (const item of snapshot.workflows) {
21
11
  const file = item.files.find((one) => one.path === path);
22
12
  if (file)
23
13
  return file.oid;
24
14
  }
25
15
  return undefined;
26
16
  }
27
- const isNote = (path) => !path.includes("/") && NOTE_FILE.test(path);
28
- /** `text` as a path held for its owner's OK keeps it: a note with `status: pending` (its origin kept), a SKILL.md or a
29
- * draft's README.md with `status: pending` last in its front matter. A draft's workflow.json is held by its README. */
30
- function heldText(path, text) {
31
- if (isNote(path)) {
32
- const note = parseNoteFile(path, text);
33
- return Array.isArray(note) ? text : mergedNote(text, { origin: note.origin, pending: true });
34
- }
35
- if (path.endsWith("/SKILL.md") || path.endsWith("/README.md"))
36
- return isHeld(text) ? text : held(text);
37
- return text;
17
+ /** A write's failure as a result: the script's precondition (a file changed since the seed: a conflict), a write past
18
+ * what one exec carries, or the folder unavailable (retry later). */
19
+ function failure(error) {
20
+ if (error instanceof FolderRefused)
21
+ return { ok: false, reason: "conflict", problems: [...error.same, ...error.absent].map((path) => ({ rule: "kept", path, detail: `${path} changed on main after the run started` })) };
22
+ if (error instanceof FolderTooLarge)
23
+ return { ok: false, reason: "refused", problems: [{ rule: "too_large", detail: error.message }] };
24
+ return { ok: false, reason: "unavailable", error: error?.message ?? String(error) };
38
25
  }
26
+ /** What a write landed, from writeLearning's answer: its new main and the paths it changed. */
27
+ const landed = (done) => "pending" in done.result
28
+ ? { ok: true, main: done.main, commit: done.main, files: done.snapshot?.log[0]?.commit === done.main ? done.snapshot.log[0].files : [], ...(done.result.refusedCorrection ? { refusedCorrection: done.result.refusedCorrection } : {}) }
29
+ : done.result;
39
30
  /**
40
- * One write on the folder's main, one exec of the learning script: a run's merge or a person's write, as `change` says.
41
- * `ok` with the new main (its commit) and the paths the commit changed; `conflict` when an oid the caller read is
42
- * another blob on main now; `refused` when the folder as it would be is refused (the index past its lines, a note out
43
- * of shape); a new note named like one main holds, or of a rule one holds, is left out alone (`left`); `unavailable` when the workspace or the exec failed (retry later). A merge whose
44
- * run already merged here is `ok` with the commit that holds it, and writes nothing.
31
+ * A run's merge on the folder's main, one exec of the learning script: its files (a held README with `status: pending`)
32
+ * and its owner's correction appended to corrections.md. `ok` with the new main and the paths the commit changed;
33
+ * `conflict` when a file is not on main as it was on the run's seed; `refused` when the correction holds what must never
34
+ * be stored, or, with no draft beside it, would take corrections.md past its cap (nothing written); `unavailable` when
35
+ * the workspace or the exec failed. The draft and the correction are judged apart: a correction past the cap beside a
36
+ * draft (the file filled by another write after the run's check, correctionRefused) is left out alone, and the draft
37
+ * merges, `ok` with the cap's refusal as `refusedCorrection`. A merge whose run already merged here (this round) is `ok`
38
+ * with the commit that holds it, and writes nothing: checked before the write, again on each main it is computed on, and
39
+ * by the script in the commit's own ref transaction, so two calls racing for one round (a reclaimed run) merge it once.
45
40
  */
46
41
  export async function learningCommit(ws, change, opts = {}) {
42
+ const key = runDiffKey(change.runId, change.round);
47
43
  try {
48
- if (change.kind === "merge") {
49
- const known = await readRunDiff(ws, runDiffKey(change.runId, change.round));
50
- if (known) {
51
- // Its merge is on main already (a retry after a crash, say): the new notes it did not write it left out, said
52
- // again as the merge's own settle says them against main now (`taken` for a name main holds with another rule,
53
- // else `same`), so a resumed merge says what the first said.
54
- const written = new Set(known.files.map((file) => file.path));
55
- const unwritten = change.files.filter(({ path }) => isNote(path) && !written.has(path));
56
- const left = unwritten.length ? await readLearning(ws, async ({ snapshot, body }) => {
57
- const settled = new Map((await settledNotes({ ...change, files: unwritten }, snapshot, body)).map(({ path, reason }) => [path, reason]));
58
- return unwritten.map(({ path }) => ({ path, reason: settled.get(path) === "taken" ? "taken" : "same" }));
59
- }) : [];
60
- return { ok: true, main: (await readSnapshot(ws))?.main ?? known.merge, commit: known.merge, files: [...written], ...(left.length ? { left } : {}), already: true };
44
+ const known = await runMergeOf(ws, change.runId, change.round);
45
+ if (known)
46
+ return await already(ws, change, known.merge, known);
47
+ const line = change.correction === undefined ? undefined : correctionLine(change.correction);
48
+ // The line alone first: what it holds is refused whatever the file around it holds.
49
+ const own = line === undefined ? [] : correctionProblems(line, change.refuseValues);
50
+ if (own.length)
51
+ return { ok: false, reason: "refused", problems: own };
52
+ const marked = new Set(change.held ?? []);
53
+ const drafts = change.files.map(({ path, text }) => ({ path, text: marked.has(path) && !isHeld(text) ? held(text) : text }));
54
+ const same = change.files.flatMap(({ path }) => {
55
+ const commit = change.bases?.[path] ?? change.seed;
56
+ return commit ? [{ commit, path }] : [];
57
+ });
58
+ const done = await writeLearning(ws, async ({ snapshot, body }) => {
59
+ // Read again on each main the write is computed on: a call that merged this round since the check above (a
60
+ // reclaimed run's two claims racing) wrote its diff before the snapshot this call read.
61
+ const merged = await readRunDiff(ws, key);
62
+ if (merged)
63
+ return { result: { merged } };
64
+ const writes = [...drafts];
65
+ let refusedCorrection;
66
+ if (line !== undefined) {
67
+ const text = appended(await correctionsOf(snapshot, body), line);
68
+ const full = capProblem(text);
69
+ if (full && !drafts.length)
70
+ return { result: { ok: false, reason: "refused", problems: [full] } };
71
+ if (full)
72
+ refusedCorrection = [full];
73
+ else
74
+ writes.push({ path: CORRECTIONS_FILE, text });
61
75
  }
62
- }
63
- const done = await writeLearning(ws, async ({ snapshot, body }) => (change.kind === "merge" ? mergeOf(change, snapshot, body) : personOf(change, snapshot)), opts.retries ?? 3);
64
- if (!("pending" in done.result))
65
- return done.result;
66
- const left = done.result.left?.length ? { left: done.result.left } : {};
67
- return { ok: true, main: done.main, commit: done.main, files: done.snapshot?.log[0]?.commit === done.main ? done.snapshot.log[0].files : [], ...left };
76
+ return { result: { pending: true, ...(refusedCorrection ? { refusedCorrection } : {}) }, change: { by: run(change.runId, change.reason, change.round), writes, ...(same.length ? { same } : {}) } };
77
+ }, opts.retries ?? 3);
78
+ const { result } = done;
79
+ return "merged" in result ? await already(ws, change, result.merged.merge, result.merged) : landed({ ...done, result });
68
80
  }
69
81
  catch (error) {
70
- // The script's exit 4: a file changed since the seed is a conflict; a name held on main by a file that is no note
71
- // (a create over it) is refused.
72
- if (error instanceof FolderRefused) {
73
- if (error.same.length)
74
- return { ok: false, reason: "conflict", problems: error.same.map((path) => ({ rule: "supersedes", path, detail: `${path} changed on main after the run started` })) };
75
- return { ok: false, reason: "refused", problems: error.absent.map((path) => ({ rule: "exists", path, detail: "the folder has a note by this name" })) };
76
- }
77
- if (error instanceof FolderTooLarge)
78
- return { ok: false, reason: "refused", problems: [{ rule: "too_large", detail: error.message }] };
79
- return { ok: false, reason: "unavailable", error: error?.message ?? String(error) };
82
+ // The round merged under this call past both reads (its diff not written yet): the script's ref transaction let
83
+ // one merge of it land, and this call reads that one.
84
+ if (error instanceof FolderMerged)
85
+ return already(ws, change, error.merge, await readRunDiff(ws, key).catch(() => undefined)).catch(failure);
86
+ return failure(error);
80
87
  }
81
88
  }
82
- /** A run's merge on main as the snapshot has it: its files (the held ones pending), the notes it supersedes marked, and
83
- * MEMORY.md rebuilt from what is active then. */
84
- async function mergeOf(change, snapshot, body) {
85
- const held = new Set(change.held ?? []);
86
- const replaced = new Set(change.replace ?? []);
87
- const left = await settledNotes(change, snapshot, body);
88
- const leftOut = new Set(left.map(({ path }) => path));
89
- // Each new note as main keeps it (mergedNote): its own origin, `status: pending` when held, the run's risky mark gone.
90
- // A skill's SKILL.md or a draft's README.md held: `status: pending` last in its front matter.
91
- const files = new Map(change.files.filter(({ path }) => !leftOut.has(path)).map(({ path, text }) => {
92
- if (isNote(path) && blobOf(snapshot, path) === undefined) {
93
- const note = parseNoteFile(path, text);
94
- return [path, Array.isArray(note) ? text : mergedNote(text, { origin: note.origin, pending: held.has(path) })];
95
- }
96
- return [path, held.has(path) ? heldText(path, text) : text];
97
- }));
98
- // Each re-filed skill main holds, not retired, retired in this write (`retire`), unless its note was left out as taken.
99
- const retiring = [];
100
- // The rules main's active notes hold now, apart from the notes this merge supersedes: a skill retires only while its rule
101
- // has an active note, the re-filed note written here or another main holds as this merge commits (a person may have
102
- // edited or retired it since the grade read main), so the rule is never left with no active copy.
103
- const superseding = new Set([...(change.supersede ?? []).map(({ path }) => path), ...[...files].filter(([path]) => isNote(path) && !held.has(path)).flatMap(([path, text]) => {
104
- const one = parseNoteFile(path, text);
105
- return Array.isArray(one) ? [] : one.supersedes;
106
- })]);
107
- const holding = new Set();
108
- if (change.retire?.length) {
109
- for (const one of snapshotNotes(snapshot).filter((note) => note.status === "active" && !superseding.has(note.file))) {
110
- const text = parseNoteFile(one.file, await body(one.file, blobOf(snapshot, one.file)));
111
- if (!Array.isArray(text))
112
- holding.add(ruleOf(text.body));
113
- }
114
- for (const [path, text] of files) {
115
- // A replacement written here counts held or not: a pending note retires its skill as before (the owner's OK makes it
116
- // the rule); only one that is not written at all (left out, or gone from main) must find another holder.
117
- const one = isNote(path) ? parseNoteFile(path, text) : undefined;
118
- if (one && !Array.isArray(one))
119
- holding.add(ruleOf(one.body));
120
- }
121
- }
122
- for (const { skill, note, rule } of change.retire ?? []) {
123
- if (left.some((one) => one.path === note && one.reason === "taken"))
124
- continue;
125
- if (!holding.has(rule))
126
- continue;
127
- const oid = blobOf(snapshot, skill);
128
- const item = snapshot?.skills.find((one) => one.files.some((file) => file.path === skill));
129
- if (oid === undefined || !item || item.status === "retired")
130
- continue;
131
- files.set(skill, retiredLearned(await body(skill, oid)));
132
- retiring.push(skill);
133
- }
134
- // Nothing left to write: every new note is one main holds already, or a name it holds. No commit.
135
- if (![...files.keys()].length)
136
- return { result: { ok: true, main: snapshot?.main ?? "", commit: snapshot?.main ?? "", files: [], left } };
137
- const notes = snapshotNotes(snapshot);
138
- const active = new Set(notes.filter((note) => note.status === "active").map((note) => note.file));
139
- const refused = [];
140
- const fresh = [...files].filter(([path]) => isNote(path) && (blobOf(snapshot, path) === undefined || replaced.has(path))).flatMap(([path, text]) => {
141
- const note = parseNoteFile(path, text);
142
- return Array.isArray(note) ? [] : [note];
143
- });
144
- // A note written over main's by its own name supersedes that note by being it: never a mark of itself.
145
- const marks = change.supersede ?? [...supersededBy(fresh)].filter(([path, by]) => path !== by).map(([path, by]) => ({ path, by }));
146
- // The verdict was on the blobs the grade read: one this merge would write (mark or replace) that is another blob on
147
- // main now is the caller's conflict, named by the note that would mark it. One it leaves alone (a held note marks
148
- // nothing) is not written over, so it stands.
149
- const touched = new Set([...marks.map((mark) => mark.path), ...change.files.map(({ path }) => path)]);
150
- const conflicts = Object.entries(change.graded).filter(([path, oid]) => touched.has(path) && blobOf(snapshot, path) !== oid).map(([path]) => ({ rule: "supersedes", path: marks.find((mark) => mark.path === path)?.by ?? path, detail: `${path} changed after the grade read it` }));
151
- if (conflicts.length)
152
- return { result: { ok: false, reason: "conflict", problems: conflicts } };
153
- for (const { path, by } of marks) {
154
- if (!active.has(path))
155
- refused.push({ rule: "supersedes", path: by, detail: `it supersedes ${path}, which is not an active note` });
156
- else
157
- files.set(path, supersede(await body(path, blobOf(snapshot, path)), by));
158
- }
159
- // The notes the merge's compiled workflow does the work of, marked: an active one this merge writes nothing else to
160
- // (a note it supersedes is no longer one to serve), its text on main as it is now. One gone or not active is left alone.
161
- const compiledMarks = new Set();
162
- if (change.compiled && files.has(`${WORKFLOWS_DIR}/${change.compiled.workflow}/workflow.json`)) {
163
- for (const path of change.compiled.notes) {
164
- if (!active.has(path) || files.has(path))
165
- continue;
166
- files.set(path, compiledInto(await body(path, blobOf(snapshot, path)), change.compiled.workflow));
167
- compiledMarks.add(path);
168
- }
169
- }
170
- const added = [];
171
- const written = [];
172
- for (const [path, text] of files) {
173
- if (!isNote(path))
174
- continue;
175
- const note = parseNoteFile(path, text);
176
- if (Array.isArray(note)) {
177
- refused.push({ rule: "note", path, detail: note.join("; ") });
178
- continue;
179
- }
180
- written.push(note);
181
- // A merge adds notes and marks the ones they supersede: it never writes over another note main holds.
182
- if (blobOf(snapshot, path) === undefined)
183
- added.push(note);
184
- else if (!marks.some((one) => one.path === path) && !replaced.has(path) && !compiledMarks.has(path))
185
- refused.push({ rule: "exists", path, detail: "the folder has a note by this name" });
186
- }
187
- // mergeProblems asks of the new notes what a merge always asks: a free name, what they supersede active and named
188
- // once, the index's lines.
189
- if (!refused.length)
190
- refused.push(...mergeProblems({ files: topOf(snapshot), active: notes.filter((note) => note.status === "active") }, added));
191
- if (refused.length)
192
- return { result: { ok: false, reason: "refused", problems: refused } };
193
- const marked = new Set(written.map((note) => note.file));
194
- files.set(INDEX_FILE, buildIndex([...notes.filter((note) => !marked.has(note.file)), ...written]));
195
- return {
196
- result: { pending: true, ...(left.length ? { left } : {}) },
197
- change: {
198
- by: { run: change.runId, reason: change.reason, ...(change.round !== undefined && change.round > 1 ? { round: change.round } : {}) },
199
- writes: [...files].map(([path, text]) => ({ path, text })),
200
- absent: added.map((note) => note.file),
201
- ...(change.seed || change.bases ? { same: [...change.files.filter(({ path }) => !isNote(path)).map(({ path }) => path), ...retiring].flatMap((path) => {
202
- const commit = change.bases?.[path] ?? change.seed;
203
- return commit ? [{ commit, path }] : [];
204
- }) } : {}),
205
- },
206
- };
89
+ /** A round that merged already, `merge` its commit: `ok` with it and the paths its diff names (none when its diff is
90
+ * not written yet), nothing written again. Its merge left the correction out only at the cap. */
91
+ async function already(ws, change, merge, known) {
92
+ const left = known !== undefined && change.correction !== undefined && !known.files.some((file) => file.path === CORRECTIONS_FILE);
93
+ const refusedCorrection = left ? [{ rule: "too_large", path: CORRECTIONS_FILE, detail: `${CORRECTIONS_FILE} was at its cap of ${CORRECTIONS_CAP} bytes when the run's merge was written` }] : [];
94
+ return { ok: true, main: (await readSnapshot(ws))?.main ?? merge, commit: merge, files: known ? known.files.map((file) => file.path) : [], already: true, ...(refusedCorrection.length ? { refusedCorrection } : {}) };
207
95
  }
208
- /** Each new note of a merge settled against main as `snapshot` has it, one note at a time and never a refusal of the
209
- * merge: a name main holds a note by (and the merge does not replace) is left out, `same` when main's note says the
210
- * same rule, else `taken`; a rule main's active notes hold under another name, or an earlier note of this merge holds,
211
- * is left out as `same` (notes.ts ruleOf), unless the note supersedes the note it repeats (an update). */
212
- async function settledNotes(change, snapshot, body) {
213
- const replaced = new Set(change.replace ?? []);
214
- const left = [];
215
- const newNotes = change.files.filter(({ path }) => isNote(path));
216
- if (newNotes.length) {
217
- // The notes this merge supersedes (its own marks, or what its new notes not held for the owner's OK supersede by
218
- // their front matter) keep no rule after it: a copy of their rule under another name is no duplicate, so a rule is
219
- // never left with no active note.
220
- const held = new Set(change.held ?? []);
221
- const going = new Set([...(change.supersede ?? []).map(({ path }) => path), ...newNotes.filter(({ path }) => !held.has(path)).flatMap(({ path, text }) => {
222
- const note = parseNoteFile(path, text);
223
- return Array.isArray(note) ? [] : note.supersedes;
224
- })]);
225
- const rules = new Map();
226
- // Every rule main's active notes hold, whoever this merge supersedes: an update of that holder takes the rule over
227
- // from an earlier note of this merge that repeated it, whatever their order.
228
- const onMainRules = new Map();
229
- for (const note of snapshotNotes(snapshot).filter((one) => one.status === "active")) {
230
- const text = parseNoteFile(note.file, await body(note.file, blobOf(snapshot, note.file)));
231
- if (Array.isArray(text))
232
- continue;
233
- onMainRules.set(ruleOf(text.body), note.file);
234
- if (!going.has(note.file))
235
- rules.set(ruleOf(text.body), note.file);
236
- }
237
- for (const { path, text } of newNotes) {
238
- const note = parseNoteFile(path, text);
239
- if (Array.isArray(note))
240
- continue;
241
- const rule = ruleOf(note.body);
242
- const onMain = blobOf(snapshot, path);
243
- if (onMain !== undefined && !replaced.has(path)) {
244
- const theirs = parseNoteFile(path, await body(path, onMain));
245
- // The same rule only when main's note keeps it active: a retired or superseded one is a name main holds.
246
- left.push({ path, reason: !Array.isArray(theirs) && theirs.status === "active" && ruleOf(theirs.body) === rule ? "same" : "taken" });
247
- continue;
248
- }
249
- const holder = rules.get(rule);
250
- if (holder !== undefined && holder !== path && !note.supersedes.includes(holder)) {
251
- const original = onMainRules.get(rule);
252
- // An update of the rule's holder on main, after a copy of it: the update holds the rule, the copy is the repeat.
253
- if (original === undefined || holder === original || !note.supersedes.includes(original) || held.has(path)) {
254
- left.push({ path, reason: "same" });
255
- continue;
256
- }
257
- left.push({ path: holder, reason: "same" });
258
- }
259
- rules.set(rule, path);
260
- }
261
- }
262
- return left;
263
- }
264
- /** A person's write on main as the snapshot has it: the file and what the action also changes, MEMORY.md rebuilt. */
265
- async function personOf(change, snapshot) {
266
- const current = blobOf(snapshot, change.file);
267
- const seen = change.ifMatch?.replace(/^"|"$/g, "") ?? null;
268
- if (change.action === "created" ? current !== undefined : seen === null || current !== seen) {
269
- return { result: { ok: false, reason: "conflict", problems: [{ rule: change.action === "created" ? "exists" : "supersedes", path: change.file, detail: current === undefined ? `${change.file} is not on main` : change.action === "created" ? "the folder has a note by this name" : `${change.file} changed since it was read` }] } };
270
- }
271
- const writes = [{ path: change.file, text: change.text }, ...(change.also ?? [])];
272
- const changed = new Map(writes.filter(({ path }) => isNote(path)).map(({ path, text }) => [path, parseNoteFile(path, text)]));
273
- const refused = [...changed].filter(([, note]) => Array.isArray(note)).map(([path, note]) => ({ rule: "note", path, detail: note.join("; ") }));
274
- if (refused.length)
275
- return { result: { ok: false, reason: "refused", problems: refused } };
276
- const after = [...snapshotNotes(snapshot).filter((note) => !changed.has(note.file)), ...[...changed.values()]];
277
- const lines = after.filter((note) => note.status === "active").length;
278
- if (lines > INDEX_LINES)
279
- return { result: { ok: false, reason: "refused", problems: [{ rule: "index_full", detail: `the index would hold ${lines} lines; it holds at most ${INDEX_LINES}, so retire a note first` }] } };
280
- return {
281
- result: { pending: true },
282
- change: { by: { person: change.action, file: change.file, approved: change.via === "console" }, writes: [...writes, { path: INDEX_FILE, text: buildIndex(after) }], ...(change.action === "created" ? { absent: [change.file] } : {}) },
283
- };
284
- }
285
- /** The texts main holds at `paths`, each a note's file, by path: one main holds no note at is not in it. From the
286
- * folder's snapshot and its bodies: no git, no exec. What a skill the grader re-filed as a note (grade.ts skillAsNote)
287
- * would be written over, read before its merge. */
288
- export async function mainTexts(ws, paths) {
289
- if (!paths.length)
290
- return {};
291
- return readLearning(ws, async ({ snapshot, body }) => {
292
- const oids = new Map((snapshot?.notes ?? []).map((note) => [note.file, note.oid]));
293
- const held = paths.filter((path) => oids.has(path));
294
- return Object.fromEntries(await Promise.all(held.map(async (path) => [path, await body(path, oids.get(path))])));
295
- });
96
+ const run = (runId, reason, round) => ({ run: runId, reason, ...(round !== undefined && round > 1 ? { round } : {}) });
97
+ /** corrections.md's text as main holds it now ("" when it holds none). */
98
+ const correctionsOf = async (snapshot, body) => snapshot?.corrections ? body(CORRECTIONS_FILE, snapshot.corrections.oid) : "";
99
+ /** corrections.md's text `before` with `line` appended on a line of its own. */
100
+ const appended = (before, line) => `${before}${before && !before.endsWith("\n") ? "\n" : ""}${line}`;
101
+ /** What a correction's line holds that must never be stored, whatever the file around it holds. */
102
+ const correctionProblems = (line, refuseValues) => correctionsProblems(line, refuseValues).filter((problem) => problem.rule !== "too_large");
103
+ /** The cap's refusal of corrections.md as `text`, in its words; none within the cap. */
104
+ const capProblem = (text) => {
105
+ const bytes = Buffer.byteLength(text, "utf8");
106
+ return bytes > CORRECTIONS_CAP ? { rule: "too_large", path: CORRECTIONS_FILE, detail: `${CORRECTIONS_FILE} would hold ${bytes} bytes; the cap is ${CORRECTIONS_CAP} bytes` } : undefined;
107
+ };
108
+ /** Why run `runId`'s correction (`text`) would be refused on main as it is now: its line's own problems, else the cap's;
109
+ * empty when it would be taken. Read before the run's merge, so a refused correction never holds its draft back. A
110
+ * round (`merge`) whose merge is on main already (a stop after its commit, before the run's end was saved) is never
111
+ * checked for room again, its own line counted twice: empty, and its merge (learningCommit) gives back what it did. */
112
+ export async function correctionRefused(ws, text, refuseValues, merge) {
113
+ const line = correctionLine(text);
114
+ const own = correctionProblems(line, refuseValues);
115
+ if (own.length)
116
+ return own;
117
+ if (merge && (await runMergeOf(ws, merge.runId, merge.round)))
118
+ return [];
119
+ const before = await readLearning(ws, ({ snapshot, body }) => correctionsOf(snapshot, body));
120
+ const full = capProblem(appended(before, line));
121
+ return full ? [full] : [];
296
122
  }
297
- /** Run `runId`'s merge on the folder, when its diff file names one (readRunDiff): the texts its own commit holds at
298
- * `paths`, each a note's file (one it holds nothing at is not in it), read at that commit, those paths alone (textsAt:
299
- * one read-only exec, bounded by the paths asked, never the whole folder or its log), never main's now, which a person
300
- * may have edited since. Undefined when the run merged nothing here. */
301
- export async function mergedAt(ws, runId, paths, round) {
302
- const diff = await readRunDiff(ws, runDiffKey(runId, round));
303
- if (!diff)
304
- return undefined;
305
- const texts = await textsAt(ws, diff.merge, paths);
306
- if (!texts)
307
- throw new Error(`the run's merge ${diff.merge} is not in the learning folder`);
308
- return { texts };
123
+ /** One line of an owner's feedback appended to corrections.md on main, by run `runId`'s merge (learningCommit's
124
+ * `correction`, with no draft beside it). */
125
+ export const appendCorrection = (ws, input) => learningCommit(ws, { runId: input.runId, ...(input.round !== undefined ? { round: input.round } : {}), reason: "the owner's correction", files: [], correction: input.text, ...(input.refuseValues ? { refuseValues: input.refuseValues } : {}) });
126
+ /** corrections.md as a person reads it: its text and its ETag's blob (an empty file's when main holds none). */
127
+ export async function readCorrections(snapshot, body) {
128
+ const text = await correctionsOf(snapshot, body);
129
+ return { text, oid: snapshot?.corrections?.oid ?? blobOid("") };
309
130
  }
310
- /** Main as the grader reads it (the runner's learning loop `readMain`): every active note's index line, the whole text
311
- * of each note the proposal supersedes, and the blob each of those was read at (`graded`, which the merge checks); the
312
- * proposal's notes named like a note main holds now, settled alone (`taken`); or `problems` when the proposal cannot
313
- * join main as it is (learn/check.ts mergeProblems). From the folder's snapshot and its bodies: no git, no exec. */
314
- export async function readMain(ws, proposal) {
315
- const added = proposal.notes.map(({ file, text }) => parseNoteFile(file, text));
316
- const bad = proposal.notes.filter((_, i) => Array.isArray(added[i])).map(({ file }) => ({ rule: "note", path: file, detail: "the proposal's note is out of shape" }));
317
- if (bad.length)
318
- return { problems: bad };
319
- return readLearning(ws, async ({ snapshot, body }) => {
320
- const main = snapshotNotes(snapshot);
321
- const oids = new Map((snapshot?.notes ?? []).map((note) => [note.file, note.oid]));
322
- // A new note named like one main holds (another run merged it since the run's seed, so the run never saw it) is
323
- // settled alone, never a drop of the whole proposal: left out of the grade, `same` when main's note says its rule,
324
- // else `taken`, and the rest is graded.
325
- const taken = [];
326
- for (const note of added) {
327
- if (!oids.has(note.file))
328
- continue;
329
- const theirs = parseNoteFile(note.file, await body(note.file, oids.get(note.file)));
330
- taken.push({ path: note.file, reason: !Array.isArray(theirs) && theirs.status === "active" && ruleOf(theirs.body) === ruleOf(note.body) ? "same" : "taken" });
331
- }
332
- const notes = added.filter((note) => !oids.has(note.file));
333
- const problems = mergeProblems({ files: topOf(snapshot), active: main.filter((note) => note.status === "active") }, notes);
334
- if (problems.length)
335
- return { problems };
336
- // mergeProblems passed: each note superseded is active on main, and named once.
337
- const superseded = await Promise.all(notes.flatMap((note) => note.supersedes).map(async (file) => ({ file, text: await body(file, oids.get(file)) })));
338
- return { active: main.filter((note) => note.status === "active").map(({ file, name, description }) => ({ file, name, description })), superseded, graded: Object.fromEntries(superseded.map(({ file }) => [file, oids.get(file)])), ...(taken.length ? { taken } : {}) };
131
+ /**
132
+ * corrections.md replaced whole by a person: `ifMatch`, the blob they read (an ETag, quoted or not; an empty file's when
133
+ * main held none), must be main's now, else a conflict. Refused, nothing written, as appendCorrection refuses. `ok` with
134
+ * the new text and its blob.
135
+ */
136
+ export async function putCorrections(ws, input) {
137
+ const problems = correctionsProblems(input.text, input.refuseValues);
138
+ if (problems.length)
139
+ return { ok: false, reason: "refused", problems };
140
+ const seen = input.ifMatch.trim().replace(/^W\//, "").replace(/^"|"$/g, "");
141
+ const { result } = await writeLearning(ws, async ({ snapshot }) => {
142
+ const now = snapshot?.corrections?.oid ?? blobOid("");
143
+ if (now !== seen)
144
+ return { result: { ok: false, reason: "conflict", problems: [{ rule: "kept", path: CORRECTIONS_FILE, detail: `${CORRECTIONS_FILE} changed since it was read` }] } };
145
+ return { result: { ok: true, text: input.text, oid: blobOid(input.text) }, change: { by: { person: "edited", file: CORRECTIONS_FILE }, writes: [{ path: CORRECTIONS_FILE, text: input.text }] } };
339
146
  });
147
+ return result;
340
148
  }