@parcha/agentrun-serve 0.1.0-beta.33 → 0.1.0-beta.34

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 (381) hide show
  1. package/CHANGELOG.md +233 -0
  2. package/README.md +266 -201
  3. package/dist/adapters.d.ts +0 -4
  4. package/dist/adapters.js +11 -15
  5. package/dist/agent-rows.d.ts +98 -0
  6. package/dist/agent-rows.js +1 -0
  7. package/dist/agents-store.d.ts +6 -2
  8. package/dist/agents-store.js +60 -25
  9. package/dist/agents.d.ts +9 -125
  10. package/dist/agents.js +15 -305
  11. package/dist/answer-json.d.ts +23 -0
  12. package/dist/answer-json.js +58 -0
  13. package/dist/api-only.d.ts +1 -1
  14. package/dist/api-only.js +2 -2
  15. package/dist/bash-read-only.d.ts +22 -0
  16. package/dist/bash-read-only.js +701 -0
  17. package/dist/box-settings.d.ts +3 -23
  18. package/dist/box-settings.js +7 -51
  19. package/dist/boxes.d.ts +17 -0
  20. package/dist/boxes.js +27 -20
  21. package/dist/browser-commands.d.ts +5 -2
  22. package/dist/browser-commands.js +4 -4
  23. package/dist/browser-computer.d.ts +5 -0
  24. package/dist/browser-computer.js +21 -0
  25. package/dist/browser-cost.d.ts +9 -6
  26. package/dist/browser-cost.js +92 -50
  27. package/dist/browser-live.d.ts +7 -20
  28. package/dist/browser-live.js +15 -93
  29. package/dist/browser-person.d.ts +18 -51
  30. package/dist/browser-person.js +94 -277
  31. package/dist/cap.d.ts +8 -5
  32. package/dist/cap.js +12 -5
  33. package/dist/cli.js +39 -13
  34. package/dist/closed-pipes.d.ts +2 -0
  35. package/dist/closed-pipes.js +13 -0
  36. package/dist/computer-commands.d.ts +2 -0
  37. package/dist/computer-commands.js +2 -0
  38. package/dist/computer-keys.js +5 -2
  39. package/dist/computer-setting.d.ts +20 -4
  40. package/dist/computer-setting.js +25 -5
  41. package/dist/computers.d.ts +30 -2
  42. package/dist/computers.js +70 -11
  43. package/dist/confine.d.ts +120 -0
  44. package/dist/confine.js +436 -0
  45. package/dist/console/assets/index-RPHGlXmM.js +540 -0
  46. package/dist/console/assets/index-opTN5Naq.css +1 -0
  47. package/dist/console/index.html +2 -2
  48. package/dist/decision-rows.d.ts +1 -1
  49. package/dist/decision-rows.js +1 -1
  50. package/dist/docker-daemon.d.ts +31 -0
  51. package/dist/docker-daemon.js +56 -0
  52. package/dist/effort-guidance.js +4 -3
  53. package/dist/environment.d.ts +0 -3
  54. package/dist/environment.js +3 -5
  55. package/dist/errors.d.ts +0 -2
  56. package/dist/errors.js +6 -9
  57. package/dist/filed-answer.d.ts +32 -0
  58. package/dist/filed-answer.js +160 -0
  59. package/dist/files.d.ts +1 -3
  60. package/dist/files.js +2 -4
  61. package/dist/github-connection.d.ts +1 -1
  62. package/dist/graph-view.d.ts +3 -5
  63. package/dist/graph-view.js +4 -14
  64. package/dist/handed-values.d.ts +9 -0
  65. package/dist/handed-values.js +26 -0
  66. package/dist/harness-install.d.ts +1 -7
  67. package/dist/harness-install.js +1 -18
  68. package/dist/harness-opencode/adapter.d.ts +7 -0
  69. package/dist/harness-opencode/adapter.js +23 -6
  70. package/dist/harness-opencode/index.d.ts +1 -1
  71. package/dist/harness-opencode/index.js +1 -1
  72. package/dist/harness-opencode/verify.d.ts +17 -0
  73. package/dist/harness-opencode/verify.js +30 -10
  74. package/dist/harness-table.js +6 -1
  75. package/dist/heard.js +1 -1
  76. package/dist/held-fd.d.ts +5 -0
  77. package/dist/held-fd.js +14 -0
  78. package/dist/host-calls.d.ts +20 -0
  79. package/dist/host-calls.js +12 -0
  80. package/dist/host-tools.d.ts +4 -21
  81. package/dist/host-tools.js +4 -61
  82. package/dist/host.d.ts +2 -4
  83. package/dist/host.js +2 -7
  84. package/dist/import-summary.d.ts +1 -5
  85. package/dist/import-summary.js +0 -21
  86. package/dist/import.d.ts +6 -6
  87. package/dist/import.js +14 -15
  88. package/dist/key-refusal.d.ts +5 -1
  89. package/dist/key-refusal.js +19 -4
  90. package/dist/learn/check.d.ts +6 -2
  91. package/dist/learn/check.js +83 -29
  92. package/dist/learn/folder.d.ts +5 -1
  93. package/dist/learn/folder.js +10 -3
  94. package/dist/learn/grade.d.ts +14 -0
  95. package/dist/learn/grade.js +68 -12
  96. package/dist/learn/index.d.ts +4 -4
  97. package/dist/learn/index.js +4 -4
  98. package/dist/learn/reflect.d.ts +31 -2
  99. package/dist/learn/reflect.js +79 -4
  100. package/dist/learn/relevance.d.ts +24 -0
  101. package/dist/learn/relevance.js +91 -0
  102. package/dist/learn/secrets.d.ts +7 -1
  103. package/dist/learn/secrets.js +86 -19
  104. package/dist/learn/skills.d.ts +112 -14
  105. package/dist/learn/skills.js +556 -52
  106. package/dist/learned-workflow.d.ts +32 -1
  107. package/dist/learned-workflow.js +100 -9
  108. package/dist/learning-routes.d.ts +8 -4
  109. package/dist/learning-routes.js +23 -9
  110. package/dist/learning-run.d.ts +9 -26
  111. package/dist/learning-run.js +22 -32
  112. package/dist/learning-words.d.ts +12 -5
  113. package/dist/learning-words.js +18 -9
  114. package/dist/learning.d.ts +93 -77
  115. package/dist/learning.js +176 -304
  116. package/dist/live/stream.js +1 -1
  117. package/dist/live/translate.js +12 -2
  118. package/dist/local-models.d.ts +11 -5
  119. package/dist/local-models.js +13 -8
  120. package/dist/mcp-gateway.d.ts +11 -49
  121. package/dist/mcp-gateway.js +44 -135
  122. package/dist/mcp-oauth.d.ts +10 -14
  123. package/dist/mcp-oauth.js +14 -32
  124. package/dist/mcp-probe.d.ts +0 -7
  125. package/dist/mcp-probe.js +4 -10
  126. package/dist/mcp-servers-store.d.ts +6 -6
  127. package/dist/mcp-servers-store.js +12 -14
  128. package/dist/mcp-store.js +3 -4
  129. package/dist/mcp.js +4 -3
  130. package/dist/model.d.ts +6 -7
  131. package/dist/model.js +43 -27
  132. package/dist/monid.d.ts +0 -2
  133. package/dist/monid.js +3 -6
  134. package/dist/openapi.d.ts +8 -1
  135. package/dist/openapi.js +121 -47
  136. package/dist/page.d.ts +1 -3
  137. package/dist/page.js +6 -18
  138. package/dist/paid-calls.d.ts +21 -27
  139. package/dist/paid-calls.js +25 -60
  140. package/dist/propose.js +83 -39
  141. package/dist/push-handler.d.ts +1 -1
  142. package/dist/reflect-range.d.ts +25 -0
  143. package/dist/reflect-range.js +30 -0
  144. package/dist/refusal-words.d.ts +5 -1
  145. package/dist/refusal-words.js +5 -1
  146. package/dist/rows.d.ts +1 -1
  147. package/dist/run-cut.d.ts +0 -6
  148. package/dist/run-cut.js +0 -6
  149. package/dist/run-db.d.ts +2 -11
  150. package/dist/run-db.js +2 -37
  151. package/dist/run-launch.d.ts +19 -2
  152. package/dist/run-launch.js +13 -5
  153. package/dist/run-learning-served.d.ts +10 -4
  154. package/dist/run-learning-served.js +23 -13
  155. package/dist/run-reads-over-store.d.ts +1 -1
  156. package/dist/run-reads-over-store.js +15 -6
  157. package/dist/run-secrets.d.ts +1 -1
  158. package/dist/run-timeline-served.d.ts +5 -1
  159. package/dist/run-timeline-served.js +15 -4
  160. package/dist/run-view.d.ts +7 -8
  161. package/dist/run-view.js +26 -7
  162. package/dist/runnable.d.ts +18 -10
  163. package/dist/runnable.js +21 -10
  164. package/dist/runner/adapter-loop.d.ts +6 -1
  165. package/dist/runner/adapter-loop.js +423 -53
  166. package/dist/runner/adapter-run-spec.d.ts +21 -8
  167. package/dist/runner/adapter-run-spec.js +90 -32
  168. package/dist/runner/adapters/index.js +11 -1
  169. package/dist/runner/adapters/pi.d.ts +12 -2
  170. package/dist/runner/adapters/pi.js +16 -5
  171. package/dist/runner/beside.d.ts +30 -3
  172. package/dist/runner/beside.js +36 -5
  173. package/dist/runner/claim-secrets.d.ts +10 -1
  174. package/dist/runner/claim-secrets.js +48 -14
  175. package/dist/runner/cli-run-spec.d.ts +2 -0
  176. package/dist/runner/cli-run-spec.js +45 -16
  177. package/dist/runner/commands.js +1 -1
  178. package/dist/runner/computer-journal.d.ts +15 -2
  179. package/dist/runner/computer-journal.js +41 -5
  180. package/dist/runner/conformance/harness.js +118 -8
  181. package/dist/runner/conformance/subject.d.ts +4 -1
  182. package/dist/runner/dispatch.d.ts +8 -1
  183. package/dist/runner/dispatch.js +81 -7
  184. package/dist/runner/end-of-run.d.ts +8 -2
  185. package/dist/runner/end-of-run.js +10 -4
  186. package/dist/runner/github-app.js +1 -1
  187. package/dist/runner/harness-loops.d.ts +9 -1
  188. package/dist/runner/harness-loops.js +15 -2
  189. package/dist/runner/journal-kinds.d.ts +24 -6
  190. package/dist/runner/journal-kinds.js +20 -5
  191. package/dist/runner/journal-reader.d.ts +1 -1
  192. package/dist/runner/journal-reader.js +2 -2
  193. package/dist/runner/learning-ask.d.ts +4 -0
  194. package/dist/runner/learning-ask.js +15 -1
  195. package/dist/runner/learning-end.d.ts +18 -3
  196. package/dist/runner/learning-end.js +113 -11
  197. package/dist/runner/learning-loop.d.ts +5 -0
  198. package/dist/runner/learning-loop.js +26 -6
  199. package/dist/runner/main.d.ts +56 -9
  200. package/dist/runner/main.js +127 -18
  201. package/dist/runner/mcp-claim.js +1 -2
  202. package/dist/runner/mcp-run.d.ts +2 -0
  203. package/dist/runner/mcp-run.js +15 -5
  204. package/dist/runner/memory-claim.d.ts +9 -0
  205. package/dist/runner/memory-claim.js +25 -0
  206. package/dist/runner/metrics.d.ts +33 -1
  207. package/dist/runner/metrics.js +85 -1
  208. package/dist/runner/model-route.d.ts +13 -0
  209. package/dist/runner/model-route.js +133 -9
  210. package/dist/runner/reflect-hook.d.ts +12 -1
  211. package/dist/runner/reflect-hook.js +33 -3
  212. package/dist/runner/runner.d.ts +20 -0
  213. package/dist/runner/runner.js +56 -10
  214. package/dist/runner/store-port.d.ts +1 -1
  215. package/dist/runner/store-secrets.d.ts +3 -3
  216. package/dist/runner/store-secrets.js +8 -12
  217. package/dist/runner/text-tools.d.ts +44 -0
  218. package/dist/runner/text-tools.js +237 -0
  219. package/dist/runner/workflow-loop.d.ts +6 -0
  220. package/dist/runner/workflow-loop.js +6 -4
  221. package/dist/runner/workflow-spec.d.ts +18 -0
  222. package/dist/runner/workflow-spec.js +63 -17
  223. package/dist/runs-over-store.d.ts +7 -2
  224. package/dist/runs-over-store.js +60 -14
  225. package/dist/seal-key.js +5 -43
  226. package/dist/sealed-settings-store.d.ts +19 -4
  227. package/dist/sealed-settings-store.js +9 -9
  228. package/dist/sealing.d.ts +4 -0
  229. package/dist/sealing.js +20 -0
  230. package/dist/secret-cut.d.ts +1 -1
  231. package/dist/secret-cut.js +1 -1
  232. package/dist/secret-name.d.ts +9 -0
  233. package/dist/secret-name.js +15 -0
  234. package/dist/secret-writes.d.ts +0 -1
  235. package/dist/secret-writes.js +0 -1
  236. package/dist/server-fs.d.ts +4 -0
  237. package/dist/server-fs.js +8 -2
  238. package/dist/server-lock.d.ts +0 -24
  239. package/dist/server-lock.js +1 -80
  240. package/dist/server-secrets-store.d.ts +4 -4
  241. package/dist/server-secrets-store.js +9 -31
  242. package/dist/server-secrets.d.ts +4 -9
  243. package/dist/server-secrets.js +12 -36
  244. package/dist/server.d.ts +16 -38
  245. package/dist/server.js +145 -106
  246. package/dist/sign-ins-store.d.ts +34 -36
  247. package/dist/sign-ins-store.js +27 -38
  248. package/dist/sign-ins.d.ts +2 -119
  249. package/dist/sign-ins.js +5 -311
  250. package/dist/standard-webhooks.d.ts +1 -1
  251. package/dist/standard-webhooks.js +1 -1
  252. package/dist/stats.d.ts +3 -2
  253. package/dist/stats.js +1 -1
  254. package/dist/store/agent-body.d.ts +8 -2
  255. package/dist/store/agent-body.js +16 -4
  256. package/dist/store/index.d.ts +2 -2
  257. package/dist/store/index.js +2 -2
  258. package/dist/store/open.d.ts +6 -3
  259. package/dist/store/open.js +2 -2
  260. package/dist/store/postgres/connection.d.ts +18 -2
  261. package/dist/store/postgres/connection.js +156 -32
  262. package/dist/store/postgres/migrate.d.ts +4 -0
  263. package/dist/store/postgres/migrate.js +9 -2
  264. package/dist/store/postgres/migrations/0003_commands_one_answer.check.sql +19 -0
  265. package/dist/store/postgres/migrations/0003_commands_one_answer.sql +13 -0
  266. package/dist/store/postgres/store.d.ts +9 -2
  267. package/dist/store/postgres/store.js +9 -0
  268. package/dist/store/schema.js +1 -1
  269. package/dist/store/seal.js +2 -2
  270. package/dist/store/sqlite/connection.js +49 -5
  271. package/dist/store/sqlite/run-patch.d.ts +3 -0
  272. package/dist/store/sqlite/run-patch.js +3 -0
  273. package/dist/store/sqlite/store.js +41 -12
  274. package/dist/store/store.d.ts +35 -6
  275. package/dist/store/store.js +12 -2
  276. package/dist/store-reads/cap.d.ts +1 -1
  277. package/dist/store-reads/cap.js +22 -4
  278. package/dist/store-reads/journal.d.ts +9 -6
  279. package/dist/store-reads/journal.js +93 -17
  280. package/dist/store-reads/learning.d.ts +26 -8
  281. package/dist/store-reads/learning.js +12 -4
  282. package/dist/store-reads/workflow.d.ts +1 -1
  283. package/dist/store-reads/workflow.js +1 -1
  284. package/dist/store-view.d.ts +5 -1
  285. package/dist/store-view.js +53 -18
  286. package/dist/template-eval.d.ts +60 -4
  287. package/dist/template-eval.js +120 -54
  288. package/dist/templates.d.ts +24 -10
  289. package/dist/templates.js +69 -38
  290. package/dist/timeline/group.d.ts +33 -0
  291. package/dist/timeline/group.js +77 -0
  292. package/dist/timeline/index.d.ts +7 -0
  293. package/dist/timeline/index.js +7 -0
  294. package/dist/timeline/labels.d.ts +79 -0
  295. package/dist/timeline/labels.js +150 -0
  296. package/dist/timeline/origin.d.ts +56 -0
  297. package/dist/timeline/origin.js +92 -0
  298. package/dist/timeline/render-text.d.ts +11 -0
  299. package/dist/timeline/render-text.js +62 -0
  300. package/dist/timeline/timeline.d.ts +274 -0
  301. package/dist/timeline/timeline.js +1308 -0
  302. package/dist/timeline/util.d.ts +17 -0
  303. package/dist/timeline/util.js +49 -0
  304. package/dist/tool-gate.d.ts +41 -216
  305. package/dist/tool-gate.js +79 -540
  306. package/dist/tool-keys-store.d.ts +11 -12
  307. package/dist/tool-keys-store.js +24 -31
  308. package/dist/tool-keys.d.ts +5 -101
  309. package/dist/tool-keys.js +8 -267
  310. package/dist/tool-plane.d.ts +14 -42
  311. package/dist/tool-plane.js +23 -81
  312. package/dist/tool-settings-store.d.ts +1 -2
  313. package/dist/tool-settings-store.js +5 -8
  314. package/dist/tools.d.ts +30 -18
  315. package/dist/tools.js +97 -107
  316. package/dist/trigger-adapter.d.ts +2 -2
  317. package/dist/trigger-targets.d.ts +13 -1
  318. package/dist/trigger-targets.js +31 -1
  319. package/dist/triggers.d.ts +2 -2
  320. package/dist/triggers.js +3 -3
  321. package/dist/turn-end.d.ts +1 -1
  322. package/dist/turn-end.js +1 -1
  323. package/dist/turns.d.ts +0 -6
  324. package/dist/turns.js +0 -19
  325. package/dist/uploads.js +1 -1
  326. package/dist/usage.d.ts +0 -2
  327. package/dist/usage.js +0 -2
  328. package/dist/validate.d.ts +2 -0
  329. package/dist/validate.js +23 -14
  330. package/dist/webhooks.d.ts +102 -76
  331. package/dist/webhooks.js +266 -233
  332. package/dist/workflow-run.d.ts +114 -0
  333. package/dist/workflow-run.js +144 -0
  334. package/dist/workspace/beneath.d.ts +19 -0
  335. package/dist/workspace/beneath.js +106 -0
  336. package/dist/workspace/computer-deps.d.ts +24 -6
  337. package/dist/workspace/computer-deps.js +108 -20
  338. package/dist/workspace/computer-for-claim.d.ts +12 -11
  339. package/dist/workspace/computer-for-claim.js +76 -15
  340. package/dist/workspace/computer-hold.d.ts +6 -1
  341. package/dist/workspace/computer-hold.js +6 -1
  342. package/dist/workspace/computer-janitor.d.ts +47 -0
  343. package/dist/workspace/computer-janitor.js +182 -0
  344. package/dist/workspace/grep-worker.d.ts +5 -0
  345. package/dist/workspace/grep-worker.js +9 -6
  346. package/dist/workspace/index.d.ts +3 -2
  347. package/dist/workspace/index.js +3 -3
  348. package/dist/workspace/local.d.ts +11 -2
  349. package/dist/workspace/local.js +161 -43
  350. package/dist/workspace/resolve-computer.d.ts +50 -10
  351. package/dist/workspace/resolve-computer.js +46 -11
  352. package/dist/workspace/run-tools.d.ts +13 -31
  353. package/dist/workspace/run-tools.js +10 -27
  354. package/dist/workspace-paths.d.ts +0 -17
  355. package/dist/workspace-paths.js +1 -41
  356. package/dist/workspace.d.ts +0 -14
  357. package/dist/workspace.js +3 -96
  358. package/package.json +13 -13
  359. package/templates/README.md +9 -3
  360. package/templates/market-monitor/agent.md +2 -2
  361. package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
  362. package/dist/bound-files.d.ts +0 -4
  363. package/dist/bound-files.js +0 -164
  364. package/dist/coding-tools.d.ts +0 -47
  365. package/dist/coding-tools.js +0 -189
  366. package/dist/computer-adb.d.ts +0 -12
  367. package/dist/computer-adb.js +0 -14
  368. package/dist/console/assets/index-Dn0ngwG9.js +0 -535
  369. package/dist/console/assets/index-DvW6Aled.css +0 -1
  370. package/dist/grade.d.ts +0 -63
  371. package/dist/grade.js +0 -150
  372. package/dist/mcp-servers.d.ts +0 -269
  373. package/dist/mcp-servers.js +0 -456
  374. package/dist/packs.d.ts +0 -71
  375. package/dist/packs.js +0 -180
  376. package/dist/sealed-settings.d.ts +0 -60
  377. package/dist/sealed-settings.js +0 -171
  378. package/dist/sessions.d.ts +0 -6
  379. package/dist/sessions.js +0 -109
  380. package/dist/workspace/computer-serves.d.ts +0 -12
  381. package/dist/workspace/computer-serves.js +0 -15
