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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (346) hide show
  1. package/CHANGELOG.md +131 -5
  2. package/README.md +371 -300
  3. package/dist/acp-package.d.ts +28 -0
  4. package/dist/acp-package.js +23 -0
  5. package/dist/adapter-facts.d.ts +8 -1
  6. package/dist/adapter-facts.js +9 -1
  7. package/dist/adapters.d.ts +5 -0
  8. package/dist/adapters.js +36 -3
  9. package/dist/agent-delete.d.ts +50 -0
  10. package/dist/agent-delete.js +125 -0
  11. package/dist/agents-store.d.ts +9 -3
  12. package/dist/agents-store.js +22 -8
  13. package/dist/agents.d.ts +5 -1
  14. package/dist/agents.js +8 -4
  15. package/dist/api-only.d.ts +8 -3
  16. package/dist/api-only.js +10 -4
  17. package/dist/archil-routes.d.ts +5 -1
  18. package/dist/archil-routes.js +57 -2
  19. package/dist/browser-wait-ended.d.ts +2 -0
  20. package/dist/browser-wait-ended.js +2 -0
  21. package/dist/calendar/ics-faux.d.ts +3 -1
  22. package/dist/calendar/ics-faux.js +12 -2
  23. package/dist/calendar/tools.js +5 -2
  24. package/dist/chatgpt-plan.js +3 -1
  25. package/dist/claude-login.d.ts +135 -0
  26. package/dist/claude-login.js +244 -0
  27. package/dist/cli.js +52 -16
  28. package/dist/composio-preset.js +69 -7
  29. package/dist/composio-triggers.d.ts +14 -0
  30. package/dist/composio-triggers.js +79 -15
  31. package/dist/computer-attach.d.ts +40 -5
  32. package/dist/computer-attach.js +130 -15
  33. package/dist/computer-keys.d.ts +3 -0
  34. package/dist/computer-keys.js +7 -1
  35. package/dist/computer-live.js +3 -2
  36. package/dist/computers.d.ts +21 -10
  37. package/dist/computers.js +39 -28
  38. package/dist/console/assets/index-3O2Yl6hZ.js +701 -0
  39. package/dist/console/assets/index-DaFsZuzQ.css +1 -0
  40. package/dist/console/index.html +2 -2
  41. package/dist/decisions.d.ts +1 -1
  42. package/dist/dial-relay.d.ts +29 -0
  43. package/dist/dial-relay.js +94 -0
  44. package/dist/email/tools.js +10 -5
  45. package/dist/env-proxy.d.ts +2 -0
  46. package/dist/env-proxy.js +35 -0
  47. package/dist/errors.d.ts +3 -1
  48. package/dist/errors.js +7 -4
  49. package/dist/faux-computers.d.ts +8 -2
  50. package/dist/faux-computers.js +83 -6
  51. package/dist/harness-archive.d.ts +67 -0
  52. package/dist/harness-archive.js +177 -0
  53. package/dist/harness-goose.d.ts +34 -0
  54. package/dist/harness-goose.js +87 -0
  55. package/dist/harness-install.d.ts +21 -0
  56. package/dist/harness-install.js +24 -11
  57. package/dist/harness-opencode/adapter.js +1 -1
  58. package/dist/harness-table.d.ts +9 -1
  59. package/dist/harness-table.js +20 -3
  60. package/dist/harness.d.ts +1 -2
  61. package/dist/harness.js +3 -4
  62. package/dist/host.js +2 -1
  63. package/dist/key-check.js +3 -0
  64. package/dist/learn/audit.d.ts +6 -6
  65. package/dist/learn/audit.js +3 -7
  66. package/dist/learn/check.d.ts +23 -89
  67. package/dist/learn/check.js +57 -335
  68. package/dist/learn/commit.d.ts +63 -90
  69. package/dist/learn/commit.js +124 -316
  70. package/dist/learn/compile.d.ts +53 -50
  71. package/dist/learn/compile.js +87 -102
  72. package/dist/learn/corrections.d.ts +13 -0
  73. package/dist/learn/corrections.js +23 -0
  74. package/dist/learn/delete.d.ts +5 -5
  75. package/dist/learn/delete.js +8 -10
  76. package/dist/learn/folder.d.ts +35 -47
  77. package/dist/learn/folder.js +118 -134
  78. package/dist/learn/index.d.ts +3 -5
  79. package/dist/learn/index.js +6 -9
  80. package/dist/learn/learning.sh +45 -15
  81. package/dist/learn/promote.d.ts +13 -1
  82. package/dist/learn/promote.js +9 -0
  83. package/dist/learn/{skills.d.ts → workflow-draft.d.ts} +30 -38
  84. package/dist/learn/{skills.js → workflow-draft.js} +48 -58
  85. package/dist/learn/workflow-nodes.js +1 -1
  86. package/dist/learned-workflow.js +2 -2
  87. package/dist/learning-cost.d.ts +38 -24
  88. package/dist/learning-cost.js +41 -24
  89. package/dist/learning-routes.d.ts +91 -170
  90. package/dist/learning-routes.js +183 -421
  91. package/dist/learning-run.d.ts +19 -40
  92. package/dist/learning-run.js +23 -93
  93. package/dist/learning-setting.d.ts +4 -4
  94. package/dist/learning-setting.js +6 -6
  95. package/dist/learning-words.d.ts +28 -74
  96. package/dist/learning-words.js +64 -162
  97. package/dist/learning.d.ts +61 -392
  98. package/dist/learning.js +101 -651
  99. package/dist/live-reach.d.ts +25 -2
  100. package/dist/live-reach.js +46 -4
  101. package/dist/llms.txt +1 -1
  102. package/dist/local-models.d.ts +3 -11
  103. package/dist/local-models.js +5 -6
  104. package/dist/mcp-servers-store.d.ts +4 -4
  105. package/dist/model-form.js +6 -5
  106. package/dist/model.d.ts +4 -0
  107. package/dist/model.js +26 -9
  108. package/dist/native/binding.js +1 -1
  109. package/dist/native/connection.js +1 -1
  110. package/dist/native/discovery.js +1 -1
  111. package/dist/native/instructions.d.ts +3 -0
  112. package/dist/native/instructions.js +5 -3
  113. package/dist/native/remote-worker-bridge.js +9 -43
  114. package/dist/native/remote.d.ts +8 -15
  115. package/dist/native/remote.js +3 -3
  116. package/dist/native/schema.js +1 -1
  117. package/dist/native/types.d.ts +2 -2
  118. package/dist/openapi.js +268 -316
  119. package/dist/paid-calls.d.ts +8 -0
  120. package/dist/paid-calls.js +20 -0
  121. package/dist/particle-provider.d.ts +11 -0
  122. package/dist/particle-provider.js +135 -0
  123. package/dist/port-models.d.ts +12 -4
  124. package/dist/port-models.js +28 -11
  125. package/dist/print.d.ts +27 -0
  126. package/dist/print.js +21 -0
  127. package/dist/provider.d.ts +7 -0
  128. package/dist/provider.js +15 -1
  129. package/dist/reflect-range.js +2 -3
  130. package/dist/run-launch.d.ts +14 -28
  131. package/dist/run-launch.js +9 -12
  132. package/dist/run-learning-served.d.ts +15 -89
  133. package/dist/run-listing.d.ts +34 -0
  134. package/dist/run-listing.js +128 -0
  135. package/dist/run-model-key.js +1 -1
  136. package/dist/run-timeline-served.js +5 -3
  137. package/dist/run-trace.d.ts +97 -0
  138. package/dist/run-trace.js +253 -0
  139. package/dist/run-view.d.ts +15 -0
  140. package/dist/run-view.js +9 -0
  141. package/dist/runnable.js +2 -1
  142. package/dist/runner/adapter-loop.d.ts +8 -6
  143. package/dist/runner/adapter-loop.js +16 -12
  144. package/dist/runner/adapter-run-spec.d.ts +16 -4
  145. package/dist/runner/adapter-run-spec.js +64 -58
  146. package/dist/runner/adapters/index.js +22 -0
  147. package/dist/runner/adapters/pi.d.ts +4 -12
  148. package/dist/runner/adapters/pi.js +2 -2
  149. package/dist/runner/audit.d.ts +1 -1
  150. package/dist/runner/audit.js +16 -8
  151. package/dist/runner/beside.d.ts +6 -2
  152. package/dist/runner/beside.js +38 -9
  153. package/dist/runner/claim-secrets.d.ts +18 -2
  154. package/dist/runner/claim-secrets.js +42 -18
  155. package/dist/runner/cli-run-spec.js +55 -51
  156. package/dist/runner/conformance/harness.d.ts +6 -0
  157. package/dist/runner/conformance/harness.js +16 -5
  158. package/dist/runner/dispatch.d.ts +2 -2
  159. package/dist/runner/end-of-run.d.ts +6 -14
  160. package/dist/runner/end-of-run.js +8 -33
  161. package/dist/runner/feedback-round.d.ts +2 -2
  162. package/dist/runner/feedback-round.js +7 -7
  163. package/dist/runner/github-app.js +3 -1
  164. package/dist/runner/harness-loops.d.ts +0 -8
  165. package/dist/runner/harness-loops.js +11 -59
  166. package/dist/runner/held.d.ts +170 -0
  167. package/dist/runner/held.js +57 -0
  168. package/dist/runner/journal-kinds.d.ts +2 -3
  169. package/dist/runner/journal-kinds.js +2 -3
  170. package/dist/runner/learned-first.d.ts +40 -0
  171. package/dist/runner/learned-first.js +32 -0
  172. package/dist/runner/learning-end.d.ts +77 -69
  173. package/dist/runner/learning-end.js +250 -303
  174. package/dist/runner/learning-seed.d.ts +15 -33
  175. package/dist/runner/learning-seed.js +24 -45
  176. package/dist/runner/loop-contract.d.ts +2 -3
  177. package/dist/runner/main.d.ts +17 -9
  178. package/dist/runner/main.js +63 -20
  179. package/dist/runner/mcp-run.d.ts +11 -0
  180. package/dist/runner/mcp-run.js +67 -0
  181. package/dist/runner/memory-claim.d.ts +10 -5
  182. package/dist/runner/memory-claim.js +12 -6
  183. package/dist/runner/model-route.d.ts +7 -4
  184. package/dist/runner/model-route.js +22 -10
  185. package/dist/runner/native-loop.d.ts +0 -7
  186. package/dist/runner/native-loop.js +9 -101
  187. package/dist/runner/promotion.d.ts +8 -6
  188. package/dist/runner/promotion.js +19 -12
  189. package/dist/runner/reflect-hook.d.ts +64 -95
  190. package/dist/runner/reflect-hook.js +112 -225
  191. package/dist/runner/run-channel.d.ts +43 -0
  192. package/dist/runner/run-channel.js +467 -0
  193. package/dist/runner/run-child-host.d.ts +169 -0
  194. package/dist/runner/run-child-host.js +1222 -0
  195. package/dist/runner/run-child.d.ts +1 -0
  196. package/dist/runner/run-child.js +254 -0
  197. package/dist/runner/run-errors.d.ts +2 -0
  198. package/dist/runner/run-errors.js +20 -0
  199. package/dist/runner/runner.js +1 -5
  200. package/dist/runner/sdk-ask.d.ts +78 -0
  201. package/dist/runner/sdk-ask.js +158 -0
  202. package/dist/runner/sdk-loop.d.ts +2 -4
  203. package/dist/runner/sdk-loop.js +29 -49
  204. package/dist/runner/store-secrets.d.ts +5 -3
  205. package/dist/runner/store-secrets.js +5 -1
  206. package/dist/runner/workflow-first.d.ts +5 -0
  207. package/dist/runner/workflow-first.js +4 -0
  208. package/dist/runner/workflow-spec.js +7 -2
  209. package/dist/runs-over-store.d.ts +16 -28
  210. package/dist/runs-over-store.js +31 -38
  211. package/dist/sdk/runs.d.ts +6 -11
  212. package/dist/sdk/runs.js +20 -41
  213. package/dist/sdk/schema.js +3 -4
  214. package/dist/seal-key.d.ts +5 -0
  215. package/dist/seal-key.js +7 -0
  216. package/dist/secrets.d.ts +3 -0
  217. package/dist/secrets.js +16 -3
  218. package/dist/server.d.ts +55 -33
  219. package/dist/server.js +685 -320
  220. package/dist/sign-ins-store.d.ts +8 -0
  221. package/dist/sign-ins-store.js +36 -1
  222. package/dist/skills.js +1 -1
  223. package/dist/store/index.d.ts +2 -2
  224. package/dist/store/index.js +1 -1
  225. package/dist/store/journal/storage.js +3 -0
  226. package/dist/store/postgres/migrations/0017_run_listing.sql +83 -0
  227. package/dist/store/postgres/migrations/0018_trigger_template.sql +6 -0
  228. package/dist/store/postgres/migrations/0019_runner_relays.sql +28 -0
  229. package/dist/store/schema.d.ts +53 -4
  230. package/dist/store/schema.js +137 -6
  231. package/dist/store/sqlite/connection.d.ts +2 -0
  232. package/dist/store/sqlite/connection.js +3 -1
  233. package/dist/store/sqlite/run-patch.d.ts +0 -17
  234. package/dist/store/sqlite/run-patch.js +4 -33
  235. package/dist/store/sqlite/store.js +246 -63
  236. package/dist/store/sqlite/worker.js +25 -0
  237. package/dist/store/store.d.ts +193 -36
  238. package/dist/store/store.js +25 -3
  239. package/dist/store-reads/learning.d.ts +102 -204
  240. package/dist/store-reads/learning.js +159 -321
  241. package/dist/store-reads/loop.d.ts +0 -1
  242. package/dist/store-reads/loop.js +1 -1
  243. package/dist/store-view.d.ts +29 -1
  244. package/dist/store-view.js +97 -31
  245. package/dist/template-eval.d.ts +3 -6
  246. package/dist/template-eval.js +22 -36
  247. package/dist/templates.d.ts +2 -1
  248. package/dist/templates.js +6 -3
  249. package/dist/timeline/index.d.ts +1 -1
  250. package/dist/timeline/index.js +1 -1
  251. package/dist/timeline/timeline.d.ts +5 -1
  252. package/dist/timeline/timeline.js +16 -0
  253. package/dist/tool-gate.d.ts +0 -7
  254. package/dist/tool-gate.js +1 -15
  255. package/dist/tool-keys-store.d.ts +1 -1
  256. package/dist/tool-keys.d.ts +6 -4
  257. package/dist/tool-keys.js +12 -6
  258. package/dist/tools.d.ts +2 -2
  259. package/dist/tools.js +11 -4
  260. package/dist/triggers.d.ts +60 -5
  261. package/dist/triggers.js +123 -18
  262. package/dist/validate.js +1 -1
  263. package/dist/webhooks.d.ts +4 -1
  264. package/dist/webhooks.js +11 -4
  265. package/dist/workspace/agent-computer.d.ts +2 -0
  266. package/dist/workspace/agent-computer.js +2 -0
  267. package/dist/workspace/archil-claim.d.ts +7 -0
  268. package/dist/workspace/archil-claim.js +29 -0
  269. package/dist/workspace/archil.d.ts +9 -1
  270. package/dist/workspace/archil.js +165 -3
  271. package/dist/workspace/attach.d.ts +3 -1
  272. package/dist/workspace/attach.js +2 -0
  273. package/dist/workspace/attached-computers.d.ts +16 -5
  274. package/dist/workspace/attached-computers.js +25 -9
  275. package/dist/workspace/computer-deps.js +1 -1
  276. package/dist/workspace/computer-for-claim.d.ts +7 -3
  277. package/dist/workspace/computer-for-claim.js +119 -22
  278. package/dist/workspace/computer-hold.js +2 -1
  279. package/dist/workspace/computer-kept.d.ts +11 -1
  280. package/dist/workspace/computer-kept.js +24 -3
  281. package/dist/workspace/resolve-computer.d.ts +33 -19
  282. package/dist/workspace/resolve-computer.js +172 -64
  283. package/dist/workspace/revision-skills.d.ts +1 -2
  284. package/dist/workspace/revision-skills.js +3 -4
  285. package/dist/workspace/run-workspace.d.ts +1 -1
  286. package/dist/workspace/run-workspace.js +5 -0
  287. package/dist/workspace/synced-folder.d.ts +85 -0
  288. package/dist/workspace/synced-folder.js +230 -0
  289. package/dist/workspace/workspace.d.ts +4 -0
  290. package/launcher/agentrun-serve +1 -1
  291. package/package.json +10 -10
  292. package/templates/README.md +7 -7
  293. package/templates/inbound-lead/agent.md +3 -3
  294. package/templates/inbound-lead/evals/learned-correction/case.yaml +3 -3
  295. package/templates/inbound-lead/evals/learned-correction/fixtures/corrections.md +1 -0
  296. package/templates/inbound-lead/evals/learned-correction/graders/applies-correction.md +22 -0
  297. package/templates/inbound-lead/evals/learned-correction/graders/learned-origin.md +1 -1
  298. package/templates/inbound-lead/evals/learned-correction/prompt.md +1 -1
  299. package/templates/inbound-lead/lead.schema.json +6 -5
  300. package/templates/inbound-lead/rules/RUBRIC.md +4 -4
  301. package/templates/inbound-lead/skills/lead/SKILL.md +4 -4
  302. package/templates/market-monitor/evals/learned-preference/case.yaml +4 -4
  303. package/templates/market-monitor/evals/learned-preference/fixtures/corrections.md +1 -0
  304. package/templates/market-monitor/evals/learned-preference/graders/omits-junior-post.md +3 -3
  305. package/templates/market-monitor/evals/learned-preference/prompt.md +1 -1
  306. package/templates/release-notes/evals/learned-style/case.yaml +4 -4
  307. package/templates/release-notes/evals/learned-style/fixtures/corrections.md +1 -0
  308. package/templates/release-notes/evals/learned-style/graders/same-changes.md +2 -2
  309. package/templates/release-notes/evals/learned-style/prompt.md +1 -1
  310. package/dist/console/assets/index-BUktRvS-.css +0 -1
  311. package/dist/console/assets/index-nB9kTfIN.js +0 -711
  312. package/dist/learn/grade.d.ts +0 -138
  313. package/dist/learn/grade.js +0 -682
  314. package/dist/learn/notes.d.ts +0 -81
  315. package/dist/learn/notes.js +0 -178
  316. package/dist/learn/reflect.d.ts +0 -77
  317. package/dist/learn/reflect.js +0 -228
  318. package/dist/learn/relevance.d.ts +0 -24
  319. package/dist/learn/relevance.js +0 -91
  320. package/dist/learn/seed-check.d.ts +0 -74
  321. package/dist/learn/seed-check.js +0 -103
  322. package/dist/learning-batch.d.ts +0 -291
  323. package/dist/learning-batch.js +0 -600
  324. package/dist/learning-evals.d.ts +0 -35
  325. package/dist/learning-evals.js +0 -99
  326. package/dist/native/context.d.ts +0 -6
  327. package/dist/native/context.js +0 -22
  328. package/dist/propose.d.ts +0 -52
  329. package/dist/propose.js +0 -224
  330. package/dist/runner/learning-ask.d.ts +0 -47
  331. package/dist/runner/learning-ask.js +0 -128
  332. package/dist/runner/learning-dispatch.d.ts +0 -45
  333. package/dist/runner/learning-dispatch.js +0 -99
  334. package/dist/runner/learning-factory.d.ts +0 -16
  335. package/dist/runner/learning-factory.js +0 -50
  336. package/dist/runner/learning-loop.d.ts +0 -273
  337. package/dist/runner/learning-loop.js +0 -521
  338. package/dist/usage.d.ts +0 -11
  339. package/dist/usage.js +0 -11
  340. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/MEMORY.md +0 -1
  341. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/no-agencies.md +0 -14
  342. package/templates/inbound-lead/evals/learned-correction/graders/applies-note.md +0 -21
  343. package/templates/market-monitor/evals/learned-preference/fixtures/learning/MEMORY.md +0 -1
  344. package/templates/market-monitor/evals/learned-preference/fixtures/learning/skip-junior-job-posts.md +0 -8
  345. package/templates/release-notes/evals/learned-style/fixtures/learning/MEMORY.md +0 -1
  346. package/templates/release-notes/evals/learned-style/fixtures/learning/say-agent-not-bot.md +0 -13
