@parcha/agentrun-serve 0.1.0-beta.43 → 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 (388) hide show
  1. package/CHANGELOG.md +231 -0
  2. package/README.md +580 -320
  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 +92 -22
  28. package/dist/composio-preset.d.ts +4 -3
  29. package/dist/composio-preset.js +81 -11
  30. package/dist/composio-triggers.d.ts +34 -2
  31. package/dist/composio-triggers.js +261 -31
  32. package/dist/computer-attach.d.ts +40 -5
  33. package/dist/computer-attach.js +130 -15
  34. package/dist/computer-keys.d.ts +3 -0
  35. package/dist/computer-keys.js +7 -1
  36. package/dist/computer-live.js +3 -2
  37. package/dist/computer-lost.d.ts +11 -0
  38. package/dist/computer-lost.js +19 -0
  39. package/dist/computer-record.d.ts +10 -2
  40. package/dist/computer-record.js +50 -18
  41. package/dist/computer-setting.d.ts +1 -0
  42. package/dist/computer-setting.js +2 -2
  43. package/dist/computers.d.ts +21 -10
  44. package/dist/computers.js +39 -28
  45. package/dist/console/assets/index-3O2Yl6hZ.js +701 -0
  46. package/dist/console/assets/index-DaFsZuzQ.css +1 -0
  47. package/dist/console/index.html +2 -2
  48. package/dist/decisions.d.ts +1 -1
  49. package/dist/dial-relay.d.ts +29 -0
  50. package/dist/dial-relay.js +94 -0
  51. package/dist/email/tools.js +10 -5
  52. package/dist/env-proxy.d.ts +2 -0
  53. package/dist/env-proxy.js +35 -0
  54. package/dist/errors.d.ts +4 -1
  55. package/dist/errors.js +10 -4
  56. package/dist/faux-computers.d.ts +8 -2
  57. package/dist/faux-computers.js +83 -6
  58. package/dist/harness-archive.d.ts +67 -0
  59. package/dist/harness-archive.js +177 -0
  60. package/dist/harness-goose.d.ts +34 -0
  61. package/dist/harness-goose.js +87 -0
  62. package/dist/harness-install.d.ts +21 -0
  63. package/dist/harness-install.js +24 -11
  64. package/dist/harness-opencode/adapter.js +1 -1
  65. package/dist/harness-table.d.ts +59 -3
  66. package/dist/harness-table.js +119 -14
  67. package/dist/harness.d.ts +17 -1
  68. package/dist/harness.js +22 -11
  69. package/dist/host.d.ts +12 -10
  70. package/dist/host.js +19 -16
  71. package/dist/import-worker.d.ts +3 -0
  72. package/dist/import-worker.js +7 -5
  73. package/dist/import.d.ts +22 -1
  74. package/dist/import.js +141 -28
  75. package/dist/key-check.js +3 -0
  76. package/dist/learn/audit.d.ts +6 -6
  77. package/dist/learn/audit.js +3 -7
  78. package/dist/learn/check.d.ts +26 -89
  79. package/dist/learn/check.js +58 -336
  80. package/dist/learn/commit.d.ts +63 -90
  81. package/dist/learn/commit.js +124 -316
  82. package/dist/learn/compile.d.ts +53 -50
  83. package/dist/learn/compile.js +88 -103
  84. package/dist/learn/corrections.d.ts +13 -0
  85. package/dist/learn/corrections.js +23 -0
  86. package/dist/learn/delete.d.ts +17 -0
  87. package/dist/learn/delete.js +42 -0
  88. package/dist/learn/folder.d.ts +35 -47
  89. package/dist/learn/folder.js +118 -134
  90. package/dist/learn/index.d.ts +3 -5
  91. package/dist/learn/index.js +6 -9
  92. package/dist/learn/learning.sh +45 -15
  93. package/dist/learn/promote.d.ts +20 -2
  94. package/dist/learn/promote.js +17 -4
  95. package/dist/learn/{skills.d.ts → workflow-draft.d.ts} +34 -38
  96. package/dist/learn/{skills.js → workflow-draft.js} +65 -59
  97. package/dist/learn/workflow-nodes.d.ts +22 -1
  98. package/dist/learn/workflow-nodes.js +29 -10
  99. package/dist/learned-workflow.js +2 -2
  100. package/dist/learning-cost.d.ts +127 -13
  101. package/dist/learning-cost.js +142 -10
  102. package/dist/learning-routes.d.ts +99 -170
  103. package/dist/learning-routes.js +199 -420
  104. package/dist/learning-run.d.ts +19 -40
  105. package/dist/learning-run.js +23 -93
  106. package/dist/learning-setting.d.ts +4 -4
  107. package/dist/learning-setting.js +6 -6
  108. package/dist/learning-words.d.ts +28 -74
  109. package/dist/learning-words.js +64 -162
  110. package/dist/learning.d.ts +62 -393
  111. package/dist/learning.js +101 -651
  112. package/dist/listen-first.d.ts +30 -0
  113. package/dist/listen-first.js +115 -0
  114. package/dist/live-reach.d.ts +25 -2
  115. package/dist/live-reach.js +46 -4
  116. package/dist/llms.txt +2 -1
  117. package/dist/local-models.d.ts +3 -11
  118. package/dist/local-models.js +5 -6
  119. package/dist/mcp-servers-store.d.ts +16 -16
  120. package/dist/model-form.js +6 -5
  121. package/dist/model.d.ts +12 -4
  122. package/dist/model.js +41 -16
  123. package/dist/native/binding.js +1 -1
  124. package/dist/native/connection.js +1 -1
  125. package/dist/native/discovery.js +1 -1
  126. package/dist/native/instructions.d.ts +3 -0
  127. package/dist/native/instructions.js +5 -3
  128. package/dist/native/remote-worker-bridge.js +9 -43
  129. package/dist/native/remote.d.ts +8 -15
  130. package/dist/native/remote.js +3 -3
  131. package/dist/native/schema.js +1 -1
  132. package/dist/native/types.d.ts +2 -2
  133. package/dist/openapi.js +375 -347
  134. package/dist/owner.d.ts +8 -0
  135. package/dist/owner.js +11 -0
  136. package/dist/paid-calls.d.ts +14 -0
  137. package/dist/paid-calls.js +31 -1
  138. package/dist/particle-provider.d.ts +11 -0
  139. package/dist/particle-provider.js +135 -0
  140. package/dist/place.d.ts +8 -3
  141. package/dist/place.js +13 -9
  142. package/dist/port-models.d.ts +12 -4
  143. package/dist/port-models.js +28 -11
  144. package/dist/print.d.ts +27 -0
  145. package/dist/print.js +21 -0
  146. package/dist/provider.d.ts +7 -0
  147. package/dist/provider.js +30 -2
  148. package/dist/reflect-range.js +2 -3
  149. package/dist/run-launch.d.ts +14 -28
  150. package/dist/run-launch.js +9 -12
  151. package/dist/run-learning-served.d.ts +16 -90
  152. package/dist/run-listing.d.ts +34 -0
  153. package/dist/run-listing.js +128 -0
  154. package/dist/run-model-key.d.ts +64 -0
  155. package/dist/run-model-key.js +147 -0
  156. package/dist/run-timeline-served.js +5 -3
  157. package/dist/run-trace.d.ts +97 -0
  158. package/dist/run-trace.js +253 -0
  159. package/dist/run-view.d.ts +15 -0
  160. package/dist/run-view.js +9 -0
  161. package/dist/runnable.js +2 -1
  162. package/dist/runner/adapter-loop.d.ts +14 -6
  163. package/dist/runner/adapter-loop.js +25 -14
  164. package/dist/runner/adapter-run-spec.d.ts +22 -7
  165. package/dist/runner/adapter-run-spec.js +130 -77
  166. package/dist/runner/adapters/index.d.ts +4 -2
  167. package/dist/runner/adapters/index.js +35 -4
  168. package/dist/runner/adapters/pi.d.ts +4 -12
  169. package/dist/runner/adapters/pi.js +2 -2
  170. package/dist/runner/audit.d.ts +5 -1
  171. package/dist/runner/audit.js +31 -12
  172. package/dist/runner/beside.d.ts +7 -2
  173. package/dist/runner/beside.js +41 -9
  174. package/dist/runner/claim-secrets.d.ts +66 -1
  175. package/dist/runner/claim-secrets.js +136 -3
  176. package/dist/runner/cli-run-spec.d.ts +2 -1
  177. package/dist/runner/cli-run-spec.js +82 -59
  178. package/dist/runner/commands.d.ts +3 -6
  179. package/dist/runner/conformance/harness.d.ts +6 -0
  180. package/dist/runner/conformance/harness.js +16 -5
  181. package/dist/runner/dispatch.d.ts +2 -2
  182. package/dist/runner/end-of-run.d.ts +6 -14
  183. package/dist/runner/end-of-run.js +8 -33
  184. package/dist/runner/feedback-round.d.ts +5 -4
  185. package/dist/runner/feedback-round.js +14 -10
  186. package/dist/runner/github-app.js +3 -1
  187. package/dist/runner/harness-loops.d.ts +0 -8
  188. package/dist/runner/harness-loops.js +30 -56
  189. package/dist/runner/held.d.ts +170 -0
  190. package/dist/runner/held.js +57 -0
  191. package/dist/runner/journal-kinds.d.ts +7 -3
  192. package/dist/runner/journal-kinds.js +2 -3
  193. package/dist/runner/learned-first.d.ts +40 -0
  194. package/dist/runner/learned-first.js +32 -0
  195. package/dist/runner/learning-end.d.ts +77 -66
  196. package/dist/runner/learning-end.js +250 -299
  197. package/dist/runner/learning-seed.d.ts +15 -33
  198. package/dist/runner/learning-seed.js +24 -45
  199. package/dist/runner/loop-contract.d.ts +8 -3
  200. package/dist/runner/loop-contract.js +4 -0
  201. package/dist/runner/loop.js +6 -2
  202. package/dist/runner/main.d.ts +18 -9
  203. package/dist/runner/main.js +82 -22
  204. package/dist/runner/mcp-run.d.ts +11 -0
  205. package/dist/runner/mcp-run.js +67 -0
  206. package/dist/runner/memory-claim.d.ts +14 -6
  207. package/dist/runner/memory-claim.js +18 -9
  208. package/dist/runner/model-route.d.ts +7 -4
  209. package/dist/runner/model-route.js +22 -10
  210. package/dist/runner/native-loop.d.ts +0 -7
  211. package/dist/runner/native-loop.js +9 -101
  212. package/dist/runner/promotion.d.ts +13 -6
  213. package/dist/runner/promotion.js +63 -17
  214. package/dist/runner/reflect-hook.d.ts +64 -91
  215. package/dist/runner/reflect-hook.js +112 -206
  216. package/dist/runner/run-channel.d.ts +43 -0
  217. package/dist/runner/run-channel.js +467 -0
  218. package/dist/runner/run-child-host.d.ts +169 -0
  219. package/dist/runner/run-child-host.js +1222 -0
  220. package/dist/runner/run-child.d.ts +1 -0
  221. package/dist/runner/run-child.js +254 -0
  222. package/dist/runner/run-errors.d.ts +2 -0
  223. package/dist/runner/run-errors.js +20 -0
  224. package/dist/runner/runner.js +58 -9
  225. package/dist/runner/sdk-ask.d.ts +78 -0
  226. package/dist/runner/sdk-ask.js +158 -0
  227. package/dist/runner/sdk-loop.d.ts +2 -4
  228. package/dist/runner/sdk-loop.js +29 -49
  229. package/dist/runner/store-secrets.d.ts +5 -3
  230. package/dist/runner/store-secrets.js +14 -6
  231. package/dist/runner/workflow-first.d.ts +5 -0
  232. package/dist/runner/workflow-first.js +4 -0
  233. package/dist/runner/workflow-spec.js +17 -4
  234. package/dist/runs-over-store.d.ts +21 -32
  235. package/dist/runs-over-store.js +44 -41
  236. package/dist/sdk/runs.d.ts +6 -11
  237. package/dist/sdk/runs.js +20 -41
  238. package/dist/sdk/schema.js +3 -4
  239. package/dist/sdk/workflows.d.ts +1 -1
  240. package/dist/seal-key.d.ts +5 -0
  241. package/dist/seal-key.js +7 -0
  242. package/dist/secrets.d.ts +3 -0
  243. package/dist/secrets.js +16 -3
  244. package/dist/server.d.ts +119 -83
  245. package/dist/server.js +1121 -419
  246. package/dist/sign-ins-store.d.ts +8 -0
  247. package/dist/sign-ins-store.js +36 -1
  248. package/dist/skills.js +1 -1
  249. package/dist/store/index.d.ts +3 -3
  250. package/dist/store/index.js +2 -2
  251. package/dist/store/journal/storage.js +3 -0
  252. package/dist/store/postgres/migrations/0017_run_listing.sql +83 -0
  253. package/dist/store/postgres/migrations/0018_trigger_template.sql +6 -0
  254. package/dist/store/postgres/migrations/0019_runner_relays.sql +28 -0
  255. package/dist/store/schema.d.ts +53 -4
  256. package/dist/store/schema.js +137 -6
  257. package/dist/store/seal.d.ts +20 -2
  258. package/dist/store/seal.js +24 -8
  259. package/dist/store/sqlite/connection.d.ts +2 -0
  260. package/dist/store/sqlite/connection.js +3 -1
  261. package/dist/store/sqlite/run-launch.d.ts +4 -1
  262. package/dist/store/sqlite/run-launch.js +12 -0
  263. package/dist/store/sqlite/run-patch.d.ts +0 -17
  264. package/dist/store/sqlite/run-patch.js +4 -33
  265. package/dist/store/sqlite/store.d.ts +3 -2
  266. package/dist/store/sqlite/store.js +360 -75
  267. package/dist/store/sqlite/worker.js +25 -0
  268. package/dist/store/store.d.ts +231 -39
  269. package/dist/store/store.js +25 -3
  270. package/dist/store-reads/analytics.js +11 -1
  271. package/dist/store-reads/learning.d.ts +131 -194
  272. package/dist/store-reads/learning.js +292 -301
  273. package/dist/store-reads/loop.d.ts +0 -1
  274. package/dist/store-reads/loop.js +1 -1
  275. package/dist/store-reads/stats.js +13 -3
  276. package/dist/store-view.d.ts +34 -2
  277. package/dist/store-view.js +108 -41
  278. package/dist/telemetry.d.ts +59 -0
  279. package/dist/telemetry.js +153 -0
  280. package/dist/template-eval.d.ts +3 -6
  281. package/dist/template-eval.js +22 -36
  282. package/dist/templates.d.ts +2 -1
  283. package/dist/templates.js +6 -3
  284. package/dist/timeline/index.d.ts +1 -1
  285. package/dist/timeline/index.js +1 -1
  286. package/dist/timeline/timeline.d.ts +5 -1
  287. package/dist/timeline/timeline.js +16 -0
  288. package/dist/tool-gate.d.ts +0 -7
  289. package/dist/tool-gate.js +1 -15
  290. package/dist/tool-keys-store.d.ts +1 -1
  291. package/dist/tool-keys.d.ts +6 -4
  292. package/dist/tool-keys.js +12 -6
  293. package/dist/tool-plane.d.ts +12 -12
  294. package/dist/tools.d.ts +13 -13
  295. package/dist/tools.js +29 -9
  296. package/dist/trigger-adapter.d.ts +3 -0
  297. package/dist/trigger-adapters.d.ts +4 -0
  298. package/dist/trigger-adapters.js +13 -0
  299. package/dist/trigger-composio.d.ts +58 -1
  300. package/dist/trigger-composio.js +160 -6
  301. package/dist/triggers.d.ts +64 -5
  302. package/dist/triggers.js +141 -17
  303. package/dist/validate.d.ts +11 -7
  304. package/dist/validate.js +9 -8
  305. package/dist/webhooks.d.ts +4 -1
  306. package/dist/webhooks.js +11 -4
  307. package/dist/workspace/agent-computer.d.ts +2 -0
  308. package/dist/workspace/agent-computer.js +2 -0
  309. package/dist/workspace/archil-claim.d.ts +7 -0
  310. package/dist/workspace/archil-claim.js +29 -0
  311. package/dist/workspace/archil.d.ts +9 -1
  312. package/dist/workspace/archil.js +165 -3
  313. package/dist/workspace/attach.d.ts +3 -1
  314. package/dist/workspace/attach.js +2 -0
  315. package/dist/workspace/attached-computers.d.ts +16 -5
  316. package/dist/workspace/attached-computers.js +25 -9
  317. package/dist/workspace/computer-deps.js +1 -1
  318. package/dist/workspace/computer-for-claim.d.ts +22 -8
  319. package/dist/workspace/computer-for-claim.js +339 -46
  320. package/dist/workspace/computer-hold.js +2 -1
  321. package/dist/workspace/computer-kept.d.ts +11 -1
  322. package/dist/workspace/computer-kept.js +24 -3
  323. package/dist/workspace/resolve-computer.d.ts +41 -21
  324. package/dist/workspace/resolve-computer.js +172 -64
  325. package/dist/workspace/revision-skills.d.ts +1 -2
  326. package/dist/workspace/revision-skills.js +3 -4
  327. package/dist/workspace/run-workspace.d.ts +1 -1
  328. package/dist/workspace/run-workspace.js +5 -0
  329. package/dist/workspace/synced-folder.d.ts +85 -0
  330. package/dist/workspace/synced-folder.js +230 -0
  331. package/dist/workspace/workspace.d.ts +4 -0
  332. package/launcher/agentrun-serve +1 -1
  333. package/package.json +10 -9
  334. package/templates/README.md +7 -7
  335. package/templates/inbound-lead/agent.md +3 -3
  336. package/templates/inbound-lead/evals/learned-correction/case.yaml +3 -3
  337. package/templates/inbound-lead/evals/learned-correction/fixtures/corrections.md +1 -0
  338. package/templates/inbound-lead/evals/learned-correction/graders/applies-correction.md +22 -0
  339. package/templates/inbound-lead/evals/learned-correction/graders/learned-origin.md +1 -1
  340. package/templates/inbound-lead/evals/learned-correction/prompt.md +1 -1
  341. package/templates/inbound-lead/lead.schema.json +6 -5
  342. package/templates/inbound-lead/rules/RUBRIC.md +4 -4
  343. package/templates/inbound-lead/skills/lead/SKILL.md +4 -4
  344. package/templates/market-monitor/evals/learned-preference/case.yaml +4 -4
  345. package/templates/market-monitor/evals/learned-preference/fixtures/corrections.md +1 -0
  346. package/templates/market-monitor/evals/learned-preference/graders/omits-junior-post.md +3 -3
  347. package/templates/market-monitor/evals/learned-preference/prompt.md +1 -1
  348. package/templates/release-notes/evals/learned-style/case.yaml +4 -4
  349. package/templates/release-notes/evals/learned-style/fixtures/corrections.md +1 -0
  350. package/templates/release-notes/evals/learned-style/graders/same-changes.md +2 -2
  351. package/templates/release-notes/evals/learned-style/prompt.md +1 -1
  352. package/dist/console/assets/index-BrcDSBD8.css +0 -1
  353. package/dist/console/assets/index-Bz6IWncl.js +0 -711
  354. package/dist/learn/grade.d.ts +0 -138
  355. package/dist/learn/grade.js +0 -682
  356. package/dist/learn/notes.d.ts +0 -81
  357. package/dist/learn/notes.js +0 -178
  358. package/dist/learn/reflect.d.ts +0 -77
  359. package/dist/learn/reflect.js +0 -228
  360. package/dist/learn/relevance.d.ts +0 -24
  361. package/dist/learn/relevance.js +0 -91
  362. package/dist/learn/seed-check.d.ts +0 -74
  363. package/dist/learn/seed-check.js +0 -103
  364. package/dist/learning-batch.d.ts +0 -291
  365. package/dist/learning-batch.js +0 -600
  366. package/dist/learning-evals.d.ts +0 -33
  367. package/dist/learning-evals.js +0 -98
  368. package/dist/native/context.d.ts +0 -6
  369. package/dist/native/context.js +0 -22
  370. package/dist/propose.d.ts +0 -52
  371. package/dist/propose.js +0 -224
  372. package/dist/runner/learning-ask.d.ts +0 -47
  373. package/dist/runner/learning-ask.js +0 -128
  374. package/dist/runner/learning-dispatch.d.ts +0 -45
  375. package/dist/runner/learning-dispatch.js +0 -99
  376. package/dist/runner/learning-factory.d.ts +0 -16
  377. package/dist/runner/learning-factory.js +0 -50
  378. package/dist/runner/learning-loop.d.ts +0 -273
  379. package/dist/runner/learning-loop.js +0 -521
  380. package/dist/usage.d.ts +0 -11
  381. package/dist/usage.js +0 -11
  382. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/MEMORY.md +0 -1
  383. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/no-agencies.md +0 -14
  384. package/templates/inbound-lead/evals/learned-correction/graders/applies-note.md +0 -21
  385. package/templates/market-monitor/evals/learned-preference/fixtures/learning/MEMORY.md +0 -1
  386. package/templates/market-monitor/evals/learned-preference/fixtures/learning/skip-junior-job-posts.md +0 -8
  387. package/templates/release-notes/evals/learned-style/fixtures/learning/MEMORY.md +0 -1
  388. package/templates/release-notes/evals/learned-style/fixtures/learning/say-agent-not-bot.md +0 -13