@@ -25,6 +25,13 @@ export class LaunchUnreadable extends Error {
25
25
  super(`run ${runId}: its launch record could not be read as a launch (it names no place, or is not an object).`);
26
26
  }
27
27
  }
28
+ /** The first wait before a buffered row whose delivery threw is tried again, and the most any later wait (doubling)
29
+ * grows to. */
30
+ export const REPLAY_RETRY_FIRST_MS = 250;
31
+ export const REPLAY_RETRY_MAX_MS = 5_000;
32
+ /** How many times in all a buffered row's delivery is tried before the dispatch stops trying it on this claim: a row that
33
+ * fails for a lasting reason is said once and left untaken for the run's next claim, never tried for the whole run. */
34
+ export const REPLAY_TRIES = 5;
28
35
  /** The loop factory the runner holds for every run: `createDispatchLoop(deps)` picks each run's harness loop at its
29
36
  * claim. The inner loop gets the same LoopDeps the dispatch does, so the runner's fence, flush, live and end-of-run
30
37
  * channels reach it unchanged. */
@@ -62,16 +69,83 @@ function dispatchLoop(deps, loopDeps) {
62
69
  // buffered row and no buffered stop is lost between choosing the loop and replaying what was buffered.
63
70
  inner = factory(innerDeps);
64
71
  const end = inner.start(claim);
65
- // Replay the buffered commands through the inner loop in order, each awaited, then let its `delivered` resolve.
72
+ // Replay the buffered commands through the inner loop in order, each awaited, then let its `delivered` resolve. A
73
+ // command whose delivery throws is never thrown out of here, where nothing would catch it and the runner's process
74
+ // would go down. The runner already moved past its row (the dispatch said it holds it), so the dispatch tries it again
75
+ // itself while the inner loop runs, as the runner tries again a row whose delivery threw (a store that failed for a
76
+ // moment), up to REPLAY_TRIES times. Once the inner loop has ended, or the tries are spent, a row still not delivered
77
+ // stays untaken (the store's take is the inner loop's own commit), so the run's next claim delivers it again and
78
+ // nobody's words are lost.
66
79
  const buffered = early.splice(0);
67
- void (async () => {
80
+ void replay(claim.runId, inner, buffered, end);
81
+ // The buffered stop, not awaited here (start() returns the inner loop's end): an inner stop that throws is said in
82
+ // the log, never an unhandled rejection, which would end the runner process and every run it holds.
83
+ if (earlyStop) {
84
+ inner.stop(earlyStop).catch((error) => {
85
+ const text = error?.message ?? String(error);
86
+ console.warn(`[runner] run ${claim.runId}: its stop failed: ${loopDeps.redact ? loopDeps.redact(text) : text}`);
87
+ });
88
+ }
89
+ return end;
90
+ }
91
+ /** Hands each of `buffered` to `loop` once, in order, then lets the loop's `delivered` resolve; the ones whose delivery
92
+ * threw are tried again, in order, until each goes through, the loop has ended, or a row has been tried REPLAY_TRIES
93
+ * times (said once; it and the failed rows after it are left untaken for the run's next claim). Never rejects. */
94
+ async function replay(runId, loop, buffered, end, tries = REPLAY_TRIES) {
95
+ let ended = false;
96
+ const loopEnded = end.then(() => undefined, () => undefined).then(() => void (ended = true));
97
+ let lastError;
98
+ const delivers = async (command) => {
99
+ try {
100
+ await loop.command(command);
101
+ return true;
102
+ }
103
+ catch (error) {
104
+ lastError = error;
105
+ return false;
106
+ }
107
+ };
108
+ // What went wrong, cut of the values the run holds (LoopDeps.redact), as the log may not show them.
109
+ const why = () => {
110
+ const text = lastError?.message ?? String(lastError);
111
+ return loopDeps.redact ? loopDeps.redact(text) : text;
112
+ };
113
+ const failed = [];
114
+ try {
115
+ // Every row is tried once before the inner loop's `delivered` resolves, a later one even when an earlier one threw:
116
+ // a loop that waits on `delivered` (a run reopened by a follow-up) never goes on with a row still held here.
68
117
  for (const command of buffered)
69
- await inner.command(command);
118
+ if (!(await delivers(command)))
119
+ failed.push(command);
120
+ }
121
+ finally {
70
122
  replayed();
71
- })();
72
- if (earlyStop)
73
- void inner.stop(earlyStop);
74
- return end;
123
+ }
124
+ if (failed.length === 0)
125
+ return;
126
+ console.warn(`[runner] ${runId}: a ${failed[0].kind} row held before its loop was chosen was not delivered (${why()}); it is tried again while the loop runs, up to ${tries} times in all`);
127
+ // How many times the first row still to deliver has been tried: once, in the pass above.
128
+ let tried = 1;
129
+ for (let wait = REPLAY_RETRY_FIRST_MS; failed.length > 0 && !ended; wait = Math.min(wait * 2, REPLAY_RETRY_MAX_MS)) {
130
+ if (tried >= tries) {
131
+ console.warn(`[runner] ${runId}: a ${failed[0].kind} row held before its loop was chosen was tried ${tried} times and not delivered (${why()}); it and the ${failed.length - 1} after it are left untaken, for the run's next claim`);
132
+ return;
133
+ }
134
+ let timer;
135
+ await Promise.race([loopEnded, new Promise((resolve) => (timer = setTimeout(resolve, wait)))]);
136
+ clearTimeout(timer);
137
+ if (ended)
138
+ break;
139
+ // The first row again; once it goes through, the rows after it at once, in order, until one throws.
140
+ while (failed.length > 0 && !ended) {
141
+ tried += 1;
142
+ if (!(await delivers(failed[0])))
143
+ break;
144
+ failed.shift();
145
+ // Every row left was tried once in the first pass.
146
+ tried = 1;
147
+ }
148
+ }
75
149
  }