@@ -1,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,208 +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
- /** Where the compile turn began, once one is asked: the run's spend as its first attempt was asked (an earlier claim's,
61
- * kept with that ask, after a restart), unknown when unread; `alone` when the compile is the round's only ask (the
62
- * whole look-back). Its turn's spend joins the revision it drafts (learning-cost.ts amortize), never the run's own. */
63
- let compileFrom;
64
- const cents = (usd) => Math.max(0, Math.round(usd * 1e9) / 1e9);
65
- // closeLearning: what looking back cost, the run's spend once the turn closed (its re-ask included) less the job's,
66
- // unknown when either is. Kept with every end the turn reaches from here, beside when the job answered and its cost;
67
- // with a compile asked, what of it the compile turn spent (`compile_usd`), from its first ask to the close.
68
- 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 };
69
54
  const total = (await deps.facts(input.runId)).costUsd;
70
- const lookBack = total === null || job === null ? null : cents(total - job);
71
- const compileUsd = compileFrom === undefined ? {} : { compile_usd: compileFrom === "alone" ? lookBack : total === null || compileFrom.usd === null ? null : cents(total - compileFrom.usd) };
72
- const answered = { answered_at: answeredAt, job_cost_usd: job, look_back_usd: lookBack, ...compileUsd, ...followUp() };
73
- 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) };
74
56
  };