@@ -1,37 +1,30 @@
1
- // The learning lane's end-of-run turn on the runner (loop-contract.ts EndOfRunTurn), the server's rules moved
2
- // (learning-run.ts reflectSkip, reflect, closeLearning; propose.ts proposeRun's checks through `check`): whether a run
3
- // looks back, its prompt, how its turn ended, and the rows that keep its learning beside the run's end. A proposal is
4
- // kept with the turn that gave it (learning_proposals) and queues one learning run (learning_run) that grades and merges
5
- // it; a learning dropped at any step is kept with its reason and words, and queues none. What the run must never learn
6
- // (`refuse`: the host's secrets and what it was handed, its sign-in pointer among them) is refused by the checks, and cut
7
- // from everything the hook keeps; a run whose list can't be read learns nothing.
1
+ // The learning lane's end-of-run step on the runner (loop-contract.ts EndOfRunTurn): what a run that learns keeps
2
+ // beside its end, with no look-back turn and no grade. Two things, one merge a round on its agent's learning folder:
3
+ // - its owner's feedback (a grade's words, a message after its first), appended to corrections.md as one line, with
4
+ // no model turn;
5
+ // - its compile turn (learn/compile.ts), when one is due: the job written as a workflow, or a revision of a held one,
6
+ // checked (`checkDraft`: the workflow checks, its dry run, the secret and hidden-character refusals) and committed
7
+ // directly, with the compiled record on the run's learning. A compile whose drafts all failed is recorded on the
8
+ // agent's promotion records (compile once: `compileFailed`), never silent.
9
+ // How it ended is kept beside the run's end (learning_proposals): merged with what it wrote, or dropped with its reason
10
+ // and words. What the run must never learn (`refuse`: the host's secrets and what it was handed, its sign-in pointer
11
+ // among them) is refused by the checks and cut from everything the step keeps; a run whose list can't be read learns
12
+ // nothing.
8
13
  import { cutStrings } from "../key-refusal.js";