76
150
  return {
77
151
  start(claim) {
@@ -10,12 +10,18 @@ export declare const reflectRequestId: (runId: string, again?: boolean) => strin
10
10
  * is answered from its submission, never asked of the model twice. */
11
11
  export declare const REFLECT_AGAIN = ":again";
12
12
  /** Where the end-of-run turn began in a run's journal: the entry its input placed, from the input submission under the
13
- * turn's request id; undefined before it is placed, or for a run that asked none. Every entry from it on, until the
14
- * run's end, is the turn's (the server's labelled: after the entry before it). */
13
+ * turn's request id; else, on a CLI harness, the turn's prompt the CLI record wrote as it asked the turn (`entries`:
14
+ * adapter-loop.ts asking, an end-of-run-turn entry of the person's, written before the turn's submission, which only
15
+ * its answer writes). Undefined before either, or for a run that asked none. Every entry from it on, until the run's
16
+ * end, is the turn's (the server's labelled: after the entry before it). */
15
17
  export declare function reflectFrom(submissions: readonly {
16
18
  readonly type?: string;
17
19
  readonly requestId?: string;
18
20
  readonly entry?: unknown;
21
+ }[], entries?: readonly {
22
+ readonly kind?: string;
23
+ readonly id?: unknown;
24
+ readonly data?: unknown;
19
25
  }[]): number | undefined;
20
26
  /** The refusal of the end-of-run turn's write or edit under work/.learning/: the tool gate's one text (NOTE_ONLY_TEXT), so
21
27
  * the adapter loop's refusal and the gate's are the same words. */
@@ -3,6 +3,7 @@
3
3
  // answer's note blocks, never from a file it writes there. The words and the rule are the tool gate's own
4
4
  // (tool-gate.ts NOTE_ONLY_TEXT, underLearning), kept equal by a test.
5
5
  import { posix, resolve, sep, join } from "node:path";
6
+ import { KINDS } from "./journal-kinds.js";
6
7
  /** The prefix of the end-of-run turn's request id: the server's own (turns.ts REFLECT_REQUEST). */
7
8
  export const REFLECT_PREFIX = "learn-reflect-";
8
9
  /** The label a reader puts on the end-of-run turn's rows and messages: the server's (learning.ts LEARNING_LABEL). */
@@ -15,11 +16,16 @@ export const reflectRequestId = (runId, again = false) => `${REFLECT_PREFIX}${ru
15
16
  * is answered from its submission, never asked of the model twice. */
16
17
  export const REFLECT_AGAIN = ":again";
17
18
  /** Where the end-of-run turn began in a run's journal: the entry its input placed, from the input submission under the
18
- * turn's request id; undefined before it is placed, or for a run that asked none. Every entry from it on, until the
19
- * run's end, is the turn's (the server's labelled: after the entry before it). */
20
- export function reflectFrom(submissions) {
19
+ * turn's request id; else, on a CLI harness, the turn's prompt the CLI record wrote as it asked the turn (`entries`:
20
+ * adapter-loop.ts asking, an end-of-run-turn entry of the person's, written before the turn's submission, which only
21
+ * its answer writes). Undefined before either, or for a run that asked none. Every entry from it on, until the run's
22
+ * end, is the turn's (the server's labelled: after the entry before it). */
23
+ export function reflectFrom(submissions, entries = []) {
21
24
  const placed = submissions.filter((record) => record.type === "input" && typeof record.requestId === "string" && record.requestId.startsWith(REFLECT_PREFIX) && record.entry !== undefined);
22
- return placed.length ? Math.min(...placed.map((record) => Number(record.entry))) : undefined;
25
+ if (placed.length)
26
+ return Math.min(...placed.map((record) => Number(record.entry)));
27
+ const asked = entries.find((entry) => entry.kind === KINDS.endOfRunTurn && entry.data?.role === "person");
28
+ return asked === undefined ? undefined : Number(asked.id);
23
29
  }
24
30
  /** The refusal of the end-of-run turn's write or edit under work/.learning/: the tool gate's one text (NOTE_ONLY_TEXT), so
25
31
  * the adapter loop's refusal and the gate's are the same words. */
@@ -4,7 +4,7 @@
4
4
  // sealed-settings-store.ts writes them), opened here with the runner's seal key and held only in the bound mint's closure.
5
5
  // No App (never connected, disconnected, or rows that do not open under this key): no access, and such a server's calls
6
6
  // are refused in words (GITHUB_UNUSABLE.noAccess).
7
- import { unseal } from "@parcha/pi-durable-agent";
7
+ import { unseal } from "../sealing.js";
8
8
  import { GITHUB_API, GITHUB_FIELDS, GITHUB_SCOPE, githubTargetOf, mintInstallationToken } from "../github.js";
9
9
  import { isObject } from "../validate.js";
10
10
  const ADAPTER = "github";
@@ -1,10 +1,18 @@
1
1
  import type { RunLaunch } from "../run-launch.js";
2
2
  import { type AdapterSpecDeps } from "./adapter-run-spec.js";
3
+ import { type GradeRouteAsk } from "./learning-dispatch.js";
4
+ import type { GraderRoute } from "./learning-ask.js";
3
5
  import type { LoopFactory } from "./loop-contract.js";
4
6
  /** The dispatch's `resolve`, built from the runner's deps: it reads the run's revision (for a workflow revision) and
5
7
  * returns the loop the run runs on, or undefined when this runner does not run it (the dispatch fails it in words). */
6
8
  export declare function resolveRunnerLoop(deps: AdapterSpecDeps): (harness: string, launch: RunLaunch, runId: string) => Promise<LoopFactory | undefined>;
9
+ /** The route a grade asks for (gradeModelFor: the grade model the server named, else the agent's own), from the runner's
10
+ * one route (`routeOf`), through gradeRouteOf. A grade model the server named (--grade-model, else AGENT_MODEL_STRONG)
11
+ * is the strong tier's: it goes with that tier's settings, sent whole as AGENT_MODEL_STRONG is (never cut to its bare id
12
+ * by AGENT_MODEL_GATEWAY_IDS) and priced at AGENT_MODEL_STRONG_PRICE. The agent's own model goes as its runs' does, on
13
+ * the default tier. */
14
+ export declare function gradeRouteFor(grade: GradeRouteAsk, env: NodeJS.ProcessEnv, routeOf: AdapterSpecDeps["routeOf"]): Promise<GraderRoute>;
7
15
  /** The loop factory the runner holds for every run (its createLoop), built from the runner's deps over the one store: the
8
- * dispatch (dispatch.ts) reads each run's launch and asks the switch (resolveRunnerLoop) for its harness loop. The server
16
+ * dispatch (dispatch.ts) reads each run's launch and asks resolveRunnerLoop for its harness loop. The server
9
17
  * start and the test server pass this to startRunner. */
10
18
  export declare function dispatchRunner(deps: AdapterSpecDeps): LoopFactory;
@@ -38,9 +38,10 @@ export function resolveRunnerLoop(deps) {
38
38
  routeOf: async (grade) => {
39
39
  const base = deps.env ?? process.env;
40
40
  const env = withModelKeys(base, await savedModelKeys({ store: deps.store, sealKey: deps.sealKey, env: base }));
41
- return gradeRouteOf(gradeModelFor(grade), env, (model) => deps.routeOf(model, env, "default", PI_WIRES));
41
+ return gradeRouteFor(grade, env, deps.routeOf);
42
42
  },
43
43
  ...(deps.log ? { log: deps.log } : {}),
44
+ ...(deps.gradeBound ? { gradeBound: deps.gradeBound } : {}),
44
45
  });
45
46
  }
46
47
  // The harness's own entry decides which spec builds the run, by the entry's facts and never the harness's name: an entry
@@ -59,8 +60,20 @@ export function resolveRunnerLoop(deps) {
59
60
  return undefined;
60
61
  };
61
62
  }
63
+ /** The route a grade asks for (gradeModelFor: the grade model the server named, else the agent's own), from the runner's
64
+ * one route (`routeOf`), through gradeRouteOf. A grade model the server named (--grade-model, else AGENT_MODEL_STRONG)
65
+ * is the strong tier's: it goes with that tier's settings, sent whole as AGENT_MODEL_STRONG is (never cut to its bare id
66
+ * by AGENT_MODEL_GATEWAY_IDS) and priced at AGENT_MODEL_STRONG_PRICE. The agent's own model goes as its runs' does, on
67
+ * the default tier. */
68
+ export function gradeRouteFor(grade, env, routeOf) {
69
+ return gradeRouteOf(gradeModelFor(grade), env, (model) => (grade.model !== undefined && model === grade.model
70
+ // The runner's environment under the grader as its strong model: every key it holds read through it, a non-enumerable
71
+ // one (a launcher-held or Settings key) too, which a copy would drop.
72
+ ? routeOf(undefined, Object.create(env, { AGENT_MODEL_STRONG: { value: model, enumerable: true } }), "strong", PI_WIRES)
73
+ : routeOf(model, env, "default", PI_WIRES)));
74
+ }
62
75
  /** The loop factory the runner holds for every run (its createLoop), built from the runner's deps over the one store: the
63
- * dispatch (dispatch.ts) reads each run's launch and asks the switch (resolveRunnerLoop) for its harness loop. The server
76
+ * dispatch (dispatch.ts) reads each run's launch and asks resolveRunnerLoop for its harness loop. The server
64
77
  * start and the test server pass this to startRunner. */
65
78
  export function dispatchRunner(deps) {
66
79
  return createDispatchLoop({
@@ -1,12 +1,24 @@
1
- import type { MessageOrigin } from "@parcha/pi-durable-agent/timeline";
1
+ import type { MessageOrigin } from "../timeline/index.js";
2
+ /** The phase row before a message a CLI run sends again (adapter-loop.ts CliRecord.sending): its runner was lost after
3
+ * sending it, before the message was recorded, so the model may read it twice, and the turn it starts is paid like any
4
+ * other. */
5
+ export declare const MESSAGE_SENT_AGAIN = "Your message was sent again after the runner restarted: it may have reached the model before, and this turn is paid again.";
2
6
  export declare const KINDS: {
3
7
  /** A tool call about to run: written before it runs (and before the flush that precedes a side effect). */
4
8
  readonly callStart: "agentrun.call.start";
9
+ /** A call whose start was written at its park (CallStartData.parked) is about to be sent: committed and flushed before
10
+ * the send, once. Only a parked call with this mark may have been sent; one without it never left. */
11
+ readonly callSent: "agentrun.call.sent";
5
12
  /** How it ended: written after it ran, or by recovery for a start with no end (replay-safe reruns and ends done;
6
13
  * any other ends interrupted, and the model is told). */
7
14
  readonly callEnd: "agentrun.call.end";
8
15
  /** A message: the person's, a steer, or the model's. */
9
16
  readonly message: "agentrun.message";
17
+ /** A message a CLI run took mid-turn is about to be sent as a turn of its own (adapter-loop.ts sendAfterTurn): its
18
+ * command row's id, committed and flushed before the send. Its row is taken only after the send, so a runner lost in
19
+ * between leaves this mark with the row untaken: the next claim sends it again and says so (MESSAGE_SENT_AGAIN). Neither
20
+ * a call nor a message, so the reader passes over it. */
21
+ readonly messageSending: "agentrun.message.sending";
10
22
  /** A pi input the person did not type (a trigger's message): pi's user entry under the host's kind, its `data.origin`
11
23
  * the Timeline's attribution (pi-durable-agent's originMatcher reads a host's user entry's own origin). The model
12
24
  * reads it as any message. */
@@ -118,6 +130,9 @@ export interface CallStartData {
118
130
  readonly input: unknown;
119
131
  /** May run again after a crash (it changes nothing outside the run): the tool registry's own flag. */
120
132
  readonly replaySafe: boolean;
133
+ /** Written as the call reached its gate through a CLI's own MCP client, before any answer: nothing is sent until its
134
+ * agentrun.call.sent mark (KINDS.callSent). */
135
+ readonly parked?: true;
121
136
  /** When it was written (ms, the loop's clock): the Timeline dates the call's row by it. */
122
137
  readonly at?: number;
123
138
  }
@@ -166,12 +181,12 @@ export interface AskData {
166
181
  /** Its approval price (the spec's approvalPrice); null when it has none. */
167
182
  readonly price?: ApprovalPrice | null;
168
183
  }
169
- /** How an ask was answered: approved, denied, or withdrawn (a stop or cancel while the call waited). */
170
- export type AnswerDecision = "allow" | "deny" | "withdrawn";
184
+ /** How an ask was answered: approved, denied, withdrawn (a stop while the call waited) or canceled (a cancel). */
185
+ export type AnswerDecision = "allow" | "deny" | "withdrawn" | "canceled";
171
186
  export interface AnswerData {
172
187
  readonly callId: string;
173
188
  readonly approved: boolean;
174
- /** allow, deny, or withdrawn: main's resolved entry. Optional for an answer written before it. */
189
+ /** allow, deny, withdrawn or canceled: main's resolved entry. Optional for an answer written before it. */
175
190
  readonly decision?: AnswerDecision;
176
191
  /** When it was answered (ISO), from the loop's clock (or the runner's, for an end it writes itself). */
177
192
  readonly answeredAt?: string;
@@ -191,12 +206,13 @@ export interface AnswerData {
191
206
  /** The withdrawn answers a stopped end writes in `entries`: for every call still waiting (an ask with no answer), and
192
207
  * for every approved call that had not finished (no result, or one its abort wrote), which the stop overrides, keeping
193
208
  * the allow it was given (main's supersede: an answer the run had not acted on to the end is withdrawn by the stop). A
194
- * call that finished before the stop keeps its allow and its result. */
209
+ * call that finished before the stop keeps its allow and its result. `decision`: "canceled" for a cancel's end, whose
210
+ * calls read cancelled with their run; "withdrawn" for a stop's. */
195
211
  export declare function withdrawnAnswers(entries: readonly {
196
212
  readonly kind: string;
197
213
  readonly data?: unknown;
198
214
  readonly model?: unknown;
199
- }[], answeredAt: string): AnswerData[];
215
+ }[], answeredAt: string, decision?: "withdrawn" | "canceled"): AnswerData[];
200
216
  export interface EndData {
201
217
  readonly state: "ended" | "stopped" | "failed";
202
218
  readonly summary?: string;
@@ -215,3 +231,5 @@ export interface SessionKeyData {
215
231
  /** The conversation whose entries are this session's resume log. */
216
232
  readonly conversation: number;
217
233
  }
234
+ /** The session a workflow's model requests are booked under, in its run's journal. */
235
+ export declare const WORKFLOW_SESSION = "workflow";
@@ -1,12 +1,24 @@
1
1
  import { INTERRUPTED_TEXT } from "@parcha/agentrun-kernel/kernel";
2
+ /** The phase row before a message a CLI run sends again (adapter-loop.ts CliRecord.sending): its runner was lost after
3
+ * sending it, before the message was recorded, so the model may read it twice, and the turn it starts is paid like any
4
+ * other. */
5
+ export const MESSAGE_SENT_AGAIN = "Your message was sent again after the runner restarted: it may have reached the model before, and this turn is paid again.";
2
6
  export const KINDS = {
3
7
  /** A tool call about to run: written before it runs (and before the flush that precedes a side effect). */
4
8
  callStart: "agentrun.call.start",
9
+ /** A call whose start was written at its park (CallStartData.parked) is about to be sent: committed and flushed before
10
+ * the send, once. Only a parked call with this mark may have been sent; one without it never left. */
11
+ callSent: "agentrun.call.sent",
5
12
  /** How it ended: written after it ran, or by recovery for a start with no end (replay-safe reruns and ends done;
6
13
  * any other ends interrupted, and the model is told). */
7
14
  callEnd: "agentrun.call.end",
8
15
  /** A message: the person's, a steer, or the model's. */
9
16
  message: "agentrun.message",
17
+ /** A message a CLI run took mid-turn is about to be sent as a turn of its own (adapter-loop.ts sendAfterTurn): its
18
+ * command row's id, committed and flushed before the send. Its row is taken only after the send, so a runner lost in
19
+ * between leaves this mark with the row untaken: the next claim sends it again and says so (MESSAGE_SENT_AGAIN). Neither
20
+ * a call nor a message, so the reader passes over it. */
21
+ messageSending: "agentrun.message.sending",
10
22
  /** A pi input the person did not type (a trigger's message): pi's user entry under the host's kind, its `data.origin`
11
23
  * the Timeline's attribution (pi-durable-agent's originMatcher reads a host's user entry's own origin). The model
12
24
  * reads it as any message. */
@@ -90,8 +102,9 @@ function finished(entries, callId) {
90
102
  /** The withdrawn answers a stopped end writes in `entries`: for every call still waiting (an ask with no answer), and
91
103
  * for every approved call that had not finished (no result, or one its abort wrote), which the stop overrides, keeping
92
104
  * the allow it was given (main's supersede: an answer the run had not acted on to the end is withdrawn by the stop). A
93
- * call that finished before the stop keeps its allow and its result. */
94
- export function withdrawnAnswers(entries, answeredAt) {
105
+ * call that finished before the stop keeps its allow and its result. `decision`: "canceled" for a cancel's end, whose
106
+ * calls read cancelled with their run; "withdrawn" for a stop's. */
107
+ export function withdrawnAnswers(entries, answeredAt, decision = "withdrawn") {
95
108
  const answers = new Map();
96
109
  for (const entry of entries)
97
110
  if (entry.kind === KINDS.answer)
@@ -107,10 +120,12 @@ export function withdrawnAnswers(entries, answeredAt) {
107
120
  seen.add(callId);
108
121
  const given = answers.get(callId);
109
122
  if (!given)
110
- out.push({ callId, approved: false, decision: "withdrawn", answeredAt });
111
- else if (given.approved && given.decision !== "withdrawn" && !finished(entries, callId)) {
112
- out.push({ callId, approved: false, decision: "withdrawn", answeredAt, superseded: { decision: "allow", ...(given.answeredAt ? { answeredAt: given.answeredAt } : {}) } });
123
+ out.push({ callId, approved: false, decision, answeredAt });
124
+ else if (given.approved && given.decision !== "withdrawn" && given.decision !== "canceled" && !finished(entries, callId)) {
125
+ out.push({ callId, approved: false, decision, answeredAt, superseded: { decision: "allow", ...(given.answeredAt ? { answeredAt: given.answeredAt } : {}) } });
113
126
  }
114
127
  }
115
128
  return out;
116
129
  }
130
+ /** The session a workflow's model requests are booked under, in its run's journal. */
131
+ export const WORKFLOW_SESSION = "workflow";
@@ -1,5 +1,5 @@
1
1
  import type { Context } from "@earendil-works/chord";
2
- import { type MessageOrigin } from "@parcha/pi-durable-agent/timeline";
2
+ import { type MessageOrigin } from "../timeline/index.js";
3
3
  import type { EntryRecord, SubmissionRecord, TaskRecord } from "@earendil-works/pi-durable";
4
4
  import { type CallEndData, type MessageRole } from "./journal-kinds.js";
5
5
  import type { JournalReads } from "../store/store.js";
@@ -1,4 +1,4 @@
1
- import { originOf } from "@parcha/pi-durable-agent/timeline";
1
+ import { originOf } from "../timeline/index.js";
2
2
  import { reflectFrom } from "./end-of-run.js";
3
3
  import { KINDS } from "./journal-kinds.js";
4
4
  const PAGE = 500;
@@ -97,7 +97,7 @@ export function messagesOf(from) {
97
97
  const out = [];
98
98
  const records = asRecords(from);
99
99
  // The end-of-run turn's, from the entry its input placed on (the reader's tag, as the server's labelled() reads it).
100
- const from_ = reflectFrom(records.submissions ?? []);
100
+ const from_ = reflectFrom(records.submissions ?? [], records.entries);
101
101
  const turn = (entry) => (from_ !== undefined && entry >= from_ ? { learning: true } : {});
102
102
  for (const entry of recordOf(records.entries)) {
103
103
  const at = entryNumber(entry);
@@ -1,5 +1,9 @@
1
1
  import { type BeforeRequest, type Wire } from "@parcha/agentrun-kernel";
2
2
  import type { LearningLoopDeps } from "./learning-loop.js";
3
+ /** How long the ask waits, once the grade answered, for the proxy to book its request: the proxy books a request after its
4
+ * client has the whole answer (when the provider's stream ends, which can come later), so the spend is read once it is
5
+ * booked. One booked later than this reads unknown (null), never $0. */
6
+ export declare const GRADE_BOOKING_WAIT_MS = 10000;
3
7
  /** The session the grader's requests are booked under, in the learning run's journal. */
4
8
  export declare const GRADE_SESSION = "learning.grade";
5
9
  /** Where the grader's request goes: the route's wire (`api`, Anthropic's Messages or OpenAI's Responses) at `origin`, for
@@ -13,6 +13,10 @@ import { startModelProxy } from "@parcha/agentrun-kernel/host";
13
13
  import { sessionProvider } from "../port-models.js";
14
14
  import { policyFor } from "./adapters/pi.js";
15
15
  import { journalSessions } from "./sessions.js";
16
+ /** How long the ask waits, once the grade answered, for the proxy to book its request: the proxy books a request after its
17
+ * client has the whole answer (when the provider's stream ends, which can come later), so the spend is read once it is
18
+ * booked. One booked later than this reads unknown (null), never $0. */
19
+ export const GRADE_BOOKING_WAIT_MS = 10_000;
16
20
  /** The session the grader's requests are booked under, in the learning run's journal. */
17
21
  export const GRADE_SESSION = "learning.grade";
18
22
  const PROVIDER = "agentrun-grade";
@@ -40,6 +44,8 @@ export function bookedAsk(options) {
40
44
  // The cap is the learning run's: asked with every session of the run summed, never this one's alone.
41
45
  const hostCheck = options.beforeRequest ? runBooking(sessions, options.beforeRequest) : undefined;
42
46
  let refused;
47
+ // Told once the grade's own request is booked (its request_end), never by a cut request booked before it.
48
+ let booked;
43
49
  const booking = await modelPortFor(writer, {
44
50
  key: GRADE_SESSION,
45
51
  label: "grade",
@@ -53,11 +59,14 @@ export function bookedAsk(options) {
53
59
  onEvent: async (event) => {
54
60
  if (event.type === "request_start")
55
61
  await run.flush();
62
+ if (event.type === "request_end")
63
+ booked?.();
56
64
  },
57
65
  });
58
66
  // A grade request an earlier attempt sent and a crash cut is booked first, at its held figure.
59
67
  await booking.bookCut();
60
68
  const before = booking.spentUsd();
69
+ const bookedOnce = new Promise((resolve) => (booked = () => resolve(true)));
61
70
  // The policy of the route's wire, as pi's session takes it (pi.ts policyFor: claude-agent's on Messages, codex's on
62
71
  // Responses, no fork); the grade offers no tool.
63
72
  const proxy = await startModelProxy({
@@ -81,7 +90,12 @@ export function bookedAsk(options) {
81
90
  if (answer.stopReason === "error" || answer.stopReason === "aborted")
82
91
  throw new Error(`the grade's request failed: ${answer.errorMessage ?? answer.stopReason}`);
83
92
  const text = answer.content.filter((part) => part.type === "text").map((part) => part.text ?? "").join("");
84
- const after = booking.spentUsd();
93
+ // The spend once the proxy booked this request: read before, it is the spend without it ($0 for the grade). Not
94
+ // booked within the wait: unknown.
95
+ let timer;
96
+ const inTime = await Promise.race([bookedOnce, new Promise((resolve) => (timer = setTimeout(() => resolve(false), GRADE_BOOKING_WAIT_MS)))]);
97
+ clearTimeout(timer);
98
+ const after = inTime ? booking.spentUsd() : null;
85
99
  const usd = before === null || after === null ? null : after - before;
86
100
  // pi-ai counts cache reads and writes apart from input, as the grade's tokens do.
87
101
  const used = answer.usage;
@@ -1,4 +1,5 @@
1
- import type { LearningFolder } from "../learn/folder.js";
1
+ import { type TimelineEvent } from "../timeline/index.js";
2
+ import { type LearningFolder } from "../learn/folder.js";
2
3
  import { type RunLaunch } from "../run-launch.js";
3
4
  import { type SignInReader } from "../run-view.js";
4
5
  import type { Store } from "../store/store.js";
@@ -23,9 +24,23 @@ export interface LearningEndDeps {
23
24
  /** Every secret the server keeps sealed, read once at the run's end (store-secrets.ts storeSecrets over the store, the
24
25
  * seal key and the data directory), never learned. A throw is a list that can't be read: the run learns nothing. */
25
26
  readonly stored?: () => Promise<readonly string[]>;
26
- /** The grade model the server names (learning.ts gradeModelOf); none, from `env` as the server reads it. */
27
- readonly gradeModel?: string;
28
27
  }
29
28
  /** The end-of-run step for a run launched with `launch`: undefined when its launch records no learning start (a run that
30
29
  * does not learn is asked nothing and keeps nothing), else the reflect hook over the run's store. */
31
30
  export declare function learningEndOfRun(launch: RunLaunch, deps: LearningEndDeps): EndOfRunTurn | undefined;
31
+ /** Whether a run's job, by its timeline `rows` (the end-of-run turn's own, labelled LEARNING_LABEL, left out), gave
32
+ * nothing new to look back on: it called tools, every one of them learned_workflow (a workflow's own nested calls are
33
+ * its), each answering; no tool result failed or was refused; no error, approval or wait; and one message, its first,
34
+ * came in. Anything else is asked its end-of-run turn. */
35
+ export declare function onlyLearnedWorkflows(rows: readonly TimelineEvent[]): boolean;
36
+ /** How many of an agent's latest runs draftForOf reads at most: a skill offered to fewer than DRAFT_AFTER_RUNS of them is
37
+ * no repeated job. */
38
+ export declare const DRAFT_SCAN_RUNS = 200;
39
+ /** The learned skill whose job the end-of-run turn asks for as a workflow draft: of the skills run `runId` was offered
40
+ * (its launch's learning start), the first in the seed's order (the one changed last first) offered to DRAFT_AFTER_RUNS
41
+ * or more runs of agent `agentId` started before it (each run's launch says what it was offered; its latest
42
+ * DRAFT_SCAN_RUNS runs are read newest first, those after `runId` in that order counted, only until every skill has its
43
+ * count), with no learned workflow of its name on the agent's main, active or waiting for
44
+ * its owner's OK (the folder's snapshot). One at most: a turn keeps one workflow draft. None when no skill qualifies, or
45
+ * when the folder can't be read: a read that fails asks for nothing, never a draft that may be there already. */
46
+ export declare function draftForOf(deps: Pick<LearningEndDeps, "store" | "learningFolder">, agentId: string, runId: string, offered: readonly string[]): Promise<string[]>;