75
- // A run cut or skipped here asks nothing: its drop alone, or with looked's figures when its look-back had started.
76
- const unasked = async (row) => (begun ? looked({ also: [row] }) : { also: [row] });
77
- // closeLearning for a run cut before it could look back: a person's stop, before or during the turn, or a fault.
78
- if (input.cut === "stopped")
79
- 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)));
80
63
  if (input.cut === "stopped-looking")
81
- return unasked(droppedRow(null, "cancelled", said.stopped(STOPPED.cancelledLooking)));
64
+ return unasked(dropped("cancelled", said.stopped(STOPPED.cancelledLooking)));
82
65
  if (input.cut === "faulted")
83
- return unasked(droppedRow(null, "crashed", said.stopped(STOPPED.crashed)));
84
- // reflectSkip: the cap keeps no reserve; a run its cap stopped has no room left, and one whose spend is unknown
85
- // cannot be kept within its cap.
86
- if (facts.cancelled)
87
- return unasked(droppedRow(null, "cancelled", said.stopped(STOPPED.cancelled)));
88
- if (facts.capReached)
89
- return unasked(droppedRow(null, "no_budget", said.noBudget));
90
- if (facts.capUsd !== undefined && facts.spentUsd === null)
91
- return unasked(droppedRow(null, "unknown_cost", said.unknownCost));
92
- // A job that only ran its learned workflows, each answering, with nothing said after its first message, gave nothing
93
- // new to learn: it is not asked its look-back (no model request), and unless a compile is due (a held revision of
94
- // its workflow, asked while its champion serves) looking back cost nothing, $0 whether or not the job's own cost is
95
- // known. Its words are the server's own.
96
- const workflowOnly = input.outcome === "completed" && deps.nothingNew !== undefined && (await deps.nothingNew(input.runId).catch(() => false));
97
- // A run of a job that compiles workflows with no word from its owner is not asked its look-back either (ownerSaid):
98
- // 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:
99
- // its compile turn, when due, asked alone, else $0 looking back.
100
- const unheard = !workflowOnly && deps.ownerSaid !== undefined && !(await deps.ownerSaid(input.runId).catch(() => true));
101
- const alone = workflowOnly || unheard;
102
- const notAsked = workflowOnly ? said.onlyWorkflow : said.noFeedback;
103
- const onlyWorkflow = () => ({ also: [droppedRow(null, "empty", notAsked, { answered_at: answeredAt, job_cost_usd: job, look_back_usd: 0, ...followUp() })] });
104
- if (alone && !deps.compileFacts)
105
- return onlyWorkflow();
106
- // What the run must never learn, read once, before it is asked anything: a run whose list can't be read learns
107
- // 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.
108
69
  const read = deps.refuse ? await deps.refuse(input.runId).catch(() => "unreadable") : [];