9
- import { DRY_RUN_FAILED, reflectAgain, reflectDryRun, reflectPrompt, redactSecrets } from "../learn/index.js";
10
- import { compileTurn, UNNAMED } from "../learn/compile.js";
11
- import { said, STOPPED } from "../learning-words.js";
12
- /** How long a run's end-of-run turn may take before it is aborted and the run ends without it. */
13
- export const REFLECT_TIMEOUT_MS = 5 * 60_000;
14
- /** The folder of a run's copy of what its agent learned, relative to its work: what the prompt names. */
15
- const LEARNED_DIR = ".learning";
14
+ import { redactSecrets } from "../learn/index.js";
15
+ import { compileTurn } from "../learn/compile.js";
16
+ import { correctionsCapOf, said, STOPPED } from "../learning-words.js";
16
17
  /** A turn the spend cap refused (the cap's refusal words, cap.ts CAP_REFUSAL_PREFIX). */
17
18
  export const CAP_REFUSAL = /^spend_cap_reached(?: \([^)]*\))?: /;
18
- /** A learning dropped before its turn answered: kept with its reason and words, beside the end. */
19
- const droppedRow = (turn, reason, detail, extra = {}) => ({ table: "learning_proposals", row: { turn, proposal: { ...extra, state: "dropped", reason, detail } } });
20
- /** A kept-learning row with every string in it cut of `refuse` in every form and of every secret-shaped run
21
- * (learn/secrets.ts redactSecrets, as proposeRun cuts what the person said), with an empty list too: the turn as it
22
- * answered, and the proposal's words. A proposal the checks kept holds none of them already (unstorable refuses it
23
- * whole). */
19
+ /** A learning kept beside the run's end: its state, reason and words, and what it wrote. */
20
+ const keptRow = (proposal) => ({ table: "learning_proposals", row: { turn: null, proposal } });
21
+ const dropped = (reason, detail, extra = {}) => keptRow({ ...extra, state: "dropped", reason, detail });
22
+ /** A kept row with every string in it cut of `refuse` in every form and of every secret-shaped run (learn/secrets.ts
23
+ * redactSecrets), with an empty list too. */
24
24
  const cutRow = (row, refuse) => row.table === "learning_proposals" ? { table: row.table, row: cutStrings(row.row, (text) => redactSecrets(text, refuse, Number.POSITIVE_INFINITY)) } : row;
