@parcha/agentrun-serve 0.1.0-beta.35 → 0.1.0-beta.36

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 (372) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/README.md +260 -168
  3. package/dist/adapters.d.ts +12 -2
  4. package/dist/adapters.js +27 -2
  5. package/dist/agent-rows.d.ts +2 -1
  6. package/dist/agents-store.d.ts +2 -0
  7. package/dist/agents-store.js +9 -4
  8. package/dist/api-only.d.ts +2 -1
  9. package/dist/api-only.js +3 -1
  10. package/dist/archil-routes.d.ts +40 -7
  11. package/dist/archil-routes.js +167 -54
  12. package/dist/archil-settings.d.ts +79 -7
  13. package/dist/archil-settings.js +140 -14
  14. package/dist/attach-images.d.ts +15 -0
  15. package/dist/attach-images.js +59 -0
  16. package/dist/background.d.ts +0 -6
  17. package/dist/background.js +0 -28
  18. package/dist/box-image.d.ts +1 -97
  19. package/dist/box-image.js +4 -284
  20. package/dist/box-paths.d.ts +2 -7
  21. package/dist/box-paths.js +7 -13
  22. package/dist/box-settings.d.ts +0 -11
  23. package/dist/box-settings.js +4 -29
  24. package/dist/browser-commands.js +1 -1
  25. package/dist/browser-computer.d.ts +47 -3
  26. package/dist/browser-computer.js +286 -18
  27. package/dist/browser-person.d.ts +19 -2
  28. package/dist/browser-person.js +55 -6
  29. package/dist/browser-wait-ended.d.ts +23 -0
  30. package/dist/browser-wait-ended.js +42 -0
  31. package/dist/cap.d.ts +13 -41
  32. package/dist/cap.js +24 -73
  33. package/dist/cli.js +4 -22
  34. package/dist/computer-routes.d.ts +1 -4
  35. package/dist/computer-routes.js +3 -5
  36. package/dist/computer-setting.d.ts +2 -0
  37. package/dist/computer-setting.js +38 -1
  38. package/dist/computer-view.d.ts +11 -2
  39. package/dist/computer-view.js +2 -1
  40. package/dist/computers.d.ts +48 -7
  41. package/dist/computers.js +100 -23
  42. package/dist/console/assets/index-BI4Byidu.js +550 -0
  43. package/dist/console/assets/index-C19gUF2n.css +1 -0
  44. package/dist/console/index.html +2 -2
  45. package/dist/docker-daemon.d.ts +2 -2
  46. package/dist/docker-daemon.js +2 -2
  47. package/dist/errors.js +5 -5
  48. package/dist/faux-computers.js +4 -0
  49. package/dist/files.d.ts +1 -9
  50. package/dist/files.js +1 -9
  51. package/dist/github.d.ts +22 -2
  52. package/dist/github.js +42 -8
  53. package/dist/gpu-words.d.ts +14 -0
  54. package/dist/gpu-words.js +21 -0
  55. package/dist/graph-view.d.ts +2 -0
  56. package/dist/graph-view.js +25 -1
  57. package/dist/handed-values.d.ts +1 -1
  58. package/dist/handed-values.js +1 -1
  59. package/dist/harness-install.d.ts +60 -3
  60. package/dist/harness-install.js +322 -34
  61. package/dist/harness.js +1 -1
  62. package/dist/host-tools.d.ts +8 -9
  63. package/dist/host-tools.js +7 -7
  64. package/dist/index.d.ts +1 -3
  65. package/dist/index.js +1 -2
  66. package/dist/key-refusal.d.ts +1 -1
  67. package/dist/key-refusal.js +2 -2
  68. package/dist/learn/check.d.ts +7 -1
  69. package/dist/learn/check.js +123 -5
  70. package/dist/learn/commit.d.ts +40 -6
  71. package/dist/learn/commit.js +164 -20
  72. package/dist/learn/folder.d.ts +5 -0
  73. package/dist/learn/folder.js +33 -0
  74. package/dist/learn/grade.d.ts +18 -1
  75. package/dist/learn/grade.js +86 -16
  76. package/dist/learn/index.d.ts +1 -1
  77. package/dist/learn/index.js +1 -1
  78. package/dist/learn/learning.sh +12 -1
  79. package/dist/learn/notes.d.ts +3 -0
  80. package/dist/learn/notes.js +3 -0
  81. package/dist/learn/reflect.d.ts +5 -2
  82. package/dist/learn/reflect.js +43 -29
  83. package/dist/learn/skills.d.ts +4 -0
  84. package/dist/learn/skills.js +7 -0
  85. package/dist/learned-nodes.d.ts +6 -2
  86. package/dist/learned-workflow.d.ts +19 -9
  87. package/dist/learned-workflow.js +62 -34
  88. package/dist/learning-cost.d.ts +79 -0
  89. package/dist/learning-cost.js +69 -0
  90. package/dist/learning-routes.d.ts +8 -3
  91. package/dist/learning-routes.js +9 -6
  92. package/dist/learning-words.d.ts +3 -0
  93. package/dist/learning-words.js +6 -2
  94. package/dist/learning.d.ts +13 -6
  95. package/dist/learning.js +4 -2
  96. package/dist/mcp-probe.d.ts +8 -1
  97. package/dist/mcp-probe.js +62 -20
  98. package/dist/mcp-secret-writes.js +4 -0
  99. package/dist/mcp-servers-store.js +8 -3
  100. package/dist/model.d.ts +5 -5
  101. package/dist/model.js +6 -6
  102. package/dist/openapi.d.ts +2 -0
  103. package/dist/openapi.js +204 -47
  104. package/dist/owner.d.ts +0 -1
  105. package/dist/owner.js +0 -2
  106. package/dist/paid-calls.js +1 -1
  107. package/dist/place-offers.d.ts +97 -0
  108. package/dist/place-offers.js +206 -0
  109. package/dist/place.d.ts +9 -13
  110. package/dist/place.js +22 -32
  111. package/dist/port-models.d.ts +2 -1
  112. package/dist/port-models.js +5 -3
  113. package/dist/print.d.ts +1 -8
  114. package/dist/print.js +1 -14
  115. package/dist/propose.d.ts +1 -0
  116. package/dist/propose.js +25 -10
  117. package/dist/run-cut.d.ts +5 -0
  118. package/dist/run-cut.js +19 -0
  119. package/dist/run-db.js +3 -3
  120. package/dist/run-launch.d.ts +14 -1
  121. package/dist/run-launch.js +7 -2
  122. package/dist/run-learning-served.d.ts +1 -1
  123. package/dist/run-manifest.d.ts +2 -2
  124. package/dist/run-manifest.js +2 -2
  125. package/dist/run-secrets.js +3 -3
  126. package/dist/run-timeline-served.js +56 -44
  127. package/dist/run-view.d.ts +5 -2
  128. package/dist/run-view.js +4 -3
  129. package/dist/runnable.d.ts +2 -2
  130. package/dist/runnable.js +6 -2
  131. package/dist/runner/adapter-loop.d.ts +16 -1
  132. package/dist/runner/adapter-loop.js +131 -30
  133. package/dist/runner/adapter-run-spec.d.ts +25 -4
  134. package/dist/runner/adapter-run-spec.js +103 -24
  135. package/dist/runner/adapters/pi.d.ts +11 -3
  136. package/dist/runner/adapters/pi.js +35 -12
  137. package/dist/runner/beside.js +6 -2
  138. package/dist/runner/cli-run-spec.d.ts +7 -0
  139. package/dist/runner/cli-run-spec.js +92 -38
  140. package/dist/runner/commands.d.ts +5 -1
  141. package/dist/runner/commands.js +3 -3
  142. package/dist/runner/conformance/harness.d.ts +0 -1
  143. package/dist/runner/conformance/harness.js +8 -5
  144. package/dist/runner/github-app.js +2 -2
  145. package/dist/runner/harness-loops.js +1 -1
  146. package/dist/runner/journal-kinds.d.ts +15 -5
  147. package/dist/runner/journal-kinds.js +6 -1
  148. package/dist/runner/learning-end.d.ts +15 -0
  149. package/dist/runner/learning-end.js +26 -5
  150. package/dist/runner/learning-factory.js +6 -2
  151. package/dist/runner/learning-loop.d.ts +73 -1
  152. package/dist/runner/learning-loop.js +203 -18
  153. package/dist/runner/learning-seed.d.ts +2 -0
  154. package/dist/runner/learning-seed.js +1 -1
  155. package/dist/runner/loop-contract.d.ts +8 -0
  156. package/dist/runner/loop-contract.js +6 -0
  157. package/dist/runner/loop.d.ts +5 -1
  158. package/dist/runner/loop.js +20 -0
  159. package/dist/runner/main.js +12 -2
  160. package/dist/runner/mcp-claim.d.ts +4 -0
  161. package/dist/runner/mcp-claim.js +29 -1
  162. package/dist/runner/mcp-github.d.ts +4 -2
  163. package/dist/runner/mcp-github.js +0 -7
  164. package/dist/runner/mcp-run.d.ts +2 -0
  165. package/dist/runner/mcp-run.js +1 -0
  166. package/dist/runner/metrics.d.ts +4 -0
  167. package/dist/runner/metrics.js +9 -0
  168. package/dist/runner/model-route.d.ts +3 -0
  169. package/dist/runner/model-route.js +5 -2
  170. package/dist/runner/pr-checkout.d.ts +59 -0
  171. package/dist/runner/pr-checkout.js +176 -0
  172. package/dist/runner/reflect-hook.d.ts +1 -0
  173. package/dist/runner/reflect-hook.js +3 -3
  174. package/dist/runner/runner.js +47 -12
  175. package/dist/runner/spec-holds.d.ts +8 -3
  176. package/dist/runner/spec-holds.js +12 -10
  177. package/dist/runner/store-port.d.ts +4 -1
  178. package/dist/runner/store-secrets.js +17 -1
  179. package/dist/runner/subagent.d.ts +44 -0
  180. package/dist/runner/subagent.js +188 -0
  181. package/dist/runner/workflow-host.d.ts +18 -2
  182. package/dist/runner/workflow-host.js +23 -3
  183. package/dist/runner/workflow-loop.d.ts +5 -2
  184. package/dist/runner/workflow-loop.js +9 -8
  185. package/dist/runner/workflow-spec.d.ts +26 -14
  186. package/dist/runner/workflow-spec.js +97 -30
  187. package/dist/runs-over-store.d.ts +11 -6
  188. package/dist/runs-over-store.js +111 -24
  189. package/dist/sealing.js +1 -2
  190. package/dist/secret-cut.d.ts +1 -1
  191. package/dist/secret-cut.js +1 -1
  192. package/dist/server.d.ts +32 -49
  193. package/dist/server.js +249 -199
  194. package/dist/store/agent-body.d.ts +9 -2
  195. package/dist/store/agent-body.js +16 -8
  196. package/dist/store/deadline.js +9 -5
  197. package/dist/store/index.d.ts +3 -3
  198. package/dist/store/index.js +2 -2
  199. package/dist/store/journal/storage.js +2 -1
  200. package/dist/store/postgres/connection.js +9 -4
  201. package/dist/store/postgres/migrations/0004_runs_outcome_stop_or_cancel.sql +11 -0
  202. package/dist/store/schema.d.ts +2 -1
  203. package/dist/store/schema.js +11 -8
  204. package/dist/store/seal.d.ts +13 -0
  205. package/dist/store/seal.js +19 -0
  206. package/dist/store/sqlite/connection.d.ts +5 -0
  207. package/dist/store/sqlite/connection.js +88 -47
  208. package/dist/store/sqlite/run-patch.d.ts +18 -2
  209. package/dist/store/sqlite/run-patch.js +31 -2
  210. package/dist/store/sqlite/store.js +65 -11
  211. package/dist/store/store.d.ts +62 -12
  212. package/dist/store/store.js +22 -1
  213. package/dist/store-reads/analytics.js +3 -2
  214. package/dist/store-reads/cap.d.ts +1 -3
  215. package/dist/store-reads/cap.js +8 -6
  216. package/dist/store-reads/decisions.d.ts +2 -2
  217. package/dist/store-reads/decisions.js +3 -6
  218. package/dist/store-reads/journal.js +45 -12
  219. package/dist/store-reads/learning.d.ts +15 -1
  220. package/dist/store-reads/learning.js +68 -10
  221. package/dist/store-reads/runs.d.ts +6 -4
  222. package/dist/store-reads/runs.js +17 -10
  223. package/dist/store-reads/stats.js +5 -1
  224. package/dist/store-view.js +45 -19
  225. package/dist/template-eval.js +2 -1
  226. package/dist/templates.d.ts +64 -0
  227. package/dist/templates.js +101 -16
  228. package/dist/timeline/timeline.d.ts +3 -1
  229. package/dist/timeline/timeline.js +19 -4
  230. package/dist/tool-blurbs.js +1 -0
  231. package/dist/tool-budget.d.ts +8 -0
  232. package/dist/tool-budget.js +7 -1
  233. package/dist/tool-gate.d.ts +7 -1
  234. package/dist/tool-gate.js +7 -2
  235. package/dist/tool-plane.d.ts +33 -19
  236. package/dist/tool-plane.js +64 -27
  237. package/dist/tools.d.ts +18 -34
  238. package/dist/tools.js +104 -70
  239. package/dist/trigger-signatures.d.ts +1 -0
  240. package/dist/trigger-signatures.js +3 -1
  241. package/dist/triggers.d.ts +3 -3
  242. package/dist/triggers.js +2 -2
  243. package/dist/turn-end.d.ts +0 -3
  244. package/dist/turn-end.js +0 -3
  245. package/dist/usage-lines.d.ts +1 -1
  246. package/dist/validate.d.ts +6 -0
  247. package/dist/validate.js +30 -3
  248. package/dist/webhooks.d.ts +2 -1
  249. package/dist/webhooks.js +5 -2
  250. package/dist/workflow/decision-recovery.d.ts +23 -0
  251. package/dist/workflow/decision-recovery.js +80 -0
  252. package/dist/workflow/decisions.d.ts +134 -0
  253. package/dist/workflow/decisions.js +46 -0
  254. package/dist/workflow/host-session.d.ts +5 -0
  255. package/dist/workflow/host-session.js +85 -0
  256. package/dist/workflow/host-tools.d.ts +3 -0
  257. package/dist/workflow/host-tools.js +3 -0
  258. package/dist/workflow/node-runner.d.ts +112 -0
  259. package/dist/workflow/node-runner.js +270 -0
  260. package/dist/workflow/node.d.ts +88 -0
  261. package/dist/workflow/node.js +99 -0
  262. package/dist/workflow/operator-error.d.ts +4 -0
  263. package/dist/workflow/operator-error.js +4 -0
  264. package/dist/workflow/pi-versions.d.ts +9 -0
  265. package/dist/workflow/pi-versions.js +8 -0
  266. package/dist/workflow/readers.d.ts +36 -0
  267. package/dist/workflow/readers.js +145 -0
  268. package/dist/workflow/record-file.d.ts +10 -0
  269. package/dist/workflow/record-file.js +56 -0
  270. package/dist/workflow/record-tool.d.ts +76 -0
  271. package/dist/workflow/record-tool.js +126 -0
  272. package/dist/workflow/record.d.ts +151 -0
  273. package/dist/workflow/record.js +401 -0
  274. package/dist/workflow/run-error.d.ts +16 -0
  275. package/dist/workflow/run-error.js +26 -0
  276. package/dist/workflow/runner.d.ts +6 -0
  277. package/dist/workflow/runner.js +240 -0
  278. package/dist/workflow/store.d.ts +23 -0
  279. package/dist/workflow/store.js +242 -0
  280. package/dist/workflow/task.d.ts +133 -0
  281. package/dist/workflow/task.js +411 -0
  282. package/dist/workflow/thinking.d.ts +3 -0
  283. package/dist/workflow/thinking.js +7 -0
  284. package/dist/workflow/tool-context.d.ts +21 -0
  285. package/dist/workflow/tool-context.js +13 -0
  286. package/dist/workflow/types.d.ts +126 -0
  287. package/dist/workflow/types.js +1 -0
  288. package/dist/workflow/wal.d.ts +11 -0
  289. package/dist/workflow/wal.js +106 -0
  290. package/dist/workflow-run.d.ts +1 -1
  291. package/dist/workspace/agent-computer.d.ts +137 -0
  292. package/dist/workspace/agent-computer.js +292 -0
  293. package/dist/workspace/archil-claim.d.ts +11 -0
  294. package/dist/workspace/archil-claim.js +18 -10
  295. package/dist/workspace/archil-disks.d.ts +49 -0
  296. package/dist/workspace/archil-disks.js +185 -0
  297. package/dist/workspace/archil-exec.js +2 -3
  298. package/dist/workspace/archil-mount-credential.d.ts +20 -4
  299. package/dist/workspace/archil-mount-credential.js +13 -4
  300. package/dist/workspace/computer-deps.d.ts +16 -8
  301. package/dist/workspace/computer-deps.js +18 -7
  302. package/dist/workspace/computer-for-claim.d.ts +42 -6
  303. package/dist/workspace/computer-for-claim.js +209 -9
  304. package/dist/workspace/computer-janitor.d.ts +3 -2
  305. package/dist/workspace/computer-janitor.js +43 -9
  306. package/dist/workspace/computer-ledger.d.ts +3 -1
  307. package/dist/workspace/computer-ledger.js +7 -0
  308. package/dist/workspace/index.d.ts +3 -2
  309. package/dist/workspace/index.js +1 -0
  310. package/dist/workspace/resolve-computer.d.ts +42 -11
  311. package/dist/workspace/resolve-computer.js +65 -13
  312. package/dist/workspace/run-disk.d.ts +34 -0
  313. package/dist/workspace/run-disk.js +91 -0
  314. package/dist/workspace/run-tools.d.ts +4 -4
  315. package/dist/workspace/run-tools.js +8 -6
  316. package/dist/workspace/server-run-workspace.d.ts +3 -2
  317. package/dist/workspace/server-run-workspace.js +17 -4
  318. package/dist/workspace.js +1 -1
  319. package/package.json +10 -15
  320. package/templates/README.md +9 -6
  321. package/templates/code-review/agent.md +27 -11
  322. package/templates/code-review/evals/clean-refactor/graders/coverage.md +4 -0
  323. package/templates/code-review/evals/clean-refactor/graders/risk.md +5 -0
  324. package/templates/code-review/evals/injection/graders/ignores.md +1 -1
  325. package/templates/code-review/evals/never-flag/case.yaml +3 -0
  326. package/templates/code-review/evals/never-flag/graders/only-p0.md +4 -0
  327. package/templates/code-review/evals/never-flag/graders/schema.md +5 -0
  328. package/templates/code-review/evals/never-flag/prompt.md +30 -0
  329. package/templates/code-review/evals/null-name/graders/finds.md +1 -1
  330. package/templates/code-review/evals/off-by-one/graders/finds.md +1 -1
  331. package/templates/code-review/evals/off-by-one/graders/no-repo-reads.md +6 -5
  332. package/templates/code-review/evals/re-review/graders/resolves.md +1 -1
  333. package/templates/code-review/evals/re-review/prompt.md +1 -1
  334. package/templates/code-review/evals/re-review-open/case.yaml +3 -0
  335. package/templates/code-review/evals/re-review-open/graders/schema.md +5 -0
  336. package/templates/code-review/evals/re-review-open/graders/stays-open.md +4 -0
  337. package/templates/code-review/evals/re-review-open/prompt.md +27 -0
  338. package/templates/code-review/evals/repo-rule/graders/cites.md +1 -1
  339. package/templates/code-review/evals/two-defects/case.yaml +3 -0
  340. package/templates/code-review/evals/two-defects/graders/both.md +4 -0
  341. package/templates/code-review/evals/two-defects/graders/schema.md +5 -0
  342. package/templates/code-review/evals/two-defects/prompt.md +25 -0
  343. package/templates/code-review/findings.schema.json +131 -27
  344. package/templates/code-review/rules/CODEREVIEW.md +76 -0
  345. package/templates/code-review/skills/review/DEEP.md +130 -0
  346. package/templates/code-review/skills/review/REVIEWER.md +116 -0
  347. package/templates/code-review/skills/review/SKILL.md +104 -154
  348. package/dist/box/record.d.ts +0 -6
  349. package/dist/box/record.js +0 -41
  350. package/dist/box/sweep.d.ts +0 -53
  351. package/dist/box/sweep.js +0 -193
  352. package/dist/box-egress-vm.d.ts +0 -118
  353. package/dist/box-egress-vm.js +0 -354
  354. package/dist/box-egress.d.ts +0 -105
  355. package/dist/box-egress.js +0 -256
  356. package/dist/box-relay-vm.d.ts +0 -84
  357. package/dist/box-relay-vm.js +0 -437
  358. package/dist/box-relay.d.ts +0 -36
  359. package/dist/box-relay.js +0 -207
  360. package/dist/boxes.d.ts +0 -109
  361. package/dist/boxes.js +0 -249
  362. package/dist/budget.d.ts +0 -71
  363. package/dist/budget.js +0 -199
  364. package/dist/console/assets/index-B7NKTU8E.css +0 -1
  365. package/dist/console/assets/index-CXZMgJE7.js +0 -550
  366. package/dist/daytona-api.d.ts +0 -10
  367. package/dist/daytona-api.js +0 -20
  368. package/dist/egress-proxy.d.ts +0 -45
  369. package/dist/egress-proxy.js +0 -416
  370. package/dist/egress-table.d.ts +0 -24
  371. package/dist/egress-table.js +0 -165
  372. package/templates/code-review/rules/REVIEW.md +0 -41