109
- const refuse = read === "unreadable" ? [] : read;
110
- // The compile turn's facts (learn/compile.ts), read once: a run not looked back on with none due ends here, as above.
111
- const compileOf = async () => (deps.compileFacts && read !== "unreadable" ? await deps.compileFacts({ runId: input.runId, agentId: input.agentId, outcome: input.outcome, refuse }).catch(() => undefined) : undefined);
112
- const early = alone ? await compileOf() : undefined;
113
- // None due now, but a compile request this run already asked and kept (a crash cut it, another run merged a workflow
114
- // since) was paid: its cost stays the look-back's, as every other cut's does (`begun`), never the task's.
115
- if (alone && !early)
116
- return begun ? unasked(droppedRow(null, "empty", notAsked)) : onlyWorkflow();
117
- // Every end from here closes through this one function: looked's timings first, then the cut LAST, over every
118
- // string the kept rows hold, whatever looked adds now or later.
119
- const close = async (rows) => ({ also: (await looked({ also: rows })).also.map((row) => cutRow(row, refuse)) });
120
70
  if (read === "unreadable")
121
- return close([droppedRow(null, "propose_failed", said.stopped(STOPPED.refuseUnread))]);
122
- const deadlineMs = deps.deadlineMs ?? REFLECT_TIMEOUT_MS;
123
- const started = now();
124
- const ask = { origin: "reflect", ...(round > 1 ? { round } : {}) };
125
- let heard = { asked: [] };
126
- const turnOf = (answer) => ({ answer, asked: [...heard.asked], ...(heard.triggered?.length ? { triggered: heard.triggered } : {}) });
127
- const checkOf = async (turn, revised) => {
128
- try {
129
- return await deps.check({ runId: input.runId, agentId: input.agentId, turn, outcome: input.outcome, refuse, ...(revised ? { revised } : {}) });
130
- }
131
- catch {
132
- return undefined;
133
- }
134
- };
135
- // The look-back: asked of every run but a workflow-only one or one its owner said nothing on, whose compile turn is
136
- // asked alone, from no answer of its own: nothing the look-back kept goes with its draft.
137
- let turn = null;
138
- let checked = { state: "dropped", reason: "empty" };
139
- if (alone)
140
- heard = await deps.asked(input.runId);
141
- else {
142
- const { active } = await deps.promptFacts(input.runId, input.agentId);
143
- 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 } });
144
- firstAsk(end.kept);
145
- // closeLearning: how the turn ended.
146
- if ("timedOut" in end)
147
- return close([droppedRow(null, "reflect_timeout", said.stopped(STOPPED.timedOut))]);
148
- if ("failed" in end)
149
- return close([CAP_REFUSAL.test(end.failed.message) ? droppedRow(null, "no_budget", said.noBudget) : droppedRow(null, "reflect_failed", said.stopped(STOPPED.failed))]);
150
- heard = await deps.asked(input.runId);
151
- turn = turnOf(end.answer);
152
- const first = await checkOf(turn);
153
- if (!first)
154
- return close([droppedRow(turn, "propose_failed", said.stopped(STOPPED.notChecked))]);
155
- checked = first;
156
- }
157
- // An answer the parser refused is asked for again once, in the same turn, saying why in the parser's own words: the
158
- // second answer is checked as the first was, and refused again it is dropped as before. Never a third time, and only
159
- // within the turn's own deadline; a re-ask that times out or fails leaves the first answer's drop.
160
- if (checked.state === "dropped" && checked.reason === "unparsed") {
161
- const left = deadlineMs - (now() - started);
162
- const again = left > 0 ? await input.ask(reflectAgain(checked.detail), { ...ask, deadlineMs: left, again: true }) : undefined;
163
- if (again && "ended" in again) {
164
- turn = turnOf(again.answer);
165
- const second = await checkOf(turn);
166
- if (!second)
167
- return close([droppedRow(turn, "propose_failed", said.stopped(STOPPED.notChecked))]);
168
- checked = second;
169
- }
170
- }
171
- else {
172
- // A workflow draft the checks left out because its dry run failed is asked for again once, in the same turn and
173
- // within its deadline, saying which draft and why: the second answer stands only when the checks keep it and no
174
- // draft in it still fails its dry run, else the first answer's, that draft left out, stands (a repair that failed
175
- // never costs a note the first answer kept). Never a third time (a turn asks again once, for either reason).
176
- const dry = dryRunFailure(checked);
177
- const left = deadlineMs - (now() - started);
178
- const again = dry && left > 0 ? await input.ask(reflectDryRun(dry.path, dry.detail), { ...ask, deadlineMs: left, again: true }) : undefined;
179
- if (again && "ended" in again) {
180
- const second = turnOf(again.answer);
181
- const kept = await checkOf(second);
182
- if (kept?.state === "proposed" && !dryRunFailure(kept)) {
183
- turn = second;
184
- checked = kept;
185
- }
186
- }
187
- }
188
- // The compile turn (learn/compile.ts): the job as a learned workflow, or a revision of one, checked as a draft is,
189
- // its refusals sent back up to COMPILE_RETRIES times. A draft that passed goes in the proposal beside the notes; one
190
- // that never did is never silent: its errors go with the learning, and its problems to its end.
191
- 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;
192
85
  let compiled;