25
- /** The first workflow draft the checks left out of `checked` because its dry run failed (learn/skills.ts DRY_RUN_FAILED):
26
- * its path and the check's words; undefined when none was. */
27
- function dryRunFailure(checked) {
28
- const problems = (checked.state === "proposed" ? checked.left ?? [] : checked.state === "dropped" && checked.reason === "checker" ? checked.problems : []);
29
- const found = problems.find((problem) => problem?.rule === "workflow" && typeof problem.path === "string" && typeof problem.detail === "string" && problem.detail.startsWith(DRY_RUN_FAILED));
30
- return found ? { path: found.path, detail: found.detail } : undefined;
31
- }
32
25
  /** `rows` with round `round` on each learning row (none on a run's first, round 1). */
33
- const roundOf = (rows, round) => !rows || round === 1 ? rows : { also: rows.also.map((row) => (row.table === "learning_proposals" || row.table === "learning_run" ? { ...row, row: { ...row.row, round } } : row)) };
34
- /** The end-of-run turn for runs that learn. */
26
+ const roundOf = (rows, round) => !rows || round === 1 ? rows : { also: rows.also.map((row) => (row.table === "learning_proposals" ? { ...row, row: { ...row.row, round } } : row)) };
27
+ /** The end-of-run step for runs that learn. */
35
28
  export function createReflectHook(deps) {
36
29
  const round = deps.round ?? 1;
37
30
  const hook = async (input) => {
@@ -39,9 +32,8 @@ export function createReflectHook(deps) {
39
32
  if (!facts.learns)
40
33
  return undefined;
41
34
  const now = deps.now ?? Date.now;
42
- // When the job answered and what it cost (learning-run.ts setLearning "reflecting"): as the turn starts, the run's
43
- // spend so far is the job's. A turn an earlier claim asked first (cut by a crash or a lost claim) reads that ask's,
44
- // which it kept with its prompt (the ask's `kept`): what the earlier attempt spent is the look-back's, never the job's.
35
+ // When the job answered and what it cost: as the step starts, the run's spend so far is the job's. A compile turn an
36
+ // earlier claim asked first (cut by a crash or a lost claim) reads that ask's, which it kept with its prompt.
45
37
  let answeredAt = new Date(now()).toISOString();
46
38
  let job = facts.costUsd;
47
39
  const firstAsk = (kept) => {
@@ -50,193 +42,88 @@ export function createReflectHook(deps) {
50
42
  [answeredAt, job] = [kept.answered_at, kept.job_cost_usd];
51
43
  return true;
52
44
  };
53
- // A look-back already started (its first ask's figures on the record, keptOf), read before any skip or cut: one this
54
- // claim closes without asking still keeps when the job answered, its cost, and what looking back spent since.
55
45
  const begun = firstAsk(input.keptOf?.(round));
56
- // A later round's: the follow-up's own spend, the run's spend now less what the round before it kept (its job's and
57
- // its look-back's), unknown when any is.
58
- const earlier = deps.before;
59
- const followUp = () => (round === 1 ? {} : { follow_up_usd: job === null || typeof earlier?.job_cost_usd !== "number" || typeof earlier.look_back_usd !== "number" ? null : Math.max(0, Math.round((job - earlier.job_cost_usd - earlier.look_back_usd) * 1e9) / 1e9) });
60
- // closeLearning: what looking back cost, the run's spend once the turn closed (its re-ask included) less the job's,
61
- // unknown when either is. Kept with every end the turn reaches from here, beside when the job answered and its cost.
62
- const looked = async (rows) => {
46
+ // What the step's model turns cost: the run's spend once it closed less the job's, unknown when either is. Kept with
47
+ // every end once a compile turn was asked; 0 for an end that asked none. The step's only model turn is its compile
48
+ // turn, so this is the compile's spend: it joins the revision it drafts, or the bill's unattributed when it kept no
49
+ // draft (store-reads/learning.ts compilesOf, learning-cost.ts amortize), never the run's own.
50
+ let asked = begun;
51
+ const spent = async () => {
52
+ if (!asked)
53
+ return { answered_at: answeredAt, job_cost_usd: job, look_back_usd: 0 };
63
54
  const total = (await deps.facts(input.runId)).costUsd;
64
- const answered = { answered_at: answeredAt, job_cost_usd: job, look_back_usd: total === null || job === null ? null : Math.max(0, Math.round((total - job) * 1e9) / 1e9), ...followUp() };
65
- return { also: rows.also.map((row) => (row.table === "learning_proposals" ? { ...row, row: { ...row.row, proposal: { ...row.row.proposal, ...answered } } } : row)) };
55
+ return { answered_at: answeredAt, job_cost_usd: job, look_back_usd: total === null || job === null ? null : Math.max(0, Math.round((total - job) * 1e9) / 1e9) };
66
56
  };
67
- // A run cut or skipped here asks nothing: its drop alone, or with looked's figures when its look-back had started.
68
- const unasked = async (row) => (begun ? looked({ also: [row] }) : { also: [row] });
69
- // closeLearning for a run cut before it could look back: a person's stop, before or during the turn, or a fault.
70
- if (input.cut === "stopped")
71
- return unasked(droppedRow(null, "cancelled", said.stopped(STOPPED.cancelled)));
57
+ // An end that asks nothing more: its drop alone, or, when an earlier claim asked the compile turn (stopped during it),
58
+ // with that ask's figures and what the turn spent since, so the run's cost stays the job's own and the rest learning's.
59
+ const unasked = async (row) => (begun ? { also: [keptRow({ ...row.row.proposal, ...(await spent()) })] } : { also: [row] });
60
+ // A person's stop, before or during the step, or a fault: nothing kept.
61
+ if (input.cut === "stopped" || facts.cancelled)
62
+ return unasked(dropped("cancelled", said.stopped(STOPPED.cancelled)));
72
63
  if (input.cut === "stopped-looking")
73
- return unasked(droppedRow(null, "cancelled", said.stopped(STOPPED.cancelledLooking)));
64
+ return unasked(dropped("cancelled", said.stopped(STOPPED.cancelledLooking)));
74
65
  if (input.cut === "faulted")
75
- return unasked(droppedRow(null, "crashed", said.stopped(STOPPED.crashed)));
76
- // reflectSkip: the cap keeps no reserve; a run its cap stopped has no room left, and one whose spend is unknown
77
- // cannot be kept within its cap.
78
- if (facts.cancelled)
79
- return unasked(droppedRow(null, "cancelled", said.stopped(STOPPED.cancelled)));
80
- if (facts.capReached)
81
- return unasked(droppedRow(null, "no_budget", said.noBudget));
82
- if (facts.capUsd !== undefined && facts.spentUsd === null)
83
- return unasked(droppedRow(null, "unknown_cost", said.unknownCost));
84
- // A job that only ran its learned workflows, each answering, with nothing said after its first message, gave nothing
85
- // new to learn: it is not asked its look-back (no model request), and unless a compile is due (a held revision of
86
- // its workflow, asked while its champion serves) looking back cost nothing, $0 whether or not the job's own cost is
87
- // known. Its words are the server's own.
88
- const workflowOnly = input.outcome === "completed" && deps.nothingNew !== undefined && (await deps.nothingNew(input.runId).catch(() => false));
89
- // A run of a job that compiles workflows with no word from its owner is not asked its look-back either (ownerSaid):
90
- // its job's trace is what a compile reads, and a look-back on every agent run cost more than the run. The same way:
91
- // its compile turn, when due, asked alone, else $0 looking back.
92
- const unheard = !workflowOnly && deps.ownerSaid !== undefined && !(await deps.ownerSaid(input.runId).catch(() => true));
93
- const alone = workflowOnly || unheard;
94
- const notAsked = workflowOnly ? said.onlyWorkflow : said.noFeedback;
95
- const onlyWorkflow = () => ({ also: [droppedRow(null, "empty", notAsked, { answered_at: answeredAt, job_cost_usd: job, look_back_usd: 0, ...followUp() })] });
96
- if (alone && !deps.compileFacts)
97
- return onlyWorkflow();
98
- // What the run must never learn, read once, before it is asked anything: a run whose list can't be read learns
99
- // nothing, never over a shorter one.
66
+ return unasked(dropped("crashed", said.stopped(STOPPED.crashed)));
67
+ // What the run must never learn, read once, before anything: a run whose list can't be read learns nothing, never
68
+ // over a shorter one.
100
69
  const read = deps.refuse ? await deps.refuse(input.runId).catch(() => "unreadable") : [];
101
- const refuse = read === "unreadable" ? [] : read;
102
- // The compile turn's facts (learn/compile.ts), read once: a run not looked back on with none due ends here, as above.
103
- const compileOf = async () => (deps.compileFacts && read !== "unreadable" ? await deps.compileFacts({ runId: input.runId, agentId: input.agentId, outcome: input.outcome, refuse }).catch(() => undefined) : undefined);
104
- const early = alone ? await compileOf() : undefined;
105
- // None due now, but a compile request this run already asked and kept (a crash cut it, another run merged a workflow
106
- // since) was paid: its cost stays the look-back's, as every other cut's does (`begun`), never the task's.
107
- if (alone && !early)
108
- return begun ? unasked(droppedRow(null, "empty", notAsked)) : onlyWorkflow();
109
- // Every end from here closes through this one function: looked's timings first, then the cut LAST, over every
110
- // string the kept rows hold, whatever looked adds now or later.
111
- const close = async (rows) => ({ also: (await looked({ also: rows })).also.map((row) => cutRow(row, refuse)) });
112
70
  if (read === "unreadable")
113
- return close([droppedRow(null, "propose_failed", said.stopped(STOPPED.refuseUnread))]);
114
- const deadlineMs = deps.deadlineMs ?? REFLECT_TIMEOUT_MS;
115
- const started = now();
116
- const ask = { origin: "reflect", ...(round > 1 ? { round } : {}) };
117
- let heard = { asked: [] };
118
- const turnOf = (answer) => ({ answer, asked: [...heard.asked], ...(heard.triggered?.length ? { triggered: heard.triggered } : {}) });
119
- const checkOf = async (turn, revised) => {
120
- try {
121
- return await deps.check({ runId: input.runId, agentId: input.agentId, turn, outcome: input.outcome, refuse, ...(revised ? { revised } : {}) });
122
- }
123
- catch {
124
- return undefined;
125
- }
126
- };
127
- // The look-back: asked of every run but a workflow-only one or one its owner said nothing on, whose compile turn is
128
- // asked alone, from no answer of its own: nothing the look-back kept goes with its draft.
129
- let turn = null;
130
- let checked = { state: "dropped", reason: "empty" };
131
- if (alone)
132
- heard = await deps.asked(input.runId);
133
- else {
134
- const { active } = await deps.promptFacts(input.runId, input.agentId);
135
- const end = await input.ask(reflectPrompt({ dir: LEARNED_DIR, active, ...(round > 1 && deps.before?.looked !== false ? { later: true } : {}) }), { ...ask, deadlineMs, keep: { answered_at: answeredAt, job_cost_usd: job } });
136
- firstAsk(end.kept);
137
- // closeLearning: how the turn ended.
138
- if ("timedOut" in end)
139
- return close([droppedRow(null, "reflect_timeout", said.stopped(STOPPED.timedOut))]);
140
- if ("failed" in end)
141
- return close([CAP_REFUSAL.test(end.failed.message) ? droppedRow(null, "no_budget", said.noBudget) : droppedRow(null, "reflect_failed", said.stopped(STOPPED.failed))]);
142
- heard = await deps.asked(input.runId);
143
- turn = turnOf(end.answer);
144
- const first = await checkOf(turn);
145
- if (!first)
146
- return close([droppedRow(turn, "propose_failed", said.stopped(STOPPED.notChecked))]);
147
- checked = first;
148
- }
149
- // An answer the parser refused is asked for again once, in the same turn, saying why in the parser's own words: the
150
- // second answer is checked as the first was, and refused again it is dropped as before. Never a third time, and only
151
- // within the turn's own deadline; a re-ask that times out or fails leaves the first answer's drop.
152
- if (checked.state === "dropped" && checked.reason === "unparsed") {
153
- const left = deadlineMs - (now() - started);
154
- const again = left > 0 ? await input.ask(reflectAgain(checked.detail), { ...ask, deadlineMs: left, again: true }) : undefined;
155
- if (again && "ended" in again) {
156
- turn = turnOf(again.answer);
157
- const second = await checkOf(turn);
158
- if (!second)
159
- return close([droppedRow(turn, "propose_failed", said.stopped(STOPPED.notChecked))]);
160
- checked = second;
161
- }
162
- }
163
- else {
164
- // A workflow draft the checks left out because its dry run failed is asked for again once, in the same turn and
165
- // within its deadline, saying which draft and why: the second answer stands only when the checks keep it and no
166
- // draft in it still fails its dry run, else the first answer's, that draft left out, stands (a repair that failed
167
- // never costs a note the first answer kept). Never a third time (a turn asks again once, for either reason).
168
- const dry = dryRunFailure(checked);
169
- const left = deadlineMs - (now() - started);
170
- const again = dry && left > 0 ? await input.ask(reflectDryRun(dry.path, dry.detail), { ...ask, deadlineMs: left, again: true }) : undefined;
171
- if (again && "ended" in again) {
172
- const second = turnOf(again.answer);
173
- const kept = await checkOf(second);
174
- if (kept?.state === "proposed" && !dryRunFailure(kept)) {
175
- turn = second;
176
- checked = kept;
177
- }
178
- }
179
- }
180
- // The compile turn (learn/compile.ts): the job as a learned workflow, or a revision of one, checked as a draft is,
181
- // its refusals sent back up to COMPILE_RETRIES times. A draft that passed goes in the proposal beside the notes; one
182
- // that never did is never silent: its errors go with the learning, and its problems to its end.
183
- const compile = alone ? early : await compileOf();
71
+ return unasked(dropped("reflect_failed", said.stopped(STOPPED.refuseUnread)));
72
+ const refuse = read;
73
+ const close = async (row) => ({ also: [cutRow(row.table === "learning_proposals" ? keptRow({ ...row.row.proposal, ...(await spent()) }) : row, refuse)] });
74
+ // The owner's words: one line of corrections.md, checked against main now so a refused one never holds a draft back.
75
+ const words = (await deps.feedback(input.runId).catch(() => [])).map((text) => text.trim()).filter(Boolean);
76
+ const correction = words.length ? words.join(" ") : undefined;
77
+ const refusedCorrection = correction === undefined ? [] : await deps.correctionRefused({ runId: input.runId, agentId: input.agentId, round, text: correction, refuse }).catch(() => [{ rule: "encoding", detail: "the owner's correction could not be checked" }]);
78
+ const taken = correction !== undefined && !refusedCorrection.length ? correction : undefined;
79
+ // The compile turn, when one is due and the run's cap has room: a cap it reached, or a spend under it that is unknown,
80
+ // asks no model turn (the cap keeps no reserve).
81
+ const noRoom = facts.capReached ? "no_budget" : facts.capUsd !== undefined && facts.spentUsd === null ? "unknown_cost" : undefined;
82
+ const workflowOnly = input.outcome === "completed" && deps.nothingNew !== undefined && (await deps.nothingNew(input.runId).catch(() => false));
83
+ const compile = !noRoom && deps.compileFacts ? await deps.compileFacts({ runId: input.runId, agentId: input.agentId, outcome: input.outcome, refuse, round }).catch(() => undefined) : undefined;
84
+ let draft;
184
85
  let compiled;
86
+ // A draft the checks refused: their problems by rule (a draft never answered refused nothing).
87
+ let draftProblems = [];
185
88
  if (compile) {
186
- // A compile turn asked alone (no look-back) is the round's first ask: it keeps the job's figures
187
- // with its prompt as the look-back's does, and a turn an earlier claim asked first answers with that ask's (`kept`),
188
- // so a restart mid compile never moves what the earlier attempt spent compiling into the job's. Each attempt keeps
189
- // the figures as they stand once the attempts before it answered (an earlier claim's, when it asked first).
190
- // A revision of main's text keeps the commit of main it was read at (`compile_base`) with each attempt the same way:
191
- // an attempt an earlier claim asked answers with that claim's commit, never the one this claim read (main may have
192
- // moved since: another run's change to the workflow), and the attempts after it keep that one too. It is the revised
193
- // workflow's conflict base in every check of the turn, so an answer written over an older main never overwrites a
194
- // change made after it.
89
+ // A revision of main's text keeps the commit of main it was read at (`compile_base`) with each attempt: an attempt
90
+ // an earlier claim asked answers with that claim's commit, never the one this claim read (main may have moved
91
+ // since), and it is the revised workflow's conflict base in every check of the turn.
195
92
  let revised = compile.mode === "revise" && compile.base ? { workflow: compile.workflow, base: compile.base } : undefined;
196
- /** An attempt the cap refused, or whose spend took the run past it: the learning is dropped as the look-back's is. */
197
93
  let capped = false;
198
94
  const result = await compileTurn({
199
95
  ask: async (prompt, attempt, left) => {
200
- const keep = { ...(alone ? { answered_at: answeredAt, job_cost_usd: job } : {}), ...(revised ? { compile_base: revised.base } : {}) };
201
- const said_ = await input.ask(prompt, { ...ask, deadlineMs: left, step: `compile-${attempt}`, ...(Object.keys(keep).length ? { keep } : {}) });
202
- if ("failed" in said_ && CAP_REFUSAL.test(said_.failed.message))
96
+ asked = true;
97
+ const keep = { answered_at: answeredAt, job_cost_usd: job, ...(revised ? { compile_base: revised.base } : {}) };
98
+ const answer = await input.ask(prompt, { origin: "reflect", ...(round > 1 ? { round } : {}), deadlineMs: left, step: `compile-${attempt}`, keep });
99
+ if ("failed" in answer && CAP_REFUSAL.test(answer.failed.message))
203
100
  capped = true;
204
- if (alone)
205
- firstAsk(said_.kept);
206
- const base = said_.kept?.compile_base;
101
+ firstAsk(answer.kept);
102
+ const base = answer.kept?.compile_base;
207
103
  if (revised && typeof base === "string")
208
104
  revised = { ...revised, base };
209
- return "ended" in said_ ? { answer: said_.answer } : undefined;
105
+ return "ended" in answer ? { answer: answer.answer } : undefined;
210
106
  },
211
107
  compile,
212
108
  refuse,
213
- // The look-back's own answer, when the checks kept it: its notes and skills go on with the draft.
214
- answer: checked.state === "proposed" && turn ? turn.answer : "",
215
- check: (answer) => checkOf(turnOf(answer), revised),
109
+ check: (one) => deps.checkDraft({ runId: input.runId, agentId: input.agentId, draft: one, refuse, ...(revised ? { revised } : {}) }),
216
110
  ...(deps.compileDeadlineMs !== undefined ? { deadlineMs: deps.compileDeadlineMs } : {}),
217
111
  now,
218
112
  });
219
- // closeLearning, as the look-back's turn when the cap has no room: the learning is dropped no_budget, never graded
220
- // on a cap it has passed or cannot fit.
221
- if (capped)
222
- return close([droppedRow(turn, "no_budget", said.noBudget)]);
223
- if (result.kept?.checked.state === "proposed") {
224
- const { workflow, from_notes } = result.record;
225
- turn = turnOf(result.kept.answer);
226
- checked = { ...result.kept.checked, proposal: { ...result.kept.checked.proposal, compiled: { workflow, from_notes } } };
113
+ if (capped && !result.kept) {
114
+ if (!taken)
115
+ return close(dropped("no_budget", said.noBudget));
116
+ }
117
+ else if (result.kept) {
118
+ draft = result.kept;
227
119
  compiled = { record: result.record };
228
120
  }
229
121
  else if (result.record) {
230
- const named = result.record.workflow !== UNNAMED ? { path: `workflows/${result.record.workflow}/workflow.json` } : {};
231
- const problems = result.draftErrors.map((detail) => ({ rule: "workflow", ...named, detail }));
232
- // Beside what the look-back kept, as what the checks left out; in place of a look-back that kept nothing, as the
233
- // checks' drop; beside the checks' own drop, with it. A look-back whose answer could not be read keeps its reason,
234
- // the draft's errors beside it.
235
- checked = checked.state === "proposed" ? { ...checked, left: [...(checked.left ?? []), ...problems] }
236
- : checked.reason === "empty" ? { state: "dropped", reason: "checker", problems }
237
- : checked.reason === "checker" ? { ...checked, problems: [...checked.problems, ...problems] }
238
- : checked;
239
122
  compiled = { record: result.record, draft_errors: result.draftErrors };
123
+ // Each error names its file already (checkDraftOf); one with no rule behind it (an answer the compile could not
124
+ // read) is the workflow's.
125
+ if (result.drafted)
126
+ draftProblems = result.draftProblems ?? result.draftErrors.map((detail) => ({ rule: "workflow", detail }));
240
127
  // Compile once: its failed drafts are the compile's attempt, on record before the learning closes. The draft's
241
128
  // name and errors are the model's words: cut of `refuse` as the proposal row is (cutRow) before they are saved
242
129
  // on the promotion record or logged, whose reads cut only the server's secrets, never this run's.
@@ -246,20 +133,39 @@ export function createReflectHook(deps) {
246
133
  }
247
134
  }
248
135
  const compiledFields = compiled ? { compiled: compiled.record, ...(compiled.draft_errors?.length ? { draft_errors: compiled.draft_errors } : {}) } : {};
249
- if (checked.state === "dropped") {
250
- const detail = checked.reason === "empty" ? (alone ? notAsked : said.empty) : checked.reason === "unparsed" ? said.unparsed : said.checker(checked.problems);
251
- return close([droppedRow(turn, checked.reason, detail, { ...checked, ...compiledFields })]);
136
+ if (draft || taken !== undefined) {
137
+ const done = await deps.commit({ runId: input.runId, agentId: input.agentId, round, ...(draft ? { draft } : {}), ...(taken !== undefined ? { correction: taken } : {}), refuse }).catch((error) => ({ ok: false, reason: "unavailable", problems: [{ rule: "encoding", detail: error instanceof Error ? error.message : String(error) }] }));
138
+ if (done.ok) {
139
+ const readme = draft?.files.find((file) => file.path.endsWith("/README.md"));
140
+ const json = draft?.files.find((file) => file.path.endsWith("/workflow.json"));
141
+ const workflows = draft && json ? [{ file: json.path, name: draft.name, ...(readme && draft.held?.includes(readme.path) ? { pending: true } : {}) }] : [];
142
+ // The draft and the correction are judged apart: one the merge left out (corrections.md filled during the compile)
143
+ // is reported as the check before the compile reports one.
144
+ const left = done.refusedCorrection?.length ? done.refusedCorrection : undefined;
145
+ const corrections = taken !== undefined && !left ? 1 : 0;
146
+ const problems = [...refusedCorrection, ...(left ?? []), ...draftProblems];
147
+ return close(keptRow({ state: "merged", reason: null, merged: done.commit, ...(workflows.length ? { workflows } : {}), ...(corrections ? { corrections } : {}), ...(problems.length ? { problems } : {}), ...compiledFields, detail: said.merged(workflows, corrections, problems) }));
148
+ }
149
+ if (done.reason === "unavailable")
150
+ return close(dropped("reflect_failed", said.stopped(STOPPED.failed), compiledFields));
151
+ return close(dropped("checker", correctionWords(done.problems), { problems: [...done.problems], ...compiledFields }));
252
152
  }
253
- // The learning run's cap: what the run's cap leaves now its look-back is spent, read after the checks. A spend under
254
- // the cap that is unknown (or a read that fails, never no cap) queues no grade: none could be kept within the cap, and
255
- // the words say the spend is unknown, never that the cap has no room (learning-end.ts capSpendOf's rule).
256
- const capUsd = deps.gradeCap ? await deps.gradeCap(input.runId).catch(() => null) : undefined;
257
- if (capUsd === null)
258
- return close([droppedRow(turn, "unknown_cost", said.gradeUnknownSpend)]);
259
- return close([
260
- { table: "learning_proposals", row: { turn, proposal: { ...checked, ...compiledFields, detail: said.proposed(checked.notes, checked.skills, checked.workflows) } } },
261
- { table: "learning_run", row: { id: deps.newRunId(), ...(deps.gradeModel ? { model: deps.gradeModel } : {}), ...(capUsd !== undefined ? { capUsd } : {}) } },
262
- ]);
153
+ if (refusedCorrection.length)
154
+ return close(dropped("checker", correctionWords(refusedCorrection), { problems: [...refusedCorrection, ...draftProblems], ...compiledFields }));
155
+ if (draftProblems.length)
156
+ return close(dropped("checker", said.checker(draftProblems), { problems: draftProblems, ...compiledFields }));
157
+ // A compile turn that never answered refused nothing: it failed, its attempts kept.
158
+ if (compiled && !draft && compiled.draft_errors?.length)
159
+ return close(dropped("reflect_failed", said.stopped(STOPPED.unanswered), compiledFields));
160
+ if (noRoom && compile === undefined && deps.compileFacts)
161
+ return close(dropped(noRoom, noRoom === "no_budget" ? said.noBudget : said.unknownCost));
162
+ return close(dropped("empty", workflowOnly ? said.onlyWorkflow : said.empty, compiledFields));
263
163
  };
264
164
  return async (input) => roundOf(await hook(input), round);
265
165
  }
166
+ /** Why a correction (or a merge) was refused, in a person's words: the cap's own sentence when the cap refused it, else
167
+ * the checks'. */
168
+ function correctionWords(problems) {
169
+ const cap = correctionsCapOf(problems);
170
+ return cap ? said.correctionsFull(cap.detail) : problems.length ? said.checker(problems) : said.stopped(STOPPED.notChecked);
171
+ }
@@ -0,0 +1,43 @@
1
+ import type { Duplex } from "node:stream";
2
+ /** The most a frame may hold, in bytes: a journal commit of a long turn fits many times over. */
3
+ export declare const MAX_FRAME: number;
4
+ /** The channel ended: its socket closed, or a frame broke the protocol. Every call in flight fails with it. */
5
+ export declare class ChannelClosed extends Error {
6
+ name: string;
7
+ }
8
+ /** A call the other side refused before running it: no such target or method. */
9
+ export declare class ChannelRefused extends Error {
10
+ name: string;
11
+ }
12
+ export declare function registerErrors(classes: Record<string, abstract new (...args: never[]) => Error>): void;
13
+ /** What a target exposed by name does with a call: its method and arguments, as decoded. */
14
+ export type Dispatch = (method: string, args: unknown[]) => unknown;
15
+ export interface ChannelOptions {
16
+ /** The most a frame may hold (MAX_FRAME). */
17
+ readonly maxFrame?: number;
18
+ /** Called once when the channel ends, with why. */
19
+ readonly onClose?: (why: Error) => void;
20
+ /** What each call this side makes carries beside its arguments, encoded as they are (a run's process: the ledger of
21
+ * the tool call it is made in, paid-calls.ts). None: nothing. */
22
+ readonly context?: () => unknown;
23
+ /** Runs each call the other side makes inside what it carried (a runner: paid-calls.ts bookedIn on that ledger). */
24
+ readonly within?: (carried: unknown, run: () => unknown) => unknown;
25
+ }
26
+ /** One side of the channel over `socket`. */
27
+ export declare class Channel {
28
+ #private;
29
+ constructor(socket: Duplex, options?: ChannelOptions);
30
+ get closed(): boolean;
31
+ /** How many functions, objects and signals this side has handed the other and still keeps for it. */
32
+ get handed(): number;
33
+ /** Answers calls to `name` with `dispatch`. */
34
+ expose(name: string, dispatch: Dispatch): void;
35
+ /** Calls `method` of target `to` with `args`: its answer, or its error. */
36
+ call(to: string | number, method: string, args: readonly unknown[]): Promise<unknown>;
37
+ /** Sends `method` of target `to` with `args`, with no answer. In order with every call. */
38
+ post(to: string | number, method: string, args: readonly unknown[]): void;
39
+ /** A stand-in for target `name`: `stub.a.b(...args)` calls method "a.b" of it. `props`: values read without a call. */
40
+ remote<T>(name: string, props?: Record<string, unknown>): T;
41
+ /** Ends the channel: every call in flight fails with `why`, and the socket is destroyed. */
42
+ close(why?: Error): void;
43
+ }