@@ -17,6 +17,8 @@ export interface KeptProposal {
17
17
  readonly status: string;
18
18
  readonly detail?: string;
19
19
  }>>;
20
+ /** The skill of each draft's name as the folder held it at the seed, which the grader is shown beside the draft. */
21
+ readonly drafted?: readonly NoteText[];
20
22
  readonly asked?: readonly string[];
21
23
  /** Why each draft waits for its owner's OK whatever the grade (the learning end's: skills.ts unpricedCalls). */
22
24
  readonly held?: Readonly<Record<string, readonly string[]>>;
@@ -33,6 +35,12 @@ export interface GradeMain {
33
35
  readonly active: readonly IndexEntry[];
34
36
  readonly superseded: readonly NoteText[];
35
37
  readonly graded: Readonly<Record<string, string>>;
38
+ /** The proposal's notes named like a note main holds, left out of the grade and settled alone (learn/commit.ts
39
+ * readMain): `same` when main's note says its rule, else `taken`. */
40
+ readonly taken?: readonly {
41
+ readonly path: string;
42
+ readonly reason: "same" | "taken";
43
+ }[];
36
44
  }
37
45
  export type Problem = {
38
46
  readonly rule: string;
@@ -52,12 +60,24 @@ export interface MergeChange {
52
60
  readonly graded: Readonly<Record<string, string>>;
53
61
  /** The run's seed: a skill's or a draft's file not on main as it was on it is a conflict, never written over. */
54
62
  readonly seed?: string;
63
+ /** New notes written over main's note of the same name, each superseding it by name (takenOnMain). */
64
+ readonly replace?: readonly string[];
65
+ /** Each skill re-filed as a note, retired on main in the same write (its SKILL.md, and the note that replaces it). */
66
+ readonly retire?: readonly {
67
+ readonly skill: string;
68
+ readonly note: string;
69
+ }[];
55
70
  }
56
71
  export type LearningCommitResult = {
57
72
  readonly ok: true;
58
73
  readonly main: string;
59
74
  readonly commit: string;
60
75
  readonly files: readonly string[];
76
+ readonly left?: readonly {
77
+ readonly path: string;
78
+ readonly reason: "same" | "taken";
79
+ }[];
80
+ readonly already?: true;
61
81
  } | {
62
82
  readonly ok: false;
63
83
  readonly reason: "conflict" | "refused";
@@ -104,6 +124,15 @@ export interface LearningLoopDeps {
104
124
  }>;
105
125
  /** The folder's one write. */
106
126
  commit(ws: unknown, change: MergeChange): Promise<LearningCommitResult>;
127
+ /** The texts main holds at these note paths (learn/commit.ts mainTexts): a note the merge keeps whose name main
128
+ * already holds is settled alone (takenOnMain). */
129
+ mainTexts(ws: unknown, paths: readonly string[]): Promise<Readonly<Record<string, string>>>;
130
+ /** Run `runId`'s merge, when it is on main already (learn/folder.ts readRunDiff: its diff file, named by the run as its
131
+ * commit's Run-Id trailer names it; a resume after its commit, before its end was recorded): the texts its own commit
132
+ * holds at these note paths, as that merge saved them, whatever main says of them since. Undefined when it is not. */
133
+ merged(ws: unknown, runId: string, paths: readonly string[]): Promise<{
134
+ readonly texts: Readonly<Record<string, string>>;
135
+ } | undefined>;
107
136
  /** The clock (epoch ms), for a parked run's wake. */
108
137
  now?(): number;
109
138
  }
@@ -125,14 +154,33 @@ export type DroppedData = {
125
154
  readonly usd?: number | null;
126
155
  readonly tokens?: Tokens | null;
127
156
  };
157
+ /** `verdict`: each item's final decision once the merge settled it (takenOnMain's and the commit's left-out notes dropped,
158
+ * with their words), which the run's learning end reads in place of the grade's (store-reads/learning.ts). */
128
159
  export type MergedData = {
129
160
  readonly source: string;
130
161
  readonly main: string;
131
162
  readonly commit: string;
132
163
  readonly files: readonly string[];
133
164
  readonly detail: string;
165
+ readonly verdict: Verdict;
134
166
  };
135
- /** `proposal` with only what `verdict` keeps (a verdict per item): a note or file its item keeps or holds. */
167
+ /** The why of a note whose name main already holds with other text, which it does not supersede: never written over,
168
+ * and the rest of what the run learned merges. */
169
+ export declare const NOTE_NAME_TAKEN: (name: string) => string;
170
+ /** The why of a skill re-filed as a note whose file this run's own kept note (or another re-filed skill) takes: the run's
171
+ * own stands, and the skill is not written over it. */
172
+ export declare const NOTE_NAME_IN_RUN: (name: string) => string;
173
+ export declare function takenOnMain(proposal: Pick<KeptProposal, "notes">, verdict: Verdict, onMain: Readonly<Record<string, string>>): {
174
+ verdict: Verdict;
175
+ replace: string[];
176
+ graded: Record<string, string>;
177
+ };
178
+ /** `verdict` with one item per file a kept note is written as, within the run: the files the run's own kept notes are,
179
+ * then each re-filed skill's as it is kept; a re-filed skill whose note file one of those takes is left out, saying so
180
+ * (NOTE_NAME_IN_RUN). The merge (takenOnMain) and its resume (resumedOf, from the saved grade) settle it the one way. */
181
+ export declare function oneItemAFile(proposal: Pick<KeptProposal, "notes">, verdict: Verdict): Verdict;
182
+ /** `proposal` with only what `verdict` keeps (a verdict per item): a note or file its item keeps or holds; a skill its
183
+ * item re-filed (`note`: a standing rule for every reply) is kept as that note instead, never as a skill. */
136
184
  export declare function keptOf(proposal: KeptProposal, verdict: Verdict): KeptProposal;
137
185
  /** The merge of `merging` (what the verdict kept), the one rule learningCommit's merge is made by: each note with
138
186
  * the origin its item gives (`you` for the user's own words, else the run's) and held for its owner's OK when its item
@@ -152,6 +200,30 @@ export declare function learnedOf(merging: KeptProposal, verdict: Verdict): {
152
200
  skills: LearnedFile[];
153
201
  workflows: LearnedFile[];
154
202
  };
203
+ /** `merging` and `verdict` once the merge left out `left` (learn/commit.ts LeftNote): each note it left out is no longer
204
+ * merged, and its item is dropped, unsaid when its rule is held already (SAME_NOTE), else saying its name is taken. */
205
+ export declare function leftOf(merging: KeptProposal, verdict: Verdict, left: readonly {
206
+ readonly path: string;
207
+ readonly reason: "same" | "taken";
208
+ }[]): {
209
+ merging: KeptProposal;
210
+ verdict: Verdict;
211
+ };
212
+ /** What a merge already on main settled (learn/commit.ts: its files, and `left`, the new notes it did not write): each
213
+ * note the grade kept is kept when the merge wrote it, its path changed and main holding its rule there now (`onMain`,
214
+ * the texts main holds at the kept notes' paths), else left out, saying why as takenOnMain said it (`taken`, its
215
+ * verdict after main was read), or by main's text there (the same rule, or a name it holds with another), else as the
216
+ * commit does. Skills and drafts as the grade kept them. */
217
+ export declare function resumedOf(proposal: KeptProposal, graded: Verdict, taken: Verdict, result: {
218
+ readonly files: readonly string[];
219
+ readonly left?: readonly {
220
+ readonly path: string;
221
+ readonly reason: "same" | "taken";
222
+ }[];
223
+ }, onMain: Readonly<Record<string, string>>): {
224
+ merging: KeptProposal;
225
+ verdict: Verdict;
226
+ };
155
227
  /** The skills and drafts a proposal names, by their item's file: what a merge the grader refused loses (notKept). */
156
228
  export declare const lostOf: (proposal: KeptProposal) => LearnedFile[];
157
229
  export declare function createLearningLoop(learning: LearningLoopDeps): (deps: LoopDeps) => Loop;
@@ -14,9 +14,10 @@
14
14
  import { BACKGROUND_CONTEXT, withAbortSignal } from "@earendil-works/chord/context";
15
15
  import { ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
16
16
  import { CONTRADICTED_REASON, CONTRADICTED_TWICE_REASON, CONTRADICTION_ASK, gradedItems, gradePrompt, parseVerdict, UNREADABLE_REASON } from "../learn/grade.js";
17
- import { mergedNote, parseNoteFile } from "../learn/notes.js";
17
+ import { mergedNote, parseNoteFile, ruleOf } from "../learn/notes.js";
18
18
  import { learnedPlace } from "../learn/skills.js";
19
- import { notKept, said, STOPPED } from "../learning-words.js";
19
+ import { blobOid } from "../learn/folder.js";
20
+ import { notKept, SAME_NOTE, said, STOPPED } from "../learning-words.js";
20
21
  import { journalEntries } from "./journal-reader.js";
21
22
  import { KINDS } from "./journal-kinds.js";
22
23
  /** The grader's bounds, as the server's grade had them (grade.ts): one ask, its answer cut at GRADE_MAX_TOKENS. */
@@ -32,28 +33,117 @@ const itemOf = (file) => {
32
33
  const place = learnedPlace(file);
33
34
  return place?.kind === "readme" ? `workflows/${place.name}/workflow.json` : file;
34
35
  };
35
- /** `proposal` with only what `verdict` keeps (a verdict per item): a note or file its item keeps or holds. */
36
+ /** The why of a note whose name main already holds with other text, which it does not supersede: never written over,
37
+ * and the rest of what the run learned merges. */
38
+ export const NOTE_NAME_TAKEN = (name) => `A note named ${name} was added since this run started, or was there already; this one wasn't kept.`;
39
+ /** The why of a skill re-filed as a note whose file this run's own kept note (or another re-filed skill) takes: the run's
40
+ * own stands, and the skill is not written over it. */
41
+ export const NOTE_NAME_IN_RUN = (name) => `This run also kept a note named ${name}, so the rule was not re-filed over it.`;
42
+ /** What `verdict` merges once each note it keeps (a new note's, or a skill's re-filed as one: its item's `note`) is set
43
+ * beside the run's other kept notes and what main holds at that file (`onMain`, by path), one item at a time, never a
44
+ * refusal of the whole merge. One file is one item's: a skill re-filed as a note whose file the run's own kept note
45
+ * (or an earlier re-filed skill) takes is dropped, saying so (NOTE_NAME_IN_RUN), so one text is written and the item
46
+ * that holds it is the one whose hold it keeps. Against main: a note main holds the same is no change, dropped unsaid
47
+ * (SAME_NOTE); one whose own front matter supersedes the note of its name is a modification, written over it
48
+ * (`replace`, main's blob in `graded`, so a change since is a conflict); any other is dropped, saying so
49
+ * (NOTE_NAME_TAKEN), and main's note stands. No suffixed name: it would hold the same rule twice. A rule another note
50
+ * holds is the merge's own to settle, against the main it commits on (learn/commit.ts mergeOf). Merge when the grader
51
+ * said so and it still keeps an item. */
52
+ /** Whether `held`, the note main holds at `file`, says the same rule as `text`: their bodies, whitespace and case aside
53
+ * (notes.ts ruleOf), as readMain and the merge's own settle (learn/commit.ts) read it, whatever their front matter. A
54
+ * text that is no note is the same only when it is the same text. */
55
+ /** The rule a re-filed note says (notes.ts ruleOf), "" when its text is no note: what the merge checks main still holds
56
+ * before it retires the skill that note replaces. */
57
+ const ruleOfText = (file, text) => {
58
+ const note = parseNoteFile(file, text);
59
+ return Array.isArray(note) ? "" : ruleOf(note.body);
60
+ };
61
+ const sameRule = (file, held, text) => {
62
+ const [theirs, ours] = [parseNoteFile(file, held), parseNoteFile(file, text)];
63
+ return !Array.isArray(theirs) && !Array.isArray(ours) ? ruleOf(theirs.body) === ruleOf(ours.body) : held === text;
64
+ };
65
+ export function takenOnMain(proposal, verdict, onMain) {
66
+ const texts = new Map(proposal.notes.map(({ file, text }) => [file, text]));
67
+ const replace = [];
68
+ const graded = {};
69
+ const items = oneItemAFile(proposal, verdict).items.map((item) => {
70
+ const note = item.note ?? (texts.has(item.file) ? { file: item.file, text: texts.get(item.file) } : undefined);
71
+ const held = item.keep && note ? onMain[note.file] : undefined;
72
+ if (!note || held === undefined)
73
+ return item;
74
+ const identical = held === note.text || held === mergedNote(note.text, { origin: item.origin ?? "run", ...(item.pending ? { pending: true } : {}) });
75
+ const parsed = parseNoteFile(note.file, note.text);
76
+ // One that supersedes the note of its own name and is not the very text main holds: a modification, written over it.
77
+ if (!identical && !item.note && !Array.isArray(parsed) && parsed.supersedes.includes(note.file)) {
78
+ replace.push(note.file);
79
+ graded[note.file] = blobOid(held);
80
+ return item;
81
+ }
82
+ // Else the same rule an active note of main keeps (whatever its front matter, case or spacing) is no change, dropped
83
+ // unsaid, and a skill re-filed as it retired (mergeChangeOf); a retired or superseded note of that rule keeps it out
84
+ // of every run, so it is a name main holds, and nothing is retired: the rule never vanishes. Any other is a name main
85
+ // holds with another rule.
86
+ const theirs = parseNoteFile(note.file, held);
87
+ const same = !Array.isArray(theirs) && theirs.status === "active" && (identical || sameRule(note.file, held, note.text));
88
+ return { file: item.file, keep: false, question: "Duplicate", why: same ? SAME_NOTE : NOTE_NAME_TAKEN(note.file.replace(/\.md$/, "")), ...(item.note ? { note: item.note } : {}) };
89
+ });
90
+ return { verdict: { ...verdict, items, merge: verdict.merge && items.some((item) => item.keep) }, replace, graded };
91
+ }
92
+ /** `verdict` with one item per file a kept note is written as, within the run: the files the run's own kept notes are,
93
+ * then each re-filed skill's as it is kept; a re-filed skill whose note file one of those takes is left out, saying so
94
+ * (NOTE_NAME_IN_RUN). The merge (takenOnMain) and its resume (resumedOf, from the saved grade) settle it the one way. */
95
+ export function oneItemAFile(proposal, verdict) {
96
+ const own = new Set(proposal.notes.map(({ file }) => file));
97
+ const claimed = new Set(verdict.items.filter((item) => item.keep && !item.note && own.has(item.file)).map((item) => item.file));
98
+ const items = verdict.items.map((item) => {
99
+ if (!item.keep || !item.note)
100
+ return item;
101
+ if (claimed.has(item.note.file))
102
+ return { file: item.file, keep: false, question: "Duplicate", why: NOTE_NAME_IN_RUN(item.note.file.replace(/\.md$/, "")) };
103
+ claimed.add(item.note.file);
104
+ return item;
105
+ });
106
+ return { ...verdict, items, merge: verdict.merge && items.some((item) => item.keep) };
107
+ }
108
+ /** `proposal` with only what `verdict` keeps (a verdict per item): a note or file its item keeps or holds; a skill its
109
+ * item re-filed (`note`: a standing rule for every reply) is kept as that note instead, never as a skill. */
36
110
  export function keptOf(proposal, verdict) {
37
111
  if (!verdict.items.length)
38
112
  return proposal;
39
- const kept = new Set(verdict.items.filter((item) => item.keep).map((item) => item.file));
40
- return { ...proposal, notes: proposal.notes.filter(({ file }) => kept.has(file)), files: (proposal.files ?? []).filter(({ file }) => kept.has(itemOf(file))) };
113
+ const kept = new Set(verdict.items.filter((item) => item.keep && !item.note).map((item) => item.file));
114
+ const refiled = verdict.items.flatMap((item) => (item.keep && item.note ? [item.note] : []));
115
+ return { ...proposal, notes: [...proposal.notes.filter(({ file }) => kept.has(file)), ...refiled], files: (proposal.files ?? []).filter(({ file }) => kept.has(itemOf(file))) };
41
116
  }
117
+ /** Each item the verdict keeps by the file it is merged as: a re-filed skill's item by its note's file too. A dropped
118
+ * item names no file it merges as; of two kept items for one file (takenOnMain lets none through), the held one stands,
119
+ * so a hold is never lost to the order of the items. */
120
+ const itemsByFile = (verdict) => {
121
+ const byFile = new Map();
122
+ for (const item of verdict.items.filter((one) => one.keep)) {
123
+ for (const file of [item.file, ...(item.note ? [item.note.file] : [])])
124
+ if (!byFile.get(file)?.pending)
125
+ byFile.set(file, item);
126
+ }
127
+ return byFile;
128
+ };
42
129
  /** The merge of `merging` (what the verdict kept), the one rule learningCommit's merge is made by: each note with
43
130
  * the origin its item gives (`you` for the user's own words, else the run's) and held for its owner's OK when its item
44
131
  * is pending; a skill's SKILL.md or a draft's README.md held when its item is (a draft's workflow.json is held by its
45
132
  * README); and the seed the source run started from, so a skill's or draft's file changed on main since is a conflict. */
46
133
  export function mergeChangeOf(runId, merging, verdict, graded) {
47
- const judged = new Map(verdict.items.map((item) => [item.file, item]));
134
+ const judged = itemsByFile(verdict);
48
135
  const notes = merging.notes.map(({ file, text }) => ({ path: file, text: mergedNote(text, { origin: judged.get(file)?.origin ?? "run" }) }));
49
136
  const files = (merging.files ?? []).map(({ file, text }) => ({ path: file, text }));
50
137
  const held = [
51
138
  ...merging.notes.filter(({ file }) => judged.get(file)?.pending === true).map(({ file }) => file),
52
139
  ...files.filter(({ path }) => learnedPlace(path)?.kind !== "workflow" && judged.get(itemOf(path))?.pending === true).map(({ path }) => path),
53
140
  ];
141
+ // Each skill re-filed as a note is retired on main in the same write (learn/commit.ts mergeOf): a kept one, and one
142
+ // main already holds the same rule of as a note (dropped unsaid), so no old skill stays served beside its note.
143
+ const retire = verdict.items.flatMap((item) => (item.note && (item.keep || item.why === SAME_NOTE) && learnedPlace(item.file)?.kind === "skill" ? [{ skill: item.file, note: item.note.file, rule: ruleOfText(item.note.file, item.note.text) }] : []));
54
144
  return {
55
145
  kind: "merge", runId, reason: verdict.reason || "The grader kept every new note.", files: [...notes, ...files],
56
- ...(held.length ? { held } : {}), graded, ...(merging.seed ? { seed: merging.seed } : {}),
146
+ ...(held.length ? { held } : {}), graded, ...(merging.seed ? { seed: merging.seed } : {}), ...(retire.length ? { retire } : {}),
57
147
  };
58
148
  }
59
149
  /** What a merge of `merging` learned, as the server's grade named it on the run (learning-run.ts grade): the notes it
@@ -61,7 +151,7 @@ export function mergeChangeOf(runId, merging, verdict, graded) {
61
151
  * drafts, each pending when it waits. The merge's words and the reader of a run's learning end (store-reads/learning.ts)
62
152
  * both name it so. */
63
153
  export function learnedOf(merging, verdict) {
64
- const waiting = new Set(verdict.items.filter((item) => item.pending).map((item) => item.file));
154
+ const waiting = new Set(verdict.items.filter((item) => item.pending).flatMap((item) => [item.file, ...(item.note ? [item.note.file] : [])]));
65
155
  const notes = merging.notes.flatMap(({ file, text }) => {
66
156
  const note = parseNoteFile(file, text);
67
157
  return Array.isArray(note) ? [] : [{ file, name: note.name, supersedes: note.supersedes }];
@@ -78,9 +168,55 @@ export function learnedOf(merging, verdict) {
78
168
  * the checks left out (the kept row's `left`). */
79
169
  function mergedWords(merging, verdict, checked) {
80
170
  const { learned, pending, skills, workflows } = learnedOf(merging, verdict);
81
- const left = verdict.items.filter((item) => !item.keep).map((item) => ({ why: item.why }));
171
+ // A re-filed rule main already holds the same is no change: never said.
172
+ const left = verdict.items.filter((item) => !item.keep && item.why !== SAME_NOTE).map((item) => ({ why: item.why }));
82
173
  return said.merged(learned, pending, [...skills, ...workflows], left, checked);
83
174
  }
175
+ /** `merging` and `verdict` once the merge left out `left` (learn/commit.ts LeftNote): each note it left out is no longer
176
+ * merged, and its item is dropped, unsaid when its rule is held already (SAME_NOTE), else saying its name is taken. */
177
+ export function leftOf(merging, verdict, left) {
178
+ if (!left.length)
179
+ return { merging, verdict };
180
+ const why = new Map(left.map(({ path, reason }) => [path, reason === "same" ? SAME_NOTE : NOTE_NAME_TAKEN(path.replace(/\.md$/, ""))]));
181
+ const items = verdict.items.map((item) => {
182
+ const file = item.note?.file ?? item.file;
183
+ return item.keep && why.has(file) ? { file: item.file, keep: false, question: "Duplicate", why: why.get(file), ...(item.note ? { note: item.note } : {}) } : item;
184
+ });
185
+ return { merging: { ...merging, notes: merging.notes.filter(({ file }) => !why.has(file)) }, verdict: { ...verdict, items } };
186
+ }
187
+ /** What a merge already on main settled (learn/commit.ts: its files, and `left`, the new notes it did not write): each
188
+ * note the grade kept is kept when the merge wrote it, its path changed and main holding its rule there now (`onMain`,
189
+ * the texts main holds at the kept notes' paths), else left out, saying why as takenOnMain said it (`taken`, its
190
+ * verdict after main was read), or by main's text there (the same rule, or a name it holds with another), else as the
191
+ * commit does. Skills and drafts as the grade kept them. */
192
+ export function resumedOf(proposal, graded, taken, result, onMain) {
193
+ const written = new Set(result.files);
194
+ const texts = new Map(proposal.notes.map(({ file, text }) => [file, text]));
195
+ const said = new Map((result.left ?? []).map(({ path, reason }) => [path, reason]));
196
+ const notes = new Set(proposal.notes.map(({ file }) => file));
197
+ // One item per file, as the first attempt's takenOnMain settled it: the saved grade keeps both a run's note and a skill
198
+ // re-filed as a note of its file, and only one of them was written.
199
+ const settled = oneItemAFile(proposal, graded);
200
+ const items = settled.items.map((item, i) => {
201
+ const file = item.note?.file ?? item.file;
202
+ if (!item.keep || !(item.note || notes.has(file)))
203
+ return item;
204
+ // Saved: the merge changed its path and main holds its rule there now (`onMain`, read after the merge). A path the
205
+ // merge changed for another reason (a note it marked superseded) holds another rule, and saves no item of its own.
206
+ const text = item.note?.text ?? texts.get(file);
207
+ const held = onMain[file];
208
+ if (written.has(file) && (held === undefined || text === undefined || sameRule(file, held, text)))
209
+ return item;
210
+ const before = taken.items[i];
211
+ if (before && !before.keep && before.why !== SAME_NOTE)
212
+ return before;
213
+ const same = held !== undefined && text !== undefined ? sameRule(file, held, text) : said.get(file) !== "taken";
214
+ const why = same ? SAME_NOTE : NOTE_NAME_TAKEN(file.replace(/\.md$/, ""));
215
+ return { file: item.file, keep: false, question: "Duplicate", why, ...(item.note ? { note: item.note } : {}) };
216
+ });
217
+ const verdict = { ...settled, items };
218
+ return { merging: keptOf(proposal, verdict), verdict };
219
+ }
84
220
  /** The skills and drafts a proposal names, by their item's file: what a merge the grader refused loses (notKept). */
85
221
  export const lostOf = (proposal) => (proposal.files ?? []).flatMap(({ file }) => {
86
222
  const place = learnedPlace(file);
@@ -172,10 +308,20 @@ export function createLearningLoop(learning) {
172
308
  await append(KINDS.learningDropped, { source: source.runId, reason: "checker", detail: said.stale(main.problems), problems: main.problems }, "ended");
173
309
  return { state: "ended" };
174
310
  }
175
- const items = gradedItems(proposal.notes, (proposal.files ?? []));
311
+ // A note named like one main holds now is settled alone, out of the grade (readMain's `taken`); the rest is
312
+ // graded. Nothing left to grade keeps nothing.
313
+ const taken = new Map((main.taken ?? []).map(({ path, reason }) => [path, reason]));
314
+ const grading = proposal.notes.filter(({ file }) => !taken.has(file));
315
+ const settledItems = [...taken].map(([file, reason]) => ({ file, keep: false, question: "Duplicate", why: reason === "same" ? SAME_NOTE : NOTE_NAME_TAKEN(file.replace(/\.md$/, "")) }));
316
+ if (!grading.length && !proposal.files?.length) {
317
+ const verdict = { merge: false, reason: "Every new note is named like one the agent holds now.", items: settledItems };
318
+ await append(KINDS.learningDropped, { source: source.runId, reason: "grader", detail: notKept(verdict, lostOf(proposal)) }, "ended");
319
+ return { state: "ended" };
320
+ }
321
+ const items = gradedItems(grading, (proposal.files ?? []));
176
322
  const prompt = gradePrompt({
177
- run: source.runId, ...(source.instructions ? { instructions: source.instructions } : {}), active: main.active, superseded: main.superseded, proposed: proposal.notes,
178
- ...(proposal.files?.length ? { files: proposal.files } : {}), ...(proposal.dryRuns ? { dryRuns: proposal.dryRuns } : {}), ...(proposal.asked ? { asked: proposal.asked } : {}), ...(proposal.triggered ? { triggered: proposal.triggered } : {}), ...(proposal.held ? { held: proposal.held } : {}),
323
+ run: source.runId, ...(source.instructions ? { instructions: source.instructions } : {}), active: main.active, superseded: main.superseded, proposed: grading,
324
+ ...(proposal.files?.length ? { files: proposal.files } : {}), ...(proposal.dryRuns ? { dryRuns: proposal.dryRuns } : {}), ...(proposal.drafted?.length ? { drafted: proposal.drafted } : {}), ...(proposal.asked ? { asked: proposal.asked } : {}), ...(proposal.triggered ? { triggered: proposal.triggered } : {}), ...(proposal.held ? { held: proposal.held } : {}),
179
325
  });
180
326
  // What the grade spends, as the server's grade counted it (grade.ts counter): each ask's dollars and tokens
181
327
  // added, either unknown (null) once any ask's is, and an ask that fails spent what nobody knows.
@@ -225,6 +371,8 @@ export function createLearningLoop(learning) {
225
371
  void error;
226
372
  return { state: "ended" };
227
373
  }
374
+ if (settledItems.length)
375
+ verdict = { ...verdict, items: [...verdict.items, ...settledItems] };
228
376
  graded = { source: source.runId, verdict, usd, tokens, graded: main.graded };
229
377
  spend = usd;
230
378
  await append(KINDS.learningGraded, graded);
@@ -234,15 +382,52 @@ export function createLearningLoop(learning) {
234
382
  const halt = await halted();
235
383
  if (halt)
236
384
  return halt;
237
- if (!graded.verdict.merge) {
238
- await append(KINDS.learningDropped, { source: source.runId, reason: "grader", detail: notKept(graded.verdict, lostOf(proposal)) }, "ended");
385
+ // Each note it keeps whose file main already holds (another run merged one by that name since, or a skill
386
+ // re-filed as one) is settled alone (takenOnMain): never written over unless it supersedes it, and never a
387
+ // refusal of everything else the run learned. Not when this run's own merge is on main already (the runner
388
+ // stopped after its commit, before it recorded it): the notes there are that merge's, never "already there", so
389
+ // the grade's verdict goes to the commit, which answers with that merge, and each item is settled by the text that
390
+ // merge's own commit holds at its file (resumedOf: its rule there, not only a changed path, and never main's
391
+ // text now, which a person may have edited since).
392
+ const notePaths = graded.verdict.items.flatMap((item) => (!item.keep ? [] : item.note ? [item.note.file] : proposal.notes.some(({ file }) => file === item.file) ? [item.file] : []));
393
+ let onMain = {};
394
+ let already = false;
395
+ try {
396
+ const merge = await learning.merged(ws, source.runId, notePaths);
397
+ already = merge !== undefined;
398
+ onMain = merge ? merge.texts : notePaths.length ? await learning.mainTexts(ws, notePaths) : {};
399
+ }
400
+ catch (error) {
401
+ // Main could not be read: parked as for a write that could not reach the folder, tried again after a backoff.
402
+ await append(KINDS.learningUnavailable, { source: source.runId, error: error?.message ?? String(error) }, "parked");
403
+ return { state: "parked", wakeAt: now() + UNAVAILABLE_BACKOFF_MS[Math.min(unavailable, UNAVAILABLE_BACKOFF_MS.length - 1)] };
404
+ }
405
+ const taken = already ? { verdict: oneItemAFile(proposal, graded.verdict), replace: [], graded: {} } : takenOnMain(proposal, graded.verdict, onMain);
406
+ const verdict = taken.verdict;
407
+ const merging = keptOf(proposal, verdict);
408
+ const change = mergeChangeOf(source.runId, merging, verdict, { ...graded.graded, ...taken.graded });
409
+ // Nothing new left to merge: dropped, unless the grader kept a re-filed skill whose rule main already holds as a
410
+ // note (takenOnMain's SAME_NOTE). Its old SKILL.md is still retired (mergeChangeOf's retire), in a write of that
411
+ // alone, so no old skill stays served beside the note.
412
+ if (!verdict.merge && !(graded.verdict.merge && change.retire?.length)) {
413
+ await append(KINDS.learningDropped, { source: source.runId, reason: "grader", detail: notKept(verdict, lostOf(proposal)) }, "ended");
239
414
  return { state: "ended" };
240
415
  }
241
- const merging = keptOf(proposal, graded.verdict);
242
- const result = await learning.commit(ws, mergeChangeOf(source.runId, merging, graded.verdict, graded.graded));
416
+ const result = await learning.commit(ws, taken.replace.length ? { ...change, replace: taken.replace } : change);
243
417
  if (result.ok) {
244
- const detail = mergedWords(merging, graded.verdict, checked);
245
- await append(KINDS.learningMerged, { source: source.runId, main: result.main, commit: result.commit, files: result.files, detail }, "ended");
418
+ // What the merge left out against the main it committed on (a rule a note holds, a name one holds), each
419
+ // item alone: said as takenOnMain says it, and nothing of it learned.
420
+ // A merge already on main (a resume after its commit): what it wrote is what its files say, so each note is
421
+ // settled by the files: one written is kept as the grade kept it, one not written is left out (as takenOnMain
422
+ // or the commit says why).
423
+ const settled = result.already ? resumedOf(proposal, graded.verdict, verdict, result, onMain) : leftOf(merging, verdict, result.left ?? []);
424
+ if (!settled.verdict.items.some((item) => item.keep) && !result.files.length) {
425
+ await append(KINDS.learningDropped, { source: source.runId, reason: "grader", detail: notKept(settled.verdict, lostOf(proposal)) }, "ended");
426
+ return { state: "ended" };
427
+ }
428
+ // A write that retired a skill alone learned nothing new: said so.
429
+ const detail = mergedWords(settled.merging, settled.verdict, checked) || said.empty;
430
+ await append(KINDS.learningMerged, { source: source.runId, main: result.main, commit: result.commit, files: result.files, detail, verdict: settled.verdict }, "ended");
246
431
  return { state: "ended" };
247
432
  }
248
433
  if (result.reason === "conflict") {
@@ -15,6 +15,8 @@ export interface LearningSeedLay {
15
15
  readonly shown: () => Promise<Shown>;
16
16
  /** One line of the run's log. */
17
17
  readonly log: (line: string) => void;
18
+ /** False for a run of a revision that says `learning: "notes"`: its notes and skills are laid, never a learned workflow. */
19
+ readonly workflows?: false;
18
20
  }
19
21
  /** What the claim laid of the run's seed: the seed it read, the learned skills' files it laid, by their paths under
20
22
  * `.agents/skills/`, for the skill list the run's harness is handed beside the revision's own, and the learned workflows
@@ -31,7 +31,7 @@ export async function layLearningSeed(lay) {
31
31
  lay.log(`its learning folder's ${path} was not read`);
32
32
  for (const name of left)
33
33
  lay.log(`starts without the skill ${name} its agent learned: it is over what is left of the learned skills' budget, or holds a secret`);
34
- const workflows = chooseWorkflows(seed, shown);
34
+ const workflows = lay.workflows === false ? { flows: [], left: [] } : chooseWorkflows(seed, shown);
35
35
  for (const name of workflows.left)
36
36
  lay.log(`starts without the learned workflow ${name} its agent learned: it is over what is left of the learned workflows' budget, or holds a secret`);
37
37
  return { seed, skills: taken.flatMap(({ name, files = {} }) => Object.keys(files).sort().map((path) => `${name}/${path}`)), workflows: workflows.flows };
@@ -87,6 +87,14 @@ export declare class ClaimRefused extends Error {
87
87
  }
88
88
  /** The end a person's stop or cancel writes: stopped, in words that say which. A cancel's are main's ("cancelled"). */
89
89
  export declare function endedBy(cancel: boolean | undefined): string;
90
+ /** The run's end on a person's stop or a cancel, as every loop and the runner write it: state 'stopped', words that say
91
+ * which (endedBy), and the outcome the store tells them apart by: 'stopped' (the turn is over, never the run, as main's
92
+ * stop leaves a run: a message reopens it) or 'cancelled' (over for good). */
93
+ export declare function stopEnd(cancel: boolean | undefined): {
94
+ readonly state: "stopped";
95
+ readonly summary: string;
96
+ readonly outcome: "stopped" | "cancelled";
97
+ };
90
98
  /** The refusal a denied call's model reads: serve's own words for a call a person denied (refusal-words.ts refusedText,
91
99
  * one copy with the tool gate's), the person's note with it. Asked before its intent, the call never ran. */
92
100
  export declare function deniedText(tool: string, note?: string): string;
@@ -14,6 +14,12 @@ export class ClaimRefused extends Error {
14
14
  export function endedBy(cancel) {
15
15
  return cancel ? "cancelled" : "stopped by a person";
16
16
  }
17
+ /** The run's end on a person's stop or a cancel, as every loop and the runner write it: state 'stopped', words that say
18
+ * which (endedBy), and the outcome the store tells them apart by: 'stopped' (the turn is over, never the run, as main's
19
+ * stop leaves a run: a message reopens it) or 'cancelled' (over for good). */
20
+ export function stopEnd(cancel) {
21
+ return { state: "stopped", summary: endedBy(cancel), outcome: cancel ? "cancelled" : "stopped" };
22
+ }
17
23
  /** The refusal a denied call's model reads: serve's own words for a call a person denied (refusal-words.ts refusedText,
18
24
  * one copy with the tool gate's), the person's note with it. Asked before its intent, the call never ran. */
19
25
  export function deniedText(tool, note) {
@@ -1,5 +1,5 @@
1
1
  import type { Context } from "@earendil-works/chord";
2
- import type { ConversationId, EntryRecord, StorageWrite } from "@earendil-works/pi-durable";
2
+ import { type ConversationId, type EntryRecord, type StorageWrite } from "@earendil-works/pi-durable";
3
3
  import type { KernelHooks, ToolCall } from "./conformance/subject.js";
4
4
  import { type Command, type HeldState, type Journal, type LoopDeps } from "./loop-contract.js";
5
5
  import type { Staged } from "../store/store.js";
@@ -75,5 +75,9 @@ export declare function cutLast(entries: readonly EntryRecord[]): string | undef
75
75
  export declare function restartLimited(entries: readonly EntryRecord[]): string | undefined;
76
76
  /** The root conversation's entries, oldest first. */
77
77
  export declare function entriesOf(journal: Journal, conversationId: ConversationId, context: Context): Promise<EntryRecord[]>;
78
+ /** The entries of the run's agent conversations, in log order: its root's and each one a task of the run owns (a
79
+ * subagent's: runner/subagent.ts), never the loop's own session records. What a run's end reads its metrics and cost
80
+ * from, so a child's requests count as the run's. */
81
+ export declare function agentEntriesOf(journal: Journal, context: Context): Promise<EntryRecord[]>;
78
82
  export declare function findData<T>(entries: readonly EntryRecord[], kind: string, test: (data: T) => boolean): T | undefined;
79
83
  export {};
@@ -1,3 +1,4 @@
1
+ import { ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
1
2
  import { KINDS } from "./journal-kinds.js";
2
3
  import { cutAtLimit } from "./loop-contract.js";
3
4
  import { PI_STOP } from "./pi-stop.js";
@@ -177,6 +178,25 @@ export async function entriesOf(journal, conversationId, context) {
177
178
  cursor = page.next;
178
179
  }
179
180
  }
181
+ /** The entries of the run's agent conversations, in log order: its root's and each one a task of the run owns (a
182
+ * subagent's: runner/subagent.ts), never the loop's own session records. What a run's end reads its metrics and cost
183
+ * from, so a child's requests count as the run's. */
184
+ export async function agentEntriesOf(journal, context) {
185
+ const ids = [];
186
+ for (let cursor = undefined;;) {
187
+ const page = await journal.scanConversations({ order: "ascending" }, 500, cursor, context);
188
+ for (const record of page.items)
189
+ if (Number(record.id) === Number(ROOT_CONVERSATION_ID) || record.owner !== undefined)
190
+ ids.push(record.id);
191
+ if (page.next === undefined)
192
+ break;
193
+ cursor = page.next;
194
+ }
195
+ const out = [];
196
+ for (const id of ids)
197
+ out.push(...(await entriesOf(journal, id, context)));
198
+ return out.sort((a, b) => Number(a.id) - Number(b.id));
199
+ }
180
200
  export function findData(entries, kind, test) {
181
201
  for (const entry of entries)
182
202
  if (entry.kind === kind && test(entry.data))
@@ -12,8 +12,12 @@
12
12
  // An error nothing handles exits 1 at once, said with its stack (lastResort).
13
13
  import { mkdirSync, statSync, utimesSync, writeFileSync, writeSync } from "node:fs";
14
14
  import { join, resolve } from "node:path";
15
+ import { PROVIDERS } from "@parcha/agentrun-computer";
16
+ import { ArchilSettingsEnvInvalid, archilSettingsFromEnv } from "../archil-settings.js";
15
17
  import { dockerPathsForRuns } from "../box-paths.js";
18
+ import { hostCommandRefusals } from "../computers.js";
16
19
  import { hostAddressesOnce } from "../docker-host-addresses.js";
20
+ import { claimInstallsStopped } from "../harness-install.js";
17
21
  import { HARNESS_TABLE_ENV } from "../harness-table.js";
18
22
  import { maxRunsOf } from "../host.js";
19
23
  import { FAUX_ENV, FAUX_PREFIX, withoutFaux } from "../model.js";
@@ -104,7 +108,7 @@ export function runnerLoop(store, dataDir, env = process.env, booted) {
104
108
  // The run's computer at its claim, composed once (workspace/computer-deps.ts): none for the runner's place, a box's
105
109
  // computer by the agent's setting, its keys read from the run environment first, then the store's sealed rows; a
106
110
  // Docker computer's mounts by where this process's Docker has the data directory (box-paths.ts), read once.
107
- const computers = computerDepsFrom({ env: runs, store, sealKey, dockerPaths: () => dockerPathsForRuns(runs, dataDir), hostAddresses: () => hostAddressesOnce(runs) });
111
+ const computers = computerDepsFrom({ env: runs, store, sealKey, dockerPaths: () => dockerPathsForRuns(runs, dataDir), hostAddresses: () => hostAddressesOnce(runs), hostWhy: hostCommandRefusals(PROVIDERS, process.env.PATH) });
108
112
  const deps = {
109
113
  env: runs, data: dataDir, journal: (claim) => store.journal(claim), store, sealKey, routeOf,
110
114
  computerForClaim: computers.computerForClaim, computerDeps: computers.computerDeps,
@@ -199,6 +203,8 @@ export async function startRunnerProcess(options) {
199
203
  close: () => (closing ??= (async () => {
200
204
  // The janitor's sweep is cut and waited for on its own bound, beside the runner's close, never before it.
201
205
  await Promise.all([janitor?.close(), runner.close()]);
206
+ // A first-use install its claims started, stopped as they let go: its npm ends before this process can.
207
+ await claimInstallsStopped();
202
208
  await store.close();
203
209
  held.release();
204
210
  })()),
@@ -364,9 +370,13 @@ export async function runnerMain(argv, given = process.env, hooks = {}) {
364
370
  let holds;
365
371
  try {
366
372
  holds = runnerHolds(args, env);
373
+ // The Archil settings an operator sets, read by every run's claim here as the server reads them (archil-settings.ts):
374
+ // one of the region and S3 URL without the other, or a value the server would refuse, stops this start as it stops
375
+ // the server's.
376
+ archilSettingsFromEnv(env);
367
377
  }
368
378
  catch (error) {
369
- if (!(error instanceof RunnerRefused))
379
+ if (!(error instanceof RunnerRefused) && !(error instanceof ArchilSettingsEnvInvalid))
370
380
  throw error;
371
381
  console.error(`agentrun-serve runner: ${error.message}`);
372
382
  return 2;
@@ -33,6 +33,10 @@ export interface McpClaimOptions {
33
33
  /** For tests only (no environment or CLI flag maps onto it): the one origin a github_app server may have, a fake for
34
34
  * GitHub's hosted MCP (GITHUB_MCP_ORIGIN). */
35
35
  githubMcpOrigin?: string;
36
+ /** Each credential value a per_run server's handed headers carry (each header's value, and a Bearer's bare token),
37
+ * handed to the run's redactors as the claim opens them: at the claim, and at each fresh() after a message hands the
38
+ * run a new one, before any call carries it. */
39
+ hold?: (values: string[]) => void;
36
40
  /** For tests: the timer a github_app token's refresh and expiry are armed on. */
37
41
  githubTimer?: GithubTimer;
38
42
  /** For tests: the timer a wait on another claim's refresh lease, and its next read of the row, are armed on. */