86
+ // A draft the checks refused: their problems by rule (a draft never answered refused nothing).
87
+ let draftProblems = [];
193
88
  if (compile) {
194
- // A compile turn asked alone (no look-back) is the round's first ask: it keeps the job's figures
195
- // with its prompt as the look-back's does, and a turn an earlier claim asked first answers with that ask's (`kept`),
196
- // so a restart mid compile never moves what the earlier attempt spent compiling into the job's. Each attempt keeps
197
- // the figures as they stand once the attempts before it answered (an earlier claim's, when it asked first).
198
- // A revision of main's text keeps the commit of main it was read at (`compile_base`) with each attempt the same way:
199
- // an attempt an earlier claim asked answers with that claim's commit, never the one this claim read (main may have
200
- // moved since: another run's change to the workflow), and the attempts after it keep that one too. It is the revised
201
- // workflow's conflict base in every check of the turn, so an answer written over an older main never overwrites a
202
- // 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.
203
92
  let revised = compile.mode === "revise" && compile.base ? { workflow: compile.workflow, base: compile.base } : undefined;
204
- /** An attempt the cap refused, or whose spend took the run past it: the learning is dropped as the look-back's is. */
205
93
  let capped = false;
206
- // Where the compile turn begins (the run's spend now), kept with each attempt as the job's figures are: a later claim
207
- // answers with the earlier one's, so what it spent compiling stays the compile's.
208
- compileFrom = alone ? "alone" : { usd: (await deps.facts(input.runId)).costUsd };
209
94
  const result = await compileTurn({
210
95
  ask: async (prompt, attempt, left) => {
211
- const from = compileFrom !== "alone" && compileFrom !== undefined ? { compile_from_usd: compileFrom.usd } : {};
212
- const keep = { ...(alone ? { answered_at: answeredAt, job_cost_usd: job } : {}), ...from, ...(revised ? { compile_base: revised.base } : {}) };
213
- const said_ = await input.ask(prompt, { ...ask, deadlineMs: left, step: `compile-${attempt}`, ...(Object.keys(keep).length ? { keep } : {}) });
214
- 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))
215
100
  capped = true;
216
- if (alone)
217
- firstAsk(said_.kept);
218
- const keptFrom = said_.kept?.compile_from_usd;
219
- if (!alone && (typeof keptFrom === "number" || keptFrom === null))
220
- compileFrom = { usd: keptFrom };
221
- const base = said_.kept?.compile_base;
101
+ firstAsk(answer.kept);
102
+ const base = answer.kept?.compile_base;
222
103
  if (revised && typeof base === "string")
223
104
  revised = { ...revised, base };
224
- return "ended" in said_ ? { answer: said_.answer } : undefined;
105
+ return "ended" in answer ? { answer: answer.answer } : undefined;
225
106
  },
226
107
  compile,
227
108
  refuse,
228
- // The look-back's own answer, when the checks kept it: its notes and skills go on with the draft.
229
- answer: checked.state === "proposed" && turn ? turn.answer : "",
230
- check: (answer) => checkOf(turnOf(answer), revised),
109
+ check: (one) => deps.checkDraft({ runId: input.runId, agentId: input.agentId, draft: one, refuse, ...(revised ? { revised } : {}) }),
231
110
  ...(deps.compileDeadlineMs !== undefined ? { deadlineMs: deps.compileDeadlineMs } : {}),
232
111
  now,
233
112
  });
234
- // closeLearning, as the look-back's turn when the cap has no room: the learning is dropped no_budget, never graded
235
- // on a cap it has passed or cannot fit.
236
- if (capped)
237
- return close([droppedRow(turn, "no_budget", said.noBudget)]);
238
- if (result.kept?.checked.state === "proposed") {
239
- const { workflow, from_notes } = result.record;
240
- turn = turnOf(result.kept.answer);
241
- 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;
242
119
  compiled = { record: result.record };
243
120
  }
244
121
  else if (result.record) {
245
- const named = result.record.workflow !== UNNAMED ? { path: `workflows/${result.record.workflow}/workflow.json` } : {};
246
- const problems = result.draftErrors.map((detail) => ({ rule: "workflow", ...named, detail }));
247
- // Beside what the look-back kept, as what the checks left out; in place of a look-back that kept nothing, as the
248
- // checks' drop; beside the checks' own drop, with it. A look-back whose answer could not be read keeps its reason,
249
- // the draft's errors beside it.
250
- checked = checked.state === "proposed" ? { ...checked, left: [...(checked.left ?? []), ...problems] }
251
- : checked.reason === "empty" ? { state: "dropped", reason: "checker", problems }
252
- : checked.reason === "checker" ? { ...checked, problems: [...checked.problems, ...problems] }
253
- : checked;
254
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 }));
255
127
  // Compile once: its failed drafts are the compile's attempt, on record before the learning closes. The draft's
256
128
  // name and errors are the model's words: cut of `refuse` as the proposal row is (cutRow) before they are saved
257
129
  // on the promotion record or logged, whose reads cut only the server's secrets, never this run's.
@@ -261,24 +133,39 @@ export function createReflectHook(deps) {
261
133
  }
262
134
  }
263
135
  const compiledFields = compiled ? { compiled: compiled.record, ...(compiled.draft_errors?.length ? { draft_errors: compiled.draft_errors } : {}) } : {};
264
- if (checked.state === "dropped") {
265
- const detail = checked.reason === "empty" ? (alone ? notAsked : said.empty) : checked.reason === "unparsed" ? said.unparsed : said.checker(checked.problems);
266
- 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 }));
267
152
  }
268
- // The learning run's cap: what the run's cap leaves now its look-back is spent, read after the checks. A spend under
269
- // the cap that is unknown (or a read that fails, never no cap) queues no grade: none could be kept within the cap, and
270
- // the words say the spend is unknown, never that the cap has no room (learning-end.ts capSpendOf's rule).
271
- // Asked here, at the end: a key a message handed during the run counts as one the run brought.
272
- const gradeOff = deps.gradeOff?.();
273
- if (gradeOff)
274
- return close([droppedRow(turn, "grade_failed", gradeOff, compiledFields)]);
275
- const capUsd = deps.gradeCap ? await deps.gradeCap(input.runId).catch(() => null) : undefined;
276
- if (capUsd === null)
277
- return close([droppedRow(turn, "unknown_cost", said.gradeUnknownSpend)]);
278
- return close([
279
- { table: "learning_proposals", row: { turn, proposal: { ...checked, ...compiledFields, detail: said.proposed(checked.notes, checked.skills, checked.workflows) } } },
280
- { table: "learning_run", row: { id: deps.newRunId(), ...(deps.gradeModel ? { model: deps.gradeModel } : {}), ...(capUsd !== undefined ? { capUsd } : {}) } },
281
- ]);
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));
282
163
  };
283
164
  return async (input) => roundOf(await hook(input), round);
284
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
+ }